Google 可视化图表未显示第一列

asa*_*afb 3 google-visualization

我遇到了 Google Visualization 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"
      });

  }
Run Code Online (Sandbox Code Playgroud)

生成时,图表第一行 (2013-5-26) 不显示任何内容,仅显示第二行的值 2 和 1(省略 0.5)。

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

有没有人有任何想法?

asa*_*afb 5

所以似乎谷歌已经提供了一些解决方案......

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

帮助!我的图表变得不稳定!

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

这让您非常沮丧,那么您可以执行以下操作之一:

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

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

//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]);
Run Code Online (Sandbox Code Playgroud)

所以整个函数变成了:

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"
  });
}
Run Code Online (Sandbox Code Playgroud)