i use some code that works fine for producing a google line chart with a discreet x axis from data in a mysql table. But when i change the column type to "date" to make the x axis continous i just keep getting an error:
a.getTime is not a function
here is the code for producing the array from mysql.
<?php
include("connect.php");
$qry = " mysql query here";
$result = mysqli_query($con,$qry);
mysqli_close($con);
$table = array();
$table['cols'] = array(
array('id' => '', 'label' => 'Date', 'type' => 'date'),
array('id' => '', 'label' => 'Amount ', 'type' => 'number'),
);
$rows = array();
foreach($result as $row){
$temp = array();
$date1 = date_create_from_format('Y-m-d H:i:s', $row['Date']);
$date2 = date_format($date1, 'd-m-Y');
$temp[] = array('v' => (string) $date2);
$temp[] = array('v' => (float) $row['Amount']);
$rows[] = array('c' => $temp);
}
$result->free();
$table['rows'] = $rows;
$jsonTable = json_encode($table, true);
echo $jsonTable;
?>
Ive tried various things like date format, etc. but still get the same error if the cloumn 1 type is "date". Any help would be appreciated.
Edit: Still having trouble with this. I cant get my head around it. The following is the array output im getting at the minute. Hope this helps point where im going wrong.
Edit 2: I now have the array coming out as follows but still have the same a.getTime error.
{"cols":[
{"label":"Reading Date","type":"date"},
{"label":"Cl Reading(mg\/l) ","type":"number"},
"rows":[
{"c":[{"v":"new Date(04\/10\/2015)"},{"v":0.4}]},
{"c":[{"v":"new Date(04\/11\/2015)"},{"v":0.45}]},
{"c":[{"v":"new Date(04\/12\/2015)"},{"v":0.9}]},
{"c":[{"v":"new Date(04\/01\/2016)"},{"v":0.5}]},
{"c":[{"v":"new Date(04\/02\/2016)"},{"v":0.43}]},
{"c":[{"v":"new Date(18\/02\/2016)"},{"v":0.6}]}]}
Thanks for all the help WhiteHat, here is the working solution:
The error is thrown because the values in the first column need to be actual
date
values.Try replacing...
{"c":[{"v":"04-10-2015"},{"v":0.4}]}
With...
{"c":[{"v":new Date("10/04/2015")},{"v":0.4}]}
Using...