• LINK
    • Dengan bangga mendukung kegiatan idsecconf 2009
    • Kajian.Net

CARI MP3 KAJIAN, EBOOK, TUTORIAL PEMROGRAMAN PHP, MATERI KULIAH DATABASE, DLL

Loading

November 6, 2009

Class Grafik Kacung - Sample Grafik Dengan PHP

Didalam Katagori: Uncategorized, PHP

Kemarin habis bikin Class buat nge-generate Grafik dalam bentuk gambar dengan PHP, Grafik ini hanya melapporkan dua item (Rata-Rata dan Total), item ini bisa diubah sesuai kebutuhan, cuma ganti caption-nya doang :D ,

<?php
   
    /*
      Author : Didin Nurdin Ahmadi
                  <didinonpqcms{ a t }gmail{ d o t }com>
      date   : 2009-11-2 3:44:13 AM     
    */
   
    class Grafik_Kacung {
   
        private $imageTemp;
        private $Font;
        private $Width;
        private $Height;
        private $ScaleX;
        private $ScaleY;
        private $XY;
       
        function __construct(){
          $this -> Scale = 30;
          $this -> XY = array();
        }
       
        public function setFont($font){
          $this -> Font = $font;
        }
       
        public function setData($data){
          $this -> Data = $data;
        }
       
        public function setCanvas($height, $width,$file = null){
          $this -> Height = $height;
          $this -> Width  = $width; 
          $this -> imageTemp = imagecreate( $this -> Width + 150, $this -> Height + 80 );
          imagecolorallocate($this -> imageTemp, 255, 255, 244);
          $this -> setColor(); 
          $this -> drawLineXY();
          $this -> drawData();
          //header(’Content-type: image/png’);
          imagepng($this -> imageTemp, $file);
        }
       
        private function drawLineXY(){
          imageline($this -> imageTemp, 20, 5, 20, $this -> Height, $this -> black);
          imageline($this -> imageTemp, 20, $this -> Height, $this -> Width, $this -> Height, $this -> black);
          $xOrdinate = array(0,10,20,30,40,50,60);
          $tempH = $this -> Height;
          $this -> ScaleY = round($tempH / count($xOrdinate));
          foreach($xOrdinate as $v){
            $i++;
            $text = " ";
            if(empty($v)){
              $text = null;
            }
            imagettftext($this -> imageTemp, 12, 0, 17, $tempH ,$this -> black, $this -> Font, $text);
            if($i>1) imageline($this -> imageTemp, 25, $tempH, $this -> Width, $tempH, $this -> grey);
            imagettftext($this -> imageTemp, 10, 0, 3, $tempH ,$this -> black, $this -> Font, $v);
            $tempH-= $this -> ScaleY;
          }
         
          $tempW = 50;
          $this -> ScaleX = ($this -> Width  / count($this -> Data));
          foreach($this -> Data as $k => $v){
            imagettftext($this -> imageTemp, 10, 0 , $tempW ,$this -> Height + 20 ,$this -> black, $this -> Font, $k);
            imageline($this -> imageTemp, $tempW, $this -> Height - 10, $tempW, 10, $this -> grey);
            imagettftext($this -> imageTemp, 9, 0 , $tempW - 30 ,$this -> Height + 40 , $this -> black, $this -> Font, wordwrap($v[’nama’], 20, "\n"));
            $tempW += $this -> ScaleX;
          }
         
        }
       
        private function drawData(){
          $tempW = 50;
          $keyInserted = array();
          foreach($this -> Data as $k => $v){
            $i++;
            $pOrd = $this -> scaleConverter($v[’total’]);
            $rOrd = $this -> scaleConverter($v[’rata’]);
            imagettftext($this -> imageTemp, 10, 0 , $tempW , $pOrd ,$this -> red, $this -> Font, " " . $v[’total’]);
            imagettftext($this -> imageTemp, 10, 0 , $tempW , $rOrd ,$this -> blue, $this -> Font, " " . $v[’rata’] );
            $this -> createSign($tempW, $pOrd);                       
            $this -> createSign($tempW, $rOrd, $this -> blue);
            $a = next($this -> Data);
            if($i < count($this -> Data)){           
              imageline($this -> imageTemp, $tempW, $pOrd, $tempW + $this -> ScaleX, $this -> scaleConverter($a[’total’]), $this -> red);
              imageline($this -> imageTemp, $tempW, $rOrd, $tempW + $this -> ScaleX, $this -> scaleConverter($a[’rata’]), $this -> blue);
            }
            $tempW += $this -> ScaleX;           
          }
         
          $this -> createNotes();
        }
       
        public function setColor(){
          $this -> violet   = imagecolorallocate($this->imageTemp, 0x00, 0x00, 0xCD);
          $this -> white    = imagecolorallocate($this->imageTemp, 0xFF, 0xFF, 0xFF);
          $this -> red      = imagecolorallocate($this->imageTemp, 0xFF, 0x66, 0x66);
          $this -> grey     = imagecolorallocate($this->imageTemp, 0xCF, 0xCF, 0xCF);
          $this -> yellow   = imagecolorallocate($this->imageTemp, 0xFF, 0xFF, 0x99);
          $this -> black    = imagecolorallocate($this->imageTemp, 0x00, 0x00, 0x00);
          $this -> green    = imagecolorallocate($this->imageTemp, 0x66, 0xCC, 0x66);
          $this -> blue     = imagecolorallocate($this->imageTemp, 0x33, 0x99, 0xFF);
        }
       
        private function scaleConverter($nilai){
          $max = $this -> Height;
          $result = $this -> Height - ($nilai * ($this -> ScaleY / 10));
          return $result;
        }
       
        private function createSign($x,$y, $color = null){
          if($color == null){
            imagefilledellipse($this -> imageTemp, $x, $y, 5, 6, $this -> red);
          }else{
            imagefilledellipse($this -> imageTemp, $x, $y, 5, 6, $color);
          }
        }
       
        private function createNotes(){
          $this -> createSign( $this -> Width + 30 , 35);
          imagettftext($this -> imageTemp, 10, 0 ,$this -> Width + 40, 40 , $this -> red, $this -> Font, "TOTAL");         
          $this -> createSign( $this -> Width + 30 , 55, $this -> blue);
          imagettftext($this -> imageTemp, 10, 0 ,$this -> Width + 40, 60 , $this -> blue, $this -> Font, "RATA-RATA");
        }
     
   
   
    } 

