Loading lesson path
Concept visual
Start from A
From simple line charts to complex hierarchical tree maps, the Google Chart gallery provides a large number of ready-to-use chart types:
Formula
Bar / Column ChartFormula
Map / Geo ChartTo use Google Chart in your web page, add a link to the charts loader:
Formula
< script src ="https://www.gstatic.com/charts/loader.js"></script> Google Chart is easy to use.
<div> element to display the chart:
<div id="myChart" style="max-width:700px; height:400px"></div>Formula
The < div > element must have a unique id.
Then load the Google Graph API:Load the Visualization API and the corechart package Set a callback function to call when the API is loaded
1 google.charts.load('current',{packages:['corechart']});
2 google.charts.setOnLoadCallback(drawChart);
That's all!Source Code function drawChart() {
// Set Data const data = google.visualization.arrayToDataTable([
['Price', 'Size'],
[50,7],[60,8],[70,8],[80,9],[90,9],[100,9],
[110,10],[120,11],[130,14],[140,14],[150,15]
]);
// Set Options const options = {
title: 'House Prices vs Size', hAxis: {title: 'Square Meters'}, vAxis: {title: 'Price in Millions'}, legend: 'none'
};
// Draw Chart const chart = new google.visualization.LineChart(document.getElementById('myChart'));
chart.draw(data, options);
}To scatter plot the same data, change google.visualization to ScatterChart:
const chart = new google.visualization.LineChart(document.getElementById('myChart'));Source Code function drawChart() {
const data = google.visualization.arrayToDataTable([
['Contry', 'Mhl'],
['Italy', 55],
['France', 49],
['Spain', 44],
['USA', 24],
['Argentina', 15]
]);
const options = {
title: 'World Wide Wine Production'
};
const chart = new google.visualization.BarChart(document.getElementById('myChart'));
chart.draw(data, options);
}Pie chart, just replace: google.visualization.
google.visualization.
PieChart const chart = new google.visualization.(document.getElementById('myChart'));3D Pie To display the Pie in 3D, just add is3D: true to the options:
const options = {
title: 'World Wide Wine Production', is3D: true
};