Google Charts:处理“点击”事件时遇到问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Google Charts:处理“点击”事件时遇到问题相关的知识,希望对你有一定的参考价值。

我有一个谷歌图表,一个条形图,我最近扩展,以便选择其中一个栏将打开一个新页面,显示与该栏对应的数据。我使用了基本交互的指南,使用selectHandler来处理事件。

然后我决定在点击其中一个标签时做同样的事情,但我无法让它工作。我仔细查看了文档,并在this older question(事实上,这是我的实际问题:如何从图表的标签中制作超链接?)作为指导。我为点击事件添加了一个监听器,并为此添加了一个处理程序,但我的图表没有响应它们。

我的基本设置是:

google.charts.load('current', {
  callback: drawChart,
  packages: ['bar']
});

var books = [
  ['2018-07', 5, 98.0],
  ['2018-08', 5, 100.0], // etc.
];
function drawChart() {

  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Date');
  data.addColumn('number', 'Number Purchased');
  data.addColumn('number', 'Price Paid');

  data.addRows(books);

  var options = {
    chart: {
      title: 'Book Purchases',
    },
    width: 800,
    height: 800,
    bars: 'horizontal',
    series: {
      0: {
        axis: 'purchased'
      },
      1: {
        axis: 'price_paid'
      }
    },
    axes: {
      x: {
        purchased: {
          side: 'top',
          label: 'Number Purchased'
        },
        price_paid: {
          label: 'Price Paid'
        }
      }
    }
  };

function selectHandler() {
  var selectedItem = chart.getSelection()[0];
  if (selectedItem) {
    var date = data.getValue(selectedItem.row, 0);
    var query_string = '?date=' + date;
    var path = window.location.pathname;
    path = path.replace("bookgraph", "");
    path = path + "search" + query_string;
    var newURL = window.location.protocol + "//" + window.location.host + path;
    window.open(newURL, '_blank');
  }
}

function clickHandler(e) {
  alert('The user has clicked on ' + e.targetID);
}

var chart = new google.charts.Bar(document.getElementById('bookgraph_material'));

google.visualization.events.addListener(chart, 'click', clickHandler);
google.visualization.events.addListener(chart, 'select', selectHandler);
chart.draw(data, options);

selectHandler工作正常。但我甚至无法让clickHandler显示警告(在此工作之后,我将实际编写其余的代码);无论我点击哪里,它显然都不会被解雇。我究竟做错了什么?

我为此设置了一个JS Fiddle page,用html框架进行试验,这样它才能真正起作用;这显示了工作选择(当然是404)和非工作点击。

谢谢。

答案

物质图表不支持'click'事件, 看看issue #: 2257 ......

并且有几个配置选项也不受支持, 看看issue #: 2143 ......

材料= google.charts.Bar - packages: ['bar']

经典= google.visualization.BarChart - packages: ['corechart']


一个解决方法是使用带有选项的经典图表 - > theme: 'material'

或者注册你自己的点击事件, 请参阅以下工作代码段...

google.charts.load('current', {
  callback: drawChart,
  packages: ['bar']
});

var books = [
  ['2016-01', 3, 45.0],
  ['2016-02', 3, 56.0],
  ['2016-03', 1, 23.0],
  ['2016-04', 4, 60.0],
  ['2016-05', 1, 0],
  ['2016-06', 3, 14.0],
  ['2016-07', 4, 65.0],
  ['2016-08', 1, 15.0],
  ['2016-09', 13, 234.0],
  ['2016-10', 20, 834.0],
  ['2016-11', 5, 115.0],
  ['2016-12', 5, 58.0],
  ['2017-01', 6, 122.0],
  ['2017-02', 4, 84.0],
  ['2017-03', 1, 0],
  ['2017-04', 1, 30.0],
  ['2017-05', 2, 38.0],
  ['2017-06', 1, 11.0],
  ['2017-07', 0, 0],
  ['2017-08', 4, 88.0],
  ['2017-09', 5, 89.0],
  ['2017-10', 4, 73.0],
  ['2017-11', 5, 79.0],
  ['2017-12', 2, 37.0],
  ['2018-01', 1, 22.0],
  ['2018-02', 5, 98.0],
  ['2018-03', 5, 132.0],
  ['2018-04', 3, 56.0],
  ['2018-05', 14, 272.0],
  ['2018-06', 4, 88.0],
  ['2018-07', 5, 98.0],
  ['2018-08', 5, 100.0],
];

function drawChart() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Date');
  data.addColumn('number', 'Number Purchased');
  data.addColumn('number', 'Price Paid');

  data.addRows(books);

  var options = {
    chart: {
      title: 'Book Purchases',
    },
    width: 800,
    height: 800,
    bars: 'horizontal',
    series: {
      0: {
        axis: 'purchased'
      },
      1: {
        axis: 'price_paid'
      }
    },
    axes: {
      x: {
        purchased: {
          side: 'top',
          label: 'Number Purchased'
        },
        price_paid: {
          label: 'Price Paid'
        }
      }
    }
  };

  function selectHandler() {
    var selectedItem = chart.getSelection()[0];
    if (selectedItem) {
      var date = data.getValue(selectedItem.row, 0);
      var query_string = '?date=' + date;
      var path = window.location.pathname;
      path = path.replace("bookgraph", "");
      path = path + "search" + query_string;
      var newURL = window.location.protocol + "//" + window.location.host + path;
      window.open(newURL, '_blank');
    }
  }

  function clickHandler(e) {
    if (e.target.tagName === 'text') {
      console.log(e.target.textContent);
    }
  }

  var container = document.getElementById('bookgraph_material');
  var chart = new google.charts.Bar(container);

  google.visualization.events.addListener(chart, 'select', selectHandler);

  container.addEventListener('click', clickHandler);

  chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="bookgraph_material"></div>

以上是关于Google Charts:处理“点击”事件时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Google Charts 动画中阻止图例消失?

v-charts修改点击图例事件,legendselectchanged

Google Charts - 工具提示中的完整html

e_charts如何添加地图markPoint的点击事件?????比如我要点击下图中的那个点并且出现浮动框

angular11给Echarts添加点击事件,无脑抄代码的时候到了~~ 超好用

使用角度谷歌地图时如何正确处理标记点击事件?