我试图配置kendo图表来显示我的模型的数据:
public class CallByCountry
{
public DateTime Date { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public DateTime Period { get { return new DateTime(Year, Month, 1); } }
public string Country { get; set; }
public int CallsCount { get; set; }
}
示例数据:
Month Year Country CallsCount
7 2015 USA 5
8 2015 USA 3
8 2015 UK 9
...
我的图表:
@(Html.Kendo().Chart<CallByCountry>()
.Name("CallByCountry")
.ChartArea(chartArea => chartArea
.Background("transparent")
)
.DataSource(dataSource => dataSource
.Read(read => read.Action("CallsByCountry", "Reports"))
.Group(group => { group.Add(model => model.Country); })
.Sort(sort => sort.Add(model => new { model.Period}).Ascending())
)
.Series(series =>
{
series.Line(model => model.CallsCount)
.Name("#= group.value #").Style(ChartLineStyle.Smooth);
})
.Legend(legend => legend
.Position(ChartLegendPosition.Bottom)
)
.ValueAxis(axis => axis.Numeric().Labels(l => l.Format("{0:n0}")).MajorUnit(1))
.CategoryAxis(axis => axis
.Categories(model => model.Period)
.Date().BaseUnit(ChartAxisBaseUnit.Months)
.Labels(lbl => lbl.Format("{0:MM/yyyy}"))
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Template("#= series.name #: #= value #"))
)
控制器:
public ActionResult CallsByCountry()
{
List<CallByCountry> callsByCountry = new List<CallByCountry>();
foreach (var call in _callsRepo.GetAll().ToList())
{
var callByCountry = new CallByCountry();
callByCountry.Date = call.StartDate.Date;
callByCountry.Country = _contactRepo.Find(call.ContactID).Country;
callsByCountry.Add(callByCountry);
}
IEnumerable<CallByCountry> data = callsByCountry.GroupBy(i => new { i.Date.Month, i.Date.Year, i.Country })
.Select(group => new CallByCountry()
{
Country = group.Key.Country,
Month = group.Key.Month,
Year = group.Key.Year,
CallsCount = group.Count()
}).OrderBy(x => x.Period);
return Json(data, JsonRequestBehavior.AllowGet);
}
但是,我的数据表示不正确。 X轴类别仅显示一个月“7/2015”,8月的部分数据显示在7月类别中。 我想这可能是json解析问题,它发生在日期,但我只使用月和年。 请指教,我做错了什么? 我很感激任何帮助!