PageRenderTime 67ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/dist/webpagetest/www/lib/jpgraph/jpgraph.php

http://webpagetest.googlecode.com/
PHP | 1955 lines | 1414 code | 260 blank | 281 comment | 386 complexity | 7e3a0a26d3f3413652116e6fdf4676bf MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-3.0, LGPL-3.0, MIT, BSD-3-Clause, ISC, LGPL-2.1

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

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