Google Chart - 條形圖



以下是條形圖示例。 我們已經在 Google Chart 設定語法 章節中瞭解了用於繪製此圖的設定。所以讓我們看看完整的示例。

設定

我們使用 **Bar** 類來展示條形圖。

//classic chart
var chart = new google.visualization.BarChart(document.getElementById('container'));

//Material chart
var chart = new google.charts.Bar(document.getElementById('container'));

//set the bar to be horizontal using options
var options = {      
   bars: 'horizontal'	  
}; 

示例

googlecharts_bar_material.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','bar']});     
      </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 = google.visualization.arrayToDataTable([
               ['Year', 'Asia', 'Europe'],
               ['2012',  900,      390],
               ['2013',  1000,      400],
               ['2014',  1170,      440],
               ['2015',  1250,       480],
               ['2016',  1530,      540]
            ]);

            var options = {title: 'Population (in millions)', bars: 'horizontal'};  

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

結果

驗證結果。

googlecharts_bar_charts.htm
廣告
© . All rights reserved.