我正在尝试使用 json 数据制作一个类似于此演示中的 highcharts 图表:http ://www.highcharts.com/demo/column-basic ,但我看不出如何在明智的情况下解析我的 json 数据. 我的数据按工作日分组,分为三类。下面是一个例子:
{ "Monday": { "inv": 1126, "oppm": 276, "perc": "24.51" }, "Tuesday": { "inv": 1072, "oppm": 273, "perc": "25.47" }, "Wednesday": { "inv": 1032, "oppm": 271, "perc": "26.26" }, "Thursday": { "inv": 989, "oppm": 259, "perc": "26.19" }, "Friday": { "inv": 937, "oppm": 240, "perc": "25.61" } }
有没有办法在一个简单的问题上做到这一点?如果我改变我的 json 数据结构会有帮助吗?
提前致谢!
编辑:解决方案:
我找到了一种不太复杂的方法,尽管我相信如果你知道你的 JS,它可以更容易地完成。无论如何,此解决方案与我的 JSON 数据配合得很好:
//Convert received AJAX data
success: function(data)
{
cleanData = $.parseJSON(data);
//Make some empty arrays for the categories and data
days = Array();
inv = Array();
oppm = Array();
perc = Array();
//Iterate the data and fill in the new arrays
$.each(cleanData, function(index, value) {
days.push(index);
inv.push(parseFloat(value['inv']));
oppm.push(parseFloat(value['oppm']));
perc.push(parseFloat(value['perc']));
});
//Use the new arrays in the chart
chart1 = new Highcharts.Chart({
chart: {
renderTo: 'div_chart',
type: 'column'
},
xAxis:{
categories: days,
title: {
text: 'Week day'
}
},
series: [{
name: 'Invited',
data: inv
}, {
name: 'Met',
data: oppm
}, {
name: '%',
data: perc
}],
});