add amendments metric
[contractdashboard.git] / lib / jpgraph / jpgraph.php
blob:a/lib/jpgraph/jpgraph.php -> blob:b/lib/jpgraph/jpgraph.php
<?php <?php
//======================================================================= //=======================================================================
// File: JPGRAPH.PHP // File: JPGRAPH.PHP
// Description: PHP Graph Plotting library. Base module. // Description: PHP Graph Plotting library. Base module.
// Created: 2001-01-08 // Created: 2001-01-08
// Ver: $Id: jpgraph.php 1912 2009-10-10 10:26:03Z ljp $ // Ver: $Id: jpgraph.php 1912 2009-10-10 10:26:03Z ljp $
// //
// Copyright (c) Aditus Consulting. All rights reserved. // Copyright (c) Aditus Consulting. All rights reserved.
//======================================================================== //========================================================================
   
require_once('jpg-config.inc.php'); require_once('jpg-config.inc.php');
require_once('jpgraph_gradient.php'); require_once('jpgraph_gradient.php');
require_once('jpgraph_errhandler.inc.php'); require_once('jpgraph_errhandler.inc.php');
require_once('jpgraph_ttf.inc.php'); require_once('jpgraph_ttf.inc.php');
require_once('jpgraph_rgb.inc.php'); require_once('jpgraph_rgb.inc.php');
require_once('jpgraph_text.inc.php'); require_once('jpgraph_text.inc.php');
require_once('jpgraph_legend.inc.php'); require_once('jpgraph_legend.inc.php');
require_once('gd_image.inc.php'); require_once('gd_image.inc.php');
   
// Version info // Version info
define('JPG_VERSION','3.0.6'); define('JPG_VERSION','3.0.6');
   
// Minimum required PHP version // Minimum required PHP version
define('MIN_PHPVERSION','5.1.0'); define('MIN_PHPVERSION','5.1.0');
   
// Special file name to indicate that we only want to calc // Special file name to indicate that we only want to calc
// the image map in the call to Graph::Stroke() used // the image map in the call to Graph::Stroke() used
// internally from the GetHTMLCSIM() method. // internally from the GetHTMLCSIM() method.
define('_CSIM_SPECIALFILE','_csim_special_'); define('_CSIM_SPECIALFILE','_csim_special_');
   
// HTTP GET argument that is used with image map // HTTP GET argument that is used with image map
// to indicate to the script to just generate the image // to indicate to the script to just generate the image
// and not the full CSIM HTML page. // and not the full CSIM HTML page.
define('_CSIM_DISPLAY','_jpg_csimd'); define('_CSIM_DISPLAY','_jpg_csimd');
   
// Special filename for Graph::Stroke(). If this filename is given // Special filename for Graph::Stroke(). If this filename is given
// then the image will NOT be streamed to browser of file. Instead the // then the image will NOT be streamed to browser of file. Instead the
// Stroke call will return the handler for the created GD image. // Stroke call will return the handler for the created GD image.
define('_IMG_HANDLER','__handle'); define('_IMG_HANDLER','__handle');
   
// Special filename for Graph::Stroke(). If this filename is given // Special filename for Graph::Stroke(). If this filename is given
// the image will be stroked to a file with a name based on the script name. // the image will be stroked to a file with a name based on the script name.
define('_IMG_AUTO','auto'); define('_IMG_AUTO','auto');
   
// Tick density // Tick density
define("TICKD_DENSE",1); define("TICKD_DENSE",1);
define("TICKD_NORMAL",2); define("TICKD_NORMAL",2);
define("TICKD_SPARSE",3); define("TICKD_SPARSE",3);
define("TICKD_VERYSPARSE",4); define("TICKD_VERYSPARSE",4);
   
// Side for ticks and labels. // Side for ticks and labels.
define("SIDE_LEFT",-1); define("SIDE_LEFT",-1);
define("SIDE_RIGHT",1); define("SIDE_RIGHT",1);
define("SIDE_DOWN",-1); define("SIDE_DOWN",-1);
define("SIDE_BOTTOM",-1); define("SIDE_BOTTOM",-1);
define("SIDE_UP",1); define("SIDE_UP",1);
define("SIDE_TOP",1); define("SIDE_TOP",1);
   
// Legend type stacked vertical or horizontal // Legend type stacked vertical or horizontal
define("LEGEND_VERT",0); define("LEGEND_VERT",0);
define("LEGEND_HOR",1); define("LEGEND_HOR",1);
   
// Mark types for plot marks // Mark types for plot marks
define("MARK_SQUARE",1); define("MARK_SQUARE",1);
define("MARK_UTRIANGLE",2); define("MARK_UTRIANGLE",2);
define("MARK_DTRIANGLE",3); define("MARK_DTRIANGLE",3);
define("MARK_DIAMOND",4); define("MARK_DIAMOND",4);
define("MARK_CIRCLE",5); define("MARK_CIRCLE",5);
define("MARK_FILLEDCIRCLE",6); define("MARK_FILLEDCIRCLE",6);
define("MARK_CROSS",7); define("MARK_CROSS",7);
define("MARK_STAR",8); define("MARK_STAR",8);
define("MARK_X",9); define("MARK_X",9);
define("MARK_LEFTTRIANGLE",10); define("MARK_LEFTTRIANGLE",10);
define("MARK_RIGHTTRIANGLE",11); define("MARK_RIGHTTRIANGLE",11);
define("MARK_FLASH",12); define("MARK_FLASH",12);
define("MARK_IMG",13); define("MARK_IMG",13);
define("MARK_FLAG1",14); define("MARK_FLAG1",14);
define("MARK_FLAG2",15); define("MARK_FLAG2",15);
define("MARK_FLAG3",16); define("MARK_FLAG3",16);
define("MARK_FLAG4",17); define("MARK_FLAG4",17);
   
// Builtin images // Builtin images
define("MARK_IMG_PUSHPIN",50); define("MARK_IMG_PUSHPIN",50);
define("MARK_IMG_SPUSHPIN",50); define("MARK_IMG_SPUSHPIN",50);
define("MARK_IMG_LPUSHPIN",51); define("MARK_IMG_LPUSHPIN",51);
define("MARK_IMG_DIAMOND",52); define("MARK_IMG_DIAMOND",52);
define("MARK_IMG_SQUARE",53); define("MARK_IMG_SQUARE",53);
define("MARK_IMG_STAR",54); define("MARK_IMG_STAR",54);
define("MARK_IMG_BALL",55); define("MARK_IMG_BALL",55);
define("MARK_IMG_SBALL",55); define("MARK_IMG_SBALL",55);
define("MARK_IMG_MBALL",56); define("MARK_IMG_MBALL",56);
define("MARK_IMG_LBALL",57); define("MARK_IMG_LBALL",57);
define("MARK_IMG_BEVEL",58); define("MARK_IMG_BEVEL",58);
   
// Inline defines // Inline defines
define("INLINE_YES",1); define("INLINE_YES",1);
define("INLINE_NO",0); define("INLINE_NO",0);
   
// Format for background images // Format for background images
define("BGIMG_FILLPLOT",1); define("BGIMG_FILLPLOT",1);
define("BGIMG_FILLFRAME",2); define("BGIMG_FILLFRAME",2);
define("BGIMG_COPY",3); define("BGIMG_COPY",3);
define("BGIMG_CENTER",4); define("BGIMG_CENTER",4);
define("BGIMG_FREE",5); define("BGIMG_FREE",5);
   
// Depth of objects // Depth of objects
define("DEPTH_BACK",0); define("DEPTH_BACK",0);
define("DEPTH_FRONT",1); define("DEPTH_FRONT",1);
   
// Direction // Direction
define("VERTICAL",1); define("VERTICAL",1);
define("HORIZONTAL",0); define("HORIZONTAL",0);
   
