Reputation: 282
I'm trying to format and load multiple series data via ajax with PHP as per this example:
The following PHP code,
<?php
$connection = pg_connect("host=localhost port=5432 dbname=pccs user=michael password=huskies1975") or die(" " . pg_last_error($connection));
$chart1 = pg_query($connection, "SELECT sample_date, a FROM monitor_nutrient WHERE station_num_id = 201 ORDER BY sample_date ASC LIMIT 5");
$row_a = array();
$row_a['name'] = 'Temperature';
while ($ra = pg_fetch_array($chart1)) {
$date = str_replace("-",",",$ra['sample_date']);
$row_a['data'][] = array($date, $ra['a']);
}
$chart1 = pg_query($connection, "SELECT sample_date, b FROM monitor_nutrient WHERE station_num_id = 201 ORDER BY sample_date ASC LIMIT 5");
$row_b = array();
$row_b['name'] = 'Salinity';
while ($rb = pg_fetch_array($chart1)) {
$date = str_replace("-",",",$rb['sample_date']);
$row_b['data'][] = array($date, $rb['b']);
}
$chart1 = pg_query($connection, "SELECT sample_date, c FROM monitor_nutrient WHERE station_num_id = 201 ORDER BY sample_date ASC LIMIT 5");
$row_c = array();
$row_c['name'] = 'Dissolved Oxygen';
while ($rc = pg_fetch_array($chart1)) {
$date = str_replace("-",",",$rc['sample_date']);
$row_c['data'][] = array($date, $rc['c']);
}
$result = array();
array_push($result, $row_a);
array_push($result, $row_b);
array_push($result, $row_c);
echo(json_encode($result, JSON_NUMERIC_CHECK));
//print json_encode($result, JSON_NUMERIC_CHECK);
pg_close($connection);
?>
produces this valid(JsonLint) ouput:
[
{
"name": "Temperature",
"data": [
[
"2012,06,12",
20.38
],
[
"2012,06,21",
24.62
],
[
"2012,07,03",
25.96
],
[
"2012,07,20",
24.92
],
[
"2012,08,03",
25.56
]
]
},
{
"name": "Salinity",
"data": [
[
"2012,06,12",
31.49
],
[
"2012,06,21",
31.47
],
[
"2012,07,03",
31.11
],
[
"2012,07,20",
30.75
],
[
"2012,08,03",
30.94
]
]
},
{
"name": "Dissolved Oxygen",
"data": [
[
"2012,06,12",
5.53
],
[
"2012,06,21",
7.07
],
[
"2012,07,03",
5.3
],
[
"2012,07,20",
3.49
],
[
"2012,08,03",
6.67
]
]
}
]
First, is this the right format for a Highcharts series, second, how and where do I convert the date to Date.UTC() like the example, JavaScript/PHP? and lastly is the following code even close to what I want.
function chartParser(data) {
$.each(data, function (key, value) {
var series = {name: key, data: []};
$.each(value, function (key, val) {
if (key == 'name') {
series.name = val;
}
else {
$.each(val, function (key, val) {
options.series.push([val[0], val[1]]);
});
}
});
var chart1 = new Highcharts.Chart(options);
});
}
Any help and examples would be greatly appreciated. I've been torturing myself with this for a couple of weeks.
Upvotes: 0
Views: 2918
Reputation: 45079
You can achieve that on client side, this way:
$.each(val, function (key, val) {
options.series.push([new Date(val[0]).getTime(), val[1]]);
});
Upvotes: 3