Google Charts - 配置語法



本章將展示使用 Google Chart API 繪製圖表所需的配置。

步驟 1:建立 HTML 頁面

建立一個包含 Google Chart 庫的 HTML 頁面。

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>
      <div id = "container" style = "width: 550px; height: 400px; margin: 0 auto">
      </div>
   </body>
</html>

這裡使用container div 來包含使用 Google Chart 庫繪製的圖表。這裡我們使用 google.charts.load 方法載入最新版本的 corecharts API。

步驟 2:建立配置

Google Chart 庫使用非常簡單的 JSON 語法配置。

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

這裡 data 表示 JSON 資料,options 表示 Google Chart 庫用於在 container div 中使用 draw() 方法繪製圖表的配置。現在我們將配置各種引數以建立所需的 JSON 字串。

標題

配置圖表的選項。

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

資料表

配置要在圖表上顯示的資料。資料表是一個特殊的表格結構集合,包含圖表的 資料。資料表的列表示圖例,行表示相應的資料。addColumn() 方法用於新增列,其中第一個引數表示資料型別,第二個引數表示圖例。addRows() 方法用於相應地新增行。

// Define the chart to be drawn.
var data = new google.visualization.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]
]);

步驟 3:繪製圖表

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

示例

以下是完整的示例:

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>
      <div id = "container" style = "width: 550px; height: 400px; margin: 0 auto">
      </div>
      <script language = "JavaScript">
         function drawChart() {
            // Define the chart to be drawn.
            var data = new google.visualization.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.visualization.PieChart(document.getElementById ('container'));
            chart.draw(data, options);
         }
         google.charts.setOnLoadCallback(drawChart);
      </script>
   </body>
</html>

以下程式碼呼叫 drawChart 函式,在 Google Chart 庫完全載入後繪製圖表。

google.charts.setOnLoadCallback(drawChart);

結果

驗證結果。

廣告
© . All rights reserved.