While more cumbersome than just dealing with the basic data in an array, Ive beeen outputting a DataTable for multiple charts and gotten it working fine. I have still not gotten an array through arrayToDataTable even once however.
Ive used single quotes, double quotes, json_encode, JSON.parse, and many other things in every possible combination, but nothing formats things the way that arrayToDataTable wants if I use jquery at all.
The array looks like :
[["Sensor", "Temp", { role : 'style' }], ["Baby",71.15,"green"], ["Main",71.2616,"green"], ["A",75.425,"green"], ["C",69.575,"blue"], ["D",60.6866,"blue"],]
This is for a column chart, and I cannot find a way get the third data column to work with a DataTable call, I suspect due to none of the listed "types" working for that column. If I plug in the array directly into arrayToDataTable it works like a charm. But now I'm back to banging my head against some way to force arrayToDataTable to take input from jquery.
So Im back to...
this works:
var data = new google.visualization.arrayToDataTable( [["Sensor", "Temp", { role : 'style' }], ["Baby",71.15,"green"], ["Main",71.2616,"green"], ["A",75.425,"green"], ["C",69.575,"blue"], ["D",60.6866,"blue"],] );
this works :
var data = new google.visualization.arrayToDataTable ( <?php include 'getData-roombars2.php'?>);
(also looks identical to above when viewed as html source in browser)
This does not work:
var jsonData = $.ajax({
url: "getData-roombars2.php",
dataType:"json",
async: false
}).responseText;
var data = new google.visualization.arrayToDataTable( jsonData );
If I do a json_encode on the php side, I get :
" [[\"Sensor\", \"Temp\", { role : 'style' }], [\"Baby\",74.1866,\"green\"], [\"Main\",74.075,\"green\"], [\"A\",75.7616,\"green\"], [\"C\",72.5,\"green\"], [\"D\",60.0116,\"blue\"],]"
but then neither var data = new google.visualization.arrayToDataTable( jsonData ); nor var data = new google.visualization.arrayToDataTable( JSON.parse(jsonData) ); work.
If I use JSON.parse on the javascript side, it just makes an array of each of the characters in the string instead of an array of the elements since it cant seem to figure out which commas to use.
Ive also tried double quotes on the role: "style", but that makes no difference.
So, my question boils down to .... if my getData-roombars2.php script is going to output something like :
[["Sensor", "Temp", { role : 'style' }], ["Baby",71.15,"green"], ["Main",71.2616,"green"], ["A",75.425,"green"], ["C",69.575,"blue"], ["D",60.6866,"blue"],]
What does the
var jsonData = $.ajax({
url: "getData-roombars2.php",
dataType:"json",
});.responseText;
need to make jsonData the right format for arrayToDataTable?
Ive tried your
$.ajax({
url: "getData-roombars2.php",
dataType:"json",
success : function (jsonData) {
var data = new google.visualization.DataTable(jsonData);
// draw chart(s) with data
}
});
Which works fine if I can get the PHP script to output all the extra non functional meta data correctly for a DataTable, But since I cant find documentation on how to do the 'type' for the third data column in a column chart, Im forced back to outputting an array. I'd like to be able to use array's since they are a much simpler anyhow, which would seem to be the point of arrayToDataTable.