From bd49fb81e9eaa98814ae8eac2a29b2a772fb6bda Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 17 Feb 2007 18:32:45 +0000 Subject: [PATCH] We can now actually graph lines and points in GraphLine --- GraphLine.php | 64 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/GraphLine.php b/GraphLine.php index 4fbc14d..ac64b57 100644 --- a/GraphLine.php +++ b/GraphLine.php @@ -61,6 +61,12 @@ class BSGraphLine extends BSGraph */ private $axis = array(0 => 'X Axis', 1 => 'Y Axis'); + /** + * Number of ticks to display on the axes + * @var integer + */ + private $ticks = 10; + // ################################################################### /** * Does the actual graphing and returns a byte stream of a PNG image @@ -68,19 +74,7 @@ class BSGraphLine extends BSGraph * @return string Byte stream */ public function graph() - { - // calculates the standard deviation of the two piles to determine the x and y intervals - $xint = $this->_standardDeviation($this->piles[0]); - $yint = $this->_standardDeviation($this->piles[1]); - - $xmin = min($this->piles[0]); - $xmin = ($xmin - $xint < 0 ? 0 : $xmin - $xint); - $xmax = max($this->piles[0]) + $xint; - - $ymin = min($this->piles[1]); - $ymin = ($ymin - $yint < 0 ? 0 : $ymin - $yint); - $ymax = max($this->piles[1]) + $yint; - + { $colors = $this->_primeColors(); $this->_paintCanvas(); @@ -93,7 +87,22 @@ class BSGraphLine extends BSGraph $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']); + + // just to give us some padding + $this->ticks++; + + // calculates the standard deviation of the two piles to determine the x and y intervals + $xmin = min($this->piles[0]); + $xmax = max($this->piles[0]); + $xint = round(($xmax - $xmin) / $this->ticks); + $xmin = ($xmin - $xint < 0 ? 0 : $xmin - $xint); + $xmax = $xmax + $xint; + + $ymin = min($this->piles[1]); + $ymax = max($this->piles[1]); + $yint = round(($ymax - $ymin) / $this->ticks); + $ymin = ($ymin - $yint < 0 ? 0 : $ymin - $yint); + $ymax = $ymax + $yint; // label the axes imagestring($this->image, 3, $length / 2, $this->dimensions['height'] - self::SPACING - imagefontheight(3), $this->axis[0], $colors['black']); @@ -101,22 +110,39 @@ class BSGraphLine extends BSGraph // score the axes $count = 0; - for ($i = $originx; $i <= $endx; $i += ($length / $xint)) + for ($i = $originx; $i <= $endx; $i += ($length / $this->ticks)) { 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)) + for ($i = $originy; $i >= $endy; $i -= ($height / $this->ticks)) { imageline($this->image, $originx, $i, $endx, $i, $colors['grey']); - imagestring($this->image, 1, self::SPACING + self::PADDING, $i, round($count), $colors['black']); + imagestring($this->image, 1, self::SPACING + self::SPACING + self::PADDING + self::SPACING, $i - self::SPACING, round($count), $colors['black']); $count += $yint; } - header("Content-Type: image/png"); - imagepng($this->image); + // go through and plot each dataset + foreach ($this->dataset AS $data) + { + // plot each point and connect the dots + $oldpoint = null; + foreach ($data[1] AS $points) + { + $xcord = $originx + ($points[0] * ($length / $xmax)); + $ycord = $originy - ($points[1] * ($height / $ymax)); + imagefilledellipse($this->image, $xcord, $ycord, 5, 5, $data[2]); + if ($oldpoint) + { + imageline($this->image, $xcord, $ycord, $oldpoint[0], $oldpoint[1], $data[2]); + } + $oldpoint = array($xcord, $ycord); + } + } + + return $this->_imageFlush(); } // ################################################################### -- 2.43.5