Google Charts
Google Charts Introduction Getting Started Area Charts Bar Charts Bubble Charts Calendar Charts Candlestick Charts Column Charts Combination Chart Gauge Chart Geo Charts Histogram Charts Line Charts Maps Organization Chart Pie Charts Sankey Charts Scatter Charts Stepped Area Charts Table Chart Trendline Charts Wordtree Charts

Google Bar Chart

Bar charts are used to draw bar based charts.

A bar chart is rendered within the browser using SVG or VML.

Like all Google charts, bar charts display tooltips when the user hovers over the data.


Basic Bar Chart

The following example shows how to draw a basic bar chart, using google charts API:

For a vertical version of this chart, see the column chart.


Example Explained

First add a <div> element with the id "chart-div" and set width and height:

<!DOCTYPE html> <html lang="en-US"> <body> <div id="chart-div" style="width:100%; height:350px;"></div> </body> </html>

Then add a reference to the Chart API:

<script src="https://www.gstatic.com/charts/loader.js"></script>

Finally add a JavaScript function:

// Load the corechart package and set a callback google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); // Callback that creates and populates a data table function drawChart() { // Create the data table var data = google.visualization.arrayToDataTable([ ['City', '2019 Population'], ['Mumbai', 20185064], ['Delhi', 29399141], ['Chennai', 10711243], ['Jaipur', 3812262], ['Bengaluru', 11882666] ]); // Set chart options var options = { title: 'Population of Largest Indian Cities' }; // Display the chart inside the <div> element with id="chart-div" var chart = new google.visualization.BarChart(document.getElementById('chart-div')); chart.draw(data, options); }


Grouped Bar Chart

The following example shows how to create grouped bar chart:

var data = google.visualization.arrayToDataTable([ ['City', '2010 Population', '2019 Population'], ['Mumbai', 18257413, 20185064], ['Delhi', 21987895, 29399141], ['Chennai', 8506193, 10711243], ['Jaipur', 2991656, 3812262], ['Bengaluru', 8295564, 11882666] ]);
Run code

Stacked Bar Chart

A stacked bar chart is a bar chart that places related values atop one another.

You create a stacked bar chart by setting the isStacked option to true:

// Set chart options var options = { title: 'Population of Largest Indian Cities', chartArea: {width: '40%'}, hAxis: {title: 'Total Population', minValue: 0}, vAxis: {title: 'City'}, isStacked: true };
Run code

Creating Material Bar Chart

Material Bar Charts have many small improvements over Classic Bar Charts, including an improved color palette, rounded corners, clearer label formatting, tighter default spacing between series, softer gridlines and titles (and the addition of subtitles).

Creating a Material Bar Chart is similar to creating what we'll now call a "Classic" Bar Chart.

You load the Google Visualization API (although with the 'bar' package instead of the 'corechart' package):

google.charts.load('current', {'packages':['bar']});

And then create an object (but of class google.charts.Bar instead of google.visualization.BarChart):

var chart = new google.charts.Bar(document.getElementById('chart-div')); chart.draw(data, google.charts.Bar.convertOptions(options));
Run code

Material Charts will not work in old versions of Internet Explorer. (IE8 and earlier versions don't support SVG, which Material Charts require.)


Animated Bar Chart

Google charts can animate smoothly in one of two ways, either on startup when you first draw the chart, or when you redraw a chart after making a change in data or options.

To animate on startup:

  • Set your chart data and options. Be sure to set an animation duration and easing type
  • Set animation: {startup: true} — setting this in your options will cause your chart to start with series values drawn at the baseline, and animate out to their final state

Here is a simple example which animate smoothly on startup:

// Set chart options var options = { title: 'Population of Largest Indian Cities', animation: { duration: 2000, easing: 'out', startup: true } };
Run code

To animate a transition:

  • Start with an already rendered chart
  • Modify the data table or options
  • Specify the transition behavior using the animation option
  • Call chart.draw() on your chart to transition to the new values

Here is a simple example which changes the values presented in a chart upon each click on a button:

Click the button to animate chart:

Run code