?>

cara pake nya bisa seperti ini

<?

$sampelData = array(
      ‘1′ => array(
        ‘nama’  => ‘TO Pertama’,
        ‘total’ => 60,
        ‘rata’ => 56
      ),
      ‘2′ => array(
        ‘nama’  => ‘TO Kedua’,
        ‘total’ => 50,
        ‘rata’ => 40
      ),
      ‘3′ => array(
        ‘nama’  => ‘TO Ketiga’,
        ‘total’ => 43,
        ‘rata’ => 60
      ),
      ‘4′ => array(
        ‘nama’  => ‘TO Keempat’,
        ‘total’ => 12,
        ‘rata’ => 40,
      ),
      ‘5′ => array(
        ‘nama’  => ‘TO Kelima’,
        ‘total’ => 59,
        ‘rata’ => 40
      ),
      ‘6′ => array(
        ‘nama’  => ‘TO Keenam’,
        ‘total’ => 7,
        ‘rata’ => 10
      )
      ,
      ‘7′ => array(
        ‘total’ => 13,
        ‘rata’ => 34
      ),
      ‘8′ => array(
        ‘total’ => 4,
        ‘rata’ => 21
      ) ,
      ‘9′ => array(
        ‘total’ => 0,
        ‘rata’ => 40
      ),
      ‘10′ => array(
        ‘total’ => 30,
        ‘rata’ => 40
      )                                                           
    );
   
   
    $graph = new Grafik_Kacung;
    $graph -> setFont(’bookos.ttf’);
    $graph -> setData($sampelData);
    $graph -> setCanvas(350,550);

?>

 Kurang lebih hasilnya seperti ini:

Sample Grafik Kacung

Semoga Bermanfaat

1 Komentar »

  1. http://h20000.www2.hp.com/bizsupport/TechSupport/SoftwareDescription.jsp?lang=en&cc=us&prodTypeId=321957&prodSeriesId=3957663&prodNameId=3957664&swEnvOID=1093&swLang=13&mode=2&taskId=135&swItem=ob-76145-1

    Comment by Juragan — November 9, 2009 @ 7:09 am

RSS feed for comments on this post.

Silahkan Kirim Komen



Anti-spam measure: please retype the above text into the box provided.

email anda tidak akan ditampilkan ke publik, HTML yang di izinkan

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>