如何检测 Google visualization.Calendar 中日期单元格上的点击事件

How to detect click event on date cell in Google visualization.Calendar

我有一个关于 google.visualization.Calendar 的问题。我已阅读 [https://developers.google.com/chart/interactive/docs/gallery/calendar#months][1] 但仍找不到任何解决方案

下面的代码是我用来测试的代码。

var dataTable=new google.visualization.DataTable();
    dataTable.addColumn({type:'date', id:'Date'});
    dataTable.addColumn({type:'number',id:'Events Found'});

    dataTable.addRows([
                       [ new Date(2016, (05-1), 19), 400],
                       [ new Date(2016, 02, 23), 300],
                       [ new Date(2016, 05, 24), 300],
                       [ new Date(2016, 06, 23), 300]
                       ]);

    var chart=new google.visualization.Calendar(document.getElementById('calendarView'));
    var options={
            title: "Event Calendar",
            width: 500,
            height:400,
            calendar: { 
                cellSize: 5 ,
                    focusedCellColor: {
                      stroke: 'red',
                      strokeOpacity: 0.8,
                      strokeWidth: 3
                    }       
            },
             /*noDataPattern: {
                   backgroundColor: '#76a7fa',
                   color: '#a0c3ff'
                 },*/


            calendar: {
                  underYearSpace: 2, 
                  yearLabel: {
                    fontName: 'Times-Roman',
                    fontSize: 32,
                    color: '#e6ecff',
                    bold: true,
                    italic: true
                  }
                }
    };

    chart.draw(dataTable,options);

我需要在用户单击特定日期时添加单击事件处理程序。怎么做?

这是我测试过的代码,没有结果。

代码:1

google.visualization.events.addListener(chart,'click',function(){
        alert ('click');
    });

代码:2

google.visualization.events.trigger(dataTable, 'select', selectHandler);
    function selectHandler() {
      alert('A table row was selected');
    }

任何解决这些问题的技巧都将不胜感激。

calendar events 包括 'select',但没有 'click'...

要使用 'select' 事件,必须将侦听器添加到 chart
在绘制之前。

参见以下示例...

google.charts.load('current', {
  callback: function () {
    var dataTable = new google.visualization.DataTable();
    dataTable.addColumn({type: 'date', id: 'Date'});
    dataTable.addColumn({type: 'number', id: 'Events Found'});

    dataTable.addRows([
      [ new Date(2016, (05-1), 19), 400],
      [ new Date(2016, 02, 23), 300],
      [ new Date(2016, 05, 24), 300],
      [ new Date(2016, 06, 23), 300]
    ]);

    var chart = new google.visualization.Calendar(document.getElementById('calendarView'));

    var options={
      title: 'Event Calendar',
      calendar: {
        focusedCellColor: {
          stroke: 'red',
          strokeOpacity: 0.8,
          strokeWidth: 3
        },
        underYearSpace: 2,
        yearLabel: {
          fontName: 'Times-Roman',
          fontSize: 32,
          color: '#e6ecff',
          bold: true,
          italic: true
        }
      },
    };

    google.visualization.events.addListener(chart, 'select', function () {
      document.getElementById('msg_div').innerHTML = JSON.stringify(chart.getSelection());
    });

    chart.draw(dataTable, options);
  },
  packages:['calendar']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="calendarView"></div>
<div id="msg_div"></div>