I’m using Flot Graph Api to display bar chart, line chart in my clien side php. I try to pass Json to plot graph as they have mentioned in their examples.
I pack the Json like this,
[{"label":"63f9311f-5d33-66ad-dcc1-353054485572","bars":{"show":true},"data": [[-170.44530493708,270.44530493708]]},{"label":"8f6dcf4a-b20c-8059-1410-353054486037","bars":{"show":true},"data":[[-791.50048402711,891.50048402711]]},{"label":"c2b4cf75-3e0b-f9ff-722e-353054485803","bars":{"show":true},"data":[[-1280.0484027106,1380.0484027106]]},{"label":"eb963e23-75cd-6131-867a-353054485894","bars":{"show":true},"data":[[-1487.2604065828,1587.2604065828]]},{"label":"ef413106-d5be-593b-1cda-353054485719","bars":{"show":true},"data":[[-1940.9583736689,2040.9583736689]]}]
But the graph is not ploting.
$.ajax({
url: '../c/By_Machine_Controller.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var Up = [];
var Down = [];
Up = data[0]; //get Up
Down = data[1]; //get Down
//alert(Up);
$.plot($("#placeholder"), [ Up , Down ]);
}
});
Is it anything to do with Json or My JS is wrong. Since, i am new to flot i don’t have any clue. Can anyone help me out.
The Answer i have entered works fine, but in this case, i’m writing the JSON into a temp file and accessing it, but my requirement is,
I calculate the values for the flot in MODEL Folder in my localhost and passing it as associative array and encoding it as a JSON in CONTROLLER Folder. Now i need to access the JSON in VIEW Folder in my localhost.
CONTROLLER CODE:
$json = json_encode($allData);
$myFile = "ReasonByTime.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $json);
fclose($fh);
But when i access the $json of the CONTROLLER in the VIEW through JS like the following, its not working. I googled it but i was not able to fix it. Can you help me out with this?
VIEW JS CODE TO ACCESS JSON From CONTROLLER.
$(document).ready(function () {
//var dataurl = '../c/By_Machine_Controller.php';
function onDataReceived(data) {
$.plot(placeholder, data, options);
}
$.ajax({
type: "GET",
url: '../c/By_Machine_Controller.php',
data: 'data',
dataType: 'json',
success: function(data) {
alert(data);
}
});
});
This works for me.
});