如何在Google Line Chart中动态更改线宽?

时间:2015-07-27 07:57:12

标签: javascript google-visualization interactive

我有一个复杂的折线图,想要在数据集上移动鼠标时突出显示(变胖)线条。事件"选择"和" onmouseover"我可以捕获,但如何动态更改数据集的线宽?或者是否还有其他可能突出显示所选行? enter image description here

1 个答案:

答案 0 :(得分:2)

根据Customizing Lines,Google Chart中的行可以通过两种略有不同的方式进行自定义:使用colors选项更改图表调色板,或使用series选项指定特定系列的颜色。

以下示例演示如何突出显示选择事件的行:



google.load('visualization', '1.1', { packages: ['corechart'] });
google.setOnLoadCallback(drawChart);

function drawChart() {

    var data = new google.visualization.DataTable();
    data.addColumn('number', 'Day');
    data.addColumn('number', 'Guardians of the Galaxy');
    data.addColumn('number', 'The Avengers');
    data.addColumn('number', 'Transformers: Age of Extinction');

    data.addRows([
        [1, 37.8, 80.8, 41.8],
        [2, 30.9, 69.5, 32.4],
        [3, 25.4, 57, 25.7],
        [4, 11.7, 18.8, 10.5],
        [5, 11.9, 17.6, 10.4],
        [6, 8.8, 13.6, 7.7],
        [7, 7.6, 12.3, 9.6],
        [8, 12.3, 29.2, 10.6],
        [9, 16.9, 42.9, 14.8],
        [10, 12.8, 30.9, 11.6],
        [11, 5.3, 7.9, 4.7],
        [12, 6.6, 8.4, 5.2],
        [13, 4.8, 6.3, 3.6],
        [14, 4.2, 6.2, 3.4]
    ]);

    var options = {
        chart: {
            title: 'Box Office Earnings in First Two Weeks of Opening',
            subtitle: 'in millions of dollars (USD)'
        },
        series: {
            0: { lineWidth: 1 },
            1: { lineWidth: 1 },
            2: { lineWidth: 1 }
        },
        width: 900,
        height: 500
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart'));
    google.visualization.events.addListener(chart, 'select', function() { highlightLine(chart,data, options); });

    chart.draw(data, options);
}


function highlightLine(chart,data,options) {
    var selectedLineWidth = 4;
    var selectedItem = chart.getSelection()[0];
    //reset series line width to default value
    for(var i in options.series) {
        options.series[i].lineWidth = 1;
    }
    options.series[selectedItem.column-1].lineWidth = selectedLineWidth; //set selected line width
    chart.draw(data, options);   //redraw
}

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart"></div>
&#13;
&#13;
&#13;