วาดแผนภูมิหลายรายการ

หน้านี้แสดงวิธีวาดแผนภูมิหลายรายการในหน้าเว็บเดียว

วาดหลายแผนภูมิในหน้าเดียว

หากต้องการวาดแผนภูมิหลายแผนภูมิในหน้าเว็บเดียว ให้ใส่โค้ดสำหรับแผนภูมิต่อไปนี้ใน <head> ของหน้า

  • โหลดแพ็กเกจทั้งหมดที่แผนภูมิจำเป็นต้องใช้ในการเรียก google.charts.load() เพียงครั้งเดียว
  • สำหรับแผนภูมิแต่ละรายการในหน้าเว็บ ให้เพิ่มการเรียกไปยัง google.charts.setOnLoadCallback() ด้วย Callback ที่ดึงแผนภูมิเป็นอินพุต เช่น google.charts.setOnLoadCallback(myPieChart)

ตัวอย่างเช่น สมมติว่าคุณต้องการวาดแผนภูมิวงกลม 2 แผนภูมิ ซึ่งแสดงปริมาณพิซซ่าที่เพื่อนๆ ซาร่าห์และแอนโธนี่กินเมื่อคืน ตัวอย่างต่อไปนี้แสดงวิธีแสดงแผนภูมิทั้ง 2 รายการแสดงคู่กัน

<html>
 
<head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">

     
// Load Charts and the corechart package.
      google
.charts.load('current', {'packages':['corechart']});

     
// Draw the pie chart for Sarah's pizza when Charts is loaded.
      google
.charts.setOnLoadCallback(drawSarahChart);

     
// Draw the pie chart for the Anthony's pizza when Charts is loaded.
      google
.charts.setOnLoadCallback(drawAnthonyChart);

     
// Callback that draws the pie chart for Sarah's pizza.
     
function drawSarahChart() {

       
// Create the data table for Sarah's pizza.
       
var data = new google.visualization.DataTable();
        data
.addColumn('string', 'Topping');
        data
.addColumn('number', 'Slices');
        data
.addRows([
         
['Mushrooms', 1],
         
['Onions', 1],
         
['Olives', 2],
         
['Zucchini', 2],
         
['Pepperoni', 1]
       
]);

       
// Set options for Sarah's pie chart.
       
var options = {title:'How Much Pizza Sarah Ate Last Night',
                       width
:400,
                       height
:300};

       
// Instantiate and draw the chart for Sarah's pizza.
       
var chart = new google.visualization.PieChart(document.getElementById('Sarah_chart_div'));
        chart
.draw(data, options);
     
}

     
// Callback that draws the pie chart for Anthony's pizza.
     
function drawAnthonyChart() {

       
// Create the data table for Anthony's pizza.
       
var data = new google.visualization.DataTable();
        data
.addColumn('string', 'Topping');
        data
.addColumn('number', 'Slices');
        data
.addRows([
         
['Mushrooms', 2],
         
['Onions', 2],
         
['Olives', 2],
         
['Zucchini', 0],
         
['Pepperoni', 3]
       
]);

       
// Set options for Anthony's pie chart.
       
var options = {title:'How Much Pizza Anthony Ate Last Night',
                       width
:400,
                       height
:300};

       
// Instantiate and draw the chart for Anthony's pizza.
       
var chart = new google.visualization.PieChart(document.getElementById('Anthony_chart_div'));
        chart
.draw(data, options);
     
}
   
</script>
 
</head>
 
<body>
   
<!--Table and divs that hold the pie charts-->
   
<table class="columns">
     
<tr>
       
<td><div id="Sarah_chart_div" style="border: 1px solid #ccc"></div></td>
       
<td><div id="Anthony_chart_div" style="border: 1px solid #ccc"></div></td>
     
</tr>
   
</table>
 
</body>
</html>


การใช้ Callback เดียวเพื่อวาดหลายแผนภูมิ

ตัวอย่างก่อนหน้านี้ใช้ Callback 2 รายการเพื่อวาดแผนภูมิ เนื่องจากข้อมูลสำหรับทั้ง 2 แผนภูมิแตกต่างกัน หากต้องการวาดแผนภูมิหลายรายการสำหรับข้อมูลเดียวกัน การเขียน Callback เดียวสําหรับทั้ง 2 แผนภูมิจะสะดวกกว่า ตัวอย่างต่อไปนี้แสดงให้เห็นภาพดังกล่าว

<html>
 
<head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
     
// Load Charts and the corechart and barchart packages.
      google
.charts.load('current', {'packages':['corechart']});

     
// Draw the pie chart and bar chart when Charts is loaded.
      google
.charts.setOnLoadCallback(drawChart);

     
function drawChart() {

       
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]
       
]);

       
var piechart_options = {title:'Pie Chart: How Much Pizza I Ate Last Night',
                       width
:400,
                       height
:300};
       
var piechart = new google.visualization.PieChart(document.getElementById('piechart_div'));
        piechart
.draw(data, piechart_options);

       
var barchart_options = {title:'Barchart: How Much Pizza I Ate Last Night',
                       width
:400,
                       height
:300,
                       legend
: 'none'};
       
var barchart = new google.visualization.BarChart(document.getElementById('barchart_div'));
        barchart
.draw(data, barchart_options);
     
}
   
</script>
 
</head>
<body>
   
<!--Table and divs that hold the pie charts-->
   
<table class="columns">
     
<tr>
       
<td><div id="piechart_div" style="border: 1px solid #ccc"></div></td>
       
<td><div id="barchart_div" style="border: 1px solid #ccc"></div></td>
     
</tr>
   
</table>
 
</body>
</html>