Stay organized with collections
Save and categorize content based on your preferences.
This page shows how to draw multiple charts on one web page.
Draw Multiple Charts on One Page
If you want to draw multiple charts on one web page, include code for the following
in the <head> of the page:
Load all the packages required by your charts in a single call to
google.charts.load().
For each chart on the page, add a call to google.charts.setOnLoadCallback()
with the callback that draws the chart as an input - for example,
google.charts.setOnLoadCallback(myPieChart).
For example, suppose you want to draw two pie charts,
showing how much pizza your friends Sarah and Anthony ate last night.
The following example shows how to display both charts side-by-side.
<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>
Using a Single Callback to Draw Multiple Charts
The previous example uses two callbacks to draw the charts, because the data
for the two charts are different. If you
want to draw multiple charts for the same data, it may be
more convenient to write a single callback for both charts. The following example
illustrates this.
<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>
[null,null,["Last updated 2024-07-10 UTC."],[],[],null,["# Draw Multiple Charts\n\nThis page shows how to draw multiple charts on one web page.\n\nDraw Multiple Charts on One Page\n--------------------------------\n\nIf you want to draw multiple charts on one web page, include code for the following\nin the `\u003chead\u003e` of the page:\n\n- Load all the packages required by your charts in a single call to `google.charts.load()`.\n- For each chart on the page, add a call to `google.charts.setOnLoadCallback()` with the callback that draws the chart as an input - for example, `google.charts.setOnLoadCallback(myPieChart)`.\n\nFor example, suppose you want to draw two pie charts,\nshowing how much pizza your friends Sarah and Anthony ate last night.\nThe following example shows how to display both charts side-by-side.\n\n|---|---|\n| | |\n\n\n\\\u003cscript type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"\\\u003e\\\u003c/script\\\u003e \\\u003c!--Table and divs that hold the pie charts--\\\u003e \\\u003ctable class=\"columns\"\\\u003e \\\u003ctr\\\u003e \\\u003ctd\\\u003e\\\u003cdiv id=\"Sarah_chart_div\" style=\"border: 1px solid #ccc\"\\\u003e\\\u003c/div\\\u003e\\\u003c/td\\\u003e \\\u003ctd\\\u003e\\\u003cdiv id=\"Anthony_chart_div\" style=\"border: 1px solid #ccc\"\\\u003e\\\u003c/div\\\u003e\\\u003c/td\\\u003e \\\u003c/tr\\\u003e \\\u003c/table\\\u003e // 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); }\n\n\u003cbr /\u003e\n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003cscript type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"\u003e\u003c/script\u003e\n \u003cscript type=\"text/javascript\"\u003e\n\n // Load Charts and the corechart package.\n google.charts.load('current', {'packages':['corechart']});\n\n // Draw the pie chart for Sarah's pizza when Charts is loaded.\n google.charts.setOnLoadCallback(drawSarahChart);\n\n // Draw the pie chart for the Anthony's pizza when Charts is loaded.\n google.charts.setOnLoadCallback(drawAnthonyChart);\n\n // Callback that draws the pie chart for Sarah's pizza.\n function drawSarahChart() {\n\n // Create the data table for Sarah's pizza.\n var data = new google.visualization.DataTable();\n data.addColumn('string', 'Topping');\n data.addColumn('number', 'Slices');\n data.addRows([\n ['Mushrooms', 1],\n ['Onions', 1],\n ['Olives', 2],\n ['Zucchini', 2],\n ['Pepperoni', 1]\n ]);\n\n // Set options for Sarah's pie chart.\n var options = {title:'How Much Pizza Sarah Ate Last Night',\n width:400,\n height:300};\n\n // Instantiate and draw the chart for Sarah's pizza.\n var chart = new google.visualization.PieChart(document.getElementById('Sarah_chart_div'));\n chart.draw(data, options);\n }\n\n // Callback that draws the pie chart for Anthony's pizza.\n function drawAnthonyChart() {\n\n // Create the data table for Anthony's pizza.\n var data = new google.visualization.DataTable();\n data.addColumn('string', 'Topping');\n data.addColumn('number', 'Slices');\n data.addRows([\n ['Mushrooms', 2],\n ['Onions', 2],\n ['Olives', 2],\n ['Zucchini', 0],\n ['Pepperoni', 3]\n ]);\n\n // Set options for Anthony's pie chart.\n var options = {title:'How Much Pizza Anthony Ate Last Night',\n width:400,\n height:300};\n\n // Instantiate and draw the chart for Anthony's pizza.\n var chart = new google.visualization.PieChart(document.getElementById('Anthony_chart_div'));\n chart.draw(data, options);\n }\n \u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003c!--Table and divs that hold the pie charts--\u003e\n \u003ctable class=\"columns\"\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cdiv id=\"Sarah_chart_div\" style=\"border: 1px solid #ccc\"\u003e\u003c/div\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cdiv id=\"Anthony_chart_div\" style=\"border: 1px solid #ccc\"\u003e\u003c/div\u003e\u003c/td\u003e\n \u003c/tr\u003e\n \u003c/table\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\nUsing a Single Callback to Draw Multiple Charts\n-----------------------------------------------\n\nThe previous example uses two callbacks to draw the charts, because the data\nfor the two charts are different. If you\nwant to draw multiple charts for the same data, it may be\nmore convenient to write a single callback for both charts. The following example\nillustrates this.\n\n|---|---|\n| | |\n\n\n\\\u003cscript type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"\\\u003e\\\u003c/script\\\u003e \\\u003c!--Table and divs that hold the pie charts--\\\u003e \\\u003ctable class=\"columns\"\\\u003e \\\u003ctr\\\u003e \\\u003ctd\\\u003e\\\u003cdiv id=\"piechart_div\" style=\"border: 1px solid #ccc\"\\\u003e\\\u003c/div\\\u003e\\\u003c/td\\\u003e \\\u003ctd\\\u003e\\\u003cdiv id=\"barchart_div\" style=\"border: 1px solid #ccc\"\\\u003e\\\u003c/div\\\u003e\\\u003c/td\\\u003e \\\u003c/tr\\\u003e \\\u003c/table\\\u003e // 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); }\n\n\u003cbr /\u003e\n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003cscript type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"\u003e\u003c/script\u003e\n \u003cscript type=\"text/javascript\"\u003e\n // Load Charts and the corechart and barchart packages.\n google.charts.load('current', {'packages':['corechart']});\n\n // Draw the pie chart and bar chart when Charts is loaded.\n google.charts.setOnLoadCallback(drawChart);\n\n function drawChart() {\n\n var data = new google.visualization.DataTable();\n data.addColumn('string', 'Topping');\n data.addColumn('number', 'Slices');\n data.addRows([\n ['Mushrooms', 3],\n ['Onions', 1],\n ['Olives', 1],\n ['Zucchini', 1],\n ['Pepperoni', 2]\n ]);\n\n var piechart_options = {title:'Pie Chart: How Much Pizza I Ate Last Night',\n width:400,\n height:300};\n var piechart = new google.visualization.PieChart(document.getElementById('piechart_div'));\n piechart.draw(data, piechart_options);\n\n var barchart_options = {title:'Barchart: How Much Pizza I Ate Last Night',\n width:400,\n height:300,\n legend: 'none'};\n var barchart = new google.visualization.BarChart(document.getElementById('barchart_div'));\n barchart.draw(data, barchart_options);\n }\n \u003c/script\u003e\n \u003c/head\u003e\n\u003cbody\u003e\n \u003c!--Table and divs that hold the pie charts--\u003e\n \u003ctable class=\"columns\"\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cdiv id=\"piechart_div\" style=\"border: 1px solid #ccc\"\u003e\u003c/div\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cdiv id=\"barchart_div\" style=\"border: 1px solid #ccc\"\u003e\u003c/div\u003e\u003c/td\u003e\n \u003c/tr\u003e\n \u003c/table\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\n[**Next: *Adding Interactivity***](/chart/interactive/docs/basic_interactivity)"]]