I am trying to pass the values at different time-stamps by quering from MongoDb, then passing them to client in form of JSON.
Then client will read the JSON object returned by php script, and on the basis of that will populate the flot chart.
I am not being able to do this, and have the following two doubts/questions:
A. In the JSON object that is being returned by php, what should be the way of constructing JSON object ?
At present, from the following code, the JSON is being produced like:
[{"1371859200000":65},{"1371860100000":63},{"1371861000000":48},{"1371861900000":47},{"1371862800000":41},{"1371863700000":19},{"1371864600000":35}]
Is the above right ? OR should the data be something like : [["1371859200000":65],["1371860100000":63], ....]
What is the difference between the two of them ? (In the above '[' is used instead of curly braces)
B. In PHP, strtotime returns int. BUT, when i echo my JSON object, the timestamp is produced like "1371859200000" (i.e. a string) and not 1371859200000 (i.e. an int). Why so ? I have to populate the flot chart, and it is necessary for the timestamp to be present as int (and not as string) in order to populate the flot chart.
Following is my PHP code (not the whole code):
$date = 2;
$tsc = 15;
$result = array();
for ($hour = 0; $hour < 24; $hour++) {
for ($min_lower = 0; $min_lower <= 60-$tsc; $min_lower += $tsc) {
// Query MongoDB.
$query = array( "date" => $date, "hour" => $hour, "minutes" => array( '$gte' => $min_lower, "\$lt" => $min_lower+$tsc ) );
$count = $coll->find($query)->count();
$time = strtotime("2013-06-".strval($date)." ".strval($hour).":".strval($min_lower).":".strval(0)." UTC") * 1000;
$data = array($time => $count);
// Push this smaller array into the main array. Is this the correct way ?
array_push($result, $data);
}
}
echo json_encode($result);
And following is my javascript/jquery code:
var options = {
lines: {
show: true
},
points: {
show: true
},
xaxis: {
mode : "time",
timeformat: "%m/%d/%y"
}
};
$("button.fetch").click(function () {
var button = $(this);
var dataurl = "MY URL";// Here the URL comes.
function onDataReceived(series) {
data.push(series);
$.plot("#placeholder", series, options);
}
$.ajax({
url: dataurl,
type: "GET",
dataType: "json",
success: onDataReceived
});
})