- 정보공유
[PHP] GD 를 이용한 그래프 만들기 SAMPLE 1
샘플.
/test.php?0,0,0,0,5,16,80,-10
[code]
<?
Header("Content-type : image/png");
$query_string = urldecode(getenv("QUERY_STRING"));
$query_string = explode("," , $query_string);
for ($i=0; $i<10; $i++){
if ($y_max < intval($query_string[$i])){
$y_max = intval($query_string[$i]);
}
}
$image = ImageCreate(274,188);
$white = ImageColorAllocate($image , 255,255,255);
$skin = ImageColorAllocate($image , 250,219,172);
$black = ImageColorAllocate($image , 0,0,0);
$dark_gray = ImageColorAllocate($image , 241,241,241);
ImageLine($image , 33,15,233,15, $black);
ImageLine($image , 33,15,33,165, $black);
ImageLine($image , 33,165,233,165, $black);
ImageLine($image , 233,15,233,165, $black);
ImageLine($image , 25,20,25,170, $black);
ImageLine($image , 25,170,225,170, $black);
ImageString($image , 2, 3, 4,"Man(%)",$black);
ImageString($image , 2, 250, 174,"Avg",$black);
//x축 안선
for ($i=25; $i < 160; $i=$i+10){
ImageLine($image , 33,$i,233,$i, $dark_gray);
}
//x축 대칭
for ($i=20; $i<171; $i=$i+10){
ImageLine ($image,25,$i,33,$i-5,$black);
}
// y축 안선
for ($i=53; $i < 230; $i=$i+20){
ImageLine($image , $i,15,$i,165, $dark_gray);
}
// y축 대칭
for ($i=25; $i<226; $i=$i+20){
ImageLine ($image,$i,170,$i+10,165,$black);
}
//x축 값
$x_position =22;
$x_value =0;
for ($i=0; $i<=10; $i++){
ImageString($image,2,$x_position,175,$x_value,$black);
$x_position= $x_position + 20;
$x_value = $x_value + 10;
}
//y축 값
$y_position = 23;
if ( $y_max <= 35){
$y_value = 28;
} else {
$y_value = $y_max;
}
$temp = intval($y_value/7);
$y_value = $temp * 7 +7;
$y_max = $y_value;
$skip = $temp +1;
for ($i=0; $i<=7; $i++) {
ImageString($image,2,7,$y_position,$y_value,$black);
$y_position=$y_position+20;
$y_value = $y_value - $skip;
}
// 막대그래프 꺽은선 그래프
$rectPosition =35;
$linePosition =40;
for ($i=0; $i<10; $i++) {
$y_rectPosition = 145 * intval($query_string[$i])/$y_max;
$y_rectPosition = 170-$y_rectPosition;
imageFilledRectangle($image, $rectPosition ,$y_rectPosition,$rectPosition+10,167, $skin);
imageFilledRectangle($image, $linePosition-2, $y_rectPosition-2, $linePosition+2, $y_rectPosition+2 ,$black);
$rectPosition = $rectPosition +20;
if ($i != 0){
ImageLine($image ,$temp1,$temp2,$linePosition,$y_rectPosition, $black);
}
$temp1 = $linePosition;
$temp2 = $y_rectPosition;
$linePosition = $linePosition +20;
}
ImagePNG($image);
ImageDestroy($image);
?>
[/code]
/test.php?0,0,0,0,5,16,80,-10
[code]
<?
Header("Content-type : image/png");
$query_string = urldecode(getenv("QUERY_STRING"));
$query_string = explode("," , $query_string);
for ($i=0; $i<10; $i++){
if ($y_max < intval($query_string[$i])){
$y_max = intval($query_string[$i]);
}
}
$image = ImageCreate(274,188);
$white = ImageColorAllocate($image , 255,255,255);
$skin = ImageColorAllocate($image , 250,219,172);
$black = ImageColorAllocate($image , 0,0,0);
$dark_gray = ImageColorAllocate($image , 241,241,241);
ImageLine($image , 33,15,233,15, $black);
ImageLine($image , 33,15,33,165, $black);
ImageLine($image , 33,165,233,165, $black);
ImageLine($image , 233,15,233,165, $black);
ImageLine($image , 25,20,25,170, $black);
ImageLine($image , 25,170,225,170, $black);
ImageString($image , 2, 3, 4,"Man(%)",$black);
ImageString($image , 2, 250, 174,"Avg",$black);
//x축 안선
for ($i=25; $i < 160; $i=$i+10){
ImageLine($image , 33,$i,233,$i, $dark_gray);
}
//x축 대칭
for ($i=20; $i<171; $i=$i+10){
ImageLine ($image,25,$i,33,$i-5,$black);
}
// y축 안선
for ($i=53; $i < 230; $i=$i+20){
ImageLine($image , $i,15,$i,165, $dark_gray);
}
// y축 대칭
for ($i=25; $i<226; $i=$i+20){
ImageLine ($image,$i,170,$i+10,165,$black);
}
//x축 값
$x_position =22;
$x_value =0;
for ($i=0; $i<=10; $i++){
ImageString($image,2,$x_position,175,$x_value,$black);
$x_position= $x_position + 20;
$x_value = $x_value + 10;
}
//y축 값
$y_position = 23;
if ( $y_max <= 35){
$y_value = 28;
} else {
$y_value = $y_max;
}
$temp = intval($y_value/7);
$y_value = $temp * 7 +7;
$y_max = $y_value;
$skip = $temp +1;
for ($i=0; $i<=7; $i++) {
ImageString($image,2,7,$y_position,$y_value,$black);
$y_position=$y_position+20;
$y_value = $y_value - $skip;
}
// 막대그래프 꺽은선 그래프
$rectPosition =35;
$linePosition =40;
for ($i=0; $i<10; $i++) {
$y_rectPosition = 145 * intval($query_string[$i])/$y_max;
$y_rectPosition = 170-$y_rectPosition;
imageFilledRectangle($image, $rectPosition ,$y_rectPosition,$rectPosition+10,167, $skin);
imageFilledRectangle($image, $linePosition-2, $y_rectPosition-2, $linePosition+2, $y_rectPosition+2 ,$black);
$rectPosition = $rectPosition +20;
if ($i != 0){
ImageLine($image ,$temp1,$temp2,$linePosition,$y_rectPosition, $black);
}
$temp1 = $linePosition;
$temp2 = $y_rectPosition;
$linePosition = $linePosition +20;
}
ImagePNG($image);
ImageDestroy($image);
?>
[/code]