English 中文(简体)
Configuration Syntax
  • 时间:2024-11-03

Google Charts - Configuration Syntax


Previous Page Next Page  

In this chapter we ll showcase the configuration required to draw a chart using Google Chart API.

Step 1: Create HTML Page

Create an HTML page with the Google Chart pbraries.

googlecharts_configuration.htm

<html>
   <head>
      <title>Google Charts Tutorial</title>
      <script type = "text/javascript" src = "https://www.gstatic.com/charts/loader.js">
      </script>
      <script type = "text/javascript">
         google.charts.load( current , {packages: [ corechart ]});     
      </script>
   </head>
   
   <body>
      <span id = "container" style = "width: 550px; height: 400px; margin: 0 auto">
      </span>
   </body>
</html>

Here container span is used to contain the chart drawn using Google Chart pbrary. Here we are loading the latest version of corecharts API using google.charts.load method.

Step 2: Create configurations

Google Chart pbrary uses very simple configurations using json syntax.

// Instantiate and draw the chart.
var chart = new google.visuapzation.PieChart(document.getElementById( container ));
chart.draw(data, options);

Here data represents the json data and options represents the configuration which Google Chart pbrary uses to draw a chart withing container span using draw() method. Now we ll configure the various parameter to create the required json string.

title

Configure the options of the chart.

// Set chart options
var options = { title : Browser market shares at a specific website, 2014 ,
    width :550,
    height :400};

DataTable

Configure the data to be displayed on the chart. DataTable is a special table structured collection which contains the data of the chart. Columns of data table represents the legends and rows represents the corresponding data. addColumn() method is used to add a column where first parameter represents the data type and second parameter represents the legend. addRows() method is used to add rows accordingly.

// Define the chart to be drawn.
var data = new google.visuapzation.DataTable();
data.addColumn( string ,  Browser );
data.addColumn( number ,  Percentage );
data.addRows([
   [ Firefox , 45.0],
   [ IE , 26.8],
   [ Chrome , 12.8],
   [ Safari , 8.5],
   [ Opera , 6.2],
   [ Others , 0.7]
]);

Step 3: Draw the chart

// Instantiate and draw the chart.
var chart = new google.visuapzation.PieChart(document.getElementById( container ));
chart.draw(data, options);

Example

Following is the complete example −

googlecharts_configuration.htm

<html>
   <head>
      <title>Google Charts Tutorial</title>
      <script type = "text/javascript" src = "https://www.gstatic.com/charts/loader.js">
      </script>
      <script type = "text/javascript">
         google.charts.load( current , {packages: [ corechart ]});     
      </script>
   </head>
   
   <body>
      <span id = "container" style = "width: 550px; height: 400px; margin: 0 auto">
      </span>
      <script language = "JavaScript">
         function drawChart() {
            // Define the chart to be drawn.
            var data = new google.visuapzation.DataTable();
            data.addColumn( string ,  Browser );
            data.addColumn( number ,  Percentage );
            data.addRows([
               [ Firefox , 45.0],
               [ IE , 26.8],
               [ Chrome , 12.8],
               [ Safari , 8.5],
               [ Opera , 6.2],
               [ Others , 0.7]
            ]);
               
            // Set chart options
            var options = { title : Browser market shares at a specific website, 2014 ,  width :550,  height :400};

            // Instantiate and draw the chart.
            var chart = new google.visuapzation.PieChart(document.getElementById ( container ));
            chart.draw(data, options);
         }
         google.charts.setOnLoadCallback(drawChart);
      </script>
   </body>
</html>

Following code call drawChart function to draws chart when Google Chart pbrary get loaded completely.

google.charts.setOnLoadCallback(drawChart);

Result

Verify the result.