BLOG main image
정민군's Blog
miniya devlog

'Language'에 해당되는 글 16건

  1. 2006.10.17 GD 이용한 통계 그래프

GD 이용한 통계 그래프

Language/PHP 2006. 10. 17. 21:17 by 정민군
그라디에이션효과를 프로그램이 아닌 배경이미지를 이용해 나타냈고,
막대에 모양을 좀 이쁘게 했습니다.
링크에서 그라디에이션배경이미지 3개를 다른이름으로 저장해서 파일과
같은 경로에 넣어주세요. 그리고 ariali.ttf 폰트로 필요하니 윈도우에 있는 폰트파일
복사해다 같은 위치에 넣주세요.

<?
class gdGraph{
      function mainCall($data,$h,$margin="50",$weight="10",$copy="",$write){
             //폰트가 있는 위치를 적어준다. /나 ./로 시작해 적어준다
             $this->font = "./ariali.ttf";
             //각각의 받은 값을 세팅
             $this->data = $data;
             $this->margin = $margin;
             $this->copystr = $copy;
             $this->weight = $weight;
             if ($this->copystr) $this->copyright = 20;//카피라이트가 있다면 스여질공간 20확보
             $this->h = $h;//이미지의 높이
             $this->w = $this->margin * 2 + sizeof($this->data) * $this->weight;//이미지의 넓이

             //각각의 역할을 하는 함수들 호출
             $this->base();//이미지바탕을만든다
             $this->gradation();//바탕의 그라디에이션효과
             $this->nomograph();//가로세로선을 그린다
             $this->selectmax();//맥스값을 구한다
             $this->drawclumn();//막대를 그린다
             if($write) $this->charwrite();//좌측에 쓰여지는 수치값
             if($write) $this->numberwrite();//막대에 쓰여지는 수치값
             if($write) $this->datewrite();//하단에 쓰여지는 수치값
             $this->copywrite();//우측 카피라이트

             //이미지출력
             header('Content-type: image/png');
             imagepng($this->image);
             imagedestroy($this->image);
      }
      function base(){
             $this->image = imagecreate($this->w + $this->copyright, $this->h);
             $this->black = imagecolorallocate($this->image, 0, 0, 0);//검은색
             $this->white = imagecolorallocate($this->image, 255, 255, 255);//흰색
             $this->light_green = imagecolorallocate($this->image, 0, 183, 0);//가로줄, 세로줄의 색
             $this->dark_green = imagecolorallocate($this->image, 153, 218, 148);//가로줄, 세로줄의 색
             $this->bgcolor = imagecolorallocate($this->image, 215, 215, 215);//배경 회색
             imagefill($this->image, 0, 0, $this->bgcolor);
      }
      function gradation(){
             /*
             배경에 그라디에이션을 넣는 부분인데 그라디에이션배경을 처리하면 막대의 그라디에이션이 깨진다.
             아직 GD가 제대로 지원을 못하는 듯한데 같은 계열의 색을 배경으로 하면 깨지지 않는다.
             하지만 초록색막대에 초록색 배경을 써야하니 이상하다.
             아래 주석을 제거하면 볼 수 있다. 푸른색은 깨지고 녹색은 된다
             */
             //$image = imagecreatefromgif("./rad01.gif");//푸른바탕은 이미지가 깨지다
             //$image = imagecreatefromgif("./rad02.gif");//녹색바탕은 제대로 나온다
             //imagecopyresized($this->image, $image, 0, 0, 0, 0, $this->w, $this->h, 200, 200);
      }
      function nomograph(){
             //세로줄
             imageline($this->image, $this->margin, $this->margin, $this->margin ,$this->h - $this->margin, $this->light_green);
             imageline($this->image, $this->w - $this->margin, $this->margin, $this->w - $this->margin ,$this->h - $this->margin, $this->light_green);
             //가로줄은 밝은녹색과 어두운녹색을 일정한 형식으로 반복해 그린다
             if($this->h - $this->margin * 2 < 200) $tmp_cell=10;
             else if ($this->h - $this->margin * 2 < 400) $tmp_cell=20;
             else $tmp_cell=40;
             $tmp_h=($this->h - $this->margin * 2) / $tmp_cell;
             for ($i = 0 ; $i <= $tmp_cell ; $i++){
                    $tmp_he = $this->margin + $i * $tmp_h;
                    imageline($this->image, $this->margin - 3, $tmp_he, $this->w - $this->margin + 3, $tmp_he, ($i % ($tmp_cell / 10))?$this->dark_green:$this->light_green);
             }
      }
      function selectmax(){
             //그래프의 최대값을 구한다.
             for ($i = 0 ; $i < sizeof($this->data) ; $i++){
                    if ($max < $this->data[$i]) $max = $this->data[$i];
             }
             $this->maxvalue=$max;
      }
      function drawclumn(){
             for($i = 0 ; $i < sizeof($this->data) ; $i++){
                    //막대의 높이
                    $tmp_hvalue = ($this->h - $this->margin * 2) - $this->data[$i]/$this->maxvalue * ($this->h - $this->margin * 2) + $this->margin;
                    //막대의 그라디에이션
                    $image = imagecreatefromgif("./rad11.gif");
                    imagecopyresized($this->image, $image, $this->margin + $i * $this->weight + 1, $tmp_hvalue, 0, 0, $this->weight - 1, $this->h - $tmp_hvalue - $this->margin, 200, 200);
                    //막대의 외형모양을 꾸미는 라인들
                    imageline($this->image, $this->margin + $i * $this->weight + 1, $tmp_hvalue, $this->margin + $i * $this->weight + $this->weight, $tmp_hvalue, $this->white);
                    imageline($this->image, $this->margin + $i * $this->weight + 1, $tmp_hvalue, $this->margin + $i * $this->weight + 1, $this->h - $this->margin, $this->white);
                    imageline($this->image, $this->margin + $i * $this->weight + $this->weight, $tmp_hvalue, $this->margin + $i * $this->weight + $this->weight, $this->h - $this->margin, $this->black);
             }
      }
      function charwrite(){
             //우측글자쓰기
             for ($i = 0 ; $i <= 10 ; $i++){
                    $value = round($this->maxvalue * (10 - $i) / 10,1);
                    $arr_size = imagettfbbox(7, 0, $this->font, $value);
                    $width = abs($arr_size[2] - $arr_size[0]);
                    $we = round($this->margin - $width - 10);
                    $height = abs($arr_size[5] - $arr_sixe[3]);
                    $he = round($height / 2);
                    $he = $this->margin + $i * (($this->h - $this->margin * 2) / 10) + $he;
                    imagettftext($this->image, 7 ,0, $we, $he, $this->black, $this->font, $value);
             }
      }
      function numberwrite(){
             //막대에 숫자쓰기
             for($i = 0 ; $i < sizeof($this->data) ; $i++){
                    //글자의 높이
                    $tmp_hvalue = ($this->h - $this->margin * 2) - $this->data[$i]/$this->maxvalue * ($this->h - $this->margin * 2) + $this->margin;
                    //DATA수치쓰기
                    imagettftext($this->image, 7, 270, $this->margin + $i * $this->weight + 5,$tmp_hvalue + 2, $this->white, $this->font, $this->data[$i]);
             }
      }
      function datewrite(){
             //하단글자쓰기
             for($i = 0 ; $i < sizeof($this->data) ; $i++){
                    if((($i + 1) % 5)==0 && $i){
                           $arr_size=imagettfbbox(7, 0, $this->font, $i + 1);
                           $width=abs($arr_size[2]-$arr_size[0]);
                           $we = ($this->weight - $width) / 2;
                           imagettftext($this->image, 7, 0, $this->margin + $i * $this->weight + $we, $this->h - $this->margin + 10, $this->black, $this->font, $i + 1);
                    }
             }
      }
      function copywrite(){
             if ($this->copystr){
                    //검은색으로 바탕을 만든다
                    imagefilledrectangle($this->image, $this->w, 0, $this->w + $this->copyright, $this->h, $this->black);
                    //글자의 키기를 구해 위치를 계산 중앙에 쓰여지게 한다
                    $arr_size=imagettfbbox(9, 270, $this->font, $this->copystr);
                    $height = abs($arr_size[5] - $arr_sixe[3]);
                    $he = round(($this->h - $height) / 2);
                    imagettftext($this->image, 9, 270, $this->w + 5, $he, $this->white, $this->font, $this->copystr);
             }
      }
}

//입력되는 데이터로 디비에서 불러와 처리해도 된다.
$arr_data=array(10,32,53,24,25,76,87,18,69,100,41,42,13,34,15,76,97,18,79,20,81,42,53,24,85,26,17,28,39,50,30);

//이미지의 높이
$height=500;

//이미지의 상하좌우여백
$margin=50;

//막대하나의 굵기
$weight=15;

//우측 카피라이트, 없으면 안나타남
$copy="Copyright CINUAE All rights reserved.";

//좌측과 하단, 막대의 숫자를 표시할 것인가를 결정(0,1)
$write=1;

//새로운 클래스를 만들어 호출
$mygdGraph=new gdGraph;
$mygdGraph->mainCall($arr_data,$height,$margin,$weight,$copy,$write);
?>

1 ··· 3 4 5 6 

카테고리

분류 전체보기 (46)
Technic (5)
Language (16)
Database (8)
System (5)
Algorithm (1)
Design (1)
Tool (3)
Framework (2)
Network (1)
Utility (1)
SmartPhone (2)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

달력

«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

글 보관함