5

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

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;
     });
}

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

4

1 回答 1

8

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

如果您阅读它说的文档:

  1. ready为包装器创建一个事件
  2. ready事件触发select包装器中图表的事件

这是他们给出的例子:

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!");
  }
}

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

   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;
     });

对于这样的事情:

   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;
     });
于 2013-03-27T00:40:31.590 回答