Stay organized with collections Save and categorize content based on your preferences.
What we've covered so far is sufficient for many web pages: you've drawn your chart on the page. However, if you want to catch user clicks, or need to manipulate properties or data in a chart that you've already drawn, you need to listen for events thrown by the chart.
All charts throw some kinds of events. Here are the most common:
DataTable
format is wrong.Listening for events is simple; simply call google.visualization.events.addListener()
passing in a handle to the chart, the name of the event to catch, and the name of a handler to call when the event is thrown. You can call this method with any chart handle, even if you haven't called draw()
yet. Note that you can call google.visualization.events.addOneTimeListener()
if you want the listener to be called exactly once before removing itself.
Here's a partial code snippet showing how to register to catch a chart's select event:
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... }
The following shows the Hello Charts code example with a new select event listener. Try it out yourself.
<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>
More Information
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["Google Charts throw events like `ready`, `select`, `error`, `onmouseover`, and `onmouseout` to enable user interaction and manipulation."],["To catch these events, use `google.visualization.events.addListener()` to register a handler function."],["The `select` event is commonly used to respond to user clicks on chart elements, enabling actions like displaying selected data."],["A provided code example demonstrates how to implement a `select` event listener to display an alert with the selected data."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4