javascript - addListener 事件——错误

标签 javascript google-visualization

我正在尝试在行和数据点之间构建一个事件处理程序。我当前收到错误:

TypeError: line.getSelection is not a function.

我不太确定如何添加此功能或者我可能会出错的地方:

        var table = new google.visualization.ChartWrapper({
            'chartType': 'Table',
            'containerId': 'TableContainer',
            'options': { 'height': '25em', 'width': '80em' }
        });

        new google.visualization.Dashboard(document.getElementById('PieChartExample')).bind([myIdSlider], [line, table]).draw(data);
        table.draw(data, { showRowNumber: true });

        google.visualization.events.addListener(line, 'select', function () {
            table.setSelection([{ row: line.getSelection()[0].row }]);
        });

        google.visualization.events.addListener(table, 'select', function () {
            line.setSelection(table.getSelection());
        });

       // table.setSelection([{ row: chart.getSelection()[0].row }]);


    }

任何帮助将不胜感激。非常感谢。

最佳答案

假设您的 line 变量是 ChartWrapper(如 table),您需要使用:

google.visualization.events.addListener(line, 'select', function () {
    var lineSelection = line.getChart().getSelection();
    var tableSelection = [];
    for (var i = 0; i < lineSelection.length; i++) {
        // iterate over lineSelection, since it could potentially contain multiple or 0 elements
        // check to see if "row" property is defined, since clicking on the legend fires a select event with no "row" property
        if (lineSelection[i].row != null) {
            tableSelection.push({row: lineSelection[i].row});
        }
    }
    table.getChart().setSelection(tableSelection);
});

google.visualization.events.addListener(table, 'select', function () {
    var tableSelection = table.getChart().getSelection();
    var lineSelection = [];
    for (var i = 0; i < lineSelection.length; i++) {
        // iterate over lineSelection, since it could potentially contain multiple or 0 elements
        lineSelection.push({
            row: tableSelection[i].row,
            column: /* choose a column to select */
        });
        // you may add more selections here if you want to select points from multiple data series
    }
    line.getChart().setSelection(lineSelection);
});

关于javascript - addListener 事件——错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23764743/

相关文章:

mysql - 从 MYSQL 将数据输入到 Google 柱形图中的 2 个以上的列。

javascript - 多次动画 Google 图表转换

javascript - 在 Google 饼图周围添加边框

javascript - 如何将Google可视化查询转化为图表?

javascript - Google 图表问题(图表标题、轴文本)

javascript - 跟踪 DOM 元素上的事件监听器

javascript - jQuery .load 按元素 ID

javascript - 如何设置数组以便在不使用 jQuery 的情况下获取单选按钮值

javascript - 客户端 javascript 驱动的网站

javascript - JavaScript 有类似 Perl 的翻译吗?