#native_company# #native_desc#
#native_cta#

Line Chart/Graph Class using GD

By Stone Fox
on September 1, 2008

Version: 1

Type: Class

Category: Graphics

License: GNU General Public License

Description: A quick (dirty) class I wrote to plot a line chart based on given values. It is quite basic but is fairly usable.

<?php
Class DrawGraph
	{
	private $graph_data;
	private $image_holder;
	private $fixed_range_minimum;
	private $fixed_range_maximum;
	private $fixed_image_width;
	private $fixed_image_height;
	private $actual_image_height;
	private $image_padding;
	
	private $y_step_range;
	private $y_block_step;
	private $x_block_step;
	
	private $background_colour;
	private $line_1_colour;
	private $axis_line_colour;
	private $axis_text_colour;
	private $axis_value_colour;
	
	function __construct($data_array, $image_width=200, $image_height=200, $padding=10, $y_levels=10, $image_colour=array())
		{
		if (count($image_colour)!=3)
			$image_colour=array(240,240,240);
			
		$this->graph_data=$data_array;
		$this->image_padding=$padding;
		$this->fixed_image_width=$image_width;
		$this->fixed_image_height=$image_height;
		$this->actual_image_height=$image_height+($padding*2);
		
		$sorted_array=$data_array;
		sort($sorted_array);
		
		$this->fixed_range_minimum=floor($sorted_array[0]);
		$this->fixed_range_maximum=ceil($sorted_array[count($sorted_array)-1]);
		$actual_range=$this->fixed_range_maximum-$this->fixed_range_minimum;
		
		$this->y_step_range=ceil($actual_range/$y_levels);
		$this->y_block_step=$image_height/$actual_range;
		$this->x_block_step=$image_width/count($data_array);

		$this->image_holder = @ImageCreate ($image_width+($padding*4), $this->actual_image_height + ($padding*4) );

		$this->background_colour = ImageColorAllocate ($this->image_holder, $image_colour[0], $image_colour[1], $image_colour[2]);
		$this->title_text_colour = ImageColorAllocate ($this->image_holder, 0, 0, 0);
		$this->line_1_colour = ImageColorAllocate ($this->image_holder, 233, 14, 91);
		$this->axis_line_colour = ImageColorAllocate ($this->image_holder, 0, 0, 254);
		$this->axis_text_colour = ImageColorAllocate ($this->image_holder, 0, 0, 0);
		$this->axis_value_colour = ImageColorAllocate ($this->image_holder, 0, 0, 0);
		}
				
	function drawYAxis()
		{
		for($a=$this->fixed_range_minimum; $a<=$this->fixed_range_maximum; $a+=$this->y_step_range)
			{
			$ypos=round( ($this->fixed_image_height+$this->image_padding) - ( ( $a - $this->fixed_range_minimum ) * $this->y_block_step) );
			imageline($this->image_holder, ($this->image_padding*2)-6, $ypos, ($this->image_padding*2)-2, $ypos, $this->axis_line_colour);
			imagestring($this->image_holder, 1, 2, $ypos-8, $a, $this->axis_text_colour);
			}
		}
		
	function drawData($x_axis_labels=array(), $title="")
		{
		imagestring($this->image_holder, 2, $this->fixed_image_width/2, ($this->actual_image_height + ($this->image_padding*2))+($this->image_padding/2), $title, $this->title_text_colour);			

		$this->drawYAxis();

		//Pad x axis label array if it doesnt have enough values in
		if (count($x_axis_labels)<count($this->graph_data))
			$x_axis_labels = array_pad($x_axis_labels, count($this->graph_data), "");
			
		for($a=0; $a<count($this->graph_data); $a++)
			{
			//Draw x axis line
			imageline($this->image_holder, ($a*$this->x_block_step)+($this->image_padding*3), ($this->actual_image_height+$this->image_padding)-2, ($a*$this->x_block_step)+($this->image_padding*3), ($this->actual_image_height+$this->image_padding)-7, $this->axis_line_colour);
			//Draw x axis label
			imagestring($this->image_holder, 1, ( ($a*$this->x_block_step) + $this->image_padding) + ($this->image_padding*2), $this->actual_image_height + $this->image_padding, $x_axis_labels[$a], $this->axis_text_colour);			
			//Draw Point Value
			imagestring($this->image_holder, 1, ( ($a*$this->x_block_step) + ($this->image_padding*3)) + 2, round( ($this->fixed_image_height+$this->image_padding) - ( ( $this->graph_data[$a] - $this->fixed_range_minimum ) * $this->y_block_step) ), $this->graph_data[$a], $this->axis_value_colour);
				
			//If this is the second value then can plot a line
			if ($a>0)
				{
				$x1=(($a-1)*$this->x_block_step)+($this->image_padding*3);
				$x2=($a*$this->x_block_step)+($this->image_padding*3);
				$y1=($this->fixed_image_height + $this->image_padding) - (( $this->graph_data[$a-1] - $this->fixed_range_minimum ) * $this->y_block_step);
				$y2=($this->fixed_image_height + $this->image_padding) - (( $this->graph_data[$a] - $this->fixed_range_minimum ) * $this->y_block_step);
				imageline ($this->image_holder, $x1, $y1, $x2, $y2, $this->line_1_colour);
				}
			}		
		}
		
	function rotateGraph($value)
		{
		$this->image_holder=imagerotate($this->image_holder, $value, 0);
		}
		
	function outputGraph()
		{
		header ("Content-type: image/png");
		ImagePng($this->image_holder);
		}
		
	function updateColour($type, $colourData=array(0,0,0))
		{
		if (count($colourData)!=3)
			$type="";
		switch ($type)
			{
			case "title":
				$this->title_text_colour = ImageColorAllocate ($this->image_holder, $colourData[0], $colourData[1], $colourData[2]);
				break;
			case "line1":
			    $this->line_1_colour = ImageColorAllocate ($this->image_holder, $colourData[0], $colourData[1], $colourData[2]);
			    break;
			case "axis_line":
			    $this->axis_line_colour = ImageColorAllocate ($this->image_holder, $colourData[0], $colourData[1], $colourData[2]);
			    break;
			case "axis_text":
			    $this->axis_text_colour = ImageColorAllocate ($this->image_holder, $colourData[0], $colourData[1], $colourData[2]);
			    break;
			case "axis_value":
			    $this->axis_value_colour = ImageColorAllocate ($this->image_holder, $colourData[0], $colourData[1], $colourData[2]);
			    break;
			}
		}
		
	}
?>