All pastes #718741 Raw Edit

Something

public text v1 · immutable
#718741 ·published 2007-09-28 17:39 UTC
rendered paste body
<?php


/*

The Line Graph generator by Ashish Kasturia (http://www.123ashish.com)
Copyright (C) 2003 Ashish Kasturia (ashish at 123ashish.com)


The Line Graph generator is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 
USA.

*/

class Line
{
	var $width = 400;
	var $height = 300;
	var $bgCol;

	var $title = "Réponse musculaire";
	var $titleCol;

	var $dataValues = array();
	var $dataXLabels = array();
	var $dataSeriesLabels = array();
	var $barCol = array();
	var $axesCol;
	var $image;
	
	function InitializeColors()
	{
		$this->bgCol = ImageColorAllocate($this->image, 255, 255, 255);
		$this->titleCol = ImageColorAllocate($this->image, 0, 0, 0);
        // couleurs de chaque série
		$this->barCol[0] = ImageColorAllocate($this->image, 255, 100, 50);
		$this->barCol[1] = ImageColorAllocate($this->image, 0, 255, 0);
		$this->barCol[2] = ImageColorAllocate($this->image, 0, 0, 255);
		$this->barCol[3] = ImageColorAllocate($this->image, 0, 255, 255);
		$this->barCol[4] = ImageColorAllocate($this->image, 255, 0, 255);
		$this->barCol[5] = ImageColorAllocate($this->image, 255, 255, 0);
		$this->barCol[6] = ImageColorAllocate($this->image, 100, 100, 55);
		$this->barCol[7] = ImageColorAllocate($this->image, 55, 100, 100);
		$this->barCol[8] = ImageColorAllocate($this->image, 100, 55, 100);

		$this->axesCol = ImageColorAllocate($this->image, 200, 200, 200);
	}

	function Line()
	{
		$this->image = ImageCreate($this->width, $this->height);
		$this->InitializeColors();
		ImageFill($this->image, 0, 0, $this->bgCol);		
	}

	function SetDimensions($width, $height)
	{
		ImageDestroy($this->image);
		$this->height = $height;
		$this->width = $width;

		$this->image = ImageCreate($this->width, $this->height);
		InitializeColors();
		ImageFill($this->image, 0, 0, $this->bgCol);
	}

	function SetBGJPEGImage($file)
	{
		// SetBGJPEGImage() and SetDimensions() cannot be used together!!!
		ImageDestroy($this->image);
		$this->image = ImageCreateFromJPEG($file);
		$this->width = ImageSX($this->image);
		$this->height = ImageSY($this->image);
		$this->InitializeColors();
	}
	function SetBGPngImage($file)
	{
		// SetBGPngImage() and SetDimensions() cannot be used together
		ImageDestroy($this->image);
		$this->image = ImageCreateFromPng($file);
		$this->width = ImageSX($this->image);
		$this->height = ImageSY($this->image);
		$this->InitializeColors();
	}
	function SetBGGifImage($file)
	{
		// SetBGGifImage() and SetDimensions() cannot be used together
		ImageDestroy($this->image);
		$this->image = ImageCreateFromGif($file);
		$this->width = ImageSX($this->image);
		$this->height = ImageSY($this->image);
		$this->InitializeColors();
	}
	function SetBGColor($bgR, $bgG, $bgB)
	{
		ImageColorDeallocate($this->image, $this->bgCol);
		$this->bgCol = ImagecolorAllocate($this->image, $bgR, $bgG, $bgB);
	}

	function SetTitle($title)
	{
		$this->title = $title;
	}

	function SetTitleColor($bgR, $bgG, $bgB)
	{
		ImageColorDeallocate($this->image, $this->titleCol);
		$this->titleCol = ImagecolorAllocate($this->image, $bgR, $bgG, $bgB);
	}

	function AddValue($xVal, $yVal)
	{
		// $yVal is an array of y values
		$this->dataValues[] = $yVal;
		$this->dataXLabels[] = $xVal;
	}

	function SetSeriesLabels($labels)
	{
		$this->dataSeriesLabels = $labels;
	}

	function SetBarColor($bgR, $bgG, $bgB)
	{
//		ImageColorDeallocate($this->image, $this->barCol);
//		$this->barCol = ImagecolorAllocate($this->image, $bgR, $bgG, $bgB);
	}

	function SetAxesColor($bgR, $bgG, $bgB)
	{
		ImageColorDeallocate($this->image, $this->axesCol);
		$this->axesCol = ImagecolorAllocate($this->image, $bgR, $bgG, $bgB);
	}

