Zend - The PHP Company




Graphs

Add Code


Easy Bar Chart  

Type: class
Added by: hermawan
Entered: 01/03/2002
Last modified: 03/12/2001
Rating: - (fewer than 3 votes)
Views: 11758
I guess you already had the Idea of this class. It create a Dynamic Bar Chart of your Dynamic Report, This class can be used for Finance Report, Site Log Report, or any other application that need Bar Chart Auto Generation


<?php 
 
/* 
  ******************************************************************** 
  * This script is written by Hermawan Haryanto [hermawan@haryanto]  * 
  * Objective   : To create Bar Chart of given Data                  * 
  *               Can be used for Finance Report, Log Report,        * 
  *               or anything that need bar chart                    * 
  * Create date : 09:25 - March 01, 2002                             * 
  * Licensed    : GPL - Copyleft                                     * 
  ******************************************************************** 
  */ 

  
class easyChart 
  

    var 
$title
    var 
$width
    var 
$height
    var 
$bartitle
    var 
$barheight
    var 
$barwidth
    var 
$background
    var 
$multiply
    var 
$spacebar
    var 
$leftspace
    var 
$rightspace
    var 
$upspace
    var 
$bottomspace
    var 
$leftlegend
    var 
$linecolor
    var 
$barcolor
    var 
$titlecolor
    var 
$legendcolor
    var 
$barinfocolor
    function 
easyChart() 
    { 
      
$this->title "Chart Title"
      
$this->background = Array(153,204,102); 
      
$this->linecolor = Array(240,240,240); 
      
$this->barcolor = Array(255,255,153); 
      
$this->titlecolor = Array(111,166,55); 
      
$this->legendcolor = Array(0,0,0); 
      
$this->barinfocolor = Array(0,0,0); 
      
$this->width 600
      
$this->height 500
      
$this->barwidth 20
      
$this->leftspace 75
      
$this->rightspace 25
      
$this->upspace 50
      
$this->bottomspace 50
      
$this->leftlegend 10
    } 
    function 
