【问题标题】:Google Visualization Chart Not Showing First Column谷歌可视化图表不显示第一列
【发布时间】:2013-07-03 02:54:23
【问题描述】:

我在使用 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

有人有什么想法吗?

【问题讨论】:

  • 有趣的是,将第一列设置为数据类型“字符串”并将值设置为 new Date(...).toString() 似乎可以解决问题...

标签: google-visualization


【解决方案1】:

所以看起来谷歌已经提供了一些解决方案......

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

所以整个函数变成了:

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

【讨论】:

  • 无论如何比添加1, 2 ,3更动态?确定我可以传递一个 int 就好了,如果我把它称为一个字符串......现在就像 OP 一样停留在日期上。
猜你喜欢
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多