簡體   English   中英

如何使用Google圖表設置數組以添加行

[英]How do I setup arrays with google charts add rows

我正在嘗試通過這些鏈接設置一個Google散點圖頁面...

https://developers.google.com/chart/interactive/docs/gallery/scatterchart並在此處創建數據表... https://developers.google.com/chart/interactive/docs/drawing_charts#chart.draw

基本上,我具有基本的演示副本。 現在,我試圖用這樣的時間和日期數據填充數據表...添加這樣的行...

// Create the data table.
  var data = new google.visualization.DataTable();
  data.addColumn('number', 'Time');
  data.addColumn('number', 'Date');
  data.addRows([

//此原始文本有效[22,5],[10,24],[9,5],[10,6],[10,7],[7,8]

//但我的var-來自數據庫的日期時間不起作用//錯誤:給定的每一行必須為null或數組。

約會時間 ]);

如果我手動輸入:

[22,5],[10,24],[9,5],[10,6],[10,7],[7,8]

有用。

現在我正在嘗試查詢數據庫-然后執行.each循環以獲取數據...

//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] 

如果我手動將其粘貼到“添加行”區域中,則可以正常工作,但是如果將datetime var粘貼到相同的“添加行”區域中,則可以正常工作。 錯誤說:

錯誤:給定的每一行必須為null或數組。

問:我確定我缺少一些基本知識,例如圖表需要數字,這些數字可能顯示為字符串,或者我的數組錯誤或其他。 有什么需要更改的圖形才能接受我的datetime var嗎?

看起來您正在嘗試從字符串創建數組,
相反,請使用addRows期望的實際數組...

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM