PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/scalr-2/tags/scalr-2.0.0/app/src/Lib/Graphics/JPGraph/jpgraph.php

http://scalr.googlecode.com/
PHP | 2039 lines | 1491 code | 265 blank | 283 comment | 413 complexity | 2e6f9cb8d74ae242218503bac70b49ea MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. //=======================================================================
  3. // File: JPGRAPH.PHP
  4. // Description: PHP Graph Plotting library. Base module.
  5. // Created: 2001-01-08
  6. // Ver: $Id: jpgraph.php 877 2007-03-25 20:20:10Z ljp $
  7. //
  8. // Copyright 2006 (c) Aditus Consulting. All rights reserved.
  9. //========================================================================
  10. require_once('jpg-config.inc.php');
  11. require_once('jpgraph_gradient.php');
  12. require_once('jpgraph_errhandler.inc.php');
  13. require_once('jpgraph_ttf.inc.php');
  14. // Version info
  15. DEFINE('JPG_VERSION','2.2');
  16. // Minimum required PHP version
  17. DEFINE('MIN_PHPVERSION','5.1.0');
  18. // Should the image be a truecolor image?
  19. DEFINE('USE_TRUECOLOR',true);
  20. //------------------------------------------------------------------------
  21. // Automatic settings of path for cache and font directory
  22. // if they have not been previously specified
  23. //------------------------------------------------------------------------
  24. if(USE_CACHE) {
  25. if (!defined('CACHE_DIR')) {
  26. if ( strstr( PHP_OS, 'WIN') ) {
  27. if( empty($_SERVER['TEMP']) ) {
  28. $t = new ErrMsgText();
  29. $msg = $t->Get(11,$file,$lineno);
  30. die($msg);
  31. }
  32. else {
  33. DEFINE('CACHE_DIR', $_SERVER['TEMP'] . '/');
  34. }
  35. } else {
  36. DEFINE('CACHE_DIR','/tmp/jpgraph_cache/');
  37. }
  38. }
  39. }
  40. elseif( !defined('CACHE_DIR') ) {
  41. DEFINE('CACHE_DIR', '');
  42. }
  43. if (!defined('TTF_DIR')) {
  44. if (strstr( PHP_OS, 'WIN') ) {
  45. $sroot = getenv('SystemRoot');
  46. if( empty($sroot) ) {
  47. $t = new ErrMsgText();
  48. $msg = $t->Get(12,$file,$lineno);
  49. die($msg);
  50. }
  51. else {
  52. DEFINE('TTF_DIR', $sroot.'/fonts/');
  53. }
  54. } else {
  55. DEFINE('TTF_DIR','/usr/X11R6/lib/X11/fonts/truetype/');
  56. }
  57. }
  58. //------------------------------------------------------------------
  59. // Constants which are used as parameters for the method calls
  60. //------------------------------------------------------------------
  61. // Tick density
  62. DEFINE("TICKD_DENSE",1);
  63. DEFINE("TICKD_NORMAL",2);
  64. DEFINE("TICKD_SPARSE",3);
  65. DEFINE("TICKD_VERYSPARSE",4);
  66. // Side for ticks and labels.
  67. DEFINE("SIDE_LEFT",-1);
  68. DEFINE("SIDE_RIGHT",1);
  69. DEFINE("SIDE_DOWN",-1);
  70. DEFINE("SIDE_BOTTOM",-1);
  71. DEFINE("SIDE_UP",1);
  72. DEFINE("SIDE_TOP",1);
  73. // Legend type stacked vertical or horizontal
  74. DEFINE("LEGEND_VERT",0);
  75. DEFINE("LEGEND_HOR",1);
  76. // Mark types for plot marks
  77. DEFINE("MARK_SQUARE",1);
  78. DEFINE("MARK_UTRIANGLE",2);
  79. DEFINE("MARK_DTRIANGLE",3);
  80. DEFINE("MARK_DIAMOND",4);
  81. DEFINE("MARK_CIRCLE",5);
  82. DEFINE("MARK_FILLEDCIRCLE",6);
  83. DEFINE("MARK_CROSS",7);
  84. DEFINE("MARK_STAR",8);
  85. DEFINE("MARK_X",9);
  86. DEFINE("MARK_LEFTTRIANGLE",10);
  87. DEFINE("MARK_RIGHTTRIANGLE",11);
  88. DEFINE("MARK_FLASH",12);
  89. DEFINE("MARK_IMG",13);
  90. DEFINE("MARK_FLAG1",14);
  91. DEFINE("MARK_FLAG2",15);
  92. DEFINE("MARK_FLAG3",16);
  93. DEFINE("MARK_FLAG4",17);
  94. // Builtin images
  95. DEFINE("MARK_IMG_PUSHPIN",50);
  96. DEFINE("MARK_IMG_SPUSHPIN",50);
  97. DEFINE("MARK_IMG_LPUSHPIN",51);
  98. DEFINE("MARK_IMG_DIAMOND",52);
  99. DEFINE("MARK_IMG_SQUARE",53);
  100. DEFINE("MARK_IMG_STAR",54);
  101. DEFINE("MARK_IMG_BALL",55);
  102. DEFINE("MARK_IMG_SBALL",55);
  103. DEFINE("MARK_IMG_MBALL",56);
  104. DEFINE("MARK_IMG_LBALL",57);
  105. DEFINE("MARK_IMG_BEVEL",58);
  106. // Inline defines
  107. DEFINE("INLINE_YES",1);
  108. DEFINE("INLINE_NO",0);
  109. // Format for background images
  110. DEFINE("BGIMG_FILLPLOT",1);
  111. DEFINE("BGIMG_FILLFRAME",2);
  112. DEFINE("BGIMG_COPY",3);
  113. DEFINE("BGIMG_CENTER",4);
  114. // Depth of objects
  115. DEFINE("DEPTH_BACK",0);
  116. DEFINE("DEPTH_FRONT",1);
  117. // Direction
  118. DEFINE("VERTICAL",1);
  119. DEFINE("HORIZONTAL",0);
  120. // Axis styles for scientific style axis
  121. DEFINE('AXSTYLE_SIMPLE',1);
  122. DEFINE('AXSTYLE_BOXIN',2);
  123. DEFINE('AXSTYLE_BOXOUT',3);
  124. DEFINE('AXSTYLE_YBOXIN',4);
  125. DEFINE('AXSTYLE_YBOXOUT',5);
  126. // Style for title backgrounds
  127. DEFINE('TITLEBKG_STYLE1',1);
  128. DEFINE('TITLEBKG_STYLE2',2);
  129. DEFINE('TITLEBKG_STYLE3',3);
  130. DEFINE('TITLEBKG_FRAME_NONE',0);
  131. DEFINE('TITLEBKG_FRAME_FULL',1);
  132. DEFINE('TITLEBKG_FRAME_BOTTOM',2);
  133. DEFINE('TITLEBKG_FRAME_BEVEL',3);
  134. DEFINE('TITLEBKG_FILLSTYLE_HSTRIPED',1);
  135. DEFINE('TITLEBKG_FILLSTYLE_VSTRIPED',2);
  136. DEFINE('TITLEBKG_FILLSTYLE_SOLID',3);
  137. // Style for background gradient fills
  138. DEFINE('BGRAD_FRAME',1);
  139. DEFINE('BGRAD_MARGIN',2);
  140. DEFINE('BGRAD_PLOT',3);
  141. // Width of tab titles
  142. DEFINE('TABTITLE_WIDTHFIT',0);
  143. DEFINE('TABTITLE_WIDTHFULL',-1);
  144. // Defines for 3D skew directions
  145. DEFINE('SKEW3D_UP',0);
  146. DEFINE('SKEW3D_DOWN',1);
  147. DEFINE('SKEW3D_LEFT',2);
  148. DEFINE('SKEW3D_RIGHT',3);
  149. // Line styles
  150. DEFINE('LINESTYLE_DOTTED',1);
  151. DEFINE('LINESTYLE_DASHED',2);
  152. DEFINE('LINESTYLE_LONGDASH',3);
  153. DEFINE('LINESTYLE_SOLID',4);
  154. // For internal use only
  155. DEFINE("_JPG_DEBUG",false);
  156. DEFINE("_FORCE_IMGTOFILE",false);
  157. DEFINE("_FORCE_IMGDIR",'/tmp/jpgimg/');
  158. function CheckPHPVersion($aMinVersion)
  159. {
  160. list($majorC, $minorC, $editC) = split('[/.-]', PHP_VERSION);
  161. list($majorR, $minorR, $editR) = split('[/.-]', $aMinVersion);
  162. if ($majorC > $majorR) return true;
  163. if ($majorC < $majorR) return false;
  164. // same major - check ninor
  165. if ($minorC > $minorR) return true;
  166. if ($minorC < $minorR) return false;
  167. // and same minor
  168. if ($editC >= $editR) return true;
  169. return true;
  170. }
  171. //
  172. // Make sure PHP version is high enough
  173. //
  174. if( !CheckPHPVersion(MIN_PHPVERSION) ) {
  175. JpGraphError::RaiseL(13,PHP_VERSION,MIN_PHPVERSION);
  176. }
  177. //
  178. // Make GD sanity check
  179. //
  180. if( !function_exists("imagetypes") || !function_exists('imagecreatefromstring') ) {
  181. JpGraphError::RaiseL(25001);
  182. //("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)");
  183. }
  184. //
  185. // Setup PHP error handler
  186. //
  187. function _phpErrorHandler($errno,$errmsg,$filename, $linenum, $vars) {
  188. // Respect current error level
  189. if( $errno & error_reporting() ) {
  190. JpGraphError::RaiseL(25003,basename($filename),$linenum,$errmsg);
  191. }
  192. }
  193. if( INSTALL_PHP_ERR_HANDLER ) {
  194. set_error_handler("_phpErrorHandler");
  195. }
  196. //
  197. //Check if there were any warnings, perhaps some wrong includes by the
  198. //user
  199. //
  200. if( isset($GLOBALS['php_errormsg']) && CATCH_PHPERRMSG &&
  201. !preg_match('|Deprecated|', $GLOBALS['php_errormsg']) ) {
  202. JpGraphError::RaiseL(25004,$GLOBALS['php_errormsg']);
  203. }
  204. // Useful mathematical function
  205. function sign($a) {return $a >= 0 ? 1 : -1;}
  206. // Utility function to generate an image name based on the filename we
  207. // are running from and assuming we use auto detection of graphic format
  208. // (top level), i.e it is safe to call this function
  209. // from a script that uses JpGraph
  210. function GenImgName() {
  211. // Determine what format we should use when we save the images
  212. $supported = imagetypes();
  213. if( $supported & IMG_PNG ) $img_format="png";
  214. elseif( $supported & IMG_GIF ) $img_format="gif";
  215. elseif( $supported & IMG_JPG ) $img_format="jpeg";
  216. if( !isset($_SERVER['PHP_SELF']) )
  217. JpGraphError::RaiseL(25005);
  218. //(" 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.");
  219. $fname = basename($_SERVER['PHP_SELF']);
  220. if( !empty($_SERVER['QUERY_STRING']) ) {
  221. $q = @$_SERVER['QUERY_STRING'];
  222. $fname .= '_'.preg_replace("/\W/", "_", $q).'.'.$img_format;
  223. }
  224. else {
  225. $fname = substr($fname,0,strlen($fname)-4).'.'.$img_format;
  226. }
  227. return $fname;
  228. }
  229. //===================================================
  230. // CLASS JpgTimer
  231. // Description: General timing utility class to handle
  232. // time measurement of generating graphs. Multiple
  233. // timers can be started.
  234. //===================================================
  235. class JpgTimer {
  236. private $start, $idx;
  237. //---------------
  238. // CONSTRUCTOR
  239. function JpgTimer() {
  240. $this->idx=0;
  241. }
  242. //---------------
  243. // PUBLIC METHODS
  244. // Push a new timer start on stack
  245. function Push() {
  246. list($ms,$s)=explode(" ",microtime());
  247. $this->start[$this->idx++]=floor($ms*1000) + 1000*$s;
  248. }
  249. // Pop the latest timer start and return the diff with the
  250. // current time
  251. function Pop() {
  252. assert($this->idx>0);
  253. list($ms,$s)=explode(" ",microtime());
  254. $etime=floor($ms*1000) + (1000*$s);
  255. $this->idx--;
  256. return $etime-$this->start[$this->idx];
  257. }
  258. } // Class
  259. $gJpgBrandTiming = BRAND_TIMING;
  260. //===================================================
  261. // CLASS DateLocale
  262. // Description: Hold localized text used in dates
  263. //===================================================
  264. class DateLocale {
  265. public $iLocale = 'C'; // environmental locale be used by default
  266. private $iDayAbb = null, $iShortDay = null, $iShortMonth = null, $iMonthName = null;
  267. //---------------
  268. // CONSTRUCTOR
  269. function DateLocale() {
  270. settype($this->iDayAbb, 'array');
  271. settype($this->iShortDay, 'array');
  272. settype($this->iShortMonth, 'array');
  273. settype($this->iMonthName, 'array');
  274. $this->Set('C');
  275. }
  276. //---------------
  277. // PUBLIC METHODS
  278. function Set($aLocale) {
  279. if ( in_array($aLocale, array_keys($this->iDayAbb)) ){
  280. $this->iLocale = $aLocale;
  281. return TRUE; // already cached nothing else to do!
  282. }
  283. $pLocale = setlocale(LC_TIME, 0); // get current locale for LC_TIME
  284. if (is_array($aLocale)) {
  285. foreach ($aLocale as $loc) {
  286. $res = @setlocale(LC_TIME, $loc);
  287. if ( $res ) {
  288. $aLocale = $loc;
  289. break;
  290. }
  291. }
  292. }
  293. else {
  294. $res = @setlocale(LC_TIME, $aLocale);
  295. }
  296. if ( ! $res ){
  297. JpGraphError::RaiseL(25007,$aLocale);
  298. //("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.");
  299. return FALSE;
  300. }
  301. $this->iLocale = $aLocale;
  302. for ( $i = 0, $ofs = 0 - strftime('%w'); $i < 7; $i++, $ofs++ ){
  303. $day = strftime('%a', strtotime("$ofs day"));
  304. $day[0] = strtoupper($day[0]);
  305. $this->iDayAbb[$aLocale][]= $day[0];
  306. $this->iShortDay[$aLocale][]= $day;
  307. }
  308. for($i=1; $i<=12; ++$i) {
  309. list($short ,$full) = explode('|', strftime("%b|%B",strtotime("2001-$i-01")));
  310. $this->iShortMonth[$aLocale][] = ucfirst($short);
  311. $this->iMonthName [$aLocale][] = ucfirst($full);
  312. }
  313. setlocale(LC_TIME, $pLocale);
  314. return TRUE;
  315. }
  316. function GetDayAbb() {
  317. return $this->iDayAbb[$this->iLocale];
  318. }
  319. function GetShortDay() {
  320. return $this->iShortDay[$this->iLocale];
  321. }
  322. function GetShortMonth() {
  323. return $this->iShortMonth[$this->iLocale];
  324. }
  325. function GetShortMonthName($aNbr) {
  326. return $this->iShortMonth[$this->iLocale][$aNbr];
  327. }
  328. function GetLongMonthName($aNbr) {
  329. return $this->iMonthName[$this->iLocale][$aNbr];
  330. }
  331. function GetMonth() {
  332. return $this->iMonthName[$this->iLocale];
  333. }
  334. }
  335. $gDateLocale = new DateLocale();
  336. $gJpgDateLocale = new DateLocale();
  337. //=======================================================
  338. // CLASS Footer
  339. // Description: Encapsulates the footer line in the Graph
  340. //=======================================================
  341. class Footer {
  342. public $iLeftMargin = 3, $iRightMargin = 3, $iBottomMargin = 3 ;
  343. public $left,$center,$right;
  344. function Footer() {
  345. $this->left = new Text();
  346. $this->left->ParagraphAlign('left');
  347. $this->center = new Text();
  348. $this->center->ParagraphAlign('center');
  349. $this->right = new Text();
  350. $this->right->ParagraphAlign('right');
  351. }
  352. function SetMargin($aLeft=3,$aRight=3,$aBottom=3) {
  353. $this->iLeftMargin = $aLeft;
  354. $this->iRightMargin = $aRight;
  355. $this->iBottomMargin = $aBottom;
  356. }
  357. function Stroke($aImg) {
  358. $y = $aImg->height - $this->iBottomMargin;
  359. $x = $this->iLeftMargin;
  360. $this->left->Align('left','bottom');
  361. $this->left->Stroke($aImg,$x,$y);
  362. $x = ($aImg->width - $this->iLeftMargin - $this->iRightMargin)/2;
  363. $this->center->Align('center','bottom');
  364. $this->center->Stroke($aImg,$x,$y);
  365. $x = $aImg->width - $this->iRightMargin;
  366. $this->right->Align('right','bottom');
  367. $this->right->Stroke($aImg,$x,$y);
  368. }
  369. }
  370. //===================================================
  371. // CLASS Graph
  372. // Description: Main class to handle graphs
  373. //===================================================
  374. class Graph {
  375. public $cache=null; // Cache object (singleton)
  376. public $img=null; // Img object (singleton)
  377. public $plots=array(); // Array of all plot object in the graph (for Y 1 axis)
  378. public $y2plots=array();// Array of all plot object in the graph (for Y 2 axis)
  379. public $ynplots=array();
  380. public $xscale=null; // X Scale object (could be instance of LinearScale or LogScale
  381. public $yscale=null,$y2scale=null, $ynscale=array();
  382. public $iIcons = array(); // Array of Icons to add to
  383. public $cache_name; // File name to be used for the current graph in the cache directory
  384. public $xgrid=null; // X Grid object (linear or logarithmic)
  385. public $ygrid=null,$y2grid=null; //dito for Y
  386. public $doframe=true,$frame_color=array(0,0,0), $frame_weight=1; // Frame around graph
  387. public $boxed=false, $box_color=array(0,0,0), $box_weight=1; // Box around plot area
  388. public $doshadow=false,$shadow_width=4,$shadow_color=array(102,102,102); // Shadow for graph
  389. public $xaxis=null; // X-axis (instane of Axis class)
  390. public $yaxis=null, $y2axis=null, $ynaxis=array(); // Y axis (instance of Axis class)
  391. public $margin_color=array(200,200,200); // Margin color of graph
  392. public $plotarea_color=array(255,255,255); // Plot area color
  393. public $title,$subtitle,$subsubtitle; // Title and subtitle(s) text object
  394. public $axtype="linlin"; // Type of axis
  395. public $xtick_factor; // Factot to determine the maximum number of ticks depending on the plot with
  396. public $texts=null, $y2texts=null; // Text object to ge shown in the graph
  397. public $lines=null, $y2lines=null;
  398. public $bands=null, $y2bands=null;
  399. public $text_scale_off=0, $text_scale_abscenteroff=-1; // Text scale in fractions and for centering bars
  400. public $background_image="",$background_image_type=-1,$background_image_format="png";
  401. public $background_image_bright=0,$background_image_contr=0,$background_image_sat=0;
  402. public $image_bright=0, $image_contr=0, $image_sat=0;
  403. public $inline;
  404. public $showcsim=0,$csimcolor="red"; //debug stuff, draw the csim boundaris on the image if <>0
  405. public $grid_depth=DEPTH_BACK; // Draw grid under all plots as default
  406. public $iAxisStyle = AXSTYLE_SIMPLE;
  407. public $iCSIMdisplay=false,$iHasStroked = false;
  408. public $footer;
  409. public $csimcachename = '', $csimcachetimeout = 0, $iCSIMImgAlt='';
  410. public $iDoClipping = false;
  411. public $y2orderback=true;
  412. public $tabtitle;
  413. public $bkg_gradtype=-1,$bkg_gradstyle=BGRAD_MARGIN;
  414. public $bkg_gradfrom='navy', $bkg_gradto='silver';
  415. public $titlebackground = false;
  416. public $titlebackground_color = 'lightblue',
  417. $titlebackground_style = 1,
  418. $titlebackground_framecolor = 'blue',
  419. $titlebackground_framestyle = 2,
  420. $titlebackground_frameweight = 1,
  421. $titlebackground_bevelheight = 3 ;
  422. public $titlebkg_fillstyle=TITLEBKG_FILLSTYLE_SOLID;
  423. public $titlebkg_scolor1='black',$titlebkg_scolor2='white';
  424. public $framebevel = false, $framebeveldepth = 2 ;
  425. public $framebevelborder = false, $framebevelbordercolor='black';
  426. public $framebevelcolor1='white@0.4', $framebevelcolor2='black@0.4';
  427. public $background_image_mix=100;
  428. public $background_cflag = '';
  429. public $background_cflag_type = BGIMG_FILLPLOT;
  430. public $background_cflag_mix = 100;
  431. public $iImgTrans=false,
  432. $iImgTransHorizon = 100,$iImgTransSkewDist=150,
  433. $iImgTransDirection = 1, $iImgTransMinSize = true,
  434. $iImgTransFillColor='white',$iImgTransHighQ=false,
  435. $iImgTransBorder=false,$iImgTransHorizonPos=0.5;
  436. protected $iYAxisDeltaPos=50;
  437. protected $iIconDepth=DEPTH_BACK;
  438. protected $iAxisLblBgType = 0,
  439. $iXAxisLblBgFillColor = 'lightgray', $iXAxisLblBgColor = 'black',
  440. $iYAxisLblBgFillColor = 'lightgray', $iYAxisLblBgColor = 'black';
  441. protected $iTables=NULL;
  442. //---------------
  443. // CONSTRUCTOR
  444. // aWIdth Width in pixels of image
  445. // aHeight Height in pixels of image
  446. // aCachedName Name for image file in cache directory
  447. // aTimeOut Timeout in minutes for image in cache
  448. // aInline If true the image is streamed back in the call to Stroke()
  449. // If false the image is just created in the cache
  450. function Graph($aWidth=300,$aHeight=200,$aCachedName="",$aTimeOut=0,$aInline=true) {
  451. GLOBAL $gJpgBrandTiming;
  452. // If timing is used create a new timing object
  453. if( $gJpgBrandTiming ) {
  454. global $tim;
  455. $tim = new JpgTimer();
  456. $tim->Push();
  457. }
  458. if( !is_numeric($aWidth) || !is_numeric($aHeight) ) {
  459. JpGraphError::RaiseL(25008);//('Image width/height argument in Graph::Graph() must be numeric');
  460. }
  461. // Automatically generate the image file name based on the name of the script that
  462. // generates the graph
  463. if( $aCachedName=="auto" )
  464. $aCachedName=GenImgName();
  465. // Should the image be streamed back to the browser or only to the cache?
  466. $this->inline=$aInline;
  467. $this->img = new RotImage($aWidth,$aHeight);
  468. $this->cache = new ImgStreamCache($this->img);
  469. $this->cache->SetTimeOut($aTimeOut);
  470. $this->title = new Text();
  471. $this->title->ParagraphAlign('center');
  472. $this->title->SetFont(FF_FONT2,FS_BOLD);
  473. $this->title->SetMargin(3);
  474. $this->title->SetAlign('center');
  475. $this->subtitle = new Text();
  476. $this->subtitle->ParagraphAlign('center');
  477. $this->subtitle->SetMargin(2);
  478. $this->subtitle->SetAlign('center');
  479. $this->subsubtitle = new Text();
  480. $this->subsubtitle->ParagraphAlign('center');
  481. $this->subsubtitle->SetMargin(2);
  482. $this->subsubtitle->SetAlign('center');
  483. $this->legend = new Legend();
  484. $this->footer = new Footer();
  485. // Window doesn't like '?' in the file name so replace it with an '_'
  486. $aCachedName = str_replace("?","_",$aCachedName);
  487. // If the cached version exist just read it directly from the
  488. // cache, stream it back to browser and exit
  489. if( $aCachedName!="" && READ_CACHE && $aInline )
  490. if( $this->cache->GetAndStream($aCachedName) ) {
  491. exit();
  492. }
  493. $this->cache_name = $aCachedName;
  494. $this->SetTickDensity(); // Normal density
  495. $this->tabtitle = new GraphTabTitle();
  496. }
  497. //---------------
  498. // PUBLIC METHODS
  499. // Enable final image perspective transformation
  500. function Set3DPerspective($aDir=1,$aHorizon=100,$aSkewDist=120,$aQuality=false,$aFillColor='#FFFFFF',$aBorder=false,$aMinSize=true,$aHorizonPos=0.5) {
  501. $this->iImgTrans = true;
  502. $this->iImgTransHorizon = $aHorizon;
  503. $this->iImgTransSkewDist= $aSkewDist;
  504. $this->iImgTransDirection = $aDir;
  505. $this->iImgTransMinSize = $aMinSize;
  506. $this->iImgTransFillColor=$aFillColor;
  507. $this->iImgTransHighQ=$aQuality;
  508. $this->iImgTransBorder=$aBorder;
  509. $this->iImgTransHorizonPos=$aHorizonPos;
  510. }
  511. // Set Image format and optional quality
  512. function SetImgFormat($aFormat,$aQuality=75) {
  513. $this->img->SetImgFormat($aFormat,$aQuality);
  514. }
  515. // Should the grid be in front or back of the plot?
  516. function SetGridDepth($aDepth) {
  517. $this->grid_depth=$aDepth;
  518. }
  519. function SetIconDepth($aDepth) {
  520. $this->iIconDepth=$aDepth;
  521. }
  522. // Specify graph angle 0-360 degrees.
  523. function SetAngle($aAngle) {
  524. $this->img->SetAngle($aAngle);
  525. }
  526. function SetAlphaBlending($aFlg=true) {
  527. $this->img->SetAlphaBlending($aFlg);
  528. }
  529. // Shortcut to image margin
  530. function SetMargin($lm,$rm,$tm,$bm) {
  531. $this->img->SetMargin($lm,$rm,$tm,$bm);
  532. }
  533. function SetY2OrderBack($aBack=true) {
  534. $this->y2orderback = $aBack;
  535. }
  536. // Rotate the graph 90 degrees and set the margin
  537. // when we have done a 90 degree rotation
  538. function Set90AndMargin($lm=0,$rm=0,$tm=0,$bm=0) {
  539. $lm = $lm ==0 ? floor(0.2 * $this->img->width) : $lm ;
  540. $rm = $rm ==0 ? floor(0.1 * $this->img->width) : $rm ;
  541. $tm = $tm ==0 ? floor(0.2 * $this->img->height) : $tm ;
  542. $bm = $bm ==0 ? floor(0.1 * $this->img->height) : $bm ;
  543. $adj = ($this->img->height - $this->img->width)/2;
  544. $this->img->SetMargin($tm-$adj,$bm-$adj,$rm+$adj,$lm+$adj);
  545. $this->img->SetCenter(floor($this->img->width/2),floor($this->img->height/2));
  546. $this->SetAngle(90);
  547. if( empty($this->yaxis) || empty($this->xaxis) ) {
  548. JpgraphError::RaiseL(25009);//('You must specify what scale to use with a call to Graph::SetScale()');
  549. }
  550. $this->xaxis->SetLabelAlign('right','center');
  551. $this->yaxis->SetLabelAlign('center','bottom');
  552. }
  553. function SetClipping($aFlg=true) {
  554. $this->iDoClipping = $aFlg ;
  555. }
  556. // Add a plot object to the graph
  557. function Add($aPlot) {
  558. if( $aPlot == null )
  559. JpGraphError::RaiseL(25010);//("Graph::Add() You tried to add a null plot to the graph.");
  560. if( is_array($aPlot) && count($aPlot) > 0 )
  561. $cl = $aPlot[0];
  562. else
  563. $cl = $aPlot;
  564. if( $cl instanceof Text )
  565. $this->AddText($aPlot);
  566. elseif( $cl instanceof PlotLine )
  567. $this->AddLine($aPlot);
  568. elseif( class_exists('PlotBand',false) && ($cl instanceof PlotBand) )
  569. $this->AddBand($aPlot);
  570. elseif( class_exists('IconPlot',false) && ($cl instanceof IconPlot) )
  571. $this->AddIcon($aPlot);
  572. elseif( class_exists('GTextTable',false) && ($cl instanceof GTextTable) )
  573. $this->AddTable($aPlot);
  574. else
  575. $this->plots[] = $aPlot;
  576. }
  577. function AddTable($aTable) {
  578. if( is_array($aTable) ) {
  579. for($i=0; $i < count($aTable); ++$i )
  580. $this->iTables[]=$aTable[$i];
  581. }
  582. else {
  583. $this->iTables[] = $aTable ;
  584. }
  585. }
  586. function AddIcon($aIcon) {
  587. if( is_array($aIcon) ) {
  588. for($i=0; $i < count($aIcon); ++$i )
  589. $this->iIcons[]=$aIcon[$i];
  590. }
  591. else {
  592. $this->iIcons[] = $aIcon ;
  593. }
  594. }
  595. // Add plot to second Y-scale
  596. function AddY2($aPlot) {
  597. if( $aPlot == null )
  598. JpGraphError::RaiseL(25011);//("Graph::AddY2() You tried to add a null plot to the graph.");
  599. if( is_array($aPlot) && count($aPlot) > 0 )
  600. $cl = $aPlot[0];
  601. else
  602. $cl = $aPlot;
  603. if( $cl instanceof Text )
  604. $this->AddText($aPlot,true);
  605. elseif( $cl instanceof PlotLine )
  606. $this->AddLine($aPlot,true);
  607. elseif( class_exists('PlotBand',false) && ($cl instanceof PlotBand) )
  608. $this->AddBand($aPlot,true);
  609. else
  610. $this->y2plots[] = $aPlot;
  611. }
  612. // Add plot to the extra Y-axises
  613. function AddY($aN,$aPlot) {
  614. if( $aPlot == null )
  615. JpGraphError::RaiseL(25012);//("Graph::AddYN() You tried to add a null plot to the graph.");
  616. if( is_array($aPlot) && count($aPlot) > 0 )
  617. $cl = $aPlot[0];
  618. else
  619. $cl = $aPlot;
  620. if( ($cl instanceof Text) || ($cl instanceof PlotLine) ||
  621. (class_exists('PlotBand',false) && ($cl instanceof PlotBand)) )
  622. JpGraph::RaiseL(25013);//('You can only add standard plots to multiple Y-axis');
  623. else
  624. $this->ynplots[$aN][] = $aPlot;
  625. }
  626. // Add text object to the graph
  627. function AddText($aTxt,$aToY2=false) {
  628. if( $aTxt == null )
  629. JpGraphError::RaiseL(25014);//("Graph::AddText() You tried to add a null text to the graph.");
  630. if( $aToY2 ) {
  631. if( is_array($aTxt) ) {
  632. for($i=0; $i < count($aTxt); ++$i )
  633. $this->y2texts[]=$aTxt[$i];
  634. }
  635. else
  636. $this->y2texts[] = $aTxt;
  637. }
  638. else {
  639. if( is_array($aTxt) ) {
  640. for($i=0; $i < count($aTxt); ++$i )
  641. $this->texts[]=$aTxt[$i];
  642. }
  643. else
  644. $this->texts[] = $aTxt;
  645. }
  646. }
  647. // Add a line object (class PlotLine) to the graph
  648. function AddLine($aLine,$aToY2=false) {
  649. if( $aLine == null )
  650. JpGraphError::RaiseL(25015);//("Graph::AddLine() You tried to add a null line to the graph.");
  651. if( $aToY2 ) {
  652. if( is_array($aLine) ) {
  653. for($i=0; $i < count($aLine); ++$i )
  654. $this->y2lines[]=$aLine[$i];
  655. }
  656. else
  657. $this->y2lines[] = $aLine;
  658. }
  659. else {
  660. if( is_array($aLine) ) {
  661. for($i=0; $i<count($aLine); ++$i )
  662. $this->lines[]=$aLine[$i];
  663. }
  664. else
  665. $this->lines[] = $aLine;
  666. }
  667. }
  668. // Add vertical or horizontal band
  669. function AddBand($aBand,$aToY2=false) {
  670. if( $aBand == null )
  671. JpGraphError::RaiseL(25016);//(" Graph::AddBand() You tried to add a null band to the graph.");
  672. if( $aToY2 ) {
  673. if( is_array($aBand) ) {
  674. for($i=0; $i < count($aBand); ++$i )
  675. $this->y2bands[] = $aBand[$i];
  676. }
  677. else
  678. $this->y2bands[] = $aBand;
  679. }
  680. else {
  681. if( is_array($aBand) ) {
  682. for($i=0; $i < count($aBand); ++$i )
  683. $this->bands[] = $aBand[$i];
  684. }
  685. else
  686. $this->bands[] = $aBand;
  687. }
  688. }
  689. function SetBackgroundGradient($aFrom='navy',$aTo='silver',$aGradType=2,$aStyle=BGRAD_FRAME) {
  690. $this->bkg_gradtype=$aGradType;
  691. $this->bkg_gradstyle=$aStyle;
  692. $this->bkg_gradfrom = $aFrom;
  693. $this->bkg_gradto = $aTo;
  694. }
  695. // Set a country flag in the background
  696. function SetBackgroundCFlag($aName,$aBgType=BGIMG_FILLPLOT,$aMix=100) {
  697. $this->background_cflag = $aName;
  698. $this->background_cflag_type = $aBgType;
  699. $this->background_cflag_mix = $aMix;
  700. }
  701. // Alias for the above method
  702. function SetBackgroundCountryFlag($aName,$aBgType=BGIMG_FILLPLOT,$aMix=100) {
  703. $this->background_cflag = $aName;
  704. $this->background_cflag_type = $aBgType;
  705. $this->background_cflag_mix = $aMix;
  706. }
  707. // Specify a background image
  708. function SetBackgroundImage($aFileName,$aBgType=BGIMG_FILLPLOT,$aImgFormat="auto") {
  709. if( !USE_TRUECOLOR ) {
  710. 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.");
  711. }
  712. // Get extension to determine image type
  713. if( $aImgFormat == "auto" ) {
  714. $e = explode('.',$aFileName);
  715. if( !$e ) {
  716. 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');
  717. }
  718. $valid_formats = array('png', 'jpg', 'gif');
  719. $aImgFormat = strtolower($e[count($e)-1]);
  720. if ($aImgFormat == 'jpeg') {
  721. $aImgFormat = 'jpg';
  722. }
  723. elseif (!in_array($aImgFormat, $valid_formats) ) {
  724. JpGraphError::RaiseL(25019,$aImgFormat);//('Unknown file extension ($aImgFormat) in Graph::SetBackgroundImage() for filename: '.$aFileName);
  725. }
  726. }
  727. $this->background_image = $aFileName;
  728. $this->background_image_type=$aBgType;
  729. $this->background_image_format=$aImgFormat;
  730. }
  731. function SetBackgroundImageMix($aMix) {
  732. $this->background_image_mix = $aMix ;
  733. }
  734. // Specify axis style (boxed or single)
  735. function SetAxisStyle($aStyle) {
  736. $this->iAxisStyle = $aStyle ;
  737. }
  738. // Set a frame around the plot area
  739. function SetBox($aDrawPlotFrame=true,$aPlotFrameColor=array(0,0,0),$aPlotFrameWeight=1) {
  740. $this->boxed = $aDrawPlotFrame;
  741. $this->box_weight = $aPlotFrameWeight;
  742. $this->box_color = $aPlotFrameColor;
  743. }
  744. // Specify color for the plotarea (not the margins)
  745. function SetColor($aColor) {
  746. $this->plotarea_color=$aColor;
  747. }
  748. // Specify color for the margins (all areas outside the plotarea)
  749. function SetMarginColor($aColor) {
  750. $this->margin_color=$aColor;
  751. }
  752. // Set a frame around the entire image
  753. function SetFrame($aDrawImgFrame=true,$aImgFrameColor=array(0,0,0),$aImgFrameWeight=1) {
  754. $this->doframe = $aDrawImgFrame;
  755. $this->frame_color = $aImgFrameColor;
  756. $this->frame_weight = $aImgFrameWeight;
  757. }
  758. function SetFrameBevel($aDepth=3,$aBorder=false,$aBorderColor='black',$aColor1='white@0.4',$aColor2='darkgray@0.4',$aFlg=true) {
  759. $this->framebevel = $aFlg ;
  760. $this->framebeveldepth = $aDepth ;
  761. $this->framebevelborder = $aBorder ;
  762. $this->framebevelbordercolor = $aBorderColor ;
  763. $this->framebevelcolor1 = $aColor1 ;
  764. $this->framebevelcolor2 = $aColor2 ;
  765. $this->doshadow = false ;
  766. }
  767. // Set the shadow around the whole image
  768. function SetShadow($aShowShadow=true,$aShadowWidth=5,$aShadowColor=array(102,102,102)) {
  769. $this->doshadow = $aShowShadow;
  770. $this->shadow_color = $aShadowColor;
  771. $this->shadow_width = $aShadowWidth;
  772. $this->footer->iBottomMargin += $aShadowWidth;
  773. $this->footer->iRightMargin += $aShadowWidth;
  774. }
  775. // Specify x,y scale. Note that if you manually specify the scale
  776. // you must also specify the tick distance with a call to Ticks::Set()
  777. function SetScale($aAxisType,$aYMin=1,$aYMax=1,$aXMin=1,$aXMax=1) {
  778. $this->axtype = $aAxisType;
  779. if( $aYMax < $aYMin || $aXMax < $aXMin )
  780. JpGraphError::RaiseL(25020);//('Graph::SetScale(): Specified Max value must be larger than the specified Min value.');
  781. $yt=substr($aAxisType,-3,3);
  782. if( $yt=="lin" )
  783. $this->yscale = new LinearScale($aYMin,$aYMax);
  784. elseif( $yt == "int" ) {
  785. $this->yscale = new LinearScale($aYMin,$aYMax);
  786. $this->yscale->SetIntScale();
  787. }
  788. elseif( $yt=="log" )
  789. $this->yscale = new LogScale($aYMin,$aYMax);
  790. else
  791. JpGraphError::RaiseL(25021,$aAxisType);//("Unknown scale specification for Y-scale. ($aAxisType)");
  792. $xt=substr($aAxisType,0,3);
  793. if( $xt == "lin" || $xt == "tex" ) {
  794. $this->xscale = new LinearScale($aXMin,$aXMax,"x");
  795. $this->xscale->textscale = ($xt == "tex");
  796. }
  797. elseif( $xt == "int" ) {
  798. $this->xscale = new LinearScale($aXMin,$aXMax,"x");
  799. $this->xscale->SetIntScale();
  800. }
  801. elseif( $xt == "dat" ) {
  802. $this->xscale = new DateScale($aXMin,$aXMax,"x");
  803. }
  804. elseif( $xt == "log" )
  805. $this->xscale = new LogScale($aXMin,$aXMax,"x");
  806. else
  807. JpGraphError::RaiseL(25022,$aAxisType);//(" Unknown scale specification for X-scale. ($aAxisType)");
  808. $this->xaxis = new Axis($this->img,$this->xscale);
  809. $this->yaxis = new Axis($this->img,$this->yscale);
  810. $this->xgrid = new Grid($this->xaxis);
  811. $this->ygrid = new Grid($this->yaxis);
  812. $this->ygrid->Show();
  813. }
  814. // Specify secondary Y scale
  815. function SetY2Scale($aAxisType="lin",$aY2Min=1,$aY2Max=1) {
  816. if( $aAxisType=="lin" )
  817. $this->y2scale = new LinearScale($aY2Min,$aY2Max);
  818. elseif( $aAxisType == "int" ) {
  819. $this->y2scale = new LinearScale($aY2Min,$aY2Max);
  820. $this->y2scale->SetIntScale();
  821. }
  822. elseif( $aAxisType=="log" ) {
  823. $this->y2scale = new LogScale($aY2Min,$aY2Max);
  824. }
  825. else JpGraphError::RaiseL(25023,$aAxisType);//("JpGraph: Unsupported Y2 axis type: $aAxisType\nMust be one of (lin,log,int)");
  826. $this->y2axis = new Axis($this->img,$this->y2scale);
  827. $this->y2axis->scale->ticks->SetDirection(SIDE_LEFT);
  828. $this->y2axis->SetLabelSide(SIDE_RIGHT);
  829. $this->y2axis->SetPos('max');
  830. $this->y2axis->SetTitleSide(SIDE_RIGHT);
  831. // Deafult position is the max x-value
  832. $this->y2grid = new Grid($this->y2axis);
  833. }
  834. // Set the delta position (in pixels) between the multiple Y-axis
  835. function SetYDeltaDist($aDist) {
  836. $this->iYAxisDeltaPos = $aDist;
  837. }
  838. // Specify secondary Y scale
  839. function SetYScale($aN,$aAxisType="lin",$aYMin=1,$aYMax=1) {
  840. if( $aAxisType=="lin" )
  841. $this->ynscale[$aN] = new LinearScale($aYMin,$aYMax);
  842. elseif( $aAxisType == "int" ) {
  843. $this->ynscale[$aN] = new LinearScale($aYMin,$aYMax);
  844. $this->ynscale[$aN]->SetIntScale();
  845. }
  846. elseif( $aAxisType=="log" ) {
  847. $this->ynscale[$aN] = new LogScale($aYMin,$aYMax);
  848. }
  849. else JpGraphError::RaiseL(25024,$aAxisType);//("JpGraph: Unsupported Y axis type: $aAxisType\nMust be one of (lin,log,int)");
  850. $this->ynaxis[$aN] = new Axis($this->img,$this->ynscale[$aN]);
  851. $this->ynaxis[$aN]->scale->ticks->SetDirection(SIDE_LEFT);
  852. $this->ynaxis[$aN]->SetLabelSide(SIDE_RIGHT);
  853. }
  854. // Specify density of ticks when autoscaling 'normal', 'dense', 'sparse', 'verysparse'
  855. // The dividing factor have been determined heuristically according to my aesthetic
  856. // sense (or lack off) y.m.m.v !
  857. function SetTickDensity($aYDensity=TICKD_NORMAL,$aXDensity=TICKD_NORMAL) {
  858. $this->xtick_factor=30;
  859. $this->ytick_factor=25;
  860. switch( $aYDensity ) {
  861. case TICKD_DENSE:
  862. $this->ytick_factor=12;
  863. break;
  864. case TICKD_NORMAL:
  865. $this->ytick_factor=25;
  866. break;
  867. case TICKD_SPARSE:
  868. $this->ytick_factor=40;
  869. break;
  870. case TICKD_VERYSPARSE:
  871. $this->ytick_factor=100;
  872. break;
  873. default:
  874. JpGraphError::RaiseL(25025,$densy);//("JpGraph: Unsupported Tick density: $densy");
  875. }
  876. switch( $aXDensity ) {
  877. case TICKD_DENSE:
  878. $this->xtick_factor=15;
  879. break;
  880. case TICKD_NORMAL:
  881. $this->xtick_factor=30;
  882. break;
  883. case TICKD_SPARSE:
  884. $this->xtick_factor=45;
  885. break;
  886. case TICKD_VERYSPARSE:
  887. $this->xtick_factor=60;
  888. break;
  889. default:
  890. JpGraphError::RaiseL(25025,$densx);//("JpGraph: Unsupported Tick density: $densx");
  891. }
  892. }
  893. // Get a string of all image map areas
  894. function GetCSIMareas() {
  895. if( !$this->iHasStroked )
  896. $this->Stroke(_CSIM_SPECIALFILE);
  897. $csim = $this->title->GetCSIMAreas();
  898. $csim .= $this->subtitle->GetCSIMAreas();
  899. $csim .= $this->subsubtitle->GetCSIMAreas();
  900. $csim .= $this->legend->GetCSIMAreas();
  901. if( $this->y2axis != NULL ) {
  902. $csim .= $this->y2axis->title->GetCSIMAreas();
  903. }
  904. if( $this->texts != null ) {
  905. $n = count($this->texts);
  906. for($i=0; $i < $n; ++$i ) {
  907. $csim .= $this->texts[$i]->GetCSIMAreas();
  908. }
  909. }
  910. if( $this->y2texts != null && $this->y2scale != null ) {
  911. $n = count($this->y2texts);
  912. for($i=0; $i < $n; ++$i ) {
  913. $csim .= $this->y2texts[$i]->GetCSIMAreas();
  914. }
  915. }
  916. if( $this->yaxis != null && $this->xaxis != null ) {
  917. $csim .= $this->yaxis->title->GetCSIMAreas();
  918. $csim .= $this->xaxis->title->GetCSIMAreas();
  919. }
  920. $n = count($this->plots);
  921. for( $i=0; $i < $n; ++$i )
  922. $csim .= $this->plots[$i]->GetCSIMareas();
  923. $n = count($this->y2plots);
  924. for( $i=0; $i < $n; ++$i )
  925. $csim .= $this->y2plots[$i]->GetCSIMareas();
  926. $n = count($this->ynaxis);
  927. for( $i=0; $i < $n; ++$i ) {
  928. $m = count($this->ynplots[$i]);
  929. for($j=0; $j < $m; ++$j ) {
  930. $csim .= $this->ynplots[$i][$j]->GetCSIMareas();
  931. }
  932. }
  933. $n = count($this->iTables);
  934. for( $i=0; $i < $n; ++$i ) {
  935. $csim .= $this->iTables[$i]->GetCSIMareas();
  936. }
  937. return $csim;
  938. }
  939. // Get a complete <MAP>..</MAP> tag for the final image map
  940. function GetHTMLImageMap($aMapName) {
  941. $im = "<map name=\"$aMapName\" id=\"$aMapName\" >\n";
  942. $im .= $this->GetCSIMareas();
  943. $im .= "</map>";
  944. return $im;
  945. }
  946. function CheckCSIMCache($aCacheName,$aTimeOut=60) {
  947. global $_SERVER;
  948. if( $aCacheName=='auto' )
  949. $aCacheName=basename($_SERVER['PHP_SELF']);
  950. $urlarg = $this->GetURLArguments();
  951. $this->csimcachename = CSIMCACHE_DIR.$aCacheName.$urlarg;
  952. $this->csimcachetimeout = $aTimeOut;
  953. // First determine if we need to check for a cached version
  954. // This differs from the standard cache in the sense that the
  955. // image and CSIM map HTML file is written relative to the directory
  956. // the script executes in and not the specified cache directory.
  957. // The reason for this is that the cache directory is not necessarily
  958. // accessible from the HTTP server.
  959. if( $this->csimcachename != '' ) {
  960. $dir = dirname($this->csimcachename);
  961. $base = basename($this->csimcachename);
  962. $base = strtok($base,'.');
  963. $suffix = strtok('.');
  964. $basecsim = $dir.'/'.$base.'?'.$urlarg.'_csim_.html';
  965. $baseimg = $dir.'/'.$base.'?'.$urlarg.'.'.$this->img->img_format;
  966. $timedout=false;
  967. // Does it exist at all ?
  968. if( file_exists($basecsim) && file_exists($baseimg) ) {
  969. // Check that it hasn't timed out
  970. $diff=time()-filemtime($basecsim);
  971. if( $this->csimcachetimeout>0 && ($diff > $this->csimcachetimeout*60) ) {
  972. $timedout=true;
  973. @unlink($basecsim);
  974. @unlink($baseimg);
  975. }
  976. else {
  977. if ($fh = @fopen($basecsim, "r")) {
  978. fpassthru($fh);
  979. return true;
  980. }
  981. else
  982. JpGraphError::RaiseL(25027,$basecsim);//(" Can't open cached CSIM \"$basecsim\" for reading.");
  983. }
  984. }
  985. }
  986. return false;
  987. }
  988. // Build the argument string to be used with the csim images
  989. function GetURLArguments() {
  990. // This is a JPGRAPH internal defined that prevents
  991. // us from recursively coming here again
  992. $urlarg = _CSIM_DISPLAY.'=1';
  993. // Now reconstruct any user URL argument
  994. reset($_GET);
  995. while( list($key,$value) = each($_GET) ) {
  996. if( is_array($value) ) {
  997. $n = count($value);
  998. for( $i=0; $i < $n; ++$i ) {
  999. $urlarg .= '&amp;'.$key.'%5B%5D='.urlencode($value[$i]);
  1000. }
  1001. }
  1002. else {
  1003. $urlarg .= '&amp;'.$key.'='.urlencode($value);
  1004. }
  1005. }
  1006. // It's not ideal to convert POST argument to GET arguments
  1007. // but there is little else we can do. One idea for the
  1008. // future might be recreate the POST header in case.
  1009. reset($_POST);
  1010. while( list($key,$value) = each($_POST) ) {
  1011. if( is_array($value) ) {
  1012. $n = count($value);
  1013. for( $i=0; $i < $n; ++$i ) {
  1014. $urlarg .= '&amp;'.$key.'%5B%5D='.urlencode($value[$i]);
  1015. }
  1016. }
  1017. else {
  1018. $urlarg .= '&amp;'.$key.'='.urlencode($value);
  1019. }
  1020. }
  1021. return $urlarg;
  1022. }
  1023. function SetCSIMImgAlt($aAlt) {
  1024. $this->iCSIMImgAlt = $aAlt;
  1025. }
  1026. function StrokeCSIM($aScriptName='auto',$aCSIMName='',$aBorder=0) {
  1027. if( $aCSIMName=='' ) {
  1028. // create a random map name
  1029. srand ((double) microtime() * 1000000);
  1030. $r = rand(0,100000);
  1031. $aCSIMName='__mapname'.$r.'__';
  1032. }
  1033. if( $aScriptName=='auto' )
  1034. $aScriptName=basename($_SERVER['PHP_SELF']);
  1035. $urlarg = $this->GetURLArguments();
  1036. if( empty($_GET[_CSIM_DISPLAY]) ) {
  1037. // First determine if we need to check for a cached version
  1038. // This differs from the standard cache in the sense that the
  1039. // image and CSIM map HTML file is written relative to the directory
  1040. // the script executes in and not the specified cache directory.
  1041. // The reason for this is that the cache directory is not necessarily
  1042. // accessible from the HTTP server.
  1043. if( $this->csimcachename != '' ) {
  1044. $dir = dirname($this->csimcachename);
  1045. $base = basename($this->csimcachename);
  1046. $base = strtok($base,'.');
  1047. $suffix = strtok('.');
  1048. $basecsim = $dir.'/'.$base.'?'.$urlarg.'_csim_.html';
  1049. $baseimg = $base.'?'.$urlarg.'.'.$this->img->img_format;
  1050. // Check that apache can write to directory specified
  1051. if( file_exists($dir) && !is_writeable($dir) ) {
  1052. JpgraphError::RaiseL(25028,$dir);//('Apache/PHP does not have permission to write to the CSIM cache directory ('.$dir.'). Check permissions.');
  1053. }
  1054. // Make sure directory exists
  1055. $this->cache->MakeDirs($dir);
  1056. // Write the image file
  1057. $this->Stroke(CSIMCACHE_DIR.$baseimg);
  1058. // Construct wrapper HTML and write to file and send it back to browser
  1059. // In the src URL we must replace the '?' with its encoding to prevent the arguments
  1060. // to be converted to real arguments.
  1061. $tmp = str_replace('?','%3f',$baseimg);
  1062. $htmlwrap = $this->GetHTMLImageMap($aCSIMName)."\n".
  1063. '<img src="'.CSIMCACHE_HTTP_DIR.$tmp.'" ismap="ismap" usemap="#'.$aCSIMName.'" border="'.$aBorder.'" width="'.$this->img->width.'" height="'.$this->img->height."\" alt=\"".$this->iCSIMImgAlt."\" />\n";
  1064. if($fh = @fopen($basecsim,'w') ) {
  1065. fwrite($fh,$htmlwrap);
  1066. fclose($fh);
  1067. echo $htmlwrap;
  1068. }
  1069. else
  1070. JpGraphError::RaiseL(25029,$basecsim);//(" Can't write CSIM \"$basecsim\" for writing. Check free space and permissions.");
  1071. }
  1072. else {
  1073. if( $aScriptName=='' ) {
  1074. 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().');
  1075. }
  1076. echo $this->GetHTMLImageMap($aCSIMName);
  1077. echo "<img src=\"".$aScriptName.'?'.$urlarg."\" ismap=\"ismap\" usemap=\"#".$aCSIMName.'" border="'.$aBorder.'" width="'.$this->img->width.'" height="'.$this->img->height."\" alt=\"".$this->iCSIMImgAlt."\" />\n";
  1078. }
  1079. }
  1080. else {
  1081. $this->Stroke();
  1082. }
  1083. }
  1084. function GetTextsYMinMax($aY2=false) {
  1085. if( $aY2 )
  1086. $txts = $this->y2texts;
  1087. else
  1088. $txts = $this->texts;
  1089. $n = count($txts);
  1090. $min=null;
  1091. $max=null;
  1092. for( $i=0; $i < $n; ++$i ) {
  1093. if( $txts[$i]->iScalePosY !== null &&
  1094. $txts[$i]->iScalePosX !== null ) {
  1095. if( $min === null ) {
  1096. $min = $max = $txts[$i]->iScalePosY ;
  1097. }
  1098. else {
  1099. $min = min($min,$txts[$i]->iScalePosY);
  1100. $max = max($max,$txts[$i]->iScalePosY);
  1101. }
  1102. }
  1103. }
  1104. if( $min !== null ) {
  1105. return array($min,$max);
  1106. }
  1107. else
  1108. return null;
  1109. }
  1110. function GetTextsXMinMax($aY2=false) {
  1111. if( $aY2 )
  1112. $txts = $this->y2texts;
  1113. else
  1114. $txts = $this->texts;
  1115. $n = count($txts);
  1116. $min=null;
  1117. $max=null;
  1118. for( $i=0; $i < $n; ++$i ) {
  1119. if( $txts[$i]->iScalePosY !== null &&
  1120. $txts[$i]->iScalePosX !== null ) {
  1121. if( $min === null ) {
  1122. $min = $max = $txts[$i]->iScalePosX ;
  1123. }
  1124. else {
  1125. $min = min($min,$txts[$i]->iScalePosX);
  1126. $max = max($max,$txts[$i]->iScalePosX);
  1127. }
  1128. }
  1129. }
  1130. if( $min !== null ) {
  1131. return array($min,$max);
  1132. }
  1133. else
  1134. return null;
  1135. }
  1136. function GetXMinMax() {
  1137. list($min,$ymin) = $this->plots[0]->Min();
  1138. list($max,$ymax) = $this->plots[0]->Max();
  1139. foreach( $this->plots as $p ) {
  1140. list($xmin,$ymin) = $p->Min();
  1141. list($xmax,$ymax) = $p->Max();
  1142. $min = Min($xmin,$min);
  1143. $max = Max($xmax,$max);
  1144. }
  1145. if( $this->y2axis != null ) {
  1146. foreach( $this->y2plots as $p ) {
  1147. list($xmin,$ymin) = $p->Min();
  1148. list($xmax,$ymax) = $p->Max();
  1149. $min = Min($xmin,$min);
  1150. $max = Max($xmax,$max);
  1151. }
  1152. }
  1153. $n = count($this->ynaxis);
  1154. for( $i=0; $i < $n; ++$i ) {
  1155. if( $this->ynaxis[$i] != null) {
  1156. foreach( $this->ynplots[$i] as $p ) {
  1157. list($xmin,$ymin) = $p->Min();
  1158. list($xmax,$ymax) = $p->Max();
  1159. $min = Min($xmin,$min);
  1160. $max = Max($xmax,$max);
  1161. }
  1162. }
  1163. }
  1164. return array($min,$max);
  1165. }
  1166. function AdjustMarginsForTitles() {
  1167. $totrequired =
  1168. ($this->title->t != '' ?
  1169. $this->title->GetTextHeight($this->img) + $this->title->margin + 5 : 0 ) +
  1170. ($this->subtitle->t != '' ?
  1171. $this->subtitle->GetTextHeight($this->img) + $this->subtitle->margin + 5 : 0 ) +
  1172. ($this->subsubtitle->t != '' ?
  1173. $this->subsubtitle->GetTextHeight($this->img) + $this->subsubtitle->margin + 5 : 0 ) ;
  1174. $btotrequired = 0;
  1175. if($this->xaxis != null && !$this->xaxis->hide && !$this->xaxis->hide_labels ) {
  1176. // Minimum bottom margin
  1177. if( $this->xaxis->title->t != '' ) {
  1178. if( $this->img->a == 90 )
  1179. $btotrequired = $this->yaxis->title->GetTextHeight($this->img) + 5 ;
  1180. else
  1181. $btotrequired = $this->xaxis->title->GetTextHeight($this->img) + 5 ;
  1182. }
  1183. else
  1184. $btotrequired = 0;
  1185. if( $this->img->a == 90 ) {
  1186. $this->img->SetFont($this->yaxis->font_family,$this->yaxis->font_style,
  1187. $this->yaxis->font_size);
  1188. $lh = $this->img->GetTextHeight('Mg',$this->yaxis->label_angle);
  1189. }
  1190. else {
  1191. $this->img->SetFont($this->xaxis->font_family,$this->xaxis->font_style,
  1192. $this->xaxis->font_size);
  1193. $lh = $this->img->GetTextHeight('Mg',$this->xaxis->label_angle);
  1194. }
  1195. $btotrequired += $lh + 5;
  1196. }
  1197. if( $this->img->a == 90 ) {
  1198. // DO Nothing. It gets too messy to do this properly for 90 deg...
  1199. }
  1200. else{
  1201. if( $this->img->top_margin < $totrequired ) {
  1202. $this->SetMargin($this->img->left_margin,$this->img->right_margin,
  1203. $totrequired,$this->img->bottom_margin);
  1204. }
  1205. if( $this->img->bottom_margin < $btotrequired ) {
  1206. $this->SetMargin($this->img->left_margin,$this->img->right_margin,
  1207. $this->img->top_margin,$btotrequired);
  1208. }
  1209. }
  1210. }
  1211. // Stroke the graph
  1212. // $aStrokeFileName If != "" the image will be written to this file and NOT
  1213. // streamed back to the browser
  1214. function Stroke($aStrokeFileName="") {
  1215. // Fist make a sanity check that user has specified a scale
  1216. if( empty($this->yscale) ) {
  1217. JpGraphError::RaiseL(25031);//('You must specify what scale to use with a call to Graph::SetScale().');
  1218. }
  1219. // Start by adjusting the margin so that potential titles will fit.
  1220. $this->AdjustMarginsForTitles();
  1221. // Setup scale constants
  1222. if( $this->yscale ) $this->yscale->InitConstants($this->img);
  1223. if( $this->xscale ) $this->xscale->InitConstants($this->img);
  1224. if( $this->y2scale ) $this->y2scale->InitConstants($this->img);
  1225. $n=count($this->ynscale);
  1226. for($i=0; $i < $n; ++$i) {
  1227. if( $this->ynscale[$i] ) $this->ynscale[$i]->InitConstants($this->img);
  1228. }
  1229. // If the filename is the predefined value = '_csim_special_'
  1230. // we assume that the call to stroke only needs to do enough
  1231. // to correctly generate the CSIM maps.
  1232. // We use this variable to skip things we don't strictly need
  1233. // to do to generate the image map to improve performance
  1234. // a best we can. Therefor you will see a lot of tests !$_csim in the
  1235. // code below.
  1236. $_csim = ($aStrokeFileName===_CSIM_SPECIALFILE);
  1237. // We need to know if we have stroked the plot in the
  1238. // GetCSIMareas. Otherwise the CSIM hasn't been generated
  1239. // and in the case of GetCSIM called before stroke to generate
  1240. // CSIM without storing an image to disk GetCSIM must call Stroke.
  1241. $this->iHasStroked = true;
  1242. // Do any pre-stroke adjustment that is needed by the different plot types
  1243. // (i.e bar plots want's to add an offset to the x-labels etc)
  1244. for($i=0; $i < count($this->plots) ; ++$i ) {
  1245. $this->plots[$i]->PreStrokeAdjust($this);
  1246. $this->plots[$i]->DoLegend($this);
  1247. }
  1248. // Any plots on the second Y scale?
  1249. if( $this->y2scale != null ) {
  1250. for($i=0; $i<count($this->y2plots) ; ++$i ) {
  1251. $this->y2plots[$i]->PreStrokeAdjust($this);
  1252. $this->y2plots[$i]->DoLegend($this);
  1253. }
  1254. }
  1255. // Any plots on the extra Y axises?
  1256. $n = count($this->ynaxis);
  1257. for($i=0; $i<$n ; ++$i ) {
  1258. if( $this->ynplots == null || $this->ynplots[$i] == null) {
  1259. JpGraphError::RaiseL(25032,$i);//("No plots for Y-axis nbr:$i");
  1260. }
  1261. $m = count($this->ynplots[$i]);
  1262. for($j=0; $j < $m; ++$j ) {
  1263. $this->ynplots[$i][$j]->PreStrokeAdjust($this);
  1264. $this->ynplots[$i][$j]->DoLegend($this);
  1265. }
  1266. }
  1267. // Bail out if any of the Y-axis not been specified and
  1268. // has no plots. (This means it is impossible to do autoscaling and
  1269. // no other scale was given so we can't possible draw anything). If you use manual
  1270. // scaling you also have to supply the tick steps as well.
  1271. if( (!$this->yscale->IsSpecified() && count($this->plots)==0) ||
  1272. ($this->y2scale!=null && !$this->y2scale->IsSpecified() && count($this->y2plots)==0) ) {
  1273. //$e = "n=".count($this->y2plots)."\n";
  1274. // $e = "Can't draw unspecified Y-scale.<br>\nYou have either:<br>\n";
  1275. // $e .= "1. Specified an Y axis for autoscaling but have not supplied any plots<br>\n";
  1276. // $e .= "2. Specified a scale manually but have forgot to specify the tick steps";
  1277. JpGraphError::RaiseL(25026);
  1278. }
  1279. // Bail out if no plots and no specified X-scale
  1280. if( (!$this->xscale->IsSpecified() && count($this->plots)==0 && count($this->y2plots)==0) )
  1281. JpGraphError::RaiseL(25034);//("<strong>JpGraph: Can't draw unspecified X-scale.</strong><br>No plots.<br>");
  1282. //Check if we should autoscale y-axis
  1283. if( !$this->yscale->IsSpecified() && count($this->plots)>0 ) {
  1284. list($min,$max) = $this->GetPlotsYMinMax($this->plots);
  1285. $lres = $this->GetLinesYMinMax($this->lines);
  1286. if( is_array($lres) ) {
  1287. list($linmin,$linmax) = $lres ;
  1288. $min = min($min,$linmin);
  1289. $max = max($max,$linmax);
  1290. }
  1291. $tres = $this->GetTextsYMinMax();
  1292. if( is_array($tres) ) {
  1293. list($tmin,$tmax) = $tres ;
  1294. $min = min($min,$tmin);
  1295. $max = max($max,$tmax);
  1296. }
  1297. $this->yscale->AutoScale($this->img,$min,$max,
  1298. $this->img->plotheight/$this->ytick_factor);
  1299. }
  1300. elseif( $this->yscale->IsSpecified() &&
  1301. ( $this->yscale->auto_ticks || !$this->yscale->ticks->IsSpecified()) ) {
  1302. // The tick calculation will use the user suplied min/max values to determine
  1303. // the ticks. If auto_ticks is false the exact user specifed min and max
  1304. // values will be used for the scale.
  1305. // If auto_ticks is true then the scale might be slightly adjusted
  1306. // so that the min and max values falls on an even major step.
  1307. $min = $this->yscale->scale[0];
  1308. $max = $this->yscale->scale[1];
  1309. $this->yscale->AutoScale($this->img,$min,$max,
  1310. $this->img->plotheight/$this->ytick_factor,
  1311. $this->yscale->auto_ticks);
  1312. }
  1313. if( $this->y2scale != null) {
  1314. if( !$this->y2scale->IsSpecified() && count($this->y2plots)>0 ) {
  1315. list($min,$max) = $this->GetPlotsYMinMax($this->y2plots);
  1316. $lres = $this->GetLinesYMinMax($this->y2lines);
  1317. if( is_array($lres) ) {
  1318. list($linmin,$linmax) = $lres ;
  1319. $min = min($min,$linmin);
  1320. $max = max($max,$linmax);
  1321. }
  1322. $tres = $this->GetTextsYMinMax(true);
  1323. if( is_array($tres) ) {
  1324. list($tmin,$tmax) = $tres ;
  1325. $min = min($min,$tmin);
  1326. $max = max($max,$tmax);
  1327. }
  1328. $this->y2scale->AutoScale($this->img,$min,$max,$this->img->plotheight/$this->ytick_factor);
  1329. }
  1330. elseif( $this->y2scale->IsSpecified() &&
  1331. ( $this->y2scale->auto_ticks || !$this->y2scale->ticks->IsSpecified()) ) {
  1332. // The tick calculation will use the user suplied min/max values to determine
  1333. // the ticks. If auto_ticks is false the exact user specifed min and max
  1334. // values will be used for the scale.
  1335. // If auto_ticks is true then the scale might be slightly adjusted
  1336. // so that the min and max values falls on an even major step.
  1337. $min = $this->y2scale->scale[0];
  1338. $max = $this->y2scale->scale[1];
  1339. $this->y2scale->AutoScale($this->img,$min,$max,
  1340. $this->img->plotheight/$this->ytick_factor,
  1341. $this->y2scale->auto_ticks);
  1342. }
  1343. }
  1344. //
  1345. // Autoscale the extra Y-axises
  1346. //
  1347. $n = count($this->ynaxis);
  1348. for( $i=0; $i < $n; ++$i ) {
  1349. if( $this->ynscale[$i] != null) {
  1350. if( !$this->ynscale[$i]->IsSpecified() && count($this->ynplots[$i])>0 ) {
  1351. list($min,$max) = $this->GetPlotsYMinMax($this->ynplots[$i]);
  1352. $this->ynscale[$i]->AutoScale($this->img,$min,$max,$this->img->plotheight/$this->ytick_factor);
  1353. }
  1354. elseif( $this->ynscale[$i]->IsSpecified() &&
  1355. ( $this->ynscale[$i]->auto_ticks || !$this->ynscale[$i]->ticks->IsSpecified()) ) {
  1356. // The tick calculation will use the user suplied min/max values to determine
  1357. // the ticks. If auto_ticks is false the exact user speā€¦

Large files files are truncated, but you can click here to view the full file