 |
การสร้างกราฟ จากโค้ดจะเป็นรูปกราฟแท่งแล้วข้างล่างจะเขียนชื่อเดือนเอาไว้ แต่จะเปลี่ยนจารชื่อเดือนให้เป็นชื่อวิชาที่เก็บอยู่ในฐานข้อมูล |
|
 |
|
|
 |
 |
|
<?php
include ("../jpgraph.php");
include ("../jpgraph_bar.php");
require_once("http://127.0.0.1/training/include.php");
require_once("http://127.0.0.1/training/function.php");
// Some data
$st_male=array(140,110,50,250);
$st_female=array(35,90,190,190);
//$datay3=array(20,60,70,140);
// Create the basic graph
$graph = new Graph(450,250,'auto');
$graph->SetScale("textlin");
$graph->img->SetMargin(40,80,30,40);
// Adjust the position of the legend box
$graph->legend->Pos(0.02,0.15);
// Adjust the color for theshadow of the legend
$graph->legend->SetShadow('[email protected]');
$graph->legend->SetFillColor('[email protected]');
// Get localised version of the month names
$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());
// Set a nice summer (in Stockholm) image
$graph->SetBackgroundImage('stship.jpg',BGIMG_COPY);
// Set axis titles and fonts
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetColor('white');
$graph->xaxis->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->SetColor('white');
$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->SetColor('white');
//$graph->ygrid->Show(false);
$graph->ygrid->SetColor('[email protected]');
// Setup graph title
$graph->title->Set ('TRAINING');
// Some extra margin (from the top)
$graph->title->SetMargin(3);
$graph->title->SetFont(FF_ARIAL,FS_NORMAL,12);
// Create the three var series we will combine
$bplot1 = new BarPlot($st_male);
$bplot2 = new BarPlot($st_female);
//$bplot3 = new BarPlot($datay3);
// Setup the colors with 40% transparency (alpha channel)
$bplot1->SetFillColor('[email protected]');
$bplot2->SetFillColor('[email protected]');
//$bplot3->SetFillColor('[email protected]');
// Setup legends
$bplot1->SetLegend('Male');
$bplot2->SetLegend('Female');
//$bplot3->SetLegend('Label 3');
// Setup each bar with a shadow of 50% transparency
$bplot1->SetShadow('[email protected]');
$bplot2->SetShadow('[email protected]');
//$bplot3->SetShadow('[email protected]');
$gbarplot = new GroupBarPlot(array($bplot1,$bplot2));
$gbarplot->SetWidth(0.6);
$graph->Add($gbarplot);
$graph->Stroke();
?>
จากโค้ดจะเป็นรูปกราฟแท่งแล้วข้างล่างจะเขียนชื่อเดือนเอาไว้
แต่จะเปลี่ยนจารชื่อเดือนให้เป็นชื่อวิชาที่เก็บอยู่ในฐานข้อมูลที่เราไม่ทราบว่าจะทำอย่างไรค่ะ
ช่วยหน่อยนะคะ ทำไม่ได้จริงๆ 
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
19 พ.ย. 2550 11:08:49 |
By :
mai |
View :
5347 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
// Get localised version of the month names
$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());
แก้เป็น
$graph->xaxis->SetTickLabels($xxxxx);
โดยให้ $xxxxx = array(วิชา1,วิชา2,....) ;
|
 |
 |
 |
 |
