0

我正在尝试处理图表的选择事件,我想知道选择了哪一行/列,但是调用的函数出现错误:

未捕获的参考错误:未定义图表。

该代码与饼图示例和选择事件处理程序中的代码非常相似。我希望我需要让图表对象在全球范围内广为人知,但不知道该怎么做。

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work',     11],
          ['Eat',      2],
          ['Sleep',    12]
        ]);
        var options = { title: 'My Daily Activities' };

        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
        google.visualization.events.addListener(chart, 'select', selectHandler);
      }
      function selectHandler() {
          var selection = visualization.getSelection();
          alert("it");
      }



    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>
4

1 回答 1

0

你有没有解决过这个问题?如果不 ...

google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);

//declare your variables in the global scope
var chart, data;

function drawChart() {
    data = google.visualization.arrayToDataTable([
        ['Task', 'Hours per Day'],
        ['Work',     11],
        ['Eat',      2],
        ['Sleep',    12]
    ]);
    var options = { title: 'My Daily Activities' };

    chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
    google.visualization.events.addListener(chart, 'select', selectHandler);
}

function selectHandler() {
    //"visualization.getSelection();" dos not exist
    //access the global variable chart and its current selection
    var selection = chart.getSelection();
    alert(data.getValue(selection[0].row, 1));
}

...警报 11、2 或 12。

见小提琴-> http://jsfiddle.net/7e6Va/

于 2013-08-15T13:14:41.137 回答