1. <?php
  2.   // 출처 http://cafe.naver.com/phpcafe/2618
  3.   //배열에 데이터값 할당 
  4.   $data = array( "Jan" => 55, "Feb" => 54, "Mar" => 53,
  5.   "Apr" => 33, "May" => 13, "Jun" => 15, 
  6.   "Jul" => 23, "Aug" => 28, "Sep" => 32, 
  7.   "Oct" => 45, "Nov" => 73, "Dec" => 71 ); 
  8.   //이미지 유형 설정 
  9.   header("Content-type: image/png");
  10.   //이미지 공간형성
  11.   $width = 480;
  12.   $height = 250; 
  13.   $image = imagecreate($width, $height);
  14.   // 색깔 설정 
  15.   $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
  16.   $navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
  17.   $black = imagecolorallocate($image, 0x00, 0x00, 0x00);
  18.   $gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
  19.   //자료의 갯수와 자료의 최대값을 구합니다.
  20.   $maxval = max($data);
  21.   $nval = sizeof($data);
  22.   //여백을 설정합니다.
  23.   $vmargin = 20; 
  24.   $hmargin = 38; 
  25.   //막대가 그려질 공간을 구합니다.
  26.   $base = floor(($width - $hmargin) / $nval);
  27.   //챠트의 가로와 세로길이를 구합니다.
  28.   $ysize = $height - 2 * $vmargin;
  29.   $xsize = $nval * $base;
  30.   //챠트테두리 생성
  31.   imagerectangle($image, $hmargin, $vmargin, 
  32.   $hmargin + $xsize, $vmargin + $ysize, $black);
  33.   // 제목의 크기와 내용입력
  34.   $titlefont = 3;
  35.   $title = "Amount of sell"; 
  36.   // 제목이 들어갈 공간의 크기를 구하고 가운데 정렬시킵니다.
  37.   $txtsz = imagefontwidth($titlefont) * strlen($title); 
  38.   $xpos = (int)($hmargin + ($xsize - $txtsz)/2);
  39.   $xpos = max(1, $xpos);
  40.   $ypos = 3;
  41.   imagestring($image, $titlefont, $xpos, $ypos, $title, $black); 
  42.   //y축 라벨의 폰트와 눈금의 간격을 구합니다.
  43.   $labelfont = 2;
  44.   $ngrid = 4;
  45.   $dydat = $maxval / $ngrid;
  46.   $dypix = $ysize / ($ngrid + 1);
  47.   for ($i = 0; $i <= ($ngrid + 1); $i++) {
  48.     //라벨값과 라벨의 y축 위치를 구합니다.
  49.     $ydat = (int)($i * $dydat);
  50.     $ypos = $vmargin + $ysize - (int)($i*$dypix);
  51.     //라벨의 크기를 구합니다.
  52.     $txtsz = imagefontwidth($labelfont) * strlen($ydat);
  53.     $txtht = imagefontheight($labelfont);
  54.     //라벨의 x축 위치를 구합니다.
  55.     $xpos = (int)(($hmargin - $txtsz) / 2);
  56.     $xpos = max(1, $xpos);
  57.     //문자열을 출력합니다.
  58.     imagestring($image, $labelfont, $xpos, $ypos - (int)
  59.     ($txtht/2), $ydat, $black);
  60.     //눈금을 그립니다.
  61.     if (!($i == 0) && !($i > $ngrid))
  62.     imageline($image, $hmargin - 3, $ypos, $hmargin + $xsize, $ypos, $gray);
  63.   }
  64.   //막대간의 간격과 y축의 단위눈금 높이를 구합니다.
  65.   $padding = 3; // half of spacing between columns
  66.   $yscale = $ysize / (($ngrid+1) * $dydat);
  67.   for ($i = 0; list($xval, $yval) = each($data); $i++) {
  68.     //막대를 그립니다.
  69.     $ymax = $vmargin + $ysize;
  70.     $ymin = $ymax - (int)($yval*$yscale);
  71.     $xmax = $hmargin + ($i+1)*$base - $padding;
  72.     $xmin = $hmargin + $i*$base + $padding;
  73.     imagefilledrectangle($image, $xmin, $ymin, $xmax, $ymax,
  74.     $navy);
  75.     //x축 라벨을 입력합니다.
  76.     $txtsz = imagefontwidth($labelfont) * strlen($xval);
  77.     $xpos = $xmin + (int)(($base - $txtsz) / 2);
  78.     $xpos = max($xmin, $xpos);
  79.     $ypos = $ymax + 3; // distance from x axis
  80.     imagestring($image, $labelfont, $xpos, $ypos, $xval, $black);
  81.     }
  82.   //메모리 할당/삭제 
  83.   imagepng($image);
  84.   imagedestroy($image);
  85. ?> 

--------------------------------------------------------------------------------------------


막대그래프-그리기2


출처 : http://cafe.naver.com/phpcafe/2618

:
BLOG main image

공지사항

카테고리

분류 (356)
main (1)
php5 (218)
HTML (36)
javascript (93)
IT 이론과 퍼온정보 (8)
깨작일상 (0)
곧 추가할 내용 (0)
추후 알아볼것들... (0)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

달력

«   2024/03   »
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
Total :
Today : Yesterday :