|
|
|
php กราฟ เส้น ดึงค่าจาก ฐานข้อมูลมาโชว์ อยากได้วิธีการทำ กราฟเส้น ด้วย php โดยดึงค่าจาก db มาแสดง ค่ะ |
|
|
|
|
|
|
|
jpgraphครับผม
|
|
|
|
|
Date :
2010-06-17 22:02:05 |
By :
sleepington |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันนี้ผมทำแบบง่ายๆนะครับ ไม่สวยแต่ใช้ได้ อิอิ
โค๊ตทำกราฟ
Code (PHP)
<?php
include('connect.php');
$sql="select*from data";
$result=mysql_query($sql);
$arr=mysql_fetch_array($result);
$isee=$arr['isee'];
$dont=$arr['dont'];
$total=$arr['total'];
if($arr['total']=="0"){
$a="1";
$b="1";
}
else{
$a=round(($isee*100)/$arr['total'],2);
$b=round(($dont*100)/$arr['total'],2);
$yes=$arr['isee'];
$no=$arr['dont'];
}
//Start the session so we can store what the security code actually is
$font = 'PSL116.ttf';
// create a blank image
$font_size=100*0.2;
$image = imagecreate(200,100);
// fill the background color
$bg = imagecolorallocate($image, 225, 225, 225);
$text_color = imagecolorallocate($image, 80, 120, 180);
$noise_color = imagecolorallocate($image, 100, 120, 180);
$white=imagecolorallocate($image,0,0,0);
$font_size=100*0.15;
imageline($image,0,0,0,70, $white);
imageline($image,0,70,150,70, $white);
for($i=20;$i<35;$i++){
imageline($image,2,$i,$a,$i, $noise_color);
}
for($i=50;$i<65;$i++){
imageline($image,2,$i,$b,$i, $text_color);
}
//text create
imagettftext($image, $font_size, 0, $a+5, 35, $text_color, $font,"เห็นด้วย".$a."%") or die('Error in imagettftext function');
imagettftext($image, $font_size, 0, $b+5, 65, $text_color, $font,"ไม่เห็นด้วย".$b."%") or die('Error in imagettftext function');
imagettftext($image, $font_size, 0, 10, 90, $text_color, $font,"ผู้โหวตทั้งหมด".$total."คน") or die('Error in imagettftext function');
// output the picture
header("Content-type: image/png");
imagepng($image);
?>
หน้า แสดงกราฟ
Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var req=false;
function test()
{
if(document.getElementById("rdo1").checked==false && document.getElementById("rdo2").checked==false ){
alert("กรุณาเลือกโหวตก่อนค่ะ");
return false;
}
else{
if(document.getElementById("rdo1").checked==true){
if (window.XMLHttpRequest)
{ // Mozilla, Safari,...
req = new XMLHttpRequest();
if (req.overrideMimeType)
{
req.overrideMimeType('text/html');
}
} else if (window.ActiveXObject)
{ // IE
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e)
{
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!req)
{
alert('Cannot create XMLHTTP instance');
return false;
}
req.onreadystatechange=function()
{
if(req.readyState==4)
{
if(req.responseText=="Y"){
alert("โหวตเรียบร้อยแล้วค่ะ");
showgraph();
}
}
}
var url="insert.php";
var str = Math.random();
var strsend="ty="+str;
strsend+="&rdo1="+"1";
strsend+="&rdo2="+"0";
req.open('POST',url,true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", strsend.length);
req.setRequestHeader("Connection", "close");
req.send(strsend);
}
if(document.getElementById("rdo2").checked==true){
if (window.XMLHttpRequest)
{ // Mozilla, Safari,...
req = new XMLHttpRequest();
if (req.overrideMimeType)
{
req.overrideMimeType('text/html');
}
} else if (window.ActiveXObject)
{ // IE
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e)
{
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!req)
{
alert('Cannot create XMLHTTP instance');
return false;
}
req.onreadystatechange=function()
{
if(req.readyState==4)
{
if(req.responseText=="Y"){
alert("โหวตเรียบร้อยแล้วค่ะ");
showgraph();
}
}
}
var url="insert.php";
var str = Math.random();
var strsend="ty="+str;
strsend+="&rdo1="+"0";
strsend+="&rdo2="+"1";
req.open('POST',url,true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", strsend.length);
req.setRequestHeader("Connection", "close");
req.send(strsend);
}
}
}
function showgraph(){
var ran= Math.random();
document.getElementById("test").src = 'makegraph.php?' + ran;
}
</script>
</head>
<body onload="showgraph();">
<table width="800" border="1">
<tr>
<td> </td>
<td><form action="" method="post" name="form1">
เห็นด้วย
<input name="rdoCheckbox" type="radio" id="rdo1" value="1">
<br>
ไม่เห็นด้วย
<input name="rdoCheckbox" type="radio" id="rdo2" value="2">
<br>
<input name="btnSubmit2" type="button" value="click" onclick="test();" />
</form></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><img id="test" alt="" />
</td>
<td> </td>
</tr>
</table>
</body>
</html>
หน้าinsert เวลาโหวต
|
|
|
|
|
Date :
2010-06-17 23:34:34 |
By :
sagi |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากมายค่ะ จะลองดูนะคะ ^^
|
|
|
|
|
Date :
2010-06-18 09:47:37 |
By :
prasara |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|