谷歌图表鼠标悬停两个图表

时间:2014-09-23 16:55:45

标签: javascript google-visualization

此问题与使用Google图表有关。

我有一个数据集,该数据集由具有贡献金额的类别组成,在一个月的日子里。我用饼图代表整个月份,并在堆积条形图中表示各个日期的数据。两个图表基本上都基于相同的信息。如果我按照正确的顺序给两个图表他们的信息,颜色在两者之间协调(即一个中的每个类别与另一个中的相同类别具有相同的颜色)。

当您将鼠标悬停在饼图切片上时,它会突出显示。我希望堆积条形图中的相应数据也能同时突出显示,反之亦然。

使用Google可视化API实现此目标的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

使用<chart type>#setSelection方法突出显示数据点。如果我正确理解您的数据结构,这样的事情应该有效:

google.visualization.events.addListener(pieChart, 'select', function () {
    var selection = pieChart.getSelection();
    if (selection.length) {
        // assumes the row in the PieChart's data corresponds to the data series in the ColumnChart, which is the nth + 1 column
        columnChart.setSelection([{column: selection[0].row + 1}]);
    }
});
google.visualization.events.addListener(columnChart, 'select', function () {
    var selection = columnChart.getSelection();
    if (selection.length) {
        // assumes the data series in the ColumnChart's data corresponds to the row in the PieChart, which is the nth column - 1
        pieChart.setSelection([{column: selection[0].column - 1}]);
    }
});