热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Ihavefollowingjsonarraywhichisgeneratedatruntime.Hencethenumberofnamedatapairsvarie

I have following json array which is generated at runtime. Hence the number of name/data pairs varies.

我有跟随在运行时生成的json数组。因此,名称/数据对的数量变化。

`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系列数据阵列

2 个解决方案

#1


4  

Instead of constructing the series array manually you could loop through the sales variable data and construct the array. So what ever the number of elements in the sales.SalesData array, all items will be there in the series array

您可以循环遍历销售变量数据并构造数组,而不是手动构建系列数组。那么sales.SalesData数组中的元素数量,所有项目都将在系列数组中

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

for (var i=0 i

This constructed series array is part of the object which you must pass as argument to highcharts method.

这个构造的系列数组是对象的一部分,您必须将其作为参数传递给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.

您还可以参考每种类型图表的演示页面中提供的小提琴,以了解有关如何显示特定类型图表的更多信息。

#2


3  

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 !!!!!

完成!!!!!


推荐阅读
author-avatar
金燁欣_973
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有