2015-09-05 27 views
0

我試着去配置劍道圖表,以顯示我的模型的數據:劍道圖表不正確的分組,ASP.NET MVC

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解析問題,這發生在日期,但即時通訊只使用月和年。 請指教,我做錯了什麼? 我會很感激任何幫助!

回答

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

+0

謝謝你,你的榜樣的偉大工程,但我不能讓我的爲什麼圖表仍然是錯誤的,當我從數據庫取回數據。你可以看看https://drive.google.com/file/d/0B1vvWN9oHO1ATWlFZGdtRDF3UkU/view?usp=sharing –

+0

@GyuzalR我無法下載該文件。你是否在''CallsByCountry()'方法中的數據上執行'.OrderBy()'來檢索數據? –

+0

請查看我的問題中的更新。 –