使用Json到HighCharts将数据从Controller检索到View

时间:2016-01-19 19:50:08

标签: javascript c# model-view-controller highcharts

我尝试在我的MVC项目中使用HighCharts。我遵循了本教程:http://www.c-sharpcorner.com/UploadFile/1f3f2a/charting-in-mvc/ 它有效,但他们使用PIE CHART,我需要BAR或LINE CHART。

在我的控制器上:

ContasReceberData recData = new ContasReceberData();
List<IndiceInadimplencia> indices = new List<IndiceInadimplencia>();

indices = recData.GetIndices(data1, data2, empresa)
      .Select(o => new IndiceInadimplencia
      {
          MesIndice = o.Mes,
          Indice = Math.Round(o.Inad_Atual / o.Receitas * 100, 2)
      }).ToList();        

return Json(indices, JsonRequestBehavior.AllowGet);
杰森回归: 指数:20 月:“JAN”

观看视频:

$(function () {
        $.ajax({
            url: 'Painel/Indices',
            dataType: "json",
            type: "GET",
            contentType: 'application/json; charset=utf-8',
            async: false,
            processData: false,
            cache: false,
            delay: 15,            
            success: function (data) {

                var series = new Array();                

                for (var i in data) {  
                    var serie = new Array(data[i].MesIndice, data[i].Indice);                   
                    series.push(serie);                                    
                }

                indicesInadimplencia(series);

            },

            error: function (xhr) {
                alert('error');
            }
        });      
    });

该功能将返回:[“JAN”,20],[“FEV”,25] ......

和HighCharts需要: 分类:[“JAN”,“FEV”,“MAR”] 意甲:[20,25,22]

如何将[“JAN”,20]更改为:

数组月= [“JAN”,“FEV”] array indice = [20,25]

2 个答案:

答案 0 :(得分:0)

循环遍历data并填充两个单独的数组一个Categories和一个Series

var Categories = new Array();
var Series = new Array();
for (var i=0; i < data.Length; i++) 
{                    
     Categories.push(data[i].MesIndice);    
     Series.push(data[i].Indice);                                
}

答案 1 :(得分:0)

工作!

我试过了:

var Categories = new Array();
var Series = new Array();

for (var i in data) { 

Categories.push(data[i].MesIndice);
Series.push(data[i].Indice);

}    

谢谢!!! Ehsan Sajjad