From 891c9e65de4e6f3b78ba32c318f0c361b93b181c Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 11 Feb 2006 02:44:28 +0000 Subject: [PATCH] The legend is now drawn all purdy --- graph_pie.php | 57 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/graph_pie.php b/graph_pie.php index d8525ae..1103204 100644 --- a/graph_pie.php +++ b/graph_pie.php @@ -116,33 +116,68 @@ class Graph_Pie $colours['black'] = imagecolorallocate($this->image, 0, 0, 0); $colours['white'] = imagecolorallocate($this->image, 255, 255, 255); + $graphpadding = 10; + $graphspacing = 3; + $diameter = $this->dimensions['height'] - (5 * $graphpadding); + $radius = $diameter / 2; + $graphstart = $graphpadding + imagefontheight(5) + $graphpadding; + $legendbox = 10; + // fill background imagefill($this->image, 0, 0, $colours['white']); // title the chart - imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, 10, $this->title, $colours['black']); + imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, $graphpadding, $this->title, $colours['black']); - $center = array('x' => ($this->dimensions['width'] / 2), 'y' => ($this->dimensions['height'] / 2) + 10); - $radius = ($this->dimensions['height'] - 55); + $center = array( + 'x' => ($this->legend ? ($radius + $graphpadding) : ($this->dimensions['width'] / 2)), + 'y' => ($this->dimensions['height'] / 2) + $graphpadding + ); // draw a border imageline($this->image, 0, 0, 0, $this->dimensions['height'], $colours['black']); // left imageline($this->image, 0, $this->dimensions['height'] - 1, $this->dimensions['width'], $this->dimensions['height'] - 1, $colours['black']); // bottom imageline($this->image, $this->dimensions['width'] - 1, 0, $this->dimensions['width'] - 1, $this->dimensions['height'], $colours['black']); // right imageline($this->image, 0, 0, $this->dimensions['width'], 0, $colours['black']); // top - - $lastdeg = 0; + $legx = (2 * $graphpadding) + $diameter; + + $lastdeg = 0; + $boxoffset = 0; foreach ($this->dataset AS $plot) { $deg = (360 / 100) * $plot[1]; - imagefilledarc($this->image, $center['x'], $center['y'], $radius, $radius, $lastdeg, $deg + $lastdeg, $plot[2], IMG_ARC_PIE); - //imagefilledarc($this->image, $center['x'], $center['y'], $radius, $radius, $lastdeg, $deg + $lastdeg, $colours['black'], IMG_ARC_EDGED | IMG_ARC_NOFILL); + imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $plot[2], IMG_ARC_PIE); + //imagefilledarc($this->image, $center['x'], $center['y'], $diameter, $diameter, $lastdeg, $deg + $lastdeg, $colours['black'], IMG_ARC_EDGED | IMG_ARC_NOFILL); $lastdeg = $deg; + + if ($this->legend) + { + $box = array( + $legx + 1 + $graphspacing, $graphstart + 1 + $graphspacing + $boxoffset, // top left + $legx + 1 + $graphspacing, $graphstart + 1 + $graphspacing + $boxoffset + $legendbox, // bottom left + $legx + 1 + $graphspacing + $legendbox, $graphstart + 1 + $graphspacing + $boxoffset + $legendbox, // bottom right + $legx + 1 + $graphspacing + $legendbox, $graphstart + 1 + $graphspacing + $boxoffset // top right + ); + imagefilledpolygon($this->image, $box, 4, $plot[2]); + + imagestring($this->image, 2, ($legx + 1 + $graphspacing + $legendbox + $graphspacing), ($graphstart + 1 + $graphspacing + $boxoffset), $plot[0] . " ($plot[1]%)", $colours['black']); + + $boxoffset += $graphspacing + $legendbox; + } } // draw the ellipse (do here so it cleans up the arc edges) - imageellipse($this->image, $center['x'], $center['y'], $radius, $radius, $colours['black']); + imageellipse($this->image, $center['x'], $center['y'], $diameter, $diameter, $colours['black']); + + // do the legend + if ($this->legend) + { + imageline($this->image, $legx, $graphstart, $this->dimensions['width'] - $graphpadding, $graphstart, $colours['black']); // top + imageline($this->image, $legx, $graphstart, $legx, $legy = ($graphstart + $graphspacing + $boxoffset + 1), $colours['black']); // left + imageline($this->image, $legx, $legy, $this->dimensions['width'] - $graphpadding, $legy, $colours['black']); // bottom + imageline($this->image, $this->dimensions['width'] - $graphpadding, $graphstart, $this->dimensions['width'] - $graphpadding, $legy, $colours['black']); // right + } imagepng($this->image); @@ -211,7 +246,11 @@ class Graph_Pie { static $colourlist = array( array(255, 0, 0), - array(100, 0, 0) + array(100, 0, 0), + array(0, 255, 0), + array(0, 100, 0), + array(0, 0, 255), + array(0, 0, 100) ); static $allocated; -- 2.22.5