Questa pagina mostra come disegnare più grafici su una pagina web.
Disegnare più grafici su una pagina
Se vuoi disegnare più grafici su una pagina web, includi il codice per i seguenti elementi in <head>
della pagina:
- Carica tutti i pacchetti richiesti dai tuoi grafici in una singola chiamata a
google.charts.load()
. - Per ogni grafico nella pagina, aggiungi una chiamata al numero
google.charts.setOnLoadCallback()
con il callback che traccia il grafico come input, ad esempiogoogle.charts.setOnLoadCallback(myPieChart)
.
Ad esempio, supponi di voler disegnare due grafici a torta, che mostrano la pizza che i tuoi amici Sarah e Anthony hanno mangiato la scorsa notte. L'esempio che segue mostra come visualizzare entrambi i grafici affiancati.
<html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> // Load Charts and the corechart package. google.charts.load('current', {'packages':['corechart']}); // Draw the pie chart for Sarah's pizza when Charts is loaded. google.charts.setOnLoadCallback(drawSarahChart); // Draw the pie chart for the Anthony's pizza when Charts is loaded. google.charts.setOnLoadCallback(drawAnthonyChart); // Callback that draws the pie chart for Sarah's pizza. function drawSarahChart() { // Create the data table for Sarah's pizza. var data = new google.visualization.DataTable(); data.addColumn('string', 'Topping'); data.addColumn('number', 'Slices'); data.addRows([ ['Mushrooms', 1], ['Onions', 1], ['Olives', 2], ['Zucchini', 2], ['Pepperoni', 1] ]); // Set options for Sarah's pie chart. var options = {title:'How Much Pizza Sarah Ate Last Night', width:400, height:300}; // Instantiate and draw the chart for Sarah's pizza. var chart = new google.visualization.PieChart(document.getElementById('Sarah_chart_div')); chart.draw(data, options); } // Callback that draws the pie chart for Anthony's pizza. function drawAnthonyChart() { // Create the data table for Anthony's pizza. var data = new google.visualization.DataTable(); data.addColumn('string', 'Topping'); data.addColumn('number', 'Slices'); data.addRows([ ['Mushrooms', 2], ['Onions', 2], ['Olives', 2], ['Zucchini', 0], ['Pepperoni', 3] ]); // Set options for Anthony's pie chart. var options = {title:'How Much Pizza Anthony Ate Last Night', width:400, height:300}; // Instantiate and draw the chart for Anthony's pizza. var chart = new google.visualization.PieChart(document.getElementById('Anthony_chart_div')); chart.draw(data, options); } </script> </head> <body> <!--Table and divs that hold the pie charts--> <table class="columns"> <tr> <td><div id="Sarah_chart_div" style="border: 1px solid #ccc"></div></td> <td><div id="Anthony_chart_div" style="border: 1px solid #ccc"></div></td> </tr> </table> </body> </html>
Creare un singolo callback per disegnare più grafici
L'esempio precedente utilizza due callback per tracciare i grafici, perché i dati dei due grafici sono diversi. Se vuoi disegnare più grafici per gli stessi dati, potrebbe essere più pratico scrivere un singolo callback per entrambi i grafici. Nel seguente esempio viene illustrato questo.
<html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> // Load Charts and the corechart and barchart packages. google.charts.load('current', {'packages':['corechart']}); // Draw the pie chart and bar chart when Charts is loaded. google.charts.setOnLoadCallback(drawChart); function drawChart() { 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] ]); var piechart_options = {title:'Pie Chart: How Much Pizza I Ate Last Night', width:400, height:300}; var piechart = new google.visualization.PieChart(document.getElementById('piechart_div')); piechart.draw(data, piechart_options); var barchart_options = {title:'Barchart: How Much Pizza I Ate Last Night', width:400, height:300, legend: 'none'}; var barchart = new google.visualization.BarChart(document.getElementById('barchart_div')); barchart.draw(data, barchart_options); } </script> </head> <body> <!--Table and divs that hold the pie charts--> <table class="columns"> <tr> <td><div id="piechart_div" style="border: 1px solid #ccc"></div></td> <td><div id="barchart_div" style="border: 1px solid #ccc"></div></td> </tr> </table> </body> </html>