与图表交互

到目前为止,我们已经介绍了很多网页的内容,即:您已经在网页上绘制了图表。不过,如果您希望捕获用户点击,或者需要操控图表中绘制的属性或数据,则需要监听该图表抛出的事件。

所有图表都会引发某些类型的事件。以下介绍的是其中最常见的几种原因:

  • ready - 图表在网页上绘制并准备好响应方法时抛出。如果您需要从图表请求信息,请监听此事件。
  • select - 用户选择图表中的某些内容(通常是通过点击条形图或饼图)时抛出。
  • error - 当图表无法呈现传入的数据时抛出,通常是因为 DataTable 格式有误。
  • onmouseoveronmouseout - 分别在用户将鼠标指针悬停在特定图表元素上或离开特定元素时抛出。

监听事件很简单:只需调用 google.visualization.events.addListener() 并传入图表的句柄、要捕获的事件的名称以及事件抛出时要调用的处理程序的名称。您可以使用任何图表句柄调用此方法,即使您尚未调用 draw()。请注意,如果您希望监听器在被移除之前仅调用一次,则可以调用 google.visualization.events.addOneTimeListener()

下面的部分代码段展示了如何注册以捕获图表的 select 事件:

load libraries...

function drawChart() {

  prepare data
...

 
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));

 
// The select handler. Call the chart's getSelection() method
 
function selectHandler() {
   
var selectedItem = chart.getSelection()[0];
   
if (selectedItem) {
     
var value = data.getValue(selectedItem.row, selectedItem.column);
      alert
('The user selected ' + value);
   
}
 
}

 
// Listen for the 'select' event, and call my function selectHandler() when
 
// the user selects something on the chart.
  google
.visualization.events.addListener(chart, 'select', selectHandler);

  draw the chart
...

}

下面显示了带有新选择事件监听器的 Hello Charts 代码示例。亲自尝试一下吧。

<html>
 
<head>
   
<!--Load the AJAX API-->
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">

     
// Load the Visualization API and the piechart package.
      google
.charts.load('current', {'packages':['corechart']});

     
// Set a callback to run when the Google Visualization API is loaded.
      google
.charts.setOnLoadCallback(drawChart);

     
// Callback that creates and populates a data table,
     
// instantiates the pie chart, passes in the data and
     
// draws it.
     
function drawChart() {

       
// Create the data table.
       
var data = new google.visualization.DataTable();
        data
.addColumn('string', 'Topping');
        data
.addColumn('number', 'Slices');
        data
.addRows([
         
['Mushrooms', 3],
         
['Onions', 1],
         
['Olives', 1],
         
['Zucchini', 1],
         
['Pepperoni', 2]
       
]);

       
// Set chart options
       
var options = {'title':'How Much Pizza I Ate Last Night',
                       
'width':400,
                       
'height':300};
 
       
// Instantiate and draw our chart, passing in some options.
       
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));

       
function selectHandler() {
         
var selectedItem = chart.getSelection()[0];
         
if (selectedItem) {
           
var topping = data.getValue(selectedItem.row, 0);
            alert
('The user selected ' + topping);
         
}
       
}

        google
.visualization.events.addListener(chart, 'select', selectHandler);    
        chart
.draw(data, options);
     
}

   
</script>
 
</head>
 
<body>
   
<!--Div that will hold the pie chart-->
   
<div id="chart_div" style="width:400; height:300"></div>
 
</body>
</html>

 

更多信息