I'm currently utilizing the Flot Graph Api to showcase bar charts and line charts on my client-side PHP environment. I am attempting to pass Json data to plot the graph as outlined in their examples.
This is how I structure the Json data:
[{"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]]}]
Despite formatting the data correctly, the graph does not show up.
$.ajax({
url: '../c/By_Machine_Controller.php',
data: "",
dataType: 'json',
success: function(data)
{
var Up = [];
var Down = [];
Up = data[0];
Down = data[1];
$.plot($("#placeholder"), [ Up , Down ]);
}
});
I am unsure if the issue lies with the JSON or if there's an error in my JavaScript code. As a newcomer to Flot, I'm seeking help to troubleshoot this problem.
The solution I've implemented involves writing the JSON data to a temporary file and then accessing it. However, my goal is different:
I generate the data for Flot in the MODEL folder locally and convert it into a JSON array in the CONTROLLER folder. Now, I need to access this JSON data in the VIEW folder on my localhost.
Here's the code snippet from the CONTROLLER:
$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);
However, when attempting to access the $json from the CONTROLLER in the VIEW via JS as shown below, it doesn't work. Despite searching online for a solution, I have been unable to resolve it. Can anyone provide guidance on how to fix this?
JS code in VIEW for accessing 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);
}
});
});