I've created file api.php
<?php
// Prevent caching.
header('Cache-Control: no-cache, must-revalidate');
// The JSON standard MIME header.
header('Content-type: application/json');
//mssql_select_db("**", mssql_connect(**));
$conn=mssql_connect(***);
$query = "SELECT ROUND([AirTemp_C],1) as [T]
,[DT]
FROM [ASUTP].[dbo].[Temperature_ER]
WHERE [Place] = 'ER06' AND [DT] > '26.05.2015 11:00'";
$qwr_res = mssql_query($query);
while ($row=mssql_fetch_array($qwr_res))
{
$temps[] = array (
'x' => $row['DT'],
'y' => $row['T']
);
}
echo json_encode($temps);
?>
It returns JSON like [{"x":"2015-05-26 11:02:04","y":26.3}] with my measurments.
I want to use this data to draw graph using canvasjs-1.6.2. Here is my code: window.onload = function () {
$.getJSON("api.php",function(data1)
{
var chart = new CanvasJS.Chart("chartContainer",
{
title:{
text: "Title",
fontSize: 30
},
animationEnabled: true,
zoomEnabled:true,
height: 500,
axisX:{
gridColor: "Silver",
tickColor: "silver",
labelAngle: -80,
valueFormatString: "DD.MM HH:mm"
},
toolTip:{
shared:true
},
theme: "theme2",
axisY: {
gridColor: "Silver",
tickColor: "silver"
},
legend:{
verticalAlign: "center",
horizontalAlign: "right"
},
data: [
{
type: "line",
showInLegend: true,
lineThickness: 2,
name: "T",
//markerType: "square",
color: "#F08080",
dataPoints: data1
}
],
legend:{
cursor:"pointer",
itemclick:function(e){
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else{
e.dataSeries.visible = true;
}
chart.render();
}
}
});
chart.render();
});
But it seems that this chart (or JS at all) cant use my Date value as a correct DateTime so chart can't render.
I've fixed this issue with such loop:
for(var i=0;i<data1.length;i++)
{
data1[i].x = new Date(data1[i].x);
}
I should notice that I'm using PHP 5.2 and I'm not allowed to upgrade it. So my questions are:
- Is my way to get data for chart via api.php correct?
- Is there a way to pass correct DateTime values from php to JS without that loop convert?