Location: PHPKode > scripts > Dia > dia/dia.class.php
<?php
/**
 * Ãåíåðàòîð êà÷åñòâåííûõ äèàãðàìì â TrueColor ñ ïîääåðæêîé êèðèëëèöû.
 * Ñèñòåìíûå òðåáîâàíèÿ: PHP4.3.2+, GD2+TTF
 * @example
 *		<code>
 *			// ñîçäàíèå ïîëîòíà 200 íà 200 òî÷åê
 *			$dia = new dia(200,200);
 *			// äîáàâëåíèå äâóõ êàòåãîðèé îòñ÷åòîâ
 *			$dia->category('Ðîñò');
 *			$dia->category('Âåñ');
 *			// äîáàâëåíèå îòñ÷åòîâ
 *			$dia->record('ß', 172, 70);
 *			$dia->record('Âàñÿ',165,60);
 *			$dia->record('Ïåòÿ',181,75);
 *			// âûâîä äèàãðàììû â àáñîëþòíîì âèäå
 *			$dia->drawAbsolute();
 *			// äîáàâëåíèå ëåãåíäû â íèæíèé ïðàâûé óãîë
 *			$dia->legend(5);
 *			// ñîõðàíåíèå â ôàéë â ôîðìàòå PNG
 *			$dia->save('sample.png','PNG');
 *		</code>
 * @author Stas Dovgodko <hide@address.com>, <http://standov.freehosting.kiev.ua>
 * @package Dia
 */


	/**
	 * óíèâåðñàëüíûé êëàññ, ãåíåðèðóþùèé äèàãðàììû
	 * @author Stas Dovgodko
	 * @version 0.2b
	 * @build 30.11.2003
	 * @changes:
	 *		0.2b
	 * 			1. Äîáàâëåíà âîçìîæíîñòü âûïîëíÿòü eval() {@link dia::evaluate()}
	 *			2. Ñîâìåñòèìîñòü ñ phpDoc, äîáàâëåíû êîììåíòû è èñïðàâëåíû íàçâàíèÿ êëàññîâ
	 *			3. Ìåòîäû dia::_t è dia::_c òåïåðü íå ñîäåðæàò óêàçàòåëü íà âíåøíèé ðèñóíîê
	 */
	class dia {
		/** @access private */
		var $_width; 
		/** @access private */
		var $_height;
		/** @access private */
		var $_im;
		
		/** @access private */
		var $_legend = array();
		/** @access private */
		var $_data;
		
		/** @access private */
		var $_background;
		
		/**
		 * Ïàëèòðà èñïîëüçóåìûõ öâåòîâ, öâåòà èñïîëüçóþòñÿ ïî ïîðÿäêó
		 * @access private
		 */
		var $_palette = array('#CC0000', '#CC9900', '#00CC00', '#00CCCC', '#0000CC','#9900CC');
		
		
		/**
		 * Èìÿ ôàéëà TrueType øðèôòà, èñïîëüçóåìîãî ïðè ãåíåðàöèè íàäïèñåé
		 * @access private
		 */
		var $_TTfont_filename = 'arial.ttf';

		/**
		 * Cîîáùåíèå î âîçíèêøåé îøèáêå
		 */
		var $error;

		/**
		 * Ñîçäàíèå íîâîé äèàãðàììû (ïîëîòíà) ñ çàäàííûìè ðàçìåðàìè è öâåòîì ôîíà.
		 *  êà÷åñòâå ïîëîòíà äèàãðàììû ìîæåò èñïîëüçîâàòüñÿ çàäàíûé ðèñóíîê.
		 * Åñëè çàäàí ðèñóíîê èñïîëüçóþòñÿ åãî ðàçìåðû.
		 *
		 * @param int $width Øèðèíà ïîëîòíà â ïèêñåëàõ
		 * @param int $height Âûñîòà ïîëîòíà â ïèêñåëàõ
		 * @param string $background Öâåò ôîíà â âèäå '#RRGGBB'
		 * @param resource $image Ðèñóíîê, èñïîëüçóåìûé â êà÷åñòâå ïîëîòíà
		 * @return dia
		 */
		function dia($width = 250, $height = 250, $background = '#FFFFFF', $image = false) {
			
			$this->_width = $width;
			$this->_height = $height;

			if ($image) {
				// ïîëó÷åíèå ðàçìåðîâ ïî ïîäëîæêå
				$this->_width = imagesx ($image);
				$this->_height = imagesy ($image);
				// ïðîâåðêà èçîáðàæåíèÿ ïîäëîæêè íà ôîðìàò
				if (!$this->_im = imageistruecolor ($image)) $this->error = " êà÷åñòâå ïîäëîæêè ìîãóò èñïîëüçîâàòüñÿ òîëüêî TrueColor èçîáðàæåíèÿ";
			} else {
				if (!$this->_im = @imagecreatetruecolor ($this->_width, $this->_height)) $this->error = "Îøèáêà ïðè ñîçäàíèè TrueColor ïîäëîæêè";
			    else {
					imagesavealpha ($this->_im, true);
					imagefill($this->_im, 0, 0,$this->_c($this->_background = $background));
				}
			}
		}

		/**
		 * Âûäåëÿåò RGB ñîñòàâëÿþùèå èç êîäà öâåòà â øåñòíàäöàòèðè÷íîì ôîðìàòå.
		 * Ñîçäàåò ïîëó÷åííûé öâåò â çàäàííîì ðèñóíêå ñ çàäàííîé ïðîçðà÷íîñòüþ äëÿ TrueColor
		 *
		 * @access private
		 * @param string $hexcolor
		 * @param int $alpha
		 * @return int
		 */
		function _c($hexcolor, $alpha = false) {
			$dec_color = hexdec($hexcolor);
			$R = ($dec_color & hexdec('FF0000')) >> 16;
			$G = ($dec_color & hexdec('00FF00')) >> 8;
			$B = $dec_color & hexdec('0000FF');

			if (!$alpha) return imagecolorallocate($this->_im, $R, $G, $B);
			else return imagecolorallocatealpha($this->_im, $R, $G, $B, $alpha);
		}


		
		/**
		 * Âîçâðàùàåò Hex-êîä öâåòà â ïàëèòðå ïîä çàäàííûì íîìåðîì.
		 * Åñëè â ïàëèòðå îòñóòâóåò óêàçàííûé öâåò - ãåíåðèðîâàíèå íîâîãî ñëó÷àéíîãî
		 *
		 * @access private
		 * @param int $index
		 * @return string
		 */
		function _p($index) {
			$palette = &$this->_palette;
			if (isset($palette[$index]) and $color = $palette[$index]) {
				// çàäàííàÿ ïàëèòðà ñîäåðæèò òðåáóåìûé öâåò
				return $color;
			} else {
				// çàäàííàÿ ïàëèòðà ìåíüøå ÷åì ÷èñëî êàòåãîðèé - ãåíåðèðîâàíèå íîâîãî öâåòà
				$color = dechex(rand (hexdec('999999'),hexdec('CCCCCC')));
				$palette[$index] = $color;
				return $color;
			}
		}

		/**
		 * Îòîáðàæåíèå íàäïèñè â çàäàííîé òî÷êå ðèñóíêà ñ çàäàíûì íàêëîíîì, çàäàííûìè öâåòîì è ðàçìåðîì øðèôòà
		 * ïðè $test=true - âîçâðàùàåò ðàçìåðû ãåíåðèðóåìîé íàäïèñè áåç åå âûâîäà
		 *
		 * @access private
		 * @param string $text Òåêñò íàäïèñè
		 * @param int $x Ïîçèöèÿ íàäïèñè ïî ãîðèçîíòàëè
		 * @param int $y Ïîçèöèÿ íàäïèñè ïî âåðòèêàëè
		 * @param string $color Öâåò âûâîäà òåêñòà â ôîðìàòå '#RRGGBB'
		 * @param float $angle Óãîë íàêëîíà íàäïèñè
		 * @param int $size Ðàçìåð øðèôòà âûâîäà òåêñòà
		 * @param boolean $test Ôëàã òåñòèðîâàíèÿ áåç âûâîäà
		 * @return array Ïàðàìåòðû ðàñïîëîæåíèÿ íàäïèñè íà äèàãðàììå
		 */
		function _t($text, $x, $y, $color = false, $angle = 0, $size = false, $test = false) {
			$text = iconv('windows-1251','utf-8', $text);
			
			$font_filename = dirname(__FILE__).'/'.$this->_TTfont_filename;
			if (!$color) $color = $this->_c('#000000');
			if (!$size) $size = 7;
			if (!$test) return imagettftext($this->_im, $size, $angle, $x, $y, $color, $font_filename, $text);
			else return imagettfbbox ($size, $angle, $font_filename, $text);
  
		}

		
		/**
		 * Äîáàâëÿåò êàòåãîðèþ è ãåíåðèðóåò èíôîðìàöèþ äëÿ âûâîäà ëåãåíäû
		 *
		 * @param string $name Íàèìåíîâàíèå êàòåãîðèè
		 * @return true
		 */
		function category($name) {
			$legend = &$this->_legend;
			$category_index = sizeof($legend);

			// ãåíåðèðîâàíèå öâåòà ïàëèòðû
			$color = $this->_p($category_index);

			// äîáàâëåíèå öâåòà â ïàëèòðó
			$this->_c($color);
			// ãåíåðèðîâàíèå çàïèñè "ëåãåíäû"
			$legend[$name] = $color;
			return true;
		}

		/**
		 * Äîáàâëÿåò ãðóïïó îòñ÷åòîâ ïî êàòåãîðèÿì
		 *
		 * @param string $name Íàèìåíîâàíèå ãðóïïû îòñ÷åòîâ
		 * @param ...
		 * @return true
		 */
		function record($caption) {
			$numargs = func_num_args();
			$arg_list = func_get_args();
    

			if ($numargs > 1) {
				$data = &$this->_data;

				for($i = 1; $i < $numargs; $i++) $data[$caption][] = $arg_list[$i];
			}
			return true;
		}
		
		/**
		 * Îñóùåñòâëÿåòñÿ ïîâîðîò äèàãðàììû íà çàäàííûé â ãðàäóñàõ óãîë ïî ÷àñîâîé ñòðåëêå
		 * @param float $angle Óãîë ïîâîðîòà ïî ÷àñòîâîé ñòðåëêå, åñëè çàäàíî îòðèöàòåëüíîå çíà÷åíèå - ïðîòèâ ÷àñîâîé
		 *					   Ïî óìîë÷àíèþ ïîâîðîò íà 90ãðàäóñîâ ïðîòèâ ÷àñîâîé ñòðåëêè
		 * @since 0.2b
		 */
		function rotate($angle = -90) {
			$this->_im = imagerotate ($this->_im, -1*$angle, $this->_c($this->_background));
		}

		
		/**
		 * Ôîðìèðîâàíèå ëåãåíäû â çàäàííîì ìåñòå ðèñóíêà
		 * ïðàâèëó óëèòêè èç âåðõíåãî ëåâîãî óãëà
		 * <pre>
		 *	1 2 3
		 *	8 9 4
		 *	7 6 5
		 * </pre>
		 *
		 * @param 1..9 $position Íîìåð ïîçèöèè
		 * @param int $fontsize Ðàçìåð øðèôòà çàïèñåé ëåãåíäû â ïèêñåëàõ
		 * @return true
		 */
		function legend($position = 3, $fontsize = 8) {
			$legend = &$this->_legend;

			// îïðåäåëåíèå ðàçìåðîâ íàäïèñè ëåãåíäû
			$legend_width = 0;
			$legend_height = 5;
			foreach($legend as $caption=>$color) {
				list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t($caption, 1,1, false, 0,$fontsize ,true);
				$legend_width = ceil(max($legend_width, $lrx - $llx));
 				$legend_height += ($lly - $uly) + 3;
			}

			$legend_width += 20;

			// îïðåäåëåíèå ïîçèöèè ëåãåíäû
			switch($position) {
				case 1:case 8:case 7:
					$x = 10;
					break;
				case 2:case 9:case 6:
					$x = floor(($this->_width - $legend_width)/2);
					break;
				default:
					$x = $this->_width - $legend_width - 10;
					
			}
			switch($position) {
				case 1:case 2:case 3:
					$y = 10;
					break;
				case 8:case 9:case 4:
					$y = floor(($this->_height - $legend_height)/2);
					break;
				default:
					$y = $this->_height - $legend_height - 10;
					
			}

			// îòîáðàæåíèå ëåãåíäû
			// îòîáðàæåíèå ïîëóïðîçðà÷íîãî ôîíà
			imagefilledrectangle ($this->_im, $x,$y, $x + $legend_width, $y + $legend_height, $this->_c($this->_background,30));
			// îòîáðàæåíèå "íàäïèñåé"
			$caption_y = $y+15;
			foreach($legend as $caption=>$color) {
				list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t($caption, 1,1, false, 0,$fontsize ,true);
				$this->_t($caption, $x+15,$caption_y, false, 0,$fontsize);
				imagefilledrectangle ($this->_im, $x+5,$caption_y-7, $x + 10, $caption_y + 5-7, $this->_c($color));
			
				$caption_y += ($lly - $uly) + 3;
			}
						
			return true;
		}

		
		/**
		 * îòîáðàæàåò àáñîëþòíûå çíà÷åíèÿ îòñ÷åòîâ â ãðóïïàõ
		 * @param int $barSize Ìèíèìàëüíàÿ øèðèíà ñòîëáöà îòñ÷åòà âíóòðè ãðóïïû, â ïèêñåëàõ
		 * @param int $axesFontSize Ðàçìåð øðèôòà ïîäïèñåé íà îñÿõ, â ïèêñåëàõ 
		 * @param int $valuesFontSize Ðàçìåð øðèôòà òåêòîâûõ çíà÷åíèé îòñ÷åòîâ, â ïèêñåëàõ
		 * @return void
		 */
		function drawAbsolute($barSize = 10, $axesFontSize = 8, $valuesFontSize = 8) {
			if ($this->_data and $this->_height > 80) {
				$data = $this->_data;
				$cols = sizeof($data);

				// îïðåäåëåíèå ìàêñèìàëüíîãî êîëè÷åñòâà áëîêîâ âíóòðè ñòîëáöà è ôîðìèðîâàíèå ïàëèòðû
				$blocks_count = $abs_max =  $header_width = 0;
				foreach($data as $caption=>$data_column) {
					$blocks_count = max($blocks_count, sizeof($data_column));
					$abs_max = max($abs_max, max($data_column));
									
					list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t($caption, 1,1, false, 0,$axesFontSize ,true);
					$header_width = max($header_width, $lrx - $llx);
				}

				// îïðåäåëåíèå ïàðàìåòðîâ ñåòêè è îòîáðàæåíèå
				$grid_x1 = $header_width + 10;
				$grid_y1 = 5;
				$grid_x2 = $this->_width - 5;
				$grid_y2 = $this->_height - 5;
				imageline($this->_im, $grid_x1,$grid_y1, $grid_x1, $grid_y2, $this->_c('#000000'));

				// îïðåäåëåíèå ïàðàìåòðîâ äèàãðàììû
				$draw_height = $grid_y2 - $grid_y1;
				$max_bar_size = $barSize; // ìèíèìàëüíàÿ øèðèíà ñòîëáöà
				if (($barSize = $draw_height/($cols*$blocks_count) - 10) < $max_bar_size) $barSize = $max_bar_size;
				$bar_space = $draw_height/($cols*$blocks_count) - $barSize;

				// îòîáðàæåíèå äàííûõ
				$space_group = 15; // ïðîìåæóòîê ìåæäó ãðóïïàìè
				$space_value = 1; // ïðîìåæóòîê ìåæäó çíà÷åíèÿìè
				
				$group_height = ($grid_y2 - $grid_y1 - $bar_space*$cols)/$cols;
				$value_height = ($group_height)/$blocks_count;

				$group_i = 0;
				foreach($data as $caption=>$group) {
					for($i=0; $i< $blocks_count; $i++) {
						if (!isset($group[$i])) $value = 0;
						else $value = $group[$i];

						$value_width = ($value/$abs_max) * ($grid_x2 - $grid_x1);
						$value_x = $grid_x1+1;
						$value_y = $grid_y1 + $bar_space/2+ ($group_height + $bar_space)*$group_i + $value_height*$i;

						imagefilledrectangle ($this->_im, $value_x,$value_y+2, $value_x + $value_width, $value_y + $value_height+2, $this->_c('AABBCC'));

						imagefilledrectangle ($this->_im, $value_x,$value_y, $value_x + $value_width, $value_y + $value_height, $this->_c($this->_p($i)));

						// ñîçäàíèå íàäïèñè çíà÷åíèÿ
						list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t((string)$value, 1,1, false, 0,$valuesFontSize ,true);
						$text_height = $lry - $uly; $text_width = $lrx - $llx;
						if ($text_width < $value_width - 8) $this->_t((string)$value, $value_x + $value_width - $text_width - 4, $value_y + $value_height/2 + $text_height/2, $this->_c('#FFFFFF'), 0, $valuesFontSize);
						else $this->_t((string)$value, $value_x + $value_width + 4, $value_y + $value_height/2 + $text_height/2, false, 0, $valuesFontSize);

					}

					// ñîçäàíèå íàäïèñè ãðóïïû
					list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t($caption, 1,1, false, 0,$axesFontSize ,true);
					$caption_width = $lrx - $llx; $caption_height = $lry - $uly;
					$this->_t($caption, $grid_x1 - $caption_width - 5, $grid_y1 + $bar_space/2 + ($group_height + $bar_space)*$group_i + $group_height/2 + $caption_height, false, 0, $axesFontSize);
					
					$group_i++;
				}
			}
		}

		/**
		 * îòîáðàæàåò îòíîñèòåëüíûå ðàñïåðåäåëåíèÿ çíà÷åíèé îòñ÷åòîâ â ãðóïïàõ
		 * @param int $barSize Ìèíèìàëüíàÿ øèðèíà ñòîëáöà ãðóïïû, â ïèêñåëàõ
		 * @param int $axesFontSize Ðàçìåð øðèôòà ïîäïèñåé íà îñÿõ, â ïèêñåëàõ 
		 * @param int $valuesFontSize Ðàçìåð øðèôòà òåêòîâûõ çíà÷åíèé îòñ÷åòîâ, â ïèêñåëàõ
		 * @return void
		 */
		function drawDistribution($barWidth = 25, $axesFontSize = 8, $valuesFontSize = 8) {
			if ($this->_data and $this->_height > 80) {
				$data = $this->_data;
				$cols = sizeof($data);

				// îïðåäåëåíèå ìàêñèìàëüíîãî êîëè÷åñòâà áëîêîâ âíóòðè ñòîëáöà è ôîðìèðîâàíèå ïàëèòðû
				$blocks_count = $header_height = 0;
				foreach($data as $caption=>$data_column) {
					$blocks_count = max($blocks_count, sizeof($data_column));
										
					list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t($caption, 1,1, false, 0,8 ,true);
					$header_height = max($header_height, $lry - $ury);
				}

				// îïðåäåëåíèå ïàðàìåòðîâ ñåòêè è îòîáðàæåíèå
				$grid_x1 = 25;
				$grid_y1 = $this->_height - $header_height - 5;
				$grid_x2 = $this->_width - 5;
				$grid_y2 = $this->_height - $header_height - 5;
				imageline($this->_im, $grid_x1,$grid_y1, $grid_x2, $grid_y2, $this->_c('#000000'));
				imageline($this->_im, $grid_x1,5, $grid_x1, $grid_y2, $this->_c('#000000'));
				
				// îòîáðàæåíèå ïðîöåíòíîé ñåòêè
				if ($this->_height < 150) $percent_step = 20;
				else $percent_step = 10;
				for($i=100; $i>=0; $i-=$percent_step) {
					$y = 5 + (100-$i)/100*($grid_y2 - 5);
					if ($i) imageline($this->_im, $grid_x1+1,$y, $grid_x2, $y, $this->_c('#EEEEEE'));

					list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t((string)$i, 1,1, false, 0,$axesFontSize ,true);
					$i_height = $lry - $uly; $i_width = $lrx - $llx;
						
					$this->_t($i, ($grid_x1 - $i_width)/2, $y+$i_height/2, false, 0,$axesFontSize);
				}
				

				// îïðåäåëåíèå ïàðàìåòðîâ äèàãðàììû
				$draw_width = $grid_x2 - $grid_x1;
				$max_bar_width = $barWidth; // ìàêñèìàëüíàÿ øèðèíà ñòîëáöà
				if (($barWidth = $draw_width/$cols - 15) > $max_bar_width) $barWidth = $max_bar_width;
				$bar_space = $draw_width/$cols - $barWidth;

				// îòîáðàæåíèå äàííûõ
				$bar_index = 0;
				foreach($data as $caption=>$group) {
					$sum = array_sum($group);
					$x = $bar_space/2 + $grid_x1 +($barWidth + $bar_space) * $bar_index;
					$y = 5;

					imagefilledrectangle ($this->_im, $x+2,$y, $x + $barWidth+2, $grid_y2 - 1 - $y + 5, $this->_c('AABBCC'));

						
					for($i=0; $i< $blocks_count; $i++) {
						if (!isset($group[$i])) $value = 0;
						else $value = $group[$i];
						$value_height = ($grid_y2 - 1 - 5)*$value/$sum;

						$y += $value_height;

						imagefilledrectangle ($this->_im, $x,$y - $value_height, $x + $barWidth, $y, $this->_c($this->_p($i)));

						// ñîçäàíèå íàäïèñè çíà÷åíèÿ
						list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t((string)$value, 1,1, false, 0,$valuesFontSize ,true);
						$text_height = $lry - $uly; $text_width = $lrx - $llx;
						if ($text_height < $value_height - 2) $this->_t((string)$value, $x + ($barWidth - $text_width)/2, $y + ($text_height - $value_height)/2, $this->_c('#FFFFFF'), 0, $valuesFontSize);
					}

					// ñîçäàíèå íàäïèñè ãðóïïû
					list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t($caption, 1,1, false, 0,$axesFontSize ,true);
					$caption_width = $lrx - $llx;
					$this->_t($caption, ($x + $barWidth/2) - $caption_width/2, $this->_height - $header_height+7, false, 0, $axesFontSize);
					
					$bar_index++;
				}
			}
		}

		
		/**
		 * âûâîäèò äèàãðàììó çàäàííîãî ôîðìàòà â òåêóùèé ïîòîê âûâîäà (â áðàóçåð)
		 * @param string $type Ôîðìàò âûâîäà ðèñóíêà - 'JPEG' èëè 'PNG'
		 */
		function show($type = 'PNG') {
			imageantialias ($this->_im, TRUE);
			imagetruecolortopalette ($this->_im, TRUE, 64);		
			
			switch ($type) {
				case 'PNG':
					header ("Content-type: image/png");
					imagepng ($this->_im);
					break;
				case 'JPEG':
					header ("Content-type: image/jpeg");
					imagejpeg ($this->_im, '', 100);
					break;
			}
		}
		

		/**
		 * ñîõðàíÿåò äèàãðàììó çàäàííîãî ôîðìàòà â çàäàííîì ôàéëå
		 * @param string $filename Èìÿ âûõîäíîãî ôàéëà
		 * @param string $type Ôîðìàò âûâîäà ðèñóíêà - 'JPEG' èëè 'PNG'
		 */
		function save($filename, $type='PNG') {
			imageantialias ($this->_im, TRUE);
			imagetruecolortopalette ($this->_im, TRUE, 64);		
			
			switch ($type) {
				case 'PNG':
					imagepng ($this->_im, $filename);
					break;
				case 'JPEG':
					imagejpeg ($this->_im, $filename,100);
					break;
			}
			return $filename;
		}
		
		/**
		 * âûïîëíÿåò ïîñëåäîâàòåëüíîñòü äåéñòâèé, çàäàííûõ â âèäå eval()
		 * @since 0.2
		 */
		function evaluate($scriptContent) {
			if (create_function('', $scriptContent."\r\n")) {
				$err_level = error_reporting(0);
				ob_start();
				@eval($scriptContent);
				ob_end_clean();
				error_reporting($err_level);
				return true;
			} else return $this->error = "Îïèñàíèå ôîðìû ñîäåðæèò îøèáêè ñèíòàêñèñà";
		}
		
		/**
		 * Äîáàâëÿåòñÿ çàãàëîâîê íà äèàãðàììó â çàäàííîì ìåñòå
		 * @param string $content Òåêñò çàãîëîâêà
		 * @param 1..9 $position Ïîëîæåíèå çàãîëîâêà íà äèàãðàììå, ïî ïðàâèëó óëèòêè èç âåðõíåãî ëåâîãî óãëà
		 *                      <pre>
		 *                           1 2 3
		 *                           8 9 4
		 *                           7 6 5
		 *                      </pre>
		 */
		function addTitle($content, $position = 1) {
			// ðàçìåð çàãîëîâêà
			$title_font_size = 15;
			// îïðåäåëåíèå øèðèíû ïîëó÷àåìîãî çàãîëîâêà
			while (list($llx,$lly, $lrx, $lry, $urx, $ury, $ulx, $uly) = $this->_t($content,0,0,false,0,$title_font_size, true)  and (($urx - $ulx) > $this->_width)) {
				if ($title_font_size > 2) $title_font_size--; else break;
			}
			
			$title_width = ($urx-$ulx); $title_height = ($lly-$uly);
			// âûâîä çàãîëîâêà â óêàçàííóþ ïîçèöèþ
			// îïðåäåëåíèå ïîçèöèè ëåãåíäû
			switch($position) {
				case 1:case 8:case 7:
					$x = $title_font_size+10;
					break;
				case 2:case 9:case 6:
					$x = floor(($this->_width - $title_width)/2);
					break;
				default:
					$x = $this->_width - $title_width - 10;
					
			}
			switch($position) {
				case 1:case 2:case 3:
					$y = $title_font_size+10;
					break;
				case 8:case 9:case 4:
					$y = floor(($this->_height - $title_height)/2);
					break;
				default:
					$y = $this->_height - $title_height - 10;
					
			}
			
			// âûâîä çàëîãîâêà
			$this->_t($content,$x, $y,false,0,$title_font_size);
		}
		 
	}
?>
Return current item: Dia