jQuery 谷歌可视化-点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15632698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:28:03  来源:igfitidea点击:

google visualization-Click event

javascriptjquerygoogle-visualization

提问by vmb

I am using google visulaization for drawing pie chart.Issue i am facing is, i cant able to capture click eventon pie chart.i am doing like this.

我正在使用谷歌可视化来绘制饼图。我面临的问题是,我无法在饼图上捕获点击事件。我正在这样做。

function drawchartfromRe()
{
    dashboard = new google.visualization.Dashboard(
            document.getElementById('dashboard_div'));
    //alert("RefuelLength"+totrefuelList.length);
    //alert("Vehicleid:"+totrefuelList[0].vehicleId);
   //google.load("visualization", "1", {packages:["corechart"]});
    data = new google.visualization.DataTable();
    data.addColumn('string', 'Task');
    data.addColumn('number', 'Quantity');
    data.addRows(totrefuelList.length);
    for (var i=0;i<totrefuelList.length;i++)
    {
       data.setValue(i, 0, totrefuelList[i].vehicleName);
       data.setValue(i, 1, totrefuelList[i].totalRefuelQty);
    }

    // var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
     chart = new google.visualization.ChartWrapper({
      'chartType': 'PieChart',
      'containerId': 'chart_div',
      'options': {
          title: 'Refuel Trend',
          height:'500',
          width:'400',
          backgroundColor: { fill:'transparent' },
        'legend': 'right'
      }
    });

  /* google.visualization.events.addListener(chart, 'select', function(e) {
       // var selection = chart.getSelection();
      var vehid= data.getValue(visualization.getSelection()[0].row, 0);
        getRefueldailywise(vehid);
    });*/
   // chart.draw(data, options);
   drawDashboard(dashboard,data,chart);
   google.visualization.events.addListener(chart, 'select', function() {
       // grab a few details before redirecting

      alert(data.getValue(chart.getSelection()[0].row, 0));
       //location.href = 'http://www.google.com?row=' + row + '&col=' + col + '&year=' + year;
     });
}

In firebug i am getting error like this.. dashboard.getChart is not a function

在萤火虫我收到这样的错误..dashboard.getChart 不是一个函数

回答by jmac

Chart Wrappers are not chart objects and do not have a clickevent. In fact, Pie Charts also do not have a click event, only select.

图表包装器不是图表对象,也没有click事件。事实上,饼图也没有点击事件,只有select.

If you read the documentationit says to:

如果您阅读文档,它会说:

  1. Create a readyevent for the wrapper
  2. Have the readyevent trigger a selectevent for the chart in the wrapper
  1. ready为包装器创建一个事件
  2. ready事件触发select包装器中图表的事件

Here is the example they give:

这是他们给出的例子:

var wrapper;
function drawVisualization() {

  // Draw a column chart
  wrapper = new google.visualization.ChartWrapper({
    chartType: 'ColumnChart',
    dataTable: [['Germany', 'USA', 'Brazil', 'Canada', 'France', 'RU'],
                [700, 300, 400, 500, 600, 800]],
    options: {'title': 'Countries'},
    containerId: 'visualization'
  });

  // Never called.
  google.visualization.events.addListener(wrapper, 'onmouseover', uselessHandler);

  // Must wait for the ready event in order to
  // request the chart and subscribe to 'onmouseover'.
  google.visualization.events.addListener(wrapper, 'ready', onReady);

  wrapper.draw();

  // Never called
  function uselessHandler() {
    alert("I am never called!");
  }

  function onReady() {
    google.visualization.events.addListener(wrapper.getChart(), 'onmouseover', usefulHandler);
  }

  // Called
  function usefulHandler() {
    alert("Mouseover event!");
  }
}

So in your case you will need to change this section:

因此,在您的情况下,您需要更改此部分:

   google.visualization.events.addListener(chart, 'ready', function() {
       // grab a few details before redirecting

      alert(data.getValue(chart.getSelection()[0].row, 0));
       //location.href = 'http://www.google.com?row=' + row + '&col=' + col + '&year=' + year;
     });

To something like this:

对于这样的事情:

   google.visualization.events.addListener(chart, 'ready', function() {
       // grab a few details before redirecting
      google.visualization.events.addListener(chart.getChart(), 'select', function() {
          chartObject = chart.getChart();
          alert(data.getValue(chartObject.getSelection()[0].row, 0));
      });

       //location.href = 'http://www.google.com?row=' + row + '&col=' + col + '&year=' + year;
     });