简体   繁体   English

Highstock - 如何使用JSON-Array添加多个系列

[英]Highstock - How to add multiple series with JSON-Array

My target: I want to draw HighStock-chart with multiple series. 我的目标:我想绘制多个系列的HighStock图表。 Data is loaded by AJAX from data.php. 数据由AJAX从data.php加载。 The output of data.php is an JSON-Array data.php的输出是一个JSON-Array

My problem: I don't know hot to grab the data from JSON Array 我的问题:我不知道从JSON Array中获取数据的热点

The output is eg 输出是例如

[[timestamp,value1,value2],[timestamp,value1,value2]] [[时间戳,值1,值],[时间戳,值1,值]]

Series 1 should be -> timestamp and value1 系列1应该是 - > timestamp和value1

Series 2 should be -> timestamp and value2 系列2应该是 - > timestamp和value2

This is my code 这是我的代码

// Draw the chart
$(function(){

        /* The chart is drawn by getting the specific data from "data.php".
         * The configuration settings are transmitted by GET-Method. The output is an JSON-Array.  */

        $.getJSON('data.php',
        function(data) {


        chart = new Highcharts.StockChart
        ({
        chart:  {  renderTo: 'chartcontainer', type: 'line'  },
        title:  { text: 'You see the data of the last hour!' },
        xAxis: {  type: 'datetime', title: { text: 'time'  } },
        yAxis: { title: { text: 'unit'  } },
        series: [{ name: 'series1', data: data },{ name: 'series2', data: data }],

        });
    });
});

I think i have to change 我想我必须改变

series: [{ name: 'series1', data: data },{ name: 'series2', data: data }],

But I dont know in to what 但我不知道到底是什么

Iterate through all items of the data array, and populate two separate arrays: 迭代data数组的所有项,并填充两个单独的数组:

var series1 = [];
var series2 = [];
for (var i = 0; i < data.length; i++) {
  series1.push([data[0], data[1]);
  series1.push([data[0], data[2]);
}

You then have the timestamp-value pairs in each of the series arrays. 然后,您在每个系列数组中都有时间戳值对。

Doing this in php might be more efficient, especially if you can replace the current json creation. 在php中执行此操作可能会更有效,尤其是如果您可以替换当前的json创建。

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

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