【JS】 Google Charts [1] ~ 円グラフ編 ~

Google Charts

 * 商用利用
 * JavaScriptで記述する
  => グラフ自体はSVG形式(Scalable Vector Graphics)で記述される



■ サンプル

https://developers.google.com/chart/interactive/docs/quick_start
<!DOCTYPE html>
<html lang="jp">
<head>
<meta charset="utf-8">
<title>Demo for Google Charts</title>
</head>
<body>
<div id="targetChart">

</div>
<!--Load the AJAX API-->
<script type="text/javascript" src="">https://www.gstatic.com/charts/loader.js">
<script type="text/javascript">
(function() {
  'use strict';
  
  // 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 pie chart, passes in the data and draws it.
  function drawChart() {
    // Set chart options
    var options = {
      'title': 'Demo for Pie Chart',
      'width': 400,
      'height':300
    };
    
    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Language');
    data.addColumn('number', 'Votes');
    data.addRows([
      ['Java', 47],
      ['C', 21],
      ['PHP', 15],
      ['Ruby', 10],
      ['Python', 8]
    ]);
    
    // Instantiate and draw our chart, passing in some options.
    var target = document.getElementById('targetChart');
    var chart = new google.visualization.PieChart(target);
    chart.draw(data, options);
  }
})();
</script>
</body>
</html>

■ 補足

【1】オプション について

公式サイトの「Configuration options」を参照する
https://developers.google.com/chart/interactive/docs/gallery/piechart#configuration-options
// Set chart options
var options = {
  'title': 'Demo for Pie Chart',
  'width': 400,
  'height':300,
  backgroundColor: '#f8f8f8',
  colors: ['red', 'blue', 'yellow', 'green', 'gray'],
  // slices: 一部を切り出して目立たさせる
  //  + 第一引数:どこを切り出すか(0番目から。「2」「3」だと「PHP」「Ruby」)
  //  + offset:どの位で切り出すか
  slices: {2: {offset: 0.4}, 3: {offset: 0.2}},
  // pieHole: 0.4, // is3Dとは一緒に使えない
  is3D: true
};

【2】データを配列で追加

google.visualization.arrayToDataTableを使う
// Create the data table.
var data = new google.visualization.arrayToDataTable([
  ['Language', 'Votes'],
  ['Java', 47],
  ['C#', 37],
  ['C', 21],
  ['PHP', 15],
  ['Ruby', 10],
  ['Python', 8]
]);

【3】Googleスプレッドシートを使う

Googleスプレッドシート/データ (共有するためのURLを取得しておく)
Language	Votes
Java	67
C	21
PHP	15
Ruby	10
Python	8
サンプル
<!--Load the AJAX API-->
<script type="text/javascript" src="">https://www.gstatic.com/charts/loader.js">
<script type="text/javascript">
(function() {
  'use strict';
  
  // 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 pie chart, passes in the data and draws it.
  function drawChart() {
    var sharedUrl = 'https://docs.google.com/spreadsheets/x/xxxxxxxxxxxxxxxxxxxx_xxxxxx/edit?usp=sharing';
    var query = new google.visualization.Query(sharedUrl);
    query.send(handleQueryResponse);
  }
  
  function handleQueryResponse(response) {
    // Set chart options
    var options = {
      'title': 'Demo for Pie Chart',
      'width': 400,
      'height': 300
    };
    
    // Create the data table.
    var data = response.getDataTable();
    
    // Instantiate and draw our chart, passing in some options.
    var target = document.getElementById('targetChart');
    var chart = new google.visualization.PieChart(target);
    chart.draw(data, options);
  }
})();
</script>

関連記事

【JS】 Google Charts [2] ~ 棒グラフ編 ~

https://blogs.yahoo.co.jp/dk521123/37572794.html

【JS】 Google Charts [3] ~ 折れ線グラフ編 ~

https://blogs.yahoo.co.jp/dk521123/37572930.html

【HTML】タイムライン を表示するには...

https://blogs.yahoo.co.jp/dk521123/37562002.html