如何在谷歌图表上绘制直方图的垂直线?

yus*_*suf 4 javascript google-visualization histogram

如果我绘制折线图没有问题,但我想在直方图上显示它.. ( https://developers.google.com/chart/interactive/docs/gallery/histogram )

对于折线图;

var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
Run Code Online (Sandbox Code Playgroud)

对于直方图;

var chart = new google.visualization.Histogram(document.querySelector('#chart_div'));
Run Code Online (Sandbox Code Playgroud)

其他代码;

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'
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

yus*_*suf 5

Daniel LaLiberte 在 Google Groups 上回答了我的问题,他是 Google 的高级软件工程师。

https://groups.google.com/forum/#!msg/google-visualization-api/7y3LrKETEwY/fR4HoYwBu-EJ

所以在谷歌图表上是不可能的..

但是 :) Google Charts 使用 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);
Run Code Online (Sandbox Code Playgroud)