Jump to content

Graph Generation Script - I think I've got my class format wrong!


NickToony

Recommended Posts

Hello,

 

I am using a tutorial script for generating a basic bar graph. I have attempted to convert this script into an object-oriented script. However, upon attempting to define the graph, it is not successfully generated. Here is my code:

 

<?

class Graph
{
	var $values;
	var $image;
	var $image_width = 450;
	var $image_height = 300;
	var $margin = 20;
	var $bar_size = 20;
	var $horizontal_lines = 20;

	var $bar_colour;
	var $border_colour;
	var $background_colour;
	var $line_colour;

	function CreateGraph()
		{
			$this->image = imagecreate($this->image_width, $this->image_height);
		}

	function SetSize($width, $height)
		{
			$this->image_width = $width;
        			$this->image_height = $height;
        		}
        	
        	function SetMargin($size)
        		{
        			$this->margin = $size;
        		}
        		
        	function SetBarSize($size)
        		{
        			$this->bar_size = $size;
        		}
        		
        	function SetHorLines($size)
        		{
        			$this->horizontal_lines = $size;
        		}
        		
        	function HexConvert($rgb) 
        		{
    			return array(
			        base_convert(substr($rgb, 0, 2), 16, 10),
			        base_convert(substr($rgb, 2, 2), 16, 10),
			        base_convert(substr($rgb, 4, 2), 16, 10),
			    );
		}
        	
        	function SetColours($bars, $background, $border, $line)
        		{
        			// This is hex based, similar to CSS, it is converted by HexConvert
        			$bars = $this->HexConvert($bars);
        			$background= $this->HexConvert($background);
        			$border= $this->HexConvert($border);
        			$line = $this->HexConvert($line);
        			
        			$this->bar_colour = imagecolorallocate($this->image, $bars[0], $bars[1], $bars[2]);
        			$this->background_colour = imagecolorallocate($this->image, $background[0], $background[1], $background[2]);
        			$this->border_colour = imagecolorallocate($this->image, $border[0], $border[1], $border[2]);
        			$this->line_colour =imagecolorallocate($this->image, $line [0], $line [1], $line [2]);
        		}
        	
        	function DrawBorders()
        		{
        			imagefilledrectangle($this->image,1,1,$this->image_width-2,$this->image_height-2,$this->border_colour);
			imagefilledrectangle($this->image,$this->margin,$this->margin,$this->image_width-1-$this->margin,$this->image_height-1-$this->margin,$this->background_colour);
		}

	function DrawHorLines($horizontal_gap, $ratio)
		{
			for($i=1;$i<=$this->horizontal_lines;$i++)
				{
					$y = $this->image_height - $this->margin - $horizontal_gap * $i ;
					imageline($this->image, $this->margin, $y, $this->image_width - $this->margin, $y, $this->line_colour);
					$v = intval($horizontal_gap * $i / $ratio);
					imagestring($this->image, 0, 5, $y-5, $v, $this->bar_colour);
				}
		}

	function DrawBars($graph_height, $gap, $ratio)
		{
			for($i=0;$i< $total_bars; $i++)
				{ 
					list($key,$value)=each($this->values); 
					$x1= $this->margin + $gap + $i * ($gap+$this->bar_size) ;
					$x2= $x1 + $this->bar_size;
					$y1 = $this->margin +$graph_height- intval($value * $ratio) ;
					$y2 = $this->image_height-$this->margin;
					imagestring($this->image,0,$x1+3,$y1-10,$value,$this->bar_colour);
					imagestring($this->image,0,$x1+3,$this->image_height-15,$key,$this->bar_colour);		
					imagefilledrectangle($this->image,$x1,$y1,$x2,$y2,$this->bar_colour);
				}
		}
        	
        	function DrawGraph()
        		{
        			$graph_width=$this->image_width - $this->margin * 2;
			$graph_height=$this->image_height - $this->margin * 2; 

			$total_bars  = count($values);
			$gap = ($graph_width- $total_bars * $this->bar_width ) / ($total_bars +1);

			$this->DrawBorders();

			$max_value=max($values);
			$ratio = $graph_height / $max_value;

			$horizontal_gap = $graph_height / $horizontal_lines;

			$this->DrawHorLines($horizontal_gap, $ratio);

			$this->DrawBars($graph_height, $gap, $ratio);
		}

	function OutputGraph()
		{
			header("Content-type:image/png");
			imagepng($this->image);
		}
}

?>

 

I expect this to be my misunderstanding of how object orientated codes work. I'm also open to any suggestions for improvement.

 

Edit: Here is the script where I use the class.

<?
include "bargraph.class.php";

$graph = new Graph;
$graph->SetSize(450, 300);
$graph->CreateGraph();
$graph->SetMargin(20);
$graph->SetBarSize(20);
$graph->SetHorLines(20);
$graph->SetColours("FFFFFF", "C0C0C0", "000000", "000000");
$graph->DrawGraph();
$graph->OutputGraph();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.