Google Charts API: Data Visualization Guide
Google Charts API: Data Visualization Guide
Google Charts
Google Charts API
Chart Animations
What is Google Charts?
Google Charts is a free, web-based data visualization library
developed by Google. It enables developers to create
interactive, customizable, and responsive charts using
HTML5/SVG technology — with simple JavaScript.
Google Charts helps transform raw data into visually
appealing and easy-to-understand graphs, making it ideal
for use in dashboards, reports, or web apps.
Launched by: Google
Written in: JavaScript
Used for: Visualizing data in a web browser
Features of Google Charts:
Wide variety of chart types: line, bar, pie, scatter, area, geo charts,
gauges, timelines, and more.
Highly customizable: colors, fonts, labels, tooltips, legends,
animation.
Interactive: supports zoom, pan, hover effects, drill-downs.
Data integration: works with JSON, CSV, Google Sheets, or manual
data input.
Responsive: adapts to different screen sizes.
Cross-browser compatibility.
Advantages of Google Charts:
Free and Easy to Use
Wide Range of Chart Types
Highly Customizable
Interactive and Responsive
Easy Integration with Web Technologies
Cross-Browser Compatibility
No Installation Required
Regular Updates and Support
Steps required to create a chart using Google
Charts
<script src="[Link]
3. Load the Graph API, and add the function to run when the API is loaded:
<script>
[Link]('current',{packages:['corechart']});
[Link](drawChart);
// Your Function
function drawChart() {
...
}
</script>
Bar Charts
<!DOCTYPE html> ['Spain',44],
<html> ['USA',24],
<script ['Argentina',15]
src="[Link]
]);
</script>
// Set Options
<body>
const options = {
<div id="myChart" style="width:100%; max-
width:600px; height:500px;"></div> title:'World Wide Wine Production'
<script> };
[Link]('current', // Draw
{'packages':['corechart']});
const chart = new
[Link](drawChart); [Link](document.
function drawChart() { getElementById('myChart'));
// Set Data [Link](data, options);
const data = }
[Link](
</script>
[['Contry', 'Mhl'],
</body>
['Italy',55],
</html>
['France',49],
Pie chart:
<html> ['USA',23.9],
<script ['Argentina',14.5]
src="[Link] ]);
"></script> // Set Options
<body> const options = {
<div title:'World Wide Wine Production',
id="myChart" style="width:100%; max- is3D:true
width:600px; height:500px;"> };
</div> // Draw
<script> const chart = new
[Link]('current', [Link](document.
{'packages':['corechart']}); getElementById('myChart'));
[Link] [Link](data, options);
(drawChart); }
function drawChart() { </script>
// Set Data </body>
const data = </html>
[Link]([
['Contry', 'Mhl'],
['Italy',54.8],
['France',48.6],
['Spain',44.4],
Line Graph
<!DOCTYPE html> // Set Options
<html> const options = {
<script title: 'House Prices vs. Size',
src="[Link] hAxis: {title: 'Square Meters'},
"></script> vAxis: {title: 'Price in Millions'},
<body> legend: 'none'
<div id="myChart" style="width:100%; max- };
width:600px; height:500px;"></div> // Draw
<script> const chart = new
[Link]('current',{packages: [Link](document.
['corechart']}); getElementById('myChart'));
[Link](drawChart); [Link](data, options);
function drawChart() { }
// Set Data </script>
const data = </body>
[Link]([ </html>
['Price', 'Size'],
[50,7],[60,8],[70,8],[80,9],[90,9],
[100,9],[110,10],[120,11],
[130,14],[140,14],[150,15]]);
Scatter Chart:
<!DOCTYPE html> // Set Options
<html> const options = {
<script title: 'House Prices vs. Size',
src="[Link]
ript> hAxis: {title: 'Square Meters'},
vAxis: {title: 'Price in Millions'},
<body>
<div id="myChart" style="width:100%; max- legend: 'none'
width:600px; height:500px;"></div> };
<script> // Draw
[Link]('current',{packages:['corechart']}) const chart = new
; [Link](document.
[Link](drawChart); getElementById('myChart'));
function drawChart() { [Link](data, options);
// Set Data }
const data = [Link]([ </script>
['Price', 'Size'], </body>
[50,7],[60,8],[70,8],[80,9],[90,9], </html>
[100,9],[110,10],[120,11],
[130,14],[140,14],[150,15]]);
Candlestick Chart
<html> ['Fri', 68, 66, 22, 15]
<head> // Treat first row as data as well.
<title>Google Charts Tutorial</title> ], true);
<script type = "text/javascript" src = // Set chart options
"[Link] var options = {legend: 'none'};
</script> // Instantiate and draw the chart.
<script type = "text/javascript"> var chart = new
[Link]('current', {packages: [Link](
['corechart']}); [Link]('container'));
</script> [Link](data, options);
</head> }
<body> [Link](drawChart);
<div id = "container" style = "width: 550px; </script>
height: 400px; margin: 0 auto"> </body>
</div> </html>
<script language = "JavaScript">
function drawChart() {
// Define the chart to be drawn.
var data =
[Link]([
['Mon', 20, 28, 38, 45],
['Tue', 31, 38, 55, 66],
['Wed', 50, 55, 77, 80],
['Thu', 77, 77, 66, 50],
What is the Google Chart API ?
The Google Chart API is an extremely simple tool that lets you easily
create a chart from some data and embed it in a webpage. You embed
the data and formatting parameters in an HTTP request, and Google
returns a PNG image of the chart.
Many types of chart are supported, and by making the request into
an image tag you can simply include the chart in a webpage
Google Charts API can be used in the airline industry
The Google Charts API can be used in the airline industry to visualize various data points, providing
insights into operations, customer behavior, and market trends. While the Google Charts API itself is
free to use, accessing specific airline data might require integration with other APIs, some of which
may have associated costs or free tiers depending on usage.
Google Business Profile API: If you're a food business owner, you can use the Google
Business Profile API to access and update information like food menus and related
attributes (allergens, dietary restrictions). This data can then be visualized using Google
Charts.
Food Allergy and Allergen data: Resources like Food Allergy Training provide information
on food allergen menu matrices which can be converted to data for charting.
Choosing a chart type
The choice of chart depends on the specific aspect of the food industry you want
to analyze:
Pie charts/Donut charts: To visualize the breakdown of menu item categories,
ingredient proportions, or customer preferences. Market share of different
food brands
Line charts: To track trends over time, like food consumption patterns,
ingredient prices, or inventory levels.
1
• Enhanced User Experience
2
• Better Data Understanding
3
• Dynamic Data Visualization
4
• Professional Presentation
5
•Useful for Emphasis and Storytelling
Chart animations for the Covid-19 application
case study.
Conclusion:
Using an animated Line Chart in a Covid-19 case study allows
Better communication of time-based trends.
Improved user experience through interactivity.
Clearer visual impact when presenting important health data
Chart animations for the World population
analysis case study
Bar charts are ideal for comparing population data between
countries in a horizontal layout.
Using animated bar charts makes the trends more readable,
especially when dealing with large numbers and multi-
country comparisons over time.
Why Use Animated Bar Charts in Population Studies?
Bar charts clearly show which country has more or less
population.
Animations make the transition between years smoother and
more understandable.
Helps identify trends such as population growth or slowing
growth rates.
Bar Chart with Animation
<!DOCTYPE html> easing: 'out'
<html> },
]); <body>
title: 'World Population by Country (in millions)', <div id="population_bar_chart" style="width: 1000px;
height: 500px;"></div>
animation: {
</body>
startup: true,
</html>
duration: 2000,
Real-World Application:
Used by UN, World Bank, and demographic researchers.
Helps policymakers visualize population pressure and growth
challenges.
Useful in interactive reports, dashboards, and classroom
teaching.
Conclusion:
Animated bar charts in world population analysis allow for:
Clear comparison of country-wise population changes.
Easy understanding of trends over time.
Enhanced visual engagement with statistical data.
Questions:
Discuss the advantages of Google Chart.
Demonstrate Google chart API in Airline industry.
Demonstrate Google chart API in Food industry.
Demonstrate Google chart API in HealthCare industry.
Demonstrate chart animations for the Covid-19 application case study
Demonstrate chart animations for the World population analysis case
study
What are chart animations in Google Charts, and why are they used?
List and explain the steps required to create a chart using Google Charts.
Course Contents
[Link]:
Making Selections,
Changing Selection’s Attribute,
Loading and Filtering External Data,
Data Formats Usable with D3,
Creating a Server to Upload Data,
D3’s Function for Loading Data, Dealing with Asynchronous
Requests,
Loading and Formatting Large Data Sets
Making Charts Interactive and Animated:
Data Joins, Updates and Exits, Interactive Buttons,
Updating Charts, Adding Transactions, Using Keys
[Link]
D3 stands for Data-Driven Documents. It is an open-source JavaScript
library developed by Mike Bostock to create custom interactive data
visualizations in the web browser using SVG, HTML and CSS.
D3 focuses on data, so it is the most appropriate and specialized tool for data visualizations.
D3 is open-source. So you can work with the source code and add your own features.
It works with web standards so you don't need any other technology or plugin other than a
browser to make use of D3
D3 works with web standards like HTML, CSS and SVG, there is no new learning or
debugging tool required to work on D3.
D3 does not provide any specific feature, so it gives you complete control over your
visualization to customize it the way you want. This gives it an edge over other popular tools
like Tableau or QlikView.
Since D3 is lightweight, and works directly with web standards, it is extremely fast and works
well with large datasets.
It can create dynamic, real-time transformation by manipulating DOM elements, quickly into
data visualization without any latency.
It works on data and is specialized and appropriate with data visualization functions
contained in the JavaScript library.
Drawbacks of [Link]
Compatibility issues: [Link] may not fully support older browser
versions, leading to inconsistent rendering or functionality across
platforms.
Security challenges: [Link] lacks built-in mechanisms for data
encryption or access control, potentially exposing sensitive
information to unauthorized access.
Design limitations: Crafting visually appealing and innovative
graph designs can be challenging due to [Link]'s complex syntax
and structure.
Steep learning curve: [Link] requires a significant learning curve,
making it difficult for beginners to grasp advanced concepts and
techniques.
Performance overhead: Implementing complex visualizations with
[Link] can sometimes result in performance issues, especially on
resource-constrained devices or browsers.
Setting setup with D3
Download [Link] from
[Link]
Put the js file in the same directory, where you are creating your other
files and
OR
Use the [Link] from [Link]. Use the following in your html file
[{ "age": 42,
"name": "Ami", "location": "Singapore"
"age": 10, },
"location": "India" {
}, "name": "Viks",
{ "age": 40,
"name": "Eva", "location": "Germany"
"age": 12, }]
"location": "USA"
}
Load json data and print on web page
The [Link]() method returned a formatted data object. It also
returned an argument "error"
[Link]("/data/[Link]", function(error, data) {
The output we want to put on our page in body tag
[Link]("body")
We choose output data in <p> tag.
.selectAll("p")
We bind the data and pass to next line of code
.data(data)
enter() Function receives the values from data function, but we haven’t
referenced the <p> tag, it returns empty placeholder references to the
new elements.
.enter()
Load json data and print on web page
We now have the references to our elements. The append()
method adds these elements to the DOM.
append("p")
Most d3 functions accept functions as parameters. we have
passed an anonymous function to the text method that returns a
concatenation of name and location from our data object.
text() is called and bound to each of our page elements with the
corresponding data value.
.text(function(d) { return [Link] + ", " + [Link]; });
If we want to handle error, we can write,
if (error) {
return [Link](error);
}
D3 code to load and print json Data
<script type="text/javascript">
[Link]("[Link]", function(error, data) {
[Link]()
[Link]("body")
.selectAll("p")
.data(data)
.enter()
.append("p")
.text(function(d) {
return [Link] + "::: " + [Link];
});
});
</script>
JSON data output
Load Text Data:
text file
Ami <head>
Ash <script src=
Hetu "[Link]
Betu </script>
</head>
<body>
<script>
[Link]("[Link]", function (d) {
[Link](d);
});
</script>
</body>
Load Text Data: Output
Filtering the data
[Link] [Link]() Function is used to filter the data
The [Link]() function in [Link] is used to filter the
given selection and return a new selection for which the
filter is true. The filter to be used may be a string or a
function.
Syntax:
[Link](filter);
Parameters: This function accepts one parameter as
mentioned above and described below:
filter: It is a string or a function that would be used to filter a
selection. The filter is applied to each selected element
when using a function.
Return Values: This function returns the new selection
Filtering the data
<!DOCTYPE html>
<html lang="en">
<head>
<script src=
"[Link]
</script>
<script src=
"[Link]
</script>
</head>
</html>
Filtering the data: odd rows
<body> <div>
<b>1. This text is in bold</b>
<b>2. This text is also in bold</b>
<b>3. CKPCET</b>
<b>4. Computer Engineering</b>
<b>5. Data visualization</b>
</div><script>
let selection = [Link]("b")
.filter(":nth-child(odd)")
//[Link](":nth-child(even)")
.nodes();
[Link]((e) => {
[Link]([Link])})
</script></body></html>
Filtering the data:Output
Creating a server to upload your data
Typically with Apache or any other web server, data
directory is made.
In that the data, i.e. xml, csv, json, text files are put.
We here put in same directory as our scripts are
there.
If we put in the data directory of htdocs folder of
apache web server, we can access it as
[Link] or /data/[Link]
D3’s function for loading data
D3’s functions for Loading the data we have seen in for text,
csv, tsv, xml, json, html files.
Let’s see code for html files.
<head><script type="text/javascript"
src="[Link]
<body>
<script type="text/javascript">
[Link]("[Link]", function(data) {
[Link](data)});
</script>
D3’s function for loading html data
:output
Dealing with Asynchronous requests
<script>
Var popdata;
[Link]("[Link]", function (d) {
popdata=d;
});
[Link](popdata);
</script>
<script>
var popdata;
[Link]("[Link]", function (d) {
popdata=d;
[Link](popdata);
});
</script>
Loading Large Data Sets
<script>
data = [Link]("[Link]", function(d) {
[Link](d); });
</script>
The next three elements can be mapped to any elements and it can be
done using the data join's enter() and selector's append() method.
• The enter() method gives access to the remaining data (which is not
mapped to the existing elements) and
• the append() method is used to create a new element from the
corresponding data.
How Data Join Works?
The code to create new a li element is as follows −
[Link]("#list").selectAll("li")
.data([10, 20, 30, 25, 15])
.text(function(d) { return "This is pre-existing element and the value is "
+ d; })
.enter()
.append("li")
.text(function(d)
{ return "This is dynamically created element and the value is " + d;
});
How Data Join Works?
Data join provides another method called as the exit()
method to process the data items removed dynamically from the
data set
[Link]("li")
.data([10, 20, 30, 15])
.exit()
.remove()
Program
<html> .data([10, 20, 30, 25, 15])
<head> .text(function(d)
<script type = "text/javascript" src = { return "This is pre-existing element and
"[Link] the value is " + d; })
<style> .enter()
body { font-family: Arial; } .append("li")
</style> .text(function(d)
</head> { return "This is dynamically created
element and the value is " + d; });
<body>
function remove() {
<ul id = "list">
[Link]("li")
<li></li>
.data([10, 20, 30, 15])
<li></li>
.exit()
</ul>
.remove()
<input type = "button" name = "remove" value =
"Remove fourth value" }
onclick = "javascript:remove()" /> </script>
<script> </body>
[Link]("#list").selectAll("li") </html>
Output
Create SVG Elements using D3
SVG provides different shapes like lines, rectangles, circles,
ellipses etc.
Hence, designing visualizations with SVG gives you more
flexibility and power in what you can achieve.
What is SVG?
SVG is an image that is text-based.
SVG is similar in structure to HTML
SVG sits in the DOM
SVG properties can be specified as attributes
SVG should have absolute positions relative to the origin (0, 0)
Line in SVG
<svg width="500" height="500">
<line x1="100" y1="50" x2="500" y2="50" stroke="black"/>
</svg>
A line's attributes are:
x1: This is the x-coordinate of the first point
y1: This is the y-coordinate of the first point
x2: This is the x-coordinate of the second point
y2: This is the y-coordinate of the second point
Draw SVG Line with D3
<!doctype html> .attr("width", width)
<html> .attr("height", height);
<head> //Create line element inside SVG
<script
src="[Link] [Link]("line")
</script> .attr("x1", 100)
</head> .attr("x2", 500)
<body> .attr("y1", 50)
<script> .attr("y2", 50)
var width = 500; .attr("stroke", "black")
var height = 500; </script>
//Create SVG element </body>
var svg = [Link]("body") </html>
.append("svg")
SVG Rectangle
<!doctype html>
<html>
<head>
<script src="[Link]
</script>
</head>
<body>
<svg width="500" height="500">
<rect x="0" y="0" width="200" height="200"></rect>
</svg>
</body>
</html>
Draw SVG Rectangle with D3
<!doctype html> var svg = [Link]("body")
.append("svg")
<html>
.attr("width", width)
<head> .attr("height", height);
<script //Create and append rectangle
src="[Link] element
</script> [Link]("rect")
</head> .attr("x", 0)
.attr("y", 0)
<body> .attr("width", 200)
<script> .attr("height", 100)
var width = 500; </script>
var height = 500; </body
//Create SVG element </html>
SVG Circle
<!doctype html>
<html>
<body>
<svg width="500" height="500">
<circle cx="250" cy="50" r="50"/>
</svg>
</body>
</html>
Circle attributes:
• cx: This is the x-coordinate of the center of the circle
• cy: This is the y-coordinate of the center of the circle
• r: This denotes the radius of the circle
Draw SVG Circle with D3
<!doctype html> .attr("height", height);
<html> //Append circle
<head> [Link]("circle")
.attr("cx", 250)
<script
src="[Link] .attr("cy", 50)
</script> .attr("r", 50)
</head> </script>
<body> </body>
<script> </html>
var width = 500;
var height = 500;
//Create SVG element
var svg = [Link]("body")
.append("svg")
.attr("width", width)
SVG Ellipse
<!doctype html> ellipse's attributes are:
<html> • cx: This is the x-coordinate of
the center of the circle
<head>
• cy: This is the y-coordinate of
<script
the center of the circle
src="[Link]
• rx: This is the x radius of the
</script>
circle
</head>
• ry: This is the y radius of the
<body> circle
<svg width="500" height="500">
.outerRadius(80); });
.append("g") </body>
const data = [
{name: "A", value: 20},
Packed bubble chart
// Draw bubbles .attr("text-anchor", "middle")
[Link]("circle") .attr("dy", ".3em")
.data([Link]()) .text(d => [Link]);
.join("circle") </script>
.attr("cx", d => d.x) </body>
.attr("cy", d => d.y)
.attr("r", d => d.r)
.attr("fill", "orange")
.attr("opacity", 0.8);
// Add labels
[Link]("text")
.data([Link]())
.join("text")
.attr("x", d => d.x)
.attr("y", d => d.y)
Animated Bubble chart
<!DOCTYPE html> {name: "B", value: 40},
<meta charset="utf-8"> {name: "C", value: 10},
<body> {name: "D", value: 30}
<svg width="500" height="300"></svg> ];
<script
src="[Link]
// Create packed layout
pt>
const root = [Link]()
<script>
.size([width, height])
const svg = [Link]("svg"),
.padding(5)(
width = +[Link]("width"),
[Link]({children: data})
height = +[Link]("height");
.sum(d => [Link])
);
const data = [
{name: "A", value: 20},
Animated Bubble chart
// Draw animated bubbles .data([Link]())
[Link]("circle") .join("text")
.data([Link]()) .attr("x", d => d.x)
.join("circle") .attr("y", d => d.y)
.attr("cx", d => d.x) .attr("text-anchor", "middle")
.attr("cy", d => d.y) .attr("dy", ".3em")
.attr("r", 0) // start small .text(d => [Link])
.attr("fill", "orange") .style("opacity", 0)
.attr("opacity", 0.8) .transition()
.transition() .delay(500)
.duration(1000) // 1 second .duration(500)
animation
.style("opacity", 1);
.attr("r", d => d.r);
</script>
</body>
// Add labels
[Link]("text")
Questions:
What is the purpose of [Link]() and [Link]()? Explain with examples.
Explain D3’s selection mechanism and how it enables data-driven DOM
manipulation. Include examples.
How does the enter-update-exit pattern work with D3 selections?
Explain the concept of a Data Join in [Link]. Why is it considered essential for
dynamic data visualization? Describe how it works using the enter-update-exit
pattern, with a code example.
Explain applications of [Link] library
Explain [Link] library
Describe the process of making selections and changing their attributes in [Link]
with suitable code snippets.
Discuss different data formats supported by [Link] (CSV, JSON, TSV). Which one
would you prefer for hierarchical data and why?
Explain how external data can be loaded and filtered in [Link]. Provide a small
example.
How can you create a simple server to upload and serve data for use with [Link]?
Illustrate with steps.
Questions:
Explain asynchronous requests in D3 with an example. How does D3 handle
callback functions or promises?
Discuss techniques for loading and formatting large data sets in [Link]. Provide
an example scenario.
What do you mean by asynchronous requests in D3? Why are they important?
Explain the role of [Link]() in loading external data.
Briefly describe how D3 handles JSON data loading.
Explain how D3 allows updating existing charts with new data. How does the
use of keys improve this process?
Discuss how interactive buttons can be used to update and animate charts in
D3. Provide an example scenario.
Write a short note on changing selection attributes in [Link].