28 Apr 2023

Integrating Google Charts into a website with HTML, CSS, and JavaScript

Google Charts is a powerful and versatile data visualization tool that can be easily integrated into any website. With its vast array of chart types and customization options, it's a great choice for displaying data in a clear and visually appealing way. In this blog, we will walk through the steps to integrate Google Charts into a website using HTML, CSS, and JavaScript.

Before we begin, it's important to note that Google Charts requires an internet connection to function. The charts are rendered using JavaScript, and the necessary scripts are loaded from Google's servers. This means that if the user is offline, the charts will not be displayed. With that in mind, let's get started.

Step 1: Create a new HTML file

The first step is to create a new HTML file and add the necessary code to include the Google Charts API. Start by opening a new text editor and creating a new file. Save the file with an appropriate name and with the extension .html. For example, you can name the file "chart.html".

Next, add the following code to the head section of the HTML file to include the Google Charts API:

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

This code loads the necessary scripts from Google's servers and prepares the environment for the charts to be displayed.

Step 2: Define a container for the chart

In order to display a chart, we need to create a container element in the HTML file. This element will be used to hold the chart and can be styled using CSS. Add the following code to the body section of the HTML file:

<div id="chart_div"></div>

This creates a new div element with an ID of "chart_div". This ID will be used to reference the container element in the JavaScript code.

Step 3: Create the chart using JavaScript

The next step is to create the chart using JavaScript. Add the following code to the bottom of the HTML file, just before the closing body tag:

<script type="text/javascript">
  // Load the Visualization API and the corechart package.
  google.charts.load('current', {'packages':['corechart']});

  // Set a callback to run when the Google Visualization API is loaded.
  google.charts.setOnLoadCallback(drawChart);

  // Callback that creates and populates a data table,
  // instantiates the chart, passes in the data and
  // draws it.
  function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Year');
    data.addColumn('number', 'Sales');
    data.addRows([
      ['2010', 1000],
      ['2011', 1170],
      ['2012', 660],
      ['2013', 1030]
    ]);

    // Set chart options
    var options = {'title':'Sales by Year',
                   'width':400,
                   'height':300};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>

This code defines a function called drawChart, which creates a new data table and sets the chart options. It then creates a new ColumnChart object and passes in the data and options. Finally, it uses the ID of the container element to render the chart inside it.

Step 4: Style the chart using CSS

The final step is to style the chart using CSS. Add the following code to the head section of the HTML file:

<style type="text/css">
  #chart_div {
    width: 400px;
    height: 300
  }
</style>

This code applies some basic styling to the container element. You can customize the width and height to fit your needs. You can also add additional CSS rules to customize the appearance of the chart itself, such as changing the colors or font styles.

Step 5: View the chart

That's it! Save the HTML file and open it in a web browser. You should see a column chart displayed in the container element you created. If you don't see the chart, make sure that you have an internet connection and that you have included the Google Charts API and all of the necessary scripts.

Conclusion

In this blog, we have walked through the steps to integrate Google Charts into a website using HTML, CSS, and JavaScript. With these tools, you can create a wide variety of charts and customize them to fit your needs. Whether you are visualizing sales data, website traffic, or any other type of information, Google Charts is a powerful and flexible tool that can help you present your data in a clear and engaging way.