setTitleColor($color
    { 
      
$this->titlecolor explode(",",$color); 
    } 
    function 
setLegendColor($color
    { 
      
$this->legendcolor explode(",",$color); 
    } 
    function 
setBarInfoColor($color
    { 
      
$this->barinfocolor explode(",",$color); 
    } 
    function 
setBarColor($color
    { 
      
$this->barcolor explode(",",$color); 
    } 
    function 
setLineColor($color
    { 
      
$this->linecolor explode(",",$color); 
    } 
    function 
setLeftLegend($leftlegend
    { 
      
$this->leftlegend $leftlegend
    } 
    function 
setLeftSpace($leftspace
    { 
      
$this->leftspace $leftspace
    } 
    function 
setRightSpace($rightspace
    { 
      
$this->rightspace $rightspace
    } 
    function 
setTitle($title
    { 
      
$this->title $title
    } 
    function 
setBackground($color
    { 
      
$this->background explode(",",$color); 
    } 
    function 
setWidth($width
    { 
      
$this->width $width
    } 
    function 
setHeight($height
    { 
      
$this->height $height
    } 
    function 
setBarWidth($barwidth
    { 
      
$this->barwidth $barwidth
    } 
    function 
addBar($bartitle,$barheight
    { 
      
$this->bartitle[] = $bartitle
      
$this->barheight[] = $barheight
    } 
    function 
prepare() 
    { 
      
$arr $this->barheight
      
sort($arr); 
      
reset($arr); 
      
$this->highestvalue end($arr); 
      
$this->valueincriement $this->highestvalue $this->leftlegend
      
$this->multiply = (  ($this->height-($this->upspace $this->bottomspace)) / $this->highestvalue); 
      
$this->spacebar = ( ($this->width - ($this->leftspace $this->rightspace)) - ($this->barwidth count($arr) ) ) / (count($arr)+1); 
      
$this->legendspace = ($this->height - ($this->upspace+$this->bottomspace)) / $this->leftlegend
      if(
$this->barwidth count($this->barheight) > ($this->width-($this->upspace+$this->bottomspace))){ 
        
$err "Bar width is to large"
      } 
      if(
$err!=""){ 
        print 
$err
        exit; 
      } 
    } 
    function 
generateChart() 
    { 
      
$im ImageCreate($this->width,$this->height); 
      
$white ImageColorAllocate($im,255,255,255); 
      
$background ImageColorAllocate($im,$this->background[0],$this->background[1],$this->background[2]); 
      
$black ImageColorAllocate($im,0,0,0); 
      
$gray ImageColorAllocate($im,100,100,100); 
      
$linecolor ImageColorAllocate($im,$this->linecolor[0],$this->linecolor[1],$this->linecolor[2]); 
      
$barcolor ImageColorAllocate($im,$this->barcolor[0],$this->barcolor[1],$this->barcolor[2]); 
      
$titlecolor ImageColorAllocate($im,$this->titlecolor[0],$this->titlecolor[1],$this->titlecolor[2]); 
      
$legendcolor ImageColorAllocate($im,$this->legendcolor[0],$this->legendcolor[1],$this->legendcolor[2]); 
      
$barinfocolor ImageColorAllocate($im,$this->barinfocolor[0],$this->barinfocolor[1],$this->barinfocolor[2]); 
      @
ImageFilledRectangle($im,0,25,$this->width,$this->height-15,$background); 
      @
ImageFilledRectangle($im,1,26,$this->width-2,$this->height-16,$white); 
      @
ImageFilledRectangle($im,2,27,$this->width-3,$this->height-17,$background); 
      @
ImageFilledRectangle($im,$this->leftspace,$this->upspace-10,$this->width-$this->rightspace,$this->height-($this->bottomspace-20),$white); 
      
$titlewidth strlen($this->title) * ImageFontWidth(5); 
      
$titlexpos = ($this->width $titlewidth)/2
      @
ImageString ($im5$titlexpos5$this->title$titlecolor); 
      @
ImageString ($im10$this->height 10"easyChart.class - Get your own copy at http://hermawan.haryan.to it's Licensed under GNU Public License"$black); 
      for(
$i=0;$i<=$this->leftlegend;$i++){ 
        
$legendx 5
        
$legendy $this->upspace + ($this->legendspace $i) - 6
        @
ImageString($im2$legendx$legendy$this->highestvalue-($this->valueincriement*$i), $legendcolor);  
        @
ImageLine ($im$this->leftspace$legendy+6$this->width $this->rightspace$legendy+6$linecolor); 
      } 
      for(
$i=0;$i<count($this->barheight);$i++){ 
        
$j=$i+1
        
$x1 = ($j $this->spacebar) + ($i $this->barwidth) + $this->leftspace
        
$y1 $this->height - ($this->barheight[$i]*$this->multiply) - $this->upspace
        
$x2 $x1 $this->barwidth
        
$y2 $this->height $this->bottomspace
        
$bartitlewidth strlen($this->bartitle[$i]) * ImageFontWidth(2); 
        
$centerofbar $x1 + ($this->barwidth 2); 
        
$bartitlex $centerofbar - ($bartitlewidth/2); 
        
$bartitley $y2 5
        @
ImageFilledRectangle($im$x1$y1$x2$y2$barcolor); 
        @
ImageString($im2$bartitlex$bartitley$this->bartitle[$i], $barinfocolor); 
      } 
      @
ImageJPEG($im); 
      @
ImageDestroy($im);  
    } 
  }; 
?> 
<?php 
  $chart 
= new easyChart();                        // Object initialized 

  
$chart->setTitle("This is my Bar Chart Title");  // Title of Chart 
  
$chart->setBackground("153,204,102");            // Background of chart  [opt] 
  
$chart->setLineColor("225,225,225");             // Line color separator [opt] 
  
$chart->setBarColor("255,255,153");              // Bar color            [opt] 
  
$chart->setTitleColor("111,166,55");             // Color Title          [opt] 
  
$chart->setLegendColor("0,0,0");                 // Legend Color         [opt] 
  
$chart->setBarInfoColor("0,0,0");                // Bar Info Color       [opt] 

  
$chart->setWidth(600);                           // Width of Chart 
  
$chart->setHeight(500);                          // Height of Chart 
  
$chart->setBarWidth(20);                         // Bar Width 
  
$chart->setLeftSpace(75);                        // Left Space 
  
$chart->setRightSpace(25);                       // Right Space 
  
$chart->setLeftLegend(10);                       // Left Legend 

  
$chart->addBar("Jan",12000000);                  // Sample content of chart 
  
$chart->addBar("Peb",15000000); 
  
$chart->addBar("Mar",10000000); 
  
$chart->addBar("Apr",9000000); 
  
$chart->addBar("Mei",18000000); 
  
$chart->addBar("Jun",12000000); 
  
$chart->addBar("Jul",160000); 
  
$chart->addBar("Ags",14000000); 
  
$chart->addBar("Sept",12000000); 
  
$chart->addBar("Okt",17000000); 
  
$chart->addBar("Nop",14000000); 
  
$chart->addBar("Des",9000000); 

  
$chart->prepare();                               // Chart Preparation 
  
$chart->generateChart();                         // Chart Generation 
?> 


Usage Example


See the example


Rate This Script





Search



This Category All Categories