3

My target: I want to draw HighStock-chart with multiple series. Data is loaded by AJAX from data.php. The output of data.php is an JSON-Array

My problem: I don't know hot to grab the data from JSON Array

The output is e.g

[[timestamp,value1,value2],[timestamp,value1,value2]]

Series 1 should be -> timestamp and value1

Series 2 should be -> timestamp and 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

4
  • How your JSON from data.php looks like? Commented Feb 1, 2013 at 9:27
  • Hey! I mentioned that in the beginning "The output is e.g [[timestamp,value1,value2],[timestamp,value1,value2]]" Commented Feb 1, 2013 at 9:33
  • Yes I know, but I would like to see your values, because data should be sorted by x, ascending. Commented Feb 1, 2013 at 9:41
  • timestamp = time() * 1000 || value1 = e.g. 23 || value2 = e.g. 25 || Commented Feb 1, 2013 at 9:49

1 Answer 1

1

Iterate through all items of the data array, and populate two separate arrays:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I have the possibility to change the output of the php-file. Maybe I should create two big arrays ? [[series1data],[series2data]]
And [series1data] is e.g. [[timestamp,value1],[timestamp,value2]]
Yes, the best way would be to create those two arrays in the php files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.