我使用带有离散值的谷歌图表,并且似乎无法绘制vAxis线(黑色)。
在连续值中显示图形时,但我不希望图表以这种方式间隔开。
这里有两种类型的图表。
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var discreteData = new google.visualization.DataTable();
discreteData.addColumn('string', 'Number');
discreteData.addColumn('number', 'Value');
discreteData.addRows([
['1.492', 10],
['30.701', 17],
['127.469', 6],
['749.382', 11]
]);
var discreteChart = new google.visualization.ColumnChart(document.getElementById('discrete_chart_div'));
discreteChart.draw(discreteData, {
title: 'Discrete Axis Line Chart'
});
var continuousData = new google.visualization.DataTable();
continuousData.addColumn('number', 'Number');
continuousData.addColumn('number', 'Value');
continuousData.addRows([
[1.492, 10],
[30.701, 17],
[127.469, 6],
[749.382, 11]
]);
var continuousChart = new google.visualization.ColumnChart(document.getElementById('continuous_chart_div'));
continuousChart.draw(continuousData, {
title: 'Continuous Axis Line Chart'
});
}
如何绘制vAxis线?
答案 0 :(得分:5)
“vAxis线”实际上是hAxis基线,仅适用于连续轴。有点hackery,您可以将数据放在一个连续的轴上,同时保持其独立性。使用DataView将数据转换为“数字”类型(使用值的行索引和字符串值作为格式化值),并从视图中的值/格式化值对构建hAxis.ticks
选项:
function drawChart() {
var discreteData = new google.visualization.DataTable();
discreteData.addColumn('string', 'Number');
discreteData.addColumn('number', 'Value');
discreteData.addRows([
['1.492', 10],
['30.701', 17],
['127.469', 6],
['749.382', 11]
]);
var view = new google.visualization.DataView(discreteData);
view.setColumns([{
type: 'number',
label: discreteData.getColumnLabel(0),
calc: function (dt, row) {
return {v: row + 1, f: dt.getFormattedValue(row, 0)};
}
}, 1]);
var ticks = [];
for (var i = 0; i < view.getNumberOfRows(); i++) {
ticks.push({v: view.getValue(i, 0), f: view.getFormattedValue(i, 0)});
}
var range = view.getColumnRange(0);
var offset = 0.5; // change this to move the left/right margins of the chart
var discreteChart = new google.visualization.ColumnChart(document.getElementById('discrete_chart_div'));
discreteChart.draw(view, {
title: 'Discrete Axis Line Chart',
hAxis: {
ticks: ticks,
viewWindow: {
min: range.min - offset,
max: range.max + offset
},
// set gridlines.color to "transparent" to hide the vertical gridlines
/*
gridlines: {
color: 'transparent'
}
*/
}
});
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});