如何在Google图表上绘制垂直线到直方图?
问题描述:
如果我画线图是没有问题的,但我想这个对色阶柱状图。(https://developers.google.com/chart/interactive/docs/gallery/histogram)如何在Google图表上绘制垂直线到直方图?
对于线型图;
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
对于直方图;
var chart = new google.visualization.Histogram(document.querySelector('#chart_div'));
其他代码;
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn({type: 'string', role: 'annotation'});
data.addColumn('number', 'Value');
data.addRows([
['Foo', null, 4],
['Bar', null, 3],
['Baz', null, 7],
['Bat', null, 9],
['Cad', 'Vertical line here', 9],
['Qud', null, 2],
['Piz', null, 6]
]);
var chart = new google.visualization.Histogram(document.querySelector('#chart_div'));
chart.draw(data, {
height: 300,
width: 400,
annotation: {
// index here is the index of the DataTable column providing the annotation
1: {
style: 'line'
}
}
});
}
答
丹尼尔·拉利伯特回答我在谷歌网上论坛,谁是在谷歌的高级软件工程师的问题..
https://groups.google.com/forum/#!msg/google-visualization-api/7y3LrKETEwY/fR4HoYwBu-EJ
所以它是不可能在谷歌图表..
但:)谷歌图表使用SVG ..对于exp。我想提请线30 x轴..
var newLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
newLine.setAttribute('id', 'lineId');
newLine.setAttribute('style', 'stroke:rgb(0,0,0); stroke-width:3;');
newLine.setAttribute('x1', chart.getChartLayoutInterface().getXLocation(30));
newLine.setAttribute('y1', chart.getChartLayoutInterface().getChartAreaBoundingBox().top);
newLine.setAttribute('x2', chart.getChartLayoutInterface().getXLocation(30));
newLine.setAttribute('y2', chart.getChartLayoutInterface().getChartAreaBoundingBox().height + chart.getChartLayoutInterface().getChartAreaBoundingBox().top);
$("svg").append(newLine);
对于未来的用户:这也可以用来制作动画上线图的“注释”行 - 我找不到任何办法让谷歌图为注释的变化位置设置动画效果,但在上面代码中绘制的线的X位置进行动画处理非常简单。感谢您的正确方向,dustqm! – roguenet