 |
รบกวนพี่ๆ ช่วยสร้าง Function กราฟ ให้หนอ่ยครับ มีตัวอย่างให้ดูด้านไหนน่ะครับ คิดไม่ออกแหะๆ |
|
 |
|
|
 |
 |
|
Code (JavaScript)
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// 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');
$.post('check_query/check_graph_hotel.php',function(res){
var txt = res.split(",");
var count = txt.length;
for(x=0;x<count;x++){
var txt2=txt[x].split("%");
alert(txt2[0]);
alert(txt2[1]);
}
});
data.addRows([
['Bangkok 300', 300],
['Pattaya 100', 100],
['Chaing Mai 120', 120],
['Krabi 80', 80],
['Phuket 62', 62],
['P 62', 62]
]);
// Set chart options
var options = {'title':'How much Hotels on this period',
'width':800,
'height':600};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
พี่ๆครับ จาก code ด้านบน ผมต้องการสร้าง ข้อมูลสำหรับ plot กราฟ ด้วยฐานข้อมูลที่ผมเตรียมไว้น่ะครับ
ประเด็นคือผมไม่รู้ว่าจะใส่ข้อมูลเข้าไปใน function addRows อย่างไรดีอ่ะครับ
โดยตัวแปร res ที่ได้จะมีข้อมูลดังนี้ 1%Bangkok,2%Phuket,4%Krabi,10%Rayong อ่ะครับ
Tag : PHP, JavaScript, jQuery
|
ประวัติการแก้ไข 2014-11-25 11:12:50
|
 |
 |
 |
 |
Date :
2014-11-25 10:05:19 |
By :
nut_ch31 |
View :
808 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
for(x=0;x<count;x++){
var txt2=txt[x].split("%");
alert(txt2[0]);
alert(txt2[1]);
}
ตรงชุดคำสั่งนี้คือ ผมได้ตัวแปรที่ต้องการแล้วน่ะครับ เหลือ apply กับฟังชั่น addrow อย่างไรดี ผมเอาใส่ไม่ถูก Y Y
|
 |
 |
 |
 |
Date :
2014-11-25 11:22:28 |
By :
nut_ch31 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมยังไม่เข้าใจว่าต้องการอะไร คือคุณต้องการเอาข้อมูลในฐานมูลมาแสดงออกเป็นกราฟ ไช่หรือเปล่าครับ
|
 |
 |
 |
 |
Date :
2014-11-25 14:41:07 |
By :
LAGO |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แต่ถ้าจะเรียกใช้กับฐานข้อมูล ตัวอย่างนี้อาจจะพอช่วยได้นะครับ
Code (PHP)
<?
$studentsSQL = "select * from class_data where t_avid='".$av_id."' order by t_id";
$studentsQRY = mysql_db_query($dbname, $studentsSQL);
$t=0;
while($studentsRS = mysql_fetch_object($studentsQRY)){
$t_id = $studentsRS -> t_id;
$t_avid = $studentsRS -> t_avid;
$t1 = $studentsRS -> t1;
$t2 = $studentsRS -> t2;
$t3 = $studentsRS -> t3;
$t4 = $studentsRS -> t4;
$t5 = $studentsRS -> t5;
$t6 = $studentsRS -> t6;
$t7 = $studentsRS -> t7;
$t8 = $studentsRS -> t8;
$t9 = $studentsRS -> t9;
$t10 = $studentsRS -> t10;
$t11 = $studentsRS -> t11;
$t12 = $studentsRS -> t12;
$t++;
?>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['ระดับชั้น', 'ชาย', 'หญิง'],
['ม.๑', <? echo"".$t1."";?>, <? echo"".$t2."";?>],
['ม.๒', <? echo"".$t3."";?>, <? echo"".$t4."";?>],
['ม.๓', <? echo"".$t5."";?>, <? echo"".$t6."";?>],
['ม.๔', <? echo"".$t7."";?>, <? echo"".$t8."";?>],
['ม.๕', <? echo"".$t9."";?>, <? echo"".$t10."";?>],
['ม.๖', <? echo"".$t11."";?>, <? echo"".$t12."";?>]
]);
var options = {
title: 'สรุปจำนวนนักเรียนชาย-หญิงระดับชั้นมัธยมศึกษา <? echo "".$av_cat."";?> ',
hAxis: {title: 'ระดับชั้นมัธยม', titleTextStyle: {color: 'red'}}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 710; height: 500px;"></div>
</body>
</html>
</td></tr><? } ?>
</table>
|
 |
 |
 |
 |
Date :
2014-11-25 14:47:43 |
By :
LAGO |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอเข้าใจแล้ว
|
 |
 |
 |
 |
Date :
2014-11-25 16:52:16 |
By :
LAGO |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|