如何在Google Charts上绘制垂直线到直方图?

时间:2014-02-24 07:42:17

标签: javascript google-visualization histogram

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

对于LineChart;

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'
            }
        }
    });
}

1 个答案:

答案 0 :(得分:3)

Daniel LaLiberte回答了我关于Google群组的问题,Google群组是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);