From 5d7d29bdef5d3d855d5727c8589791c7f7c05af4 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 17 Feb 2007 03:56:55 +0000 Subject: [PATCH] - Moving some important things (like painting a blank canvas and allocating common colors) to BSGraph from BSGraphPie - We can now properly label our axes in BSGraphLine, though we don't show any points --- Graph.php | 48 +++++++++++++++++++++++++++++++++++++++++++ GraphLine.php | 56 +++++++++++++++++++++++++++++++++++++++++++++++++- GraphPie.php | 48 +++++++++++++++---------------------------- docs/phpdoc.sh | 2 +- 4 files changed, 121 insertions(+), 33 deletions(-) diff --git a/Graph.php b/Graph.php index 9e7b93f..fcc6357 100644 --- a/Graph.php +++ b/Graph.php @@ -69,6 +69,18 @@ abstract class BSGraph */ protected $title = 'ISSO Pie Chart'; + /** + * Padding in the graph + * @var integer + */ + const PADDING = 10; + + /** + * Spacing in the graph + * @var integer + */ + const SPACING = 4; + // ################################################################### /** * Constructor @@ -192,6 +204,42 @@ abstract class BSGraph return imagecolorallocate($this->image, $color[0], $color[1], $color[2]); } + + // ################################################################### + /** + * Draws a white box, the title, and then a black border around the graph + */ + protected function _paintCanvas() + { + $colors = $this->_primeColors(); + + // fill background + imagefill($this->image, 0, 0, $colors['white']); + + // title the chart + imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, self::PADDING, $this->title, $colors['black']); + + // draw a border + imageline($this->image, 0, 0, 0, $this->dimensions['height'], $colors['black']); // left + imageline($this->image, 0, $this->dimensions['height'] - 1, $this->dimensions['width'], $this->dimensions['height'] - 1, $colors['black']); // bottom + imageline($this->image, $this->dimensions['width'] - 1, 0, $this->dimensions['width'] - 1, $this->dimensions['height'], $colors['black']); // right + imageline($this->image, 0, 0, $this->dimensions['width'], 0, $colors['black']); // top + } + + // ################################################################### + /** + * Returns an array of colors that are useful (black, white, and grey) + * + * @return array Colors + */ + protected function _primeColors() + { + $colors = array(); + $colors['black'] = imagecolorallocate($this->image, 0, 0, 0); + $colors['white'] = imagecolorallocate($this->image, 255, 255, 255); + $colors['grey'] = imagecolorallocate($this->image, 121, 121, 123); + return $colors; + } } /*=====================================================================* diff --git a/GraphLine.php b/GraphLine.php index 466f235..4fbc14d 100644 --- a/GraphLine.php +++ b/GraphLine.php @@ -55,6 +55,12 @@ class BSGraphLine extends BSGraph */ private $piles = array(0 => array(), 1 => array()); + /** + * The names of the axes + * @var array + */ + private $axis = array(0 => 'X Axis', 1 => 'Y Axis'); + // ################################################################### /** * Does the actual graphing and returns a byte stream of a PNG image @@ -72,10 +78,45 @@ class BSGraphLine extends BSGraph $xmax = max($this->piles[0]) + $xint; $ymin = min($this->piles[1]); - $ymin = ($ymin - $int < 0 ? 0 : $ymin - $yint); + $ymin = ($ymin - $yint < 0 ? 0 : $ymin - $yint); $ymax = max($this->piles[1]) + $yint; + $colors = $this->_primeColors(); + $this->_paintCanvas(); + + // draw the axes + $originx = self::PADDING + imagefontwidth(1) + self::SPACING + imagefontwidth(3) + self::PADDING; + $originy = $this->dimensions['height'] - (self::PADDING + imagefontheight(1) + self::SPACING + imagefontheight(3) + self::SPACING); + $endx = $this->dimensions['width'] - self::PADDING - 150 - self::PADDING; + $endy = 40; + $length = $endx - $originx; + $height = $originy - $endy; + imageline($this->image, $originx, $originy, $endx, $originy, $colors['grey']); + imageline($this->image, $originx, $originy, $originx, $endy, $colors['grey']); + imageline($this->image, $endx, $originy, $endx, $endy, $colors['grey']); + + // label the axes + imagestring($this->image, 3, $length / 2, $this->dimensions['height'] - self::SPACING - imagefontheight(3), $this->axis[0], $colors['black']); + imagestringup($this->image, 3, self::SPACING, $height / 2 + $endy, $this->axis[1], $colors['black']); + + // score the axes + $count = 0; + for ($i = $originx; $i <= $endx; $i += ($length / $xint)) + { + imageline($this->image, $i - self::SPACING, $originy + self::SPACING, $i + self::SPACING, $originy - self::SPACING, $colors['grey']); + imagestring($this->image, 1, $i, $originy + self::PADDING, round($count), $colors['black']); + $count += $xint; + } + $count = 0; + for ($i = $originy; $i >= $endy; $i -= ($height / $yint)) + { + imageline($this->image, $originx, $i, $endx, $i, $colors['grey']); + imagestring($this->image, 1, self::SPACING + self::PADDING, $i, round($count), $colors['black']); + $count += $yint; + } + header("Content-Type: image/png"); + imagepng($this->image); } // ################################################################### @@ -223,6 +264,19 @@ class BSGraphLine extends BSGraph { return array_sum($vals) / count($vals); } + + // ################################################################### + /** + * Sets the titles of the two axes + * + * @param string X-axis name + * @param string Y-axis name + */ + public function setAxes($xaxis, $yaxis) + { + $this->axis[0] = $xaxis; + $this->axis[1] = $yaxis; + } } /*=====================================================================* diff --git a/GraphPie.php b/GraphPie.php index 127077c..ac5c574 100644 --- a/GraphPie.php +++ b/GraphPie.php @@ -56,35 +56,21 @@ class BSGraphPie extends BSGraph */ public function graph() { - $colors['black'] = imagecolorallocate($this->image, 0, 0, 0); - $colors['white'] = imagecolorallocate($this->image, 255, 255, 255); - $colors['grey'] = imagecolorallocate($this->image, 121, 121, 123); + $colors = $this->_primeColors(); - $graphpadding = 10; - $graphspacing = 4; - $diameter = $this->dimensions['height'] - (5 * $graphpadding); + $diameter = $this->dimensions['height'] - (5 * self::PADDING); $radius = $diameter / 2; - $graphstart = $graphpadding + imagefontheight(5) + $graphpadding; + $graphstart = self::PADDING + imagefontheight(5) + self::PADDING; $legendbox = 10; - // fill background - imagefill($this->image, 0, 0, $colors['white']); - - // title the chart - imagestring($this->image, 5, ($this->dimensions['width'] - (imagefontwidth(5) * strlen($this->title))) / 2, $graphpadding, $this->title, $colors['black']); + $this->_paintCanvas(); $center = array( - 'x' => ($this->legend ? ($radius + $graphpadding) : ($this->dimensions['width'] / 2)), - 'y' => ($this->dimensions['height'] / 2) + $graphpadding + 'x' => ($this->legend ? ($radius + self::PADDING) : ($this->dimensions['width'] / 2)), + 'y' => ($this->dimensions['height'] / 2) + self::PADDING ); - // draw a border - imageline($this->image, 0, 0, 0, $this->dimensions['height'], $colors['black']); // left - imageline($this->image, 0, $this->dimensions['height'] - 1, $this->dimensions['width'], $this->dimensions['height'] - 1, $colors['black']); // bottom - imageline($this->image, $this->dimensions['width'] - 1, 0, $this->dimensions['width'] - 1, $this->dimensions['height'], $colors['black']); // right - imageline($this->image, 0, 0, $this->dimensions['width'], 0, $colors['black']); // top - - $legx = (2 * $graphpadding) + $diameter; + $legx = (2 * self::PADDING) + $diameter; $lastdeg = 0; $boxoffset = 0; @@ -98,16 +84,16 @@ class BSGraphPie extends BSGraph 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 + $legx + 1 + self::SPACING, $graphstart + 1 + self::SPACING + $boxoffset, // top left + $legx + 1 + self::SPACING, $graphstart + 1 + self::SPACING + $boxoffset + $legendbox, // bottom left + $legx + 1 + self::SPACING + $legendbox, $graphstart + 1 + self::SPACING + $boxoffset + $legendbox, // bottom right + $legx + 1 + self::SPACING + $legendbox, $graphstart + 1 + self::SPACING + $boxoffset // top right ); imagefilledpolygon($this->image, $box, 4, $plot[2]); - imagestring($this->image, 2, ($legx + 1 + $graphspacing + $legendbox + $graphspacing), ($graphstart + $graphspacing + $boxoffset), $plot[0] . " ($plot[1]%)", $colors['black']); + imagestring($this->image, 2, ($legx + 1 + self::SPACING + $legendbox + self::SPACING), ($graphstart + self::SPACING + $boxoffset), $plot[0] . " ($plot[1]%)", $colors['black']); - $boxoffset += $graphspacing + $legendbox; + $boxoffset += self::SPACING + $legendbox; } } @@ -117,10 +103,10 @@ class BSGraphPie extends BSGraph // do the legend if ($this->legend) { - imageline($this->image, $legx, $graphstart, $this->dimensions['width'] - $graphpadding, $graphstart, $colors['black']); // top - imageline($this->image, $legx, $graphstart, $legx, $legy = ($graphstart + $graphspacing + $boxoffset + 1), $colors['black']); // left - imageline($this->image, $legx, $legy, $this->dimensions['width'] - $graphpadding, $legy, $colors['black']); // bottom - imageline($this->image, $this->dimensions['width'] - $graphpadding, $graphstart, $this->dimensions['width'] - $graphpadding, $legy, $colors['black']); // right + imageline($this->image, $legx, $graphstart, $this->dimensions['width'] - self::PADDING, $graphstart, $colors['black']); // top + imageline($this->image, $legx, $graphstart, $legx, $legy = ($graphstart + self::SPACING + $boxoffset + 1), $colors['black']); // left + imageline($this->image, $legx, $legy, $this->dimensions['width'] - self::PADDING, $legy, $colors['black']); // bottom + imageline($this->image, $this->dimensions['width'] - self::PADDING, $graphstart, $this->dimensions['width'] - self::PADDING, $legy, $colors['black']); // right } ob_start(); diff --git a/docs/phpdoc.sh b/docs/phpdoc.sh index f10ad04..898cb4e 100755 --- a/docs/phpdoc.sh +++ b/docs/phpdoc.sh @@ -31,4 +31,4 @@ mkdir apidoc # hilight source code # output options -/Server/bin/phpdoc -d ../ -t ./apidoc -i ../UnitTest/ -i ../docs/ -dh off -ti "Blue Static ISSO Framework" -dn ISSO -dc ISSO -s on -o HTML:Smarty:PHP > ./apidoc/log.txt \ No newline at end of file +/Server/bin/phpdoc -d ../ -t ./apidoc -i ../UnitTest/ -i * -dh off -ti "Blue Static ISSO Framework" -dn ISSO -dc ISSO -s on -o HTML:Smarty:PHP > ./apidoc/log.txt \ No newline at end of file -- 2.43.5