You are currently viewing Beginner’s Guide: Learn Google Charts with Code Examples

Beginner’s Guide: Learn Google Charts with Code Examples

Introduction to Google Charts

Google Charts is a powerful tool for creating interactive and customizable charts and graphs to visualize data on your website or web application. In this tutorial, we’ll walk through the basics of getting started with Google Charts, including how to set up your environment and create your first chart.

Setting Up Google Charts

Before we dive into creating charts, let’s make sure we have everything set up:

  1. Include Google Charts Library: First, include the Google Charts library in your HTML file. You can do this by adding the following script tag to the <head> section of your HTML file. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  2. Load the Visualization Library: After including the Google Charts library, load the visualization library using the google.charts.load method. <script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); </script>

Creating Your First Chart

Now that we have everything set up, let’s create our first chart. We’ll start with a simple bar chart.

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  <script type="text/javascript">
    google.charts.load('current', {'packages':['corechart']});
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {
      var data = google.visualization.arrayToDataTable([
        ['Year', 'Sales', 'Expenses'],
        ['2014', 1000, 400],
        ['2015', 1170, 460],
        ['2016', 660, 1120],
        ['2017', 1030, 540]
      ]);

      var options = {
        title: 'Company Performance',
        curveType: 'function',
        legend: { position: 'bottom' }
      };

      var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

      chart.draw(data, options);
    }
  </script>
</head>
<body>
  <div id="curve_chart" style="width: 900px; height: 500px"></div>
</body>
</html>

Understanding the Code

Let’s break down the code we just wrote:

  • We start by loading the Google Charts library and specifying the visualization package we want to use (corechart).
  • Next, we define a function drawChart() where we create our chart.
  • Inside the drawChart() function, we create a data table using the google.visualization.arrayToDataTable() method. This method converts a two-dimensional array into a DataTable object.
  • We define options for our chart, such as the chart title and curve type.
  • Finally, we create a new instance of the chart (in this case, a LineChart) and draw it on the page.

Conclusion

Congratulations! You’ve successfully created your first Google Chart. This is just the beginning of what you can do with Google Charts. Experiment with different chart types, customization options, and data sources to create compelling visualizations for your projects. Happy charting!

Leave a Reply