javascript - Google 可视化图表对选择事件数据发出警报

标签 javascript jquery events google-api google-visualization

我正在尝试使用 Google 可视化柱形图在选择特定列时简单地发出警报。 IE。我想在选择 Company1 的列数据时执行某些操作(发出警报):

      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        data.addColumn('number', 'Company1');
        data.addColumn('number', 'Company2');
        data.addColumn('number', 'Company3');
        data.addColumn('number', 'Company4');
        data.addColumn('number', 'Company5');
        data.addColumn('number', 'Company6');
        data.addRows([
          ['Feb 1, 2012 - Mar 13, 2012', 10, 10, 5, 15, 10, 55]

        ]);

        var options = {
          title: 'Total Reviews',
          hAxis: {title: '',  titleTextStyle: {color: 'blue'}}
        };

        var chart = new google.visualization.ColumnChart(document.getElementById('total'));
        chart.draw(data, options);

google.visualization.events.addListener(chart, 'select', function() {
 //SOMETHING GOES HERE WHEN ie. Company1 is selected, probably an IF but I cant seem to let it know when Company1 is selected.
 alert('Company1 was selected!');       

}

最佳答案

您必须调用getSelection函数来检索当前选择。选择是对象的数组。每个对象都有行和列属性(如果有)。使用第一列检索标签名称:

google.visualization.events.addListener(chart, 'select', function() {
  var selection = chart.getSelection()[0];
  var label = data.getColumnLabel(selection.column);

  if (label === "Company1"){
    alert("!");
  }
});

Documentation (点击链接阅读更多内容):

selection_array: An array of selected objects, each one describing a data element in the underlying table used to create the visualization (a DataView or a DataTable). Each object has properties row and/or column, with the index of the row and/or column of the selected item in the underlying DataTable. If the row property is null, then the selection is a column; if the column property is null, then the selection is a row; if both are non-null, then it is a specific data item. You can call the DataTable.getValue() method to get the value of the selected item. The retrieved array can be passed into setSelection()

关于javascript - Google 可视化图表对选择事件数据发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9687776/

相关文章:

c# - 可以要求我的事件的处理程序立即返回吗?

javascript - 页面滚动时滑入和滑出按钮

javascript - react 与 Bootstrap : returning the value of selected radio button

javascript - 如何根据下拉列表显示 id?

jquery - 如何仅使用 jQuery 创建维基百科的脚注突出显示

jquery - 监听 HTML 导航事件

c# - 在构造函数中注册事件?

javascript - 脚本不会在 html 文件中执行

javascript - jQuery ui Accordion 事件类

javascript - 如何强制事件等待 Angular.js 中的 promise 解决?