Reputation: 129
I am trying to set up a google scatter chart page from these links...
https://developers.google.com/chart/interactive/docs/gallery/scatterchart and create data table here... https://developers.google.com/chart/interactive/docs/drawing_charts#chart.draw
Basically I have the basic demo copy working. Now I am trying to populate the data table with time and date data like this... add rows like this...
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('number', 'Time');
data.addColumn('number', 'Date');
data.addRows([
// this raw text works [22,5],[10,24],[9,5],[10,6],[10,7],[7,8]
// but my var - datetime from database not working //Error: Every row given must be either null or an array.
datetime ]);
If I manually type in this:
[22,5],[10,24],[9,5],[10,6],[10,7],[7,8]
it works.
Now I am trying to query a database - then do a .each loop to get the data...
//Query the database...
// query here..
datetime = "";
$.each(data, function(key, value) {
//note: each row comes in from the database like:
//thedate: "02/17/2018" thetime: "18:00:00"
//then I split to just get basic hour and day
t = value.thetime.split(":")
d = value.thedate.split("/")
//and create as numbers
tnum = parseInt(t[0]);
dnum = parseInt(d[1]);
//and add to the datetime var
datetime += "["+ dnum + ","+ tnum + "],"
// there was more code to eliminate last ","
// then the end result var datetime looks something like:
//[22,5],[10,24],[9,5],[10,6],[10,7],[7,8]
If I manually paste this into the add rows area - it works, however if just paste the datetime var into the same add rows area. the error says:
Error: Every row given must be either null or an array.
Q: I'm sure I am missing something basic, such as the graph needs numbers that may be showing as strings or my array is wrong or something else. Is there something to change to get the graph to accept my datetime var?
Upvotes: 2
Views: 709
Reputation: 61230
looks like you're trying to create an array from a string,
instead, use an actual array, which addRows
expects...
datetime = [];
$.each(data, function(key, value) {
t = value.thetime.split(":")
d = value.thedate.split("/")
tnum = parseInt(t[0]);
dnum = parseInt(d[1]);
datetime.push([dnum, tnum]);
});
data.addRows(datetime);
Upvotes: 2