PageRenderTime 55ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 2ms

/lib/jpgraph/src/jpgraph.php

https://github.com/web2project/web2project
PHP | 8581 lines | 6480 code | 1002 blank | 1099 comment | 1315 complexity | 2fbbb0cff41c6a8a9e375e17996b37f0 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. //=======================================================================
  3. // File: JPGRAPH.PHP
  4. // Description: PHP Graph Plotting library. Base module.
  5. // Created: 2001-01-08
  6. // Author: Johan Persson (johanp@aditus.nu)
  7. // Ver: $Id$
  8. //
  9. // Copyright (c) Aditus Consulting. All rights reserved.
  10. //========================================================================
  11. require_once('jpg-config.inc');
  12. // Version info
  13. DEFINE('JPG_VERSION','1.20.3');
  14. // For internal use only
  15. DEFINE("_JPG_DEBUG",false);
  16. DEFINE("_FORCE_IMGTOFILE",false);
  17. DEFINE("_FORCE_IMGDIR",'/tmp/jpgimg/');
  18. //------------------------------------------------------------------------
  19. // Automatic settings of path for cache and font directory
  20. // if they have not been previously specified
  21. //------------------------------------------------------------------------
  22. if(USE_CACHE) {
  23. if (!defined('CACHE_DIR')) {
  24. if ( strstr( PHP_OS, 'WIN') ) {
  25. if( empty($_SERVER['TEMP']) ) {
  26. $t = new ErrMsgText();
  27. $msg = $t->Get(11,$file,$lineno);
  28. die($msg);
  29. }
  30. else {
  31. DEFINE('CACHE_DIR', $_SERVER['TEMP'] . '/');
  32. }
  33. } else {
  34. DEFINE('CACHE_DIR','/tmp/jpgraph_cache/');
  35. }
  36. }
  37. }
  38. elseif( !defined('CACHE_DIR') ) {
  39. DEFINE('CACHE_DIR', '');
  40. }
  41. if (!defined('TTF_DIR')) {
  42. if (strstr( PHP_OS, 'WIN') ) {
  43. $sroot = getenv('SystemRoot');
  44. if( empty($sroot) ) {
  45. $t = new ErrMsgText();
  46. $msg = $t->Get(12,$file,$lineno);
  47. die($msg);
  48. }
  49. else {
  50. DEFINE('TTF_DIR', $sroot.'/fonts/');
  51. }
  52. } else {
  53. DEFINE('TTF_DIR','/usr/X11R6/lib/X11/fonts/truetype/');
  54. }
  55. }
  56. //------------------------------------------------------------------
  57. // Constants which are used as parameters for the method calls
  58. //------------------------------------------------------------------
  59. // TTF Font families
  60. DEFINE("FF_COURIER",10);
  61. DEFINE("FF_VERDANA",11);
  62. DEFINE("FF_TIMES",12);
  63. DEFINE("FF_COMIC",14);
  64. DEFINE("FF_ARIAL",15);
  65. DEFINE("FF_GEORGIA",16);
  66. DEFINE("FF_TREBUCHE",17);
  67. // Gnome Vera font
  68. // Available from http://www.gnome.org/fonts/
  69. DEFINE("FF_VERA",19);
  70. DEFINE("FF_VERAMONO",20);
  71. DEFINE("FF_VERASERIF",21);
  72. // Chinese font
  73. DEFINE("FF_SIMSUN",30);
  74. DEFINE("FF_CHINESE",31);
  75. DEFINE("FF_BIG5",31);
  76. // Japanese font
  77. DEFINE("FF_MINCHO",40);
  78. DEFINE("FF_PMINCHO",41);
  79. DEFINE("FF_GOTHIC",42);
  80. DEFINE("FF_PGOTHIC",43);
  81. // Limits for TTF fonts
  82. DEFINE('_FF_FIRST',10);
  83. DEFINE('_FF_LAST',43);
  84. // Older deprecated fonts
  85. DEFINE("FF_BOOK",91); // Deprecated fonts from 1.9
  86. DEFINE("FF_HANDWRT",92); // Deprecated fonts from 1.9
  87. // TTF Font styles
  88. DEFINE("FS_NORMAL",9001);
  89. DEFINE("FS_BOLD",9002);
  90. DEFINE("FS_ITALIC",9003);
  91. DEFINE("FS_BOLDIT",9004);
  92. DEFINE("FS_BOLDITALIC",9004);
  93. //Definitions for internal font, new style
  94. DEFINE("FF_FONT0",1);
  95. DEFINE("FF_FONT1",2);
  96. DEFINE("FF_FONT2",4);
  97. // Tick density
  98. DEFINE("TICKD_DENSE",1);
  99. DEFINE("TICKD_NORMAL",2);
  100. DEFINE("TICKD_SPARSE",3);
  101. DEFINE("TICKD_VERYSPARSE",4);
  102. // Side for ticks and labels.
  103. DEFINE("SIDE_LEFT",-1);
  104. DEFINE("SIDE_RIGHT",1);
  105. DEFINE("SIDE_DOWN",-1);
  106. DEFINE("SIDE_BOTTOM",-1);
  107. DEFINE("SIDE_UP",1);
  108. DEFINE("SIDE_TOP",1);
  109. // Legend type stacked vertical or horizontal
  110. DEFINE("LEGEND_VERT",0);
  111. DEFINE("LEGEND_HOR",1);
  112. // Mark types for plot marks
  113. DEFINE("MARK_SQUARE",1);
  114. DEFINE("MARK_UTRIANGLE",2);
  115. DEFINE("MARK_DTRIANGLE",3);
  116. DEFINE("MARK_DIAMOND",4);
  117. DEFINE("MARK_CIRCLE",5);
  118. DEFINE("MARK_FILLEDCIRCLE",6);
  119. DEFINE("MARK_CROSS",7);
  120. DEFINE("MARK_STAR",8);
  121. DEFINE("MARK_X",9);
  122. DEFINE("MARK_LEFTTRIANGLE",10);
  123. DEFINE("MARK_RIGHTTRIANGLE",11);
  124. DEFINE("MARK_FLASH",12);
  125. DEFINE("MARK_IMG",13);
  126. DEFINE("MARK_FLAG1",14);
  127. DEFINE("MARK_FLAG2",15);
  128. DEFINE("MARK_FLAG3",16);
  129. DEFINE("MARK_FLAG4",17);
  130. // Builtin images
  131. DEFINE("MARK_IMG_PUSHPIN",50);
  132. DEFINE("MARK_IMG_SPUSHPIN",50);
  133. DEFINE("MARK_IMG_LPUSHPIN",51);
  134. DEFINE("MARK_IMG_DIAMOND",52);
  135. DEFINE("MARK_IMG_SQUARE",53);
  136. DEFINE("MARK_IMG_STAR",54);
  137. DEFINE("MARK_IMG_BALL",55);
  138. DEFINE("MARK_IMG_SBALL",55);
  139. DEFINE("MARK_IMG_MBALL",56);
  140. DEFINE("MARK_IMG_LBALL",57);
  141. DEFINE("MARK_IMG_BEVEL",58);
  142. // Inline defines
  143. DEFINE("INLINE_YES",1);
  144. DEFINE("INLINE_NO",0);
  145. // Format for background images
  146. DEFINE("BGIMG_FILLPLOT",1);
  147. DEFINE("BGIMG_FILLFRAME",2);
  148. DEFINE("BGIMG_COPY",3);
  149. DEFINE("BGIMG_CENTER",4);
  150. // Depth of objects
  151. DEFINE("DEPTH_BACK",0);
  152. DEFINE("DEPTH_FRONT",1);
  153. // Direction
  154. DEFINE("VERTICAL",1);
  155. DEFINE("HORIZONTAL",0);
  156. // Axis styles for scientific style axis
  157. DEFINE('AXSTYLE_SIMPLE',1);
  158. DEFINE('AXSTYLE_BOXIN',2);
  159. DEFINE('AXSTYLE_BOXOUT',3);
  160. DEFINE('AXSTYLE_YBOXIN',4);
  161. DEFINE('AXSTYLE_YBOXOUT',5);
  162. // Style for title backgrounds
  163. DEFINE('TITLEBKG_STYLE1',1);
  164. DEFINE('TITLEBKG_STYLE2',2);
  165. DEFINE('TITLEBKG_STYLE3',3);
  166. DEFINE('TITLEBKG_FRAME_NONE',0);
  167. DEFINE('TITLEBKG_FRAME_FULL',1);
  168. DEFINE('TITLEBKG_FRAME_BOTTOM',2);
  169. DEFINE('TITLEBKG_FRAME_BEVEL',3);
  170. DEFINE('TITLEBKG_FILLSTYLE_HSTRIPED',1);
  171. DEFINE('TITLEBKG_FILLSTYLE_VSTRIPED',2);
  172. DEFINE('TITLEBKG_FILLSTYLE_SOLID',3);
  173. // Style for background gradient fills
  174. DEFINE('BGRAD_FRAME',1);
  175. DEFINE('BGRAD_MARGIN',2);
  176. DEFINE('BGRAD_PLOT',3);
  177. // Width of tab titles
  178. DEFINE('TABTITLE_WIDTHFIT',0);
  179. DEFINE('TABTITLE_WIDTHFULL',-1);
  180. // Defines for 3D skew directions
  181. DEFINE('SKEW3D_UP',0);
  182. DEFINE('SKEW3D_DOWN',1);
  183. DEFINE('SKEW3D_LEFT',2);
  184. DEFINE('SKEW3D_RIGHT',3);
  185. //
  186. // Get hold of gradient class (In Version 2.x)
  187. // A client of the library has to manually include this
  188. //
  189. require_once 'jpgraph_gradient.php';
  190. class ErrMsgText {
  191. var $lt=NULL;
  192. var $supportedLocales = array('en');
  193. function ErrMsgText() {
  194. GLOBAL $__jpg_err_locale;
  195. if( !in_array($__jpg_err_locale,$this->supportedLocales) )
  196. $aLoc = 'en';
  197. require_once('lang/'.$__jpg_err_locale.'.inc.php');
  198. $this->lt = $_jpg_messages;
  199. }
  200. function Get($errnbr,$a1=null,$a2=null,$a3=null,$a4=null,$a5=null) {
  201. if( !isset($this->lt[$errnbr]) ) {
  202. return 'Internal error: The specified error message does not exist in the chosen locale. (Please blame the translator...))';
  203. }
  204. $ea = $this->lt[$errnbr];
  205. $j=0;
  206. if( $a1 !== null ) {
  207. $argv[$j++] = $a1;
  208. if( $a2 !== null ) {
  209. $argv[$j++] = $a2;
  210. if( $a3 !== null ) {
  211. $argv[$j++] = $a3;
  212. if( $a4 !== null ) {
  213. $argv[$j++] = $a4;
  214. if( $a5 !== null ) {
  215. $argv[$j++] = $a5;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. $numargs = $j;
  222. if( $ea[1] != $numargs ) {
  223. // Error message argument count do not match.
  224. // Just return the error message without arguments.
  225. return $ea[0];
  226. }
  227. switch( $numargs ) {
  228. case 1:
  229. $msg = sprintf($ea[0],$argv[0]);
  230. break;
  231. case 2:
  232. $msg = sprintf($ea[0],$argv[0],$argv[1]);
  233. break;
  234. case 3:
  235. $msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2]);
  236. break;
  237. case 4:
  238. $msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2],$argv[3]);
  239. break;
  240. case 5:
  241. $msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2],$argv[3],$argv[4]);
  242. break;
  243. case 0:
  244. default:
  245. $msg = sprintf($ea[0]);
  246. break;
  247. }
  248. return $msg;
  249. }
  250. }
  251. //
  252. // A wrapper class that is used to access the specified error object
  253. // (to hide the global error parameter and avoid having a GLOBAL directive
  254. // in all methods.
  255. //
  256. GLOBAL $__jpg_err;
  257. GLOBAL $__jpg_err_locale ;
  258. $__jpg_err_locale = 'en';
  259. class JpGraphError {
  260. function Install($aErrObject) {
  261. GLOBAL $__jpg_err;
  262. $__jpg_err = $aErrObject;
  263. }
  264. function Raise($aMsg,$aHalt=true){
  265. GLOBAL $__jpg_err;
  266. $tmp = new $__jpg_err;
  267. $tmp->Raise($aMsg,$aHalt);
  268. }
  269. function RaiseL($errnbr,$a1=null,$a2=null,$a3=null,$a4=null,$a5=null) {
  270. GLOBAL $__jpg_err;
  271. $t = new ErrMsgText();
  272. $msg = $t->Get($errnbr,$a1,$a2,$a3,$a4,$a5);
  273. $tmp = new $__jpg_err;
  274. $tmp->Raise($msg);
  275. }
  276. }
  277. //
  278. // ... and install the default error handler
  279. //
  280. if( USE_IMAGE_ERROR_HANDLER ) {
  281. $__jpg_err = "JpGraphErrObjectImg";
  282. }
  283. else {
  284. $__jpg_err = "JpGraphErrObject";
  285. }
  286. //
  287. // Make GD sanity check
  288. //
  289. if( !function_exists("imagetypes") || !function_exists('imagecreatefromstring') ) {
  290. JpGraphError::RaiseL(25001);
  291. //("This PHP installation is not configured with the GD library. Please recompile PHP with GD support to run JpGraph. (Neither function imagetypes() nor imagecreatefromstring() does exist)");
  292. }
  293. //
  294. // Routine to determine if GD1 or GD2 is installed
  295. //
  296. function CheckGDVersion() {
  297. $GDfuncList = get_extension_funcs('gd');
  298. if( !$GDfuncList ) return 0 ;
  299. else {
  300. if( in_array('imagegd2',$GDfuncList) &&
  301. in_array('imagecreatetruecolor',$GDfuncList))
  302. return 2;
  303. else
  304. return 1;
  305. }
  306. }
  307. //
  308. // Check what version of the GD library is installed.
  309. //
  310. $GLOBALS['gd2'] = false;
  311. if( USE_LIBRARY_GD2 === 'auto' ) {
  312. $gdversion = CheckGDVersion();
  313. if( $gdversion == 2 ) {
  314. $GLOBALS['gd2'] = true;
  315. $GLOBALS['copyfunc'] = 'imagecopyresampled';
  316. }
  317. elseif( $gdversion == 1 ) {
  318. $GLOBALS['gd2'] = false;
  319. $GLOBALS['copyfunc'] = 'imagecopyresized';
  320. }
  321. else {
  322. JpGraphError::RaiseL(25002);
  323. //(" Your PHP installation does not seem to have the required GD library. Please see the PHP documentation on how to install and enable the GD library.");
  324. }
  325. }
  326. else {
  327. $GLOBALS['gd2'] = USE_LIBRARY_GD2;
  328. $GLOBALS['copyfunc'] = USE_LIBRARY_GD2 ? 'imagecopyresampled' : 'imagecopyresized';
  329. }
  330. //
  331. // First of all set up a default error handler
  332. //
  333. //=============================================================
  334. // The default trivial text error handler.
  335. //=============================================================
  336. class JpGraphErrObject {
  337. var $iTitle = "JpGraph Error";
  338. var $iDest = false;
  339. function JpGraphErrObject() {
  340. // Empty. Reserved for future use
  341. }
  342. function SetTitle($aTitle) {
  343. $this->iTitle = $aTitle;
  344. }
  345. function SetStrokeDest($aDest) {
  346. $this->iDest = $aDest;
  347. }
  348. // If aHalt is true then execution can't continue. Typical used for fatal errors.
  349. function Raise($aMsg,$aHalt=true) {
  350. $aMsg = $this->iTitle.' '.$aMsg;
  351. if ($this->iDest) {
  352. $f = @fopen($this->iDest,'a');
  353. if( $f ) {
  354. @fwrite($f,$aMsg);
  355. @fclose($f);
  356. }
  357. }
  358. else {
  359. echo $aMsg;
  360. }
  361. if( $aHalt )
  362. die();
  363. }
  364. }
  365. //==============================================================
  366. // An image based error handler
  367. //==============================================================
  368. class JpGraphErrObjectImg extends JpGraphErrObject {
  369. function Raise($aMsg,$aHalt=true) {
  370. $img_iconerror =
  371. 'iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAAAaV'.
  372. 'BMVEX//////2Xy8mLl5V/Z2VvMzFi/v1WyslKlpU+ZmUyMjEh/'.
  373. 'f0VyckJlZT9YWDxMTDjAwMDy8sLl5bnY2K/MzKW/v5yyspKlpY'.
  374. 'iYmH+MjHY/PzV/f2xycmJlZVlZWU9MTEXY2Ms/PzwyMjLFTjea'.
  375. 'AAAAAXRSTlMAQObYZgAAAAFiS0dEAIgFHUgAAAAJcEhZcwAACx'.
  376. 'IAAAsSAdLdfvwAAAAHdElNRQfTBgISOCqusfs5AAABLUlEQVR4'.
  377. '2tWV3XKCMBBGWfkranCIVClKLd/7P2Q3QsgCxjDTq+6FE2cPH+'.
  378. 'xJ0Ogn2lQbsT+Wrs+buAZAV4W5T6Bs0YXBBwpKgEuIu+JERAX6'.
  379. 'wM2rHjmDdEITmsQEEmWADgZm6rAjhXsoMGY9B/NZBwJzBvn+e3'.
  380. 'wHntCAJdGu9SviwIwoZVDxPB9+Rc0TSEbQr0j3SA1gwdSn6Db0'.
  381. '6Tm1KfV6yzWGQO7zdpvyKLKBDmRFjzeB3LYgK7r6A/noDAfjtS'.
  382. 'IXaIzbJSv6WgUebTMV4EoRB8a2mQiQjgtF91HdKDKZ1gtFtQjk'.
  383. 'YcWaR5OKOhkYt+ZsTFdJRfPAApOpQYJTNHvCRSJR6SJngQadfc'.
  384. 'vd69OLMddVOPCGVnmrFD8bVYd3JXfxXPtLR/+mtv59/ALWiiMx'.
  385. 'qL72fwAAAABJRU5ErkJggg==' ;
  386. if( function_exists("imagetypes") )
  387. $supported = imagetypes();
  388. else
  389. $supported = 0;
  390. if( !function_exists('imagecreatefromstring') )
  391. $supported = 0;
  392. if( ob_get_length() || headers_sent() || !($supported & IMG_PNG) ) {
  393. // Special case for headers already sent or that the installation doesn't support
  394. // the PNG format (which the error icon is encoded in).
  395. // Dont return an image since it can't be displayed
  396. die($this->iTitle.' '.$aMsg);
  397. }
  398. $aMsg = wordwrap($aMsg,55);
  399. $lines = substr_count($aMsg,"\n");
  400. // Create the error icon GD
  401. $erricon = Image::CreateFromString(base64_decode($img_iconerror));
  402. // Create an image that contains the error text.
  403. $w=400;
  404. $h=100 + 15*max(0,$lines-3);
  405. $img = new Image($w,$h);
  406. // Drop shadow
  407. $img->SetColor("gray");
  408. $img->FilledRectangle(5,5,$w-1,$h-1,10);
  409. $img->SetColor("gray:0.7");
  410. $img->FilledRectangle(5,5,$w-3,$h-3,10);
  411. // Window background
  412. $img->SetColor("lightblue");
  413. $img->FilledRectangle(1,1,$w-5,$h-5);
  414. $img->CopyCanvasH($img->img,$erricon,5,30,0,0,40,40);
  415. // Window border
  416. $img->SetColor("black");
  417. $img->Rectangle(1,1,$w-5,$h-5);
  418. $img->Rectangle(0,0,$w-4,$h-4);
  419. // Window top row
  420. $img->SetColor("darkred");
  421. for($y=3; $y < 18; $y += 2 )
  422. $img->Line(1,$y,$w-6,$y);
  423. // "White shadow"
  424. $img->SetColor("white");
  425. // Left window edge
  426. $img->Line(2,2,2,$h-5);
  427. $img->Line(2,2,$w-6,2);
  428. // "Gray button shadow"
  429. $img->SetColor("darkgray");
  430. // Gray window shadow
  431. $img->Line(2,$h-6,$w-5,$h-6);
  432. $img->Line(3,$h-7,$w-5,$h-7);
  433. // Window title
  434. $m = floor($w/2-5);
  435. $l = 100;
  436. $img->SetColor("lightgray:1.3");
  437. $img->FilledRectangle($m-$l,2,$m+$l,16);
  438. // Stroke text
  439. $img->SetColor("darkred");
  440. $img->SetFont(FF_FONT2,FS_BOLD);
  441. $img->StrokeText($m-50,15,$this->iTitle);
  442. $img->SetColor("black");
  443. $img->SetFont(FF_FONT1,FS_NORMAL);
  444. $txt = new Text($aMsg,52,25);
  445. $txt->Align("left","top");
  446. $txt->Stroke($img);
  447. if ($this->iDest) {
  448. $img->Stream($this->iDest);
  449. } else {
  450. $img->Headers();
  451. $img->Stream();
  452. }
  453. if( $aHalt )
  454. die();
  455. }
  456. }
  457. //
  458. // Setup PHP error handler
  459. //
  460. function _phpErrorHandler($errno,$errmsg,$filename, $linenum, $vars) {
  461. // Respect current error level
  462. if( $errno & error_reporting() ) {
  463. JpGraphError::RaiseL(25003,basename($filename),$linenum,$errmsg);
  464. }
  465. }
  466. if( INSTALL_PHP_ERR_HANDLER ) {
  467. set_error_handler("_phpErrorHandler");
  468. }
  469. //
  470. //Check if there were any warnings, perhaps some wrong includes by the user
  471. //
  472. if( isset($GLOBALS['php_errormsg']) && CATCH_PHPERRMSG &&
  473. !preg_match('|Deprecated|', $GLOBALS['php_errormsg']) && !preg_match('|deprecated|', $GLOBALS['php_errormsg']) && !preg_match('|Non-static|', $GLOBALS['php_errormsg'])) {
  474. JpGraphError::RaiseL(25004,$GLOBALS['php_errormsg']);
  475. }
  476. // Useful mathematical function
  477. function sign($a) {return $a >= 0 ? 1 : -1;}
  478. // Utility function to generate an image name based on the filename we
  479. // are running from and assuming we use auto detection of graphic format
  480. // (top level), i.e it is safe to call this function
  481. // from a script that uses JpGraph
  482. function GenImgName() {
  483. global $_SERVER;
  484. // Determine what format we should use when we save the images
  485. $supported = imagetypes();
  486. if( $supported & IMG_PNG ) $img_format="png";
  487. elseif( $supported & IMG_GIF ) $img_format="gif";
  488. elseif( $supported & IMG_JPG ) $img_format="jpeg";
  489. if( !isset($_SERVER['PHP_SELF']) )
  490. JpGraphError::RaiseL(25005);
  491. //(" Can't access PHP_SELF, PHP global variable. You can't run PHP from command line if you want to use the 'auto' naming of cache or image files.");
  492. $fname = basename($_SERVER['PHP_SELF']);
  493. if( !empty($_SERVER['QUERY_STRING']) ) {
  494. $q = @$_SERVER['QUERY_STRING'];
  495. $fname .= '?'.preg_replace("/\W/", "_", $q).'.'.$img_format;
  496. }
  497. else {
  498. $fname = substr($fname,0,strlen($fname)-4).'.'.$img_format;
  499. }
  500. return $fname;
  501. }
  502. class LanguageConv {
  503. var $g2312 = null ;
  504. function Convert($aTxt,$aFF) {
  505. if( LANGUAGE_CYRILLIC ) {
  506. /* Auto-detect whether the cyrillic conversion
  507. ** based on the character_set info that has
  508. ** been passed to jpgraph from the including
  509. ** application.
  510. **
  511. ** Call the cyrillic converter only when a cyrillic
  512. ** charset derivate is used. Otherwise do nothing
  513. ** and prevent from erraneously converted strings.
  514. */
  515. if( CYRILLIC_FROM_WINDOWS && ( !defined('LANGUAGE_CHARSET') || stristr(LANGUAGE_CHARSET, 'windows-1251') ) ) {
  516. $aTxt = convert_cyr_string($aTxt, "w", "k");
  517. }
  518. if (!defined('LANGUAGE_CHARSET') || stristr(LANGUAGE_CHARSET, 'koi8-r') || stristr(LANGUAGE_CHARSET, 'windows-1251')) {
  519. $isostring = convert_cyr_string($aTxt, "k", "i");
  520. $unistring = LanguageConv::iso2uni($isostring);
  521. } else {
  522. $unistring = $aTxt;
  523. }
  524. return $unistring;
  525. }
  526. elseif( $aFF === FF_SIMSUN ) {
  527. // Do Chinese conversion
  528. if( $this->g2312 == null ) {
  529. include_once 'jpgraph_gb2312.php' ;
  530. $this->g2312 = new GB2312toUTF8();
  531. }
  532. return $this->g2312->gb2utf8($aTxt);
  533. }
  534. elseif( $aFF === FF_CHINESE ) {
  535. if( !function_exists('iconv') ) {
  536. JpGraphError::RaiseL(25006);
  537. //('Usage of FF_CHINESE (FF_BIG5) font family requires that your PHP setup has the iconv() function. By default this is not compiled into PHP (needs the "--width-iconv" when configured).');
  538. }
  539. return iconv('BIG5','UTF-8',$aTxt);
  540. }
  541. else
  542. return $aTxt;
  543. }
  544. // Translate iso encoding to unicode
  545. function iso2uni ($isoline){
  546. $uniline='';
  547. for ($i=0; $i < strlen($isoline); $i++){
  548. $thischar=substr($isoline,$i,1);
  549. $charcode=ord($thischar);
  550. $uniline.=($charcode>175) ? "&#" . (1040+($charcode-176)). ";" : $thischar;
  551. }
  552. return $uniline;
  553. }
  554. }
  555. //===================================================
  556. // CLASS JpgTimer
  557. // Description: General timing utility class to handle
  558. // time measurement of generating graphs. Multiple
  559. // timers can be started.
  560. //===================================================
  561. class JpgTimer {
  562. var $start;
  563. var $idx;
  564. //---------------
  565. // CONSTRUCTOR
  566. function JpgTimer() {
  567. $this->idx=0;
  568. }
  569. //---------------
  570. // PUBLIC METHODS
  571. // Push a new timer start on stack
  572. function Push() {
  573. list($ms,$s)=explode(" ",microtime());
  574. $this->start[$this->idx++]=floor($ms*1000) + 1000*$s;
  575. }
  576. // Pop the latest timer start and return the diff with the
  577. // current time
  578. function Pop() {
  579. assert($this->idx>0);
  580. list($ms,$s)=explode(" ",microtime());
  581. $etime=floor($ms*1000) + (1000*$s);
  582. $this->idx--;
  583. return $etime-$this->start[$this->idx];
  584. }
  585. } // Class
  586. $gJpgBrandTiming = BRAND_TIMING;
  587. //===================================================
  588. // CLASS DateLocale
  589. // Description: Hold localized text used in dates
  590. //===================================================
  591. class DateLocale {
  592. var $iLocale = 'C'; // environmental locale be used by default
  593. var $iDayAbb = null;
  594. var $iShortDay = null;
  595. var $iShortMonth = null;
  596. var $iMonthName = null;
  597. //---------------
  598. // CONSTRUCTOR
  599. function DateLocale() {
  600. settype($this->iDayAbb, 'array');
  601. settype($this->iShortDay, 'array');
  602. settype($this->iShortMonth, 'array');
  603. settype($this->iMonthName, 'array');
  604. $this->Set('C');
  605. }
  606. //---------------
  607. // PUBLIC METHODS
  608. function Set($aLocale) {
  609. if ( in_array($aLocale, array_keys($this->iDayAbb)) ){
  610. $this->iLocale = $aLocale;
  611. return TRUE; // already cached nothing else to do!
  612. }
  613. $pLocale = setlocale(LC_TIME, 0); // get current locale for LC_TIME
  614. $res = @setlocale(LC_TIME, $aLocale);
  615. if ( ! $res ){
  616. JpGraphError::RaiseL(25007,$aLocale);
  617. //("You are trying to use the locale ($aLocale) which your PHP installation does not support. Hint: Use '' to indicate the default locale for this geographic region.");
  618. return FALSE;
  619. }
  620. $this->iLocale = $aLocale;
  621. for ( $i = 0, $ofs = 0 - strftime('%w'); $i < 7; $i++, $ofs++ ){
  622. $day = strftime('%a', strtotime("$ofs day"));
  623. $day{0} = strtoupper($day{0});
  624. $this->iDayAbb[$aLocale][]= $day{0};
  625. $this->iShortDay[$aLocale][]= $day;
  626. }
  627. for($i=1; $i<=12; ++$i) {
  628. list($short ,$full) = explode('|', strftime("%b|%B",strtotime("2001-$i-01")));
  629. $this->iShortMonth[$aLocale][] = ucfirst($short);
  630. $this->iMonthName [$aLocale][] = ucfirst($full);
  631. }
  632. // Return to original locale
  633. setlocale(LC_TIME, $pLocale);
  634. return TRUE;
  635. }
  636. function GetDayAbb() {
  637. return $this->iDayAbb[$this->iLocale];
  638. }
  639. function GetShortDay() {
  640. return $this->iShortDay[$this->iLocale];
  641. }
  642. function GetShortMonth() {
  643. return $this->iShortMonth[$this->iLocale];
  644. }
  645. function GetShortMonthName($aNbr) {
  646. return $this->iShortMonth[$this->iLocale][$aNbr];
  647. }
  648. function GetLongMonthName($aNbr) {
  649. return $this->iMonthName[$this->iLocale][$aNbr];
  650. }
  651. function GetMonth() {
  652. return $this->iMonthName[$this->iLocale];
  653. }
  654. }
  655. $gDateLocale = new DateLocale();
  656. $gJpgDateLocale = new DateLocale();
  657. //=======================================================
  658. // CLASS Footer
  659. // Description: Encapsulates the footer line in the Graph
  660. //=======================================================
  661. class Footer {
  662. var $left,$center,$right;
  663. var $iLeftMargin = 3;
  664. var $iRightMargin = 3;
  665. var $iBottomMargin = 3;
  666. function Footer() {
  667. $this->left = new Text();
  668. $this->left->ParagraphAlign('left');
  669. $this->center = new Text();
  670. $this->center->ParagraphAlign('center');
  671. $this->right = new Text();
  672. $this->right->ParagraphAlign('right');
  673. }
  674. function Stroke(&$aImg) {
  675. $y = $aImg->height - $this->iBottomMargin;
  676. $x = $this->iLeftMargin;
  677. $this->left->Align('left','bottom');
  678. $this->left->Stroke($aImg,$x,$y);
  679. $x = ($aImg->width - $this->iLeftMargin - $this->iRightMargin)/2;
  680. $this->center->Align('center','bottom');
  681. $this->center->Stroke($aImg,$x,$y);
  682. $x = $aImg->width - $this->iRightMargin;
  683. $this->right->Align('right','bottom');
  684. $this->right->Stroke($aImg,$x,$y);
  685. }
  686. }
  687. //===================================================
  688. // CLASS Graph
  689. // Description: Main class to handle graphs
  690. //===================================================
  691. class Graph {
  692. var $cache=null; // Cache object (singleton)
  693. var $img=null; // Img object (singleton)
  694. var $plots=array(); // Array of all plot object in the graph (for Y 1 axis)
  695. var $y2plots=array();// Array of all plot object in the graph (for Y 2 axis)
  696. var $ynplots=array();
  697. var $xscale=null; // X Scale object (could be instance of LinearScale or LogScale
  698. var $yscale=null,$y2scale=null, $ynscale=array();
  699. var $iIcons = array(); // Array of Icons to add to
  700. var $cache_name; // File name to be used for the current graph in the cache directory
  701. var $xgrid=null; // X Grid object (linear or logarithmic)
  702. var $ygrid=null,$y2grid=null;
  703. var $doframe=true,$frame_color=array(0,0,0), $frame_weight=1; // Frame around graph
  704. var $boxed=false, $box_color=array(0,0,0), $box_weight=1; // Box around plot area
  705. var $doshadow=false,$shadow_width=4,$shadow_color=array(102,102,102); // Shadow for graph
  706. var $xaxis=null; // X-axis (instane of Axis class)
  707. var $yaxis=null, $y2axis=null, $ynaxis=array(); // Y axis (instance of Axis class)
  708. var $margin_color=array(200,200,200); // Margin color of graph
  709. var $plotarea_color=array(255,255,255); // Plot area color
  710. var $title,$subtitle,$subsubtitle; // Title and subtitle(s) text object
  711. var $axtype="linlin"; // Type of axis
  712. var $xtick_factor; // Factot to determine the maximum number of ticks depending on the plot with
  713. var $texts=null, $y2texts=null; // Text object to ge shown in the graph
  714. var $lines=null, $y2lines=null;
  715. var $bands=null, $y2bands=null;
  716. var $text_scale_off=0, $text_scale_abscenteroff=-1; // Text scale offset in fractions and for centering bars in absolute pixels
  717. var $background_image="",$background_image_type=-1,$background_image_format="png";
  718. var $background_image_bright=0,$background_image_contr=0,$background_image_sat=0;
  719. var $image_bright=0, $image_contr=0, $image_sat=0;
  720. var $inline;
  721. var $showcsim=0,$csimcolor="red"; //debug stuff, draw the csim boundaris on the image if <>0
  722. var $grid_depth=DEPTH_BACK; // Draw grid under all plots as default
  723. var $iAxisStyle = AXSTYLE_SIMPLE;
  724. var $iCSIMdisplay=false,$iHasStroked = false;
  725. var $footer;
  726. var $csimcachename = '', $csimcachetimeout = 0;
  727. var $iDoClipping = false;
  728. var $y2orderback=true;
  729. var $tabtitle;
  730. var $bkg_gradtype=-1,$bkg_gradstyle=BGRAD_MARGIN;
  731. var $bkg_gradfrom='navy', $bkg_gradto='silver';
  732. var $titlebackground = false;
  733. var $titlebackground_color = 'lightblue',
  734. $titlebackground_style = 1,
  735. $titlebackground_framecolor = 'blue',
  736. $titlebackground_framestyle = 2,
  737. $titlebackground_frameweight = 1,
  738. $titlebackground_bevelheight = 3 ;
  739. var $titlebkg_fillstyle=TITLEBKG_FILLSTYLE_SOLID;
  740. var $titlebkg_scolor1='black',$titlebkg_scolor2='white';
  741. var $framebevel = false, $framebeveldepth = 2 ;
  742. var $framebevelborder = false, $framebevelbordercolor='black';
  743. var $framebevelcolor1='white@0.4', $framebevelcolor2='black@0.4';
  744. var $background_image_mix=100;
  745. var $background_cflag = '';
  746. var $background_cflag_type = BGIMG_FILLPLOT;
  747. var $background_cflag_mix = 100;
  748. var $iImgTrans=false,
  749. $iImgTransHorizon = 100,$iImgTransSkewDist=150,
  750. $iImgTransDirection = 1, $iImgTransMinSize = true,
  751. $iImgTransFillColor='white',$iImgTransHighQ=false,
  752. $iImgTransBorder=false,$iImgTransHorizonPos=0.5;
  753. var $iYAxisDeltaPos=50;
  754. var $iIconDepth=DEPTH_BACK;
  755. var $iAxisLblBgType = 0,
  756. $iXAxisLblBgFillColor = 'lightgray', $iXAxisLblBgColor = 'black',
  757. $iYAxisLblBgFillColor = 'lightgray', $iYAxisLblBgColor = 'black';
  758. var $iTables=NULL;
  759. //---------------
  760. // CONSTRUCTOR
  761. // aWIdth Width in pixels of image
  762. // aHeight Height in pixels of image
  763. // aCachedName Name for image file in cache directory
  764. // aTimeOut Timeout in minutes for image in cache
  765. // aInline If true the image is streamed back in the call to Stroke()
  766. // If false the image is just created in the cache
  767. function Graph($aWidth=300,$aHeight=200,$aCachedName="",$aTimeOut=0,$aInline=true) {
  768. GLOBAL $gJpgBrandTiming;
  769. // If timing is used create a new timing object
  770. if( $gJpgBrandTiming ) {
  771. global $tim;
  772. $tim = new JpgTimer();
  773. $tim->Push();
  774. }
  775. if( !is_numeric($aWidth) || !is_numeric($aHeight) ) {
  776. JpGraphError::RaiseL(25008);//('Image width/height argument in Graph::Graph() must be numeric');
  777. }
  778. // Automatically generate the image file name based on the name of the script that
  779. // generates the graph
  780. if( $aCachedName=="auto" )
  781. $aCachedName=GenImgName();
  782. // Should the image be streamed back to the browser or only to the cache?
  783. $this->inline=$aInline;
  784. $this->img = new RotImage($aWidth,$aHeight);
  785. $this->cache = new ImgStreamCache($this->img);
  786. $this->cache->SetTimeOut($aTimeOut);
  787. $this->title = new Text();
  788. $this->title->ParagraphAlign('center');
  789. $this->title->SetFont(FF_FONT2,FS_BOLD);
  790. $this->title->SetMargin(3);
  791. $this->title->SetAlign('center');
  792. $this->subtitle = new Text();
  793. $this->subtitle->ParagraphAlign('center');
  794. $this->subtitle->SetMargin(2);
  795. $this->subtitle->SetAlign('center');
  796. $this->subsubtitle = new Text();
  797. $this->subsubtitle->ParagraphAlign('center');
  798. $this->subsubtitle->SetMargin(2);
  799. $this->subsubtitle->SetAlign('center');
  800. $this->legend = new Legend();
  801. $this->footer = new Footer();
  802. // Window doesn't like '?' in the file name so replace it with an '_'
  803. $aCachedName = str_replace("?","_",$aCachedName);
  804. // If the cached version exist just read it directly from the
  805. // cache, stream it back to browser and exit
  806. if( $aCachedName!="" && READ_CACHE && $aInline )
  807. if( $this->cache->GetAndStream($aCachedName) ) {
  808. exit();
  809. }
  810. $this->cache_name = $aCachedName;
  811. $this->SetTickDensity(); // Normal density
  812. $this->tabtitle = new GraphTabTitle();
  813. }
  814. //---------------
  815. // PUBLIC METHODS
  816. // Enable final image perspective transformation
  817. function Set3DPerspective($aDir=1,$aHorizon=100,$aSkewDist=120,$aQuality=false,$aFillColor='#FFFFFF',$aBorder=false,$aMinSize=true,$aHorizonPos=0.5) {
  818. $this->iImgTrans = true;
  819. $this->iImgTransHorizon = $aHorizon;
  820. $this->iImgTransSkewDist= $aSkewDist;
  821. $this->iImgTransDirection = $aDir;
  822. $this->iImgTransMinSize = $aMinSize;
  823. $this->iImgTransFillColor=$aFillColor;
  824. $this->iImgTransHighQ=$aQuality;
  825. $this->iImgTransBorder=$aBorder;
  826. $this->iImgTransHorizonPos=$aHorizonPos;
  827. }
  828. // Set Image format and optional quality
  829. function SetImgFormat($aFormat,$aQuality=75) {
  830. $this->img->SetImgFormat($aFormat,$aQuality);
  831. }
  832. // Should the grid be in front or back of the plot?
  833. function SetGridDepth($aDepth) {
  834. $this->grid_depth=$aDepth;
  835. }
  836. function SetIconDepth($aDepth) {
  837. $this->iIconDepth=$aDepth;
  838. }
  839. // Specify graph angle 0-360 degrees.
  840. function SetAngle($aAngle) {
  841. $this->img->SetAngle($aAngle);
  842. }
  843. function SetAlphaBlending($aFlg=true) {
  844. $this->img->SetAlphaBlending($aFlg);
  845. }
  846. // Shortcut to image margin
  847. function SetMargin($lm,$rm,$tm,$bm) {
  848. $this->img->SetMargin($lm,$rm,$tm,$bm);
  849. }
  850. function SetY2OrderBack($aBack=true) {
  851. $this->y2orderback = $aBack;
  852. }
  853. // Rotate the graph 90 degrees and set the margin
  854. // when we have done a 90 degree rotation
  855. function Set90AndMargin($lm=0,$rm=0,$tm=0,$bm=0) {
  856. $lm = $lm ==0 ? floor(0.2 * $this->img->width) : $lm ;
  857. $rm = $rm ==0 ? floor(0.1 * $this->img->width) : $rm ;
  858. $tm = $tm ==0 ? floor(0.2 * $this->img->height) : $tm ;
  859. $bm = $bm ==0 ? floor(0.1 * $this->img->height) : $bm ;
  860. $adj = ($this->img->height - $this->img->width)/2;
  861. $this->img->SetMargin($tm-$adj,$bm-$adj,$rm+$adj,$lm+$adj);
  862. $this->img->SetCenter(floor($this->img->width/2),floor($this->img->height/2));
  863. $this->SetAngle(90);
  864. if( empty($this->yaxis) || empty($this->xaxis) ) {
  865. JpgraphError::RaiseL(25009);//('You must specify what scale to use with a call to Graph::SetScale()');
  866. }
  867. $this->xaxis->SetLabelAlign('right','center');
  868. $this->yaxis->SetLabelAlign('center','bottom');
  869. }
  870. function SetClipping($aFlg=true) {
  871. $this->iDoClipping = $aFlg ;
  872. }
  873. // Add a plot object to the graph
  874. function Add(&$aPlot) {
  875. if( $aPlot == null )
  876. JpGraphError::RaiseL(25010);//("Graph::Add() You tried to add a null plot to the graph.");
  877. if( is_array($aPlot) && count($aPlot) > 0 )
  878. $cl = $aPlot[0];
  879. else
  880. $cl = $aPlot;
  881. if( is_a($cl,'Text') )
  882. $this->AddText($aPlot);
  883. elseif( is_a($cl,'PlotLine') )
  884. $this->AddLine($aPlot);
  885. elseif( is_a($cl,'PlotBand') )
  886. $this->AddBand($aPlot);
  887. elseif( is_a($cl,'IconPlot') )
  888. $this->AddIcon($aPlot);
  889. elseif( is_a($cl,'GTextTable') )
  890. $this->AddTable($aPlot);
  891. else
  892. $this->plots[] = &$aPlot;
  893. }
  894. function AddTable(&$aTable) {
  895. if( is_array($aTable) ) {
  896. for($i=0; $i < count($aTable); ++$i )
  897. $this->iTables[]=&$aTable[$i];
  898. }
  899. else {
  900. $this->iTables[] = &$aTable ;
  901. }
  902. }
  903. function AddIcon(&$aIcon) {
  904. if( is_array($aIcon) ) {
  905. for($i=0; $i < count($aIcon); ++$i )
  906. $this->iIcons[]=&$aIcon[$i];
  907. }
  908. else {
  909. $this->iIcons[] = &$aIcon ;
  910. }
  911. }
  912. // Add plot to second Y-scale
  913. function AddY2(&$aPlot) {
  914. if( $aPlot == null )
  915. JpGraphError::RaiseL(25011);//("Graph::AddY2() You tried to add a null plot to the graph.");
  916. if( is_array($aPlot) && count($aPlot) > 0 )
  917. $cl = $aPlot[0];
  918. else
  919. $cl = $aPlot;
  920. if( is_a($cl,'Text') )
  921. $this->AddText($aPlot,true);
  922. elseif( is_a($cl,'PlotLine') )
  923. $this->AddLine($aPlot,true);
  924. elseif( is_a($cl,'PlotBand') )
  925. $this->AddBand($aPlot,true);
  926. else
  927. $this->y2plots[] = &$aPlot;
  928. }
  929. // Add plot to second Y-scale
  930. function AddY($aN,&$aPlot) {
  931. if( $aPlot == null )
  932. JpGraphError::RaiseL(25012);//("Graph::AddYN() You tried to add a null plot to the graph.");
  933. if( is_array($aPlot) && count($aPlot) > 0 )
  934. $cl = $aPlot[0];
  935. else
  936. $cl = $aPlot;
  937. if( is_a($cl,'Text') || is_a($cl,'PlotLine') || is_a($cl,'PlotBand') )
  938. JpGraph::RaiseL(25013);//('You can only add standard plots to multiple Y-axis');
  939. else
  940. $this->ynplots[$aN][] = &$aPlot;
  941. }
  942. // Add text object to the graph
  943. function AddText(&$aTxt,$aToY2=false) {
  944. if( $aTxt == null )
  945. JpGraphError::RaiseL(25014);//("Graph::AddText() You tried to add a null text to the graph.");
  946. if( $aToY2 ) {
  947. if( is_array($aTxt) ) {
  948. for($i=0; $i < count($aTxt); ++$i )
  949. $this->y2texts[]=&$aTxt[$i];
  950. }
  951. else
  952. $this->y2texts[] = &$aTxt;
  953. }
  954. else {
  955. if( is_array($aTxt) ) {
  956. for($i=0; $i < count($aTxt); ++$i )
  957. $this->texts[]=&$aTxt[$i];
  958. }
  959. else
  960. $this->texts[] = &$aTxt;
  961. }
  962. }
  963. // Add a line object (class PlotLine) to the graph
  964. function AddLine(&$aLine,$aToY2=false) {
  965. if( $aLine == null )
  966. JpGraphError::RaiseL(25015);//("Graph::AddLine() You tried to add a null line to the graph.");
  967. if( $aToY2 ) {
  968. if( is_array($aLine) ) {
  969. for($i=0; $i < count($aLine); ++$i )
  970. $this->y2lines[]=&$aLine[$i];
  971. }
  972. else
  973. $this->y2lines[] = &$aLine;
  974. }
  975. else {
  976. if( is_array($aLine) ) {
  977. for($i=0; $i < count($aLine); ++$i )
  978. $this->lines[]=&$aLine[$i];
  979. }
  980. else
  981. $this->lines[] = &$aLine;
  982. }
  983. }
  984. // Add vertical or horizontal band
  985. function AddBand(&$aBand,$aToY2=false) {
  986. if( $aBand == null )
  987. JpGraphError::RaiseL(25016);//(" Graph::AddBand() You tried to add a null band to the graph.");
  988. if( $aToY2 ) {
  989. if( is_array($aBand) ) {
  990. for($i=0; $i < count($aBand); ++$i )
  991. $this->y2bands[] = &$aBand[$i];
  992. }
  993. else
  994. $this->y2bands[] = &$aBand;
  995. }
  996. else {
  997. if( is_array($aBand) ) {
  998. for($i=0; $i < count($aBand); ++$i )
  999. $this->bands[] = &$aBand[$i];
  1000. }
  1001. else
  1002. $this->bands[] = &$aBand;
  1003. }
  1004. }
  1005. function SetBackgroundGradient($aFrom='navy',$aTo='silver',$aGradType=2,$aStyle=BGRAD_FRAME) {
  1006. $this->bkg_gradtype=$aGradType;
  1007. $this->bkg_gradstyle=$aStyle;
  1008. $this->bkg_gradfrom = $aFrom;
  1009. $this->bkg_gradto = $aTo;
  1010. }
  1011. // Set a country flag in the background
  1012. function SetBackgroundCFlag($aName,$aBgType=BGIMG_FILLPLOT,$aMix=100) {
  1013. $this->background_cflag = $aName;
  1014. $this->background_cflag_type = $aBgType;
  1015. $this->background_cflag_mix = $aMix;
  1016. }
  1017. // Alias for the above method
  1018. function SetBackgroundCountryFlag($aName,$aBgType=BGIMG_FILLPLOT,$aMix=100) {
  1019. $this->background_cflag = $aName;
  1020. $this->background_cflag_type = $aBgType;
  1021. $this->background_cflag_mix = $aMix;
  1022. }
  1023. // Specify a background image
  1024. function SetBackgroundImage($aFileName,$aBgType=BGIMG_FILLPLOT,$aImgFormat="auto") {
  1025. if( $GLOBALS['gd2'] && !USE_TRUECOLOR ) {
  1026. JpGraphError::RaiseL(25017);//("You are using GD 2.x and are trying to use a background images on a non truecolor image. To use background images with GD 2.x you <b>must</b> enable truecolor by setting the USE_TRUECOLOR constant to TRUE. Due to a bug in GD 2.0.1 using any truetype fonts with truecolor images will result in very poor quality fonts.");
  1027. }
  1028. // Get extension to determine image type
  1029. if( $aImgFormat == "auto" ) {
  1030. $e = explode('.',$aFileName);
  1031. if( !$e ) {
  1032. JpGraphError::RaiseL(25018,$aFileName);//('Incorrect file name for Graph::SetBackgroundImage() : '.$aFileName.' Must have a valid image extension (jpg,gif,png) when using autodetection of image type');
  1033. }
  1034. $valid_formats = array('png', 'jpg', 'gif');
  1035. $aImgFormat = strtolower($e[count($e)-1]);
  1036. if ($aImgFormat == 'jpeg') {
  1037. $aImgFormat = 'jpg';
  1038. }
  1039. elseif (!in_array($aImgFormat, $valid_formats) ) {
  1040. JpGraphError::RaiseL(25019,$aImgFormat);//('Unknown file extension ($aImgFormat) in Graph::SetBackgroundImage() for filename: '.$aFileName);
  1041. }
  1042. }
  1043. $this->background_image = $aFileName;
  1044. $this->background_image_type=$aBgType;
  1045. $this->background_image_format=$aImgFormat;
  1046. }
  1047. function SetBackgroundImageMix($aMix) {
  1048. $this->background_image_mix = $aMix ;
  1049. }
  1050. // Adjust brightness and constrast for background image
  1051. function AdjBackgroundImage($aBright,$aContr=0,$aSat=0) {
  1052. $this->background_image_bright=$aBright;
  1053. $this->background_image_contr=$aContr;
  1054. $this->background_image_sat=$aSat;
  1055. }
  1056. // Adjust brightness and constrast for image
  1057. function AdjImage($aBright,$aContr=0,$aSat=0) {
  1058. $this->image_bright=$aBright;
  1059. $this->image_contr=$aContr;
  1060. $this->image_sat=$aSat;
  1061. }
  1062. // Specify axis style (boxed or single)
  1063. function SetAxisStyle($aStyle) {
  1064. $this->iAxisStyle = $aStyle ;
  1065. }
  1066. // Set a frame around the plot area
  1067. function SetBox($aDrawPlotFrame=true,$aPlotFrameColor=array(0,0,0),$aPlotFrameWeight=1) {
  1068. $this->boxed = $aDrawPlotFrame;
  1069. $this->box_weight = $aPlotFrameWeight;
  1070. $this->box_color = $aPlotFrameColor;
  1071. }
  1072. // Specify color for the plotarea (not the margins)
  1073. function SetColor($aColor) {
  1074. $this->plotarea_color=$aColor;
  1075. }
  1076. // Specify color for the margins (all areas outside the plotarea)
  1077. function SetMarginColor($aColor) {
  1078. $this->margin_color=$aColor;
  1079. }
  1080. // Set a frame around the entire image
  1081. function SetFrame($aDrawImgFrame=true,$aImgFrameColor=array(0,0,0),$aImgFrameWeight=1) {
  1082. $this->doframe = $aDrawImgFrame;
  1083. $this->frame_color = $aImgFrameColor;
  1084. $this->frame_weight = $aImgFrameWeight;
  1085. }
  1086. function SetFrameBevel($aDepth=3,$aBorder=false,$aBorderColor='black',$aColor1='white@0.4',$aColor2='darkgray@0.4',$aFlg=true) {
  1087. $this->framebevel = $aFlg ;
  1088. $this->framebeveldepth = $aDepth ;
  1089. $this->framebevelborder = $aBorder ;
  1090. $this->framebevelbordercolor = $aBorderColor ;
  1091. $this->framebevelcolor1 = $aColor1 ;
  1092. $this->framebevelcolor2 = $aColor2 ;
  1093. $this->doshadow = false ;
  1094. }
  1095. // Set the shadow around the whole image
  1096. function SetShadow($aShowShadow=true,$aShadowWidth=5,$aShadowColor=array(102,102,102)) {
  1097. $this->doshadow = $aShowShadow;
  1098. $this->shadow_color = $aShadowColor;
  1099. $this->shadow_width = $aShadowWidth;
  1100. $this->footer->iBottomMargin += $aShadowWidth;
  1101. $this->footer->iRightMargin += $aShadowWidth;
  1102. }
  1103. // Specify x,y scale. Note that if you manually specify the scale
  1104. // you must also specify the tick distance with a call to Ticks::Set()
  1105. function SetScale($aAxisType,$aYMin=1,$aYMax=1,$aXMin=1,$aXMax=1) {
  1106. $this->axtype = $aAxisType;
  1107. if( $aYMax < $aYMin || $aXMax < $aXMin )
  1108. JpGraphError::RaiseL(25020);//('Graph::SetScale(): Specified Max value must be larger than the specified Min value.');
  1109. $yt=substr($aAxisType,-3,3);
  1110. if( $yt=="lin" )
  1111. $this->yscale = new LinearScale($aYMin,$aYMax);
  1112. elseif( $yt == "int" ) {
  1113. $this->yscale = new LinearScale($aYMin,$aYMax);
  1114. $this->yscale->SetIntScale();
  1115. }
  1116. elseif( $yt=="log" )
  1117. $this->yscale = new LogScale($aYMin,$aYMax);
  1118. else
  1119. JpGraphError::RaiseL(25021,$aAxisType);//("Unknown scale specification for Y-scale. ($aAxisType)");
  1120. $xt=substr($aAxisType,0,3);
  1121. if( $xt == "lin" || $xt == "tex" ) {
  1122. $this->xscale = new LinearScale($aXMin,$aXMax,"x");
  1123. $this->xscale->textscale = ($xt == "tex");
  1124. }
  1125. elseif( $xt == "int" ) {
  1126. $this->xscale = new LinearScale($aXMin,$aXMax,"x");
  1127. $this->xscale->SetIntScale();
  1128. }
  1129. elseif( $xt == "dat" ) {
  1130. $this->xscale = new DateScale($aXMin,$aXMax,"x");
  1131. }
  1132. elseif( $xt == "log" )
  1133. $this->xscale = new LogScale($aXMin,$aXMax,"x");
  1134. else
  1135. JpGraphError::RaiseL(25022,$aAxisType);//(" Unknown scale specification for X-scale. ($aAxisType)");
  1136. $this->xaxis = new Axis($this->img,$this->xscale);
  1137. $this->yaxis = new Axis($this->img,$this->yscale);
  1138. $this->xgrid = new Grid($this->xaxis);
  1139. $this->ygrid = new Grid($this->yaxis);
  1140. $this->ygrid->Show();
  1141. }
  1142. // Specify secondary Y scale
  1143. function SetY2Scale($aAxisType="lin",$aY2Min=1,$aY2Max=1) {
  1144. if( $aAxisType=="lin" )
  1145. $this->y2scale = new LinearScale($aY2Min,$aY2Max);
  1146. elseif( $aAxisType == "int" ) {
  1147. $this->y2scale = new LinearScale($aY2Min,$aY2Max);
  1148. $this->y2scale->SetIntScale();
  1149. }
  1150. elseif( $aAxisType=="log" ) {
  1151. $this->y2scale = new LogScale($aY2Min,$aY2Max);
  1152. }
  1153. else JpGraphError::RaiseL(25023,$aAxisType);//("JpGraph: Unsupported Y2 axis type: $aAxisType\nMust be one of (lin,log,int)");
  1154. $this->y2axis = new Axis($this->img,$this->y2scale);
  1155. $this->y2axis->scale->ticks->SetDirection(SIDE_LEFT);
  1156. $this->y2axis->SetLabelSide(SIDE_RIGHT);
  1157. $this->y2axis->SetPos('max');
  1158. $this->y2axis->SetTitleSide(SIDE_RIGHT);
  1159. // Deafult position is the max x-value
  1160. $this->y2grid = new Grid($this->y2axis);
  1161. }
  1162. // Set the delta position (in pixels) between the multiple Y-axis
  1163. function SetYDeltaDist($aDist) {
  1164. $this->iYAxisDeltaPos = $aDist;
  1165. }
  1166. // Specify secondary Y scale
  1167. function SetYScale($aN,$aAxisType="lin",$aYMin=1,$aYMax=1) {
  1168. if( $aAxisType=="lin" )
  1169. $this->ynscale[$aN] = new LinearScale($aYMin,$aYMax);
  1170. elseif( $aAxisType == "int" ) {
  1171. $this->ynscale[$aN] = new LinearScale($aYMin,$aYMax);
  1172. $this->ynscale[$aN]->SetIntScale();
  1173. }
  1174. elseif( $aAxisType=="log" ) {
  1175. $this->ynscale[$aN] = new LogScale($aYMin,$aYMax);
  1176. }
  1177. else JpGraphError::RaiseL(25024,$aAxisType);//("JpGraph: Unsupported Y axis type: $aAxisType\nMust be one of (lin,log,int)");
  1178. $this->ynaxis[$aN] = new Axis($this->img,$this->ynscale[$aN]);
  1179. $this->ynaxis[$aN]->scale->ticks->SetDirection(SIDE_LEFT);
  1180. $this->ynaxis[$aN]->SetLabelSide(SIDE_RIGHT);
  1181. }
  1182. // Specify density of ticks when autoscaling 'normal', 'dense', 'sparse', 'verysparse'
  1183. // The dividing factor have been determined heuristically according to my aesthetic
  1184. // sense (or lack off) y.m.m.v !
  1185. function SetTickDensity($aYDensity=TICKD_NORMAL,$aXDensity=TICKD_NORMAL) {
  1186. $this->xtick_factor=30;
  1187. $this->ytick_factor=25;
  1188. switch( $aYDensity ) {
  1189. case TICKD_DENSE:
  1190. $this->ytick_factor=12;
  1191. break;
  1192. case TICKD_NORMAL:
  1193. $this->ytick_factor=25;
  1194. break;
  1195. case TICKD_SPARSE:
  1196. $this->ytick_factor=40;
  1197. break;
  1198. case TICKD_VERYSPARSE:
  1199. $this->ytick_factor=100;
  1200. break;
  1201. default:
  1202. JpGraphError::RaiseL(25025,$densy);//("JpGraph: Unsupported Tick density: $densy");
  1203. }
  1204. switch( $aXDensity ) {
  1205. case TICKD_DENSE:
  1206. $this->xtick_factor=15;
  1207. break;
  1208. case TICKD_NORMAL:
  1209. $this->xtick_factor=30;
  1210. break;
  1211. case TICKD_SPARSE:
  1212. $this->xtick_factor=45;
  1213. break;
  1214. case TICKD_VERYSPARSE:
  1215. $this->xtick_factor=60;
  1216. break;
  1217. default:
  1218. JpGraphError::RaiseL(25025,$densx);//("JpGraph: Unsupported Tick density: $densx");
  1219. }
  1220. }
  1221. // Get a string of all image map areas
  1222. function GetCSIMareas() {
  1223. if( !$this->iHasStroked )
  1224. $this->Stroke(_CSIM_SPECIALFILE);
  1225. $csim = $this->title->GetCSIMAreas();
  1226. $csim .= $this->subtitle->GetCSIMAreas();
  1227. $csim .= $this->subsubtitle->GetCSIMAreas();
  1228. $csim .= $this->legend->GetCSIMAreas();
  1229. if( $this->y2axis != NULL ) {
  1230. $csim .= $this->y2axis->title->GetCSIMAreas();
  1231. }
  1232. if( $this->texts != null ) {
  1233. $n = count($this->texts);
  1234. for($i=0; $i < $n; ++$i ) {
  1235. $csim .= $this->texts[$i]->GetCSIMAreas();
  1236. }
  1237. }
  1238. if( $this->y2texts != null && $this->y2scale != null ) {
  1239. $n = count($this->y2texts);
  1240. for($i=0; $i < $n; ++$i ) {
  1241. $csim .= $this->y2texts[$i]->GetCSIMAreas();
  1242. }
  1243. }
  1244. if( $this->yaxis != null && $this->xaxis != null ) {
  1245. $csim .= $this->yaxis->title->GetCSIMAreas();
  1246. $csim .= $this->xaxis->title->GetCSIMAreas();
  1247. }
  1248. $n = count($this->plots);
  1249. for( $i=0; $i < $n; ++$i )
  1250. $csim .= $this->plots[$i]->GetCSIMareas();
  1251. $n = count($this->y2plots);
  1252. for( $i=0; $i < $n; ++$i )
  1253. $csim .= $this->y2plots[$i]->GetCSIMareas();
  1254. $n = count($this->ynaxis);
  1255. for( $i=0; $i < $n; ++$i ) {
  1256. $m = count($this->ynplots[$i]);
  1257. for($j=0; $j < $m; ++$j ) {
  1258. $csim .= $this->ynplots[$i][$j]->GetCSIMareas();
  1259. }
  1260. }
  1261. $n = count($this->iTables);
  1262. for( $i=0; $i < $n; ++$i ) {
  1263. $csim .= $this->iTables[$i]->GetCSIMareas();
  1264. }
  1265. return $csim;
  1266. }
  1267. // Get a complete <MAP>..</MAP> tag for the final image map
  1268. function GetHTMLImageMap($aMapName) {
  1269. //$im = "<map name=\"$aMapName\" id=\"$aMapName\">\n";
  1270. $im = "<map name=\"$aMapName\" />\n";
  1271. $im .= $this->GetCSIMareas();
  1272. $im .= "</map>";
  1273. return $im;
  1274. }
  1275. function CheckCSIMCache($aCacheName,$aTimeOut=60) {
  1276. global $_SERVER;
  1277. if( $aCacheName=='auto' )
  1278. $aCacheName=basename($_SERVER['PHP_SELF']);
  1279. $this->csimcachename = CSIMCACHE_DIR.$aCacheName;
  1280. $this->csimcachetimeout = $aTimeOut;
  1281. // First determine if we need to check for a cached version
  1282. // This differs from the standard cache in the sense that the
  1283. // image and CSIM map HTML file is written relative to the directory
  1284. // the script executes in and not the specified cache directory.
  1285. // The reason for this is that the cache directory is not necessarily
  1286. // accessible from the HTTP server.
  1287. if( $this->csimcachename != '' ) {
  1288. $dir = dirname($this->csimcachename);
  1289. $base = basename($this->csimcachename);
  1290. $base = strtok($base,'.');
  1291. $suffix = strtok('.');
  1292. $basecsim = $dir.'/'.$base.'_csim_.html';
  1293. $baseimg = $dir.'/'.$base.'.'.$this->img->img_format;
  1294. $timedout=false;
  1295. // Does it exist at all ?
  1296. if( file_exists($basecsim) && file_exists($baseimg) ) {
  1297. // Check that it hasn't timed out
  1298. $diff=time()-filemtime($basecsim);
  1299. if( $this->csimcachetimeout>0 && ($diff > $this->csimcachetimeout*60) ) {
  1300. $timedout=true;
  1301. @unlink($basecsim);
  1302. @unlink($baseimg);
  1303. }
  1304. else {
  1305. if ($fh = @fopen($basecsim, "r")) {
  1306. fpassthru($fh);
  1307. return true;
  1308. }
  1309. else
  1310. JpGraphError::RaiseL(25027,$basecsim);//(" Can't open cached CSIM \"$basecsim\" for reading.");
  1311. }
  1312. }
  1313. }
  1314. return false;
  1315. }
  1316. function StrokeCSIM($aScriptName='auto',$aCSIMName='',$aBorder=0) {
  1317. if( $aCSIMName=='' ) {
  1318. // create a random map name
  1319. srand ((double) microtime() * 1000000);
  1320. $r = rand(0,100000);
  1321. $aCSIMName='__mapname'.$r.'__';
  1322. }
  1323. if( $aScriptName=='auto' )
  1324. $aScriptName=basename($_SERVER['PHP_SELF']);
  1325. if( empty($_GET[_CSIM_DISPLAY]) ) {
  1326. // First determine if we need to check for a cached version
  1327. // This differs from the standard cache in the sense that the
  1328. // image and CSIM map HTML file is written relative to the directory
  1329. // the script executes in and not the specified cache directory.
  1330. // The reason for this is that the cache directory is not necessarily
  1331. // accessible from the HTTP server.
  1332. if( $this->csimcachename != '' ) {
  1333. $dir = dirname($this->csimcachename);
  1334. $base = basename($this->csimcachename);
  1335. $base = strtok($base,'.');
  1336. $suffix = strtok('.');
  1337. $basecsim = $dir.'/'.$base.'_csim_.html';
  1338. $baseimg = $base.'.'.$this->img->img_format;
  1339. // Check that apache can write to directory specified
  1340. if( file_exists($dir) && !is_writeable($dir) ) {
  1341. JpgraphError::RaiseL(25028,$dir);//('Apache/PHP does not have permission to write to the CSIM cache directory ('.$dir.'). Check permissions.');
  1342. }
  1343. // Make sure directory exists
  1344. $this->cache->MakeDirs($dir);
  1345. // Write the image file
  1346. $this->Stroke(CSIMCACHE_DIR.$baseimg);
  1347. // Construct wrapper HTML and write to file and send it back to browser
  1348. $htmlwrap = $this->GetHTMLImageMap($aCSIMName)."\n".
  1349. '<img src="'.htmlentities(CSIMCACHE_HTTP_DIR.$baseimg).'" ismap usemap="#'.$aCSIMName.'" border='.$aBorder.' width='.$this->img->width.' height='.$this->img->height." alt=\"\" />\n";
  1350. if($fh = @fopen($basecsim,'w') ) {
  1351. fwrite($fh,$htmlwrap);
  1352. fclose($fh);
  1353. echo $htmlwrap;
  1354. }
  1355. else
  1356. JpGraphError::RaiseL(25029,$basecsim);//(" Can't write CSIM \"$basecsim\" for writing. Check free space and permissions.");
  1357. }
  1358. else {
  1359. if( $aScriptName=='' ) {
  1360. JpGraphError::RaiseL(25030);//('Missing script name in call to StrokeCSIM(). You must specify the name of the actual image script as the first parameter to StrokeCSIM().');
  1361. exit();
  1362. }
  1363. // This is a JPGRAPH internal defined that prevents
  1364. // us from recursively coming here again
  1365. $urlarg='?'._CSIM_DISPLAY.'=1';
  1366. // Now reconstruct any user URL argument
  1367. reset($_GET);
  1368. while( list($key,$value) = each($_GET) ) {
  1369. if( is_array($value) ) {
  1370. $n = count($value);
  1371. for( $i=0; $i < $n; ++$i ) {
  1372. $urlarg .= '&'.$key.'%5B%5D='.urlencode($value[$i]);
  1373. }
  1374. }
  1375. else {
  1376. $urlarg .= '&'.$key.'='.urlencode($value);
  1377. }
  1378. }
  1379. // It's not ideal to convert POST argument to GET arguments
  1380. // but there is little else we can do. One idea for the
  1381. // future might be recreate the POST header in case.
  1382. reset($_POST);
  1383. while( list($key,$value) = each($_POST) ) {
  1384. if( is_array($value) ) {
  1385. $n = count($value);
  1386. for( $i=0; $i < $n; ++$i ) {
  1387. $urlarg .= '&'.$key.'%5B%5D='.urlencode($value[$i]);
  1388. }
  1389. }
  1390. else {
  1391. $urlarg .= '&'.$key.'='.urlencode($value);
  1392. }
  1393. }
  1394. echo $this->GetHTMLImageMap($aCSIMName);
  1395. echo "<img src='".htmlentities($aScriptName.$urlarg)."' ismap usemap='#".$aCSIMName.'\' border='.$aBorder.' width='.$this->img->width.' height='.$this->img->height." alt=\"\" />\n";
  1396. }
  1397. }
  1398. else {
  1399. $this->Stroke();
  1400. }
  1401. }
  1402. function GetTextsYMinMax($aY2=false) {
  1403. if( $aY2 )
  1404. $txts = $this->y2texts;
  1405. else
  1406. $txts = $this->texts;
  1407. $n = count($txts);
  1408. $min=null;
  1409. $max=null;
  1410. for( $i=0; $i < $n; ++$i ) {
  1411. if( $txts[$i]->iScalePosY !== null &&
  1412. $txts[$i]->iScalePosX !== null ) {
  1413. if( $min === null ) {
  1414. $min = $max = $txts[$i]->iScalePosY ;
  1415. }
  1416. else {
  1417. $min = min($min,$txts[$i]->iScalePosY);
  1418. $max = max($max,$txts[$i]->iScalePosY);
  1419. }
  1420. }
  1421. }
  1422. if( $min !== null ) {
  1423. return array($min,$max);
  1424. }
  1425. else
  1426. return null;
  1427. }
  1428. function GetTextsXMinMax($aY2=false) {
  1429. if( $aY2 )
  1430. $txts = $this->y2texts;
  1431. else
  1432. $txts = $this->texts;
  1433. $n = count($txts);
  1434. $min=null;
  1435. $max=null;
  1436. for( $i=0; $i < $n; ++$i ) {
  1437. if( $txts[$i]->iScalePosY !== null &&
  1438. $txts[$i]->iScalePosX !== null ) {
  1439. if( $min === null ) {
  1440. $min = $max = $txts[$i]->iScalePosX ;
  1441. }
  1442. else {
  1443. $min = min($min,$txts[$i]->iScalePosX);
  1444. $max = max($max,$txts[$i]->iScalePosX);
  1445. }
  1446. }
  1447. }
  1448. if( $min !== null ) {
  1449. return array($min,$max);
  1450. }
  1451. else
  1452. return null;
  1453. }
  1454. function GetXMinMax() {
  1455. list($min,$ymin) = $this->plots[0]->Min();
  1456. list($max,$ymax) = $this->plots[0]->Max();
  1457. foreach( $this->plots as $p ) {
  1458. list($xmin,$ymin) = $p->Min();
  1459. list($xmax,$ymax) = $p->Max();
  1460. $min = Min($xmin,$min);
  1461. $max = Max($xmax,$max);
  1462. }
  1463. if( $this->y2axis != null ) {
  1464. foreach( $this->y2plots as $p ) {
  1465. list($xmin,$ymin) = $p->Min();
  1466. list($xmax,$ymax) = $p->Max();
  1467. $min = Min($xmin,$min);
  1468. $max = Max($xmax,$max);
  1469. }
  1470. }
  1471. $n = count($this->ynaxis);
  1472. for( $i=0; $i < $n; ++$i ) {
  1473. if( $this->ynaxis[$i] != null) {
  1474. foreach( $this->ynplots[$i] as $p ) {
  1475. list($xmin,$ymin) = $p->Min();
  1476. list($xmax,$ymax) = $p->Max();
  1477. $min = Min($xmin,$min);
  1478. $max = Max($xmax,$max);
  1479. }
  1480. }
  1481. }
  1482. return array($min,$max);
  1483. }
  1484. function AdjustMarginsForTitles() {
  1485. $totrequired =
  1486. ($this->title->t != '' ?
  1487. $this->title->GetTextHeight($this->img) + $this->title->margin + 5 : 0 ) +
  1488. ($this->subtitle->t != '' ?
  1489. $this->subtitle->GetTextHeight($this->img) + $this->subtitle->margin + 5 : 0 ) +
  1490. ($this->subsubtitle->t != '' ?
  1491. $this->subsubtitle->GetTextHeight($this->img) + $this->subsubtitle->margin + 5 : 0 ) ;
  1492. $btotrequired = 0;
  1493. if($this->xaxis != null && !$this->xaxis->hide && !$this->xaxis->hide_labels ) {
  1494. // Minimum bottom margin
  1495. if( $this->xaxis->title->t != '' ) {
  1496. if( $this->img->a == 90 )
  1497. $btotrequired = $this->yaxis->title->GetTextHeight($this->img) + 5 ;
  1498. else
  1499. $btotrequired = $this->xaxis->title->GetTextHeight($this->img) + 5 ;
  1500. }
  1501. else
  1502. $btotrequired = 0;
  1503. if( $this->img->a == 90 ) {
  1504. $this->img->SetFont($this->yaxis->font_family,$this->yaxis->font_style,
  1505. $this->yaxis->font_size);
  1506. $lh = $this->img->GetTextHeight('Mg',$this->yaxis->label_angle);
  1507. }
  1508. else {
  1509. $this->img->SetFont($this->xaxis->font_family,$this->xaxis->font_style,
  1510. $this->xaxis->font_size);
  1511. $lh = $this->img->GetTextHeight('Mg',$this->xaxis->label_angle);
  1512. }
  1513. $btotrequired += $lh + 5;
  1514. }
  1515. if( $this->img->a == 90 ) {
  1516. // DO Nothing. It gets too messy to do this properly for 90 deg...
  1517. }
  1518. else{
  1519. if( $this->img->top_margin < $totrequired ) {
  1520. $this->SetMargin($this->img->left_margin,$this->img->right_margin,
  1521. $totrequired,$this->img->bottom_margin);
  1522. }
  1523. if( $this->img->bottom_margin < $btotrequired ) {
  1524. $this->SetMargin($this->img->left_margin,$this->img->right_margin,
  1525. $this->img->top_margin,$btotrequired);
  1526. }
  1527. }
  1528. }
  1529. // Stroke the graph
  1530. // $aStrokeFileName If != "" the image will be written to this file and NOT
  1531. // streamed back to the browser
  1532. function Stroke($aStrokeFileName="") {
  1533. // Fist make a sanity check that user has specified a scale
  1534. if( empty($this->yscale) ) {
  1535. JpGraphError::RaiseL(25031);//('You must specify what scale to use with a call to Graph::SetScale().');
  1536. }
  1537. // Start by adjusting the margin so that potential titles will fit.
  1538. $this->AdjustMarginsForTitles();
  1539. // Setup scale constants
  1540. if( $this->yscale ) $this->yscale->InitConstants($this->img);
  1541. if( $this->xscale ) $this->xscale->InitConstants($this->img);
  1542. if( $this->y2scale ) $this->y2scale->InitConstants($this->img);
  1543. $n=count($this->ynscale);
  1544. for($i=0; $i < $n; ++$i) {
  1545. if( $this->ynscale[$i] ) $this->ynscale[$i]->InitConstants($this->img);
  1546. }
  1547. // If the filename is the predefined value = '_csim_special_'
  1548. // we assume that the call to stroke only needs to do enough
  1549. // to correctly generate the CSIM maps.
  1550. // We use this variable to skip things we don't strictly need
  1551. // to do to generate the image map to improve performance
  1552. // a best we can. Therefor you will see a lot of tests !$_csim in the
  1553. // code below.
  1554. $_csim = ($aStrokeFileName===_CSIM_SPECIALFILE);
  1555. // We need to know if we have stroked the plot in the
  1556. // GetCSIMareas. Otherwise the CSIM hasn't been generated
  1557. // and in the case of GetCSIM called before stroke to generate
  1558. // CSIM without storing an image to disk GetCSIM must call Stroke.
  1559. $this->iHasStroked = true;
  1560. // Do any pre-stroke adjustment that is needed by the different plot types
  1561. // (i.e bar plots want's to add an offset to the x-labels etc)
  1562. for($i=0; $i < count($this->plots) ; ++$i ) {
  1563. $this->plots[$i]->PreStrokeAdjust($this);
  1564. $this->plots[$i]->DoLegend($this);
  1565. }
  1566. // Any plots on the second Y scale?
  1567. if( $this->y2scale != null ) {
  1568. for($i=0; $i<count($this->y2plots) ; ++$i ) {
  1569. $this->y2plots[$i]->PreStrokeAdjust($this);
  1570. $this->y2plots[$i]->DoLegend($this);
  1571. }
  1572. }
  1573. // Any plots on the extra Y axises?
  1574. $n = count($this->ynaxis);
  1575. for($i=0; $i<$n ; ++$i ) {
  1576. if( $this->ynplots == null || $this->ynplots[$i] == null) {
  1577. JpGraphError::RaiseL(25032,$i);//("No plots for Y-axis nbr:$i");
  1578. }
  1579. $m = count($this->ynplots[$i]);
  1580. for($j=0; $j < $m; ++$j ) {
  1581. $this->ynplots[$i][$j]->PreStrokeAdjust($this);
  1582. $this->ynplots[$i][$j]->DoLegend($this);
  1583. }
  1584. }
  1585. // Bail out if any of the Y-axis not been specified and
  1586. // has no plots. (This means it is impossible to do autoscaling and
  1587. // no other scale was given so we can't possible draw anything). If you use manual
  1588. // scaling you also have to supply the tick steps as well.
  1589. if( (!$this->yscale->IsSpecified() && count($this->plots)==0) ||
  1590. ($this->y2scale!=null && !$this->y2scale->IsSpecified() && count($this->y2plots)==0) ) {
  1591. //$e = "n=".count($this->y2plots)."\n";
  1592. // $e = "Can't draw unspecified Y-scale.<br>\nYou have either:<br>\n";
  1593. // $e .= "1. Specified an Y axis for autoscaling but have not supplied any plots<br>\n";
  1594. // $e .= "2. Specified a scale manually but have forgot to specify the tick steps";
  1595. JpGraphError::RaiseL(25026);
  1596. }
  1597. // Bail out if no plots and no specified X-scale
  1598. if( (!$this->xscale->IsSpecified() && count($this->plots)==0 && count($this->y2plots)==0) )
  1599. JpGraphError::RaiseL(25034);//("<strong>JpGraph: Can't draw unspecified X-scale.</strong><br>No plots.<br>");
  1600. //Check if we should autoscale y-axis
  1601. if( !$this->yscale->IsSpecified() && count($this->plots)>0 ) {
  1602. list($min,$max) = $this->GetPlotsYMinMax($this->plots);
  1603. $lres = $this->GetLinesYMinMax($this->lines);
  1604. if( is_array($lres) ) {
  1605. list($linmin,$linmax) = $lres ;
  1606. $min = min($min,$linmin);
  1607. $max = max($max,$linmax);
  1608. }
  1609. $tres = $this->GetTextsYMinMax();
  1610. if( is_array($tres) ) {
  1611. list($tmin,$tmax) = $tres ;
  1612. $min = min($min,$tmin);
  1613. $max = max($max,$tmax);
  1614. }
  1615. $this->yscale->AutoScale($this->img,$min,$max,
  1616. $this->img->plotheight/$this->ytick_factor);
  1617. }
  1618. elseif( $this->yscale->IsSpecified() &&
  1619. ( $this->yscale->auto_ticks || !$this->yscale->ticks->IsSpecified()) ) {
  1620. // The tick calculation will use the user suplied min/max values to determine
  1621. // the ticks. If auto_ticks is false the exact user specifed min and max
  1622. // values will be used for the scale.
  1623. // If auto_ticks is true then the scale might be slightly adjusted
  1624. // so that the min and max values falls on an even major step.
  1625. $min = $this->yscale->scale[0];
  1626. $max = $this->yscale->scale[1];
  1627. $this->yscale->AutoScale($this->img,$min,$max,
  1628. $this->img->plotheight/$this->ytick_factor,
  1629. $this->yscale->auto_ticks);
  1630. }
  1631. if( $this->y2scale != null) {
  1632. if( !$this->y2scale->IsSpecified() && count($this->y2plots)>0 ) {
  1633. list($min,$max) = $this->GetPlotsYMinMax($this->y2plots);
  1634. $lres = $this->GetLinesYMinMax($this->y2lines);
  1635. if( is_array($lres) ) {
  1636. list($linmin,$linmax) = $lres ;
  1637. $min = min($min,$linmin);
  1638. $max = max($max,$linmax);
  1639. }
  1640. $tres = $this->GetTextsYMinMax(true);
  1641. if( is_array($tres) ) {
  1642. list($tmin,$tmax) = $tres ;
  1643. $min = min($min,$tmin);
  1644. $max = max($max,$tmax);
  1645. }
  1646. $this->y2scale->AutoScale($this->img,$min,$max,$this->img->plotheight/$this->ytick_factor);
  1647. }
  1648. elseif( $this->y2scale->IsSpecified() &&
  1649. ( $this->y2scale->auto_ticks || !$this->y2scale->ticks->IsSpecified()) ) {
  1650. // The tick calculation will use the user suplied min/max values to determine
  1651. // the ticks. If auto_ticks is false the exact user specifed min and max
  1652. // values will be used for the scale.
  1653. // If auto_ticks is true then the scale might be slightly adjusted
  1654. // so that the min and max values falls on an even major step.
  1655. $min = $this->y2scale->scale[0];
  1656. $max = $this->y2scale->scale[1];
  1657. $this->y2scale->AutoScale($this->img,$min,$max,
  1658. $this->img->plotheight/$this->ytick_factor,
  1659. $this->y2scale->auto_ticks);
  1660. }
  1661. }
  1662. //
  1663. // Autoscale the multiple Y-axis
  1664. //
  1665. $n = count($this->ynaxis);
  1666. for( $i=0; $i < $n; ++$i ) {
  1667. if( $this->ynscale[$i] != null) {
  1668. if( !$this->ynscale[$i]->IsSpecified() && count($this->ynplots[$i])>0 ) {
  1669. list($min,$max) = $this->GetPlotsYMinMax($this->ynplots[$i]);
  1670. $this->ynscale[$i]->AutoScale($this->img,$min,$max,$this->img->plotheight/$this->ytick_factor);
  1671. }
  1672. elseif( $this->ynscale[$i]->IsSpecified() &&
  1673. ( $this->ynscale[$i]->auto_ticks || !$this->ynscale[$i]->ticks->IsSpecified()) ) {
  1674. // The tick calculation will use the user suplied min/max values to determine
  1675. // the ticks. If auto_ticks is false the exact user specifed min and max
  1676. // values will be used for the scale.
  1677. // If auto_ticks is true then the scale might be slightly adjusted
  1678. // so that the min and max values falls on an even major step.
  1679. $min = $this->ynscale[$i]->scale[0];
  1680. $max = $this->ynscale[$i]->scale[1];
  1681. $this->ynscale[$i]->AutoScale($this->img,$min,$max,
  1682. $this->img->plotheight/$this->ytick_factor,
  1683. $this->ynscale[$i]->auto_ticks);
  1684. }
  1685. }
  1686. }
  1687. //Check if we should autoscale x-axis
  1688. if( !$this->xscale->IsSpecified() ) {
  1689. if( substr($this->axtype,0,4) == "text" ) {
  1690. $max=0;
  1691. $n = count($this->plots);
  1692. for($i=0; $i < $n; ++$i ) {
  1693. $p = $this->plots[$i];
  1694. // We need some unfortunate sub class knowledge here in order
  1695. // to increase number of data points in case it is a line plot
  1696. // which has the barcenter set. If not it could mean that the
  1697. // last point of the data is outside the scale since the barcenter
  1698. // settings means that we will shift the entire plot half a tick step
  1699. // to the right in oder to align with the center of the bars.
  1700. if( is_a($p,'BarPlot') || empty($p->barcenter)) {
  1701. $max=max($max,$p->numpoints-1);
  1702. }
  1703. else {
  1704. $max=max($max,$p->numpoints);
  1705. }
  1706. }
  1707. $min=0;
  1708. if( $this->y2axis != null ) {
  1709. foreach( $this->y2plots as $p ) {
  1710. $max=max($max,$p->numpoints-1);
  1711. }
  1712. }
  1713. $n = count($this->ynaxis);
  1714. for( $i=0; $i < $n; ++$i ) {
  1715. if( $this->ynaxis[$i] != null) {
  1716. foreach( $this->ynplots[$i] as $p ) {
  1717. $max=max($max,$p->numpoints-1);
  1718. }
  1719. }
  1720. }
  1721. $this->xscale->Update($this->img,$min,$max);
  1722. $this->xscale->ticks->Set($this->xaxis->tick_step,1);
  1723. $this->xscale->ticks->SupressMinorTickMarks();
  1724. }
  1725. else {
  1726. list($min,$max) = $this->GetXMinMax();
  1727. $lres = $this->GetLinesXMinMax($this->lines);
  1728. if( $lres ) {
  1729. list($linmin,$linmax) = $lres ;
  1730. $min = min($min,$linmin);
  1731. $max = max($max,$linmax);
  1732. }
  1733. $lres = $this->GetLinesXMinMax($this->y2lines);
  1734. if( $lres ) {
  1735. list($linmin,$linmax) = $lres ;
  1736. $min = min($min,$linmin);
  1737. $max = max($max,$linmax);
  1738. }
  1739. $tres = $this->GetTextsXMinMax();
  1740. if( $tres ) {
  1741. list($tmin,$tmax) = $tres ;
  1742. $min = min($min,$tmin);
  1743. $max = max($max,$tmax);
  1744. }
  1745. $tres = $this->GetTextsXMinMax(true);
  1746. if( $tres ) {
  1747. list($tmin,$tmax) = $tres ;
  1748. $min = min($min,$tmin);
  1749. $max = max($max,$tmax);
  1750. }
  1751. $this->xscale->AutoScale($this->img,$min,$max,round($this->img->plotwidth/$this->xtick_factor));
  1752. }
  1753. //Adjust position of y-axis and y2-axis to minimum/maximum of x-scale
  1754. if( !is_numeric($this->yaxis->pos) && !is_string($this->yaxis->pos) )
  1755. $this->yaxis->SetPos($this->xscale->GetMinVal());
  1756. if( $this->y2axis != null ) {
  1757. if( !is_numeric($this->y2axis->pos) && !is_string($this->y2axis->pos) )
  1758. $this->y2axis->SetPos($this->xscale->GetMaxVal());
  1759. $this->y2axis->SetTitleSide(SIDE_RIGHT);
  1760. }
  1761. $n = count($this->ynaxis);
  1762. $nY2adj = $this->y2axis != null ? $this->iYAxisDeltaPos : 0;
  1763. for( $i=0; $i < $n; ++$i ) {
  1764. if( $this->ynaxis[$i] != null ) {
  1765. if( !is_numeric($this->ynaxis[$i]->pos) && !is_string($this->ynaxis[$i]->pos) ) {
  1766. $this->ynaxis[$i]->SetPos($this->xscale->GetMaxVal());
  1767. $this->ynaxis[$i]->SetPosAbsDelta($i*$this->iYAxisDeltaPos + $nY2adj);
  1768. }
  1769. $this->ynaxis[$i]->SetTitleSide(SIDE_RIGHT);
  1770. }
  1771. }
  1772. }
  1773. elseif( $this->xscale->IsSpecified() &&
  1774. ( $this->xscale->auto_ticks || !$this->xscale->ticks->IsSpecified()) ) {
  1775. // The tick calculation will use the user suplied min/max values to determine
  1776. // the ticks. If auto_ticks is false the exact user specifed min and max
  1777. // values will be used for the scale.
  1778. // If auto_ticks is true then the scale might be slightly adjusted
  1779. // so that the min and max values falls on an even major step.
  1780. $min = $this->xscale->scale[0];
  1781. $max = $this->xscale->scale[1];
  1782. $this->xscale->AutoScale($this->img,$min,$max,
  1783. $this->img->plotwidth/$this->xtick_factor,
  1784. false);
  1785. if( $this->y2axis != null ) {
  1786. if( !is_numeric($this->y2axis->pos) && !is_string($this->y2axis->pos) )
  1787. $this->y2axis->SetPos($this->xscale->GetMaxVal());
  1788. $this->y2axis->SetTitleSide(SIDE_RIGHT);
  1789. }
  1790. }
  1791. // If we have a negative values and x-axis position is at 0
  1792. // we need to supress the first and possible the last tick since
  1793. // they will be drawn on top of the y-axis (and possible y2 axis)
  1794. // The test below might seem strange the reasone being that if
  1795. // the user hasn't specified a value for position this will not
  1796. // be set until we do the stroke for the axis so as of now it
  1797. // is undefined.
  1798. // For X-text scale we ignore all this since the tick are usually
  1799. // much further in and not close to the Y-axis. Hence the test
  1800. // for 'text'
  1801. if( ($this->yaxis->pos==$this->xscale->GetMinVal() ||
  1802. (is_string($this->yaxis->pos) && $this->yaxis->pos=='min')) &&
  1803. !is_numeric($this->xaxis->pos) && $this->yscale->GetMinVal() < 0 &&
  1804. substr($this->axtype,0,4) != 'text' && $this->xaxis->pos!="min" ) {
  1805. //$this->yscale->ticks->SupressZeroLabel(false);
  1806. $this->xscale->ticks->SupressFirst();
  1807. if( $this->y2axis != null ) {
  1808. $this->xscale->ticks->SupressLast();
  1809. }
  1810. }
  1811. elseif( !is_numeric($this->yaxis->pos) && $this->yaxis->pos=='max' ) {
  1812. $this->xscale->ticks->SupressLast();
  1813. }
  1814. if( !$_csim ) {
  1815. $this->StrokePlotArea();
  1816. if( $this->iIconDepth == DEPTH_BACK ) {
  1817. $this->StrokeIcons();
  1818. }
  1819. }
  1820. $this->StrokeAxis();
  1821. // Stroke bands
  1822. if( $this->bands != null && !$_csim)
  1823. for($i=0; $i < count($this->bands); ++$i) {
  1824. // Stroke all bands that asks to be in the background
  1825. if( $this->bands[$i]->depth == DEPTH_BACK )
  1826. $this->bands[$i]->Stroke($this->img,$this->xscale,$this->yscale);
  1827. }
  1828. if( $this->y2bands != null && $this->y2scale != null && !$_csim )
  1829. for($i=0; $i < count($this->y2bands); ++$i) {
  1830. // Stroke all bands that asks to be in the foreground
  1831. if( $this->y2bands[$i]->depth == DEPTH_BACK )
  1832. $this->y2bands[$i]->Stroke($this->img,$this->xscale,$this->y2scale);
  1833. }
  1834. if( $this->grid_depth == DEPTH_BACK && !$_csim) {
  1835. $this->ygrid->Stroke();
  1836. $this->xgrid->Stroke();
  1837. }
  1838. // Stroke Y2-axis
  1839. if( $this->y2axis != null && !$_csim) {
  1840. $this->y2axis->Stroke($this->xscale);
  1841. $this->y2grid->Stroke();
  1842. }
  1843. // Stroke yn-axis
  1844. $n = count($this->ynaxis);
  1845. for( $i=0; $i < $n; ++$i ) {
  1846. $this->ynaxis[$i]->Stroke($this->xscale);
  1847. }
  1848. $oldoff=$this->xscale->off;
  1849. if(substr($this->axtype,0,4)=="text") {
  1850. if( $this->text_scale_abscenteroff > -1 ) {
  1851. // For a text scale the scale factor is the number of pixel per step.
  1852. // Hence we can use the scale factor as a substitute for number of pixels
  1853. // per major scale step and use that in order to adjust the offset so that
  1854. // an object of width "abscenteroff" becomes centered.
  1855. $this->xscale->off += round($this->xscale->scale_factor/2)-round($this->text_scale_abscenteroff/2);
  1856. }
  1857. else {
  1858. $this->xscale->off +=
  1859. ceil($this->xscale->scale_factor*$this->text_scale_off*$this->xscale->ticks->minor_step);
  1860. }
  1861. }
  1862. if( $this->iDoClipping ) {
  1863. $oldimage = $this->img->CloneCanvasH();
  1864. }
  1865. if( ! $this->y2orderback ) {
  1866. // Stroke all plots for Y axis
  1867. for($i=0; $i < count($this->plots); ++$i) {
  1868. $this->plots[$i]->Stroke($this->img,$this->xscale,$this->yscale);
  1869. $this->plots[$i]->StrokeMargin($this->img);
  1870. }
  1871. }
  1872. // Stroke all plots for Y2 axis
  1873. if( $this->y2scale != null )
  1874. for($i=0; $i< count($this->y2plots); ++$i ) {
  1875. $this->y2plots[$i]->Stroke($this->img,$this->xscale,$this->y2scale);
  1876. }
  1877. if( $this->y2orderback ) {
  1878. // Stroke all plots for Y1 axis
  1879. for($i=0; $i < count($this->plots); ++$i) {
  1880. $this->plots[$i]->Stroke($this->img,$this->xscale,$this->yscale);
  1881. $this->plots[$i]->StrokeMargin($this->img);
  1882. }
  1883. }
  1884. $n = count($this->ynaxis);
  1885. for( $i=0; $i < $n; ++$i ) {
  1886. $m = count($this->ynplots[$i]);
  1887. for( $j=0; $j < $m; ++$j ) {
  1888. $this->ynplots[$i][$j]->Stroke($this->img,$this->xscale,$this->ynscale[$i]);
  1889. $this->ynplots[$i][$j]->StrokeMargin($this->img);
  1890. }
  1891. }
  1892. if( $this->iIconDepth == DEPTH_FRONT) {
  1893. $this->StrokeIcons();
  1894. }
  1895. if( $this->iDoClipping ) {
  1896. // Clipping only supports graphs at 0 and 90 degrees
  1897. if( $this->img->a == 0 ) {
  1898. $this->img->CopyCanvasH($oldimage,$this->img->img,
  1899. $this->img->left_margin,$this->img->top_margin,
  1900. $this->img->left_margin,$this->img->top_margin,
  1901. $this->img->plotwidth+1,$this->img->plotheight);
  1902. }
  1903. elseif( $this->img->a == 90 ) {
  1904. $adj = ($this->img->height - $this->img->width)/2;
  1905. $this->img->CopyCanvasH($oldimage,$this->img->img,
  1906. $this->img->bottom_margin-$adj,$this->img->left_margin+$adj,
  1907. $this->img->bottom_margin-$adj,$this->img->left_margin+$adj,
  1908. $this->img->plotheight+1,$this->img->plotwidth);
  1909. }
  1910. else {
  1911. JpGraphError::RaiseL(25035,$this->img->a);//('You have enabled clipping. Cliping is only supported for graphs at 0 or 90 degrees rotation. Please adjust you current angle (='.$this->img->a.' degrees) or disable clipping.');
  1912. }
  1913. $this->img->Destroy();
  1914. $this->img->SetCanvasH($oldimage);
  1915. }
  1916. $this->xscale->off=$oldoff;
  1917. if( $this->grid_depth == DEPTH_FRONT && !$_csim ) {
  1918. $this->ygrid->Stroke();
  1919. $this->xgrid->Stroke();
  1920. }
  1921. // Stroke bands
  1922. if( $this->bands!= null )
  1923. for($i=0; $i < count($this->bands); ++$i) {
  1924. // Stroke all bands that asks to be in the foreground
  1925. if( $this->bands[$i]->depth == DEPTH_FRONT )
  1926. $this->bands[$i]->Stroke($this->img,$this->xscale,$this->yscale);
  1927. }
  1928. if( $this->y2bands!= null && $this->y2scale != null )
  1929. for($i=0; $i < count($this->y2bands); ++$i) {
  1930. // Stroke all bands that asks to be in the foreground
  1931. if( $this->y2bands[$i]->depth == DEPTH_FRONT )
  1932. $this->y2bands[$i]->Stroke($this->img,$this->xscale,$this->y2scale);
  1933. }
  1934. // Stroke any lines added
  1935. if( $this->lines != null ) {
  1936. for($i=0; $i < count($this->lines); ++$i) {
  1937. $this->lines[$i]->Stroke($this->img,$this->xscale,$this->yscale);
  1938. }
  1939. }
  1940. if( $this->y2lines != null && $this->y2scale != null ) {
  1941. for($i=0; $i < count($this->y2lines); ++$i) {
  1942. $this->y2lines[$i]->Stroke($this->img,$this->xscale,$this->y2scale);
  1943. }
  1944. }
  1945. // Finally draw the axis again since some plots may have nagged
  1946. // the axis in the edges.However we do no stroke the labels again
  1947. // since any user defined callback would be called twice. It also
  1948. // enhances performance.
  1949. if( !$_csim )
  1950. $this->StrokeAxis(false);
  1951. if( $this->y2scale != null && !$_csim )
  1952. $this->y2axis->Stroke($this->xscale,false);
  1953. if( !$_csim ) {
  1954. $this->StrokePlotBox();
  1955. }
  1956. // The titles and legends never gets rotated so make sure
  1957. // that the angle is 0 before stroking them
  1958. $aa = $this->img->SetAngle(0);
  1959. $this->StrokeTitles();
  1960. $this->footer->Stroke($this->img);
  1961. $this->legend->Stroke($this->img);
  1962. $this->img->SetAngle($aa);
  1963. $this->StrokeTexts();
  1964. $this->StrokeTables();
  1965. if( !$_csim ) {
  1966. $this->img->SetAngle($aa);
  1967. // Draw an outline around the image map
  1968. if(_JPG_DEBUG) {
  1969. $this->DisplayClientSideaImageMapAreas();
  1970. }
  1971. // Adjust the appearance of the image
  1972. $this->AdjustSaturationBrightnessContrast();
  1973. // Should we do any final image transformation
  1974. if( $this->iImgTrans ) {
  1975. if( !class_exists('ImgTrans') ) {
  1976. require_once('jpgraph_imgtrans.php');
  1977. //JpGraphError::Raise('In order to use image transformation you must include the file jpgraph_imgtrans.php in your script.');
  1978. }
  1979. $tform = new ImgTrans($this->img->img);
  1980. $this->img->img = $tform->Skew3D($this->iImgTransHorizon,$this->iImgTransSkewDist,
  1981. $this->iImgTransDirection,$this->iImgTransHighQ,
  1982. $this->iImgTransMinSize,$this->iImgTransFillColor,
  1983. $this->iImgTransBorder);
  1984. }
  1985. // If the filename is given as the special "__handle"
  1986. // then the image handler is returned and the image is NOT
  1987. // streamed back
  1988. if( $aStrokeFileName == _IMG_HANDLER ) {
  1989. return $this->img->img;
  1990. }
  1991. else {
  1992. // Finally stream the generated picture
  1993. $this->cache->PutAndStream($this->img,$this->cache_name,$this->inline,$aStrokeFileName);
  1994. }
  1995. }
  1996. }
  1997. function SetAxisLabelBackground($aType,$aXFColor='lightgray',$aXColor='black',$aYFColor='lightgray',$aYColor='black') {
  1998. $this->iAxisLblBgType = $aType;
  1999. $this->iXAxisLblBgFillColor = $aXFColor;
  2000. $this->iXAxisLblBgColor = $aXColor;
  2001. $this->iYAxisLblBgFillColor = $aYFColor;
  2002. $this->iYAxisLblBgColor = $aYColor;
  2003. }
  2004. //---------------
  2005. // PRIVATE METHODS
  2006. function StrokeAxisLabelBackground() {
  2007. // Types
  2008. // 0 = No background
  2009. // 1 = Only X-labels, length of axis
  2010. // 2 = Only Y-labels, length of axis
  2011. // 3 = As 1 but extends to width of graph
  2012. // 4 = As 2 but extends to height of graph
  2013. // 5 = Combination of 3 & 4
  2014. // 6 = Combination of 1 & 2
  2015. $t = $this->iAxisLblBgType ;
  2016. if( $t < 1 ) return;
  2017. // Stroke optional X-axis label background color
  2018. if( $t == 1 || $t == 3 || $t == 5 || $t == 6 ) {
  2019. $this->img->PushColor($this->iXAxisLblBgFillColor);
  2020. if( $t == 1 || $t == 6 ) {
  2021. $xl = $this->img->left_margin;
  2022. $yu = $this->img->height - $this->img->bottom_margin + 1;
  2023. $xr = $this->img->width - $this->img->right_margin ;
  2024. $yl = $this->img->height-1-$this->frame_weight;
  2025. }
  2026. else { // t==3 || t==5
  2027. $xl = $this->frame_weight;
  2028. $yu = $this->img->height - $this->img->bottom_margin + 1;
  2029. $xr = $this->img->width - 1 - $this->frame_weight;
  2030. $yl = $this->img->height-1-$this->frame_weight;
  2031. }
  2032. $this->img->FilledRectangle($xl,$yu,$xr,$yl);
  2033. $this->img->PopColor();
  2034. // Check if we should add the vertical lines at left and right edge
  2035. if( $this->iXAxisLblBgColor !== '' ) {
  2036. $this->img->PushColor($this->iXAxisLblBgColor);
  2037. if( $t == 1 || $t == 6 ) {
  2038. $this->img->Line($xl,$yu,$xl,$yl);
  2039. $this->img->Line($xr,$yu,$xr,$yl);
  2040. }
  2041. else {
  2042. $xl = $this->img->width - $this->img->right_margin ;
  2043. $this->img->Line($xl,$yu-1,$xr,$yu-1);
  2044. }
  2045. $this->img->PopColor();
  2046. }
  2047. }
  2048. if( $t == 2 || $t == 4 || $t == 5 || $t == 6 ) {
  2049. $this->img->PushColor($this->iYAxisLblBgFillColor);
  2050. if( $t == 2 || $t == 6 ) {
  2051. $xl = $this->frame_weight;
  2052. $yu = $this->frame_weight+$this->img->top_margin;
  2053. $xr = $this->img->left_margin - 1;
  2054. $yl = $this->img->height - $this->img->bottom_margin + 1;
  2055. }
  2056. else {
  2057. $xl = $this->frame_weight;
  2058. $yu = $this->frame_weight;
  2059. $xr = $this->img->left_margin - 1;
  2060. $yl = $this->img->height-1-$this->frame_weight;
  2061. }
  2062. $this->img->FilledRectangle($xl,$yu,$xr,$yl);
  2063. $this->img->PopColor();
  2064. // Check if we should add the vertical lines at left and right edge
  2065. if( $this->iXAxisLblBgColor !== '' ) {
  2066. $this->img->PushColor($this->iXAxisLblBgColor);
  2067. if( $t == 2 || $t == 6 ) {
  2068. $this->img->Line($xl,$yu-1,$xr,$yu-1);
  2069. $this->img->Line($xl,$yl-1,$xr,$yl-1);
  2070. }
  2071. else {
  2072. $this->img->Line($xr+1,$yu,$xr+1,$this->img->top_margin);
  2073. }
  2074. $this->img->PopColor();
  2075. }
  2076. }
  2077. }
  2078. function StrokeAxis($aStrokeLabels=true) {
  2079. if( $aStrokeLabels ) {
  2080. $this->StrokeAxisLabelBackground();
  2081. }
  2082. // Stroke axis
  2083. if( $this->iAxisStyle != AXSTYLE_SIMPLE ) {
  2084. switch( $this->iAxisStyle ) {
  2085. case AXSTYLE_BOXIN :
  2086. $toppos = SIDE_DOWN;
  2087. $bottompos = SIDE_UP;
  2088. $leftpos = SIDE_RIGHT;
  2089. $rightpos = SIDE_LEFT;
  2090. break;
  2091. case AXSTYLE_BOXOUT :
  2092. $toppos = SIDE_UP;
  2093. $bottompos = SIDE_DOWN;
  2094. $leftpos = SIDE_LEFT;
  2095. $rightpos = SIDE_RIGHT;
  2096. break;
  2097. case AXSTYLE_YBOXIN:
  2098. $toppos = -100;
  2099. $bottompos = SIDE_UP;
  2100. $leftpos = SIDE_RIGHT;
  2101. $rightpos = SIDE_LEFT;
  2102. break;
  2103. case AXSTYLE_YBOXOUT:
  2104. $toppos = -100;
  2105. $bottompos = SIDE_DOWN;
  2106. $leftpos = SIDE_LEFT;
  2107. $rightpos = SIDE_RIGHT;
  2108. break;
  2109. default:
  2110. JpGRaphError::RaiseL(25036,$this->iAxisStyle); //('Unknown AxisStyle() : '.$this->iAxisStyle);
  2111. break;
  2112. }
  2113. $this->xaxis->SetPos('min');
  2114. // By default we hide the first label so it doesn't cross the
  2115. // Y-axis in case the positon hasn't been set by the user.
  2116. // However, if we use a box we always want the first value
  2117. // displayed so we make sure it will be displayed.
  2118. $this->xscale->ticks->SupressFirst(false);
  2119. $this->xaxis->SetLabelSide(SIDE_DOWN);
  2120. $this->xaxis->scale->ticks->SetSide($bottompos);
  2121. $this->xaxis->Stroke($this->yscale);
  2122. if( $toppos != -100 ) {
  2123. // To avoid side effects we work on a new copy
  2124. $maxis = $this->xaxis;
  2125. $maxis->SetPos('max');
  2126. $maxis->SetLabelSide(SIDE_UP);
  2127. $maxis->SetLabelMargin(7);
  2128. $this->xaxis->scale->ticks->SetSide($toppos);
  2129. $maxis->Stroke($this->yscale);
  2130. }
  2131. $this->yaxis->SetPos('min');
  2132. $this->yaxis->SetLabelMargin(10);
  2133. $this->yaxis->SetLabelSide(SIDE_LEFT);
  2134. $this->yaxis->scale->ticks->SetSide($leftpos);
  2135. $this->yaxis->Stroke($this->xscale);
  2136. $myaxis = $this->yaxis;
  2137. $myaxis->SetPos('max');
  2138. $myaxis->SetLabelMargin(10);
  2139. $myaxis->SetLabelSide(SIDE_RIGHT);
  2140. $myaxis->title->Set('');
  2141. $myaxis->scale->ticks->SetSide($rightpos);
  2142. $myaxis->Stroke($this->xscale);
  2143. }
  2144. else {
  2145. $this->xaxis->Stroke($this->yscale,$aStrokeLabels);
  2146. $this->yaxis->Stroke($this->xscale,$aStrokeLabels);
  2147. }
  2148. }
  2149. // Private helper function for backgound image
  2150. function LoadBkgImage($aImgFormat='',$aFile='',$aImgStr='') {
  2151. if( $aImgStr != '' ) {
  2152. return Image::CreateFromString($aImgStr);
  2153. }
  2154. if( $aFile == '' )
  2155. $aFile = $this->background_image;
  2156. // Remove case sensitivity and setup appropriate function to create image
  2157. // Get file extension. This should be the LAST '.' separated part of the filename
  2158. $e = explode('.',$aFile);
  2159. $ext = strtolower($e[count($e)-1]);
  2160. if ($ext == "jpeg") {
  2161. $ext = "jpg";
  2162. }
  2163. if( trim($ext) == '' )
  2164. $ext = 'png'; // Assume PNG if no extension specified
  2165. if( $aImgFormat == '' )
  2166. $imgtag = $ext;
  2167. else
  2168. $imgtag = $aImgFormat;
  2169. $supported = imagetypes();
  2170. if( ( $ext == 'jpg' && !($supported & IMG_JPG) ) ||
  2171. ( $ext == 'gif' && !($supported & IMG_GIF) ) ||
  2172. ( $ext == 'png' && !($supported & IMG_PNG) ) ) {
  2173. JpGraphError::RaiseL(25037,$aFile);//('The image format of your background image ('.$aFile.') is not supported in your system configuration. ');
  2174. }
  2175. if( $imgtag == "jpg" || $imgtag == "jpeg")
  2176. {
  2177. $f = "imagecreatefromjpeg";
  2178. $imgtag = "jpg";
  2179. }
  2180. else
  2181. {
  2182. $f = "imagecreatefrom".$imgtag;
  2183. }
  2184. // Compare specified image type and file extension
  2185. if( $imgtag != $ext ) {
  2186. //$t = "Background image seems to be of different type (has different file extension) than specified imagetype. Specified: '".$aImgFormat."'File: '".$aFile."'";
  2187. JpGraphError::RaiseL(25038, $aImgFormat, $aFile);
  2188. }
  2189. $img = @$f($aFile);
  2190. if( !$img ) {
  2191. JpGraphError::RaiseL(25039,$aFile);//(" Can't read background image: '".$aFile."'");
  2192. }
  2193. return $img;
  2194. }
  2195. function StrokeBackgroundGrad() {
  2196. if( $this->bkg_gradtype < 0 )
  2197. return;
  2198. $grad = new Gradient($this->img);
  2199. if( $this->bkg_gradstyle == BGRAD_PLOT ) {
  2200. $xl = $this->img->left_margin;
  2201. $yt = $this->img->top_margin;
  2202. $xr = $xl + $this->img->plotwidth+1 ;
  2203. $yb = $yt + $this->img->plotheight ;
  2204. $grad->FilledRectangle($xl,$yt,$xr,$yb,$this->bkg_gradfrom,$this->bkg_gradto,$this->bkg_gradtype);
  2205. }
  2206. else {
  2207. $xl = 0;
  2208. $yt = 0;
  2209. $xr = $xl + $this->img->width - 1;
  2210. $yb = $yt + $this->img->height;
  2211. if( $this->doshadow ) {
  2212. $xr -= $this->shadow_width;
  2213. $yb -= $this->shadow_width;
  2214. }
  2215. if( $this->doframe ) {
  2216. $yt += $this->frame_weight;
  2217. $yb -= $this->frame_weight;
  2218. $xl += $this->frame_weight;
  2219. $xr -= $this->frame_weight;
  2220. }
  2221. $aa = $this->img->SetAngle(0);
  2222. $grad->FilledRectangle($xl,$yt,$xr,$yb,$this->bkg_gradfrom,$this->bkg_gradto,$this->bkg_gradtype);
  2223. $aa = $this->img->SetAngle($aa);
  2224. }
  2225. }
  2226. function StrokeFrameBackground() {
  2227. if( $this->background_image != "" && $this->background_cflag != "" ) {
  2228. JpGraphError::RaiseL(25040);//('It is not possible to specify both a background image and a background country flag.');
  2229. }
  2230. if( $this->background_image != "" ) {
  2231. $bkgimg = $this->LoadBkgImage($this->background_image_format);
  2232. $this->img->_AdjBrightContrast($bkgimg,$this->background_image_bright,
  2233. $this->background_image_contr);
  2234. $this->img->_AdjSat($bkgimg,$this->background_image_sat);
  2235. }
  2236. elseif( $this->background_cflag != "" ) {
  2237. if( ! class_exists('FlagImages') ) {
  2238. JpGraphError::RaiseL(25041);//('In order to use Country flags as backgrounds you must include the "jpgraph_flags.php" file.');
  2239. }
  2240. $fobj = new FlagImages(FLAGSIZE4);
  2241. $dummy='';
  2242. $bkgimg = $fobj->GetImgByName($this->background_cflag,$dummy);
  2243. $this->background_image_mix = $this->background_cflag_mix;
  2244. $this->background_image_type = $this->background_cflag_type;
  2245. }
  2246. else {
  2247. return ;
  2248. }
  2249. $bw = ImageSX($bkgimg);
  2250. $bh = ImageSY($bkgimg);
  2251. // No matter what the angle is we always stroke the image and frame
  2252. // assuming it is 0 degree
  2253. $aa = $this->img->SetAngle(0);
  2254. switch( $this->background_image_type ) {
  2255. case BGIMG_FILLPLOT: // Resize to just fill the plotarea
  2256. $this->FillMarginArea();
  2257. $this->StrokeFrame();
  2258. $this->FillPlotArea();
  2259. $this->img->CopyMerge($bkgimg,
  2260. $this->img->left_margin,$this->img->top_margin,
  2261. 0,0,$this->img->plotwidth+1,$this->img->plotheight,
  2262. $bw,$bh,$this->background_image_mix);
  2263. break;
  2264. case BGIMG_FILLFRAME: // Fill the whole area from upper left corner, resize to just fit
  2265. $hadj=0; $vadj=0;
  2266. if( $this->doshadow ) {
  2267. $hadj = $this->shadow_width;
  2268. $vadj = $this->shadow_width;
  2269. }
  2270. $this->FillMarginArea();
  2271. $this->FillPlotArea();
  2272. $this->img->CopyMerge($bkgimg,0,0,0,0,$this->img->width-$hadj,$this->img->height-$vadj,
  2273. $bw,$bh,$this->background_image_mix);
  2274. $this->StrokeFrame();
  2275. break;
  2276. case BGIMG_COPY: // Just copy the image from left corner, no resizing
  2277. $this->FillMarginArea();
  2278. $this->FillPlotArea();
  2279. $this->img->CopyMerge($bkgimg,0,0,0,0,$bw,$bh,
  2280. $bw,$bh,$this->background_image_mix);
  2281. $this->StrokeFrame();
  2282. break;
  2283. case BGIMG_CENTER: // Center original image in the plot area
  2284. $this->FillMarginArea();
  2285. $this->FillPlotArea();
  2286. $centerx = round($this->img->plotwidth/2+$this->img->left_margin-$bw/2);
  2287. $centery = round($this->img->plotheight/2+$this->img->top_margin-$bh/2);
  2288. $this->img->CopyMerge($bkgimg,$centerx,$centery,0,0,$bw,$bh,
  2289. $bw,$bh,$this->background_image_mix);
  2290. $this->StrokeFrame();
  2291. break;
  2292. default:
  2293. JpGraphError::RaiseL(25042);//(" Unknown background image layout");
  2294. }
  2295. $this->img->SetAngle($aa);
  2296. }
  2297. // Private
  2298. // Draw a frame around the image
  2299. function StrokeFrame() {
  2300. if( !$this->doframe ) return;
  2301. if( $this->background_image_type <= 1 &&
  2302. ($this->bkg_gradtype < 0 || ($this->bkg_gradtype > 0 && $this->bkg_gradstyle==BGRAD_PLOT)) ) {
  2303. $c = $this->margin_color;
  2304. }
  2305. else {
  2306. $c = false;
  2307. }
  2308. if( $this->doshadow ) {
  2309. $this->img->SetColor($this->frame_color);
  2310. $this->img->ShadowRectangle(0,0,$this->img->width,$this->img->height,
  2311. $c,$this->shadow_width,$this->shadow_color);
  2312. }
  2313. elseif( $this->framebevel ) {
  2314. if( $c ) {
  2315. $this->img->SetColor($this->margin_color);
  2316. $this->img->FilledRectangle(0,0,$this->img->width-1,$this->img->height-1);
  2317. }
  2318. $this->img->Bevel(1,1,$this->img->width-2,$this->img->height-2,
  2319. $this->framebeveldepth,
  2320. $this->framebevelcolor1,$this->framebevelcolor2);
  2321. if( $this->framebevelborder ) {
  2322. $this->img->SetColor($this->framebevelbordercolor);
  2323. $this->img->Rectangle(0,0,$this->img->width-1,$this->img->height-1);
  2324. }
  2325. }
  2326. else {
  2327. $this->img->SetLineWeight($this->frame_weight);
  2328. if( $c ) {
  2329. $this->img->SetColor($this->margin_color);
  2330. $this->img->FilledRectangle(0,0,$this->img->width-1,$this->img->height-1);
  2331. }
  2332. $this->img->SetColor($this->frame_color);
  2333. $this->img->Rectangle(0,0,$this->img->width-1,$this->img->height-1);
  2334. }
  2335. }
  2336. function FillMarginArea() {
  2337. $hadj=0; $vadj=0;
  2338. if( $this->doshadow ) {
  2339. $hadj = $this->shadow_width;
  2340. $vadj = $this->shadow_width;
  2341. }
  2342. $this->img->SetColor($this->margin_color);
  2343. // $this->img->FilledRectangle(0,0,$this->img->width-1-$hadj,$this->img->height-1-$vadj);
  2344. $this->img->FilledRectangle(0,0,$this->img->width-1-$hadj,$this->img->top_margin);
  2345. $this->img->FilledRectangle(0,$this->img->top_margin,$this->img->left_margin,$this->img->height-1-$hadj);
  2346. $this->img->FilledRectangle($this->img->left_margin+1,
  2347. $this->img->height-$this->img->bottom_margin,
  2348. $this->img->width-1-$hadj,
  2349. $this->img->height-1-$hadj);
  2350. $this->img->FilledRectangle($this->img->width-$this->img->right_margin,
  2351. $this->img->top_margin+1,
  2352. $this->img->width-1-$hadj,
  2353. $this->img->height-$this->img->bottom_margin-1);
  2354. }
  2355. function FillPlotArea() {
  2356. $this->img->PushColor($this->plotarea_color);
  2357. $this->img->FilledRectangle($this->img->left_margin,
  2358. $this->img->top_margin,
  2359. $this->img->width-$this->img->right_margin,
  2360. $this->img->height-$this->img->bottom_margin);
  2361. $this->img->PopColor();
  2362. }
  2363. // Stroke the plot area with either a solid color or a background image
  2364. function StrokePlotArea() {
  2365. // Note: To be consistent we really should take a possible shadow
  2366. // into account. However, that causes some problem for the LinearScale class
  2367. // since in the current design it does not have any links to class Graph which
  2368. // means it has no way of compensating for the adjusted plotarea in case of a
  2369. // shadow. So, until I redesign LinearScale we can't compensate for this.
  2370. // So just set the two adjustment parameters to zero for now.
  2371. $boxadj = 0; //$this->doframe ? $this->frame_weight : 0 ;
  2372. $adj = 0; //$this->doshadow ? $this->shadow_width : 0 ;
  2373. if( $this->background_image != "" || $this->background_cflag != "" ) {
  2374. $this->StrokeFrameBackground();
  2375. }
  2376. else {
  2377. $aa = $this->img->SetAngle(0);
  2378. $this->StrokeFrame();
  2379. $aa = $this->img->SetAngle($aa);
  2380. $this->StrokeBackgroundGrad();
  2381. if( $this->bkg_gradtype < 0 ||
  2382. ($this->bkg_gradtype > 0 && $this->bkg_gradstyle==BGRAD_MARGIN) ) {
  2383. $this->FillPlotArea();
  2384. }
  2385. }
  2386. }
  2387. function StrokeIcons() {
  2388. $n = count($this->iIcons);
  2389. for( $i=0; $i < $n; ++$i ) {
  2390. $this->iIcons[$i]->StrokeWithScale($this->img,$this->xscale,$this->yscale);
  2391. }
  2392. }
  2393. function StrokePlotBox() {
  2394. // Should we draw a box around the plot area?
  2395. if( $this->boxed ) {
  2396. $this->img->SetLineWeight(1);
  2397. $this->img->SetLineStyle('solid');
  2398. $this->img->SetColor($this->box_color);
  2399. for($i=0; $i < $this->box_weight; ++$i ) {
  2400. $this->img->Rectangle(
  2401. $this->img->left_margin-$i,$this->img->top_margin-$i,
  2402. $this->img->width-$this->img->right_margin+$i,
  2403. $this->img->height-$this->img->bottom_margin+$i);
  2404. }
  2405. }
  2406. }
  2407. function SetTitleBackgroundFillStyle($aStyle,$aColor1='black',$aColor2='white') {
  2408. $this->titlebkg_fillstyle = $aStyle;
  2409. $this->titlebkg_scolor1 = $aColor1;
  2410. $this->titlebkg_scolor2 = $aColor2;
  2411. }
  2412. function SetTitleBackground($aBackColor='gray', $aStyle=TITLEBKG_STYLE1, $aFrameStyle=TITLEBKG_FRAME_NONE, $aFrameColor='black', $aFrameWeight=1, $aBevelHeight=3, $aEnable=true) {
  2413. $this->titlebackground = $aEnable;
  2414. $this->titlebackground_color = $aBackColor;
  2415. $this->titlebackground_style = $aStyle;
  2416. $this->titlebackground_framecolor = $aFrameColor;
  2417. $this->titlebackground_framestyle = $aFrameStyle;
  2418. $this->titlebackground_frameweight = $aFrameWeight;
  2419. $this->titlebackground_bevelheight = $aBevelHeight ;
  2420. }
  2421. function StrokeTitles() {
  2422. $margin=3;
  2423. if( $this->titlebackground ) {
  2424. // Find out height
  2425. $this->title->margin += 2 ;
  2426. $h = $this->title->GetTextHeight($this->img)+$this->title->margin+$margin;
  2427. if( $this->subtitle->t != "" && !$this->subtitle->hide ) {
  2428. $h += $this->subtitle->GetTextHeight($this->img)+$margin+
  2429. $this->subtitle->margin;
  2430. $h += 2;
  2431. }
  2432. if( $this->subsubtitle->t != "" && !$this->subsubtitle->hide ) {
  2433. $h += $this->subsubtitle->GetTextHeight($this->img)+$margin+
  2434. $this->subsubtitle->margin;
  2435. $h += 2;
  2436. }
  2437. $this->img->PushColor($this->titlebackground_color);
  2438. if( $this->titlebackground_style === TITLEBKG_STYLE1 ) {
  2439. // Inside the frame
  2440. if( $this->framebevel ) {
  2441. $x1 = $y1 = $this->framebeveldepth + 1 ;
  2442. $x2 = $this->img->width - $this->framebeveldepth - 2 ;
  2443. $this->title->margin += $this->framebeveldepth + 1 ;
  2444. $h += $y1 ;
  2445. $h += 2;
  2446. }
  2447. else {
  2448. $x1 = $y1 = $this->frame_weight;
  2449. $x2 = $this->img->width - 2*$x1;
  2450. }
  2451. }
  2452. elseif( $this->titlebackground_style === TITLEBKG_STYLE2 ) {
  2453. // Cover the frame as well
  2454. $x1 = $y1 = 0;
  2455. $x2 = $this->img->width - 1 ;
  2456. }
  2457. elseif( $this->titlebackground_style === TITLEBKG_STYLE3 ) {
  2458. // Cover the frame as well (the difference is that
  2459. // for style==3 a bevel frame border is on top
  2460. // of the title background)
  2461. $x1 = $y1 = 0;
  2462. $x2 = $this->img->width - 1 ;
  2463. $h += $this->framebeveldepth ;
  2464. $this->title->margin += $this->framebeveldepth ;
  2465. }
  2466. else {
  2467. JpGraphError::RaiseL(25043);//('Unknown title background style.');
  2468. }
  2469. if( $this->titlebackground_framestyle === 3 ) {
  2470. $h += $this->titlebackground_bevelheight*2 + 1 ;
  2471. $this->title->margin += $this->titlebackground_bevelheight ;
  2472. }
  2473. if( $this->doshadow ) {
  2474. $x2 -= $this->shadow_width ;
  2475. }
  2476. $indent=0;
  2477. if( $this->titlebackground_framestyle == TITLEBKG_FRAME_BEVEL ) {
  2478. $ind = $this->titlebackground_bevelheight;
  2479. }
  2480. if( $this->titlebkg_fillstyle==TITLEBKG_FILLSTYLE_HSTRIPED ) {
  2481. $this->img->FilledRectangle2($x1+$ind,$y1+$ind,$x2-$ind,$h-$ind,
  2482. $this->titlebkg_scolor1,
  2483. $this->titlebkg_scolor2);
  2484. }
  2485. elseif( $this->titlebkg_fillstyle==TITLEBKG_FILLSTYLE_VSTRIPED ) {
  2486. $this->img->FilledRectangle2($x1+$ind,$y1+$ind,$x2-$ind,$h-$ind,
  2487. $this->titlebkg_scolor1,
  2488. $this->titlebkg_scolor2,2);
  2489. }
  2490. else {
  2491. // Solid fill
  2492. $this->img->FilledRectangle($x1,$y1,$x2,$h);
  2493. }
  2494. $this->img->PopColor();
  2495. $this->img->PushColor($this->titlebackground_framecolor);
  2496. $this->img->SetLineWeight($this->titlebackground_frameweight);
  2497. if( $this->titlebackground_framestyle == TITLEBKG_FRAME_FULL ) {
  2498. // Frame background
  2499. $this->img->Rectangle($x1,$y1,$x2,$h);
  2500. }
  2501. elseif( $this->titlebackground_framestyle == TITLEBKG_FRAME_BOTTOM ) {
  2502. // Bottom line only
  2503. $this->img->Line($x1,$h,$x2,$h);
  2504. }
  2505. elseif( $this->titlebackground_framestyle == TITLEBKG_FRAME_BEVEL ) {
  2506. $this->img->Bevel($x1,$y1,$x2,$h,$this->titlebackground_bevelheight);
  2507. }
  2508. $this->img->PopColor();
  2509. // This is clumsy. But we neeed to stroke the whole graph frame if it is
  2510. // set to bevel to get the bevel shading on top of the text background
  2511. if( $this->framebevel && $this->doframe &&
  2512. $this->titlebackground_style === 3 ) {
  2513. $this->img->Bevel(1,1,$this->img->width-2,$this->img->height-2,
  2514. $this->framebeveldepth,
  2515. $this->framebevelcolor1,$this->framebevelcolor2);
  2516. if( $this->framebevelborder ) {
  2517. $this->img->SetColor($this->framebevelbordercolor);
  2518. $this->img->Rectangle(0,0,$this->img->width-1,$this->img->height-1);
  2519. }
  2520. }
  2521. }
  2522. // Stroke title
  2523. $y = $this->title->margin;
  2524. if( $this->title->halign == 'center' )
  2525. $this->title->Center(0,$this->img->width,$y);
  2526. elseif( $this->title->halign == 'left' ) {
  2527. $this->title->SetPos($this->title->margin+2,$y);
  2528. }
  2529. elseif( $this->title->halign == 'right' ) {
  2530. $indent = 0;
  2531. if( $this->doshadow )
  2532. $indent = $this->shadow_width+2;
  2533. $this->title->SetPos($this->img->width-$this->title->margin-$indent,$y,'right');
  2534. }
  2535. $this->title->Stroke($this->img);
  2536. // ... and subtitle
  2537. $y += $this->title->GetTextHeight($this->img) + $margin + $this->subtitle->margin;
  2538. if( $this->subtitle->halign == 'center' )
  2539. $this->subtitle->Center(0,$this->img->width,$y);
  2540. elseif( $this->subtitle->halign == 'left' ) {
  2541. $this->subtitle->SetPos($this->subtitle->margin+2,$y);
  2542. }
  2543. elseif( $this->subtitle->halign == 'right' ) {
  2544. $indent = 0;
  2545. if( $this->doshadow )
  2546. $indent = $this->shadow_width+2;
  2547. $this->subtitle->SetPos($this->img->width-$this->subtitle->margin-$indent,$y,'right');
  2548. }
  2549. $this->subtitle->Stroke($this->img);
  2550. // ... and subsubtitle
  2551. $y += $this->subtitle->GetTextHeight($this->img) + $margin + $this->subsubtitle->margin;
  2552. if( $this->subsubtitle->halign == 'center' )
  2553. $this->subsubtitle->Center(0,$this->img->width,$y);
  2554. elseif( $this->subsubtitle->halign == 'left' ) {
  2555. $this->subsubtitle->SetPos($this->subsubtitle->margin+2,$y);
  2556. }
  2557. elseif( $this->subsubtitle->halign == 'right' ) {
  2558. $indent = 0;
  2559. if( $this->doshadow )
  2560. $indent = $this->shadow_width+2;
  2561. $this->subsubtitle->SetPos($this->img->width-$this->subsubtitle->margin-$indent,$y,'right');
  2562. }
  2563. $this->subsubtitle->Stroke($this->img);
  2564. // ... and fancy title
  2565. $this->tabtitle->Stroke($this->img);
  2566. }
  2567. function StrokeTexts() {
  2568. // Stroke any user added text objects
  2569. if( $this->texts != null ) {
  2570. for($i=0; $i < count($this->texts); ++$i) {
  2571. $this->texts[$i]->StrokeWithScale($this->img,$this->xscale,$this->yscale);
  2572. }
  2573. }
  2574. if( $this->y2texts != null && $this->y2scale != null ) {
  2575. for($i=0; $i < count($this->y2texts); ++$i) {
  2576. $this->y2texts[$i]->StrokeWithScale($this->img,$this->xscale,$this->y2scale);
  2577. }
  2578. }
  2579. }
  2580. function StrokeTables() {
  2581. if( $this->iTables != null ) {
  2582. $n = count($this->iTables);
  2583. for( $i=0; $i < $n; ++$i ) {
  2584. $this->iTables[$i]->StrokeWithScale($this->img,$this->xscale,$this->yscale);
  2585. }
  2586. }
  2587. }
  2588. function DisplayClientSideaImageMapAreas() {
  2589. // Debug stuff - display the outline of the image map areas
  2590. $csim='';
  2591. foreach ($this->plots as $p) {
  2592. $csim.= $p->GetCSIMareas();
  2593. }
  2594. $csim .= $this->legend->GetCSIMareas();
  2595. if (preg_match_all("/area shape=\"(\w+)\" coords=\"([0-9\, ]+)\"/", $csim, $coords)) {
  2596. $this->img->SetColor($this->csimcolor);
  2597. $n = count($coords[0]);
  2598. for ($i=0; $i < $n; $i++) {
  2599. if ($coords[1][$i]=="poly") {
  2600. preg_match_all('/\s*([0-9]+)\s*,\s*([0-9]+)\s*,*/',$coords[2][$i],$pts);
  2601. $this->img->SetStartPoint($pts[1][count($pts[0])-1],$pts[2][count($pts[0])-1]);
  2602. $m = count($pts[0]);
  2603. for ($j=0; $j < $m; $j++) {
  2604. $this->img->LineTo($pts[1][$j],$pts[2][$j]);
  2605. }
  2606. } else if ($coords[1][$i]=="rect") {
  2607. $pts = preg_split('/,/', $coords[2][$i]);
  2608. $this->img->SetStartPoint($pts[0],$pts[1]);
  2609. $this->img->LineTo($pts[2],$pts[1]);
  2610. $this->img->LineTo($pts[2],$pts[3]);
  2611. $this->img->LineTo($pts[0],$pts[3]);
  2612. $this->img->LineTo($pts[0],$pts[1]);
  2613. }
  2614. }
  2615. }
  2616. }
  2617. function AdjustSaturationBrightnessContrast() {
  2618. // Adjust the brightness and contrast of the image
  2619. if( $this->image_contr || $this->image_bright )
  2620. $this->img->AdjBrightContrast($this->image_bright,$this->image_contr);
  2621. if( $this->image_sat )
  2622. $this->img->AdjSat($this->image_sat);
  2623. }
  2624. // Text scale offset in fractions of a major scale step
  2625. function SetTextScaleOff($aOff) {
  2626. $this->text_scale_off = $aOff;
  2627. $this->xscale->text_scale_off = $aOff;
  2628. }
  2629. // Text width of bar to be centered in absolute pixels
  2630. function SetTextScaleAbsCenterOff($aOff) {
  2631. $this->text_scale_abscenteroff = $aOff;
  2632. }
  2633. // Get Y min and max values for added lines
  2634. function GetLinesYMinMax( $aLines ) {
  2635. $n = count($aLines);
  2636. if( $n == 0 ) return false;
  2637. $min = $aLines[0]->scaleposition ;
  2638. $max = $min ;
  2639. $flg = false;
  2640. for( $i=0; $i < $n; ++$i ) {
  2641. if( $aLines[$i]->direction == HORIZONTAL ) {
  2642. $flg = true ;
  2643. $v = $aLines[$i]->scaleposition ;
  2644. if( $min > $v ) $min = $v ;
  2645. if( $max < $v ) $max = $v ;
  2646. }
  2647. }
  2648. return $flg ? array($min,$max) : false ;
  2649. }
  2650. // Get X min and max values for added lines
  2651. function GetLinesXMinMax( $aLines ) {
  2652. $n = count($aLines);
  2653. if( $n == 0 ) return false ;
  2654. $min = $aLines[0]->scaleposition ;
  2655. $max = $min ;
  2656. $flg = false;
  2657. for( $i=0; $i < $n; ++$i ) {
  2658. if( $aLines[$i]->direction == VERTICAL ) {
  2659. $flg = true ;
  2660. $v = $aLines[$i]->scaleposition ;
  2661. if( $min > $v ) $min = $v ;
  2662. if( $max < $v ) $max = $v ;
  2663. }
  2664. }
  2665. return $flg ? array($min,$max) : false ;
  2666. }
  2667. // Get min and max values for all included plots
  2668. function GetPlotsYMinMax(&$aPlots) {
  2669. $n = count($aPlots);
  2670. $i=0;
  2671. do {
  2672. list($xmax,$max) = $aPlots[$i]->Max();
  2673. } while( ++$i < $n && !is_numeric($max) );
  2674. $i=0;
  2675. do {
  2676. list($xmin,$min) = $aPlots[$i]->Min();
  2677. } while( ++$i < $n && !is_numeric($min) );
  2678. if( !is_numeric($min) || !is_numeric($max) ) {
  2679. JpGraphError::RaiseL(25044);//('Cannot use autoscaling since it is impossible to determine a valid min/max value of the Y-axis (only null values).');
  2680. }
  2681. list($xmax,$max) = $aPlots[0]->Max();
  2682. list($xmin,$min) = $aPlots[0]->Min();
  2683. for($i=0; $i < count($aPlots); ++$i ) {
  2684. list($xmax,$ymax)=$aPlots[$i]->Max();
  2685. list($xmin,$ymin)=$aPlots[$i]->Min();
  2686. if (is_numeric($ymax)) $max=max($max,$ymax);
  2687. if (is_numeric($ymin)) $min=min($min,$ymin);
  2688. }
  2689. if( $min == '' ) $min = 0;
  2690. if( $max == '' ) $max = 0;
  2691. if( $min == 0 && $max == 0 ) {
  2692. // Special case if all values are 0
  2693. $min=0;$max=1;
  2694. }
  2695. return array($min,$max);
  2696. }
  2697. } // Class
  2698. //===================================================
  2699. // CLASS TTF
  2700. // Description: Handle TTF font names
  2701. //===================================================
  2702. class TTF {
  2703. var $font_files,$style_names;
  2704. //---------------
  2705. // CONSTRUCTOR
  2706. function TTF() {
  2707. $this->style_names=array(FS_NORMAL=>'normal',FS_BOLD=>'bold',FS_ITALIC=>'italic',FS_BOLDITALIC=>'bolditalic');
  2708. // File names for available fonts
  2709. $this->font_files=array(
  2710. FF_COURIER => array(FS_NORMAL=>'cour.ttf', FS_BOLD=>'courbd.ttf', FS_ITALIC=>'couri.ttf', FS_BOLDITALIC=>'courbi.ttf' ),
  2711. FF_GEORGIA => array(FS_NORMAL=>'georgia.ttf', FS_BOLD=>'georgiab.ttf', FS_ITALIC=>'georgiai.ttf', FS_BOLDITALIC=>'' ),
  2712. FF_TREBUCHE =>array(FS_NORMAL=>'trebuc.ttf', FS_BOLD=>'trebucbd.ttf', FS_ITALIC=>'trebucit.ttf', FS_BOLDITALIC=>'trebucbi.ttf' ),
  2713. FF_VERDANA => array(FS_NORMAL=>'verdana.ttf', FS_BOLD=>'verdanab.ttf', FS_ITALIC=>'verdanai.ttf', FS_BOLDITALIC=>'' ),
  2714. FF_TIMES => array(FS_NORMAL=>'times.ttf', FS_BOLD=>'timesbd.ttf', FS_ITALIC=>'timesi.ttf', FS_BOLDITALIC=>'timesbi.ttf' ),
  2715. FF_COMIC => array(FS_NORMAL=>'comic.ttf', FS_BOLD=>'comicbd.ttf', FS_ITALIC=>'', FS_BOLDITALIC=>'' ),
  2716. FF_ARIAL => array(FS_NORMAL=>'arial.ttf', FS_BOLD=>'arialbd.ttf', FS_ITALIC=>'ariali.ttf', FS_BOLDITALIC=>'arialbi.ttf' ) ,
  2717. FF_VERA => array(FS_NORMAL=>'Vera.ttf', FS_BOLD=>'VeraBd.ttf', FS_ITALIC=>'VeraIt.ttf', FS_BOLDITALIC=>'VeraBI.ttf' ),
  2718. FF_VERAMONO => array(FS_NORMAL=>'VeraMono.ttf', FS_BOLD=>'VeraMoBd.ttf', FS_ITALIC=>'VeraMoIt.ttf', FS_BOLDITALIC=>'VeraMoBI.ttf' ),
  2719. FF_VERASERIF => array(FS_NORMAL=>'VeraSe.ttf', FS_BOLD=>'VeraSeBd.ttf', FS_ITALIC=>'', FS_BOLDITALIC=>'' ) ,
  2720. FF_SIMSUN => array(FS_NORMAL=>'simsun.ttc', FS_BOLD=>'simhei.ttf', FS_ITALIC=>'', FS_BOLDITALIC=>'' ),
  2721. FF_CHINESE => array(FS_NORMAL=>CHINESE_TTF_FONT, FS_BOLD=>'', FS_ITALIC=>'', FS_BOLDITALIC=>'' ),
  2722. FF_MINCHO => array(FS_NORMAL=>MINCHO_TTF_FONT, FS_BOLD=>'', FS_ITALIC=>'', FS_BOLDITALIC=>'' ),
  2723. FF_PMINCHO => array(FS_NORMAL=>PMINCHO_TTF_FONT, FS_BOLD=>'', FS_ITALIC=>'', FS_BOLDITALIC=>'' ),
  2724. FF_GOTHIC => array(FS_NORMAL=>GOTHIC_TTF_FONT, FS_BOLD=>'', FS_ITALIC=>'', FS_BOLDITALIC=>'' ),
  2725. FF_PGOTHIC => array(FS_NORMAL=>PGOTHIC_TTF_FONT, FS_BOLD=>'', FS_ITALIC=>'', FS_BOLDITALIC=>'' ),
  2726. FF_MINCHO => array(FS_NORMAL=>PMINCHO_TTF_FONT, FS_BOLD=>'', FS_ITALIC=>'', FS_BOLDITALIC=>'' ),
  2727. FF_CUSTOM => array(FS_NORMAL=>CUSTOM_TTF_FONT, FS_BOLD=>CUSTOM_TTF_FONT_BOLD, FS_ITALIC=>CUSTOM_TTF_FONT_ITALIC, FS_BOLDITALIC=>CUSTOM_TTF_FONT_BOLDITALIC)
  2728. );
  2729. }
  2730. //---------------
  2731. // PUBLIC METHODS
  2732. // Create the TTF file from the font specification
  2733. function File($family,$style=FS_NORMAL) {
  2734. if( $family == FF_HANDWRT || $family==FF_BOOK ) {
  2735. JpGraphError::RaiseL(25045);//('Font families FF_HANDWRT and FF_BOOK are no longer available due to copyright problem with these fonts. Fonts can no longer be distributed with JpGraph. Please download fonts from http://corefonts.sourceforge.net/');
  2736. }
  2737. $fam = @$this->font_files[$family];
  2738. if( !$fam ) {
  2739. JpGraphError::RaiseL(25046,$family);//("Specified TTF font family (id=$family) is unknown or does not exist. Please note that TTF fonts are not distributed with JpGraph for copyright reasons. You can find the MS TTF WEB-fonts (arial, courier etc) for download at http://corefonts.sourceforge.net/");
  2740. }
  2741. $f = @$fam[$style];
  2742. if( $f==='' )
  2743. JpGraphError::RaiseL(25047,$this->style_names[$style],$this->font_files[$family][FS_NORMAL]);//('Style "'.$this->style_names[$style].'" is not available for font family '.$this->font_files[$family][FS_NORMAL].'.');
  2744. if( !$f ) {
  2745. JpGraphError::RaiseL(25048,$fam);//("Unknown font style specification [$fam].");
  2746. }
  2747. if ($family >= FF_MINCHO && $family <= FF_PGOTHIC) {
  2748. $f = MBTTF_DIR.$f;
  2749. } else {
  2750. $f = TTF_DIR.$f;
  2751. }
  2752. if( file_exists($f) === false || is_readable($f) === false ) {
  2753. JpGraphError::RaiseL(25049,$f);//("Font file \"$f\" is not readable or does not exist.");
  2754. }
  2755. return $f;
  2756. }
  2757. } // Class
  2758. //===================================================
  2759. // CLASS LineProperty
  2760. // Description: Holds properties for a line
  2761. //===================================================
  2762. class LineProperty {
  2763. var $iWeight=1, $iColor="black",$iStyle="solid";
  2764. var $iShow=true;
  2765. //---------------
  2766. // PUBLIC METHODS
  2767. function SetColor($aColor) {
  2768. $this->iColor = $aColor;
  2769. }
  2770. function SetWeight($aWeight) {
  2771. $this->iWeight = $aWeight;
  2772. }
  2773. function SetStyle($aStyle) {
  2774. $this->iStyle = $aStyle;
  2775. }
  2776. function Show($aShow=true) {
  2777. $this->iShow=$aShow;
  2778. }
  2779. function Stroke(&$aImg,$aX1,$aY1,$aX2,$aY2) {
  2780. if( $this->iShow ) {
  2781. $aImg->PushColor($this->iColor);
  2782. $oldls = $aImg->line_style;
  2783. $oldlw = $aImg->line_weight;
  2784. $aImg->SetLineWeight($this->iWeight);
  2785. $aImg->SetLineStyle($this->iStyle);
  2786. $aImg->StyleLine($aX1,$aY1,$aX2,$aY2);
  2787. $aImg->PopColor($this->iColor);
  2788. $aImg->line_style = $oldls;
  2789. $aImg->line_weight = $oldlw;
  2790. }
  2791. }
  2792. }
  2793. //===================================================
  2794. // CLASS Text
  2795. // Description: Arbitrary text object that can be added to the graph
  2796. //===================================================
  2797. class Text {
  2798. var $t,$x=0,$y=0,$halign="left",$valign="top",$color=array(0,0,0);
  2799. var $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
  2800. var $hide=false, $dir=0;
  2801. var $boxed=false; // Should the text be boxed
  2802. var $paragraph_align="left";
  2803. var $margin=0;
  2804. var $icornerradius=0,$ishadowwidth=3;
  2805. var $iScalePosY=null,$iScalePosX=null;
  2806. var $iWordwrap=0;
  2807. var $fcolor='white',$bcolor='black',$shadow=false;
  2808. var $iCSIMarea='',$iCSIMalt='',$iCSIMtarget='';
  2809. //---------------
  2810. // CONSTRUCTOR
  2811. // Create new text at absolute pixel coordinates
  2812. function Text($aTxt="",$aXAbsPos=0,$aYAbsPos=0) {
  2813. if( ! is_string($aTxt) ) {
  2814. JpGraphError::RaiseL(25050);//('First argument to Text::Text() must be s atring.');
  2815. }
  2816. $this->t = $aTxt;
  2817. $this->x = round($aXAbsPos);
  2818. $this->y = round($aYAbsPos);
  2819. $this->margin = 0;
  2820. }
  2821. //---------------
  2822. // PUBLIC METHODS
  2823. // Set the string in the text object
  2824. function Set($aTxt) {
  2825. $this->t = $aTxt;
  2826. }
  2827. // Alias for Pos()
  2828. function SetPos($aXAbsPos=0,$aYAbsPos=0,$aHAlign="left",$aVAlign="top") {
  2829. $this->Pos($aXAbsPos,$aYAbsPos,$aHAlign,$aVAlign);
  2830. }
  2831. // Specify the position and alignment for the text object
  2832. function Pos($aXAbsPos=0,$aYAbsPos=0,$aHAlign="left",$aVAlign="top") {
  2833. $this->x = $aXAbsPos;
  2834. $this->y = $aYAbsPos;
  2835. $this->halign = $aHAlign;
  2836. $this->valign = $aVAlign;
  2837. }
  2838. function SetScalePos($aX,$aY) {
  2839. $this->iScalePosX = $aX;
  2840. $this->iScalePosY = $aY;
  2841. }
  2842. // Specify alignment for the text
  2843. function Align($aHAlign,$aVAlign="top",$aParagraphAlign="") {
  2844. $this->halign = $aHAlign;
  2845. $this->valign = $aVAlign;
  2846. if( $aParagraphAlign != "" )
  2847. $this->paragraph_align = $aParagraphAlign;
  2848. }
  2849. // Alias
  2850. function SetAlign($aHAlign,$aVAlign="top",$aParagraphAlign="") {
  2851. $this->Align($aHAlign,$aVAlign,$aParagraphAlign);
  2852. }
  2853. // Specifies the alignment for a multi line text
  2854. function ParagraphAlign($aAlign) {
  2855. $this->paragraph_align = $aAlign;
  2856. }
  2857. // Specifies the alignment for a multi line text
  2858. function SetParagraphAlign($aAlign) {
  2859. $this->paragraph_align = $aAlign;
  2860. }
  2861. function SetShadow($aShadowColor='gray',$aShadowWidth=3) {
  2862. $this->ishadowwidth=$aShadowWidth;
  2863. $this->shadow=$aShadowColor;
  2864. $this->boxed=true;
  2865. }
  2866. function SetWordWrap($aCol) {
  2867. $this->iWordwrap = $aCol ;
  2868. }
  2869. // Specify that the text should be boxed. fcolor=frame color, bcolor=border color,
  2870. // $shadow=drop shadow should be added around the text.
  2871. function SetBox($aFrameColor=array(255,255,255),$aBorderColor=array(0,0,0),$aShadowColor=false,$aCornerRadius=4,$aShadowWidth=3) {
  2872. if( $aFrameColor==false )
  2873. $this->boxed=false;
  2874. else
  2875. $this->boxed=true;
  2876. $this->fcolor=$aFrameColor;
  2877. $this->bcolor=$aBorderColor;
  2878. // For backwards compatibility when shadow was just true or false
  2879. if( $aShadowColor === true )
  2880. $aShadowColor = 'gray';
  2881. $this->shadow=$aShadowColor;
  2882. $this->icornerradius=$aCornerRadius;
  2883. $this->ishadowwidth=$aShadowWidth;
  2884. }
  2885. // Hide the text
  2886. function Hide($aHide=true) {
  2887. $this->hide=$aHide;
  2888. }
  2889. // This looks ugly since it's not a very orthogonal design
  2890. // but I added this "inverse" of Hide() to harmonize
  2891. // with some classes which I designed more recently (especially)
  2892. // jpgraph_gantt
  2893. function Show($aShow=true) {
  2894. $this->hide=!$aShow;
  2895. }
  2896. // Specify font
  2897. function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
  2898. $this->font_family=$aFamily;
  2899. $this->font_style=$aStyle;
  2900. $this->font_size=$aSize;
  2901. }
  2902. // Center the text between $left and $right coordinates
  2903. function Center($aLeft,$aRight,$aYAbsPos=false) {
  2904. $this->x = $aLeft + ($aRight-$aLeft )/2;
  2905. $this->halign = "center";
  2906. if( is_numeric($aYAbsPos) )
  2907. $this->y = $aYAbsPos;
  2908. }
  2909. // Set text color
  2910. function SetColor($aColor) {
  2911. $this->color = $aColor;
  2912. }
  2913. function SetAngle($aAngle) {
  2914. $this->SetOrientation($aAngle);
  2915. }
  2916. // Orientation of text. Note only TTF fonts can have an arbitrary angle
  2917. function SetOrientation($aDirection=0) {
  2918. if( is_numeric($aDirection) )
  2919. $this->dir=$aDirection;
  2920. elseif( $aDirection=="h" )
  2921. $this->dir = 0;
  2922. elseif( $aDirection=="v" )
  2923. $this->dir = 90;
  2924. else JpGraphError::RaiseL(25051);//(" Invalid direction specified for text.");
  2925. }
  2926. // Total width of text
  2927. function GetWidth(&$aImg) {
  2928. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  2929. $w = $aImg->GetTextWidth($this->t,$this->dir);
  2930. return $w;
  2931. }
  2932. // Hight of font
  2933. function GetFontHeight(&$aImg) {
  2934. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  2935. $h = $aImg->GetFontHeight();
  2936. return $h;
  2937. }
  2938. function GetTextHeight(&$aImg) {
  2939. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  2940. $h = $aImg->GetTextHeight($this->t,$this->dir);
  2941. return $h;
  2942. }
  2943. function GetHeight(&$aImg) {
  2944. // Synonym for GetTextHeight()
  2945. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  2946. $h = $aImg->GetTextHeight($this->t,$this->dir);
  2947. return $h;
  2948. }
  2949. // Set the margin which will be interpretated differently depending
  2950. // on the context.
  2951. function SetMargin($aMarg) {
  2952. $this->margin = $aMarg;
  2953. }
  2954. function StrokeWithScale(&$aImg,$axscale,$ayscale) {
  2955. if( $this->iScalePosX === null ||
  2956. $this->iScalePosY === null ) {
  2957. $this->Stroke($aImg);
  2958. }
  2959. else {
  2960. $this->Stroke($aImg,
  2961. round($axscale->Translate($this->iScalePosX)),
  2962. round($ayscale->Translate($this->iScalePosY)));
  2963. }
  2964. }
  2965. function SetCSIMTarget($aTarget,$aAlt=null) {
  2966. $this->iCSIMtarget = $aTarget;
  2967. $this->iCSIMalt = $aAlt;
  2968. }
  2969. function GetCSIMareas() {
  2970. if( $this->iCSIMtarget !== '' )
  2971. return $this->iCSIMarea;
  2972. else
  2973. return '';
  2974. }
  2975. // Display text in image
  2976. function Stroke(&$aImg,$x=null,$y=null) {
  2977. if( !empty($x) ) $this->x = round($x);
  2978. if( !empty($y) ) $this->y = round($y);
  2979. // Insert newlines
  2980. if( $this->iWordwrap > 0 ) {
  2981. $this->t = wordwrap($this->t,$this->iWordwrap,"\n");
  2982. }
  2983. // If position been given as a fraction of the image size
  2984. // calculate the absolute position
  2985. if( $this->x < 1 && $this->x > 0 ) $this->x *= $aImg->width;
  2986. if( $this->y < 1 && $this->y > 0 ) $this->y *= $aImg->height;
  2987. $aImg->PushColor($this->color);
  2988. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  2989. $aImg->SetTextAlign($this->halign,$this->valign);
  2990. if( $this->boxed ) {
  2991. if( $this->fcolor=="nofill" )
  2992. $this->fcolor=false;
  2993. $aImg->SetLineWeight(1);
  2994. $bbox = $aImg->StrokeBoxedText($this->x,$this->y,$this->t,
  2995. $this->dir,$this->fcolor,$this->bcolor,$this->shadow,
  2996. $this->paragraph_align,5,5,$this->icornerradius,
  2997. $this->ishadowwidth);
  2998. }
  2999. else {
  3000. $bbox = $aImg->StrokeText($this->x,$this->y,$this->t,$this->dir,$this->paragraph_align);
  3001. }
  3002. // Create CSIM targets
  3003. $coords = $bbox[0].','.$bbox[1].','.$bbox[2].','.$bbox[3].','.$bbox[4].','.$bbox[5].','.$bbox[6].','.$bbox[7];
  3004. $this->iCSIMarea = "<area shape=\"poly\" coords=\"$coords\" href=\"".$this->iCSIMtarget."\"";
  3005. $this->iCSIMarea .= " alt=\"".$this->iCSIMalt."\" title=\"".$this->iCSIMalt."\" />\n";
  3006. $aImg->PopColor($this->color);
  3007. }
  3008. } // Class
  3009. class GraphTabTitle extends Text{
  3010. var $corner = 6 , $posx = 7, $posy = 4;
  3011. var $color='darkred',$fillcolor='lightyellow',$bordercolor='black';
  3012. var $align = 'left', $width=TABTITLE_WIDTHFIT;
  3013. function GraphTabTitle() {
  3014. $this->t = '';
  3015. $this->font_style = FS_BOLD;
  3016. $this->hide = true;
  3017. }
  3018. function SetColor($aTxtColor,$aFillColor='lightyellow',$aBorderColor='black') {
  3019. $this->color = $aTxtColor;
  3020. $this->fillcolor = $aFillColor;
  3021. $this->bordercolor = $aBorderColor;
  3022. }
  3023. function SetFillColor($aFillColor) {
  3024. $this->fillcolor = $aFillColor;
  3025. }
  3026. function SetTabAlign($aAlign) {
  3027. // Synonym for SetPos
  3028. $this->align = $aAlign;
  3029. }
  3030. function SetPos($aAlign) {
  3031. $this->align = $aAlign;
  3032. }
  3033. function SetWidth($aWidth) {
  3034. $this->width = $aWidth ;
  3035. }
  3036. function Set($t) {
  3037. $this->t = $t;
  3038. $this->hide = false;
  3039. }
  3040. function SetCorner($aD) {
  3041. $this->corner = $aD ;
  3042. }
  3043. function Stroke(&$aImg) {
  3044. if( $this->hide )
  3045. return;
  3046. $this->boxed = false;
  3047. $w = $this->GetWidth($aImg) + 2*$this->posx;
  3048. $h = $this->GetTextHeight($aImg) + 2*$this->posy;
  3049. $x = $aImg->left_margin;
  3050. $y = $aImg->top_margin;
  3051. if( $this->width === TABTITLE_WIDTHFIT ) {
  3052. if( $this->align == 'left' ) {
  3053. $p = array($x, $y,
  3054. $x, $y-$h+$this->corner,
  3055. $x + $this->corner,$y-$h,
  3056. $x + $w - $this->corner, $y-$h,
  3057. $x + $w, $y-$h+$this->corner,
  3058. $x + $w, $y);
  3059. }
  3060. elseif( $this->align == 'center' ) {
  3061. $x += round($aImg->plotwidth/2) - round($w/2);
  3062. $p = array($x, $y,
  3063. $x, $y-$h+$this->corner,
  3064. $x + $this->corner, $y-$h,
  3065. $x + $w - $this->corner, $y-$h,
  3066. $x + $w, $y-$h+$this->corner,
  3067. $x + $w, $y);
  3068. }
  3069. else {
  3070. $x += $aImg->plotwidth -$w;
  3071. $p = array($x, $y,
  3072. $x, $y-$h+$this->corner,
  3073. $x + $this->corner,$y-$h,
  3074. $x + $w - $this->corner, $y-$h,
  3075. $x + $w, $y-$h+$this->corner,
  3076. $x + $w, $y);
  3077. }
  3078. }
  3079. else {
  3080. if( $this->width === TABTITLE_WIDTHFULL )
  3081. $w = $aImg->plotwidth ;
  3082. else
  3083. $w = $this->width ;
  3084. // Make the tab fit the width of the plot area
  3085. $p = array($x, $y,
  3086. $x, $y-$h+$this->corner,
  3087. $x + $this->corner,$y-$h,
  3088. $x + $w - $this->corner, $y-$h,
  3089. $x + $w, $y-$h+$this->corner,
  3090. $x + $w, $y);
  3091. }
  3092. if( $this->halign == 'left' ) {
  3093. $aImg->SetTextAlign('left','bottom');
  3094. $x += $this->posx;
  3095. $y -= $this->posy;
  3096. }
  3097. elseif( $this->halign == 'center' ) {
  3098. $aImg->SetTextAlign('center','bottom');
  3099. $x += $w/2;
  3100. $y -= $this->posy;
  3101. }
  3102. else {
  3103. $aImg->SetTextAlign('right','bottom');
  3104. $x += $w - $this->posx;
  3105. $y -= $this->posy;
  3106. }
  3107. $aImg->SetColor($this->fillcolor);
  3108. $aImg->FilledPolygon($p);
  3109. $aImg->SetColor($this->bordercolor);
  3110. $aImg->Polygon($p,true);
  3111. $aImg->SetColor($this->color);
  3112. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  3113. $aImg->StrokeText($x,$y,$this->t,0,'center');
  3114. }
  3115. }
  3116. //===================================================
  3117. // CLASS SuperScriptText
  3118. // Description: Format a superscript text
  3119. //===================================================
  3120. class SuperScriptText extends Text {
  3121. var $iSuper="";
  3122. var $sfont_family="",$sfont_style="",$sfont_size=8;
  3123. var $iSuperMargin=2,$iVertOverlap=4,$iSuperScale=0.65;
  3124. var $iSDir=0;
  3125. var $iSimple=false;
  3126. function SuperScriptText($aTxt="",$aSuper="",$aXAbsPos=0,$aYAbsPos=0) {
  3127. parent::Text($aTxt,$aXAbsPos,$aYAbsPos);
  3128. $this->iSuper = $aSuper;
  3129. }
  3130. function FromReal($aVal,$aPrecision=2) {
  3131. // Convert a floating point number to scientific notation
  3132. $neg=1.0;
  3133. if( $aVal < 0 ) {
  3134. $neg = -1.0;
  3135. $aVal = -$aVal;
  3136. }
  3137. $l = floor(log10($aVal));
  3138. $a = sprintf("%0.".$aPrecision."f",round($aVal / pow(10,$l),$aPrecision));
  3139. $a *= $neg;
  3140. if( $this->iSimple && ($a == 1 || $a==-1) ) $a = '';
  3141. if( $a != '' )
  3142. $this->t = $a.' * 10';
  3143. else {
  3144. if( $neg == 1 )
  3145. $this->t = '10';
  3146. else
  3147. $this->t = '-10';
  3148. }
  3149. $this->iSuper = $l;
  3150. }
  3151. function Set($aTxt,$aSuper="") {
  3152. $this->t = $aTxt;
  3153. $this->iSuper = $aSuper;
  3154. }
  3155. function SetSuperFont($aFontFam,$aFontStyle=FS_NORMAL,$aFontSize=8) {
  3156. $this->sfont_family = $aFontFam;
  3157. $this->sfont_style = $aFontStyle;
  3158. $this->sfont_size = $aFontSize;
  3159. }
  3160. // Total width of text
  3161. function GetWidth(&$aImg) {
  3162. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  3163. $w = $aImg->GetTextWidth($this->t);
  3164. $aImg->SetFont($this->sfont_family,$this->sfont_style,$this->sfont_size);
  3165. $w += $aImg->GetTextWidth($this->iSuper);
  3166. $w += $this->iSuperMargin;
  3167. return $w;
  3168. }
  3169. // Hight of font (approximate the height of the text)
  3170. function GetFontHeight(&$aImg) {
  3171. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  3172. $h = $aImg->GetFontHeight();
  3173. $aImg->SetFont($this->sfont_family,$this->sfont_style,$this->sfont_size);
  3174. $h += $aImg->GetFontHeight();
  3175. return $h;
  3176. }
  3177. // Hight of text
  3178. function GetTextHeight(&$aImg) {
  3179. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  3180. $h = $aImg->GetTextHeight($this->t);
  3181. $aImg->SetFont($this->sfont_family,$this->sfont_style,$this->sfont_size);
  3182. $h += $aImg->GetTextHeight($this->iSuper);
  3183. return $h;
  3184. }
  3185. function Stroke(&$aImg,$ax=-1,$ay=-1) {
  3186. // To position the super script correctly we need different
  3187. // cases to handle the alignmewnt specified since that will
  3188. // determine how we can interpret the x,y coordinates
  3189. $w = parent::GetWidth($aImg);
  3190. $h = parent::GetTextHeight($aImg);
  3191. switch( $this->valign ) {
  3192. case 'top':
  3193. $sy = $this->y;
  3194. break;
  3195. case 'center':
  3196. $sy = $this->y - $h/2;
  3197. break;
  3198. case 'bottom':
  3199. $sy = $this->y - $h;
  3200. break;
  3201. default:
  3202. JpGraphError::RaiseL(25052);//('PANIC: Internal error in SuperScript::Stroke(). Unknown vertical alignment for text');
  3203. exit();
  3204. }
  3205. switch( $this->halign ) {
  3206. case 'left':
  3207. $sx = $this->x + $w;
  3208. break;
  3209. case 'center':
  3210. $sx = $this->x + $w/2;
  3211. break;
  3212. case 'right':
  3213. $sx = $this->x;
  3214. break;
  3215. default:
  3216. JpGraphError::RaiseL(25053);//('PANIC: Internal error in SuperScript::Stroke(). Unknown horizontal alignment for text');
  3217. exit();
  3218. }
  3219. $sx += $this->iSuperMargin;
  3220. $sy += $this->iVertOverlap;
  3221. // Should we automatically determine the font or
  3222. // has the user specified it explicetly?
  3223. if( $this->sfont_family == "" ) {
  3224. if( $this->font_family <= FF_FONT2 ) {
  3225. if( $this->font_family == FF_FONT0 ) {
  3226. $sff = FF_FONT0;
  3227. }
  3228. elseif( $this->font_family == FF_FONT1 ) {
  3229. if( $this->font_style == FS_NORMAL )
  3230. $sff = FF_FONT0;
  3231. else
  3232. $sff = FF_FONT1;
  3233. }
  3234. else {
  3235. $sff = FF_FONT1;
  3236. }
  3237. $sfs = $this->font_style;
  3238. $sfz = $this->font_size;
  3239. }
  3240. else {
  3241. // TTF fonts
  3242. $sff = $this->font_family;
  3243. $sfs = $this->font_style;
  3244. $sfz = floor($this->font_size*$this->iSuperScale);
  3245. if( $sfz < 8 ) $sfz = 8;
  3246. }
  3247. $this->sfont_family = $sff;
  3248. $this->sfont_style = $sfs;
  3249. $this->sfont_size = $sfz;
  3250. }
  3251. else {
  3252. $sff = $this->sfont_family;
  3253. $sfs = $this->sfont_style;
  3254. $sfz = $this->sfont_size;
  3255. }
  3256. parent::Stroke($aImg,$ax,$ay);
  3257. // For the builtin fonts we need to reduce the margins
  3258. // since the bounding bx reported for the builtin fonts
  3259. // are much larger than for the TTF fonts.
  3260. if( $sff <= FF_FONT2 ) {
  3261. $sx -= 2;
  3262. $sy += 3;
  3263. }
  3264. $aImg->SetTextAlign('left','bottom');
  3265. $aImg->SetFont($sff,$sfs,$sfz);
  3266. $aImg->PushColor($this->color);
  3267. $aImg->StrokeText($sx,$sy,$this->iSuper,$this->iSDir,'left');
  3268. $aImg->PopColor();
  3269. }
  3270. }
  3271. //===================================================
  3272. // CLASS Grid
  3273. // Description: responsible for drawing grid lines in graph
  3274. //===================================================
  3275. class Grid {
  3276. var $img;
  3277. var $scale;
  3278. var $grid_color='#DDDDDD',$grid_mincolor='#DDDDDD';
  3279. var $type="solid";
  3280. var $show=false, $showMinor=false,$weight=1;
  3281. var $fill=false,$fillcolor=array('#EFEFEF','#BBCCFF');
  3282. //---------------
  3283. // CONSTRUCTOR
  3284. function Grid(&$aAxis) {
  3285. $this->scale = &$aAxis->scale;
  3286. $this->img = &$aAxis->img;
  3287. }
  3288. //---------------
  3289. // PUBLIC METHODS
  3290. function SetColor($aMajColor,$aMinColor=false) {
  3291. $this->grid_color=$aMajColor;
  3292. if( $aMinColor === false )
  3293. $aMinColor = $aMajColor ;
  3294. $this->grid_mincolor = $aMinColor;
  3295. }
  3296. function SetWeight($aWeight) {
  3297. $this->weight=$aWeight;
  3298. }
  3299. // Specify if grid should be dashed, dotted or solid
  3300. function SetLineStyle($aType) {
  3301. $this->type = $aType;
  3302. }
  3303. // Decide if both major and minor grid should be displayed
  3304. function Show($aShowMajor=true,$aShowMinor=false) {
  3305. $this->show=$aShowMajor;
  3306. $this->showMinor=$aShowMinor;
  3307. }
  3308. function SetFill($aFlg=true,$aColor1='lightgray',$aColor2='lightblue') {
  3309. $this->fill = $aFlg;
  3310. $this->fillcolor = array( $aColor1, $aColor2 );
  3311. }
  3312. // Display the grid
  3313. function Stroke() {
  3314. if( $this->showMinor ) {
  3315. $tmp = $this->grid_color;
  3316. $this->grid_color = $this->grid_mincolor;
  3317. $this->DoStroke($this->scale->ticks->ticks_pos);
  3318. $this->grid_color = $tmp;
  3319. $this->DoStroke($this->scale->ticks->maj_ticks_pos);
  3320. }
  3321. else {
  3322. $this->DoStroke($this->scale->ticks->maj_ticks_pos);
  3323. }
  3324. }
  3325. //--------------
  3326. // Private methods
  3327. // Draw the grid
  3328. function DoStroke(&$aTicksPos) {
  3329. if( !$this->show )
  3330. return;
  3331. $nbrgrids = count($aTicksPos);
  3332. if( $this->scale->type=="y" ) {
  3333. $xl=$this->img->left_margin;
  3334. $xr=$this->img->width-$this->img->right_margin;
  3335. if( $this->fill ) {
  3336. // Draw filled areas
  3337. $y2 = $aTicksPos[0];
  3338. $i=1;
  3339. while( $i < $nbrgrids ) {
  3340. $y1 = $y2;
  3341. $y2 = $aTicksPos[$i++];
  3342. $this->img->SetColor($this->fillcolor[$i & 1]);
  3343. $this->img->FilledRectangle($xl,$y1,$xr,$y2);
  3344. }
  3345. }
  3346. $this->img->SetColor($this->grid_color);
  3347. $this->img->SetLineWeight($this->weight);
  3348. // Draw grid lines
  3349. for($i=0; $i<$nbrgrids; ++$i) {
  3350. $y=$aTicksPos[$i];
  3351. if( $this->type == "solid" )
  3352. $this->img->Line($xl,$y,$xr,$y);
  3353. elseif( $this->type == "dotted" )
  3354. $this->img->DashedLine($xl,$y,$xr,$y,1,6);
  3355. elseif( $this->type == "dashed" )
  3356. $this->img->DashedLine($xl,$y,$xr,$y,2,4);
  3357. elseif( $this->type == "longdashed" )
  3358. $this->img->DashedLine($xl,$y,$xr,$y,8,6);
  3359. }
  3360. }
  3361. elseif( $this->scale->type=="x" ) {
  3362. $yu=$this->img->top_margin;
  3363. $yl=$this->img->height-$this->img->bottom_margin;
  3364. $limit=$this->img->width-$this->img->right_margin;
  3365. if( $this->fill ) {
  3366. // Draw filled areas
  3367. $x2 = $aTicksPos[0];
  3368. $i=1;
  3369. while( $i < $nbrgrids ) {
  3370. $x1 = $x2;
  3371. $x2 = min($aTicksPos[$i++],$limit) ;
  3372. $this->img->SetColor($this->fillcolor[$i & 1]);
  3373. $this->img->FilledRectangle($x1,$yu,$x2,$yl);
  3374. }
  3375. }
  3376. $this->img->SetColor($this->grid_color);
  3377. $this->img->SetLineWeight($this->weight);
  3378. // We must also test for limit since we might have
  3379. // an offset and the number of ticks is calculated with
  3380. // assumption offset==0 so we might end up drawing one
  3381. // to many gridlines
  3382. $i=0;
  3383. $x=$aTicksPos[$i];
  3384. while( $i<count($aTicksPos) && ($x=$aTicksPos[$i]) <= $limit ) {
  3385. if( $this->type == "solid" )
  3386. $this->img->Line($x,$yl,$x,$yu);
  3387. elseif( $this->type == "dotted" )
  3388. $this->img->DashedLine($x,$yl,$x,$yu,1,6);
  3389. elseif( $this->type == "dashed" )
  3390. $this->img->DashedLine($x,$yl,$x,$yu,2,4);
  3391. elseif( $this->type == "longdashed" )
  3392. $this->img->DashedLine($x,$yl,$x,$yu,8,6);
  3393. ++$i;
  3394. }
  3395. }
  3396. else {
  3397. JpGraphError::RaiseL(25054,$this->scale->type);//('Internal error: Unknown grid axis ['.$this->scale->type.']');
  3398. }
  3399. return true;
  3400. }
  3401. } // Class
  3402. //===================================================
  3403. // CLASS Axis
  3404. // Description: Defines X and Y axis. Notes that at the
  3405. // moment the code is not really good since the axis on
  3406. // several occasion must know wheter it's an X or Y axis.
  3407. // This was a design decision to make the code easier to
  3408. // follow.
  3409. //===================================================
  3410. class Axis {
  3411. var $pos = false;
  3412. var $weight=1;
  3413. var $color=array(0,0,0),$label_color=array(0,0,0);
  3414. var $img=null,$scale=null;
  3415. var $hide=false;
  3416. var $ticks_label=false, $ticks_label_colors=null;
  3417. var $show_first_label=true,$show_last_label=true;
  3418. var $label_step=1; // Used by a text axis to specify what multiple of major steps
  3419. // should be labeled.
  3420. var $tick_step=1;
  3421. var $labelPos=0; // Which side of the axis should the labels be?
  3422. var $title=null,$title_adjust,$title_margin,$title_side=SIDE_LEFT;
  3423. var $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12,$label_angle=0;
  3424. var $tick_label_margin=5;
  3425. var $label_halign = '',$label_valign = '', $label_para_align='left';
  3426. var $hide_line=false,$hide_labels=false;
  3427. var $iDeltaAbsPos=0;
  3428. //var $hide_zero_label=false;
  3429. //---------------
  3430. // CONSTRUCTOR
  3431. function Axis(&$img,&$aScale,$color=array(0,0,0)) {
  3432. $this->img = &$img;
  3433. $this->scale = &$aScale;
  3434. $this->color = $color;
  3435. $this->title=new Text("");
  3436. if( $aScale->type=="y" ) {
  3437. $this->title_margin = 25;
  3438. $this->title_adjust="middle";
  3439. $this->title->SetOrientation(90);
  3440. $this->tick_label_margin=7;
  3441. $this->labelPos=SIDE_LEFT;
  3442. //$this->SetLabelFormat('%.1f');
  3443. }
  3444. else {
  3445. $this->title_margin = 5;
  3446. $this->title_adjust="high";
  3447. $this->title->SetOrientation(0);
  3448. $this->tick_label_margin=5;
  3449. $this->labelPos=SIDE_DOWN;
  3450. $this->title_side=SIDE_DOWN;
  3451. //$this->SetLabelFormat('%.0f');
  3452. }
  3453. }
  3454. //---------------
  3455. // PUBLIC METHODS
  3456. function SetLabelFormat($aFormStr) {
  3457. $this->scale->ticks->SetLabelFormat($aFormStr);
  3458. }
  3459. function SetLabelFormatString($aFormStr,$aDate=false) {
  3460. $this->scale->ticks->SetLabelFormat($aFormStr,$aDate);
  3461. }
  3462. function SetLabelFormatCallback($aFuncName) {
  3463. $this->scale->ticks->SetFormatCallback($aFuncName);
  3464. }
  3465. function SetLabelAlign($aHAlign,$aVAlign="top",$aParagraphAlign='left') {
  3466. $this->label_halign = $aHAlign;
  3467. $this->label_valign = $aVAlign;
  3468. $this->label_para_align = $aParagraphAlign;
  3469. }
  3470. // Don't display the first label
  3471. function HideFirstTickLabel($aShow=false) {
  3472. $this->show_first_label=$aShow;
  3473. }
  3474. function HideLastTickLabel($aShow=false) {
  3475. $this->show_last_label=$aShow;
  3476. }
  3477. // Manually specify the major and (optional) minor tick position and labels
  3478. function SetTickPositions($aMajPos,$aMinPos=NULL,$aLabels=NULL) {
  3479. $this->scale->ticks->SetTickPositions($aMajPos,$aMinPos,$aLabels);
  3480. }
  3481. // Manually specify major tick positions and optional labels
  3482. function SetMajTickPositions($aMajPos,$aLabels=NULL) {
  3483. $this->scale->ticks->SetTickPositions($aMajPos,NULL,$aLabels);
  3484. }
  3485. // Hide minor or major tick marks
  3486. function HideTicks($aHideMinor=true,$aHideMajor=true) {
  3487. $this->scale->ticks->SupressMinorTickMarks($aHideMinor);
  3488. $this->scale->ticks->SupressTickMarks($aHideMajor);
  3489. }
  3490. // Hide zero label
  3491. function HideZeroLabel($aFlag=true) {
  3492. $this->scale->ticks->SupressZeroLabel();
  3493. //$this->hide_zero_label = $aFlag;
  3494. }
  3495. function HideFirstLastLabel() {
  3496. // The two first calls to ticks method will supress
  3497. // automatically generated scale values. However, that
  3498. // will not affect manually specified value, e.g text-scales.
  3499. // therefor we also make a kludge here to supress manually
  3500. // specified scale labels.
  3501. $this->scale->ticks->SupressLast();
  3502. $this->scale->ticks->SupressFirst();
  3503. $this->show_first_label = false;
  3504. $this->show_last_label = false;
  3505. }
  3506. // Hide the axis
  3507. function Hide($aHide=true) {
  3508. $this->hide=$aHide;
  3509. }
  3510. // Hide the actual axis-line, but still print the labels
  3511. function HideLine($aHide=true) {
  3512. $this->hide_line = $aHide;
  3513. }
  3514. function HideLabels($aHide=true) {
  3515. $this->hide_labels = $aHide;
  3516. }
  3517. // Weight of axis
  3518. function SetWeight($aWeight) {
  3519. $this->weight = $aWeight;
  3520. }
  3521. // Axis color
  3522. function SetColor($aColor,$aLabelColor=false) {
  3523. $this->color = $aColor;
  3524. if( !$aLabelColor ) $this->label_color = $aColor;
  3525. else $this->label_color = $aLabelColor;
  3526. }
  3527. // Title on axis
  3528. function SetTitle($aTitle,$aAdjustAlign="high") {
  3529. $this->title->Set($aTitle);
  3530. $this->title_adjust=$aAdjustAlign;
  3531. }
  3532. // Specify distance from the axis
  3533. function SetTitleMargin($aMargin) {
  3534. $this->title_margin=$aMargin;
  3535. }
  3536. // Which side of the axis should the axis title be?
  3537. function SetTitleSide($aSideOfAxis) {
  3538. $this->title_side = $aSideOfAxis;
  3539. }
  3540. // Utility function to set the direction for tick marks
  3541. function SetTickDirection($aDir) {
  3542. // Will be deprecated from 1.7
  3543. if( ERR_DEPRECATED )
  3544. JpGraphError::RaiseL(25055);//('Axis::SetTickDirection() is deprecated. Use Axis::SetTickSide() instead');
  3545. $this->scale->ticks->SetSide($aDir);
  3546. }
  3547. function SetTickSide($aDir) {
  3548. $this->scale->ticks->SetSide($aDir);
  3549. }
  3550. // Specify text labels for the ticks. One label for each data point
  3551. function SetTickLabels($aLabelArray,$aLabelColorArray=null) {
  3552. $this->ticks_label = $aLabelArray;
  3553. $this->ticks_label_colors = $aLabelColorArray;
  3554. }
  3555. // How far from the axis should the labels be drawn
  3556. function SetTickLabelMargin($aMargin) {
  3557. if( ERR_DEPRECATED )
  3558. JpGraphError::RaiseL(25056);//('SetTickLabelMargin() is deprecated. Use Axis::SetLabelMargin() instead.');
  3559. $this->tick_label_margin=$aMargin;
  3560. }
  3561. function SetLabelMargin($aMargin) {
  3562. $this->tick_label_margin=$aMargin;
  3563. }
  3564. // Specify that every $step of the ticks should be displayed starting
  3565. // at $start
  3566. // DEPRECATED FUNCTION: USE SetTextTickInterval() INSTEAD
  3567. function SetTextTicks($step,$start=0) {
  3568. JpGraphError::RaiseL(25057);//(" SetTextTicks() is deprecated. Use SetTextTickInterval() instead.");
  3569. }
  3570. // Specify that every $step of the ticks should be displayed starting
  3571. // at $start
  3572. function SetTextTickInterval($aStep,$aStart=0) {
  3573. $this->scale->ticks->SetTextLabelStart($aStart);
  3574. $this->tick_step=$aStep;
  3575. }
  3576. // Specify that every $step tick mark should have a label
  3577. // should be displayed starting
  3578. function SetTextLabelInterval($aStep,$aStart=0) {
  3579. if( $aStep < 1 )
  3580. JpGraphError::RaiseL(25058);//(" Text label interval must be specified >= 1.");
  3581. $this->scale->ticks->SetTextLabelStart($aStart);
  3582. $this->label_step=$aStep;
  3583. }
  3584. // Which side of the axis should the labels be on?
  3585. function SetLabelPos($aSidePos) {
  3586. // This will be deprecated from 1.7
  3587. if( ERR_DEPRECATED )
  3588. JpGraphError::RaiseL(25059);//('SetLabelPos() is deprecated. Use Axis::SetLabelSide() instead.');
  3589. $this->labelPos=$aSidePos;
  3590. }
  3591. function SetLabelSide($aSidePos) {
  3592. $this->labelPos=$aSidePos;
  3593. }
  3594. // Set the font
  3595. function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
  3596. $this->font_family = $aFamily;
  3597. $this->font_style = $aStyle;
  3598. $this->font_size = $aSize;
  3599. }
  3600. // Position for axis line on the "other" scale
  3601. function SetPos($aPosOnOtherScale) {
  3602. $this->pos=$aPosOnOtherScale;
  3603. }
  3604. // Set the position of the axis to be X-pixels delta to the right
  3605. // of the max X-position (used to position the multiple Y-axis)
  3606. function SetPosAbsDelta($aDelta) {
  3607. $this->iDeltaAbsPos=$aDelta;
  3608. }
  3609. // Specify the angle for the tick labels
  3610. function SetLabelAngle($aAngle) {
  3611. $this->label_angle = $aAngle;
  3612. }
  3613. // Stroke the axis.
  3614. function Stroke($aOtherAxisScale,$aStrokeLabels=true) {
  3615. if( $this->hide ) return;
  3616. if( is_numeric($this->pos) ) {
  3617. $pos=$aOtherAxisScale->Translate($this->pos);
  3618. }
  3619. else { // Default to minimum of other scale if pos not set
  3620. if( ($aOtherAxisScale->GetMinVal() >= 0 && $this->pos==false) || $this->pos=="min" ) {
  3621. $pos = $aOtherAxisScale->scale_abs[0];
  3622. }
  3623. elseif($this->pos == "max") {
  3624. $pos = $aOtherAxisScale->scale_abs[1];
  3625. }
  3626. else { // If negative set x-axis at 0
  3627. $this->pos=0;
  3628. $pos=$aOtherAxisScale->Translate(0);
  3629. }
  3630. }
  3631. $pos += $this->iDeltaAbsPos;
  3632. $this->img->SetLineWeight($this->weight);
  3633. $this->img->SetColor($this->color);
  3634. $this->img->SetFont($this->font_family,$this->font_style,$this->font_size);
  3635. if( $this->scale->type == "x" ) {
  3636. if( !$this->hide_line )
  3637. $this->img->FilledRectangle($this->img->left_margin,$pos,
  3638. $this->img->width-$this->img->right_margin,$pos+$this->weight-1);
  3639. if( $this->title_side == SIDE_DOWN ) {
  3640. $y = $pos + $this->img->GetFontHeight() + $this->title_margin + $this->title->margin;
  3641. $yalign = 'top';
  3642. }
  3643. else {
  3644. $y = $pos - $this->img->GetFontHeight() - $this->title_margin - $this->title->margin;
  3645. $yalign = 'bottom';
  3646. }
  3647. if( $this->title_adjust=="high" )
  3648. $this->title->Pos($this->img->width-$this->img->right_margin,$y,"right",$yalign);
  3649. elseif( $this->title_adjust=="middle" || $this->title_adjust=="center" )
  3650. $this->title->Pos(($this->img->width-$this->img->left_margin-$this->img->right_margin)/2+$this->img->left_margin,$y,"center",$yalign);
  3651. elseif($this->title_adjust=="low")
  3652. $this->title->Pos($this->img->left_margin,$y,"left",$yalign);
  3653. else {
  3654. JpGraphError::RaiseL(25060,$this->title_adjust);//('Unknown alignment specified for X-axis title. ('.$this->title_adjust.')');
  3655. }
  3656. }
  3657. elseif( $this->scale->type == "y" ) {
  3658. // Add line weight to the height of the axis since
  3659. // the x-axis could have a width>1 and we want the axis to fit nicely together.
  3660. if( !$this->hide_line )
  3661. $this->img->FilledRectangle($pos-$this->weight+1,$this->img->top_margin,
  3662. $pos,$this->img->height-$this->img->bottom_margin+$this->weight-1);
  3663. $x=$pos ;
  3664. if( $this->title_side == SIDE_LEFT ) {
  3665. $x -= $this->title_margin;
  3666. $x -= $this->title->margin;
  3667. $halign="right";
  3668. }
  3669. else {
  3670. $x += $this->title_margin;
  3671. $x += $this->title->margin;
  3672. $halign="left";
  3673. }
  3674. // If the user has manually specified an hor. align
  3675. // then we override the automatic settings with this
  3676. // specifed setting. Since default is 'left' we compare
  3677. // with that. (This means a manually set 'left' align
  3678. // will have no effect.)
  3679. if( $this->title->halign != 'left' )
  3680. $halign = $this->title->halign;
  3681. if( $this->title_adjust=="high" )
  3682. $this->title->Pos($x,$this->img->top_margin,$halign,"top");
  3683. elseif($this->title_adjust=="middle" || $this->title_adjust=="center")
  3684. $this->title->Pos($x,($this->img->height-$this->img->top_margin-$this->img->bottom_margin)/2+$this->img->top_margin,$halign,"center");
  3685. elseif($this->title_adjust=="low")
  3686. $this->title->Pos($x,$this->img->height-$this->img->bottom_margin,$halign,"bottom");
  3687. else
  3688. JpGraphError::RaiseL(25061,$this->title_adjust);//('Unknown alignment specified for Y-axis title. ('.$this->title_adjust.')');
  3689. }
  3690. $this->scale->ticks->Stroke($this->img,$this->scale,$pos);
  3691. if( $aStrokeLabels ) {
  3692. if( !$this->hide_labels )
  3693. $this->StrokeLabels($pos);
  3694. $this->title->Stroke($this->img);
  3695. }
  3696. }
  3697. //---------------
  3698. // PRIVATE METHODS
  3699. // Draw all the tick labels on major tick marks
  3700. function StrokeLabels($aPos,$aMinor=false,$aAbsLabel=false) {
  3701. $this->img->SetColor($this->label_color);
  3702. $this->img->SetFont($this->font_family,$this->font_style,$this->font_size);
  3703. $yoff=$this->img->GetFontHeight()/2;
  3704. // Only draw labels at major tick marks
  3705. $nbr = count($this->scale->ticks->maj_ticks_label);
  3706. // We have the option to not-display the very first mark
  3707. // (Usefull when the first label might interfere with another
  3708. // axis.)
  3709. $i = $this->show_first_label ? 0 : 1 ;
  3710. if( !$this->show_last_label ) --$nbr;
  3711. // Now run through all labels making sure we don't overshoot the end
  3712. // of the scale.
  3713. $ncolor=0;
  3714. if( isset($this->ticks_label_colors) )
  3715. $ncolor=count($this->ticks_label_colors);
  3716. while( $i<$nbr ) {
  3717. // $tpos holds the absolute text position for the label
  3718. $tpos=$this->scale->ticks->maj_ticklabels_pos[$i];
  3719. // Note. the $limit is only used for the x axis since we
  3720. // might otherwise overshoot if the scale has been centered
  3721. // This is due to us "loosing" the last tick mark if we center.
  3722. if( $this->scale->type=="x" && $tpos > $this->img->width-$this->img->right_margin+1 ) {
  3723. return;
  3724. }
  3725. // we only draw every $label_step label
  3726. if( ($i % $this->label_step)==0 ) {
  3727. // Set specific label color if specified
  3728. if( $ncolor > 0 )
  3729. $this->img->SetColor($this->ticks_label_colors[$i % $ncolor]);
  3730. // If the label has been specified use that and in other case
  3731. // just label the mark with the actual scale value
  3732. $m=$this->scale->ticks->GetMajor();
  3733. // ticks_label has an entry for each data point and is the array
  3734. // that holds the labels set by the user. If the user hasn't
  3735. // specified any values we use whats in the automatically asigned
  3736. // labels in the maj_ticks_label
  3737. if( isset($this->ticks_label[$i*$m]) )
  3738. $label=$this->ticks_label[$i*$m];
  3739. else {
  3740. if( $aAbsLabel )
  3741. $label=abs($this->scale->ticks->maj_ticks_label[$i]);
  3742. else
  3743. $label=$this->scale->ticks->maj_ticks_label[$i];
  3744. if( $this->scale->textscale && $this->scale->ticks->label_formfunc == '' ) {
  3745. ++$label;
  3746. }
  3747. }
  3748. //if( $this->hide_zero_label && $label==0.0 ) {
  3749. // ++$i;
  3750. // continue;
  3751. //}
  3752. if( $this->scale->type == "x" ) {
  3753. if( $this->labelPos == SIDE_DOWN ) {
  3754. if( $this->label_angle==0 || $this->label_angle==90 ) {
  3755. if( $this->label_halign=='' && $this->label_valign=='')
  3756. $this->img->SetTextAlign('center','top');
  3757. else
  3758. $this->img->SetTextAlign($this->label_halign,$this->label_valign);
  3759. }
  3760. else {
  3761. if( $this->label_halign=='' && $this->label_valign=='')
  3762. $this->img->SetTextAlign("right","top");
  3763. else
  3764. $this->img->SetTextAlign($this->label_halign,$this->label_valign);
  3765. }
  3766. $this->img->StrokeText($tpos,$aPos+$this->tick_label_margin,$label,
  3767. $this->label_angle,$this->label_para_align);
  3768. }
  3769. else {
  3770. if( $this->label_angle==0 || $this->label_angle==90 ) {
  3771. if( $this->label_halign=='' && $this->label_valign=='')
  3772. $this->img->SetTextAlign("center","bottom");
  3773. else
  3774. $this->img->SetTextAlign($this->label_halign,$this->label_valign);
  3775. }
  3776. else {
  3777. if( $this->label_halign=='' && $this->label_valign=='')
  3778. $this->img->SetTextAlign("right","bottom");
  3779. else
  3780. $this->img->SetTextAlign($this->label_halign,$this->label_valign);
  3781. }
  3782. $this->img->StrokeText($tpos,$aPos-$this->tick_label_margin,$label,
  3783. $this->label_angle,$this->label_para_align);
  3784. }
  3785. }
  3786. else {
  3787. // scale->type == "y"
  3788. //if( $this->label_angle!=0 )
  3789. //JpGraphError::Raise(" Labels at an angle are not supported on Y-axis");
  3790. if( $this->labelPos == SIDE_LEFT ) { // To the left of y-axis
  3791. if( $this->label_halign=='' && $this->label_valign=='')
  3792. $this->img->SetTextAlign("right","center");
  3793. else
  3794. $this->img->SetTextAlign($this->label_halign,$this->label_valign);
  3795. $this->img->StrokeText($aPos-$this->tick_label_margin,$tpos,$label,$this->label_angle,$this->label_para_align);
  3796. }
  3797. else { // To the right of the y-axis
  3798. if( $this->label_halign=='' && $this->label_valign=='')
  3799. $this->img->SetTextAlign("left","center");
  3800. else
  3801. $this->img->SetTextAlign($this->label_halign,$this->label_valign);
  3802. $this->img->StrokeText($aPos+$this->tick_label_margin,$tpos,$label,$this->label_angle,$this->label_para_align);
  3803. }
  3804. }
  3805. }
  3806. ++$i;
  3807. }
  3808. }
  3809. } // Class
  3810. //===================================================
  3811. // CLASS Ticks
  3812. // Description: Abstract base class for drawing linear and logarithmic
  3813. // tick marks on axis
  3814. //===================================================
  3815. class Ticks {
  3816. var $minor_abs_size=3, $major_abs_size=5;
  3817. var $direction=1; // Should ticks be in(=1) the plot area or outside (=-1)?
  3818. var $scale;
  3819. var $is_set=false;
  3820. var $precision;
  3821. var $supress_zerolabel=false,$supress_first=false;
  3822. var $supress_last=false,$supress_tickmarks=false,$supress_minor_tickmarks=false;
  3823. var $mincolor="",$majcolor="";
  3824. var $weight=1;
  3825. var $label_formatstr=''; // C-style format string to use for labels
  3826. var $label_formfunc='';
  3827. var $label_dateformatstr='';
  3828. var $label_usedateformat=FALSE;
  3829. //---------------
  3830. // CONSTRUCTOR
  3831. function Ticks(&$aScale) {
  3832. $this->scale=&$aScale;
  3833. $this->precision = -1;
  3834. }
  3835. //---------------
  3836. // PUBLIC METHODS
  3837. // Set format string for automatic labels
  3838. function SetLabelFormat($aFormatString,$aDate=FALSE) {
  3839. $this->label_formatstr=$aFormatString;
  3840. $this->label_usedateformat=$aDate;
  3841. }
  3842. function SetLabelDateFormat($aFormatString) {
  3843. $this->label_dateformatstr=$aFormatString;
  3844. }
  3845. function SetFormatCallback($aCallbackFuncName) {
  3846. $this->label_formfunc = $aCallbackFuncName;
  3847. }
  3848. // Don't display the first zero label
  3849. function SupressZeroLabel($aFlag=true) {
  3850. $this->supress_zerolabel=$aFlag;
  3851. }
  3852. // Don't display minor tick marks
  3853. function SupressMinorTickMarks($aHide=true) {
  3854. $this->supress_minor_tickmarks=$aHide;
  3855. }
  3856. // Don't display major tick marks
  3857. function SupressTickMarks($aHide=true) {
  3858. $this->supress_tickmarks=$aHide;
  3859. }
  3860. // Hide the first tick mark
  3861. function SupressFirst($aHide=true) {
  3862. $this->supress_first=$aHide;
  3863. }
  3864. // Hide the last tick mark
  3865. function SupressLast($aHide=true) {
  3866. $this->supress_last=$aHide;
  3867. }
  3868. // Size (in pixels) of minor tick marks
  3869. function GetMinTickAbsSize() {
  3870. return $this->minor_abs_size;
  3871. }
  3872. // Size (in pixels) of major tick marks
  3873. function GetMajTickAbsSize() {
  3874. return $this->major_abs_size;
  3875. }
  3876. function SetSize($aMajSize,$aMinSize=3) {
  3877. $this->major_abs_size = $aMajSize;
  3878. $this->minor_abs_size = $aMinSize;
  3879. }
  3880. // Have the ticks been specified
  3881. function IsSpecified() {
  3882. return $this->is_set;
  3883. }
  3884. // Set the distance between major and minor tick marks
  3885. function Set($aMaj,$aMin) {
  3886. // "Virtual method"
  3887. // Should be implemented by the concrete subclass
  3888. // if any action is wanted.
  3889. }
  3890. // Specify number of decimals in automatic labels
  3891. // Deprecated from 1.4. Use SetFormatString() instead
  3892. function SetPrecision($aPrecision) {
  3893. if( ERR_DEPRECATED )
  3894. JpGraphError::RaiseL(25063);//('Ticks::SetPrecision() is deprecated. Use Ticks::SetLabelFormat() (or Ticks::SetFormatCallback()) instead');
  3895. $this->precision=$aPrecision;
  3896. }
  3897. function SetSide($aSide) {
  3898. $this->direction=$aSide;
  3899. }
  3900. // Which side of the axis should the ticks be on
  3901. function SetDirection($aSide=SIDE_RIGHT) {
  3902. $this->direction=$aSide;
  3903. }
  3904. // Set colors for major and minor tick marks
  3905. function SetMarkColor($aMajorColor,$aMinorColor="") {
  3906. $this->SetColor($aMajorColor,$aMinorColor);
  3907. }
  3908. function SetColor($aMajorColor,$aMinorColor="") {
  3909. $this->majcolor=$aMajorColor;
  3910. // If not specified use same as major
  3911. if( $aMinorColor=="" )
  3912. $this->mincolor=$aMajorColor;
  3913. else
  3914. $this->mincolor=$aMinorColor;
  3915. }
  3916. function SetWeight($aWeight) {
  3917. $this->weight=$aWeight;
  3918. }
  3919. } // Class
  3920. //===================================================
  3921. // CLASS LinearTicks
  3922. // Description: Draw linear ticks on axis
  3923. //===================================================
  3924. class LinearTicks extends Ticks {
  3925. var $minor_step=1, $major_step=2;
  3926. var $xlabel_offset=0,$xtick_offset=0;
  3927. var $label_offset=0; // What offset should the displayed label have
  3928. // i.e should we display 0,1,2 or 1,2,3,4 or 2,3,4 etc
  3929. var $text_label_start=0;
  3930. var $iManualTickPos = NULL, $iManualMinTickPos = NULL, $iManualTickLabels = NULL;
  3931. var $maj_ticks_pos = array(), $maj_ticklabels_pos = array(),
  3932. $ticks_pos = array(), $maj_ticks_label = array();
  3933. //---------------
  3934. // CONSTRUCTOR
  3935. function LinearTicks() {
  3936. $this->precision = -1;
  3937. }
  3938. //---------------
  3939. // PUBLIC METHODS
  3940. // Return major step size in world coordinates
  3941. function GetMajor() {
  3942. return $this->major_step;
  3943. }
  3944. // Return minor step size in world coordinates
  3945. function GetMinor() {
  3946. return $this->minor_step;
  3947. }
  3948. // Set Minor and Major ticks (in world coordinates)
  3949. function Set($aMajStep,$aMinStep=false) {
  3950. if( $aMinStep==false )
  3951. $aMinStep=$aMajStep;
  3952. if( $aMajStep <= 0 || $aMinStep <= 0 ) {
  3953. JpGraphError::RaiseL(25064);
  3954. //(" Minor or major step size is 0. Check that you haven't got an accidental SetTextTicks(0) in your code. If this is not the case you might have stumbled upon a bug in JpGraph. Please report this and if possible include the data that caused the problem.");
  3955. }
  3956. $this->major_step=$aMajStep;
  3957. $this->minor_step=$aMinStep;
  3958. $this->is_set = true;
  3959. }
  3960. function SetMajTickPositions($aMajPos,$aLabels=NULL) {
  3961. $this->SetTickPositions($aMajPos,NULL,$aLabels);
  3962. }
  3963. function SetTickPositions($aMajPos,$aMinPos=NULL,$aLabels=NULL) {
  3964. if( !is_array($aMajPos) || ($aMinPos!==NULL && !is_array($aMinPos)) ) {
  3965. JpGraphError::RaiseL(25065);//('Tick positions must be specifued as an array()');
  3966. return;
  3967. }
  3968. $n=count($aMajPos);
  3969. if( is_array($aLabels) && (count($aLabels) != $n) ) {
  3970. JpGraphError::RaiseL(25066);//('When manually specifying tick positions and labels the number of labels must be the same as the number of specified ticks.');
  3971. return;
  3972. }
  3973. $this->iManualTickPos = $aMajPos;
  3974. $this->iManualMinTickPos = $aMinPos;
  3975. $this->iManualTickLabels = $aLabels;
  3976. }
  3977. // Specify all the tick positions manually and possible also the exact labels
  3978. function _doManualTickPos($aScale) {
  3979. $n=count($this->iManualTickPos);
  3980. $m=count($this->iManualMinTickPos);
  3981. $doLbl=count($this->iManualTickLabels) > 0;
  3982. $this->use_manualtickpos=true;
  3983. $this->maj_ticks_pos = array();
  3984. $this->maj_ticklabels_pos = array();
  3985. $this->ticks_pos = array();
  3986. // Now loop through the supplied positions and translate them to screen coordinates
  3987. // and store them in the maj_label_positions
  3988. $minScale = $aScale->scale[0];
  3989. $maxScale = $aScale->scale[1];
  3990. $j=0;
  3991. for($i=0; $i < $n ; ++$i ) {
  3992. // First make sure that the first tick is not lower than the lower scale value
  3993. if( !isset($this->iManualTickPos[$i]) ||
  3994. $this->iManualTickPos[$i] < $minScale || $this->iManualTickPos[$i] > $maxScale) {
  3995. continue;
  3996. }
  3997. $this->maj_ticks_pos[$j] = $aScale->Translate($this->iManualTickPos[$i]);
  3998. $this->maj_ticklabels_pos[$j] = $this->maj_ticks_pos[$j];
  3999. // Set the minor tick marks the same as major if not specified
  4000. if( $m <= 0 ) {
  4001. $this->ticks_pos[$j] = $this->maj_ticks_pos[$j];
  4002. }
  4003. if( $doLbl ) {
  4004. $this->maj_ticks_label[$j] = $this->iManualTickLabels[$i];
  4005. }
  4006. else {
  4007. $this->maj_ticks_label[$j]=$this->_doLabelFormat($this->iManualTickPos[$i],$i,$n);
  4008. }
  4009. ++$j;
  4010. }
  4011. // Some sanity check
  4012. if( count($this->maj_ticks_pos) < 2 ) {
  4013. JpGraphError::RaiseL(25067);//('Your manually specified scale and ticks is not correct. The scale seems to be too small to hold any of the specified tickl marks.');
  4014. }
  4015. // Setup the minor tick marks
  4016. $j=0;
  4017. for($i=0; $i < $m; ++$i ) {
  4018. if( empty($this->iManualMinTickPos[$i]) ||
  4019. $this->iManualMinTickPos[$i] < $minScale || $this->iManualMinTickPos[$i] > $maxScale)
  4020. continue;
  4021. $this->ticks_pos[$j] = $aScale->Translate($this->iManualMinTickPos[$i]);
  4022. ++$j;
  4023. }
  4024. }
  4025. function _doAutoTickPos($aScale) {
  4026. $maj_step_abs = $aScale->scale_factor*$this->major_step;
  4027. $min_step_abs = $aScale->scale_factor*$this->minor_step;
  4028. if( $min_step_abs==0 || $maj_step_abs==0 ) {
  4029. JpGraphError::RaiseL(25068);//("A plot has an illegal scale. This could for example be that you are trying to use text autoscaling to draw a line plot with only one point or that the plot area is too small. It could also be that no input data value is numeric (perhaps only '-' or 'x')");
  4030. }
  4031. // We need to make this an int since comparing it below
  4032. // with the result from round() can give wrong result, such that
  4033. // (40 < 40) == TRUE !!!
  4034. $limit = (int)$aScale->scale_abs[1];
  4035. if( $aScale->textscale ) {
  4036. // This can only be true for a X-scale (horizontal)
  4037. // Define ticks for a text scale. This is slightly different from a
  4038. // normal linear type of scale since the position might be adjusted
  4039. // and the labels start at on
  4040. $label = (float)$aScale->GetMinVal()+$this->text_label_start+$this->label_offset;
  4041. $start_abs=$aScale->scale_factor*$this->text_label_start;
  4042. $nbrmajticks=ceil(($aScale->GetMaxVal()-$aScale->GetMinVal()-$this->text_label_start )/$this->major_step)+1;
  4043. $x = $aScale->scale_abs[0]+$start_abs+$this->xlabel_offset*$min_step_abs;
  4044. for( $i=0; $label <= $aScale->GetMaxVal()+$this->label_offset; ++$i ) {
  4045. // Apply format to label
  4046. $this->maj_ticks_label[$i]=$this->_doLabelFormat($label,$i,$nbrmajticks);
  4047. $label+=$this->major_step;
  4048. // The x-position of the tick marks can be different from the labels.
  4049. // Note that we record the tick position (not the label) so that the grid
  4050. // happen upon tick marks and not labels.
  4051. $xtick=$aScale->scale_abs[0]+$start_abs+$this->xtick_offset*$min_step_abs+$i*$maj_step_abs;
  4052. $this->maj_ticks_pos[$i]=$xtick;
  4053. $this->maj_ticklabels_pos[$i] = round($x);
  4054. $x += $maj_step_abs;
  4055. }
  4056. }
  4057. else {
  4058. $label = $aScale->GetMinVal();
  4059. $abs_pos = $aScale->scale_abs[0];
  4060. $j=0; $i=0;
  4061. $step = round($maj_step_abs/$min_step_abs);
  4062. if( $aScale->type == "x" ) {
  4063. // For a normal linear type of scale the major ticks will always be multiples
  4064. // of the minor ticks. In order to avoid any rounding issues the major ticks are
  4065. // defined as every "step" minor ticks and not calculated separately
  4066. $nbrmajticks=ceil(($aScale->GetMaxVal()-$aScale->GetMinVal()-$this->text_label_start )/$this->major_step)+1;
  4067. while( round($abs_pos) <= $limit ) {
  4068. $this->ticks_pos[] = round($abs_pos);
  4069. $this->ticks_label[] = $label;
  4070. if( $i % $step == 0 && $j < $nbrmajticks ) {
  4071. $this->maj_ticks_pos[$j] = round($abs_pos);
  4072. $this->maj_ticklabels_pos[$j] = round($abs_pos);
  4073. $this->maj_ticks_label[$j]=$this->_doLabelFormat($label,$j,$nbrmajticks);
  4074. ++$j;
  4075. }
  4076. ++$i;
  4077. $abs_pos += $min_step_abs;
  4078. $label+=$this->minor_step;
  4079. }
  4080. }
  4081. elseif( $aScale->type == "y" ) {
  4082. $nbrmajticks=floor(($aScale->GetMaxVal()-$aScale->GetMinVal())/$this->major_step)+1;
  4083. while( round($abs_pos) >= $limit ) {
  4084. $this->ticks_pos[$i] = round($abs_pos);
  4085. $this->ticks_label[$i]=$label;
  4086. if( $i % $step == 0 && $j < $nbrmajticks) {
  4087. $this->maj_ticks_pos[$j] = round($abs_pos);
  4088. $this->maj_ticklabels_pos[$j] = round($abs_pos);
  4089. $this->maj_ticks_label[$j]=$this->_doLabelFormat($label,$j,$nbrmajticks);
  4090. ++$j;
  4091. }
  4092. ++$i;
  4093. $abs_pos += $min_step_abs;
  4094. $label += $this->minor_step;
  4095. }
  4096. }
  4097. }
  4098. }
  4099. function _doLabelFormat($aVal,$aIdx,$aNbrTicks) {
  4100. // If precision hasn't been specified set it to a sensible value
  4101. if( $this->precision==-1 ) {
  4102. $t = log10($this->minor_step);
  4103. if( $t > 0 )
  4104. $precision = 0;
  4105. else
  4106. $precision = -floor($t);
  4107. }
  4108. else
  4109. $precision = $this->precision;
  4110. if( $this->label_formfunc != '' ) {
  4111. $f=$this->label_formfunc;
  4112. $l = call_user_func($f,$aVal);
  4113. }
  4114. elseif( $this->label_formatstr != '' || $this->label_dateformatstr != '' ) {
  4115. if( $this->label_usedateformat ) {
  4116. $l = date($this->label_formatstr,$aVal);
  4117. }
  4118. else {
  4119. if( $this->label_dateformatstr !== '' )
  4120. $l = date($this->label_dateformatstr,$aVal);
  4121. else
  4122. $l = sprintf($this->label_formatstr,$aVal);
  4123. }
  4124. }
  4125. else {
  4126. $l = sprintf('%01.'.$precision.'f',round($aVal,$precision));
  4127. }
  4128. if( ($this->supress_zerolabel && $l==0) || ($this->supress_first && $aIdx==0) ||
  4129. ($this->supress_last && $aIdx==$aNbrTicks-1) ) {
  4130. $l='';
  4131. }
  4132. return $l;
  4133. }
  4134. // Stroke ticks on either X or Y axis
  4135. function _StrokeTicks(&$aImg,$aScale,$aPos) {
  4136. $hor = $aScale->type == 'x';
  4137. $aImg->SetLineWeight($this->weight);
  4138. // We need to make this an int since comparing it below
  4139. // with the result from round() can give wrong result, such that
  4140. // (40 < 40) == TRUE !!!
  4141. $limit = (int)$aScale->scale_abs[1];
  4142. // A text scale doesn't have any minor ticks
  4143. if( !$aScale->textscale ) {
  4144. // Stroke minor ticks
  4145. $yu = $aPos - $this->direction*$this->GetMinTickAbsSize();
  4146. $xr = $aPos + $this->direction*$this->GetMinTickAbsSize();
  4147. $n = count($this->ticks_pos);
  4148. for($i=0; $i < $n; ++$i ) {
  4149. if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
  4150. if( $this->mincolor!="" ) $aImg->PushColor($this->mincolor);
  4151. if( $hor ) {
  4152. //if( $this->ticks_pos[$i] <= $limit )
  4153. $aImg->Line($this->ticks_pos[$i],$aPos,$this->ticks_pos[$i],$yu);
  4154. }
  4155. else {
  4156. //if( $this->ticks_pos[$i] >= $limit )
  4157. $aImg->Line($aPos,$this->ticks_pos[$i],$xr,$this->ticks_pos[$i]);
  4158. }
  4159. if( $this->mincolor!="" ) $aImg->PopColor();
  4160. }
  4161. }
  4162. }
  4163. // Stroke major ticks
  4164. $yu = $aPos - $this->direction*$this->GetMajTickAbsSize();
  4165. $xr = $aPos + $this->direction*$this->GetMajTickAbsSize();
  4166. $nbrmajticks=ceil(($aScale->GetMaxVal()-$aScale->GetMinVal()-$this->text_label_start )/$this->major_step)+1;
  4167. $n = count($this->maj_ticks_pos);
  4168. for($i=0; $i < $n ; ++$i ) {
  4169. if(!($this->xtick_offset > 0 && $i==$nbrmajticks-1) && !$this->supress_tickmarks) {
  4170. if( $this->majcolor!="" ) $aImg->PushColor($this->majcolor);
  4171. if( $hor ) {
  4172. //if( $this->maj_ticks_pos[$i] <= $limit )
  4173. $aImg->Line($this->maj_ticks_pos[$i],$aPos,$this->maj_ticks_pos[$i],$yu);
  4174. }
  4175. else {
  4176. //if( $this->maj_ticks_pos[$i] >= $limit )
  4177. $aImg->Line($aPos,$this->maj_ticks_pos[$i],$xr,$this->maj_ticks_pos[$i]);
  4178. }
  4179. if( $this->majcolor!="" ) $aImg->PopColor();
  4180. }
  4181. }
  4182. }
  4183. // Draw linear ticks
  4184. function Stroke(&$aImg,$aScale,$aPos) {
  4185. if( $this->iManualTickPos != NULL )
  4186. $this->_doManualTickPos($aScale);
  4187. else
  4188. $this->_doAutoTickPos($aScale);
  4189. $this->_StrokeTicks($aImg,$aScale,$aPos, $aScale->type == 'x' );
  4190. }
  4191. //---------------
  4192. // PRIVATE METHODS
  4193. // Spoecify the offset of the displayed tick mark with the tick "space"
  4194. // Legal values for $o is [0,1] used to adjust where the tick marks and label
  4195. // should be positioned within the major tick-size
  4196. // $lo specifies the label offset and $to specifies the tick offset
  4197. // this comes in handy for example in bar graphs where we wont no offset for the
  4198. // tick but have the labels displayed halfway under the bars.
  4199. function SetXLabelOffset($aLabelOff,$aTickOff=-1) {
  4200. $this->xlabel_offset=$aLabelOff;
  4201. if( $aTickOff==-1 ) // Same as label offset
  4202. $this->xtick_offset=$aLabelOff;
  4203. else
  4204. $this->xtick_offset=$aTickOff;
  4205. if( $aLabelOff>0 )
  4206. $this->SupressLast(); // The last tick wont fit
  4207. }
  4208. // Which tick label should we start with?
  4209. function SetTextLabelStart($aTextLabelOff) {
  4210. $this->text_label_start=$aTextLabelOff;
  4211. }
  4212. } // Class
  4213. //===================================================
  4214. // CLASS LinearScale
  4215. // Description: Handle linear scaling between screen and world
  4216. //===================================================
  4217. class LinearScale {
  4218. var $scale=array(0,0);
  4219. var $scale_abs=array(0,0);
  4220. var $scale_factor; // Scale factor between world and screen
  4221. var $world_size; // Plot area size in world coordinates
  4222. var $world_abs_size; // Plot area size in pixels
  4223. var $off; // Offset between image edge and plot area
  4224. var $type; // is this x or y scale ?
  4225. var $ticks=null; // Store ticks
  4226. var $text_scale_off = 0;
  4227. var $autoscale_min=false; // Forced minimum value, auto determine max
  4228. var $autoscale_max=false; // Forced maximum value, auto determine min
  4229. var $gracetop=0,$gracebottom=0;
  4230. var $intscale=false; // Restrict autoscale to integers
  4231. var $textscale=false; // Just a flag to let the Plot class find out if
  4232. // we are a textscale or not. This is a cludge since
  4233. // this ionformatyion is availabale in Graph::axtype but
  4234. // we don't have access to the graph object in the Plots
  4235. // stroke method. So we let graph store the status here
  4236. // when the linear scale is created. A real cludge...
  4237. var $auto_ticks=false; // When using manual scale should the ticks be automatically set?
  4238. var $name = 'lin';
  4239. //---------------
  4240. // CONSTRUCTOR
  4241. function LinearScale($aMin=0,$aMax=0,$aType="y") {
  4242. assert($aType=="x" || $aType=="y" );
  4243. assert($aMin<=$aMax);
  4244. $this->type=$aType;
  4245. $this->scale=array($aMin,$aMax);
  4246. $this->world_size=$aMax-$aMin;
  4247. $this->ticks = new LinearTicks();
  4248. }
  4249. //---------------
  4250. // PUBLIC METHODS
  4251. // Check if scale is set or if we should autoscale
  4252. // We should do this is either scale or ticks has not been set
  4253. function IsSpecified() {
  4254. if( $this->GetMinVal()==$this->GetMaxVal() ) { // Scale not set
  4255. return false;
  4256. }
  4257. return true;
  4258. }
  4259. // Set the minimum data value when the autoscaling is used.
  4260. // Usefull if you want a fix minimum (like 0) but have an
  4261. // automatic maximum
  4262. function SetAutoMin($aMin) {
  4263. $this->autoscale_min=$aMin;
  4264. }
  4265. // Set the minimum data value when the autoscaling is used.
  4266. // Usefull if you want a fix minimum (like 0) but have an
  4267. // automatic maximum
  4268. function SetAutoMax($aMax) {
  4269. $this->autoscale_max=$aMax;
  4270. }
  4271. // If the user manually specifies a scale should the ticks
  4272. // still be set automatically?
  4273. function SetAutoTicks($aFlag=true) {
  4274. $this->auto_ticks = $aFlag;
  4275. }
  4276. // Specify scale "grace" value (top and bottom)
  4277. function SetGrace($aGraceTop,$aGraceBottom=0) {
  4278. if( $aGraceTop<0 || $aGraceBottom < 0 )
  4279. JpGraphError::RaiseL(25069);//(" Grace must be larger then 0");
  4280. $this->gracetop=$aGraceTop;
  4281. $this->gracebottom=$aGraceBottom;
  4282. }
  4283. // Get the minimum value in the scale
  4284. function GetMinVal() {
  4285. return $this->scale[0];
  4286. }
  4287. // get maximum value for scale
  4288. function GetMaxVal() {
  4289. return $this->scale[1];
  4290. }
  4291. // Specify a new min/max value for sclae
  4292. function Update(&$aImg,$aMin,$aMax) {
  4293. $this->scale=array($aMin,$aMax);
  4294. $this->world_size=$aMax-$aMin;
  4295. $this->InitConstants($aImg);
  4296. }
  4297. // Translate between world and screen
  4298. function Translate($aCoord) {
  4299. if( !is_numeric($aCoord) ) {
  4300. if( $aCoord != '' && $aCoord != '-' && $aCoord != 'x' )
  4301. JpGraphError::RaiseL(25070);//('Your data contains non-numeric values.');
  4302. return 0;
  4303. }
  4304. else {
  4305. return $this->off + ($aCoord - $this->scale[0])*$this->scale_factor;
  4306. }
  4307. }
  4308. // Relative translate (don't include offset) usefull when we just want
  4309. // to know the relative position (in pixels) on the axis
  4310. function RelTranslate($aCoord) {
  4311. if( !is_numeric($aCoord) ) {
  4312. if( $aCoord != '' && $aCoord != '-' && $aCoord != 'x' )
  4313. JpGraphError::RaiseL(25070);//('Your data contains non-numeric values.');
  4314. return 0;
  4315. }
  4316. else {
  4317. return ($aCoord - $this->scale[0]) * $this->scale_factor;
  4318. }
  4319. }
  4320. // Restrict autoscaling to only use integers
  4321. function SetIntScale($aIntScale=true) {
  4322. $this->intscale=$aIntScale;
  4323. }
  4324. // Calculate an integer autoscale
  4325. function IntAutoScale(&$img,$min,$max,$maxsteps,$majend=true) {
  4326. // Make sure limits are integers
  4327. $min=floor($min);
  4328. $max=ceil($max);
  4329. if( abs($min-$max)==0 ) {
  4330. --$min; ++$max;
  4331. }
  4332. $maxsteps = floor($maxsteps);
  4333. $gracetop=round(($this->gracetop/100.0)*abs($max-$min));
  4334. $gracebottom=round(($this->gracebottom/100.0)*abs($max-$min));
  4335. if( is_numeric($this->autoscale_min) ) {
  4336. $min = ceil($this->autoscale_min);
  4337. if( $min >= $max ) {
  4338. JpGraphError::RaiseL(25071);//('You have specified a min value with SetAutoMin() which is larger than the maximum value used for the scale. This is not possible.');
  4339. }
  4340. }
  4341. if( is_numeric($this->autoscale_max) ) {
  4342. $max = ceil($this->autoscale_max);
  4343. if( $min >= $max ) {
  4344. JpGraphError::RaiseL(25072);//('You have specified a max value with SetAutoMax() which is smaller than the miminum value used for the scale. This is not possible.');
  4345. }
  4346. }
  4347. if( abs($min-$max ) == 0 ) {
  4348. ++$max;
  4349. --$min;
  4350. }
  4351. $min -= $gracebottom;
  4352. $max += $gracetop;
  4353. // First get tickmarks as multiples of 1, 10, ...
  4354. if( $majend ) {
  4355. list($num1steps,$adj1min,$adj1max,$maj1step) =
  4356. $this->IntCalcTicks($maxsteps,$min,$max,1);
  4357. }
  4358. else {
  4359. $adj1min = $min;
  4360. $adj1max = $max;
  4361. list($num1steps,$maj1step) =
  4362. $this->IntCalcTicksFreeze($maxsteps,$min,$max,1);
  4363. }
  4364. if( abs($min-$max) > 2 ) {
  4365. // Then get tick marks as 2:s 2, 20, ...
  4366. if( $majend ) {
  4367. list($num2steps,$adj2min,$adj2max,$maj2step) =
  4368. $this->IntCalcTicks($maxsteps,$min,$max,5);
  4369. }
  4370. else {
  4371. $adj2min = $min;
  4372. $adj2max = $max;
  4373. list($num2steps,$maj2step) =
  4374. $this->IntCalcTicksFreeze($maxsteps,$min,$max,5);
  4375. }
  4376. }
  4377. else {
  4378. $num2steps = 10000; // Dummy high value so we don't choose this
  4379. }
  4380. if( abs($min-$max) > 5 ) {
  4381. // Then get tickmarks as 5:s 5, 50, 500, ...
  4382. if( $majend ) {
  4383. list($num5steps,$adj5min,$adj5max,$maj5step) =
  4384. $this->IntCalcTicks($maxsteps,$min,$max,2);
  4385. }
  4386. else {
  4387. $adj5min = $min;
  4388. $adj5max = $max;
  4389. list($num5steps,$maj5step) =
  4390. $this->IntCalcTicksFreeze($maxsteps,$min,$max,2);
  4391. }
  4392. }
  4393. else {
  4394. $num5steps = 10000; // Dummy high value so we don't choose this
  4395. }
  4396. // Check to see whichof 1:s, 2:s or 5:s fit better with
  4397. // the requested number of major ticks
  4398. $match1=abs($num1steps-$maxsteps);
  4399. $match2=abs($num2steps-$maxsteps);
  4400. if( !empty($maj5step) && $maj5step > 1 )
  4401. $match5=abs($num5steps-$maxsteps);
  4402. else
  4403. $match5=10000; // Dummy high value
  4404. // Compare these three values and see which is the closest match
  4405. // We use a 0.6 weight to gravitate towards multiple of 5:s
  4406. if( $match1 < $match2 ) {
  4407. if( $match1 < $match5 )
  4408. $r=1;
  4409. else
  4410. $r=3;
  4411. }
  4412. else {
  4413. if( $match2 < $match5 )
  4414. $r=2;
  4415. else
  4416. $r=3;
  4417. }
  4418. // Minsteps are always the same as maxsteps for integer scale
  4419. switch( $r ) {
  4420. case 1:
  4421. $this->ticks->Set($maj1step,$maj1step);
  4422. $this->Update($img,$adj1min,$adj1max);
  4423. break;
  4424. case 2:
  4425. $this->ticks->Set($maj2step,$maj2step);
  4426. $this->Update($img,$adj2min,$adj2max);
  4427. break;
  4428. case 3:
  4429. $this->ticks->Set($maj5step,$maj5step);
  4430. $this->Update($img,$adj5min,$adj5max);
  4431. break;
  4432. default:
  4433. JpGraphError::RaiseL(25073,$r);//('Internal error. Integer scale algorithm comparison out of bound (r=$r)');
  4434. }
  4435. }
  4436. // Calculate autoscale. Used if user hasn't given a scale and ticks
  4437. // $maxsteps is the maximum number of major tickmarks allowed.
  4438. function AutoScale(&$img,$min,$max,$maxsteps,$majend=true) {
  4439. if( $this->intscale ) {
  4440. $this->IntAutoScale($img,$min,$max,$maxsteps,$majend);
  4441. return;
  4442. }
  4443. if( abs($min-$max) < 0.00001 ) {
  4444. // We need some difference to be able to autoscale
  4445. // make it 5% above and 5% below value
  4446. if( $min==0 && $max==0 ) { // Special case
  4447. $min=-1; $max=1;
  4448. }
  4449. else {
  4450. $delta = (abs($max)+abs($min))*0.005;
  4451. $min -= $delta;
  4452. $max += $delta;
  4453. }
  4454. }
  4455. $gracetop=($this->gracetop/100.0)*abs($max-$min);
  4456. $gracebottom=($this->gracebottom/100.0)*abs($max-$min);
  4457. if( is_numeric($this->autoscale_min) ) {
  4458. $min = $this->autoscale_min;
  4459. if( $min >= $max ) {
  4460. JpGraphError::RaiseL(25071);//('You have specified a min value with SetAutoMin() which is larger than the maximum value used for the scale. This is not possible.');
  4461. }
  4462. if( abs($min-$max ) < 0.00001 )
  4463. $max *= 1.2;
  4464. }
  4465. if( is_numeric($this->autoscale_max) ) {
  4466. $max = $this->autoscale_max;
  4467. if( $min >= $max ) {
  4468. JpGraphError::RaiseL(25072);//('You have specified a max value with SetAutoMax() which is smaller than the miminum value used for the scale. This is not possible.');
  4469. }
  4470. if( abs($min-$max ) < 0.00001 )
  4471. $min *= 0.8;
  4472. }
  4473. $min -= $gracebottom;
  4474. $max += $gracetop;
  4475. // First get tickmarks as multiples of 0.1, 1, 10, ...
  4476. if( $majend ) {
  4477. list($num1steps,$adj1min,$adj1max,$min1step,$maj1step) =
  4478. $this->CalcTicks($maxsteps,$min,$max,1,2);
  4479. }
  4480. else {
  4481. $adj1min=$min;
  4482. $adj1max=$max;
  4483. list($num1steps,$min1step,$maj1step) =
  4484. $this->CalcTicksFreeze($maxsteps,$min,$max,1,2,false);
  4485. }
  4486. // Then get tick marks as 2:s 0.2, 2, 20, ...
  4487. if( $majend ) {
  4488. list($num2steps,$adj2min,$adj2max,$min2step,$maj2step) =
  4489. $this->CalcTicks($maxsteps,$min,$max,5,2);
  4490. }
  4491. else {
  4492. $adj2min=$min;
  4493. $adj2max=$max;
  4494. list($num2steps,$min2step,$maj2step) =
  4495. $this->CalcTicksFreeze($maxsteps,$min,$max,5,2,false);
  4496. }
  4497. // Then get tickmarks as 5:s 0.05, 0.5, 5, 50, ...
  4498. if( $majend ) {
  4499. list($num5steps,$adj5min,$adj5max,$min5step,$maj5step) =
  4500. $this->CalcTicks($maxsteps,$min,$max,2,5);
  4501. }
  4502. else {
  4503. $adj5min=$min;
  4504. $adj5max=$max;
  4505. list($num5steps,$min5step,$maj5step) =
  4506. $this->CalcTicksFreeze($maxsteps,$min,$max,2,5,false);
  4507. }
  4508. // Check to see whichof 1:s, 2:s or 5:s fit better with
  4509. // the requested number of major ticks
  4510. $match1=abs($num1steps-$maxsteps);
  4511. $match2=abs($num2steps-$maxsteps);
  4512. $match5=abs($num5steps-$maxsteps);
  4513. // Compare these three values and see which is the closest match
  4514. // We use a 0.8 weight to gravitate towards multiple of 5:s
  4515. $r=$this->MatchMin3($match1,$match2,$match5,0.8);
  4516. switch( $r ) {
  4517. case 1:
  4518. $this->Update($img,$adj1min,$adj1max);
  4519. $this->ticks->Set($maj1step,$min1step);
  4520. break;
  4521. case 2:
  4522. $this->Update($img,$adj2min,$adj2max);
  4523. $this->ticks->Set($maj2step,$min2step);
  4524. break;
  4525. case 3:
  4526. $this->Update($img,$adj5min,$adj5max);
  4527. $this->ticks->Set($maj5step,$min5step);
  4528. break;
  4529. }
  4530. }
  4531. //---------------
  4532. // PRIVATE METHODS
  4533. // This method recalculates all constants that are depending on the
  4534. // margins in the image. If the margins in the image are changed
  4535. // this method should be called for every scale that is registred with
  4536. // that image. Should really be installed as an observer of that image.
  4537. function InitConstants(&$img) {
  4538. if( $this->type=="x" ) {
  4539. $this->world_abs_size=$img->width - $img->left_margin - $img->right_margin;
  4540. $this->off=$img->left_margin;
  4541. $this->scale_factor = 0;
  4542. if( $this->world_size > 0 )
  4543. $this->scale_factor=$this->world_abs_size/($this->world_size*1.0);
  4544. }
  4545. else { // y scale
  4546. $this->world_abs_size=$img->height - $img->top_margin - $img->bottom_margin;
  4547. $this->off=$img->top_margin+$this->world_abs_size;
  4548. $this->scale_factor = 0;
  4549. if( $this->world_size > 0 ) {
  4550. $this->scale_factor=-$this->world_abs_size/($this->world_size*1.0);
  4551. }
  4552. }
  4553. $size = $this->world_size * $this->scale_factor;
  4554. $this->scale_abs=array($this->off,$this->off + $size);
  4555. }
  4556. // Initialize the conversion constants for this scale
  4557. // This tries to pre-calculate as much as possible to speed up the
  4558. // actual conversion (with Translate()) later on
  4559. // $start =scale start in absolute pixels (for x-scale this is an y-position
  4560. // and for an y-scale this is an x-position
  4561. // $len =absolute length in pixels of scale
  4562. function SetConstants($aStart,$aLen) {
  4563. $this->world_abs_size=$aLen;
  4564. $this->off=$aStart;
  4565. if( $this->world_size<=0 ) {
  4566. // This should never ever happen !!
  4567. JpGraphError::RaiseL(25074);
  4568. //("You have unfortunately stumbled upon a bug in JpGraph. It seems like the scale range is ".$this->world_size." [for ".$this->type." scale] <br> Please report Bug #01 to jpgraph@aditus.nu and include the script that gave this error. This problem could potentially be caused by trying to use \"illegal\" values in the input data arrays (like trying to send in strings or only NULL values) which causes the autoscaling to fail.");
  4569. }
  4570. // scale_factor = number of pixels per world unit
  4571. $this->scale_factor=$this->world_abs_size/($this->world_size*1.0);
  4572. // scale_abs = start and end points of scale in absolute pixels
  4573. $this->scale_abs=array($this->off,$this->off+$this->world_size*$this->scale_factor);
  4574. }
  4575. // Calculate number of ticks steps with a specific division
  4576. // $a is the divisor of 10**x to generate the first maj tick intervall
  4577. // $a=1, $b=2 give major ticks with multiple of 10, ...,0.1,1,10,...
  4578. // $a=5, $b=2 give major ticks with multiple of 2:s ...,0.2,2,20,...
  4579. // $a=2, $b=5 give major ticks with multiple of 5:s ...,0.5,5,50,...
  4580. // We return a vector of
  4581. // [$numsteps,$adjmin,$adjmax,$minstep,$majstep]
  4582. // If $majend==true then the first and last marks on the axis will be major
  4583. // labeled tick marks otherwise it will be adjusted to the closest min tick mark
  4584. function CalcTicks($maxsteps,$min,$max,$a,$b,$majend=true) {
  4585. $diff=$max-$min;
  4586. if( $diff==0 )
  4587. $ld=0;
  4588. else
  4589. $ld=floor(log10($diff));
  4590. // Gravitate min towards zero if we are close
  4591. if( $min>0 && $min < pow(10,$ld) ) $min=0;
  4592. //$majstep=pow(10,$ld-1)/$a;
  4593. $majstep=pow(10,$ld)/$a;
  4594. $minstep=$majstep/$b;
  4595. $adjmax=ceil($max/$minstep)*$minstep;
  4596. $adjmin=floor($min/$minstep)*$minstep;
  4597. $adjdiff = $adjmax-$adjmin;
  4598. $numsteps=$adjdiff/$majstep;
  4599. while( $numsteps>$maxsteps ) {
  4600. $majstep=pow(10,$ld)/$a;
  4601. $numsteps=$adjdiff/$majstep;
  4602. ++$ld;
  4603. }
  4604. $minstep=$majstep/$b;
  4605. $adjmin=floor($min/$minstep)*$minstep;
  4606. $adjdiff = $adjmax-$adjmin;
  4607. if( $majend ) {
  4608. $adjmin = floor($min/$majstep)*$majstep;
  4609. $adjdiff = $adjmax-$adjmin;
  4610. $adjmax = ceil($adjdiff/$majstep)*$majstep+$adjmin;
  4611. }
  4612. else
  4613. $adjmax=ceil($max/$minstep)*$minstep;
  4614. return array($numsteps,$adjmin,$adjmax,$minstep,$majstep);
  4615. }
  4616. function CalcTicksFreeze($maxsteps,$min,$max,$a,$b) {
  4617. // Same as CalcTicks but don't adjust min/max values
  4618. $diff=$max-$min;
  4619. if( $diff==0 )
  4620. $ld=0;
  4621. else
  4622. $ld=floor(log10($diff));
  4623. //$majstep=pow(10,$ld-1)/$a;
  4624. $majstep=pow(10,$ld)/$a;
  4625. $minstep=$majstep/$b;
  4626. $numsteps=floor($diff/$majstep);
  4627. while( $numsteps > $maxsteps ) {
  4628. $majstep=pow(10,$ld)/$a;
  4629. $numsteps=floor($diff/$majstep);
  4630. ++$ld;
  4631. }
  4632. $minstep=$majstep/$b;
  4633. return array($numsteps,$minstep,$majstep);
  4634. }
  4635. function IntCalcTicks($maxsteps,$min,$max,$a,$majend=true) {
  4636. $diff=$max-$min;
  4637. if( $diff==0 )
  4638. JpGraphError::RaiseL(25075);//('Can\'t automatically determine ticks since min==max.');
  4639. else
  4640. $ld=floor(log10($diff));
  4641. // Gravitate min towards zero if we are close
  4642. if( $min>0 && $min < pow(10,$ld) ) $min=0;
  4643. if( $ld == 0 ) $ld=1;
  4644. if( $a == 1 )
  4645. $majstep = 1;
  4646. else
  4647. $majstep=pow(10,$ld)/$a;
  4648. $adjmax=ceil($max/$majstep)*$majstep;
  4649. $adjmin=floor($min/$majstep)*$majstep;
  4650. $adjdiff = $adjmax-$adjmin;
  4651. $numsteps=$adjdiff/$majstep;
  4652. while( $numsteps>$maxsteps ) {
  4653. $majstep=pow(10,$ld)/$a;
  4654. $numsteps=$adjdiff/$majstep;
  4655. ++$ld;
  4656. }
  4657. $adjmin=floor($min/$majstep)*$majstep;
  4658. $adjdiff = $adjmax-$adjmin;
  4659. if( $majend ) {
  4660. $adjmin = floor($min/$majstep)*$majstep;
  4661. $adjdiff = $adjmax-$adjmin;
  4662. $adjmax = ceil($adjdiff/$majstep)*$majstep+$adjmin;
  4663. }
  4664. else
  4665. $adjmax=ceil($max/$majstep)*$majstep;
  4666. return array($numsteps,$adjmin,$adjmax,$majstep);
  4667. }
  4668. function IntCalcTicksFreeze($maxsteps,$min,$max,$a) {
  4669. // Same as IntCalcTick but don't change min/max values
  4670. $diff=$max-$min;
  4671. if( $diff==0 )
  4672. JpGraphError::RaiseL(25075);//('Can\'t automatically determine ticks since min==max.');
  4673. else
  4674. $ld=floor(log10($diff));
  4675. if( $ld == 0 ) $ld=1;
  4676. if( $a == 1 )
  4677. $majstep = 1;
  4678. else
  4679. $majstep=pow(10,$ld)/$a;
  4680. $numsteps=floor($diff/$majstep);
  4681. while( $numsteps > $maxsteps ) {
  4682. $majstep=pow(10,$ld)/$a;
  4683. $numsteps=floor($diff/$majstep);
  4684. ++$ld;
  4685. }
  4686. return array($numsteps,$majstep);
  4687. }
  4688. // Determine the minimum of three values witha weight for last value
  4689. function MatchMin3($a,$b,$c,$weight) {
  4690. if( $a < $b ) {
  4691. if( $a < ($c*$weight) )
  4692. return 1; // $a smallest
  4693. else
  4694. return 3; // $c smallest
  4695. }
  4696. elseif( $b < ($c*$weight) )
  4697. return 2; // $b smallest
  4698. return 3; // $c smallest
  4699. }
  4700. } // Class
  4701. //===================================================
  4702. // CLASS RGB
  4703. // Description: Color definitions as RGB triples
  4704. //===================================================
  4705. class RGB {
  4706. var $rgb_table;
  4707. var $img;
  4708. function RGB(&$aImg) {
  4709. $this->img = &$aImg;
  4710. // Conversion array between color names and RGB
  4711. $this->rgb_table = array(
  4712. "aqua"=> array(0,255,255),
  4713. "lime"=> array(0,255,0),
  4714. "teal"=> array(0,128,128),
  4715. "whitesmoke"=>array(245,245,245),
  4716. "gainsboro"=>array(220,220,220),
  4717. "oldlace"=>array(253,245,230),
  4718. "linen"=>array(250,240,230),
  4719. "antiquewhite"=>array(250,235,215),
  4720. "papayawhip"=>array(255,239,213),
  4721. "blanchedalmond"=>array(255,235,205),
  4722. "bisque"=>array(255,228,196),
  4723. "peachpuff"=>array(255,218,185),
  4724. "navajowhite"=>array(255,222,173),
  4725. "moccasin"=>array(255,228,181),
  4726. "cornsilk"=>array(255,248,220),
  4727. "ivory"=>array(255,255,240),
  4728. "lemonchiffon"=>array(255,250,205),
  4729. "seashell"=>array(255,245,238),
  4730. "mintcream"=>array(245,255,250),
  4731. "azure"=>array(240,255,255),
  4732. "aliceblue"=>array(240,248,255),
  4733. "lavender"=>array(230,230,250),
  4734. "lavenderblush"=>array(255,240,245),
  4735. "mistyrose"=>array(255,228,225),
  4736. "white"=>array(255,255,255),
  4737. "black"=>array(0,0,0),
  4738. "darkslategray"=>array(47,79,79),
  4739. "dimgray"=>array(105,105,105),
  4740. "slategray"=>array(112,128,144),
  4741. "lightslategray"=>array(119,136,153),
  4742. "gray"=>array(190,190,190),
  4743. "lightgray"=>array(211,211,211),
  4744. "midnightblue"=>array(25,25,112),
  4745. "navy"=>array(0,0,128),
  4746. "cornflowerblue"=>array(100,149,237),
  4747. "darkslateblue"=>array(72,61,139),
  4748. "slateblue"=>array(106,90,205),
  4749. "mediumslateblue"=>array(123,104,238),
  4750. "lightslateblue"=>array(132,112,255),
  4751. "mediumblue"=>array(0,0,205),
  4752. "royalblue"=>array(65,105,225),
  4753. "blue"=>array(0,0,255),
  4754. "dodgerblue"=>array(30,144,255),
  4755. "deepskyblue"=>array(0,191,255),
  4756. "skyblue"=>array(135,206,235),
  4757. "lightskyblue"=>array(135,206,250),
  4758. "steelblue"=>array(70,130,180),
  4759. "lightred"=>array(211,167,168),
  4760. "lightsteelblue"=>array(176,196,222),
  4761. "lightblue"=>array(173,216,230),
  4762. "powderblue"=>array(176,224,230),
  4763. "paleturquoise"=>array(175,238,238),
  4764. "darkturquoise"=>array(0,206,209),
  4765. "mediumturquoise"=>array(72,209,204),
  4766. "turquoise"=>array(64,224,208),
  4767. "cyan"=>array(0,255,255),
  4768. "lightcyan"=>array(224,255,255),
  4769. "cadetblue"=>array(95,158,160),
  4770. "mediumaquamarine"=>array(102,205,170),
  4771. "aquamarine"=>array(127,255,212),
  4772. "darkgreen"=>array(0,100,0),
  4773. "darkolivegreen"=>array(85,107,47),
  4774. "darkseagreen"=>array(143,188,143),
  4775. "seagreen"=>array(46,139,87),
  4776. "mediumseagreen"=>array(60,179,113),
  4777. "lightseagreen"=>array(32,178,170),
  4778. "palegreen"=>array(152,251,152),
  4779. "springgreen"=>array(0,255,127),
  4780. "lawngreen"=>array(124,252,0),
  4781. "green"=>array(0,255,0),
  4782. "chartreuse"=>array(127,255,0),
  4783. "mediumspringgreen"=>array(0,250,154),
  4784. "greenyellow"=>array(173,255,47),
  4785. "limegreen"=>array(50,205,50),
  4786. "yellowgreen"=>array(154,205,50),
  4787. "forestgreen"=>array(34,139,34),
  4788. "olivedrab"=>array(107,142,35),
  4789. "darkkhaki"=>array(189,183,107),
  4790. "khaki"=>array(240,230,140),
  4791. "palegoldenrod"=>array(238,232,170),
  4792. "lightgoldenrodyellow"=>array(250,250,210),
  4793. "lightyellow"=>array(255,255,200),
  4794. "yellow"=>array(255,255,0),
  4795. "gold"=>array(255,215,0),
  4796. "lightgoldenrod"=>array(238,221,130),
  4797. "goldenrod"=>array(218,165,32),
  4798. "darkgoldenrod"=>array(184,134,11),
  4799. "rosybrown"=>array(188,143,143),
  4800. "indianred"=>array(205,92,92),
  4801. "saddlebrown"=>array(139,69,19),
  4802. "sienna"=>array(160,82,45),
  4803. "peru"=>array(205,133,63),
  4804. "burlywood"=>array(222,184,135),
  4805. "beige"=>array(245,245,220),
  4806. "wheat"=>array(245,222,179),
  4807. "sandybrown"=>array(244,164,96),
  4808. "tan"=>array(210,180,140),
  4809. "chocolate"=>array(210,105,30),
  4810. "firebrick"=>array(178,34,34),
  4811. "brown"=>array(165,42,42),
  4812. "darksalmon"=>array(233,150,122),
  4813. "salmon"=>array(250,128,114),
  4814. "lightsalmon"=>array(255,160,122),
  4815. "orange"=>array(255,165,0),
  4816. "darkorange"=>array(255,140,0),
  4817. "coral"=>array(255,127,80),
  4818. "lightcoral"=>array(240,128,128),
  4819. "tomato"=>array(255,99,71),
  4820. "orangered"=>array(255,69,0),
  4821. "red"=>array(255,0,0),
  4822. "hotpink"=>array(255,105,180),
  4823. "deeppink"=>array(255,20,147),
  4824. "pink"=>array(255,192,203),
  4825. "lightpink"=>array(255,182,193),
  4826. "palevioletred"=>array(219,112,147),
  4827. "maroon"=>array(176,48,96),
  4828. "mediumvioletred"=>array(199,21,133),
  4829. "violetred"=>array(208,32,144),
  4830. "magenta"=>array(255,0,255),
  4831. "violet"=>array(238,130,238),
  4832. "plum"=>array(221,160,221),
  4833. "orchid"=>array(218,112,214),
  4834. "mediumorchid"=>array(186,85,211),
  4835. "darkorchid"=>array(153,50,204),
  4836. "darkviolet"=>array(148,0,211),
  4837. "blueviolet"=>array(138,43,226),
  4838. "purple"=>array(160,32,240),
  4839. "mediumpurple"=>array(147,112,219),
  4840. "thistle"=>array(216,191,216),
  4841. "snow1"=>array(255,250,250),
  4842. "snow2"=>array(238,233,233),
  4843. "snow3"=>array(205,201,201),
  4844. "snow4"=>array(139,137,137),
  4845. "seashell1"=>array(255,245,238),
  4846. "seashell2"=>array(238,229,222),
  4847. "seashell3"=>array(205,197,191),
  4848. "seashell4"=>array(139,134,130),
  4849. "AntiqueWhite1"=>array(255,239,219),
  4850. "AntiqueWhite2"=>array(238,223,204),
  4851. "AntiqueWhite3"=>array(205,192,176),
  4852. "AntiqueWhite4"=>array(139,131,120),
  4853. "bisque1"=>array(255,228,196),
  4854. "bisque2"=>array(238,213,183),
  4855. "bisque3"=>array(205,183,158),
  4856. "bisque4"=>array(139,125,107),
  4857. "peachPuff1"=>array(255,218,185),
  4858. "peachpuff2"=>array(238,203,173),
  4859. "peachpuff3"=>array(205,175,149),
  4860. "peachpuff4"=>array(139,119,101),
  4861. "navajowhite1"=>array(255,222,173),
  4862. "navajowhite2"=>array(238,207,161),
  4863. "navajowhite3"=>array(205,179,139),
  4864. "navajowhite4"=>array(139,121,94),
  4865. "lemonchiffon1"=>array(255,250,205),
  4866. "lemonchiffon2"=>array(238,233,191),
  4867. "lemonchiffon3"=>array(205,201,165),
  4868. "lemonchiffon4"=>array(139,137,112),
  4869. "ivory1"=>array(255,255,240),
  4870. "ivory2"=>array(238,238,224),
  4871. "ivory3"=>array(205,205,193),
  4872. "ivory4"=>array(139,139,131),
  4873. "honeydew"=>array(193,205,193),
  4874. "lavenderblush1"=>array(255,240,245),
  4875. "lavenderblush2"=>array(238,224,229),
  4876. "lavenderblush3"=>array(205,193,197),
  4877. "lavenderblush4"=>array(139,131,134),
  4878. "mistyrose1"=>array(255,228,225),
  4879. "mistyrose2"=>array(238,213,210),
  4880. "mistyrose3"=>array(205,183,181),
  4881. "mistyrose4"=>array(139,125,123),
  4882. "azure1"=>array(240,255,255),
  4883. "azure2"=>array(224,238,238),
  4884. "azure3"=>array(193,205,205),
  4885. "azure4"=>array(131,139,139),
  4886. "slateblue1"=>array(131,111,255),
  4887. "slateblue2"=>array(122,103,238),
  4888. "slateblue3"=>array(105,89,205),
  4889. "slateblue4"=>array(71,60,139),
  4890. "royalblue1"=>array(72,118,255),
  4891. "royalblue2"=>array(67,110,238),
  4892. "royalblue3"=>array(58,95,205),
  4893. "royalblue4"=>array(39,64,139),
  4894. "dodgerblue1"=>array(30,144,255),
  4895. "dodgerblue2"=>array(28,134,238),
  4896. "dodgerblue3"=>array(24,116,205),
  4897. "dodgerblue4"=>array(16,78,139),
  4898. "steelblue1"=>array(99,184,255),
  4899. "steelblue2"=>array(92,172,238),
  4900. "steelblue3"=>array(79,148,205),
  4901. "steelblue4"=>array(54,100,139),
  4902. "deepskyblue1"=>array(0,191,255),
  4903. "deepskyblue2"=>array(0,178,238),
  4904. "deepskyblue3"=>array(0,154,205),
  4905. "deepskyblue4"=>array(0,104,139),
  4906. "skyblue1"=>array(135,206,255),
  4907. "skyblue2"=>array(126,192,238),
  4908. "skyblue3"=>array(108,166,205),
  4909. "skyblue4"=>array(74,112,139),
  4910. "lightskyblue1"=>array(176,226,255),
  4911. "lightskyblue2"=>array(164,211,238),
  4912. "lightskyblue3"=>array(141,182,205),
  4913. "lightskyblue4"=>array(96,123,139),
  4914. "slategray1"=>array(198,226,255),
  4915. "slategray2"=>array(185,211,238),
  4916. "slategray3"=>array(159,182,205),
  4917. "slategray4"=>array(108,123,139),
  4918. "lightsteelblue1"=>array(202,225,255),
  4919. "lightsteelblue2"=>array(188,210,238),
  4920. "lightsteelblue3"=>array(162,181,205),
  4921. "lightsteelblue4"=>array(110,123,139),
  4922. "lightblue1"=>array(191,239,255),
  4923. "lightblue2"=>array(178,223,238),
  4924. "lightblue3"=>array(154,192,205),
  4925. "lightblue4"=>array(104,131,139),
  4926. "lightcyan1"=>array(224,255,255),
  4927. "lightcyan2"=>array(209,238,238),
  4928. "lightcyan3"=>array(180,205,205),
  4929. "lightcyan4"=>array(122,139,139),
  4930. "paleturquoise1"=>array(187,255,255),
  4931. "paleturquoise2"=>array(174,238,238),
  4932. "paleturquoise3"=>array(150,205,205),
  4933. "paleturquoise4"=>array(102,139,139),
  4934. "cadetblue1"=>array(152,245,255),
  4935. "cadetblue2"=>array(142,229,238),
  4936. "cadetblue3"=>array(122,197,205),
  4937. "cadetblue4"=>array(83,134,139),
  4938. "turquoise1"=>array(0,245,255),
  4939. "turquoise2"=>array(0,229,238),
  4940. "turquoise3"=>array(0,197,205),
  4941. "turquoise4"=>array(0,134,139),
  4942. "cyan1"=>array(0,255,255),
  4943. "cyan2"=>array(0,238,238),
  4944. "cyan3"=>array(0,205,205),
  4945. "cyan4"=>array(0,139,139),
  4946. "darkslategray1"=>array(151,255,255),
  4947. "darkslategray2"=>array(141,238,238),
  4948. "darkslategray3"=>array(121,205,205),
  4949. "darkslategray4"=>array(82,139,139),
  4950. "aquamarine1"=>array(127,255,212),
  4951. "aquamarine2"=>array(118,238,198),
  4952. "aquamarine3"=>array(102,205,170),
  4953. "aquamarine4"=>array(69,139,116),
  4954. "darkseagreen1"=>array(193,255,193),
  4955. "darkseagreen2"=>array(180,238,180),
  4956. "darkseagreen3"=>array(155,205,155),
  4957. "darkseagreen4"=>array(105,139,105),
  4958. "seagreen1"=>array(84,255,159),
  4959. "seagreen2"=>array(78,238,148),
  4960. "seagreen3"=>array(67,205,128),
  4961. "seagreen4"=>array(46,139,87),
  4962. "palegreen1"=>array(154,255,154),
  4963. "palegreen2"=>array(144,238,144),
  4964. "palegreen3"=>array(124,205,124),
  4965. "palegreen4"=>array(84,139,84),
  4966. "springgreen1"=>array(0,255,127),
  4967. "springgreen2"=>array(0,238,118),
  4968. "springgreen3"=>array(0,205,102),
  4969. "springgreen4"=>array(0,139,69),
  4970. "chartreuse1"=>array(127,255,0),
  4971. "chartreuse2"=>array(118,238,0),
  4972. "chartreuse3"=>array(102,205,0),
  4973. "chartreuse4"=>array(69,139,0),
  4974. "olivedrab1"=>array(192,255,62),
  4975. "olivedrab2"=>array(179,238,58),
  4976. "olivedrab3"=>array(154,205,50),
  4977. "olivedrab4"=>array(105,139,34),
  4978. "darkolivegreen1"=>array(202,255,112),
  4979. "darkolivegreen2"=>array(188,238,104),
  4980. "darkolivegreen3"=>array(162,205,90),
  4981. "darkolivegreen4"=>array(110,139,61),
  4982. "khaki1"=>array(255,246,143),
  4983. "khaki2"=>array(238,230,133),
  4984. "khaki3"=>array(205,198,115),
  4985. "khaki4"=>array(139,134,78),
  4986. "lightgoldenrod1"=>array(255,236,139),
  4987. "lightgoldenrod2"=>array(238,220,130),
  4988. "lightgoldenrod3"=>array(205,190,112),
  4989. "lightgoldenrod4"=>array(139,129,76),
  4990. "yellow1"=>array(255,255,0),
  4991. "yellow2"=>array(238,238,0),
  4992. "yellow3"=>array(205,205,0),
  4993. "yellow4"=>array(139,139,0),
  4994. "gold1"=>array(255,215,0),
  4995. "gold2"=>array(238,201,0),
  4996. "gold3"=>array(205,173,0),
  4997. "gold4"=>array(139,117,0),
  4998. "goldenrod1"=>array(255,193,37),
  4999. "goldenrod2"=>array(238,180,34),
  5000. "goldenrod3"=>array(205,155,29),
  5001. "goldenrod4"=>array(139,105,20),
  5002. "darkgoldenrod1"=>array(255,185,15),
  5003. "darkgoldenrod2"=>array(238,173,14),
  5004. "darkgoldenrod3"=>array(205,149,12),
  5005. "darkgoldenrod4"=>array(139,101,8),
  5006. "rosybrown1"=>array(255,193,193),
  5007. "rosybrown2"=>array(238,180,180),
  5008. "rosybrown3"=>array(205,155,155),
  5009. "rosybrown4"=>array(139,105,105),
  5010. "indianred1"=>array(255,106,106),
  5011. "indianred2"=>array(238,99,99),
  5012. "indianred3"=>array(205,85,85),
  5013. "indianred4"=>array(139,58,58),
  5014. "sienna1"=>array(255,130,71),
  5015. "sienna2"=>array(238,121,66),
  5016. "sienna3"=>array(205,104,57),
  5017. "sienna4"=>array(139,71,38),
  5018. "burlywood1"=>array(255,211,155),
  5019. "burlywood2"=>array(238,197,145),
  5020. "burlywood3"=>array(205,170,125),
  5021. "burlywood4"=>array(139,115,85),
  5022. "wheat1"=>array(255,231,186),
  5023. "wheat2"=>array(238,216,174),
  5024. "wheat3"=>array(205,186,150),
  5025. "wheat4"=>array(139,126,102),
  5026. "tan1"=>array(255,165,79),
  5027. "tan2"=>array(238,154,73),
  5028. "tan3"=>array(205,133,63),
  5029. "tan4"=>array(139,90,43),
  5030. "chocolate1"=>array(255,127,36),
  5031. "chocolate2"=>array(238,118,33),
  5032. "chocolate3"=>array(205,102,29),
  5033. "chocolate4"=>array(139,69,19),
  5034. "firebrick1"=>array(255,48,48),
  5035. "firebrick2"=>array(238,44,44),
  5036. "firebrick3"=>array(205,38,38),
  5037. "firebrick4"=>array(139,26,26),
  5038. "brown1"=>array(255,64,64),
  5039. "brown2"=>array(238,59,59),
  5040. "brown3"=>array(205,51,51),
  5041. "brown4"=>array(139,35,35),
  5042. "salmon1"=>array(255,140,105),
  5043. "salmon2"=>array(238,130,98),
  5044. "salmon3"=>array(205,112,84),
  5045. "salmon4"=>array(139,76,57),
  5046. "lightsalmon1"=>array(255,160,122),
  5047. "lightsalmon2"=>array(238,149,114),
  5048. "lightsalmon3"=>array(205,129,98),
  5049. "lightsalmon4"=>array(139,87,66),
  5050. "orange1"=>array(255,165,0),
  5051. "orange2"=>array(238,154,0),
  5052. "orange3"=>array(205,133,0),
  5053. "orange4"=>array(139,90,0),
  5054. "darkorange1"=>array(255,127,0),
  5055. "darkorange2"=>array(238,118,0),
  5056. "darkorange3"=>array(205,102,0),
  5057. "darkorange4"=>array(139,69,0),
  5058. "coral1"=>array(255,114,86),
  5059. "coral2"=>array(238,106,80),
  5060. "coral3"=>array(205,91,69),
  5061. "coral4"=>array(139,62,47),
  5062. "tomato1"=>array(255,99,71),
  5063. "tomato2"=>array(238,92,66),
  5064. "tomato3"=>array(205,79,57),
  5065. "tomato4"=>array(139,54,38),
  5066. "orangered1"=>array(255,69,0),
  5067. "orangered2"=>array(238,64,0),
  5068. "orangered3"=>array(205,55,0),
  5069. "orangered4"=>array(139,37,0),
  5070. "deeppink1"=>array(255,20,147),
  5071. "deeppink2"=>array(238,18,137),
  5072. "deeppink3"=>array(205,16,118),
  5073. "deeppink4"=>array(139,10,80),
  5074. "hotpink1"=>array(255,110,180),
  5075. "hotpink2"=>array(238,106,167),
  5076. "hotpink3"=>array(205,96,144),
  5077. "hotpink4"=>array(139,58,98),
  5078. "pink1"=>array(255,181,197),
  5079. "pink2"=>array(238,169,184),
  5080. "pink3"=>array(205,145,158),
  5081. "pink4"=>array(139,99,108),
  5082. "lightpink1"=>array(255,174,185),
  5083. "lightpink2"=>array(238,162,173),
  5084. "lightpink3"=>array(205,140,149),
  5085. "lightpink4"=>array(139,95,101),
  5086. "palevioletred1"=>array(255,130,171),
  5087. "palevioletred2"=>array(238,121,159),
  5088. "palevioletred3"=>array(205,104,137),
  5089. "palevioletred4"=>array(139,71,93),
  5090. "maroon1"=>array(255,52,179),
  5091. "maroon2"=>array(238,48,167),
  5092. "maroon3"=>array(205,41,144),
  5093. "maroon4"=>array(139,28,98),
  5094. "violetred1"=>array(255,62,150),
  5095. "violetred2"=>array(238,58,140),
  5096. "violetred3"=>array(205,50,120),
  5097. "violetred4"=>array(139,34,82),
  5098. "magenta1"=>array(255,0,255),
  5099. "magenta2"=>array(238,0,238),
  5100. "magenta3"=>array(205,0,205),
  5101. "magenta4"=>array(139,0,139),
  5102. "mediumred"=>array(140,34,34),
  5103. "orchid1"=>array(255,131,250),
  5104. "orchid2"=>array(238,122,233),
  5105. "orchid3"=>array(205,105,201),
  5106. "orchid4"=>array(139,71,137),
  5107. "plum1"=>array(255,187,255),
  5108. "plum2"=>array(238,174,238),
  5109. "plum3"=>array(205,150,205),
  5110. "plum4"=>array(139,102,139),
  5111. "mediumorchid1"=>array(224,102,255),
  5112. "mediumorchid2"=>array(209,95,238),
  5113. "mediumorchid3"=>array(180,82,205),
  5114. "mediumorchid4"=>array(122,55,139),
  5115. "darkorchid1"=>array(191,62,255),
  5116. "darkorchid2"=>array(178,58,238),
  5117. "darkorchid3"=>array(154,50,205),
  5118. "darkorchid4"=>array(104,34,139),
  5119. "purple1"=>array(155,48,255),
  5120. "purple2"=>array(145,44,238),
  5121. "purple3"=>array(125,38,205),
  5122. "purple4"=>array(85,26,139),
  5123. "mediumpurple1"=>array(171,130,255),
  5124. "mediumpurple2"=>array(159,121,238),
  5125. "mediumpurple3"=>array(137,104,205),
  5126. "mediumpurple4"=>array(93,71,139),
  5127. "thistle1"=>array(255,225,255),
  5128. "thistle2"=>array(238,210,238),
  5129. "thistle3"=>array(205,181,205),
  5130. "thistle4"=>array(139,123,139),
  5131. "gray1"=>array(10,10,10),
  5132. "gray2"=>array(40,40,30),
  5133. "gray3"=>array(70,70,70),
  5134. "gray4"=>array(100,100,100),
  5135. "gray5"=>array(130,130,130),
  5136. "gray6"=>array(160,160,160),
  5137. "gray7"=>array(190,190,190),
  5138. "gray8"=>array(210,210,210),
  5139. "gray9"=>array(240,240,240),
  5140. "darkgray"=>array(100,100,100),
  5141. "darkblue"=>array(0,0,139),
  5142. "darkcyan"=>array(0,139,139),
  5143. "darkmagenta"=>array(139,0,139),
  5144. "darkred"=>array(139,0,0),
  5145. "silver"=>array(192, 192, 192),
  5146. "eggplant"=>array(144,176,168),
  5147. "lightgreen"=>array(144,238,144));
  5148. }
  5149. //----------------
  5150. // PUBLIC METHODS
  5151. // Colors can be specified as either
  5152. // 1. #xxxxxx HTML style
  5153. // 2. "colorname" as a named color
  5154. // 3. array(r,g,b) RGB triple
  5155. // This function translates this to a native RGB format and returns an
  5156. // RGB triple.
  5157. function Color($aColor) {
  5158. if (is_string($aColor)) {
  5159. // Strip of any alpha factor
  5160. $pos = strpos($aColor,'@');
  5161. if( $pos === false ) {
  5162. $alpha = 0;
  5163. }
  5164. else {
  5165. $pos2 = strpos($aColor,':');
  5166. if( $pos2===false )
  5167. $pos2 = $pos-1; // Sentinel
  5168. if( $pos > $pos2 ) {
  5169. $alpha = substr($aColor,$pos+1);
  5170. $aColor = substr($aColor,0,$pos);
  5171. }
  5172. else {
  5173. $alpha = substr($aColor,$pos+1,$pos2-$pos-1);
  5174. $aColor = substr($aColor,0,$pos).substr($aColor,$pos2);
  5175. }
  5176. }
  5177. // Extract potential adjustment figure at end of color
  5178. // specification
  5179. $pos = strpos($aColor,":");
  5180. if( $pos === false ) {
  5181. $adj = 1.0;
  5182. }
  5183. else {
  5184. $adj = 0.0 + substr($aColor,$pos+1);
  5185. $aColor = substr($aColor,0,$pos);
  5186. }
  5187. if( $adj < 0 )
  5188. JpGraphError::RaiseL(25077);//('Adjustment factor for color must be > 0');
  5189. if (substr($aColor, 0, 1) == "#") {
  5190. $r = hexdec(substr($aColor, 1, 2));
  5191. $g = hexdec(substr($aColor, 3, 2));
  5192. $b = hexdec(substr($aColor, 5, 2));
  5193. } else {
  5194. if(!isset($this->rgb_table[$aColor]) )
  5195. JpGraphError::RaiseL(25078,$aColor);//(" Unknown color: $aColor");
  5196. $tmp=$this->rgb_table[$aColor];
  5197. $r = $tmp[0];
  5198. $g = $tmp[1];
  5199. $b = $tmp[2];
  5200. }
  5201. // Scale adj so that an adj=2 always
  5202. // makes the color 100% white (i.e. 255,255,255.
  5203. // and adj=1 neutral and adj=0 black.
  5204. if( $adj > 1 ) {
  5205. $m = ($adj-1.0)*(255-min(255,min($r,min($g,$b))));
  5206. return array(min(255,$r+$m), min(255,$g+$m), min(255,$b+$m),$alpha);
  5207. }
  5208. elseif( $adj < 1 ) {
  5209. $m = ($adj-1.0)*max(255,max($r,max($g,$b)));
  5210. return array(max(0,$r+$m), max(0,$g+$m), max(0,$b+$m),$alpha);
  5211. }
  5212. else {
  5213. return array($r,$g,$b,$alpha);
  5214. }
  5215. } elseif( is_array($aColor) ) {
  5216. if( count($aColor)==3 ) {
  5217. $aColor[3]=0;
  5218. return $aColor;
  5219. }
  5220. else
  5221. return $aColor;
  5222. }
  5223. else
  5224. JpGraphError::RaiseL(25079,$aColor,count($aColor));//(" Unknown color specification: $aColor , size=".count($aColor));
  5225. }
  5226. // Compare two colors
  5227. // return true if equal
  5228. function Equal($aCol1,$aCol2) {
  5229. $c1 = $this->Color($aCol1);
  5230. $c2 = $this->Color($aCol2);
  5231. if( $c1[0]==$c2[0] && $c1[1]==$c2[1] && $c1[2]==$c2[2] )
  5232. return true;
  5233. else
  5234. return false;
  5235. }
  5236. // Allocate a new color in the current image
  5237. // Return new color index, -1 if no more colors could be allocated
  5238. function Allocate($aColor,$aAlpha=0.0) {
  5239. list ($r, $g, $b, $a) = $this->color($aColor);
  5240. // If alpha is specified in the color string then this
  5241. // takes precedence over the second argument
  5242. if( $a > 0 )
  5243. $aAlpha = $a;
  5244. if( $GLOBALS['gd2'] ) {
  5245. if( $aAlpha < 0 || $aAlpha > 1 ) {
  5246. JpGraphError::RaiseL(25080);//('Alpha parameter for color must be between 0.0 and 1.0');
  5247. }
  5248. return imagecolorresolvealpha($this->img, $r, $g, $b, round($aAlpha * 127));
  5249. } else {
  5250. $index = imagecolorexact($this->img, $r, $g, $b);
  5251. if ($index == -1) {
  5252. $index = imagecolorallocate($this->img, $r, $g, $b);
  5253. if( USE_APPROX_COLORS && $index == -1 )
  5254. $index = imagecolorresolve($this->img, $r, $g, $b);
  5255. }
  5256. return $index;
  5257. }
  5258. }
  5259. } // Class
  5260. //===================================================
  5261. // CLASS Image
  5262. // Description: Wrapper class with some goodies to form the
  5263. // Interface to low level image drawing routines.
  5264. //===================================================
  5265. class Image {
  5266. var $img_format;
  5267. var $expired=true;
  5268. var $img=null;
  5269. var $left_margin=30,$right_margin=20,$top_margin=20,$bottom_margin=30;
  5270. var $plotwidth=0,$plotheight=0;
  5271. var $rgb=null;
  5272. var $current_color,$current_color_name;
  5273. var $lastx=0, $lasty=0;
  5274. var $width=0, $height=0;
  5275. var $line_weight=1;
  5276. var $line_style=1; // Default line style is solid
  5277. var $obs_list=array();
  5278. var $font_size=12,$font_family=FF_FONT1, $font_style=FS_NORMAL;
  5279. var $font_file='';
  5280. var $text_halign="left",$text_valign="bottom";
  5281. var $ttf=null;
  5282. var $use_anti_aliasing=false;
  5283. var $quality=null;
  5284. var $colorstack=array(),$colorstackidx=0;
  5285. var $canvascolor = 'white' ;
  5286. var $langconv = null ;
  5287. //---------------
  5288. // CONSTRUCTOR
  5289. function Image($aWidth,$aHeight,$aFormat=DEFAULT_GFORMAT) {
  5290. $this->CreateImgCanvas($aWidth,$aHeight);
  5291. $this->SetAutoMargin();
  5292. if( !$this->SetImgFormat($aFormat) ) {
  5293. JpGraphError::RaiseL(25081,$aFormat);//("JpGraph: Selected graphic format is either not supported or unknown [$aFormat]");
  5294. }
  5295. $this->ttf = new TTF();
  5296. $this->langconv = new LanguageConv();
  5297. }
  5298. // Should we use anti-aliasing. Note: This really slows down graphics!
  5299. function SetAntiAliasing() {
  5300. $this->use_anti_aliasing=true;
  5301. }
  5302. function CreateRawCanvas($aWidth=0,$aHeight=0) {
  5303. if( $aWidth <= 1 || $aHeight <= 1 ) {
  5304. JpGraphError::RaiseL(25082,$aWidth,$aHeight);//("Illegal sizes specified for width or height when creating an image, (width=$aWidth, height=$aHeight)");
  5305. }
  5306. if( @$GLOBALS['gd2']==true && USE_TRUECOLOR ) {
  5307. $this->img = @imagecreatetruecolor($aWidth, $aHeight);
  5308. if( $this->img < 1 ) {
  5309. JpGraphError::RaiseL(25126);
  5310. //die("Can't create truecolor image. Check that you really have GD2 library installed.");
  5311. }
  5312. $this->SetAlphaBlending();
  5313. } else {
  5314. $this->img = @imagecreate($aWidth, $aHeight);
  5315. if( $this->img < 1 ) {
  5316. JpGraphError::RaiseL(25126);
  5317. //die("<b>JpGraph Error:</b> Can't create image. Check that you really have the GD library installed.");
  5318. }
  5319. }
  5320. if( $this->rgb != null )
  5321. $this->rgb->img = $this->img ;
  5322. else
  5323. $this->rgb = new RGB($this->img);
  5324. }
  5325. function CloneCanvasH() {
  5326. $oldimage = $this->img;
  5327. $this->CreateRawCanvas($this->width,$this->height);
  5328. imagecopy($this->img,$oldimage,0,0,0,0,$this->width,$this->height);
  5329. return $oldimage;
  5330. }
  5331. function CreateImgCanvas($aWidth=0,$aHeight=0) {
  5332. $old = array($this->img,$this->width,$this->height);
  5333. $aWidth = round($aWidth);
  5334. $aHeight = round($aHeight);
  5335. $this->width=$aWidth;
  5336. $this->height=$aHeight;
  5337. if( $aWidth==0 || $aHeight==0 ) {
  5338. // We will set the final size later.
  5339. // Note: The size must be specified before any other
  5340. // img routines that stroke anything are called.
  5341. $this->img = null;
  5342. $this->rgb = null;
  5343. return $old;
  5344. }
  5345. $this->CreateRawCanvas($aWidth,$aHeight);
  5346. // Set canvas color (will also be the background color for a
  5347. // a pallett image
  5348. $this->SetColor($this->canvascolor);
  5349. $this->FilledRectangle(0,0,$aWidth,$aHeight);
  5350. return $old ;
  5351. }
  5352. function CopyCanvasH($aToHdl,$aFromHdl,$aToX,$aToY,$aFromX,$aFromY,$aWidth,$aHeight,$aw=-1,$ah=-1) {
  5353. if( $aw === -1 ) {
  5354. $aw = $aWidth;
  5355. $ah = $aHeight;
  5356. $f = 'imagecopyresized';
  5357. }
  5358. else {
  5359. $f = $GLOBALS['copyfunc'] ;
  5360. }
  5361. $f($aToHdl,$aFromHdl,
  5362. $aToX,$aToY,$aFromX,$aFromY, $aWidth,$aHeight,$aw,$ah);
  5363. }
  5364. function Copy($fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth=-1,$fromHeight=-1) {
  5365. $this->CopyCanvasH($this->img,$fromImg,$toX,$toY,$fromX,$fromY,
  5366. $toWidth,$toHeight,$fromWidth,$fromHeight);
  5367. }
  5368. function CopyMerge($fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth=-1,$fromHeight=-1,$aMix=100) {
  5369. if( $aMix == 100 ) {
  5370. $this->CopyCanvasH($this->img,$fromImg,
  5371. $toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth,$fromHeight);
  5372. }
  5373. else {
  5374. if( ($fromWidth != -1 && ($fromWidth != $toWidth)) ||
  5375. ($fromHeight != -1 && ($fromHeight != $fromHeight)) ) {
  5376. // Create a new canvas that will hold the re-scaled original from image
  5377. if( $toWidth <= 1 || $toHeight <= 1 ) {
  5378. JpGraphError::RaiseL(25083);//('Illegal image size when copying image. Size for copied to image is 1 pixel or less.');
  5379. }
  5380. if( @$GLOBALS['gd2']==true && USE_TRUECOLOR ) {
  5381. $tmpimg = @imagecreatetruecolor($toWidth, $toHeight);
  5382. } else {
  5383. $tmpimg = @imagecreate($toWidth, $toHeight);
  5384. }
  5385. if( $tmpimg < 1 ) {
  5386. JpGraphError::RaiseL(25084);//('Failed to create temporary GD canvas. Out of memory ?');
  5387. }
  5388. $this->CopyCanvasH($tmpimg,$fromImg,0,0,0,0,
  5389. $toWidth,$toHeight,$fromWidth,$fromHeight);
  5390. $fromImg = $tmpimg;
  5391. }
  5392. imagecopymerge($this->img,$fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$aMix);
  5393. }
  5394. }
  5395. function GetWidth($aImg=null) {
  5396. if( $aImg === null )
  5397. $aImg = $this->img;
  5398. return imagesx($aImg);
  5399. }
  5400. function GetHeight($aImg=null) {
  5401. if( $aImg === null )
  5402. $aImg = $this->img;
  5403. return imagesy($aImg);
  5404. }
  5405. function CreateFromString($aStr) {
  5406. $img = imagecreatefromstring($aStr);
  5407. if( $img === false ) {
  5408. JpGraphError::RaiseL(25085);//('An image can not be created from the supplied string. It is either in a format not supported or the string is representing an corrupt image.');
  5409. }
  5410. return $img;
  5411. }
  5412. function SetCanvasH($aHdl) {
  5413. $this->img = $aHdl;
  5414. $this->rgb->img = $aHdl;
  5415. }
  5416. function SetCanvasColor($aColor) {
  5417. $this->canvascolor = $aColor ;
  5418. }
  5419. function SetAlphaBlending($aFlg=true) {
  5420. if( $GLOBALS['gd2'] )
  5421. ImageAlphaBlending($this->img,$aFlg);
  5422. else
  5423. JpGraphError::RaiseL(25086);//('You only seem to have GD 1.x installed. To enable Alphablending requires GD 2.x or higher. Please install GD or make sure the constant USE_GD2 is specified correctly to reflect your installation. By default it tries to autodetect what version of GD you have installed. On some very rare occasions it may falsely detect GD2 where only GD1 is installed. You must then set USE_GD2 to false.');
  5424. }
  5425. function SetAutoMargin() {
  5426. GLOBAL $gJpgBrandTiming;
  5427. $min_bm=10;
  5428. /*
  5429. if( $gJpgBrandTiming )
  5430. $min_bm=15;
  5431. */
  5432. $lm = min(40,$this->width/7);
  5433. $rm = min(20,$this->width/10);
  5434. $tm = max(20,$this->height/7);
  5435. $bm = max($min_bm,$this->height/7);
  5436. $this->SetMargin($lm,$rm,$tm,$bm);
  5437. }
  5438. //---------------
  5439. // PUBLIC METHODS
  5440. function SetFont($family,$style=FS_NORMAL,$size=10) {
  5441. $this->font_family=$family;
  5442. $this->font_style=$style;
  5443. $this->font_size=$size;
  5444. $this->font_file='';
  5445. if( ($this->font_family==FF_FONT1 || $this->font_family==FF_FONT2) && $this->font_style==FS_BOLD ){
  5446. ++$this->font_family;
  5447. }
  5448. if( $this->font_family > FF_FONT2+1 ) { // A TTF font so get the font file
  5449. // Check that this PHP has support for TTF fonts
  5450. if( !function_exists('imagettfbbox') ) {
  5451. JpGraphError::RaiseL(25087);//('This PHP build has not been configured with TTF support. You need to recompile your PHP installation with FreeType support.');
  5452. exit();
  5453. }
  5454. $this->font_file = $this->ttf->File($this->font_family,$this->font_style);
  5455. }
  5456. }
  5457. // Get the specific height for a text string
  5458. function GetTextHeight($txt="",$angle=0) {
  5459. $tmp = preg_split("/\n/",$txt);
  5460. $n = count($tmp);
  5461. $m=0;
  5462. for($i=0; $i< $n; ++$i)
  5463. $m = max($m,strlen($tmp[$i]));
  5464. if( $this->font_family <= FF_FONT2+1 ) {
  5465. if( $angle==0 ) {
  5466. $h = imagefontheight($this->font_family);
  5467. if( $h === false ) {
  5468. JpGraphError::RaiseL(25088);//('You have a misconfigured GD font support. The call to imagefontwidth() fails.');
  5469. }
  5470. return $n*$h;
  5471. }
  5472. else {
  5473. $w = @imagefontwidth($this->font_family);
  5474. if( $w === false ) {
  5475. JpGraphError::RaiseL(25088);//('You have a misconfigured GD font support. The call to imagefontwidth() fails.');
  5476. }
  5477. return $m*$w;
  5478. }
  5479. }
  5480. else {
  5481. $bbox = $this->GetTTFBBox($txt,$angle);
  5482. return $bbox[1]-$bbox[5];
  5483. }
  5484. }
  5485. // Estimate font height
  5486. function GetFontHeight($angle=0) {
  5487. $txt = "XOMg";
  5488. return $this->GetTextHeight($txt,$angle);
  5489. }
  5490. // Approximate font width with width of letter "O"
  5491. function GetFontWidth($angle=0) {
  5492. $txt = 'O';
  5493. return $this->GetTextWidth($txt,$angle);
  5494. }
  5495. // Get actual width of text in absolute pixels
  5496. function GetTextWidth($txt,$angle=0) {
  5497. $tmp = preg_split("/\n/",$txt);
  5498. $n = count($tmp);
  5499. if( $this->font_family <= FF_FONT2+1 ) {
  5500. $m=0;
  5501. for($i=0; $i < $n; ++$i) {
  5502. $l=strlen($tmp[$i]);
  5503. if( $l > $m ) {
  5504. $m = $l;
  5505. }
  5506. }
  5507. if( $angle==0 ) {
  5508. $w = @imagefontwidth($this->font_family);
  5509. if( $w === false ) {
  5510. JpGraphError::RaiseL(25088);//('You have a misconfigured GD font support. The call to imagefontwidth() fails.');
  5511. }
  5512. return $m*$w;
  5513. }
  5514. else {
  5515. // 90 degrees internal so height becomes width
  5516. $h = @imagefontheight($this->font_family);
  5517. if( $h === false ) {
  5518. JpGraphError::RaiseL(25089);//('You have a misconfigured GD font support. The call to imagefontheight() fails.');
  5519. }
  5520. return $n*$h;
  5521. }
  5522. }
  5523. else {
  5524. // For TTF fonts we must walk through a lines and find the
  5525. // widest one which we use as the width of the multi-line
  5526. // paragraph
  5527. $m=0;
  5528. for( $i=0; $i < $n; ++$i ) {
  5529. $bbox = $this->GetTTFBBox($tmp[$i],$angle);
  5530. $mm = $bbox[2] - $bbox[0];
  5531. if( $mm > $m )
  5532. $m = $mm;
  5533. }
  5534. return $m;
  5535. }
  5536. }
  5537. // Draw text with a box around it
  5538. function StrokeBoxedText($x,$y,$txt,$dir=0,$fcolor="white",$bcolor="black",
  5539. $shadowcolor=false,$paragraph_align="left",
  5540. $xmarg=6,$ymarg=4,$cornerradius=0,$dropwidth=3) {
  5541. if( !is_numeric($dir) ) {
  5542. if( $dir=="h" ) $dir=0;
  5543. elseif( $dir=="v" ) $dir=90;
  5544. else JpGraphError::RaiseL(25090,$dir);//(" Unknown direction specified in call to StrokeBoxedText() [$dir]");
  5545. }
  5546. if( $this->font_family >= FF_FONT0 && $this->font_family <= FF_FONT2+1) {
  5547. $width=$this->GetTextWidth($txt,$dir) ;
  5548. $height=$this->GetTextHeight($txt,$dir) ;
  5549. }
  5550. else {
  5551. $width=$this->GetBBoxWidth($txt,$dir) ;
  5552. $height=$this->GetBBoxHeight($txt,$dir) ;
  5553. }
  5554. $height += 2*$ymarg;
  5555. $width += 2*$xmarg;
  5556. if( $this->text_halign=="right" ) $x -= $width;
  5557. elseif( $this->text_halign=="center" ) $x -= $width/2;
  5558. if( $this->text_valign=="bottom" ) $y -= $height;
  5559. elseif( $this->text_valign=="center" ) $y -= $height/2;
  5560. if( $shadowcolor ) {
  5561. $this->PushColor($shadowcolor);
  5562. $this->FilledRoundedRectangle($x-$xmarg+$dropwidth,$y-$ymarg+$dropwidth,
  5563. $x+$width+$dropwidth,$y+$height-$ymarg+$dropwidth,
  5564. $cornerradius);
  5565. $this->PopColor();
  5566. $this->PushColor($fcolor);
  5567. $this->FilledRoundedRectangle($x-$xmarg,$y-$ymarg,
  5568. $x+$width,$y+$height-$ymarg,
  5569. $cornerradius);
  5570. $this->PopColor();
  5571. $this->PushColor($bcolor);
  5572. $this->RoundedRectangle($x-$xmarg,$y-$ymarg,
  5573. $x+$width,$y+$height-$ymarg,$cornerradius);
  5574. $this->PopColor();
  5575. }
  5576. else {
  5577. if( $fcolor ) {
  5578. $oc=$this->current_color;
  5579. $this->SetColor($fcolor);
  5580. $this->FilledRoundedRectangle($x-$xmarg,$y-$ymarg,$x+$width,$y+$height-$ymarg,$cornerradius);
  5581. $this->current_color=$oc;
  5582. }
  5583. if( $bcolor ) {
  5584. $oc=$this->current_color;
  5585. $this->SetColor($bcolor);
  5586. $this->RoundedRectangle($x-$xmarg,$y-$ymarg,$x+$width,$y+$height-$ymarg,$cornerradius);
  5587. $this->current_color=$oc;
  5588. }
  5589. }
  5590. $h=$this->text_halign;
  5591. $v=$this->text_valign;
  5592. $this->SetTextAlign("left","top");
  5593. $this->StrokeText($x, $y, $txt, $dir, $paragraph_align);
  5594. $bb = array($x-$xmarg,$y+$height-$ymarg,$x+$width,$y+$height-$ymarg,
  5595. $x+$width,$y-$ymarg,$x-$xmarg,$y-$ymarg);
  5596. $this->SetTextAlign($h,$v);
  5597. return $bb;
  5598. }
  5599. // Set text alignment
  5600. function SetTextAlign($halign,$valign="bottom") {
  5601. $this->text_halign=$halign;
  5602. $this->text_valign=$valign;
  5603. }
  5604. function _StrokeBuiltinFont($x,$y,$txt,$dir=0,$paragraph_align="left",&$aBoundingBox,$aDebug=false) {
  5605. if( is_numeric($dir) && $dir!=90 && $dir!=0)
  5606. JpGraphError::RaiseL(25091);//(" Internal font does not support drawing text at arbitrary angle. Use TTF fonts instead.");
  5607. $h=$this->GetTextHeight($txt);
  5608. $fh=$this->GetFontHeight();
  5609. $w=$this->GetTextWidth($txt);
  5610. if( $this->text_halign=="right")
  5611. $x -= $dir==0 ? $w : $h;
  5612. elseif( $this->text_halign=="center" ) {
  5613. // For center we subtract 1 pixel since this makes the middle
  5614. // be prefectly in the middle
  5615. $x -= $dir==0 ? $w/2-1 : $h/2;
  5616. }
  5617. if( $this->text_valign=="top" )
  5618. $y += $dir==0 ? $h : $w;
  5619. elseif( $this->text_valign=="center" )
  5620. $y += $dir==0 ? $h/2 : $w/2;
  5621. if( $dir==90 ) {
  5622. imagestringup($this->img,$this->font_family,$x,$y,$txt,$this->current_color);
  5623. $aBoundingBox = array(round($x),round($y),round($x),round($y-$w),round($x+$h),round($y-$w),round($x+$h),round($y));
  5624. if( $aDebug ) {
  5625. // Draw bounding box
  5626. $this->PushColor('green');
  5627. $this->Polygon($aBoundingBox,true);
  5628. $this->PopColor();
  5629. }
  5630. }
  5631. else {
  5632. if( preg_match("/\n/",$txt) ) {
  5633. $tmp = preg_split("/\n/",$txt);
  5634. for($i=0; $i < count($tmp); ++$i) {
  5635. $w1 = $this->GetTextWidth($tmp[$i]);
  5636. if( $paragraph_align=="left" ) {
  5637. imagestring($this->img,$this->font_family,$x,$y-$h+1+$i*$fh,$tmp[$i],$this->current_color);
  5638. }
  5639. elseif( $paragraph_align=="right" ) {
  5640. imagestring($this->img,$this->font_family,$x+($w-$w1),
  5641. $y-$h+1+$i*$fh,$tmp[$i],$this->current_color);
  5642. }
  5643. else {
  5644. imagestring($this->img,$this->font_family,$x+$w/2-$w1/2,
  5645. $y-$h+1+$i*$fh,$tmp[$i],$this->current_color);
  5646. }
  5647. }
  5648. }
  5649. else {
  5650. //Put the text
  5651. imagestring($this->img,$this->font_family,$x,$y-$h+1,$txt,$this->current_color);
  5652. }
  5653. if( $aDebug ) {
  5654. // Draw the bounding rectangle and the bounding box
  5655. $p1 = array(round($x),round($y),round($x),round($y-$h),round($x+$w),round($y-$h),round($x+$w),round($y));
  5656. // Draw bounding box
  5657. $this->PushColor('green');
  5658. $this->Polygon($p1,true);
  5659. $this->PopColor();
  5660. }
  5661. $aBoundingBox=array(round($x),round($y),round($x),round($y-$h),round($x+$w),round($y-$h),round($x+$w),round($y));
  5662. }
  5663. }
  5664. function AddTxtCR($aTxt) {
  5665. // If the user has just specified a '\n'
  5666. // instead of '\n\t' we have to add '\r' since
  5667. // the width will be too muchy otherwise since when
  5668. // we print we stroke the individually lines by hand.
  5669. $e = explode("\n",$aTxt);
  5670. $n = count($e);
  5671. for($i=0; $i<$n; ++$i) {
  5672. $e[$i]=str_replace("\r","",$e[$i]);
  5673. }
  5674. return implode("\n\r",$e);
  5675. }
  5676. function GetTTFBBox($aTxt,$aAngle=0) {
  5677. $bbox = @ImageTTFBBox($this->font_size,$aAngle,$this->font_file,$aTxt);
  5678. if( $bbox === false ) {
  5679. JpGraphError::RaiseL(25092,$this->font_file);
  5680. //("There is either a configuration problem with TrueType or a problem reading font file (".$this->font_file."). Make sure file exists and is in a readable place for the HTTP process. (If 'basedir' restriction is enabled in PHP then the font file must be located in the document root.). It might also be a wrongly installed FreeType library. Try uppgrading to at least FreeType 2.1.13 and recompile GD with the correct setup so it can find the new FT library.");
  5681. }
  5682. return $bbox;
  5683. }
  5684. function GetBBoxTTF($aTxt,$aAngle=0) {
  5685. // Normalize the bounding box to become a minimum
  5686. // enscribing rectangle
  5687. $aTxt = $this->AddTxtCR($aTxt);
  5688. if( !is_readable($this->font_file) ) {
  5689. JpGraphError::RaiseL(25093,$this->font_file);
  5690. //('Can not read font file ('.$this->font_file.') in call to Image::GetBBoxTTF. Please make sure that you have set a font before calling this method and that the font is installed in the TTF directory.');
  5691. }
  5692. $bbox = $this->GetTTFBBox($aTxt,$aAngle);
  5693. if( $aAngle==0 )
  5694. return $bbox;
  5695. if( $aAngle >= 0 ) {
  5696. if( $aAngle <= 90 ) { //<=0
  5697. $bbox = array($bbox[6],$bbox[1],$bbox[2],$bbox[1],
  5698. $bbox[2],$bbox[5],$bbox[6],$bbox[5]);
  5699. }
  5700. elseif( $aAngle <= 180 ) { //<= 2
  5701. $bbox = array($bbox[4],$bbox[7],$bbox[0],$bbox[7],
  5702. $bbox[0],$bbox[3],$bbox[4],$bbox[3]);
  5703. }
  5704. elseif( $aAngle <= 270 ) { //<= 3
  5705. $bbox = array($bbox[2],$bbox[5],$bbox[6],$bbox[5],
  5706. $bbox[6],$bbox[1],$bbox[2],$bbox[1]);
  5707. }
  5708. else {
  5709. $bbox = array($bbox[0],$bbox[3],$bbox[4],$bbox[3],
  5710. $bbox[4],$bbox[7],$bbox[0],$bbox[7]);
  5711. }
  5712. }
  5713. elseif( $aAngle < 0 ) {
  5714. if( $aAngle <= -270 ) { // <= -3
  5715. $bbox = array($bbox[6],$bbox[1],$bbox[2],$bbox[1],
  5716. $bbox[2],$bbox[5],$bbox[6],$bbox[5]);
  5717. }
  5718. elseif( $aAngle <= -180 ) { // <= -2
  5719. $bbox = array($bbox[0],$bbox[3],$bbox[4],$bbox[3],
  5720. $bbox[4],$bbox[7],$bbox[0],$bbox[7]);
  5721. }
  5722. elseif( $aAngle <= -90 ) { // <= -1
  5723. $bbox = array($bbox[2],$bbox[5],$bbox[6],$bbox[5],
  5724. $bbox[6],$bbox[1],$bbox[2],$bbox[1]);
  5725. }
  5726. else {
  5727. $bbox = array($bbox[0],$bbox[3],$bbox[4],$bbox[3],
  5728. $bbox[4],$bbox[7],$bbox[0],$bbox[7]);
  5729. }
  5730. }
  5731. return $bbox;
  5732. }
  5733. function GetBBoxHeight($aTxt,$aAngle=0) {
  5734. $box = $this->GetBBoxTTF($aTxt,$aAngle);
  5735. return $box[1]-$box[7]+1;
  5736. }
  5737. function GetBBoxWidth($aTxt,$aAngle=0) {
  5738. $box = $this->GetBBoxTTF($aTxt,$aAngle);
  5739. return $box[2]-$box[0]+1;
  5740. }
  5741. function _StrokeTTF($x,$y,$txt,$dir=0,$paragraph_align="left",&$aBoundingBox,$debug=false) {
  5742. // Setupo default inter line margin for paragraphs to
  5743. // 25% of the font height.
  5744. $ConstLineSpacing = 0.25 ;
  5745. // Remember the anchor point before adjustment
  5746. if( $debug ) {
  5747. $ox=$x;
  5748. $oy=$y;
  5749. }
  5750. if( !preg_match("/\n/",$txt) || ($dir>0 && preg_match("/\n/",$txt)) ) {
  5751. // Format a single line
  5752. $txt = $this->AddTxtCR($txt);
  5753. $bbox=$this->GetBBoxTTF($txt,$dir);
  5754. // Align x,y ot lower left corner of bbox
  5755. $x -= $bbox[0];
  5756. $y -= $bbox[1];
  5757. // Note to self: "topanchor" is deprecated after we changed the
  5758. // bopunding box stuff.
  5759. if( $this->text_halign=="right" || $this->text_halign=="topanchor" )
  5760. $x -= $bbox[2]-$bbox[0];
  5761. elseif( $this->text_halign=="center" ) $x -= ($bbox[2]-$bbox[0])/2;
  5762. if( $this->text_valign=="top" ) $y += abs($bbox[5])+$bbox[1];
  5763. elseif( $this->text_valign=="center" ) $y -= ($bbox[5]-$bbox[1])/2;
  5764. ImageTTFText ($this->img, $this->font_size, $dir, $x, $y,
  5765. $this->current_color,$this->font_file,$txt);
  5766. // Calculate and return the co-ordinates for the bounding box
  5767. $box=@ImageTTFBBox($this->font_size,$dir,$this->font_file,$txt);
  5768. $p1 = array();
  5769. for($i=0; $i < 4; ++$i) {
  5770. $p1[] = round($box[$i*2]+$x);
  5771. $p1[] = round($box[$i*2+1]+$y);
  5772. }
  5773. $aBoundingBox = $p1;
  5774. // Debugging code to highlight the bonding box and bounding rectangle
  5775. // For text at 0 degrees the bounding box and bounding rectangle are the
  5776. // same
  5777. if( $debug ) {
  5778. // Draw the bounding rectangle and the bounding box
  5779. $box=@ImageTTFBBox($this->font_size,$dir,$this->font_file,$txt);
  5780. $p = array();
  5781. $p1 = array();
  5782. for($i=0; $i < 4; ++$i) {
  5783. $p[] = $bbox[$i*2]+$x;
  5784. $p[] = $bbox[$i*2+1]+$y;
  5785. $p1[] = $box[$i*2]+$x;
  5786. $p1[] = $box[$i*2+1]+$y;
  5787. }
  5788. // Draw bounding box
  5789. $this->PushColor('green');
  5790. $this->Polygon($p1,true);
  5791. $this->PopColor();
  5792. // Draw bounding rectangle
  5793. $this->PushColor('darkgreen');
  5794. $this->Polygon($p,true);
  5795. $this->PopColor();
  5796. // Draw a cross at the anchor point
  5797. $this->PushColor('red');
  5798. $this->Line($ox-15,$oy,$ox+15,$oy);
  5799. $this->Line($ox,$oy-15,$ox,$oy+15);
  5800. $this->PopColor();
  5801. }
  5802. }
  5803. else {
  5804. // Format a text paragraph
  5805. $fh=$this->GetFontHeight();
  5806. // Line margin is 25% of font height
  5807. $linemargin=round($fh*$ConstLineSpacing);
  5808. $fh += $linemargin;
  5809. $w=$this->GetTextWidth($txt);
  5810. $y -= $linemargin/2;
  5811. $tmp = preg_split("/\n/",$txt);
  5812. $nl = count($tmp);
  5813. $h = $nl * $fh;
  5814. if( $this->text_halign=="right")
  5815. $x -= $dir==0 ? $w : $h;
  5816. elseif( $this->text_halign=="center" ) {
  5817. $x -= $dir==0 ? $w/2 : $h/2;
  5818. }
  5819. if( $this->text_valign=="top" )
  5820. $y += $dir==0 ? $h : $w;
  5821. elseif( $this->text_valign=="center" )
  5822. $y += $dir==0 ? $h/2 : $w/2;
  5823. // Here comes a tricky bit.
  5824. // Since we have to give the position for the string at the
  5825. // baseline this means thaht text will move slightly up
  5826. // and down depending on any of it's character descend below
  5827. // the baseline, for example a 'g'. To adjust the Y-position
  5828. // we therefore adjust the text with the baseline Y-offset
  5829. // as used for the current font and size. This will keep the
  5830. // baseline at a fixed positoned disregarding the actual
  5831. // characters in the string.
  5832. $standardbox = $this->GetTTFBBox('Gg',$dir);
  5833. $yadj = $standardbox[1];
  5834. $xadj = $standardbox[0];
  5835. $aBoundingBox = array();
  5836. for($i=0; $i < $nl; ++$i) {
  5837. $wl = $this->GetTextWidth($tmp[$i]);
  5838. $bbox = $this->GetTTFBBox($tmp[$i],$dir);
  5839. if( $paragraph_align=="left" ) {
  5840. $xl = $x;
  5841. }
  5842. elseif( $paragraph_align=="right" ) {
  5843. $xl = $x + ($w-$wl);
  5844. }
  5845. else {
  5846. // Center
  5847. $xl = $x + $w/2 - $wl/2 ;
  5848. }
  5849. $xl -= $bbox[0];
  5850. $yl = $y - $yadj;
  5851. $xl = $xl - $xadj;
  5852. ImageTTFText ($this->img, $this->font_size, $dir,
  5853. $xl, $yl-($h-$fh)+$fh*$i,
  5854. $this->current_color,$this->font_file,$tmp[$i]);
  5855. if( $debug ) {
  5856. // Draw the bounding rectangle around each line
  5857. $box=@ImageTTFBBox($this->font_size,$dir,$this->font_file,$tmp[$i]);
  5858. $p = array();
  5859. for($j=0; $j < 4; ++$j) {
  5860. $p[] = $bbox[$j*2]+$xl;
  5861. $p[] = $bbox[$j*2+1]+$yl-($h-$fh)+$fh*$i;
  5862. }
  5863. // Draw bounding rectangle
  5864. $this->PushColor('darkgreen');
  5865. $this->Polygon($p,true);
  5866. $this->PopColor();
  5867. }
  5868. }
  5869. // Get the bounding box
  5870. $bbox = $this->GetBBoxTTF($txt,$dir);
  5871. for($j=0; $j < 4; ++$j) {
  5872. $bbox[$j*2]+= round($x);
  5873. $bbox[$j*2+1]+= round($y - ($h-$fh) - $yadj);
  5874. }
  5875. $aBoundingBox = $bbox;
  5876. if( $debug ) {
  5877. // Draw a cross at the anchor point
  5878. $this->PushColor('red');
  5879. $this->Line($ox-25,$oy,$ox+25,$oy);
  5880. $this->Line($ox,$oy-25,$ox,$oy+25);
  5881. $this->PopColor();
  5882. }
  5883. }
  5884. }
  5885. function StrokeText($x,$y,$txt,$dir=0,$paragraph_align="left",$debug=false) {
  5886. $x = round($x);
  5887. $y = round($y);
  5888. // Do special language encoding
  5889. $txt = $this->langconv->Convert($txt,$this->font_family);
  5890. if( !is_numeric($dir) )
  5891. JpGraphError::RaiseL(25094);//(" Direction for text most be given as an angle between 0 and 90.");
  5892. if( $this->font_family >= FF_FONT0 && $this->font_family <= FF_FONT2+1) {
  5893. $this->_StrokeBuiltinFont($x,$y,$txt,$dir,$paragraph_align,$boundingbox,$debug);
  5894. }
  5895. elseif($this->font_family >= _FF_FIRST && $this->font_family <= _FF_LAST) {
  5896. $this->_StrokeTTF($x,$y,$txt,$dir,$paragraph_align,$boundingbox,$debug);
  5897. }
  5898. else
  5899. JpGraphError::RaiseL(25095);//(" Unknown font font family specification. ");
  5900. return $boundingbox;
  5901. }
  5902. function SetMargin($lm,$rm,$tm,$bm) {
  5903. $this->left_margin=$lm;
  5904. $this->right_margin=$rm;
  5905. $this->top_margin=$tm;
  5906. $this->bottom_margin=$bm;
  5907. $this->plotwidth=$this->width - $this->left_margin-$this->right_margin ;
  5908. $this->plotheight=$this->height - $this->top_margin-$this->bottom_margin ;
  5909. if( $this->width > 0 && $this->height > 0 ) {
  5910. if( $this->plotwidth < 0 || $this->plotheight < 0 )
  5911. JpGraphError::raise("Too small plot area. ($lm,$rm,$tm,$bm : $this->plotwidth x $this->plotheight). With the given image size and margins there is to little space left for the plot. Increase the plot size or reduce the margins.");
  5912. }
  5913. }
  5914. function SetTransparent($color) {
  5915. imagecolortransparent ($this->img,$this->rgb->allocate($color));
  5916. }
  5917. function SetColor($color,$aAlpha=0) {
  5918. $this->current_color_name = $color;
  5919. $this->current_color=$this->rgb->allocate($color,$aAlpha);
  5920. if( $this->current_color == -1 ) {
  5921. $tc=imagecolorstotal($this->img);
  5922. JpGraphError::RaiseL(25096);
  5923. //("Can't allocate any more colors. Image has already allocated maximum of <b>$tc colors</b>. This might happen if you have anti-aliasing turned on together with a background image or perhaps gradient fill since this requires many, many colors. Try to turn off anti-aliasing. If there is still a problem try downgrading the quality of the background image to use a smaller pallete to leave some entries for your graphs. You should try to limit the number of colors in your background image to 64. If there is still problem set the constant DEFINE(\"USE_APPROX_COLORS\",true); in jpgraph.php This will use approximative colors when the palette is full. Unfortunately there is not much JpGraph can do about this since the palette size is a limitation of current graphic format and what the underlying GD library suppports.");
  5924. }
  5925. return $this->current_color;
  5926. }
  5927. function PushColor($color) {
  5928. if( $color != "" ) {
  5929. $this->colorstack[$this->colorstackidx]=$this->current_color_name;
  5930. $this->colorstack[$this->colorstackidx+1]=$this->current_color;
  5931. $this->colorstackidx+=2;
  5932. $this->SetColor($color);
  5933. }
  5934. else {
  5935. JpGraphError::RaiseL(25097);//("Color specified as empty string in PushColor().");
  5936. }
  5937. }
  5938. function PopColor() {
  5939. if($this->colorstackidx<1)
  5940. JpGraphError::RaiseL(25098);//(" Negative Color stack index. Unmatched call to PopColor()");
  5941. $this->current_color=$this->colorstack[--$this->colorstackidx];
  5942. $this->current_color_name=$this->colorstack[--$this->colorstackidx];
  5943. }
  5944. // Why this duplication? Because this way we can call this method
  5945. // for any image and not only the current objsct
  5946. function AdjSat($sat) {
  5947. if( $GLOBALS['gd2'] && USE_TRUECOLOR )
  5948. return;
  5949. $this->_AdjSat($this->img,$sat);
  5950. }
  5951. function _AdjSat($img,$sat) {
  5952. $nbr = imagecolorstotal ($img);
  5953. for( $i=0; $i<$nbr; ++$i ) {
  5954. $colarr = imagecolorsforindex ($img,$i);
  5955. $rgb[0]=$colarr["red"];
  5956. $rgb[1]=$colarr["green"];
  5957. $rgb[2]=$colarr["blue"];
  5958. $rgb = $this->AdjRGBSat($rgb,$sat);
  5959. imagecolorset ($img, $i, $rgb[0], $rgb[1], $rgb[2]);
  5960. }
  5961. }
  5962. function AdjBrightContrast($bright,$contr=0) {
  5963. if( $GLOBALS['gd2'] && USE_TRUECOLOR )
  5964. return;
  5965. $this->_AdjBrightContrast($this->img,$bright,$contr);
  5966. }
  5967. function _AdjBrightContrast($img,$bright,$contr=0) {
  5968. if( $bright < -1 || $bright > 1 || $contr < -1 || $contr > 1 )
  5969. JpGraphError::RaiseL(25099);//(" Parameters for brightness and Contrast out of range [-1,1]");
  5970. $nbr = imagecolorstotal ($img);
  5971. for( $i=0; $i<$nbr; ++$i ) {
  5972. $colarr = imagecolorsforindex ($img,$i);
  5973. $r = $this->AdjRGBBrightContrast($colarr["red"],$bright,$contr);
  5974. $g = $this->AdjRGBBrightContrast($colarr["green"],$bright,$contr);
  5975. $b = $this->AdjRGBBrightContrast($colarr["blue"],$bright,$contr);
  5976. imagecolorset ($img, $i, $r, $g, $b);
  5977. }
  5978. }
  5979. // Private helper function for adj sat
  5980. // Adjust saturation for RGB array $u. $sat is a value between -1 and 1
  5981. // Note: Due to GD inability to handle true color the RGB values are only between
  5982. // 8 bit. This makes saturation quite sensitive for small increases in parameter sat.
  5983. //
  5984. // Tip: To get a grayscale picture set sat=-100, values <-100 changes the colors
  5985. // to it's complement.
  5986. //
  5987. // Implementation note: The saturation is implemented directly in the RGB space
  5988. // by adjusting the perpendicular distance between the RGB point and the "grey"
  5989. // line (1,1,1). Setting $sat>0 moves the point away from the line along the perp.
  5990. // distance and a negative value moves the point closer to the line.
  5991. // The values are truncated when the color point hits the bounding box along the
  5992. // RGB axis.
  5993. // DISCLAIMER: I'm not 100% sure this is he correct way to implement a color
  5994. // saturation function in RGB space. However, it looks ok and has the expected effect.
  5995. function AdjRGBSat($rgb,$sat) {
  5996. // TODO: Should be moved to the RGB class
  5997. // Grey vector
  5998. $v=array(1,1,1);
  5999. // Dot product
  6000. $dot = $rgb[0]*$v[0]+$rgb[1]*$v[1]+$rgb[2]*$v[2];
  6001. // Normalize dot product
  6002. $normdot = $dot/3; // dot/|v|^2
  6003. // Direction vector between $u and its projection onto $v
  6004. for($i=0; $i<3; ++$i)
  6005. $r[$i] = $rgb[$i] - $normdot*$v[$i];
  6006. // Adjustment factor so that sat==1 sets the highest RGB value to 255
  6007. if( $sat > 0 ) {
  6008. $m=0;
  6009. for( $i=0; $i<3; ++$i) {
  6010. if( sign($r[$i]) == 1 && $r[$i]>0)
  6011. $m=max($m,(255-$rgb[$i])/$r[$i]);
  6012. }
  6013. $tadj=$m;
  6014. }
  6015. else
  6016. $tadj=1;
  6017. $tadj = $tadj*$sat;
  6018. for($i=0; $i<3; ++$i) {
  6019. $un[$i] = round($rgb[$i] + $tadj*$r[$i]);
  6020. if( $un[$i]<0 ) $un[$i]=0; // Truncate color when they reach 0
  6021. if( $un[$i]>255 ) $un[$i]=255;// Avoid potential rounding error
  6022. }
  6023. return $un;
  6024. }
  6025. // Private helper function for AdjBrightContrast
  6026. function AdjRGBBrightContrast($rgb,$bright,$contr) {
  6027. // TODO: Should be moved to the RGB class
  6028. // First handle contrast, i.e change the dynamic range around grey
  6029. if( $contr <= 0 ) {
  6030. // Decrease contrast
  6031. $adj = abs($rgb-128) * (-$contr);
  6032. if( $rgb < 128 ) $rgb += $adj;
  6033. else $rgb -= $adj;
  6034. }
  6035. else { // $contr > 0
  6036. // Increase contrast
  6037. if( $rgb < 128 ) $rgb = $rgb - ($rgb * $contr);
  6038. else $rgb = $rgb + ((255-$rgb) * $contr);
  6039. }
  6040. // Add (or remove) various amount of white
  6041. $rgb += $bright*255;
  6042. $rgb=min($rgb,255);
  6043. $rgb=max($rgb,0);
  6044. return $rgb;
  6045. }
  6046. function SetLineWeight($weight) {
  6047. $this->line_weight = $weight;
  6048. }
  6049. function SetStartPoint($x,$y) {
  6050. $this->lastx=round($x);
  6051. $this->lasty=round($y);
  6052. }
  6053. function Arc($cx,$cy,$w,$h,$s,$e) {
  6054. // GD Arc doesn't like negative angles
  6055. while( $s < 0) $s += 360;
  6056. while( $e < 0) $e += 360;
  6057. imagearc($this->img,round($cx),round($cy),round($w),round($h),
  6058. $s,$e,$this->current_color);
  6059. }
  6060. function FilledArc($xc,$yc,$w,$h,$s,$e,$style="") {
  6061. if( $GLOBALS['gd2'] ) {
  6062. while( $s < 0 ) $s += 360;
  6063. while( $e < 0 ) $e += 360;
  6064. if( $style=="" )
  6065. $style=IMG_ARC_PIE;
  6066. imagefilledarc($this->img,round($xc),round($yc),round($w),round($h),
  6067. round($s),round($e),$this->current_color,$style);
  6068. return;
  6069. }
  6070. // In GD 1.x we have to do it ourself interesting enough there is surprisingly
  6071. // little difference in time between doing it PHP and using the optimised GD
  6072. // library (roughly ~20%) I had expected it to be at least 100% slower doing it
  6073. // manually with a polygon approximation in PHP.....
  6074. $fillcolor = $this->current_color_name;
  6075. $w /= 2; // We use radius in our calculations instead
  6076. $h /= 2;
  6077. // Setup the angles so we have the same conventions as the builtin
  6078. // FilledArc() which is a little bit strange if you ask me....
  6079. $s = 360-$s;
  6080. $e = 360-$e;
  6081. if( $e > $s ) {
  6082. $e = $e - 360;
  6083. $da = $s - $e;
  6084. }
  6085. $da = $s-$e;
  6086. // We use radians
  6087. $s *= M_PI/180;
  6088. $e *= M_PI/180;
  6089. $da *= M_PI/180;
  6090. // Calculate a polygon approximation
  6091. $p[0] = $xc;
  6092. $p[1] = $yc;
  6093. // Heuristic on how many polygons we need to make the
  6094. // arc look good
  6095. $numsteps = round(8 * abs($da) * ($w+$h)*($w+$h)/1500);
  6096. if( $numsteps == 0 ) return;
  6097. if( $numsteps < 7 ) $numsteps=7;
  6098. $delta = abs($da)/$numsteps;
  6099. $pa=array();
  6100. $a = $s;
  6101. for($i=1; $i<=$numsteps; ++$i ) {
  6102. $p[2*$i] = round($xc + $w*cos($a));
  6103. $p[2*$i+1] = round($yc - $h*sin($a));
  6104. //$a = $s + $i*$delta;
  6105. $a -= $delta;
  6106. $pa[2*($i-1)] = $p[2*$i];
  6107. $pa[2*($i-1)+1] = $p[2*$i+1];
  6108. }
  6109. // Get the last point at the exact ending angle to avoid
  6110. // any rounding errors.
  6111. $p[2*$i] = round($xc + $w*cos($e));
  6112. $p[2*$i+1] = round($yc - $h*sin($e));
  6113. $pa[2*($i-1)] = $p[2*$i];
  6114. $pa[2*($i-1)+1] = $p[2*$i+1];
  6115. $i++;
  6116. $p[2*$i] = $xc;
  6117. $p[2*$i+1] = $yc;
  6118. if( $fillcolor != "" ) {
  6119. $this->PushColor($fillcolor);
  6120. imagefilledpolygon($this->img,$p,count($p)/2,$this->current_color);
  6121. $this->PopColor();
  6122. }
  6123. }
  6124. function FilledCakeSlice($cx,$cy,$w,$h,$s,$e) {
  6125. $this->CakeSlice($cx,$cy,$w,$h,$s,$e,$this->current_color_name);
  6126. }
  6127. function CakeSlice($xc,$yc,$w,$h,$s,$e,$fillcolor="",$arccolor="") {
  6128. $s = round($s); $e = round($e);
  6129. $w = round($w); $h = round($h);
  6130. $xc = round($xc); $yc = round($yc);
  6131. $this->PushColor($fillcolor);
  6132. $this->FilledArc($xc,$yc,2*$w,2*$h,$s,$e);
  6133. $this->PopColor();
  6134. if( $arccolor != "" ) {
  6135. $this->PushColor($arccolor);
  6136. // We add 2 pixels to make the Arc() better aligned with the filled arc.
  6137. if( $GLOBALS['gd2'] ) {
  6138. imagefilledarc($this->img,$xc,$yc,2*$w,2*$h,$s,$e,$this->current_color,IMG_ARC_NOFILL | IMG_ARC_EDGED ) ;
  6139. }
  6140. else {
  6141. $this->Arc($xc,$yc,2*$w+2,2*$h+2,$s,$e);
  6142. $xx = $w * cos(2*M_PI - $s*M_PI/180) + $xc;
  6143. $yy = $yc - $h * sin(2*M_PI - $s*M_PI/180);
  6144. $this->Line($xc,$yc,$xx,$yy);
  6145. $xx = $w * cos(2*M_PI - $e*M_PI/180) + $xc;
  6146. $yy = $yc - $h * sin(2*M_PI - $e*M_PI/180);
  6147. $this->Line($xc,$yc,$xx,$yy);
  6148. }
  6149. $this->PopColor();
  6150. }
  6151. }
  6152. function Ellipse($xc,$yc,$w,$h) {
  6153. $this->Arc($xc,$yc,$w,$h,0,360);
  6154. }
  6155. // Breseham circle gives visually better result then using GD
  6156. // built in arc(). It takes some more time but gives better
  6157. // accuracy.
  6158. function BresenhamCircle($xc,$yc,$r) {
  6159. $d = 3-2*$r;
  6160. $x = 0;
  6161. $y = $r;
  6162. while($x<=$y) {
  6163. $this->Point($xc+$x,$yc+$y);
  6164. $this->Point($xc+$x,$yc-$y);
  6165. $this->Point($xc-$x,$yc+$y);
  6166. $this->Point($xc-$x,$yc-$y);
  6167. $this->Point($xc+$y,$yc+$x);
  6168. $this->Point($xc+$y,$yc-$x);
  6169. $this->Point($xc-$y,$yc+$x);
  6170. $this->Point($xc-$y,$yc-$x);
  6171. if( $d<0 ) $d += 4*$x+6;
  6172. else {
  6173. $d += 4*($x-$y)+10;
  6174. --$y;
  6175. }
  6176. ++$x;
  6177. }
  6178. }
  6179. function Circle($xc,$yc,$r) {
  6180. if( USE_BRESENHAM )
  6181. $this->BresenhamCircle($xc,$yc,$r);
  6182. else {
  6183. /*
  6184. // Some experimental code snippet to see if we can get a decent
  6185. // result doing a trig-circle
  6186. // Create an approximated circle with 0.05 rad resolution
  6187. $end = 2*M_PI;
  6188. $l = $r/10;
  6189. if( $l < 3 ) $l=3;
  6190. $step_size = 2*M_PI/(2*$r*M_PI/$l);
  6191. $pts = array();
  6192. $pts[] = $r + $xc;
  6193. $pts[] = $yc;
  6194. for( $a=$step_size; $a <= $end; $a += $step_size ) {
  6195. $pts[] = round($xc + $r*cos($a));
  6196. $pts[] = round($yc - $r*sin($a));
  6197. }
  6198. imagepolygon($this->img,$pts,count($pts)/2,$this->current_color);
  6199. */
  6200. $this->Arc($xc,$yc,$r*2,$r*2,0,360);
  6201. // For some reason imageellipse() isn't in GD 2.0.1, PHP 4.1.1
  6202. //imageellipse($this->img,$xc,$yc,$r,$r,$this->current_color);
  6203. }
  6204. }
  6205. function FilledCircle($xc,$yc,$r) {
  6206. if( $GLOBALS['gd2'] ) {
  6207. imagefilledellipse($this->img,round($xc),round($yc),
  6208. 2*$r,2*$r,$this->current_color);
  6209. }
  6210. else {
  6211. for( $i=1; $i < 2*$r; $i += 2 ) {
  6212. // To avoid moire patterns we have to draw some
  6213. // 1 extra "skewed" filled circles
  6214. $this->Arc($xc,$yc,$i,$i,0,360);
  6215. $this->Arc($xc,$yc,$i+1,$i,0,360);
  6216. $this->Arc($xc,$yc,$i+1,$i+1,0,360);
  6217. }
  6218. }
  6219. }
  6220. // Linear Color InterPolation
  6221. function lip($f,$t,$p) {
  6222. $p = round($p,1);
  6223. $r = $f[0] + ($t[0]-$f[0])*$p;
  6224. $g = $f[1] + ($t[1]-$f[1])*$p;
  6225. $b = $f[2] + ($t[2]-$f[2])*$p;
  6226. return array($r,$g,$b);
  6227. }
  6228. // Anti-aliased line.
  6229. // Note that this is roughly 8 times slower then a normal line!
  6230. function WuLine($x1,$y1,$x2,$y2) {
  6231. // Get foreground line color
  6232. $lc = imagecolorsforindex($this->img,$this->current_color);
  6233. $lc = array($lc["red"],$lc["green"],$lc["blue"]);
  6234. $dx = $x2-$x1;
  6235. $dy = $y2-$y1;
  6236. if( abs($dx) > abs($dy) ) {
  6237. if( $dx<0 ) {
  6238. $dx = -$dx;$dy = -$dy;
  6239. $tmp=$x2;$x2=$x1;$x1=$tmp;
  6240. $tmp=$y2;$y2=$y1;$y1=$tmp;
  6241. }
  6242. $x=$x1<<16; $y=$y1<<16;
  6243. $yinc = ($dy*65535)/$dx;
  6244. while( ($x >> 16) < $x2 ) {
  6245. $bc = @imagecolorsforindex($this->img,imagecolorat($this->img,$x>>16,$y>>16));
  6246. if( $bc <= 0 ) {
  6247. JpGraphError::RaiseL(25100);//('Problem with color palette and your GD setup. Please disable anti-aliasing or use GD2 with true-color. If you have GD2 library installed please make sure that you have set the USE_GD2 constant to true and that truecolor is enabled.');
  6248. }
  6249. $bc=array($bc["red"],$bc["green"],$bc["blue"]);
  6250. $this->SetColor($this->lip($lc,$bc,($y & 0xFFFF)/65535));
  6251. imagesetpixel($this->img,$x>>16,$y>>16,$this->current_color);
  6252. $this->SetColor($this->lip($lc,$bc,(~$y & 0xFFFF)/65535));
  6253. imagesetpixel($this->img,$x>>16,($y>>16)+1,$this->current_color);
  6254. $x += 65536; $y += $yinc;
  6255. }
  6256. }
  6257. else {
  6258. if( $dy<0 ) {
  6259. $dx = -$dx;$dy = -$dy;
  6260. $tmp=$x2;$x2=$x1;$x1=$tmp;
  6261. $tmp=$y2;$y2=$y1;$y1=$tmp;
  6262. }
  6263. $x=$x1<<16; $y=$y1<<16;
  6264. $xinc = ($dx*65535)/$dy;
  6265. while( ($y >> 16) < $y2 ) {
  6266. $bc = @imagecolorsforindex($this->img,imagecolorat($this->img,$x>>16,$y>>16));
  6267. if( $bc <= 0 ) {
  6268. JpGraphError::RaiseL(25100);//('Problem with color palette and your GD setup. Please disable anti-aliasing or use GD2 with true-color. If you have GD2 library installed please make sure that you have set the USE_GD2 constant to true and truecolor is enabled.');
  6269. }
  6270. $bc=array($bc["red"],$bc["green"],$bc["blue"]);
  6271. $this->SetColor($this->lip($lc,$bc,($x & 0xFFFF)/65535));
  6272. imagesetpixel($this->img,$x>>16,$y>>16,$this->current_color);
  6273. $this->SetColor($this->lip($lc,$bc,(~$x & 0xFFFF)/65535));
  6274. imagesetpixel($this->img,($x>>16)+1,$y>>16,$this->current_color);
  6275. $y += 65536; $x += $xinc;
  6276. }
  6277. }
  6278. $this->SetColor($lc);
  6279. imagesetpixel($this->img,$x2,$y2,$this->current_color);
  6280. imagesetpixel($this->img,$x1,$y1,$this->current_color);
  6281. }
  6282. // Set line style dashed, dotted etc
  6283. function SetLineStyle($s) {
  6284. if( is_numeric($s) ) {
  6285. if( $s<1 || $s>4 )
  6286. JpGraphError::RaiseL(25101,$s);//(" Illegal numeric argument to SetLineStyle(): ($s)");
  6287. }
  6288. elseif( is_string($s) ) {
  6289. if( $s == "solid" ) $s=1;
  6290. elseif( $s == "dotted" ) $s=2;
  6291. elseif( $s == "dashed" ) $s=3;
  6292. elseif( $s == "longdashed" ) $s=4;
  6293. else JpGraphError::RaiseL(25102,$s);//(" Illegal string argument to SetLineStyle(): $s");
  6294. }
  6295. else JpGraphError::RaiseL(25103,$s);//(" Illegal argument to SetLineStyle $s");
  6296. $this->line_style=$s;
  6297. }
  6298. // Same as Line but take the line_style into account
  6299. function StyleLine($x1,$y1,$x2,$y2) {
  6300. switch( $this->line_style ) {
  6301. case 1:// Solid
  6302. $this->Line($x1,$y1,$x2,$y2);
  6303. break;
  6304. case 2: // Dotted
  6305. $this->DashedLine($x1,$y1,$x2,$y2,1,6);
  6306. break;
  6307. case 3: // Dashed
  6308. $this->DashedLine($x1,$y1,$x2,$y2,2,4);
  6309. break;
  6310. case 4: // Longdashes
  6311. $this->DashedLine($x1,$y1,$x2,$y2,8,6);
  6312. break;
  6313. default:
  6314. JpGraphError::RaiseL(25104,$this->line_style);//(" Unknown line style: $this->line_style ");
  6315. break;
  6316. }
  6317. }
  6318. function Line($x1,$y1,$x2,$y2) {
  6319. $x1 = round($x1);
  6320. $x2 = round($x2);
  6321. $y1 = round($y1);
  6322. $y2 = round($y2);
  6323. if( $this->line_weight==0 ) return;
  6324. if( $this->use_anti_aliasing ) {
  6325. $dx = $x2-$x1;
  6326. $dy = $y2-$y1;
  6327. // Vertical, Horizontal or 45 lines don't need anti-aliasing
  6328. if( $dx!=0 && $dy!=0 && $dx!=$dy ) {
  6329. $this->WuLine($x1,$y1,$x2,$y2);
  6330. return;
  6331. }
  6332. }
  6333. if( $this->line_weight==1 ) {
  6334. imageline($this->img,$x1,$y1,$x2,$y2,$this->current_color);
  6335. }
  6336. elseif( $x1==$x2 ) { // Special case for vertical lines
  6337. imageline($this->img,$x1,$y1,$x2,$y2,$this->current_color);
  6338. $w1=floor($this->line_weight/2);
  6339. $w2=floor(($this->line_weight-1)/2);
  6340. for($i=1; $i<=$w1; ++$i)
  6341. imageline($this->img,$x1+$i,$y1,$x2+$i,$y2,$this->current_color);
  6342. for($i=1; $i<=$w2; ++$i)
  6343. imageline($this->img,$x1-$i,$y1,$x2-$i,$y2,$this->current_color);
  6344. }
  6345. elseif( $y1==$y2 ) { // Special case for horizontal lines
  6346. imageline($this->img,$x1,$y1,$x2,$y2,$this->current_color);
  6347. $w1=floor($this->line_weight/2);
  6348. $w2=floor(($this->line_weight-1)/2);
  6349. for($i=1; $i<=$w1; ++$i)
  6350. imageline($this->img,$x1,$y1+$i,$x2,$y2+$i,$this->current_color);
  6351. for($i=1; $i<=$w2; ++$i)
  6352. imageline($this->img,$x1,$y1-$i,$x2,$y2-$i,$this->current_color);
  6353. }
  6354. else { // General case with a line at an angle
  6355. $a = atan2($y1-$y2,$x2-$x1);
  6356. // Now establish some offsets from the center. This gets a little
  6357. // bit involved since we are dealing with integer functions and we
  6358. // want the apperance to be as smooth as possible and never be thicker
  6359. // then the specified width.
  6360. // We do the trig stuff to make sure that the endpoints of the line
  6361. // are perpendicular to the line itself.
  6362. $dx=(sin($a)*$this->line_weight/2);
  6363. $dy=(cos($a)*$this->line_weight/2);
  6364. $pnts = array($x2+$dx,$y2+$dy,$x2-$dx,$y2-$dy,$x1-$dx,$y1-$dy,$x1+$dx,$y1+$dy);
  6365. imagefilledpolygon($this->img,$pnts,count($pnts)/2,$this->current_color);
  6366. }
  6367. $this->lastx=$x2; $this->lasty=$y2;
  6368. }
  6369. function Polygon($p,$closed=FALSE,$fast=FALSE) {
  6370. if( $this->line_weight==0 ) return;
  6371. $n=count($p);
  6372. $oldx = $p[0];
  6373. $oldy = $p[1];
  6374. if( $fast ) {
  6375. for( $i=2; $i < $n; $i+=2 ) {
  6376. imageline($this->img,$oldx,$oldy,$p[$i],$p[$i+1],$this->current_color);
  6377. $oldx = $p[$i];
  6378. $oldy = $p[$i+1];
  6379. }
  6380. if( $closed ) {
  6381. imageline($this->img,$p[$n*2-2],$p[$n*2-1],$p[0],$p[1],$this->current_color);
  6382. }
  6383. }
  6384. else {
  6385. for( $i=2; $i < $n; $i+=2 ) {
  6386. $this->StyleLine($oldx,$oldy,$p[$i],$p[$i+1]);
  6387. $oldx = $p[$i];
  6388. $oldy = $p[$i+1];
  6389. }
  6390. }
  6391. if( $closed )
  6392. $this->Line($oldx,$oldy,$p[0],$p[1]);
  6393. }
  6394. function FilledPolygon($pts) {
  6395. $n=count($pts);
  6396. if( $n == 0 ) {
  6397. JpGraphError::RaiseL(25105);//('NULL data specified for a filled polygon. Check that your data is not NULL.');
  6398. }
  6399. for($i=0; $i < $n; ++$i)
  6400. $pts[$i] = round($pts[$i]);
  6401. imagefilledpolygon($this->img,$pts,count($pts)/2,$this->current_color);
  6402. }
  6403. function Rectangle($xl,$yu,$xr,$yl) {
  6404. $this->Polygon(array($xl,$yu,$xr,$yu,$xr,$yl,$xl,$yl,$xl,$yu));
  6405. }
  6406. function FilledRectangle($xl,$yu,$xr,$yl) {
  6407. $this->FilledPolygon(array($xl,$yu,$xr,$yu,$xr,$yl,$xl,$yl));
  6408. }
  6409. function FilledRectangle2($xl,$yu,$xr,$yl,$color1,$color2,$style=1) {
  6410. // Fill a rectangle with lines of two colors
  6411. if( $style===1 ) {
  6412. // Horizontal stripe
  6413. if( $yl < $yu ) {
  6414. $t = $yl; $yl=$yu; $yu=$t;
  6415. }
  6416. for( $y=$yu; $y <= $yl; ++$y) {
  6417. $this->SetColor($color1);
  6418. $this->Line($xl,$y,$xr,$y);
  6419. ++$y;
  6420. $this->SetColor($color2);
  6421. $this->Line($xl,$y,$xr,$y);
  6422. }
  6423. }
  6424. else {
  6425. if( $xl < $xl ) {
  6426. $t = $xl; $xl=$xr; $xr=$t;
  6427. }
  6428. for( $x=$xl; $x <= $xr; ++$x) {
  6429. $this->SetColor($color1);
  6430. $this->Line($x,$yu,$x,$yl);
  6431. ++$x;
  6432. $this->SetColor($color2);
  6433. $this->Line($x,$yu,$x,$yl);
  6434. }
  6435. }
  6436. }
  6437. function ShadowRectangle($xl,$yu,$xr,$yl,$fcolor=false,$shadow_width=3,$shadow_color=array(102,102,102)) {
  6438. // This is complicated by the fact that we must also handle the case where
  6439. // the reactangle has no fill color
  6440. $this->PushColor($shadow_color);
  6441. $this->FilledRectangle($xr-$shadow_width,$yu+$shadow_width,$xr,$yl-$shadow_width-1);
  6442. $this->FilledRectangle($xl+$shadow_width,$yl-$shadow_width,$xr,$yl);
  6443. //$this->FilledRectangle($xl+$shadow_width,$yu+$shadow_width,$xr,$yl);
  6444. $this->PopColor();
  6445. if( $fcolor==false )
  6446. $this->Rectangle($xl,$yu,$xr-$shadow_width-1,$yl-$shadow_width-1);
  6447. else {
  6448. $this->PushColor($fcolor);
  6449. $this->FilledRectangle($xl,$yu,$xr-$shadow_width-1,$yl-$shadow_width-1);
  6450. $this->PopColor();
  6451. $this->Rectangle($xl,$yu,$xr-$shadow_width-1,$yl-$shadow_width-1);
  6452. }
  6453. }
  6454. function FilledRoundedRectangle($xt,$yt,$xr,$yl,$r=5) {
  6455. if( $r==0 ) {
  6456. $this->FilledRectangle($xt,$yt,$xr,$yl);
  6457. return;
  6458. }
  6459. // To avoid overlapping fillings (which will look strange
  6460. // when alphablending is enabled) we have no choice but
  6461. // to fill the five distinct areas one by one.
  6462. // Center square
  6463. $this->FilledRectangle($xt+$r,$yt+$r,$xr-$r,$yl-$r);
  6464. // Top band
  6465. $this->FilledRectangle($xt+$r,$yt,$xr-$r,$yt+$r-1);
  6466. // Bottom band
  6467. $this->FilledRectangle($xt+$r,$yl-$r+1,$xr-$r,$yl);
  6468. // Left band
  6469. $this->FilledRectangle($xt,$yt+$r+1,$xt+$r-1,$yl-$r);
  6470. // Right band
  6471. $this->FilledRectangle($xr-$r+1,$yt+$r,$xr,$yl-$r);
  6472. // Topleft & Topright arc
  6473. $this->FilledArc($xt+$r,$yt+$r,$r*2,$r*2,180,270);
  6474. $this->FilledArc($xr-$r,$yt+$r,$r*2,$r*2,270,360);
  6475. // Bottomleft & Bottom right arc
  6476. $this->FilledArc($xt+$r,$yl-$r,$r*2,$r*2,90,180);
  6477. $this->FilledArc($xr-$r,$yl-$r,$r*2,$r*2,0,90);
  6478. }
  6479. function RoundedRectangle($xt,$yt,$xr,$yl,$r=5) {
  6480. if( $r==0 ) {
  6481. $this->Rectangle($xt,$yt,$xr,$yl);
  6482. return;
  6483. }
  6484. // Top & Bottom line
  6485. $this->Line($xt+$r,$yt,$xr-$r,$yt);
  6486. $this->Line($xt+$r,$yl,$xr-$r,$yl);
  6487. // Left & Right line
  6488. $this->Line($xt,$yt+$r,$xt,$yl-$r);
  6489. $this->Line($xr,$yt+$r,$xr,$yl-$r);
  6490. // Topleft & Topright arc
  6491. $this->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270);
  6492. $this->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360);
  6493. // Bottomleft & Bottomright arc
  6494. $this->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180);
  6495. $this->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90);
  6496. }
  6497. function FilledBevel($x1,$y1,$x2,$y2,$depth=2,$color1='white@0.4',$color2='darkgray@0.4') {
  6498. $this->FilledRectangle($x1,$y1,$x2,$y2);
  6499. $this->Bevel($x1,$y1,$x2,$y2,$depth,$color1,$color2);
  6500. }
  6501. function Bevel($x1,$y1,$x2,$y2,$depth=2,$color1='white@0.4',$color2='black@0.5') {
  6502. $this->PushColor($color1);
  6503. for( $i=0; $i < $depth; ++$i ) {
  6504. $this->Line($x1+$i,$y1+$i,$x1+$i,$y2-$i);
  6505. $this->Line($x1+$i,$y1+$i,$x2-$i,$y1+$i);
  6506. }
  6507. $this->PopColor();
  6508. $this->PushColor($color2);
  6509. for( $i=0; $i < $depth; ++$i ) {
  6510. $this->Line($x1+$i,$y2-$i,$x2-$i,$y2-$i);
  6511. $this->Line($x2-$i,$y1+$i,$x2-$i,$y2-$i-1);
  6512. }
  6513. $this->PopColor();
  6514. }
  6515. function StyleLineTo($x,$y) {
  6516. $this->StyleLine($this->lastx,$this->lasty,$x,$y);
  6517. $this->lastx=$x;
  6518. $this->lasty=$y;
  6519. }
  6520. function LineTo($x,$y) {
  6521. $this->Line($this->lastx,$this->lasty,$x,$y);
  6522. $this->lastx=$x;
  6523. $this->lasty=$y;
  6524. }
  6525. function Point($x,$y) {
  6526. imagesetpixel($this->img,round($x),round($y),$this->current_color);
  6527. }
  6528. function Fill($x,$y) {
  6529. imagefill($this->img,round($x),round($y),$this->current_color);
  6530. }
  6531. function FillToBorder($x,$y,$aBordColor) {
  6532. $bc = $this->rgb->allocate($aBordColor);
  6533. if( $bc == -1 ) {
  6534. JpGraphError::RaiseL(25106);//('Image::FillToBorder : Can not allocate more colors');
  6535. exit();
  6536. }
  6537. imagefilltoborder($this->img,round($x),round($y),$bc,$this->current_color);
  6538. }
  6539. function DashedLine($x1,$y1,$x2,$y2,$dash_length=1,$dash_space=4) {
  6540. $x1 = round($x1);
  6541. $x2 = round($x2);
  6542. $y1 = round($y1);
  6543. $y2 = round($y2);
  6544. // Code based on, but not identical to, work by Ariel Garza and James Pine
  6545. $line_length = ceil (sqrt(pow(($x2 - $x1),2) + pow(($y2 - $y1),2)) );
  6546. $dx = ($line_length) ? ($x2 - $x1) / $line_length : 0;
  6547. $dy = ($line_length) ? ($y2 - $y1) / $line_length : 0;
  6548. $lastx = $x1; $lasty = $y1;
  6549. $xmax = max($x1,$x2);
  6550. $xmin = min($x1,$x2);
  6551. $ymax = max($y1,$y2);
  6552. $ymin = min($y1,$y2);
  6553. for ($i = 0; $i < $line_length; $i += ($dash_length + $dash_space)) {
  6554. $x = ($dash_length * $dx) + $lastx;
  6555. $y = ($dash_length * $dy) + $lasty;
  6556. // The last section might overshoot so we must take a computational hit
  6557. // and check this.
  6558. if( $x>$xmax ) $x=$xmax;
  6559. if( $y>$ymax ) $y=$ymax;
  6560. if( $x<$xmin ) $x=$xmin;
  6561. if( $y<$ymin ) $y=$ymin;
  6562. $this->Line($lastx,$lasty,$x,$y);
  6563. $lastx = $x + ($dash_space * $dx);
  6564. $lasty = $y + ($dash_space * $dy);
  6565. }
  6566. }
  6567. function SetExpired($aFlg=true) {
  6568. $this->expired = $aFlg;
  6569. }
  6570. // Generate image header
  6571. function Headers() {
  6572. // In case we are running from the command line with the client version of
  6573. // PHP we can't send any headers.
  6574. $sapi = php_sapi_name();
  6575. if( $sapi == 'cli' )
  6576. return;
  6577. if( headers_sent($file,$lineno) ) {
  6578. $file=basename($file);
  6579. $t = new ErrMsgText();
  6580. $msg = $t->Get(10,$file,$lineno);
  6581. die($msg);
  6582. }
  6583. if ($this->expired) {
  6584. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  6585. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
  6586. header("Cache-Control: no-cache, must-revalidate");
  6587. header("Pragma: no-cache");
  6588. }
  6589. header("Content-type: image/$this->img_format");
  6590. }
  6591. // Adjust image quality for formats that allow this
  6592. function SetQuality($q) {
  6593. $this->quality = $q;
  6594. }
  6595. // Stream image to browser or to file
  6596. function Stream($aFile="") {
  6597. $func="image".$this->img_format;
  6598. if( $this->img_format=="jpeg" && $this->quality != null ) {
  6599. $res = @$func($this->img,$aFile,$this->quality);
  6600. }
  6601. else {
  6602. if( $aFile != "" ) {
  6603. $res = @$func($this->img,$aFile);
  6604. if( !$res )
  6605. JpGraphError::RaiseL(25107,$aFile);//("Can't write to file '$aFile'. Check that the process running PHP has enough permission.");
  6606. }
  6607. else {
  6608. $res = @$func($this->img);
  6609. if( !$res )
  6610. JpGraphError::RaiseL(25108);//("Can't stream image. This is most likely due to a faulty PHP/GD setup. Try to recompile PHP and use the built-in GD library that comes with PHP.");
  6611. }
  6612. }
  6613. }
  6614. // Clear resource tide up by image
  6615. function Destroy() {
  6616. imagedestroy($this->img);
  6617. }
  6618. // Specify image format. Note depending on your installation
  6619. // of PHP not all formats may be supported.
  6620. function SetImgFormat($aFormat,$aQuality=75) {
  6621. $this->quality = $aQuality;
  6622. $aFormat = strtolower($aFormat);
  6623. $tst = true;
  6624. $supported = imagetypes();
  6625. if( $aFormat=="auto" ) {
  6626. if( $supported & IMG_PNG )
  6627. $this->img_format="png";
  6628. elseif( $supported & IMG_JPG )
  6629. $this->img_format="jpeg";
  6630. elseif( $supported & IMG_GIF )
  6631. $this->img_format="gif";
  6632. else
  6633. JpGraphError::RaiseL(25109);//("Your PHP (and GD-lib) installation does not appear to support any known graphic formats. You need to first make sure GD is compiled as a module to PHP. If you also want to use JPEG images you must get the JPEG library. Please see the PHP docs for details.");
  6634. return true;
  6635. }
  6636. else {
  6637. if( $aFormat=="jpeg" || $aFormat=="png" || $aFormat=="gif" ) {
  6638. if( $aFormat=="jpeg" && !($supported & IMG_JPG) )
  6639. $tst=false;
  6640. elseif( $aFormat=="png" && !($supported & IMG_PNG) )
  6641. $tst=false;
  6642. elseif( $aFormat=="gif" && !($supported & IMG_GIF) )
  6643. $tst=false;
  6644. else {
  6645. $this->img_format=$aFormat;
  6646. return true;
  6647. }
  6648. }
  6649. else
  6650. $tst=false;
  6651. if( !$tst )
  6652. JpGraphError::RaiseL(25110,$aFormat);//(" Your PHP installation does not support the chosen graphic format: $aFormat");
  6653. }
  6654. }
  6655. } // CLASS
  6656. //===================================================
  6657. // CLASS RotImage
  6658. // Description: Exactly as Image but draws the image at
  6659. // a specified angle around a specified rotation point.
  6660. //===================================================
  6661. class RotImage extends Image {
  6662. var $m=array();
  6663. var $a=0;
  6664. var $dx=0,$dy=0,$transx=0,$transy=0;
  6665. function RotImage($aWidth,$aHeight,$a=0,$aFormat=DEFAULT_GFORMAT) {
  6666. $this->Image($aWidth,$aHeight,$aFormat);
  6667. $this->dx=$this->left_margin+$this->plotwidth/2;
  6668. $this->dy=$this->top_margin+$this->plotheight/2;
  6669. $this->SetAngle($a);
  6670. }
  6671. function SetCenter($dx,$dy) {
  6672. $old_dx = $this->dx;
  6673. $old_dy = $this->dy;
  6674. $this->dx=$dx;
  6675. $this->dy=$dy;
  6676. $this->SetAngle($this->a);
  6677. return array($old_dx,$old_dy);
  6678. }
  6679. function SetTranslation($dx,$dy) {
  6680. $old = array($this->transx,$this->transy);
  6681. $this->transx = $dx;
  6682. $this->transy = $dy;
  6683. return $old;
  6684. }
  6685. function UpdateRotMatrice() {
  6686. $a = $this->a;
  6687. $a *= M_PI/180;
  6688. $sa=sin($a); $ca=cos($a);
  6689. // Create the rotation matrix
  6690. $this->m[0][0] = $ca;
  6691. $this->m[0][1] = -$sa;
  6692. $this->m[0][2] = $this->dx*(1-$ca) + $sa*$this->dy ;
  6693. $this->m[1][0] = $sa;
  6694. $this->m[1][1] = $ca;
  6695. $this->m[1][2] = $this->dy*(1-$ca) - $sa*$this->dx ;
  6696. }
  6697. function SetAngle($a) {
  6698. $tmp = $this->a;
  6699. $this->a = $a;
  6700. $this->UpdateRotMatrice();
  6701. return $tmp;
  6702. }
  6703. function Circle($xc,$yc,$r) {
  6704. // Circle get's rotated through the Arc() call
  6705. // made in the parent class
  6706. parent::Circle($xc,$yc,$r);
  6707. }
  6708. function FilledCircle($xc,$yc,$r) {
  6709. // If we use GD1 then Image::FilledCircle will use a
  6710. // call to Arc so it will get rotated through the Arc
  6711. // call.
  6712. if( $GLOBALS['gd2'] ) {
  6713. list($xc,$yc) = $this->Rotate($xc,$yc);
  6714. }
  6715. parent::FilledCircle($xc,$yc,$r);
  6716. }
  6717. function Arc($xc,$yc,$w,$h,$s,$e) {
  6718. list($xc,$yc) = $this->Rotate($xc,$yc);
  6719. $s += $this->a;
  6720. $e += $this->a;
  6721. parent::Arc($xc,$yc,$w,$h,$s,$e);
  6722. }
  6723. function FilledArc($xc,$yc,$w,$h,$s,$e) {
  6724. list($xc,$yc) = $this->Rotate($xc,$yc);
  6725. $s += $this->a;
  6726. $e += $this->a;
  6727. parent::FilledArc($xc,$yc,$w,$h,$s,$e);
  6728. }
  6729. function SetMargin($lm,$rm,$tm,$bm) {
  6730. parent::SetMargin($lm,$rm,$tm,$bm);
  6731. $this->dx=$this->left_margin+$this->plotwidth/2;
  6732. $this->dy=$this->top_margin+$this->plotheight/2;
  6733. $this->UpdateRotMatrice();
  6734. }
  6735. function Rotate($x,$y) {
  6736. // Optimization. Ignore rotation if Angle==0 || ANgle==360
  6737. if( $this->a == 0 || $this->a == 360 ) {
  6738. return array($x + $this->transx, $y + $this->transy );
  6739. }
  6740. else {
  6741. $x1=round($this->m[0][0]*$x + $this->m[0][1]*$y,1) + $this->m[0][2] + $this->transx;
  6742. $y1=round($this->m[1][0]*$x + $this->m[1][1]*$y,1) + $this->m[1][2] + $this->transy;
  6743. return array($x1,$y1);
  6744. }
  6745. }
  6746. function CopyMerge($fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth=-1,$fromHeight=-1,$aMix=100) {
  6747. list($toX,$toY) = $this->Rotate($toX,$toY);
  6748. parent::CopyMerge($fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth,$fromHeight,$aMix);
  6749. }
  6750. function ArrRotate($pnts) {
  6751. $n = count($pnts)-1;
  6752. for($i=0; $i < $n; $i+=2) {
  6753. list ($x,$y) = $this->Rotate($pnts[$i],$pnts[$i+1]);
  6754. $pnts[$i] = $x; $pnts[$i+1] = $y;
  6755. }
  6756. return $pnts;
  6757. }
  6758. function Line($x1,$y1,$x2,$y2) {
  6759. list($x1,$y1) = $this->Rotate($x1,$y1);
  6760. list($x2,$y2) = $this->Rotate($x2,$y2);
  6761. parent::Line($x1,$y1,$x2,$y2);
  6762. }
  6763. function Rectangle($x1,$y1,$x2,$y2) {
  6764. // Rectangle uses Line() so it will be rotated through that call
  6765. parent::Rectangle($x1,$y1,$x2,$y2);
  6766. }
  6767. function FilledRectangle($x1,$y1,$x2,$y2) {
  6768. if( $y1==$y2 || $x1==$x2 )
  6769. $this->Line($x1,$y1,$x2,$y2);
  6770. else
  6771. $this->FilledPolygon(array($x1,$y1,$x2,$y1,$x2,$y2,$x1,$y2));
  6772. }
  6773. function Polygon($pnts,$closed=FALSE,$fast=false) {
  6774. //Polygon uses Line() so it will be rotated through that call
  6775. parent::Polygon($pnts,$closed,$fast);
  6776. }
  6777. function FilledPolygon($pnts) {
  6778. parent::FilledPolygon($this->ArrRotate($pnts));
  6779. }
  6780. function Point($x,$y) {
  6781. list($xp,$yp) = $this->Rotate($x,$y);
  6782. parent::Point($xp,$yp);
  6783. }
  6784. function StrokeText($x,$y,$txt,$dir=0,$paragraph_align="left",$debug=false) {
  6785. list($xp,$yp) = $this->Rotate($x,$y);
  6786. return parent::StrokeText($xp,$yp,$txt,$dir,$paragraph_align,$debug);
  6787. }
  6788. }
  6789. //===================================================
  6790. // CLASS ImgStreamCache
  6791. // Description: Handle caching of graphs to files
  6792. //===================================================
  6793. class ImgStreamCache {
  6794. var $cache_dir;
  6795. var $img=null;
  6796. var $timeout=0; // Infinite timeout
  6797. //---------------
  6798. // CONSTRUCTOR
  6799. function ImgStreamCache(&$aImg, $aCacheDir=CACHE_DIR) {
  6800. $this->img = &$aImg;
  6801. $this->cache_dir = $aCacheDir;
  6802. }
  6803. //---------------
  6804. // PUBLIC METHODS
  6805. // Specify a timeout (in minutes) for the file. If the file is older then the
  6806. // timeout value it will be overwritten with a newer version.
  6807. // If timeout is set to 0 this is the same as infinite large timeout and if
  6808. // timeout is set to -1 this is the same as infinite small timeout
  6809. function SetTimeout($aTimeout) {
  6810. $this->timeout=$aTimeout;
  6811. }
  6812. // Output image to browser and also write it to the cache
  6813. function PutAndStream(&$aImage,$aCacheFileName,$aInline,$aStrokeFileName) {
  6814. // Some debugging code to brand the image with numbe of colors
  6815. // used
  6816. GLOBAL $gJpgBrandTiming;
  6817. if( $gJpgBrandTiming ) {
  6818. global $tim;
  6819. $t=$tim->Pop()/1000.0;
  6820. $c=$aImage->SetColor("black");
  6821. $t=sprintf(BRAND_TIME_FORMAT,round($t,3));
  6822. imagestring($this->img->img,2,5,$this->img->height-20,$t,$c);
  6823. }
  6824. // Check if we should stroke the image to an arbitrary file
  6825. if( _FORCE_IMGTOFILE ) {
  6826. $aStrokeFileName = _FORCE_IMGDIR.GenImgName();
  6827. }
  6828. if( $aStrokeFileName!="" ) {
  6829. if( $aStrokeFileName == "auto" )
  6830. $aStrokeFileName = GenImgName();
  6831. if( file_exists($aStrokeFileName) ) {
  6832. // Delete the old file
  6833. if( !@unlink($aStrokeFileName) )
  6834. JpGraphError::RaiseL(25111,$aStrokeFileName);//(" Can't delete cached image $aStrokeFileName. Permission problem?");
  6835. }
  6836. $aImage->Stream($aStrokeFileName);
  6837. return;
  6838. }
  6839. if( $aCacheFileName != "" && USE_CACHE) {
  6840. $aCacheFileName = $this->cache_dir . $aCacheFileName;
  6841. if( file_exists($aCacheFileName) ) {
  6842. if( !$aInline ) {
  6843. // If we are generating image off-line (just writing to the cache)
  6844. // and the file exists and is still valid (no timeout)
  6845. // then do nothing, just return.
  6846. $diff=time()-filemtime($aCacheFileName);
  6847. if( $diff < 0 )
  6848. JpGraphError::RaiseL(25112,$aCacheFileName);//(" Cached imagefile ($aCacheFileName) has file date in the future!!");
  6849. if( $this->timeout>0 && ($diff <= $this->timeout*60) )
  6850. return;
  6851. }
  6852. if( !@unlink($aCacheFileName) )
  6853. JpGraphError::RaiseL(25113,$aStrokeFileName);//(" Can't delete cached image $aStrokeFileName. Permission problem?");
  6854. $aImage->Stream($aCacheFileName);
  6855. }
  6856. else {
  6857. $this->MakeDirs(dirname($aCacheFileName));
  6858. if( !is_writeable(dirname($aCacheFileName)) ) {
  6859. JpGraphError::RaiseL(25114,$aCacheFileName);//('PHP has not enough permissions to write to the cache file '.$aCacheFileName.'. Please make sure that the user running PHP has write permission for this file if you wan to use the cache system with JpGraph.');
  6860. }
  6861. $aImage->Stream($aCacheFileName);
  6862. }
  6863. $res=true;
  6864. // Set group to specified
  6865. if( CACHE_FILE_GROUP != "" )
  6866. $res = @chgrp($aCacheFileName,CACHE_FILE_GROUP);
  6867. if( CACHE_FILE_MOD != "" )
  6868. $res = @chmod($aCacheFileName,CACHE_FILE_MOD);
  6869. if( !$res )
  6870. JpGraphError::RaiseL(25115,$aStrokeFileName);//(" Can't set permission for cached image $aStrokeFileName. Permission problem?");
  6871. $aImage->Destroy();
  6872. if( $aInline ) {
  6873. if ($fh = @fopen($aCacheFileName, "rb") ) {
  6874. $this->img->Headers();
  6875. fpassthru($fh);
  6876. return;
  6877. }
  6878. else
  6879. JpGraphError::RaiseL(25116,$aFile);//(" Cant open file from cache [$aFile]");
  6880. }
  6881. }
  6882. elseif( $aInline ) {
  6883. $this->img->Headers();
  6884. $aImage->Stream();
  6885. return;
  6886. }
  6887. }
  6888. // Check if a given image is in cache and in that case
  6889. // pass it directly on to web browser. Return false if the
  6890. // image file doesn't exist or exists but is to old
  6891. function GetAndStream($aCacheFileName) {
  6892. $aCacheFileName = $this->cache_dir.$aCacheFileName;
  6893. if ( USE_CACHE && file_exists($aCacheFileName) && $this->timeout>=0 ) {
  6894. $diff=time()-filemtime($aCacheFileName);
  6895. if( $this->timeout>0 && ($diff > $this->timeout*60) ) {
  6896. return false;
  6897. }
  6898. else {
  6899. if ($fh = @fopen($aCacheFileName, "rb")) {
  6900. $this->img->Headers();
  6901. fpassthru($fh);
  6902. return true;
  6903. }
  6904. else
  6905. JpGraphError::RaiseL(25117,$aCacheFileName);//(" Can't open cached image \"$aCacheFileName\" for reading.");
  6906. }
  6907. }
  6908. return false;
  6909. }
  6910. //---------------
  6911. // PRIVATE METHODS
  6912. // Create all necessary directories in a path
  6913. function MakeDirs($aFile) {
  6914. $dirs = array();
  6915. while ( !(file_exists($aFile)) ) {
  6916. $dirs[] = $aFile;
  6917. $aFile = dirname($aFile);
  6918. }
  6919. for ($i = sizeof($dirs)-1; $i>=0; $i--) {
  6920. if(! @mkdir($dirs[$i],0777) )
  6921. JpGraphError::RaiseL(25118,$aFile);//(" Can't create directory $aFile. Make sure PHP has write permission to this directory.");
  6922. // We also specify mode here after we have changed group.
  6923. // This is necessary if Apache user doesn't belong the
  6924. // default group and hence can't specify group permission
  6925. // in the previous mkdir() call
  6926. if( CACHE_FILE_GROUP != "" ) {
  6927. $res=true;
  6928. $res =@chgrp($dirs[$i],CACHE_FILE_GROUP);
  6929. $res &= @chmod($dirs[$i],0777);
  6930. if( !$res )
  6931. JpGraphError::RaiseL(25119,$aFile);//(" Can't set permissions for $aFile. Permission problems?");
  6932. }
  6933. }
  6934. return true;
  6935. }
  6936. } // CLASS Cache
  6937. //===================================================
  6938. // CLASS Legend
  6939. // Description: Responsible for drawing the box containing
  6940. // all the legend text for the graph
  6941. //===================================================
  6942. DEFINE('_DEFAULT_LPM_SIZE',8);
  6943. class Legend {
  6944. var $color=array(0,0,0); // Default fram color
  6945. var $fill_color=array(235,235,235); // Default fill color
  6946. var $shadow=true; // Shadow around legend "box"
  6947. var $shadow_color='gray';
  6948. var $txtcol=array();
  6949. var $mark_abs_hsize=_DEFAULT_LPM_SIZE, $mark_abs_vsize=_DEFAULT_LPM_SIZE;
  6950. var $xmargin=5,$ymargin=3,$shadow_width=2;
  6951. var $xlmargin=2, $ylmargin='';
  6952. var $xpos=0.05, $ypos=0.15, $xabspos=-1, $yabspos=-1;
  6953. var $halign="right", $valign="top";
  6954. var $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
  6955. var $font_color='black';
  6956. var $hide=false,$layout_n=1;
  6957. var $weight=1,$frameweight=1;
  6958. var $csimareas='';
  6959. var $reverse = false ;
  6960. //---------------
  6961. // CONSTRUCTOR
  6962. function Legend() {
  6963. // Empty
  6964. }
  6965. //---------------
  6966. // PUBLIC METHODS
  6967. function Hide($aHide=true) {
  6968. $this->hide=$aHide;
  6969. }
  6970. function SetHColMargin($aXMarg) {
  6971. $this->xmargin = $aXMarg;
  6972. }
  6973. function SetVColMargin($aSpacing) {
  6974. $this->ymargin = $aSpacing ;
  6975. }
  6976. function SetLeftMargin($aXMarg) {
  6977. $this->xlmargin = $aXMarg;
  6978. }
  6979. // Synonym
  6980. function SetLineSpacing($aSpacing) {
  6981. $this->ymargin = $aSpacing ;
  6982. }
  6983. function SetShadow($aShow='gray',$aWidth=2) {
  6984. if( is_string($aShow) ) {
  6985. $this->shadow_color = $aShow;
  6986. $this->shadow=true;
  6987. }
  6988. else
  6989. $this->shadow=$aShow;
  6990. $this->shadow_width=$aWidth;
  6991. }
  6992. function SetMarkAbsSize($aSize) {
  6993. $this->mark_abs_vsize = $aSize ;
  6994. $this->mark_abs_hsize = $aSize ;
  6995. }
  6996. function SetMarkAbsVSize($aSize) {
  6997. $this->mark_abs_vsize = $aSize ;
  6998. }
  6999. function SetMarkAbsHSize($aSize) {
  7000. $this->mark_abs_hsize = $aSize ;
  7001. }
  7002. function SetLineWeight($aWeight) {
  7003. $this->weight = $aWeight;
  7004. }
  7005. function SetFrameWeight($aWeight) {
  7006. $this->frameweight = $aWeight;
  7007. }
  7008. function SetLayout($aDirection=LEGEND_VERT) {
  7009. $this->layout_n = $aDirection==LEGEND_VERT ? 1 : 99 ;
  7010. }
  7011. function SetColumns($aCols) {
  7012. $this->layout_n = $aCols ;
  7013. }
  7014. function SetReverse($f=true) {
  7015. $this->reverse = $f ;
  7016. }
  7017. // Set color on frame around box
  7018. function SetColor($aFontColor,$aColor='black') {
  7019. $this->font_color=$aFontColor;
  7020. $this->color=$aColor;
  7021. }
  7022. function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
  7023. $this->font_family = $aFamily;
  7024. $this->font_style = $aStyle;
  7025. $this->font_size = $aSize;
  7026. }
  7027. function SetPos($aX,$aY,$aHAlign="right",$aVAlign="top") {
  7028. $this->Pos($aX,$aY,$aHAlign,$aVAlign);
  7029. }
  7030. function SetAbsPos($aX,$aY,$aHAlign="right",$aVAlign="top") {
  7031. $this->xabspos=$aX;
  7032. $this->yabspos=$aY;
  7033. $this->halign=$aHAlign;
  7034. $this->valign=$aVAlign;
  7035. }
  7036. function Pos($aX,$aY,$aHAlign="right",$aVAlign="top") {
  7037. if( !($aX<1 && $aY<1) )
  7038. JpGraphError::RaiseL(25120);//(" Position for legend must be given as percentage in range 0-1");
  7039. $this->xpos=$aX;
  7040. $this->ypos=$aY;
  7041. $this->halign=$aHAlign;
  7042. $this->valign=$aVAlign;
  7043. }
  7044. function SetFillColor($aColor) {
  7045. $this->fill_color=$aColor;
  7046. }
  7047. function Add($aTxt,$aColor,$aPlotmark="",$aLinestyle=0,$csimtarget="",$csimalt="") {
  7048. $this->txtcol[]=array($aTxt,$aColor,$aPlotmark,$aLinestyle,$csimtarget,$csimalt);
  7049. }
  7050. function GetCSIMAreas() {
  7051. return $this->csimareas;
  7052. }
  7053. function Stroke(&$aImg) {
  7054. // Constant
  7055. $fillBoxFrameWeight=1;
  7056. if( $this->hide ) return;
  7057. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  7058. if( $this->reverse ) {
  7059. $this->txtcol = array_reverse($this->txtcol);
  7060. }
  7061. $n=count($this->txtcol);
  7062. if( $n == 0 ) return;
  7063. // Find out the max width and height of each column to be able
  7064. // to size the legend box.
  7065. $numcolumns = ($n > $this->layout_n ? $this->layout_n : $n);
  7066. for( $i=0; $i < $numcolumns; ++$i ) {
  7067. $colwidth[$i] = $aImg->GetTextWidth($this->txtcol[$i][0]) +
  7068. 2*$this->xmargin + 2*$this->mark_abs_hsize;
  7069. $colheight[$i] = 0;
  7070. }
  7071. // Find our maximum height in each row
  7072. $rows = 0 ; $rowheight[0] = 0;
  7073. for( $i=0; $i < $n; ++$i ) {
  7074. $h = max($this->mark_abs_vsize,$aImg->GetTextHeight($this->txtcol[$i][0]))+$this->ymargin;
  7075. if( $i % $numcolumns == 0 ) {
  7076. $rows++;
  7077. $rowheight[$rows-1] = 0;
  7078. }
  7079. $rowheight[$rows-1] = max($rowheight[$rows-1],$h);
  7080. }
  7081. $abs_height = 0;
  7082. for( $i=0; $i < $rows; ++$i ) {
  7083. $abs_height += $rowheight[$i] ;
  7084. }
  7085. // Make sure that the height is at least as high as mark size + ymargin
  7086. $abs_height = max($abs_height,$this->mark_abs_vsize);
  7087. // We add 3 extra pixels height to compensate for the difficult in
  7088. // calculating font height
  7089. $abs_height += $this->ymargin+3;
  7090. // Find out the maximum width in each column
  7091. for( $i=$numcolumns; $i < $n; ++$i ) {
  7092. $colwidth[$i % $numcolumns] = max(
  7093. $aImg->GetTextWidth($this->txtcol[$i][0])+2*$this->xmargin+2*$this->mark_abs_hsize,$colwidth[$i % $numcolumns]);
  7094. }
  7095. // Get the total width
  7096. $mtw = 0;
  7097. for( $i=0; $i < $numcolumns; ++$i ) {
  7098. $mtw += $colwidth[$i] ;
  7099. }
  7100. // Find out maximum width we need for legend box
  7101. $abs_width = $mtw+$this->xlmargin;
  7102. if( $this->xabspos === -1 && $this->yabspos === -1 ) {
  7103. $this->xabspos = $this->xpos*$aImg->width ;
  7104. $this->yabspos = $this->ypos*$aImg->height ;
  7105. }
  7106. // Positioning of the legend box
  7107. if( $this->halign=="left" )
  7108. $xp = $this->xabspos;
  7109. elseif( $this->halign=="center" )
  7110. $xp = $this->xabspos - $abs_width/2;
  7111. else
  7112. $xp = $aImg->width - $this->xabspos - $abs_width;
  7113. $yp=$this->yabspos;
  7114. if( $this->valign=="center" )
  7115. $yp-=$abs_height/2;
  7116. elseif( $this->valign=="bottom" )
  7117. $yp-=$abs_height;
  7118. // Stroke legend box
  7119. $aImg->SetColor($this->color);
  7120. $aImg->SetLineWeight($this->frameweight);
  7121. $aImg->SetLineStyle('solid');
  7122. if( $this->shadow )
  7123. $aImg->ShadowRectangle($xp,$yp,$xp+$abs_width+$this->shadow_width,
  7124. $yp+$abs_height+$this->shadow_width,
  7125. $this->fill_color,$this->shadow_width,$this->shadow_color);
  7126. else {
  7127. $aImg->SetColor($this->fill_color);
  7128. $aImg->FilledRectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
  7129. $aImg->SetColor($this->color);
  7130. $aImg->Rectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
  7131. }
  7132. // x1,y1 is the position for the legend mark
  7133. $x1=$xp+$this->mark_abs_hsize+$this->xlmargin;
  7134. $y1=$yp + $this->ymargin;
  7135. $f2 = round($aImg->GetTextHeight('X')/2);
  7136. $grad = new Gradient($aImg);
  7137. $patternFactory = null;
  7138. // Now stroke each legend in turn
  7139. // Each plot has added the following information to the legend
  7140. // p[0] = Legend text
  7141. // p[1] = Color,
  7142. // p[2] = For markers a reference to the PlotMark object
  7143. // p[3] = For lines the line style, for gradient the negative gradient style
  7144. // p[4] = CSIM target
  7145. // p[5] = CSIM Alt text
  7146. $i = 1 ; $row = 0;
  7147. foreach($this->txtcol as $p) {
  7148. // STROKE DEBUG BOX
  7149. if( _JPG_DEBUG ) {
  7150. $aImg->SetLineWeight(1);
  7151. $aImg->SetColor('red');
  7152. $aImg->SetLineStyle('solid');
  7153. $aImg->Rectangle($xp,$y1,$xp+$abs_width,$y1+$rowheight[$row]);
  7154. }
  7155. $aImg->SetLineWeight($this->weight);
  7156. $x1 = round($x1); $y1=round($y1);
  7157. if ( $p[2] != "" && $p[2]->GetType() > -1 ) {
  7158. // Make a plot mark legend
  7159. $aImg->SetColor($p[1]);
  7160. if( is_string($p[3]) || $p[3]>0 ) {
  7161. $aImg->SetLineStyle($p[3]);
  7162. $aImg->StyleLine($x1-$this->mark_abs_hsize,$y1+$f2,$x1+$this->mark_abs_hsize,$y1+$f2);
  7163. }
  7164. // Stroke a mark with the standard size
  7165. // (As long as it is not an image mark )
  7166. if( $p[2]->GetType() != MARK_IMG ) {
  7167. $p[2]->iFormatCallback = '';
  7168. // Since size for circles is specified as the radius
  7169. // this means that we must half the size to make the total
  7170. // width behave as the other marks
  7171. if( $p[2]->GetType() == MARK_FILLEDCIRCLE || $p[2]->GetType() == MARK_CIRCLE ) {
  7172. $p[2]->SetSize(min($this->mark_abs_vsize,$this->mark_abs_hsize)/2);
  7173. $p[2]->Stroke($aImg,$x1,$y1+$f2);
  7174. }
  7175. else {
  7176. $p[2]->SetSize(min($this->mark_abs_vsize,$this->mark_abs_hsize));
  7177. $p[2]->Stroke($aImg,$x1,$y1+$f2);
  7178. }
  7179. }
  7180. }
  7181. elseif ( $p[2] != "" && (is_string($p[3]) || $p[3]>0 ) ) {
  7182. // Draw a styled line
  7183. $aImg->SetColor($p[1]);
  7184. $aImg->SetLineStyle($p[3]);
  7185. $aImg->StyleLine($x1-1,$y1+$f2,$x1+$this->mark_abs_hsize,$y1+$f2);
  7186. $aImg->StyleLine($x1-1,$y1+$f2+1,$x1+$this->mark_abs_hsize,$y1+$f2+1);
  7187. }
  7188. else {
  7189. // Draw a colored box
  7190. $color = $p[1] ;
  7191. // We make boxes slightly larger to better show
  7192. $boxsize = min($this->mark_abs_vsize,$this->mark_abs_hsize) + 2 ;
  7193. $ym = round($y1 + $f2 - $boxsize/2);
  7194. // We either need to plot a gradient or a
  7195. // pattern. To differentiate we use a kludge.
  7196. // Patterns have a p[3] value of < -100
  7197. if( $p[3] < -100 ) {
  7198. // p[1][0] == iPattern, p[1][1] == iPatternColor, p[1][2] == iPatternDensity
  7199. if( $patternFactory == null ) {
  7200. $patternFactory = new RectPatternFactory();
  7201. }
  7202. $prect = $patternFactory->Create($p[1][0],$p[1][1],1);
  7203. $prect->SetBackground($p[1][3]);
  7204. $prect->SetDensity($p[1][2]+1);
  7205. $prect->SetPos(new Rectangle($x1,$ym,$boxsize,$boxsize));
  7206. $prect->Stroke($aImg);
  7207. $prect=null;
  7208. }
  7209. else {
  7210. if( is_array($color) && count($color)==2 ) {
  7211. // The client want a gradient color
  7212. $grad->FilledRectangle($x1,$ym,
  7213. $x1+$boxsize,$ym+$boxsize,
  7214. $color[0],$color[1],-$p[3]);
  7215. }
  7216. else {
  7217. $aImg->SetColor($p[1]);
  7218. $aImg->FilledRectangle($x1,$ym,$x1+$boxsize,$ym+$boxsize);
  7219. }
  7220. $aImg->SetColor($this->color);
  7221. $aImg->SetLineWeight($fillBoxFrameWeight);
  7222. $aImg->Rectangle($x1,$ym,$x1+$boxsize,$ym+$boxsize);
  7223. }
  7224. }
  7225. $aImg->SetColor($this->font_color);
  7226. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  7227. $aImg->SetTextAlign("left","top");
  7228. $aImg->StrokeText(round($x1+$this->mark_abs_hsize+$this->xmargin),$y1,$p[0]);
  7229. // Add CSIM for Legend if defined
  7230. if( $p[4] != "" ) {
  7231. $xe = $x1 + $this->xmargin+$this->mark_abs_hsize+$aImg->GetTextWidth($p[0]);
  7232. $ye = $y1 + max($this->mark_abs_vsize,$aImg->GetTextHeight($p[0]));
  7233. $coords = "$x1,$y1,$xe,$y1,$xe,$ye,$x1,$ye";
  7234. if( ! empty($p[4]) ) {
  7235. $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".$p[4]."\"";
  7236. if( !empty($p[5]) ) {
  7237. $tmp=sprintf($p[5],$p[0]);
  7238. $this->csimareas .= " title=\"$tmp\"";
  7239. }
  7240. $this->csimareas .= " alt=\"\" />\n";
  7241. }
  7242. }
  7243. if( $i >= $this->layout_n ) {
  7244. $x1 = $xp+$this->mark_abs_hsize+$this->xlmargin;
  7245. $y1 += $rowheight[$row++];
  7246. $i = 1;
  7247. }
  7248. else {
  7249. $x1 += $colwidth[($i-1) % $numcolumns] ;
  7250. ++$i;
  7251. }
  7252. }
  7253. }
  7254. } // Class
  7255. //===================================================
  7256. // CLASS DisplayValue
  7257. // Description: Used to print data values at data points
  7258. //===================================================
  7259. class DisplayValue {
  7260. var $show=false,$format="%.1f",$negformat="";
  7261. var $iFormCallback='';
  7262. var $angle=0;
  7263. var $ff=FF_FONT1,$fs=FS_NORMAL,$fsize=10;
  7264. var $color="navy",$negcolor="";
  7265. var $margin=5,$valign="",$halign="center";
  7266. var $iHideZero=false;
  7267. function Show($aFlag=true) {
  7268. $this->show=$aFlag;
  7269. }
  7270. function SetColor($aColor,$aNegcolor="") {
  7271. $this->color = $aColor;
  7272. $this->negcolor = $aNegcolor;
  7273. }
  7274. function SetFont($aFontFamily,$aFontStyle=FS_NORMAL,$aFontSize=10) {
  7275. $this->ff=$aFontFamily;
  7276. $this->fs=$aFontStyle;
  7277. $this->fsize=$aFontSize;
  7278. }
  7279. function SetMargin($aMargin) {
  7280. $this->margin = $aMargin;
  7281. }
  7282. function SetAngle($aAngle) {
  7283. $this->angle = $aAngle;
  7284. }
  7285. function SetAlign($aHAlign,$aVAlign='') {
  7286. $this->halign = $aHAlign;
  7287. $this->valign = $aVAlign;
  7288. }
  7289. function SetFormat($aFormat,$aNegFormat="") {
  7290. $this->format= $aFormat;
  7291. $this->negformat= $aNegFormat;
  7292. }
  7293. function SetFormatCallback($aFunc) {
  7294. $this->iFormCallback = $aFunc;
  7295. }
  7296. function HideZero($aFlag=true) {
  7297. $this->iHideZero=$aFlag;
  7298. }
  7299. function Stroke($img,$aVal,$x,$y) {
  7300. if( $this->show )
  7301. {
  7302. if( $this->negformat=="" ) $this->negformat=$this->format;
  7303. if( $this->negcolor=="" ) $this->negcolor=$this->color;
  7304. if( $aVal===NULL || (is_string($aVal) && ($aVal=="" || $aVal=="-" || $aVal=="x" ) ) )
  7305. return;
  7306. if( is_numeric($aVal) && $aVal==0 && $this->iHideZero ) {
  7307. return;
  7308. }
  7309. // Since the value is used in different cirumstances we need to check what
  7310. // kind of formatting we shall use. For example, to display values in a line
  7311. // graph we simply display the formatted value, but in the case where the user
  7312. // has already specified a text string we don't fo anything.
  7313. if( $this->iFormCallback != '' ) {
  7314. $f = $this->iFormCallback;
  7315. $sval = call_user_func($f,$aVal);
  7316. }
  7317. elseif( is_numeric($aVal) ) {
  7318. if( $aVal >= 0 )
  7319. $sval=sprintf($this->format,$aVal);
  7320. else
  7321. $sval=sprintf($this->negformat,$aVal);
  7322. }
  7323. else
  7324. $sval=$aVal;
  7325. $y = $y-sign($aVal)*$this->margin;
  7326. $txt = new Text($sval,$x,$y);
  7327. $txt->SetFont($this->ff,$this->fs,$this->fsize);
  7328. if( $this->valign == "" ) {
  7329. if( $aVal >= 0 )
  7330. $valign = "bottom";
  7331. else
  7332. $valign = "top";
  7333. }
  7334. else
  7335. $valign = $this->valign;
  7336. $txt->Align($this->halign,$valign);
  7337. $txt->SetOrientation($this->angle);
  7338. if( $aVal > 0 )
  7339. $txt->SetColor($this->color);
  7340. else
  7341. $txt->SetColor($this->negcolor);
  7342. $txt->Stroke($img);
  7343. }
  7344. }
  7345. }
  7346. //===================================================
  7347. // CLASS Plot
  7348. // Description: Abstract base class for all concrete plot classes
  7349. //===================================================
  7350. class Plot {
  7351. var $line_weight=1;
  7352. var $coords=array();
  7353. var $legend='',$hidelegend=false;
  7354. var $csimtargets=array(); // Array of targets for CSIM
  7355. var $csimareas=""; // Resultant CSIM area tags
  7356. var $csimalts=null; // ALT:s for corresponding target
  7357. var $color="black";
  7358. var $numpoints=0;
  7359. var $weight=1;
  7360. var $value;
  7361. var $center=false;
  7362. var $legendcsimtarget='';
  7363. var $legendcsimalt='';
  7364. //---------------
  7365. // CONSTRUCTOR
  7366. function Plot(&$aDatay,$aDatax=false) {
  7367. $this->numpoints = count($aDatay);
  7368. if( $this->numpoints==0 )
  7369. JpGraphError::RaiseL(25121);//("Empty input data array specified for plot. Must have at least one data point.");
  7370. $this->coords[0]=$aDatay;
  7371. if( is_array($aDatax) )
  7372. $this->coords[1]=$aDatax;
  7373. $this->value = new DisplayValue();
  7374. }
  7375. //---------------
  7376. // PUBLIC METHODS
  7377. // Stroke the plot
  7378. // "virtual" function which must be implemented by
  7379. // the subclasses
  7380. function Stroke(&$aImg,&$aXScale,&$aYScale) {
  7381. JpGraphError::RaiseL(25122);//("JpGraph: Stroke() must be implemented by concrete subclass to class Plot");
  7382. }
  7383. function HideLegend($f=true) {
  7384. $this->hidelegend = $f;
  7385. }
  7386. function DoLegend(&$graph) {
  7387. if( !$this->hidelegend )
  7388. $this->Legend($graph);
  7389. }
  7390. function StrokeDataValue($img,$aVal,$x,$y) {
  7391. $this->value->Stroke($img,$aVal,$x,$y);
  7392. }
  7393. // Set href targets for CSIM
  7394. function SetCSIMTargets($aTargets,$aAlts=null) {
  7395. $this->csimtargets=$aTargets;
  7396. $this->csimalts=$aAlts;
  7397. }
  7398. // Get all created areas
  7399. function GetCSIMareas() {
  7400. return $this->csimareas;
  7401. }
  7402. // "Virtual" function which gets called before any scale
  7403. // or axis are stroked used to do any plot specific adjustment
  7404. function PreStrokeAdjust(&$aGraph) {
  7405. if( substr($aGraph->axtype,0,4) == "text" && (isset($this->coords[1])) )
  7406. JpGraphError::RaiseL(25123);//("JpGraph: You can't use a text X-scale with specified X-coords. Use a \"int\" or \"lin\" scale instead.");
  7407. return true;
  7408. }
  7409. // Get minimum values in plot
  7410. function Min() {
  7411. if( isset($this->coords[1]) )
  7412. $x=$this->coords[1];
  7413. else
  7414. $x="";
  7415. if( $x != "" && count($x) > 0 )
  7416. $xm=min($x);
  7417. else
  7418. $xm=0;
  7419. $y=$this->coords[0];
  7420. $cnt = count($y);
  7421. if( $cnt > 0 ) {
  7422. /*
  7423. if( ! isset($y[0]) ) {
  7424. JpGraphError('The input data array must have consecutive values from position 0 and forward. The given y-array starts with empty values (NULL)');
  7425. }
  7426. */
  7427. //$ym = $y[0];
  7428. $i=0;
  7429. while( $i<$cnt && !is_numeric($ym=$y[$i]) )
  7430. $i++;
  7431. while( $i < $cnt) {
  7432. if( is_numeric($y[$i]) )
  7433. $ym=min($ym,$y[$i]);
  7434. ++$i;
  7435. }
  7436. }
  7437. else
  7438. $ym="";
  7439. return array($xm,$ym);
  7440. }
  7441. // Get maximum value in plot
  7442. function Max() {
  7443. if( isset($this->coords[1]) )
  7444. $x=$this->coords[1];
  7445. else
  7446. $x="";
  7447. if( $x!="" && count($x) > 0 )
  7448. $xm=max($x);
  7449. else {
  7450. $xm = $this->numpoints-1;
  7451. }
  7452. $y=$this->coords[0];
  7453. if( count($y) > 0 ) {
  7454. /*
  7455. if( !isset($y[0]) ) {
  7456. JpGraphError::Raise('The input data array must have consecutive values from position 0 and forward. The given y-array starts with empty values (NULL)');
  7457. //$y[0] = 0;
  7458. // Change in 1.5.1 Don't treat this as an error any more. Just silently convert to 0
  7459. // Change in 1.17 Treat his as an error again !! This is the right way to do !!
  7460. }
  7461. */
  7462. $cnt = count($y);
  7463. $i=0;
  7464. while( $i<$cnt && !is_numeric($ym=$y[$i]) )
  7465. $i++;
  7466. while( $i < $cnt ) {
  7467. if( is_numeric($y[$i]) )
  7468. $ym=max($ym,$y[$i]);
  7469. ++$i;
  7470. }
  7471. }
  7472. else
  7473. $ym="";
  7474. return array($xm,$ym);
  7475. }
  7476. function SetColor($aColor) {
  7477. $this->color=$aColor;
  7478. }
  7479. function SetLegend($aLegend,$aCSIM="",$aCSIMAlt="") {
  7480. $this->legend = $aLegend;
  7481. $this->legendcsimtarget = $aCSIM;
  7482. $this->legendcsimalt = $aCSIMAlt;
  7483. }
  7484. function SetWeight($aWeight) {
  7485. $this->weight=$aWeight;
  7486. }
  7487. function SetLineWeight($aWeight=1) {
  7488. $this->line_weight=$aWeight;
  7489. }
  7490. function SetCenter($aCenter=true) {
  7491. $this->center = $aCenter;
  7492. }
  7493. // This method gets called by Graph class to plot anything that should go
  7494. // into the margin after the margin color has been set.
  7495. function StrokeMargin(&$aImg) {
  7496. return true;
  7497. }
  7498. // Framework function the chance for each plot class to set a legend
  7499. function Legend(&$aGraph) {
  7500. if( $this->legend != "" )
  7501. $aGraph->legend->Add($this->legend,$this->color,"",0,$this->legendcsimtarget,$this->legendcsimalt);
  7502. }
  7503. } // Class
  7504. //===================================================
  7505. // CLASS PlotLine
  7506. // Description:
  7507. // Data container class to hold properties for a static
  7508. // line that is drawn directly in the plot area.
  7509. // Usefull to add static borders inside a plot to show
  7510. // for example set-values
  7511. //===================================================
  7512. class PlotLine {
  7513. var $weight=1;
  7514. var $color="black";
  7515. var $direction=-1;
  7516. var $scaleposition;
  7517. //---------------
  7518. // CONSTRUCTOR
  7519. function PlotLine($aDir=HORIZONTAL,$aPos=0,$aColor="black",$aWeight=1) {
  7520. $this->direction = $aDir;
  7521. $this->color=$aColor;
  7522. $this->weight=$aWeight;
  7523. $this->scaleposition=$aPos;
  7524. }
  7525. //---------------
  7526. // PUBLIC METHODS
  7527. function SetPosition($aScalePosition) {
  7528. $this->scaleposition=$aScalePosition;
  7529. }
  7530. function SetDirection($aDir) {
  7531. $this->direction = $aDir;
  7532. }
  7533. function SetColor($aColor) {
  7534. $this->color=$aColor;
  7535. }
  7536. function SetWeight($aWeight) {
  7537. $this->weight=$aWeight;
  7538. }
  7539. function PreStrokeAdjust($aGraph) {
  7540. // Nothing to do
  7541. }
  7542. function Stroke(&$aImg,&$aXScale,&$aYScale) {
  7543. $aImg->SetColor($this->color);
  7544. $aImg->SetLineWeight($this->weight);
  7545. if( $this->direction == VERTICAL ) {
  7546. $ymin_abs=$aYScale->Translate($aYScale->GetMinVal());
  7547. $ymax_abs=$aYScale->Translate($aYScale->GetMaxVal());
  7548. $xpos_abs=$aXScale->Translate($this->scaleposition);
  7549. $aImg->Line($xpos_abs, $ymin_abs, $xpos_abs, $ymax_abs);
  7550. }
  7551. elseif( $this->direction == HORIZONTAL ) {
  7552. $xmin_abs=$aXScale->Translate($aXScale->GetMinVal());
  7553. $xmax_abs=$aXScale->Translate($aXScale->GetMaxVal());
  7554. $ypos_abs=$aYScale->Translate($this->scaleposition);
  7555. $aImg->Line($xmin_abs, $ypos_abs, $xmax_abs, $ypos_abs);
  7556. }
  7557. else
  7558. JpGraphError::RaiseL(25125);//(" Illegal direction for static line");
  7559. }
  7560. }
  7561. // <EOF>
  7562. ?>