Google Charts でjsonファイルの情報をグラフ表示する

Googleの公式ドキュメントを少し修正して、棒グラフにしてみました。
もう少し頑張って、mysqlのデータをグラフ出力する所までやってみたいと思ってます。

f:id:peanweb:20150830171028j:plain

■環境
XAMPP fow windows 1.7.4

■参考dotinstall.com

Populating Data Using Server-Side Code(Google公式)
Populating Data Using Server-Side Code  |  Charts  |  Google Developers

nvnote.com

■使用したファイル

sampleData.json

{
  "cols": [
        {"id":"","label":"","pattern":"","type":"string"},
        {"id":"","label":"売上","pattern":"","type":"number"}
      ],
  "rows": [
        {"c":[{"v":"2000","f":null},{"v":500,"f":null}]},
        {"c":[{"v":"2001","f":null},{"v":1000,"f":null}]},
        {"c":[{"v":"2002","f":null},{"v":1500,"f":null}]},
        {"c":[{"v":"2003","f":null},{"v":2000,"f":null}]},
      ]
}

getData.php

<?php 

// This is just an example of reading server side data and sending it to the client.
// It reads a json formatted text file and outputs it.

$string = file_get_contents("sampleData.json");
echo $string;

// Instead you can query your database and parse into JSON etc etc

?>


index.html

<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">

    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
      var jsonData = $.ajax({
          url: "getData.php",
          dataType:"json",
          async: false
          }).responseText;

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(jsonData);

      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(data, {width: 400, height: 240});
    }

    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>