// Axis styles for scientific style axis // Axis styles for scientific style axis
define('AXSTYLE_SIMPLE',1); define('AXSTYLE_SIMPLE',1);
define('AXSTYLE_BOXIN',2); define('AXSTYLE_BOXIN',2);
define('AXSTYLE_BOXOUT',3); define('AXSTYLE_BOXOUT',3);
define('AXSTYLE_YBOXIN',4); define('AXSTYLE_YBOXIN',4);
define('AXSTYLE_YBOXOUT',5); define('AXSTYLE_YBOXOUT',5);
   
// Style for title backgrounds // Style for title backgrounds
define('TITLEBKG_STYLE1',1); define('TITLEBKG_STYLE1',1);
define('TITLEBKG_STYLE2',2); define('TITLEBKG_STYLE2',2);
define('TITLEBKG_STYLE3',3); define('TITLEBKG_STYLE3',3);
define('TITLEBKG_FRAME_NONE',0); define('TITLEBKG_FRAME_NONE',0);
define('TITLEBKG_FRAME_FULL',1); define('TITLEBKG_FRAME_FULL',1);
define('TITLEBKG_FRAME_BOTTOM',2); define('TITLEBKG_FRAME_BOTTOM',2);
define('TITLEBKG_FRAME_BEVEL',3); define('TITLEBKG_FRAME_BEVEL',3);
define('TITLEBKG_FILLSTYLE_HSTRIPED',1); define('TITLEBKG_FILLSTYLE_HSTRIPED',1);
define('TITLEBKG_FILLSTYLE_VSTRIPED',2); define('TITLEBKG_FILLSTYLE_VSTRIPED',2);
define('TITLEBKG_FILLSTYLE_SOLID',3); define('TITLEBKG_FILLSTYLE_SOLID',3);
   
// Styles for axis labels background // Styles for axis labels background
define('LABELBKG_NONE',0); define('LABELBKG_NONE',0);
define('LABELBKG_XAXIS',1); define('LABELBKG_XAXIS',1);
define('LABELBKG_YAXIS',2); define('LABELBKG_YAXIS',2);
define('LABELBKG_XAXISFULL',3); define('LABELBKG_XAXISFULL',3);
define('LABELBKG_YAXISFULL',4); define('LABELBKG_YAXISFULL',4);
define('LABELBKG_XYFULL',5); define('LABELBKG_XYFULL',5);
define('LABELBKG_XY',6); define('LABELBKG_XY',6);
   
   
// Style for background gradient fills // Style for background gradient fills
define('BGRAD_FRAME',1); define('BGRAD_FRAME',1);
define('BGRAD_MARGIN',2); define('BGRAD_MARGIN',2);
define('BGRAD_PLOT',3); define('BGRAD_PLOT',3);
   
// Width of tab titles // Width of tab titles
define('TABTITLE_WIDTHFIT',0); define('TABTITLE_WIDTHFIT',0);
define('TABTITLE_WIDTHFULL',-1); define('TABTITLE_WIDTHFULL',-1);
   
// Defines for 3D skew directions // Defines for 3D skew directions
define('SKEW3D_UP',0); define('SKEW3D_UP',0);
define('SKEW3D_DOWN',1); define('SKEW3D_DOWN',1);
define('SKEW3D_LEFT',2); define('SKEW3D_LEFT',2);
define('SKEW3D_RIGHT',3); define('SKEW3D_RIGHT',3);
   
// For internal use only // For internal use only
define("_JPG_DEBUG",false); define("_JPG_DEBUG",false);
define("_FORCE_IMGTOFILE",false); define("_FORCE_IMGTOFILE",false);
define("_FORCE_IMGDIR",'/tmp/jpgimg/'); define("_FORCE_IMGDIR",'/tmp/jpgimg/');
   
