所以看来谷歌已经提供了一些解决方案......
https://developers.google.com/chart/interactive/docs/customizing_axes#Discrete_vs_Continously https://developers.google.com/chart/interactive/docs/customizing_axes#Discrete_vs_Continuous
帮助!我的图表变得不稳定!
我的域轴类型不是字符串,但我仍然想要一个离散域轴:
这让你非常沮丧,那么你可以做其中之一
下列的:
- 将第一个数据表列的类型更改为字符串。
- 使用 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"
});
}