|
|
|
ช่วยแก้ code กราฟ หน่อยสิครับ คือ ว่ามีโค้ด กราฟ อะครับ แต่ว่ามันไม่ได้ดึงมาจากฐาน |
|
|
|
|
|
|
|
คือ ว่ามีโค้ด กราฟ อะครับ แต่ว่ามันไม่ได้ดึงมาจากฐาน "ช่วยแก้ให้มันดึงข้อมูลจากฐานได้ไหม ครับ" คือว่าแก้ไม่ถูกอะครับ .......... ช่วยทีนะครับ..........ตัวอย่างโปรแกรมนะครับ
ไฟล์main
<?php
if (isset($_POST["send"]))
process_form();
else
show_form();
//???????????????????????????????????????? 5 ???
function show_form() {
echo <<<HTMLBLOCK
<h3>???????????????????????????</h3>
<form method="POST" action="{$_SERVER['PHP_SELF']}">\n
HTMLBLOCK;
//??????????????????????? 5 ???? ???????????????? data[] ????????????????????
for ($i = 1; $i <= 5; $i++) {
echo "ข้อมูลที่ {$i}: <input type=\"text\" name=\"data[]\" size=\"10\" maxlength=\"3\"><br>\n";
}
echo <<<HTMLBLOCK
<input type="submit" name="send" value="??????">
<input type="reset" value="??????????">
</form>
HTMLBLOCK;
}
//???????????????????????????? ???????????????????????????? 5 ???????????????????
function process_form() {
/* ????? Query String ???????????????????? 5 ??????????? bar.php ??? pie.php ???????????????? Query String ?????????? (?????????? data[]) */
for ($i = 0; $i < 5; $i++) {
$query_str .= "data[]={$_POST['data'][$i]}&";
}
echo <<<HTMLBLOCK
<h3>?????????????????????????????</h3>
<img src="bar.php?{$query_str}">
<h3>??????????????????????????????</h3>
<img src="pie.php?{$query_str}">
HTMLBLOCK;
}
ไฟล์ bar
<?php
/* ?????????????????????????????????????? $data (?????????????????? ????????? $_GET["data"] ???????????) */
$data = $_GET["data"];
$sum = array_sum($data); //???????????????????????
$bartick = 20; //?????????????????????????????
$barspace = 10; //?????????????????????????????????????
$barmax = 400; //???????????????????????????
$baseX = 50; //?????????????????????????????????
$baseY = 20; //????????????????????????????????????????
/* ??????????????????????????????????? ????????????????????????????? 5 ???????????????????????????????????????????????????????????????????????????????????????? */
$image_width = $barmax + $baseX + 100;
$image_height = ($bartick * 5) + ($barspace * 4) + (2 * $baseY);
$im = ImageCreate($image_width, $image_height); //????????????????????
ImageColorAllocate($im, 255, 255, 255); //?????????????????????
//?????????????????????
$text_color = ImageColorAllocate($im, 0, 0, 0);
$line_color = ImageColorAllocate($im ,176, 208, 210);
$fill_color = ImageColorAllocate($im, 122, 176, 180);
//??????????????????? ????????????????????????????????????? 5 ???
ImageLine($im, $baseX, $baseY - 10, $baseX, $baseY + ($bartick * 5) + ($barspace * 4) + 10, $line_color);
//???????????????????????????????? 5 ???
for ($i = 0; $i < 5; $i++) {
//?????????????????? (???????????????????????? 100%)
ImageRectangle($im, $baseX, $baseY, $baseX + $barmax, $baseY + $bartick, $line_color);
//?????????????????????????????????????????????????????????????????????????
$percent = (100 * $data[$i]) / $sum;
//??????????????????????????????????????????????????????????????????
$barlength = ($percent / 100) * $barmax;
//??????????????
ImageFilledRectangle($im, $baseX, $baseY, $baseX + $barlength, $baseY + $bartick, $fill_color);
//??????????????????????????????????? (??????? Data1 ??? Data5)
ImageString($im, 3, $baseX - 40, $baseY + 5, "Data" . ($i + 1), $text_color);
//???????????????????????????????????
ImageString($im, 3, $baseX + $barlength + 10, $baseY + 5, number_format($data[$i]) . " [" . number_format($percent, 2) . "%]", $text_color);
$baseY += $bartick + $barspace; //???????????????????????????????????????????????
}
//???????????????????????????????????????????????
header("Content-type: image/png");
ImagePNG($im);
//?????????????????????
ImageDestroy($im);
?>
ไฟล์ pie
<?php
/* ?????????????????????????????????????? $data (???????????????? ????????? $_GET["data"]) */
$data = $_GET["data"];
$sum = array_sum($data); //???????????????????????
$degrees = array(); //??????????? $degrees ???????????????????????
$diameter = 150; //?????????????????????????????????????????
$radius = $diameter / 2; //???????????????????????????????? $radius
$margin_left = 10; //???????????????????????????????????
$margin_right = 200; //??????????????????????????????????
$margin_top = 10; //?????????????????????????????????
$margin_bottom = 10; //???????????????????????????????????
//?????????????????????????????????????????????? ???????????????????? ? $degrees
for ($i = 0; $i < 5; $i++) {
$degrees[$i] = ($data[$i] / $sum) * 360;
}
/* ??????????????????????????????????? ???????????????????????????????????????*/
$image_width = $diameter + $margin_left + $margin_right;
$image_height = $diameter + $margin_top + $margin_bottom;
$im = ImageCreate($image_width, $image_height); //?????????????????
ImageColorAllocate($im, 255, 255, 255); //?????????????????????
$red = ImageColorAllocate($im, 255, 0, 0);
$green = ImageColorAllocate($im, 0, 255, 0);
$blue = ImageColorAllocate($im, 0, 0, 255);
$yellow = ImageColorAllocate($im, 255, 255, 0);
$pink = ImageColorAllocate($im, 255, 0, 255);
$black = ImageColorAllocate($im, 0, 0, 0);
//?????????????????????? ??????????????????????????????
$colors = array($red, $green, $blue, $yellow, $pink);
$last_angle = 0; //?????????????????????????????????????
//
for ($i = 0; $i < 5; $i++) {
//????????? (filled arc)
ImageFilledArc($im, $margin_left + $radius, $margin_top + $radius, $diameter, $diameter, $last_angle, $last_angle + $degrees[$i], $colors[$i], IMG_ARC_PIE);
//????????????????
ImageFilledArc($im, $margin_left + $radius, $margin_top + $radius, $diameter, $diameter, $last_angle, $last_angle + $degrees[$i], $black, IMG_ARC_EDGED | IMG_ARC_NOFILL);
//???????????????????????????????????????????
$last_angle += $degrees[$i];
}
$startX = $margin_left + $diameter + 20;
$startY = $margin_top;
//??????????????????????????????????????????
for ($i = 0; $i < 5; $i++) {
ImageFilledRectangle($im, $startX, $startY, $startX + 15, $startY + 15, $colors[$i]);
ImageRectangle($im, $startX, $startY, $startX + 15, $startY + 15, $black);
$percent = (100 * $data[$i]) / $sum;
ImageString($im, 3, $startX + 25, $startY, "Data" . (string) ($i + 1) . ": " . number_format($data[$i]) . " [" . number_format($percent, 2) . "%]", $black);
$startY += 25;
}
//???????????????????????????????????????????????
header("Content-type: image/png");
ImagePNG($im);
//?????????????????????
ImageDestroy($im);
?>
Tag : - - - -
|
|
|
|
|
|
Date :
2009-09-15 14:16:10 |
By :
korn_250431 |
View :
1021 |
Reply :
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|