// //
// Automatic settings of path for cache and font directory // Automatic settings of path for cache and font directory
// if they have not been previously specified // if they have not been previously specified
// //
if(USE_CACHE) { if(USE_CACHE) {
if (!defined('CACHE_DIR')) { if (!defined('CACHE_DIR')) {
if ( strstr( PHP_OS, 'WIN') ) { if ( strstr( PHP_OS, 'WIN') ) {
if( empty($_SERVER['TEMP']) ) { if( empty($_SERVER['TEMP']) ) {
$t = new ErrMsgText(); $t = new ErrMsgText();
$msg = $t->Get(11,$file,$lineno); $msg = $t->Get(11,$file,$lineno);
die($msg); die($msg);
} }
else { else {
define('CACHE_DIR', $_SERVER['TEMP'] . '/'); define('CACHE_DIR', $_SERVER['TEMP'] . '/');
} }
} else { } else {
define('CACHE_DIR','/tmp/jpgraph_cache/'); define('CACHE_DIR','/tmp/jpgraph_cache/');
} }
} }
} }
elseif( !defined('CACHE_DIR') ) { elseif( !defined('CACHE_DIR') ) {
define('CACHE_DIR', ''); define('CACHE_DIR', '');
} }
   
// //
// Setup path for western/latin TTF fonts // Setup path for western/latin TTF fonts
// //
if (!defined('TTF_DIR')) { if (!defined('TTF_DIR')) {
if (strstr( PHP_OS, 'WIN') ) { if (strstr( PHP_OS, 'WIN') ) {
$sroot = getenv('SystemRoot'); $sroot = getenv('SystemRoot');
if( empty($sroot) ) { if( empty($sroot) ) {
$t = new ErrMsgText(); $t = new ErrMsgText();
$msg = $t->Get(12,$file,$lineno); $msg = $t->Get(12,$file,$lineno);
die($msg); die($msg);
} }
else { else {
define('TTF_DIR', $sroot.'/fonts/'); define('TTF_DIR', $sroot.'/fonts/');
} }
} else { } else {
define('TTF_DIR','/usr/share/fonts/truetype/'); define('TTF_DIR','/usr/share/fonts/truetype/');
} }
} }
   
// //
// Setup path for MultiByte TTF fonts (japanese, chinese etc.) // Setup path for MultiByte TTF fonts (japanese, chinese etc.)
// //
if (!defined('MBTTF_DIR')) { if (!defined('MBTTF_DIR')) {
if (strstr( PHP_OS, 'WIN') ) { if (strstr( PHP_OS, 'WIN') ) {
$sroot = getenv('SystemRoot'); $sroot = getenv('SystemRoot');
if( empty($sroot) ) { if( empty($sroot) ) {
$t = new ErrMsgText(); $t = new ErrMsgText();
$msg = $t->Get(12,$file,$lineno); $msg = $t->Get(12,$file,$lineno);
die($msg); die($msg);
} }
else { else {
define('MBTTF_DIR', $sroot.'/fonts/'); define('MBTTF_DIR', $sroot.'/fonts/');
} }
} else { } else {
define('MBTTF_DIR','/usr/share/fonts/truetype/'); define('MBTTF_DIR','/usr/share/fonts/truetype/');
} }
} }
   
// //
// Check minimum PHP version // Check minimum PHP version
// //
function CheckPHPVersion($aMinVersion) { function CheckPHPVersion($aMinVersion) {
list($majorC, $minorC, $editC) = preg_split('/[\/.-]/', PHP_VERSION); list($majorC, $minorC, $editC) = preg_split('/[\/.-]/', PHP_VERSION);
list($majorR, $minorR, $editR) = preg_split('/[\/.-]/', $aMinVersion); list($majorR, $minorR, $editR) = preg_split('/[\/.-]/', $aMinVersion);
   
if ($majorC != $majorR) return false; if ($majorC != $majorR) return false;
if ($majorC < $majorR) return false; if ($majorC < $majorR) return false;
// same major - check minor // same major - check minor
if ($minorC > $minorR) return true; if ($minorC > $minorR) return true;
if ($minorC < $minorR) return false; if ($minorC < $minorR) return false;
// and same minor // and same minor
if ($editC >= $editR) return true; if ($editC >= $editR) return true;
return true; return true;
} }
   
// //
// Make sure PHP version is high enough // Make sure PHP version is high enough
// //
if( !CheckPHPVersion(MIN_PHPVERSION) ) { if( !CheckPHPVersion(MIN_PHPVERSION) ) {
JpGraphError::RaiseL(13,PHP_VERSION,MIN_PHPVERSION); JpGraphError::RaiseL(13,PHP_VERSION,MIN_PHPVERSION);
die(); die();
} }
   
// //
// Make GD sanity check // Make GD sanity check
// //
if( !function_exists("imagetypes") || !function_exists('imagecreatefromstring') ) { if( !function_exists("imagetypes") || !function_exists('imagecreatefromstring') ) {
JpGraphError::RaiseL(25001); JpGraphError::RaiseL(25001);
//("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)"); //("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)");
} }
   
// //
// Setup PHP error handler // Setup PHP error handler
// //
function _phpErrorHandler($errno,$errmsg,$filename, $linenum, $vars) { function _phpErrorHandler($errno,$errmsg,$filename, $linenum, $vars) {
// Respect current error level // Respect current error level
if( $errno & error_reporting() ) { if( $errno & error_reporting() ) {
JpGraphError::RaiseL(25003,basename($filename),$linenum,$errmsg); JpGraphError::RaiseL(25003,basename($filename),$linenum,$errmsg);
} }
} }
   
if( INSTALL_PHP_ERR_HANDLER ) { if( INSTALL_PHP_ERR_HANDLER ) {
set_error_handler("_phpErrorHandler"); set_error_handler("_phpErrorHandler");
} }
   
// //
// Check if there were any warnings, perhaps some wrong includes by the user. In this // Check if there were any warnings, perhaps some wrong includes by the user. In this
// case we raise it immediately since otherwise the image will not show and makes // case we raise it immediately since otherwise the image will not show and makes
// debugging difficult. This is controlled by the user setting CATCH_PHPERRMSG // debugging difficult. This is controlled by the user setting CATCH_PHPERRMSG
// //
if( isset($GLOBALS['php_errormsg']) && CATCH_PHPERRMSG && !preg_match('/|Deprecated|/i', $GLOBALS['php_errormsg']) ) { if( isset($GLOBALS['php_errormsg']) && CATCH_PHPERRMSG && !preg_match('/|Deprecated|/i', $GLOBALS['php_errormsg']) ) {
JpGraphError::RaiseL(25004,$GLOBALS['php_errormsg']); JpGraphError::RaiseL(25004,$GLOBALS['php_errormsg']);
} }
   
// Useful mathematical function // Useful mathematical function
function sign($a) {return $a >= 0 ? 1 : -1;} function sign($a) {return $a >= 0 ? 1 : -1;}
   
// //
// Utility function to generate an image name based on the filename we // Utility function to generate an image name based on the filename we
// are running from and assuming we use auto detection of graphic format // are running from and assuming we use auto detection of graphic format
// (top level), i.e it is safe to call this function // (top level), i.e it is safe to call this function
// from a script that uses JpGraph // from a script that uses JpGraph
// //
function GenImgName() { function GenImgName() {
// Determine what format we should use when we save the images // Determine what format we should use when we save the images
$supported = imagetypes(); $supported = imagetypes();
if( $supported & IMG_PNG ) $img_format="png"; if( $supported & IMG_PNG ) $img_format="png";
elseif( $supported & IMG_GIF ) $img_format="gif"; elseif( $supported & IMG_GIF ) $img_format="gif";
elseif( $supported & IMG_JPG ) $img_format="jpeg"; elseif( $supported & IMG_JPG ) $img_format="jpeg";
elseif( $supported & IMG_WBMP ) $img_format="wbmp"; elseif( $supported & IMG_WBMP ) $img_format="wbmp";
elseif( $supported & IMG_XPM ) $img_format="xpm"; elseif( $supported & IMG_XPM ) $img_format="xpm";
   
   
if( !isset($_SERVER['PHP_SELF']) ) { if( !isset($_SERVER['PHP_SELF']) ) {
JpGraphError::RaiseL(25005); JpGraphError::RaiseL(25005);
//(" 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."); //(" 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.");
} }
$fname = basename($_SERVER['PHP_SELF']); $fname = basename($_SERVER['PHP_SELF']);
if( !empty($_SERVER['QUERY_STRING']) ) { if( !empty($_SERVER['QUERY_STRING']) ) {
$q = @$_SERVER['QUERY_STRING']; $q = @$_SERVER['QUERY_STRING'];
$fname .= '_'.preg_replace("/\W/", "_", $q).'.'.$img_format; $fname .= '_'.preg_replace("/\W/", "_", $q).'.'.$img_format;
} }
else { else {
$fname = substr($fname,0,strlen($fname)-4).'.'.$img_format; $fname = substr($fname,0,strlen($fname)-4).'.'.$img_format;
} }
return $fname; return $fname;
} }
   
//=================================================== //===================================================
// CLASS JpgTimer // CLASS JpgTimer
// Description: General timing utility class to handle // Description: General timing utility class to handle
// time measurement of generating graphs. Multiple // time measurement of generating graphs. Multiple
// timers can be started. // timers can be started.
//=================================================== //===================================================
class JpgTimer { class JpgTimer {
private $start, $idx; private $start, $idx;
   
function __construct() { function __construct() {
$this->idx=0; $this->idx=0;
} }
   
// Push a new timer start on stack // Push a new timer start on stack
function Push() { function Push() {
list($ms,$s)=explode(" ",microtime()); list($ms,$s)=explode(" ",microtime());
$this->start[$this->idx++]=floor($ms*1000) + 1000*$s; $this->start[$this->idx++]=floor($ms*1000) + 1000*$s;
} }
   
// Pop the latest timer start and return the diff with the // Pop the latest timer start and return the diff with the
// current time // current time
function Pop() { function Pop() {
assert($this->idx>0); assert($this->idx>0);
list($ms,$s)=explode(" ",microtime()); list($ms,$s)=explode(" ",microtime());
$etime=floor($ms*1000) + (1000*$s); $etime=floor($ms*1000) + (1000*$s);
$this->idx--; $this->idx--;
return $etime-$this->start[$this->idx]; return $etime-$this->start[$this->idx];
} }
} // Class } // Class
   
//=================================================== //===================================================
// CLASS DateLocale // CLASS DateLocale
// Description: Hold localized text used in dates // Description: Hold localized text used in dates
//=================================================== //===================================================
class DateLocale { class DateLocale {
   
public $iLocale = 'C'; // environmental locale be used by default public $iLocale = 'C'; // environmental locale be used by default
private $iDayAbb = null, $iShortDay = null, $iShortMonth = null, $iMonthName = null; private $iDayAbb = null, $iShortDay = null, $iShortMonth = null, $iMonthName = null;
   
function __construct() { function __construct() {
settype($this->iDayAbb, 'array'); settype($this->iDayAbb, 'array');
settype($this->iShortDay, 'array'); settype($this->iShortDay, 'array');
settype($this->iShortMonth, 'array'); settype($this->iShortMonth, 'array');
settype($this->iMonthName, 'array'); settype($this->iMonthName, 'array');
$this->Set('C'); $this->Set('C');
} }
   
function Set($aLocale) { function Set($aLocale) {
if ( in_array($aLocale, array_keys($this->iDayAbb)) ){ if ( in_array($aLocale, array_keys($this->iDayAbb)) ){
$this->iLocale = $aLocale; $this->iLocale = $aLocale;