gpt4 book ai didi

google-visualization - 谷歌可视化图表不显示第一列

转载 作者:行者123 更新时间:2023-12-04 00:42:29 26 4
gpt4 key购买 nike

我在使用 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"
});

}

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

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

有没有人有什么想法?

最佳答案

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

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

Help! My chart has gone wonky!

My domain axis type is not string but I still want a discrete domain axis:

and this makes you terribly upset, then you can do one of the following:

  1. Change the type of your first data table column to string.
  2. Use a DataView as adapter to convert the type of your first data table column to string:

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

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

关于google-visualization - 谷歌可视化图表不显示第一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17438924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com