Google Visualization图表未显示第一列

时间:2013-07-03 02:54:23

标签: google-visualization

我对Google Visualization API存在问题,因为图表中的某些数据未显示。图表相当简单,它有4列和2行。

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)的任何内容,并且仅显示第二行的值2和1(省略0.5)。

我怀疑这可能类似于Google Column Chart Missing data

有没有人有任何想法?

1 个答案:

答案 0 :(得分:4)

因此Google似乎在某种程度上提供了解决方案......

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

  

帮助!我的图表变得很糟糕!

     

我的域轴类型不是字符串,但我仍然想要一个离散的域轴:

     

这会让你非常沮丧,然后你可以做其中一个   以下内容:

     
      
  1. 将第一个数据表列的类型更改为字符串。
  2.   
  3. 使用DataView作为适配器来转换第一个数据表的类型   列到字符串:
  4.   

所以上图的解决方案是添加:

  
//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"
  });
}