我在使用Google条形图时遇到问题。它没有按顺序排列y轴值(图A)。另一个问题是即使值是零,它也会显示条形图(图B)。
请在下面查看我的代码:
success: function (r) {
var data = new google.visualization.DataTable();
var newData = r.d;
// determine the number of rows and columns.
var numRows = newData.length;
if (numRows > 0) {
var numCols = newData[0].length;
// in this case the first column is of type 'string'.
data.addColumn('string', newData[0][0]);
// all other columns are of type 'number'.
for (var i = 1; i < numCols; i++)
data.addColumn('string', newData[0][i]);
// now add the rows.
for (var i = 1; i < numRows; i++)
data.addRow(newData[i]);
}
else {
data.addColumn('string', "Category");
data.addColumn('number', "No Data");
data.addRow(null);
}
var options = {
bars: 'vertical',
height: 350,
bar: { groupWidth: "40" },
legend: { position: 'top', maxLines: 3 },
chartArea: { 'width': '50%', 'height': '100%' }
};
var chart = new google.charts.Bar(document.getElementById('chart'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
图A
图B
如何解决此问题?
答案 0 :(得分:1)
使用字符串值而不是数字时会发生这种情况...
在这里,注释提到使用数字,但是代码包含字符串
// all other columns are of type 'number'.
for (var i = 1; i < numCols; i++)
data.addColumn('string', newData[0][i]);
尝试更改为以下内容...
// all other columns are of type 'number'.
for (var i = 1; i < numCols; i++)
data.addColumn('number', parseFloat(newData[0][i]));