Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Graphics Google Chart

JS Graphics Google Chart

From simple line charts to complex hierarchical tree maps, the Google Chart gallery provides a large number of ready-to-use chart types

Installation

Google Chart provides an easy and straight forward method to install and use Google Charts.

Visit Google Chart website to learn more.

Basic Library Loading

Add the following link to <head> tag.

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

You can use <div> for this.

					 
        
          <div id="myChart"></div>
        
      
Load Graph API

Load the Graph API, and add the function to run when the API is loaded.

					 
        
          <script>
            google.charts.load('current',{packages:['corechart']});
            google.charts.setOnLoadCallback(drawChart);

            // Your Function
            function drawChart() {
              // Chart related code goes here
            }
          </script>
        
      
Chart Types
  • Area Charts
  • Bar Charts
  • Line Charts
  • Scatter Charts
  • Pie Charts
  • 3D Charts
Area Charts
					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="https://www.gstatic.com/charts/loader.js"></script>
              <script>
                google.charts.load('current', {'packages':['corechart']});
                google.charts.setOnLoadCallback(drawChart);

                function drawChart() {
                  var data = google.visualization.arrayToDataTable([
                    ['Year', 'Sales', 'Expenses'],
                    ['2013', 1000, 400],
                    ['2014', 1170, 460],
                    ['2015', 660, 1120],
                    ['2016', 1030, 540]
                  ]);
                  var options = {
                    title: 'Company Performance',
                    hAxis: {title: 'Year',  titleTextStyle: {color: '#333'}},
                    vAxis: {minValue: 0}
                  };
                  var chart = new google.visualization.AreaChart(document.getElementById('myChart'));
                  chart.draw(data, options);
                }
              </script>
            </head>
            <body>
              <div id="myChart" style="width: 100%; height: 500px;"></div>
            </body>
          </html>
        
      
Bar Charts
					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="https://www.gstatic.com/charts/loader.js"></script>
              <script>
                google.charts.load('current', {'packages':['bar']});
                google.charts.setOnLoadCallback(drawChart);

                function drawChart() {
                  var data = google.visualization.arrayToDataTable([
                    ['Opening Move', 'Percentage'],
                    ["King's pawn (e4)", 44],
                    ["Queen's pawn (d4)", 31],
                    ["Knight to King 3 (Nf3)", 12],
                    ["Queen's bishop pawn (c4)", 10],
                    ['Other', 3]
                  ]);
                  var options = {
                    title: 'Chess opening moves',
                    width: 900,
                    legend: { position: 'none' },
                    chart: {
                      title: 'Chess opening moves',
                      subtitle: 'popularity by percentage'
                    },
                    bars: 'horizontal',
                    axes: {
                      x: {
                        0: { side: 'top', label: 'Percentage'}
                      }
                    },
                    bar: {groupWidth: "90%"}
                  };
                  var chart = new google.charts.Bar(document.getElementById('myChart'));
                  chart.draw(data, options);
                }
              </script>
            </head>
            <body>
              <div id="myChart" style="width: 100%; height: 500px;"></div>
            </body>
          </html>
        
      
Line Charts
					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="https://www.gstatic.com/charts/loader.js"></script>
              <script>
                google.charts.load('current', {'packages':['corechart']});
                google.charts.setOnLoadCallback(drawChart);

                function drawChart() {
                  var data = google.visualization.arrayToDataTable([
                    ['Year', 'Sales', 'Expenses'],
                    ['2004', 1000, 400],
                    ['2005', 1170, 460],
                    ['2006', 660,  1120],
                    ['2007', 1030, 540]
                  ]);
                  var options = {
                    title: 'Company Performance',
                    curveType: 'function',
                    legend: { position: 'bottom' }
                  };
                  var chart = new google.visualization.LineChart(document.getElementById('myChart'));
                  chart.draw(data, options);
                }
              </script>
            </head>
            <body>
              <div id="myChart" style="width: 100%; height: 500px;"></div>
            </body>
          </html>
        
      
Scatter Charts
					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="https://www.gstatic.com/charts/loader.js"></script>
              <script>
                google.charts.load('current', {'packages':['corechart']});
                google.charts.setOnLoadCallback(drawChart);

                function drawChart() {
                  var data = google.visualization.arrayToDataTable([
                    ['Age', 'Weight'],
                    [8, 12],
                    [4, 5.5],
                    [11, 14],
                    [4, 5],
                    [3, 3.5],
                    [6.5, 7]
                  ]);
                  var options = {
                    title: 'Age vs. Weight comparison',
                    hAxis: {title: 'Age', minValue: 0, maxValue: 15},
                    vAxis: {title: 'Weight', minValue: 0, maxValue: 15},
                    legend: 'none'
                  };
                  var chart = new google.visualization.ScatterChart(document.getElementById('myChart'));
                  chart.draw(data, options);
                }
              </script>
            </head>
            <body>
              <div id="myChart" style="width: 100%; height: 500px;"></div>
            </body>
          </html>
        
      
Pie Charts
					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="https://www.gstatic.com/charts/loader.js"></script>
              <script>
                google.charts.load('current', {'packages':['corechart']});
                google.charts.setOnLoadCallback(drawChart);

                function drawChart() {
                  var data = google.visualization.arrayToDataTable([
                    ['Task', 'Hours per Day'],
                    ['Work', 11],
                    ['Eat',  2],
                    ['Commute', 2],
                    ['Watch TV', 2],
                    ['Sleep', 7]
                  ]);
                  var options = {
                    title: 'My Daily Activities'
                  };
                  var chart = new google.visualization.PieChart(document.getElementById('myChart'));
                  chart.draw(data, options);
                }
              </script>
            </head>
            <body>
              <div id="myChart" style="width: 100%; height: 500px;"></div>
            </body>
          </html>
        
      
3D Charts
					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="https://www.gstatic.com/charts/loader.js"></script>
              <script>
                google.charts.load('current', {'packages':['corechart']});
                google.charts.setOnLoadCallback(drawChart);

                function drawChart() {
                  var data = google.visualization.arrayToDataTable([
                    ['Task', 'Hours per Day'],
                    ['Work', 11],
                    ['Eat',  2],
                    ['Commute', 2],
                    ['Watch TV', 2],
                    ['Sleep', 7]
                  ]);
                  var options = {
                    title: 'My Daily Activities',
                    is3D: true,
                  };
                  var chart = new google.visualization.PieChart(document.getElementById('myChart'));
                  chart.draw(data, options);
                }
              </script>
            </head>
            <body>
              <div id="myChart" style="width: 100%; height: 500px;"></div>
            </body>
          </html>
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved