嵌套的json in Highcharts Stacked

时间:2016-10-07 10:37:43

标签: javascript json highcharts

我用google搜索了关于Highcharts stacked percentage column的嵌套json,但dint得到了正确的解决。

这是我尝试过的代码,但是dint得到了正确的输出。

任何人都可以帮我排序我正在做的错误吗?

提前谢谢。

$(function () {
  var processed_json = new Array();
  $.getJSON('javascripts/data.json', function(data) {
    // Populate series
    for (i = 0; i < data.length; i++){
      processed_json.push([data[i].key, data[i].value]);
    }
    // draw chart
    $('#container').highcharts({
      chart: {
        type: "bar"
      },
      title: {
        text: "Student data"
      },
      xAxis: {
        type: 'category',
        allowDecimals: false,
        title: {
          text: ""
        }
      },
      yAxis: {
        title: {
          text: "Scores"
        }
      },
      plotOptions: {
        column: {
          stacking: 'normal'
        }
      },
      series: [{
        name: 'Subjects',
        data: processed_json,
      }]
    });
  });
});

// this is how am displaying in data.json

[
  {"key":"john","value":[34,53,45,45,98]},
  {"key":"Rita","value":[98,34,43,54,66,66]},
  {"key":"james","value":[91,33,45,65,65,38]},
  {"key":"jade","value":[98,54,54,45,45,45]},
  {"key":"lara","value":[23,23,98,23,23,23]} 
]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 400px"></div>

1 个答案:

答案 0 :(得分:0)

您的变量processed_json的格式不是Highcharts API可以处理的。

看看这里:http://jsfiddle.net/jo8foxtz/

以下是代码:

$(function() {

  var processed_json = new Array();
  
  data = [{
    "key": "john",
    "value": [34, 53, 45, 45, 98]
  }, {
    "key": "Rita",
    "value": [98, 34, 43, 54, 66, 66]
  }, {
    "key": "james",
    "value": [91, 33, 45, 65, 65, 38]
  }, {
    "key": "jade",
    "value": [98, 54, 54, 45, 45, 45]
  }, {
    "key": "lara",
    "value": [23, 23, 98, 23, 23, 23]
  }];

  for (i = 0; i < data.length; i++) {
    var item = {};
    item["name"] = data[i].key;
    item["data"] = data[i].value;
    processed_json.push(item);
  }


  $('#container').highcharts({
    chart: {
      type: 'bar'
    },
    title: {
      text: 'Student data'
    },
    xAxis: {
      type: 'category',
      allowDecimals: false,
      title: {
        text: ""
      }
    },
    yAxis: {
      min: 0,
      title: {
        text: 'Scores'
      }
    },
    tooltip: {
      pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f}%)<br/>',
      shared: true
    },
    plotOptions: {
      column: {
        stacking: 'percent'
      }
    },
    series: processed_json
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>