Highcharts data series issue with ajax/json and PHP Highcharts data series issue with ajax/json and PHP php php

Highcharts data series issue with ajax/json and PHP


SOLVED:

I found an answer that finally worked! Frustrating that it took me several days because it wasn't well documented by Highcharts (and/or maybe my understanding of jQuert and JavaScript is still at a novice level).

The answer is do not send the X/Y data as a preformed array in your json object, but instead simply send the numbers that make up the Date (x value) and the Value (y value) as a list of comma separated values in a json object, and then do the parsing and pushing on the client end.

For example: I originally was trying to get my PHP to send something like

[{"name":"Name 1","data":["[Date.UTC(2011,11,08), 4 ]","[Date.UTC(2011,11,09), 4 ]","[Date.UTC(2011,11,10), 4 ]","[Date.UTC(2011,11,11), 4 ]","[Date.UTC(2011,11,14), 3 ]"]}

but what I really needed to do was send something like

[{"name":"Name 1","data":["2011,11,08, 4","2011,11,09,4"....`

The first step is to make sure you have the "series" option set to an empty array series: []

(I mention this because I've seen other answers where this was done differently).

Then in your getJSON function, you need to create a new object for each object you're pulling in, which also includes an empty "data" array (see "var series" below).

Then nest a few $.each loops to parse and push the data into their necessary arrays, and populate the "name" of each object:

    $.getJSON('http://exmaple.com/getdata.php?ID=' + id, function(data) {                   $.each(data, function(key,value) {            var series = { data: []};            $.each(value, function(key,val) {                if (key == 'name') {                    series.name = val;                }                else                {                    $.each(val, function(key,val) {                        var d = val.split(",");                        var x = Date.UTC(d[0],d[1],d[2]);                        series.data.push([x,d[3]]);                    });                }            });            options.series.push(series);        });

After the above code, you need to instantiate the chart:

var chart = new Highcharts.Chart(options);

And that should be it!

Hopefully somebody else finds this answer useful and doesn't need to beat themselves over the head for days trying to figure it out. :-)


Since Javascript UTC method will return with an Integer, it's just better if you just pass the Unix Timestamp as an integer when you're generating the JSON on the serverside instead trying to pass a function (UTC.Date) in the JSON - that makes the entire JSON invalid!

So

Instead of this

while ($item = mysql_fetch_assoc($result)) {                $name = $item['Item1'];    $date = str_replace("-",",",$item['Item2']);    $pos = $item['Item3'];    $arr = array("name"=>$name,"data"=>"[Date.UTC(".$date."), ".$pos." ],");    echo json_encode($arr);         }

Do this:

while ($item = mysql_fetch_assoc($result)) {                $name = $item['Item1'];    $unix_date = date("Y-m-d", strtotime($item['Item2']));    $pos = $item['Item3'];    $arr = array("name"=>$name,"data"=>"[".($unix_date*1000).", ".$pos." ],");    echo json_encode($arr);         }

Please note we're multiplying the PHP unix timestamp by 1000 to make it JS compatible.

Reference: http://www.w3schools.com/jsref/jsref_utc.asp


Just checked with some sample code for Highcharts, series needs to be an array of JSON objects each containing {"name":"Item1","data":"[Date.UTC(2011,11,08), 4 ],"} etc.

The main problem is your output from page2, is not an array.

You'll need to fix your $.each first, you need to push value, not data:

$.each(data, function(key,value) {    options.series.push(value);});

This will properly set each item in series to the full json object.

Then to fix the output:

$output = [];while ($item = mysql_fetch_assoc($result)) {                $name = $item['Item1'];    $date = str_replace("-",",",$item['Item2']);    $pos = $item['Item3'];    //I don't think there's supposed to be a comma after this square bracket    $arr = array("name"=>$name,"data"=>"[Date.UTC(".$date."), ".$pos." ]");    array_push($output, json_encode($arr));     }echo "[";foreach($output as $val){    echo $val;    if($val != end($output)) echo ",";}echo "]";

And that should do it.