简体   繁体   English

如何将json数据传递给highcharts系列?

[英]How to pass json data to highcharts series?

I have following json array which is generated at runtime. 我有跟随在运行时生成的json数组。 Hence the number of name/data pairs varies. 因此,名称/数据对的数量不同。

`var sales = { "SalesData" : [ 
{ "name"  : "AllProducts|Canada", "data" :[44936.0,50752.0] },
{ "name"  : "AllProducts|Mexico", "data" : [200679.0,226838.0] },
{ "name"  : "AllProducts|USA",    "data" : [288993.0,289126.0] }
                    ]}    `

I want to pass this data to series in highcharts. 我想将这些数据传递给highcharts中的系列。

This is how I am doing it currently. 这就是我目前的做法。

series: [     
        {name:sales.SalesData[0].name,data:sales.SalesData[0].data},
        {name:sales.SalesData[1].name,data:sales.SalesData[1].data},
        {name:sales.SalesData[2].name,data:sales.SalesData[2].data}

            ]

But if the number of elements in array are changed then this won't work. 但是如果数组中的元素数量发生了变化,那么这将不起作用。 How do I solve this problem ? 我该如何解决这个问题? Demo code will help me. 演示代码将帮助我。

I have refereed following questions but I was not able to solve the problem. 我已经审问了以下问题,但我无法解决问题。

Dynamically adding to Highcharts 动态添加到Highcharts

Highcharts series data array Highcharts系列数据阵列

Instead of constructing the series array manually you could loop through the sales variable data and construct the array. 您可以循环遍历sales变量数据并构造数组,而不是手动构建series数组。 So what ever the number of elements in the sales.SalesData array, all items will be there in the series array 那么sales.SalesData数组中的元素数量,所有项目都将在series数组中

var series = [],
    salesData= sales.SalesData;

for (var i=0 i< salesData.length; i++) {
    series.push({"name" : key, "data" : sales[key]})
}

This constructed series array is part of the object which you must pass as argument to highcharts method. 这个构造的series数组是对象的一部分,您必须将其作为参数传递给highcharts方法。

var chartdata = {
    chart: {type: 'column'},
    title: {text: 'Sales Data'},
    xAxis: {
        categories: ['Category 1','Category 2']
    },
    yAxis: {
        min: 0,
        title: {text: 'Sales'}
    },
    series : []
}

chartdata.series = series;

$('#chart').highcharts(chartdata);

where #chart is the container where you want to display the chart. 其中#chart是您要显示图表的容器。

you can also refer to the fiddles which are available in their demo pages for each type of chart to know more on how to display a particular type of chart. 您还可以参考每种类型图表的演示页面中提供的小提琴,以了解有关如何显示特定类型图表的更多信息。

I solved the problem 我解决了这个问题

Changed json array as follows: 更改了json数组如下:

var sales = [ 
              { "name"  : "AllProducts123|Canada", "data" :[44936.0,50752.0] },
              { "name"  : "AllProducts|Mexico", "data" : [200679.0,226838.0] },
              { "name"  : "AllProducts|USA",    "data" : [288993.0,289126.0] }
            ]

Now pass it directly to series in highcharts. 现在将它直接传递给highcharts中的系列。

 series:sales

Done !!!!! 完成!!!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM