PageRenderTime 106ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/jpgraph/jpgraph.php

http://yt-cache.googlecode.com/
PHP | 1848 lines | 1393 code | 235 blank | 220 comment | 302 complexity | c93da975852b9db5a734cfa0b5e55d82 MD5 | raw file
Possible License(s): MIT

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

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