如何在Highcharts中绘制JSON中的数据值

时间:2013-11-13 09:24:16

标签: jquery json highcharts

我写了一个jquery:

$(function () {
var data = [
{"series_name":"Actual","period_name":"Q1 / 2013","period_final_value":"17"},
{"series_name":"Actual","period_name":"Q2 / 2013","period_final_value":"15"},
{"series_name":"Actual","period_name":"Q3 / 2013","period_final_value":"13"},

{"series_name":"Alarm","period_name":"Q1 / 2013","period_final_value":"14.103"},
{"series_name":"Alarm","period_name":"Q2 / 2013","period_final_value":"14.404499999999999"},
{"series_name":"Alarm","period_name":"Q3 / 2013","period_final_value":"14.966999999999999"},

{"series_name":"Target","period_name":"Q1 / 2013","period_final_value":"15.67"},
{"series_name":"Target","period_name":"Q2 / 2013","period_final_value":"16.005"},
{"series_name":"Target","period_name":"Q3 / 2013","period_final_value":"16.63"}
];
var seriesData = [];
var xCategories = [];
var i, cat;
for(i = 0; i < data.length; i++){
     cat = '' + data[i].period_name;
     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].series_name;});
      if(currSeries.length === 0){
          seriesData[seriesData.length] = currSeries = {name: data[i].series_name, data: []};
      } else {
          currSeries = currSeries[0];
      }
      var index = currSeries.data.length;
      currSeries.data[index] = data[i].period_final_value;
    } else {
       seriesData[0] = {name: data[i].series_name, data: [data[i].period_final_value]}
    }
}
var chart;
$(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'column'
        },
        title: {
            text: 'Stacked column chart'
        },
        xAxis: {
            categories: xCategories
        },
        yAxis: {
            min: 0,
            max: 30,
            title: {
                text: 'Total fruit consumption'
            },
            stackLabels: {
                enabled: false,
                style: {
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                }
            }
        },
        legend: {
            align: 'right',
            x: -100,
            verticalAlign: 'top',
            y: 20,
            floating: true,
            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;
            }
        },

        series: seriesData
    });
});

});

上面的代码也包含JSON数据格式。 但价值没有出现。 代码绘制X轴,Q1 Q2和Q3以及Y轴,系列也显示在图例中 但代码在屏幕上没有任何列。 任何帮助赞赏。 谢谢。

1 个答案:

答案 0 :(得分:0)

您需要使用数字而不是字符串,因此每个y值都应该由parseFloat()解析以获得正确的值。

你的seriesData [0]在输出上的样子如何?