	function spit($type)
	{
		// spit out the graph   -- fonction principale qui génère le graphe une fois les variables initialisées

		$black = ImageColorAllocate($this->image, 0, 0, 0);

		// draw the box
		ImageLine($this->image, 0, 0, $this->width - 1, 0, $black);
		ImageLine($this->image, $this->width - 1, 0, $this->width - 1, $this->height - 1, $black);
		ImageLine($this->image, $this->width - 1, $this->height - 1, 0, $this->height - 1, $black);
		ImageLine($this->image, 0, $this->height - 1, 0, 0, $black);

		// draw the axes ** (40,40) (x,y)0 & (80,40) (width-x,height-y)1
		// Y  - remplacer 10 par le nombre de divisions voulues
		for($i = 0; $i <= 10; $i++)
		{
			$y1 = 40 + (($i * ($this->height - 80)) / 10);
			ImageLine($this->image, 40, $y1, $this->width - 80, $y1, $this->axesCol);
		}
		// X
  		for($i = 0; $i <= 10; $i++)
		{
			$x1 = 40 + (($i * ($this->width - 120)) / 10);
			ImageLine($this->image, $x1, 40, $x1, $this->height - 40, $this->axesCol);
		}

		// calculate the max & min of each range
		$tmpArray = Array();
		$maxValues = Array();
		$numSequences = sizeof($this->dataValues[0]);

		for($i = 0; $i < $numSequences; $i++)
		{
			$tmpArray[$i] = Array();
			for($j = 0; $j < sizeof($this->dataValues); $j++)
			{
				$tmpArray[$i][] = $this->dataValues[$j][$i];
			}
		}
        // éventuellement modifier maxValues = roundUp(Max/1000)*1000 ou fixe...
		for($i = 0; $i < $numSequences; $i++)
		{
			$maxValues[$i] = max($tmpArray[$i]);
            $minValues[$i] = min($tmpArray[$i]);
		}

		// put the y axis values - 10 nombre d'axes y
        // à modifier pas assez de place si + de divisions!!!
        // rajouter un offset d'un caractère si $str est négatif...
		for($i = 0; $i <= 10; $i++)
		{
			$y1 = 40 + (($i * ($this->height - 80)) / 10);
			
			for($j = 0; $j < $numSequences; $j++)
			{
				//$str = sprintf("%.2f", ($maxValues[$j] * (10 - $i) / 10));
                $str = sprintf("%.2f", ($maxValues[$j] * (10 - $i) / 10 + $minValues[$j] * ($i) / 10));
				$strHeight = ImageFontHeight(2);
				ImageString($this->image, 2, 5, $y1 + (($j - $numSequences / 2) * $strHeight), $str, $this->barCol[$j % 9]);
			}
		}
		
		// put the title
		$titleWidth = ImageFontWidth(3) * strlen($this->title);
		ImageString($this->image, 3, ($this->width - $titleWidth) / 2, 10, $this->title, $this->titleCol);

		// put the series legend
		$legendWidth = ImageFontWidth(3) * strlen("Légende");
		ImageString($this->image, 3, $this->width - $legendWidth - 5, 40, "Légende", $this->titleCol);
		for($i = 0; $i < sizeof($this->dataSeriesLabels); $i++)
		{
			$legendWidth = ImageFontWidth(3) * strlen($this->dataSeriesLabels[$i]);
			ImageString($this->image, 3, $this->width - $legendWidth - 5, 60 + $i * ImageFontWidth(2) * 2, $this->dataSeriesLabels[$i], $this->barCol[$i % 9]);
		}

		// divide the area for the values - pas de l'axe X
		$xUnit = ($this->width - 120) / sizeof($this->dataValues);

		// finally draw the graphs
		$x2 = Array();
		$y2 = Array();		
		for($i = 0; $i < sizeof($this->dataValues); $i++)
		{
			$labelWidth = ImageFontWidth(1) * strlen($this->dataXLabels[$i]);
			$labelHeight = ImageFontHeight(1);

			ImageString($this->image, 1, 
				40 + $xUnit * ($i + 0.5) - $labelWidth / 2, 
				$this->height - 35 + ($i % 2) * $labelHeight, 
				$this->dataXLabels[$i], $this->titleCol);

			for($j = 0; $j < sizeof($this->dataValues[$i]); $j++)
			{
				$x1 = 40 + ($xUnit * ($i + 0.5));

				// $maxValues[$j] corresponds to $this->height - 80
				$tmpVal = $maxValues[$j] - $this->dataValues[$i][$j];
				// $tmpVal corresponds to ($tmpVal * ($this->height - 80)) / $maxValues[$j];
				$y1 = 40 + (($tmpVal * ($this->height - 80)) / ($maxValues[$j]-$minValues[$j]));
                // Uncomment pour afficher des carrés à chaque mesure....
                //ImageFilledRectangle($this->image, $x1 - 2, $y1 - 2, $x1 + 2, $y1 + 2, $this->barCol[$j % 9]);
				if($i != 0)
				{
					ImageLine($this->image, $x1, $y1, $x2[$j], $y2[$j], $this->barCol[$j % 9]);
				}
				$x2[$j] = $x1;
				$y2[$j] = $y1;
			}
		}


		if($type == "jpg")
		{
			Header("Content-type: image/jpeg");
			ImageJpeg($this->image);
		}
		if($type == "png")
		{
			Header("Content-type: image/png");
			ImagePng($this->image);
		}
		if($type == "gif")
		{
			Header("Content-type: image/gif");
			ImageGif($this->image);
		}
		
		ImageDestroy($this->image);

	}
}


?>