ลองใช้งาน Google Chart กัน

วันนี้ก็จะมาสำเสนอลองใช้งาน Google Chart กันครับ

ปัจจุบันก็มี Chart library ทั้งแบบฟรีและไม่ฟรีหลายตัวมากมาย เช่น Chart.js, D3.js (ตัวนี้อย่างเทพแต่ว่าก็ใช้ยากสุดๆ เหมือนกัน) แต่ผมได้เลือก Google Chart มาเพราะว่าอะไรหรอ? อย่างแรกเลย ง่ายครับ เรียนรู้ง่าย คู่มือละเอียด และก็เร็วครับ ปัญหาน้อย (อันนี้เทียบกับ Chart.js เลย)





เข้าไปที่หน้าเว็บ https://google-developers.appspot.com/chart/ ได้เลย

วิธีการติดตั้ง

1. ง่ายมาก เพิ่มเข้าไปที่ site

<!--Load the AJAX API-->

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

<script type="text/javascript">



  // Load the Visualization API library and the piechart library.

  google.load('visualization', '1.0', {'packages':['corechart']});

  google.setOnLoadCallback(drawChart);

     // ... draw the chart...

</script>


2. การใช้งาน จากขั้นตอนที่ 1 กำหนดชื่อ function ไว้ว่าง drawChart ก็กำทำการสร้าง function ตามนั้น

// Callback that creates and populates a data table,

      // instantiates the pie chart, passes in the data and

      // draws it.

      function drawChart() {



      // Create the data table.

      var data = new google.visualization.DataTable();

      data.addColumn('string', 'Topping');

      data.addColumn('number', 'Slices');

      data.addRows([

        ['Mushrooms', 3],

        ['Onions', 1],

        ['Olives', 1],

        ['Zucchini', 1],

        ['Pepperoni', 2]

      ]);



      // Set chart options

      var options = {'title':'How Much Pizza I Ate Last Night',

                     'width':400,

                     'height':300};



      // Instantiate and draw our chart, passing in some options.

      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));

      chart.draw(data, options);




3. ที่ body ให้เพิ่มดังนี้

<body>

<!--Div that will hold the pie chart-->

    <div id="chart_div" style="width:400; height:300"></div>

  </body>




4. Run! ก็แสดงผลเลยครับ ง่าย

ส่วนถ้าใครต้องการศึกษาเพิ่มเติ่มก็เข้าไปดูได้ที่หน้าเว็บเลยน่ะครับ เอกสารเขียนมาอย่างดีเข้าใจง่ายครับ

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

CakePHP ตอนที่ 1 Introduction and Installation

MIME Types – Complete List