如何在HighCharts中格式化堆栈列表的json数据

时间:2012-07-13 14:53:07

标签: javascript json highcharts

这是我的json数据

var data = [
    {"unit":"a", "status":"Stopped / Idle", "val":21.2022222222222222},
    {"unit":"a", "status":"Working", "val":53.3066666666666667},
    {"unit":"a", "status":"Headland Turning", "val":0.04694444444444444444},
    {"unit":"a", "status":"Transport", "val":5.1425000000000000},
    {"unit":"b", "status":"Stopped / Idle", "val":334.7358333333333333},
    {"unit":"b", "status":"Working", "val":212.6386111111111111},
    {"unit":"b", "status":"Headland Turning", "val":26.2955555555555556},
    {"unit":"b", "status":"Transport", "val":0.00444444444444444444}
];

unit是类别

我希望数据可以按以下方式格式化,以便我可以插入HighCharts中的series选项:

series: [{
                name: 'Stopped / Idle',
                data: [21.2022222222222222, 334.7358333333333333]},
            {
                name: 'Working',
                data: [53.3066666666666667, 212.6386111111111111]},
            {
                name: 'Headland Turning',
                data: [0.04694444444444444444, 26.2955555555555556]},
            {
                name: 'Transport',
                data: [5.1425000000000000, 0.00444444444444444444]}]
        });

谢谢。

2 个答案:

答案 0 :(得分:4)

var data = [
    {"unit":"a", "status":"Stopped / Idle", "val":21.2022222222222222},
    {"unit":"a", "status":"Working", "val":53.3066666666666667},
    {"unit":"a", "status":"Headland Turning", "val":0.04694444444444444444},
    {"unit":"a", "status":"Transport", "val":5.1425000000000000},
    {"unit":"b", "status":"Stopped / Idle", "val":334.7358333333333333},
    {"unit":"b", "status":"Working", "val":212.6386111111111111},
    {"unit":"b", "status":"Headland Turning", "val":26.2955555555555556},
    {"unit":"b", "status":"Transport", "val":0.00444444444444444444}
];
var seriesData = [];
var xCategories = [];
var i, cat;
for(i = 0; i < data.length; i++){
     cat = 'Unit ' + data[i].unit;
     if(xCategories.indexOf(cat) === -1){
        xCategories[xCategories.length] = cat;
     }
}
for(i = 0; i < data.length; i++){
    if(seriesData){
      var currSeries = seriesData.filter(function(seriesObject){ return seriesObject.name == data[i].status;});
      if(currSeries.length === 0){
          currSeries = seriesData[seriesData.length] = {name: data[i].status, data: []};
      } else {
          currSeries = currSeries[0];
      }
      var index = currSeries.data.length;
      currSeries.data[index] = data[i].val;
    } else {
       seriesData[0] = {name: data[i].status, data: [data[i].val]}
    }
}

现在您已seriesDataxCategories,您可以使用类似于

的内容来实例化图表
  chart = new Highchart({chart: {renderTo: 'chart-div', 
                                   type: 'column'
                                  }, 
                           plotOptions: {column: {stacking: 'normal'}},
                           xAxis:{ categories: xCategories}, 
                           series: seriesData
                         });

编辑:这是jsFiddle:http://jsfiddle.net/sujay/UMeGS/

答案 1 :(得分:1)

在HighCharts网站上,我导航到演示库&gt; HighChart演示部分,然后单击页面左侧的Stacked列。从那里他们有一个JSFiddle with a demo的链接,从那里我看到了两段与你相关的代码。

第一部分是HighChart对象的xAxis属性。以下是我将其更改为:

xAxis: {
  categories: ['Unit A', 'Unit B']
}

这些将是您正在堆叠数据的每个列。

下一部分是series属性。您可以在此处将要查看的数据传递到这些列中。它看起来像这样:

series: [{
  name: 'Stopped / Idle',
  data: [21.2022222222222222, 334.7358333333333333]
}, {
  name: 'Working',
  data: [53.3066666666666667, 212.6386111111111111]
}, {
  name: 'Headland Turning',
  data: [0.04694444444444444444, 26.2955555555555556]
}, {
  name: 'Transport',
  data: [5.1425000000000000, 0.00444444444444444444]                         
}]

在此部分中,您可以指定特定数据类型的名称,然后指定您在xAxis属性中指定的每列的值。

我非常快速地使用了一些选项,而且你可以做更多的事情,但here's the JSFiddle有一个堆积的柱状图和你的数据。

希望这有帮助!