数组在JavaScript中不正确的格式用于HighChart
-
25-10-2019 - |
题
嗨,我将列表对象从控制器传递到JavaScript,并将其放回数组中,并用作HighChart Bar Graph的数据源。
在下面的代码中,如果我在原始数据中发表评论。如果我使用称为“结果”的数组变量,我什么也没得到
如果我使用调试器和输出结果进入JavaScript,则可以立即窗口,它看起来像:
?结果[0] {...} [0]:“ 2” [1]:{...}
?结果[0] [1] {...} [0]:0 [1]:0 [2]:0 [3]:0 [4]:0 [5]:0 [6]:1 [7 ]:0 [8]:0 [9]:0 [10]:0 [11]:0
这是代码:
function CreateChart3(surl) {
// Code block for fetching Array as jsonResult (js)
var url = $("#AbsolutePath").val() + "Complaint.mvc/GetChartData_MonthlyByType/" + surl;
var chart;
$.getJSON(url, null, function(data) {
var result = [];
jQuery.each(data, function(i, val) {
result[i] = [val.Type, val.Count];
});
// var seriesData = [];
// for (var i = 0; i < result.length; i++) {
// seriesData.push({ data: result[i][1] ,name: result[i][0]});
// }
debugger;
chart = new Highcharts.Chart({
chart: {
renderTo: 'chart3',
defaultSeriesType: 'column',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Stacked Bar Monthly By Type'
},
yAxis: {
min: 0,
title: {
text: 'Total No Of Complaints'
},
xAxis: {
categories: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'],
title: 'Month'
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',
color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
}
}
},
legend: {
backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
formatter: function() {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
}
}
},
// series:
// [{
// name: "2",
// data: [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
// }, {
// name: "3",
// data: [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 0]
// }]
series: [{
data: result
}]
});
});
};
从控制器代码中提取的话,如果有帮助……
var qry = from i in _db.Complaints
where i.Site.SiteDescription.Contains(searchTextSite)
&& (i.Raised >= startDate && i.Raised <= endDate)
group i by i.ComplaintNatureTypeId.ToString()
into grp
select new
{
Type = grp.Key,
Count = new int[] {
grp.Where(c => c.Raised.Month == 1).Count(),
grp.Where(c => c.Raised.Month == 2).Count(),
grp.Where(c => c.Raised.Month == 3).Count(),
grp.Where(c => c.Raised.Month == 4).Count(),
grp.Where(c => c.Raised.Month == 5).Count(),
grp.Where(c => c.Raised.Month == 6).Count(),
grp.Where(c => c.Raised.Month == 7).Count(),
grp.Where(c => c.Raised.Month == 8).Count(),
grp.Where(c => c.Raised.Month == 9).Count() ,
grp.Where(c => c.Raised.Month == 10).Count() ,
grp.Where(c => c.Raised.Month == 11).Count(),
grp.Where(c => c.Raised.Month == 12).Count() }
};
return Json(qry.ToArray(), JsonRequestBehavior.AllowGet);
解决方案
我记得 data
财产的 series
是
- Y值
- 一对xy值
- 点对象
据我了解,您的数组的每个元素都是数组,其中元素[0]为 name
元素[1]是y值的数组。因此,您有一些系列。您必须手动转换您将 { name: ..., data: ...}
对象并将其传递给Highcharts构造函数。利用 for
环形:
var seriesData = [];
for (var i = 0; i < result.length; i++){
seriesData.push({ name: result[i][0], data: result[i][1] });
}
并使用类似的系列数据:
series: seriesData
其他提示
查看调试器输出JSON并不是您所期望的。
很难确定,但是您是否从控制器中返回正确的对象?看起来您正在回来 qry
当您应该使用的时候 grp
.
不隶属于 StackOverflow