Date :
19 พ.ย. 2550 15:38:33 |
By :
sinchai9 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มัน Error อย่างงี้
Warning: include(../jpgraph.php) [function.include]: failed to open stream: No such file or directory in D:\MyWebSite\04-01.php on line 2
Warning: include() [function.include]: Failed opening '../jpgraph.php' for inclusion (include_path='.;C:\php5\pear') in D:\MyWebSite\04-01.php on line 2
Warning: include(../jpgraph_bar.php) [function.include]: failed to open stream: No such file or directory in D:\MyWebSite\04-01.php on line 3
Warning: include() [function.include]: Failed opening '../jpgraph_bar.php' for inclusion (include_path='.;C:\php5\pear') in D:\MyWebSite\04-01.php on line 3
Warning: require_once() [function.require-once]: URL file-access is disabled in the server configuration in D:\MyWebSite\04-01.php on line 4
Warning: require_once(http://127.0.0.1/training/include.php) [function.require-once]: failed to open stream: no suitable wrapper could be found in D:\MyWebSite\04-01.php on line 4
Fatal error: require_once() [function.require]: Failed opening required 'http://127.0.0.1/training/include.php' (include_path='.;C:\php5\pear') in D:\MyWebSite\04-01.php on line 4
|
 |
 |
 |
 |
Date :
2 ส.ค. 2551 16:36:25 |
By :
nnop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างนี้เป็นการสร้างกราฟแผนภูมิแท่ง ใช้ PHP ในการสร้างครับ
Code (PHP)
<?
header("Content-type: image/png");
// ค่าของข้อมูลที่ต้องการสร้างกราฟ
$data[1]="30";
$data[2]="40";
$data[3]="50";
$data[4]="60";
$data[5]="70";
$data[6]="80";
$data[7]="90";
$data[8]="100";
$img = ImageCreate(500, 310); //สร้างรูปขนาด 250x320
ImageColorAllocate($img, 255, 255, 255); //กำหนดพื้นรูปเป็นสีขาว
//กำหนดสีที่ใช้ในการวาด
$line = ImageColorAllocate($img ,176, 208, 210);
$dataFill = ImageColorAllocate($img, 122, 176, 180);
$black = ImageColorAllocate($img, 87, 32, 324);
//สร้างเส้นตรงในแนวตั้ง ซึ่งเป็นเส้นที่ขนาบขอบด้านซ้ายของแผนภูมิแท่ง
ImageLine($img, 50, 50, 50, 50, $line);
$bartick = 20; //ความหนาของแผนภูมิแท่งแต่ละอัน
$barspace = 30; //ระยะห่างระหว่างแผนภูมิแท่งแต่ละอัน
$barmax = 400; //ความยาวสูงสุดของแผนภูมิแท่ง
$baseX = 50; //จุดเริ่มต้นในแนวนอนของแผนภูมิแท่ง
$baseY = 30; //จุดเริ่มต้นในแนวตั้งของแผนภูมิแท่ง
//วนลูปเพื่อสร้างแผนภูมิแท่ง
for ($i=1; $i<=count($data); $i++) {
//หาจำนวนเความกว้างของกราฟ
$percent[$i]=$data[$i];
$data[$i]=$data[$i]*$barmax /100;
/* สร้างตัวแปรอาร์เรย์เก็บพิกัดของมุมต่างๆของแผนภูมิแท่ง เพื่อนำไปกำหนดให้ฟังก์ชั่น
ImagePolygon() */
$setBar[$i] = array($baseX, $baseY,
($baseX + $barmax), $baseY,
($baseX + $barmax), ($baseY + $bartick),
$baseX, ($baseY + $bartick));
//ใช้ฟังก์ชั่น ImagePolygon() เพื่อสร้างแผนภูมิแท่ง
ImagePolygon($img, $setBar[$i], 4, $line);
//วาดแผนภูมิแท่ง โดยใช้ฟังก์ชั่น ImageFilledRectangle()
ImageFilledRectangle($img, $baseX, $baseY, ($data[$i]+50), ($baseY + $bartick), $dataFill);
//แสดงข้อความเพื่อบอกค่าของแผนภูมิแท่งแต่ละอัน
ImageString($img, 3, ($baseX - 10), $baseY + 5, $i , $black);
ImageString($img, 3, ($data[$i] + 55), $baseY + 5,
number_format($percent[$i]) ."%", $black);
$baseY += $barspace; //ขยับจุดเริ่มต้นในแนวตั้งของแผนภูมิแท่ง
}
ImagePNG($img);
ImageDestroy($img);
?>
Code (Sample1.php)
<html>
<head>
<title>กราฟ</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
</head>
<body>
ตัวอย่างกราฟนะจ๊ะ
<br>
<?php
echo "<img src='Sample2.php'><br><br>\n";
?>
</body>
</html>
Code (Sample2.php)
<?
header("Content-type: image/png");
// ค่าของข้อมูลที่ต้องการสร้างกราฟ
$data[1]="30";
$data[2]="40";
$data[3]="50";
$data[4]="60";
$data[5]="70";
$data[6]="80";
$data[7]="90";
$data[8]="100";
$img = ImageCreate(500, 310); //สร้างรูปขนาด 250x320
ImageColorAllocate($img, 255, 255, 255); //กำหนดพื้นรูปเป็นสีขาว
//กำหนดสีที่ใช้ในการวาด
$line = ImageColorAllocate($img ,176, 208, 210);
$dataFill = ImageColorAllocate($img, 122, 176, 180);
$black = ImageColorAllocate($img, 87, 32, 324);
//สร้างเส้นตรงในแนวตั้ง ซึ่งเป็นเส้นที่ขนาบขอบด้านซ้ายของแผนภูมิแท่ง
ImageLine($img, 50, 50, 50, 50, $line);
$bartick = 20; //ความหนาของแผนภูมิแท่งแต่ละอัน
$barspace = 30; //ระยะห่างระหว่างแผนภูมิแท่งแต่ละอัน
$barmax = 400; //ความยาวสูงสุดของแผนภูมิแท่ง
$baseX = 50; //จุดเริ่มต้นในแนวนอนของแผนภูมิแท่ง
$baseY = 30; //จุดเริ่มต้นในแนวตั้งของแผนภูมิแท่ง
//วนลูปเพื่อสร้างแผนภูมิแท่ง
for ($i=1; $i<=count($data); $i++) {
//หาจำนวนเความกว้างของกราฟ
$percent[$i]=$data[$i];
$data[$i]=$data[$i]*$barmax /100;
/* สร้างตัวแปรอาร์เรย์เก็บพิกัดของมุมต่างๆของแผนภูมิแท่ง เพื่อนำไปกำหนดให้ฟังก์ชั่น
ImagePolygon() */
$setBar[$i] = array($baseX, $baseY,
($baseX + $barmax), $baseY,
($baseX + $barmax), ($baseY + $bartick),
$baseX, ($baseY + $bartick));
//ใช้ฟังก์ชั่น ImagePolygon() เพื่อสร้างแผนภูมิแท่ง
ImagePolygon($img, $setBar[$i], 4, $line);
//วาดแผนภูมิแท่ง โดยใช้ฟังก์ชั่น ImageFilledRectangle()
ImageFilledRectangle($img, $baseX, $baseY, ($data[$i]+50), ($baseY + $bartick), $dataFill);
//แสดงข้อความเพื่อบอกค่าของแผนภูมิแท่งแต่ละอัน
ImageString($img, 3, ($baseX - 10), $baseY + 5, $i , $black);
ImageString($img, 3, ($data[$i] + 55), $baseY + 5,
number_format($percent[$i]) ."%", $black);
$baseY += $barspace; //ขยับจุดเริ่มต้นในแนวตั้งของแผนภูมิแท่ง
}
ImagePNG($img);
ImageDestroy($img);
?>
สำหรับอื่น ๆ สามารถอ่านได้จาก
Ref : PHP Graph & Charts
หรือจะลองใช้งาน
PHP JpGraph
ครับ
|
 |
 |
 |
 |
Date :
2009-05-08 06:42:55 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Warning: main(../jpgraph.php): failed to open stream: No such file or directory in c:\appserv\www\test\chart.php on line 10
Warning: main(): Failed opening '../jpgraph.php' for inclusion (include_path='.;c:\php4\pear') in c:\appserv\www\test\chart.php on line 10
Warning: main(../jpgraph_bar.php): failed to open stream: No such file or directory in c:\appserv\www\test\chart.php on line 11
Warning: main(): Failed opening '../jpgraph_bar.php' for inclusion (include_path='.;c:\php4\pear') in c:\appserv\www\test\chart.php on line 11
Warning: main(http://127.0.0.1/training/include.php): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in c:\appserv\www\test\chart.php on line 12
Fatal error: main(): Failed opening required 'http://127.0.0.1/training/include.php' (include_path='.;c:\php4\pear') in c:\appserv\www\test\chart.php on line 12
|
 |
 |
 |
 |
Date :
2009-05-16 23:39:43 |
By :
sukanyabb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูPath ให้ถูกครับ
|
 |
 |
 |
 |
Date :
2009-12-14 11:42:24 |
By :
tapi |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|