我正在尝试处理图表的选择事件,我想知道选择了哪一行/列,但是调用的函数出现错误:
未捕获的参考错误:未定义图表。
该代码与饼图示例和选择事件处理程序中的代码非常相似。我希望我需要让图表对象在全球范围内广为人知,但不知道该怎么做。
<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>