我想计算一个月内新用户的数量。然而,这是我收到的输出。我如何将重复的几个月分组到" Jan"," Nov"。非常感谢你的帮助。
public ActionResult UserPerMonth()
{
var _con = new DBEntities();
ArrayList xValue = new ArrayList();
ArrayList yValue = new ArrayList();
var results = (from c in _con.Users select c);
results.ToList().ForEach(rs => yValue.Add(rs.id.ToString().Count()));
results.ToList().ForEach(rs => xValue.Add(rs.date.Value.ToString("MMM-yyyy")));
var chart = new Chart(width: 300, height: 200)
.AddTitle("Users per month")
.AddLegend()
.AddSeries(
chartType: "Column",
xValue: xValue,
yValues: yValue)
.GetBytes("png");
return File(chart, "image/png");
}
答案 0 :(得分:1)
可能有更简洁的方法,但这对我有用。为图表数据添加一个类,例如
public class ChartData
{
public string Month { get; set; }
public int Count { get; set; }
}
然后你可以使用带有groupby和count的linq查询来获取数据并将其放入ChartData类型,然后将这些值移动到相关的轴:
public ActionResult UserPerMonth()
{
var _con = new DBEntities();
ArrayList xValue = new ArrayList();
ArrayList yValue = new ArrayList();
var results = (from c in _con.Users select c);
var axis = results.GroupBy(r => r.date.Value.ToString("MMM-yyyy"))
.Select(r => new ChartData
{
Month = r.Key,
Count = r.Count()
}).ToList();
foreach (var item in axis)
{
xValue.Add(item.Month);
yValue.Add(item.Count);
}
var chart = new Chart(width: 300, height: 200)
.AddTitle("Users per month")
.AddLegend()
.AddSeries(
chartType: "Column",
xValue: xValue,
yValues: yValue)
.GetBytes("png");
return File(chart, "image/png");
}