2013-07-03 69 views
1

我有一個谷歌可視化API的問題,因爲圖表中的一些數據沒有顯示。圖表非常簡單,它有4列和兩行。谷歌可視化圖表不顯示第一列

http://savedbythegoog.appspot.com/?id=ae0853b788af3292b5547a5b7f1224aed76abfff

function drawVisualization() { 
     // Create and populate the data table. 

     var data_table = new google.visualization.DataTable(); 
     data_table.addColumn({"type": "date","label": "Date"}); 
     data_table.addColumn({"type": "number","label": "A"}); 
     data_table.addColumn({"type": "number","label": "B"}); 
     data_table.addColumn({"type": "number","label": "C"}); 

     data_table.addRow([{v: new Date(2013, 5, 26)}, {v: 1}, {v: 0}, {v: 0}]); 
     data_table.addRow([{v: new Date(2013, 5, 27)}, {v: 2}, {v: 1}, {v: 0.5}]); 

     var chart = new google.visualization.ColumnChart(document.getElementById('visualization')); 
     chart.draw(data_table, { 
      legend: "bottom" 
     }); 

    } 

當生成時,圖表顯示什麼對於第一行(2013年5月26日),並且示出了僅針對第二行(省略0.5)的2和1的值。

我懷疑這可能是類似於Google Column Chart Missing data

有沒有人有什麼想法?

+0

有趣的是,設置將第一列設置爲數據類型「字符串」並將值設置爲新Date(...)。toString()似乎可以解決問題... – asafb

回答

3

如此看來谷歌已經提供了一定程度上的解決方案......

https://developers.google.com/chart/interactive/docs/customizing_axes#Discrete_vs_Continuous

幫助!我的圖表變得won!!

我的域名軸類型不是字符串,但我還是希望有一個獨立域名軸:

,這使你心亂如麻,那麼你可以做的 之一以下:

  1. 更改類型你的第一個數據表列的字符串。
  2. 使用數據視圖作爲適配器類型 列轉換你的第一個數據表的字符串:

所以解決上面的圖是添加:

//Create a DataView from the data_table 
var dataView = new google.visualization.DataView(data_table); 

//Set the first column of the dataview to format as a string, and return the other columns [1, 2 and 3] 
dataView.setColumns([{calc: function(data, row) { return data.getFormattedValue(row, 0); }, type:'string'}, 1, 2, 3]); 

所以整個功能變爲:

function drawVisualization() { 
var data_table = new google.visualization.DataTable(); 
    data_table.addColumn({"type": "date","label": "Date"}); 
    data_table.addColumn({"type": "number","label": "A"}); 
    data_table.addColumn({"type": "number","label": "B"}); 
    data_table.addColumn({"type": "number","label": "C"}); 

    data_table.addRow([{v: new Date(2013, 5, 26)}, {v: 1}, {v: 0}, {v: 0}]); 
    data_table.addRow([{v: new Date(2013, 5, 27)}, {v: 2}, {v: 1}, {v: 0.5}]); 

    //Create a DataView from the data_table 
    var dataView = new google.visualization.DataView(data_table); 

    //Set the first column of the dataview to format as a string, and return the other columns [1, 2 and 3] 
    dataView.setColumns([{calc: function(data, row) { return data.getFormattedValue(row, 0); }, type:'string'}, 1, 2, 3]); 
    var chart = new google.visualization.ColumnChart(document.getElementById('visualization')); 
    chart.draw(dataView, { 
     legend: "bottom" 
    }); 
}