【问题标题】:Kendo chart incorrect grouping, ASP.NET MVC剑道图表分组不正确,ASP.NET MVC
【发布时间】:2015-09-05 11:45:26
【问题描述】:

我正在尝试配置剑道图表以显示我的模型的数据:

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 轴仅显示 1 个月“7/2015”,而 8 月的部分数据显示在 7 月类别中。 我想这可能是 json 解析问题,它与日期一起发生,但我只使用月份和年份。 请指教,我做错了什么? 如有任何帮助,我将不胜感激!

【问题讨论】:

    标签: c# asp.net asp.net-mvc kendo-ui kendo-asp.net-mvc


    【解决方案1】:

    我已经调整了一些东西。 我建议不要在您的模型上使用 Period 属性,而是执行以下操作(创建 DateTime 而不是字符串):

    public DateTime Date { get { return new DateTime(Year, Month, 1); } }
    

    这将允许您在网格的 CategoryAxis 上利用 .Date() 构建器,如下所示:

    .CategoryAxis(axis => axis
              .Categories(model => model.Date)
              .Date().BaseUnit(ChartAxisBaseUnit.Months)
              .Labels(lbl => lbl.Format("{0:MM/yyyy}"))
    )
    

    数据的排序似乎也存在问题。我将您的.Sort() 调整为

    .Sort(sort => sort.Add(model => new {model.Date}).Ascending())
    

    但我注意到数据仍然没有正确显示。在您的 CallsByCountry() 操作方法中,在返回之前对数据进行排序。

    完整示例:https://github.com/mmillican/KendoMvcExamples/commit/9ebaa7c4b5c2ddd2a65890cf3d5d77a484d8a3aa

    【讨论】:

    • 谢谢您,您的示例效果很好,但是当我从数据库中检索数据时,我不明白为什么我的图表仍然错误。你能看看drive.google.com/file/d/0B1vvWN9oHO1ATWlFZGdtRDF3UkU/…
    • @GyuzalR 我无法下载文件。您是否正在对检索数据的CallsByCountry() 方法中的数据执行.OrderBy()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多