From 123de95d0e2ae76ba70444547659ddce16a8270d Mon Sep 17 00:00:00 2001 From: spotweb Date: Wed, 4 Jul 2012 00:54:25 +0200 Subject: [PATCH] Line ending fixes --- .gitattributes | 2 +- Crypt/Hash.php | 1630 +- Crypt/Random.php | 258 +- INSTALL | 54 +- Math/BigInteger.php | 7088 ++++----- Mobile_Detect.php | 440 +- js/dynatree/MIT-License.txt | 12 +- js/dynatree/jquery.dynatree.min.js | 538 +- js/dynatree/skin-vista/ui.dynatree.css | 912 +- js/fancybox/jquery.fancybox-1.3.4.css | 716 +- js/jquery/jquery.hotkeys.js | 196 +- js/posting/posting.js | 430 +- lib/SpotNotificationTemplate.php | 98 +- lib/SpotTranslation.php | 98 +- lib/SpotWebVersionCheck.php | 136 +- lib/dbstruct/SpotStruct_pgsql.php | 866 +- lib/exceptions/CustomException.php | 84 +- .../DatabaseConnectionException.php | 2 +- lib/exceptions/InvalidLocalDirException.php | 4 +- .../InvalidOwnSettingsSettingException.php | 4 +- .../InvalidSettingsUpdateException.php | 4 +- lib/exceptions/NntpException.php | 26 +- .../OwnsettingsCreatedOutputException.php | 4 +- lib/exceptions/ParseSpotXmlException.php | 4 +- lib/exceptions/PermissionDeniedException.php | 42 +- lib/exceptions/RetrieverRunningException.php | 4 +- lib/exceptions/SchemaNotUpgradedException.php | 4 +- .../SecurityNotUpgradedException.php | 4 +- .../SettingsNotUpgradedException.php | 4 +- ...SpotwebCannotBeUpgradedTooOldException.php | 2 +- lib/exceptions/SqlErrorException.php | 4 +- lib/notifications/Notifications_Boxcar.php | 46 +- lib/notifications/Notifications_Email.php | 42 +- lib/notifications/Notifications_Factory.php | 64 +- lib/notifications/Notifications_Growl.php | 48 +- lib/notifications/Notifications_Notifo.php | 50 +- lib/notifications/Notifications_Prowl.php | 96 +- lib/notifications/Notifications_Twitter.php | 110 +- lib/notifications/Notifications_abs.php | 22 +- lib/page/SpotPage_erasedls.php | 28 +- lib/page/SpotPage_logout.php | 54 +- lib/page/SpotPage_markallasread.php | 50 +- lib/page/SpotPage_sabapi.php | 82 +- lib/page/SpotPage_versioncheck.php | 32 +- notifications.xml | 558 +- settings.php | 300 +- .../mobile/includes/form-messages.inc.php | 32 +- templates/mobile/login.inc.php | 108 +- templates/mobile/logincontrol.inc.php | 10 +- templates/mobile/permdenied.inc.php | 98 +- vendor/pchart/pData.class.php | 1572 +- vendor/pchart/pDraw.class.php | 12384 ++++++++-------- vendor/pchart/pImage.class.php | 942 +- vendor/pchart/pPie.class.php | 2998 ++-- 54 files changed, 16698 insertions(+), 16698 deletions(-) diff --git a/.gitattributes b/.gitattributes index a582a1793..412eeda78 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,5 @@ # Auto detect text files and perform LF normalization -* text=LF +* text=auto # Custom for Visual Studio *.cs diff=csharp diff --git a/Crypt/Hash.php b/Crypt/Hash.php index ef3a85802..e4dfde331 100755 --- a/Crypt/Hash.php +++ b/Crypt/Hash.php @@ -1,816 +1,816 @@ - - * setKey('abcdefg'); - * - * echo base64_encode($hash->hash('abcdefg')); - * ?> - * - * - * LICENSE: This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, - * MA 02111-1307 USA - * - * @category Crypt - * @package Crypt_Hash - * @author Jim Wigginton - * @copyright MMVII Jim Wigginton - * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: Hash.php,v 1.6 2009/11/23 23:37:07 terrafrost Exp $ - * @link http://phpseclib.sourceforge.net - */ - -/**#@+ - * @access private - * @see Crypt_Hash::Crypt_Hash() - */ -/** - * Toggles the internal implementation - */ -define('CRYPT_HASH_MODE_INTERNAL', 1); -/** - * Toggles the mhash() implementation, which has been deprecated on PHP 5.3.0+. - */ -define('CRYPT_HASH_MODE_MHASH', 2); -/** - * Toggles the hash() implementation, which works on PHP 5.1.2+. - */ -define('CRYPT_HASH_MODE_HASH', 3); -/**#@-*/ - -/** - * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions. - * - * @author Jim Wigginton - * @version 0.1.0 - * @access public - * @package Crypt_Hash - */ -class Crypt_Hash { - /** - * Byte-length of compression blocks / key (Internal HMAC) - * - * @see Crypt_Hash::setAlgorithm() - * @var Integer - * @access private - */ - var $b; - - /** - * Byte-length of hash output (Internal HMAC) - * - * @see Crypt_Hash::setHash() - * @var Integer - * @access private - */ - var $l = false; - - /** - * Hash Algorithm - * - * @see Crypt_Hash::setHash() - * @var String - * @access private - */ - var $hash; - - /** - * Key - * - * @see Crypt_Hash::setKey() - * @var String - * @access private - */ - var $key = ''; - - /** - * Outer XOR (Internal HMAC) - * - * @see Crypt_Hash::setKey() - * @var String - * @access private - */ - var $opad; - - /** - * Inner XOR (Internal HMAC) - * - * @see Crypt_Hash::setKey() - * @var String - * @access private - */ - var $ipad; - - /** - * Default Constructor. - * - * @param optional String $hash - * @return Crypt_Hash - * @access public - */ - function Crypt_Hash($hash = 'sha1') - { - if ( !defined('CRYPT_HASH_MODE') ) { - switch (true) { - case extension_loaded('hash'): - define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_HASH); - break; - case extension_loaded('mhash'): - define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_MHASH); - break; - default: - define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_INTERNAL); - } - } - - $this->setHash($hash); - } - - /** - * Sets the key for HMACs - * - * Keys can be of any length. - * - * @access public - * @param String $key - */ - function setKey($key) - { - $this->key = $key; - } - - /** - * Sets the hash function. - * - * @access public - * @param String $hash - */ - function setHash($hash) - { - switch ($hash) { - case 'md5-96': - case 'sha1-96': - $this->l = 12; // 96 / 8 = 12 - break; - case 'md2': - case 'md5': - $this->l = 16; - break; - case 'sha1': - $this->l = 20; - break; - case 'sha256': - $this->l = 32; - break; - case 'sha384': - $this->l = 48; - break; - case 'sha512': - $this->l = 64; - } - - switch ($hash) { - case 'md2': - $mode = CRYPT_HASH_MODE_INTERNAL; - break; - case 'sha384': - case 'sha512': - $mode = CRYPT_HASH_MODE == CRYPT_HASH_MODE_MHASH ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE; - break; - default: - $mode = CRYPT_HASH_MODE; - } - - switch ( $mode ) { - case CRYPT_HASH_MODE_MHASH: - switch ($hash) { - case 'md5': - case 'md5-96': - $this->hash = MHASH_MD5; - break; - case 'sha256': - $this->hash = MHASH_SHA256; - break; - case 'sha1': - case 'sha1-96': - default: - $this->hash = MHASH_SHA1; - } - return; - case CRYPT_HASH_MODE_HASH: - switch ($hash) { - case 'md5': - case 'md5-96': - $this->hash = 'md5'; - return; - case 'sha256': - case 'sha384': - case 'sha512': - $this->hash = $hash; - return; - case 'sha1': - case 'sha1-96': - default: - $this->hash = 'sha1'; - } - return; - } - - switch ($hash) { - case 'md2': - $this->b = 16; - $this->hash = array($this, '_md2'); - break; - case 'md5': - case 'md5-96': - $this->b = 64; - $this->hash = array($this, '_md5'); - break; - case 'sha256': - $this->b = 64; - $this->hash = array($this, '_sha256'); - break; - case 'sha384': - case 'sha512': - $this->b = 128; - $this->hash = array($this, '_sha512'); - break; - case 'sha1': - case 'sha1-96': - default: - $this->b = 64; - $this->hash = array($this, '_sha1'); - } - - $this->ipad = str_repeat(chr(0x36), $this->b); - $this->opad = str_repeat(chr(0x5C), $this->b); - } - - /** - * Compute the HMAC. - * - * @access public - * @param String $text - * @return String - */ - function hash($text) - { - $mode = is_array($this->hash) ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE; - - if (!empty($this->key)) { - switch ( $mode ) { - case CRYPT_HASH_MODE_MHASH: - $output = mhash($this->hash, $text, $this->key); - break; - case CRYPT_HASH_MODE_HASH: - $output = hash_hmac($this->hash, $text, $this->key, true); - break; - case CRYPT_HASH_MODE_INTERNAL: - /* "Applications that use keys longer than B bytes will first hash the key using H and then use the - resultant L byte string as the actual key to HMAC." - - -- http://tools.ietf.org/html/rfc2104#section-2 */ - $key = strlen($this->key) > $this->b ? call_user_func($this->$hash, $this->key) : $this->key; - - $key = str_pad($key, $this->b, chr(0)); // step 1 - $temp = $this->ipad ^ $key; // step 2 - $temp .= $text; // step 3 - $temp = call_user_func($this->hash, $temp); // step 4 - $output = $this->opad ^ $key; // step 5 - $output.= $temp; // step 6 - $output = call_user_func($this->hash, $output); // step 7 - } - } else { - switch ( $mode ) { - case CRYPT_HASH_MODE_MHASH: - $output = mhash($this->hash, $text); - break; - case CRYPT_HASH_MODE_HASH: - $output = hash($this->hash, $text, true); - break; - case CRYPT_HASH_MODE_INTERNAL: - $output = call_user_func($this->hash, $text); - } - } - - return substr($output, 0, $this->l); - } - - /** - * Returns the hash length (in bytes) - * - * @access private - * @return Integer - */ - function getLength() - { - return $this->l; - } - - /** - * Wrapper for MD5 - * - * @access private - * @param String $text - */ - function _md5($m) - { - return pack('H*', md5($m)); - } - - /** - * Wrapper for SHA1 - * - * @access private - * @param String $text - */ - function _sha1($m) - { - return pack('H*', sha1($m)); - } - - /** - * Pure-PHP implementation of MD2 - * - * See {@link http://tools.ietf.org/html/rfc1319 RFC1319}. - * - * @access private - * @param String $text - */ - function _md2($m) - { - static $s = array( - 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, - 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, - 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, - 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, - 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, - 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, - 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, - 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, - 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, - 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, - 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, - 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, - 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, - 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, - 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, - 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, - 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, - 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 - ); - - // Step 1. Append Padding Bytes - $pad = 16 - (strlen($m) & 0xF); - $m.= str_repeat(chr($pad), $pad); - - $length = strlen($m); - - // Step 2. Append Checksum - $c = str_repeat(chr(0), 16); - $l = chr(0); - for ($i = 0; $i < $length; $i+= 16) { - for ($j = 0; $j < 16; $j++) { - $c[$j] = chr($s[ord($m[$i + $j] ^ $l)]); - $l = $c[$j]; - } - } - $m.= $c; - - $length+= 16; - - // Step 3. Initialize MD Buffer - $x = str_repeat(chr(0), 48); - - // Step 4. Process Message in 16-Byte Blocks - for ($i = 0; $i < $length; $i+= 16) { - for ($j = 0; $j < 16; $j++) { - $x[$j + 16] = $m[$i + $j]; - $x[$j + 32] = $x[$j + 16] ^ $x[$j]; - } - $t = chr(0); - for ($j = 0; $j < 18; $j++) { - for ($k = 0; $k < 48; $k++) { - $x[$k] = $t = $x[$k] ^ chr($s[ord($t)]); - //$t = $x[$k] = $x[$k] ^ chr($s[ord($t)]); - } - $t = chr(ord($t) + $j); - } - } - - // Step 5. Output - return substr($x, 0, 16); - } - - /** - * Pure-PHP implementation of SHA256 - * - * See {@link http://en.wikipedia.org/wiki/SHA_hash_functions#SHA-256_.28a_SHA-2_variant.29_pseudocode SHA-256 (a SHA-2 variant) pseudocode - Wikipedia}. - * - * @access private - * @param String $text - */ - function _sha256($m) - { - if (extension_loaded('suhosin')) { - return pack('H*', sha256($m)); - } - - // Initialize variables - $hash = array( - 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 - ); - // Initialize table of round constants - // (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311) - static $k = array( - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 - ); - - // Pre-processing - $length = strlen($m); - // to round to nearest 56 mod 64, we'll add 64 - (length + (64 - 56)) % 64 - $m.= str_repeat(chr(0), 64 - (($length + 8) & 0x3F)); - $m[$length] = chr(0x80); - // we don't support hashing strings 512MB long - $m.= pack('N2', 0, $length << 3); - - // Process the message in successive 512-bit chunks - $chunks = str_split($m, 64); - foreach ($chunks as $chunk) { - $w = array(); - for ($i = 0; $i < 16; $i++) { - extract(unpack('Ntemp', $this->_string_shift($chunk, 4))); - $w[] = $temp; - } - - // Extend the sixteen 32-bit words into sixty-four 32-bit words - for ($i = 16; $i < 64; $i++) { - $s0 = $this->_rightRotate($w[$i - 15], 7) ^ - $this->_rightRotate($w[$i - 15], 18) ^ - $this->_rightShift( $w[$i - 15], 3); - $s1 = $this->_rightRotate($w[$i - 2], 17) ^ - $this->_rightRotate($w[$i - 2], 19) ^ - $this->_rightShift( $w[$i - 2], 10); - $w[$i] = $this->_add($w[$i - 16], $s0, $w[$i - 7], $s1); - - } - - // Initialize hash value for this chunk - list($a, $b, $c, $d, $e, $f, $g, $h) = $hash; - - // Main loop - for ($i = 0; $i < 64; $i++) { - $s0 = $this->_rightRotate($a, 2) ^ - $this->_rightRotate($a, 13) ^ - $this->_rightRotate($a, 22); - $maj = ($a & $b) ^ - ($a & $c) ^ - ($b & $c); - $t2 = $this->_add($s0, $maj); - - $s1 = $this->_rightRotate($e, 6) ^ - $this->_rightRotate($e, 11) ^ - $this->_rightRotate($e, 25); - $ch = ($e & $f) ^ - ($this->_not($e) & $g); - $t1 = $this->_add($h, $s1, $ch, $k[$i], $w[$i]); - - $h = $g; - $g = $f; - $f = $e; - $e = $this->_add($d, $t1); - $d = $c; - $c = $b; - $b = $a; - $a = $this->_add($t1, $t2); - } - - // Add this chunk's hash to result so far - $hash = array( - $this->_add($hash[0], $a), - $this->_add($hash[1], $b), - $this->_add($hash[2], $c), - $this->_add($hash[3], $d), - $this->_add($hash[4], $e), - $this->_add($hash[5], $f), - $this->_add($hash[6], $g), - $this->_add($hash[7], $h) - ); - } - - // Produce the final hash value (big-endian) - return pack('N8', $hash[0], $hash[1], $hash[2], $hash[3], $hash[4], $hash[5], $hash[6], $hash[7]); - } - - /** - * Pure-PHP implementation of SHA384 and SHA512 - * - * @access private - * @param String $text - */ - function _sha512($m) - { - if (!class_exists('Math_BigInteger')) { - require_once('Math/BigInteger.php'); - } - - static $init384, $init512, $k; - - if (!isset($k)) { - // Initialize variables - $init384 = array( // initial values for SHA384 - 'cbbb9d5dc1059ed8', '629a292a367cd507', '9159015a3070dd17', '152fecd8f70e5939', - '67332667ffc00b31', '8eb44a8768581511', 'db0c2e0d64f98fa7', '47b5481dbefa4fa4' - ); - $init512 = array( // initial values for SHA512 - '6a09e667f3bcc908', 'bb67ae8584caa73b', '3c6ef372fe94f82b', 'a54ff53a5f1d36f1', - '510e527fade682d1', '9b05688c2b3e6c1f', '1f83d9abfb41bd6b', '5be0cd19137e2179' - ); - - for ($i = 0; $i < 8; $i++) { - $init384[$i] = new Math_BigInteger($init384[$i], 16); - $init384[$i]->setPrecision(64); - $init512[$i] = new Math_BigInteger($init512[$i], 16); - $init512[$i]->setPrecision(64); - } - - // Initialize table of round constants - // (first 64 bits of the fractional parts of the cube roots of the first 80 primes 2..409) - $k = array( - '428a2f98d728ae22', '7137449123ef65cd', 'b5c0fbcfec4d3b2f', 'e9b5dba58189dbbc', - '3956c25bf348b538', '59f111f1b605d019', '923f82a4af194f9b', 'ab1c5ed5da6d8118', - 'd807aa98a3030242', '12835b0145706fbe', '243185be4ee4b28c', '550c7dc3d5ffb4e2', - '72be5d74f27b896f', '80deb1fe3b1696b1', '9bdc06a725c71235', 'c19bf174cf692694', - 'e49b69c19ef14ad2', 'efbe4786384f25e3', '0fc19dc68b8cd5b5', '240ca1cc77ac9c65', - '2de92c6f592b0275', '4a7484aa6ea6e483', '5cb0a9dcbd41fbd4', '76f988da831153b5', - '983e5152ee66dfab', 'a831c66d2db43210', 'b00327c898fb213f', 'bf597fc7beef0ee4', - 'c6e00bf33da88fc2', 'd5a79147930aa725', '06ca6351e003826f', '142929670a0e6e70', - '27b70a8546d22ffc', '2e1b21385c26c926', '4d2c6dfc5ac42aed', '53380d139d95b3df', - '650a73548baf63de', '766a0abb3c77b2a8', '81c2c92e47edaee6', '92722c851482353b', - 'a2bfe8a14cf10364', 'a81a664bbc423001', 'c24b8b70d0f89791', 'c76c51a30654be30', - 'd192e819d6ef5218', 'd69906245565a910', 'f40e35855771202a', '106aa07032bbd1b8', - '19a4c116b8d2d0c8', '1e376c085141ab53', '2748774cdf8eeb99', '34b0bcb5e19b48a8', - '391c0cb3c5c95a63', '4ed8aa4ae3418acb', '5b9cca4f7763e373', '682e6ff3d6b2b8a3', - '748f82ee5defb2fc', '78a5636f43172f60', '84c87814a1f0ab72', '8cc702081a6439ec', - '90befffa23631e28', 'a4506cebde82bde9', 'bef9a3f7b2c67915', 'c67178f2e372532b', - 'ca273eceea26619c', 'd186b8c721c0c207', 'eada7dd6cde0eb1e', 'f57d4f7fee6ed178', - '06f067aa72176fba', '0a637dc5a2c898a6', '113f9804bef90dae', '1b710b35131c471b', - '28db77f523047d84', '32caab7b40c72493', '3c9ebe0a15c9bebc', '431d67c49c100d4c', - '4cc5d4becb3e42b6', '597f299cfc657e2a', '5fcb6fab3ad6faec', '6c44198c4a475817' - ); - - for ($i = 0; $i < 80; $i++) { - $k[$i] = new Math_BigInteger($k[$i], 16); - } - } - - $hash = $this->l == 48 ? $init384 : $init512; - - // Pre-processing - $length = strlen($m); - // to round to nearest 112 mod 128, we'll add 128 - (length + (128 - 112)) % 128 - $m.= str_repeat(chr(0), 128 - (($length + 16) & 0x7F)); - $m[$length] = chr(0x80); - // we don't support hashing strings 512MB long - $m.= pack('N4', 0, 0, 0, $length << 3); - - // Process the message in successive 1024-bit chunks - $chunks = str_split($m, 128); - foreach ($chunks as $chunk) { - $w = array(); - for ($i = 0; $i < 16; $i++) { - $temp = new Math_BigInteger($this->_string_shift($chunk, 8), 256); - $temp->setPrecision(64); - $w[] = $temp; - } - - // Extend the sixteen 32-bit words into eighty 32-bit words - for ($i = 16; $i < 80; $i++) { - $temp = array( - $w[$i - 15]->bitwise_rightRotate(1), - $w[$i - 15]->bitwise_rightRotate(8), - $w[$i - 15]->bitwise_rightShift(7) - ); - $s0 = $temp[0]->bitwise_xor($temp[1]); - $s0 = $s0->bitwise_xor($temp[2]); - $temp = array( - $w[$i - 2]->bitwise_rightRotate(19), - $w[$i - 2]->bitwise_rightRotate(61), - $w[$i - 2]->bitwise_rightShift(6) - ); - $s1 = $temp[0]->bitwise_xor($temp[1]); - $s1 = $s1->bitwise_xor($temp[2]); - $w[$i] = $w[$i - 16]->copy(); - $w[$i] = $w[$i]->add($s0); - $w[$i] = $w[$i]->add($w[$i - 7]); - $w[$i] = $w[$i]->add($s1); - } - - // Initialize hash value for this chunk - $a = $hash[0]->copy(); - $b = $hash[1]->copy(); - $c = $hash[2]->copy(); - $d = $hash[3]->copy(); - $e = $hash[4]->copy(); - $f = $hash[5]->copy(); - $g = $hash[6]->copy(); - $h = $hash[7]->copy(); - - // Main loop - for ($i = 0; $i < 80; $i++) { - $temp = array( - $a->bitwise_rightRotate(28), - $a->bitwise_rightRotate(34), - $a->bitwise_rightRotate(39) - ); - $s0 = $temp[0]->bitwise_xor($temp[1]); - $s0 = $s0->bitwise_xor($temp[2]); - $temp = array( - $a->bitwise_and($b), - $a->bitwise_and($c), - $b->bitwise_and($c) - ); - $maj = $temp[0]->bitwise_xor($temp[1]); - $maj = $maj->bitwise_xor($temp[2]); - $t2 = $s0->add($maj); - - $temp = array( - $e->bitwise_rightRotate(14), - $e->bitwise_rightRotate(18), - $e->bitwise_rightRotate(41) - ); - $s1 = $temp[0]->bitwise_xor($temp[1]); - $s1 = $s1->bitwise_xor($temp[2]); - $temp = array( - $e->bitwise_and($f), - $g->bitwise_and($e->bitwise_not()) - ); - $ch = $temp[0]->bitwise_xor($temp[1]); - $t1 = $h->add($s1); - $t1 = $t1->add($ch); - $t1 = $t1->add($k[$i]); - $t1 = $t1->add($w[$i]); - - $h = $g->copy(); - $g = $f->copy(); - $f = $e->copy(); - $e = $d->add($t1); - $d = $c->copy(); - $c = $b->copy(); - $b = $a->copy(); - $a = $t1->add($t2); - } - - // Add this chunk's hash to result so far - $hash = array( - $hash[0]->add($a), - $hash[1]->add($b), - $hash[2]->add($c), - $hash[3]->add($d), - $hash[4]->add($e), - $hash[5]->add($f), - $hash[6]->add($g), - $hash[7]->add($h) - ); - } - - // Produce the final hash value (big-endian) - // (Crypt_Hash::hash() trims the output for hashes but not for HMACs. as such, we trim the output here) - $temp = $hash[0]->toBytes() . $hash[1]->toBytes() . $hash[2]->toBytes() . $hash[3]->toBytes() . - $hash[4]->toBytes() . $hash[5]->toBytes(); - if ($this->l != 48) { - $temp.= $hash[6]->toBytes() . $hash[7]->toBytes(); - } - - return $temp; - } - - /** - * Right Rotate - * - * @access private - * @param Integer $int - * @param Integer $amt - * @see _sha256() - * @return Integer - */ - function _rightRotate($int, $amt) - { - $invamt = 32 - $amt; - $mask = (1 << $invamt) - 1; - return (($int << $invamt) & 0xFFFFFFFF) | (($int >> $amt) & $mask); - } - - /** - * Right Shift - * - * @access private - * @param Integer $int - * @param Integer $amt - * @see _sha256() - * @return Integer - */ - function _rightShift($int, $amt) - { - $mask = (1 << (32 - $amt)) - 1; - return ($int >> $amt) & $mask; - } - - /** - * Not - * - * @access private - * @param Integer $int - * @see _sha256() - * @return Integer - */ - function _not($int) - { - return ~$int & 0xFFFFFFFF; - } - - /** - * Add - * - * _sha256() adds multiple unsigned 32-bit integers. Since PHP doesn't support unsigned integers and since the - * possibility of overflow exists, care has to be taken. Math_BigInteger() could be used but this should be faster. - * - * @param String $string - * @param optional Integer $index - * @return String - * @see _sha256() - * @access private - */ - function _add() - { - static $mod; - if (!isset($mod)) { - $mod = pow(2, 32); - } - - $result = 0; - $arguments = func_get_args(); - foreach ($arguments as $argument) { - $result+= $argument < 0 ? ($argument & 0x7FFFFFFF) + 0x80000000 : $argument; - } - - return fmod($result, $mod); - } - - /** - * String Shift - * - * Inspired by array_shift - * - * @param String $string - * @param optional Integer $index - * @return String - * @access private - */ - function _string_shift(&$string, $index = 1) - { - $substr = substr($string, 0, $index); - $string = substr($string, $index); - return $substr; - } + + * setKey('abcdefg'); + * + * echo base64_encode($hash->hash('abcdefg')); + * ?> + * + * + * LICENSE: This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * @category Crypt + * @package Crypt_Hash + * @author Jim Wigginton + * @copyright MMVII Jim Wigginton + * @license http://www.gnu.org/licenses/lgpl.txt + * @version $Id: Hash.php,v 1.6 2009/11/23 23:37:07 terrafrost Exp $ + * @link http://phpseclib.sourceforge.net + */ + +/**#@+ + * @access private + * @see Crypt_Hash::Crypt_Hash() + */ +/** + * Toggles the internal implementation + */ +define('CRYPT_HASH_MODE_INTERNAL', 1); +/** + * Toggles the mhash() implementation, which has been deprecated on PHP 5.3.0+. + */ +define('CRYPT_HASH_MODE_MHASH', 2); +/** + * Toggles the hash() implementation, which works on PHP 5.1.2+. + */ +define('CRYPT_HASH_MODE_HASH', 3); +/**#@-*/ + +/** + * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions. + * + * @author Jim Wigginton + * @version 0.1.0 + * @access public + * @package Crypt_Hash + */ +class Crypt_Hash { + /** + * Byte-length of compression blocks / key (Internal HMAC) + * + * @see Crypt_Hash::setAlgorithm() + * @var Integer + * @access private + */ + var $b; + + /** + * Byte-length of hash output (Internal HMAC) + * + * @see Crypt_Hash::setHash() + * @var Integer + * @access private + */ + var $l = false; + + /** + * Hash Algorithm + * + * @see Crypt_Hash::setHash() + * @var String + * @access private + */ + var $hash; + + /** + * Key + * + * @see Crypt_Hash::setKey() + * @var String + * @access private + */ + var $key = ''; + + /** + * Outer XOR (Internal HMAC) + * + * @see Crypt_Hash::setKey() + * @var String + * @access private + */ + var $opad; + + /** + * Inner XOR (Internal HMAC) + * + * @see Crypt_Hash::setKey() + * @var String + * @access private + */ + var $ipad; + + /** + * Default Constructor. + * + * @param optional String $hash + * @return Crypt_Hash + * @access public + */ + function Crypt_Hash($hash = 'sha1') + { + if ( !defined('CRYPT_HASH_MODE') ) { + switch (true) { + case extension_loaded('hash'): + define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_HASH); + break; + case extension_loaded('mhash'): + define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_MHASH); + break; + default: + define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_INTERNAL); + } + } + + $this->setHash($hash); + } + + /** + * Sets the key for HMACs + * + * Keys can be of any length. + * + * @access public + * @param String $key + */ + function setKey($key) + { + $this->key = $key; + } + + /** + * Sets the hash function. + * + * @access public + * @param String $hash + */ + function setHash($hash) + { + switch ($hash) { + case 'md5-96': + case 'sha1-96': + $this->l = 12; // 96 / 8 = 12 + break; + case 'md2': + case 'md5': + $this->l = 16; + break; + case 'sha1': + $this->l = 20; + break; + case 'sha256': + $this->l = 32; + break; + case 'sha384': + $this->l = 48; + break; + case 'sha512': + $this->l = 64; + } + + switch ($hash) { + case 'md2': + $mode = CRYPT_HASH_MODE_INTERNAL; + break; + case 'sha384': + case 'sha512': + $mode = CRYPT_HASH_MODE == CRYPT_HASH_MODE_MHASH ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE; + break; + default: + $mode = CRYPT_HASH_MODE; + } + + switch ( $mode ) { + case CRYPT_HASH_MODE_MHASH: + switch ($hash) { + case 'md5': + case 'md5-96': + $this->hash = MHASH_MD5; + break; + case 'sha256': + $this->hash = MHASH_SHA256; + break; + case 'sha1': + case 'sha1-96': + default: + $this->hash = MHASH_SHA1; + } + return; + case CRYPT_HASH_MODE_HASH: + switch ($hash) { + case 'md5': + case 'md5-96': + $this->hash = 'md5'; + return; + case 'sha256': + case 'sha384': + case 'sha512': + $this->hash = $hash; + return; + case 'sha1': + case 'sha1-96': + default: + $this->hash = 'sha1'; + } + return; + } + + switch ($hash) { + case 'md2': + $this->b = 16; + $this->hash = array($this, '_md2'); + break; + case 'md5': + case 'md5-96': + $this->b = 64; + $this->hash = array($this, '_md5'); + break; + case 'sha256': + $this->b = 64; + $this->hash = array($this, '_sha256'); + break; + case 'sha384': + case 'sha512': + $this->b = 128; + $this->hash = array($this, '_sha512'); + break; + case 'sha1': + case 'sha1-96': + default: + $this->b = 64; + $this->hash = array($this, '_sha1'); + } + + $this->ipad = str_repeat(chr(0x36), $this->b); + $this->opad = str_repeat(chr(0x5C), $this->b); + } + + /** + * Compute the HMAC. + * + * @access public + * @param String $text + * @return String + */ + function hash($text) + { + $mode = is_array($this->hash) ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE; + + if (!empty($this->key)) { + switch ( $mode ) { + case CRYPT_HASH_MODE_MHASH: + $output = mhash($this->hash, $text, $this->key); + break; + case CRYPT_HASH_MODE_HASH: + $output = hash_hmac($this->hash, $text, $this->key, true); + break; + case CRYPT_HASH_MODE_INTERNAL: + /* "Applications that use keys longer than B bytes will first hash the key using H and then use the + resultant L byte string as the actual key to HMAC." + + -- http://tools.ietf.org/html/rfc2104#section-2 */ + $key = strlen($this->key) > $this->b ? call_user_func($this->$hash, $this->key) : $this->key; + + $key = str_pad($key, $this->b, chr(0)); // step 1 + $temp = $this->ipad ^ $key; // step 2 + $temp .= $text; // step 3 + $temp = call_user_func($this->hash, $temp); // step 4 + $output = $this->opad ^ $key; // step 5 + $output.= $temp; // step 6 + $output = call_user_func($this->hash, $output); // step 7 + } + } else { + switch ( $mode ) { + case CRYPT_HASH_MODE_MHASH: + $output = mhash($this->hash, $text); + break; + case CRYPT_HASH_MODE_HASH: + $output = hash($this->hash, $text, true); + break; + case CRYPT_HASH_MODE_INTERNAL: + $output = call_user_func($this->hash, $text); + } + } + + return substr($output, 0, $this->l); + } + + /** + * Returns the hash length (in bytes) + * + * @access private + * @return Integer + */ + function getLength() + { + return $this->l; + } + + /** + * Wrapper for MD5 + * + * @access private + * @param String $text + */ + function _md5($m) + { + return pack('H*', md5($m)); + } + + /** + * Wrapper for SHA1 + * + * @access private + * @param String $text + */ + function _sha1($m) + { + return pack('H*', sha1($m)); + } + + /** + * Pure-PHP implementation of MD2 + * + * See {@link http://tools.ietf.org/html/rfc1319 RFC1319}. + * + * @access private + * @param String $text + */ + function _md2($m) + { + static $s = array( + 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, + 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, + 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, + 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, + 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, + 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, + 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, + 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, + 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, + 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, + 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, + 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, + 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, + 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, + 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, + 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, + 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, + 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 + ); + + // Step 1. Append Padding Bytes + $pad = 16 - (strlen($m) & 0xF); + $m.= str_repeat(chr($pad), $pad); + + $length = strlen($m); + + // Step 2. Append Checksum + $c = str_repeat(chr(0), 16); + $l = chr(0); + for ($i = 0; $i < $length; $i+= 16) { + for ($j = 0; $j < 16; $j++) { + $c[$j] = chr($s[ord($m[$i + $j] ^ $l)]); + $l = $c[$j]; + } + } + $m.= $c; + + $length+= 16; + + // Step 3. Initialize MD Buffer + $x = str_repeat(chr(0), 48); + + // Step 4. Process Message in 16-Byte Blocks + for ($i = 0; $i < $length; $i+= 16) { + for ($j = 0; $j < 16; $j++) { + $x[$j + 16] = $m[$i + $j]; + $x[$j + 32] = $x[$j + 16] ^ $x[$j]; + } + $t = chr(0); + for ($j = 0; $j < 18; $j++) { + for ($k = 0; $k < 48; $k++) { + $x[$k] = $t = $x[$k] ^ chr($s[ord($t)]); + //$t = $x[$k] = $x[$k] ^ chr($s[ord($t)]); + } + $t = chr(ord($t) + $j); + } + } + + // Step 5. Output + return substr($x, 0, 16); + } + + /** + * Pure-PHP implementation of SHA256 + * + * See {@link http://en.wikipedia.org/wiki/SHA_hash_functions#SHA-256_.28a_SHA-2_variant.29_pseudocode SHA-256 (a SHA-2 variant) pseudocode - Wikipedia}. + * + * @access private + * @param String $text + */ + function _sha256($m) + { + if (extension_loaded('suhosin')) { + return pack('H*', sha256($m)); + } + + // Initialize variables + $hash = array( + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + ); + // Initialize table of round constants + // (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311) + static $k = array( + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + ); + + // Pre-processing + $length = strlen($m); + // to round to nearest 56 mod 64, we'll add 64 - (length + (64 - 56)) % 64 + $m.= str_repeat(chr(0), 64 - (($length + 8) & 0x3F)); + $m[$length] = chr(0x80); + // we don't support hashing strings 512MB long + $m.= pack('N2', 0, $length << 3); + + // Process the message in successive 512-bit chunks + $chunks = str_split($m, 64); + foreach ($chunks as $chunk) { + $w = array(); + for ($i = 0; $i < 16; $i++) { + extract(unpack('Ntemp', $this->_string_shift($chunk, 4))); + $w[] = $temp; + } + + // Extend the sixteen 32-bit words into sixty-four 32-bit words + for ($i = 16; $i < 64; $i++) { + $s0 = $this->_rightRotate($w[$i - 15], 7) ^ + $this->_rightRotate($w[$i - 15], 18) ^ + $this->_rightShift( $w[$i - 15], 3); + $s1 = $this->_rightRotate($w[$i - 2], 17) ^ + $this->_rightRotate($w[$i - 2], 19) ^ + $this->_rightShift( $w[$i - 2], 10); + $w[$i] = $this->_add($w[$i - 16], $s0, $w[$i - 7], $s1); + + } + + // Initialize hash value for this chunk + list($a, $b, $c, $d, $e, $f, $g, $h) = $hash; + + // Main loop + for ($i = 0; $i < 64; $i++) { + $s0 = $this->_rightRotate($a, 2) ^ + $this->_rightRotate($a, 13) ^ + $this->_rightRotate($a, 22); + $maj = ($a & $b) ^ + ($a & $c) ^ + ($b & $c); + $t2 = $this->_add($s0, $maj); + + $s1 = $this->_rightRotate($e, 6) ^ + $this->_rightRotate($e, 11) ^ + $this->_rightRotate($e, 25); + $ch = ($e & $f) ^ + ($this->_not($e) & $g); + $t1 = $this->_add($h, $s1, $ch, $k[$i], $w[$i]); + + $h = $g; + $g = $f; + $f = $e; + $e = $this->_add($d, $t1); + $d = $c; + $c = $b; + $b = $a; + $a = $this->_add($t1, $t2); + } + + // Add this chunk's hash to result so far + $hash = array( + $this->_add($hash[0], $a), + $this->_add($hash[1], $b), + $this->_add($hash[2], $c), + $this->_add($hash[3], $d), + $this->_add($hash[4], $e), + $this->_add($hash[5], $f), + $this->_add($hash[6], $g), + $this->_add($hash[7], $h) + ); + } + + // Produce the final hash value (big-endian) + return pack('N8', $hash[0], $hash[1], $hash[2], $hash[3], $hash[4], $hash[5], $hash[6], $hash[7]); + } + + /** + * Pure-PHP implementation of SHA384 and SHA512 + * + * @access private + * @param String $text + */ + function _sha512($m) + { + if (!class_exists('Math_BigInteger')) { + require_once('Math/BigInteger.php'); + } + + static $init384, $init512, $k; + + if (!isset($k)) { + // Initialize variables + $init384 = array( // initial values for SHA384 + 'cbbb9d5dc1059ed8', '629a292a367cd507', '9159015a3070dd17', '152fecd8f70e5939', + '67332667ffc00b31', '8eb44a8768581511', 'db0c2e0d64f98fa7', '47b5481dbefa4fa4' + ); + $init512 = array( // initial values for SHA512 + '6a09e667f3bcc908', 'bb67ae8584caa73b', '3c6ef372fe94f82b', 'a54ff53a5f1d36f1', + '510e527fade682d1', '9b05688c2b3e6c1f', '1f83d9abfb41bd6b', '5be0cd19137e2179' + ); + + for ($i = 0; $i < 8; $i++) { + $init384[$i] = new Math_BigInteger($init384[$i], 16); + $init384[$i]->setPrecision(64); + $init512[$i] = new Math_BigInteger($init512[$i], 16); + $init512[$i]->setPrecision(64); + } + + // Initialize table of round constants + // (first 64 bits of the fractional parts of the cube roots of the first 80 primes 2..409) + $k = array( + '428a2f98d728ae22', '7137449123ef65cd', 'b5c0fbcfec4d3b2f', 'e9b5dba58189dbbc', + '3956c25bf348b538', '59f111f1b605d019', '923f82a4af194f9b', 'ab1c5ed5da6d8118', + 'd807aa98a3030242', '12835b0145706fbe', '243185be4ee4b28c', '550c7dc3d5ffb4e2', + '72be5d74f27b896f', '80deb1fe3b1696b1', '9bdc06a725c71235', 'c19bf174cf692694', + 'e49b69c19ef14ad2', 'efbe4786384f25e3', '0fc19dc68b8cd5b5', '240ca1cc77ac9c65', + '2de92c6f592b0275', '4a7484aa6ea6e483', '5cb0a9dcbd41fbd4', '76f988da831153b5', + '983e5152ee66dfab', 'a831c66d2db43210', 'b00327c898fb213f', 'bf597fc7beef0ee4', + 'c6e00bf33da88fc2', 'd5a79147930aa725', '06ca6351e003826f', '142929670a0e6e70', + '27b70a8546d22ffc', '2e1b21385c26c926', '4d2c6dfc5ac42aed', '53380d139d95b3df', + '650a73548baf63de', '766a0abb3c77b2a8', '81c2c92e47edaee6', '92722c851482353b', + 'a2bfe8a14cf10364', 'a81a664bbc423001', 'c24b8b70d0f89791', 'c76c51a30654be30', + 'd192e819d6ef5218', 'd69906245565a910', 'f40e35855771202a', '106aa07032bbd1b8', + '19a4c116b8d2d0c8', '1e376c085141ab53', '2748774cdf8eeb99', '34b0bcb5e19b48a8', + '391c0cb3c5c95a63', '4ed8aa4ae3418acb', '5b9cca4f7763e373', '682e6ff3d6b2b8a3', + '748f82ee5defb2fc', '78a5636f43172f60', '84c87814a1f0ab72', '8cc702081a6439ec', + '90befffa23631e28', 'a4506cebde82bde9', 'bef9a3f7b2c67915', 'c67178f2e372532b', + 'ca273eceea26619c', 'd186b8c721c0c207', 'eada7dd6cde0eb1e', 'f57d4f7fee6ed178', + '06f067aa72176fba', '0a637dc5a2c898a6', '113f9804bef90dae', '1b710b35131c471b', + '28db77f523047d84', '32caab7b40c72493', '3c9ebe0a15c9bebc', '431d67c49c100d4c', + '4cc5d4becb3e42b6', '597f299cfc657e2a', '5fcb6fab3ad6faec', '6c44198c4a475817' + ); + + for ($i = 0; $i < 80; $i++) { + $k[$i] = new Math_BigInteger($k[$i], 16); + } + } + + $hash = $this->l == 48 ? $init384 : $init512; + + // Pre-processing + $length = strlen($m); + // to round to nearest 112 mod 128, we'll add 128 - (length + (128 - 112)) % 128 + $m.= str_repeat(chr(0), 128 - (($length + 16) & 0x7F)); + $m[$length] = chr(0x80); + // we don't support hashing strings 512MB long + $m.= pack('N4', 0, 0, 0, $length << 3); + + // Process the message in successive 1024-bit chunks + $chunks = str_split($m, 128); + foreach ($chunks as $chunk) { + $w = array(); + for ($i = 0; $i < 16; $i++) { + $temp = new Math_BigInteger($this->_string_shift($chunk, 8), 256); + $temp->setPrecision(64); + $w[] = $temp; + } + + // Extend the sixteen 32-bit words into eighty 32-bit words + for ($i = 16; $i < 80; $i++) { + $temp = array( + $w[$i - 15]->bitwise_rightRotate(1), + $w[$i - 15]->bitwise_rightRotate(8), + $w[$i - 15]->bitwise_rightShift(7) + ); + $s0 = $temp[0]->bitwise_xor($temp[1]); + $s0 = $s0->bitwise_xor($temp[2]); + $temp = array( + $w[$i - 2]->bitwise_rightRotate(19), + $w[$i - 2]->bitwise_rightRotate(61), + $w[$i - 2]->bitwise_rightShift(6) + ); + $s1 = $temp[0]->bitwise_xor($temp[1]); + $s1 = $s1->bitwise_xor($temp[2]); + $w[$i] = $w[$i - 16]->copy(); + $w[$i] = $w[$i]->add($s0); + $w[$i] = $w[$i]->add($w[$i - 7]); + $w[$i] = $w[$i]->add($s1); + } + + // Initialize hash value for this chunk + $a = $hash[0]->copy(); + $b = $hash[1]->copy(); + $c = $hash[2]->copy(); + $d = $hash[3]->copy(); + $e = $hash[4]->copy(); + $f = $hash[5]->copy(); + $g = $hash[6]->copy(); + $h = $hash[7]->copy(); + + // Main loop + for ($i = 0; $i < 80; $i++) { + $temp = array( + $a->bitwise_rightRotate(28), + $a->bitwise_rightRotate(34), + $a->bitwise_rightRotate(39) + ); + $s0 = $temp[0]->bitwise_xor($temp[1]); + $s0 = $s0->bitwise_xor($temp[2]); + $temp = array( + $a->bitwise_and($b), + $a->bitwise_and($c), + $b->bitwise_and($c) + ); + $maj = $temp[0]->bitwise_xor($temp[1]); + $maj = $maj->bitwise_xor($temp[2]); + $t2 = $s0->add($maj); + + $temp = array( + $e->bitwise_rightRotate(14), + $e->bitwise_rightRotate(18), + $e->bitwise_rightRotate(41) + ); + $s1 = $temp[0]->bitwise_xor($temp[1]); + $s1 = $s1->bitwise_xor($temp[2]); + $temp = array( + $e->bitwise_and($f), + $g->bitwise_and($e->bitwise_not()) + ); + $ch = $temp[0]->bitwise_xor($temp[1]); + $t1 = $h->add($s1); + $t1 = $t1->add($ch); + $t1 = $t1->add($k[$i]); + $t1 = $t1->add($w[$i]); + + $h = $g->copy(); + $g = $f->copy(); + $f = $e->copy(); + $e = $d->add($t1); + $d = $c->copy(); + $c = $b->copy(); + $b = $a->copy(); + $a = $t1->add($t2); + } + + // Add this chunk's hash to result so far + $hash = array( + $hash[0]->add($a), + $hash[1]->add($b), + $hash[2]->add($c), + $hash[3]->add($d), + $hash[4]->add($e), + $hash[5]->add($f), + $hash[6]->add($g), + $hash[7]->add($h) + ); + } + + // Produce the final hash value (big-endian) + // (Crypt_Hash::hash() trims the output for hashes but not for HMACs. as such, we trim the output here) + $temp = $hash[0]->toBytes() . $hash[1]->toBytes() . $hash[2]->toBytes() . $hash[3]->toBytes() . + $hash[4]->toBytes() . $hash[5]->toBytes(); + if ($this->l != 48) { + $temp.= $hash[6]->toBytes() . $hash[7]->toBytes(); + } + + return $temp; + } + + /** + * Right Rotate + * + * @access private + * @param Integer $int + * @param Integer $amt + * @see _sha256() + * @return Integer + */ + function _rightRotate($int, $amt) + { + $invamt = 32 - $amt; + $mask = (1 << $invamt) - 1; + return (($int << $invamt) & 0xFFFFFFFF) | (($int >> $amt) & $mask); + } + + /** + * Right Shift + * + * @access private + * @param Integer $int + * @param Integer $amt + * @see _sha256() + * @return Integer + */ + function _rightShift($int, $amt) + { + $mask = (1 << (32 - $amt)) - 1; + return ($int >> $amt) & $mask; + } + + /** + * Not + * + * @access private + * @param Integer $int + * @see _sha256() + * @return Integer + */ + function _not($int) + { + return ~$int & 0xFFFFFFFF; + } + + /** + * Add + * + * _sha256() adds multiple unsigned 32-bit integers. Since PHP doesn't support unsigned integers and since the + * possibility of overflow exists, care has to be taken. Math_BigInteger() could be used but this should be faster. + * + * @param String $string + * @param optional Integer $index + * @return String + * @see _sha256() + * @access private + */ + function _add() + { + static $mod; + if (!isset($mod)) { + $mod = pow(2, 32); + } + + $result = 0; + $arguments = func_get_args(); + foreach ($arguments as $argument) { + $result+= $argument < 0 ? ($argument & 0x7FFFFFFF) + 0x80000000 : $argument; + } + + return fmod($result, $mod); + } + + /** + * String Shift + * + * Inspired by array_shift + * + * @param String $string + * @param optional Integer $index + * @return String + * @access private + */ + function _string_shift(&$string, $index = 1) + { + $substr = substr($string, 0, $index); + $string = substr($string, $index); + return $substr; + } } \ No newline at end of file diff --git a/Crypt/Random.php b/Crypt/Random.php index a91c4d3c5..221cba533 100755 --- a/Crypt/Random.php +++ b/Crypt/Random.php @@ -1,130 +1,130 @@ - - * - * - * - * LICENSE: This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, - * MA 02111-1307 USA - * - * @category Crypt - * @package Crypt_Random - * @author Jim Wigginton - * @copyright MMVII Jim Wigginton - * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: Random.php,v 1.9 2010/04/24 06:40:48 terrafrost Exp $ - * @link http://phpseclib.sourceforge.net - */ - -/** - * Generate a random value. - * - * On 32-bit machines, the largest distance that can exist between $min and $max is 2**31. - * If $min and $max are farther apart than that then the last ($max - range) numbers. - * - * Depending on how this is being used, it may be worth while to write a replacement. For example, - * a PHP-based web app that stores its data in an SQL database can collect more entropy than this function - * can. - * - * @param optional Integer $min - * @param optional Integer $max - * @return Integer - * @access public - */ -function crypt_random($min = 0, $max = 0x7FFFFFFF) -{ - if ($min == $max) { - return $min; - } - - // see http://en.wikipedia.org/wiki//dev/random - // if open_basedir is enabled file_exists() will ouput an "open_basedir restriction in effect" warning, - // so we suppress it. - if (@file_exists('/dev/urandom')) { - static $fp; - if (!$fp) { - $fp = fopen('/dev/urandom', 'rb'); - } - extract(unpack('Nrandom', fread($fp, 4))); - - // say $min = 0 and $max = 3. if we didn't do abs() then we could have stuff like this: - // -4 % 3 + 0 = -1, even though -1 < $min - return abs($random) % ($max - $min) + $min; - } - - /* Prior to PHP 4.2.0, mt_srand() had to be called before mt_rand() could be called. - Prior to PHP 5.2.6, mt_rand()'s automatic seeding was subpar, as elaborated here: - - http://www.suspekt.org/2008/08/17/mt_srand-and-not-so-random-numbers/ - - The seeding routine is pretty much ripped from PHP's own internal GENERATE_SEED() macro: - - http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3_2/ext/standard/php_rand.h?view=markup */ - if (version_compare(PHP_VERSION, '5.2.5', '<=')) { - static $seeded; - if (!isset($seeded)) { - $seeded = true; - mt_srand(fmod(time() * getmypid(), 0x7FFFFFFF) ^ fmod(1000000 * lcg_value(), 0x7FFFFFFF)); - } - } - - static $crypto; - - // The CSPRNG's Yarrow and Fortuna periodically reseed. This function can be reseeded by hitting F5 - // in the browser and reloading the page. - - if (!isset($crypto)) { - $key = $iv = ''; - for ($i = 0; $i < 8; $i++) { - $key.= pack('n', mt_rand(0, 0xFFFF)); - $iv .= pack('n', mt_rand(0, 0xFFFF)); - } - switch (true) { - case class_exists('Crypt_AES'): - $crypto = new Crypt_AES(CRYPT_AES_MODE_CTR); - break; - case class_exists('Crypt_TripleDES'): - $crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR); - break; - case class_exists('Crypt_DES'): - $crypto = new Crypt_DES(CRYPT_DES_MODE_CTR); - break; - case class_exists('Crypt_RC4'): - $crypto = new Crypt_RC4(); - break; - default: - extract(unpack('Nrandom', pack('H*', sha1(mt_rand(0, 0x7FFFFFFF))))); - return abs($random) % ($max - $min) + $min; - } - $crypto->setKey($key); - $crypto->setIV($iv); - $crypto->enableContinuousBuffer(); - } - - extract(unpack('Nrandom', $crypto->encrypt("\0\0\0\0"))); - return abs($random) % ($max - $min) + $min; -} + + * + * + * + * LICENSE: This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * @category Crypt + * @package Crypt_Random + * @author Jim Wigginton + * @copyright MMVII Jim Wigginton + * @license http://www.gnu.org/licenses/lgpl.txt + * @version $Id: Random.php,v 1.9 2010/04/24 06:40:48 terrafrost Exp $ + * @link http://phpseclib.sourceforge.net + */ + +/** + * Generate a random value. + * + * On 32-bit machines, the largest distance that can exist between $min and $max is 2**31. + * If $min and $max are farther apart than that then the last ($max - range) numbers. + * + * Depending on how this is being used, it may be worth while to write a replacement. For example, + * a PHP-based web app that stores its data in an SQL database can collect more entropy than this function + * can. + * + * @param optional Integer $min + * @param optional Integer $max + * @return Integer + * @access public + */ +function crypt_random($min = 0, $max = 0x7FFFFFFF) +{ + if ($min == $max) { + return $min; + } + + // see http://en.wikipedia.org/wiki//dev/random + // if open_basedir is enabled file_exists() will ouput an "open_basedir restriction in effect" warning, + // so we suppress it. + if (@file_exists('/dev/urandom')) { + static $fp; + if (!$fp) { + $fp = fopen('/dev/urandom', 'rb'); + } + extract(unpack('Nrandom', fread($fp, 4))); + + // say $min = 0 and $max = 3. if we didn't do abs() then we could have stuff like this: + // -4 % 3 + 0 = -1, even though -1 < $min + return abs($random) % ($max - $min) + $min; + } + + /* Prior to PHP 4.2.0, mt_srand() had to be called before mt_rand() could be called. + Prior to PHP 5.2.6, mt_rand()'s automatic seeding was subpar, as elaborated here: + + http://www.suspekt.org/2008/08/17/mt_srand-and-not-so-random-numbers/ + + The seeding routine is pretty much ripped from PHP's own internal GENERATE_SEED() macro: + + http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3_2/ext/standard/php_rand.h?view=markup */ + if (version_compare(PHP_VERSION, '5.2.5', '<=')) { + static $seeded; + if (!isset($seeded)) { + $seeded = true; + mt_srand(fmod(time() * getmypid(), 0x7FFFFFFF) ^ fmod(1000000 * lcg_value(), 0x7FFFFFFF)); + } + } + + static $crypto; + + // The CSPRNG's Yarrow and Fortuna periodically reseed. This function can be reseeded by hitting F5 + // in the browser and reloading the page. + + if (!isset($crypto)) { + $key = $iv = ''; + for ($i = 0; $i < 8; $i++) { + $key.= pack('n', mt_rand(0, 0xFFFF)); + $iv .= pack('n', mt_rand(0, 0xFFFF)); + } + switch (true) { + case class_exists('Crypt_AES'): + $crypto = new Crypt_AES(CRYPT_AES_MODE_CTR); + break; + case class_exists('Crypt_TripleDES'): + $crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CTR); + break; + case class_exists('Crypt_DES'): + $crypto = new Crypt_DES(CRYPT_DES_MODE_CTR); + break; + case class_exists('Crypt_RC4'): + $crypto = new Crypt_RC4(); + break; + default: + extract(unpack('Nrandom', pack('H*', sha1(mt_rand(0, 0x7FFFFFFF))))); + return abs($random) % ($max - $min) + $min; + } + $crypto->setKey($key); + $crypto->setIV($iv); + $crypto->enableContinuousBuffer(); + } + + extract(unpack('Nrandom', $crypto->encrypt("\0\0\0\0"))); + return abs($random) % ($max - $min) + $min; +} ?> \ No newline at end of file diff --git a/INSTALL b/INSTALL index 4f45dcc09..1ea6d072e 100755 --- a/INSTALL +++ b/INSTALL @@ -1,27 +1,27 @@ -== Installation requirements == -Spotweb has been regulary tested on several different systems. Spotweb is mostly used on: - -* Unix-based (Linux, FreeBSD) operating systems or small NAS systems like Synology and Qnap -* Apache Webserver -* PHP v5.3 or higher (modules required: ctyle, curl, DOM, gettext, mbstring, xml, zip, zlib, gd, openssl) -* MySQL webserver. - -Spotweb also supports PostgreSQL and SQLite where SQLite is the least supported and tested database engine. - -Please run 'testinstall.php' from within your browser before attempting anything further with Spotweb and make sure -all items are checked 'OK'. - -== Installation instructions == -1. Ensure you have an database server installed (either MySQL or PostgreSQL) -2. Create an empty 'spotweb' database -3. Ensure you have a webserver running and PHP is configured for this webserver -3. Download Spotweb -4. Unpack Spotweb to a directory of your choosing -5. Run 'install.php' from your browser until everything is 'OK'. Fix the parts which aren't OK. -6. Follow the wizard and perform the instructions as given by the wizard. - -== Troubleshooting == -When a white page appears instead of your Spotweb installation, this usually indicates an typing error in either -your ownsettings.php, dbsettings.inc.php or a configuration error in your webserver. - -Please consult your Apache's errorlog for the exact error and fix it. +== Installation requirements == +Spotweb has been regulary tested on several different systems. Spotweb is mostly used on: + +* Unix-based (Linux, FreeBSD) operating systems or small NAS systems like Synology and Qnap +* Apache Webserver +* PHP v5.3 or higher (modules required: ctyle, curl, DOM, gettext, mbstring, xml, zip, zlib, gd, openssl) +* MySQL webserver. + +Spotweb also supports PostgreSQL and SQLite where SQLite is the least supported and tested database engine. + +Please run 'testinstall.php' from within your browser before attempting anything further with Spotweb and make sure +all items are checked 'OK'. + +== Installation instructions == +1. Ensure you have an database server installed (either MySQL or PostgreSQL) +2. Create an empty 'spotweb' database +3. Ensure you have a webserver running and PHP is configured for this webserver +3. Download Spotweb +4. Unpack Spotweb to a directory of your choosing +5. Run 'install.php' from your browser until everything is 'OK'. Fix the parts which aren't OK. +6. Follow the wizard and perform the instructions as given by the wizard. + +== Troubleshooting == +When a white page appears instead of your Spotweb installation, this usually indicates an typing error in either +your ownsettings.php, dbsettings.inc.php or a configuration error in your webserver. + +Please consult your Apache's errorlog for the exact error and fix it. diff --git a/Math/BigInteger.php b/Math/BigInteger.php index 5b3a4fc8b..4373805f9 100755 --- a/Math/BigInteger.php +++ b/Math/BigInteger.php @@ -1,3545 +1,3545 @@ -> and << cannot be used, nor can the modulo operator %, - * which only supports integers. Although this fact will slow this library down, the fact that such a high - * base is being used should more than compensate. - * - * When PHP version 6 is officially released, we'll be able to use 64-bit integers. This should, once again, - * allow bitwise operators, and will increase the maximum possible base to 2**31 (or 2**62 for addition / - * subtraction). - * - * Numbers are stored in {@link http://en.wikipedia.org/wiki/Endianness little endian} format. ie. - * (new Math_BigInteger(pow(2, 26)))->value = array(0, 1) - * - * Useful resources are as follows: - * - * - {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf Handbook of Applied Cryptography (HAC)} - * - {@link http://math.libtomcrypt.com/files/tommath.pdf Multi-Precision Math (MPM)} - * - Java's BigInteger classes. See /j2se/src/share/classes/java/math in jdk-1_5_0-src-jrl.zip - * - * Here's an example of how to use this library: - * - * add($b); - * - * echo $c->toString(); // outputs 5 - * ?> - * - * - * LICENSE: This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, - * MA 02111-1307 USA - * - * @category Math - * @package Math_BigInteger - * @author Jim Wigginton - * @copyright MMVI Jim Wigginton - * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: BigInteger.php,v 1.33 2010/03/22 22:32:03 terrafrost Exp $ - * @link http://pear.php.net/package/Math_BigInteger - */ - -/**#@+ - * Reduction constants - * - * @access private - * @see Math_BigInteger::_reduce() - */ -/** - * @see Math_BigInteger::_montgomery() - * @see Math_BigInteger::_prepMontgomery() - */ -define('MATH_BIGINTEGER_MONTGOMERY', 0); -/** - * @see Math_BigInteger::_barrett() - */ -define('MATH_BIGINTEGER_BARRETT', 1); -/** - * @see Math_BigInteger::_mod2() - */ -define('MATH_BIGINTEGER_POWEROF2', 2); -/** - * @see Math_BigInteger::_remainder() - */ -define('MATH_BIGINTEGER_CLASSIC', 3); -/** - * @see Math_BigInteger::__clone() - */ -define('MATH_BIGINTEGER_NONE', 4); -/**#@-*/ - -/**#@+ - * Array constants - * - * Rather than create a thousands and thousands of new Math_BigInteger objects in repeated function calls to add() and - * multiply() or whatever, we'll just work directly on arrays, taking them in as parameters and returning them. - * - * @access private - */ -/** - * $result[MATH_BIGINTEGER_VALUE] contains the value. - */ -define('MATH_BIGINTEGER_VALUE', 0); -/** - * $result[MATH_BIGINTEGER_SIGN] contains the sign. - */ -define('MATH_BIGINTEGER_SIGN', 1); -/**#@-*/ - -/**#@+ - * @access private - * @see Math_BigInteger::_montgomery() - * @see Math_BigInteger::_barrett() - */ -/** - * Cache constants - * - * $cache[MATH_BIGINTEGER_VARIABLE] tells us whether or not the cached data is still valid. - */ -define('MATH_BIGINTEGER_VARIABLE', 0); -/** - * $cache[MATH_BIGINTEGER_DATA] contains the cached data. - */ -define('MATH_BIGINTEGER_DATA', 1); -/**#@-*/ - -/**#@+ - * Mode constants. - * - * @access private - * @see Math_BigInteger::Math_BigInteger() - */ -/** - * To use the pure-PHP implementation - */ -define('MATH_BIGINTEGER_MODE_INTERNAL', 1); -/** - * To use the BCMath library - * - * (if enabled; otherwise, the internal implementation will be used) - */ -define('MATH_BIGINTEGER_MODE_BCMATH', 2); -/** - * To use the GMP library - * - * (if present; otherwise, either the BCMath or the internal implementation will be used) - */ -define('MATH_BIGINTEGER_MODE_GMP', 3); -/**#@-*/ - -/** - * The largest digit that may be used in addition / subtraction - * - * (we do pow(2, 52) instead of using 4503599627370496, directly, because some PHP installations - * will truncate 4503599627370496) - * - * @access private - */ -define('MATH_BIGINTEGER_MAX_DIGIT52', pow(2, 52)); - -/** - * Karatsuba Cutoff - * - * At what point do we switch between Karatsuba multiplication and schoolbook long multiplication? - * - * @access private - */ -define('MATH_BIGINTEGER_KARATSUBA_CUTOFF', 25); - -/** - * Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 - * numbers. - * - * @author Jim Wigginton - * @version 1.0.0RC4 - * @access public - * @package Math_BigInteger - */ -class Math_BigInteger { - /** - * Holds the BigInteger's value. - * - * @var Array - * @access private - */ - var $value; - - /** - * Holds the BigInteger's magnitude. - * - * @var Boolean - * @access private - */ - var $is_negative = false; - - /** - * Random number generator function - * - * @see setRandomGenerator() - * @access private - */ - var $generator = 'mt_rand'; - - /** - * Precision - * - * @see setPrecision() - * @access private - */ - var $precision = -1; - - /** - * Precision Bitmask - * - * @see setPrecision() - * @access private - */ - var $bitmask = false; - - /** - * Mode independant value used for serialization. - * - * If the bcmath or gmp extensions are installed $this->value will be a non-serializable resource, hence the need for - * a variable that'll be serializable regardless of whether or not extensions are being used. Unlike $this->value, - * however, $this->hex is only calculated when $this->__sleep() is called. - * - * @see __sleep() - * @see __wakeup() - * @var String - * @access private - */ - var $hex; - - /** - * Converts base-2, base-10, base-16, and binary strings (eg. base-256) to BigIntegers. - * - * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using - * two's compliment. The sole exception to this is -10, which is treated the same as 10 is. - * - * Here's an example: - * - * toString(); // outputs 50 - * ?> - * - * - * @param optional $x base-10 number or base-$base number if $base set. - * @param optional integer $base - * @return Math_BigInteger - * @access public - */ - function Math_BigInteger($x = 0, $base = 10) - { - if ( !defined('MATH_BIGINTEGER_MODE') ) { - switch (true) { - case extension_loaded('gmp'): - define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_GMP); - break; - case extension_loaded('bcmath'): - define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_BCMATH); - break; - default: - define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_INTERNAL); - } - } - - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - if (is_resource($x) && get_resource_type($x) == 'GMP integer') { - $this->value = $x; - return; - } - $this->value = gmp_init(0); - break; - case MATH_BIGINTEGER_MODE_BCMATH: - $this->value = '0'; - break; - default: - $this->value = array(); - } - - if (empty($x)) { - return; - } - - switch ($base) { - case -256: - if (ord($x[0]) & 0x80) { - $x = ~$x; - $this->is_negative = true; - } - case 256: - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $sign = $this->is_negative ? '-' : ''; - $this->value = gmp_init($sign . '0x' . bin2hex($x)); - break; - case MATH_BIGINTEGER_MODE_BCMATH: - // round $len to the nearest 4 (thanks, DavidMJ!) - $len = (strlen($x) + 3) & 0xFFFFFFFC; - - $x = str_pad($x, $len, chr(0), STR_PAD_LEFT); - - for ($i = 0; $i < $len; $i+= 4) { - $this->value = bcmul($this->value, '4294967296', 0); // 4294967296 == 2**32 - $this->value = bcadd($this->value, 0x1000000 * ord($x[$i]) + ((ord($x[$i + 1]) << 16) | (ord($x[$i + 2]) << 8) | ord($x[$i + 3])), 0); - } - - if ($this->is_negative) { - $this->value = '-' . $this->value; - } - - break; - // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb) - default: - while (strlen($x)) { - $this->value[] = $this->_bytes2int($this->_base256_rshift($x, 26)); - } - } - - if ($this->is_negative) { - if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) { - $this->is_negative = false; - } - $temp = $this->add(new Math_BigInteger('-1')); - $this->value = $temp->value; - } - break; - case 16: - case -16: - if ($base > 0 && $x[0] == '-') { - $this->is_negative = true; - $x = substr($x, 1); - } - - $x = preg_replace('#^(?:0x)?([A-Fa-f0-9]*).*#', '$1', $x); - - $is_negative = false; - if ($base < 0 && hexdec($x[0]) >= 8) { - $this->is_negative = $is_negative = true; - $x = bin2hex(~pack('H*', $x)); - } - - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp = $this->is_negative ? '-0x' . $x : '0x' . $x; - $this->value = gmp_init($temp); - $this->is_negative = false; - break; - case MATH_BIGINTEGER_MODE_BCMATH: - $x = ( strlen($x) & 1 ) ? '0' . $x : $x; - $temp = new Math_BigInteger(pack('H*', $x), 256); - $this->value = $this->is_negative ? '-' . $temp->value : $temp->value; - $this->is_negative = false; - break; - default: - $x = ( strlen($x) & 1 ) ? '0' . $x : $x; - $temp = new Math_BigInteger(pack('H*', $x), 256); - $this->value = $temp->value; - } - - if ($is_negative) { - $temp = $this->add(new Math_BigInteger('-1')); - $this->value = $temp->value; - } - break; - case 10: - case -10: - $x = preg_replace('#^(-?[0-9]*).*#', '$1', $x); - - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $this->value = gmp_init($x); - break; - case MATH_BIGINTEGER_MODE_BCMATH: - // explicitly casting $x to a string is necessary, here, since doing $x[0] on -1 yields different - // results then doing it on '-1' does (modInverse does $x[0]) - $this->value = (string) $x; - break; - default: - $temp = new Math_BigInteger(); - - // array(10000000) is 10**7 in base-2**26. 10**7 is the closest to 2**26 we can get without passing it. - $multiplier = new Math_BigInteger(); - $multiplier->value = array(10000000); - - if ($x[0] == '-') { - $this->is_negative = true; - $x = substr($x, 1); - } - - $x = str_pad($x, strlen($x) + (6 * strlen($x)) % 7, 0, STR_PAD_LEFT); - - while (strlen($x)) { - $temp = $temp->multiply($multiplier); - $temp = $temp->add(new Math_BigInteger($this->_int2bytes(substr($x, 0, 7)), 256)); - $x = substr($x, 7); - } - - $this->value = $temp->value; - } - break; - case 2: // base-2 support originally implemented by Lluis Pamies - thanks! - case -2: - if ($base > 0 && $x[0] == '-') { - $this->is_negative = true; - $x = substr($x, 1); - } - - $x = preg_replace('#^([01]*).*#', '$1', $x); - $x = str_pad($x, strlen($x) + (3 * strlen($x)) % 4, 0, STR_PAD_LEFT); - - $str = '0x'; - while (strlen($x)) { - $part = substr($x, 0, 4); - $str.= dechex(bindec($part)); - $x = substr($x, 4); - } - - if ($this->is_negative) { - $str = '-' . $str; - } - - $temp = new Math_BigInteger($str, 8 * $base); // ie. either -16 or +16 - $this->value = $temp->value; - $this->is_negative = $temp->is_negative; - - break; - default: - // base not supported, so we'll let $this == 0 - } - } - - /** - * Converts a BigInteger to a byte string (eg. base-256). - * - * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're - * saved as two's compliment. - * - * Here's an example: - * - * toBytes(); // outputs chr(65) - * ?> - * - * - * @param Boolean $twos_compliment - * @return String - * @access public - * @internal Converts a base-2**26 number to base-2**8 - */ - function toBytes($twos_compliment = false) - { - if ($twos_compliment) { - $comparison = $this->compare(new Math_BigInteger()); - if ($comparison == 0) { - return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; - } - - $temp = $comparison < 0 ? $this->add(new Math_BigInteger(1)) : $this->copy(); - $bytes = $temp->toBytes(); - - if (empty($bytes)) { // eg. if the number we're trying to convert is -1 - $bytes = chr(0); - } - - if (ord($bytes[0]) & 0x80) { - $bytes = chr(0) . $bytes; - } - - return $comparison < 0 ? ~$bytes : $bytes; - } - - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - if (gmp_cmp($this->value, gmp_init(0)) == 0) { - return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; - } - - $temp = gmp_strval(gmp_abs($this->value), 16); - $temp = ( strlen($temp) & 1 ) ? '0' . $temp : $temp; - $temp = pack('H*', $temp); - - return $this->precision > 0 ? - substr(str_pad($temp, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) : - ltrim($temp, chr(0)); - case MATH_BIGINTEGER_MODE_BCMATH: - if ($this->value === '0') { - return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; - } - - $value = ''; - $current = $this->value; - - if ($current[0] == '-') { - $current = substr($current, 1); - } - - while (bccomp($current, '0', 0) > 0) { - $temp = bcmod($current, '16777216'); - $value = chr($temp >> 16) . chr($temp >> 8) . chr($temp) . $value; - $current = bcdiv($current, '16777216', 0); - } - - return $this->precision > 0 ? - substr(str_pad($value, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) : - ltrim($value, chr(0)); - } - - if (!count($this->value)) { - return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; - } - $result = $this->_int2bytes($this->value[count($this->value) - 1]); - - $temp = $this->copy(); - - for ($i = count($temp->value) - 2; $i >= 0; --$i) { - $temp->_base256_lshift($result, 26); - $result = $result | str_pad($temp->_int2bytes($temp->value[$i]), strlen($result), chr(0), STR_PAD_LEFT); - } - - return $this->precision > 0 ? - str_pad(substr($result, -(($this->precision + 7) >> 3)), ($this->precision + 7) >> 3, chr(0), STR_PAD_LEFT) : - $result; - } - - /** - * Converts a BigInteger to a hex string (eg. base-16)). - * - * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're - * saved as two's compliment. - * - * Here's an example: - * - * toHex(); // outputs '41' - * ?> - * - * - * @param Boolean $twos_compliment - * @return String - * @access public - * @internal Converts a base-2**26 number to base-2**8 - */ - function toHex($twos_compliment = false) - { - return bin2hex($this->toBytes($twos_compliment)); - } - - /** - * Converts a BigInteger to a bit string (eg. base-2). - * - * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're - * saved as two's compliment. - * - * Here's an example: - * - * toBits(); // outputs '1000001' - * ?> - * - * - * @param Boolean $twos_compliment - * @return String - * @access public - * @internal Converts a base-2**26 number to base-2**2 - */ - function toBits($twos_compliment = false) - { - $hex = $this->toHex($twos_compliment); - $bits = ''; - for ($i = 0; $i < strlen($hex); $i+=8) { - $bits.= str_pad(decbin(hexdec(substr($hex, $i, 8))), 32, '0', STR_PAD_LEFT); - } - return $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0'); - } - - /** - * Converts a BigInteger to a base-10 number. - * - * Here's an example: - * - * toString(); // outputs 50 - * ?> - * - * - * @return String - * @access public - * @internal Converts a base-2**26 number to base-10**7 (which is pretty much base-10) - */ - function toString() - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - return gmp_strval($this->value); - case MATH_BIGINTEGER_MODE_BCMATH: - if ($this->value === '0') { - return '0'; - } - - return ltrim($this->value, '0'); - } - - if (!count($this->value)) { - return '0'; - } - - $temp = $this->copy(); - $temp->is_negative = false; - - $divisor = new Math_BigInteger(); - $divisor->value = array(10000000); // eg. 10**7 - $result = ''; - while (count($temp->value)) { - list($temp, $mod) = $temp->divide($divisor); - $result = str_pad(isset($mod->value[0]) ? $mod->value[0] : '', 7, '0', STR_PAD_LEFT) . $result; - } - $result = ltrim($result, '0'); - if (empty($result)) { - $result = '0'; - } - - if ($this->is_negative) { - $result = '-' . $result; - } - - return $result; - } - - /** - * Copy an object - * - * PHP5 passes objects by reference while PHP4 passes by value. As such, we need a function to guarantee - * that all objects are passed by value, when appropriate. More information can be found here: - * - * {@link http://php.net/language.oop5.basic#51624} - * - * @access public - * @see __clone() - * @return Math_BigInteger - */ - function copy() - { - $temp = new Math_BigInteger(); - $temp->value = $this->value; - $temp->is_negative = $this->is_negative; - $temp->generator = $this->generator; - $temp->precision = $this->precision; - $temp->bitmask = $this->bitmask; - return $temp; - } - - /** - * __toString() magic method - * - * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call - * toString(). - * - * @access public - * @internal Implemented per a suggestion by Techie-Michael - thanks! - */ - function __toString() - { - return $this->toString(); - } - - /** - * __clone() magic method - * - * Although you can call Math_BigInteger::__toString() directly in PHP5, you cannot call Math_BigInteger::__clone() - * directly in PHP5. You can in PHP4 since it's not a magic method, but in PHP5, you have to call it by using the PHP5 - * only syntax of $y = clone $x. As such, if you're trying to write an application that works on both PHP4 and PHP5, - * call Math_BigInteger::copy(), instead. - * - * @access public - * @see copy() - * @return Math_BigInteger - */ - function __clone() - { - return $this->copy(); - } - - /** - * __sleep() magic method - * - * Will be called, automatically, when serialize() is called on a Math_BigInteger object. - * - * @see __wakeup() - * @access public - */ - function __sleep() - { - $this->hex = $this->toHex(true); - $vars = array('hex'); - if ($this->generator != 'mt_rand') { - $vars[] = 'generator'; - } - if ($this->precision > 0) { - $vars[] = 'precision'; - } - return $vars; - - } - - /** - * __wakeup() magic method - * - * Will be called, automatically, when unserialize() is called on a Math_BigInteger object. - * - * @see __sleep() - * @access public - */ - function __wakeup() - { - $temp = new Math_BigInteger($this->hex, -16); - $this->value = $temp->value; - $this->is_negative = $temp->is_negative; - $this->setRandomGenerator($this->generator); - if ($this->precision > 0) { - // recalculate $this->bitmask - $this->setPrecision($this->precision); - } - } - - /** - * Adds two BigIntegers. - * - * Here's an example: - * - * add($b); - * - * echo $c->toString(); // outputs 30 - * ?> - * - * - * @param Math_BigInteger $y - * @return Math_BigInteger - * @access public - * @internal Performs base-2**52 addition - */ - function add($y) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp = new Math_BigInteger(); - $temp->value = gmp_add($this->value, $y->value); - - return $this->_normalize($temp); - case MATH_BIGINTEGER_MODE_BCMATH: - $temp = new Math_BigInteger(); - $temp->value = bcadd($this->value, $y->value, 0); - - return $this->_normalize($temp); - } - - $temp = $this->_add($this->value, $this->is_negative, $y->value, $y->is_negative); - - $result = new Math_BigInteger(); - $result->value = $temp[MATH_BIGINTEGER_VALUE]; - $result->is_negative = $temp[MATH_BIGINTEGER_SIGN]; - - return $this->_normalize($result); - } - - /** - * Performs addition. - * - * @param Array $x_value - * @param Boolean $x_negative - * @param Array $y_value - * @param Boolean $y_negative - * @return Array - * @access private - */ - function _add($x_value, $x_negative, $y_value, $y_negative) - { - $x_size = count($x_value); - $y_size = count($y_value); - - if ($x_size == 0) { - return array( - MATH_BIGINTEGER_VALUE => $y_value, - MATH_BIGINTEGER_SIGN => $y_negative - ); - } else if ($y_size == 0) { - return array( - MATH_BIGINTEGER_VALUE => $x_value, - MATH_BIGINTEGER_SIGN => $x_negative - ); - } - - // subtract, if appropriate - if ( $x_negative != $y_negative ) { - if ( $x_value == $y_value ) { - return array( - MATH_BIGINTEGER_VALUE => array(), - MATH_BIGINTEGER_SIGN => false - ); - } - - $temp = $this->_subtract($x_value, false, $y_value, false); - $temp[MATH_BIGINTEGER_SIGN] = $this->_compare($x_value, false, $y_value, false) > 0 ? - $x_negative : $y_negative; - - return $temp; - } - - if ($x_size < $y_size) { - $size = $x_size; - $value = $y_value; - } else { - $size = $y_size; - $value = $x_value; - } - - $value[] = 0; // just in case the carry adds an extra digit - - $carry = 0; - for ($i = 0, $j = 1; $j < $size; $i+=2, $j+=2) { - $sum = $x_value[$j] * 0x4000000 + $x_value[$i] + $y_value[$j] * 0x4000000 + $y_value[$i] + $carry; - $carry = $sum >= MATH_BIGINTEGER_MAX_DIGIT52; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1 - $sum = $carry ? $sum - MATH_BIGINTEGER_MAX_DIGIT52 : $sum; - - $temp = (int) ($sum / 0x4000000); - - $value[$i] = (int) ($sum - 0x4000000 * $temp); // eg. a faster alternative to fmod($sum, 0x4000000) - $value[$j] = $temp; - } - - if ($j == $size) { // ie. if $y_size is odd - $sum = $x_value[$i] + $y_value[$i] + $carry; - $carry = $sum >= 0x4000000; - $value[$i] = $carry ? $sum - 0x4000000 : $sum; - ++$i; // ie. let $i = $j since we've just done $value[$i] - } - - if ($carry) { - for (; $value[$i] == 0x3FFFFFF; ++$i) { - $value[$i] = 0; - } - ++$value[$i]; - } - - return array( - MATH_BIGINTEGER_VALUE => $this->_trim($value), - MATH_BIGINTEGER_SIGN => $x_negative - ); - } - - /** - * Subtracts two BigIntegers. - * - * Here's an example: - * - * subtract($b); - * - * echo $c->toString(); // outputs -10 - * ?> - * - * - * @param Math_BigInteger $y - * @return Math_BigInteger - * @access public - * @internal Performs base-2**52 subtraction - */ - function subtract($y) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp = new Math_BigInteger(); - $temp->value = gmp_sub($this->value, $y->value); - - return $this->_normalize($temp); - case MATH_BIGINTEGER_MODE_BCMATH: - $temp = new Math_BigInteger(); - $temp->value = bcsub($this->value, $y->value, 0); - - return $this->_normalize($temp); - } - - $temp = $this->_subtract($this->value, $this->is_negative, $y->value, $y->is_negative); - - $result = new Math_BigInteger(); - $result->value = $temp[MATH_BIGINTEGER_VALUE]; - $result->is_negative = $temp[MATH_BIGINTEGER_SIGN]; - - return $this->_normalize($result); - } - - /** - * Performs subtraction. - * - * @param Array $x_value - * @param Boolean $x_negative - * @param Array $y_value - * @param Boolean $y_negative - * @return Array - * @access private - */ - function _subtract($x_value, $x_negative, $y_value, $y_negative) - { - $x_size = count($x_value); - $y_size = count($y_value); - - if ($x_size == 0) { - return array( - MATH_BIGINTEGER_VALUE => $y_value, - MATH_BIGINTEGER_SIGN => !$y_negative - ); - } else if ($y_size == 0) { - return array( - MATH_BIGINTEGER_VALUE => $x_value, - MATH_BIGINTEGER_SIGN => $x_negative - ); - } - - // add, if appropriate (ie. -$x - +$y or +$x - -$y) - if ( $x_negative != $y_negative ) { - $temp = $this->_add($x_value, false, $y_value, false); - $temp[MATH_BIGINTEGER_SIGN] = $x_negative; - - return $temp; - } - - $diff = $this->_compare($x_value, $x_negative, $y_value, $y_negative); - - if ( !$diff ) { - return array( - MATH_BIGINTEGER_VALUE => array(), - MATH_BIGINTEGER_SIGN => false - ); - } - - // switch $x and $y around, if appropriate. - if ( (!$x_negative && $diff < 0) || ($x_negative && $diff > 0) ) { - $temp = $x_value; - $x_value = $y_value; - $y_value = $temp; - - $x_negative = !$x_negative; - - $x_size = count($x_value); - $y_size = count($y_value); - } - - // at this point, $x_value should be at least as big as - if not bigger than - $y_value - - $carry = 0; - for ($i = 0, $j = 1; $j < $y_size; $i+=2, $j+=2) { - $sum = $x_value[$j] * 0x4000000 + $x_value[$i] - $y_value[$j] * 0x4000000 - $y_value[$i] - $carry; - $carry = $sum < 0; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1 - $sum = $carry ? $sum + MATH_BIGINTEGER_MAX_DIGIT52 : $sum; - - $temp = (int) ($sum / 0x4000000); - - $x_value[$i] = (int) ($sum - 0x4000000 * $temp); - $x_value[$j] = $temp; - } - - if ($j == $y_size) { // ie. if $y_size is odd - $sum = $x_value[$i] - $y_value[$i] - $carry; - $carry = $sum < 0; - $x_value[$i] = $carry ? $sum + 0x4000000 : $sum; - ++$i; - } - - if ($carry) { - for (; !$x_value[$i]; ++$i) { - $x_value[$i] = 0x3FFFFFF; - } - --$x_value[$i]; - } - - return array( - MATH_BIGINTEGER_VALUE => $this->_trim($x_value), - MATH_BIGINTEGER_SIGN => $x_negative - ); - } - - /** - * Multiplies two BigIntegers - * - * Here's an example: - * - * multiply($b); - * - * echo $c->toString(); // outputs 200 - * ?> - * - * - * @param Math_BigInteger $x - * @return Math_BigInteger - * @access public - */ - function multiply($x) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp = new Math_BigInteger(); - $temp->value = gmp_mul($this->value, $x->value); - - return $this->_normalize($temp); - case MATH_BIGINTEGER_MODE_BCMATH: - $temp = new Math_BigInteger(); - $temp->value = bcmul($this->value, $x->value, 0); - - return $this->_normalize($temp); - } - - $temp = $this->_multiply($this->value, $this->is_negative, $x->value, $x->is_negative); - - $product = new Math_BigInteger(); - $product->value = $temp[MATH_BIGINTEGER_VALUE]; - $product->is_negative = $temp[MATH_BIGINTEGER_SIGN]; - - return $this->_normalize($product); - } - - /** - * Performs multiplication. - * - * @param Array $x_value - * @param Boolean $x_negative - * @param Array $y_value - * @param Boolean $y_negative - * @return Array - * @access private - */ - function _multiply($x_value, $x_negative, $y_value, $y_negative) - { - //if ( $x_value == $y_value ) { - // return array( - // MATH_BIGINTEGER_VALUE => $this->_square($x_value), - // MATH_BIGINTEGER_SIGN => $x_sign != $y_value - // ); - //} - - $x_length = count($x_value); - $y_length = count($y_value); - - if ( !$x_length || !$y_length ) { // a 0 is being multiplied - return array( - MATH_BIGINTEGER_VALUE => array(), - MATH_BIGINTEGER_SIGN => false - ); - } - - return array( - MATH_BIGINTEGER_VALUE => min($x_length, $y_length) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ? - $this->_trim($this->_regularMultiply($x_value, $y_value)) : - $this->_trim($this->_karatsuba($x_value, $y_value)), - MATH_BIGINTEGER_SIGN => $x_negative != $y_negative - ); - } - - /** - * Performs long multiplication on two BigIntegers - * - * Modeled after 'multiply' in MutableBigInteger.java. - * - * @param Array $x_value - * @param Array $y_value - * @return Array - * @access private - */ - function _regularMultiply($x_value, $y_value) - { - $x_length = count($x_value); - $y_length = count($y_value); - - if ( !$x_length || !$y_length ) { // a 0 is being multiplied - return array(); - } - - if ( $x_length < $y_length ) { - $temp = $x_value; - $x_value = $y_value; - $y_value = $temp; - - $x_length = count($x_value); - $y_length = count($y_value); - } - - $product_value = $this->_array_repeat(0, $x_length + $y_length); - - // the following for loop could be removed if the for loop following it - // (the one with nested for loops) initially set $i to 0, but - // doing so would also make the result in one set of unnecessary adds, - // since on the outermost loops first pass, $product->value[$k] is going - // to always be 0 - - $carry = 0; - - for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0 - $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0 - $carry = (int) ($temp / 0x4000000); - $product_value[$j] = (int) ($temp - 0x4000000 * $carry); - } - - $product_value[$j] = $carry; - - // the above for loop is what the previous comment was talking about. the - // following for loop is the "one with nested for loops" - for ($i = 1; $i < $y_length; ++$i) { - $carry = 0; - - for ($j = 0, $k = $i; $j < $x_length; ++$j, ++$k) { - $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry; - $carry = (int) ($temp / 0x4000000); - $product_value[$k] = (int) ($temp - 0x4000000 * $carry); - } - - $product_value[$k] = $carry; - } - - return $product_value; - } - - /** - * Performs Karatsuba multiplication on two BigIntegers - * - * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and - * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=120 MPM 5.2.3}. - * - * @param Array $x_value - * @param Array $y_value - * @return Array - * @access private - */ - function _karatsuba($x_value, $y_value) - { - $m = min(count($x_value) >> 1, count($y_value) >> 1); - - if ($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF) { - return $this->_regularMultiply($x_value, $y_value); - } - - $x1 = array_slice($x_value, $m); - $x0 = array_slice($x_value, 0, $m); - $y1 = array_slice($y_value, $m); - $y0 = array_slice($y_value, 0, $m); - - $z2 = $this->_karatsuba($x1, $y1); - $z0 = $this->_karatsuba($x0, $y0); - - $z1 = $this->_add($x1, false, $x0, false); - $temp = $this->_add($y1, false, $y0, false); - $z1 = $this->_karatsuba($z1[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_VALUE]); - $temp = $this->_add($z2, false, $z0, false); - $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false); - - $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2); - $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]); - - $xy = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]); - $xy = $this->_add($xy[MATH_BIGINTEGER_VALUE], $xy[MATH_BIGINTEGER_SIGN], $z0, false); - - return $xy[MATH_BIGINTEGER_VALUE]; - } - - /** - * Performs squaring - * - * @param Array $x - * @return Array - * @access private - */ - function _square($x = false) - { - return count($x) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ? - $this->_trim($this->_baseSquare($x)) : - $this->_trim($this->_karatsubaSquare($x)); - } - - /** - * Performs traditional squaring on two BigIntegers - * - * Squaring can be done faster than multiplying a number by itself can be. See - * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=7 HAC 14.2.4} / - * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=141 MPM 5.3} for more information. - * - * @param Array $value - * @return Array - * @access private - */ - function _baseSquare($value) - { - if ( empty($value) ) { - return array(); - } - $square_value = $this->_array_repeat(0, 2 * count($value)); - - for ($i = 0, $max_index = count($value) - 1; $i <= $max_index; ++$i) { - $i2 = $i << 1; - - $temp = $square_value[$i2] + $value[$i] * $value[$i]; - $carry = (int) ($temp / 0x4000000); - $square_value[$i2] = (int) ($temp - 0x4000000 * $carry); - - // note how we start from $i+1 instead of 0 as we do in multiplication. - for ($j = $i + 1, $k = $i2 + 1; $j <= $max_index; ++$j, ++$k) { - $temp = $square_value[$k] + 2 * $value[$j] * $value[$i] + $carry; - $carry = (int) ($temp / 0x4000000); - $square_value[$k] = (int) ($temp - 0x4000000 * $carry); - } - - // the following line can yield values larger 2**15. at this point, PHP should switch - // over to floats. - $square_value[$i + $max_index + 1] = $carry; - } - - return $square_value; - } - - /** - * Performs Karatsuba "squaring" on two BigIntegers - * - * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and - * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=151 MPM 5.3.4}. - * - * @param Array $value - * @return Array - * @access private - */ - function _karatsubaSquare($value) - { - $m = count($value) >> 1; - - if ($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF) { - return $this->_baseSquare($value); - } - - $x1 = array_slice($value, $m); - $x0 = array_slice($value, 0, $m); - - $z2 = $this->_karatsubaSquare($x1); - $z0 = $this->_karatsubaSquare($x0); - - $z1 = $this->_add($x1, false, $x0, false); - $z1 = $this->_karatsubaSquare($z1[MATH_BIGINTEGER_VALUE]); - $temp = $this->_add($z2, false, $z0, false); - $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false); - - $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2); - $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]); - - $xx = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]); - $xx = $this->_add($xx[MATH_BIGINTEGER_VALUE], $xx[MATH_BIGINTEGER_SIGN], $z0, false); - - return $xx[MATH_BIGINTEGER_VALUE]; - } - - /** - * Divides two BigIntegers. - * - * Returns an array whose first element contains the quotient and whose second element contains the - * "common residue". If the remainder would be positive, the "common residue" and the remainder are the - * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder - * and the divisor (basically, the "common residue" is the first positive modulo). - * - * Here's an example: - * - * divide($b); - * - * echo $quotient->toString(); // outputs 0 - * echo "\r\n"; - * echo $remainder->toString(); // outputs 10 - * ?> - * - * - * @param Math_BigInteger $y - * @return Array - * @access public - * @internal This function is based off of {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=9 HAC 14.20}. - */ - function divide($y) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $quotient = new Math_BigInteger(); - $remainder = new Math_BigInteger(); - - list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value); - - if (gmp_sign($remainder->value) < 0) { - $remainder->value = gmp_add($remainder->value, gmp_abs($y->value)); - } - - return array($this->_normalize($quotient), $this->_normalize($remainder)); - case MATH_BIGINTEGER_MODE_BCMATH: - $quotient = new Math_BigInteger(); - $remainder = new Math_BigInteger(); - - $quotient->value = bcdiv($this->value, $y->value, 0); - $remainder->value = bcmod($this->value, $y->value); - - if ($remainder->value[0] == '-') { - $remainder->value = bcadd($remainder->value, $y->value[0] == '-' ? substr($y->value, 1) : $y->value, 0); - } - - return array($this->_normalize($quotient), $this->_normalize($remainder)); - } - - if (count($y->value) == 1) { - list($q, $r) = $this->_divide_digit($this->value, $y->value[0]); - $quotient = new Math_BigInteger(); - $remainder = new Math_BigInteger(); - $quotient->value = $q; - $remainder->value = array($r); - $quotient->is_negative = $this->is_negative != $y->is_negative; - return array($this->_normalize($quotient), $this->_normalize($remainder)); - } - - static $zero; - if ( !isset($zero) ) { - $zero = new Math_BigInteger(); - } - - $x = $this->copy(); - $y = $y->copy(); - - $x_sign = $x->is_negative; - $y_sign = $y->is_negative; - - $x->is_negative = $y->is_negative = false; - - $diff = $x->compare($y); - - if ( !$diff ) { - $temp = new Math_BigInteger(); - $temp->value = array(1); - $temp->is_negative = $x_sign != $y_sign; - return array($this->_normalize($temp), $this->_normalize(new Math_BigInteger())); - } - - if ( $diff < 0 ) { - // if $x is negative, "add" $y. - if ( $x_sign ) { - $x = $y->subtract($x); - } - return array($this->_normalize(new Math_BigInteger()), $this->_normalize($x)); - } - - // normalize $x and $y as described in HAC 14.23 / 14.24 - $msb = $y->value[count($y->value) - 1]; - for ($shift = 0; !($msb & 0x2000000); ++$shift) { - $msb <<= 1; - } - $x->_lshift($shift); - $y->_lshift($shift); - $y_value = &$y->value; - - $x_max = count($x->value) - 1; - $y_max = count($y->value) - 1; - - $quotient = new Math_BigInteger(); - $quotient_value = &$quotient->value; - $quotient_value = $this->_array_repeat(0, $x_max - $y_max + 1); - - static $temp, $lhs, $rhs; - if (!isset($temp)) { - $temp = new Math_BigInteger(); - $lhs = new Math_BigInteger(); - $rhs = new Math_BigInteger(); - } - $temp_value = &$temp->value; - $rhs_value = &$rhs->value; - - // $temp = $y << ($x_max - $y_max-1) in base 2**26 - $temp_value = array_merge($this->_array_repeat(0, $x_max - $y_max), $y_value); - - while ( $x->compare($temp) >= 0 ) { - // calculate the "common residue" - ++$quotient_value[$x_max - $y_max]; - $x = $x->subtract($temp); - $x_max = count($x->value) - 1; - } - - for ($i = $x_max; $i >= $y_max + 1; --$i) { - $x_value = &$x->value; - $x_window = array( - isset($x_value[$i]) ? $x_value[$i] : 0, - isset($x_value[$i - 1]) ? $x_value[$i - 1] : 0, - isset($x_value[$i - 2]) ? $x_value[$i - 2] : 0 - ); - $y_window = array( - $y_value[$y_max], - ( $y_max > 0 ) ? $y_value[$y_max - 1] : 0 - ); - - $q_index = $i - $y_max - 1; - if ($x_window[0] == $y_window[0]) { - $quotient_value[$q_index] = 0x3FFFFFF; - } else { - $quotient_value[$q_index] = (int) ( - ($x_window[0] * 0x4000000 + $x_window[1]) - / - $y_window[0] - ); - } - - $temp_value = array($y_window[1], $y_window[0]); - - $lhs->value = array($quotient_value[$q_index]); - $lhs = $lhs->multiply($temp); - - $rhs_value = array($x_window[2], $x_window[1], $x_window[0]); - - while ( $lhs->compare($rhs) > 0 ) { - --$quotient_value[$q_index]; - - $lhs->value = array($quotient_value[$q_index]); - $lhs = $lhs->multiply($temp); - } - - $adjust = $this->_array_repeat(0, $q_index); - $temp_value = array($quotient_value[$q_index]); - $temp = $temp->multiply($y); - $temp_value = &$temp->value; - $temp_value = array_merge($adjust, $temp_value); - - $x = $x->subtract($temp); - - if ($x->compare($zero) < 0) { - $temp_value = array_merge($adjust, $y_value); - $x = $x->add($temp); - - --$quotient_value[$q_index]; - } - - $x_max = count($x_value) - 1; - } - - // unnormalize the remainder - $x->_rshift($shift); - - $quotient->is_negative = $x_sign != $y_sign; - - // calculate the "common residue", if appropriate - if ( $x_sign ) { - $y->_rshift($shift); - $x = $y->subtract($x); - } - - return array($this->_normalize($quotient), $this->_normalize($x)); - } - - /** - * Divides a BigInteger by a regular integer - * - * abc / x = a00 / x + b0 / x + c / x - * - * @param Array $dividend - * @param Array $divisor - * @return Array - * @access private - */ - function _divide_digit($dividend, $divisor) - { - $carry = 0; - $result = array(); - - for ($i = count($dividend) - 1; $i >= 0; --$i) { - $temp = 0x4000000 * $carry + $dividend[$i]; - $result[$i] = (int) ($temp / $divisor); - $carry = (int) ($temp - $divisor * $result[$i]); - } - - return array($result, $carry); - } - - /** - * Performs modular exponentiation. - * - * Here's an example: - * - * modPow($b, $c); - * - * echo $c->toString(); // outputs 10 - * ?> - * - * - * @param Math_BigInteger $e - * @param Math_BigInteger $n - * @return Math_BigInteger - * @access public - * @internal The most naive approach to modular exponentiation has very unreasonable requirements, and - * and although the approach involving repeated squaring does vastly better, it, too, is impractical - * for our purposes. The reason being that division - by far the most complicated and time-consuming - * of the basic operations (eg. +,-,*,/) - occurs multiple times within it. - * - * Modular reductions resolve this issue. Although an individual modular reduction takes more time - * then an individual division, when performed in succession (with the same modulo), they're a lot faster. - * - * The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction, - * although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the - * base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because - * the product of two odd numbers is odd), but what about when RSA isn't used? - * - * In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a - * Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the - * modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however, - * uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and - * the other, a power of two - and recombine them, later. This is the method that this modPow function uses. - * {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates. - */ - function modPow($e, $n) - { - $n = $this->bitmask !== false && $this->bitmask->compare($n) < 0 ? $this->bitmask : $n->abs(); - - if ($e->compare(new Math_BigInteger()) < 0) { - $e = $e->abs(); - - $temp = $this->modInverse($n); - if ($temp === false) { - return false; - } - - return $this->_normalize($temp->modPow($e, $n)); - } - - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp = new Math_BigInteger(); - $temp->value = gmp_powm($this->value, $e->value, $n->value); - - return $this->_normalize($temp); - case MATH_BIGINTEGER_MODE_BCMATH: - $temp = new Math_BigInteger(); - $temp->value = bcpowmod($this->value, $e->value, $n->value, 0); - - return $this->_normalize($temp); - } - - if ( empty($e->value) ) { - $temp = new Math_BigInteger(); - $temp->value = array(1); - return $this->_normalize($temp); - } - - if ( $e->value == array(1) ) { - list(, $temp) = $this->divide($n); - return $this->_normalize($temp); - } - - if ( $e->value == array(2) ) { - $temp = new Math_BigInteger(); - $temp->value = $this->_square($this->value); - list(, $temp) = $temp->divide($n); - return $this->_normalize($temp); - } - - return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_BARRETT)); - - // is the modulo odd? - if ( $n->value[0] & 1 ) { - return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_MONTGOMERY)); - } - // if it's not, it's even - - // find the lowest set bit (eg. the max pow of 2 that divides $n) - for ($i = 0; $i < count($n->value); ++$i) { - if ( $n->value[$i] ) { - $temp = decbin($n->value[$i]); - $j = strlen($temp) - strrpos($temp, '1') - 1; - $j+= 26 * $i; - break; - } - } - // at this point, 2^$j * $n/(2^$j) == $n - - $mod1 = $n->copy(); - $mod1->_rshift($j); - $mod2 = new Math_BigInteger(); - $mod2->value = array(1); - $mod2->_lshift($j); - - $part1 = ( $mod1->value != array(1) ) ? $this->_slidingWindow($e, $mod1, MATH_BIGINTEGER_MONTGOMERY) : new Math_BigInteger(); - $part2 = $this->_slidingWindow($e, $mod2, MATH_BIGINTEGER_POWEROF2); - - $y1 = $mod2->modInverse($mod1); - $y2 = $mod1->modInverse($mod2); - - $result = $part1->multiply($mod2); - $result = $result->multiply($y1); - - $temp = $part2->multiply($mod1); - $temp = $temp->multiply($y2); - - $result = $result->add($temp); - list(, $result) = $result->divide($n); - - return $this->_normalize($result); - } - - /** - * Performs modular exponentiation. - * - * Alias for Math_BigInteger::modPow() - * - * @param Math_BigInteger $e - * @param Math_BigInteger $n - * @return Math_BigInteger - * @access public - */ - function powMod($e, $n) - { - return $this->modPow($e, $n); - } - - /** - * Sliding Window k-ary Modular Exponentiation - * - * Based on {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=27 HAC 14.85} / - * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=210 MPM 7.7}. In a departure from those algorithims, - * however, this function performs a modular reduction after every multiplication and squaring operation. - * As such, this function has the same preconditions that the reductions being used do. - * - * @param Math_BigInteger $e - * @param Math_BigInteger $n - * @param Integer $mode - * @return Math_BigInteger - * @access private - */ - function _slidingWindow($e, $n, $mode) - { - static $window_ranges = array(7, 25, 81, 241, 673, 1793); // from BigInteger.java's oddModPow function - //static $window_ranges = array(0, 7, 36, 140, 450, 1303, 3529); // from MPM 7.3.1 - - $e_value = $e->value; - $e_length = count($e_value) - 1; - $e_bits = decbin($e_value[$e_length]); - for ($i = $e_length - 1; $i >= 0; --$i) { - $e_bits.= str_pad(decbin($e_value[$i]), 26, '0', STR_PAD_LEFT); - } - - $e_length = strlen($e_bits); - - // calculate the appropriate window size. - // $window_size == 3 if $window_ranges is between 25 and 81, for example. - for ($i = 0, $window_size = 1; $e_length > $window_ranges[$i] && $i < count($window_ranges); ++$window_size, ++$i); - - $n_value = $n->value; - - // precompute $this^0 through $this^$window_size - $powers = array(); - $powers[1] = $this->_prepareReduce($this->value, $n_value, $mode); - $powers[2] = $this->_squareReduce($powers[1], $n_value, $mode); - - // we do every other number since substr($e_bits, $i, $j+1) (see below) is supposed to end - // in a 1. ie. it's supposed to be odd. - $temp = 1 << ($window_size - 1); - for ($i = 1; $i < $temp; ++$i) { - $i2 = $i << 1; - $powers[$i2 + 1] = $this->_multiplyReduce($powers[$i2 - 1], $powers[2], $n_value, $mode); - } - - $result = array(1); - $result = $this->_prepareReduce($result, $n_value, $mode); - - for ($i = 0; $i < $e_length; ) { - if ( !$e_bits[$i] ) { - $result = $this->_squareReduce($result, $n_value, $mode); - ++$i; - } else { - for ($j = $window_size - 1; $j > 0; --$j) { - if ( !empty($e_bits[$i + $j]) ) { - break; - } - } - - for ($k = 0; $k <= $j; ++$k) {// eg. the length of substr($e_bits, $i, $j+1) - $result = $this->_squareReduce($result, $n_value, $mode); - } - - $result = $this->_multiplyReduce($result, $powers[bindec(substr($e_bits, $i, $j + 1))], $n_value, $mode); - - $i+=$j + 1; - } - } - - $temp = new Math_BigInteger(); - $temp->value = $this->_reduce($result, $n_value, $mode); - - return $temp; - } - - /** - * Modular reduction - * - * For most $modes this will return the remainder. - * - * @see _slidingWindow() - * @access private - * @param Array $x - * @param Array $n - * @param Integer $mode - * @return Array - */ - function _reduce($x, $n, $mode) - { - switch ($mode) { - case MATH_BIGINTEGER_MONTGOMERY: - return $this->_montgomery($x, $n); - case MATH_BIGINTEGER_BARRETT: - return $this->_barrett($x, $n); - case MATH_BIGINTEGER_POWEROF2: - $lhs = new Math_BigInteger(); - $lhs->value = $x; - $rhs = new Math_BigInteger(); - $rhs->value = $n; - return $x->_mod2($n); - case MATH_BIGINTEGER_CLASSIC: - $lhs = new Math_BigInteger(); - $lhs->value = $x; - $rhs = new Math_BigInteger(); - $rhs->value = $n; - list(, $temp) = $lhs->divide($rhs); - return $temp->value; - case MATH_BIGINTEGER_NONE: - return $x; - default: - // an invalid $mode was provided - } - } - - /** - * Modular reduction preperation - * - * @see _slidingWindow() - * @access private - * @param Array $x - * @param Array $n - * @param Integer $mode - * @return Array - */ - function _prepareReduce($x, $n, $mode) - { - if ($mode == MATH_BIGINTEGER_MONTGOMERY) { - return $this->_prepMontgomery($x, $n); - } - return $this->_reduce($x, $n, $mode); - } - - /** - * Modular multiply - * - * @see _slidingWindow() - * @access private - * @param Array $x - * @param Array $y - * @param Array $n - * @param Integer $mode - * @return Array - */ - function _multiplyReduce($x, $y, $n, $mode) - { - if ($mode == MATH_BIGINTEGER_MONTGOMERY) { - return $this->_montgomeryMultiply($x, $y, $n); - } - $temp = $this->_multiply($x, false, $y, false); - return $this->_reduce($temp[MATH_BIGINTEGER_VALUE], $n, $mode); - } - - /** - * Modular square - * - * @see _slidingWindow() - * @access private - * @param Array $x - * @param Array $n - * @param Integer $mode - * @return Array - */ - function _squareReduce($x, $n, $mode) - { - if ($mode == MATH_BIGINTEGER_MONTGOMERY) { - return $this->_montgomeryMultiply($x, $x, $n); - } - return $this->_reduce($this->_square($x), $n, $mode); - } - - /** - * Modulos for Powers of Two - * - * Calculates $x%$n, where $n = 2**$e, for some $e. Since this is basically the same as doing $x & ($n-1), - * we'll just use this function as a wrapper for doing that. - * - * @see _slidingWindow() - * @access private - * @param Math_BigInteger - * @return Math_BigInteger - */ - function _mod2($n) - { - $temp = new Math_BigInteger(); - $temp->value = array(1); - return $this->bitwise_and($n->subtract($temp)); - } - - /** - * Barrett Modular Reduction - * - * See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=14 HAC 14.3.3} / - * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=165 MPM 6.2.5} for more information. Modified slightly, - * so as not to require negative numbers (initially, this script didn't support negative numbers). - * - * Employs "folding", as described at - * {@link http://www.cosic.esat.kuleuven.be/publications/thesis-149.pdf#page=66 thesis-149.pdf#page=66}. To quote from - * it, "the idea [behind folding] is to find a value x' such that x (mod m) = x' (mod m), with x' being smaller than x." - * - * Unfortunately, the "Barrett Reduction with Folding" algorithm described in thesis-149.pdf is not, as written, all that - * usable on account of (1) its not using reasonable radix points as discussed in - * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=162 MPM 6.2.2} and (2) the fact that, even with reasonable - * radix points, it only works when there are an even number of digits in the denominator. The reason for (2) is that - * (x >> 1) + (x >> 1) != x / 2 + x / 2. If x is even, they're the same, but if x is odd, they're not. See the in-line - * comments for details. - * - * @see _slidingWindow() - * @access private - * @param Array $n - * @param Array $m - * @return Array - */ - function _barrett($n, $m) - { - static $cache = array( - MATH_BIGINTEGER_VARIABLE => array(), - MATH_BIGINTEGER_DATA => array() - ); - - $m_length = count($m); - - // if ($this->_compare($n, $this->_square($m)) >= 0) { - if (count($n) > 2 * $m_length) { - $lhs = new Math_BigInteger(); - $rhs = new Math_BigInteger(); - $lhs->value = $n; - $rhs->value = $m; - list(, $temp) = $lhs->divide($rhs); - return $temp->value; - } - - // if (m.length >> 1) + 2 <= m.length then m is too small and n can't be reduced - if ($m_length < 5) { - return $this->_regularBarrett($n, $m); - } - - // n = 2 * m.length - - if ( ($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { - $key = count($cache[MATH_BIGINTEGER_VARIABLE]); - $cache[MATH_BIGINTEGER_VARIABLE][] = $m; - - $lhs = new Math_BigInteger(); - $lhs_value = &$lhs->value; - $lhs_value = $this->_array_repeat(0, $m_length + ($m_length >> 1)); - $lhs_value[] = 1; - $rhs = new Math_BigInteger(); - $rhs->value = $m; - - list($u, $m1) = $lhs->divide($rhs); - $u = $u->value; - $m1 = $m1->value; - - $cache[MATH_BIGINTEGER_DATA][] = array( - 'u' => $u, // m.length >> 1 (technically (m.length >> 1) + 1) - 'm1'=> $m1 // m.length - ); - } else { - extract($cache[MATH_BIGINTEGER_DATA][$key]); - } - - $cutoff = $m_length + ($m_length >> 1); - $lsd = array_slice($n, 0, $cutoff); // m.length + (m.length >> 1) - $msd = array_slice($n, $cutoff); // m.length >> 1 - $lsd = $this->_trim($lsd); - $temp = $this->_multiply($msd, false, $m1, false); - $n = $this->_add($lsd, false, $temp[MATH_BIGINTEGER_VALUE], false); // m.length + (m.length >> 1) + 1 - - if ($m_length & 1) { - return $this->_regularBarrett($n[MATH_BIGINTEGER_VALUE], $m); - } - - // (m.length + (m.length >> 1) + 1) - (m.length - 1) == (m.length >> 1) + 2 - $temp = array_slice($n[MATH_BIGINTEGER_VALUE], $m_length - 1); - // if even: ((m.length >> 1) + 2) + (m.length >> 1) == m.length + 2 - // if odd: ((m.length >> 1) + 2) + (m.length >> 1) == (m.length - 1) + 2 == m.length + 1 - $temp = $this->_multiply($temp, false, $u, false); - // if even: (m.length + 2) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) + 1 - // if odd: (m.length + 1) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) - $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], ($m_length >> 1) + 1); - // if even: (m.length - (m.length >> 1) + 1) + m.length = 2 * m.length - (m.length >> 1) + 1 - // if odd: (m.length - (m.length >> 1)) + m.length = 2 * m.length - (m.length >> 1) - $temp = $this->_multiply($temp, false, $m, false); - - // at this point, if m had an odd number of digits, we'd be subtracting a 2 * m.length - (m.length >> 1) digit - // number from a m.length + (m.length >> 1) + 1 digit number. ie. there'd be an extra digit and the while loop - // following this comment would loop a lot (hence our calling _regularBarrett() in that situation). - - $result = $this->_subtract($n[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false); - - while ($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false) >= 0) { - $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false); - } - - return $result[MATH_BIGINTEGER_VALUE]; - } - - /** - * (Regular) Barrett Modular Reduction - * - * For numbers with more than four digits Math_BigInteger::_barrett() is faster. The difference between that and this - * is that this function does not fold the denominator into a smaller form. - * - * @see _slidingWindow() - * @access private - * @param Array $x - * @param Array $n - * @return Array - */ - function _regularBarrett($x, $n) - { - static $cache = array( - MATH_BIGINTEGER_VARIABLE => array(), - MATH_BIGINTEGER_DATA => array() - ); - - $n_length = count($n); - - if (count($x) > 2 * $n_length) { - $lhs = new Math_BigInteger(); - $rhs = new Math_BigInteger(); - $lhs->value = $x; - $rhs->value = $n; - list(, $temp) = $lhs->divide($rhs); - return $temp->value; - } - - if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { - $key = count($cache[MATH_BIGINTEGER_VARIABLE]); - $cache[MATH_BIGINTEGER_VARIABLE][] = $n; - $lhs = new Math_BigInteger(); - $lhs_value = &$lhs->value; - $lhs_value = $this->_array_repeat(0, 2 * $n_length); - $lhs_value[] = 1; - $rhs = new Math_BigInteger(); - $rhs->value = $n; - list($temp, ) = $lhs->divide($rhs); // m.length - $cache[MATH_BIGINTEGER_DATA][] = $temp->value; - } - - // 2 * m.length - (m.length - 1) = m.length + 1 - $temp = array_slice($x, $n_length - 1); - // (m.length + 1) + m.length = 2 * m.length + 1 - $temp = $this->_multiply($temp, false, $cache[MATH_BIGINTEGER_DATA][$key], false); - // (2 * m.length + 1) - (m.length - 1) = m.length + 2 - $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], $n_length + 1); - - // m.length + 1 - $result = array_slice($x, 0, $n_length + 1); - // m.length + 1 - $temp = $this->_multiplyLower($temp, false, $n, false, $n_length + 1); - // $temp == array_slice($temp->_multiply($temp, false, $n, false)->value, 0, $n_length + 1) - - if ($this->_compare($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]) < 0) { - $corrector_value = $this->_array_repeat(0, $n_length + 1); - $corrector_value[] = 1; - $result = $this->_add($result, false, $corrector, false); - $result = $result[MATH_BIGINTEGER_VALUE]; - } - - // at this point, we're subtracting a number with m.length + 1 digits from another number with m.length + 1 digits - $result = $this->_subtract($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]); - while ($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false) > 0) { - $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false); - } - - return $result[MATH_BIGINTEGER_VALUE]; - } - - /** - * Performs long multiplication up to $stop digits - * - * If you're going to be doing array_slice($product->value, 0, $stop), some cycles can be saved. - * - * @see _regularBarrett() - * @param Array $x_value - * @param Boolean $x_negative - * @param Array $y_value - * @param Boolean $y_negative - * @return Array - * @access private - */ - function _multiplyLower($x_value, $x_negative, $y_value, $y_negative, $stop) - { - $x_length = count($x_value); - $y_length = count($y_value); - - if ( !$x_length || !$y_length ) { // a 0 is being multiplied - return array( - MATH_BIGINTEGER_VALUE => array(), - MATH_BIGINTEGER_SIGN => false - ); - } - - if ( $x_length < $y_length ) { - $temp = $x_value; - $x_value = $y_value; - $y_value = $temp; - - $x_length = count($x_value); - $y_length = count($y_value); - } - - $product_value = $this->_array_repeat(0, $x_length + $y_length); - - // the following for loop could be removed if the for loop following it - // (the one with nested for loops) initially set $i to 0, but - // doing so would also make the result in one set of unnecessary adds, - // since on the outermost loops first pass, $product->value[$k] is going - // to always be 0 - - $carry = 0; - - for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0, $k = $i - $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0 - $carry = (int) ($temp / 0x4000000); - $product_value[$j] = (int) ($temp - 0x4000000 * $carry); - } - - if ($j < $stop) { - $product_value[$j] = $carry; - } - - // the above for loop is what the previous comment was talking about. the - // following for loop is the "one with nested for loops" - - for ($i = 1; $i < $y_length; ++$i) { - $carry = 0; - - for ($j = 0, $k = $i; $j < $x_length && $k < $stop; ++$j, ++$k) { - $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry; - $carry = (int) ($temp / 0x4000000); - $product_value[$k] = (int) ($temp - 0x4000000 * $carry); - } - - if ($k < $stop) { - $product_value[$k] = $carry; - } - } - - return array( - MATH_BIGINTEGER_VALUE => $this->_trim($product_value), - MATH_BIGINTEGER_SIGN => $x_negative != $y_negative - ); - } - - /** - * Montgomery Modular Reduction - * - * ($x->_prepMontgomery($n))->_montgomery($n) yields $x % $n. - * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=170 MPM 6.3} provides insights on how this can be - * improved upon (basically, by using the comba method). gcd($n, 2) must be equal to one for this function - * to work correctly. - * - * @see _prepMontgomery() - * @see _slidingWindow() - * @access private - * @param Array $x - * @param Array $n - * @return Array - */ - function _montgomery($x, $n) - { - static $cache = array( - MATH_BIGINTEGER_VARIABLE => array(), - MATH_BIGINTEGER_DATA => array() - ); - - if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { - $key = count($cache[MATH_BIGINTEGER_VARIABLE]); - $cache[MATH_BIGINTEGER_VARIABLE][] = $x; - $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($n); - } - - $k = count($n); - - $result = array(MATH_BIGINTEGER_VALUE => $x); - - for ($i = 0; $i < $k; ++$i) { - $temp = $result[MATH_BIGINTEGER_VALUE][$i] * $cache[MATH_BIGINTEGER_DATA][$key]; - $temp = (int) ($temp - 0x4000000 * ((int) ($temp / 0x4000000))); - $temp = $this->_regularMultiply(array($temp), $n); - $temp = array_merge($this->_array_repeat(0, $i), $temp); - $result = $this->_add($result[MATH_BIGINTEGER_VALUE], false, $temp, false); - } - - $result[MATH_BIGINTEGER_VALUE] = array_slice($result[MATH_BIGINTEGER_VALUE], $k); - - if ($this->_compare($result, false, $n, false) >= 0) { - $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], false, $n, false); - } - - return $result[MATH_BIGINTEGER_VALUE]; - } - - /** - * Montgomery Multiply - * - * Interleaves the montgomery reduction and long multiplication algorithms together as described in - * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=13 HAC 14.36} - * - * @see _prepMontgomery() - * @see _montgomery() - * @access private - * @param Array $x - * @param Array $y - * @param Array $m - * @return Array - */ - function _montgomeryMultiply($x, $y, $m) - { - $temp = $this->_multiply($x, false, $y, false); - return $this->_montgomery($temp[MATH_BIGINTEGER_VALUE], $m); - - static $cache = array( - MATH_BIGINTEGER_VARIABLE => array(), - MATH_BIGINTEGER_DATA => array() - ); - - if ( ($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { - $key = count($cache[MATH_BIGINTEGER_VARIABLE]); - $cache[MATH_BIGINTEGER_VARIABLE][] = $m; - $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($m); - } - - $n = max(count($x), count($y), count($m)); - $x = array_pad($x, $n, 0); - $y = array_pad($y, $n, 0); - $m = array_pad($m, $n, 0); - $a = array(MATH_BIGINTEGER_VALUE => $this->_array_repeat(0, $n + 1)); - for ($i = 0; $i < $n; ++$i) { - $temp = $a[MATH_BIGINTEGER_VALUE][0] + $x[$i] * $y[0]; - $temp = (int) ($temp - 0x4000000 * ((int) ($temp / 0x4000000))); - $temp = $temp * $cache[MATH_BIGINTEGER_DATA][$key]; - $temp = (int) ($temp - 0x4000000 * ((int) ($temp / 0x4000000))); - $temp = $this->_add($this->_regularMultiply(array($x[$i]), $y), false, $this->_regularMultiply(array($temp), $m), false); - $a = $this->_add($a[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false); - $a[MATH_BIGINTEGER_VALUE] = array_slice($a[MATH_BIGINTEGER_VALUE], 1); - } - if ($this->_compare($a[MATH_BIGINTEGER_VALUE], false, $m, false) >= 0) { - $a = $this->_subtract($a[MATH_BIGINTEGER_VALUE], false, $m, false); - } - return $a[MATH_BIGINTEGER_VALUE]; - } - - /** - * Prepare a number for use in Montgomery Modular Reductions - * - * @see _montgomery() - * @see _slidingWindow() - * @access private - * @param Array $x - * @param Array $n - * @return Array - */ - function _prepMontgomery($x, $n) - { - $lhs = new Math_BigInteger(); - $lhs->value = array_merge($this->_array_repeat(0, count($n)), $x); - $rhs = new Math_BigInteger(); - $rhs->value = $n; - - list(, $temp) = $lhs->divide($rhs); - return $temp->value; - } - - /** - * Modular Inverse of a number mod 2**26 (eg. 67108864) - * - * Based off of the bnpInvDigit function implemented and justified in the following URL: - * - * {@link http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js} - * - * The following URL provides more info: - * - * {@link http://groups.google.com/group/sci.crypt/msg/7a137205c1be7d85} - * - * As for why we do all the bitmasking... strange things can happen when converting from floats to ints. For - * instance, on some computers, var_dump((int) -4294967297) yields int(-1) and on others, it yields - * int(-2147483648). To avoid problems stemming from this, we use bitmasks to guarantee that ints aren't - * auto-converted to floats. The outermost bitmask is present because without it, there's no guarantee that - * the "residue" returned would be the so-called "common residue". We use fmod, in the last step, because the - * maximum possible $x is 26 bits and the maximum $result is 16 bits. Thus, we have to be able to handle up to - * 40 bits, which only 64-bit floating points will support. - * - * Thanks to Pedro Gimeno Fortea for input! - * - * @see _montgomery() - * @access private - * @param Array $x - * @return Integer - */ - function _modInverse67108864($x) // 2**26 == 67108864 - { - $x = -$x[0]; - $result = $x & 0x3; // x**-1 mod 2**2 - $result = ($result * (2 - $x * $result)) & 0xF; // x**-1 mod 2**4 - $result = ($result * (2 - ($x & 0xFF) * $result)) & 0xFF; // x**-1 mod 2**8 - $result = ($result * ((2 - ($x & 0xFFFF) * $result) & 0xFFFF)) & 0xFFFF; // x**-1 mod 2**16 - $result = fmod($result * (2 - fmod($x * $result, 0x4000000)), 0x4000000); // x**-1 mod 2**26 - return $result & 0x3FFFFFF; - } - - /** - * Calculates modular inverses. - * - * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses. - * - * Here's an example: - * - * modInverse($b); - * echo $c->toString(); // outputs 4 - * - * echo "\r\n"; - * - * $d = $a->multiply($c); - * list(, $d) = $d->divide($b); - * echo $d; // outputs 1 (as per the definition of modular inverse) - * ?> - * - * - * @param Math_BigInteger $n - * @return mixed false, if no modular inverse exists, Math_BigInteger, otherwise. - * @access public - * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=21 HAC 14.64} for more information. - */ - function modInverse($n) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp = new Math_BigInteger(); - $temp->value = gmp_invert($this->value, $n->value); - - return ( $temp->value === false ) ? false : $this->_normalize($temp); - } - - static $zero, $one; - if (!isset($zero)) { - $zero = new Math_BigInteger(); - $one = new Math_BigInteger(1); - } - - // $x mod $n == $x mod -$n. - $n = $n->abs(); - - if ($this->compare($zero) < 0) { - $temp = $this->abs(); - $temp = $temp->modInverse($n); - return $negated === false ? false : $this->_normalize($n->subtract($temp)); - } - - extract($this->extendedGCD($n)); - - if (!$gcd->equals($one)) { - return false; - } - - $x = $x->compare($zero) < 0 ? $x->add($n) : $x; - - return $this->compare($zero) < 0 ? $this->_normalize($n->subtract($x)) : $this->_normalize($x); - } - - /** - * Calculates the greatest common divisor and Bézout's identity. - * - * Say you have 693 and 609. The GCD is 21. Bézout's identity states that there exist integers x and y such that - * 693*x + 609*y == 21. In point of fact, there are actually an infinite number of x and y combinations and which - * combination is returned is dependant upon which mode is in use. See - * {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bézout's identity - Wikipedia} for more information. - * - * Here's an example: - * - * extendedGCD($b)); - * - * echo $gcd->toString() . "\r\n"; // outputs 21 - * echo $a->toString() * $x->toString() + $b->toString() * $y->toString(); // outputs 21 - * ?> - * - * - * @param Math_BigInteger $n - * @return Math_BigInteger - * @access public - * @internal Calculates the GCD using the binary xGCD algorithim described in - * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=19 HAC 14.61}. As the text above 14.61 notes, - * the more traditional algorithim requires "relatively costly multiple-precision divisions". - */ - function extendedGCD($n) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - extract(gmp_gcdext($this->value, $n->value)); - - return array( - 'gcd' => $this->_normalize(new Math_BigInteger($g)), - 'x' => $this->_normalize(new Math_BigInteger($s)), - 'y' => $this->_normalize(new Math_BigInteger($t)) - ); - case MATH_BIGINTEGER_MODE_BCMATH: - // it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works - // best when the base is a power of 2 and (2) i don't think it'd make much difference, anyway. as is, - // the basic extended euclidean algorithim is what we're using. - - $u = $this->value; - $v = $n->value; - - $a = '1'; - $b = '0'; - $c = '0'; - $d = '1'; - - while (bccomp($v, '0', 0) != 0) { - $q = bcdiv($u, $v, 0); - - $temp = $u; - $u = $v; - $v = bcsub($temp, bcmul($v, $q, 0), 0); - - $temp = $a; - $a = $c; - $c = bcsub($temp, bcmul($a, $q, 0), 0); - - $temp = $b; - $b = $d; - $d = bcsub($temp, bcmul($b, $q, 0), 0); - } - - return array( - 'gcd' => $this->_normalize(new Math_BigInteger($u)), - 'x' => $this->_normalize(new Math_BigInteger($a)), - 'y' => $this->_normalize(new Math_BigInteger($b)) - ); - } - - $y = $n->copy(); - $x = $this->copy(); - $g = new Math_BigInteger(); - $g->value = array(1); - - while ( !(($x->value[0] & 1)|| ($y->value[0] & 1)) ) { - $x->_rshift(1); - $y->_rshift(1); - $g->_lshift(1); - } - - $u = $x->copy(); - $v = $y->copy(); - - $a = new Math_BigInteger(); - $b = new Math_BigInteger(); - $c = new Math_BigInteger(); - $d = new Math_BigInteger(); - - $a->value = $d->value = $g->value = array(1); - $b->value = $c->value = array(); - - while ( !empty($u->value) ) { - while ( !($u->value[0] & 1) ) { - $u->_rshift(1); - if ( (!empty($a->value) && ($a->value[0] & 1)) || (!empty($b->value) && ($b->value[0] & 1)) ) { - $a = $a->add($y); - $b = $b->subtract($x); - } - $a->_rshift(1); - $b->_rshift(1); - } - - while ( !($v->value[0] & 1) ) { - $v->_rshift(1); - if ( (!empty($d->value) && ($d->value[0] & 1)) || (!empty($c->value) && ($c->value[0] & 1)) ) { - $c = $c->add($y); - $d = $d->subtract($x); - } - $c->_rshift(1); - $d->_rshift(1); - } - - if ($u->compare($v) >= 0) { - $u = $u->subtract($v); - $a = $a->subtract($c); - $b = $b->subtract($d); - } else { - $v = $v->subtract($u); - $c = $c->subtract($a); - $d = $d->subtract($b); - } - } - - return array( - 'gcd' => $this->_normalize($g->multiply($v)), - 'x' => $this->_normalize($c), - 'y' => $this->_normalize($d) - ); - } - - /** - * Calculates the greatest common divisor - * - * Say you have 693 and 609. The GCD is 21. - * - * Here's an example: - * - * extendedGCD($b); - * - * echo $gcd->toString() . "\r\n"; // outputs 21 - * ?> - * - * - * @param Math_BigInteger $n - * @return Math_BigInteger - * @access public - */ - function gcd($n) - { - extract($this->extendedGCD($n)); - return $gcd; - } - - /** - * Absolute value. - * - * @return Math_BigInteger - * @access public - */ - function abs() - { - $temp = new Math_BigInteger(); - - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp->value = gmp_abs($this->value); - break; - case MATH_BIGINTEGER_MODE_BCMATH: - $temp->value = (bccomp($this->value, '0', 0) < 0) ? substr($this->value, 1) : $this->value; - break; - default: - $temp->value = $this->value; - } - - return $temp; - } - - /** - * Compares two numbers. - * - * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is - * demonstrated thusly: - * - * $x > $y: $x->compare($y) > 0 - * $x < $y: $x->compare($y) < 0 - * $x == $y: $x->compare($y) == 0 - * - * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y). - * - * @param Math_BigInteger $x - * @return Integer < 0 if $this is less than $x; > 0 if $this is greater than $x, and 0 if they are equal. - * @access public - * @see equals() - * @internal Could return $this->subtract($x), but that's not as fast as what we do do. - */ - function compare($y) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - return gmp_cmp($this->value, $y->value); - case MATH_BIGINTEGER_MODE_BCMATH: - return bccomp($this->value, $y->value, 0); - } - - return $this->_compare($this->value, $this->is_negative, $y->value, $y->is_negative); - } - - /** - * Compares two numbers. - * - * @param Array $x_value - * @param Boolean $x_negative - * @param Array $y_value - * @param Boolean $y_negative - * @return Integer - * @see compare() - * @access private - */ - function _compare($x_value, $x_negative, $y_value, $y_negative) - { - if ( $x_negative != $y_negative ) { - return ( !$x_negative && $y_negative ) ? 1 : -1; - } - - $result = $x_negative ? -1 : 1; - - if ( count($x_value) != count($y_value) ) { - return ( count($x_value) > count($y_value) ) ? $result : -$result; - } - $size = max(count($x_value), count($y_value)); - - $x_value = array_pad($x_value, $size, 0); - $y_value = array_pad($y_value, $size, 0); - - for ($i = count($x_value) - 1; $i >= 0; --$i) { - if ($x_value[$i] != $y_value[$i]) { - return ( $x_value[$i] > $y_value[$i] ) ? $result : -$result; - } - } - - return 0; - } - - /** - * Tests the equality of two numbers. - * - * If you need to see if one number is greater than or less than another number, use Math_BigInteger::compare() - * - * @param Math_BigInteger $x - * @return Boolean - * @access public - * @see compare() - */ - function equals($x) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - return gmp_cmp($this->value, $x->value) == 0; - default: - return $this->value === $x->value && $this->is_negative == $x->is_negative; - } - } - - /** - * Set Precision - * - * Some bitwise operations give different results depending on the precision being used. Examples include left - * shift, not, and rotates. - * - * @param Math_BigInteger $x - * @access public - * @return Math_BigInteger - */ - function setPrecision($bits) - { - $this->precision = $bits; - if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ) { - $this->bitmask = new Math_BigInteger(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256); - } else { - $this->bitmask = new Math_BigInteger(bcpow('2', $bits, 0)); - } - - $temp = $this->_normalize($this); - $this->value = $temp->value; - } - - /** - * Logical And - * - * @param Math_BigInteger $x - * @access public - * @internal Implemented per a request by Lluis Pamies i Juarez - * @return Math_BigInteger - */ - function bitwise_and($x) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp = new Math_BigInteger(); - $temp->value = gmp_and($this->value, $x->value); - - return $this->_normalize($temp); - case MATH_BIGINTEGER_MODE_BCMATH: - $left = $this->toBytes(); - $right = $x->toBytes(); - - $length = max(strlen($left), strlen($right)); - - $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); - $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); - - return $this->_normalize(new Math_BigInteger($left & $right, 256)); - } - - $result = $this->copy(); - - $length = min(count($x->value), count($this->value)); - - $result->value = array_slice($result->value, 0, $length); - - for ($i = 0; $i < $length; ++$i) { - $result->value[$i] = $result->value[$i] & $x->value[$i]; - } - - return $this->_normalize($result); - } - - /** - * Logical Or - * - * @param Math_BigInteger $x - * @access public - * @internal Implemented per a request by Lluis Pamies i Juarez - * @return Math_BigInteger - */ - function bitwise_or($x) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp = new Math_BigInteger(); - $temp->value = gmp_or($this->value, $x->value); - - return $this->_normalize($temp); - case MATH_BIGINTEGER_MODE_BCMATH: - $left = $this->toBytes(); - $right = $x->toBytes(); - - $length = max(strlen($left), strlen($right)); - - $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); - $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); - - return $this->_normalize(new Math_BigInteger($left | $right, 256)); - } - - $length = max(count($this->value), count($x->value)); - $result = $this->copy(); - $result->value = array_pad($result->value, 0, $length); - $x->value = array_pad($x->value, 0, $length); - - for ($i = 0; $i < $length; ++$i) { - $result->value[$i] = $this->value[$i] | $x->value[$i]; - } - - return $this->_normalize($result); - } - - /** - * Logical Exclusive-Or - * - * @param Math_BigInteger $x - * @access public - * @internal Implemented per a request by Lluis Pamies i Juarez - * @return Math_BigInteger - */ - function bitwise_xor($x) - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - $temp = new Math_BigInteger(); - $temp->value = gmp_xor($this->value, $x->value); - - return $this->_normalize($temp); - case MATH_BIGINTEGER_MODE_BCMATH: - $left = $this->toBytes(); - $right = $x->toBytes(); - - $length = max(strlen($left), strlen($right)); - - $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); - $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); - - return $this->_normalize(new Math_BigInteger($left ^ $right, 256)); - } - - $length = max(count($this->value), count($x->value)); - $result = $this->copy(); - $result->value = array_pad($result->value, 0, $length); - $x->value = array_pad($x->value, 0, $length); - - for ($i = 0; $i < $length; ++$i) { - $result->value[$i] = $this->value[$i] ^ $x->value[$i]; - } - - return $this->_normalize($result); - } - - /** - * Logical Not - * - * @access public - * @internal Implemented per a request by Lluis Pamies i Juarez - * @return Math_BigInteger - */ - function bitwise_not() - { - // calculuate "not" without regard to $this->precision - // (will always result in a smaller number. ie. ~1 isn't 1111 1110 - it's 0) - $temp = $this->toBytes(); - $pre_msb = decbin(ord($temp[0])); - $temp = ~$temp; - $msb = decbin(ord($temp[0])); - if (strlen($msb) == 8) { - $msb = substr($msb, strpos($msb, '0')); - } - $temp[0] = chr(bindec($msb)); - - // see if we need to add extra leading 1's - $current_bits = strlen($pre_msb) + 8 * strlen($temp) - 8; - $new_bits = $this->precision - $current_bits; - if ($new_bits <= 0) { - return $this->_normalize(new Math_BigInteger($temp, 256)); - } - - // generate as many leading 1's as we need to. - $leading_ones = chr((1 << ($new_bits & 0x7)) - 1) . str_repeat(chr(0xFF), $new_bits >> 3); - $this->_base256_lshift($leading_ones, $current_bits); - - $temp = str_pad($temp, ceil($this->bits / 8), chr(0), STR_PAD_LEFT); - - return $this->_normalize(new Math_BigInteger($leading_ones | $temp, 256)); - } - - /** - * Logical Right Shift - * - * Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift. - * - * @param Integer $shift - * @return Math_BigInteger - * @access public - * @internal The only version that yields any speed increases is the internal version. - */ - function bitwise_rightShift($shift) - { - $temp = new Math_BigInteger(); - - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - static $two; - - if (!isset($two)) { - $two = gmp_init('2'); - } - - $temp->value = gmp_div_q($this->value, gmp_pow($two, $shift)); - - break; - case MATH_BIGINTEGER_MODE_BCMATH: - $temp->value = bcdiv($this->value, bcpow('2', $shift, 0), 0); - - break; - default: // could just replace _lshift with this, but then all _lshift() calls would need to be rewritten - // and I don't want to do that... - $temp->value = $this->value; - $temp->_rshift($shift); - } - - return $this->_normalize($temp); - } - - /** - * Logical Left Shift - * - * Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift. - * - * @param Integer $shift - * @return Math_BigInteger - * @access public - * @internal The only version that yields any speed increases is the internal version. - */ - function bitwise_leftShift($shift) - { - $temp = new Math_BigInteger(); - - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - static $two; - - if (!isset($two)) { - $two = gmp_init('2'); - } - - $temp->value = gmp_mul($this->value, gmp_pow($two, $shift)); - - break; - case MATH_BIGINTEGER_MODE_BCMATH: - $temp->value = bcmul($this->value, bcpow('2', $shift, 0), 0); - - break; - default: // could just replace _rshift with this, but then all _lshift() calls would need to be rewritten - // and I don't want to do that... - $temp->value = $this->value; - $temp->_lshift($shift); - } - - return $this->_normalize($temp); - } - - /** - * Logical Left Rotate - * - * Instead of the top x bits being dropped they're appended to the shifted bit string. - * - * @param Integer $shift - * @return Math_BigInteger - * @access public - */ - function bitwise_leftRotate($shift) - { - $bits = $this->toBytes(); - - if ($this->precision > 0) { - $precision = $this->precision; - if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) { - $mask = $this->bitmask->subtract(new Math_BigInteger(1)); - $mask = $mask->toBytes(); - } else { - $mask = $this->bitmask->toBytes(); - } - } else { - $temp = ord($bits[0]); - for ($i = 0; $temp >> $i; ++$i); - $precision = 8 * strlen($bits) - 8 + $i; - $mask = chr((1 << ($precision & 0x7)) - 1) . str_repeat(chr(0xFF), $precision >> 3); - } - - if ($shift < 0) { - $shift+= $precision; - } - $shift%= $precision; - - if (!$shift) { - return $this->copy(); - } - - $left = $this->bitwise_leftShift($shift); - $left = $left->bitwise_and(new Math_BigInteger($mask, 256)); - $right = $this->bitwise_rightShift($precision - $shift); - $result = MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ? $left->bitwise_or($right) : $left->add($right); - return $this->_normalize($result); - } - - /** - * Logical Right Rotate - * - * Instead of the bottom x bits being dropped they're prepended to the shifted bit string. - * - * @param Integer $shift - * @return Math_BigInteger - * @access public - */ - function bitwise_rightRotate($shift) - { - return $this->bitwise_leftRotate(-$shift); - } - - /** - * Set random number generator function - * - * $generator should be the name of a random generating function whose first parameter is the minimum - * value and whose second parameter is the maximum value. If this function needs to be seeded, it should - * be seeded prior to calling Math_BigInteger::random() or Math_BigInteger::randomPrime() - * - * If the random generating function is not explicitly set, it'll be assumed to be mt_rand(). - * - * @see random() - * @see randomPrime() - * @param optional String $generator - * @access public - */ - function setRandomGenerator($generator) - { - $this->generator = $generator; - } - - /** - * Generate a random number - * - * @param optional Integer $min - * @param optional Integer $max - * @return Math_BigInteger - * @access public - */ - function random($min = false, $max = false) - { - if ($min === false) { - $min = new Math_BigInteger(0); - } - - if ($max === false) { - $max = new Math_BigInteger(0x7FFFFFFF); - } - - $compare = $max->compare($min); - - if (!$compare) { - return $this->_normalize($min); - } else if ($compare < 0) { - // if $min is bigger then $max, swap $min and $max - $temp = $max; - $max = $min; - $min = $temp; - } - - $generator = $this->generator; - - $max = $max->subtract($min); - $max = ltrim($max->toBytes(), chr(0)); - $size = strlen($max) - 1; - $random = ''; - - $bytes = $size & 1; - for ($i = 0; $i < $bytes; ++$i) { - $random.= chr($generator(0, 255)); - } - - $blocks = $size >> 1; - for ($i = 0; $i < $blocks; ++$i) { - // mt_rand(-2147483648, 0x7FFFFFFF) always produces -2147483648 on some systems - $random.= pack('n', $generator(0, 0xFFFF)); - } - - $temp = new Math_BigInteger($random, 256); - if ($temp->compare(new Math_BigInteger(substr($max, 1), 256)) > 0) { - $random = chr($generator(0, ord($max[0]) - 1)) . $random; - } else { - $random = chr($generator(0, ord($max[0]) )) . $random; - } - - $random = new Math_BigInteger($random, 256); - - return $this->_normalize($random->add($min)); - } - - /** - * Generate a random prime number. - * - * If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed, - * give up and return false. - * - * @param optional Integer $min - * @param optional Integer $max - * @param optional Integer $timeout - * @return Math_BigInteger - * @access public - * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}. - */ - function randomPrime($min = false, $max = false, $timeout = false) - { - $compare = $max->compare($min); - - if (!$compare) { - return $min; - } else if ($compare < 0) { - // if $min is bigger then $max, swap $min and $max - $temp = $max; - $max = $min; - $min = $temp; - } - - // gmp_nextprime() requires PHP 5 >= 5.2.0 per . - if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && function_exists('gmp_nextprime') ) { - // we don't rely on Math_BigInteger::random()'s min / max when gmp_nextprime() is being used since this function - // does its own checks on $max / $min when gmp_nextprime() is used. When gmp_nextprime() is not used, however, - // the same $max / $min checks are not performed. - if ($min === false) { - $min = new Math_BigInteger(0); - } - - if ($max === false) { - $max = new Math_BigInteger(0x7FFFFFFF); - } - - $x = $this->random($min, $max); - - $x->value = gmp_nextprime($x->value); - - if ($x->compare($max) <= 0) { - return $x; - } - - $x->value = gmp_nextprime($min->value); - - if ($x->compare($max) <= 0) { - return $x; - } - - return false; - } - - static $one, $two; - if (!isset($one)) { - $one = new Math_BigInteger(1); - $two = new Math_BigInteger(2); - } - - $start = time(); - - $x = $this->random($min, $max); - if ($x->equals($two)) { - return $x; - } - - $x->_make_odd(); - if ($x->compare($max) > 0) { - // if $x > $max then $max is even and if $min == $max then no prime number exists between the specified range - if ($min->equals($max)) { - return false; - } - $x = $min->copy(); - $x->_make_odd(); - } - - $initial_x = $x->copy(); - - while (true) { - if ($timeout !== false && time() - $start > $timeout) { - return false; - } - - if ($x->isPrime()) { - return $x; - } - - $x = $x->add($two); - - if ($x->compare($max) > 0) { - $x = $min->copy(); - if ($x->equals($two)) { - return $x; - } - $x->_make_odd(); - } - - if ($x->equals($initial_x)) { - return false; - } - } - } - - /** - * Make the current number odd - * - * If the current number is odd it'll be unchanged. If it's even, one will be added to it. - * - * @see randomPrime() - * @access private - */ - function _make_odd() - { - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - gmp_setbit($this->value, 0); - break; - case MATH_BIGINTEGER_MODE_BCMATH: - if ($this->value[strlen($this->value) - 1] % 2 == 0) { - $this->value = bcadd($this->value, '1'); - } - break; - default: - $this->value[0] |= 1; - } - } - - /** - * Checks a numer to see if it's prime - * - * Assuming the $t parameter is not set, this function has an error rate of 2**-80. The main motivation for the - * $t parameter is distributability. Math_BigInteger::randomPrime() can be distributed accross multiple pageloads - * on a website instead of just one. - * - * @param optional Integer $t - * @return Boolean - * @access public - * @internal Uses the - * {@link http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Miller-Rabin primality test}. See - * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=8 HAC 4.24}. - */ - function isPrime($t = false) - { - $length = strlen($this->toBytes()); - - if (!$t) { - // see HAC 4.49 "Note (controlling the error probability)" - if ($length >= 163) { $t = 2; } // floor(1300 / 8) - else if ($length >= 106) { $t = 3; } // floor( 850 / 8) - else if ($length >= 81 ) { $t = 4; } // floor( 650 / 8) - else if ($length >= 68 ) { $t = 5; } // floor( 550 / 8) - else if ($length >= 56 ) { $t = 6; } // floor( 450 / 8) - else if ($length >= 50 ) { $t = 7; } // floor( 400 / 8) - else if ($length >= 43 ) { $t = 8; } // floor( 350 / 8) - else if ($length >= 37 ) { $t = 9; } // floor( 300 / 8) - else if ($length >= 31 ) { $t = 12; } // floor( 250 / 8) - else if ($length >= 25 ) { $t = 15; } // floor( 200 / 8) - else if ($length >= 18 ) { $t = 18; } // floor( 150 / 8) - else { $t = 27; } - } - - // ie. gmp_testbit($this, 0) - // ie. isEven() or !isOdd() - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - return gmp_prob_prime($this->value, $t) != 0; - case MATH_BIGINTEGER_MODE_BCMATH: - if ($this->value === '2') { - return true; - } - if ($this->value[strlen($this->value) - 1] % 2 == 0) { - return false; - } - break; - default: - if ($this->value == array(2)) { - return true; - } - if (~$this->value[0] & 1) { - return false; - } - } - - static $primes, $zero, $one, $two; - - if (!isset($primes)) { - $primes = array( - 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, - 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, - 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, - 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, - 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, - 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, - 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, - 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, - 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, - 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, - 953, 967, 971, 977, 983, 991, 997 - ); - - if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) { - for ($i = 0; $i < count($primes); ++$i) { - $primes[$i] = new Math_BigInteger($primes[$i]); - } - } - - $zero = new Math_BigInteger(); - $one = new Math_BigInteger(1); - $two = new Math_BigInteger(2); - } - - if ($this->equals($one)) { - return false; - } - - // see HAC 4.4.1 "Random search for probable primes" - if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) { - foreach ($primes as $prime) { - list(, $r) = $this->divide($prime); - if ($r->equals($zero)) { - return $this->equals($prime); - } - } - } else { - $value = $this->value; - foreach ($primes as $prime) { - list(, $r) = $this->_divide_digit($value, $prime); - if (!$r) { - return count($value) == 1 && $value[0] == $prime; - } - } - } - - $n = $this->copy(); - $n_1 = $n->subtract($one); - $n_2 = $n->subtract($two); - - $r = $n_1->copy(); - $r_value = $r->value; - // ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s)); - if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) { - $s = 0; - // if $n was 1, $r would be 0 and this would be an infinite loop, hence our $this->equals($one) check earlier - while ($r->value[strlen($r->value) - 1] % 2 == 0) { - $r->value = bcdiv($r->value, '2', 0); - ++$s; - } - } else { - for ($i = 0, $r_length = count($r_value); $i < $r_length; ++$i) { - $temp = ~$r_value[$i] & 0xFFFFFF; - for ($j = 1; ($temp >> $j) & 1; ++$j); - if ($j != 25) { - break; - } - } - $s = 26 * $i + $j - 1; - $r->_rshift($s); - } - - for ($i = 0; $i < $t; ++$i) { - $a = $this->random($two, $n_2); - $y = $a->modPow($r, $n); - - if (!$y->equals($one) && !$y->equals($n_1)) { - for ($j = 1; $j < $s && !$y->equals($n_1); ++$j) { - $y = $y->modPow($two, $n); - if ($y->equals($one)) { - return false; - } - } - - if (!$y->equals($n_1)) { - return false; - } - } - } - return true; - } - - /** - * Logical Left Shift - * - * Shifts BigInteger's by $shift bits. - * - * @param Integer $shift - * @access private - */ - function _lshift($shift) - { - if ( $shift == 0 ) { - return; - } - - $num_digits = (int) ($shift / 26); - $shift %= 26; - $shift = 1 << $shift; - - $carry = 0; - - for ($i = 0; $i < count($this->value); ++$i) { - $temp = $this->value[$i] * $shift + $carry; - $carry = (int) ($temp / 0x4000000); - $this->value[$i] = (int) ($temp - $carry * 0x4000000); - } - - if ( $carry ) { - $this->value[] = $carry; - } - - while ($num_digits--) { - array_unshift($this->value, 0); - } - } - - /** - * Logical Right Shift - * - * Shifts BigInteger's by $shift bits. - * - * @param Integer $shift - * @access private - */ - function _rshift($shift) - { - if ($shift == 0) { - return; - } - - $num_digits = (int) ($shift / 26); - $shift %= 26; - $carry_shift = 26 - $shift; - $carry_mask = (1 << $shift) - 1; - - if ( $num_digits ) { - $this->value = array_slice($this->value, $num_digits); - } - - $carry = 0; - - for ($i = count($this->value) - 1; $i >= 0; --$i) { - $temp = $this->value[$i] >> $shift | $carry; - $carry = ($this->value[$i] & $carry_mask) << $carry_shift; - $this->value[$i] = $temp; - } - - $this->value = $this->_trim($this->value); - } - - /** - * Normalize - * - * Removes leading zeros and truncates (if necessary) to maintain the appropriate precision - * - * @param Math_BigInteger - * @return Math_BigInteger - * @see _trim() - * @access private - */ - function _normalize($result) - { - $result->precision = $this->precision; - $result->bitmask = $this->bitmask; - - switch ( MATH_BIGINTEGER_MODE ) { - case MATH_BIGINTEGER_MODE_GMP: - if (!empty($result->bitmask->value)) { - $result->value = gmp_and($result->value, $result->bitmask->value); - } - - return $result; - case MATH_BIGINTEGER_MODE_BCMATH: - if (!empty($result->bitmask->value)) { - $result->value = bcmod($result->value, $result->bitmask->value); - } - - return $result; - } - - $value = &$result->value; - - if ( !count($value) ) { - return $result; - } - - $value = $this->_trim($value); - - if (!empty($result->bitmask->value)) { - $length = min(count($value), count($this->bitmask->value)); - $value = array_slice($value, 0, $length); - - for ($i = 0; $i < $length; ++$i) { - $value[$i] = $value[$i] & $this->bitmask->value[$i]; - } - } - - return $result; - } - - /** - * Trim - * - * Removes leading zeros - * - * @return Math_BigInteger - * @access private - */ - function _trim($value) - { - for ($i = count($value) - 1; $i >= 0; --$i) { - if ( $value[$i] ) { - break; - } - unset($value[$i]); - } - - return $value; - } - - /** - * Array Repeat - * - * @param $input Array - * @param $multiplier mixed - * @return Array - * @access private - */ - function _array_repeat($input, $multiplier) - { - return ($multiplier) ? array_fill(0, $multiplier, $input) : array(); - } - - /** - * Logical Left Shift - * - * Shifts binary strings $shift bits, essentially multiplying by 2**$shift. - * - * @param $x String - * @param $shift Integer - * @return String - * @access private - */ - function _base256_lshift(&$x, $shift) - { - if ($shift == 0) { - return; - } - - $num_bytes = $shift >> 3; // eg. floor($shift/8) - $shift &= 7; // eg. $shift % 8 - - $carry = 0; - for ($i = strlen($x) - 1; $i >= 0; --$i) { - $temp = ord($x[$i]) << $shift | $carry; - $x[$i] = chr($temp); - $carry = $temp >> 8; - } - $carry = ($carry != 0) ? chr($carry) : ''; - $x = $carry . $x . str_repeat(chr(0), $num_bytes); - } - - /** - * Logical Right Shift - * - * Shifts binary strings $shift bits, essentially dividing by 2**$shift and returning the remainder. - * - * @param $x String - * @param $shift Integer - * @return String - * @access private - */ - function _base256_rshift(&$x, $shift) - { - if ($shift == 0) { - $x = ltrim($x, chr(0)); - return ''; - } - - $num_bytes = $shift >> 3; // eg. floor($shift/8) - $shift &= 7; // eg. $shift % 8 - - $remainder = ''; - if ($num_bytes) { - $start = $num_bytes > strlen($x) ? -strlen($x) : -$num_bytes; - $remainder = substr($x, $start); - $x = substr($x, 0, -$num_bytes); - } - - $carry = 0; - $carry_shift = 8 - $shift; - for ($i = 0; $i < strlen($x); ++$i) { - $temp = (ord($x[$i]) >> $shift) | $carry; - $carry = (ord($x[$i]) << $carry_shift) & 0xFF; - $x[$i] = chr($temp); - } - $x = ltrim($x, chr(0)); - - $remainder = chr($carry >> $carry_shift) . $remainder; - - return ltrim($remainder, chr(0)); - } - - // one quirk about how the following functions are implemented is that PHP defines N to be an unsigned long - // at 32-bits, while java's longs are 64-bits. - - /** - * Converts 32-bit integers to bytes. - * - * @param Integer $x - * @return String - * @access private - */ - function _int2bytes($x) - { - return ltrim(pack('N', $x), chr(0)); - } - - /** - * Converts bytes to 32-bit integers - * - * @param String $x - * @return Integer - * @access private - */ - function _bytes2int($x) - { - $temp = unpack('Nint', str_pad($x, 4, chr(0), STR_PAD_LEFT)); - return $temp['int']; - } +> and << cannot be used, nor can the modulo operator %, + * which only supports integers. Although this fact will slow this library down, the fact that such a high + * base is being used should more than compensate. + * + * When PHP version 6 is officially released, we'll be able to use 64-bit integers. This should, once again, + * allow bitwise operators, and will increase the maximum possible base to 2**31 (or 2**62 for addition / + * subtraction). + * + * Numbers are stored in {@link http://en.wikipedia.org/wiki/Endianness little endian} format. ie. + * (new Math_BigInteger(pow(2, 26)))->value = array(0, 1) + * + * Useful resources are as follows: + * + * - {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf Handbook of Applied Cryptography (HAC)} + * - {@link http://math.libtomcrypt.com/files/tommath.pdf Multi-Precision Math (MPM)} + * - Java's BigInteger classes. See /j2se/src/share/classes/java/math in jdk-1_5_0-src-jrl.zip + * + * Here's an example of how to use this library: + * + * add($b); + * + * echo $c->toString(); // outputs 5 + * ?> + * + * + * LICENSE: This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * @category Math + * @package Math_BigInteger + * @author Jim Wigginton + * @copyright MMVI Jim Wigginton + * @license http://www.gnu.org/licenses/lgpl.txt + * @version $Id: BigInteger.php,v 1.33 2010/03/22 22:32:03 terrafrost Exp $ + * @link http://pear.php.net/package/Math_BigInteger + */ + +/**#@+ + * Reduction constants + * + * @access private + * @see Math_BigInteger::_reduce() + */ +/** + * @see Math_BigInteger::_montgomery() + * @see Math_BigInteger::_prepMontgomery() + */ +define('MATH_BIGINTEGER_MONTGOMERY', 0); +/** + * @see Math_BigInteger::_barrett() + */ +define('MATH_BIGINTEGER_BARRETT', 1); +/** + * @see Math_BigInteger::_mod2() + */ +define('MATH_BIGINTEGER_POWEROF2', 2); +/** + * @see Math_BigInteger::_remainder() + */ +define('MATH_BIGINTEGER_CLASSIC', 3); +/** + * @see Math_BigInteger::__clone() + */ +define('MATH_BIGINTEGER_NONE', 4); +/**#@-*/ + +/**#@+ + * Array constants + * + * Rather than create a thousands and thousands of new Math_BigInteger objects in repeated function calls to add() and + * multiply() or whatever, we'll just work directly on arrays, taking them in as parameters and returning them. + * + * @access private + */ +/** + * $result[MATH_BIGINTEGER_VALUE] contains the value. + */ +define('MATH_BIGINTEGER_VALUE', 0); +/** + * $result[MATH_BIGINTEGER_SIGN] contains the sign. + */ +define('MATH_BIGINTEGER_SIGN', 1); +/**#@-*/ + +/**#@+ + * @access private + * @see Math_BigInteger::_montgomery() + * @see Math_BigInteger::_barrett() + */ +/** + * Cache constants + * + * $cache[MATH_BIGINTEGER_VARIABLE] tells us whether or not the cached data is still valid. + */ +define('MATH_BIGINTEGER_VARIABLE', 0); +/** + * $cache[MATH_BIGINTEGER_DATA] contains the cached data. + */ +define('MATH_BIGINTEGER_DATA', 1); +/**#@-*/ + +/**#@+ + * Mode constants. + * + * @access private + * @see Math_BigInteger::Math_BigInteger() + */ +/** + * To use the pure-PHP implementation + */ +define('MATH_BIGINTEGER_MODE_INTERNAL', 1); +/** + * To use the BCMath library + * + * (if enabled; otherwise, the internal implementation will be used) + */ +define('MATH_BIGINTEGER_MODE_BCMATH', 2); +/** + * To use the GMP library + * + * (if present; otherwise, either the BCMath or the internal implementation will be used) + */ +define('MATH_BIGINTEGER_MODE_GMP', 3); +/**#@-*/ + +/** + * The largest digit that may be used in addition / subtraction + * + * (we do pow(2, 52) instead of using 4503599627370496, directly, because some PHP installations + * will truncate 4503599627370496) + * + * @access private + */ +define('MATH_BIGINTEGER_MAX_DIGIT52', pow(2, 52)); + +/** + * Karatsuba Cutoff + * + * At what point do we switch between Karatsuba multiplication and schoolbook long multiplication? + * + * @access private + */ +define('MATH_BIGINTEGER_KARATSUBA_CUTOFF', 25); + +/** + * Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 + * numbers. + * + * @author Jim Wigginton + * @version 1.0.0RC4 + * @access public + * @package Math_BigInteger + */ +class Math_BigInteger { + /** + * Holds the BigInteger's value. + * + * @var Array + * @access private + */ + var $value; + + /** + * Holds the BigInteger's magnitude. + * + * @var Boolean + * @access private + */ + var $is_negative = false; + + /** + * Random number generator function + * + * @see setRandomGenerator() + * @access private + */ + var $generator = 'mt_rand'; + + /** + * Precision + * + * @see setPrecision() + * @access private + */ + var $precision = -1; + + /** + * Precision Bitmask + * + * @see setPrecision() + * @access private + */ + var $bitmask = false; + + /** + * Mode independant value used for serialization. + * + * If the bcmath or gmp extensions are installed $this->value will be a non-serializable resource, hence the need for + * a variable that'll be serializable regardless of whether or not extensions are being used. Unlike $this->value, + * however, $this->hex is only calculated when $this->__sleep() is called. + * + * @see __sleep() + * @see __wakeup() + * @var String + * @access private + */ + var $hex; + + /** + * Converts base-2, base-10, base-16, and binary strings (eg. base-256) to BigIntegers. + * + * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using + * two's compliment. The sole exception to this is -10, which is treated the same as 10 is. + * + * Here's an example: + * + * toString(); // outputs 50 + * ?> + * + * + * @param optional $x base-10 number or base-$base number if $base set. + * @param optional integer $base + * @return Math_BigInteger + * @access public + */ + function Math_BigInteger($x = 0, $base = 10) + { + if ( !defined('MATH_BIGINTEGER_MODE') ) { + switch (true) { + case extension_loaded('gmp'): + define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_GMP); + break; + case extension_loaded('bcmath'): + define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_BCMATH); + break; + default: + define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_INTERNAL); + } + } + + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + if (is_resource($x) && get_resource_type($x) == 'GMP integer') { + $this->value = $x; + return; + } + $this->value = gmp_init(0); + break; + case MATH_BIGINTEGER_MODE_BCMATH: + $this->value = '0'; + break; + default: + $this->value = array(); + } + + if (empty($x)) { + return; + } + + switch ($base) { + case -256: + if (ord($x[0]) & 0x80) { + $x = ~$x; + $this->is_negative = true; + } + case 256: + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $sign = $this->is_negative ? '-' : ''; + $this->value = gmp_init($sign . '0x' . bin2hex($x)); + break; + case MATH_BIGINTEGER_MODE_BCMATH: + // round $len to the nearest 4 (thanks, DavidMJ!) + $len = (strlen($x) + 3) & 0xFFFFFFFC; + + $x = str_pad($x, $len, chr(0), STR_PAD_LEFT); + + for ($i = 0; $i < $len; $i+= 4) { + $this->value = bcmul($this->value, '4294967296', 0); // 4294967296 == 2**32 + $this->value = bcadd($this->value, 0x1000000 * ord($x[$i]) + ((ord($x[$i + 1]) << 16) | (ord($x[$i + 2]) << 8) | ord($x[$i + 3])), 0); + } + + if ($this->is_negative) { + $this->value = '-' . $this->value; + } + + break; + // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb) + default: + while (strlen($x)) { + $this->value[] = $this->_bytes2int($this->_base256_rshift($x, 26)); + } + } + + if ($this->is_negative) { + if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) { + $this->is_negative = false; + } + $temp = $this->add(new Math_BigInteger('-1')); + $this->value = $temp->value; + } + break; + case 16: + case -16: + if ($base > 0 && $x[0] == '-') { + $this->is_negative = true; + $x = substr($x, 1); + } + + $x = preg_replace('#^(?:0x)?([A-Fa-f0-9]*).*#', '$1', $x); + + $is_negative = false; + if ($base < 0 && hexdec($x[0]) >= 8) { + $this->is_negative = $is_negative = true; + $x = bin2hex(~pack('H*', $x)); + } + + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp = $this->is_negative ? '-0x' . $x : '0x' . $x; + $this->value = gmp_init($temp); + $this->is_negative = false; + break; + case MATH_BIGINTEGER_MODE_BCMATH: + $x = ( strlen($x) & 1 ) ? '0' . $x : $x; + $temp = new Math_BigInteger(pack('H*', $x), 256); + $this->value = $this->is_negative ? '-' . $temp->value : $temp->value; + $this->is_negative = false; + break; + default: + $x = ( strlen($x) & 1 ) ? '0' . $x : $x; + $temp = new Math_BigInteger(pack('H*', $x), 256); + $this->value = $temp->value; + } + + if ($is_negative) { + $temp = $this->add(new Math_BigInteger('-1')); + $this->value = $temp->value; + } + break; + case 10: + case -10: + $x = preg_replace('#^(-?[0-9]*).*#', '$1', $x); + + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $this->value = gmp_init($x); + break; + case MATH_BIGINTEGER_MODE_BCMATH: + // explicitly casting $x to a string is necessary, here, since doing $x[0] on -1 yields different + // results then doing it on '-1' does (modInverse does $x[0]) + $this->value = (string) $x; + break; + default: + $temp = new Math_BigInteger(); + + // array(10000000) is 10**7 in base-2**26. 10**7 is the closest to 2**26 we can get without passing it. + $multiplier = new Math_BigInteger(); + $multiplier->value = array(10000000); + + if ($x[0] == '-') { + $this->is_negative = true; + $x = substr($x, 1); + } + + $x = str_pad($x, strlen($x) + (6 * strlen($x)) % 7, 0, STR_PAD_LEFT); + + while (strlen($x)) { + $temp = $temp->multiply($multiplier); + $temp = $temp->add(new Math_BigInteger($this->_int2bytes(substr($x, 0, 7)), 256)); + $x = substr($x, 7); + } + + $this->value = $temp->value; + } + break; + case 2: // base-2 support originally implemented by Lluis Pamies - thanks! + case -2: + if ($base > 0 && $x[0] == '-') { + $this->is_negative = true; + $x = substr($x, 1); + } + + $x = preg_replace('#^([01]*).*#', '$1', $x); + $x = str_pad($x, strlen($x) + (3 * strlen($x)) % 4, 0, STR_PAD_LEFT); + + $str = '0x'; + while (strlen($x)) { + $part = substr($x, 0, 4); + $str.= dechex(bindec($part)); + $x = substr($x, 4); + } + + if ($this->is_negative) { + $str = '-' . $str; + } + + $temp = new Math_BigInteger($str, 8 * $base); // ie. either -16 or +16 + $this->value = $temp->value; + $this->is_negative = $temp->is_negative; + + break; + default: + // base not supported, so we'll let $this == 0 + } + } + + /** + * Converts a BigInteger to a byte string (eg. base-256). + * + * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're + * saved as two's compliment. + * + * Here's an example: + * + * toBytes(); // outputs chr(65) + * ?> + * + * + * @param Boolean $twos_compliment + * @return String + * @access public + * @internal Converts a base-2**26 number to base-2**8 + */ + function toBytes($twos_compliment = false) + { + if ($twos_compliment) { + $comparison = $this->compare(new Math_BigInteger()); + if ($comparison == 0) { + return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; + } + + $temp = $comparison < 0 ? $this->add(new Math_BigInteger(1)) : $this->copy(); + $bytes = $temp->toBytes(); + + if (empty($bytes)) { // eg. if the number we're trying to convert is -1 + $bytes = chr(0); + } + + if (ord($bytes[0]) & 0x80) { + $bytes = chr(0) . $bytes; + } + + return $comparison < 0 ? ~$bytes : $bytes; + } + + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + if (gmp_cmp($this->value, gmp_init(0)) == 0) { + return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; + } + + $temp = gmp_strval(gmp_abs($this->value), 16); + $temp = ( strlen($temp) & 1 ) ? '0' . $temp : $temp; + $temp = pack('H*', $temp); + + return $this->precision > 0 ? + substr(str_pad($temp, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) : + ltrim($temp, chr(0)); + case MATH_BIGINTEGER_MODE_BCMATH: + if ($this->value === '0') { + return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; + } + + $value = ''; + $current = $this->value; + + if ($current[0] == '-') { + $current = substr($current, 1); + } + + while (bccomp($current, '0', 0) > 0) { + $temp = bcmod($current, '16777216'); + $value = chr($temp >> 16) . chr($temp >> 8) . chr($temp) . $value; + $current = bcdiv($current, '16777216', 0); + } + + return $this->precision > 0 ? + substr(str_pad($value, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) : + ltrim($value, chr(0)); + } + + if (!count($this->value)) { + return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; + } + $result = $this->_int2bytes($this->value[count($this->value) - 1]); + + $temp = $this->copy(); + + for ($i = count($temp->value) - 2; $i >= 0; --$i) { + $temp->_base256_lshift($result, 26); + $result = $result | str_pad($temp->_int2bytes($temp->value[$i]), strlen($result), chr(0), STR_PAD_LEFT); + } + + return $this->precision > 0 ? + str_pad(substr($result, -(($this->precision + 7) >> 3)), ($this->precision + 7) >> 3, chr(0), STR_PAD_LEFT) : + $result; + } + + /** + * Converts a BigInteger to a hex string (eg. base-16)). + * + * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're + * saved as two's compliment. + * + * Here's an example: + * + * toHex(); // outputs '41' + * ?> + * + * + * @param Boolean $twos_compliment + * @return String + * @access public + * @internal Converts a base-2**26 number to base-2**8 + */ + function toHex($twos_compliment = false) + { + return bin2hex($this->toBytes($twos_compliment)); + } + + /** + * Converts a BigInteger to a bit string (eg. base-2). + * + * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're + * saved as two's compliment. + * + * Here's an example: + * + * toBits(); // outputs '1000001' + * ?> + * + * + * @param Boolean $twos_compliment + * @return String + * @access public + * @internal Converts a base-2**26 number to base-2**2 + */ + function toBits($twos_compliment = false) + { + $hex = $this->toHex($twos_compliment); + $bits = ''; + for ($i = 0; $i < strlen($hex); $i+=8) { + $bits.= str_pad(decbin(hexdec(substr($hex, $i, 8))), 32, '0', STR_PAD_LEFT); + } + return $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0'); + } + + /** + * Converts a BigInteger to a base-10 number. + * + * Here's an example: + * + * toString(); // outputs 50 + * ?> + * + * + * @return String + * @access public + * @internal Converts a base-2**26 number to base-10**7 (which is pretty much base-10) + */ + function toString() + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + return gmp_strval($this->value); + case MATH_BIGINTEGER_MODE_BCMATH: + if ($this->value === '0') { + return '0'; + } + + return ltrim($this->value, '0'); + } + + if (!count($this->value)) { + return '0'; + } + + $temp = $this->copy(); + $temp->is_negative = false; + + $divisor = new Math_BigInteger(); + $divisor->value = array(10000000); // eg. 10**7 + $result = ''; + while (count($temp->value)) { + list($temp, $mod) = $temp->divide($divisor); + $result = str_pad(isset($mod->value[0]) ? $mod->value[0] : '', 7, '0', STR_PAD_LEFT) . $result; + } + $result = ltrim($result, '0'); + if (empty($result)) { + $result = '0'; + } + + if ($this->is_negative) { + $result = '-' . $result; + } + + return $result; + } + + /** + * Copy an object + * + * PHP5 passes objects by reference while PHP4 passes by value. As such, we need a function to guarantee + * that all objects are passed by value, when appropriate. More information can be found here: + * + * {@link http://php.net/language.oop5.basic#51624} + * + * @access public + * @see __clone() + * @return Math_BigInteger + */ + function copy() + { + $temp = new Math_BigInteger(); + $temp->value = $this->value; + $temp->is_negative = $this->is_negative; + $temp->generator = $this->generator; + $temp->precision = $this->precision; + $temp->bitmask = $this->bitmask; + return $temp; + } + + /** + * __toString() magic method + * + * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call + * toString(). + * + * @access public + * @internal Implemented per a suggestion by Techie-Michael - thanks! + */ + function __toString() + { + return $this->toString(); + } + + /** + * __clone() magic method + * + * Although you can call Math_BigInteger::__toString() directly in PHP5, you cannot call Math_BigInteger::__clone() + * directly in PHP5. You can in PHP4 since it's not a magic method, but in PHP5, you have to call it by using the PHP5 + * only syntax of $y = clone $x. As such, if you're trying to write an application that works on both PHP4 and PHP5, + * call Math_BigInteger::copy(), instead. + * + * @access public + * @see copy() + * @return Math_BigInteger + */ + function __clone() + { + return $this->copy(); + } + + /** + * __sleep() magic method + * + * Will be called, automatically, when serialize() is called on a Math_BigInteger object. + * + * @see __wakeup() + * @access public + */ + function __sleep() + { + $this->hex = $this->toHex(true); + $vars = array('hex'); + if ($this->generator != 'mt_rand') { + $vars[] = 'generator'; + } + if ($this->precision > 0) { + $vars[] = 'precision'; + } + return $vars; + + } + + /** + * __wakeup() magic method + * + * Will be called, automatically, when unserialize() is called on a Math_BigInteger object. + * + * @see __sleep() + * @access public + */ + function __wakeup() + { + $temp = new Math_BigInteger($this->hex, -16); + $this->value = $temp->value; + $this->is_negative = $temp->is_negative; + $this->setRandomGenerator($this->generator); + if ($this->precision > 0) { + // recalculate $this->bitmask + $this->setPrecision($this->precision); + } + } + + /** + * Adds two BigIntegers. + * + * Here's an example: + * + * add($b); + * + * echo $c->toString(); // outputs 30 + * ?> + * + * + * @param Math_BigInteger $y + * @return Math_BigInteger + * @access public + * @internal Performs base-2**52 addition + */ + function add($y) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp = new Math_BigInteger(); + $temp->value = gmp_add($this->value, $y->value); + + return $this->_normalize($temp); + case MATH_BIGINTEGER_MODE_BCMATH: + $temp = new Math_BigInteger(); + $temp->value = bcadd($this->value, $y->value, 0); + + return $this->_normalize($temp); + } + + $temp = $this->_add($this->value, $this->is_negative, $y->value, $y->is_negative); + + $result = new Math_BigInteger(); + $result->value = $temp[MATH_BIGINTEGER_VALUE]; + $result->is_negative = $temp[MATH_BIGINTEGER_SIGN]; + + return $this->_normalize($result); + } + + /** + * Performs addition. + * + * @param Array $x_value + * @param Boolean $x_negative + * @param Array $y_value + * @param Boolean $y_negative + * @return Array + * @access private + */ + function _add($x_value, $x_negative, $y_value, $y_negative) + { + $x_size = count($x_value); + $y_size = count($y_value); + + if ($x_size == 0) { + return array( + MATH_BIGINTEGER_VALUE => $y_value, + MATH_BIGINTEGER_SIGN => $y_negative + ); + } else if ($y_size == 0) { + return array( + MATH_BIGINTEGER_VALUE => $x_value, + MATH_BIGINTEGER_SIGN => $x_negative + ); + } + + // subtract, if appropriate + if ( $x_negative != $y_negative ) { + if ( $x_value == $y_value ) { + return array( + MATH_BIGINTEGER_VALUE => array(), + MATH_BIGINTEGER_SIGN => false + ); + } + + $temp = $this->_subtract($x_value, false, $y_value, false); + $temp[MATH_BIGINTEGER_SIGN] = $this->_compare($x_value, false, $y_value, false) > 0 ? + $x_negative : $y_negative; + + return $temp; + } + + if ($x_size < $y_size) { + $size = $x_size; + $value = $y_value; + } else { + $size = $y_size; + $value = $x_value; + } + + $value[] = 0; // just in case the carry adds an extra digit + + $carry = 0; + for ($i = 0, $j = 1; $j < $size; $i+=2, $j+=2) { + $sum = $x_value[$j] * 0x4000000 + $x_value[$i] + $y_value[$j] * 0x4000000 + $y_value[$i] + $carry; + $carry = $sum >= MATH_BIGINTEGER_MAX_DIGIT52; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1 + $sum = $carry ? $sum - MATH_BIGINTEGER_MAX_DIGIT52 : $sum; + + $temp = (int) ($sum / 0x4000000); + + $value[$i] = (int) ($sum - 0x4000000 * $temp); // eg. a faster alternative to fmod($sum, 0x4000000) + $value[$j] = $temp; + } + + if ($j == $size) { // ie. if $y_size is odd + $sum = $x_value[$i] + $y_value[$i] + $carry; + $carry = $sum >= 0x4000000; + $value[$i] = $carry ? $sum - 0x4000000 : $sum; + ++$i; // ie. let $i = $j since we've just done $value[$i] + } + + if ($carry) { + for (; $value[$i] == 0x3FFFFFF; ++$i) { + $value[$i] = 0; + } + ++$value[$i]; + } + + return array( + MATH_BIGINTEGER_VALUE => $this->_trim($value), + MATH_BIGINTEGER_SIGN => $x_negative + ); + } + + /** + * Subtracts two BigIntegers. + * + * Here's an example: + * + * subtract($b); + * + * echo $c->toString(); // outputs -10 + * ?> + * + * + * @param Math_BigInteger $y + * @return Math_BigInteger + * @access public + * @internal Performs base-2**52 subtraction + */ + function subtract($y) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp = new Math_BigInteger(); + $temp->value = gmp_sub($this->value, $y->value); + + return $this->_normalize($temp); + case MATH_BIGINTEGER_MODE_BCMATH: + $temp = new Math_BigInteger(); + $temp->value = bcsub($this->value, $y->value, 0); + + return $this->_normalize($temp); + } + + $temp = $this->_subtract($this->value, $this->is_negative, $y->value, $y->is_negative); + + $result = new Math_BigInteger(); + $result->value = $temp[MATH_BIGINTEGER_VALUE]; + $result->is_negative = $temp[MATH_BIGINTEGER_SIGN]; + + return $this->_normalize($result); + } + + /** + * Performs subtraction. + * + * @param Array $x_value + * @param Boolean $x_negative + * @param Array $y_value + * @param Boolean $y_negative + * @return Array + * @access private + */ + function _subtract($x_value, $x_negative, $y_value, $y_negative) + { + $x_size = count($x_value); + $y_size = count($y_value); + + if ($x_size == 0) { + return array( + MATH_BIGINTEGER_VALUE => $y_value, + MATH_BIGINTEGER_SIGN => !$y_negative + ); + } else if ($y_size == 0) { + return array( + MATH_BIGINTEGER_VALUE => $x_value, + MATH_BIGINTEGER_SIGN => $x_negative + ); + } + + // add, if appropriate (ie. -$x - +$y or +$x - -$y) + if ( $x_negative != $y_negative ) { + $temp = $this->_add($x_value, false, $y_value, false); + $temp[MATH_BIGINTEGER_SIGN] = $x_negative; + + return $temp; + } + + $diff = $this->_compare($x_value, $x_negative, $y_value, $y_negative); + + if ( !$diff ) { + return array( + MATH_BIGINTEGER_VALUE => array(), + MATH_BIGINTEGER_SIGN => false + ); + } + + // switch $x and $y around, if appropriate. + if ( (!$x_negative && $diff < 0) || ($x_negative && $diff > 0) ) { + $temp = $x_value; + $x_value = $y_value; + $y_value = $temp; + + $x_negative = !$x_negative; + + $x_size = count($x_value); + $y_size = count($y_value); + } + + // at this point, $x_value should be at least as big as - if not bigger than - $y_value + + $carry = 0; + for ($i = 0, $j = 1; $j < $y_size; $i+=2, $j+=2) { + $sum = $x_value[$j] * 0x4000000 + $x_value[$i] - $y_value[$j] * 0x4000000 - $y_value[$i] - $carry; + $carry = $sum < 0; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1 + $sum = $carry ? $sum + MATH_BIGINTEGER_MAX_DIGIT52 : $sum; + + $temp = (int) ($sum / 0x4000000); + + $x_value[$i] = (int) ($sum - 0x4000000 * $temp); + $x_value[$j] = $temp; + } + + if ($j == $y_size) { // ie. if $y_size is odd + $sum = $x_value[$i] - $y_value[$i] - $carry; + $carry = $sum < 0; + $x_value[$i] = $carry ? $sum + 0x4000000 : $sum; + ++$i; + } + + if ($carry) { + for (; !$x_value[$i]; ++$i) { + $x_value[$i] = 0x3FFFFFF; + } + --$x_value[$i]; + } + + return array( + MATH_BIGINTEGER_VALUE => $this->_trim($x_value), + MATH_BIGINTEGER_SIGN => $x_negative + ); + } + + /** + * Multiplies two BigIntegers + * + * Here's an example: + * + * multiply($b); + * + * echo $c->toString(); // outputs 200 + * ?> + * + * + * @param Math_BigInteger $x + * @return Math_BigInteger + * @access public + */ + function multiply($x) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp = new Math_BigInteger(); + $temp->value = gmp_mul($this->value, $x->value); + + return $this->_normalize($temp); + case MATH_BIGINTEGER_MODE_BCMATH: + $temp = new Math_BigInteger(); + $temp->value = bcmul($this->value, $x->value, 0); + + return $this->_normalize($temp); + } + + $temp = $this->_multiply($this->value, $this->is_negative, $x->value, $x->is_negative); + + $product = new Math_BigInteger(); + $product->value = $temp[MATH_BIGINTEGER_VALUE]; + $product->is_negative = $temp[MATH_BIGINTEGER_SIGN]; + + return $this->_normalize($product); + } + + /** + * Performs multiplication. + * + * @param Array $x_value + * @param Boolean $x_negative + * @param Array $y_value + * @param Boolean $y_negative + * @return Array + * @access private + */ + function _multiply($x_value, $x_negative, $y_value, $y_negative) + { + //if ( $x_value == $y_value ) { + // return array( + // MATH_BIGINTEGER_VALUE => $this->_square($x_value), + // MATH_BIGINTEGER_SIGN => $x_sign != $y_value + // ); + //} + + $x_length = count($x_value); + $y_length = count($y_value); + + if ( !$x_length || !$y_length ) { // a 0 is being multiplied + return array( + MATH_BIGINTEGER_VALUE => array(), + MATH_BIGINTEGER_SIGN => false + ); + } + + return array( + MATH_BIGINTEGER_VALUE => min($x_length, $y_length) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ? + $this->_trim($this->_regularMultiply($x_value, $y_value)) : + $this->_trim($this->_karatsuba($x_value, $y_value)), + MATH_BIGINTEGER_SIGN => $x_negative != $y_negative + ); + } + + /** + * Performs long multiplication on two BigIntegers + * + * Modeled after 'multiply' in MutableBigInteger.java. + * + * @param Array $x_value + * @param Array $y_value + * @return Array + * @access private + */ + function _regularMultiply($x_value, $y_value) + { + $x_length = count($x_value); + $y_length = count($y_value); + + if ( !$x_length || !$y_length ) { // a 0 is being multiplied + return array(); + } + + if ( $x_length < $y_length ) { + $temp = $x_value; + $x_value = $y_value; + $y_value = $temp; + + $x_length = count($x_value); + $y_length = count($y_value); + } + + $product_value = $this->_array_repeat(0, $x_length + $y_length); + + // the following for loop could be removed if the for loop following it + // (the one with nested for loops) initially set $i to 0, but + // doing so would also make the result in one set of unnecessary adds, + // since on the outermost loops first pass, $product->value[$k] is going + // to always be 0 + + $carry = 0; + + for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0 + $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0 + $carry = (int) ($temp / 0x4000000); + $product_value[$j] = (int) ($temp - 0x4000000 * $carry); + } + + $product_value[$j] = $carry; + + // the above for loop is what the previous comment was talking about. the + // following for loop is the "one with nested for loops" + for ($i = 1; $i < $y_length; ++$i) { + $carry = 0; + + for ($j = 0, $k = $i; $j < $x_length; ++$j, ++$k) { + $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry; + $carry = (int) ($temp / 0x4000000); + $product_value[$k] = (int) ($temp - 0x4000000 * $carry); + } + + $product_value[$k] = $carry; + } + + return $product_value; + } + + /** + * Performs Karatsuba multiplication on two BigIntegers + * + * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and + * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=120 MPM 5.2.3}. + * + * @param Array $x_value + * @param Array $y_value + * @return Array + * @access private + */ + function _karatsuba($x_value, $y_value) + { + $m = min(count($x_value) >> 1, count($y_value) >> 1); + + if ($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF) { + return $this->_regularMultiply($x_value, $y_value); + } + + $x1 = array_slice($x_value, $m); + $x0 = array_slice($x_value, 0, $m); + $y1 = array_slice($y_value, $m); + $y0 = array_slice($y_value, 0, $m); + + $z2 = $this->_karatsuba($x1, $y1); + $z0 = $this->_karatsuba($x0, $y0); + + $z1 = $this->_add($x1, false, $x0, false); + $temp = $this->_add($y1, false, $y0, false); + $z1 = $this->_karatsuba($z1[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_VALUE]); + $temp = $this->_add($z2, false, $z0, false); + $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false); + + $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2); + $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]); + + $xy = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]); + $xy = $this->_add($xy[MATH_BIGINTEGER_VALUE], $xy[MATH_BIGINTEGER_SIGN], $z0, false); + + return $xy[MATH_BIGINTEGER_VALUE]; + } + + /** + * Performs squaring + * + * @param Array $x + * @return Array + * @access private + */ + function _square($x = false) + { + return count($x) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ? + $this->_trim($this->_baseSquare($x)) : + $this->_trim($this->_karatsubaSquare($x)); + } + + /** + * Performs traditional squaring on two BigIntegers + * + * Squaring can be done faster than multiplying a number by itself can be. See + * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=7 HAC 14.2.4} / + * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=141 MPM 5.3} for more information. + * + * @param Array $value + * @return Array + * @access private + */ + function _baseSquare($value) + { + if ( empty($value) ) { + return array(); + } + $square_value = $this->_array_repeat(0, 2 * count($value)); + + for ($i = 0, $max_index = count($value) - 1; $i <= $max_index; ++$i) { + $i2 = $i << 1; + + $temp = $square_value[$i2] + $value[$i] * $value[$i]; + $carry = (int) ($temp / 0x4000000); + $square_value[$i2] = (int) ($temp - 0x4000000 * $carry); + + // note how we start from $i+1 instead of 0 as we do in multiplication. + for ($j = $i + 1, $k = $i2 + 1; $j <= $max_index; ++$j, ++$k) { + $temp = $square_value[$k] + 2 * $value[$j] * $value[$i] + $carry; + $carry = (int) ($temp / 0x4000000); + $square_value[$k] = (int) ($temp - 0x4000000 * $carry); + } + + // the following line can yield values larger 2**15. at this point, PHP should switch + // over to floats. + $square_value[$i + $max_index + 1] = $carry; + } + + return $square_value; + } + + /** + * Performs Karatsuba "squaring" on two BigIntegers + * + * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and + * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=151 MPM 5.3.4}. + * + * @param Array $value + * @return Array + * @access private + */ + function _karatsubaSquare($value) + { + $m = count($value) >> 1; + + if ($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF) { + return $this->_baseSquare($value); + } + + $x1 = array_slice($value, $m); + $x0 = array_slice($value, 0, $m); + + $z2 = $this->_karatsubaSquare($x1); + $z0 = $this->_karatsubaSquare($x0); + + $z1 = $this->_add($x1, false, $x0, false); + $z1 = $this->_karatsubaSquare($z1[MATH_BIGINTEGER_VALUE]); + $temp = $this->_add($z2, false, $z0, false); + $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false); + + $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2); + $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]); + + $xx = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]); + $xx = $this->_add($xx[MATH_BIGINTEGER_VALUE], $xx[MATH_BIGINTEGER_SIGN], $z0, false); + + return $xx[MATH_BIGINTEGER_VALUE]; + } + + /** + * Divides two BigIntegers. + * + * Returns an array whose first element contains the quotient and whose second element contains the + * "common residue". If the remainder would be positive, the "common residue" and the remainder are the + * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder + * and the divisor (basically, the "common residue" is the first positive modulo). + * + * Here's an example: + * + * divide($b); + * + * echo $quotient->toString(); // outputs 0 + * echo "\r\n"; + * echo $remainder->toString(); // outputs 10 + * ?> + * + * + * @param Math_BigInteger $y + * @return Array + * @access public + * @internal This function is based off of {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=9 HAC 14.20}. + */ + function divide($y) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $quotient = new Math_BigInteger(); + $remainder = new Math_BigInteger(); + + list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value); + + if (gmp_sign($remainder->value) < 0) { + $remainder->value = gmp_add($remainder->value, gmp_abs($y->value)); + } + + return array($this->_normalize($quotient), $this->_normalize($remainder)); + case MATH_BIGINTEGER_MODE_BCMATH: + $quotient = new Math_BigInteger(); + $remainder = new Math_BigInteger(); + + $quotient->value = bcdiv($this->value, $y->value, 0); + $remainder->value = bcmod($this->value, $y->value); + + if ($remainder->value[0] == '-') { + $remainder->value = bcadd($remainder->value, $y->value[0] == '-' ? substr($y->value, 1) : $y->value, 0); + } + + return array($this->_normalize($quotient), $this->_normalize($remainder)); + } + + if (count($y->value) == 1) { + list($q, $r) = $this->_divide_digit($this->value, $y->value[0]); + $quotient = new Math_BigInteger(); + $remainder = new Math_BigInteger(); + $quotient->value = $q; + $remainder->value = array($r); + $quotient->is_negative = $this->is_negative != $y->is_negative; + return array($this->_normalize($quotient), $this->_normalize($remainder)); + } + + static $zero; + if ( !isset($zero) ) { + $zero = new Math_BigInteger(); + } + + $x = $this->copy(); + $y = $y->copy(); + + $x_sign = $x->is_negative; + $y_sign = $y->is_negative; + + $x->is_negative = $y->is_negative = false; + + $diff = $x->compare($y); + + if ( !$diff ) { + $temp = new Math_BigInteger(); + $temp->value = array(1); + $temp->is_negative = $x_sign != $y_sign; + return array($this->_normalize($temp), $this->_normalize(new Math_BigInteger())); + } + + if ( $diff < 0 ) { + // if $x is negative, "add" $y. + if ( $x_sign ) { + $x = $y->subtract($x); + } + return array($this->_normalize(new Math_BigInteger()), $this->_normalize($x)); + } + + // normalize $x and $y as described in HAC 14.23 / 14.24 + $msb = $y->value[count($y->value) - 1]; + for ($shift = 0; !($msb & 0x2000000); ++$shift) { + $msb <<= 1; + } + $x->_lshift($shift); + $y->_lshift($shift); + $y_value = &$y->value; + + $x_max = count($x->value) - 1; + $y_max = count($y->value) - 1; + + $quotient = new Math_BigInteger(); + $quotient_value = &$quotient->value; + $quotient_value = $this->_array_repeat(0, $x_max - $y_max + 1); + + static $temp, $lhs, $rhs; + if (!isset($temp)) { + $temp = new Math_BigInteger(); + $lhs = new Math_BigInteger(); + $rhs = new Math_BigInteger(); + } + $temp_value = &$temp->value; + $rhs_value = &$rhs->value; + + // $temp = $y << ($x_max - $y_max-1) in base 2**26 + $temp_value = array_merge($this->_array_repeat(0, $x_max - $y_max), $y_value); + + while ( $x->compare($temp) >= 0 ) { + // calculate the "common residue" + ++$quotient_value[$x_max - $y_max]; + $x = $x->subtract($temp); + $x_max = count($x->value) - 1; + } + + for ($i = $x_max; $i >= $y_max + 1; --$i) { + $x_value = &$x->value; + $x_window = array( + isset($x_value[$i]) ? $x_value[$i] : 0, + isset($x_value[$i - 1]) ? $x_value[$i - 1] : 0, + isset($x_value[$i - 2]) ? $x_value[$i - 2] : 0 + ); + $y_window = array( + $y_value[$y_max], + ( $y_max > 0 ) ? $y_value[$y_max - 1] : 0 + ); + + $q_index = $i - $y_max - 1; + if ($x_window[0] == $y_window[0]) { + $quotient_value[$q_index] = 0x3FFFFFF; + } else { + $quotient_value[$q_index] = (int) ( + ($x_window[0] * 0x4000000 + $x_window[1]) + / + $y_window[0] + ); + } + + $temp_value = array($y_window[1], $y_window[0]); + + $lhs->value = array($quotient_value[$q_index]); + $lhs = $lhs->multiply($temp); + + $rhs_value = array($x_window[2], $x_window[1], $x_window[0]); + + while ( $lhs->compare($rhs) > 0 ) { + --$quotient_value[$q_index]; + + $lhs->value = array($quotient_value[$q_index]); + $lhs = $lhs->multiply($temp); + } + + $adjust = $this->_array_repeat(0, $q_index); + $temp_value = array($quotient_value[$q_index]); + $temp = $temp->multiply($y); + $temp_value = &$temp->value; + $temp_value = array_merge($adjust, $temp_value); + + $x = $x->subtract($temp); + + if ($x->compare($zero) < 0) { + $temp_value = array_merge($adjust, $y_value); + $x = $x->add($temp); + + --$quotient_value[$q_index]; + } + + $x_max = count($x_value) - 1; + } + + // unnormalize the remainder + $x->_rshift($shift); + + $quotient->is_negative = $x_sign != $y_sign; + + // calculate the "common residue", if appropriate + if ( $x_sign ) { + $y->_rshift($shift); + $x = $y->subtract($x); + } + + return array($this->_normalize($quotient), $this->_normalize($x)); + } + + /** + * Divides a BigInteger by a regular integer + * + * abc / x = a00 / x + b0 / x + c / x + * + * @param Array $dividend + * @param Array $divisor + * @return Array + * @access private + */ + function _divide_digit($dividend, $divisor) + { + $carry = 0; + $result = array(); + + for ($i = count($dividend) - 1; $i >= 0; --$i) { + $temp = 0x4000000 * $carry + $dividend[$i]; + $result[$i] = (int) ($temp / $divisor); + $carry = (int) ($temp - $divisor * $result[$i]); + } + + return array($result, $carry); + } + + /** + * Performs modular exponentiation. + * + * Here's an example: + * + * modPow($b, $c); + * + * echo $c->toString(); // outputs 10 + * ?> + * + * + * @param Math_BigInteger $e + * @param Math_BigInteger $n + * @return Math_BigInteger + * @access public + * @internal The most naive approach to modular exponentiation has very unreasonable requirements, and + * and although the approach involving repeated squaring does vastly better, it, too, is impractical + * for our purposes. The reason being that division - by far the most complicated and time-consuming + * of the basic operations (eg. +,-,*,/) - occurs multiple times within it. + * + * Modular reductions resolve this issue. Although an individual modular reduction takes more time + * then an individual division, when performed in succession (with the same modulo), they're a lot faster. + * + * The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction, + * although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the + * base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because + * the product of two odd numbers is odd), but what about when RSA isn't used? + * + * In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a + * Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the + * modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however, + * uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and + * the other, a power of two - and recombine them, later. This is the method that this modPow function uses. + * {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates. + */ + function modPow($e, $n) + { + $n = $this->bitmask !== false && $this->bitmask->compare($n) < 0 ? $this->bitmask : $n->abs(); + + if ($e->compare(new Math_BigInteger()) < 0) { + $e = $e->abs(); + + $temp = $this->modInverse($n); + if ($temp === false) { + return false; + } + + return $this->_normalize($temp->modPow($e, $n)); + } + + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp = new Math_BigInteger(); + $temp->value = gmp_powm($this->value, $e->value, $n->value); + + return $this->_normalize($temp); + case MATH_BIGINTEGER_MODE_BCMATH: + $temp = new Math_BigInteger(); + $temp->value = bcpowmod($this->value, $e->value, $n->value, 0); + + return $this->_normalize($temp); + } + + if ( empty($e->value) ) { + $temp = new Math_BigInteger(); + $temp->value = array(1); + return $this->_normalize($temp); + } + + if ( $e->value == array(1) ) { + list(, $temp) = $this->divide($n); + return $this->_normalize($temp); + } + + if ( $e->value == array(2) ) { + $temp = new Math_BigInteger(); + $temp->value = $this->_square($this->value); + list(, $temp) = $temp->divide($n); + return $this->_normalize($temp); + } + + return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_BARRETT)); + + // is the modulo odd? + if ( $n->value[0] & 1 ) { + return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_MONTGOMERY)); + } + // if it's not, it's even + + // find the lowest set bit (eg. the max pow of 2 that divides $n) + for ($i = 0; $i < count($n->value); ++$i) { + if ( $n->value[$i] ) { + $temp = decbin($n->value[$i]); + $j = strlen($temp) - strrpos($temp, '1') - 1; + $j+= 26 * $i; + break; + } + } + // at this point, 2^$j * $n/(2^$j) == $n + + $mod1 = $n->copy(); + $mod1->_rshift($j); + $mod2 = new Math_BigInteger(); + $mod2->value = array(1); + $mod2->_lshift($j); + + $part1 = ( $mod1->value != array(1) ) ? $this->_slidingWindow($e, $mod1, MATH_BIGINTEGER_MONTGOMERY) : new Math_BigInteger(); + $part2 = $this->_slidingWindow($e, $mod2, MATH_BIGINTEGER_POWEROF2); + + $y1 = $mod2->modInverse($mod1); + $y2 = $mod1->modInverse($mod2); + + $result = $part1->multiply($mod2); + $result = $result->multiply($y1); + + $temp = $part2->multiply($mod1); + $temp = $temp->multiply($y2); + + $result = $result->add($temp); + list(, $result) = $result->divide($n); + + return $this->_normalize($result); + } + + /** + * Performs modular exponentiation. + * + * Alias for Math_BigInteger::modPow() + * + * @param Math_BigInteger $e + * @param Math_BigInteger $n + * @return Math_BigInteger + * @access public + */ + function powMod($e, $n) + { + return $this->modPow($e, $n); + } + + /** + * Sliding Window k-ary Modular Exponentiation + * + * Based on {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=27 HAC 14.85} / + * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=210 MPM 7.7}. In a departure from those algorithims, + * however, this function performs a modular reduction after every multiplication and squaring operation. + * As such, this function has the same preconditions that the reductions being used do. + * + * @param Math_BigInteger $e + * @param Math_BigInteger $n + * @param Integer $mode + * @return Math_BigInteger + * @access private + */ + function _slidingWindow($e, $n, $mode) + { + static $window_ranges = array(7, 25, 81, 241, 673, 1793); // from BigInteger.java's oddModPow function + //static $window_ranges = array(0, 7, 36, 140, 450, 1303, 3529); // from MPM 7.3.1 + + $e_value = $e->value; + $e_length = count($e_value) - 1; + $e_bits = decbin($e_value[$e_length]); + for ($i = $e_length - 1; $i >= 0; --$i) { + $e_bits.= str_pad(decbin($e_value[$i]), 26, '0', STR_PAD_LEFT); + } + + $e_length = strlen($e_bits); + + // calculate the appropriate window size. + // $window_size == 3 if $window_ranges is between 25 and 81, for example. + for ($i = 0, $window_size = 1; $e_length > $window_ranges[$i] && $i < count($window_ranges); ++$window_size, ++$i); + + $n_value = $n->value; + + // precompute $this^0 through $this^$window_size + $powers = array(); + $powers[1] = $this->_prepareReduce($this->value, $n_value, $mode); + $powers[2] = $this->_squareReduce($powers[1], $n_value, $mode); + + // we do every other number since substr($e_bits, $i, $j+1) (see below) is supposed to end + // in a 1. ie. it's supposed to be odd. + $temp = 1 << ($window_size - 1); + for ($i = 1; $i < $temp; ++$i) { + $i2 = $i << 1; + $powers[$i2 + 1] = $this->_multiplyReduce($powers[$i2 - 1], $powers[2], $n_value, $mode); + } + + $result = array(1); + $result = $this->_prepareReduce($result, $n_value, $mode); + + for ($i = 0; $i < $e_length; ) { + if ( !$e_bits[$i] ) { + $result = $this->_squareReduce($result, $n_value, $mode); + ++$i; + } else { + for ($j = $window_size - 1; $j > 0; --$j) { + if ( !empty($e_bits[$i + $j]) ) { + break; + } + } + + for ($k = 0; $k <= $j; ++$k) {// eg. the length of substr($e_bits, $i, $j+1) + $result = $this->_squareReduce($result, $n_value, $mode); + } + + $result = $this->_multiplyReduce($result, $powers[bindec(substr($e_bits, $i, $j + 1))], $n_value, $mode); + + $i+=$j + 1; + } + } + + $temp = new Math_BigInteger(); + $temp->value = $this->_reduce($result, $n_value, $mode); + + return $temp; + } + + /** + * Modular reduction + * + * For most $modes this will return the remainder. + * + * @see _slidingWindow() + * @access private + * @param Array $x + * @param Array $n + * @param Integer $mode + * @return Array + */ + function _reduce($x, $n, $mode) + { + switch ($mode) { + case MATH_BIGINTEGER_MONTGOMERY: + return $this->_montgomery($x, $n); + case MATH_BIGINTEGER_BARRETT: + return $this->_barrett($x, $n); + case MATH_BIGINTEGER_POWEROF2: + $lhs = new Math_BigInteger(); + $lhs->value = $x; + $rhs = new Math_BigInteger(); + $rhs->value = $n; + return $x->_mod2($n); + case MATH_BIGINTEGER_CLASSIC: + $lhs = new Math_BigInteger(); + $lhs->value = $x; + $rhs = new Math_BigInteger(); + $rhs->value = $n; + list(, $temp) = $lhs->divide($rhs); + return $temp->value; + case MATH_BIGINTEGER_NONE: + return $x; + default: + // an invalid $mode was provided + } + } + + /** + * Modular reduction preperation + * + * @see _slidingWindow() + * @access private + * @param Array $x + * @param Array $n + * @param Integer $mode + * @return Array + */ + function _prepareReduce($x, $n, $mode) + { + if ($mode == MATH_BIGINTEGER_MONTGOMERY) { + return $this->_prepMontgomery($x, $n); + } + return $this->_reduce($x, $n, $mode); + } + + /** + * Modular multiply + * + * @see _slidingWindow() + * @access private + * @param Array $x + * @param Array $y + * @param Array $n + * @param Integer $mode + * @return Array + */ + function _multiplyReduce($x, $y, $n, $mode) + { + if ($mode == MATH_BIGINTEGER_MONTGOMERY) { + return $this->_montgomeryMultiply($x, $y, $n); + } + $temp = $this->_multiply($x, false, $y, false); + return $this->_reduce($temp[MATH_BIGINTEGER_VALUE], $n, $mode); + } + + /** + * Modular square + * + * @see _slidingWindow() + * @access private + * @param Array $x + * @param Array $n + * @param Integer $mode + * @return Array + */ + function _squareReduce($x, $n, $mode) + { + if ($mode == MATH_BIGINTEGER_MONTGOMERY) { + return $this->_montgomeryMultiply($x, $x, $n); + } + return $this->_reduce($this->_square($x), $n, $mode); + } + + /** + * Modulos for Powers of Two + * + * Calculates $x%$n, where $n = 2**$e, for some $e. Since this is basically the same as doing $x & ($n-1), + * we'll just use this function as a wrapper for doing that. + * + * @see _slidingWindow() + * @access private + * @param Math_BigInteger + * @return Math_BigInteger + */ + function _mod2($n) + { + $temp = new Math_BigInteger(); + $temp->value = array(1); + return $this->bitwise_and($n->subtract($temp)); + } + + /** + * Barrett Modular Reduction + * + * See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=14 HAC 14.3.3} / + * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=165 MPM 6.2.5} for more information. Modified slightly, + * so as not to require negative numbers (initially, this script didn't support negative numbers). + * + * Employs "folding", as described at + * {@link http://www.cosic.esat.kuleuven.be/publications/thesis-149.pdf#page=66 thesis-149.pdf#page=66}. To quote from + * it, "the idea [behind folding] is to find a value x' such that x (mod m) = x' (mod m), with x' being smaller than x." + * + * Unfortunately, the "Barrett Reduction with Folding" algorithm described in thesis-149.pdf is not, as written, all that + * usable on account of (1) its not using reasonable radix points as discussed in + * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=162 MPM 6.2.2} and (2) the fact that, even with reasonable + * radix points, it only works when there are an even number of digits in the denominator. The reason for (2) is that + * (x >> 1) + (x >> 1) != x / 2 + x / 2. If x is even, they're the same, but if x is odd, they're not. See the in-line + * comments for details. + * + * @see _slidingWindow() + * @access private + * @param Array $n + * @param Array $m + * @return Array + */ + function _barrett($n, $m) + { + static $cache = array( + MATH_BIGINTEGER_VARIABLE => array(), + MATH_BIGINTEGER_DATA => array() + ); + + $m_length = count($m); + + // if ($this->_compare($n, $this->_square($m)) >= 0) { + if (count($n) > 2 * $m_length) { + $lhs = new Math_BigInteger(); + $rhs = new Math_BigInteger(); + $lhs->value = $n; + $rhs->value = $m; + list(, $temp) = $lhs->divide($rhs); + return $temp->value; + } + + // if (m.length >> 1) + 2 <= m.length then m is too small and n can't be reduced + if ($m_length < 5) { + return $this->_regularBarrett($n, $m); + } + + // n = 2 * m.length + + if ( ($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { + $key = count($cache[MATH_BIGINTEGER_VARIABLE]); + $cache[MATH_BIGINTEGER_VARIABLE][] = $m; + + $lhs = new Math_BigInteger(); + $lhs_value = &$lhs->value; + $lhs_value = $this->_array_repeat(0, $m_length + ($m_length >> 1)); + $lhs_value[] = 1; + $rhs = new Math_BigInteger(); + $rhs->value = $m; + + list($u, $m1) = $lhs->divide($rhs); + $u = $u->value; + $m1 = $m1->value; + + $cache[MATH_BIGINTEGER_DATA][] = array( + 'u' => $u, // m.length >> 1 (technically (m.length >> 1) + 1) + 'm1'=> $m1 // m.length + ); + } else { + extract($cache[MATH_BIGINTEGER_DATA][$key]); + } + + $cutoff = $m_length + ($m_length >> 1); + $lsd = array_slice($n, 0, $cutoff); // m.length + (m.length >> 1) + $msd = array_slice($n, $cutoff); // m.length >> 1 + $lsd = $this->_trim($lsd); + $temp = $this->_multiply($msd, false, $m1, false); + $n = $this->_add($lsd, false, $temp[MATH_BIGINTEGER_VALUE], false); // m.length + (m.length >> 1) + 1 + + if ($m_length & 1) { + return $this->_regularBarrett($n[MATH_BIGINTEGER_VALUE], $m); + } + + // (m.length + (m.length >> 1) + 1) - (m.length - 1) == (m.length >> 1) + 2 + $temp = array_slice($n[MATH_BIGINTEGER_VALUE], $m_length - 1); + // if even: ((m.length >> 1) + 2) + (m.length >> 1) == m.length + 2 + // if odd: ((m.length >> 1) + 2) + (m.length >> 1) == (m.length - 1) + 2 == m.length + 1 + $temp = $this->_multiply($temp, false, $u, false); + // if even: (m.length + 2) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) + 1 + // if odd: (m.length + 1) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) + $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], ($m_length >> 1) + 1); + // if even: (m.length - (m.length >> 1) + 1) + m.length = 2 * m.length - (m.length >> 1) + 1 + // if odd: (m.length - (m.length >> 1)) + m.length = 2 * m.length - (m.length >> 1) + $temp = $this->_multiply($temp, false, $m, false); + + // at this point, if m had an odd number of digits, we'd be subtracting a 2 * m.length - (m.length >> 1) digit + // number from a m.length + (m.length >> 1) + 1 digit number. ie. there'd be an extra digit and the while loop + // following this comment would loop a lot (hence our calling _regularBarrett() in that situation). + + $result = $this->_subtract($n[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false); + + while ($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false) >= 0) { + $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false); + } + + return $result[MATH_BIGINTEGER_VALUE]; + } + + /** + * (Regular) Barrett Modular Reduction + * + * For numbers with more than four digits Math_BigInteger::_barrett() is faster. The difference between that and this + * is that this function does not fold the denominator into a smaller form. + * + * @see _slidingWindow() + * @access private + * @param Array $x + * @param Array $n + * @return Array + */ + function _regularBarrett($x, $n) + { + static $cache = array( + MATH_BIGINTEGER_VARIABLE => array(), + MATH_BIGINTEGER_DATA => array() + ); + + $n_length = count($n); + + if (count($x) > 2 * $n_length) { + $lhs = new Math_BigInteger(); + $rhs = new Math_BigInteger(); + $lhs->value = $x; + $rhs->value = $n; + list(, $temp) = $lhs->divide($rhs); + return $temp->value; + } + + if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { + $key = count($cache[MATH_BIGINTEGER_VARIABLE]); + $cache[MATH_BIGINTEGER_VARIABLE][] = $n; + $lhs = new Math_BigInteger(); + $lhs_value = &$lhs->value; + $lhs_value = $this->_array_repeat(0, 2 * $n_length); + $lhs_value[] = 1; + $rhs = new Math_BigInteger(); + $rhs->value = $n; + list($temp, ) = $lhs->divide($rhs); // m.length + $cache[MATH_BIGINTEGER_DATA][] = $temp->value; + } + + // 2 * m.length - (m.length - 1) = m.length + 1 + $temp = array_slice($x, $n_length - 1); + // (m.length + 1) + m.length = 2 * m.length + 1 + $temp = $this->_multiply($temp, false, $cache[MATH_BIGINTEGER_DATA][$key], false); + // (2 * m.length + 1) - (m.length - 1) = m.length + 2 + $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], $n_length + 1); + + // m.length + 1 + $result = array_slice($x, 0, $n_length + 1); + // m.length + 1 + $temp = $this->_multiplyLower($temp, false, $n, false, $n_length + 1); + // $temp == array_slice($temp->_multiply($temp, false, $n, false)->value, 0, $n_length + 1) + + if ($this->_compare($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]) < 0) { + $corrector_value = $this->_array_repeat(0, $n_length + 1); + $corrector_value[] = 1; + $result = $this->_add($result, false, $corrector, false); + $result = $result[MATH_BIGINTEGER_VALUE]; + } + + // at this point, we're subtracting a number with m.length + 1 digits from another number with m.length + 1 digits + $result = $this->_subtract($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]); + while ($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false) > 0) { + $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false); + } + + return $result[MATH_BIGINTEGER_VALUE]; + } + + /** + * Performs long multiplication up to $stop digits + * + * If you're going to be doing array_slice($product->value, 0, $stop), some cycles can be saved. + * + * @see _regularBarrett() + * @param Array $x_value + * @param Boolean $x_negative + * @param Array $y_value + * @param Boolean $y_negative + * @return Array + * @access private + */ + function _multiplyLower($x_value, $x_negative, $y_value, $y_negative, $stop) + { + $x_length = count($x_value); + $y_length = count($y_value); + + if ( !$x_length || !$y_length ) { // a 0 is being multiplied + return array( + MATH_BIGINTEGER_VALUE => array(), + MATH_BIGINTEGER_SIGN => false + ); + } + + if ( $x_length < $y_length ) { + $temp = $x_value; + $x_value = $y_value; + $y_value = $temp; + + $x_length = count($x_value); + $y_length = count($y_value); + } + + $product_value = $this->_array_repeat(0, $x_length + $y_length); + + // the following for loop could be removed if the for loop following it + // (the one with nested for loops) initially set $i to 0, but + // doing so would also make the result in one set of unnecessary adds, + // since on the outermost loops first pass, $product->value[$k] is going + // to always be 0 + + $carry = 0; + + for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0, $k = $i + $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0 + $carry = (int) ($temp / 0x4000000); + $product_value[$j] = (int) ($temp - 0x4000000 * $carry); + } + + if ($j < $stop) { + $product_value[$j] = $carry; + } + + // the above for loop is what the previous comment was talking about. the + // following for loop is the "one with nested for loops" + + for ($i = 1; $i < $y_length; ++$i) { + $carry = 0; + + for ($j = 0, $k = $i; $j < $x_length && $k < $stop; ++$j, ++$k) { + $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry; + $carry = (int) ($temp / 0x4000000); + $product_value[$k] = (int) ($temp - 0x4000000 * $carry); + } + + if ($k < $stop) { + $product_value[$k] = $carry; + } + } + + return array( + MATH_BIGINTEGER_VALUE => $this->_trim($product_value), + MATH_BIGINTEGER_SIGN => $x_negative != $y_negative + ); + } + + /** + * Montgomery Modular Reduction + * + * ($x->_prepMontgomery($n))->_montgomery($n) yields $x % $n. + * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=170 MPM 6.3} provides insights on how this can be + * improved upon (basically, by using the comba method). gcd($n, 2) must be equal to one for this function + * to work correctly. + * + * @see _prepMontgomery() + * @see _slidingWindow() + * @access private + * @param Array $x + * @param Array $n + * @return Array + */ + function _montgomery($x, $n) + { + static $cache = array( + MATH_BIGINTEGER_VARIABLE => array(), + MATH_BIGINTEGER_DATA => array() + ); + + if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { + $key = count($cache[MATH_BIGINTEGER_VARIABLE]); + $cache[MATH_BIGINTEGER_VARIABLE][] = $x; + $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($n); + } + + $k = count($n); + + $result = array(MATH_BIGINTEGER_VALUE => $x); + + for ($i = 0; $i < $k; ++$i) { + $temp = $result[MATH_BIGINTEGER_VALUE][$i] * $cache[MATH_BIGINTEGER_DATA][$key]; + $temp = (int) ($temp - 0x4000000 * ((int) ($temp / 0x4000000))); + $temp = $this->_regularMultiply(array($temp), $n); + $temp = array_merge($this->_array_repeat(0, $i), $temp); + $result = $this->_add($result[MATH_BIGINTEGER_VALUE], false, $temp, false); + } + + $result[MATH_BIGINTEGER_VALUE] = array_slice($result[MATH_BIGINTEGER_VALUE], $k); + + if ($this->_compare($result, false, $n, false) >= 0) { + $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], false, $n, false); + } + + return $result[MATH_BIGINTEGER_VALUE]; + } + + /** + * Montgomery Multiply + * + * Interleaves the montgomery reduction and long multiplication algorithms together as described in + * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=13 HAC 14.36} + * + * @see _prepMontgomery() + * @see _montgomery() + * @access private + * @param Array $x + * @param Array $y + * @param Array $m + * @return Array + */ + function _montgomeryMultiply($x, $y, $m) + { + $temp = $this->_multiply($x, false, $y, false); + return $this->_montgomery($temp[MATH_BIGINTEGER_VALUE], $m); + + static $cache = array( + MATH_BIGINTEGER_VARIABLE => array(), + MATH_BIGINTEGER_DATA => array() + ); + + if ( ($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { + $key = count($cache[MATH_BIGINTEGER_VARIABLE]); + $cache[MATH_BIGINTEGER_VARIABLE][] = $m; + $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($m); + } + + $n = max(count($x), count($y), count($m)); + $x = array_pad($x, $n, 0); + $y = array_pad($y, $n, 0); + $m = array_pad($m, $n, 0); + $a = array(MATH_BIGINTEGER_VALUE => $this->_array_repeat(0, $n + 1)); + for ($i = 0; $i < $n; ++$i) { + $temp = $a[MATH_BIGINTEGER_VALUE][0] + $x[$i] * $y[0]; + $temp = (int) ($temp - 0x4000000 * ((int) ($temp / 0x4000000))); + $temp = $temp * $cache[MATH_BIGINTEGER_DATA][$key]; + $temp = (int) ($temp - 0x4000000 * ((int) ($temp / 0x4000000))); + $temp = $this->_add($this->_regularMultiply(array($x[$i]), $y), false, $this->_regularMultiply(array($temp), $m), false); + $a = $this->_add($a[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false); + $a[MATH_BIGINTEGER_VALUE] = array_slice($a[MATH_BIGINTEGER_VALUE], 1); + } + if ($this->_compare($a[MATH_BIGINTEGER_VALUE], false, $m, false) >= 0) { + $a = $this->_subtract($a[MATH_BIGINTEGER_VALUE], false, $m, false); + } + return $a[MATH_BIGINTEGER_VALUE]; + } + + /** + * Prepare a number for use in Montgomery Modular Reductions + * + * @see _montgomery() + * @see _slidingWindow() + * @access private + * @param Array $x + * @param Array $n + * @return Array + */ + function _prepMontgomery($x, $n) + { + $lhs = new Math_BigInteger(); + $lhs->value = array_merge($this->_array_repeat(0, count($n)), $x); + $rhs = new Math_BigInteger(); + $rhs->value = $n; + + list(, $temp) = $lhs->divide($rhs); + return $temp->value; + } + + /** + * Modular Inverse of a number mod 2**26 (eg. 67108864) + * + * Based off of the bnpInvDigit function implemented and justified in the following URL: + * + * {@link http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js} + * + * The following URL provides more info: + * + * {@link http://groups.google.com/group/sci.crypt/msg/7a137205c1be7d85} + * + * As for why we do all the bitmasking... strange things can happen when converting from floats to ints. For + * instance, on some computers, var_dump((int) -4294967297) yields int(-1) and on others, it yields + * int(-2147483648). To avoid problems stemming from this, we use bitmasks to guarantee that ints aren't + * auto-converted to floats. The outermost bitmask is present because without it, there's no guarantee that + * the "residue" returned would be the so-called "common residue". We use fmod, in the last step, because the + * maximum possible $x is 26 bits and the maximum $result is 16 bits. Thus, we have to be able to handle up to + * 40 bits, which only 64-bit floating points will support. + * + * Thanks to Pedro Gimeno Fortea for input! + * + * @see _montgomery() + * @access private + * @param Array $x + * @return Integer + */ + function _modInverse67108864($x) // 2**26 == 67108864 + { + $x = -$x[0]; + $result = $x & 0x3; // x**-1 mod 2**2 + $result = ($result * (2 - $x * $result)) & 0xF; // x**-1 mod 2**4 + $result = ($result * (2 - ($x & 0xFF) * $result)) & 0xFF; // x**-1 mod 2**8 + $result = ($result * ((2 - ($x & 0xFFFF) * $result) & 0xFFFF)) & 0xFFFF; // x**-1 mod 2**16 + $result = fmod($result * (2 - fmod($x * $result, 0x4000000)), 0x4000000); // x**-1 mod 2**26 + return $result & 0x3FFFFFF; + } + + /** + * Calculates modular inverses. + * + * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses. + * + * Here's an example: + * + * modInverse($b); + * echo $c->toString(); // outputs 4 + * + * echo "\r\n"; + * + * $d = $a->multiply($c); + * list(, $d) = $d->divide($b); + * echo $d; // outputs 1 (as per the definition of modular inverse) + * ?> + * + * + * @param Math_BigInteger $n + * @return mixed false, if no modular inverse exists, Math_BigInteger, otherwise. + * @access public + * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=21 HAC 14.64} for more information. + */ + function modInverse($n) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp = new Math_BigInteger(); + $temp->value = gmp_invert($this->value, $n->value); + + return ( $temp->value === false ) ? false : $this->_normalize($temp); + } + + static $zero, $one; + if (!isset($zero)) { + $zero = new Math_BigInteger(); + $one = new Math_BigInteger(1); + } + + // $x mod $n == $x mod -$n. + $n = $n->abs(); + + if ($this->compare($zero) < 0) { + $temp = $this->abs(); + $temp = $temp->modInverse($n); + return $negated === false ? false : $this->_normalize($n->subtract($temp)); + } + + extract($this->extendedGCD($n)); + + if (!$gcd->equals($one)) { + return false; + } + + $x = $x->compare($zero) < 0 ? $x->add($n) : $x; + + return $this->compare($zero) < 0 ? $this->_normalize($n->subtract($x)) : $this->_normalize($x); + } + + /** + * Calculates the greatest common divisor and Bézout's identity. + * + * Say you have 693 and 609. The GCD is 21. Bézout's identity states that there exist integers x and y such that + * 693*x + 609*y == 21. In point of fact, there are actually an infinite number of x and y combinations and which + * combination is returned is dependant upon which mode is in use. See + * {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bézout's identity - Wikipedia} for more information. + * + * Here's an example: + * + * extendedGCD($b)); + * + * echo $gcd->toString() . "\r\n"; // outputs 21 + * echo $a->toString() * $x->toString() + $b->toString() * $y->toString(); // outputs 21 + * ?> + * + * + * @param Math_BigInteger $n + * @return Math_BigInteger + * @access public + * @internal Calculates the GCD using the binary xGCD algorithim described in + * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=19 HAC 14.61}. As the text above 14.61 notes, + * the more traditional algorithim requires "relatively costly multiple-precision divisions". + */ + function extendedGCD($n) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + extract(gmp_gcdext($this->value, $n->value)); + + return array( + 'gcd' => $this->_normalize(new Math_BigInteger($g)), + 'x' => $this->_normalize(new Math_BigInteger($s)), + 'y' => $this->_normalize(new Math_BigInteger($t)) + ); + case MATH_BIGINTEGER_MODE_BCMATH: + // it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works + // best when the base is a power of 2 and (2) i don't think it'd make much difference, anyway. as is, + // the basic extended euclidean algorithim is what we're using. + + $u = $this->value; + $v = $n->value; + + $a = '1'; + $b = '0'; + $c = '0'; + $d = '1'; + + while (bccomp($v, '0', 0) != 0) { + $q = bcdiv($u, $v, 0); + + $temp = $u; + $u = $v; + $v = bcsub($temp, bcmul($v, $q, 0), 0); + + $temp = $a; + $a = $c; + $c = bcsub($temp, bcmul($a, $q, 0), 0); + + $temp = $b; + $b = $d; + $d = bcsub($temp, bcmul($b, $q, 0), 0); + } + + return array( + 'gcd' => $this->_normalize(new Math_BigInteger($u)), + 'x' => $this->_normalize(new Math_BigInteger($a)), + 'y' => $this->_normalize(new Math_BigInteger($b)) + ); + } + + $y = $n->copy(); + $x = $this->copy(); + $g = new Math_BigInteger(); + $g->value = array(1); + + while ( !(($x->value[0] & 1)|| ($y->value[0] & 1)) ) { + $x->_rshift(1); + $y->_rshift(1); + $g->_lshift(1); + } + + $u = $x->copy(); + $v = $y->copy(); + + $a = new Math_BigInteger(); + $b = new Math_BigInteger(); + $c = new Math_BigInteger(); + $d = new Math_BigInteger(); + + $a->value = $d->value = $g->value = array(1); + $b->value = $c->value = array(); + + while ( !empty($u->value) ) { + while ( !($u->value[0] & 1) ) { + $u->_rshift(1); + if ( (!empty($a->value) && ($a->value[0] & 1)) || (!empty($b->value) && ($b->value[0] & 1)) ) { + $a = $a->add($y); + $b = $b->subtract($x); + } + $a->_rshift(1); + $b->_rshift(1); + } + + while ( !($v->value[0] & 1) ) { + $v->_rshift(1); + if ( (!empty($d->value) && ($d->value[0] & 1)) || (!empty($c->value) && ($c->value[0] & 1)) ) { + $c = $c->add($y); + $d = $d->subtract($x); + } + $c->_rshift(1); + $d->_rshift(1); + } + + if ($u->compare($v) >= 0) { + $u = $u->subtract($v); + $a = $a->subtract($c); + $b = $b->subtract($d); + } else { + $v = $v->subtract($u); + $c = $c->subtract($a); + $d = $d->subtract($b); + } + } + + return array( + 'gcd' => $this->_normalize($g->multiply($v)), + 'x' => $this->_normalize($c), + 'y' => $this->_normalize($d) + ); + } + + /** + * Calculates the greatest common divisor + * + * Say you have 693 and 609. The GCD is 21. + * + * Here's an example: + * + * extendedGCD($b); + * + * echo $gcd->toString() . "\r\n"; // outputs 21 + * ?> + * + * + * @param Math_BigInteger $n + * @return Math_BigInteger + * @access public + */ + function gcd($n) + { + extract($this->extendedGCD($n)); + return $gcd; + } + + /** + * Absolute value. + * + * @return Math_BigInteger + * @access public + */ + function abs() + { + $temp = new Math_BigInteger(); + + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp->value = gmp_abs($this->value); + break; + case MATH_BIGINTEGER_MODE_BCMATH: + $temp->value = (bccomp($this->value, '0', 0) < 0) ? substr($this->value, 1) : $this->value; + break; + default: + $temp->value = $this->value; + } + + return $temp; + } + + /** + * Compares two numbers. + * + * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is + * demonstrated thusly: + * + * $x > $y: $x->compare($y) > 0 + * $x < $y: $x->compare($y) < 0 + * $x == $y: $x->compare($y) == 0 + * + * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y). + * + * @param Math_BigInteger $x + * @return Integer < 0 if $this is less than $x; > 0 if $this is greater than $x, and 0 if they are equal. + * @access public + * @see equals() + * @internal Could return $this->subtract($x), but that's not as fast as what we do do. + */ + function compare($y) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + return gmp_cmp($this->value, $y->value); + case MATH_BIGINTEGER_MODE_BCMATH: + return bccomp($this->value, $y->value, 0); + } + + return $this->_compare($this->value, $this->is_negative, $y->value, $y->is_negative); + } + + /** + * Compares two numbers. + * + * @param Array $x_value + * @param Boolean $x_negative + * @param Array $y_value + * @param Boolean $y_negative + * @return Integer + * @see compare() + * @access private + */ + function _compare($x_value, $x_negative, $y_value, $y_negative) + { + if ( $x_negative != $y_negative ) { + return ( !$x_negative && $y_negative ) ? 1 : -1; + } + + $result = $x_negative ? -1 : 1; + + if ( count($x_value) != count($y_value) ) { + return ( count($x_value) > count($y_value) ) ? $result : -$result; + } + $size = max(count($x_value), count($y_value)); + + $x_value = array_pad($x_value, $size, 0); + $y_value = array_pad($y_value, $size, 0); + + for ($i = count($x_value) - 1; $i >= 0; --$i) { + if ($x_value[$i] != $y_value[$i]) { + return ( $x_value[$i] > $y_value[$i] ) ? $result : -$result; + } + } + + return 0; + } + + /** + * Tests the equality of two numbers. + * + * If you need to see if one number is greater than or less than another number, use Math_BigInteger::compare() + * + * @param Math_BigInteger $x + * @return Boolean + * @access public + * @see compare() + */ + function equals($x) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + return gmp_cmp($this->value, $x->value) == 0; + default: + return $this->value === $x->value && $this->is_negative == $x->is_negative; + } + } + + /** + * Set Precision + * + * Some bitwise operations give different results depending on the precision being used. Examples include left + * shift, not, and rotates. + * + * @param Math_BigInteger $x + * @access public + * @return Math_BigInteger + */ + function setPrecision($bits) + { + $this->precision = $bits; + if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ) { + $this->bitmask = new Math_BigInteger(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256); + } else { + $this->bitmask = new Math_BigInteger(bcpow('2', $bits, 0)); + } + + $temp = $this->_normalize($this); + $this->value = $temp->value; + } + + /** + * Logical And + * + * @param Math_BigInteger $x + * @access public + * @internal Implemented per a request by Lluis Pamies i Juarez + * @return Math_BigInteger + */ + function bitwise_and($x) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp = new Math_BigInteger(); + $temp->value = gmp_and($this->value, $x->value); + + return $this->_normalize($temp); + case MATH_BIGINTEGER_MODE_BCMATH: + $left = $this->toBytes(); + $right = $x->toBytes(); + + $length = max(strlen($left), strlen($right)); + + $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); + $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); + + return $this->_normalize(new Math_BigInteger($left & $right, 256)); + } + + $result = $this->copy(); + + $length = min(count($x->value), count($this->value)); + + $result->value = array_slice($result->value, 0, $length); + + for ($i = 0; $i < $length; ++$i) { + $result->value[$i] = $result->value[$i] & $x->value[$i]; + } + + return $this->_normalize($result); + } + + /** + * Logical Or + * + * @param Math_BigInteger $x + * @access public + * @internal Implemented per a request by Lluis Pamies i Juarez + * @return Math_BigInteger + */ + function bitwise_or($x) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp = new Math_BigInteger(); + $temp->value = gmp_or($this->value, $x->value); + + return $this->_normalize($temp); + case MATH_BIGINTEGER_MODE_BCMATH: + $left = $this->toBytes(); + $right = $x->toBytes(); + + $length = max(strlen($left), strlen($right)); + + $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); + $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); + + return $this->_normalize(new Math_BigInteger($left | $right, 256)); + } + + $length = max(count($this->value), count($x->value)); + $result = $this->copy(); + $result->value = array_pad($result->value, 0, $length); + $x->value = array_pad($x->value, 0, $length); + + for ($i = 0; $i < $length; ++$i) { + $result->value[$i] = $this->value[$i] | $x->value[$i]; + } + + return $this->_normalize($result); + } + + /** + * Logical Exclusive-Or + * + * @param Math_BigInteger $x + * @access public + * @internal Implemented per a request by Lluis Pamies i Juarez + * @return Math_BigInteger + */ + function bitwise_xor($x) + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + $temp = new Math_BigInteger(); + $temp->value = gmp_xor($this->value, $x->value); + + return $this->_normalize($temp); + case MATH_BIGINTEGER_MODE_BCMATH: + $left = $this->toBytes(); + $right = $x->toBytes(); + + $length = max(strlen($left), strlen($right)); + + $left = str_pad($left, $length, chr(0), STR_PAD_LEFT); + $right = str_pad($right, $length, chr(0), STR_PAD_LEFT); + + return $this->_normalize(new Math_BigInteger($left ^ $right, 256)); + } + + $length = max(count($this->value), count($x->value)); + $result = $this->copy(); + $result->value = array_pad($result->value, 0, $length); + $x->value = array_pad($x->value, 0, $length); + + for ($i = 0; $i < $length; ++$i) { + $result->value[$i] = $this->value[$i] ^ $x->value[$i]; + } + + return $this->_normalize($result); + } + + /** + * Logical Not + * + * @access public + * @internal Implemented per a request by Lluis Pamies i Juarez + * @return Math_BigInteger + */ + function bitwise_not() + { + // calculuate "not" without regard to $this->precision + // (will always result in a smaller number. ie. ~1 isn't 1111 1110 - it's 0) + $temp = $this->toBytes(); + $pre_msb = decbin(ord($temp[0])); + $temp = ~$temp; + $msb = decbin(ord($temp[0])); + if (strlen($msb) == 8) { + $msb = substr($msb, strpos($msb, '0')); + } + $temp[0] = chr(bindec($msb)); + + // see if we need to add extra leading 1's + $current_bits = strlen($pre_msb) + 8 * strlen($temp) - 8; + $new_bits = $this->precision - $current_bits; + if ($new_bits <= 0) { + return $this->_normalize(new Math_BigInteger($temp, 256)); + } + + // generate as many leading 1's as we need to. + $leading_ones = chr((1 << ($new_bits & 0x7)) - 1) . str_repeat(chr(0xFF), $new_bits >> 3); + $this->_base256_lshift($leading_ones, $current_bits); + + $temp = str_pad($temp, ceil($this->bits / 8), chr(0), STR_PAD_LEFT); + + return $this->_normalize(new Math_BigInteger($leading_ones | $temp, 256)); + } + + /** + * Logical Right Shift + * + * Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift. + * + * @param Integer $shift + * @return Math_BigInteger + * @access public + * @internal The only version that yields any speed increases is the internal version. + */ + function bitwise_rightShift($shift) + { + $temp = new Math_BigInteger(); + + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + static $two; + + if (!isset($two)) { + $two = gmp_init('2'); + } + + $temp->value = gmp_div_q($this->value, gmp_pow($two, $shift)); + + break; + case MATH_BIGINTEGER_MODE_BCMATH: + $temp->value = bcdiv($this->value, bcpow('2', $shift, 0), 0); + + break; + default: // could just replace _lshift with this, but then all _lshift() calls would need to be rewritten + // and I don't want to do that... + $temp->value = $this->value; + $temp->_rshift($shift); + } + + return $this->_normalize($temp); + } + + /** + * Logical Left Shift + * + * Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift. + * + * @param Integer $shift + * @return Math_BigInteger + * @access public + * @internal The only version that yields any speed increases is the internal version. + */ + function bitwise_leftShift($shift) + { + $temp = new Math_BigInteger(); + + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + static $two; + + if (!isset($two)) { + $two = gmp_init('2'); + } + + $temp->value = gmp_mul($this->value, gmp_pow($two, $shift)); + + break; + case MATH_BIGINTEGER_MODE_BCMATH: + $temp->value = bcmul($this->value, bcpow('2', $shift, 0), 0); + + break; + default: // could just replace _rshift with this, but then all _lshift() calls would need to be rewritten + // and I don't want to do that... + $temp->value = $this->value; + $temp->_lshift($shift); + } + + return $this->_normalize($temp); + } + + /** + * Logical Left Rotate + * + * Instead of the top x bits being dropped they're appended to the shifted bit string. + * + * @param Integer $shift + * @return Math_BigInteger + * @access public + */ + function bitwise_leftRotate($shift) + { + $bits = $this->toBytes(); + + if ($this->precision > 0) { + $precision = $this->precision; + if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) { + $mask = $this->bitmask->subtract(new Math_BigInteger(1)); + $mask = $mask->toBytes(); + } else { + $mask = $this->bitmask->toBytes(); + } + } else { + $temp = ord($bits[0]); + for ($i = 0; $temp >> $i; ++$i); + $precision = 8 * strlen($bits) - 8 + $i; + $mask = chr((1 << ($precision & 0x7)) - 1) . str_repeat(chr(0xFF), $precision >> 3); + } + + if ($shift < 0) { + $shift+= $precision; + } + $shift%= $precision; + + if (!$shift) { + return $this->copy(); + } + + $left = $this->bitwise_leftShift($shift); + $left = $left->bitwise_and(new Math_BigInteger($mask, 256)); + $right = $this->bitwise_rightShift($precision - $shift); + $result = MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ? $left->bitwise_or($right) : $left->add($right); + return $this->_normalize($result); + } + + /** + * Logical Right Rotate + * + * Instead of the bottom x bits being dropped they're prepended to the shifted bit string. + * + * @param Integer $shift + * @return Math_BigInteger + * @access public + */ + function bitwise_rightRotate($shift) + { + return $this->bitwise_leftRotate(-$shift); + } + + /** + * Set random number generator function + * + * $generator should be the name of a random generating function whose first parameter is the minimum + * value and whose second parameter is the maximum value. If this function needs to be seeded, it should + * be seeded prior to calling Math_BigInteger::random() or Math_BigInteger::randomPrime() + * + * If the random generating function is not explicitly set, it'll be assumed to be mt_rand(). + * + * @see random() + * @see randomPrime() + * @param optional String $generator + * @access public + */ + function setRandomGenerator($generator) + { + $this->generator = $generator; + } + + /** + * Generate a random number + * + * @param optional Integer $min + * @param optional Integer $max + * @return Math_BigInteger + * @access public + */ + function random($min = false, $max = false) + { + if ($min === false) { + $min = new Math_BigInteger(0); + } + + if ($max === false) { + $max = new Math_BigInteger(0x7FFFFFFF); + } + + $compare = $max->compare($min); + + if (!$compare) { + return $this->_normalize($min); + } else if ($compare < 0) { + // if $min is bigger then $max, swap $min and $max + $temp = $max; + $max = $min; + $min = $temp; + } + + $generator = $this->generator; + + $max = $max->subtract($min); + $max = ltrim($max->toBytes(), chr(0)); + $size = strlen($max) - 1; + $random = ''; + + $bytes = $size & 1; + for ($i = 0; $i < $bytes; ++$i) { + $random.= chr($generator(0, 255)); + } + + $blocks = $size >> 1; + for ($i = 0; $i < $blocks; ++$i) { + // mt_rand(-2147483648, 0x7FFFFFFF) always produces -2147483648 on some systems + $random.= pack('n', $generator(0, 0xFFFF)); + } + + $temp = new Math_BigInteger($random, 256); + if ($temp->compare(new Math_BigInteger(substr($max, 1), 256)) > 0) { + $random = chr($generator(0, ord($max[0]) - 1)) . $random; + } else { + $random = chr($generator(0, ord($max[0]) )) . $random; + } + + $random = new Math_BigInteger($random, 256); + + return $this->_normalize($random->add($min)); + } + + /** + * Generate a random prime number. + * + * If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed, + * give up and return false. + * + * @param optional Integer $min + * @param optional Integer $max + * @param optional Integer $timeout + * @return Math_BigInteger + * @access public + * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}. + */ + function randomPrime($min = false, $max = false, $timeout = false) + { + $compare = $max->compare($min); + + if (!$compare) { + return $min; + } else if ($compare < 0) { + // if $min is bigger then $max, swap $min and $max + $temp = $max; + $max = $min; + $min = $temp; + } + + // gmp_nextprime() requires PHP 5 >= 5.2.0 per . + if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && function_exists('gmp_nextprime') ) { + // we don't rely on Math_BigInteger::random()'s min / max when gmp_nextprime() is being used since this function + // does its own checks on $max / $min when gmp_nextprime() is used. When gmp_nextprime() is not used, however, + // the same $max / $min checks are not performed. + if ($min === false) { + $min = new Math_BigInteger(0); + } + + if ($max === false) { + $max = new Math_BigInteger(0x7FFFFFFF); + } + + $x = $this->random($min, $max); + + $x->value = gmp_nextprime($x->value); + + if ($x->compare($max) <= 0) { + return $x; + } + + $x->value = gmp_nextprime($min->value); + + if ($x->compare($max) <= 0) { + return $x; + } + + return false; + } + + static $one, $two; + if (!isset($one)) { + $one = new Math_BigInteger(1); + $two = new Math_BigInteger(2); + } + + $start = time(); + + $x = $this->random($min, $max); + if ($x->equals($two)) { + return $x; + } + + $x->_make_odd(); + if ($x->compare($max) > 0) { + // if $x > $max then $max is even and if $min == $max then no prime number exists between the specified range + if ($min->equals($max)) { + return false; + } + $x = $min->copy(); + $x->_make_odd(); + } + + $initial_x = $x->copy(); + + while (true) { + if ($timeout !== false && time() - $start > $timeout) { + return false; + } + + if ($x->isPrime()) { + return $x; + } + + $x = $x->add($two); + + if ($x->compare($max) > 0) { + $x = $min->copy(); + if ($x->equals($two)) { + return $x; + } + $x->_make_odd(); + } + + if ($x->equals($initial_x)) { + return false; + } + } + } + + /** + * Make the current number odd + * + * If the current number is odd it'll be unchanged. If it's even, one will be added to it. + * + * @see randomPrime() + * @access private + */ + function _make_odd() + { + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + gmp_setbit($this->value, 0); + break; + case MATH_BIGINTEGER_MODE_BCMATH: + if ($this->value[strlen($this->value) - 1] % 2 == 0) { + $this->value = bcadd($this->value, '1'); + } + break; + default: + $this->value[0] |= 1; + } + } + + /** + * Checks a numer to see if it's prime + * + * Assuming the $t parameter is not set, this function has an error rate of 2**-80. The main motivation for the + * $t parameter is distributability. Math_BigInteger::randomPrime() can be distributed accross multiple pageloads + * on a website instead of just one. + * + * @param optional Integer $t + * @return Boolean + * @access public + * @internal Uses the + * {@link http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Miller-Rabin primality test}. See + * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=8 HAC 4.24}. + */ + function isPrime($t = false) + { + $length = strlen($this->toBytes()); + + if (!$t) { + // see HAC 4.49 "Note (controlling the error probability)" + if ($length >= 163) { $t = 2; } // floor(1300 / 8) + else if ($length >= 106) { $t = 3; } // floor( 850 / 8) + else if ($length >= 81 ) { $t = 4; } // floor( 650 / 8) + else if ($length >= 68 ) { $t = 5; } // floor( 550 / 8) + else if ($length >= 56 ) { $t = 6; } // floor( 450 / 8) + else if ($length >= 50 ) { $t = 7; } // floor( 400 / 8) + else if ($length >= 43 ) { $t = 8; } // floor( 350 / 8) + else if ($length >= 37 ) { $t = 9; } // floor( 300 / 8) + else if ($length >= 31 ) { $t = 12; } // floor( 250 / 8) + else if ($length >= 25 ) { $t = 15; } // floor( 200 / 8) + else if ($length >= 18 ) { $t = 18; } // floor( 150 / 8) + else { $t = 27; } + } + + // ie. gmp_testbit($this, 0) + // ie. isEven() or !isOdd() + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + return gmp_prob_prime($this->value, $t) != 0; + case MATH_BIGINTEGER_MODE_BCMATH: + if ($this->value === '2') { + return true; + } + if ($this->value[strlen($this->value) - 1] % 2 == 0) { + return false; + } + break; + default: + if ($this->value == array(2)) { + return true; + } + if (~$this->value[0] & 1) { + return false; + } + } + + static $primes, $zero, $one, $two; + + if (!isset($primes)) { + $primes = array( + 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, + 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, + 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, + 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, + 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, + 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, + 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, + 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, + 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, + 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, + 953, 967, 971, 977, 983, 991, 997 + ); + + if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) { + for ($i = 0; $i < count($primes); ++$i) { + $primes[$i] = new Math_BigInteger($primes[$i]); + } + } + + $zero = new Math_BigInteger(); + $one = new Math_BigInteger(1); + $two = new Math_BigInteger(2); + } + + if ($this->equals($one)) { + return false; + } + + // see HAC 4.4.1 "Random search for probable primes" + if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) { + foreach ($primes as $prime) { + list(, $r) = $this->divide($prime); + if ($r->equals($zero)) { + return $this->equals($prime); + } + } + } else { + $value = $this->value; + foreach ($primes as $prime) { + list(, $r) = $this->_divide_digit($value, $prime); + if (!$r) { + return count($value) == 1 && $value[0] == $prime; + } + } + } + + $n = $this->copy(); + $n_1 = $n->subtract($one); + $n_2 = $n->subtract($two); + + $r = $n_1->copy(); + $r_value = $r->value; + // ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s)); + if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) { + $s = 0; + // if $n was 1, $r would be 0 and this would be an infinite loop, hence our $this->equals($one) check earlier + while ($r->value[strlen($r->value) - 1] % 2 == 0) { + $r->value = bcdiv($r->value, '2', 0); + ++$s; + } + } else { + for ($i = 0, $r_length = count($r_value); $i < $r_length; ++$i) { + $temp = ~$r_value[$i] & 0xFFFFFF; + for ($j = 1; ($temp >> $j) & 1; ++$j); + if ($j != 25) { + break; + } + } + $s = 26 * $i + $j - 1; + $r->_rshift($s); + } + + for ($i = 0; $i < $t; ++$i) { + $a = $this->random($two, $n_2); + $y = $a->modPow($r, $n); + + if (!$y->equals($one) && !$y->equals($n_1)) { + for ($j = 1; $j < $s && !$y->equals($n_1); ++$j) { + $y = $y->modPow($two, $n); + if ($y->equals($one)) { + return false; + } + } + + if (!$y->equals($n_1)) { + return false; + } + } + } + return true; + } + + /** + * Logical Left Shift + * + * Shifts BigInteger's by $shift bits. + * + * @param Integer $shift + * @access private + */ + function _lshift($shift) + { + if ( $shift == 0 ) { + return; + } + + $num_digits = (int) ($shift / 26); + $shift %= 26; + $shift = 1 << $shift; + + $carry = 0; + + for ($i = 0; $i < count($this->value); ++$i) { + $temp = $this->value[$i] * $shift + $carry; + $carry = (int) ($temp / 0x4000000); + $this->value[$i] = (int) ($temp - $carry * 0x4000000); + } + + if ( $carry ) { + $this->value[] = $carry; + } + + while ($num_digits--) { + array_unshift($this->value, 0); + } + } + + /** + * Logical Right Shift + * + * Shifts BigInteger's by $shift bits. + * + * @param Integer $shift + * @access private + */ + function _rshift($shift) + { + if ($shift == 0) { + return; + } + + $num_digits = (int) ($shift / 26); + $shift %= 26; + $carry_shift = 26 - $shift; + $carry_mask = (1 << $shift) - 1; + + if ( $num_digits ) { + $this->value = array_slice($this->value, $num_digits); + } + + $carry = 0; + + for ($i = count($this->value) - 1; $i >= 0; --$i) { + $temp = $this->value[$i] >> $shift | $carry; + $carry = ($this->value[$i] & $carry_mask) << $carry_shift; + $this->value[$i] = $temp; + } + + $this->value = $this->_trim($this->value); + } + + /** + * Normalize + * + * Removes leading zeros and truncates (if necessary) to maintain the appropriate precision + * + * @param Math_BigInteger + * @return Math_BigInteger + * @see _trim() + * @access private + */ + function _normalize($result) + { + $result->precision = $this->precision; + $result->bitmask = $this->bitmask; + + switch ( MATH_BIGINTEGER_MODE ) { + case MATH_BIGINTEGER_MODE_GMP: + if (!empty($result->bitmask->value)) { + $result->value = gmp_and($result->value, $result->bitmask->value); + } + + return $result; + case MATH_BIGINTEGER_MODE_BCMATH: + if (!empty($result->bitmask->value)) { + $result->value = bcmod($result->value, $result->bitmask->value); + } + + return $result; + } + + $value = &$result->value; + + if ( !count($value) ) { + return $result; + } + + $value = $this->_trim($value); + + if (!empty($result->bitmask->value)) { + $length = min(count($value), count($this->bitmask->value)); + $value = array_slice($value, 0, $length); + + for ($i = 0; $i < $length; ++$i) { + $value[$i] = $value[$i] & $this->bitmask->value[$i]; + } + } + + return $result; + } + + /** + * Trim + * + * Removes leading zeros + * + * @return Math_BigInteger + * @access private + */ + function _trim($value) + { + for ($i = count($value) - 1; $i >= 0; --$i) { + if ( $value[$i] ) { + break; + } + unset($value[$i]); + } + + return $value; + } + + /** + * Array Repeat + * + * @param $input Array + * @param $multiplier mixed + * @return Array + * @access private + */ + function _array_repeat($input, $multiplier) + { + return ($multiplier) ? array_fill(0, $multiplier, $input) : array(); + } + + /** + * Logical Left Shift + * + * Shifts binary strings $shift bits, essentially multiplying by 2**$shift. + * + * @param $x String + * @param $shift Integer + * @return String + * @access private + */ + function _base256_lshift(&$x, $shift) + { + if ($shift == 0) { + return; + } + + $num_bytes = $shift >> 3; // eg. floor($shift/8) + $shift &= 7; // eg. $shift % 8 + + $carry = 0; + for ($i = strlen($x) - 1; $i >= 0; --$i) { + $temp = ord($x[$i]) << $shift | $carry; + $x[$i] = chr($temp); + $carry = $temp >> 8; + } + $carry = ($carry != 0) ? chr($carry) : ''; + $x = $carry . $x . str_repeat(chr(0), $num_bytes); + } + + /** + * Logical Right Shift + * + * Shifts binary strings $shift bits, essentially dividing by 2**$shift and returning the remainder. + * + * @param $x String + * @param $shift Integer + * @return String + * @access private + */ + function _base256_rshift(&$x, $shift) + { + if ($shift == 0) { + $x = ltrim($x, chr(0)); + return ''; + } + + $num_bytes = $shift >> 3; // eg. floor($shift/8) + $shift &= 7; // eg. $shift % 8 + + $remainder = ''; + if ($num_bytes) { + $start = $num_bytes > strlen($x) ? -strlen($x) : -$num_bytes; + $remainder = substr($x, $start); + $x = substr($x, 0, -$num_bytes); + } + + $carry = 0; + $carry_shift = 8 - $shift; + for ($i = 0; $i < strlen($x); ++$i) { + $temp = (ord($x[$i]) >> $shift) | $carry; + $carry = (ord($x[$i]) << $carry_shift) & 0xFF; + $x[$i] = chr($temp); + } + $x = ltrim($x, chr(0)); + + $remainder = chr($carry >> $carry_shift) . $remainder; + + return ltrim($remainder, chr(0)); + } + + // one quirk about how the following functions are implemented is that PHP defines N to be an unsigned long + // at 32-bits, while java's longs are 64-bits. + + /** + * Converts 32-bit integers to bytes. + * + * @param Integer $x + * @return String + * @access private + */ + function _int2bytes($x) + { + return ltrim(pack('N', $x), chr(0)); + } + + /** + * Converts bytes to 32-bit integers + * + * @param String $x + * @return Integer + * @access private + */ + function _bytes2int($x) + { + $temp = unpack('Nint', str_pad($x, 4, chr(0), STR_PAD_LEFT)); + return $temp['int']; + } } \ No newline at end of file diff --git a/Mobile_Detect.php b/Mobile_Detect.php index 4b8c9fecb..7d2df38d1 100644 --- a/Mobile_Detect.php +++ b/Mobile_Detect.php @@ -1,221 +1,221 @@ -isMobile() or $detect->isTablet() - * - * For more specific usage see the documentation navigate to: - * http://code.google.com/p/php-mobile-detect/wiki/Mobile_Detect - * - * @license http://www.opensource.org/licenses/mit-license.php The MIT License - */ - -class Mobile_Detect { - - protected $detectionRules; - protected $userAgent = null; - protected $accept = null; - // Assume the visitor has a desktop environment. - protected $isMobile = false; - protected $isTablet = false; - protected $phoneDeviceName = null; - protected $tabletDevicename = null; - protected $operatingSystemName = null; - protected $userAgentName = null; - // List of mobile devices (phones) - protected $phoneDevices = array( - 'iPhone' => '(iPhone.*Mobile|iPod|iTunes)', - 'BlackBerry' => 'BlackBerry|rim[0-9]+', - 'HTC' => 'HTC|HTC.*(6800|8100|8900|A7272|S510e|C110e|Legend|Desire|T8282)|APX515CKT|Qtek9090|APA9292KT', - 'Nexus' => 'Nexus One|Nexus S|Galaxy.*Nexus|Android.*Nexus', - 'Dell' => 'Dell.*Streak|Dell.*Aero|Dell.*Venue|DELL.*Venue Pro|Dell Flash|Dell Smoke|Dell Mini 3iX|XCD28|XCD35', - 'Motorola' => '\bDroid\b.*Build|DROIDX|HRI39|MOT\-|A1260|A1680|A555|A853|A855|A953|A955|A956|Motorola.*ELECTRIFY|Motorola.*i1|i867|i940|MB200|MB300|MB501|MB502|MB508|MB511|MB520|MB525|MB526|MB611|MB612|MB632|MB810|MB855|MB860|MB861|MB865|MB870|ME501|ME502|ME511|ME525|ME600|ME632|ME722|ME811|ME860|ME863|ME865|MT620|MT710|MT716|MT720|MT810|MT870|MT917|Motorola.*TITANIUM|WX435|WX445|XT300|XT301|XT311|XT316|XT317|XT319|XT320|XT390|XT502|XT530|XT531|XT532|XT535|XT603|XT610|XT611|XT615|XT681|XT701|XT702|XT711|XT720|XT800|XT806|XT860|XT862|XT875|XT882|XT883|XT894|XT909|XT910|XT912|XT928', - 'Samsung' => 'Samsung|BGT-S5230|GT-B2100|GT-B2700|GT-B2710|GT-B3210|GT-B3310|GT-B3410|GT-B3730|GT-B3740|GT-B5510|GT-B5512|GT-B5722|GT-B6520|GT-B7300|GT-B7320|GT-B7330|GT-B7350|GT-B7510|GT-B7722|GT-B7800|GT-C3010|GT-C3011|GT-C3060|GT-C3200|GT-C3212|GT-C3212I|GT-C3222|GT-C3300|GT-C3300K|GT-C3303|GT-C3303K|GT-C3310|GT-C3322|GT-C3330|GT-C3350|GT-C3500|GT-C3510|GT-C3530|GT-C3630|GT-C3780|GT-C5010|GT-C5212|GT-C6620|GT-C6625|GT-C6712|GT-E1050|GT-E1070|GT-E1075|GT-E1080|GT-E1081|GT-E1085|GT-E1087|GT-E1100|GT-E1107|GT-E1110|GT-E1120|GT-E1125|GT-E1130|GT-E1160|GT-E1170|GT-E1175|GT-E1180|GT-E1182|GT-E1200|GT-E1210|GT-E1225|GT-E1230|GT-E1390|GT-E2100|GT-E2120|GT-E2121|GT-E2152|GT-E2220|GT-E2222|GT-E2230|GT-E2232|GT-E2250|GT-E2370|GT-E2550|GT-E2652|GT-E3210|GT-E3213|GT-I5500|GT-I5503|GT-I5700|GT-I5800|GT-I5801|GT-I6410|GT-I6420|GT-I7110|GT-I7410|GT-I7500|GT-I8000|GT-I8150|GT-I8160|GT-I8320|GT-I8330|GT-I8350|GT-I8530|GT-I8700|GT-I8703|GT-I8910|GT-I9000|GT-I9001|GT-I9003|GT-I9010|GT-I9020|GT-I9023|GT-I9070|GT-I9100|GT-I9103|GT-I9220|GT-I9250|GT-I9300|GT-I9300 |GT-M3510|GT-M5650|GT-M7500|GT-M7600|GT-M7603|GT-M8800|GT-M8910|GT-N7000|GT-P6810|GT-P7100|GT-S3110|GT-S3310|GT-S3350|GT-S3353|GT-S3370|GT-S3650|GT-S3653|GT-S3770|GT-S3850|GT-S5210|GT-S5220|GT-S5229|GT-S5230|GT-S5233|GT-S5250|GT-S5253|GT-S5260|GT-S5263|GT-S5270|GT-S5300|GT-S5330|GT-S5350|GT-S5360|GT-S5363|GT-S5369|GT-S5380|GT-S5380D|GT-S5560|GT-S5570|GT-S5600|GT-S5603|GT-S5610|GT-S5620|GT-S5660|GT-S5670|GT-S5690|GT-S5750|GT-S5780|GT-S5830|GT-S5839|GT-S6102|GT-S6500|GT-S7070|GT-S7200|GT-S7220|GT-S7230|GT-S7233|GT-S7250|GT-S7500|GT-S7530|GT-S7550|GT-S8000|GT-S8003|GT-S8500|GT-S8530|GT-S8600|SCH-A310|SCH-A530|SCH-A570|SCH-A610|SCH-A630|SCH-A650|SCH-A790|SCH-A795|SCH-A850|SCH-A870|SCH-A890|SCH-A930|SCH-A950|SCH-A970|SCH-A990|SCH-I100|SCH-I110|SCH-I400|SCH-I405|SCH-I500|SCH-I510|SCH-I515|SCH-I600|SCH-I730|SCH-I760|SCH-I770|SCH-I830|SCH-I910|SCH-I920|SCH-LC11|SCH-N150|SCH-N300|SCH-R100|SCH-R300|SCH-R351|SCH-R400|SCH-R410|SCH-T300|SCH-U310|SCH-U320|SCH-U350|SCH-U360|SCH-U365|SCH-U370|SCH-U380|SCH-U410|SCH-U430|SCH-U450|SCH-U460|SCH-U470|SCH-U490|SCH-U540|SCH-U550|SCH-U620|SCH-U640|SCH-U650|SCH-U660|SCH-U700|SCH-U740|SCH-U750|SCH-U810|SCH-U820|SCH-U900|SCH-U940|SCH-U960|SCS-26UC|SGH-A107|SGH-A117|SGH-A127|SGH-A137|SGH-A157|SGH-A167|SGH-A177|SGH-A187|SGH-A197|SGH-A227|SGH-A237|SGH-A257|SGH-A437|SGH-A517|SGH-A597|SGH-A637|SGH-A657|SGH-A667|SGH-A687|SGH-A697|SGH-A707|SGH-A717|SGH-A727|SGH-A737|SGH-A747|SGH-A767|SGH-A777|SGH-A797|SGH-A817|SGH-A827|SGH-A837|SGH-A847|SGH-A867|SGH-A877|SGH-A887|SGH-A897|SGH-A927|SGH-B100|SGH-B130|SGH-B200|SGH-B220|SGH-C100|SGH-C110|SGH-C120|SGH-C130|SGH-C140|SGH-C160|SGH-C170|SGH-C180|SGH-C200|SGH-C207|SGH-C210|SGH-C225|SGH-C230|SGH-C417|SGH-C450|SGH-D307|SGH-D347|SGH-D357|SGH-D407|SGH-D415|SGH-D780|SGH-D807|SGH-D980|SGH-E105|SGH-E200|SGH-E315|SGH-E316|SGH-E317|SGH-E335|SGH-E590|SGH-E635|SGH-E715|SGH-E890|SGH-F300|SGH-F480|SGH-I200|SGH-I300|SGH-I320|SGH-I550|SGH-I577|SGH-I600|SGH-I607|SGH-I617|SGH-I627|SGH-I637|SGH-I677|SGH-I700|SGH-I717|SGH-I727|SGH-I777|SGH-I780|SGH-I827|SGH-I847|SGH-I857|SGH-I896|SGH-I897|SGH-I900|SGH-I907|SGH-I917|SGH-I927|SGH-I937|SGH-I997|SGH-J150|SGH-J200|SGH-L170|SGH-L700|SGH-M110|SGH-M150|SGH-M200|SGH-N105|SGH-N500|SGH-N600|SGH-N620|SGH-N625|SGH-N700|SGH-N710|SGH-P107|SGH-P207|SGH-P300|SGH-P310|SGH-P520|SGH-P735|SGH-P777|SGH-Q105|SGH-R210|SGH-R220|SGH-R225|SGH-S105|SGH-S307|SGH-T109|SGH-T119|SGH-T139|SGH-T209|SGH-T219|SGH-T229|SGH-T239|SGH-T249|SGH-T259|SGH-T309|SGH-T319|SGH-T329|SGH-T339|SGH-T349|SGH-T359|SGH-T369|SGH-T379|SGH-T409|SGH-T429|SGH-T439|SGH-T459|SGH-T469|SGH-T479|SGH-T499|SGH-T509|SGH-T519|SGH-T539|SGH-T559|SGH-T589|SGH-T609|SGH-T619|SGH-T629|SGH-T639|SGH-T659|SGH-T669|SGH-T679|SGH-T709|SGH-T719|SGH-T729|SGH-T739|SGH-T746|SGH-T749|SGH-T759|SGH-T769|SGH-T809|SGH-T819|SGH-T839|SGH-T919|SGH-T929|SGH-T939|SGH-T959|SGH-T989|SGH-U100|SGH-U200|SGH-U800|SGH-V205|SGH-V206|SGH-X100|SGH-X105|SGH-X120|SGH-X140|SGH-X426|SGH-X427|SGH-X475|SGH-X495|SGH-X497|SGH-X507|SGH-X600|SGH-X610|SGH-X620|SGH-X630|SGH-X700|SGH-X820|SGH-X890|SGH-Z130|SGH-Z150|SGH-Z170|SGH-ZX10|SGH-ZX20|SHW-M110|SPH-A120|SPH-A400|SPH-A420|SPH-A460|SPH-A500|SPH-A560|SPH-A600|SPH-A620|SPH-A660|SPH-A700|SPH-A740|SPH-A760|SPH-A790|SPH-A800|SPH-A820|SPH-A840|SPH-A880|SPH-A900|SPH-A940|SPH-A960|SPH-D600|SPH-D700|SPH-D710|SPH-D720|SPH-I300|SPH-I325|SPH-I330|SPH-I350|SPH-I500|SPH-I600|SPH-I700|SPH-L700|SPH-M100|SPH-M220|SPH-M240|SPH-M300|SPH-M305|SPH-M320|SPH-M330|SPH-M350|SPH-M360|SPH-M370|SPH-M380|SPH-M510|SPH-M540|SPH-M550|SPH-M560|SPH-M570|SPH-M580|SPH-M610|SPH-M620|SPH-M630|SPH-M800|SPH-M810|SPH-M850|SPH-M900|SPH-M910|SPH-M920|SPH-M930|SPH-N100|SPH-N200|SPH-N240|SPH-N300|SPH-N400|SPH-Z400|SWC-E100', - 'Sony' => 'E10i|SonyEricsson|SonyEricssonLT15iv', - 'Asus' => 'Asus.*Galaxy', - 'Palm' => 'PalmSource|Palm', // avantgo|blazer|elaine|hiptop|plucker|xiino ; @todo - complete the regex. - 'Vertu' => 'Vertu|Vertu.*Ltd|Vertu.*Ascent|Vertu.*Ayxta|Vertu.*Constellation(F|Quest)?|Vertu.*Monika|Vertu.*Signature', // Just for fun ;) - 'GenericPhone' => '(mmp|pocket|psp|symbian|Smartphone|smartfon|treo|up.browser|up.link|vodafone|wap|nokia|Series40|Series60|S60|SonyEricsson|N900|PPC;|MAUI.*WAP.*Browser|LG-P500)' - ); - // List of tablet devices. - protected $tabletDevices = array( - 'BlackBerryTablet' => 'PlayBook|RIM Tablet', - 'iPad' => 'iPad|iPad.*Mobile', // @todo: check for mobile friendly emails topic. - 'Kindle' => 'Kindle|Silk.*Accelerated', - 'SamsungTablet' => 'SAMSUNG.*Tablet|Galaxy.*Tab|GT-P1000|GT-P1010|GT-P6210|GT-P6800|GT-P6810|GT-P7100|GT-P7300|GT-P7310|GT-P7500|GT-P7510|SCH-I800|SCH-I815|SCH-I905|SGH-I957|SGH-I987|SGH-T849|SGH-T859|SGH-T869|SPH-P100|GT-P1000|GT-P3100|GT-P3110|GT-P5100|GT-P5110|GT-P6200|GT-P7300|GT-P7320|GT-P7500|GT-P7510|GT-P7511', - 'HTCtablet' => 'HTC Flyer|HTC Jetstream|HTC-P715a|HTC EVO View 4G|PG41200', - 'MotorolaTablet' => 'xoom|sholest|MZ615|MZ605|MZ505|MZ601|MZ602|MZ603|MZ604|MZ606|MZ607|MZ608|MZ609|MZ615|MZ616|MZ617', - 'AsusTablet' => 'Transformer|TF101', - 'NookTablet' => 'NookColor|nook browser|BNTV250A|LogicPD Zoom2', - 'AcerTablet' => 'Android.*\b(A100|A101|A200|A500|A501|A510|W500|W500P|W501|W501P)\b', - 'YarvikTablet' => 'Android.*(TAB210|TAB211|TAB224|TAB250|TAB260|TAB264|TAB310|TAB360|TAB364|TAB410|TAB411|TAB420|TAB424|TAB450|TAB460|TAB461|TAB464|TAB465|TAB467|TAB468)', - 'GenericTablet' => 'Tablet(?!.*PC)|ViewPad7|LG-V909|MID7015|BNTV250A|LogicPD Zoom2|\bA7EB\b|CatNova8|A1_07|CT704|CT1002|\bM721\b', - ); - // List of mobile Operating Systems. - protected $operatingSystems = array( - 'AndroidOS' => '(android.*mobile|android(?!.*mobile))', - 'BlackBerryOS' => '(blackberry|rim tablet os)', - 'PalmOS' => '(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)', - 'SymbianOS' => 'Symbian|SymbOS|Series60|Series40|\bS60\b', - 'WindowsMobileOS' => 'IEMobile|Windows Phone|Windows CE.*(PPC|Smartphone)|MSIEMobile|Window Mobile|XBLWP7', - 'iOS' => '(iphone|ipod|ipad)', - 'FlashLiteOS' => '', - 'JavaOS' => '', - 'NokiaOS' => '', - 'webOS' => '', - 'badaOS' => '\bBada\b', - 'BREWOS' => '', - ); - // List of mobile User Agents. - protected $userAgents = array( - 'Chrome' => '\bCrMo\b|Chrome\/[.0-9]* Mobile', - 'Dolfin' => '\bDolfin\b', - 'Opera' => 'Opera.*Mini|Opera.*Mobi|Android.*Opera', - 'Skyfire' => 'skyfire', - 'IE' => 'IEMobile|MSIEMobile', - 'Firefox' => 'fennec|firefox.*maemo|(Mobile|Tablet).*Firefox|Firefox.*Mobile', - 'Bolt' => 'bolt', - 'TeaShark' => 'teashark', - 'Blazer' => 'Blazer', - 'Safari' => 'Mobile.*Safari|Safari.*Mobile', - 'Midori' => 'midori', - 'GenericBrowser' => 'NokiaBrowser|OviBrowser|SEMC.*Browser' - ); - - function __construct(){ - - // Merge all rules together. - $this->detectionRules = array_merge( - $this->phoneDevices, - $this->tabletDevices, - $this->operatingSystems, - $this->userAgents - ); - $this->userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; - $this->accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : null; - - if ( - isset($_SERVER['HTTP_X_WAP_PROFILE']) || - isset($_SERVER['HTTP_X_WAP_CLIENTID']) || - isset($_SERVER['HTTP_WAP_CONNECTION']) || - isset($_SERVER['HTTP_PROFILE']) || - isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) || // Reported by Nokia devices (eg. C3) - isset($_SERVER['HTTP_X_NOKIA_IPADDRESS']) || - isset($_SERVER['HTTP_X_NOKIA_GATEWAY_ID']) || - isset($_SERVER['HTTP_X_ORANGE_ID']) || - isset($_SERVER['HTTP_X_VODAFONE_3GPDPCONTEXT']) || - isset($_SERVER['HTTP_X_HUAWEI_USERID']) || - isset($_SERVER['HTTP_UA_OS']) || // Reported by Windows Smartphones - (isset($_SERVER['HTTP_UA_CPU']) && $_SERVER['HTTP_UA_CPU'] == 'ARM') // Seen this on a HTC - ) { - $this->isMobile = true; - } elseif (!empty($this->accept) && (strpos($this->accept, 'text/vnd.wap.wml') !== false || strpos($this->accept, 'application/vnd.wap.xhtml+xml') !== false)) { - $this->isMobile = true; - } else { - $this->_detect(); - } - - } - - public function getRules() - { - return $this->detectionRules; - } - - /** - * Magic overloading method. - * - * @method boolean is[...]() - * @param string $name - * @param array $arguments - * @return mixed - */ - public function __call($name, $arguments) - { - - $key = substr($name, 2); - return $this->_detect($key); - - } - - /** - * Private method that does the detection of the - * mobile devices. - * - * @param string $key - * @return boolean|null - */ - private function _detect($key='') - { - - if(empty($key)){ - - // Begin general search. - foreach($this->detectionRules as $_regex){ - if(empty($_regex)){ continue; } - if(preg_match('/'.$_regex.'/is', $this->userAgent)){ - $this->isMobile = true; - return true; - } - } - return false; - - } else { - - // Search for a certain key. - // Make the keys lowecase so we can match: isIphone(), isiPhone(), isiphone(), etc. - $key = strtolower($key); - $_rules = array_change_key_case($this->detectionRules); - - if(array_key_exists($key, $_rules)){ - if(empty($_rules[$key])){ return null; } - if(preg_match('/'.$_rules[$key].'/is', $this->userAgent)){ - $this->isMobile = true; - return true; - } else { - return false; - } - } else { - trigger_error("Method $key is not defined", E_USER_WARNING); - } - - return false; - - } - - } - - /** - * Check if the device is mobile. - * Returns true if any type of mobile device detected, including special ones - * @return bool - */ - public function isMobile() - { - return $this->isMobile; - } - - /** - * Check if the device is a tablet. - * Return true if any type of tablet device is detected. - * @return boolean - */ - public function isTablet() - { - - foreach($this->tabletDevices as $_regex){ - if(preg_match('/'.$_regex.'/is', $this->userAgent)){ - $this->isTablet = true; - return true; - } - } - - return false; - - } - - +isMobile() or $detect->isTablet() + * + * For more specific usage see the documentation navigate to: + * http://code.google.com/p/php-mobile-detect/wiki/Mobile_Detect + * + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ + +class Mobile_Detect { + + protected $detectionRules; + protected $userAgent = null; + protected $accept = null; + // Assume the visitor has a desktop environment. + protected $isMobile = false; + protected $isTablet = false; + protected $phoneDeviceName = null; + protected $tabletDevicename = null; + protected $operatingSystemName = null; + protected $userAgentName = null; + // List of mobile devices (phones) + protected $phoneDevices = array( + 'iPhone' => '(iPhone.*Mobile|iPod|iTunes)', + 'BlackBerry' => 'BlackBerry|rim[0-9]+', + 'HTC' => 'HTC|HTC.*(6800|8100|8900|A7272|S510e|C110e|Legend|Desire|T8282)|APX515CKT|Qtek9090|APA9292KT', + 'Nexus' => 'Nexus One|Nexus S|Galaxy.*Nexus|Android.*Nexus', + 'Dell' => 'Dell.*Streak|Dell.*Aero|Dell.*Venue|DELL.*Venue Pro|Dell Flash|Dell Smoke|Dell Mini 3iX|XCD28|XCD35', + 'Motorola' => '\bDroid\b.*Build|DROIDX|HRI39|MOT\-|A1260|A1680|A555|A853|A855|A953|A955|A956|Motorola.*ELECTRIFY|Motorola.*i1|i867|i940|MB200|MB300|MB501|MB502|MB508|MB511|MB520|MB525|MB526|MB611|MB612|MB632|MB810|MB855|MB860|MB861|MB865|MB870|ME501|ME502|ME511|ME525|ME600|ME632|ME722|ME811|ME860|ME863|ME865|MT620|MT710|MT716|MT720|MT810|MT870|MT917|Motorola.*TITANIUM|WX435|WX445|XT300|XT301|XT311|XT316|XT317|XT319|XT320|XT390|XT502|XT530|XT531|XT532|XT535|XT603|XT610|XT611|XT615|XT681|XT701|XT702|XT711|XT720|XT800|XT806|XT860|XT862|XT875|XT882|XT883|XT894|XT909|XT910|XT912|XT928', + 'Samsung' => 'Samsung|BGT-S5230|GT-B2100|GT-B2700|GT-B2710|GT-B3210|GT-B3310|GT-B3410|GT-B3730|GT-B3740|GT-B5510|GT-B5512|GT-B5722|GT-B6520|GT-B7300|GT-B7320|GT-B7330|GT-B7350|GT-B7510|GT-B7722|GT-B7800|GT-C3010|GT-C3011|GT-C3060|GT-C3200|GT-C3212|GT-C3212I|GT-C3222|GT-C3300|GT-C3300K|GT-C3303|GT-C3303K|GT-C3310|GT-C3322|GT-C3330|GT-C3350|GT-C3500|GT-C3510|GT-C3530|GT-C3630|GT-C3780|GT-C5010|GT-C5212|GT-C6620|GT-C6625|GT-C6712|GT-E1050|GT-E1070|GT-E1075|GT-E1080|GT-E1081|GT-E1085|GT-E1087|GT-E1100|GT-E1107|GT-E1110|GT-E1120|GT-E1125|GT-E1130|GT-E1160|GT-E1170|GT-E1175|GT-E1180|GT-E1182|GT-E1200|GT-E1210|GT-E1225|GT-E1230|GT-E1390|GT-E2100|GT-E2120|GT-E2121|GT-E2152|GT-E2220|GT-E2222|GT-E2230|GT-E2232|GT-E2250|GT-E2370|GT-E2550|GT-E2652|GT-E3210|GT-E3213|GT-I5500|GT-I5503|GT-I5700|GT-I5800|GT-I5801|GT-I6410|GT-I6420|GT-I7110|GT-I7410|GT-I7500|GT-I8000|GT-I8150|GT-I8160|GT-I8320|GT-I8330|GT-I8350|GT-I8530|GT-I8700|GT-I8703|GT-I8910|GT-I9000|GT-I9001|GT-I9003|GT-I9010|GT-I9020|GT-I9023|GT-I9070|GT-I9100|GT-I9103|GT-I9220|GT-I9250|GT-I9300|GT-I9300 |GT-M3510|GT-M5650|GT-M7500|GT-M7600|GT-M7603|GT-M8800|GT-M8910|GT-N7000|GT-P6810|GT-P7100|GT-S3110|GT-S3310|GT-S3350|GT-S3353|GT-S3370|GT-S3650|GT-S3653|GT-S3770|GT-S3850|GT-S5210|GT-S5220|GT-S5229|GT-S5230|GT-S5233|GT-S5250|GT-S5253|GT-S5260|GT-S5263|GT-S5270|GT-S5300|GT-S5330|GT-S5350|GT-S5360|GT-S5363|GT-S5369|GT-S5380|GT-S5380D|GT-S5560|GT-S5570|GT-S5600|GT-S5603|GT-S5610|GT-S5620|GT-S5660|GT-S5670|GT-S5690|GT-S5750|GT-S5780|GT-S5830|GT-S5839|GT-S6102|GT-S6500|GT-S7070|GT-S7200|GT-S7220|GT-S7230|GT-S7233|GT-S7250|GT-S7500|GT-S7530|GT-S7550|GT-S8000|GT-S8003|GT-S8500|GT-S8530|GT-S8600|SCH-A310|SCH-A530|SCH-A570|SCH-A610|SCH-A630|SCH-A650|SCH-A790|SCH-A795|SCH-A850|SCH-A870|SCH-A890|SCH-A930|SCH-A950|SCH-A970|SCH-A990|SCH-I100|SCH-I110|SCH-I400|SCH-I405|SCH-I500|SCH-I510|SCH-I515|SCH-I600|SCH-I730|SCH-I760|SCH-I770|SCH-I830|SCH-I910|SCH-I920|SCH-LC11|SCH-N150|SCH-N300|SCH-R100|SCH-R300|SCH-R351|SCH-R400|SCH-R410|SCH-T300|SCH-U310|SCH-U320|SCH-U350|SCH-U360|SCH-U365|SCH-U370|SCH-U380|SCH-U410|SCH-U430|SCH-U450|SCH-U460|SCH-U470|SCH-U490|SCH-U540|SCH-U550|SCH-U620|SCH-U640|SCH-U650|SCH-U660|SCH-U700|SCH-U740|SCH-U750|SCH-U810|SCH-U820|SCH-U900|SCH-U940|SCH-U960|SCS-26UC|SGH-A107|SGH-A117|SGH-A127|SGH-A137|SGH-A157|SGH-A167|SGH-A177|SGH-A187|SGH-A197|SGH-A227|SGH-A237|SGH-A257|SGH-A437|SGH-A517|SGH-A597|SGH-A637|SGH-A657|SGH-A667|SGH-A687|SGH-A697|SGH-A707|SGH-A717|SGH-A727|SGH-A737|SGH-A747|SGH-A767|SGH-A777|SGH-A797|SGH-A817|SGH-A827|SGH-A837|SGH-A847|SGH-A867|SGH-A877|SGH-A887|SGH-A897|SGH-A927|SGH-B100|SGH-B130|SGH-B200|SGH-B220|SGH-C100|SGH-C110|SGH-C120|SGH-C130|SGH-C140|SGH-C160|SGH-C170|SGH-C180|SGH-C200|SGH-C207|SGH-C210|SGH-C225|SGH-C230|SGH-C417|SGH-C450|SGH-D307|SGH-D347|SGH-D357|SGH-D407|SGH-D415|SGH-D780|SGH-D807|SGH-D980|SGH-E105|SGH-E200|SGH-E315|SGH-E316|SGH-E317|SGH-E335|SGH-E590|SGH-E635|SGH-E715|SGH-E890|SGH-F300|SGH-F480|SGH-I200|SGH-I300|SGH-I320|SGH-I550|SGH-I577|SGH-I600|SGH-I607|SGH-I617|SGH-I627|SGH-I637|SGH-I677|SGH-I700|SGH-I717|SGH-I727|SGH-I777|SGH-I780|SGH-I827|SGH-I847|SGH-I857|SGH-I896|SGH-I897|SGH-I900|SGH-I907|SGH-I917|SGH-I927|SGH-I937|SGH-I997|SGH-J150|SGH-J200|SGH-L170|SGH-L700|SGH-M110|SGH-M150|SGH-M200|SGH-N105|SGH-N500|SGH-N600|SGH-N620|SGH-N625|SGH-N700|SGH-N710|SGH-P107|SGH-P207|SGH-P300|SGH-P310|SGH-P520|SGH-P735|SGH-P777|SGH-Q105|SGH-R210|SGH-R220|SGH-R225|SGH-S105|SGH-S307|SGH-T109|SGH-T119|SGH-T139|SGH-T209|SGH-T219|SGH-T229|SGH-T239|SGH-T249|SGH-T259|SGH-T309|SGH-T319|SGH-T329|SGH-T339|SGH-T349|SGH-T359|SGH-T369|SGH-T379|SGH-T409|SGH-T429|SGH-T439|SGH-T459|SGH-T469|SGH-T479|SGH-T499|SGH-T509|SGH-T519|SGH-T539|SGH-T559|SGH-T589|SGH-T609|SGH-T619|SGH-T629|SGH-T639|SGH-T659|SGH-T669|SGH-T679|SGH-T709|SGH-T719|SGH-T729|SGH-T739|SGH-T746|SGH-T749|SGH-T759|SGH-T769|SGH-T809|SGH-T819|SGH-T839|SGH-T919|SGH-T929|SGH-T939|SGH-T959|SGH-T989|SGH-U100|SGH-U200|SGH-U800|SGH-V205|SGH-V206|SGH-X100|SGH-X105|SGH-X120|SGH-X140|SGH-X426|SGH-X427|SGH-X475|SGH-X495|SGH-X497|SGH-X507|SGH-X600|SGH-X610|SGH-X620|SGH-X630|SGH-X700|SGH-X820|SGH-X890|SGH-Z130|SGH-Z150|SGH-Z170|SGH-ZX10|SGH-ZX20|SHW-M110|SPH-A120|SPH-A400|SPH-A420|SPH-A460|SPH-A500|SPH-A560|SPH-A600|SPH-A620|SPH-A660|SPH-A700|SPH-A740|SPH-A760|SPH-A790|SPH-A800|SPH-A820|SPH-A840|SPH-A880|SPH-A900|SPH-A940|SPH-A960|SPH-D600|SPH-D700|SPH-D710|SPH-D720|SPH-I300|SPH-I325|SPH-I330|SPH-I350|SPH-I500|SPH-I600|SPH-I700|SPH-L700|SPH-M100|SPH-M220|SPH-M240|SPH-M300|SPH-M305|SPH-M320|SPH-M330|SPH-M350|SPH-M360|SPH-M370|SPH-M380|SPH-M510|SPH-M540|SPH-M550|SPH-M560|SPH-M570|SPH-M580|SPH-M610|SPH-M620|SPH-M630|SPH-M800|SPH-M810|SPH-M850|SPH-M900|SPH-M910|SPH-M920|SPH-M930|SPH-N100|SPH-N200|SPH-N240|SPH-N300|SPH-N400|SPH-Z400|SWC-E100', + 'Sony' => 'E10i|SonyEricsson|SonyEricssonLT15iv', + 'Asus' => 'Asus.*Galaxy', + 'Palm' => 'PalmSource|Palm', // avantgo|blazer|elaine|hiptop|plucker|xiino ; @todo - complete the regex. + 'Vertu' => 'Vertu|Vertu.*Ltd|Vertu.*Ascent|Vertu.*Ayxta|Vertu.*Constellation(F|Quest)?|Vertu.*Monika|Vertu.*Signature', // Just for fun ;) + 'GenericPhone' => '(mmp|pocket|psp|symbian|Smartphone|smartfon|treo|up.browser|up.link|vodafone|wap|nokia|Series40|Series60|S60|SonyEricsson|N900|PPC;|MAUI.*WAP.*Browser|LG-P500)' + ); + // List of tablet devices. + protected $tabletDevices = array( + 'BlackBerryTablet' => 'PlayBook|RIM Tablet', + 'iPad' => 'iPad|iPad.*Mobile', // @todo: check for mobile friendly emails topic. + 'Kindle' => 'Kindle|Silk.*Accelerated', + 'SamsungTablet' => 'SAMSUNG.*Tablet|Galaxy.*Tab|GT-P1000|GT-P1010|GT-P6210|GT-P6800|GT-P6810|GT-P7100|GT-P7300|GT-P7310|GT-P7500|GT-P7510|SCH-I800|SCH-I815|SCH-I905|SGH-I957|SGH-I987|SGH-T849|SGH-T859|SGH-T869|SPH-P100|GT-P1000|GT-P3100|GT-P3110|GT-P5100|GT-P5110|GT-P6200|GT-P7300|GT-P7320|GT-P7500|GT-P7510|GT-P7511', + 'HTCtablet' => 'HTC Flyer|HTC Jetstream|HTC-P715a|HTC EVO View 4G|PG41200', + 'MotorolaTablet' => 'xoom|sholest|MZ615|MZ605|MZ505|MZ601|MZ602|MZ603|MZ604|MZ606|MZ607|MZ608|MZ609|MZ615|MZ616|MZ617', + 'AsusTablet' => 'Transformer|TF101', + 'NookTablet' => 'NookColor|nook browser|BNTV250A|LogicPD Zoom2', + 'AcerTablet' => 'Android.*\b(A100|A101|A200|A500|A501|A510|W500|W500P|W501|W501P)\b', + 'YarvikTablet' => 'Android.*(TAB210|TAB211|TAB224|TAB250|TAB260|TAB264|TAB310|TAB360|TAB364|TAB410|TAB411|TAB420|TAB424|TAB450|TAB460|TAB461|TAB464|TAB465|TAB467|TAB468)', + 'GenericTablet' => 'Tablet(?!.*PC)|ViewPad7|LG-V909|MID7015|BNTV250A|LogicPD Zoom2|\bA7EB\b|CatNova8|A1_07|CT704|CT1002|\bM721\b', + ); + // List of mobile Operating Systems. + protected $operatingSystems = array( + 'AndroidOS' => '(android.*mobile|android(?!.*mobile))', + 'BlackBerryOS' => '(blackberry|rim tablet os)', + 'PalmOS' => '(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)', + 'SymbianOS' => 'Symbian|SymbOS|Series60|Series40|\bS60\b', + 'WindowsMobileOS' => 'IEMobile|Windows Phone|Windows CE.*(PPC|Smartphone)|MSIEMobile|Window Mobile|XBLWP7', + 'iOS' => '(iphone|ipod|ipad)', + 'FlashLiteOS' => '', + 'JavaOS' => '', + 'NokiaOS' => '', + 'webOS' => '', + 'badaOS' => '\bBada\b', + 'BREWOS' => '', + ); + // List of mobile User Agents. + protected $userAgents = array( + 'Chrome' => '\bCrMo\b|Chrome\/[.0-9]* Mobile', + 'Dolfin' => '\bDolfin\b', + 'Opera' => 'Opera.*Mini|Opera.*Mobi|Android.*Opera', + 'Skyfire' => 'skyfire', + 'IE' => 'IEMobile|MSIEMobile', + 'Firefox' => 'fennec|firefox.*maemo|(Mobile|Tablet).*Firefox|Firefox.*Mobile', + 'Bolt' => 'bolt', + 'TeaShark' => 'teashark', + 'Blazer' => 'Blazer', + 'Safari' => 'Mobile.*Safari|Safari.*Mobile', + 'Midori' => 'midori', + 'GenericBrowser' => 'NokiaBrowser|OviBrowser|SEMC.*Browser' + ); + + function __construct(){ + + // Merge all rules together. + $this->detectionRules = array_merge( + $this->phoneDevices, + $this->tabletDevices, + $this->operatingSystems, + $this->userAgents + ); + $this->userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; + $this->accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : null; + + if ( + isset($_SERVER['HTTP_X_WAP_PROFILE']) || + isset($_SERVER['HTTP_X_WAP_CLIENTID']) || + isset($_SERVER['HTTP_WAP_CONNECTION']) || + isset($_SERVER['HTTP_PROFILE']) || + isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) || // Reported by Nokia devices (eg. C3) + isset($_SERVER['HTTP_X_NOKIA_IPADDRESS']) || + isset($_SERVER['HTTP_X_NOKIA_GATEWAY_ID']) || + isset($_SERVER['HTTP_X_ORANGE_ID']) || + isset($_SERVER['HTTP_X_VODAFONE_3GPDPCONTEXT']) || + isset($_SERVER['HTTP_X_HUAWEI_USERID']) || + isset($_SERVER['HTTP_UA_OS']) || // Reported by Windows Smartphones + (isset($_SERVER['HTTP_UA_CPU']) && $_SERVER['HTTP_UA_CPU'] == 'ARM') // Seen this on a HTC + ) { + $this->isMobile = true; + } elseif (!empty($this->accept) && (strpos($this->accept, 'text/vnd.wap.wml') !== false || strpos($this->accept, 'application/vnd.wap.xhtml+xml') !== false)) { + $this->isMobile = true; + } else { + $this->_detect(); + } + + } + + public function getRules() + { + return $this->detectionRules; + } + + /** + * Magic overloading method. + * + * @method boolean is[...]() + * @param string $name + * @param array $arguments + * @return mixed + */ + public function __call($name, $arguments) + { + + $key = substr($name, 2); + return $this->_detect($key); + + } + + /** + * Private method that does the detection of the + * mobile devices. + * + * @param string $key + * @return boolean|null + */ + private function _detect($key='') + { + + if(empty($key)){ + + // Begin general search. + foreach($this->detectionRules as $_regex){ + if(empty($_regex)){ continue; } + if(preg_match('/'.$_regex.'/is', $this->userAgent)){ + $this->isMobile = true; + return true; + } + } + return false; + + } else { + + // Search for a certain key. + // Make the keys lowecase so we can match: isIphone(), isiPhone(), isiphone(), etc. + $key = strtolower($key); + $_rules = array_change_key_case($this->detectionRules); + + if(array_key_exists($key, $_rules)){ + if(empty($_rules[$key])){ return null; } + if(preg_match('/'.$_rules[$key].'/is', $this->userAgent)){ + $this->isMobile = true; + return true; + } else { + return false; + } + } else { + trigger_error("Method $key is not defined", E_USER_WARNING); + } + + return false; + + } + + } + + /** + * Check if the device is mobile. + * Returns true if any type of mobile device detected, including special ones + * @return bool + */ + public function isMobile() + { + return $this->isMobile; + } + + /** + * Check if the device is a tablet. + * Return true if any type of tablet device is detected. + * @return boolean + */ + public function isTablet() + { + + foreach($this->tabletDevices as $_regex){ + if(preg_match('/'.$_regex.'/is', $this->userAgent)){ + $this->isTablet = true; + return true; + } + } + + return false; + + } + + } \ No newline at end of file diff --git a/js/dynatree/MIT-License.txt b/js/dynatree/MIT-License.txt index e300dc2a7..636db4355 100755 --- a/js/dynatree/MIT-License.txt +++ b/js/dynatree/MIT-License.txt @@ -1,7 +1,7 @@ -Copyright (c) 2008-2011 Martin Wendt (http://wwWendt.de) - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - +Copyright (c) 2008-2011 Martin Wendt (http://wwWendt.de) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/js/dynatree/jquery.dynatree.min.js b/js/dynatree/jquery.dynatree.min.js index bddd113e5..f7e57b88a 100755 --- a/js/dynatree/jquery.dynatree.min.js +++ b/js/dynatree/jquery.dynatree.min.js @@ -1,270 +1,270 @@ -// jquery.dynatree.js build 1.2.0 -// Revision: 528, date: 2011-09-17 18:58:59 -// Copyright (c) 2008-10 Martin Wendt (http://dynatree.googlecode.com/) -// Dual licensed under the MIT or GPL Version 2 licenses. - -var _canLog=true;function _log(mode,msg){if(!_canLog){return;} -var args=Array.prototype.slice.apply(arguments,[1]);var dt=new Date();var tag=dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds()+"."+dt.getMilliseconds();args[0]=tag+" - "+args[0];try{switch(mode){case"info":window.console.info.apply(window.console,args);break;case"warn":window.console.warn.apply(window.console,args);break;default:window.console.log.apply(window.console,args);break;}}catch(e){if(!window.console){_canLog=false;}}} -function logMsg(msg){Array.prototype.unshift.apply(arguments,["debug"]);_log.apply(this,arguments);} -var getDynaTreePersistData=null;var DTNodeStatus_Error=-1;var DTNodeStatus_Loading=1;var DTNodeStatus_Ok=0;(function($){var Class={create:function(){return function(){this.initialize.apply(this,arguments);};}};function getDtNodeFromElement(el){var iMax=5;while(el&&iMax--){if(el.dtnode){return el.dtnode;} -el=el.parentNode;} -return null;} -function noop(){} -var DynaTreeNode=Class.create();DynaTreeNode.prototype={initialize:function(parent,tree,data){this.parent=parent;this.tree=tree;if(typeof data==="string"){data={title:data};} -if(data.key===undefined){data.key="_"+tree._nodeCount++;} -this.data=$.extend({},$.ui.dynatree.nodedatadefaults,data);this.li=null;this.span=null;this.ul=null;this.childList=null;this.isLoading=false;this.hasSubSel=false;this.bExpanded=false;this.bSelected=false;},toString:function(){return"DynaTreeNode<"+this.data.key+">: '"+this.data.title+"'";},toDict:function(recursive,callback){var dict=$.extend({},this.data);dict.activate=(this.tree.activeNode===this);dict.focus=(this.tree.focusNode===this);dict.expand=this.bExpanded;dict.select=this.bSelected;if(callback){callback(dict);} -if(recursive&&this.childList){dict.children=[];for(var i=0,l=this.childList.length;i1){res+=cache.tagConnector;}}else if(this.hasChildren()!==false){res+=cache.tagExpander;}else{res+=cache.tagConnector;} -if(opts.checkbox&&data.hideCheckbox!==true&&!data.isStatusNode){res+=cache.tagCheckbox;} -if(data.icon){res+="";}else if(data.icon===false){noop();}else{res+=cache.tagNodeIcon;} -var nodeTitle="";if(opts.onCustomRender){nodeTitle=opts.onCustomRender.call(tree,this)||"";} -if(!nodeTitle){var tooltip=data.tooltip?" title='"+data.tooltip+"'":"";if(opts.noLink||data.noLink){nodeTitle=""+data.title+"";}else{nodeTitle=""+data.title+"";}} -res+=nodeTitle;return res;},_fixOrder:function(){var cl=this.childList;if(!cl||!this.ul){return;} -var childLI=this.ul.firstChild;for(var i=0,l=cl.length-1;i1){this.ul.className=cn.container+" "+cn.noConnector;}else{this.ul.className=cn.container;}}else if(parent){if(!this.li){firstTime=true;this.li=document.createElement("li");this.li.dtnode=this;if(data.key&&opts.generateIds){this.li.id=opts.idPrefix+data.key;} -this.span=document.createElement("span");this.span.className=cn.title;this.li.appendChild(this.span);if(!parent.ul){parent.ul=document.createElement("ul");parent.ul.style.display="none";parent.li.appendChild(parent.ul);} -parent.ul.appendChild(this.li);} -this.span.innerHTML=this._getInnerHtml();var cnList=[];cnList.push(cn.node);if(data.isFolder){cnList.push(cn.folder);} -if(this.bExpanded){cnList.push(cn.expanded);} -if(this.hasChildren()!==false){cnList.push(cn.hasChildren);} -if(data.isLazy&&this.childList===null){cnList.push(cn.lazy);} -if(isLastSib){cnList.push(cn.lastsib);} -if(this.bSelected){cnList.push(cn.selected);} -if(this.hasSubSel){cnList.push(cn.partsel);} -if(tree.activeNode===this){cnList.push(cn.active);} -if(data.addClass){cnList.push(data.addClass);} -cnList.push(cn.combinedExpanderPrefix -+(this.bExpanded?"e":"c") -+(data.isLazy&&this.childList===null?"d":"") -+(isLastSib?"l":""));cnList.push(cn.combinedIconPrefix -+(this.bExpanded?"e":"c") -+(data.isFolder?"f":""));this.span.className=cnList.join(" ");this.li.className=isLastSib?cn.lastsib:"";if(firstTime&&opts.onCreate){opts.onCreate.call(tree,this,this.span);} -if(opts.onRender){opts.onRender.call(tree,this,this.span);}} -if((this.bExpanded||includeInvisible===true)&&this.childList){for(var i=0,l=this.childList.length;iy?1:-1;};cl.sort(cmp);if(deep){for(var i=0,l=cl.length;i0){this.childList[0].focus();}else{this.focus();}} -break;case DTNodeStatus_Loading:this.isLoading=true;$(this.span).addClass(this.tree.options.classNames.nodeLoading);if(!this.parent){this._setStatusNode({title:this.tree.options.strings.loading+info,tooltip:tooltip,addClass:this.tree.options.classNames.nodeWait});} -break;case DTNodeStatus_Error:this.isLoading=false;this._setStatusNode({title:this.tree.options.strings.loadError+info,tooltip:tooltip,addClass:this.tree.options.classNames.nodeError});break;default:throw"Bad LazyNodeStatus: '"+lts+"'.";}},_parentList:function(includeRoot,includeSelf){var l=[];var dtn=includeSelf?this:this.parent;while(dtn){if(includeRoot||dtn.parent){l.unshift(dtn);} -dtn=dtn.parent;} -return l;},getLevel:function(){var level=0;var dtn=this.parent;while(dtn){level++;dtn=dtn.parent;} -return level;},_getTypeForOuterNodeEvent:function(event){var cns=this.tree.options.classNames;var target=event.target;if(target.className.indexOf(cns.node)<0){return null;} -var eventX=event.pageX-target.offsetLeft;var eventY=event.pageY-target.offsetTop;for(var i=0,l=target.childNodes.length;i=x&&eventX<=(x+nx)&&eventY>=y&&eventY<=(y+ny)){if(cn.className==cns.title){return"title";}else if(cn.className==cns.expander){return"expander";}else if(cn.className==cns.checkbox){return"checkbox";}else if(cn.className==cns.nodeIcon){return"icon";}}} -return"prefix";},getEventTargetType:function(event){var tcn=event&&event.target?event.target.className:"";var cns=this.tree.options.classNames;if(tcn===cns.title){return"title";}else if(tcn===cns.expander){return"expander";}else if(tcn===cns.checkbox){return"checkbox";}else if(tcn===cns.nodeIcon){return"icon";}else if(tcn===cns.empty||tcn===cns.vline||tcn===cns.connector){return"prefix";}else if(tcn.indexOf(cns.node)>=0){return this._getTypeForOuterNodeEvent(event);} -return null;},isVisible:function(){var parents=this._parentList(true,false);for(var i=0,l=parents.length;ia").focus();}catch(e){}},isFocused:function(){return(this.tree.tnFocused===this);},_activate:function(flag,fireEvents){this.tree.logDebug("dtnode._activate(%o, fireEvents=%o) - %o",flag,fireEvents,this);var opts=this.tree.options;if(this.data.isStatusNode){return;} -if(fireEvents&&opts.onQueryActivate&&opts.onQueryActivate.call(this.tree,flag,this)===false){return;} -if(flag){if(this.tree.activeNode){if(this.tree.activeNode===this){return;} -this.tree.activeNode.deactivate();} -if(opts.activeVisible){this.makeVisible();} -this.tree.activeNode=this;if(opts.persist){$.cookie(opts.cookieId+"-active",this.data.key,opts.cookie);} -this.tree.persistence.activeKey=this.data.key;$(this.span).addClass(opts.classNames.active);if(fireEvents&&opts.onActivate){opts.onActivate.call(this.tree,this);}}else{if(this.tree.activeNode===this){if(opts.onQueryActivate&&opts.onQueryActivate.call(this.tree,false,this)===false){return;} -$(this.span).removeClass(opts.classNames.active);if(opts.persist){$.cookie(opts.cookieId+"-active","",opts.cookie);} -this.tree.persistence.activeKey=null;this.tree.activeNode=null;if(fireEvents&&opts.onDeactivate){opts.onDeactivate.call(this.tree,this);}}}},activate:function(){this._activate(true,true);},activateSilently:function(){this._activate(true,false);},deactivate:function(){this._activate(false,true);},isActive:function(){return(this.tree.activeNode===this);},_userActivate:function(){var activate=true;var expand=false;if(this.data.isFolder){switch(this.tree.options.clickFolderMode){case 2:activate=false;expand=true;break;case 3:activate=expand=true;break;}} -if(this.parent===null){expand=false;} -if(expand){this.toggleExpand();this.focus();} -if(activate){this.activate();}},_setSubSel:function(hasSubSel){if(hasSubSel){this.hasSubSel=true;$(this.span).addClass(this.tree.options.classNames.partsel);}else{this.hasSubSel=false;$(this.span).removeClass(this.tree.options.classNames.partsel);}},_updatePartSelectionState:function(){var sel;if(!this.hasChildren()){sel=(this.bSelected&&!this.data.unselectable&&!this.data.isStatusNode);this._setSubSel(false);return sel;} -var i,l,cl=this.childList,allSelected=true,allDeselected=true;for(i=0,l=cl.length;i=0;i--){sib=parents[i].getNextSibling();if(sib){break;}}} -if(sib){sib.focus();} -break;default:handled=false;} -if(handled){event.preventDefault();}},_onKeypress:function(event){},_onFocus:function(event){var opts=this.tree.options;if(event.type=="blur"||event.type=="focusout"){if(opts.onBlur){opts.onBlur.call(this.tree,this);} -if(this.tree.tnFocused){$(this.tree.tnFocused.span).removeClass(opts.classNames.focused);} -this.tree.tnFocused=null;if(opts.persist){$.cookie(opts.cookieId+"-focus","",opts.cookie);}}else if(event.type=="focus"||event.type=="focusin"){if(this.tree.tnFocused&&this.tree.tnFocused!==this){this.tree.logDebug("dtnode.onFocus: out of sync: curFocus: %o",this.tree.tnFocused);$(this.tree.tnFocused.span).removeClass(opts.classNames.focused);} -this.tree.tnFocused=this;if(opts.onFocus){opts.onFocus.call(this.tree,this);} -$(this.tree.tnFocused.span).addClass(opts.classNames.focused);if(opts.persist){$.cookie(opts.cookieId+"-focus",this.data.key,opts.cookie);}}},visit:function(fn,includeSelf){var res=true;if(includeSelf===true){res=fn(this);if(res===false||res=="skip"){return res;}} -if(this.childList){for(var i=0,l=this.childList.length;i reloading %s...",this,keyPath,child);var self=this;child.reloadChildren(function(node,isOk){if(isOk){tree.logDebug("%s._loadKeyPath(%s) -> reloaded %s.",node,keyPath,node);callback.call(tree,child,"loaded");node._loadKeyPath(segList.join(tree.options.keyPathSeparator),callback);}else{tree.logWarning("%s._loadKeyPath(%s) -> reloadChildren() failed.",self,keyPath);callback.call(tree,child,"error");}});}else{callback.call(tree,child,"loaded");child._loadKeyPath(segList.join(tree.options.keyPathSeparator),callback);} -return;}} -tree.logWarning("Node not found: "+seg);return;},resetLazy:function(){if(this.parent===null){throw"Use tree.reload() instead";}else if(!this.data.isLazy){throw"node.resetLazy() requires lazy nodes.";} -this.expand(false);this.removeChildren();},_addChildNode:function(dtnode,beforeNode){var tree=this.tree,opts=tree.options,pers=tree.persistence;dtnode.parent=this;if(this.childList===null){this.childList=[];}else if(!beforeNode){if(this.childList.length>0){$(this.childList[this.childList.length-1].span).removeClass(opts.classNames.lastsib);}} -if(beforeNode){var iBefore=$.inArray(beforeNode,this.childList);if(iBefore<0){throw" must be a child of ";} -this.childList.splice(iBefore,0,dtnode);}else{this.childList.push(dtnode);} -var isInitializing=tree.isInitializing();if(opts.persist&&pers.cookiesFound&&isInitializing){if(pers.activeKey===dtnode.data.key){tree.activeNode=dtnode;} -if(pers.focusedKey===dtnode.data.key){tree.focusNode=dtnode;} -dtnode.bExpanded=($.inArray(dtnode.data.key,pers.expandedKeyList)>=0);dtnode.bSelected=($.inArray(dtnode.data.key,pers.selectedKeyList)>=0);}else{if(dtnode.data.activate){tree.activeNode=dtnode;if(opts.persist){pers.activeKey=dtnode.data.key;}} -if(dtnode.data.focus){tree.focusNode=dtnode;if(opts.persist){pers.focusedKey=dtnode.data.key;}} -dtnode.bExpanded=(dtnode.data.expand===true);if(dtnode.bExpanded&&opts.persist){pers.addExpand(dtnode.data.key);} -dtnode.bSelected=(dtnode.data.select===true);if(dtnode.bSelected&&opts.persist){pers.addSelect(dtnode.data.key);}} -if(opts.minExpandLevel>=dtnode.getLevel()){this.bExpanded=true;} -if(dtnode.bSelected&&opts.selectMode==3){var p=this;while(p){if(!p.hasSubSel){p._setSubSel(true);} -p=p.parent;}} -if(tree.bEnableUpdate){this.render();} -return dtnode;},addChild:function(obj,beforeNode){if(typeof(obj)=="string"){throw"Invalid data type for "+obj;}else if(!obj||obj.length===0){return;}else if(obj instanceof DynaTreeNode){return this._addChildNode(obj,beforeNode);} -if(!obj.length){obj=[obj];} -var prevFlag=this.tree.enableUpdate(false);var tnFirst=null;for(var i=0,l=obj.length;i=0){this.expandedKeyList.splice(idx,1);$.cookie(this.cookieId+"-expand",this.expandedKeyList.join(","),this.cookieOpts);}},addSelect:function(key){if($.inArray(key,this.selectedKeyList)<0){this.selectedKeyList.push(key);$.cookie(this.cookieId+"-select",this.selectedKeyList.join(","),this.cookieOpts);}},clearSelect:function(key){var idx=$.inArray(key,this.selectedKeyList);if(idx>=0){this.selectedKeyList.splice(idx,1);$.cookie(this.cookieId+"-select",this.selectedKeyList.join(","),this.cookieOpts);}},isReloading:function(){return this.cookiesFound===true;},toDict:function(){return{cookiesFound:this.cookiesFound,activeKey:this.activeKey,focusedKey:this.activeKey,expandedKeyList:this.expandedKeyList,selectedKeyList:this.selectedKeyList};},lastentry:undefined};var DynaTree=Class.create();DynaTree.version="$Version: 1.2.0$";DynaTree.prototype={initialize:function($widget){this.phase="init";this.$widget=$widget;this.options=$widget.options;this.$tree=$widget.element;this.timer=null;this.divTree=this.$tree.get(0);_initDragAndDrop(this);},_load:function(callback){var $widget=this.$widget;var opts=this.options,self=this;this.bEnableUpdate=true;this._nodeCount=1;this.activeNode=null;this.focusNode=null;if(opts.rootVisible!==undefined){this.logWarning("Option 'rootVisible' is no longer supported.");} -if(opts.minExpandLevel<1){this.logWarning("Option 'minExpandLevel' must be >= 1.");opts.minExpandLevel=1;} -if(opts.classNames!==$.ui.dynatree.prototype.options.classNames){opts.classNames=$.extend({},$.ui.dynatree.prototype.options.classNames,opts.classNames);} -if(opts.ajaxDefaults!==$.ui.dynatree.prototype.options.ajaxDefaults){opts.ajaxDefaults=$.extend({},$.ui.dynatree.prototype.options.ajaxDefaults,opts.ajaxDefaults);} -if(opts.dnd!==$.ui.dynatree.prototype.options.dnd){opts.dnd=$.extend({},$.ui.dynatree.prototype.options.dnd,opts.dnd);} -if(!opts.imagePath){$("script").each(function(){var _rexDtLibName=/.*dynatree[^\/]*\.js$/i;if(this.src.search(_rexDtLibName)>=0){if(this.src.indexOf("/")>=0){opts.imagePath=this.src.slice(0,this.src.lastIndexOf("/"))+"/skin/";}else{opts.imagePath="skin/";} -self.logDebug("Guessing imagePath from '%s': '%s'",this.src,opts.imagePath);return false;}});} -this.persistence=new DynaTreeStatus(opts.cookieId,opts.cookie);if(opts.persist){if(!$.cookie){_log("warn","Please include jquery.cookie.js to use persistence.");} -this.persistence.read();} -this.logDebug("DynaTree.persistence: %o",this.persistence.toDict());this.cache={tagEmpty:"",tagVline:"",tagExpander:"",tagConnector:"",tagNodeIcon:"",tagCheckbox:"",lastentry:undefined};if(opts.children||(opts.initAjax&&opts.initAjax.url)||opts.initId){$(this.divTree).empty();} -var $ulInitialize=this.$tree.find(">ul:first").hide();this.tnRoot=new DynaTreeNode(null,this,{});this.tnRoot.bExpanded=true;this.tnRoot.render();this.divTree.appendChild(this.tnRoot.ul);var root=this.tnRoot;var isReloading=(opts.persist&&this.persistence.isReloading());var isLazy=false;var prevFlag=this.enableUpdate(false);this.logDebug("Dynatree._load(): read tree structure...");if(opts.children){root.addChild(opts.children);}else if(opts.initAjax&&opts.initAjax.url){isLazy=true;root.data.isLazy=true;this._reloadAjax(callback);}else if(opts.initId){this._createFromTag(root,$("#"+opts.initId));}else{this._createFromTag(root,$ulInitialize);$ulInitialize.remove();} -this._checkConsistency();if(!isLazy&&opts.selectMode==3){root._updatePartSelectionState();} -this.logDebug("Dynatree._load(): render nodes...");this.enableUpdate(prevFlag);this.logDebug("Dynatree._load(): bind events...");this.$widget.bind();this.logDebug("Dynatree._load(): postInit...");this.phase="postInit";if(opts.persist){this.persistence.write();} -if(this.focusNode&&this.focusNode.isVisible()){this.logDebug("Focus on init: %o",this.focusNode);this.focusNode.focus();} -if(!isLazy){if(opts.onPostInit){opts.onPostInit.call(this,isReloading,false);} -if(callback){callback.call(this,"ok");}} -this.phase="idle";},_reloadAjax:function(callback){var opts=this.options;if(!opts.initAjax||!opts.initAjax.url){throw"tree.reload() requires 'initAjax' mode.";} -var pers=this.persistence;var ajaxOpts=$.extend({},opts.initAjax);if(ajaxOpts.addActiveKey){ajaxOpts.data.activeKey=pers.activeKey;} -if(ajaxOpts.addFocusedKey){ajaxOpts.data.focusedKey=pers.focusedKey;} -if(ajaxOpts.addExpandedKeyList){ajaxOpts.data.expandedKeyList=pers.expandedKeyList.join(",");} -if(ajaxOpts.addSelectedKeyList){ajaxOpts.data.selectedKeyList=pers.selectedKeyList.join(",");} -if(ajaxOpts.success){this.logWarning("initAjax: success callback is ignored; use onPostInit instead.");} -if(ajaxOpts.error){this.logWarning("initAjax: error callback is ignored; use onPostInit instead.");} -var isReloading=pers.isReloading();ajaxOpts.success=function(dtnode,data,textStatus){if(opts.selectMode==3){dtnode.tree.tnRoot._updatePartSelectionState();} -if(opts.onPostInit){opts.onPostInit.call(dtnode.tree,isReloading,false);} -if(callback){callback.call(dtnode.tree,"ok");}};ajaxOpts.error=function(dtnode,XMLHttpRequest,textStatus,errorThrown){if(opts.onPostInit){opts.onPostInit.call(dtnode.tree,isReloading,true,XMLHttpRequest,textStatus,errorThrown);} -if(callback){callback.call(dtnode.tree,"error",XMLHttpRequest,textStatus,errorThrown);}};this.logDebug("Dynatree._init(): send Ajax request...");this.tnRoot.appendAjax(ajaxOpts);},toString:function(){return"Dynatree '"+this.$tree.attr("id")+"'";},toDict:function(){return this.tnRoot.toDict(true);},serializeArray:function(stopOnParents){var nodeList=this.getSelectedNodes(stopOnParents),name=this.$tree.attr("name")||this.$tree.attr("id"),arr=[];for(var i=0,l=nodeList.length;i=2){Array.prototype.unshift.apply(arguments,["debug"]);_log.apply(this,arguments);}},logInfo:function(msg){if(this.options.debugLevel>=1){Array.prototype.unshift.apply(arguments,["info"]);_log.apply(this,arguments);}},logWarning:function(msg){Array.prototype.unshift.apply(arguments,["warn"]);_log.apply(this,arguments);},isInitializing:function(){return(this.phase=="init"||this.phase=="postInit");},isReloading:function(){return(this.phase=="init"||this.phase=="postInit")&&this.options.persist&&this.persistence.cookiesFound;},isUserEvent:function(){return(this.phase=="userEvent");},redraw:function(){this.tnRoot.render(false,false);},renderInvisibleNodes:function(){this.tnRoot.render(false,true);},reload:function(callback){this._load(callback);},getRoot:function(){return this.tnRoot;},enable:function(){this.$widget.enable();},disable:function(){this.$widget.disable();},getNodeByKey:function(key){var el=document.getElementById(this.options.idPrefix+key);if(el){return el.dtnode?el.dtnode:null;} -var match=null;this.visit(function(node){if(node.data.key==key){match=node;return false;}},true);return match;},getActiveNode:function(){return this.activeNode;},reactivate:function(setFocus){var node=this.activeNode;if(node){this.activeNode=null;node.activate();if(setFocus){node.focus();}}},getSelectedNodes:function(stopOnParents){var nodeList=[];this.tnRoot.visit(function(node){if(node.bSelected){nodeList.push(node);if(stopOnParents===true){return"skip";}}});return nodeList;},activateKey:function(key){var dtnode=(key===null)?null:this.getNodeByKey(key);if(!dtnode){if(this.activeNode){this.activeNode.deactivate();} -this.activeNode=null;return null;} -dtnode.focus();dtnode.activate();return dtnode;},loadKeyPath:function(keyPath,callback){var segList=keyPath.split(this.options.keyPathSeparator);if(segList[0]===""){segList.shift();} -if(segList[0]==this.tnRoot.data.key){this.logDebug("Removed leading root key.");segList.shift();} -keyPath=segList.join(this.options.keyPathSeparator);return this.tnRoot._loadKeyPath(keyPath,callback);},selectKey:function(key,select){var dtnode=this.getNodeByKey(key);if(!dtnode){return null;} -dtnode.select(select);return dtnode;},enableUpdate:function(bEnable){if(this.bEnableUpdate==bEnable){return bEnable;} -this.bEnableUpdate=bEnable;if(bEnable){this.redraw();} -return!bEnable;},count:function(){return this.tnRoot.countChildren();},visit:function(fn,includeRoot){return this.tnRoot.visit(fn,includeRoot);},_createFromTag:function(parentTreeNode,$ulParent){var self=this;$ulParent.find(">li").each(function(){var $li=$(this),$liSpan=$li.find(">span:first"),$liA=$li.find(">a:first"),title,href=null,target=null,tooltip;if($liSpan.length){title=$liSpan.html();}else if($liA.length){title=$liA.html();href=$liA.attr("href");target=$liA.attr("target");tooltip=$liA.attr("title");}else{title=$li.html();var iPos=title.search(/
    =0){title=$.trim(title.substring(0,iPos));}else{title=$.trim(title);}} -var data={title:title,tooltip:tooltip,isFolder:$li.hasClass("folder"),isLazy:$li.hasClass("lazy"),expand:$li.hasClass("expanded"),select:$li.hasClass("selected"),activate:$li.hasClass("active"),focus:$li.hasClass("focused"),noLink:$li.hasClass("noLink")};if(href){data.href=href;data.target=target;} -if($li.attr("title")){data.tooltip=$li.attr("title");} -if($li.attr("id")){data.key=$li.attr("id");} -if($li.attr("data")){var dataAttr=$.trim($li.attr("data"));if(dataAttr){if(dataAttr.charAt(0)!="{"){dataAttr="{"+dataAttr+"}";} -try{$.extend(data,eval("("+dataAttr+")"));}catch(e){throw("Error parsing node data: "+e+"\ndata:\n'"+dataAttr+"'");}}} -var childNode=parentTreeNode.addChild(data);var $ul=$li.find(">ul:first");if($ul.length){self._createFromTag(childNode,$ul);}});},_checkConsistency:function(){},_setDndStatus:function(sourceNode,targetNode,helper,hitMode,accept){var $source=sourceNode?$(sourceNode.span):null,$target=$(targetNode.span);if(!this.$dndMarker){this.$dndMarker=$("
    ").hide().prependTo($(this.divTree).parent());} -if(hitMode==="after"||hitMode==="before"||hitMode==="over"){var pos=$target.offset();switch(hitMode){case"before":this.$dndMarker.removeClass("dynatree-drop-after dynatree-drop-over");this.$dndMarker.addClass("dynatree-drop-before");pos.top-=8;break;case"after":this.$dndMarker.removeClass("dynatree-drop-before dynatree-drop-over");this.$dndMarker.addClass("dynatree-drop-after");pos.top+=8;break;default:this.$dndMarker.removeClass("dynatree-drop-after dynatree-drop-before");this.$dndMarker.addClass("dynatree-drop-over");$target.addClass("dynatree-drop-target");pos.left+=8;} -this.$dndMarker.offset({left:pos.left,top:pos.top}).css({"z-index":1000}).show();}else{$target.removeClass("dynatree-drop-target");this.$dndMarker.hide();} -if(hitMode==="after"){$target.addClass("dynatree-drop-after");}else{$target.removeClass("dynatree-drop-after");} -if(hitMode==="before"){$target.addClass("dynatree-drop-before");}else{$target.removeClass("dynatree-drop-before");} -if(accept===true){if($source){$source.addClass("dynatree-drop-accept");} -$target.addClass("dynatree-drop-accept");helper.addClass("dynatree-drop-accept");}else{if($source){$source.removeClass("dynatree-drop-accept");} -$target.removeClass("dynatree-drop-accept");helper.removeClass("dynatree-drop-accept");} -if(accept===false){if($source){$source.addClass("dynatree-drop-reject");} -$target.addClass("dynatree-drop-reject");helper.addClass("dynatree-drop-reject");}else{if($source){$source.removeClass("dynatree-drop-reject");} -$target.removeClass("dynatree-drop-reject");helper.removeClass("dynatree-drop-reject");}},_onDragEvent:function(eventName,node,otherNode,event,ui,draggable){var opts=this.options;var dnd=this.options.dnd;var res=null;var nodeTag=$(node.span);var hitMode;switch(eventName){case"helper":var helper=$("
    ").append($(event.target).closest('a').clone());helper.data("dtSourceNode",node);res=helper;break;case"start":if(node.isStatusNode()){res=false;}else if(dnd.onDragStart){res=dnd.onDragStart(node);} -if(res===false){this.logDebug("tree.onDragStart() cancelled");ui.helper.trigger("mouseup");ui.helper.hide();}else{nodeTag.addClass("dynatree-drag-source");} -break;case"enter":res=dnd.onDragEnter?dnd.onDragEnter(node,otherNode):null;res={over:(res!==false)&&((res===true)||(res==="over")||$.inArray("over",res)>=0),before:(res!==false)&&((res===true)||(res==="before")||$.inArray("before",res)>=0),after:(res!==false)&&((res===true)||(res==="after")||$.inArray("after",res)>=0)};ui.helper.data("enterResponse",res);break;case"over":var enterResponse=ui.helper.data("enterResponse");hitMode=null;if(enterResponse===false){break;}else if(typeof enterResponse==="string"){hitMode=enterResponse;}else{var nodeOfs=nodeTag.offset();var relPos={x:event.pageX-nodeOfs.left,y:event.pageY-nodeOfs.top};var relPos2={x:relPos.x/nodeTag.width(),y:relPos.y/nodeTag.height()};if(enterResponse.after&&relPos2.y>0.75){hitMode="after";}else if(!enterResponse.over&&enterResponse.after&&relPos2.y>0.5){hitMode="after";}else if(enterResponse.before&&relPos2.y<=0.25){hitMode="before";}else if(!enterResponse.over&&enterResponse.before&&relPos2.y<=0.5){hitMode="before";}else if(enterResponse.over){hitMode="over";} -if(dnd.preventVoidMoves){if(node===otherNode){hitMode=null;}else if(hitMode==="before"&&otherNode&&node===otherNode.getNextSibling()){hitMode=null;}else if(hitMode==="after"&&otherNode&&node===otherNode.getPrevSibling()){hitMode=null;}else if(hitMode==="over"&&otherNode&&otherNode.parent===node&&otherNode.isLastSibling()){hitMode=null;}} -ui.helper.data("hitMode",hitMode);} -if(hitMode==="over"&&dnd.autoExpandMS&&node.hasChildren()!==false&&!node.bExpanded){node.scheduleAction("expand",dnd.autoExpandMS);} -if(hitMode&&dnd.onDragOver){res=dnd.onDragOver(node,otherNode,hitMode);} -this._setDndStatus(otherNode,node,ui.helper,hitMode,res!==false);break;case"drop":hitMode=ui.helper.data("hitMode");if(hitMode&&dnd.onDrop){dnd.onDrop(node,otherNode,hitMode,ui,draggable);} -break;case"leave":node.scheduleAction("cancel");ui.helper.data("enterResponse",null);ui.helper.data("hitMode",null);this._setDndStatus(otherNode,node,ui.helper,"out",undefined);if(dnd.onDragLeave){dnd.onDragLeave(node,otherNode);} -break;case"stop":nodeTag.removeClass("dynatree-drag-source");if(dnd.onDragStop){dnd.onDragStop(node);} -break;default:throw"Unsupported drag event: "+eventName;} -return res;},cancelDrag:function(){var dd=$.ui.ddmanager.current;if(dd){dd.cancel();}},lastentry:undefined};$.widget("ui.dynatree",{_init:function(){if(parseFloat($.ui.version)<1.8){if(this.options.debugLevel>=0){_log("warn","ui.dynatree._init() was called; you should upgrade to jquery.ui.core.js v1.8 or higher.");} -return this._create();} -if(this.options.debugLevel>=2){_log("debug","ui.dynatree._init() was called; no current default functionality.");}},_create:function(){var opts=this.options;if(opts.debugLevel>=1){logMsg("Dynatree._create(): version='%s', debugLevel=%o.",$.ui.dynatree.version,this.options.debugLevel);} -this.options.event+=".dynatree";var divTree=this.element.get(0);this.tree=new DynaTree(this);this.tree._load();this.tree.logDebug("Dynatree._init(): done.");},bind:function(){this.unbind();var eventNames="click.dynatree dblclick.dynatree";if(this.options.keyboard){eventNames+=" keypress.dynatree keydown.dynatree";} -this.element.bind(eventNames,function(event){var dtnode=getDtNodeFromElement(event.target);if(!dtnode){return true;} -var tree=dtnode.tree;var o=tree.options;tree.logDebug("event(%s): dtnode: %s",event.type,dtnode);var prevPhase=tree.phase;tree.phase="userEvent";try{switch(event.type){case"click":return(o.onClick&&o.onClick.call(tree,dtnode,event)===false)?false:dtnode._onClick(event);case"dblclick":return(o.onDblClick&&o.onDblClick.call(tree,dtnode,event)===false)?false:dtnode._onDblClick(event);case"keydown":return(o.onKeydown&&o.onKeydown.call(tree,dtnode,event)===false)?false:dtnode._onKeydown(event);case"keypress":return(o.onKeypress&&o.onKeypress.call(tree,dtnode,event)===false)?false:dtnode._onKeypress(event);}}catch(e){var _=null;tree.logWarning("bind(%o): dtnode: %o, error: %o",event,dtnode,e);}finally{tree.phase=prevPhase;}});function __focusHandler(event){event=$.event.fix(event||window.event);var dtnode=getDtNodeFromElement(event.target);return dtnode?dtnode._onFocus(event):false;} -var div=this.tree.divTree;if(div.addEventListener){div.addEventListener("focus",__focusHandler,true);div.addEventListener("blur",__focusHandler,true);}else{div.onfocusin=div.onfocusout=__focusHandler;}},unbind:function(){this.element.unbind(".dynatree");},enable:function(){this.bind();$.Widget.prototype.enable.apply(this,arguments);},disable:function(){this.unbind();$.Widget.prototype.disable.apply(this,arguments);},getTree:function(){return this.tree;},getRoot:function(){return this.tree.getRoot();},getActiveNode:function(){return this.tree.getActiveNode();},getSelectedNodes:function(){return this.tree.getSelectedNodes();},lastentry:undefined});if(parseFloat($.ui.version)<1.8){$.ui.dynatree.getter="getTree getRoot getActiveNode getSelectedNodes";} -$.ui.dynatree.version="$Version: 1.2.0$";$.ui.dynatree.getNode=function(el){if(el instanceof DynaTreeNode){return el;} -var $el=el.selector===undefined?$(el):el,parent=$el.parents("[dtnode]").first(),node;if(typeof parent.prop=="function"){node=parent.prop("dtnode");}else{node=parent.attr("dtnode");} -return node;} -$.ui.dynatree.getPersistData=DynaTreeStatus._getTreePersistData;$.ui.dynatree.prototype.options={title:"Dynatree",minExpandLevel:1,imagePath:null,children:null,initId:null,initAjax:null,autoFocus:true,keyboard:true,persist:false,autoCollapse:false,clickFolderMode:3,activeVisible:true,checkbox:false,selectMode:2,fx:null,noLink:false,onClick:null,onDblClick:null,onKeydown:null,onKeypress:null,onFocus:null,onBlur:null,onQueryActivate:null,onQuerySelect:null,onQueryExpand:null,onPostInit:null,onActivate:null,onDeactivate:null,onSelect:null,onExpand:null,onLazyRead:null,onCustomRender:null,onCreate:null,onRender:null,dnd:{onDragStart:null,onDragStop:null,autoExpandMS:1000,preventVoidMoves:true,onDragEnter:null,onDragOver:null,onDrop:null,onDragLeave:null},ajaxDefaults:{cache:false,dataType:"json"},strings:{loading:"Loading…",loadError:"Load error!"},generateIds:false,idPrefix:"dynatree-id-",keyPathSeparator:"/",cookieId:"dynatree",cookie:{expires:null},classNames:{container:"dynatree-container",node:"dynatree-node",folder:"dynatree-folder",empty:"dynatree-empty",vline:"dynatree-vline",expander:"dynatree-expander",connector:"dynatree-connector",checkbox:"dynatree-checkbox",nodeIcon:"dynatree-icon",title:"dynatree-title",noConnector:"dynatree-no-connector",nodeError:"dynatree-statusnode-error",nodeWait:"dynatree-statusnode-wait",hidden:"dynatree-hidden",combinedExpanderPrefix:"dynatree-exp-",combinedIconPrefix:"dynatree-ico-",nodeLoading:"dynatree-loading",hasChildren:"dynatree-has-children",active:"dynatree-active",selected:"dynatree-selected",expanded:"dynatree-expanded",lazy:"dynatree-lazy",focused:"dynatree-focused",partsel:"dynatree-partsel",lastsib:"dynatree-lastsib"},debugLevel:1,lastentry:undefined};if(parseFloat($.ui.version)<1.8){$.ui.dynatree.defaults=$.ui.dynatree.prototype.options;} -$.ui.dynatree.nodedatadefaults={title:null,key:null,isFolder:false,isLazy:false,tooltip:null,icon:null,addClass:null,noLink:false,activate:false,focus:false,expand:false,select:false,hideCheckbox:false,unselectable:false,children:null,lastentry:undefined};function _initDragAndDrop(tree){var dnd=tree.options.dnd||null;if(dnd&&(dnd.onDragStart||dnd.onDrop)){_registerDnd();} -if(dnd&&dnd.onDragStart){tree.$tree.draggable({addClasses:false,appendTo:"body",containment:false,delay:0,distance:4,revert:false,connectToDynatree:true,helper:function(event){var sourceNode=getDtNodeFromElement(event.target);return sourceNode.tree._onDragEvent("helper",sourceNode,null,event,null,null);},_last:null});} -if(dnd&&dnd.onDrop){tree.$tree.droppable({addClasses:false,tolerance:"intersect",greedy:false,_last:null});}} -var didRegisterDnd=false;var _registerDnd=function(){if(didRegisterDnd){return;} -$.ui.plugin.add("draggable","connectToDynatree",{start:function(event,ui){var draggable=$(this).data("draggable");var sourceNode=ui.helper.data("dtSourceNode")||null;if(sourceNode){draggable.offset.click.top=-2;draggable.offset.click.left=+16;return sourceNode.tree._onDragEvent("start",sourceNode,null,event,ui,draggable);}},drag:function(event,ui){var draggable=$(this).data("draggable");var sourceNode=ui.helper.data("dtSourceNode")||null;var prevTargetNode=ui.helper.data("dtTargetNode")||null;var targetNode=getDtNodeFromElement(event.target);if(event.target&&!targetNode){var isHelper=$(event.target).closest("div.dynatree-drag-helper,#dynatree-drop-marker").length>0;if(isHelper){return;}} -ui.helper.data("dtTargetNode",targetNode);if(prevTargetNode&&prevTargetNode!==targetNode){prevTargetNode.tree._onDragEvent("leave",prevTargetNode,sourceNode,event,ui,draggable);} -if(targetNode){if(!targetNode.tree.options.dnd.onDrop){noop();}else if(targetNode===prevTargetNode){targetNode.tree._onDragEvent("over",targetNode,sourceNode,event,ui,draggable);}else{targetNode.tree._onDragEvent("enter",targetNode,sourceNode,event,ui,draggable);}}},stop:function(event,ui){var draggable=$(this).data("draggable");var sourceNode=ui.helper.data("dtSourceNode")||null;var targetNode=ui.helper.data("dtTargetNode")||null;var mouseDownEvent=draggable._mouseDownEvent;var eventType=event.type;var dropped=(eventType=="mouseup"&&event.which==1);if(!dropped){logMsg("Drag was cancelled");} -if(targetNode){if(dropped){targetNode.tree._onDragEvent("drop",targetNode,sourceNode,event,ui,draggable);} -targetNode.tree._onDragEvent("leave",targetNode,sourceNode,event,ui,draggable);} +// jquery.dynatree.js build 1.2.0 +// Revision: 528, date: 2011-09-17 18:58:59 +// Copyright (c) 2008-10 Martin Wendt (http://dynatree.googlecode.com/) +// Dual licensed under the MIT or GPL Version 2 licenses. + +var _canLog=true;function _log(mode,msg){if(!_canLog){return;} +var args=Array.prototype.slice.apply(arguments,[1]);var dt=new Date();var tag=dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds()+"."+dt.getMilliseconds();args[0]=tag+" - "+args[0];try{switch(mode){case"info":window.console.info.apply(window.console,args);break;case"warn":window.console.warn.apply(window.console,args);break;default:window.console.log.apply(window.console,args);break;}}catch(e){if(!window.console){_canLog=false;}}} +function logMsg(msg){Array.prototype.unshift.apply(arguments,["debug"]);_log.apply(this,arguments);} +var getDynaTreePersistData=null;var DTNodeStatus_Error=-1;var DTNodeStatus_Loading=1;var DTNodeStatus_Ok=0;(function($){var Class={create:function(){return function(){this.initialize.apply(this,arguments);};}};function getDtNodeFromElement(el){var iMax=5;while(el&&iMax--){if(el.dtnode){return el.dtnode;} +el=el.parentNode;} +return null;} +function noop(){} +var DynaTreeNode=Class.create();DynaTreeNode.prototype={initialize:function(parent,tree,data){this.parent=parent;this.tree=tree;if(typeof data==="string"){data={title:data};} +if(data.key===undefined){data.key="_"+tree._nodeCount++;} +this.data=$.extend({},$.ui.dynatree.nodedatadefaults,data);this.li=null;this.span=null;this.ul=null;this.childList=null;this.isLoading=false;this.hasSubSel=false;this.bExpanded=false;this.bSelected=false;},toString:function(){return"DynaTreeNode<"+this.data.key+">: '"+this.data.title+"'";},toDict:function(recursive,callback){var dict=$.extend({},this.data);dict.activate=(this.tree.activeNode===this);dict.focus=(this.tree.focusNode===this);dict.expand=this.bExpanded;dict.select=this.bSelected;if(callback){callback(dict);} +if(recursive&&this.childList){dict.children=[];for(var i=0,l=this.childList.length;i1){res+=cache.tagConnector;}}else if(this.hasChildren()!==false){res+=cache.tagExpander;}else{res+=cache.tagConnector;} +if(opts.checkbox&&data.hideCheckbox!==true&&!data.isStatusNode){res+=cache.tagCheckbox;} +if(data.icon){res+="";}else if(data.icon===false){noop();}else{res+=cache.tagNodeIcon;} +var nodeTitle="";if(opts.onCustomRender){nodeTitle=opts.onCustomRender.call(tree,this)||"";} +if(!nodeTitle){var tooltip=data.tooltip?" title='"+data.tooltip+"'":"";if(opts.noLink||data.noLink){nodeTitle=""+data.title+"";}else{nodeTitle=""+data.title+"";}} +res+=nodeTitle;return res;},_fixOrder:function(){var cl=this.childList;if(!cl||!this.ul){return;} +var childLI=this.ul.firstChild;for(var i=0,l=cl.length-1;i1){this.ul.className=cn.container+" "+cn.noConnector;}else{this.ul.className=cn.container;}}else if(parent){if(!this.li){firstTime=true;this.li=document.createElement("li");this.li.dtnode=this;if(data.key&&opts.generateIds){this.li.id=opts.idPrefix+data.key;} +this.span=document.createElement("span");this.span.className=cn.title;this.li.appendChild(this.span);if(!parent.ul){parent.ul=document.createElement("ul");parent.ul.style.display="none";parent.li.appendChild(parent.ul);} +parent.ul.appendChild(this.li);} +this.span.innerHTML=this._getInnerHtml();var cnList=[];cnList.push(cn.node);if(data.isFolder){cnList.push(cn.folder);} +if(this.bExpanded){cnList.push(cn.expanded);} +if(this.hasChildren()!==false){cnList.push(cn.hasChildren);} +if(data.isLazy&&this.childList===null){cnList.push(cn.lazy);} +if(isLastSib){cnList.push(cn.lastsib);} +if(this.bSelected){cnList.push(cn.selected);} +if(this.hasSubSel){cnList.push(cn.partsel);} +if(tree.activeNode===this){cnList.push(cn.active);} +if(data.addClass){cnList.push(data.addClass);} +cnList.push(cn.combinedExpanderPrefix ++(this.bExpanded?"e":"c") ++(data.isLazy&&this.childList===null?"d":"") ++(isLastSib?"l":""));cnList.push(cn.combinedIconPrefix ++(this.bExpanded?"e":"c") ++(data.isFolder?"f":""));this.span.className=cnList.join(" ");this.li.className=isLastSib?cn.lastsib:"";if(firstTime&&opts.onCreate){opts.onCreate.call(tree,this,this.span);} +if(opts.onRender){opts.onRender.call(tree,this,this.span);}} +if((this.bExpanded||includeInvisible===true)&&this.childList){for(var i=0,l=this.childList.length;iy?1:-1;};cl.sort(cmp);if(deep){for(var i=0,l=cl.length;i0){this.childList[0].focus();}else{this.focus();}} +break;case DTNodeStatus_Loading:this.isLoading=true;$(this.span).addClass(this.tree.options.classNames.nodeLoading);if(!this.parent){this._setStatusNode({title:this.tree.options.strings.loading+info,tooltip:tooltip,addClass:this.tree.options.classNames.nodeWait});} +break;case DTNodeStatus_Error:this.isLoading=false;this._setStatusNode({title:this.tree.options.strings.loadError+info,tooltip:tooltip,addClass:this.tree.options.classNames.nodeError});break;default:throw"Bad LazyNodeStatus: '"+lts+"'.";}},_parentList:function(includeRoot,includeSelf){var l=[];var dtn=includeSelf?this:this.parent;while(dtn){if(includeRoot||dtn.parent){l.unshift(dtn);} +dtn=dtn.parent;} +return l;},getLevel:function(){var level=0;var dtn=this.parent;while(dtn){level++;dtn=dtn.parent;} +return level;},_getTypeForOuterNodeEvent:function(event){var cns=this.tree.options.classNames;var target=event.target;if(target.className.indexOf(cns.node)<0){return null;} +var eventX=event.pageX-target.offsetLeft;var eventY=event.pageY-target.offsetTop;for(var i=0,l=target.childNodes.length;i=x&&eventX<=(x+nx)&&eventY>=y&&eventY<=(y+ny)){if(cn.className==cns.title){return"title";}else if(cn.className==cns.expander){return"expander";}else if(cn.className==cns.checkbox){return"checkbox";}else if(cn.className==cns.nodeIcon){return"icon";}}} +return"prefix";},getEventTargetType:function(event){var tcn=event&&event.target?event.target.className:"";var cns=this.tree.options.classNames;if(tcn===cns.title){return"title";}else if(tcn===cns.expander){return"expander";}else if(tcn===cns.checkbox){return"checkbox";}else if(tcn===cns.nodeIcon){return"icon";}else if(tcn===cns.empty||tcn===cns.vline||tcn===cns.connector){return"prefix";}else if(tcn.indexOf(cns.node)>=0){return this._getTypeForOuterNodeEvent(event);} +return null;},isVisible:function(){var parents=this._parentList(true,false);for(var i=0,l=parents.length;ia").focus();}catch(e){}},isFocused:function(){return(this.tree.tnFocused===this);},_activate:function(flag,fireEvents){this.tree.logDebug("dtnode._activate(%o, fireEvents=%o) - %o",flag,fireEvents,this);var opts=this.tree.options;if(this.data.isStatusNode){return;} +if(fireEvents&&opts.onQueryActivate&&opts.onQueryActivate.call(this.tree,flag,this)===false){return;} +if(flag){if(this.tree.activeNode){if(this.tree.activeNode===this){return;} +this.tree.activeNode.deactivate();} +if(opts.activeVisible){this.makeVisible();} +this.tree.activeNode=this;if(opts.persist){$.cookie(opts.cookieId+"-active",this.data.key,opts.cookie);} +this.tree.persistence.activeKey=this.data.key;$(this.span).addClass(opts.classNames.active);if(fireEvents&&opts.onActivate){opts.onActivate.call(this.tree,this);}}else{if(this.tree.activeNode===this){if(opts.onQueryActivate&&opts.onQueryActivate.call(this.tree,false,this)===false){return;} +$(this.span).removeClass(opts.classNames.active);if(opts.persist){$.cookie(opts.cookieId+"-active","",opts.cookie);} +this.tree.persistence.activeKey=null;this.tree.activeNode=null;if(fireEvents&&opts.onDeactivate){opts.onDeactivate.call(this.tree,this);}}}},activate:function(){this._activate(true,true);},activateSilently:function(){this._activate(true,false);},deactivate:function(){this._activate(false,true);},isActive:function(){return(this.tree.activeNode===this);},_userActivate:function(){var activate=true;var expand=false;if(this.data.isFolder){switch(this.tree.options.clickFolderMode){case 2:activate=false;expand=true;break;case 3:activate=expand=true;break;}} +if(this.parent===null){expand=false;} +if(expand){this.toggleExpand();this.focus();} +if(activate){this.activate();}},_setSubSel:function(hasSubSel){if(hasSubSel){this.hasSubSel=true;$(this.span).addClass(this.tree.options.classNames.partsel);}else{this.hasSubSel=false;$(this.span).removeClass(this.tree.options.classNames.partsel);}},_updatePartSelectionState:function(){var sel;if(!this.hasChildren()){sel=(this.bSelected&&!this.data.unselectable&&!this.data.isStatusNode);this._setSubSel(false);return sel;} +var i,l,cl=this.childList,allSelected=true,allDeselected=true;for(i=0,l=cl.length;i=0;i--){sib=parents[i].getNextSibling();if(sib){break;}}} +if(sib){sib.focus();} +break;default:handled=false;} +if(handled){event.preventDefault();}},_onKeypress:function(event){},_onFocus:function(event){var opts=this.tree.options;if(event.type=="blur"||event.type=="focusout"){if(opts.onBlur){opts.onBlur.call(this.tree,this);} +if(this.tree.tnFocused){$(this.tree.tnFocused.span).removeClass(opts.classNames.focused);} +this.tree.tnFocused=null;if(opts.persist){$.cookie(opts.cookieId+"-focus","",opts.cookie);}}else if(event.type=="focus"||event.type=="focusin"){if(this.tree.tnFocused&&this.tree.tnFocused!==this){this.tree.logDebug("dtnode.onFocus: out of sync: curFocus: %o",this.tree.tnFocused);$(this.tree.tnFocused.span).removeClass(opts.classNames.focused);} +this.tree.tnFocused=this;if(opts.onFocus){opts.onFocus.call(this.tree,this);} +$(this.tree.tnFocused.span).addClass(opts.classNames.focused);if(opts.persist){$.cookie(opts.cookieId+"-focus",this.data.key,opts.cookie);}}},visit:function(fn,includeSelf){var res=true;if(includeSelf===true){res=fn(this);if(res===false||res=="skip"){return res;}} +if(this.childList){for(var i=0,l=this.childList.length;i reloading %s...",this,keyPath,child);var self=this;child.reloadChildren(function(node,isOk){if(isOk){tree.logDebug("%s._loadKeyPath(%s) -> reloaded %s.",node,keyPath,node);callback.call(tree,child,"loaded");node._loadKeyPath(segList.join(tree.options.keyPathSeparator),callback);}else{tree.logWarning("%s._loadKeyPath(%s) -> reloadChildren() failed.",self,keyPath);callback.call(tree,child,"error");}});}else{callback.call(tree,child,"loaded");child._loadKeyPath(segList.join(tree.options.keyPathSeparator),callback);} +return;}} +tree.logWarning("Node not found: "+seg);return;},resetLazy:function(){if(this.parent===null){throw"Use tree.reload() instead";}else if(!this.data.isLazy){throw"node.resetLazy() requires lazy nodes.";} +this.expand(false);this.removeChildren();},_addChildNode:function(dtnode,beforeNode){var tree=this.tree,opts=tree.options,pers=tree.persistence;dtnode.parent=this;if(this.childList===null){this.childList=[];}else if(!beforeNode){if(this.childList.length>0){$(this.childList[this.childList.length-1].span).removeClass(opts.classNames.lastsib);}} +if(beforeNode){var iBefore=$.inArray(beforeNode,this.childList);if(iBefore<0){throw" must be a child of ";} +this.childList.splice(iBefore,0,dtnode);}else{this.childList.push(dtnode);} +var isInitializing=tree.isInitializing();if(opts.persist&&pers.cookiesFound&&isInitializing){if(pers.activeKey===dtnode.data.key){tree.activeNode=dtnode;} +if(pers.focusedKey===dtnode.data.key){tree.focusNode=dtnode;} +dtnode.bExpanded=($.inArray(dtnode.data.key,pers.expandedKeyList)>=0);dtnode.bSelected=($.inArray(dtnode.data.key,pers.selectedKeyList)>=0);}else{if(dtnode.data.activate){tree.activeNode=dtnode;if(opts.persist){pers.activeKey=dtnode.data.key;}} +if(dtnode.data.focus){tree.focusNode=dtnode;if(opts.persist){pers.focusedKey=dtnode.data.key;}} +dtnode.bExpanded=(dtnode.data.expand===true);if(dtnode.bExpanded&&opts.persist){pers.addExpand(dtnode.data.key);} +dtnode.bSelected=(dtnode.data.select===true);if(dtnode.bSelected&&opts.persist){pers.addSelect(dtnode.data.key);}} +if(opts.minExpandLevel>=dtnode.getLevel()){this.bExpanded=true;} +if(dtnode.bSelected&&opts.selectMode==3){var p=this;while(p){if(!p.hasSubSel){p._setSubSel(true);} +p=p.parent;}} +if(tree.bEnableUpdate){this.render();} +return dtnode;},addChild:function(obj,beforeNode){if(typeof(obj)=="string"){throw"Invalid data type for "+obj;}else if(!obj||obj.length===0){return;}else if(obj instanceof DynaTreeNode){return this._addChildNode(obj,beforeNode);} +if(!obj.length){obj=[obj];} +var prevFlag=this.tree.enableUpdate(false);var tnFirst=null;for(var i=0,l=obj.length;i=0){this.expandedKeyList.splice(idx,1);$.cookie(this.cookieId+"-expand",this.expandedKeyList.join(","),this.cookieOpts);}},addSelect:function(key){if($.inArray(key,this.selectedKeyList)<0){this.selectedKeyList.push(key);$.cookie(this.cookieId+"-select",this.selectedKeyList.join(","),this.cookieOpts);}},clearSelect:function(key){var idx=$.inArray(key,this.selectedKeyList);if(idx>=0){this.selectedKeyList.splice(idx,1);$.cookie(this.cookieId+"-select",this.selectedKeyList.join(","),this.cookieOpts);}},isReloading:function(){return this.cookiesFound===true;},toDict:function(){return{cookiesFound:this.cookiesFound,activeKey:this.activeKey,focusedKey:this.activeKey,expandedKeyList:this.expandedKeyList,selectedKeyList:this.selectedKeyList};},lastentry:undefined};var DynaTree=Class.create();DynaTree.version="$Version: 1.2.0$";DynaTree.prototype={initialize:function($widget){this.phase="init";this.$widget=$widget;this.options=$widget.options;this.$tree=$widget.element;this.timer=null;this.divTree=this.$tree.get(0);_initDragAndDrop(this);},_load:function(callback){var $widget=this.$widget;var opts=this.options,self=this;this.bEnableUpdate=true;this._nodeCount=1;this.activeNode=null;this.focusNode=null;if(opts.rootVisible!==undefined){this.logWarning("Option 'rootVisible' is no longer supported.");} +if(opts.minExpandLevel<1){this.logWarning("Option 'minExpandLevel' must be >= 1.");opts.minExpandLevel=1;} +if(opts.classNames!==$.ui.dynatree.prototype.options.classNames){opts.classNames=$.extend({},$.ui.dynatree.prototype.options.classNames,opts.classNames);} +if(opts.ajaxDefaults!==$.ui.dynatree.prototype.options.ajaxDefaults){opts.ajaxDefaults=$.extend({},$.ui.dynatree.prototype.options.ajaxDefaults,opts.ajaxDefaults);} +if(opts.dnd!==$.ui.dynatree.prototype.options.dnd){opts.dnd=$.extend({},$.ui.dynatree.prototype.options.dnd,opts.dnd);} +if(!opts.imagePath){$("script").each(function(){var _rexDtLibName=/.*dynatree[^\/]*\.js$/i;if(this.src.search(_rexDtLibName)>=0){if(this.src.indexOf("/")>=0){opts.imagePath=this.src.slice(0,this.src.lastIndexOf("/"))+"/skin/";}else{opts.imagePath="skin/";} +self.logDebug("Guessing imagePath from '%s': '%s'",this.src,opts.imagePath);return false;}});} +this.persistence=new DynaTreeStatus(opts.cookieId,opts.cookie);if(opts.persist){if(!$.cookie){_log("warn","Please include jquery.cookie.js to use persistence.");} +this.persistence.read();} +this.logDebug("DynaTree.persistence: %o",this.persistence.toDict());this.cache={tagEmpty:"",tagVline:"",tagExpander:"",tagConnector:"",tagNodeIcon:"",tagCheckbox:"",lastentry:undefined};if(opts.children||(opts.initAjax&&opts.initAjax.url)||opts.initId){$(this.divTree).empty();} +var $ulInitialize=this.$tree.find(">ul:first").hide();this.tnRoot=new DynaTreeNode(null,this,{});this.tnRoot.bExpanded=true;this.tnRoot.render();this.divTree.appendChild(this.tnRoot.ul);var root=this.tnRoot;var isReloading=(opts.persist&&this.persistence.isReloading());var isLazy=false;var prevFlag=this.enableUpdate(false);this.logDebug("Dynatree._load(): read tree structure...");if(opts.children){root.addChild(opts.children);}else if(opts.initAjax&&opts.initAjax.url){isLazy=true;root.data.isLazy=true;this._reloadAjax(callback);}else if(opts.initId){this._createFromTag(root,$("#"+opts.initId));}else{this._createFromTag(root,$ulInitialize);$ulInitialize.remove();} +this._checkConsistency();if(!isLazy&&opts.selectMode==3){root._updatePartSelectionState();} +this.logDebug("Dynatree._load(): render nodes...");this.enableUpdate(prevFlag);this.logDebug("Dynatree._load(): bind events...");this.$widget.bind();this.logDebug("Dynatree._load(): postInit...");this.phase="postInit";if(opts.persist){this.persistence.write();} +if(this.focusNode&&this.focusNode.isVisible()){this.logDebug("Focus on init: %o",this.focusNode);this.focusNode.focus();} +if(!isLazy){if(opts.onPostInit){opts.onPostInit.call(this,isReloading,false);} +if(callback){callback.call(this,"ok");}} +this.phase="idle";},_reloadAjax:function(callback){var opts=this.options;if(!opts.initAjax||!opts.initAjax.url){throw"tree.reload() requires 'initAjax' mode.";} +var pers=this.persistence;var ajaxOpts=$.extend({},opts.initAjax);if(ajaxOpts.addActiveKey){ajaxOpts.data.activeKey=pers.activeKey;} +if(ajaxOpts.addFocusedKey){ajaxOpts.data.focusedKey=pers.focusedKey;} +if(ajaxOpts.addExpandedKeyList){ajaxOpts.data.expandedKeyList=pers.expandedKeyList.join(",");} +if(ajaxOpts.addSelectedKeyList){ajaxOpts.data.selectedKeyList=pers.selectedKeyList.join(",");} +if(ajaxOpts.success){this.logWarning("initAjax: success callback is ignored; use onPostInit instead.");} +if(ajaxOpts.error){this.logWarning("initAjax: error callback is ignored; use onPostInit instead.");} +var isReloading=pers.isReloading();ajaxOpts.success=function(dtnode,data,textStatus){if(opts.selectMode==3){dtnode.tree.tnRoot._updatePartSelectionState();} +if(opts.onPostInit){opts.onPostInit.call(dtnode.tree,isReloading,false);} +if(callback){callback.call(dtnode.tree,"ok");}};ajaxOpts.error=function(dtnode,XMLHttpRequest,textStatus,errorThrown){if(opts.onPostInit){opts.onPostInit.call(dtnode.tree,isReloading,true,XMLHttpRequest,textStatus,errorThrown);} +if(callback){callback.call(dtnode.tree,"error",XMLHttpRequest,textStatus,errorThrown);}};this.logDebug("Dynatree._init(): send Ajax request...");this.tnRoot.appendAjax(ajaxOpts);},toString:function(){return"Dynatree '"+this.$tree.attr("id")+"'";},toDict:function(){return this.tnRoot.toDict(true);},serializeArray:function(stopOnParents){var nodeList=this.getSelectedNodes(stopOnParents),name=this.$tree.attr("name")||this.$tree.attr("id"),arr=[];for(var i=0,l=nodeList.length;i=2){Array.prototype.unshift.apply(arguments,["debug"]);_log.apply(this,arguments);}},logInfo:function(msg){if(this.options.debugLevel>=1){Array.prototype.unshift.apply(arguments,["info"]);_log.apply(this,arguments);}},logWarning:function(msg){Array.prototype.unshift.apply(arguments,["warn"]);_log.apply(this,arguments);},isInitializing:function(){return(this.phase=="init"||this.phase=="postInit");},isReloading:function(){return(this.phase=="init"||this.phase=="postInit")&&this.options.persist&&this.persistence.cookiesFound;},isUserEvent:function(){return(this.phase=="userEvent");},redraw:function(){this.tnRoot.render(false,false);},renderInvisibleNodes:function(){this.tnRoot.render(false,true);},reload:function(callback){this._load(callback);},getRoot:function(){return this.tnRoot;},enable:function(){this.$widget.enable();},disable:function(){this.$widget.disable();},getNodeByKey:function(key){var el=document.getElementById(this.options.idPrefix+key);if(el){return el.dtnode?el.dtnode:null;} +var match=null;this.visit(function(node){if(node.data.key==key){match=node;return false;}},true);return match;},getActiveNode:function(){return this.activeNode;},reactivate:function(setFocus){var node=this.activeNode;if(node){this.activeNode=null;node.activate();if(setFocus){node.focus();}}},getSelectedNodes:function(stopOnParents){var nodeList=[];this.tnRoot.visit(function(node){if(node.bSelected){nodeList.push(node);if(stopOnParents===true){return"skip";}}});return nodeList;},activateKey:function(key){var dtnode=(key===null)?null:this.getNodeByKey(key);if(!dtnode){if(this.activeNode){this.activeNode.deactivate();} +this.activeNode=null;return null;} +dtnode.focus();dtnode.activate();return dtnode;},loadKeyPath:function(keyPath,callback){var segList=keyPath.split(this.options.keyPathSeparator);if(segList[0]===""){segList.shift();} +if(segList[0]==this.tnRoot.data.key){this.logDebug("Removed leading root key.");segList.shift();} +keyPath=segList.join(this.options.keyPathSeparator);return this.tnRoot._loadKeyPath(keyPath,callback);},selectKey:function(key,select){var dtnode=this.getNodeByKey(key);if(!dtnode){return null;} +dtnode.select(select);return dtnode;},enableUpdate:function(bEnable){if(this.bEnableUpdate==bEnable){return bEnable;} +this.bEnableUpdate=bEnable;if(bEnable){this.redraw();} +return!bEnable;},count:function(){return this.tnRoot.countChildren();},visit:function(fn,includeRoot){return this.tnRoot.visit(fn,includeRoot);},_createFromTag:function(parentTreeNode,$ulParent){var self=this;$ulParent.find(">li").each(function(){var $li=$(this),$liSpan=$li.find(">span:first"),$liA=$li.find(">a:first"),title,href=null,target=null,tooltip;if($liSpan.length){title=$liSpan.html();}else if($liA.length){title=$liA.html();href=$liA.attr("href");target=$liA.attr("target");tooltip=$liA.attr("title");}else{title=$li.html();var iPos=title.search(/
      =0){title=$.trim(title.substring(0,iPos));}else{title=$.trim(title);}} +var data={title:title,tooltip:tooltip,isFolder:$li.hasClass("folder"),isLazy:$li.hasClass("lazy"),expand:$li.hasClass("expanded"),select:$li.hasClass("selected"),activate:$li.hasClass("active"),focus:$li.hasClass("focused"),noLink:$li.hasClass("noLink")};if(href){data.href=href;data.target=target;} +if($li.attr("title")){data.tooltip=$li.attr("title");} +if($li.attr("id")){data.key=$li.attr("id");} +if($li.attr("data")){var dataAttr=$.trim($li.attr("data"));if(dataAttr){if(dataAttr.charAt(0)!="{"){dataAttr="{"+dataAttr+"}";} +try{$.extend(data,eval("("+dataAttr+")"));}catch(e){throw("Error parsing node data: "+e+"\ndata:\n'"+dataAttr+"'");}}} +var childNode=parentTreeNode.addChild(data);var $ul=$li.find(">ul:first");if($ul.length){self._createFromTag(childNode,$ul);}});},_checkConsistency:function(){},_setDndStatus:function(sourceNode,targetNode,helper,hitMode,accept){var $source=sourceNode?$(sourceNode.span):null,$target=$(targetNode.span);if(!this.$dndMarker){this.$dndMarker=$("
      ").hide().prependTo($(this.divTree).parent());} +if(hitMode==="after"||hitMode==="before"||hitMode==="over"){var pos=$target.offset();switch(hitMode){case"before":this.$dndMarker.removeClass("dynatree-drop-after dynatree-drop-over");this.$dndMarker.addClass("dynatree-drop-before");pos.top-=8;break;case"after":this.$dndMarker.removeClass("dynatree-drop-before dynatree-drop-over");this.$dndMarker.addClass("dynatree-drop-after");pos.top+=8;break;default:this.$dndMarker.removeClass("dynatree-drop-after dynatree-drop-before");this.$dndMarker.addClass("dynatree-drop-over");$target.addClass("dynatree-drop-target");pos.left+=8;} +this.$dndMarker.offset({left:pos.left,top:pos.top}).css({"z-index":1000}).show();}else{$target.removeClass("dynatree-drop-target");this.$dndMarker.hide();} +if(hitMode==="after"){$target.addClass("dynatree-drop-after");}else{$target.removeClass("dynatree-drop-after");} +if(hitMode==="before"){$target.addClass("dynatree-drop-before");}else{$target.removeClass("dynatree-drop-before");} +if(accept===true){if($source){$source.addClass("dynatree-drop-accept");} +$target.addClass("dynatree-drop-accept");helper.addClass("dynatree-drop-accept");}else{if($source){$source.removeClass("dynatree-drop-accept");} +$target.removeClass("dynatree-drop-accept");helper.removeClass("dynatree-drop-accept");} +if(accept===false){if($source){$source.addClass("dynatree-drop-reject");} +$target.addClass("dynatree-drop-reject");helper.addClass("dynatree-drop-reject");}else{if($source){$source.removeClass("dynatree-drop-reject");} +$target.removeClass("dynatree-drop-reject");helper.removeClass("dynatree-drop-reject");}},_onDragEvent:function(eventName,node,otherNode,event,ui,draggable){var opts=this.options;var dnd=this.options.dnd;var res=null;var nodeTag=$(node.span);var hitMode;switch(eventName){case"helper":var helper=$("
      ").append($(event.target).closest('a').clone());helper.data("dtSourceNode",node);res=helper;break;case"start":if(node.isStatusNode()){res=false;}else if(dnd.onDragStart){res=dnd.onDragStart(node);} +if(res===false){this.logDebug("tree.onDragStart() cancelled");ui.helper.trigger("mouseup");ui.helper.hide();}else{nodeTag.addClass("dynatree-drag-source");} +break;case"enter":res=dnd.onDragEnter?dnd.onDragEnter(node,otherNode):null;res={over:(res!==false)&&((res===true)||(res==="over")||$.inArray("over",res)>=0),before:(res!==false)&&((res===true)||(res==="before")||$.inArray("before",res)>=0),after:(res!==false)&&((res===true)||(res==="after")||$.inArray("after",res)>=0)};ui.helper.data("enterResponse",res);break;case"over":var enterResponse=ui.helper.data("enterResponse");hitMode=null;if(enterResponse===false){break;}else if(typeof enterResponse==="string"){hitMode=enterResponse;}else{var nodeOfs=nodeTag.offset();var relPos={x:event.pageX-nodeOfs.left,y:event.pageY-nodeOfs.top};var relPos2={x:relPos.x/nodeTag.width(),y:relPos.y/nodeTag.height()};if(enterResponse.after&&relPos2.y>0.75){hitMode="after";}else if(!enterResponse.over&&enterResponse.after&&relPos2.y>0.5){hitMode="after";}else if(enterResponse.before&&relPos2.y<=0.25){hitMode="before";}else if(!enterResponse.over&&enterResponse.before&&relPos2.y<=0.5){hitMode="before";}else if(enterResponse.over){hitMode="over";} +if(dnd.preventVoidMoves){if(node===otherNode){hitMode=null;}else if(hitMode==="before"&&otherNode&&node===otherNode.getNextSibling()){hitMode=null;}else if(hitMode==="after"&&otherNode&&node===otherNode.getPrevSibling()){hitMode=null;}else if(hitMode==="over"&&otherNode&&otherNode.parent===node&&otherNode.isLastSibling()){hitMode=null;}} +ui.helper.data("hitMode",hitMode);} +if(hitMode==="over"&&dnd.autoExpandMS&&node.hasChildren()!==false&&!node.bExpanded){node.scheduleAction("expand",dnd.autoExpandMS);} +if(hitMode&&dnd.onDragOver){res=dnd.onDragOver(node,otherNode,hitMode);} +this._setDndStatus(otherNode,node,ui.helper,hitMode,res!==false);break;case"drop":hitMode=ui.helper.data("hitMode");if(hitMode&&dnd.onDrop){dnd.onDrop(node,otherNode,hitMode,ui,draggable);} +break;case"leave":node.scheduleAction("cancel");ui.helper.data("enterResponse",null);ui.helper.data("hitMode",null);this._setDndStatus(otherNode,node,ui.helper,"out",undefined);if(dnd.onDragLeave){dnd.onDragLeave(node,otherNode);} +break;case"stop":nodeTag.removeClass("dynatree-drag-source");if(dnd.onDragStop){dnd.onDragStop(node);} +break;default:throw"Unsupported drag event: "+eventName;} +return res;},cancelDrag:function(){var dd=$.ui.ddmanager.current;if(dd){dd.cancel();}},lastentry:undefined};$.widget("ui.dynatree",{_init:function(){if(parseFloat($.ui.version)<1.8){if(this.options.debugLevel>=0){_log("warn","ui.dynatree._init() was called; you should upgrade to jquery.ui.core.js v1.8 or higher.");} +return this._create();} +if(this.options.debugLevel>=2){_log("debug","ui.dynatree._init() was called; no current default functionality.");}},_create:function(){var opts=this.options;if(opts.debugLevel>=1){logMsg("Dynatree._create(): version='%s', debugLevel=%o.",$.ui.dynatree.version,this.options.debugLevel);} +this.options.event+=".dynatree";var divTree=this.element.get(0);this.tree=new DynaTree(this);this.tree._load();this.tree.logDebug("Dynatree._init(): done.");},bind:function(){this.unbind();var eventNames="click.dynatree dblclick.dynatree";if(this.options.keyboard){eventNames+=" keypress.dynatree keydown.dynatree";} +this.element.bind(eventNames,function(event){var dtnode=getDtNodeFromElement(event.target);if(!dtnode){return true;} +var tree=dtnode.tree;var o=tree.options;tree.logDebug("event(%s): dtnode: %s",event.type,dtnode);var prevPhase=tree.phase;tree.phase="userEvent";try{switch(event.type){case"click":return(o.onClick&&o.onClick.call(tree,dtnode,event)===false)?false:dtnode._onClick(event);case"dblclick":return(o.onDblClick&&o.onDblClick.call(tree,dtnode,event)===false)?false:dtnode._onDblClick(event);case"keydown":return(o.onKeydown&&o.onKeydown.call(tree,dtnode,event)===false)?false:dtnode._onKeydown(event);case"keypress":return(o.onKeypress&&o.onKeypress.call(tree,dtnode,event)===false)?false:dtnode._onKeypress(event);}}catch(e){var _=null;tree.logWarning("bind(%o): dtnode: %o, error: %o",event,dtnode,e);}finally{tree.phase=prevPhase;}});function __focusHandler(event){event=$.event.fix(event||window.event);var dtnode=getDtNodeFromElement(event.target);return dtnode?dtnode._onFocus(event):false;} +var div=this.tree.divTree;if(div.addEventListener){div.addEventListener("focus",__focusHandler,true);div.addEventListener("blur",__focusHandler,true);}else{div.onfocusin=div.onfocusout=__focusHandler;}},unbind:function(){this.element.unbind(".dynatree");},enable:function(){this.bind();$.Widget.prototype.enable.apply(this,arguments);},disable:function(){this.unbind();$.Widget.prototype.disable.apply(this,arguments);},getTree:function(){return this.tree;},getRoot:function(){return this.tree.getRoot();},getActiveNode:function(){return this.tree.getActiveNode();},getSelectedNodes:function(){return this.tree.getSelectedNodes();},lastentry:undefined});if(parseFloat($.ui.version)<1.8){$.ui.dynatree.getter="getTree getRoot getActiveNode getSelectedNodes";} +$.ui.dynatree.version="$Version: 1.2.0$";$.ui.dynatree.getNode=function(el){if(el instanceof DynaTreeNode){return el;} +var $el=el.selector===undefined?$(el):el,parent=$el.parents("[dtnode]").first(),node;if(typeof parent.prop=="function"){node=parent.prop("dtnode");}else{node=parent.attr("dtnode");} +return node;} +$.ui.dynatree.getPersistData=DynaTreeStatus._getTreePersistData;$.ui.dynatree.prototype.options={title:"Dynatree",minExpandLevel:1,imagePath:null,children:null,initId:null,initAjax:null,autoFocus:true,keyboard:true,persist:false,autoCollapse:false,clickFolderMode:3,activeVisible:true,checkbox:false,selectMode:2,fx:null,noLink:false,onClick:null,onDblClick:null,onKeydown:null,onKeypress:null,onFocus:null,onBlur:null,onQueryActivate:null,onQuerySelect:null,onQueryExpand:null,onPostInit:null,onActivate:null,onDeactivate:null,onSelect:null,onExpand:null,onLazyRead:null,onCustomRender:null,onCreate:null,onRender:null,dnd:{onDragStart:null,onDragStop:null,autoExpandMS:1000,preventVoidMoves:true,onDragEnter:null,onDragOver:null,onDrop:null,onDragLeave:null},ajaxDefaults:{cache:false,dataType:"json"},strings:{loading:"Loading…",loadError:"Load error!"},generateIds:false,idPrefix:"dynatree-id-",keyPathSeparator:"/",cookieId:"dynatree",cookie:{expires:null},classNames:{container:"dynatree-container",node:"dynatree-node",folder:"dynatree-folder",empty:"dynatree-empty",vline:"dynatree-vline",expander:"dynatree-expander",connector:"dynatree-connector",checkbox:"dynatree-checkbox",nodeIcon:"dynatree-icon",title:"dynatree-title",noConnector:"dynatree-no-connector",nodeError:"dynatree-statusnode-error",nodeWait:"dynatree-statusnode-wait",hidden:"dynatree-hidden",combinedExpanderPrefix:"dynatree-exp-",combinedIconPrefix:"dynatree-ico-",nodeLoading:"dynatree-loading",hasChildren:"dynatree-has-children",active:"dynatree-active",selected:"dynatree-selected",expanded:"dynatree-expanded",lazy:"dynatree-lazy",focused:"dynatree-focused",partsel:"dynatree-partsel",lastsib:"dynatree-lastsib"},debugLevel:1,lastentry:undefined};if(parseFloat($.ui.version)<1.8){$.ui.dynatree.defaults=$.ui.dynatree.prototype.options;} +$.ui.dynatree.nodedatadefaults={title:null,key:null,isFolder:false,isLazy:false,tooltip:null,icon:null,addClass:null,noLink:false,activate:false,focus:false,expand:false,select:false,hideCheckbox:false,unselectable:false,children:null,lastentry:undefined};function _initDragAndDrop(tree){var dnd=tree.options.dnd||null;if(dnd&&(dnd.onDragStart||dnd.onDrop)){_registerDnd();} +if(dnd&&dnd.onDragStart){tree.$tree.draggable({addClasses:false,appendTo:"body",containment:false,delay:0,distance:4,revert:false,connectToDynatree:true,helper:function(event){var sourceNode=getDtNodeFromElement(event.target);return sourceNode.tree._onDragEvent("helper",sourceNode,null,event,null,null);},_last:null});} +if(dnd&&dnd.onDrop){tree.$tree.droppable({addClasses:false,tolerance:"intersect",greedy:false,_last:null});}} +var didRegisterDnd=false;var _registerDnd=function(){if(didRegisterDnd){return;} +$.ui.plugin.add("draggable","connectToDynatree",{start:function(event,ui){var draggable=$(this).data("draggable");var sourceNode=ui.helper.data("dtSourceNode")||null;if(sourceNode){draggable.offset.click.top=-2;draggable.offset.click.left=+16;return sourceNode.tree._onDragEvent("start",sourceNode,null,event,ui,draggable);}},drag:function(event,ui){var draggable=$(this).data("draggable");var sourceNode=ui.helper.data("dtSourceNode")||null;var prevTargetNode=ui.helper.data("dtTargetNode")||null;var targetNode=getDtNodeFromElement(event.target);if(event.target&&!targetNode){var isHelper=$(event.target).closest("div.dynatree-drag-helper,#dynatree-drop-marker").length>0;if(isHelper){return;}} +ui.helper.data("dtTargetNode",targetNode);if(prevTargetNode&&prevTargetNode!==targetNode){prevTargetNode.tree._onDragEvent("leave",prevTargetNode,sourceNode,event,ui,draggable);} +if(targetNode){if(!targetNode.tree.options.dnd.onDrop){noop();}else if(targetNode===prevTargetNode){targetNode.tree._onDragEvent("over",targetNode,sourceNode,event,ui,draggable);}else{targetNode.tree._onDragEvent("enter",targetNode,sourceNode,event,ui,draggable);}}},stop:function(event,ui){var draggable=$(this).data("draggable");var sourceNode=ui.helper.data("dtSourceNode")||null;var targetNode=ui.helper.data("dtTargetNode")||null;var mouseDownEvent=draggable._mouseDownEvent;var eventType=event.type;var dropped=(eventType=="mouseup"&&event.which==1);if(!dropped){logMsg("Drag was cancelled");} +if(targetNode){if(dropped){targetNode.tree._onDragEvent("drop",targetNode,sourceNode,event,ui,draggable);} +targetNode.tree._onDragEvent("leave",targetNode,sourceNode,event,ui,draggable);} if(sourceNode){sourceNode.tree._onDragEvent("stop",sourceNode,null,event,ui,draggable);}}});didRegisterDnd=true;};})(jQuery); \ No newline at end of file diff --git a/js/dynatree/skin-vista/ui.dynatree.css b/js/dynatree/skin-vista/ui.dynatree.css index c3d743de6..41cd4378b 100755 --- a/js/dynatree/skin-vista/ui.dynatree.css +++ b/js/dynatree/skin-vista/ui.dynatree.css @@ -1,456 +1,456 @@ -/******************************************************************************* - * Tree container - */ -ul.dynatree-container -{ - font-family: tahoma, arial, helvetica; - font-size: 10pt; /* font size should not be too big */ - white-space: nowrap; - padding: 3px; - margin: 0; /* issue 201 */ - - background-color: white; - border: 1px dotted gray; - - overflow: auto; -} - -ul.dynatree-container ul -{ - padding: 0 0 0 16px; - margin: 0; -} - -ul.dynatree-container li -{ - list-style-image: none; - list-style-position: outside; - list-style-type: none; - -moz-background-clip:border; - -moz-background-inline-policy: continuous; - -moz-background-origin: padding; - background-attachment: scroll; - background-color: transparent; - background-position: 0 0; - background-repeat: repeat-y; - background-image: none; /* no v-lines */ - - margin:0; - padding:1px 0 0 0; -} -/* Suppress lines for last child node */ -ul.dynatree-container li.dynatree-lastsib -{ - background-image: none; -} -/* Suppress lines if level is fixed expanded (option minExpandLevel) */ -ul.dynatree-no-connector > li -{ - background-image: none; -} - -/* Style, when control is disabled */ -.ui-dynatree-disabled ul.dynatree-container -{ - opacity: 0.5; -/* filter: alpha(opacity=50); /* Yields a css warning */ - background-color: silver; -} - - -/******************************************************************************* - * Common icon definitions - */ -span.dynatree-empty, -span.dynatree-vline, -span.dynatree-connector, -span.dynatree-expander, -span.dynatree-icon, -span.dynatree-checkbox, -span.dynatree-radio, -span.dynatree-drag-helper-img, -#dynatree-drop-marker -{ - width: 16px; - height: 16px; - display: -moz-inline-box; /* @ FF 1+2 */ - display: inline-block; /* Required to make a span sizeable */ - vertical-align: top; - background-repeat: no-repeat; - background-position: left; - background-image: url("icons.gif"); - background-position: 0 0; -} - -/** Used by 'icon' node option: */ -ul.dynatree-container img -{ - width: 16px; - height: 16px; - margin-left: 3px; - vertical-align: top; - border-style: none; -} - - -/******************************************************************************* - * Lines and connectors - */ - -/* -span.dynatree-empty -{ -} -span.dynatree-vline -{ -} -*/ -span.dynatree-connector -{ - background-image: none; -} -/* -.dynatree-lastsib span.dynatree-connector -{ -} -*/ -/******************************************************************************* - * Expander icon - * Note: IE6 doesn't correctly evaluate multiples class names, - * so we create combined class names that can be used in the CSS. - * - * Prefix: dynatree-exp- - * 1st character: 'e': expanded, 'c': collapsed - * 2nd character (optional): 'd': lazy (Delayed) - * 3rd character (optional): 'l': Last sibling - */ - -span.dynatree-expander -{ - background-position: 0px -80px; - cursor: pointer; -} -span.dynatree-expander:hover -{ - background-position: -16px -80px; -} -.dynatree-exp-cl span.dynatree-expander /* Collapsed, not delayed, last sibling */ -{ -} -.dynatree-exp-cd span.dynatree-expander /* Collapsed, delayed, not last sibling */ -{ -} -.dynatree-exp-cdl span.dynatree-expander /* Collapsed, delayed, last sibling */ -{ -} -.dynatree-exp-e span.dynatree-expander, /* Expanded, not delayed, not last sibling */ -.dynatree-exp-ed span.dynatree-expander, /* Expanded, delayed, not last sibling */ -.dynatree-exp-el span.dynatree-expander, /* Expanded, not delayed, last sibling */ -.dynatree-exp-edl span.dynatree-expander /* Expanded, delayed, last sibling */ -{ - background-position: -32px -80px; -} -.dynatree-exp-e span.dynatree-expander:hover, /* Expanded, not delayed, not last sibling */ -.dynatree-exp-ed span.dynatree-expander:hover, /* Expanded, delayed, not last sibling */ -.dynatree-exp-el span.dynatree-expander:hover, /* Expanded, not delayed, last sibling */ -.dynatree-exp-edl span.dynatree-expander:hover /* Expanded, delayed, last sibling */ -{ - background-position: -48px -80px; -} -.dynatree-loading span.dynatree-expander /* 'Loading' status overrides all others */ -{ - background-position: 0 0; - background-image: url("loading.gif"); -} - - -/******************************************************************************* - * Checkbox icon - */ -span.dynatree-checkbox -{ - margin-left: 3px; - background-position: 0px -32px; -} -span.dynatree-checkbox:hover -{ - background-position: -16px -32px; -} - -.dynatree-partsel span.dynatree-checkbox -{ - background-position: -64px -32px; -} -.dynatree-partsel span.dynatree-checkbox:hover -{ - background-position: -80px -32px; -} - -.dynatree-selected span.dynatree-checkbox -{ - background-position: -32px -32px; -} -.dynatree-selected span.dynatree-checkbox:hover -{ - background-position: -48px -32px; -} - -/******************************************************************************* - * Radiobutton icon - * This is a customization, that may be activated by overriding the 'checkbox' - * class name as 'dynatree-radio' in the tree options. - */ -span.dynatree-radio -{ - margin-left: 3px; - background-position: 0px -48px; -} -span.dynatree-radio:hover -{ - background-position: -16px -48px; -} - -.dynatree-partsel span.dynatree-radio -{ - background-position: -64px -48px; -} -.dynatree-partsel span.dynatree-radio:hover -{ - background-position: -80px -48px; -} - -.dynatree-selected span.dynatree-radio -{ - background-position: -32px -48px; -} -.dynatree-selected span.dynatree-radio:hover -{ - background-position: -48px -48px; -} - -/******************************************************************************* - * Node type icon - * Note: IE6 doesn't correctly evaluate multiples class names, - * so we create combined class names that can be used in the CSS. - * - * Prefix: dynatree-ico- - * 1st character: 'e': expanded, 'c': collapsed - * 2nd character (optional): 'f': folder - */ - -span.dynatree-icon /* Default icon */ -{ - margin-left: 3px; - background-position: 0px 0px; -} - -.dynatree-has-children span.dynatree-icon /* Default icon */ -{ -/* background-position: 0px -16px; */ -} - -.dynatree-ico-cf span.dynatree-icon /* Collapsed Folder */ -{ - background-position: 0px -16px; -} - -.dynatree-ico-ef span.dynatree-icon /* Expanded Folder */ -{ - background-position: -64px -16px; -} - -/* Status node icons */ - -.dynatree-statusnode-wait span.dynatree-icon -{ - background-image: url("loading.gif"); -} - -.dynatree-statusnode-error span.dynatree-icon -{ - background-position: 0px -112px; -/* background-image: url("ltError.gif");*/ -} - -/******************************************************************************* - * Node titles - */ - -/* @Chrome: otherwise hit area of node titles is broken (issue 133) - Removed again for issue 165; (133 couldn't be reproduced) */ -span.dynatree-node -{ - display: -moz-inline-box; /* issue 133, 165, 172, 192 */ -/* display: inline-block; /* Required to make a span sizeable */ -} - - -/* Remove blue color and underline from title links */ -ul.dynatree-container a -/*, ul.dynatree-container a:visited*/ -{ - color: black; /* inherit doesn't work on IE */ - text-decoration: none; - vertical-align: top; - margin: 0px; - margin-left: 3px; -/* outline: 0; /* @ Firefox, prevent dotted border after click */ - /* Set transparent border to prevent jumping when active node gets a border - (we can do this, because this theme doesn't use vertical lines) - */ - border: 1px solid white; /* Note: 'transparent' would not work in IE6 */ - -} - -ul.dynatree-container a:hover -{ -/* text-decoration: underline; */ - background: #F2F7FD; /* light blue */ - border-color: #B8D6FB; /* darker light blue */ -} - -span.dynatree-node a -{ - display: inline-block; /* Better alignment, when title contains
      */ -/* vertical-align: top;*/ - padding-left: 3px; - padding-right: 3px; /* Otherwise italic font will be outside bounds */ - /* line-height: 16px; /* should be the same as img height, in case 16 px */ -} -span.dynatree-folder a -{ -/* font-weight: bold; */ /* custom */ -} - -ul.dynatree-container a:focus, -span.dynatree-focused a:link /* @IE */ -{ - background-color: #EFEBDE; /* gray */ -} - -span.dynatree-has-children a -{ -/* font-style: oblique; /* custom: */ -} - -span.dynatree-expanded a -{ -} - -span.dynatree-selected a -{ -/* color: green; */ - font-style: italic; -} - -span.dynatree-active a -{ - border: 1px solid #99DEFD; - background-color: #D8F0FA; -} - -/******************************************************************************* - * Drag'n'drop support - */ - -/*** Helper object ************************************************************/ -div.dynatree-drag-helper -{ -} -div.dynatree-drag-helper a -{ - border: 1px solid gray; - background-color: white; - padding-left: 5px; - padding-right: 5px; - opacity: 0.8; -} -span.dynatree-drag-helper-img -{ - /* - position: relative; - left: -16px; - */ -} -div.dynatree-drag-helper /*.dynatree-drop-accept*/ -{ -/* border-color: green; - background-color: red;*/ -} -div.dynatree-drop-accept span.dynatree-drag-helper-img -{ - background-position: -32px -112px; -} -div.dynatree-drag-helper.dynatree-drop-reject -{ - border-color: red; -} -div.dynatree-drop-reject span.dynatree-drag-helper-img -{ - background-position: -16px -112px; -} - -/*** Drop marker icon *********************************************************/ - -#dynatree-drop-marker -{ - width: 24px; - position: absolute; - background-position: 0 -128px; -} -#dynatree-drop-marker.dynatree-drop-after, -#dynatree-drop-marker.dynatree-drop-before -{ - width:64px; - background-position: 0 -144px; -} -#dynatree-drop-marker.dynatree-drop-copy -{ - background-position: -64px -128px; -} -#dynatree-drop-marker.dynatree-drop-move -{ - background-position: -64px -128px; -} - -/*** Source node while dragging ***********************************************/ - -span.dynatree-drag-source -{ - /* border: 1px dotted gray; */ - background-color: #e0e0e0; -} -span.dynatree-drag-source a -{ - color: gray; -} - -/*** Target node while dragging cursor is over it *****************************/ - -span.dynatree-drop-target -{ - /*border: 1px solid gray;*/ -} -span.dynatree-drop-target a -{ - /*background-repeat: no-repeat; - background-position: right; - background-image: url("drop_child.gif");*/ -} -span.dynatree-drop-target.dynatree-drop-accept a -{ - /*border: 1px solid green;*/ - background-color: #3169C6 !important; - color: white !important; /* @ IE6 */ - text-decoration: none; -} -span.dynatree-drop-target.dynatree-drop-reject -{ - /*border: 1px solid red;*/ -} -span.dynatree-drop-target.dynatree-drop-after a -{ - /*background-repeat: repeat-x; - background-position: bottom; - background-image: url("drop_append.gif");*/ -} +/******************************************************************************* + * Tree container + */ +ul.dynatree-container +{ + font-family: tahoma, arial, helvetica; + font-size: 10pt; /* font size should not be too big */ + white-space: nowrap; + padding: 3px; + margin: 0; /* issue 201 */ + + background-color: white; + border: 1px dotted gray; + + overflow: auto; +} + +ul.dynatree-container ul +{ + padding: 0 0 0 16px; + margin: 0; +} + +ul.dynatree-container li +{ + list-style-image: none; + list-style-position: outside; + list-style-type: none; + -moz-background-clip:border; + -moz-background-inline-policy: continuous; + -moz-background-origin: padding; + background-attachment: scroll; + background-color: transparent; + background-position: 0 0; + background-repeat: repeat-y; + background-image: none; /* no v-lines */ + + margin:0; + padding:1px 0 0 0; +} +/* Suppress lines for last child node */ +ul.dynatree-container li.dynatree-lastsib +{ + background-image: none; +} +/* Suppress lines if level is fixed expanded (option minExpandLevel) */ +ul.dynatree-no-connector > li +{ + background-image: none; +} + +/* Style, when control is disabled */ +.ui-dynatree-disabled ul.dynatree-container +{ + opacity: 0.5; +/* filter: alpha(opacity=50); /* Yields a css warning */ + background-color: silver; +} + + +/******************************************************************************* + * Common icon definitions + */ +span.dynatree-empty, +span.dynatree-vline, +span.dynatree-connector, +span.dynatree-expander, +span.dynatree-icon, +span.dynatree-checkbox, +span.dynatree-radio, +span.dynatree-drag-helper-img, +#dynatree-drop-marker +{ + width: 16px; + height: 16px; + display: -moz-inline-box; /* @ FF 1+2 */ + display: inline-block; /* Required to make a span sizeable */ + vertical-align: top; + background-repeat: no-repeat; + background-position: left; + background-image: url("icons.gif"); + background-position: 0 0; +} + +/** Used by 'icon' node option: */ +ul.dynatree-container img +{ + width: 16px; + height: 16px; + margin-left: 3px; + vertical-align: top; + border-style: none; +} + + +/******************************************************************************* + * Lines and connectors + */ + +/* +span.dynatree-empty +{ +} +span.dynatree-vline +{ +} +*/ +span.dynatree-connector +{ + background-image: none; +} +/* +.dynatree-lastsib span.dynatree-connector +{ +} +*/ +/******************************************************************************* + * Expander icon + * Note: IE6 doesn't correctly evaluate multiples class names, + * so we create combined class names that can be used in the CSS. + * + * Prefix: dynatree-exp- + * 1st character: 'e': expanded, 'c': collapsed + * 2nd character (optional): 'd': lazy (Delayed) + * 3rd character (optional): 'l': Last sibling + */ + +span.dynatree-expander +{ + background-position: 0px -80px; + cursor: pointer; +} +span.dynatree-expander:hover +{ + background-position: -16px -80px; +} +.dynatree-exp-cl span.dynatree-expander /* Collapsed, not delayed, last sibling */ +{ +} +.dynatree-exp-cd span.dynatree-expander /* Collapsed, delayed, not last sibling */ +{ +} +.dynatree-exp-cdl span.dynatree-expander /* Collapsed, delayed, last sibling */ +{ +} +.dynatree-exp-e span.dynatree-expander, /* Expanded, not delayed, not last sibling */ +.dynatree-exp-ed span.dynatree-expander, /* Expanded, delayed, not last sibling */ +.dynatree-exp-el span.dynatree-expander, /* Expanded, not delayed, last sibling */ +.dynatree-exp-edl span.dynatree-expander /* Expanded, delayed, last sibling */ +{ + background-position: -32px -80px; +} +.dynatree-exp-e span.dynatree-expander:hover, /* Expanded, not delayed, not last sibling */ +.dynatree-exp-ed span.dynatree-expander:hover, /* Expanded, delayed, not last sibling */ +.dynatree-exp-el span.dynatree-expander:hover, /* Expanded, not delayed, last sibling */ +.dynatree-exp-edl span.dynatree-expander:hover /* Expanded, delayed, last sibling */ +{ + background-position: -48px -80px; +} +.dynatree-loading span.dynatree-expander /* 'Loading' status overrides all others */ +{ + background-position: 0 0; + background-image: url("loading.gif"); +} + + +/******************************************************************************* + * Checkbox icon + */ +span.dynatree-checkbox +{ + margin-left: 3px; + background-position: 0px -32px; +} +span.dynatree-checkbox:hover +{ + background-position: -16px -32px; +} + +.dynatree-partsel span.dynatree-checkbox +{ + background-position: -64px -32px; +} +.dynatree-partsel span.dynatree-checkbox:hover +{ + background-position: -80px -32px; +} + +.dynatree-selected span.dynatree-checkbox +{ + background-position: -32px -32px; +} +.dynatree-selected span.dynatree-checkbox:hover +{ + background-position: -48px -32px; +} + +/******************************************************************************* + * Radiobutton icon + * This is a customization, that may be activated by overriding the 'checkbox' + * class name as 'dynatree-radio' in the tree options. + */ +span.dynatree-radio +{ + margin-left: 3px; + background-position: 0px -48px; +} +span.dynatree-radio:hover +{ + background-position: -16px -48px; +} + +.dynatree-partsel span.dynatree-radio +{ + background-position: -64px -48px; +} +.dynatree-partsel span.dynatree-radio:hover +{ + background-position: -80px -48px; +} + +.dynatree-selected span.dynatree-radio +{ + background-position: -32px -48px; +} +.dynatree-selected span.dynatree-radio:hover +{ + background-position: -48px -48px; +} + +/******************************************************************************* + * Node type icon + * Note: IE6 doesn't correctly evaluate multiples class names, + * so we create combined class names that can be used in the CSS. + * + * Prefix: dynatree-ico- + * 1st character: 'e': expanded, 'c': collapsed + * 2nd character (optional): 'f': folder + */ + +span.dynatree-icon /* Default icon */ +{ + margin-left: 3px; + background-position: 0px 0px; +} + +.dynatree-has-children span.dynatree-icon /* Default icon */ +{ +/* background-position: 0px -16px; */ +} + +.dynatree-ico-cf span.dynatree-icon /* Collapsed Folder */ +{ + background-position: 0px -16px; +} + +.dynatree-ico-ef span.dynatree-icon /* Expanded Folder */ +{ + background-position: -64px -16px; +} + +/* Status node icons */ + +.dynatree-statusnode-wait span.dynatree-icon +{ + background-image: url("loading.gif"); +} + +.dynatree-statusnode-error span.dynatree-icon +{ + background-position: 0px -112px; +/* background-image: url("ltError.gif");*/ +} + +/******************************************************************************* + * Node titles + */ + +/* @Chrome: otherwise hit area of node titles is broken (issue 133) + Removed again for issue 165; (133 couldn't be reproduced) */ +span.dynatree-node +{ + display: -moz-inline-box; /* issue 133, 165, 172, 192 */ +/* display: inline-block; /* Required to make a span sizeable */ +} + + +/* Remove blue color and underline from title links */ +ul.dynatree-container a +/*, ul.dynatree-container a:visited*/ +{ + color: black; /* inherit doesn't work on IE */ + text-decoration: none; + vertical-align: top; + margin: 0px; + margin-left: 3px; +/* outline: 0; /* @ Firefox, prevent dotted border after click */ + /* Set transparent border to prevent jumping when active node gets a border + (we can do this, because this theme doesn't use vertical lines) + */ + border: 1px solid white; /* Note: 'transparent' would not work in IE6 */ + +} + +ul.dynatree-container a:hover +{ +/* text-decoration: underline; */ + background: #F2F7FD; /* light blue */ + border-color: #B8D6FB; /* darker light blue */ +} + +span.dynatree-node a +{ + display: inline-block; /* Better alignment, when title contains
      */ +/* vertical-align: top;*/ + padding-left: 3px; + padding-right: 3px; /* Otherwise italic font will be outside bounds */ + /* line-height: 16px; /* should be the same as img height, in case 16 px */ +} +span.dynatree-folder a +{ +/* font-weight: bold; */ /* custom */ +} + +ul.dynatree-container a:focus, +span.dynatree-focused a:link /* @IE */ +{ + background-color: #EFEBDE; /* gray */ +} + +span.dynatree-has-children a +{ +/* font-style: oblique; /* custom: */ +} + +span.dynatree-expanded a +{ +} + +span.dynatree-selected a +{ +/* color: green; */ + font-style: italic; +} + +span.dynatree-active a +{ + border: 1px solid #99DEFD; + background-color: #D8F0FA; +} + +/******************************************************************************* + * Drag'n'drop support + */ + +/*** Helper object ************************************************************/ +div.dynatree-drag-helper +{ +} +div.dynatree-drag-helper a +{ + border: 1px solid gray; + background-color: white; + padding-left: 5px; + padding-right: 5px; + opacity: 0.8; +} +span.dynatree-drag-helper-img +{ + /* + position: relative; + left: -16px; + */ +} +div.dynatree-drag-helper /*.dynatree-drop-accept*/ +{ +/* border-color: green; + background-color: red;*/ +} +div.dynatree-drop-accept span.dynatree-drag-helper-img +{ + background-position: -32px -112px; +} +div.dynatree-drag-helper.dynatree-drop-reject +{ + border-color: red; +} +div.dynatree-drop-reject span.dynatree-drag-helper-img +{ + background-position: -16px -112px; +} + +/*** Drop marker icon *********************************************************/ + +#dynatree-drop-marker +{ + width: 24px; + position: absolute; + background-position: 0 -128px; +} +#dynatree-drop-marker.dynatree-drop-after, +#dynatree-drop-marker.dynatree-drop-before +{ + width:64px; + background-position: 0 -144px; +} +#dynatree-drop-marker.dynatree-drop-copy +{ + background-position: -64px -128px; +} +#dynatree-drop-marker.dynatree-drop-move +{ + background-position: -64px -128px; +} + +/*** Source node while dragging ***********************************************/ + +span.dynatree-drag-source +{ + /* border: 1px dotted gray; */ + background-color: #e0e0e0; +} +span.dynatree-drag-source a +{ + color: gray; +} + +/*** Target node while dragging cursor is over it *****************************/ + +span.dynatree-drop-target +{ + /*border: 1px solid gray;*/ +} +span.dynatree-drop-target a +{ + /*background-repeat: no-repeat; + background-position: right; + background-image: url("drop_child.gif");*/ +} +span.dynatree-drop-target.dynatree-drop-accept a +{ + /*border: 1px solid green;*/ + background-color: #3169C6 !important; + color: white !important; /* @ IE6 */ + text-decoration: none; +} +span.dynatree-drop-target.dynatree-drop-reject +{ + /*border: 1px solid red;*/ +} +span.dynatree-drop-target.dynatree-drop-after a +{ + /*background-repeat: repeat-x; + background-position: bottom; + background-image: url("drop_append.gif");*/ +} diff --git a/js/fancybox/jquery.fancybox-1.3.4.css b/js/fancybox/jquery.fancybox-1.3.4.css index 6f53d8f4a..8fe0fbd5a 100755 --- a/js/fancybox/jquery.fancybox-1.3.4.css +++ b/js/fancybox/jquery.fancybox-1.3.4.css @@ -1,359 +1,359 @@ -/* - * FancyBox - jQuery Plugin - * Simple and fancy lightbox alternative - * - * Examples and documentation at: http://fancybox.net - * - * Copyright (c) 2008 - 2010 Janis Skarnelis - * That said, it is hardly a one-person project. Many people have submitted bugs, code, and offered their advice freely. Their support is greatly appreciated. - * - * Version: 1.3.4 (11/11/2010) - * Requires: jQuery v1.3+ - * - * Dual licensed under the MIT and GPL licenses: - * http://www.opensource.org/licenses/mit-license.php - * http://www.gnu.org/licenses/gpl.html - */ - -#fancybox-loading { - position: fixed; - top: 50%; - left: 50%; - width: 40px; - height: 40px; - margin-top: -20px; - margin-left: -20px; - cursor: pointer; - overflow: hidden; - z-index: 1104; - display: none; -} - -#fancybox-loading div { - position: absolute; - top: 0; - left: 0; - width: 40px; - height: 480px; - background-image: url('fancybox.png'); -} - -#fancybox-overlay { - position: absolute; - top: 0; - left: 0; - width: 100%; - z-index: 1100; - display: none; -} - -#fancybox-tmp { - padding: 0; - margin: 0; - border: 0; - overflow: auto; - display: none; -} - -#fancybox-wrap { - position: absolute; - top: 0; - left: 0; - padding: 20px; - z-index: 1101; - outline: none; - display: none; -} - -#fancybox-outer { - position: relative; - width: 100%; - height: 100%; - background: #fff; -} - -#fancybox-content { - width: 0; - height: 0; - padding: 0; - outline: none; - position: relative; - overflow: hidden; - z-index: 1102; - border: 0px solid #fff; -} - -#fancybox-hide-sel-frame { - position: absolute; - top: 0; - left: 0; - width: 100%; - height: 100%; - background: transparent; - z-index: 1101; -} - -#fancybox-close { - position: absolute; - top: -15px; - right: -15px; - width: 30px; - height: 30px; - background: transparent url('fancybox.png') -40px 0px; - cursor: pointer; - z-index: 1103; - display: none; -} - -#fancybox-error { - color: #444; - font: normal 12px/20px Arial; - padding: 14px; - margin: 0; -} - -#fancybox-img { - width: 100%; - height: 100%; - padding: 0; - margin: 0; - border: none; - outline: none; - line-height: 0; - vertical-align: top; -} - -#fancybox-frame { - width: 100%; - height: 100%; - border: none; - display: block; -} - -#fancybox-left, #fancybox-right { - position: absolute; - bottom: 0px; - height: 100%; - width: 35%; - cursor: pointer; - outline: none; - background: transparent url('blank.gif'); - z-index: 1102; - display: none; -} - -#fancybox-left { - left: 0px; -} - -#fancybox-right { - right: 0px; -} - -#fancybox-left-ico, #fancybox-right-ico { - position: absolute; - top: 50%; - left: -9999px; - width: 30px; - height: 30px; - margin-top: -15px; - cursor: pointer; - z-index: 1102; - display: block; -} - -#fancybox-left-ico { - background-image: url('fancybox.png'); - background-position: -40px -30px; -} - -#fancybox-right-ico { - background-image: url('fancybox.png'); - background-position: -40px -60px; -} - -#fancybox-left:hover, #fancybox-right:hover { - visibility: visible; /* IE6 */ -} - -#fancybox-left:hover span { - left: 20px; -} - -#fancybox-right:hover span { - left: auto; - right: 20px; -} - -.fancybox-bg { - position: absolute; - padding: 0; - margin: 0; - border: 0; - width: 20px; - height: 20px; - z-index: 1001; -} - -#fancybox-bg-n { - top: -20px; - left: 0; - width: 100%; - background-image: url('fancybox-x.png'); -} - -#fancybox-bg-ne { - top: -20px; - right: -20px; - background-image: url('fancybox.png'); - background-position: -40px -162px; -} - -#fancybox-bg-e { - top: 0; - right: -20px; - height: 100%; - background-image: url('fancybox-y.png'); - background-position: -20px 0px; -} - -#fancybox-bg-se { - bottom: -20px; - right: -20px; - background-image: url('fancybox.png'); - background-position: -40px -182px; -} - -#fancybox-bg-s { - bottom: -20px; - left: 0; - width: 100%; - background-image: url('fancybox-x.png'); - background-position: 0px -20px; -} - -#fancybox-bg-sw { - bottom: -20px; - left: -20px; - background-image: url('fancybox.png'); - background-position: -40px -142px; -} - -#fancybox-bg-w { - top: 0; - left: -20px; - height: 100%; - background-image: url('fancybox-y.png'); -} - -#fancybox-bg-nw { - top: -20px; - left: -20px; - background-image: url('fancybox.png'); - background-position: -40px -122px; -} - -#fancybox-title { - font-family: Helvetica; - font-size: 12px; - z-index: 1102; -} - -.fancybox-title-inside { - padding-bottom: 10px; - text-align: center; - color: #333; - background: #fff; - position: relative; -} - -.fancybox-title-outside { - padding-top: 10px; - color: #fff; -} - -.fancybox-title-over { - position: absolute; - bottom: 0; - left: 0; - color: #FFF; - text-align: left; -} - -#fancybox-title-over { - padding: 10px; - background-image: url('fancy_title_over.png'); - display: block; -} - -.fancybox-title-float { - position: absolute; - left: 0; - bottom: -20px; - height: 32px; -} - -#fancybox-title-float-wrap { - border: none; - border-collapse: collapse; - width: auto; -} - -#fancybox-title-float-wrap td { - border: none; - white-space: nowrap; -} - -#fancybox-title-float-left { - padding: 0 0 0 15px; - background: url('fancybox.png') -40px -90px no-repeat; -} - -#fancybox-title-float-main { - color: #FFF; - line-height: 29px; - font-weight: bold; - padding: 0 0 3px 0; - background: url('fancybox-x.png') 0px -40px; -} - -#fancybox-title-float-right { - padding: 0 0 0 15px; - background: url('fancybox.png') -55px -90px no-repeat; -} - -/* IE6 */ - -.fancybox-ie6 #fancybox-close { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_close.png', sizingMethod='scale'); } - -.fancybox-ie6 #fancybox-left-ico { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_nav_left.png', sizingMethod='scale'); } -.fancybox-ie6 #fancybox-right-ico { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_nav_right.png', sizingMethod='scale'); } - -.fancybox-ie6 #fancybox-title-over { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_title_over.png', sizingMethod='scale'); zoom: 1; } -.fancybox-ie6 #fancybox-title-float-left { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_title_left.png', sizingMethod='scale'); } -.fancybox-ie6 #fancybox-title-float-main { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_title_main.png', sizingMethod='scale'); } -.fancybox-ie6 #fancybox-title-float-right { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_title_right.png', sizingMethod='scale'); } - -.fancybox-ie6 #fancybox-bg-w, .fancybox-ie6 #fancybox-bg-e, .fancybox-ie6 #fancybox-left, .fancybox-ie6 #fancybox-right, #fancybox-hide-sel-frame { - height: expression(this.parentNode.clientHeight + "px"); -} - -#fancybox-loading.fancybox-ie6 { - position: absolute; margin-top: 0; - top: expression( (-20 + (document.documentElement.clientHeight ? document.documentElement.clientHeight/2 : document.body.clientHeight/2 ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop )) + 'px'); -} - -#fancybox-loading.fancybox-ie6 div { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_loading.png', sizingMethod='scale'); } - -/* IE6, IE7, IE8 */ - -.fancybox-ie .fancybox-bg { background: transparent !important; } - -.fancybox-ie #fancybox-bg-n { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_n.png', sizingMethod='scale'); } -.fancybox-ie #fancybox-bg-ne { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_ne.png', sizingMethod='scale'); } -.fancybox-ie #fancybox-bg-e { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_e.png', sizingMethod='scale'); } -.fancybox-ie #fancybox-bg-se { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_se.png', sizingMethod='scale'); } -.fancybox-ie #fancybox-bg-s { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_s.png', sizingMethod='scale'); } -.fancybox-ie #fancybox-bg-sw { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_sw.png', sizingMethod='scale'); } -.fancybox-ie #fancybox-bg-w { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_w.png', sizingMethod='scale'); } +/* + * FancyBox - jQuery Plugin + * Simple and fancy lightbox alternative + * + * Examples and documentation at: http://fancybox.net + * + * Copyright (c) 2008 - 2010 Janis Skarnelis + * That said, it is hardly a one-person project. Many people have submitted bugs, code, and offered their advice freely. Their support is greatly appreciated. + * + * Version: 1.3.4 (11/11/2010) + * Requires: jQuery v1.3+ + * + * Dual licensed under the MIT and GPL licenses: + * http://www.opensource.org/licenses/mit-license.php + * http://www.gnu.org/licenses/gpl.html + */ + +#fancybox-loading { + position: fixed; + top: 50%; + left: 50%; + width: 40px; + height: 40px; + margin-top: -20px; + margin-left: -20px; + cursor: pointer; + overflow: hidden; + z-index: 1104; + display: none; +} + +#fancybox-loading div { + position: absolute; + top: 0; + left: 0; + width: 40px; + height: 480px; + background-image: url('fancybox.png'); +} + +#fancybox-overlay { + position: absolute; + top: 0; + left: 0; + width: 100%; + z-index: 1100; + display: none; +} + +#fancybox-tmp { + padding: 0; + margin: 0; + border: 0; + overflow: auto; + display: none; +} + +#fancybox-wrap { + position: absolute; + top: 0; + left: 0; + padding: 20px; + z-index: 1101; + outline: none; + display: none; +} + +#fancybox-outer { + position: relative; + width: 100%; + height: 100%; + background: #fff; +} + +#fancybox-content { + width: 0; + height: 0; + padding: 0; + outline: none; + position: relative; + overflow: hidden; + z-index: 1102; + border: 0px solid #fff; +} + +#fancybox-hide-sel-frame { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + background: transparent; + z-index: 1101; +} + +#fancybox-close { + position: absolute; + top: -15px; + right: -15px; + width: 30px; + height: 30px; + background: transparent url('fancybox.png') -40px 0px; + cursor: pointer; + z-index: 1103; + display: none; +} + +#fancybox-error { + color: #444; + font: normal 12px/20px Arial; + padding: 14px; + margin: 0; +} + +#fancybox-img { + width: 100%; + height: 100%; + padding: 0; + margin: 0; + border: none; + outline: none; + line-height: 0; + vertical-align: top; +} + +#fancybox-frame { + width: 100%; + height: 100%; + border: none; + display: block; +} + +#fancybox-left, #fancybox-right { + position: absolute; + bottom: 0px; + height: 100%; + width: 35%; + cursor: pointer; + outline: none; + background: transparent url('blank.gif'); + z-index: 1102; + display: none; +} + +#fancybox-left { + left: 0px; +} + +#fancybox-right { + right: 0px; +} + +#fancybox-left-ico, #fancybox-right-ico { + position: absolute; + top: 50%; + left: -9999px; + width: 30px; + height: 30px; + margin-top: -15px; + cursor: pointer; + z-index: 1102; + display: block; +} + +#fancybox-left-ico { + background-image: url('fancybox.png'); + background-position: -40px -30px; +} + +#fancybox-right-ico { + background-image: url('fancybox.png'); + background-position: -40px -60px; +} + +#fancybox-left:hover, #fancybox-right:hover { + visibility: visible; /* IE6 */ +} + +#fancybox-left:hover span { + left: 20px; +} + +#fancybox-right:hover span { + left: auto; + right: 20px; +} + +.fancybox-bg { + position: absolute; + padding: 0; + margin: 0; + border: 0; + width: 20px; + height: 20px; + z-index: 1001; +} + +#fancybox-bg-n { + top: -20px; + left: 0; + width: 100%; + background-image: url('fancybox-x.png'); +} + +#fancybox-bg-ne { + top: -20px; + right: -20px; + background-image: url('fancybox.png'); + background-position: -40px -162px; +} + +#fancybox-bg-e { + top: 0; + right: -20px; + height: 100%; + background-image: url('fancybox-y.png'); + background-position: -20px 0px; +} + +#fancybox-bg-se { + bottom: -20px; + right: -20px; + background-image: url('fancybox.png'); + background-position: -40px -182px; +} + +#fancybox-bg-s { + bottom: -20px; + left: 0; + width: 100%; + background-image: url('fancybox-x.png'); + background-position: 0px -20px; +} + +#fancybox-bg-sw { + bottom: -20px; + left: -20px; + background-image: url('fancybox.png'); + background-position: -40px -142px; +} + +#fancybox-bg-w { + top: 0; + left: -20px; + height: 100%; + background-image: url('fancybox-y.png'); +} + +#fancybox-bg-nw { + top: -20px; + left: -20px; + background-image: url('fancybox.png'); + background-position: -40px -122px; +} + +#fancybox-title { + font-family: Helvetica; + font-size: 12px; + z-index: 1102; +} + +.fancybox-title-inside { + padding-bottom: 10px; + text-align: center; + color: #333; + background: #fff; + position: relative; +} + +.fancybox-title-outside { + padding-top: 10px; + color: #fff; +} + +.fancybox-title-over { + position: absolute; + bottom: 0; + left: 0; + color: #FFF; + text-align: left; +} + +#fancybox-title-over { + padding: 10px; + background-image: url('fancy_title_over.png'); + display: block; +} + +.fancybox-title-float { + position: absolute; + left: 0; + bottom: -20px; + height: 32px; +} + +#fancybox-title-float-wrap { + border: none; + border-collapse: collapse; + width: auto; +} + +#fancybox-title-float-wrap td { + border: none; + white-space: nowrap; +} + +#fancybox-title-float-left { + padding: 0 0 0 15px; + background: url('fancybox.png') -40px -90px no-repeat; +} + +#fancybox-title-float-main { + color: #FFF; + line-height: 29px; + font-weight: bold; + padding: 0 0 3px 0; + background: url('fancybox-x.png') 0px -40px; +} + +#fancybox-title-float-right { + padding: 0 0 0 15px; + background: url('fancybox.png') -55px -90px no-repeat; +} + +/* IE6 */ + +.fancybox-ie6 #fancybox-close { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_close.png', sizingMethod='scale'); } + +.fancybox-ie6 #fancybox-left-ico { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_nav_left.png', sizingMethod='scale'); } +.fancybox-ie6 #fancybox-right-ico { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_nav_right.png', sizingMethod='scale'); } + +.fancybox-ie6 #fancybox-title-over { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_title_over.png', sizingMethod='scale'); zoom: 1; } +.fancybox-ie6 #fancybox-title-float-left { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_title_left.png', sizingMethod='scale'); } +.fancybox-ie6 #fancybox-title-float-main { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_title_main.png', sizingMethod='scale'); } +.fancybox-ie6 #fancybox-title-float-right { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_title_right.png', sizingMethod='scale'); } + +.fancybox-ie6 #fancybox-bg-w, .fancybox-ie6 #fancybox-bg-e, .fancybox-ie6 #fancybox-left, .fancybox-ie6 #fancybox-right, #fancybox-hide-sel-frame { + height: expression(this.parentNode.clientHeight + "px"); +} + +#fancybox-loading.fancybox-ie6 { + position: absolute; margin-top: 0; + top: expression( (-20 + (document.documentElement.clientHeight ? document.documentElement.clientHeight/2 : document.body.clientHeight/2 ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop )) + 'px'); +} + +#fancybox-loading.fancybox-ie6 div { background: transparent; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_loading.png', sizingMethod='scale'); } + +/* IE6, IE7, IE8 */ + +.fancybox-ie .fancybox-bg { background: transparent !important; } + +.fancybox-ie #fancybox-bg-n { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_n.png', sizingMethod='scale'); } +.fancybox-ie #fancybox-bg-ne { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_ne.png', sizingMethod='scale'); } +.fancybox-ie #fancybox-bg-e { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_e.png', sizingMethod='scale'); } +.fancybox-ie #fancybox-bg-se { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_se.png', sizingMethod='scale'); } +.fancybox-ie #fancybox-bg-s { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_s.png', sizingMethod='scale'); } +.fancybox-ie #fancybox-bg-sw { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_sw.png', sizingMethod='scale'); } +.fancybox-ie #fancybox-bg-w { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_w.png', sizingMethod='scale'); } .fancybox-ie #fancybox-bg-nw { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_nw.png', sizingMethod='scale'); } \ No newline at end of file diff --git a/js/jquery/jquery.hotkeys.js b/js/jquery/jquery.hotkeys.js index 401458765..f814c30af 100755 --- a/js/jquery/jquery.hotkeys.js +++ b/js/jquery/jquery.hotkeys.js @@ -1,99 +1,99 @@ -/* - * jQuery Hotkeys Plugin - * Copyright 2010, John Resig - * Dual licensed under the MIT or GPL Version 2 licenses. - * - * Based upon the plugin by Tzury Bar Yochay: - * http://github.com/tzuryby/hotkeys - * - * Original idea by: - * Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/ -*/ - -(function(jQuery){ - - jQuery.hotkeys = { - version: "0.8", - - specialKeys: { - 8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause", - 20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home", - 37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del", - 96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7", - 104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/", - 112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8", - 120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 191: "/", 224: "meta" - }, - - shiftNums: { - "`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&", - "8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<", - ".": ">", "/": "?", "\\": "|" - } - }; - - function keyHandler( handleObj ) { - // Only care when a possible input has been specified - if ( typeof handleObj.data !== "string" ) { - return; - } - - var origHandler = handleObj.handler, - keys = handleObj.data.toLowerCase().split(" "); - - handleObj.handler = function( event ) { - // Don't fire in text-accepting inputs that we didn't directly bind to - if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) || - event.target.type === "text" || event.target.type === "password") ) { - return; - } - - // Keypress represents characters, not special keys - var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ], - character = String.fromCharCode( event.which ).toLowerCase(), - key, modif = "", possible = {}; - - // check combinations (alt|ctrl|shift+anything) - if ( event.altKey && special !== "alt" ) { - modif += "alt+"; - } - - if ( event.ctrlKey && special !== "ctrl" ) { - modif += "ctrl+"; - } - - // TODO: Need to make sure this works consistently across platforms - if ( event.metaKey && !event.ctrlKey && special !== "meta" ) { - modif += "meta+"; - } - - if ( event.shiftKey && special !== "shift" ) { - modif += "shift+"; - } - - if ( special ) { - possible[ modif + special ] = true; - - } else { - possible[ modif + character ] = true; - possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true; - - // "$" can be triggered as "Shift+4" or "Shift+$" or just "$" - if ( modif === "shift+" ) { - possible[ jQuery.hotkeys.shiftNums[ character ] ] = true; - } - } - - for ( var i = 0, l = keys.length; i < l; i++ ) { - if ( possible[ keys[i] ] ) { - return origHandler.apply( this, arguments ); - } - } - }; - } - - jQuery.each([ "keydown", "keyup", "keypress" ], function() { - jQuery.event.special[ this ] = { add: keyHandler }; - }); - +/* + * jQuery Hotkeys Plugin + * Copyright 2010, John Resig + * Dual licensed under the MIT or GPL Version 2 licenses. + * + * Based upon the plugin by Tzury Bar Yochay: + * http://github.com/tzuryby/hotkeys + * + * Original idea by: + * Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/ +*/ + +(function(jQuery){ + + jQuery.hotkeys = { + version: "0.8", + + specialKeys: { + 8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause", + 20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home", + 37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del", + 96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7", + 104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/", + 112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8", + 120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 191: "/", 224: "meta" + }, + + shiftNums: { + "`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&", + "8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<", + ".": ">", "/": "?", "\\": "|" + } + }; + + function keyHandler( handleObj ) { + // Only care when a possible input has been specified + if ( typeof handleObj.data !== "string" ) { + return; + } + + var origHandler = handleObj.handler, + keys = handleObj.data.toLowerCase().split(" "); + + handleObj.handler = function( event ) { + // Don't fire in text-accepting inputs that we didn't directly bind to + if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) || + event.target.type === "text" || event.target.type === "password") ) { + return; + } + + // Keypress represents characters, not special keys + var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ], + character = String.fromCharCode( event.which ).toLowerCase(), + key, modif = "", possible = {}; + + // check combinations (alt|ctrl|shift+anything) + if ( event.altKey && special !== "alt" ) { + modif += "alt+"; + } + + if ( event.ctrlKey && special !== "ctrl" ) { + modif += "ctrl+"; + } + + // TODO: Need to make sure this works consistently across platforms + if ( event.metaKey && !event.ctrlKey && special !== "meta" ) { + modif += "meta+"; + } + + if ( event.shiftKey && special !== "shift" ) { + modif += "shift+"; + } + + if ( special ) { + possible[ modif + special ] = true; + + } else { + possible[ modif + character ] = true; + possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true; + + // "$" can be triggered as "Shift+4" or "Shift+$" or just "$" + if ( modif === "shift+" ) { + possible[ jQuery.hotkeys.shiftNums[ character ] ] = true; + } + } + + for ( var i = 0, l = keys.length; i < l; i++ ) { + if ( possible[ keys[i] ] ) { + return origHandler.apply( this, arguments ); + } + } + }; + } + + jQuery.each([ "keydown", "keyup", "keypress" ], function() { + jQuery.event.special[ this ] = { add: keyHandler }; + }); + })( jQuery ); \ No newline at end of file diff --git a/js/posting/posting.js b/js/posting/posting.js index 0f6e279c2..b594ce2eb 100755 --- a/js/posting/posting.js +++ b/js/posting/posting.js @@ -1,215 +1,215 @@ -function spotPosting() { - this.commentForm = null; - this.reportForm = null; - this.uiStart = null; - this.uiDone = null; - - this.cbHashcashCalculated = function (self, hash) { - self.commentForm['postcommentform[newmessageid]'].value = hash; - self.commentForm['postcommentform[submitpost]'].value = 'Post'; - self.uiDone(); - - // convert the processed form to post values - var dataString = $(self.commentForm).serialize() - - // and actually process the call - $.ajax({ - type: "POST", - url: "?page=postcomment", - dataType: "xml", - data: dataString, - success: function(xml) { - var result = $(xml).find('result').text(); - if(result == 'success') { - var user = $(xml).find('user').text(); - var spotterid = $(xml).find('spotterid').text(); - var rating = $(xml).find('rating').text(); - var text = $(xml).find('body').text(); - var spotteridurl = 'http://'+window.location.hostname+window.location.pathname+'?search[tree]=&search[type]=SpotterID&search[text]='+spotterid; - var commentimage = $(xml).find('commentimage').text(); - - var data = "
    • Posted by %1".replace("%1", ""+user+"") + " ("+spotterid+") @ just now
      "+text+"
    • "; - - $("li.nocomments").remove(); - $("li.firstComment").removeClass("firstComment"); - $("li.addComment").after(data).next().hide().addClass("firstComment").fadeIn(function(){ - $("#commentslist > li").removeClass("even"); - $("#commentslist > li:nth-child(even)").addClass('even'); - $("span.commentcount").html('# '+$("#commentslist").children().not(".addComment").size()); - }); - } - }, - error: function(xml) { - console.log('error: '+((new XMLSerializer()).serializeToString(xml))); - } - }); - } // cbHashcashCalculated - - this.rpHashcashCalculated = function (self, hash) { - self.reportForm['postreportform[newmessageid]'].value = hash; - self.reportForm['postreportform[submitpost]'].value = 'Post'; - self.uiDone(); - - var dataString2 = $(self.reportForm).serialize() - - $.ajax({ - type: "POST", - url: "?page=reportpost", - dataType: "xml", - data: dataString2, - success: function(xml) { - var result = $(xml).find('result').text(); - var errors = $(xml).find('errors').text(); - - if(result != 'success') { - console.log('error: '+((new XMLSerializer()).serializeToString(xml))); - - $(".spamreport-button").attr('title', result + ': ' + errors); - alert('Marking as spam was not successfull: ' + errors); - } // else - }, - error: function(xml) { - console.log('error: '+((new XMLSerializer()).serializeToString(xml))); - } - }); - } // callback rpHashcashCalculated - - this.spotHashcashCalculated = function (self, hash) { - // and enter the form's inputfields - self.newSpotForm['newspotform[newmessageid]'].value = hash; - self.newSpotForm['newspotform[submitpost]'].value = 'Post'; - self.uiDone(); - - $(self.newSpotForm).ajaxSubmit({ - type: "POST", - url: "?page=postspot", - dataType: "xml", - success: function(xml) { - var $dialdiv = $("#editdialogdiv") - var result = $(xml).find('result').text(); - - var $formerrors = $dialdiv.find("ul.formerrors"); - $formerrors.empty(); - var $forminfo = $dialdiv.find("ul.forminformation"); - $forminfo.empty(); - - if (result == 'success') { - // zet de information van het formulier in de infolijst - $('info', xml).each(function() { - $forminfo.append("
    • " + $(this).text() + "
    • "); - }); // each - - /** - * We succesfully posted a new spot, now clear the title field - * and body. This makes the other field stay in tact, so if a - * user is posting several of the same type of spots, he doesn't - * have to enter everything again - */ - $("input[name='newspotform[title]']").val(''); - $("textarea[name='newspotform[body]']").val(''); - $("input[name='newspotform[nzbfile]']").val(''); - $("input[name='newspotform[imagefile]']").val(''); - } else { - // voeg nu de errors in de html - // zet de errors van het formulier in de errorlijst - $('errors', xml).each(function() { - $formerrors.append("
    • " + $(this).text() + "
    • "); - }); // each - } // if post was not succesful - }, // success() - error: function(xml) { - console.log('error: '+((new XMLSerializer()).serializeToString(xml))); - } - }); - } // callback spotHashcashCalculated - - this.postComment = function(commentForm, uiStart, uiDone) { - this.commentForm = commentForm; - this.uiStart = uiStart; - this.uiDone = uiDone; - - // update the UI - this.uiStart(); - - // First retrieve some values from the form we are submitting - var randomstr = commentForm['postcommentform[randomstr]'].value; - var rating = commentForm['postcommentform[rating]'].value; - - // inreplyto is the whole messageid, we strip off the @ part to add - // our own stuff - var inreplyto = commentForm['postcommentform[inreplyto]'].value; - inreplyto = inreplyto.substring(0, inreplyto.indexOf('@')); - - /* Nu vragen we om, asynchroon, een hashcash te berekenen. Zie comments van calculateCommentHashCash() - waarom dit asynhcroon verloopt */ - this.calculateCommentHashCash('<' + inreplyto + '.' + rating + '.' + randomstr + '.', '@spot.net>', 0, this.cbHashcashCalculated); - } // postComment - - this.postReport = function(reportForm, uiStart, uiDone) { - this.reportForm = reportForm; - this.uiStart = uiStart; - this.uiDone = uiDone; - - this.uiStart(); - - var randomstr = reportForm['postreportform[randomstr]'].value; - - var inreplyto = reportForm['postreportform[inreplyto]'].value; - inreplyto = inreplyto.substring(0, inreplyto.indexOf('@')); - - this.calculateCommentHashCash('<' + inreplyto + '.' + randomstr + '.', '@spot.net>', 0, this.rpHashcashCalculated); - } // postReport - - this.postNewSpot = function(newSpotForm, uiStart, uiDone) { - this.newSpotForm = newSpotForm; - this.uiStart = uiStart; - this.uiDone = uiDone; - - /* Clear the errors */ - var $dialdiv = $("#editdialogdiv") - $dialdiv.find("ul.formerrors").empty(); - $dialdiv.find("ul.forminformation").empty(); - - this.uiStart(); - - var randomstr = newSpotForm['newspotform[randomstr]'].value; - - this.calculateCommentHashCash('<' + randomstr, '@spot.net>', 0, this.spotHashcashCalculated); - } // postNewSpot - - // - // We breken de make expensive hash op in stukken omdat - // anders IE8 gaat zeuren over scripts die te lang lopen. - // - this.calculateCommentHashCash = function(prefix, suffix, runCount, cbWhenFound) { - var possibleChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; - var hash = prefix + suffix; - var validHash = false; - - do { - runCount++; - uniquePart = ''; - - for(i = 0; i < 15; i++) { - var irand = Math.round(Math.random() * (possibleChars.length - 1)); - uniquePart += possibleChars.charAt(irand); - } // for - - hash = $.sha1(prefix + uniquePart + suffix); - validHash = (hash.substr(0, 4) == '0000'); - } while ((!validHash) && ((runCount % 500) != 0)); - - if (validHash) { - cbWhenFound(this, prefix + uniquePart + suffix); - } else { - if (runCount > 400000) { - alert("Calculation of SHA1 hash was not successfull: " + runCount); - cbWhenFound(this, ''); - } else { - var _this = this; - window.setTimeout(function() { _this.calculateCommentHashCash(prefix, suffix, runCount, cbWhenFound); }, 0); - } // else - } // if - - } // calculateCommentHashCash -}; +function spotPosting() { + this.commentForm = null; + this.reportForm = null; + this.uiStart = null; + this.uiDone = null; + + this.cbHashcashCalculated = function (self, hash) { + self.commentForm['postcommentform[newmessageid]'].value = hash; + self.commentForm['postcommentform[submitpost]'].value = 'Post'; + self.uiDone(); + + // convert the processed form to post values + var dataString = $(self.commentForm).serialize() + + // and actually process the call + $.ajax({ + type: "POST", + url: "?page=postcomment", + dataType: "xml", + data: dataString, + success: function(xml) { + var result = $(xml).find('result').text(); + if(result == 'success') { + var user = $(xml).find('user').text(); + var spotterid = $(xml).find('spotterid').text(); + var rating = $(xml).find('rating').text(); + var text = $(xml).find('body').text(); + var spotteridurl = 'http://'+window.location.hostname+window.location.pathname+'?search[tree]=&search[type]=SpotterID&search[text]='+spotterid; + var commentimage = $(xml).find('commentimage').text(); + + var data = "
    • Posted by %1".replace("%1", ""+user+"") + " ("+spotterid+") @ just now
      "+text+"
    • "; + + $("li.nocomments").remove(); + $("li.firstComment").removeClass("firstComment"); + $("li.addComment").after(data).next().hide().addClass("firstComment").fadeIn(function(){ + $("#commentslist > li").removeClass("even"); + $("#commentslist > li:nth-child(even)").addClass('even'); + $("span.commentcount").html('# '+$("#commentslist").children().not(".addComment").size()); + }); + } + }, + error: function(xml) { + console.log('error: '+((new XMLSerializer()).serializeToString(xml))); + } + }); + } // cbHashcashCalculated + + this.rpHashcashCalculated = function (self, hash) { + self.reportForm['postreportform[newmessageid]'].value = hash; + self.reportForm['postreportform[submitpost]'].value = 'Post'; + self.uiDone(); + + var dataString2 = $(self.reportForm).serialize() + + $.ajax({ + type: "POST", + url: "?page=reportpost", + dataType: "xml", + data: dataString2, + success: function(xml) { + var result = $(xml).find('result').text(); + var errors = $(xml).find('errors').text(); + + if(result != 'success') { + console.log('error: '+((new XMLSerializer()).serializeToString(xml))); + + $(".spamreport-button").attr('title', result + ': ' + errors); + alert('Marking as spam was not successfull: ' + errors); + } // else + }, + error: function(xml) { + console.log('error: '+((new XMLSerializer()).serializeToString(xml))); + } + }); + } // callback rpHashcashCalculated + + this.spotHashcashCalculated = function (self, hash) { + // and enter the form's inputfields + self.newSpotForm['newspotform[newmessageid]'].value = hash; + self.newSpotForm['newspotform[submitpost]'].value = 'Post'; + self.uiDone(); + + $(self.newSpotForm).ajaxSubmit({ + type: "POST", + url: "?page=postspot", + dataType: "xml", + success: function(xml) { + var $dialdiv = $("#editdialogdiv") + var result = $(xml).find('result').text(); + + var $formerrors = $dialdiv.find("ul.formerrors"); + $formerrors.empty(); + var $forminfo = $dialdiv.find("ul.forminformation"); + $forminfo.empty(); + + if (result == 'success') { + // zet de information van het formulier in de infolijst + $('info', xml).each(function() { + $forminfo.append("
    • " + $(this).text() + "
    • "); + }); // each + + /** + * We succesfully posted a new spot, now clear the title field + * and body. This makes the other field stay in tact, so if a + * user is posting several of the same type of spots, he doesn't + * have to enter everything again + */ + $("input[name='newspotform[title]']").val(''); + $("textarea[name='newspotform[body]']").val(''); + $("input[name='newspotform[nzbfile]']").val(''); + $("input[name='newspotform[imagefile]']").val(''); + } else { + // voeg nu de errors in de html + // zet de errors van het formulier in de errorlijst + $('errors', xml).each(function() { + $formerrors.append("
    • " + $(this).text() + "
    • "); + }); // each + } // if post was not succesful + }, // success() + error: function(xml) { + console.log('error: '+((new XMLSerializer()).serializeToString(xml))); + } + }); + } // callback spotHashcashCalculated + + this.postComment = function(commentForm, uiStart, uiDone) { + this.commentForm = commentForm; + this.uiStart = uiStart; + this.uiDone = uiDone; + + // update the UI + this.uiStart(); + + // First retrieve some values from the form we are submitting + var randomstr = commentForm['postcommentform[randomstr]'].value; + var rating = commentForm['postcommentform[rating]'].value; + + // inreplyto is the whole messageid, we strip off the @ part to add + // our own stuff + var inreplyto = commentForm['postcommentform[inreplyto]'].value; + inreplyto = inreplyto.substring(0, inreplyto.indexOf('@')); + + /* Nu vragen we om, asynchroon, een hashcash te berekenen. Zie comments van calculateCommentHashCash() + waarom dit asynhcroon verloopt */ + this.calculateCommentHashCash('<' + inreplyto + '.' + rating + '.' + randomstr + '.', '@spot.net>', 0, this.cbHashcashCalculated); + } // postComment + + this.postReport = function(reportForm, uiStart, uiDone) { + this.reportForm = reportForm; + this.uiStart = uiStart; + this.uiDone = uiDone; + + this.uiStart(); + + var randomstr = reportForm['postreportform[randomstr]'].value; + + var inreplyto = reportForm['postreportform[inreplyto]'].value; + inreplyto = inreplyto.substring(0, inreplyto.indexOf('@')); + + this.calculateCommentHashCash('<' + inreplyto + '.' + randomstr + '.', '@spot.net>', 0, this.rpHashcashCalculated); + } // postReport + + this.postNewSpot = function(newSpotForm, uiStart, uiDone) { + this.newSpotForm = newSpotForm; + this.uiStart = uiStart; + this.uiDone = uiDone; + + /* Clear the errors */ + var $dialdiv = $("#editdialogdiv") + $dialdiv.find("ul.formerrors").empty(); + $dialdiv.find("ul.forminformation").empty(); + + this.uiStart(); + + var randomstr = newSpotForm['newspotform[randomstr]'].value; + + this.calculateCommentHashCash('<' + randomstr, '@spot.net>', 0, this.spotHashcashCalculated); + } // postNewSpot + + // + // We breken de make expensive hash op in stukken omdat + // anders IE8 gaat zeuren over scripts die te lang lopen. + // + this.calculateCommentHashCash = function(prefix, suffix, runCount, cbWhenFound) { + var possibleChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; + var hash = prefix + suffix; + var validHash = false; + + do { + runCount++; + uniquePart = ''; + + for(i = 0; i < 15; i++) { + var irand = Math.round(Math.random() * (possibleChars.length - 1)); + uniquePart += possibleChars.charAt(irand); + } // for + + hash = $.sha1(prefix + uniquePart + suffix); + validHash = (hash.substr(0, 4) == '0000'); + } while ((!validHash) && ((runCount % 500) != 0)); + + if (validHash) { + cbWhenFound(this, prefix + uniquePart + suffix); + } else { + if (runCount > 400000) { + alert("Calculation of SHA1 hash was not successfull: " + runCount); + cbWhenFound(this, ''); + } else { + var _this = this; + window.setTimeout(function() { _this.calculateCommentHashCash(prefix, suffix, runCount, cbWhenFound); }, 0); + } // else + } // if + + } // calculateCommentHashCash +}; diff --git a/lib/SpotNotificationTemplate.php b/lib/SpotNotificationTemplate.php index 502488a90..988d0c788 100755 --- a/lib/SpotNotificationTemplate.php +++ b/lib/SpotNotificationTemplate.php @@ -1,49 +1,49 @@ -_db = $db; - $this->_settings = $settings; - $this->_currentSession = $currentSession; - $this->_spotSec = $currentSession['security']; - } # ctor - - /* - * Vraagt de inhoud van de template op - */ - function template($tpl, $params = array()) { - SpotTiming::start(__FUNCTION__ . ':notifications:' . $tpl); - - extract($params, EXTR_REFS); - $settings = $this->_settings; - - # We maken een aantal variabelen / objecten standaard beschikbaar in de template. - $currentSession = $this->_currentSession; - $spotSec = $this->_currentSession['security']; - - # start output buffering - ob_start(); - - # en we spelen de template af - require 'templates/notifications/' . $tpl . '.inc.php'; - - # nu vraag de inhoud van de output buffer op - $notificationContent = ob_get_contents(); - ob_end_clean(); - - # de eerste regel is het onderwerp, de tweede regel is een spatie, - # en de rest is daadwerkelijke buffer - $notificationArray = explode("\n", $notificationContent); - - SpotTiming::stop(__FUNCTION__ . ':notifications:' . $tpl, array($params)); - - return array('title' => $notificationArray[0], - 'body' => array_slice($notificationArray, 2)); - } # template - -} # class +_db = $db; + $this->_settings = $settings; + $this->_currentSession = $currentSession; + $this->_spotSec = $currentSession['security']; + } # ctor + + /* + * Vraagt de inhoud van de template op + */ + function template($tpl, $params = array()) { + SpotTiming::start(__FUNCTION__ . ':notifications:' . $tpl); + + extract($params, EXTR_REFS); + $settings = $this->_settings; + + # We maken een aantal variabelen / objecten standaard beschikbaar in de template. + $currentSession = $this->_currentSession; + $spotSec = $this->_currentSession['security']; + + # start output buffering + ob_start(); + + # en we spelen de template af + require 'templates/notifications/' . $tpl . '.inc.php'; + + # nu vraag de inhoud van de output buffer op + $notificationContent = ob_get_contents(); + ob_end_clean(); + + # de eerste regel is het onderwerp, de tweede regel is een spatie, + # en de rest is daadwerkelijke buffer + $notificationArray = explode("\n", $notificationContent); + + SpotTiming::stop(__FUNCTION__ . ':notifications:' . $tpl, array($params)); + + return array('title' => $notificationArray[0], + 'body' => array_slice($notificationArray, 2)); + } # template + +} # class diff --git a/lib/SpotTranslation.php b/lib/SpotTranslation.php index 8478c4365..54f44928d 100755 --- a/lib/SpotTranslation.php +++ b/lib/SpotTranslation.php @@ -1,49 +1,49 @@ -gettext($msg); - } # _ alias of gettext - - function gettext($msg) { - return $GLOBALS['_gt_obj']->gettext($msg); - } # gettext - - function dgettext($domain, $msg) { - return $GLOBALS['_gt_obj']->dgettext($domain, $msg); - } # dgettext - - function ngettext($msg, $msg_plural, $count) { - return $GLOBALS['_gt_obj']->ngettext($msg, $msg_plural, $count); - } # ngettext - - function dngettext($domain, $msg, $msg_plural, $count) { - return $GLOBALS['_gt_obj']->dngettext($domain, $msg, $msg_plural, $count); - } # dngettext -} # if +gettext($msg); + } # _ alias of gettext + + function gettext($msg) { + return $GLOBALS['_gt_obj']->gettext($msg); + } # gettext + + function dgettext($domain, $msg) { + return $GLOBALS['_gt_obj']->dgettext($domain, $msg); + } # dgettext + + function ngettext($msg, $msg_plural, $count) { + return $GLOBALS['_gt_obj']->ngettext($msg, $msg_plural, $count); + } # ngettext + + function dngettext($domain, $msg, $msg_plural, $count) { + return $GLOBALS['_gt_obj']->dngettext($domain, $msg, $msg_plural, $count); + } # dngettext +} # if diff --git a/lib/SpotWebVersionCheck.php b/lib/SpotWebVersionCheck.php index 90dd8fa3f..3c86f85e2 100755 --- a/lib/SpotWebVersionCheck.php +++ b/lib/SpotWebVersionCheck.php @@ -1,68 +1,68 @@ -retrieveRss(); - } - - /* - * Retrieves the RSS feed from Github - */ - private function retrieveRss() { - $rssFile = file_get_contents(SpotWebVersionCheck::rss_url); - - # Supress the namespace warning - $this->_xml = @simplexml_load_string($rssFile); - } # retrieveRss - - /* - * Returns the news items - */ - function getItems() { - $itemList = array(); - - /* - * Returning the array directly from simplexml doesn't work - */ - foreach($this->_xml->channel->item as $item) { - $itemArray = array( - 'title' => $item->title, - 'description' => $item->description, - 'link' => $item->link, - 'guid' => $item->guid, - 'pubDate' => $item->pubDate, - 'author' => $item->author, - 'schema_version' => $item->schema_version, - 'settings_version' => $item->settings_version, - 'security_version' => $item->security_version, - 'feature_version' => $item->feature_version); - $itemArray['is_newer_than_installed'] = !$this->isLatestVersion($itemArray); - - $itemList[] = $itemArray; - } # foreach - - return $itemList; - } # getItems - - /* - * Returns whether the current system is up-to-date - */ - function isLatestVersion($item) { - return ($item['schema_version'] <= SPOTDB_SCHEMA_VERSION) && - ($item['settings_version'] <= SPOTWEB_SETTINGS_VERSION) && - ($item['security_version'] <= SPOTWEB_SECURITY_VERSION) && - ($item['feature_version'] <= SPOTWEB_FEATURE_VERSION); - } # isLatestVersion - -} # class SpotWebVersionCheck.php +retrieveRss(); + } + + /* + * Retrieves the RSS feed from Github + */ + private function retrieveRss() { + $rssFile = file_get_contents(SpotWebVersionCheck::rss_url); + + # Supress the namespace warning + $this->_xml = @simplexml_load_string($rssFile); + } # retrieveRss + + /* + * Returns the news items + */ + function getItems() { + $itemList = array(); + + /* + * Returning the array directly from simplexml doesn't work + */ + foreach($this->_xml->channel->item as $item) { + $itemArray = array( + 'title' => $item->title, + 'description' => $item->description, + 'link' => $item->link, + 'guid' => $item->guid, + 'pubDate' => $item->pubDate, + 'author' => $item->author, + 'schema_version' => $item->schema_version, + 'settings_version' => $item->settings_version, + 'security_version' => $item->security_version, + 'feature_version' => $item->feature_version); + $itemArray['is_newer_than_installed'] = !$this->isLatestVersion($itemArray); + + $itemList[] = $itemArray; + } # foreach + + return $itemList; + } # getItems + + /* + * Returns whether the current system is up-to-date + */ + function isLatestVersion($item) { + return ($item['schema_version'] <= SPOTDB_SCHEMA_VERSION) && + ($item['settings_version'] <= SPOTWEB_SETTINGS_VERSION) && + ($item['security_version'] <= SPOTWEB_SECURITY_VERSION) && + ($item['feature_version'] <= SPOTWEB_FEATURE_VERSION); + } # isLatestVersion + +} # class SpotWebVersionCheck.php diff --git a/lib/dbstruct/SpotStruct_pgsql.php b/lib/dbstruct/SpotStruct_pgsql.php index 1842c5ebc..0017e0e33 100755 --- a/lib/dbstruct/SpotStruct_pgsql.php +++ b/lib/dbstruct/SpotStruct_pgsql.php @@ -1,433 +1,433 @@ -_dbcon->rawExec("VACUUM ANALYZE spots"); - $this->_dbcon->rawExec("VACUUM ANALYZE spotsfull"); - $this->_dbcon->rawExec("VACUUM ANALYZE commentsxover"); - $this->_dbcon->rawExec("VACUUM ANALYZE commentsfull"); - $this->_dbcon->rawExec("VACUUM ANALYZE sessions"); - $this->_dbcon->rawExec("VACUUM ANALYZE filters"); - $this->_dbcon->rawExec("VACUUM ANALYZE spotteridblacklist"); - $this->_dbcon->rawExec("VACUUM ANALYZE filtercounts"); - $this->_dbcon->rawExec("VACUUM ANALYZE spotstatelist"); - $this->_dbcon->rawExec("VACUUM ANALYZE users"); - $this->_dbcon->rawExec("VACUUM ANALYZE cache"); - } # analyze - - /* - * Converts a 'spotweb' internal datatype to a - * database specific datatype - */ - function swDtToNative($colType) { - switch(strtoupper($colType)) { - case 'INTEGER' : $colType = 'integer'; break; - case 'UNSIGNED INTEGER' : $colType = 'bigint'; break; - case 'BIGINTEGER' : $colType = 'bigint'; break; - case 'UNSIGNED BIGINTEGER' : $colType = 'bigint'; break; - case 'BOOLEAN' : $colType = 'boolean'; break; - case 'MEDIUMBLOB' : $colType = 'bytea'; break; - } # switch - - return $colType; - } # swDtToNative - - /* - * Converts a database native datatype to a spotweb native - * datatype - */ - function nativeDtToSw($colInfo) { - switch(strtolower($colInfo)) { - case 'integer' : $colInfo = 'INTEGER'; break; - case 'bigint' : $colInfo = 'BIGINTEGER'; break; - case 'boolean' : $colInfo = 'BOOLEAN'; break; - case 'bytea' : $colInfo = 'MEDIUMBLOB'; break; - } # switch - - return $colInfo; - } # nativeDtToSw - - /* checks if an index exists */ - function indexExists($idxname, $tablename) { - $q = $this->_dbcon->arrayQuery("SELECT indexname FROM pg_indexes WHERE schemaname = CURRENT_SCHEMA() AND tablename = '%s' AND indexname = '%s'", - Array($tablename, $idxname)); - return !empty($q); - } # indexExists - - /* checks if a column exists */ - function columnExists($tablename, $colname) { - $q = $this->_dbcon->arrayQuery("SELECT column_name FROM information_schema.columns - WHERE table_schema = CURRENT_SCHEMA() AND table_name = '%s' AND column_name = '%s'", - Array($tablename, $colname)); - return !empty($q); - } # columnExists - - /* checks if a fts text index exists */ - function ftsExists($ftsname, $tablename, $colList) { - foreach($colList as $num => $col) { - $indexInfo = $this->getIndexInfo($ftsname . '_' . $num, $tablename); - - if ((empty($indexInfo)) || (strtolower($indexInfo[0]['column_name']) != strtolower($col))) { - return false; - } # if - } # foreach - - return true; - } # ftsExists - - /* creates a full text index */ - function createFts($ftsname, $tablename, $colList) { - foreach($colList as $num => $col) { - $indexInfo = $this->getIndexInfo($ftsname . '_' . $num, $tablename); - - if ((empty($indexInfo)) || (strtolower($indexInfo[0]['column_name']) != strtolower($col))) { - $this->dropIndex($ftsname . '_' . $num, $tablename); - $this->addIndex($ftsname . '_' . $num, 'FULLTEXT', $tablename, array($col)); - } # if - } # foreach - } # createFts - - /* drops a fulltext index */ - function dropFts($ftsname, $tablename, $colList) { - foreach($colList as $num => $col) { - $this->dropIndex($ftsname . '_' . $num, $tablename); - } # foreach - } # dropFts - - /* returns FTS info */ - function getFtsInfo($ftsname, $tablename, $colList) { - $ftsList = array(); - - foreach($colList as $num => $col) { - $tmpIndex = $this->getIndexInfo($ftsname . '_' . $num, $tablename); - - if (!empty($tmpIndex)) { - $ftsList[] = $tmpIndex[0]; - } # if - } # foreach - - return $ftsList; - } # getFtsInfo - - /* - * Adds an index, but first checks if the index doesn't - * exist already. - * - * $idxType can be either 'UNIQUE', '' or 'FULLTEXT' - */ - function addIndex($idxname, $idxType, $tablename, $colList) { - if (!$this->indexExists($idxname, $tablename)) { - switch($idxType) { - case 'UNIQUE': { - $this->_dbcon->rawExec("CREATE UNIQUE INDEX " . $idxname . " ON " . $tablename . "(" . implode(",", $colList) . ")"); - break; - } # case - - case 'FULLTEXT' : { - $this->_dbcon->rawExec("CREATE INDEX " . $idxname . " ON " . $tablename . " USING gin(to_tsvector('dutch', " . implode(",", $colList) . "))"); - break; - } # case - - default : { - $this->_dbcon->rawExec("CREATE INDEX " . $idxname . " ON " . $tablename . "(" . implode(",", $colList) . ")"); - } # default - } # switch - } # if - } # addIndex - - /* drops an index if it exists */ - function dropIndex($idxname, $tablename) { - /* - * Make sure the table exists, else this will return an error - * and return a fatal - */ - if (!$this->tableExists($tablename)) { - return ; - } # if - - if ($this->indexExists($idxname, $tablename)) { - $this->_dbcon->rawExec("DROP INDEX " . $idxname); - } # if - } # dropIndex - - /* adds a column if the column doesn't exist yet */ - function addColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation) { - if (!$this->columnExists($tablename, $colName)) { - # set the DEFAULT value - if (strlen($colDefault) != 0) { - $colDefault = 'DEFAULT ' . $colDefault; - } # if - - # Convert the column type to a type we use in PostgreSQL - $colType = $this->swDtToNative($colType); - - /* - * Only pgsql 9.1 (only just released) supports per-column collation, so for now - * we ignore this - */ - switch(strtolower($collation)) { - case 'utf8' : - case 'ascii' : - case 'ascii_bin' : - case '' : $colSetting = ''; break; - default : throw new Exception("Invalid collation setting"); - } # switch - - # and define the 'NOT NULL' part - switch($notNull) { - case true : $nullStr = 'NOT NULL'; break; - default : $nullStr = ''; - } # switch - - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . - " ADD COLUMN " . $colName . " " . $colType . " " . $colSetting . " " . $colDefault . " " . $nullStr); - } # if - } # addColumn - - /* alters a column - does not check if the column doesn't adhere to the given definition */ - function modifyColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation, $what) { - # set the DEFAULT value - if (strlen($colDefault) != 0) { - $colDefault = 'DEFAULT ' . $colDefault; - } # if - - # Convert the column type to a type we use in PostgreSQL - $colType = $this->swDtToNative($colType); - - /* - * Only pgsql 9.1 (only just released) supports per-column collation, so for now - * we ignore this - */ - switch(strtolower($collation)) { - case 'utf8' : - case 'ascii' : - case 'ascii_bin' : - case '' : $colSetting = ''; break; - default : throw new Exception("Invalid collation setting"); - } # switch - - # and define the 'NOT NULL' part - switch($notNull) { - case true : $nullStr = 'NOT NULL'; break; - default : $nullStr = ''; - } # switch - - # Alter the column type - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " TYPE " . $colType); - - # Change the default value (if one set, else drop it) - if (strlen($colDefault) > 0) { - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " SET " . $colDefault); - } else { - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " DROP DEFAULT"); - } # if - - # and changes the null/not-null constraint - if (strlen($notNull) > 0) { - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " SET NOT NULL"); - } else { - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " DROP NOT NULL"); - } # if - } # modifyColumn - - /* drops a column */ - function dropColumn($colName, $tablename) { - if ($this->columnExists($tablename, $colName)) { - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " DROP COLUMN " . $colName); - } # if - } # dropColumn - - /* checks if a table exists */ - function tableExists($tablename) { - $q = $this->_dbcon->arrayQuery("SELECT tablename FROM pg_tables WHERE schemaname = CURRENT_SCHEMA() AND (tablename = '%s')", array($tablename)); - return !empty($q); - } # tableExists - - /* creates an empty table with only an ID field. Collation should be either UTF8 or ASCII */ - function createTable($tablename, $collation) { - if (!$this->tableExists($tablename)) { - /* - * Only pgsql 9.1 (only just released) supports per-column collation, so for now - * we ignore this - */ - switch(strtolower($collation)) { - case 'utf8' : - case 'ascii' : - case '' : $colSetting = ''; break; - default : throw new Exception("Invalid collation setting"); - } # switch - - $this->_dbcon->rawExec("CREATE TABLE " . $tablename . " (id SERIAL PRIMARY KEY) " . $colSetting); - } # if - } # createTable - - /* drop a table */ - function dropTable($tablename) { - if ($this->tableExists($tablename)) { - $this->_dbcon->rawExec("DROP TABLE " . $tablename); - } # if - } # dropTable - - /* dummy - postgresql doesn't know storage engines of course */ - function alterStorageEngine($tablename, $engine) { - return false; - } # alterStorageEngine - - /* rename a table */ - function renameTable($tablename, $newTableName) { - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " RENAME TO " . $newTableName); - } # renameTable - - /* drop a foreign key constraint */ - function dropForeignKey($tablename, $colname, $reftable, $refcolumn, $action) { - /* SQL from http://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys */ - $q = $this->_dbcon->arrayQuery("SELECT - tc.constraint_name AS CONSTRAINT_NAME, - tc.table_name AS TABLE_NAME, - tc.constraint_schema AS TABLE_SCHEMA, - kcu.column_name AS COLUMN_NAME, - ccu.table_name AS REFERENCED_TABLE_NAME, - ccu.column_name AS REFERENCED_COLUMN_NAME - FROM - information_schema.table_constraints AS tc - JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name - JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name - WHERE constraint_type = 'FOREIGN KEY' - AND tc.TABLE_SCHEMA = CURRENT_SCHEMA() - AND tc.TABLE_NAME = '%s' - AND kcu.COLUMN_NAME = '%s' - AND ccu.table_name = '%s' - AND ccu.column_name = '%s'", - Array($tablename, $colname, $reftable, $refcolumn)); - if (!empty($q)) { - foreach($q as $res) { - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " DROP CONSTRAINT " . $res['constraint_name']); - } # foreach - } # if - } # dropForeignKey - - /* create a foreign key constraint */ - function addForeignKey($tablename, $colname, $reftable, $refcolumn, $action) { - /* SQL from http://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys */ - $q = $this->_dbcon->arrayQuery("SELECT - tc.constraint_name AS CONSTRAINT_NAME, - tc.table_name AS TABLE_NAME, - tc.constraint_schema AS TABLE_SCHEMA, - kcu.column_name AS COLUMN_NAME, - ccu.table_name AS REFERENCED_TABLE_NAME, - ccu.column_name AS REFERENCED_COLUMN_NAME - FROM - information_schema.table_constraints AS tc - JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name - JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name - WHERE constraint_type = 'FOREIGN KEY' - AND tc.TABLE_SCHEMA = CURRENT_SCHEMA() - AND tc.TABLE_NAME = '%s' - AND kcu.COLUMN_NAME = '%s' - AND ccu.table_name = '%s' - AND ccu.column_name = '%s'", - Array($tablename, $colname, $reftable, $refcolumn)); - if (empty($q)) { - $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ADD FOREIGN KEY (" . $colname . ") - REFERENCES " . $reftable . " (" . $refcolumn . ") " . $action); - } # if - } # addForeignKey - - /* Returns in a fixed format, column information */ - function getColumnInfo($tablename, $colname) { - $q = $this->_dbcon->arrayQuery("SELECT column_name AS \"COLUMN_NAME\", - column_default AS \"COLUMN_DEFAULT\", - is_nullable AS \"IS_NULLABLE\", - data_type AS \"DATA_TYPE\", - numeric_precision AS \"NUMERIC_PRECISION\", - CASE - WHEN (data_type = 'character varying') THEN 'varchar(' || character_maximum_length || ')' - WHEN (data_type = 'integer') THEN 'integer' - WHEN (data_type = 'bigint') THEN 'bigint' - WHEN (data_type = 'boolean') THEN 'boolean' - WHEN (data_type = 'text') THEN 'text' - WHEN (data_type = 'bytea') THEN 'bytea' - END as \"COLUMN_TYPE\", - character_set_name AS \"CHARACTER_SET_NAME\", - collation_name AS \"COLLATION_NAME\" - FROM information_schema.COLUMNS - WHERE TABLE_SCHEMA = CURRENT_SCHEMA() - AND TABLE_NAME = '%s' - AND COLUMN_NAME = '%s'", - Array($tablename, $colname)); - if (!empty($q)) { - $q = $q[0]; - - $q['NOTNULL'] = ($q['IS_NULLABLE'] != 'YES'); - - # a default value has to given, so make it compareable to what we define - if ((strlen($q['COLUMN_DEFAULT']) == 0) && (is_string($q['COLUMN_DEFAULT']))) { - $q['COLUMN_DEFAULT'] = "''"; - } # if - - /* - * PostgreSQL per default explicitly typecasts the value, but - * we cannot do this, so we strip the default value of its typecast - */ - if (strpos($q['COLUMN_DEFAULT'], ':') !== false) { - $elems = explode(':', $q['COLUMN_DEFAULT']); - - $q['COLUMN_DEFAULT'] = $elems[0]; - } # if - } # if - - return $q; - } # getColumnInfo - - /* Returns in a fixed format, index information */ - function getIndexInfo($idxname, $tablename) { - $q = $this->_dbcon->arrayQuery("SELECT * - FROM pg_indexes - WHERE schemaname = CURRENT_SCHEMA() - AND tablename = '%s' - AND indexname = '%s'", Array($tablename, $idxname)); - if (empty($q)) { - return array(); - } # if - - # a index name has to be unique - $q = $q[0]; - - # is the index marked as unique - $tmpAr = explode(" ", $q['indexdef']); - $isNotUnique = (strtolower($tmpAr[1]) != 'unique'); - - # retrieve the column list and seperate the column definition per comma - preg_match_all("/\((.*)\)/", $q['indexdef'], $tmpAr); - - $colList = explode(",", $tmpAr[1][0]); - $colList = array_map('trim', $colList); - - # gin indexes (fulltext search) only have 1 column, so we excempt them - $idxInfo = array(); - if (stripos($tmpAr[1][0], 'to_tsvector') === false) { - for($i = 0; $i < count($colList); $i++) { - $idxInfo[] = array('column_name' => $colList[$i], - 'non_unique' => (int) $isNotUnique, - 'index_type' => 'BTREE' - ); - } # foreach - } else { - # extract the column name - preg_match_all("/\((.*)\)/U", $colList[1], $tmpAr); - - # and create the index info - $idxInfo[] = array('column_name' => $tmpAr[1][0], - 'non_unique' => (int) $isNotUnique, - 'index_type' => 'FULLTEXT'); - } # else - - return $idxInfo; - } # getIndexInfo - -} # class +_dbcon->rawExec("VACUUM ANALYZE spots"); + $this->_dbcon->rawExec("VACUUM ANALYZE spotsfull"); + $this->_dbcon->rawExec("VACUUM ANALYZE commentsxover"); + $this->_dbcon->rawExec("VACUUM ANALYZE commentsfull"); + $this->_dbcon->rawExec("VACUUM ANALYZE sessions"); + $this->_dbcon->rawExec("VACUUM ANALYZE filters"); + $this->_dbcon->rawExec("VACUUM ANALYZE spotteridblacklist"); + $this->_dbcon->rawExec("VACUUM ANALYZE filtercounts"); + $this->_dbcon->rawExec("VACUUM ANALYZE spotstatelist"); + $this->_dbcon->rawExec("VACUUM ANALYZE users"); + $this->_dbcon->rawExec("VACUUM ANALYZE cache"); + } # analyze + + /* + * Converts a 'spotweb' internal datatype to a + * database specific datatype + */ + function swDtToNative($colType) { + switch(strtoupper($colType)) { + case 'INTEGER' : $colType = 'integer'; break; + case 'UNSIGNED INTEGER' : $colType = 'bigint'; break; + case 'BIGINTEGER' : $colType = 'bigint'; break; + case 'UNSIGNED BIGINTEGER' : $colType = 'bigint'; break; + case 'BOOLEAN' : $colType = 'boolean'; break; + case 'MEDIUMBLOB' : $colType = 'bytea'; break; + } # switch + + return $colType; + } # swDtToNative + + /* + * Converts a database native datatype to a spotweb native + * datatype + */ + function nativeDtToSw($colInfo) { + switch(strtolower($colInfo)) { + case 'integer' : $colInfo = 'INTEGER'; break; + case 'bigint' : $colInfo = 'BIGINTEGER'; break; + case 'boolean' : $colInfo = 'BOOLEAN'; break; + case 'bytea' : $colInfo = 'MEDIUMBLOB'; break; + } # switch + + return $colInfo; + } # nativeDtToSw + + /* checks if an index exists */ + function indexExists($idxname, $tablename) { + $q = $this->_dbcon->arrayQuery("SELECT indexname FROM pg_indexes WHERE schemaname = CURRENT_SCHEMA() AND tablename = '%s' AND indexname = '%s'", + Array($tablename, $idxname)); + return !empty($q); + } # indexExists + + /* checks if a column exists */ + function columnExists($tablename, $colname) { + $q = $this->_dbcon->arrayQuery("SELECT column_name FROM information_schema.columns + WHERE table_schema = CURRENT_SCHEMA() AND table_name = '%s' AND column_name = '%s'", + Array($tablename, $colname)); + return !empty($q); + } # columnExists + + /* checks if a fts text index exists */ + function ftsExists($ftsname, $tablename, $colList) { + foreach($colList as $num => $col) { + $indexInfo = $this->getIndexInfo($ftsname . '_' . $num, $tablename); + + if ((empty($indexInfo)) || (strtolower($indexInfo[0]['column_name']) != strtolower($col))) { + return false; + } # if + } # foreach + + return true; + } # ftsExists + + /* creates a full text index */ + function createFts($ftsname, $tablename, $colList) { + foreach($colList as $num => $col) { + $indexInfo = $this->getIndexInfo($ftsname . '_' . $num, $tablename); + + if ((empty($indexInfo)) || (strtolower($indexInfo[0]['column_name']) != strtolower($col))) { + $this->dropIndex($ftsname . '_' . $num, $tablename); + $this->addIndex($ftsname . '_' . $num, 'FULLTEXT', $tablename, array($col)); + } # if + } # foreach + } # createFts + + /* drops a fulltext index */ + function dropFts($ftsname, $tablename, $colList) { + foreach($colList as $num => $col) { + $this->dropIndex($ftsname . '_' . $num, $tablename); + } # foreach + } # dropFts + + /* returns FTS info */ + function getFtsInfo($ftsname, $tablename, $colList) { + $ftsList = array(); + + foreach($colList as $num => $col) { + $tmpIndex = $this->getIndexInfo($ftsname . '_' . $num, $tablename); + + if (!empty($tmpIndex)) { + $ftsList[] = $tmpIndex[0]; + } # if + } # foreach + + return $ftsList; + } # getFtsInfo + + /* + * Adds an index, but first checks if the index doesn't + * exist already. + * + * $idxType can be either 'UNIQUE', '' or 'FULLTEXT' + */ + function addIndex($idxname, $idxType, $tablename, $colList) { + if (!$this->indexExists($idxname, $tablename)) { + switch($idxType) { + case 'UNIQUE': { + $this->_dbcon->rawExec("CREATE UNIQUE INDEX " . $idxname . " ON " . $tablename . "(" . implode(",", $colList) . ")"); + break; + } # case + + case 'FULLTEXT' : { + $this->_dbcon->rawExec("CREATE INDEX " . $idxname . " ON " . $tablename . " USING gin(to_tsvector('dutch', " . implode(",", $colList) . "))"); + break; + } # case + + default : { + $this->_dbcon->rawExec("CREATE INDEX " . $idxname . " ON " . $tablename . "(" . implode(",", $colList) . ")"); + } # default + } # switch + } # if + } # addIndex + + /* drops an index if it exists */ + function dropIndex($idxname, $tablename) { + /* + * Make sure the table exists, else this will return an error + * and return a fatal + */ + if (!$this->tableExists($tablename)) { + return ; + } # if + + if ($this->indexExists($idxname, $tablename)) { + $this->_dbcon->rawExec("DROP INDEX " . $idxname); + } # if + } # dropIndex + + /* adds a column if the column doesn't exist yet */ + function addColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation) { + if (!$this->columnExists($tablename, $colName)) { + # set the DEFAULT value + if (strlen($colDefault) != 0) { + $colDefault = 'DEFAULT ' . $colDefault; + } # if + + # Convert the column type to a type we use in PostgreSQL + $colType = $this->swDtToNative($colType); + + /* + * Only pgsql 9.1 (only just released) supports per-column collation, so for now + * we ignore this + */ + switch(strtolower($collation)) { + case 'utf8' : + case 'ascii' : + case 'ascii_bin' : + case '' : $colSetting = ''; break; + default : throw new Exception("Invalid collation setting"); + } # switch + + # and define the 'NOT NULL' part + switch($notNull) { + case true : $nullStr = 'NOT NULL'; break; + default : $nullStr = ''; + } # switch + + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . + " ADD COLUMN " . $colName . " " . $colType . " " . $colSetting . " " . $colDefault . " " . $nullStr); + } # if + } # addColumn + + /* alters a column - does not check if the column doesn't adhere to the given definition */ + function modifyColumn($colName, $tablename, $colType, $colDefault, $notNull, $collation, $what) { + # set the DEFAULT value + if (strlen($colDefault) != 0) { + $colDefault = 'DEFAULT ' . $colDefault; + } # if + + # Convert the column type to a type we use in PostgreSQL + $colType = $this->swDtToNative($colType); + + /* + * Only pgsql 9.1 (only just released) supports per-column collation, so for now + * we ignore this + */ + switch(strtolower($collation)) { + case 'utf8' : + case 'ascii' : + case 'ascii_bin' : + case '' : $colSetting = ''; break; + default : throw new Exception("Invalid collation setting"); + } # switch + + # and define the 'NOT NULL' part + switch($notNull) { + case true : $nullStr = 'NOT NULL'; break; + default : $nullStr = ''; + } # switch + + # Alter the column type + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " TYPE " . $colType); + + # Change the default value (if one set, else drop it) + if (strlen($colDefault) > 0) { + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " SET " . $colDefault); + } else { + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " DROP DEFAULT"); + } # if + + # and changes the null/not-null constraint + if (strlen($notNull) > 0) { + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " SET NOT NULL"); + } else { + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ALTER COLUMN " . $colName . " DROP NOT NULL"); + } # if + } # modifyColumn + + /* drops a column */ + function dropColumn($colName, $tablename) { + if ($this->columnExists($tablename, $colName)) { + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " DROP COLUMN " . $colName); + } # if + } # dropColumn + + /* checks if a table exists */ + function tableExists($tablename) { + $q = $this->_dbcon->arrayQuery("SELECT tablename FROM pg_tables WHERE schemaname = CURRENT_SCHEMA() AND (tablename = '%s')", array($tablename)); + return !empty($q); + } # tableExists + + /* creates an empty table with only an ID field. Collation should be either UTF8 or ASCII */ + function createTable($tablename, $collation) { + if (!$this->tableExists($tablename)) { + /* + * Only pgsql 9.1 (only just released) supports per-column collation, so for now + * we ignore this + */ + switch(strtolower($collation)) { + case 'utf8' : + case 'ascii' : + case '' : $colSetting = ''; break; + default : throw new Exception("Invalid collation setting"); + } # switch + + $this->_dbcon->rawExec("CREATE TABLE " . $tablename . " (id SERIAL PRIMARY KEY) " . $colSetting); + } # if + } # createTable + + /* drop a table */ + function dropTable($tablename) { + if ($this->tableExists($tablename)) { + $this->_dbcon->rawExec("DROP TABLE " . $tablename); + } # if + } # dropTable + + /* dummy - postgresql doesn't know storage engines of course */ + function alterStorageEngine($tablename, $engine) { + return false; + } # alterStorageEngine + + /* rename a table */ + function renameTable($tablename, $newTableName) { + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " RENAME TO " . $newTableName); + } # renameTable + + /* drop a foreign key constraint */ + function dropForeignKey($tablename, $colname, $reftable, $refcolumn, $action) { + /* SQL from http://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys */ + $q = $this->_dbcon->arrayQuery("SELECT + tc.constraint_name AS CONSTRAINT_NAME, + tc.table_name AS TABLE_NAME, + tc.constraint_schema AS TABLE_SCHEMA, + kcu.column_name AS COLUMN_NAME, + ccu.table_name AS REFERENCED_TABLE_NAME, + ccu.column_name AS REFERENCED_COLUMN_NAME + FROM + information_schema.table_constraints AS tc + JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name + JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name + WHERE constraint_type = 'FOREIGN KEY' + AND tc.TABLE_SCHEMA = CURRENT_SCHEMA() + AND tc.TABLE_NAME = '%s' + AND kcu.COLUMN_NAME = '%s' + AND ccu.table_name = '%s' + AND ccu.column_name = '%s'", + Array($tablename, $colname, $reftable, $refcolumn)); + if (!empty($q)) { + foreach($q as $res) { + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " DROP CONSTRAINT " . $res['constraint_name']); + } # foreach + } # if + } # dropForeignKey + + /* create a foreign key constraint */ + function addForeignKey($tablename, $colname, $reftable, $refcolumn, $action) { + /* SQL from http://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys */ + $q = $this->_dbcon->arrayQuery("SELECT + tc.constraint_name AS CONSTRAINT_NAME, + tc.table_name AS TABLE_NAME, + tc.constraint_schema AS TABLE_SCHEMA, + kcu.column_name AS COLUMN_NAME, + ccu.table_name AS REFERENCED_TABLE_NAME, + ccu.column_name AS REFERENCED_COLUMN_NAME + FROM + information_schema.table_constraints AS tc + JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name + JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name + WHERE constraint_type = 'FOREIGN KEY' + AND tc.TABLE_SCHEMA = CURRENT_SCHEMA() + AND tc.TABLE_NAME = '%s' + AND kcu.COLUMN_NAME = '%s' + AND ccu.table_name = '%s' + AND ccu.column_name = '%s'", + Array($tablename, $colname, $reftable, $refcolumn)); + if (empty($q)) { + $this->_dbcon->rawExec("ALTER TABLE " . $tablename . " ADD FOREIGN KEY (" . $colname . ") + REFERENCES " . $reftable . " (" . $refcolumn . ") " . $action); + } # if + } # addForeignKey + + /* Returns in a fixed format, column information */ + function getColumnInfo($tablename, $colname) { + $q = $this->_dbcon->arrayQuery("SELECT column_name AS \"COLUMN_NAME\", + column_default AS \"COLUMN_DEFAULT\", + is_nullable AS \"IS_NULLABLE\", + data_type AS \"DATA_TYPE\", + numeric_precision AS \"NUMERIC_PRECISION\", + CASE + WHEN (data_type = 'character varying') THEN 'varchar(' || character_maximum_length || ')' + WHEN (data_type = 'integer') THEN 'integer' + WHEN (data_type = 'bigint') THEN 'bigint' + WHEN (data_type = 'boolean') THEN 'boolean' + WHEN (data_type = 'text') THEN 'text' + WHEN (data_type = 'bytea') THEN 'bytea' + END as \"COLUMN_TYPE\", + character_set_name AS \"CHARACTER_SET_NAME\", + collation_name AS \"COLLATION_NAME\" + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = CURRENT_SCHEMA() + AND TABLE_NAME = '%s' + AND COLUMN_NAME = '%s'", + Array($tablename, $colname)); + if (!empty($q)) { + $q = $q[0]; + + $q['NOTNULL'] = ($q['IS_NULLABLE'] != 'YES'); + + # a default value has to given, so make it compareable to what we define + if ((strlen($q['COLUMN_DEFAULT']) == 0) && (is_string($q['COLUMN_DEFAULT']))) { + $q['COLUMN_DEFAULT'] = "''"; + } # if + + /* + * PostgreSQL per default explicitly typecasts the value, but + * we cannot do this, so we strip the default value of its typecast + */ + if (strpos($q['COLUMN_DEFAULT'], ':') !== false) { + $elems = explode(':', $q['COLUMN_DEFAULT']); + + $q['COLUMN_DEFAULT'] = $elems[0]; + } # if + } # if + + return $q; + } # getColumnInfo + + /* Returns in a fixed format, index information */ + function getIndexInfo($idxname, $tablename) { + $q = $this->_dbcon->arrayQuery("SELECT * + FROM pg_indexes + WHERE schemaname = CURRENT_SCHEMA() + AND tablename = '%s' + AND indexname = '%s'", Array($tablename, $idxname)); + if (empty($q)) { + return array(); + } # if + + # a index name has to be unique + $q = $q[0]; + + # is the index marked as unique + $tmpAr = explode(" ", $q['indexdef']); + $isNotUnique = (strtolower($tmpAr[1]) != 'unique'); + + # retrieve the column list and seperate the column definition per comma + preg_match_all("/\((.*)\)/", $q['indexdef'], $tmpAr); + + $colList = explode(",", $tmpAr[1][0]); + $colList = array_map('trim', $colList); + + # gin indexes (fulltext search) only have 1 column, so we excempt them + $idxInfo = array(); + if (stripos($tmpAr[1][0], 'to_tsvector') === false) { + for($i = 0; $i < count($colList); $i++) { + $idxInfo[] = array('column_name' => $colList[$i], + 'non_unique' => (int) $isNotUnique, + 'index_type' => 'BTREE' + ); + } # foreach + } else { + # extract the column name + preg_match_all("/\((.*)\)/U", $colList[1], $tmpAr); + + # and create the index info + $idxInfo[] = array('column_name' => $tmpAr[1][0], + 'non_unique' => (int) $isNotUnique, + 'index_type' => 'FULLTEXT'); + } # else + + return $idxInfo; + } # getIndexInfo + +} # class diff --git a/lib/exceptions/CustomException.php b/lib/exceptions/CustomException.php index b856b7ca7..c88ea4d80 100755 --- a/lib/exceptions/CustomException.php +++ b/lib/exceptions/CustomException.php @@ -1,43 +1,43 @@ -message}' in {$this->file}({$this->line})\n" - . "{$this->getTraceAsString()}"; - } -} +message}' in {$this->file}({$this->line})\n" + . "{$this->getTraceAsString()}"; + } +} ?> \ No newline at end of file diff --git a/lib/exceptions/DatabaseConnectionException.php b/lib/exceptions/DatabaseConnectionException.php index db9a1c47b..95518cb17 100755 --- a/lib/exceptions/DatabaseConnectionException.php +++ b/lib/exceptions/DatabaseConnectionException.php @@ -1,2 +1,2 @@ -_detail = $detail; - $this->_code = $code; - $this->_response = $response; - - parent::__construct($detail . ' [response: "' . $response . '"]', $code); - } # ctor - - +_detail = $detail; + $this->_code = $code; + $this->_response = $response; + + parent::__construct($detail . ' [response: "' . $response . '"]', $code); + } # ctor + + } # NntpException \ No newline at end of file diff --git a/lib/exceptions/OwnsettingsCreatedOutputException.php b/lib/exceptions/OwnsettingsCreatedOutputException.php index 0c9a2369e..7fffa1a27 100755 --- a/lib/exceptions/OwnsettingsCreatedOutputException.php +++ b/lib/exceptions/OwnsettingsCreatedOutputException.php @@ -1,2 +1,2 @@ -_permId = $message; - $this->_object = $code; - parent::__construct("Permission denied [" . $message . "] for objectid [" . $code . "]", 5); - } # ctor - - function getPermId() { - return $this->_permId; - } # getPermId - - function getObject() { - return $this->_object; - } # getObject - -} # class - +_permId = $message; + $this->_object = $code; + parent::__construct("Permission denied [" . $message . "] for objectid [" . $code . "]", 5); + } # ctor + + function getPermId() { + return $this->_permId; + } # getPermId + + function getObject() { + return $this->_object; + } # getObject + +} # class + diff --git a/lib/exceptions/RetrieverRunningException.php b/lib/exceptions/RetrieverRunningException.php index 37aea0c04..0331cc1b1 100755 --- a/lib/exceptions/RetrieverRunningException.php +++ b/lib/exceptions/RetrieverRunningException.php @@ -1,2 +1,2 @@ -boxcarObj = new boxcar_api($dataArray['api_key'], $dataArray['api_secret']); - $this->_appName = $appName; - $this->_dataArray = $dataArray; - } # ctor - - function register() { - $this->boxcarObj->invite($this->_dataArray['email']); - } # register - - function sendMessage($type, $title, $body, $sourceUrl) { - $this->boxcarObj->notify($this->_dataArray['email'], $this->_appName, $body, null, null, $sourceUrl); - } # sendMessage - -} # Notifications_Boxcar +boxcarObj = new boxcar_api($dataArray['api_key'], $dataArray['api_secret']); + $this->_appName = $appName; + $this->_dataArray = $dataArray; + } # ctor + + function register() { + $this->boxcarObj->invite($this->_dataArray['email']); + } # register + + function sendMessage($type, $title, $body, $sourceUrl) { + $this->boxcarObj->notify($this->_dataArray['email'], $this->_appName, $body, null, null, $sourceUrl); + } # sendMessage + +} # Notifications_Boxcar diff --git a/lib/notifications/Notifications_Email.php b/lib/notifications/Notifications_Email.php index d12656fa1..3bb2fe2ea 100755 --- a/lib/notifications/Notifications_Email.php +++ b/lib/notifications/Notifications_Email.php @@ -1,21 +1,21 @@ -_appName = $appName; - $this->_dataArray = $dataArray; - } # ctor - - function register() { - return; - } # register - - function sendMessage($type, $title, $body, $sourceUrl) { - $header = "From: ". $this->_appName . " <" . $this->_dataArray['sender'] . ">\r\n"; - $body = wordwrap($body, 70); - mail($this->_dataArray['receiver'], $title, $body, $header); - } # sendMessage - -} # Notifications_Email +_appName = $appName; + $this->_dataArray = $dataArray; + } # ctor + + function register() { + return; + } # register + + function sendMessage($type, $title, $body, $sourceUrl) { + $header = "From: ". $this->_appName . " <" . $this->_dataArray['sender'] . ">\r\n"; + $body = wordwrap($body, 70); + mail($this->_dataArray['receiver'], $title, $body, $header); + } # sendMessage + +} # Notifications_Email diff --git a/lib/notifications/Notifications_Factory.php b/lib/notifications/Notifications_Factory.php index cd6de4e1c..85a2734f6 100755 --- a/lib/notifications/Notifications_Factory.php +++ b/lib/notifications/Notifications_Factory.php @@ -1,32 +1,32 @@ -growlObj = new Growl($appName); - $this->_connection = array('address' => $dataArray['host'], 'password' => $dataArray['password']); - } # ctor - - function register() { - $this->growlObj->addNotification('Single'); - $this->growlObj->addNotification('Multi'); - $this->growlObj->register($this->_connection); - } # register - - function sendMessage($type, $title, $body, $sourceUrl) { - $this->growlObj->notify($this->_connection, $type, $title, $body); - } # sendMessage - -} # Notifications_Growl +growlObj = new Growl($appName); + $this->_connection = array('address' => $dataArray['host'], 'password' => $dataArray['password']); + } # ctor + + function register() { + $this->growlObj->addNotification('Single'); + $this->growlObj->addNotification('Multi'); + $this->growlObj->register($this->_connection); + } # register + + function sendMessage($type, $title, $body, $sourceUrl) { + $this->growlObj->notify($this->_connection, $type, $title, $body); + } # sendMessage + +} # Notifications_Growl diff --git a/lib/notifications/Notifications_Notifo.php b/lib/notifications/Notifications_Notifo.php index 062566c1b..cadee9a3e 100755 --- a/lib/notifications/Notifications_Notifo.php +++ b/lib/notifications/Notifications_Notifo.php @@ -1,25 +1,25 @@ -notifoObj = new Notifo_API($dataArray['username'], $dataArray['api']); - $this->_appName = $appName; - } # ctor - - function register() { - return; - } # register - - function sendMessage($type, $title, $body, $sourceUrl) { - $params = array('label' => $this->_appName, - 'title' => $title, - 'msg' => $body, - 'uri' => $sourceUrl); - $response = $this->notifoObj->send_notification($params); - } # sendMessage - -} # Notifications_Notifo +notifoObj = new Notifo_API($dataArray['username'], $dataArray['api']); + $this->_appName = $appName; + } # ctor + + function register() { + return; + } # register + + function sendMessage($type, $title, $body, $sourceUrl) { + $params = array('label' => $this->_appName, + 'title' => $title, + 'msg' => $body, + 'uri' => $sourceUrl); + $response = $this->notifoObj->send_notification($params); + } # sendMessage + +} # Notifications_Notifo diff --git a/lib/notifications/Notifications_Prowl.php b/lib/notifications/Notifications_Prowl.php index 7fcd84035..643b72319 100755 --- a/lib/notifications/Notifications_Prowl.php +++ b/lib/notifications/Notifications_Prowl.php @@ -1,48 +1,48 @@ -= 0) { - /* - * We includen de libraries hier en in deze volgorde om te voorkomen - * dat de autoclass loader triggered, die snapt namelijk op dit moment - * nog niets van namespaces en dan gaat het mis - * - * https://github.com/xenji/ProwlPHP - */ - require_once "lib/notifications/prowl/Connector.php"; - require_once "lib/notifications/prowl/Message.php"; - require_once "lib/notifications/prowl/Security/Secureable.php"; - require_once "lib/notifications/prowl/Response.php"; - require_once "lib/notifications/prowl/Security/PassthroughFilterImpl.php"; - - $this->prowlObj = new \Prowl\Connector(); - } # if - - $this->_appName = $appName; - $this->_apikey = $dataArray['apikey']; - } # ctor - - function register() { - return; - } # register - - function sendMessage($type, $title, $body, $sourceUrl) { - if (version_compare(PHP_VERSION, '5.3.0') >= 0) { - $oMsg = new \Prowl\Message(); - $oMsg->addApiKey($this->_apikey); - $oMsg->setApplication($this->_appName); - $oMsg->setEvent($title); - $oMsg->setDescription($body); - - $oFilter = new \Prowl\Security\PassthroughFilterImpl(); - $this->prowlObj->setFilter($oFilter); - $this->prowlObj->setIsPostRequest(true); - $oResponse = $this->prowlObj->push($oMsg); - } # if - } # sendMessage - -} # Notifications_Prowl += 0) { + /* + * We includen de libraries hier en in deze volgorde om te voorkomen + * dat de autoclass loader triggered, die snapt namelijk op dit moment + * nog niets van namespaces en dan gaat het mis + * + * https://github.com/xenji/ProwlPHP + */ + require_once "lib/notifications/prowl/Connector.php"; + require_once "lib/notifications/prowl/Message.php"; + require_once "lib/notifications/prowl/Security/Secureable.php"; + require_once "lib/notifications/prowl/Response.php"; + require_once "lib/notifications/prowl/Security/PassthroughFilterImpl.php"; + + $this->prowlObj = new \Prowl\Connector(); + } # if + + $this->_appName = $appName; + $this->_apikey = $dataArray['apikey']; + } # ctor + + function register() { + return; + } # register + + function sendMessage($type, $title, $body, $sourceUrl) { + if (version_compare(PHP_VERSION, '5.3.0') >= 0) { + $oMsg = new \Prowl\Message(); + $oMsg->addApiKey($this->_apikey); + $oMsg->setApplication($this->_appName); + $oMsg->setEvent($title); + $oMsg->setDescription($body); + + $oFilter = new \Prowl\Security\PassthroughFilterImpl(); + $this->prowlObj->setFilter($oFilter); + $this->prowlObj->setIsPostRequest(true); + $oResponse = $this->prowlObj->push($oMsg); + } # if + } # sendMessage + +} # Notifications_Prowl diff --git a/lib/notifications/Notifications_Twitter.php b/lib/notifications/Notifications_Twitter.php index 9ae5ca69d..3b2b779aa 100755 --- a/lib/notifications/Notifications_Twitter.php +++ b/lib/notifications/Notifications_Twitter.php @@ -1,55 +1,55 @@ -_appName = $appName; - $this->dataArray = $dataArray; - } # ctor - - function register() { - return; - } # register - - function requestAuthorizeURL() { - $this->twitterObj = new TwitterOAuth($this->dataArray['consumer_key'], $this->dataArray['consumer_secret']); - $request_token = $this->twitterObj->getRequestToken(); - - switch ($this->twitterObj->http_code) { - case 200 : $registerURL = $this->twitterObj->getAuthorizeURL($request_token); break; - default : $registerURL = ''; - } # switch - - return array($this->twitterObj->http_code, $request_token, $registerURL); - } # requestAuthorizeURL - - function verifyPIN($pin) { - $this->twitterObj = new TwitterOAuth($this->dataArray['consumer_key'], $this->dataArray['consumer_secret'], $this->dataArray['request_token'], $this->dataArray['request_token_secret']); - $access_token = $this->twitterObj->getAccessToken($pin); - - switch ($this->twitterObj->http_code) { - case 200 : break; - default : $access_token = array(); - } # switch - - return array($this->twitterObj->http_code, $access_token); - } # verifyPIN - - function sendMessage($type, $title, $body, $sourceUrl) { - $this->twitterObj = new TwitterOAuth($this->dataArray['consumer_key'], $this->dataArray['consumer_secret'], $this->dataArray['access_token'], $this->dataArray['access_token_secret']); - - $message = array(); - $message['status'] = substr($this->_appName . ': ' . $body, 0, 140); - if (empty($sourceUrl)) { - $this->twitterObj->post('statuses/update', array('status' => $message)); - } else { - $annotations = array(array('webpage' => array('title' => $this->_appName, 'url' => $sourceUrl))); - $this->twitterObj->post('statuses/update', array('status' => $message, 'annotations' => json_encode($annotations))); - } # if - } # sendMessage - -} # Notifications_Twitter +_appName = $appName; + $this->dataArray = $dataArray; + } # ctor + + function register() { + return; + } # register + + function requestAuthorizeURL() { + $this->twitterObj = new TwitterOAuth($this->dataArray['consumer_key'], $this->dataArray['consumer_secret']); + $request_token = $this->twitterObj->getRequestToken(); + + switch ($this->twitterObj->http_code) { + case 200 : $registerURL = $this->twitterObj->getAuthorizeURL($request_token); break; + default : $registerURL = ''; + } # switch + + return array($this->twitterObj->http_code, $request_token, $registerURL); + } # requestAuthorizeURL + + function verifyPIN($pin) { + $this->twitterObj = new TwitterOAuth($this->dataArray['consumer_key'], $this->dataArray['consumer_secret'], $this->dataArray['request_token'], $this->dataArray['request_token_secret']); + $access_token = $this->twitterObj->getAccessToken($pin); + + switch ($this->twitterObj->http_code) { + case 200 : break; + default : $access_token = array(); + } # switch + + return array($this->twitterObj->http_code, $access_token); + } # verifyPIN + + function sendMessage($type, $title, $body, $sourceUrl) { + $this->twitterObj = new TwitterOAuth($this->dataArray['consumer_key'], $this->dataArray['consumer_secret'], $this->dataArray['access_token'], $this->dataArray['access_token_secret']); + + $message = array(); + $message['status'] = substr($this->_appName . ': ' . $body, 0, 140); + if (empty($sourceUrl)) { + $this->twitterObj->post('statuses/update', array('status' => $message)); + } else { + $annotations = array(array('webpage' => array('title' => $this->_appName, 'url' => $sourceUrl))); + $this->twitterObj->post('statuses/update', array('status' => $message, 'annotations' => json_encode($annotations))); + } # if + } # sendMessage + +} # Notifications_Twitter diff --git a/lib/notifications/Notifications_abs.php b/lib/notifications/Notifications_abs.php index 8a6395df8..e630f64d7 100755 --- a/lib/notifications/Notifications_abs.php +++ b/lib/notifications/Notifications_abs.php @@ -1,12 +1,12 @@ -_spotSec->fatalPermCheck(SpotSecurity::spotsec_keep_own_downloadlist, ''); - $this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_keep_own_downloadlist, 'erasedls'); - - $this->_tplHelper->clearDownloadList(); - - $this->sendExpireHeaders(true); - echo "ok"; - } # render() - +_spotSec->fatalPermCheck(SpotSecurity::spotsec_keep_own_downloadlist, ''); + $this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_keep_own_downloadlist, 'erasedls'); + + $this->_tplHelper->clearDownloadList(); + + $this->sendExpireHeaders(true); + echo "ok"; + } # render() + } # SpotPage_erasedls \ No newline at end of file diff --git a/lib/page/SpotPage_logout.php b/lib/page/SpotPage_logout.php index cb9dd15f4..d4727f851 100755 --- a/lib/page/SpotPage_logout.php +++ b/lib/page/SpotPage_logout.php @@ -1,27 +1,27 @@ -_spotSec->fatalPermCheck(SpotSecurity::spotsec_perform_logout, ''); - - # Instanatiate the spotweb user system - $spotUserSystem = new SpotUserSystem($this->_db, $this->_settings); - - # make sure the logout isn't cached - $this->sendExpireHeaders(true); - - # send the appropriate content-type header - $this->sendContentTypeHeader('xml'); - - # and remove the users' session if the user isn't the anonymous one - if ($this->_currentSession['user']['userid'] != $this->_settings->get('nonauthenticated_userid')) { - $spotUserSystem->removeSession($this->_currentSession['session']['sessionid']); - - echo 'OK'; - } else { - echo 'ERROR'; - } # else - } # render - -} # class SpotPage_logout +_spotSec->fatalPermCheck(SpotSecurity::spotsec_perform_logout, ''); + + # Instanatiate the spotweb user system + $spotUserSystem = new SpotUserSystem($this->_db, $this->_settings); + + # make sure the logout isn't cached + $this->sendExpireHeaders(true); + + # send the appropriate content-type header + $this->sendContentTypeHeader('xml'); + + # and remove the users' session if the user isn't the anonymous one + if ($this->_currentSession['user']['userid'] != $this->_settings->get('nonauthenticated_userid')) { + $spotUserSystem->removeSession($this->_currentSession['session']['sessionid']); + + echo 'OK'; + } else { + echo 'ERROR'; + } # else + } # render + +} # class SpotPage_logout diff --git a/lib/page/SpotPage_markallasread.php b/lib/page/SpotPage_markallasread.php index 46967742f..504120583 100755 --- a/lib/page/SpotPage_markallasread.php +++ b/lib/page/SpotPage_markallasread.php @@ -1,25 +1,25 @@ -_spotSec->fatalPermCheck(SpotSecurity::spotsec_mark_spots_asread, ''); - - # en update het user record - $spotUserSystem = new SpotUserSystem($this->_db, $this->_settings); - - # clear the spotstate list als dit toegestaan is - if ($this->_spotSec->allowed(SpotSecurity::spotsec_keep_own_seenlist, '')) { - $this->_db->markAllAsRead($this->_currentSession['user']['userid']); - } # if - - # we willen niet dat dit gecached wordt - $this->sendExpireHeaders(true); - - # reset the lastvisit en lastread timestamp - $spotUserSystem->resetReadStamp($this->_currentSession['user']); - - echo "ok"; - } # render() - -} # SpotPage_markallasread +_spotSec->fatalPermCheck(SpotSecurity::spotsec_mark_spots_asread, ''); + + # en update het user record + $spotUserSystem = new SpotUserSystem($this->_db, $this->_settings); + + # clear the spotstate list als dit toegestaan is + if ($this->_spotSec->allowed(SpotSecurity::spotsec_keep_own_seenlist, '')) { + $this->_db->markAllAsRead($this->_currentSession['user']['userid']); + } # if + + # we willen niet dat dit gecached wordt + $this->sendExpireHeaders(true); + + # reset the lastvisit en lastread timestamp + $spotUserSystem->resetReadStamp($this->_currentSession['user']); + + echo "ok"; + } # render() + +} # SpotPage_markallasread diff --git a/lib/page/SpotPage_sabapi.php b/lib/page/SpotPage_sabapi.php index 30236e7c0..6ff2899b5 100755 --- a/lib/page/SpotPage_sabapi.php +++ b/lib/page/SpotPage_sabapi.php @@ -1,42 +1,42 @@ -_spotSec->fatalPermCheck(SpotSecurity::spotsec_use_sabapi, ''); - - parse_str($_SERVER['QUERY_STRING'], $request); - $nzbhandling = $this->_currentSession['user']['prefs']['nzbhandling']; - $sabnzbd = $nzbhandling['sabnzbd']; - - if ($nzbhandling['action'] != 'push-sabnzbd' && $nzbhandling['action'] != 'client-sabnzbd') { - die ('SABzndb is not configured on this node.'); - } elseif (!isset($request['sabapikey'])) { - die ('API Key Required'); - } elseif ($this->_tplHelper->apiToHash($this->_currentSession['user']['apikey']) != $request['sabapikey']) { - die ('API Key Incorrect'); - } # else - - # de sabapi output moet niet gecached worden - $this->sendExpireHeaders(true); - - if (stripos($_SERVER['QUERY_STRING'], 'output=xml')) { - $this->sendContentTypeHeader('xml'); - } elseif (stripos($_SERVER['QUERY_STRING'], 'output=json')) { - $this->sendContentTypeHeader('json'); - } # else - - $apicall = array(); - foreach($request as $key => $value) { - if ($key != 'page' && $key != 'sabapikey') { - $apicall[] = $key . '=' . $value; - } # if - } # foreach - $request = implode('&', $apicall); - - $output = @file_get_contents($sabnzbd['url'] . 'sabnzbd/api?' . $request . '&apikey=' . $sabnzbd['apikey']); - - echo $output; - } # render - +_spotSec->fatalPermCheck(SpotSecurity::spotsec_use_sabapi, ''); + + parse_str($_SERVER['QUERY_STRING'], $request); + $nzbhandling = $this->_currentSession['user']['prefs']['nzbhandling']; + $sabnzbd = $nzbhandling['sabnzbd']; + + if ($nzbhandling['action'] != 'push-sabnzbd' && $nzbhandling['action'] != 'client-sabnzbd') { + die ('SABzndb is not configured on this node.'); + } elseif (!isset($request['sabapikey'])) { + die ('API Key Required'); + } elseif ($this->_tplHelper->apiToHash($this->_currentSession['user']['apikey']) != $request['sabapikey']) { + die ('API Key Incorrect'); + } # else + + # de sabapi output moet niet gecached worden + $this->sendExpireHeaders(true); + + if (stripos($_SERVER['QUERY_STRING'], 'output=xml')) { + $this->sendContentTypeHeader('xml'); + } elseif (stripos($_SERVER['QUERY_STRING'], 'output=json')) { + $this->sendContentTypeHeader('json'); + } # else + + $apicall = array(); + foreach($request as $key => $value) { + if ($key != 'page' && $key != 'sabapikey') { + $apicall[] = $key . '=' . $value; + } # if + } # foreach + $request = implode('&', $apicall); + + $output = @file_get_contents($sabnzbd['url'] . 'sabnzbd/api?' . $request . '&apikey=' . $sabnzbd['apikey']); + + echo $output; + } # render + } # class SpotPage_sabapi \ No newline at end of file diff --git a/lib/page/SpotPage_versioncheck.php b/lib/page/SpotPage_versioncheck.php index 10fbae311..f638d6b96 100755 --- a/lib/page/SpotPage_versioncheck.php +++ b/lib/page/SpotPage_versioncheck.php @@ -1,17 +1,17 @@ -_spotSec->fatalPermCheck(SpotSecurity::spotsec_view_spotweb_updates, ''); - - # And instantiate the Spotweb version checker - $versionCheck = new SpotWebVersionCheck(); - $itemList = $versionCheck->getItems(); - - #- display stuff -# - $this->template('versioncheck', array('items' => $itemList, - 'uptodate' => $versionCheck->isLatestVersion($itemList[0]))); - } # render() - +_spotSec->fatalPermCheck(SpotSecurity::spotsec_view_spotweb_updates, ''); + + # And instantiate the Spotweb version checker + $versionCheck = new SpotWebVersionCheck(); + $itemList = $versionCheck->getItems(); + + #- display stuff -# + $this->template('versioncheck', array('items' => $itemList, + 'uptodate' => $versionCheck->isLatestVersion($itemList[0]))); + } # render() + } # SpotPage_versioncheck \ No newline at end of file diff --git a/notifications.xml b/notifications.xml index 8a2aefebf..13bb387a7 100755 --- a/notifications.xml +++ b/notifications.xml @@ -1,279 +1,279 @@ - - - - Spotweb update notifications - Major changes are described in this newsfeed - https://github.com/spotweb/spotweb/ - Tue, 29 May 2012 00:00:00 +0100 - Tue, 29 May 2012 00:00:00 +0100 - 1800 - - - - Added permissions for specific templates - - After a run of upgrade-db.php the permissions for the default templates are automaticaly added. - ]]> - https://github.com/spotweb/spotweb/ - 11 - Sun, 29 May 2012 00:00:00 +0100 - spotweb - - 0.58 - 0.23 - 0.29 - 0.06 - - - - - Installer changes - - First, you need to remove the database information from your "ownsettings.php" file for now. You can either run the Spotweb installation wizard (recommended way) but this will create a new user and reset group membership for all users. An alternative way is to create an own dbsettings.inc.php file. Please consult the Spotweb wiki for more information about creating this dbsettings.inc.php file. - ]]> - https://github.com/spotweb/spotweb/ - 11 - Sun, 6 May 2012 00:00:00 +0100 - spotweb - - 0.58 - 0.23 - 0.28 - 0.06 - - - - Database changes - - https://github.com/spotweb/spotweb/ - 11 - Mon, 27 Feb 2012 00:00:00 +0100 - spotweb - - 0.58 - 0.20 - 0.27 - 0.06 - - - - Database changes - - https://github.com/spotweb/spotweb/ - 11 - Mon, 6 Feb 2012 00:00:00 +0100 - MassD67 - - 0.57 - 0.20 - 0.27 - 0.06 - - - - - Database changes - - https://github.com/spotweb/spotweb/ - 10 - Mon, 6 Feb 2012 00:00:00 +0100 - MassD67 - - 0.55 - 0.19 - 0.27 - 0.06 - - - - - - Systemwide Custom CSS - - https://github.com/spotweb/spotweb/ - 09 - Tue, 31 Jan 2012 10:30:00 +0100 - spotweb - - 0.54 - 0.19 - 0.27 - 0.06 - - - - - Security group reset -
      - In order to facilitate this better, the default Spotweb security groups have been changed adding two types of groups: "Trusted users" and "Anonymous user - closed system", - these groups allow you to support more finegrained security permissions without tinkering too much with the settings in Spotweb. -

      - Unfortunately this means we have to reset all security groups within Spotweb - this means that if you customized any security settings within Spotweb that you have to check - the system does not respond any differently than you want. -

      - If you want a closed system, please remove the "Anonymous" user from the "Anonymous user - open system" group, and add it to the "Anonymous user - closed system" securitygroup. - ]]>
      - https://github.com/spotweb/spotweb/ - 08 - Mon, 23 Dec 2011 00:00:00 +0100 - spotweb - - 0.54 - 0.18 - 0.27 - 0.06 -
      - - - Quick Links linked to user preferences - - If you have quicklinks set manually in your (own)settings.php, they need to be updated in this version. Otherwise everything goes automaticlly. - ]]> - https://github.com/spotweb/spotweb/ - 07 - Mon, 5 Dec 2011 09:00:00 +0100 - nightspirit81 - - 0.54 - 0.18 - 0.26 - 0.06 - - - - Optimized spotcounting -
      - In this new version Spotweb uses a slightly less accurate, but much faster, method to determine which spots are new. This requires a very small - schema change which will not take a long time to process.

      - After the database upgrade, a retrieve.php run should be done. This first retrieve run might take a very long time to complete as initial statistics - are gathered at that moment. From then on, a very small retrieve.php impact might be shown but should be neglectable. Spotcounts however are not - limited anymore and should be much faster.
      - ]]>
      - https://github.com/spotweb/spotweb/ - 06 - Tue, 29 Nov 2011 21:57:00 +0100 - spotweb - - 0.54 - 0.16 - 0.26 - 0.05 -
      - - - Moving settings to database - - https://github.com/spotweb/spotweb/ - 07 - Tue, 29 Nov 2011 19:45:00 +0100 - nightspirit81 - - 0.54 - 0.17 - 0.26 - 0.04 - - - - Introducing settings panel - -
      - Please note that the old 'Admin Panel' has been renamed to 'User & group management'. - ]]>
      - https://github.com/spotweb/spotweb/ - 05 - Sun, 27 Nov 2011 20:45:00 +0100 - nightspirit81 - - 0.53 - 0.16 - 0.26 - 0.03 -
      - - - Keeping the IP address for a session - This requires a - database upgrade which should be done rather quick unless your sessions table is very large (which is unlikely). - ]]> - https://github.com/spotweb/spotweb/ - 04 - Sun, 27 Nov 2011 09:45:00 +0100 - spotweb - - 0.53 - 0.15 - 0.25 - 0.02 - - - - Introduced Spotweb change updates - -
      - For all comments an extra button is added which adds the user to the senders' blacklist]]>
      - https://github.com/spotweb/spotweb/ - 03 - Fri, 25 Nov 2011 18:53:00 +0100 - spotweb - - 0.52 - 0.15 - 0.24 - 0.01 -
      - - - Introduced Spotweb change updates - -
      - Please note that a lot of minor changes will not be communicated here and we recommend to keep your Spotweb installation always up to date.]]>
      - https://github.com/spotweb/spotweb/ - 02 - Fri, 25 Nov 2011 07:04:00 +0100 - spotweb - - 0.52 - 0.15 - 0.24 - 0.01 -
      - - - Comment moderation - - https://github.com/spotweb/spotweb/ - 01 - Wed, 23 Nov 2011 23:00:00 +0100 - spotweb - - 0.52 - 0.15 - 0.24 - 0.00 - -
      -
      - + + + + Spotweb update notifications + Major changes are described in this newsfeed + https://github.com/spotweb/spotweb/ + Tue, 29 May 2012 00:00:00 +0100 + Tue, 29 May 2012 00:00:00 +0100 + 1800 + + + + Added permissions for specific templates + + After a run of upgrade-db.php the permissions for the default templates are automaticaly added. + ]]> + https://github.com/spotweb/spotweb/ + 11 + Sun, 29 May 2012 00:00:00 +0100 + spotweb + + 0.58 + 0.23 + 0.29 + 0.06 + + + + + Installer changes + + First, you need to remove the database information from your "ownsettings.php" file for now. You can either run the Spotweb installation wizard (recommended way) but this will create a new user and reset group membership for all users. An alternative way is to create an own dbsettings.inc.php file. Please consult the Spotweb wiki for more information about creating this dbsettings.inc.php file. + ]]> + https://github.com/spotweb/spotweb/ + 11 + Sun, 6 May 2012 00:00:00 +0100 + spotweb + + 0.58 + 0.23 + 0.28 + 0.06 + + + + Database changes + + https://github.com/spotweb/spotweb/ + 11 + Mon, 27 Feb 2012 00:00:00 +0100 + spotweb + + 0.58 + 0.20 + 0.27 + 0.06 + + + + Database changes + + https://github.com/spotweb/spotweb/ + 11 + Mon, 6 Feb 2012 00:00:00 +0100 + MassD67 + + 0.57 + 0.20 + 0.27 + 0.06 + + + + + Database changes + + https://github.com/spotweb/spotweb/ + 10 + Mon, 6 Feb 2012 00:00:00 +0100 + MassD67 + + 0.55 + 0.19 + 0.27 + 0.06 + + + + + + Systemwide Custom CSS + + https://github.com/spotweb/spotweb/ + 09 + Tue, 31 Jan 2012 10:30:00 +0100 + spotweb + + 0.54 + 0.19 + 0.27 + 0.06 + + + + + Security group reset +
      + In order to facilitate this better, the default Spotweb security groups have been changed adding two types of groups: "Trusted users" and "Anonymous user - closed system", + these groups allow you to support more finegrained security permissions without tinkering too much with the settings in Spotweb. +

      + Unfortunately this means we have to reset all security groups within Spotweb - this means that if you customized any security settings within Spotweb that you have to check + the system does not respond any differently than you want. +

      + If you want a closed system, please remove the "Anonymous" user from the "Anonymous user - open system" group, and add it to the "Anonymous user - closed system" securitygroup. + ]]>
      + https://github.com/spotweb/spotweb/ + 08 + Mon, 23 Dec 2011 00:00:00 +0100 + spotweb + + 0.54 + 0.18 + 0.27 + 0.06 +
      + + + Quick Links linked to user preferences + + If you have quicklinks set manually in your (own)settings.php, they need to be updated in this version. Otherwise everything goes automaticlly. + ]]> + https://github.com/spotweb/spotweb/ + 07 + Mon, 5 Dec 2011 09:00:00 +0100 + nightspirit81 + + 0.54 + 0.18 + 0.26 + 0.06 + + + + Optimized spotcounting +
      + In this new version Spotweb uses a slightly less accurate, but much faster, method to determine which spots are new. This requires a very small + schema change which will not take a long time to process.

      + After the database upgrade, a retrieve.php run should be done. This first retrieve run might take a very long time to complete as initial statistics + are gathered at that moment. From then on, a very small retrieve.php impact might be shown but should be neglectable. Spotcounts however are not + limited anymore and should be much faster.
      + ]]>
      + https://github.com/spotweb/spotweb/ + 06 + Tue, 29 Nov 2011 21:57:00 +0100 + spotweb + + 0.54 + 0.16 + 0.26 + 0.05 +
      + + + Moving settings to database + + https://github.com/spotweb/spotweb/ + 07 + Tue, 29 Nov 2011 19:45:00 +0100 + nightspirit81 + + 0.54 + 0.17 + 0.26 + 0.04 + + + + Introducing settings panel + +
      + Please note that the old 'Admin Panel' has been renamed to 'User & group management'. + ]]>
      + https://github.com/spotweb/spotweb/ + 05 + Sun, 27 Nov 2011 20:45:00 +0100 + nightspirit81 + + 0.53 + 0.16 + 0.26 + 0.03 +
      + + + Keeping the IP address for a session + This requires a + database upgrade which should be done rather quick unless your sessions table is very large (which is unlikely). + ]]> + https://github.com/spotweb/spotweb/ + 04 + Sun, 27 Nov 2011 09:45:00 +0100 + spotweb + + 0.53 + 0.15 + 0.25 + 0.02 + + + + Introduced Spotweb change updates + +
      + For all comments an extra button is added which adds the user to the senders' blacklist]]>
      + https://github.com/spotweb/spotweb/ + 03 + Fri, 25 Nov 2011 18:53:00 +0100 + spotweb + + 0.52 + 0.15 + 0.24 + 0.01 +
      + + + Introduced Spotweb change updates + +
      + Please note that a lot of minor changes will not be communicated here and we recommend to keep your Spotweb installation always up to date.]]>
      + https://github.com/spotweb/spotweb/ + 02 + Fri, 25 Nov 2011 07:04:00 +0100 + spotweb + + 0.52 + 0.15 + 0.24 + 0.01 +
      + + + Comment moderation + + https://github.com/spotweb/spotweb/ + 01 + Wed, 23 Nov 2011 23:00:00 +0100 + spotweb + + 0.52 + 0.15 + 0.24 + 0.00 + +
      +
      + diff --git a/settings.php b/settings.php index 1d0156777..452046d8a 100755 --- a/settings.php +++ b/settings.php @@ -1,150 +1,150 @@ - 2 && !filter_var(ltrim($cookie_domain, '.'), FILTER_VALIDATE_IP)) { - $settings['cookie_host'] = $cookie_domain; - unset($cookie_domain); -} else { - $settings['cookie_host'] = ''; -} # else - -# vertaal de categorieen uit spots (zie SpotCategories.php) naar sabnzbd categorieen -$settings['sabnzbd']['categories'] = Array( - 0 => Array('default' => "movies", # Default categorie als niets anders matched - 'a5' => "books", - 'd2' => "anime", - 'd11' => "tv", - 'd29' => "anime"), - 1 => Array('default' => 'music'), - 2 => Array('default' => 'games'), - 3 => Array('default' => 'apps', - 'a3' => 'consoles', - 'a3' => 'consoles', - 'a4' => 'consoles', - 'a5' => 'consoles', - 'a6' => 'consoles', - 'a7' => 'consoles', - 'a8' => 'consoles', - 'a9' => 'consoles', - 'a10' => 'consoles', - 'a11' => 'consoles', - 'a12' => 'consoles', - 'a13' => 'pda', - 'a14' => 'pda', - 'a15' => 'pda') - ); - -# -# Include eventueel eigen settings, dit is ook een PHP file. -# Settings welke hierin staan zullen de instellingen van deze file overiden. -# -# We raden aan om je instellingen in deze eigen file te zetten zodat bij een upgrade -# je instellingen bewaard blijven. -# -if (@file_exists('../ownsettings.php')) { include_once('../ownsettings.php'); } # <== deze lijn mag je eventueel verwijderen -if (file_exists('ownsettings.php')) { include_once('ownsettings.php'); } # <== deze lijn mag je eventueel verwijderen - -# QuickLinks, we testen eerst of hij niet al door iemand anders is gezet in ownsettings.php en -# anders vullen we hem zelf op. We kunnen dit niet boven ownsettings.php plaatsen want dan missen -# we de keep_watchlist en keep_downloadlist settings. -if (!isset($settings['quicklinks'])) { - $settings['quicklinks'] = Array(); - $settings['quicklinks'][] = Array('Reset filters', "home", "?search[tree]=&search[unfiltered]=true", "", Array(SpotSecurity::spotsec_view_spots_index, ''), null); - $settings['quicklinks'][] = Array('New', "today", "?search[tree]=&search[unfiltered]=true&search[value][]=New:0", "", Array(SpotSecurity::spotsec_keep_own_seenlist, ''), 'count_newspots'); - $settings['quicklinks'][] = Array('Watchlist', "fav", "?search[tree]=&search[unfiltered]=true&search[value][]=Watch:0", "", Array(SpotSecurity::spotsec_keep_own_watchlist, ''), 'keep_watchlist'); - $settings['quicklinks'][] = Array('Downloaded', "download", "?search[tree]=&search[unfiltered]=true&search[value][]=Downloaded:0", "", Array(SpotSecurity::spotsec_keep_own_downloadlist, ''), 'keep_downloadlist'); - $settings['quicklinks'][] = Array('Recently viewed', "eye", "?search[tree]=&search[unfiltered]=true&search[value][]=Seen:0", "", Array(SpotSecurity::spotsec_keep_own_seenlist, ''), 'keep_seenlist'); - $settings['quicklinks'][] = Array('My spots', "fav", "?search[tree]=&search[unfiltered]=true&search[value][]=MyPostedSpots:0", "", Array(SpotSecurity::spotsec_post_spot, ''), null); - $settings['quicklinks'][] = Array('Statistics', "stats", "?page=statistics", "", Array(SpotSecurity::spotsec_view_statistics, ''), null); - $settings['quicklinks'][] = Array('Documentation', "help", "https://github.com/spotweb/spotweb/wiki", "external", Array(SpotSecurity::spotsec_view_spots_index, ''), null); -} # if isset - -# Als de OpenSSL module geladen is, moet de openssl_cnf_path naar een -# leesbare configuratie file wijzen -if ((!is_readable($settings['openssl_cnf_path'])) && (extension_loaded("openssl"))) { - throw new InvalidOwnSettingsSettingException("openssl_cnf_path does not contain a readable OpenSSL configuration filepath"); -} # if - -# Voeg een sluitende slash toe als die er nog niet is -if (substr($settings['spotweburl'], -1) != '/') { - $settings['spotweburl'] .= '/'; -} # if - -# Preferences lokaal niet meer toestaan -if (isset($settings['prefs'])) { - throw new InvalidOwnSettingsSettingException("Preferences are set per user, not in your ownsettings.php"); -} # if - -# deprecated settings niet meer toestaan -$ownsettingserror = ''; -$array = array('blacklist_url', 'cookie_expires', 'deny_robots', 'enable_stacktrace', 'enable_timing', 'external_blacklist', 'nntp_hdr', - 'nntp_nzb', 'nntp_post', 'prefetch_image', 'prefetch_nzb', 'retention', 'retrieve_comments', 'retrieve_full', 'retrieve_full_comments', - 'retrieve_increment', 'retrieve_newer_than', 'retrieve_reports', 'sendwelcomemail', 'spot_moderation', 'allow_user_template', - 'auto_markasread', 'filters', 'index_filter', 'keep_downloadlist', 'keep_watchlist', 'nzb_search_engine', 'nzbhandling', 'show_multinzb', - 'count_newspots', 'keep_seenlist', 'show_nzbbutton', 'show_updatebutton', 'newuser_grouplist', 'nonauthenticated_userid', - 'templates'); -foreach ($array as $value) { - if (isset($settings[$value])) { - $ownsettingserror .= ' * ' . $value . PHP_EOL; - } # if -} # foreach - -if (!empty($ownsettingserror)) { - throw new InvalidOwnSettingsSettingException("Please remove " . $ownsettingserror . " from your 'ownsettings.php' file, this setting is set in the settings panel from within Spotweb itself"); -} # if - -# Controleer op oud type quicklinks (zonder preference link) -foreach($settings['quicklinks'] as $link) { - if (count($link) < 6) { - throw new InvalidOwnSettingsSettingException("Quicklinks have to have a preferences check as well. Please modify the quickinks in your ownettings.php or remove them from your ownsetings.php"); - } # if -} # foreach - -/* - * First make sure no database settings are left in the main ownsettings.php anymore, as this is the first - * part to deprecating the kludge that settings.php has become completely. - */ -if (!empty($settings['db'])) { - throw new InvalidOwnSettingsSettingException("You need to remove the database settings from your ownsettings.php file and open install.php from your webbrowser. If you are upgrading, please consult https://github.com/spotweb/spotweb/wiki/Frequently-asked-questions/ first"); -} # if - -/* - * Allow database settings to be entered in dbsettings.inc.php - */ -@include "dbsettings.inc.php"; -if (empty($dbsettings)) { - throw new InvalidOwnSettingsSettingException("No databasesettings have been entered, please use the 'install.php' wizard to install and configure Spotweb" . PHP_EOL . - "If you are upgrading from an earlier version of Spotweb, please consult https://github.com/spotweb/spotweb/wiki/Frequently-asked-questions/ first"); -} else { - $settings['db'] = $dbsettings; -} # else - -if (file_exists('reallymyownsettings.php')) { include_once('reallymyownsettings.php'); } + 2 && !filter_var(ltrim($cookie_domain, '.'), FILTER_VALIDATE_IP)) { + $settings['cookie_host'] = $cookie_domain; + unset($cookie_domain); +} else { + $settings['cookie_host'] = ''; +} # else + +# vertaal de categorieen uit spots (zie SpotCategories.php) naar sabnzbd categorieen +$settings['sabnzbd']['categories'] = Array( + 0 => Array('default' => "movies", # Default categorie als niets anders matched + 'a5' => "books", + 'd2' => "anime", + 'd11' => "tv", + 'd29' => "anime"), + 1 => Array('default' => 'music'), + 2 => Array('default' => 'games'), + 3 => Array('default' => 'apps', + 'a3' => 'consoles', + 'a3' => 'consoles', + 'a4' => 'consoles', + 'a5' => 'consoles', + 'a6' => 'consoles', + 'a7' => 'consoles', + 'a8' => 'consoles', + 'a9' => 'consoles', + 'a10' => 'consoles', + 'a11' => 'consoles', + 'a12' => 'consoles', + 'a13' => 'pda', + 'a14' => 'pda', + 'a15' => 'pda') + ); + +# +# Include eventueel eigen settings, dit is ook een PHP file. +# Settings welke hierin staan zullen de instellingen van deze file overiden. +# +# We raden aan om je instellingen in deze eigen file te zetten zodat bij een upgrade +# je instellingen bewaard blijven. +# +if (@file_exists('../ownsettings.php')) { include_once('../ownsettings.php'); } # <== deze lijn mag je eventueel verwijderen +if (file_exists('ownsettings.php')) { include_once('ownsettings.php'); } # <== deze lijn mag je eventueel verwijderen + +# QuickLinks, we testen eerst of hij niet al door iemand anders is gezet in ownsettings.php en +# anders vullen we hem zelf op. We kunnen dit niet boven ownsettings.php plaatsen want dan missen +# we de keep_watchlist en keep_downloadlist settings. +if (!isset($settings['quicklinks'])) { + $settings['quicklinks'] = Array(); + $settings['quicklinks'][] = Array('Reset filters', "home", "?search[tree]=&search[unfiltered]=true", "", Array(SpotSecurity::spotsec_view_spots_index, ''), null); + $settings['quicklinks'][] = Array('New', "today", "?search[tree]=&search[unfiltered]=true&search[value][]=New:0", "", Array(SpotSecurity::spotsec_keep_own_seenlist, ''), 'count_newspots'); + $settings['quicklinks'][] = Array('Watchlist', "fav", "?search[tree]=&search[unfiltered]=true&search[value][]=Watch:0", "", Array(SpotSecurity::spotsec_keep_own_watchlist, ''), 'keep_watchlist'); + $settings['quicklinks'][] = Array('Downloaded', "download", "?search[tree]=&search[unfiltered]=true&search[value][]=Downloaded:0", "", Array(SpotSecurity::spotsec_keep_own_downloadlist, ''), 'keep_downloadlist'); + $settings['quicklinks'][] = Array('Recently viewed', "eye", "?search[tree]=&search[unfiltered]=true&search[value][]=Seen:0", "", Array(SpotSecurity::spotsec_keep_own_seenlist, ''), 'keep_seenlist'); + $settings['quicklinks'][] = Array('My spots', "fav", "?search[tree]=&search[unfiltered]=true&search[value][]=MyPostedSpots:0", "", Array(SpotSecurity::spotsec_post_spot, ''), null); + $settings['quicklinks'][] = Array('Statistics', "stats", "?page=statistics", "", Array(SpotSecurity::spotsec_view_statistics, ''), null); + $settings['quicklinks'][] = Array('Documentation', "help", "https://github.com/spotweb/spotweb/wiki", "external", Array(SpotSecurity::spotsec_view_spots_index, ''), null); +} # if isset + +# Als de OpenSSL module geladen is, moet de openssl_cnf_path naar een +# leesbare configuratie file wijzen +if ((!is_readable($settings['openssl_cnf_path'])) && (extension_loaded("openssl"))) { + throw new InvalidOwnSettingsSettingException("openssl_cnf_path does not contain a readable OpenSSL configuration filepath"); +} # if + +# Voeg een sluitende slash toe als die er nog niet is +if (substr($settings['spotweburl'], -1) != '/') { + $settings['spotweburl'] .= '/'; +} # if + +# Preferences lokaal niet meer toestaan +if (isset($settings['prefs'])) { + throw new InvalidOwnSettingsSettingException("Preferences are set per user, not in your ownsettings.php"); +} # if + +# deprecated settings niet meer toestaan +$ownsettingserror = ''; +$array = array('blacklist_url', 'cookie_expires', 'deny_robots', 'enable_stacktrace', 'enable_timing', 'external_blacklist', 'nntp_hdr', + 'nntp_nzb', 'nntp_post', 'prefetch_image', 'prefetch_nzb', 'retention', 'retrieve_comments', 'retrieve_full', 'retrieve_full_comments', + 'retrieve_increment', 'retrieve_newer_than', 'retrieve_reports', 'sendwelcomemail', 'spot_moderation', 'allow_user_template', + 'auto_markasread', 'filters', 'index_filter', 'keep_downloadlist', 'keep_watchlist', 'nzb_search_engine', 'nzbhandling', 'show_multinzb', + 'count_newspots', 'keep_seenlist', 'show_nzbbutton', 'show_updatebutton', 'newuser_grouplist', 'nonauthenticated_userid', + 'templates'); +foreach ($array as $value) { + if (isset($settings[$value])) { + $ownsettingserror .= ' * ' . $value . PHP_EOL; + } # if +} # foreach + +if (!empty($ownsettingserror)) { + throw new InvalidOwnSettingsSettingException("Please remove " . $ownsettingserror . " from your 'ownsettings.php' file, this setting is set in the settings panel from within Spotweb itself"); +} # if + +# Controleer op oud type quicklinks (zonder preference link) +foreach($settings['quicklinks'] as $link) { + if (count($link) < 6) { + throw new InvalidOwnSettingsSettingException("Quicklinks have to have a preferences check as well. Please modify the quickinks in your ownettings.php or remove them from your ownsetings.php"); + } # if +} # foreach + +/* + * First make sure no database settings are left in the main ownsettings.php anymore, as this is the first + * part to deprecating the kludge that settings.php has become completely. + */ +if (!empty($settings['db'])) { + throw new InvalidOwnSettingsSettingException("You need to remove the database settings from your ownsettings.php file and open install.php from your webbrowser. If you are upgrading, please consult https://github.com/spotweb/spotweb/wiki/Frequently-asked-questions/ first"); +} # if + +/* + * Allow database settings to be entered in dbsettings.inc.php + */ +@include "dbsettings.inc.php"; +if (empty($dbsettings)) { + throw new InvalidOwnSettingsSettingException("No databasesettings have been entered, please use the 'install.php' wizard to install and configure Spotweb" . PHP_EOL . + "If you are upgrading from an earlier version of Spotweb, please consult https://github.com/spotweb/spotweb/wiki/Frequently-asked-questions/ first"); +} else { + $settings['db'] = $dbsettings; +} # else + +if (file_exists('reallymyownsettings.php')) { include_once('reallymyownsettings.php'); } diff --git a/templates/mobile/includes/form-messages.inc.php b/templates/mobile/includes/form-messages.inc.php index 86846a80f..69647b74c 100755 --- a/templates/mobile/includes/form-messages.inc.php +++ b/templates/mobile/includes/form-messages.inc.php @@ -1,16 +1,16 @@ -"; - if (isset($formmessages['errors'])) { - foreach($formmessages['errors'] as $formError) { - echo "
    • " . $formError . "
    • "; - } # foreach - } # if - echo "
    "; - - echo "
      "; - if (isset($formmessages['info'])) { - foreach($formmessages['info'] as $formInfo) { - echo "
    • " . $formInfo . "
    • "; - } # foreach - } # if - echo "
    "; +"; + if (isset($formmessages['errors'])) { + foreach($formmessages['errors'] as $formError) { + echo "
  • " . $formError . "
  • "; + } # foreach + } # if + echo "
"; + + echo "
    "; + if (isset($formmessages['info'])) { + foreach($formmessages['info'] as $formInfo) { + echo "
  • " . $formInfo . "
  • "; + } # foreach + } # if + echo "
"; diff --git a/templates/mobile/login.inc.php b/templates/mobile/login.inc.php index a6862b14e..02e133e6d 100755 --- a/templates/mobile/login.inc.php +++ b/templates/mobile/login.inc.php @@ -1,55 +1,55 @@ -redirect($tplHelper->makeBaseUrl('')); - - return ; - } # if -} # if - -if (($currentSession['user']['userid'] == $settings->get('nonauthenticated_userid')) && (empty($loginresult))) { - include "includes/form-messages.inc.php"; - -?> - - - - Inloggen - - - - - - - - -
- -
-

Spotweb

-
-
-
- - - - -
- -
-
- -
- -
-
-
- -
- - +redirect($tplHelper->makeBaseUrl('')); + + return ; + } # if +} # if + +if (($currentSession['user']['userid'] == $settings->get('nonauthenticated_userid')) && (empty($loginresult))) { + include "includes/form-messages.inc.php"; + +?> + + + + Inloggen + + + + + + + + +
+ +
+

Spotweb

+
+
+
+ + + + +
+ +
+
+ +
+ +
+
+
+ +
+ + \ No newline at end of file diff --git a/templates/mobile/logincontrol.inc.php b/templates/mobile/logincontrol.inc.php index 078f11ef4..5ab5d93f0 100644 --- a/templates/mobile/logincontrol.inc.php +++ b/templates/mobile/logincontrol.inc.php @@ -1,5 +1,5 @@ -get('nonauthenticated_userid')): ?> -   -    -  -(uitloggen) - +get('nonauthenticated_userid')): ?> +   -    +  +(uitloggen) + diff --git a/templates/mobile/permdenied.inc.php b/templates/mobile/permdenied.inc.php index 3dee2ad5e..919453a02 100755 --- a/templates/mobile/permdenied.inc.php +++ b/templates/mobile/permdenied.inc.php @@ -1,49 +1,49 @@ - - - - - - -
-
-

- Toegang geweigerd voor [permToString($exception->getPermId()); ?> (getPermId(); ?>)]::[getObject(); ?>] -

-
-
- -
- - allowed(SpotSecurity::spotsec_perform_login, '') && ($currentSession['user']['userid'] == $settings->get('nonauthenticated_userid'))) { - # loginform verwacht deze twee variables door de renderer, dus die faken we - $data['performredirect'] = true; - $loginform = array('username' => '', 'password' => ''); - - require_once "login.inc.php"; - } # if - ?> - - + + + + + + +
+
+

+ Toegang geweigerd voor [permToString($exception->getPermId()); ?> (getPermId(); ?>)]::[getObject(); ?>] +

+
+
+ +
+ + allowed(SpotSecurity::spotsec_perform_login, '') && ($currentSession['user']['userid'] == $settings->get('nonauthenticated_userid'))) { + # loginform verwacht deze twee variables door de renderer, dus die faken we + $data['performredirect'] = true; + $loginform = array('username' => '', 'password' => ''); + + require_once "login.inc.php"; + } # if + ?> + + diff --git a/vendor/pchart/pData.class.php b/vendor/pchart/pData.class.php index 23123da75..275051926 100755 --- a/vendor/pchart/pData.class.php +++ b/vendor/pchart/pData.class.php @@ -1,787 +1,787 @@ -array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100), - "1"=>array("R"=>224,"G"=>100,"B"=>46,"Alpha"=>100), - "2"=>array("R"=>224,"G"=>214,"B"=>46,"Alpha"=>100), - "3"=>array("R"=>46,"G"=>151,"B"=>224,"Alpha"=>100), - "4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100), - "5"=>array("R"=>224,"G"=>46,"B"=>117,"Alpha"=>100), - "6"=>array("R"=>92,"G"=>224,"B"=>46,"Alpha"=>100), - "7"=>array("R"=>224,"G"=>176,"B"=>46,"Alpha"=>100)); - - /* Class creator */ - function pData() - { - $this->Data = ""; - $this->Data["XAxisDisplay"] = AXIS_FORMAT_DEFAULT; - $this->Data["XAxisFormat"] = NULL; - $this->Data["XAxisName"] = NULL; - $this->Data["XAxisUnit"] = NULL; - $this->Data["Abscissa"] = NULL; - $this->Data["AbsicssaPosition"] = AXIS_POSITION_BOTTOM; - - $this->Data["Axis"][0]["Display"] = AXIS_FORMAT_DEFAULT; - $this->Data["Axis"][0]["Position"] = AXIS_POSITION_LEFT; - $this->Data["Axis"][0]["Identity"] = AXIS_Y; - } - - /* Add a single point or an array to the given serie */ - function addPoints($Values,$SerieName="Serie1") - { - if (!isset($this->Data["Series"][$SerieName])) - $this->initialise($SerieName); - - if ( is_array($Values) ) - { - foreach($Values as $Key => $Value) - { $this->Data["Series"][$SerieName]["Data"][] = $Value; } - } - else - $this->Data["Series"][$SerieName]["Data"][] = $Values; - - if ( $Values != VOID ) - { - $StrippedData = $this->stripVOID($this->Data["Series"][$SerieName]["Data"]); - if ( empty($StrippedData) ) { $this->Data["Series"][$SerieName]["Max"] = 0; $this->Data["Series"][$SerieName]["Min"] =0; return(0); } - $this->Data["Series"][$SerieName]["Max"] = max($StrippedData); - $this->Data["Series"][$SerieName]["Min"] = min($StrippedData); - } - } - - /* Strip VOID values */ - function stripVOID($Values) - { if (!is_array($Values)) { return(array()); } $Result = array(); foreach($Values as $Key => $Value) { if ( $Value != VOID ) { $Result[] = $Value; } } return($Result); } - - /* Return the number of values contained in a given serie */ - function getSerieCount($Serie) - { if (isset($this->Data["Series"][$Serie]["Data"])) { return(sizeof($this->Data["Series"][$Serie]["Data"])); } else { return(0); } } - - /* Remove a serie from the pData object */ - function removeSerie($Series) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie])) { unset($this->Data["Series"][$Serie]); } } - } - - /* Return a value from given serie & index */ - function getValueAt($Serie,$Index=0) - { if (isset($this->Data["Series"][$Serie]["Data"][$Index])) { return($this->Data["Series"][$Serie]["Data"][$Index]); } else { return(NULL); } } - - /* Return the values array */ - function getValues($Serie) - { if (isset($this->Data["Series"][$Serie]["Data"])) { return($this->Data["Series"][$Serie]["Data"]); } else { return(NULL); } } - - /* Reverse the values in the given serie */ - function reverseSerie($Series) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]["Data"])) { $this->Data["Series"][$Serie]["Data"] = array_reverse($this->Data["Series"][$Serie]["Data"]); } } - } - - /* Return the sum of the serie values */ - function getSum($Serie) - { if (isset($this->Data["Series"][$Serie])) { return(array_sum($this->Data["Series"][$Serie]["Data"])); } else { return(NULL); } } - - /* Return the max value of a given serie */ - function getMax($Serie) - { if (isset($this->Data["Series"][$Serie]["Max"])) { return($this->Data["Series"][$Serie]["Max"]); } else { return(NULL); } } - - /* Return the min value of a given serie */ - function getMin($Serie) - { if (isset($this->Data["Series"][$Serie]["Min"])) { return($this->Data["Series"][$Serie]["Min"]); } else { return(NULL); } } - - /* Set the description of a given serie */ - function setSerieShape($Series,$Shape=SERIE_SHAPE_FILLEDCIRCLE) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Shape"] = $Shape; } } - } - - /* Set the description of a given serie */ - function setSerieDescription($Series,$Description="My serie") - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Description"] = $Description; } } - } - - /* Set a serie as "drawable" while calling a rendering function */ - function setSerieDrawable($Series,$Drawable=TRUE) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["isDrawable"] = $Drawable; } } - } - - /* Set the icon associated to a given serie */ - function setSeriePicture($Series,$Picture=NULL) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Picture"] = $Picture; } } - } - - /* Set the name of the X Axis */ - function setXAxisName($Name) - { $this->Data["XAxisName"] = $Name; } - - /* Set the display mode of the X Axis */ - function setXAxisDisplay($Mode,$Format=NULL) - { $this->Data["XAxisDisplay"] = $Mode; $this->Data["XAxisFormat"] = $Format; } - - /* Set the unit that will be displayed on the X axis */ - function setXAxisUnit($Unit) - { $this->Data["XAxisUnit"] = $Unit; } - - /* Set the serie that will be used as abscissa */ - function setAbscissa($Serie) - { if (isset($this->Data["Series"][$Serie])) { $this->Data["Abscissa"] = $Serie; } } - - function setAbsicssaPosition($Position = AXIS_POSITION_BOTTOM) - { $this->Data["AbsicssaPosition"] = $Position; } - - /* Set the name of the abscissa axis */ - function setAbscissaName($Name) - { $this->Data["AbscissaName"] = $Name; } - - /* Create a scatter group specifyin X and Y data series */ - function setScatterSerie($SerieX,$SerieY,$ID=0) - { if (isset($this->Data["Series"][$SerieX]) && isset($this->Data["Series"][$SerieY]) ) { $this->initScatterSerie($ID); $this->Data["ScatterSeries"][$ID]["X"] = $SerieX; $this->Data["ScatterSeries"][$ID]["Y"] = $SerieY; } } - - /* Set the shape of a given sctatter serie */ - function setScatterSerieShape($ID,$Shape=SERIE_SHAPE_FILLEDCIRCLE) - { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Shape"] = $Shape; } } - - /* Set the description of a given scatter serie */ - function setScatterSerieDescription($ID,$Description="My serie") - { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Description"] = $Description; } } - - /* Set the icon associated to a given scatter serie */ - function setScatterSeriePicture($ID,$Picture=NULL) - { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Picture"] = $Picture; } } - - /* Set a scatter serie as "drawable" while calling a rendering function */ - function setScatterSerieDrawable($ID ,$Drawable=TRUE) - { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["isDrawable"] = $Drawable; } } - - /* Define if a scatter serie should be draw with ticks */ - function setScatterSerieTicks($ID,$Width=0) - { if ( isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Ticks"] = $Width; } } - - /* Define if a scatter serie should be draw with a special weight */ - function setScatterSerieWeight($ID,$Weight=0) - { if ( isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Weight"] = $Weight; } } - - /* Associate a color to a scatter serie */ - function setScatterSerieColor($ID,$Format) - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - - if ( isset($this->Data["ScatterSeries"][$ID]) ) - { - $this->Data["ScatterSeries"][$ID]["Color"]["R"] = $R; - $this->Data["ScatterSeries"][$ID]["Color"]["G"] = $G; - $this->Data["ScatterSeries"][$ID]["Color"]["B"] = $B; - $this->Data["ScatterSeries"][$ID]["Color"]["Alpha"] = $Alpha; - } - } - - /* Compute the series limits for an individual and global point of view */ - function limits() - { - $GlobalMin = ABSOLUTE_MAX; - $GlobalMax = ABSOLUTE_MIN; - - foreach($this->Data["Series"] as $Key => $Value) - { - if ( $this->Data["Abscissa"] != $Key && $this->Data["Series"][$Key]["isDrawable"] == TRUE) - { - if ( $GlobalMin > $this->Data["Series"][$Key]["Min"] ) { $GlobalMin = $this->Data["Series"][$Key]["Min"]; } - if ( $GlobalMax < $this->Data["Series"][$Key]["Max"] ) { $GlobalMax = $this->Data["Series"][$Key]["Max"]; } - } - } - $this->Data["Min"] = $GlobalMin; - $this->Data["Max"] = $GlobalMax; - - return(array($GlobalMin,$GlobalMax)); - } - - /* Mark all series as drawable */ - function drawAll() - { foreach($this->Data["Series"] as $Key => $Value) { if ( $this->Data["Abscissa"] != $Key ) { $this->Data["Series"][$Key]["isDrawable"]=TRUE; } } } - - /* Return the average value of the given serie */ - function getSerieAverage($Serie) - { - if ( isset($this->Data["Series"][$Serie]) ) - { - $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); - return(array_sum($SerieData)/sizeof($SerieData)); - } - else - return(NULL); - } - - /* Return the geometric mean of the given serie */ - function getGeometricMean($Serie) - { - if ( isset($this->Data["Series"][$Serie]) ) - { - $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); - $Seriesum = 1; foreach($SerieData as $Key => $Value) { $Seriesum = $Seriesum * $Value; } - return(pow($Seriesum,1/sizeof($SerieData))); - } - else - return(NULL); - } - - /* Return the harmonic mean of the given serie */ - function getHarmonicMean($Serie) - { - if ( isset($this->Data["Series"][$Serie]) ) - { - $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); - $Seriesum = 0; foreach($SerieData as $Key => $Value) { $Seriesum = $Seriesum + 1/$Value; } - return(sizeof($SerieData)/$Seriesum); - } - else - return(NULL); - } - - /* Return the standard deviation of the given serie */ - function getStandardDeviation($Serie) - { - if ( isset($this->Data["Series"][$Serie]) ) - { - $Average = $this->getSerieAverage($Serie); - $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); - - $DeviationSum = 0; - foreach($SerieData as $Key => $Value) - $DeviationSum = $DeviationSum + ($Value-$Average)*($Value-$Average); - - $Deviation = sqrt($DeviationSum/count($SerieData)); - - return($Deviation); - } - else - return(NULL); - } - - /* Return the Coefficient of variation of the given serie */ - function getCoefficientOfVariation($Serie) - { - if ( isset($this->Data["Series"][$Serie]) ) - { - $Average = $this->getSerieAverage($Serie); - $StandardDeviation = $this->getStandardDeviation($Serie); - - if ( $StandardDeviation != 0 ) - return($StandardDeviation/$Average); - else - return(NULL); - } - else - return(NULL); - } - - /* Return the median value of the given serie */ - function getSerieMedian($Serie) - { - if ( isset($this->Data["Series"][$Serie]) ) - { - $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); - sort($SerieData); - $SerieCenter = floor(sizeof($SerieData)/2); - - if ( isset($SerieData[$SerieCenter]) ) - return($SerieData[$SerieCenter]); - else - return(NULL); - } - else - return(NULL); - } - - /* Return the x th percentil of the given serie */ - function getSeriePercentile($Serie="Serie1",$Percentil=95) - { - if (!isset($this->Data["Series"][$Serie]["Data"])) { return(NULL); } - - $Values = count($this->Data["Series"][$Serie]["Data"])-1; - if ( $Values < 0 ) { $Values = 0; } - - $PercentilID = floor(($Values/100)*$Percentil+.5); - $SortedValues = $this->Data["Series"][$Serie]["Data"]; - sort($SortedValues); - - if ( is_numeric($SortedValues[$PercentilID]) ) - return($SortedValues[$PercentilID]); - else - return(NULL); - } - - /* Add random values to a given serie */ - function addRandomValues($SerieName="Serie1",$Options="") - { - $Values = isset($Options["Values"]) ? $Options["Values"] : 20; - $Min = isset($Options["Min"]) ? $Options["Min"] : 0; - $Max = isset($Options["Max"]) ? $Options["Max"] : 100; - $withFloat = isset($Options["withFloat"]) ? $Options["withFloat"] : FALSE; - - for ($i=0;$i<=$Values;$i++) - { - if ( $withFloat ) { $Value = rand($Min*100,$Max*100)/100; } else { $Value = rand($Min,$Max); } - $this->addPoints($Value,$SerieName); - } - } - - /* Test if we have valid data */ - function containsData() - { - if (!isset($this->Data["Series"])) { return(FALSE); } - - $Result = FALSE; - foreach($this->Data["Series"] as $Key => $Value) - { if ( $this->Data["Abscissa"] != $Key && $this->Data["Series"][$Key]["isDrawable"]==TRUE) { $Result=TRUE; } } - return($Result); - } - - /* Set the display mode of an Axis */ - function setAxisDisplay($AxisID,$Mode=AXIS_FORMAT_DEFAULT,$Format=NULL) - { - if ( isset($this->Data["Axis"][$AxisID] ) ) - { - $this->Data["Axis"][$AxisID]["Display"] = $Mode; - if ( $Format != NULL ) { $this->Data["Axis"][$AxisID]["Format"] = $Format; } - } - } - - /* Set the position of an Axis */ - function setAxisPosition($AxisID,$Position=AXIS_POSITION_LEFT) - { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Position"] = $Position; } } - - /* Associate an unit to an axis */ - function setAxisUnit($AxisID,$Unit) - { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Unit"] = $Unit; } } - - /* Associate a name to an axis */ - function setAxisName($AxisID,$Name) - { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Name"] = $Name; } } - - /* Associate a color to an axis */ - function setAxisColor($AxisID,$Format) - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - - if ( isset($this->Data["Axis"][$AxisID] ) ) - { - $this->Data["Axis"][$AxisID]["Color"]["R"] = $R; - $this->Data["Axis"][$AxisID]["Color"]["G"] = $G; - $this->Data["Axis"][$AxisID]["Color"]["B"] = $B; - $this->Data["Axis"][$AxisID]["Color"]["Alpha"] = $Alpha; - } - } - - - /* Design an axis as X or Y member */ - function setAxisXY($AxisID,$Identity=AXIS_Y) - { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Identity"] = $Identity; } } - - /* Associate one data serie with one axis */ - function setSerieOnAxis($Series,$AxisID) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $Serie) - { - $PreviousAxis = $this->Data["Series"][$Serie]["Axis"]; - - /* Create missing axis */ - if ( !isset($this->Data["Axis"][$AxisID] ) ) - { $this->Data["Axis"][$AxisID]["Position"] = AXIS_POSITION_LEFT; $this->Data["Axis"][$AxisID]["Identity"] = AXIS_Y;} - - $this->Data["Series"][$Serie]["Axis"] = $AxisID; - - /* Cleanup unused axis */ - $Found = FALSE; - foreach($this->Data["Series"] as $SerieName => $Values) { if ( $Values["Axis"] == $PreviousAxis ) { $Found = TRUE; } } - if (!$Found) { unset($this->Data["Axis"][$PreviousAxis]); } - } - } - - /* Define if a serie should be draw with ticks */ - function setSerieTicks($Series,$Width=0) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $Serie) { if ( isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Ticks"] = $Width; } } - } - - /* Define if a serie should be draw with a special weight */ - function setSerieWeight($Series,$Weight=0) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $Serie) { if ( isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Weight"] = $Weight; } } - } - - /* Returns the palette of the given serie */ - function getSeriePalette($Serie) - { - if ( !isset($this->Data["Series"][$Serie]) ) { return(NULL); } - - $Result = ""; - $Result["R"] = $this->Data["Series"][$Serie]["Color"]["R"]; - $Result["G"] = $this->Data["Series"][$Serie]["Color"]["G"]; - $Result["B"] = $this->Data["Series"][$Serie]["Color"]["B"]; - $Result["Alpha"] = $this->Data["Series"][$Serie]["Color"]["Alpha"]; - - return($Result); - } - - /* Set the color of one serie */ - function setPalette($Series,$Format=NULL) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - - foreach($Series as $Key => $Serie) - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - - if ( isset($this->Data["Series"][$Serie]) ) - { - $OldR = $this->Data["Series"][$Serie]["Color"]["R"]; $OldG = $this->Data["Series"][$Serie]["Color"]["G"]; $OldB = $this->Data["Series"][$Serie]["Color"]["B"]; - $this->Data["Series"][$Serie]["Color"]["R"] = $R; - $this->Data["Series"][$Serie]["Color"]["G"] = $G; - $this->Data["Series"][$Serie]["Color"]["B"] = $B; - $this->Data["Series"][$Serie]["Color"]["Alpha"] = $Alpha; - - /* Do reverse processing on the internal palette array */ - foreach ($this->Palette as $Key => $Value) - { if ($Value["R"] == $OldR && $Value["G"] == $OldG && $Value["B"] == $OldB) { $this->Palette[$Key]["R"] = $R; $this->Palette[$Key]["G"] = $G; $this->Palette[$Key]["B"] = $B; $this->Palette[$Key]["Alpha"] = $Alpha;} } - } - } - } - - /* Load a palette file */ - function loadPalette($FileName,$Overwrite=FALSE) - { - if ( !file_exists($FileName) ) { return(-1); } - if ( $Overwrite ) { $this->Palette = ""; } - - $fileHandle = @fopen($FileName, "r"); - if (!$fileHandle) { return(-1); } - while (!feof($fileHandle)) - { - $buffer = fgets($fileHandle, 4096); - if ( preg_match("/,/",$buffer) ) - { - list($R,$G,$B,$Alpha) = preg_split("/,/",$buffer); - if ( $this->Palette == "" ) { $ID = 0; } else { $ID = count($this->Palette); } - $this->Palette[$ID] = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); - } - } - fclose($fileHandle); - - /* Apply changes to current series */ - $ID = 0; - if ( isset($this->Data["Series"])) - { - foreach($this->Data["Series"] as $Key => $Value) - { - if ( !isset($this->Palette[$ID]) ) - $this->Data["Series"][$Key]["Color"] = array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>0); - else - $this->Data["Series"][$Key]["Color"] = $this->Palette[$ID]; - $ID++; - } - } - } - - /* Initialise a given scatter serie */ - function initScatterSerie($ID) - { - if ( isset($this->Data["ScatterSeries"][$ID]) ) { return(0); } - - $this->Data["ScatterSeries"][$ID]["Description"] = "Scatter ".$ID; - $this->Data["ScatterSeries"][$ID]["isDrawable"] = TRUE; - $this->Data["ScatterSeries"][$ID]["Picture"] = NULL; - $this->Data["ScatterSeries"][$ID]["Ticks"] = 0; - $this->Data["ScatterSeries"][$ID]["Weight"] = 0; - - if ( isset($this->Palette[$ID]) ) - $this->Data["ScatterSeries"][$ID]["Color"] = $this->Palette[$ID]; - else - { - $this->Data["ScatterSeries"][$ID]["Color"]["R"] = rand(0,255); - $this->Data["ScatterSeries"][$ID]["Color"]["G"] = rand(0,255); - $this->Data["ScatterSeries"][$ID]["Color"]["B"] = rand(0,255); - $this->Data["ScatterSeries"][$ID]["Color"]["Alpha"] = 100; - } - } - - /* Initialise a given serie */ - function initialise($Serie) - { - if ( isset($this->Data["Series"]) ) { $ID = count($this->Data["Series"]); } else { $ID = 0; } - - $this->Data["Series"][$Serie]["Description"] = $Serie; - $this->Data["Series"][$Serie]["isDrawable"] = TRUE; - $this->Data["Series"][$Serie]["Picture"] = NULL; - $this->Data["Series"][$Serie]["Max"] = NULL; - $this->Data["Series"][$Serie]["Min"] = NULL; - $this->Data["Series"][$Serie]["Axis"] = 0; - $this->Data["Series"][$Serie]["Ticks"] = 0; - $this->Data["Series"][$Serie]["Weight"] = 0; - $this->Data["Series"][$Serie]["Shape"] = SERIE_SHAPE_FILLEDCIRCLE; - - if ( isset($this->Palette[$ID]) ) - $this->Data["Series"][$Serie]["Color"] = $this->Palette[$ID]; - else - { - $this->Data["Series"][$Serie]["Color"]["R"] = rand(0,255); - $this->Data["Series"][$Serie]["Color"]["G"] = rand(0,255); - $this->Data["Series"][$Serie]["Color"]["B"] = rand(0,255); - $this->Data["Series"][$Serie]["Color"]["Alpha"] = 100; - } - } - - function normalize($NormalizationFactor=100,$UnitChange=NULL,$Round=1) - { - $Abscissa = $this->Data["Abscissa"]; - - $SelectedSeries = ""; - $MaxVal = 0; - foreach($this->Data["Axis"] as $AxisID => $Axis) - { - if ( $UnitChange != NULL ) { $this->Data["Axis"][$AxisID]["Unit"] = $UnitChange; } - - foreach($this->Data["Series"] as $SerieName => $Serie) - { - if ($Serie["Axis"] == $AxisID && $Serie["isDrawable"] == TRUE && $SerieName != $Abscissa) - { - $SelectedSeries[$SerieName] = $SerieName; - - if ( count($Serie["Data"] ) > $MaxVal ) { $MaxVal = count($Serie["Data"]); } - } - } - } - - for($i=0;$i<=$MaxVal-1;$i++) - { - $Factor = 0; - foreach ($SelectedSeries as $Key => $SerieName ) - { - $Value = $this->Data["Series"][$SerieName]["Data"][$i]; - if ( $Value != VOID ) - $Factor = $Factor + abs($Value); - } - - if ( $Factor != 0 ) - { - $Factor = $NormalizationFactor / $Factor; - - foreach ($SelectedSeries as $Key => $SerieName ) - { - $Value = $this->Data["Series"][$SerieName]["Data"][$i]; - - if ( $Value != VOID && $Factor != $NormalizationFactor ) - $this->Data["Series"][$SerieName]["Data"][$i] = round(abs($Value)*$Factor,$Round); - elseif ( $Value == VOID || $Value == 0 ) - $this->Data["Series"][$SerieName]["Data"][$i] = VOID; - elseif ( $Factor == $NormalizationFactor ) - $this->Data["Series"][$SerieName]["Data"][$i] = $NormalizationFactor; - } - } - } - - foreach ($SelectedSeries as $Key => $SerieName ) - { - $this->Data["Series"][$SerieName]["Max"] = max($this->stripVOID($this->Data["Series"][$SerieName]["Data"])); - $this->Data["Series"][$SerieName]["Min"] = min($this->stripVOID($this->Data["Series"][$SerieName]["Data"])); - } - } - - /* Load data from a CSV (or similar) data source */ - function importFromCSV($FileName,$Options="") - { - $Delimiter = isset($Options["Delimiter"]) ? $Options["Delimiter"] : ","; - $GotHeader = isset($Options["GotHeader"]) ? $Options["GotHeader"] : FALSE; - $SkipColumns = isset($Options["SkipColumns"]) ? $Options["SkipColumns"] : array(-1); - $DefaultSerieName = isset($Options["DefaultSerieName"]) ? $Options["DefaultSerieName"] : "Serie"; - - $Handle = @fopen($FileName,"r"); - if ($Handle) - { - $HeaderParsed = FALSE; $SerieNames = ""; - while (!feof($Handle)) - { - $Buffer = fgets($Handle, 4096); - $Buffer = str_replace(chr(10),"",$Buffer); - $Buffer = str_replace(chr(13),"",$Buffer); - $Values = preg_split("/".$Delimiter."/",$Buffer); - - if ( $Buffer != "" ) - { - if ( $GotHeader && !$HeaderParsed ) - { - foreach($Values as $Key => $Name) { if ( !in_array($Key,$SkipColumns) ) { $SerieNames[$Key] = $Name; } } - $HeaderParsed = TRUE; - } - else - { - if ($SerieNames == "" ) { foreach($Values as $Key => $Name) { if ( !in_array($Key,$SkipColumns) ) { $SerieNames[$Key] = $DefaultSerieName.$Key; } } } - foreach($Values as $Key => $Value) { if ( !in_array($Key,$SkipColumns) ) { $this->addPoints($Value,$SerieNames[$Key]); } } - } - } - } - fclose($Handle); - } - } - - /* Create a dataset based on a formula */ - function createFunctionSerie($SerieName,$Formula="",$Options="") - { - $MinX = isset($Options["MinX"]) ? $Options["MinX"] : -10; - $MaxX = isset($Options["MaxX"]) ? $Options["MaxX"] : 10; - $XStep = isset($Options["XStep"]) ? $Options["XStep"] : 1; - $AutoDescription = isset($Options["AutoDescription"]) ? $Options["AutoDescription"] : FALSE; - $RecordAbscissa = isset($Options["RecordAbscissa"]) ? $Options["RecordAbscissa"] : FALSE; - $AbscissaSerie = isset($Options["AbscissaSerie"]) ? $Options["AbscissaSerie"] : "Abscissa"; - - if ( $Formula == "" ) { return(0); } - - $Result = ""; $Abscissa = ""; - for($i=$MinX; $i<=$MaxX; $i=$i+$XStep) - { - $Expression = "\$return = '!'.(".str_replace("z",$i,$Formula).");"; - if ( @eval($Expression) === FALSE ) { $return = VOID; } - if ( $return == "!" ) { $return = VOID; } else { $return = $this->right($return,strlen($return)-1); } - if ( $return == "NAN" ) { $return = VOID; } - if ( $return == "INF" ) { $return = VOID; } - if ( $return == "-INF" ) { $return = VOID; } - - $Abscissa[] = $i; - $Result[] = $return; - } - - $this->addPoints($Result,$SerieName); - if ( $AutoDescription ) { $this->setSerieDescription($SerieName,$Formula); } - if ( $RecordAbscissa ) { $this->addPoints($Abscissa,$AbscissaSerie); } - } - - function negateValues($Series) - { - if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } - foreach($Series as $Key => $SerieName) - { - if (isset($this->Data["Series"][$SerieName])) - { - $Data = ""; - foreach($this->Data["Series"][$SerieName]["Data"] as $Key => $Value) - { if ( $Value == VOID ) { $Data[] = VOID; } else { $Data[] = -$Value; } } - $this->Data["Series"][$SerieName]["Data"] = $Data; - - $this->Data["Series"][$SerieName]["Max"] = max($this->stripVOID($this->Data["Series"][$SerieName]["Data"])); - $this->Data["Series"][$SerieName]["Min"] = min($this->stripVOID($this->Data["Series"][$SerieName]["Data"])); - } - } - } - - /* Return the data & configuration of the series */ - function getData() - { return($this->Data); } - - /* Save a palette element */ - function savePalette($ID,$Color) - { $this->Palette[$ID] = $Color; } - - /* Return the palette of the series */ - function getPalette() - { return($this->Palette); } - - /* Called by the scaling algorithm to save the config */ - function saveAxisConfig($Axis) { $this->Data["Axis"]=$Axis; } - - /* Save the Y Margin if set */ - function saveYMargin($Value) { $this->Data["YMargin"]=$Value; } - - /* Save extended configuration to the pData object */ - function saveExtendedData($Tag,$Values) { $this->Data["Extended"][$Tag]=$Values; } - - /* Called by the scaling algorithm to save the orientation of the scale */ - function saveOrientation($Orientation) { $this->Data["Orientation"]=$Orientation; } - - /* Convert a string to a single elements array */ - function convertToArray($Value) - { $Values = ""; $Values[] = $Value; return($Values); } - - /* Class string wrapper */ - function __toString() - { return("pData object."); } - - function left($value,$NbChar) { return substr($value,0,$NbChar); } - function right($value,$NbChar) { return substr($value,strlen($value)-$NbChar,$NbChar); } - function mid($value,$Depart,$NbChar) { return substr($value,$Depart-1,$NbChar); } - } +array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100), + "1"=>array("R"=>224,"G"=>100,"B"=>46,"Alpha"=>100), + "2"=>array("R"=>224,"G"=>214,"B"=>46,"Alpha"=>100), + "3"=>array("R"=>46,"G"=>151,"B"=>224,"Alpha"=>100), + "4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100), + "5"=>array("R"=>224,"G"=>46,"B"=>117,"Alpha"=>100), + "6"=>array("R"=>92,"G"=>224,"B"=>46,"Alpha"=>100), + "7"=>array("R"=>224,"G"=>176,"B"=>46,"Alpha"=>100)); + + /* Class creator */ + function pData() + { + $this->Data = ""; + $this->Data["XAxisDisplay"] = AXIS_FORMAT_DEFAULT; + $this->Data["XAxisFormat"] = NULL; + $this->Data["XAxisName"] = NULL; + $this->Data["XAxisUnit"] = NULL; + $this->Data["Abscissa"] = NULL; + $this->Data["AbsicssaPosition"] = AXIS_POSITION_BOTTOM; + + $this->Data["Axis"][0]["Display"] = AXIS_FORMAT_DEFAULT; + $this->Data["Axis"][0]["Position"] = AXIS_POSITION_LEFT; + $this->Data["Axis"][0]["Identity"] = AXIS_Y; + } + + /* Add a single point or an array to the given serie */ + function addPoints($Values,$SerieName="Serie1") + { + if (!isset($this->Data["Series"][$SerieName])) + $this->initialise($SerieName); + + if ( is_array($Values) ) + { + foreach($Values as $Key => $Value) + { $this->Data["Series"][$SerieName]["Data"][] = $Value; } + } + else + $this->Data["Series"][$SerieName]["Data"][] = $Values; + + if ( $Values != VOID ) + { + $StrippedData = $this->stripVOID($this->Data["Series"][$SerieName]["Data"]); + if ( empty($StrippedData) ) { $this->Data["Series"][$SerieName]["Max"] = 0; $this->Data["Series"][$SerieName]["Min"] =0; return(0); } + $this->Data["Series"][$SerieName]["Max"] = max($StrippedData); + $this->Data["Series"][$SerieName]["Min"] = min($StrippedData); + } + } + + /* Strip VOID values */ + function stripVOID($Values) + { if (!is_array($Values)) { return(array()); } $Result = array(); foreach($Values as $Key => $Value) { if ( $Value != VOID ) { $Result[] = $Value; } } return($Result); } + + /* Return the number of values contained in a given serie */ + function getSerieCount($Serie) + { if (isset($this->Data["Series"][$Serie]["Data"])) { return(sizeof($this->Data["Series"][$Serie]["Data"])); } else { return(0); } } + + /* Remove a serie from the pData object */ + function removeSerie($Series) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie])) { unset($this->Data["Series"][$Serie]); } } + } + + /* Return a value from given serie & index */ + function getValueAt($Serie,$Index=0) + { if (isset($this->Data["Series"][$Serie]["Data"][$Index])) { return($this->Data["Series"][$Serie]["Data"][$Index]); } else { return(NULL); } } + + /* Return the values array */ + function getValues($Serie) + { if (isset($this->Data["Series"][$Serie]["Data"])) { return($this->Data["Series"][$Serie]["Data"]); } else { return(NULL); } } + + /* Reverse the values in the given serie */ + function reverseSerie($Series) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]["Data"])) { $this->Data["Series"][$Serie]["Data"] = array_reverse($this->Data["Series"][$Serie]["Data"]); } } + } + + /* Return the sum of the serie values */ + function getSum($Serie) + { if (isset($this->Data["Series"][$Serie])) { return(array_sum($this->Data["Series"][$Serie]["Data"])); } else { return(NULL); } } + + /* Return the max value of a given serie */ + function getMax($Serie) + { if (isset($this->Data["Series"][$Serie]["Max"])) { return($this->Data["Series"][$Serie]["Max"]); } else { return(NULL); } } + + /* Return the min value of a given serie */ + function getMin($Serie) + { if (isset($this->Data["Series"][$Serie]["Min"])) { return($this->Data["Series"][$Serie]["Min"]); } else { return(NULL); } } + + /* Set the description of a given serie */ + function setSerieShape($Series,$Shape=SERIE_SHAPE_FILLEDCIRCLE) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Shape"] = $Shape; } } + } + + /* Set the description of a given serie */ + function setSerieDescription($Series,$Description="My serie") + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Description"] = $Description; } } + } + + /* Set a serie as "drawable" while calling a rendering function */ + function setSerieDrawable($Series,$Drawable=TRUE) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["isDrawable"] = $Drawable; } } + } + + /* Set the icon associated to a given serie */ + function setSeriePicture($Series,$Picture=NULL) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Picture"] = $Picture; } } + } + + /* Set the name of the X Axis */ + function setXAxisName($Name) + { $this->Data["XAxisName"] = $Name; } + + /* Set the display mode of the X Axis */ + function setXAxisDisplay($Mode,$Format=NULL) + { $this->Data["XAxisDisplay"] = $Mode; $this->Data["XAxisFormat"] = $Format; } + + /* Set the unit that will be displayed on the X axis */ + function setXAxisUnit($Unit) + { $this->Data["XAxisUnit"] = $Unit; } + + /* Set the serie that will be used as abscissa */ + function setAbscissa($Serie) + { if (isset($this->Data["Series"][$Serie])) { $this->Data["Abscissa"] = $Serie; } } + + function setAbsicssaPosition($Position = AXIS_POSITION_BOTTOM) + { $this->Data["AbsicssaPosition"] = $Position; } + + /* Set the name of the abscissa axis */ + function setAbscissaName($Name) + { $this->Data["AbscissaName"] = $Name; } + + /* Create a scatter group specifyin X and Y data series */ + function setScatterSerie($SerieX,$SerieY,$ID=0) + { if (isset($this->Data["Series"][$SerieX]) && isset($this->Data["Series"][$SerieY]) ) { $this->initScatterSerie($ID); $this->Data["ScatterSeries"][$ID]["X"] = $SerieX; $this->Data["ScatterSeries"][$ID]["Y"] = $SerieY; } } + + /* Set the shape of a given sctatter serie */ + function setScatterSerieShape($ID,$Shape=SERIE_SHAPE_FILLEDCIRCLE) + { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Shape"] = $Shape; } } + + /* Set the description of a given scatter serie */ + function setScatterSerieDescription($ID,$Description="My serie") + { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Description"] = $Description; } } + + /* Set the icon associated to a given scatter serie */ + function setScatterSeriePicture($ID,$Picture=NULL) + { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Picture"] = $Picture; } } + + /* Set a scatter serie as "drawable" while calling a rendering function */ + function setScatterSerieDrawable($ID ,$Drawable=TRUE) + { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["isDrawable"] = $Drawable; } } + + /* Define if a scatter serie should be draw with ticks */ + function setScatterSerieTicks($ID,$Width=0) + { if ( isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Ticks"] = $Width; } } + + /* Define if a scatter serie should be draw with a special weight */ + function setScatterSerieWeight($ID,$Weight=0) + { if ( isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Weight"] = $Weight; } } + + /* Associate a color to a scatter serie */ + function setScatterSerieColor($ID,$Format) + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + + if ( isset($this->Data["ScatterSeries"][$ID]) ) + { + $this->Data["ScatterSeries"][$ID]["Color"]["R"] = $R; + $this->Data["ScatterSeries"][$ID]["Color"]["G"] = $G; + $this->Data["ScatterSeries"][$ID]["Color"]["B"] = $B; + $this->Data["ScatterSeries"][$ID]["Color"]["Alpha"] = $Alpha; + } + } + + /* Compute the series limits for an individual and global point of view */ + function limits() + { + $GlobalMin = ABSOLUTE_MAX; + $GlobalMax = ABSOLUTE_MIN; + + foreach($this->Data["Series"] as $Key => $Value) + { + if ( $this->Data["Abscissa"] != $Key && $this->Data["Series"][$Key]["isDrawable"] == TRUE) + { + if ( $GlobalMin > $this->Data["Series"][$Key]["Min"] ) { $GlobalMin = $this->Data["Series"][$Key]["Min"]; } + if ( $GlobalMax < $this->Data["Series"][$Key]["Max"] ) { $GlobalMax = $this->Data["Series"][$Key]["Max"]; } + } + } + $this->Data["Min"] = $GlobalMin; + $this->Data["Max"] = $GlobalMax; + + return(array($GlobalMin,$GlobalMax)); + } + + /* Mark all series as drawable */ + function drawAll() + { foreach($this->Data["Series"] as $Key => $Value) { if ( $this->Data["Abscissa"] != $Key ) { $this->Data["Series"][$Key]["isDrawable"]=TRUE; } } } + + /* Return the average value of the given serie */ + function getSerieAverage($Serie) + { + if ( isset($this->Data["Series"][$Serie]) ) + { + $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); + return(array_sum($SerieData)/sizeof($SerieData)); + } + else + return(NULL); + } + + /* Return the geometric mean of the given serie */ + function getGeometricMean($Serie) + { + if ( isset($this->Data["Series"][$Serie]) ) + { + $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); + $Seriesum = 1; foreach($SerieData as $Key => $Value) { $Seriesum = $Seriesum * $Value; } + return(pow($Seriesum,1/sizeof($SerieData))); + } + else + return(NULL); + } + + /* Return the harmonic mean of the given serie */ + function getHarmonicMean($Serie) + { + if ( isset($this->Data["Series"][$Serie]) ) + { + $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); + $Seriesum = 0; foreach($SerieData as $Key => $Value) { $Seriesum = $Seriesum + 1/$Value; } + return(sizeof($SerieData)/$Seriesum); + } + else + return(NULL); + } + + /* Return the standard deviation of the given serie */ + function getStandardDeviation($Serie) + { + if ( isset($this->Data["Series"][$Serie]) ) + { + $Average = $this->getSerieAverage($Serie); + $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); + + $DeviationSum = 0; + foreach($SerieData as $Key => $Value) + $DeviationSum = $DeviationSum + ($Value-$Average)*($Value-$Average); + + $Deviation = sqrt($DeviationSum/count($SerieData)); + + return($Deviation); + } + else + return(NULL); + } + + /* Return the Coefficient of variation of the given serie */ + function getCoefficientOfVariation($Serie) + { + if ( isset($this->Data["Series"][$Serie]) ) + { + $Average = $this->getSerieAverage($Serie); + $StandardDeviation = $this->getStandardDeviation($Serie); + + if ( $StandardDeviation != 0 ) + return($StandardDeviation/$Average); + else + return(NULL); + } + else + return(NULL); + } + + /* Return the median value of the given serie */ + function getSerieMedian($Serie) + { + if ( isset($this->Data["Series"][$Serie]) ) + { + $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]); + sort($SerieData); + $SerieCenter = floor(sizeof($SerieData)/2); + + if ( isset($SerieData[$SerieCenter]) ) + return($SerieData[$SerieCenter]); + else + return(NULL); + } + else + return(NULL); + } + + /* Return the x th percentil of the given serie */ + function getSeriePercentile($Serie="Serie1",$Percentil=95) + { + if (!isset($this->Data["Series"][$Serie]["Data"])) { return(NULL); } + + $Values = count($this->Data["Series"][$Serie]["Data"])-1; + if ( $Values < 0 ) { $Values = 0; } + + $PercentilID = floor(($Values/100)*$Percentil+.5); + $SortedValues = $this->Data["Series"][$Serie]["Data"]; + sort($SortedValues); + + if ( is_numeric($SortedValues[$PercentilID]) ) + return($SortedValues[$PercentilID]); + else + return(NULL); + } + + /* Add random values to a given serie */ + function addRandomValues($SerieName="Serie1",$Options="") + { + $Values = isset($Options["Values"]) ? $Options["Values"] : 20; + $Min = isset($Options["Min"]) ? $Options["Min"] : 0; + $Max = isset($Options["Max"]) ? $Options["Max"] : 100; + $withFloat = isset($Options["withFloat"]) ? $Options["withFloat"] : FALSE; + + for ($i=0;$i<=$Values;$i++) + { + if ( $withFloat ) { $Value = rand($Min*100,$Max*100)/100; } else { $Value = rand($Min,$Max); } + $this->addPoints($Value,$SerieName); + } + } + + /* Test if we have valid data */ + function containsData() + { + if (!isset($this->Data["Series"])) { return(FALSE); } + + $Result = FALSE; + foreach($this->Data["Series"] as $Key => $Value) + { if ( $this->Data["Abscissa"] != $Key && $this->Data["Series"][$Key]["isDrawable"]==TRUE) { $Result=TRUE; } } + return($Result); + } + + /* Set the display mode of an Axis */ + function setAxisDisplay($AxisID,$Mode=AXIS_FORMAT_DEFAULT,$Format=NULL) + { + if ( isset($this->Data["Axis"][$AxisID] ) ) + { + $this->Data["Axis"][$AxisID]["Display"] = $Mode; + if ( $Format != NULL ) { $this->Data["Axis"][$AxisID]["Format"] = $Format; } + } + } + + /* Set the position of an Axis */ + function setAxisPosition($AxisID,$Position=AXIS_POSITION_LEFT) + { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Position"] = $Position; } } + + /* Associate an unit to an axis */ + function setAxisUnit($AxisID,$Unit) + { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Unit"] = $Unit; } } + + /* Associate a name to an axis */ + function setAxisName($AxisID,$Name) + { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Name"] = $Name; } } + + /* Associate a color to an axis */ + function setAxisColor($AxisID,$Format) + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + + if ( isset($this->Data["Axis"][$AxisID] ) ) + { + $this->Data["Axis"][$AxisID]["Color"]["R"] = $R; + $this->Data["Axis"][$AxisID]["Color"]["G"] = $G; + $this->Data["Axis"][$AxisID]["Color"]["B"] = $B; + $this->Data["Axis"][$AxisID]["Color"]["Alpha"] = $Alpha; + } + } + + + /* Design an axis as X or Y member */ + function setAxisXY($AxisID,$Identity=AXIS_Y) + { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Identity"] = $Identity; } } + + /* Associate one data serie with one axis */ + function setSerieOnAxis($Series,$AxisID) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $Serie) + { + $PreviousAxis = $this->Data["Series"][$Serie]["Axis"]; + + /* Create missing axis */ + if ( !isset($this->Data["Axis"][$AxisID] ) ) + { $this->Data["Axis"][$AxisID]["Position"] = AXIS_POSITION_LEFT; $this->Data["Axis"][$AxisID]["Identity"] = AXIS_Y;} + + $this->Data["Series"][$Serie]["Axis"] = $AxisID; + + /* Cleanup unused axis */ + $Found = FALSE; + foreach($this->Data["Series"] as $SerieName => $Values) { if ( $Values["Axis"] == $PreviousAxis ) { $Found = TRUE; } } + if (!$Found) { unset($this->Data["Axis"][$PreviousAxis]); } + } + } + + /* Define if a serie should be draw with ticks */ + function setSerieTicks($Series,$Width=0) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $Serie) { if ( isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Ticks"] = $Width; } } + } + + /* Define if a serie should be draw with a special weight */ + function setSerieWeight($Series,$Weight=0) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $Serie) { if ( isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Weight"] = $Weight; } } + } + + /* Returns the palette of the given serie */ + function getSeriePalette($Serie) + { + if ( !isset($this->Data["Series"][$Serie]) ) { return(NULL); } + + $Result = ""; + $Result["R"] = $this->Data["Series"][$Serie]["Color"]["R"]; + $Result["G"] = $this->Data["Series"][$Serie]["Color"]["G"]; + $Result["B"] = $this->Data["Series"][$Serie]["Color"]["B"]; + $Result["Alpha"] = $this->Data["Series"][$Serie]["Color"]["Alpha"]; + + return($Result); + } + + /* Set the color of one serie */ + function setPalette($Series,$Format=NULL) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + + foreach($Series as $Key => $Serie) + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + + if ( isset($this->Data["Series"][$Serie]) ) + { + $OldR = $this->Data["Series"][$Serie]["Color"]["R"]; $OldG = $this->Data["Series"][$Serie]["Color"]["G"]; $OldB = $this->Data["Series"][$Serie]["Color"]["B"]; + $this->Data["Series"][$Serie]["Color"]["R"] = $R; + $this->Data["Series"][$Serie]["Color"]["G"] = $G; + $this->Data["Series"][$Serie]["Color"]["B"] = $B; + $this->Data["Series"][$Serie]["Color"]["Alpha"] = $Alpha; + + /* Do reverse processing on the internal palette array */ + foreach ($this->Palette as $Key => $Value) + { if ($Value["R"] == $OldR && $Value["G"] == $OldG && $Value["B"] == $OldB) { $this->Palette[$Key]["R"] = $R; $this->Palette[$Key]["G"] = $G; $this->Palette[$Key]["B"] = $B; $this->Palette[$Key]["Alpha"] = $Alpha;} } + } + } + } + + /* Load a palette file */ + function loadPalette($FileName,$Overwrite=FALSE) + { + if ( !file_exists($FileName) ) { return(-1); } + if ( $Overwrite ) { $this->Palette = ""; } + + $fileHandle = @fopen($FileName, "r"); + if (!$fileHandle) { return(-1); } + while (!feof($fileHandle)) + { + $buffer = fgets($fileHandle, 4096); + if ( preg_match("/,/",$buffer) ) + { + list($R,$G,$B,$Alpha) = preg_split("/,/",$buffer); + if ( $this->Palette == "" ) { $ID = 0; } else { $ID = count($this->Palette); } + $this->Palette[$ID] = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); + } + } + fclose($fileHandle); + + /* Apply changes to current series */ + $ID = 0; + if ( isset($this->Data["Series"])) + { + foreach($this->Data["Series"] as $Key => $Value) + { + if ( !isset($this->Palette[$ID]) ) + $this->Data["Series"][$Key]["Color"] = array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>0); + else + $this->Data["Series"][$Key]["Color"] = $this->Palette[$ID]; + $ID++; + } + } + } + + /* Initialise a given scatter serie */ + function initScatterSerie($ID) + { + if ( isset($this->Data["ScatterSeries"][$ID]) ) { return(0); } + + $this->Data["ScatterSeries"][$ID]["Description"] = "Scatter ".$ID; + $this->Data["ScatterSeries"][$ID]["isDrawable"] = TRUE; + $this->Data["ScatterSeries"][$ID]["Picture"] = NULL; + $this->Data["ScatterSeries"][$ID]["Ticks"] = 0; + $this->Data["ScatterSeries"][$ID]["Weight"] = 0; + + if ( isset($this->Palette[$ID]) ) + $this->Data["ScatterSeries"][$ID]["Color"] = $this->Palette[$ID]; + else + { + $this->Data["ScatterSeries"][$ID]["Color"]["R"] = rand(0,255); + $this->Data["ScatterSeries"][$ID]["Color"]["G"] = rand(0,255); + $this->Data["ScatterSeries"][$ID]["Color"]["B"] = rand(0,255); + $this->Data["ScatterSeries"][$ID]["Color"]["Alpha"] = 100; + } + } + + /* Initialise a given serie */ + function initialise($Serie) + { + if ( isset($this->Data["Series"]) ) { $ID = count($this->Data["Series"]); } else { $ID = 0; } + + $this->Data["Series"][$Serie]["Description"] = $Serie; + $this->Data["Series"][$Serie]["isDrawable"] = TRUE; + $this->Data["Series"][$Serie]["Picture"] = NULL; + $this->Data["Series"][$Serie]["Max"] = NULL; + $this->Data["Series"][$Serie]["Min"] = NULL; + $this->Data["Series"][$Serie]["Axis"] = 0; + $this->Data["Series"][$Serie]["Ticks"] = 0; + $this->Data["Series"][$Serie]["Weight"] = 0; + $this->Data["Series"][$Serie]["Shape"] = SERIE_SHAPE_FILLEDCIRCLE; + + if ( isset($this->Palette[$ID]) ) + $this->Data["Series"][$Serie]["Color"] = $this->Palette[$ID]; + else + { + $this->Data["Series"][$Serie]["Color"]["R"] = rand(0,255); + $this->Data["Series"][$Serie]["Color"]["G"] = rand(0,255); + $this->Data["Series"][$Serie]["Color"]["B"] = rand(0,255); + $this->Data["Series"][$Serie]["Color"]["Alpha"] = 100; + } + } + + function normalize($NormalizationFactor=100,$UnitChange=NULL,$Round=1) + { + $Abscissa = $this->Data["Abscissa"]; + + $SelectedSeries = ""; + $MaxVal = 0; + foreach($this->Data["Axis"] as $AxisID => $Axis) + { + if ( $UnitChange != NULL ) { $this->Data["Axis"][$AxisID]["Unit"] = $UnitChange; } + + foreach($this->Data["Series"] as $SerieName => $Serie) + { + if ($Serie["Axis"] == $AxisID && $Serie["isDrawable"] == TRUE && $SerieName != $Abscissa) + { + $SelectedSeries[$SerieName] = $SerieName; + + if ( count($Serie["Data"] ) > $MaxVal ) { $MaxVal = count($Serie["Data"]); } + } + } + } + + for($i=0;$i<=$MaxVal-1;$i++) + { + $Factor = 0; + foreach ($SelectedSeries as $Key => $SerieName ) + { + $Value = $this->Data["Series"][$SerieName]["Data"][$i]; + if ( $Value != VOID ) + $Factor = $Factor + abs($Value); + } + + if ( $Factor != 0 ) + { + $Factor = $NormalizationFactor / $Factor; + + foreach ($SelectedSeries as $Key => $SerieName ) + { + $Value = $this->Data["Series"][$SerieName]["Data"][$i]; + + if ( $Value != VOID && $Factor != $NormalizationFactor ) + $this->Data["Series"][$SerieName]["Data"][$i] = round(abs($Value)*$Factor,$Round); + elseif ( $Value == VOID || $Value == 0 ) + $this->Data["Series"][$SerieName]["Data"][$i] = VOID; + elseif ( $Factor == $NormalizationFactor ) + $this->Data["Series"][$SerieName]["Data"][$i] = $NormalizationFactor; + } + } + } + + foreach ($SelectedSeries as $Key => $SerieName ) + { + $this->Data["Series"][$SerieName]["Max"] = max($this->stripVOID($this->Data["Series"][$SerieName]["Data"])); + $this->Data["Series"][$SerieName]["Min"] = min($this->stripVOID($this->Data["Series"][$SerieName]["Data"])); + } + } + + /* Load data from a CSV (or similar) data source */ + function importFromCSV($FileName,$Options="") + { + $Delimiter = isset($Options["Delimiter"]) ? $Options["Delimiter"] : ","; + $GotHeader = isset($Options["GotHeader"]) ? $Options["GotHeader"] : FALSE; + $SkipColumns = isset($Options["SkipColumns"]) ? $Options["SkipColumns"] : array(-1); + $DefaultSerieName = isset($Options["DefaultSerieName"]) ? $Options["DefaultSerieName"] : "Serie"; + + $Handle = @fopen($FileName,"r"); + if ($Handle) + { + $HeaderParsed = FALSE; $SerieNames = ""; + while (!feof($Handle)) + { + $Buffer = fgets($Handle, 4096); + $Buffer = str_replace(chr(10),"",$Buffer); + $Buffer = str_replace(chr(13),"",$Buffer); + $Values = preg_split("/".$Delimiter."/",$Buffer); + + if ( $Buffer != "" ) + { + if ( $GotHeader && !$HeaderParsed ) + { + foreach($Values as $Key => $Name) { if ( !in_array($Key,$SkipColumns) ) { $SerieNames[$Key] = $Name; } } + $HeaderParsed = TRUE; + } + else + { + if ($SerieNames == "" ) { foreach($Values as $Key => $Name) { if ( !in_array($Key,$SkipColumns) ) { $SerieNames[$Key] = $DefaultSerieName.$Key; } } } + foreach($Values as $Key => $Value) { if ( !in_array($Key,$SkipColumns) ) { $this->addPoints($Value,$SerieNames[$Key]); } } + } + } + } + fclose($Handle); + } + } + + /* Create a dataset based on a formula */ + function createFunctionSerie($SerieName,$Formula="",$Options="") + { + $MinX = isset($Options["MinX"]) ? $Options["MinX"] : -10; + $MaxX = isset($Options["MaxX"]) ? $Options["MaxX"] : 10; + $XStep = isset($Options["XStep"]) ? $Options["XStep"] : 1; + $AutoDescription = isset($Options["AutoDescription"]) ? $Options["AutoDescription"] : FALSE; + $RecordAbscissa = isset($Options["RecordAbscissa"]) ? $Options["RecordAbscissa"] : FALSE; + $AbscissaSerie = isset($Options["AbscissaSerie"]) ? $Options["AbscissaSerie"] : "Abscissa"; + + if ( $Formula == "" ) { return(0); } + + $Result = ""; $Abscissa = ""; + for($i=$MinX; $i<=$MaxX; $i=$i+$XStep) + { + $Expression = "\$return = '!'.(".str_replace("z",$i,$Formula).");"; + if ( @eval($Expression) === FALSE ) { $return = VOID; } + if ( $return == "!" ) { $return = VOID; } else { $return = $this->right($return,strlen($return)-1); } + if ( $return == "NAN" ) { $return = VOID; } + if ( $return == "INF" ) { $return = VOID; } + if ( $return == "-INF" ) { $return = VOID; } + + $Abscissa[] = $i; + $Result[] = $return; + } + + $this->addPoints($Result,$SerieName); + if ( $AutoDescription ) { $this->setSerieDescription($SerieName,$Formula); } + if ( $RecordAbscissa ) { $this->addPoints($Abscissa,$AbscissaSerie); } + } + + function negateValues($Series) + { + if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); } + foreach($Series as $Key => $SerieName) + { + if (isset($this->Data["Series"][$SerieName])) + { + $Data = ""; + foreach($this->Data["Series"][$SerieName]["Data"] as $Key => $Value) + { if ( $Value == VOID ) { $Data[] = VOID; } else { $Data[] = -$Value; } } + $this->Data["Series"][$SerieName]["Data"] = $Data; + + $this->Data["Series"][$SerieName]["Max"] = max($this->stripVOID($this->Data["Series"][$SerieName]["Data"])); + $this->Data["Series"][$SerieName]["Min"] = min($this->stripVOID($this->Data["Series"][$SerieName]["Data"])); + } + } + } + + /* Return the data & configuration of the series */ + function getData() + { return($this->Data); } + + /* Save a palette element */ + function savePalette($ID,$Color) + { $this->Palette[$ID] = $Color; } + + /* Return the palette of the series */ + function getPalette() + { return($this->Palette); } + + /* Called by the scaling algorithm to save the config */ + function saveAxisConfig($Axis) { $this->Data["Axis"]=$Axis; } + + /* Save the Y Margin if set */ + function saveYMargin($Value) { $this->Data["YMargin"]=$Value; } + + /* Save extended configuration to the pData object */ + function saveExtendedData($Tag,$Values) { $this->Data["Extended"][$Tag]=$Values; } + + /* Called by the scaling algorithm to save the orientation of the scale */ + function saveOrientation($Orientation) { $this->Data["Orientation"]=$Orientation; } + + /* Convert a string to a single elements array */ + function convertToArray($Value) + { $Values = ""; $Values[] = $Value; return($Values); } + + /* Class string wrapper */ + function __toString() + { return("pData object."); } + + function left($value,$NbChar) { return substr($value,0,$NbChar); } + function right($value,$NbChar) { return substr($value,strlen($value)-$NbChar,$NbChar); } + function mid($value,$Depart,$NbChar) { return substr($value,$Depart-1,$NbChar); } + } ?> \ No newline at end of file diff --git a/vendor/pchart/pDraw.class.php b/vendor/pchart/pDraw.class.php index d8ceb50f3..750fbb575 100755 --- a/vendor/pchart/pDraw.class.php +++ b/vendor/pchart/pDraw.class.php @@ -1,6193 +1,6193 @@ -DataSet->getData(); - - foreach($Data["Series"] as $SerieName => $Serie) - { if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) { $Results++; } } - - return($Results); - } - - /* Fix box coordinates */ - function fixBoxCoordinates($Xa,$Ya,$Xb,$Yb) - { - $X1 = min($Xa,$Xb); $Y1 = min($Ya,$Yb); - $X2 = max($Xa,$Xb); $Y2 = max($Ya,$Yb); - - return(array($X1,$Y1,$X2,$Y2)); - } - - /* Draw a polygon */ - function drawPolygon($Points,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $NoFill = isset($Format["NoFill"]) ? $Format["NoFill"] : FALSE; - $NoBorder = isset($Format["NoBorder"]) ? $Format["NoBorder"] : FALSE; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; - $BorderAlpha = isset($Format["Alpha"]) ? $Format["Alpha"] : $Alpha / 2; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - $SkipX = isset($Format["SkipX"]) ? $Format["SkipX"] : OUT_OF_SIGHT; - $SkipY = isset($Format["SkipY"]) ? $Format["SkipY"] : OUT_OF_SIGHT; - - /* Calling the ImageFilledPolygon() function over the $Points array will round it */ - $Backup = $Points; - - if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } - - if ( $SkipX != OUT_OF_SIGHT ) { $SkipX = floor($SkipX); } - if ( $SkipY != OUT_OF_SIGHT ) { $SkipY = floor($SkipY); } - - $RestoreShadow = $this->Shadow; - if ( !$NoFill ) - { - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $this->Shadow = FALSE; - for($i=0;$i<=count($Points)-1;$i=$i+2) - { $Shadow[] = $Points[$i] + $this->ShadowX; $Shadow[] = $Points[$i+1] + $this->ShadowY; } - $this->drawPolygon($Shadow,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"NoBorder"=>TRUE)); - } - - $FillColor = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - - if ( count($Points) >= 6 ) - { ImageFilledPolygon($this->Picture,$Points,count($Points)/2,$FillColor); } - } - - if ( !$NoBorder ) - { - $Points = $Backup; - - if ( $NoFill ) - $BorderSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); - else - $BorderSettings = array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha); - - for($i=0;$i<=count($Points)-1;$i=$i+2) - { - if ( isset($Points[$i+2]) ) - { - if ( !($Points[$i] == $Points[$i+2] && $Points[$i] == $SkipX ) && !($Points[$i+1] == $Points[$i+3] && $Points[$i+1] == $SkipY ) ) - $this->drawLine($Points[$i],$Points[$i+1],$Points[$i+2],$Points[$i+3],$BorderSettings); - } - else - { - if ( !($Points[$i] == $Points[0] && $Points[$i] == $SkipX ) && !($Points[$i+1] == $Points[1] && $Points[$i+1] == $SkipY ) ) - $this->drawLine($Points[$i],$Points[$i+1],$Points[0],$Points[1],$BorderSettings); - } - } - } - - $this->Shadow = $RestoreShadow; - } - - /* Apply AALias correction to the rounded box boundaries */ - function offsetCorrection($Value,$Mode) - { - $Value = round($Value,1); - - if ( $Value == 0 && $Mode == 1 ) { return(.9); } - if ( $Value == 0 ) { return(0); } - - if ( $Mode == 1) - { if ( $Value == 1 ) { return(.9); }; if ( $Value == .1 ) { return(.9); }; if ( $Value == .2 ) { return(.8); }; if ( $Value == .3 ) { return(.8); }; if ( $Value == .4 ) { return(.7); }; if ( $Value == .5 ) { return(.5); }; if ( $Value == .6 ) { return(.8); }; if ( $Value == .7 ) { return(.7); }; if ( $Value == .8 ) { return(.6); }; if ( $Value == .9 ) { return(.9); }; } - - if ( $Mode == 2) - { if ( $Value == 1 ) { return(.9); }; if ( $Value == .1 ) { return(.1); }; if ( $Value == .2 ) { return(.2); }; if ( $Value == .3 ) { return(.3); }; if ( $Value == .4 ) { return(.4); }; if ( $Value == .5 ) { return(.5); }; if ( $Value == .6 ) { return(.8); }; if ( $Value == .7 ) { return(.7); }; if ( $Value == .8 ) { return(.8); }; if ( $Value == .9 ) { return(.9); }; } - - if ( $Mode == 3) - { if ( $Value == 1 ) { return(.1); }; if ( $Value == .1 ) { return(.1); }; if ( $Value == .2 ) { return(.2); }; if ( $Value == .3 ) { return(.3); }; if ( $Value == .4 ) { return(.4); }; if ( $Value == .5 ) { return(.9); }; if ( $Value == .6 ) { return(.6); }; if ( $Value == .7 ) { return(.7); }; if ( $Value == .8 ) { return(.4); }; if ( $Value == .9 ) { return(.5); }; } - - if ( $Mode == 4) - { if ( $Value == 1 ) { return(-1); }; if ( $Value == .1 ) { return(.1); }; if ( $Value == .2 ) { return(.2); }; if ( $Value == .3 ) { return(.3); }; if ( $Value == .4 ) { return(.1); }; if ( $Value == .5 ) { return(-.1); }; if ( $Value == .6 ) { return(.8); }; if ( $Value == .7 ) { return(.1); }; if ( $Value == .8 ) { return(.1); }; if ( $Value == .9 ) { return(.1); }; } - } - - /* Draw a rectangle with rounded corners */ - function drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - - list($X1,$Y1,$X2,$Y2) = $this->fixBoxCoordinates($X1,$Y1,$X2,$Y2); - - if ( $X2 - $X1 < $Radius ) { $Radius = floor((($X2-$X1))/2); } - if ( $Y2 - $Y1 < $Radius ) { $Radius = floor((($Y2-$Y1))/2); } - - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"NoBorder"=>TRUE); - - if ( $Radius <= 0 ) { $this->drawRectangle($X1,$Y1,$X2,$Y2,$Color); return(0); } - - if ( $this->Antialias ) - { - $this->drawLine($X1+$Radius,$Y1,$X2-$Radius,$Y1,$Color); - $this->drawLine($X2,$Y1+$Radius,$X2,$Y2-$Radius,$Color); - $this->drawLine($X2-$Radius,$Y2,$X1+$Radius,$Y2,$Color); - $this->drawLine($X1,$Y1+$Radius,$X1,$Y2-$Radius,$Color); - } - else - { - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - imageline($this->Picture,$X1+$Radius,$Y1,$X2-$Radius,$Y1,$Color); - imageline($this->Picture,$X2,$Y1+$Radius,$X2,$Y2-$Radius,$Color); - imageline($this->Picture,$X2-$Radius,$Y2,$X1+$Radius,$Y2,$Color); - imageline($this->Picture,$X1,$Y1+$Radius,$X1,$Y2-$Radius,$Color); - } - - $Step = 360 / (2 * PI * $Radius); - for($i=0;$i<=90;$i=$i+$Step) - { - $X = cos(($i+180)*PI/180) * $Radius + $X1 + $Radius; - $Y = sin(($i+180)*PI/180) * $Radius + $Y1 + $Radius; - $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - - $X = cos(($i+90)*PI/180) * $Radius + $X1 + $Radius; - $Y = sin(($i+90)*PI/180) * $Radius + $Y2 - $Radius; - $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - - $X = cos($i*PI/180) * $Radius + $X2 - $Radius; - $Y = sin($i*PI/180) * $Radius + $Y2 - $Radius; - $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - - $X = cos(($i+270)*PI/180) * $Radius + $X2 - $Radius; - $Y = sin(($i+270)*PI/180) * $Radius + $Y1 + $Radius; - $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - } - } - - /* Draw a rectangle with rounded corners */ - function drawRoundedFilledRectangle($X1,$Y1,$X2,$Y2,$Radius,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - - /* Temporary fix for AA issue */ - $Y1 = floor($Y1); $Y2 = floor($Y2); $X1 = floor($X1); $X2 = floor($X2); - - if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } - if ( $BorderR == -1 ) { $BorderR = $R; $BorderG = $G; $BorderB = $B; } - - list($X1,$Y1,$X2,$Y2) = $this->fixBoxCoordinates($X1,$Y1,$X2,$Y2); - - if ( $X2 - $X1 < $Radius*2 ) { $Radius = floor((($X2-$X1))/4); } - if ( $Y2 - $Y1 < $Radius*2 ) { $Radius = floor((($Y2-$Y1))/4); } - - $RestoreShadow = $this->Shadow; - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $this->Shadow = FALSE; - $this->drawRoundedFilledRectangle($X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,$Radius,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); - } - - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"NoBorder"=>TRUE); - - if ( $Radius <= 0 ) { $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$Color); return(0); } - - $YTop = $Y1+$Radius; - $YBottom = $Y2-$Radius; - - $Step = 360 / (2 * PI * $Radius); - $Positions = ""; $Radius--; $MinY = ""; $MaxY = ""; - for($i=0;$i<=90;$i=$i+$Step) - { - $Xp1 = cos(($i+180)*PI/180) * $Radius + $X1 + $Radius; - $Xp2 = cos(((90-$i)+270)*PI/180) * $Radius + $X2 - $Radius; - $Yp = floor(sin(($i+180)*PI/180) * $Radius + $YTop); - if ( $MinY == "" || $Yp > $MinY ) { $MinY = $Yp; } - - if ( $Xp1 <= floor($X1) ) { $Xp1++; } - if ( $Xp2 >= floor($X2) ) { $Xp2--; } - $Xp1++; - - if ( !isset($Positions[$Yp]) ) - { $Positions[$Yp]["X1"] = $Xp1; $Positions[$Yp]["X2"] = $Xp2; } - else - { $Positions[$Yp]["X1"] = ($Positions[$Yp]["X1"]+$Xp1)/2; $Positions[$Yp]["X2"] = ($Positions[$Yp]["X2"]+$Xp2)/2; } - - $Xp1 = cos(($i+90)*PI/180) * $Radius + $X1 + $Radius; - $Xp2 = cos((90-$i)*PI/180) * $Radius + $X2 - $Radius; - $Yp = floor(sin(($i+90)*PI/180) * $Radius + $YBottom); - if ( $MaxY == "" || $Yp < $MaxY ) { $MaxY = $Yp; } - - if ( $Xp1 <= floor($X1) ) { $Xp1++; } - if ( $Xp2 >= floor($X2) ) { $Xp2--; } - $Xp1++; - - if ( !isset($Positions[$Yp]) ) - { $Positions[$Yp]["X1"] = $Xp1; $Positions[$Yp]["X2"] = $Xp2; } - else - { $Positions[$Yp]["X1"] = ($Positions[$Yp]["X1"]+$Xp1)/2; $Positions[$Yp]["X2"] = ($Positions[$Yp]["X2"]+$Xp2)/2; } - } - - $ManualColor = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - foreach($Positions as $Yp => $Bounds) - { - $X1 = $Bounds["X1"]; $X1Dec = $this->getFirstDecimal($X1); if ( $X1Dec != 0 ) { $X1 = floor($X1)+1; } - $X2 = $Bounds["X2"]; $X2Dec = $this->getFirstDecimal($X2); if ( $X2Dec != 0 ) { $X2 = floor($X2)-1; } - imageline($this->Picture,$X1,$Yp,$X2,$Yp,$ManualColor); - } - $this->drawFilledRectangle($X1,$MinY+1,floor($X2),$MaxY-1,$Color); - - $Radius++; - $this->drawRoundedRectangle($X1,$Y1,$X2+1,$Y2-1,$Radius,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); - - $this->Shadow = $RestoreShadow; - } - - /* Draw a rectangle with rounded corners */ - function drawRoundedFilledRectangle_deprecated($X1,$Y1,$X2,$Y2,$Radius,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - - if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } - if ( $BorderR == -1 ) { $BorderR = $R; $BorderG = $G; $BorderB = $B; } - - list($X1,$Y1,$X2,$Y2) = $this->fixBoxCoordinates($X1,$Y1,$X2,$Y2); - - if ( $X2 - $X1 < $Radius ) { $Radius = floor((($X2-$X1)+2)/2); } - if ( $Y2 - $Y1 < $Radius ) { $Radius = floor((($Y2-$Y1)+2)/2); } - - $RestoreShadow = $this->Shadow; - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $this->Shadow = FALSE; - $this->drawRoundedFilledRectangle($X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,$Radius,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); - } - - if ( $this->getFirstDecimal($X2) >= 5 ) { $XOffset2 = 1; } else { $XOffset2 = 0; } - if ( $this->getFirstDecimal($X1) <= 5 ) { $XOffset1 = 1; } else { $XOffset1 = 0; } - - if ( !$this->Antialias ) { $XOffset1 = 1; $XOffset2 = 1; } - - $YTop = floor($Y1+$Radius); - $YBottom = floor($Y2-$Radius); - - $this->drawFilledRectangle($X1-$XOffset1,$YTop,$X2+$XOffset2,$YBottom,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"NoBorder"=>TRUE)); - - $Step = 360 / (2 * PI * $Radius); - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - $Color2 = $this->allocateColor($this->Picture,255,0,0,$Alpha); - $Drawn = ""; - - if ( $Alpha < 100 ) { $Drawn[$YTop] = FALSE; } - if ( $Alpha < 100 ) { $Drawn[$YBottom] = TRUE; } - - for($i=0;$i<=90;$i=$i+$Step) - { - $Xp1 = cos(($i+180)*PI/180) * $Radius + $X1 + $Radius; - $Xp2 = cos(((90-$i)+270)*PI/180) * $Radius + $X2 - $Radius; - $Yp = sin(($i+180)*PI/180) * $Radius + $YTop; - - if ( $this->getFirstDecimal($Xp1) > 5 ) { $XOffset1 = 1; } else { $XOffset1 = 0; } - if ( $this->getFirstDecimal($Xp2) > 5 ) { $XOffset2 = 1; } else { $XOffset2 = 0; } - if ( $this->getFirstDecimal($Yp) > 5 ) { $YOffset = 1; } else { $YOffset = 0; } - - if ( !isset($Drawn[$Yp+$YOffset]) || $Alpha == 100 ) - imageline($this->Picture,$Xp1+$XOffset1,$Yp+$YOffset,$Xp2+$XOffset2,$Yp+$YOffset,$Color); - - $Drawn[$Yp+$YOffset] = $Xp2; - - $Xp1 = cos(($i+90)*PI/180) * $Radius + $X1 + $Radius; - $Xp2 = cos((90-$i)*PI/180) * $Radius + $X2 - $Radius; - $Yp = sin(($i+90)*PI/180) * $Radius + $YBottom; - - if ( $this->getFirstDecimal($Xp1) > 7 ) { $XOffset1 = 1; } else { $XOffset1 = 0; } - if ( $this->getFirstDecimal($Xp2) > 7 ) { $XOffset2 = 1; } else { $XOffset2 = 0; } - if ( $this->getFirstDecimal($Yp) > 5 ) { $YOffset = 1; } else { $YOffset = 0; } - - if ( !isset($Drawn[$Yp+$YOffset]) || $Alpha == 100 ) - imageline($this->Picture,$Xp1+$XOffset1,$Yp+$YOffset,$Xp2+$XOffset2,$Yp+$YOffset,$Color); - - $Drawn[$Yp+$YOffset] = $Xp2; - } - - $this->drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); - - $this->Shadow = $RestoreShadow; - } - - /* Draw a rectangle */ - function drawRectangle($X1,$Y1,$X2,$Y2,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; - $NoAngle = isset($Format["NoAngle"]) ? $Format["NoAngle"] : FALSE; - - if ($X1 > $X2) { list($X1, $X2) = array($X2, $X1); } - if ($Y1 > $Y2) { list($Y1, $Y2) = array($Y2, $Y1); } - - if ( $this->Antialias ) - { - if ( $NoAngle ) - { - $this->drawLine($X1+1,$Y1,$X2-1,$Y1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - $this->drawLine($X2,$Y1+1,$X2,$Y2-1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - $this->drawLine($X2-1,$Y2,$X1+1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - $this->drawLine($X1,$Y1+1,$X1,$Y2-1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - } - else - { - $this->drawLine($X1+1,$Y1,$X2-1,$Y1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - $this->drawLine($X2,$Y1,$X2,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - $this->drawLine($X2-1,$Y2,$X1+1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - $this->drawLine($X1,$Y1,$X1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - } - } - else - { - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - imagerectangle($this->Picture,$X1,$Y1,$X2,$Y2,$Color); - } - } - - /* Draw a filled rectangle */ - function drawFilledRectangle($X1,$Y1,$X2,$Y2,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; - $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; - $NoAngle = isset($Format["NoAngle"]) ? $Format["NoAngle"] : NULL; - $Dash = isset($Format["Dash"]) ? $Format["Dash"] : FALSE; - $DashStep = isset($Format["DashStep"]) ? $Format["DashStep"] : 4; - $DashR = isset($Format["DashR"]) ? $Format["DashR"] : 0; - $DashG = isset($Format["DashG"]) ? $Format["DashG"] : 0; - $DashB = isset($Format["DashB"]) ? $Format["DashB"] : 0; - $NoBorder = isset($Format["NoBorder"]) ? $Format["NoBorder"] : FALSE; - - if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } - - if ($X1 > $X2) { list($X1, $X2) = array($X2, $X1); } - if ($Y1 > $Y2) { list($Y1, $Y2) = array($Y2, $Y1); } - - $RestoreShadow = $this->Shadow; - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $this->Shadow = FALSE; - $this->drawFilledRectangle($X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"Ticks"=>$Ticks,"NoAngle"=>$NoAngle)); - } - - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - if ( $NoAngle ) - { - imagefilledrectangle($this->Picture,ceil($X1)+1,ceil($Y1),floor($X2)-1,floor($Y2),$Color); - imageline($this->Picture,ceil($X1),ceil($Y1)+1,ceil($X1),floor($Y2)-1,$Color); - imageline($this->Picture,floor($X2),ceil($Y1)+1,floor($X2),floor($Y2)-1,$Color); - } - else - imagefilledrectangle($this->Picture,ceil($X1),ceil($Y1),floor($X2),floor($Y2),$Color); - - if ( $Dash ) - { - if ( $BorderR != -1 ) { $iX1=$X1+1; $iY1=$Y1+1; $iX2=$X2-1; $iY2=$Y2-1; } else { $iX1=$X1; $iY1=$Y1; $iX2=$X2; $iY2=$Y2; } - - $Color = $this->allocateColor($this->Picture,$DashR,$DashG,$DashB,$Alpha); - $Y=$iY1-$DashStep; - for($X=$iX1; $X<=$iX2+($iY2-$iY1); $X=$X+$DashStep) - { - $Y=$Y+$DashStep; - if ( $X > $iX2 ) { $Xa = $X-($X-$iX2); $Ya = $iY1+($X-$iX2); } else { $Xa = $X; $Ya = $iY1; } - if ( $Y > $iY2 ) { $Xb = $iX1+($Y-$iY2); $Yb = $Y-($Y-$iY2); } else { $Xb = $iX1; $Yb = $Y; } - imageline($this->Picture,$Xa,$Ya,$Xb,$Yb,$Color); - } - } - - if ( $this->Antialias && !$NoBorder ) - { - if ( $X1 < ceil($X1) ) - { - $AlphaA = $Alpha * (ceil($X1) - $X1); - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$AlphaA); - imageline($this->Picture,ceil($X1)-1,ceil($Y1),ceil($X1)-1,floor($Y2),$Color); - } - - if ( $Y1 < ceil($Y1) ) - { - $AlphaA = $Alpha * (ceil($Y1) - $Y1); - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$AlphaA); - imageline($this->Picture,ceil($X1),ceil($Y1)-1,floor($X2),ceil($Y1)-1,$Color); - } - - if ( $X2 > floor($X2) ) - { - $AlphaA = $Alpha * (.5-($X2 - floor($X2))); - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$AlphaA); - imageline($this->Picture,floor($X2)+1,ceil($Y1),floor($X2)+1,floor($Y2),$Color); - } - - if ( $Y2 > floor($Y2) ) - { - $AlphaA = $Alpha * (.5-($Y2 - floor($Y2))); - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$AlphaA); - imageline($this->Picture,ceil($X1),floor($Y2)+1,floor($X2),floor($Y2)+1,$Color); - } - } - - if ( $BorderR != -1 ) - $this->drawRectangle($X1,$Y1,$X2,$Y2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$Ticks,"NoAngle"=>$NoAngle)); - - $this->Shadow = $RestoreShadow; - } - - /* Draw a rectangular marker of the specified size */ - function drawRectangleMarker($X,$Y,$Format="") - { - $Size = isset($Format["Size"]) ? $Format["Size"] : 4; - - $HalfSize = floor($Size/2); - $this->drawFilledRectangle($X-$HalfSize,$Y-$HalfSize,$X+$HalfSize,$Y+$HalfSize,$Format); - } - - /* Drawn a spline based on the bezier function */ - function drawSpline($Coordinates,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Force = isset($Format["Force"]) ? $Format["Force"] : 30; - $Forces = isset($Format["Forces"]) ? $Format["Forces"] : NULL; - $ShowC = isset($Format["ShowControl"]) ? $Format["ShowControl"] : FALSE; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; - $PathOnly = isset($Format["PathOnly"]) ? $Format["PathOnly"] : FALSE; - $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; - - $Cpt = NULL; $Mode = NULL; $Result = ""; - for($i=1;$i<=count($Coordinates)-1;$i++) - { - $X1 = $Coordinates[$i-1][0]; $Y1 = $Coordinates[$i-1][1]; - $X2 = $Coordinates[$i][0]; $Y2 = $Coordinates[$i][1]; - - if ( $Forces != NULL ) { $Force = $Forces[$i]; } - - /* First segment */ - if ( $i == 1 ) - { $Xv1 = $X1; $Yv1 = $Y1; } - else - { - $Angle1 = $this->getAngle($XLast,$YLast,$X1,$Y1); - $Angle2 = $this->getAngle($X1,$Y1,$X2,$Y2); - $XOff = cos($Angle2 * PI / 180) * $Force + $X1; - $YOff = sin($Angle2 * PI / 180) * $Force + $Y1; - - $Xv1 = cos($Angle1 * PI / 180) * $Force + $XOff; - $Yv1 = sin($Angle1 * PI / 180) * $Force + $YOff; - } - - /* Last segment */ - if ( $i == count($Coordinates)-1 ) - { $Xv2 = $X2; $Yv2 = $Y2; } - else - { - $Angle1 = $this->getAngle($X2,$Y2,$Coordinates[$i+1][0],$Coordinates[$i+1][1]); - $Angle2 = $this->getAngle($X1,$Y1,$X2,$Y2); - $XOff = cos(($Angle2+180) * PI / 180) * $Force + $X2; - $YOff = sin(($Angle2+180) * PI / 180) * $Force + $Y2; - - $Xv2 = cos(($Angle1+180) * PI / 180) * $Force + $XOff; - $Yv2 = sin(($Angle1+180) * PI / 180) * $Force + $YOff; - } - - $Path = $this->drawBezier($X1,$Y1,$X2,$Y2,$Xv1,$Yv1,$Xv2,$Yv2,$Format); - if ($PathOnly) { $Result[] = $Path; } - - $XLast = $X1; $YLast = $Y1; - } - - return($Result); - } - - /* Draw a bezier curve with two controls points */ - function drawBezier($X1,$Y1,$X2,$Y2,$Xv1,$Yv1,$Xv2,$Yv2,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $ShowC = isset($Format["ShowControl"]) ? $Format["ShowControl"] : FALSE; - $Segments = isset($Format["Segments"]) ? $Format["Segments"] : NULL; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; - $NoDraw = isset($Format["NoDraw"]) ? $Format["NoDraw"] : FALSE; - $PathOnly = isset($Format["PathOnly"]) ? $Format["PathOnly"] : FALSE; - $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; - $DrawArrow = isset($Format["DrawArrow"]) ? $Format["DrawArrow"] : FALSE; - $ArrowSize = isset($Format["ArrowSize"]) ? $Format["ArrowSize"] : 10; - $ArrowRatio = isset($Format["ArrowRatio"]) ? $Format["ArrowRatio"] : .5; - $ArrowTwoHeads = isset($Format["ArrowTwoHeads"]) ? $Format["ArrowTwoHeads"] : FALSE; - - if ( $Segments == NULL ) - { - $Length = $this->getLength($X1,$Y1,$X2,$Y2); - $Precision = ($Length*125)/1000; - } - else - $Precision = $Segments; - - $P[0]["X"] = $X1; $P[0]["Y"] = $Y1; - $P[1]["X"] = $Xv1; $P[1]["Y"] = $Yv1; - $P[2]["X"] = $Xv2; $P[2]["Y"] = $Yv2; - $P[3]["X"] = $X2; $P[3]["Y"] = $Y2; - - /* Compute the bezier points */ - $Q = ""; $ID = 0; $Path = ""; - for($i=0;$i<=$Precision;$i=$i+1) - { - $u = $i / $Precision; - - $C = ""; - $C[0] = (1 - $u) * (1 - $u) * (1 - $u); - $C[1] = ($u * 3) * (1 - $u) * (1 - $u); - $C[2] = 3 * $u * $u * (1 - $u); - $C[3] = $u * $u * $u; - - for($j=0;$j<=3;$j++) - { - if ( !isset($Q[$ID]) ) { $Q[$ID] = ""; } - if ( !isset($Q[$ID]["X"]) ) { $Q[$ID]["X"] = 0; } - if ( !isset($Q[$ID]["Y"]) ) { $Q[$ID]["Y"] = 0; } - - $Q[$ID]["X"] = $Q[$ID]["X"] + $P[$j]["X"] * $C[$j]; - $Q[$ID]["Y"] = $Q[$ID]["Y"] + $P[$j]["Y"] * $C[$j]; - } - $ID++; - } - $Q[$ID]["X"] = $X2; $Q[$ID]["Y"] = $Y2; - - if ( !$NoDraw ) - { - /* Display the control points */ - if ( $ShowC && !$PathOnly ) - { - $Xv1 = floor($Xv1); $Yv1 = floor($Yv1); $Xv2 = floor($Xv2); $Yv2 = floor($Yv2); - - $this->drawLine($X1,$Y1,$X2,$Y2,array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>30)); - - $MyMarkerSettings = array("R"=>255,"G"=>0,"B"=>0,"BorderR"=>255,"BorderB"=>255,"BorderG"=>255,"Size"=>4); - $this->drawRectangleMarker($Xv1,$Yv1,$MyMarkerSettings); - $this->drawText($Xv1+4,$Yv1,"v1"); - $MyMarkerSettings = array("R"=>0,"G"=>0,"B"=>255,"BorderR"=>255,"BorderB"=>255,"BorderG"=>255,"Size"=>4); - $this->drawRectangleMarker($Xv2,$Yv2,$MyMarkerSettings); - $this->drawText($Xv2+4,$Yv2,"v2"); - } - - /* Draw the bezier */ - $LastX = NULL; $LastY = NULL; $Cpt = NULL; $Mode = NULL; $ArrowS = NULL; - foreach ($Q as $Key => $Point) - { - $X = $Point["X"]; $Y = $Point["Y"]; - - /* Get the first segment */ - if ( $ArrowS == NULL && $LastX != NULL && $LastY != NULL ) - { $ArrowS["X2"] = $LastX; $ArrowS["Y2"] = $LastY; $ArrowS["X1"] = $X; $ArrowS["Y1"] = $Y; } - - if ( $LastX != NULL && $LastY != NULL && !$PathOnly) - list($Cpt,$Mode) = $this->drawLine($LastX,$LastY,$X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Cpt"=>$Cpt,"Mode"=>$Mode,"Weight"=>$Weight)); - - /* Get the last segment */ - $ArrowE["X1"] = $LastX; $ArrowE["Y1"] = $LastY; $ArrowE["X2"] = $X; $ArrowE["Y2"] = $Y; - - $LastX = $X; $LastY = $Y; - } - - if ( $DrawArrow && !$PathOnly ) - { - $ArrowSettings = array("FillR"=>$R,"FillG"=>$G,"FillB"=>$B,"Alpha"=>$Alpha,"Size"=>$ArrowSize,"Ratio"=>$ArrowRatio); - if ( $ArrowTwoHeads ) - $this->drawArrow($ArrowS["X1"],$ArrowS["Y1"],$ArrowS["X2"],$ArrowS["Y2"],$ArrowSettings); - - $this->drawArrow($ArrowE["X1"],$ArrowE["Y1"],$ArrowE["X2"],$ArrowE["Y2"],$ArrowSettings); - } - } - return($Q); - } - - /* Draw a line between two points */ - function drawLine($X1,$Y1,$X2,$Y2,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; - $Cpt = isset($Format["Cpt"]) ? $Format["Cpt"] : 1; - $Mode = isset($Format["Mode"]) ? $Format["Mode"] : 1; - $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; - $Threshold = isset($Format["Threshold"]) ? $Format["Threshold"] : NULL; - - if ( $this->Antialias == FALSE && $Ticks == NULL ) - { - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $ShadowColor = $this->allocateColor($this->Picture,$this->ShadowR,$this->ShadowG,$this->ShadowB,$this->Shadowa); - imageline($this->Picture,$X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,$ShadowColor); - } - - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - imageline($this->Picture,$X1,$Y1,$X2,$Y2,$Color); - return(0); - } - - $Distance = sqrt(($X2-$X1)*($X2-$X1)+($Y2-$Y1)*($Y2-$Y1)); - if ( $Distance == 0 ) { return(-1); } - - /* Derivative algorithm for overweighted lines, re-route to polygons primitives */ - if ( $Weight != NULL ) - { - $Angle = $this->getAngle($X1,$Y1,$X2,$Y2); - $PolySettings = array ("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderAlpha"=>$Alpha); - - if ( $Ticks == NULL ) - { - $Points = ""; - $Points[] = cos(deg2rad($Angle-90)) * $Weight + $X1; $Points[] = sin(deg2rad($Angle-90)) * $Weight + $Y1; - $Points[] = cos(deg2rad($Angle+90)) * $Weight + $X1; $Points[] = sin(deg2rad($Angle+90)) * $Weight + $Y1; - $Points[] = cos(deg2rad($Angle+90)) * $Weight + $X2; $Points[] = sin(deg2rad($Angle+90)) * $Weight + $Y2; - $Points[] = cos(deg2rad($Angle-90)) * $Weight + $X2; $Points[] = sin(deg2rad($Angle-90)) * $Weight + $Y2; - - $this->drawPolygon($Points,$PolySettings); - } - else - { - for($i=0;$i<=$Distance;$i=$i+$Ticks*2) - { - $Xa = (($X2-$X1)/$Distance) * $i + $X1; $Ya = (($Y2-$Y1)/$Distance) * $i + $Y1; - $Xb = (($X2-$X1)/$Distance) * ($i+$Ticks) + $X1; $Yb = (($Y2-$Y1)/$Distance) * ($i+$Ticks) + $Y1; - - $Points = ""; - $Points[] = cos(deg2rad($Angle-90)) * $Weight + $Xa; $Points[] = sin(deg2rad($Angle-90)) * $Weight + $Ya; - $Points[] = cos(deg2rad($Angle+90)) * $Weight + $Xa; $Points[] = sin(deg2rad($Angle+90)) * $Weight + $Ya; - $Points[] = cos(deg2rad($Angle+90)) * $Weight + $Xb; $Points[] = sin(deg2rad($Angle+90)) * $Weight + $Yb; - $Points[] = cos(deg2rad($Angle-90)) * $Weight + $Xb; $Points[] = sin(deg2rad($Angle-90)) * $Weight + $Yb; - - $this->drawPolygon($Points,$PolySettings); - } - } - - return(1); - } - - $XStep = ($X2-$X1) / $Distance; - $YStep = ($Y2-$Y1) / $Distance; - - for($i=0;$i<=$Distance;$i++) - { - $X = $i * $XStep + $X1; - $Y = $i * $YStep + $Y1; - - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); - - if ( $Threshold != NULL ) - { - foreach($Threshold as $Key => $Parameters) - { - if ( $Y <= $Parameters["MinX"] && $Y >= $Parameters["MaxX"]) - { - if ( isset($Parameters["R"]) ) { $RT = $Parameters["R"]; } else { $RT = 0; } - if ( isset($Parameters["G"]) ) { $GT = $Parameters["G"]; } else { $GT = 0; } - if ( isset($Parameters["B"]) ) { $BT = $Parameters["B"]; } else { $BT = 0; } - if ( isset($Parameters["Alpha"]) ) { $AlphaT = $Parameters["Alpha"]; } else { $AlphaT = 0; } - $Color = array("R"=>$RT,"G"=>$GT,"B"=>$BT,"Alpha"=>$AlphaT); - } - } - } - - if ( $Ticks != NULL ) - { - if ( $Cpt % $Ticks == 0 ) - { $Cpt = 0; if ( $Mode == 1 ) { $Mode = 0; } else { $Mode = 1; } } - - if ( $Mode == 1 ) - $this->drawAntialiasPixel($X,$Y,$Color); - - $Cpt++; - } - else - $this->drawAntialiasPixel($X,$Y,$Color); - } - - return(array($Cpt,$Mode)); - } - - /* Draw a circle */ - function drawCircle($Xc,$Yc,$Height,$Width,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; - - $Height = abs($Height); - $Width = abs($Width); - - if ( $Height == 0 ) { $Height = 1; } - if ( $Width == 0 ) { $Width = 1; } - $Xc = floor($Xc); $Yc = floor($Yc); - - $RestoreShadow = $this->Shadow; - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $this->Shadow = FALSE; - $this->drawCircle($Xc+$this->ShadowX,$Yc+$this->ShadowY,$Height,$Width,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"Ticks"=>$Ticks)); - } - - if ( $Width == 0 ) { $Width = $Height; } - if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } - if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } - if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } - - $Step = 360 / (2 * PI * max($Width,$Height)); - $Mode = 1; $Cpt = 1; - for($i=0;$i<=360;$i=$i+$Step) - { - $X = cos($i*PI/180) * $Height + $Xc; - $Y = sin($i*PI/180) * $Width + $Yc; - - if ( $Ticks != NULL ) - { - if ( $Cpt % $Ticks == 0 ) - { $Cpt = 0; if ( $Mode == 1 ) { $Mode = 0; } else { $Mode = 1; } } - - if ( $Mode == 1 ) - $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - - $Cpt++; - } - else - $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - - } - $this->Shadow = $RestoreShadow; - } - - /* Draw a filled circle */ - function drawFilledCircle($X,$Y,$Radius,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; - $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - - if ( $Radius == 0 ) { $Radius = 1; } - if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } - $X = floor($X); $Y = floor($Y); - - $Radius = abs($Radius); - - $RestoreShadow = $this->Shadow; - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $this->Shadow = FALSE; - $this->drawFilledCircle($X+$this->ShadowX,$Y+$this->ShadowY,$Radius,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"Ticks"=>$Ticks)); - } - - $this->Mask = ""; - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - for ($i=0; $i<=$Radius*2; $i++) - { - $Slice = sqrt($Radius * $Radius - ($Radius - $i) * ($Radius - $i)); - $XPos = floor($Slice); - $YPos = $Y + $i - $Radius; - $AAlias = $Slice - floor($Slice); - - $this->Mask[$X-$XPos][$YPos] = TRUE; - $this->Mask[$X+$XPos][$YPos] = TRUE; - imageline($this->Picture,$X-$XPos,$YPos,$X+$XPos,$YPos,$Color); - } - if ( $this->Antialias ) - $this->drawCircle($X,$Y,$Radius,$Radius,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - - $this->Mask = ""; - - if ( $BorderR != -1 ) - $this->drawCircle($X,$Y,$Radius,$Radius,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$Ticks)); - - $this->Shadow = $RestoreShadow; - } - - /* Write text */ - function drawText($X,$Y,$Text,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : $this->FontColorR; - $G = isset($Format["G"]) ? $Format["G"] : $this->FontColorG; - $B = isset($Format["B"]) ? $Format["B"] : $this->FontColorB; - $Angle = isset($Format["Angle"]) ? $Format["Angle"] : 0; - $Align = isset($Format["Align"]) ? $Format["Align"] : TEXT_ALIGN_BOTTOMLEFT; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : $this->FontColorA; - $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; - $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; - $ShowOrigine = isset($Format["ShowOrigine"]) ? $Format["ShowOrigine"] : FALSE; - $TOffset = isset($Format["TOffset"]) ? $Format["TOffset"] : 2; - $DrawBox = isset($Format["DrawBox"]) ? $Format["DrawBox"] : FALSE; - $DrawBoxBorder = isset($Format["DrawBoxBorder"]) ? $Format["DrawBoxBorder"] : TRUE; - $BorderOffset = isset($Format["BorderOffset"]) ? $Format["BorderOffset"] : 6; - $BoxRounded = isset($Format["BoxRounded"]) ? $Format["BoxRounded"] : FALSE; - $RoundedRadius = isset($Format["RoundedRadius"]) ? $Format["RoundedRadius"] : 6; - $BoxR = isset($Format["BoxR"]) ? $Format["BoxR"] : 255; - $BoxG = isset($Format["BoxG"]) ? $Format["BoxG"] : 255; - $BoxB = isset($Format["BoxB"]) ? $Format["BoxB"] : 255; - $BoxAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 50; - $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : ""; - $BoxBorderR = isset($Format["BoxR"]) ? $Format["BoxR"] : 0; - $BoxBorderG = isset($Format["BoxG"]) ? $Format["BoxG"] : 0; - $BoxBorderB = isset($Format["BoxB"]) ? $Format["BoxB"] : 0; - $BoxBorderAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 50; - $NoShadow = isset($Format["NoShadow"]) ? $Format["NoShadow"] : FALSE; - - $Shadow = $this->Shadow; - if ( $NoShadow ) { $this->Shadow = FALSE; } - - if ( $BoxSurrounding != "" ) { $BoxBorderR = $BoxR - $BoxSurrounding; $BoxBorderG = $BoxG - $BoxSurrounding; $BoxBorderB = $BoxB - $BoxSurrounding; $BoxBorderAlpha = $BoxAlpha; } - - if ( $ShowOrigine ) - { - $MyMarkerSettings = array("R"=>255,"G"=>0,"B"=>0,"BorderR"=>255,"BorderB"=>255,"BorderG"=>255,"Size"=>4); - $this->drawRectangleMarker($X,$Y,$MyMarkerSettings); - } - - $TxtPos = $this->getTextBox($X,$Y,$FontName,$FontSize,$Angle,$Text); - - if ( $DrawBox && ($Angle == 0 || $Angle == 90 || $Angle == 180 || $Angle == 270)) - { - $T[0]["X"]=0;$T[0]["Y"]=0;$T[1]["X"]=0;$T[1]["Y"]=0;$T[2]["X"]=0;$T[2]["Y"]=0;$T[3]["X"]=0;$T[3]["Y"]=0; - if ( $Angle == 0 ) { $T[0]["X"]=-$TOffset;$T[0]["Y"]=$TOffset;$T[1]["X"]=$TOffset;$T[1]["Y"]=$TOffset;$T[2]["X"]=$TOffset;$T[2]["Y"]=-$TOffset;$T[3]["X"]=-$TOffset;$T[3]["Y"]=-$TOffset; } - - $X1 = min($TxtPos[0]["X"],$TxtPos[1]["X"],$TxtPos[2]["X"],$TxtPos[3]["X"]) - $BorderOffset + 3; - $Y1 = min($TxtPos[0]["Y"],$TxtPos[1]["Y"],$TxtPos[2]["Y"],$TxtPos[3]["Y"]) - $BorderOffset; - $X2 = max($TxtPos[0]["X"],$TxtPos[1]["X"],$TxtPos[2]["X"],$TxtPos[3]["X"]) + $BorderOffset + 3; - $Y2 = max($TxtPos[0]["Y"],$TxtPos[1]["Y"],$TxtPos[2]["Y"],$TxtPos[3]["Y"]) + $BorderOffset - 3; - - $X1 = $X1 - $TxtPos[$Align]["X"] + $X + $T[0]["X"]; - $Y1 = $Y1 - $TxtPos[$Align]["Y"] + $Y + $T[0]["Y"]; - $X2 = $X2 - $TxtPos[$Align]["X"] + $X + $T[0]["X"]; - $Y2 = $Y2 - $TxtPos[$Align]["Y"] + $Y + $T[0]["Y"]; - - $Settings = array("R"=>$BoxR,"G"=>$BoxG,"B"=>$BoxB,"Alpha"=>$BoxAlpha,"BorderR"=>$BoxBorderR,"BorderG"=>$BoxBorderG,"BorderB"=>$BoxBorderB,"BorderAlpha"=>$BoxBorderAlpha); - - if ( $BoxRounded ) - { $this->drawRoundedFilledRectangle($X1,$Y1,$X2,$Y2,$RoundedRadius,$Settings); } - else - { $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$Settings); } - } - - $X = $X - $TxtPos[$Align]["X"] + $X; - $Y = $Y - $TxtPos[$Align]["Y"] + $Y; - - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $C_ShadowColor = $this->allocateColor($this->Picture,$this->ShadowR,$this->ShadowG,$this->ShadowB,$this->Shadowa); - imagettftext($this->Picture,$FontSize,$Angle,$X+$this->ShadowX,$Y+$this->ShadowY,$C_ShadowColor,$FontName,$Text); - } - - $C_TextColor = $this->AllocateColor($this->Picture,$R,$G,$B,$Alpha); - imagettftext($this->Picture,$FontSize,$Angle,$X,$Y,$C_TextColor,$FontName,$Text); - - $this->Shadow = $Shadow; - - return($TxtPos); - } - - /* Draw a gradient within a defined area */ - function drawGradientArea($X1,$Y1,$X2,$Y2,$Direction,$Format="") - { - $StartR = isset($Format["StartR"]) ? $Format["StartR"] : 90; - $StartG = isset($Format["StartG"]) ? $Format["StartG"] : 90; - $StartB = isset($Format["StartB"]) ? $Format["StartB"] : 90; - $EndR = isset($Format["EndR"]) ? $Format["EndR"] : 0; - $EndG = isset($Format["EndG"]) ? $Format["EndG"] : 0; - $EndB = isset($Format["EndB"]) ? $Format["EndB"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Levels = isset($Format["Levels"]) ? $Format["Levels"] : NULL; - - $Shadow = $this->Shadow; - $this->Shadow = FALSE; - - if ( $StartR == $EndR && $StartG == $EndG && $StartB == $EndB ) - { - $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,array("R"=>$StartR,"G"=>$StartG,"B"=>$StartB,"Alpha"=>$Alpha)); - return(0); - } - - if ( $Levels != NULL ) - { $EndR=$StartR+$Levels; $EndG=$StartG+$Levels; $EndB=$StartB+$Levels; } - - if ($X1 > $X2) { list($X1, $X2) = array($X2, $X1); } - if ($Y1 > $Y2) { list($Y1, $Y2) = array($Y2, $Y1); } - - if ( $Direction == DIRECTION_VERTICAL ) { $Width = abs($Y2-$Y1); } - if ( $Direction == DIRECTION_HORIZONTAL ) { $Width = abs($X2-$X1); } - - $Step = max(abs($EndR-$StartR),abs($EndG-$StartG),abs($EndB-$StartB)); - $StepSize = $Width/$Step; - $RStep = ($EndR-$StartR)/$Step; - $GStep = ($EndG-$StartG)/$Step; - $BStep = ($EndB-$StartB)/$Step; - - $R=$StartR;$G=$StartG;$B=$StartB; - switch($Direction) - { - case DIRECTION_VERTICAL: - $StartY = $Y1; $EndY = floor($Y2)+1; $LastY2 = $StartY; - for($i=0;$i<=$Step;$i++) - { - $Y2 = floor($StartY + ($i * $StepSize)); - - if ($Y2 > $EndY) { $Y2 = $EndY; } - if (($Y1 != $Y2 && $Y1 < $Y2) || $Y2 == $EndY) - { - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); - $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$Color); - $LastY2 = max($LastY2,$Y2); - $Y1 = $Y2+1; - } - $R = $R + $RStep; $G = $G + $GStep; $B = $B + $BStep; - } - if ( $LastY2 < $EndY && isset($Color)) { for ($i=$LastY2+1;$i<=$EndY;$i++) { $this->drawLine($X1,$i,$X2,$i,$Color); } } - break; - - case DIRECTION_HORIZONTAL: - $StartX = $X1; $EndX = $X2; - for($i=0;$i<=$Step;$i++) - { - $X2 = floor($StartX + ($i * $StepSize)); - - if ($X2 > $EndX) { $X2 = $EndX; } - if (($X1 != $X2 && $X1 < $X2) || $X2 == $EndX) - { - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); - $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$Color); - $X1 = $X2+1; - } - $R = $R + $RStep; $G = $G + $GStep; $B = $B + $BStep; - } - if ( $X2 < $EndX && isset($Color)) { $this->drawFilledRectangle($X2,$Y1,$EndX,$Y2,$Color); } - break; - } - - $this->Shadow = $Shadow; - - } - - /* Draw an aliased pixel */ - function drawAntialiasPixel($X,$Y,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - - if ( $X < 0 || $Y < 0 || $X >= $this->XSize || $Y >= $this->YSize ) - return(-1); - - if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } - if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } - if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } - - if ( !$this->Antialias ) - { - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $ShadowColor = $this->allocateColor($this->Picture,$this->ShadowR,$this->ShadowG,$this->ShadowB,$this->Shadowa); - imagesetpixel($this->Picture,$X+$this->ShadowX,$Y+$this->ShadowY,$ShadowColor); - } - - $PlotColor = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - imagesetpixel($this->Picture,$X,$Y,$PlotColor); - - return(0); - } - - $Plot = ""; - $Xi = floor($X); - $Yi = floor($Y); - - if ( $Xi == $X && $Yi == $Y) - { - if ( $Alpha == 100 ) - $this->drawAlphaPixel($X,$Y,100,$R,$G,$B); - else - $this->drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B); - } - else - { - $Alpha1 = (((1 - ($X - floor($X))) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha; - if ( $Alpha1 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi,$Alpha1,$R,$G,$B); } - - $Alpha2 = ((($X - floor($X)) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha; - if ( $Alpha2 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi,$Alpha2,$R,$G,$B); } - - $Alpha3 = (((1 - ($X - floor($X))) * ($Y - floor($Y)) * 100) / 100) * $Alpha; - if ( $Alpha3 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi+1,$Alpha3,$R,$G,$B); } - - $Alpha4 = ((($X - floor($X)) * ($Y - floor($Y)) * 100) / 100) * $Alpha; - if ( $Alpha4 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi+1,$Alpha4,$R,$G,$B); } - } - } - - /* Draw a semi-transparent pixel */ - function drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B) - { - if ( isset($this->Mask[$X])) { if ( isset($this->Mask[$X][$Y]) ) { return(0); } } - - if ( $X < 0 || $Y < 0 || $X >= $this->XSize || $Y >= $this->YSize ) - return(-1); - - if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } - if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } - if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } - - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $AlphaFactor = floor(($Alpha / 100) * $this->Shadowa); - $ShadowColor = $this->allocateColor($this->Picture,$this->ShadowR,$this->ShadowG,$this->ShadowB,$AlphaFactor); - imagesetpixel($this->Picture,$X+$this->ShadowX,$Y+$this->ShadowY,$ShadowColor); - } - - $C_Aliased = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - imagesetpixel($this->Picture,$X,$Y,$C_Aliased); - } - - /* Convert apha to base 10 */ - function convertAlpha($AlphaValue) - { return((127/100)*(100-$AlphaValue)); } - - /* Allocate a color with transparency */ - function allocateColor($Picture,$R,$G,$B,$Alpha=100) - { - if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } - if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } - if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } - if ( $Alpha < 0 ) { $Alpha = 0; } - if ( $Alpha > 100) { $Alpha = 100; } - - $Alpha = $this->convertAlpha($Alpha); - return(imagecolorallocatealpha($Picture,$R,$G,$B,$Alpha)); - } - - /* Load a PNG file and draw it over the chart */ - function drawFromPNG($X,$Y,$FileName) - { $this->drawFromPicture(1,$FileName,$X,$Y); } - - /* Load a GIF file and draw it over the chart */ - function drawFromGIF($X,$Y,$FileName) - { $this->drawFromPicture(2,$FileName,$X,$Y); } - - /* Load a JPEG file and draw it over the chart */ - function drawFromJPG($X,$Y,$FileName) - { $this->drawFromPicture(3,$FileName,$X,$Y); } - - function getPicInfo($FileName) - { - $Infos = getimagesize($FileName); - $Width = $Infos[0]; - $Height = $Infos[1]; - $Type = $Infos["mime"]; - - if ( $Type == "image/png") { $Type = 1; } - if ( $Type == "image/gif") { $Type = 2; } - if ( $Type == "image/jpeg ") { $Type = 3; } - - return(array($Width,$Height,$Type)); - } - - /* Generic loader function for external pictures */ - function drawFromPicture($PicType,$FileName,$X,$Y) - { - if ( file_exists($FileName)) - { - list($Width,$Height) = $this->getPicInfo($FileName); - - if ( $PicType == 1 ) - { $Raster = imagecreatefrompng($FileName); } - elseif ( $PicType == 2 ) - { $Raster = imagecreatefromgif($FileName); } - elseif ( $PicType == 3 ) - { $Raster = imagecreatefromjpeg($FileName); } - else - { return(0); } - - - $RestoreShadow = $this->Shadow; - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $this->Shadow = FALSE; - if ( $PicType == 3 ) - $this->drawFilledRectangle($X+$this->ShadowX,$Y+$this->ShadowY,$X+$Width+$this->ShadowX,$Y+$Height+$this->ShadowY,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); - else - { - $TranparentID = imagecolortransparent($Raster); - for ($Xc=0;$Xc<=$Width-1;$Xc++) - { - for ($Yc=0;$Yc<=$Height-1;$Yc++) - { - $RGBa = imagecolorat($Raster,$Xc,$Yc); - $Values = imagecolorsforindex($Raster,$RGBa); - if ( $Values["alpha"] < 120 ) - { - $AlphaFactor = floor(($this->Shadowa / 100) * ((100 / 127) * (127-$Values["alpha"]))); - $this->drawAlphaPixel($X+$Xc+$this->ShadowX,$Y+$Yc+$this->ShadowY,$AlphaFactor,$this->ShadowR,$this->ShadowG,$this->ShadowB); - } - } - } - } - } - $this->Shadow = $RestoreShadow; - - imagecopy($this->Picture,$Raster,$X,$Y,0,0,$Width,$Height); - imagedestroy($Raster); - } - } - - /* Draw an arrow */ - function drawArrow($X1,$Y1,$X2,$Y2,$Format="") - { - $FillR = isset($Format["FillR"]) ? $Format["FillR"] : 0; - $FillG = isset($Format["FillG"]) ? $Format["FillG"] : 0; - $FillB = isset($Format["FillB"]) ? $Format["FillB"] : 0; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $FillR; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $FillG; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $FillB; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Size = isset($Format["Size"]) ? $Format["Size"] : 10; - $Ratio = isset($Format["Ratio"]) ? $Format["Ratio"] : .5; - $TwoHeads = isset($Format["TwoHeads"]) ? $Format["TwoHeads"] : FALSE; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : FALSE; - - /* Calculate the line angle */ - $Angle = $this->getAngle($X1,$Y1,$X2,$Y2); - - /* Override Shadow support, this will be managed internally */ - $RestoreShadow = $this->Shadow; - if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) - { - $this->Shadow = FALSE; - $this->drawArrow($X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,array("FillR"=>$this->ShadowR,"FillG"=>$this->ShadowG,"FillB"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"Size"=>$Size,"Ratio"=>$Ratio,"TwoHeads"=>$TwoHeads,"Ticks"=>$Ticks)); - } - - /* Draw the 1st Head */ - $TailX = cos(($Angle-180)*PI/180)*$Size+$X2; - $TailY = sin(($Angle-180)*PI/180)*$Size+$Y2; - - $Points = ""; - $Points[] = $X2; $Points[] = $Y2; - $Points[] = cos(($Angle-90)*PI/180)*$Size*$Ratio+$TailX; $Points[] = sin(($Angle-90)*PI/180)*$Size*$Ratio+$TailY; - $Points[] = cos(($Angle-270)*PI/180)*$Size*$Ratio+$TailX; $Points[] = sin(($Angle-270)*PI/180)*$Size*$Ratio+$TailY; - $Points[] = $X2; $Points[] = $Y2; - - /* Visual correction */ - if ($Angle == 180 || $Angle == 360 ) { $Points[4] = $Points[2]; } - if ($Angle == 90 || $Angle == 270 ) { $Points[5] = $Points[3]; } - - $ArrowColor = $this->allocateColor($this->Picture,$FillR,$FillG,$FillB,$Alpha); - ImageFilledPolygon($this->Picture,$Points,4,$ArrowColor); - - $this->drawLine($Points[0],$Points[1],$Points[2],$Points[3],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); - $this->drawLine($Points[2],$Points[3],$Points[4],$Points[5],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); - $this->drawLine($Points[0],$Points[1],$Points[4],$Points[5],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); - - /* Draw the second head */ - if ( $TwoHeads ) - { - $Angle = $this->getAngle($X2,$Y2,$X1,$Y1); - - $TailX2 = cos(($Angle-180)*PI/180)*$Size+$X1; - $TailY2 = sin(($Angle-180)*PI/180)*$Size+$Y1; - - $Points = ""; - $Points[] = $X1; $Points[] = $Y1; - $Points[] = cos(($Angle-90)*PI/180)*$Size*$Ratio+$TailX2; $Points[] = sin(($Angle-90)*PI/180)*$Size*$Ratio+$TailY2; - $Points[] = cos(($Angle-270)*PI/180)*$Size*$Ratio+$TailX2; $Points[] = sin(($Angle-270)*PI/180)*$Size*$Ratio+$TailY2; - $Points[] = $X1; $Points[] = $Y1; - - /* Visual correction */ - if ($Angle == 180 || $Angle == 360 ) { $Points[4] = $Points[2]; } - if ($Angle == 90 || $Angle == 270 ) { $Points[5] = $Points[3]; } - - $ArrowColor = $this->allocateColor($this->Picture,$FillR,$FillG,$FillB,$Alpha); - ImageFilledPolygon($this->Picture,$Points,4,$ArrowColor); - - $this->drawLine($Points[0],$Points[1],$Points[2],$Points[3],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); - $this->drawLine($Points[2],$Points[3],$Points[4],$Points[5],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); - $this->drawLine($Points[0],$Points[1],$Points[4],$Points[5],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); - - $this->drawLine($TailX,$TailY,$TailX2,$TailY2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - } - else - $this->drawLine($X1,$Y1,$TailX,$TailY,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - - /* Re-enable shadows */ - $this->Shadow = $RestoreShadow; - } - - /* Draw a label with associated arrow */ - function drawArrowLabel($X1,$Y1,$Text,$Format="") - { - $FillR = isset($Format["FillR"]) ? $Format["FillR"] : 0; - $FillG = isset($Format["FillG"]) ? $Format["FillG"] : 0; - $FillB = isset($Format["FillB"]) ? $Format["FillB"] : 0; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $FillR; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $FillG; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $FillB; - $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; - $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Length = isset($Format["Length"]) ? $Format["Length"] : 50; - $Angle = isset($Format["Angle"]) ? $Format["Angle"] : 315; - $Size = isset($Format["Size"]) ? $Format["Size"] : 10; - $Position = isset($Format["Position"]) ? $Format["Position"] : POSITION_TOP; - $RoundPos = isset($Format["RoundPos"]) ? $Format["RoundPos"] : FALSE; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; - - $Angle = $Angle % 360; - - $X2 = sin(($Angle+180)*PI/180)*$Length+$X1; - $Y2 = cos(($Angle+180)*PI/180)*$Length+$Y1; - - if ( $RoundPos && $Angle > 0 && $Angle < 180 ) { $Y2 = ceil($Y2); } - if ( $RoundPos && $Angle > 180 ) { $Y2 = floor($Y2); } - - $this->drawArrow($X2,$Y2,$X1,$Y1,$Format); - - $Size = imagettfbbox($FontSize,0,$FontName,$Text); - $TxtWidth = max(abs($Size[2]-$Size[0]),abs($Size[0]-$Size[6])); - $TxtHeight = max(abs($Size[1]-$Size[7]),abs($Size[3]-$Size[1])); - - if ( $Angle > 0 && $Angle < 180 ) - { - $this->drawLine($X2,$Y2,$X2-$TxtWidth,$Y2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - if ( $Position == POSITION_TOP ) - $this->drawText($X2,$Y2-2,$Text,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Align"=>TEXT_ALIGN_BOTTOMRIGHT)); - else - $this->drawText($X2,$Y2+4,$Text,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Align"=>TEXT_ALIGN_TOPRIGHT)); - } - else - { - $this->drawLine($X2,$Y2,$X2+$TxtWidth,$Y2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - if ( $Position == POSITION_TOP ) - $this->drawText($X2,$Y2-2,$Text,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); - else - $this->drawText($X2,$Y2+4,$Text,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Align"=>TEXT_ALIGN_TOPLEFT)); - } - } - - /* Draw a progress bar filled with specified % */ - function drawProgress($X,$Y,$Percent,$Format="") - { - if ( $Percent > 100 ) { $Percent = 100; } - if ( $Percent < 0 ) { $Percent = 0; } - - $Width = isset($Format["Width"]) ? $Format["Width"] : 200; - $Height = isset($Format["Height"]) ? $Format["Height"] : 20; - $Orientation = isset($Format["Orientation"]) ? $Format["Orientation"] : ORIENTATION_HORIZONTAL; - $ShowLabel = isset($Format["ShowLabel"]) ? $Format["ShowLabel"] : FALSE; - $LabelPos = isset($Format["LabelPos"]) ? $Format["LabelPos"] : LABEL_POS_INSIDE; - $Margin = isset($Format["Margin"]) ? $Format["Margin"] : 10; - $R = isset($Format["R"]) ? $Format["R"] : 130; - $G = isset($Format["G"]) ? $Format["G"] : 130; - $B = isset($Format["B"]) ? $Format["B"] : 130; - $RFade = isset($Format["RFade"]) ? $Format["RFade"] : -1; - $GFade = isset($Format["GFade"]) ? $Format["GFade"] : -1; - $BFade = isset($Format["BFade"]) ? $Format["BFade"] : -1; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; - $BoxBorderR = isset($Format["BoxBorderR"]) ? $Format["BoxBorderR"] : 0; - $BoxBorderG = isset($Format["BoxBorderG"]) ? $Format["BoxBorderG"] : 0; - $BoxBorderB = isset($Format["BoxBorderB"]) ? $Format["BoxBorderB"] : 0; - $BoxBackR = isset($Format["BoxBackR"]) ? $Format["BoxBackR"] : 255; - $BoxBackG = isset($Format["BoxBackG"]) ? $Format["BoxBackG"] : 255; - $BoxBackB = isset($Format["BoxBackB"]) ? $Format["BoxBackB"] : 255; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : NULL; - $NoAngle = isset($Format["NoAngle"]) ? $Format["NoAngle"] : FALSE; - - if ( $RFade != -1 && $GFade != -1 && $BFade != -1 ) - { - $RFade = (($RFade-$R)/100)*$Percent+$R; - $GFade = (($GFade-$G)/100)*$Percent+$G; - $BFade = (($BFade-$B)/100)*$Percent+$B; - } - - if ( $Surrounding != NULL ) { $BorderR = $R + $Surrounding; $BorderG = $G + $Surrounding; $BorderB = $B + $Surrounding; } - if ( $BoxSurrounding != NULL ) { $BoxBorderR = $BoxBackR + $Surrounding; $BoxBorderG = $BoxBackG + $Surrounding; $BoxBorderB = $BoxBackB + $Surrounding; } - - if ( $Orientation == ORIENTATION_VERTICAL ) - { - $InnerHeight = (($Height-2)/100)*$Percent; - $this->drawFilledRectangle($X,$Y,$X+$Width,$Y-$Height,array("R"=>$BoxBackR,"G"=>$BoxBackG,"B"=>$BoxBackB,"BorderR"=>$BoxBorderR,"BorderG"=>$BoxBorderG,"BorderB"=>$BoxBorderB,"NoAngle"=>$NoAngle)); - - $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; - if ( $RFade != -1 && $GFade != -1 && $BFade != -1 ) - { - $GradientOptions = array("StartR"=>$RFade,"StartG"=>$GFade,"StartB"=>$BFade,"EndR"=>$R,"EndG"=>$G,"EndB"=>$B); - $this->drawGradientArea($X+1,$Y-1,$X+$Width-1,$Y-$InnerHeight,DIRECTION_VERTICAL,$GradientOptions); - - if ( $Surrounding ) - $this->drawRectangle($X+1,$Y-1,$X+$Width-1,$Y-$InnerHeight,array("R"=>255,"G"=>255,"B"=>255,"Alpha"=>$Surrounding)); - } - else - $this->drawFilledRectangle($X+1,$Y-1,$X+$Width-1,$Y-$InnerHeight,array("R"=>$R,"G"=>$G,"B"=>$B,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); - - $this->Shadow = $RestoreShadow; - - if ( $ShowLabel && $LabelPos == LABEL_POS_BOTTOM ) { $this->drawText($X+($Width/2),$Y+$Margin,$Percent."%",array("Align"=>TEXT_ALIGN_TOPMIDDLE)); } - if ( $ShowLabel && $LabelPos == LABEL_POS_TOP ) { $this->drawText($X+($Width/2),$Y-$Height-$Margin,$Percent."%",array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); } - if ( $ShowLabel && $LabelPos == LABEL_POS_INSIDE ) { $this->drawText($X+($Width/2),$Y-$InnerHeight-$Margin,$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLELEFT,"Angle"=>90)); } - if ( $ShowLabel && $LabelPos == LABEL_POS_CENTER ) { $this->drawText($X+($Width/2),$Y-($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"Angle"=>90)); } - } - else - { - if ( $Percent == 100 ) - $InnerWidth = $Width-1; - else - $InnerWidth = (($Width-2)/100)*$Percent; - - $this->drawFilledRectangle($X,$Y,$X+$Width,$Y+$Height,array("R"=>$BoxBackR,"G"=>$BoxBackG,"B"=>$BoxBackB,"BorderR"=>$BoxBorderR,"BorderG"=>$BoxBorderG,"BorderB"=>$BoxBorderB,"NoAngle"=>$NoAngle)); - - $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; - if ( $RFade != -1 && $GFade != -1 && $BFade != -1 ) - { - $GradientOptions = array("StartR"=>$R,"StartG"=>$G,"StartB"=>$B,"EndR"=>$RFade,"EndG"=>$GFade,"EndB"=>$BFade); - $this->drawGradientArea($X+1,$Y+1,$X+$InnerWidth,$Y+$Height-1,DIRECTION_HORIZONTAL,$GradientOptions); - - if ( $Surrounding ) - $this->drawRectangle($X+1,$Y+1,$X+$InnerWidth,$Y+$Height-1,array("R"=>255,"G"=>255,"B"=>255,"Alpha"=>$Surrounding)); - } - else - $this->drawFilledRectangle($X+1,$Y+1,$X+$InnerWidth,$Y+$Height-1,array("R"=>$R,"G"=>$G,"B"=>$B,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); - - $this->Shadow = $RestoreShadow; - - if ( $ShowLabel && $LabelPos == LABEL_POS_LEFT ) { $this->drawText($X-$Margin,$Y+($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLERIGHT)); } - if ( $ShowLabel && $LabelPos == LABEL_POS_RIGHT ) { $this->drawText($X+$Width+$Margin,$Y+($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLELEFT)); } - if ( $ShowLabel && $LabelPos == LABEL_POS_CENTER ) { $this->drawText($X+($Width/2),$Y+($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); } - if ( $ShowLabel && $LabelPos == LABEL_POS_INSIDE ) { $this->drawText($X+$InnerWidth+$Margin,$Y+($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLELEFT)); } - } - } - - /* Get the legend box size */ - function getLegendSize($Format="") - { - $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; - $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; - $BoxSize = isset($Format["BoxSize"]) ? $Format["BoxSize"] : 5; - $Margin = isset($Format["Margin"]) ? $Format["Margin"] : 5; - $Style = isset($Format["Style"]) ? $Format["Style"] : LEGEND_ROUND; - $Mode = isset($Format["Mode"]) ? $Format["Mode"] : LEGEND_VERTICAL; - $BoxWidth = isset($Format["BoxWidth"]) ? $Format["BoxWidth"] : 5; - $BoxHeight = isset($Format["BoxHeight"]) ? $Format["BoxHeight"] : 5; - $IconAreaWidth = isset($Format["IconAreaWidth"]) ? $Format["IconAreaWidth"] : $BoxWidth; - $IconAreaHeight = isset($Format["IconAreaHeight"]) ? $Format["IconAreaHeight"] : $BoxHeight; - $XSpacing = isset($Format["XSpacing"]) ? $Format["XSpacing"] : 5; - - $Data = $this->DataSet->getData(); - - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] && isset($Serie["Picture"])) - { - list($PicWidth,$PicHeight) = $this->getPicInfo($Serie["Picture"]); - if ( $IconAreaWidth < $PicWidth ) { $IconAreaWidth = $PicWidth; } - if ( $IconAreaHeight < $PicHeight ) { $IconAreaHeight = $PicHeight; } - } - } - - $YStep = max($this->FontSize,$IconAreaHeight) + 5; - $XStep = $IconAreaWidth + 5; - $XStep = $XSpacing; - - $X=100; $Y=100; - - $Boundaries = ""; $Boundaries["L"] = $X; $Boundaries["T"] = $Y; $Boundaries["R"] = 0; $Boundaries["B"] = 0; $vY = $Y; $vX = $X; - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - if ( $Mode == LEGEND_VERTICAL ) - { - $BoxArray = $this->getTextBox($vX+$IconAreaWidth+4,$vY+$IconAreaHeight/2,$FontName,$FontSize,0,$Serie["Description"]); - - if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$IconAreaHeight/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$IconAreaHeight/2; } - if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } - if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$IconAreaHeight/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$IconAreaHeight/2; } - - $Lines = preg_split("/\n/",$Serie["Description"]); - $vY = $vY + max($this->FontSize*count($Lines),$IconAreaHeight) + 5; - } - elseif ( $Mode == LEGEND_HORIZONTAL ) - { - $Lines = preg_split("/\n/",$Serie["Description"]); - $Width = ""; - foreach($Lines as $Key => $Value) - { - $BoxArray = $this->getTextBox($vX+$IconAreaWidth+6,$Y+$IconAreaHeight/2+(($this->FontSize+3)*$Key),$FontName,$FontSize,0,$Value); - - if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$IconAreaHeight/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$IconAreaHeight/2; } - if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } - if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$IconAreaHeight/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$IconAreaHeight/2; } - - $Width[] = $BoxArray[1]["X"]; - } - - $vX=max($Width)+$XStep; - } - } - } - $vY=$vY-$YStep; $vX=$vX-$XStep; - - $TopOffset = $Y - $Boundaries["T"]; - if ( $Boundaries["B"]-($vY+$IconAreaHeight) < $TopOffset ) { $Boundaries["B"] = $vY+$IconAreaHeight+$TopOffset; } - - $Width = ($Boundaries["R"]+$Margin) - ($Boundaries["L"]-$Margin); - $Height = ($Boundaries["B"]+$Margin) - ($Boundaries["T"]-$Margin); - - return(array("Width"=>$Width,"Height"=>$Height)); - } - - /* Draw the legend of the active series */ - function drawLegend($X,$Y,$Format="") - { - $Family = isset($Format["Family"]) ? $Format["Family"] : LEGEND_FAMILY_BOX; - $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; - $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; - $FontR = isset($Format["FontR"]) ? $Format["FontR"] : $this->FontColorR; - $FontG = isset($Format["FontG"]) ? $Format["FontG"] : $this->FontColorG; - $FontB = isset($Format["FontB"]) ? $Format["FontB"] : $this->FontColorB; - $BoxWidth = isset($Format["BoxWidth"]) ? $Format["BoxWidth"] : 5; - $BoxHeight = isset($Format["BoxHeight"]) ? $Format["BoxHeight"] : 5; - $IconAreaWidth = isset($Format["IconAreaWidth"]) ? $Format["IconAreaWidth"] : $BoxWidth; - $IconAreaHeight = isset($Format["IconAreaHeight"]) ? $Format["IconAreaHeight"] : $BoxHeight; - $XSpacing = isset($Format["XSpacing"]) ? $Format["XSpacing"] : 5; - $Margin = isset($Format["Margin"]) ? $Format["Margin"] : 5; - $R = isset($Format["R"]) ? $Format["R"] : 200; - $G = isset($Format["G"]) ? $Format["G"] : 200; - $B = isset($Format["B"]) ? $Format["B"] : 200; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 255; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 255; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 255; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - $Style = isset($Format["Style"]) ? $Format["Style"] : LEGEND_ROUND; - $Mode = isset($Format["Mode"]) ? $Format["Mode"] : LEGEND_VERTICAL; - - if ( $Surrounding != NULL ) { $BorderR = $R + $Surrounding; $BorderG = $G + $Surrounding; $BorderB = $B + $Surrounding; } - - $Data = $this->DataSet->getData(); - - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] && isset($Serie["Picture"])) - { - list($PicWidth,$PicHeight) = $this->getPicInfo($Serie["Picture"]); - if ( $IconAreaWidth < $PicWidth ) { $IconAreaWidth = $PicWidth; } - if ( $IconAreaHeight < $PicHeight ) { $IconAreaHeight = $PicHeight; } - } - } - - $YStep = max($this->FontSize,$IconAreaHeight) + 5; - $XStep = $IconAreaWidth + 5; - $XStep = $XSpacing; - - $Boundaries = ""; $Boundaries["L"] = $X; $Boundaries["T"] = $Y; $Boundaries["R"] = 0; $Boundaries["B"] = 0; $vY = $Y; $vX = $X; - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - if ( $Mode == LEGEND_VERTICAL ) - { - $BoxArray = $this->getTextBox($vX+$IconAreaWidth+4,$vY+$IconAreaHeight/2,$FontName,$FontSize,0,$Serie["Description"]); - - if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$IconAreaHeight/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$IconAreaHeight/2; } - if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } - if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$IconAreaHeight/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$IconAreaHeight/2; } - - $Lines = preg_split("/\n/",$Serie["Description"]); - $vY = $vY + max($this->FontSize*count($Lines),$IconAreaHeight) + 5; - } - elseif ( $Mode == LEGEND_HORIZONTAL ) - { - $Lines = preg_split("/\n/",$Serie["Description"]); - $Width = ""; - foreach($Lines as $Key => $Value) - { - $BoxArray = $this->getTextBox($vX+$IconAreaWidth+6,$Y+$IconAreaHeight/2+(($this->FontSize+3)*$Key),$FontName,$FontSize,0,$Value); - - if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$IconAreaHeight/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$IconAreaHeight/2; } - if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } - if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$IconAreaHeight/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$IconAreaHeight/2; } - - $Width[] = $BoxArray[1]["X"]; - } - - $vX=max($Width)+$XStep; - } - } - } - $vY=$vY-$YStep; $vX=$vX-$XStep; - - $TopOffset = $Y - $Boundaries["T"]; - if ( $Boundaries["B"]-($vY+$IconAreaHeight) < $TopOffset ) { $Boundaries["B"] = $vY+$IconAreaHeight+$TopOffset; } - - if ( $Style == LEGEND_ROUND ) - $this->drawRoundedFilledRectangle($Boundaries["L"]-$Margin,$Boundaries["T"]-$Margin,$Boundaries["R"]+$Margin,$Boundaries["B"]+$Margin,$Margin,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); - elseif ( $Style == LEGEND_BOX ) - $this->drawFilledRectangle($Boundaries["L"]-$Margin,$Boundaries["T"]-$Margin,$Boundaries["R"]+$Margin,$Boundaries["B"]+$Margin,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); - - $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; - $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; - - if ( isset($Serie["Picture"]) ) - { - $Picture = $Serie["Picture"]; - list($PicWidth,$PicHeight) = $this->getPicInfo($Picture); - $PicX = $X+$IconAreaWidth/2; $PicY = $Y+$IconAreaHeight/2; - - $this->drawFromPNG($PicX-$PicWidth/2,$PicY-$PicHeight/2,$Picture); - } - else - { - if ( $Family == LEGEND_FAMILY_BOX ) - { - if ( $BoxWidth != $IconAreaWidth ) { $XOffset = floor(($IconAreaWidth-$BoxWidth)/2); } else { $XOffset = 0; } - if ( $BoxHeight != $IconAreaHeight ) { $YOffset = floor(($IconAreaHeight-$BoxHeight)/2); } else { $YOffset = 0; } - - $this->drawFilledRectangle($X+1+$XOffset,$Y+1+$YOffset,$X+$BoxWidth+$XOffset+1,$Y+$BoxHeight+1+$YOffset,array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20)); - $this->drawFilledRectangle($X+$XOffset,$Y+$YOffset,$X+$BoxWidth+$XOffset,$Y+$BoxHeight+$YOffset,array("R"=>$R,"G"=>$G,"B"=>$B,"Surrounding"=>20)); - } - elseif ( $Family == LEGEND_FAMILY_CIRCLE ) - { - $this->drawFilledCircle($X+1+$IconAreaWidth/2,$Y+1+$IconAreaHeight/2,min($IconAreaHeight/2,$IconAreaWidth/2),array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20)); - $this->drawFilledCircle($X+$IconAreaWidth/2,$Y+$IconAreaHeight/2,min($IconAreaHeight/2,$IconAreaWidth/2),array("R"=>$R,"G"=>$G,"B"=>$B,"Surrounding"=>20)); - } - elseif ( $Family == LEGEND_FAMILY_LINE ) - { - $this->drawLine($X+1,$Y+1+$IconAreaHeight/2,$X+1+$IconAreaWidth,$Y+1+$IconAreaHeight/2,array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20,"Ticks"=>$Ticks,"Weight"=>$Weight)); - $this->drawLine($X,$Y+$IconAreaHeight/2,$X+$IconAreaWidth,$Y+$IconAreaHeight/2,array("R"=>$R,"G"=>$G,"B"=>$B,"Ticks"=>$Ticks,"Weight"=>$Weight)); - } - } - - if ( $Mode == LEGEND_VERTICAL ) - { - $Lines = preg_split("/\n/",$Serie["Description"]); - foreach($Lines as $Key => $Value) - $this->drawText($X+$IconAreaWidth+4,$Y+$IconAreaHeight/2+(($this->FontSize+3)*$Key),$Value,array("R"=>$FontR,"G"=>$FontG,"B"=>$FontB,"Align"=>TEXT_ALIGN_MIDDLELEFT,"FontSize"=>$FontSize,"FontName"=>$FontName)); - - $Y=$Y+max($this->FontSize*count($Lines),$IconAreaHeight) + 5; - } - elseif ( $Mode == LEGEND_HORIZONTAL ) - { - $Lines = preg_split("/\n/",$Serie["Description"]); - $Width = ""; - foreach($Lines as $Key => $Value) - { - $BoxArray = $this->drawText($X+$IconAreaWidth+4,$Y+$IconAreaHeight/2+(($this->FontSize+3)*$Key),$Value,array("R"=>$FontR,"G"=>$FontG,"B"=>$FontB,"Align"=>TEXT_ALIGN_MIDDLELEFT,"FontSize"=>$FontSize,"FontName"=>$FontName)); - $Width[] = $BoxArray[1]["X"]; - } - $X=max($Width)+2+$XStep; - } - } - } - - - $this->Shadow = $RestoreShadow; - } - - function drawScale($Format="") - { - $Pos = isset($Format["Pos"]) ? $Format["Pos"] : SCALE_POS_LEFTRIGHT; - $Floating = isset($Format["Floating"]) ? $Format["Floating"] : FALSE; - $Mode = isset($Format["Mode"]) ? $Format["Mode"] : SCALE_MODE_FLOATING; - $RemoveXAxis = isset($Format["RemoveXAxis"]) ? $Format["RemoveXAxis"] : FALSE; - $MinDivHeight = isset($Format["MinDivHeight"]) ? $Format["MinDivHeight"] : 20; - $Factors = isset($Format["Factors"]) ? $Format["Factors"] : array(1,2,5); - $ManualScale = isset($Format["ManualScale"]) ? $Format["ManualScale"] : array("0"=>array("Min"=>-100,"Max"=>100)); - $XMargin = isset($Format["XMargin"]) ? $Format["XMargin"] : AUTO; - $YMargin = isset($Format["YMargin"]) ? $Format["YMargin"] : 0; - $ScaleSpacing = isset($Format["ScaleSpacing"]) ? $Format["ScaleSpacing"] : 15; - $InnerTickWidth = isset($Format["InnerTickWidth"]) ? $Format["InnerTickWidth"] : 2; - $OuterTickWidth = isset($Format["OuterTickWidth"]) ? $Format["OuterTickWidth"] : 2; - $DrawXLines = isset($Format["DrawXLines"]) ? $Format["DrawXLines"] : TRUE; - $DrawYLines = isset($Format["DrawYLines"]) ? $Format["DrawYLines"] : ALL; - $GridTicks = isset($Format["GridTicks"]) ? $Format["GridTicks"] : 4; - $GridR = isset($Format["GridR"]) ? $Format["GridR"] : 255; - $GridG = isset($Format["GridG"]) ? $Format["GridG"] : 255; - $GridB = isset($Format["GridB"]) ? $Format["GridB"] : 255; - $GridAlpha = isset($Format["GridAlpha"]) ? $Format["GridAlpha"] : 40; - $AxisRo = isset($Format["AxisR"]) ? $Format["AxisR"] : 0; - $AxisGo = isset($Format["AxisG"]) ? $Format["AxisG"] : 0; - $AxisBo = isset($Format["AxisB"]) ? $Format["AxisB"] : 0; - $AxisAlpha = isset($Format["AxisAlpha"]) ? $Format["AxisAlpha"] : 100; - $TickRo = isset($Format["TickR"]) ? $Format["TickR"] : 0; - $TickGo = isset($Format["TickG"]) ? $Format["TickG"] : 0; - $TickBo = isset($Format["TickB"]) ? $Format["TickB"] : 0; - $TickAlpha = isset($Format["TickAlpha"]) ? $Format["TickAlpha"] : 100; - $DrawSubTicks = isset($Format["DrawSubTicks"]) ? $Format["DrawSubTicks"] : FALSE; - $InnerSubTickWidth = isset($Format["InnerSubTickWidth"]) ? $Format["InnerSubTickWidth"] : 0; - $OuterSubTickWidth = isset($Format["OuterSubTickWidth"]) ? $Format["OuterSubTickWidth"] : 2; - $SubTickR = isset($Format["SubTickR"]) ? $Format["SubTickR"] : 255; - $SubTickG = isset($Format["SubTickG"]) ? $Format["SubTickG"] : 0; - $SubTickB = isset($Format["SubTickB"]) ? $Format["SubTickB"] : 0; - $SubTickAlpha = isset($Format["SubTickAlpha"]) ? $Format["SubTickAlpha"] : 100; - $AutoAxisLabels = isset($Format["AutoAxisLabels"]) ? $Format["AutoAxisLabels"] : TRUE; - $XReleasePercent = isset($Format["XReleasePercent"]) ? $Format["XReleasePercent"] : 1; - $DrawArrows = isset($Format["DrawArrows"]) ? $Format["DrawArrows"] : FALSE; - $ArrowSize = isset($Format["ArrowSize"]) ? $Format["ArrowSize"] : 8; - $CycleBackground = isset($Format["CycleBackground"]) ? $Format["CycleBackground"] : FALSE; - $BackgroundR1 = isset($Format["BackgroundR1"]) ? $Format["BackgroundR1"] : 255; - $BackgroundG1 = isset($Format["BackgroundG1"]) ? $Format["BackgroundG1"] : 255; - $BackgroundB1 = isset($Format["BackgroundB1"]) ? $Format["BackgroundB1"] : 255; - $BackgroundAlpha1 = isset($Format["BackgroundAlpha1"]) ? $Format["BackgroundAlpha1"] : 20; - $BackgroundR2 = isset($Format["BackgroundR2"]) ? $Format["BackgroundR2"] : 230; - $BackgroundG2 = isset($Format["BackgroundG2"]) ? $Format["BackgroundG2"] : 230; - $BackgroundB2 = isset($Format["BackgroundB2"]) ? $Format["BackgroundB2"] : 230; - $BackgroundAlpha2 = isset($Format["BackgroundAlpha2"]) ? $Format["BackgroundAlpha2"] : 20; - $LabelingMethod = isset($Format["LabelingMethod"]) ? $Format["LabelingMethod"] : LABELING_ALL; - $LabelSkip = isset($Format["LabelSkip"]) ? $Format["LabelSkip"] : 0; - $LabelRotation = isset($Format["LabelRotation"]) ? $Format["LabelRotation"] : 0; - $SkippedAxisTicks = isset($Format["SkippedAxisTicks"]) ? $Format["SkippedAxisTicks"] : $GridTicks+2; - $SkippedAxisR = isset($Format["SkippedAxisR"]) ? $Format["SkippedAxisR"] : $GridR; - $SkippedAxisG = isset($Format["SkippedAxisG"]) ? $Format["SkippedAxisG"] : $GridG; - $SkippedAxisB = isset($Format["SkippedAxisB"]) ? $Format["SkippedAxisB"] : $GridB; - $SkippedAxisAlpha = isset($Format["SkippedAxisAlpha"]) ? $Format["SkippedAxisAlpha"] : $GridAlpha-30; - $SkippedTickR = isset($Format["SkippedTickR"]) ? $Format["SkippedTickR"] : $TickRo; - $SkippedTickG = isset($Format["SkippedTickG"]) ? $Format["SkippedTickG"] : $TickGo; - $SkippedTickB = isset($Format["SkippedTicksB"]) ? $Format["SkippedTickB"] : $TickBo; - $SkippedTickAlpha = isset($Format["SkippedTickAlpha"]) ? $Format["SkippedTickAlpha"] : $TickAlpha-80; - $SkippedInnerTickWidth = isset($Format["SkippedInnerTickWidth"]) ? $Format["SkippedInnerTickWidth"] : 0; - $SkippedOuterTickWidth = isset($Format["SkippedOuterTickWidth"]) ? $Format["SkippedOuterTickWidth"] : 2; - - /* Floating scale require X & Y margins to be set manually */ - if ( $Floating && ( $XMargin == AUTO || $YMargin == 0 ) ) { $Floating = FALSE; } - - /* Skip a NOTICE event in case of an empty array */ - if ( $DrawYLines == NONE || $DrawYLines == FALSE ) { $DrawYLines = array("zarma"=>"31"); } - - /* Define the color for the skipped elements */ - $SkippedAxisColor = array("R"=>$SkippedAxisR,"G"=>$SkippedAxisG,"B"=>$SkippedAxisB,"Alpha"=>$SkippedAxisAlpha,"Ticks"=>$SkippedAxisTicks); - $SkippedTickColor = array("R"=>$SkippedTickR,"G"=>$SkippedTickG,"B"=>$SkippedTickB,"Alpha"=>$SkippedTickAlpha); - - $Data = $this->DataSet->getData(); - if ( isset($Data["Abscissa"]) ) { $Abscissa = $Data["Abscissa"]; } else { $Abscissa = NULL; } - - /* Unset the abscissa axis, needed if we display multiple charts on the same picture */ - if ( $Abscissa != NULL ) - { - foreach($Data["Axis"] as $AxisID => $Parameters) - { if ($Parameters["Identity"] == AXIS_X) { unset($Data["Axis"][$AxisID]); } } - } - - /* Build the scale settings */ - $GotXAxis = FALSE; - foreach($Data["Axis"] as $AxisID => $AxisParameter) - { - if ( $AxisParameter["Identity"] == AXIS_X ) { $GotXAxis = TRUE; } - - if ( $Pos == SCALE_POS_LEFTRIGHT && $AxisParameter["Identity"] == AXIS_Y) - { $Height = $this->GraphAreaY2-$this->GraphAreaY1 - $YMargin*2; } - elseif ( $Pos == SCALE_POS_LEFTRIGHT && $AxisParameter["Identity"] == AXIS_X) - { $Height = $this->GraphAreaX2-$this->GraphAreaX1; } - elseif ( $Pos == SCALE_POS_TOPBOTTOM && $AxisParameter["Identity"] == AXIS_Y) - { $Height = $this->GraphAreaX2-$this->GraphAreaX1 - $YMargin*2;; } - else - { $Height = $this->GraphAreaY2-$this->GraphAreaY1; } - - $AxisMin = ABSOLUTE_MAX; $AxisMax = OUT_OF_SIGHT; - if ( $Mode == SCALE_MODE_FLOATING || $Mode == SCALE_MODE_START0 ) - { - foreach($Data["Series"] as $SerieID => $SerieParameter) - { - if ( $SerieParameter["Axis"] == $AxisID && $Data["Series"][$SerieID]["isDrawable"] && $Data["Abscissa"] != $SerieID) - { - $AxisMax = max($AxisMax,$Data["Series"][$SerieID]["Max"]); - $AxisMin = min($AxisMin,$Data["Series"][$SerieID]["Min"]); - } - } - $AutoMargin = (($AxisMax-$AxisMin)/100)*$XReleasePercent; - - $Data["Axis"][$AxisID]["Min"] = $AxisMin-$AutoMargin; $Data["Axis"][$AxisID]["Max"] = $AxisMax+$AutoMargin; - if ( $Mode == SCALE_MODE_START0 ) { $Data["Axis"][$AxisID]["Min"] = 0; } - } - elseif ( $Mode == SCALE_MODE_MANUAL ) - { - if ( isset($ManualScale[$AxisID]["Min"]) && isset($ManualScale[$AxisID]["Max"]) ) - { - $Data["Axis"][$AxisID]["Min"] = $ManualScale[$AxisID]["Min"]; - $Data["Axis"][$AxisID]["Max"] = $ManualScale[$AxisID]["Max"]; - } - else - { echo "Manual scale boundaries not set."; exit(); } - } - elseif ( $Mode == SCALE_MODE_ADDALL || $Mode == SCALE_MODE_ADDALL_START0 ) - { - $Series = ""; - foreach($Data["Series"] as $SerieID => $SerieParameter) - { if ( $SerieParameter["Axis"] == $AxisID && $SerieParameter["isDrawable"] && $Data["Abscissa"] != $SerieID ) { $Series[$SerieID] = count($Data["Series"][$SerieID]["Data"]); } } - - for ($ID=0;$ID<=max($Series)-1;$ID++) - { - $PointMin = 0; $PointMax = 0; - foreach($Series as $SerieID => $ValuesCount ) - { - if (isset($Data["Series"][$SerieID]["Data"][$ID]) && $Data["Series"][$SerieID]["Data"][$ID] != NULL ) - { - $Value = $Data["Series"][$SerieID]["Data"][$ID]; - if ( $Value > 0 ) { $PointMax = $PointMax + $Value; } else { $PointMin = $PointMin + $Value; } - } - } - $AxisMax = max($AxisMax,$PointMax); - $AxisMin = min($AxisMin,$PointMin); - } - $AutoMargin = (($AxisMax-$AxisMin)/100)*$XReleasePercent; - $Data["Axis"][$AxisID]["Min"] = $AxisMin-$AutoMargin; $Data["Axis"][$AxisID]["Max"] = $AxisMax+$AutoMargin; - } - $MaxDivs = floor($Height/$MinDivHeight); - - if ( $Mode == SCALE_MODE_ADDALL_START0 ) { $Data["Axis"][$AxisID]["Min"] = 0; } - - $Scale = $this->computeScale($Data["Axis"][$AxisID]["Min"],$Data["Axis"][$AxisID]["Max"],$MaxDivs,$Factors,$AxisID); - - $Data["Axis"][$AxisID]["Margin"] = $AxisParameter["Identity"] == AXIS_X ? $XMargin : $YMargin; - $Data["Axis"][$AxisID]["ScaleMin"] = $Scale["XMin"]; - $Data["Axis"][$AxisID]["ScaleMax"] = $Scale["XMax"]; - $Data["Axis"][$AxisID]["Rows"] = $Scale["Rows"]; - $Data["Axis"][$AxisID]["RowHeight"] = $Scale["RowHeight"]; - - if ( isset($Scale["Format"]) ) { $Data["Axis"][$AxisID]["Format"] = $Scale["Format"]; } - - if ( !isset($Data["Axis"][$AxisID]["Display"]) ) { $Data["Axis"][$AxisID]["Display"] = NULL; } - if ( !isset($Data["Axis"][$AxisID]["Format"]) ) { $Data["Axis"][$AxisID]["Format"] = NULL; } - if ( !isset($Data["Axis"][$AxisID]["Unit"]) ) { $Data["Axis"][$AxisID]["Unit"] = NULL; } - } - - /* Still no X axis */ - if ( $GotXAxis == FALSE ) - { - if ( $Abscissa != NULL ) - { - $Points = count($Data["Series"][$Abscissa]["Data"]); - if ( $AutoAxisLabels ) - $AxisName = isset($Data["Series"][$Abscissa]["Description"]) ? $Data["Series"][$Abscissa]["Description"] : NULL; - else - $AxisName = NULL; - } - else - { - $Points = 0; - $AxisName = isset($Data["XAxisName"]) ? $Data["XAxisName"] : NULL; - foreach($Data["Series"] as $SerieID => $SerieParameter) - { if ( $SerieParameter["isDrawable"] ) { $Points = max($Points,count($SerieParameter["Data"])); } } - } - - $AxisID = count($Data["Axis"]); - $Data["Axis"][$AxisID]["Identity"] = AXIS_X; - if ( $Pos == SCALE_POS_LEFTRIGHT ) { $Data["Axis"][$AxisID]["Position"] = AXIS_POSITION_BOTTOM; } else { $Data["Axis"][$AxisID]["Position"] = AXIS_POSITION_LEFT; } - if ( isset($Data["AbscissaName"]) ) { $Data["Axis"][$AxisID]["Name"] = $Data["AbscissaName"]; } - if ( $XMargin == AUTO ) - { - if ( $Pos == SCALE_POS_LEFTRIGHT ) - { $Height = $this->GraphAreaX2-$this->GraphAreaX1; } - else - { $Height = $this->GraphAreaY2-$this->GraphAreaY1; } - - if ( $Points == 1 ) - $Data["Axis"][$AxisID]["Margin"] = $Height / 2; - else - $Data["Axis"][$AxisID]["Margin"] = ($Height/$Points) / 2; - } - else - { $Data["Axis"][$AxisID]["Margin"] = $XMargin; } - $Data["Axis"][$AxisID]["Rows"] = $Points-1; - if ( !isset($Data["Axis"][$AxisID]["Display"]) ) { $Data["Axis"][$AxisID]["Display"] = NULL; } - if ( !isset($Data["Axis"][$AxisID]["Format"]) ) { $Data["Axis"][$AxisID]["Format"] = NULL; } - if ( !isset($Data["Axis"][$AxisID]["Unit"]) ) { $Data["Axis"][$AxisID]["Unit"] = NULL; } - } - - /* Do we need to reverse the abscissa position? */ - if ( $Pos != SCALE_POS_LEFTRIGHT ) - { - if ( $Data["AbsicssaPosition"] == AXIS_POSITION_BOTTOM ) - { $Data["AbsicssaPosition"] = AXIS_POSITION_LEFT; } - else - { $Data["AbsicssaPosition"] = AXIS_POSITION_RIGHT; } - } - $Data["Axis"][$AxisID]["Position"] = $Data["AbsicssaPosition"]; - - $this->DataSet->saveOrientation($Pos); - $this->DataSet->saveAxisConfig($Data["Axis"]); - $this->DataSet->saveYMargin($YMargin); - - $FontColorRo = $this->FontColorR; $FontColorGo = $this->FontColorG; $FontColorBo = $this->FontColorB; - - $AxisPos["L"] = $this->GraphAreaX1; $AxisPos["R"] = $this->GraphAreaX2; $AxisPos["T"] = $this->GraphAreaY1; $AxisPos["B"] = $this->GraphAreaY2; - foreach($Data["Axis"] as $AxisID => $Parameters) - { - if ( isset($Parameters["Color"]) ) - { - $AxisR = $Parameters["Color"]["R"]; $AxisG = $Parameters["Color"]["G"]; $AxisB = $Parameters["Color"]["B"]; - $TickR = $Parameters["Color"]["R"]; $TickG = $Parameters["Color"]["G"]; $TickB = $Parameters["Color"]["B"]; - $this->setFontProperties(array("R"=>$Parameters["Color"]["R"],"G"=>$Parameters["Color"]["G"],"B"=>$Parameters["Color"]["B"])); - } - else - { - $AxisR = $AxisRo; $AxisG = $AxisGo; $AxisB = $AxisBo; - $TickR = $TickRo; $TickG = $TickGo; $TickB = $TickBo; - $this->setFontProperties(array("R"=>$FontColorRo,"G"=>$FontColorGo,"B"=>$FontColorBo)); - } - - $LastValue = "w00t"; $ID = 1; - if ( $Parameters["Identity"] == AXIS_X ) - { - if ( $Pos == SCALE_POS_LEFTRIGHT ) - { - if ( $Parameters["Position"] == AXIS_POSITION_BOTTOM ) - { - if ( $LabelRotation == 0 ) { $LabelAlign = TEXT_ALIGN_TOPMIDDLE; $YLabelOffset = 2; } - if ( $LabelRotation > 0 && $LabelRotation < 190 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $YLabelOffset = 5; } - if ( $LabelRotation == 180 ) { $LabelAlign = TEXT_ALIGN_BOTTOMMIDDLE; $YLabelOffset = 5; } - if ( $LabelRotation > 180 && $LabelRotation < 360 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $YLabelOffset = 2; } - - if ( !$RemoveXAxis ) - { - if ( $Floating ) - { $FloatingOffset = $YMargin; $this->drawLine($this->GraphAreaX1+$Parameters["Margin"],$AxisPos["B"],$this->GraphAreaX2-$Parameters["Margin"],$AxisPos["B"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - else - { $FloatingOffset = 0; $this->drawLine($this->GraphAreaX1,$AxisPos["B"],$this->GraphAreaX2,$AxisPos["B"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - - if ( $DrawArrows ) { $this->drawArrow($this->GraphAreaX2-$Parameters["Margin"],$AxisPos["B"],$this->GraphAreaX2+($ArrowSize*2),$AxisPos["B"],array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } - } - - $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Parameters["Margin"]*2; - - if ($Parameters["Rows"] == 0 ) { $Step = $Width; } else { $Step = $Width / ($Parameters["Rows"]); } - - $MaxBottom = $AxisPos["B"]; - for($i=0;$i<=$Parameters["Rows"];$i++) - { - $XPos = $this->GraphAreaX1 + $Parameters["Margin"] + $Step*$i; - $YPos = $AxisPos["B"]; - - if ( $Abscissa != NULL ) - { if ( isset($Data["Series"][$Abscissa]["Data"][$i]) ) { $Value = $this->scaleFormat($Data["Series"][$Abscissa]["Data"][$i],$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); } else { $Value = ""; } } - else - { - if ( isset($Parameters["ScaleMin"]) && isset ($Parameters["RowHeight"]) ) - $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); - else - $Value = $i; - } - - $ID++; $Skipped = TRUE; - if ( $this->isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) && !$RemoveXAxis) - { - $Bounds = $this->drawText($XPos,$YPos+$OuterTickWidth+$YLabelOffset,$Value,array("Angle"=>$LabelRotation,"Align"=>$LabelAlign)); - $TxtBottom = $YPos+$OuterTickWidth+2+($Bounds[0]["Y"]-$Bounds[2]["Y"]); - $MaxBottom = max($MaxBottom,$TxtBottom); - $LastValue = $Value; - $Skipped = FALSE; - } - - if ( $RemoveXAxis ) { $Skipped = FALSE; } - - if ( $Skipped ) - { - if ( $DrawXLines ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,$SkippedAxisColor); } - if ( ($SkippedInnerTickWidth !=0 || $SkippedOuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos,$YPos-$SkippedInnerTickWidth,$XPos,$YPos+$SkippedOuterTickWidth,$SkippedTickColor); } - } - else - { - if ( $DrawXLines && ($XPos != $this->GraphAreaX1 && $XPos != $this->GraphAreaX2) ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } - if ( ($InnerTickWidth !=0 || $OuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos,$YPos-$InnerTickWidth,$XPos,$YPos+$OuterTickWidth,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); } - } - } - - if ( isset($Parameters["Name"]) && !$RemoveXAxis) - { - $YPos = $MaxBottom+2; - $XPos = $this->GraphAreaX1+($this->GraphAreaX2-$this->GraphAreaX1)/2; - $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_TOPMIDDLE)); - $MaxBottom = $Bounds[0]["Y"]; - - $this->DataSet->Data["GraphArea"]["Y2"] = $MaxBottom + $this->FontSize; - } - - $AxisPos["B"] = $MaxBottom + $ScaleSpacing; - } - elseif ( $Parameters["Position"] == AXIS_POSITION_TOP ) - { - if ( $LabelRotation == 0 ) { $LabelAlign = TEXT_ALIGN_BOTTOMMIDDLE; $YLabelOffset = 2; } - if ( $LabelRotation > 0 && $LabelRotation < 190 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $YLabelOffset = 2; } - if ( $LabelRotation == 180 ) { $LabelAlign = TEXT_ALIGN_TOPMIDDLE; $YLabelOffset = 5; } - if ( $LabelRotation > 180 && $LabelRotation < 360 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $YLabelOffset = 5; } - - if ( !$RemoveXAxis ) - { - if ( $Floating ) - { $FloatingOffset = $YMargin; $this->drawLine($this->GraphAreaX1+$Parameters["Margin"],$AxisPos["T"],$this->GraphAreaX2-$Parameters["Margin"],$AxisPos["T"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - else - { $FloatingOffset = 0; $this->drawLine($this->GraphAreaX1,$AxisPos["T"],$this->GraphAreaX2,$AxisPos["T"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - - if ( $DrawArrows ) { $this->drawArrow($this->GraphAreaX2-$Parameters["Margin"],$AxisPos["T"],$this->GraphAreaX2+($ArrowSize*2),$AxisPos["T"],array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } - } - - $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Parameters["Margin"]*2; - - if ($Parameters["Rows"] == 0 ) { $Step = $Width; } else { $Step = $Width / $Parameters["Rows"]; } - - $MinTop = $AxisPos["T"]; - for($i=0;$i<=$Parameters["Rows"];$i++) - { - $XPos = $this->GraphAreaX1 + $Parameters["Margin"] + $Step*$i; - $YPos = $AxisPos["T"]; - - if ( $Abscissa != NULL ) - { if ( isset($Data["Series"][$Abscissa]["Data"][$i]) ) { $Value = $this->scaleFormat($Data["Series"][$Abscissa]["Data"][$i],$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); } else { $Value = ""; } } - else - { - if ( isset($Parameters["ScaleMin"]) && isset ($Parameters["RowHeight"]) ) - $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); - else - $Value = $i; - } - - $ID++; $Skipped = TRUE; - if ( $this->isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) && !$RemoveXAxis) - { - $Bounds = $this->drawText($XPos,$YPos-$OuterTickWidth-$YLabelOffset,$Value,array("Angle"=>$LabelRotation,"Align"=>$LabelAlign)); - $TxtBox = $YPos-$OuterTickWidth-2-($Bounds[0]["Y"]-$Bounds[2]["Y"]); - $MinTop = min($MinTop,$TxtBox); - $LastValue = $Value; - $Skipped = FALSE; - } - - if ( $RemoveXAxis ) { $Skipped = FALSE; } - - if ( $Skipped ) - { - if ( $DrawXLines ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,$SkippedAxisColor); } - if ( ($SkippedInnerTickWidth !=0 || $SkippedOuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos,$YPos+$SkippedInnerTickWidth,$XPos,$YPos-$SkippedOuterTickWidth,$SkippedTickColor); } - } - else - { - if ( $DrawXLines ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } - if ( ($InnerTickWidth !=0 || $OuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos,$YPos+$InnerTickWidth,$XPos,$YPos-$OuterTickWidth,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); } - } - - } - - if ( isset($Parameters["Name"]) && !$RemoveXAxis ) - { - $YPos = $MinTop-2; - $XPos = $this->GraphAreaX1+($this->GraphAreaX2-$this->GraphAreaX1)/2; - $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); - $MinTop = $Bounds[2]["Y"]; - - $this->DataSet->Data["GraphArea"]["Y1"] = $MinTop; - } - - $AxisPos["T"] = $MinTop - $ScaleSpacing; - } - } - elseif ( $Pos == SCALE_POS_TOPBOTTOM ) - { - if ( $Parameters["Position"] == AXIS_POSITION_LEFT ) - { - if ( $LabelRotation == 0 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $XLabelOffset = -2; } - if ( $LabelRotation > 0 && $LabelRotation < 190 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $XLabelOffset = -6; } - if ( $LabelRotation == 180 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $XLabelOffset = -2; } - if ( $LabelRotation > 180 && $LabelRotation < 360 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $XLabelOffset = -5; } - - if ( !$RemoveXAxis ) - { - if ( $Floating ) - { $FloatingOffset = $YMargin; $this->drawLine($AxisPos["L"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["L"],$this->GraphAreaY2-$Parameters["Margin"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - else - { $FloatingOffset = 0; $this->drawLine($AxisPos["L"],$this->GraphAreaY1,$AxisPos["L"],$this->GraphAreaY2,array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - - if ( $DrawArrows ) { $this->drawArrow($AxisPos["L"],$this->GraphAreaY2-$Parameters["Margin"],$AxisPos["L"],$this->GraphAreaY2+($ArrowSize*2),array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } - } - - $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Parameters["Margin"]*2; - - if ($Parameters["Rows"] == 0 ) { $Step = $Height; } else { $Step = $Height / $Parameters["Rows"]; } - - $MinLeft = $AxisPos["L"]; - for($i=0;$i<=$Parameters["Rows"];$i++) - { - $YPos = $this->GraphAreaY1 + $Parameters["Margin"] + $Step*$i; - $XPos = $AxisPos["L"]; - - if ( $Abscissa != NULL ) - { if ( isset($Data["Series"][$Abscissa]["Data"][$i]) ) { $Value = $this->scaleFormat($Data["Series"][$Abscissa]["Data"][$i],$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); } else { $Value = ""; } } - else - { - if ( isset($Parameters["ScaleMin"]) && isset ($Parameters["RowHeight"]) ) - $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); - else - $Value = $i; - } - - $ID++; $Skipped = TRUE; - if ( $this->isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) && !$RemoveXAxis) - { - $Bounds = $this->drawText($XPos-$OuterTickWidth+$XLabelOffset,$YPos,$Value,array("Angle"=>$LabelRotation,"Align"=>$LabelAlign)); - $TxtBox = $XPos-$OuterTickWidth-2-($Bounds[1]["X"]-$Bounds[0]["X"]); - $MinLeft = min($MinLeft,$TxtBox); - $LastValue = $Value; - $Skipped = FALSE; - } - - if ( $RemoveXAxis ) { $Skipped = FALSE; } - - if ( $Skipped ) - { - if ( $DrawXLines ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,$SkippedAxisColor); } - if ( ($SkippedInnerTickWidth !=0 || $SkippedOuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos-$SkippedOuterTickWidth,$YPos,$XPos+$SkippedInnerTickWidth,$YPos,$SkippedTickColor); } - } - else - { - if ( $DrawXLines && ($YPos != $this->GraphAreaY1 && $YPos != $this->GraphAreaY2) ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } - if ( ($InnerTickWidth !=0 || $OuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos-$OuterTickWidth,$YPos,$XPos+$InnerTickWidth,$YPos,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); } - } - - } - if ( isset($Parameters["Name"]) && !$RemoveXAxis ) - { - $XPos = $MinLeft-2; - $YPos = $this->GraphAreaY1+($this->GraphAreaY2-$this->GraphAreaY1)/2; - $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE,"Angle"=>90)); - $MinLeft = $Bounds[0]["X"]; - - $this->DataSet->Data["GraphArea"]["X1"] = $MinLeft; - } - - $AxisPos["L"] = $MinLeft - $ScaleSpacing; - } - elseif ( $Parameters["Position"] == AXIS_POSITION_RIGHT ) - { - if ( $LabelRotation == 0 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $XLabelOffset = 2; } - if ( $LabelRotation > 0 && $LabelRotation < 190 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $XLabelOffset = 6; } - if ( $LabelRotation == 180 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $XLabelOffset = 5; } - if ( $LabelRotation > 180 && $LabelRotation < 360 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $XLabelOffset = 7; } - - if ( !$RemoveXAxis ) - { - if ( $Floating ) - { $FloatingOffset = $YMargin; $this->drawLine($AxisPos["R"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["R"],$this->GraphAreaY2-$Parameters["Margin"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - else - { $FloatingOffset = 0; $this->drawLine($AxisPos["R"],$this->GraphAreaY1,$AxisPos["R"],$this->GraphAreaY2,array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - - if ( $DrawArrows ) { $this->drawArrow($AxisPos["R"],$this->GraphAreaY2-$Parameters["Margin"],$AxisPos["R"],$this->GraphAreaY2+($ArrowSize*2),array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } - } - - $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Parameters["Margin"]*2; - - if ($Parameters["Rows"] == 0 ) { $Step = $Height; } else { $Step = $Height / $Parameters["Rows"]; } - - $MaxRight = $AxisPos["R"]; - for($i=0;$i<=$Parameters["Rows"];$i++) - { - $YPos = $this->GraphAreaY1 + $Parameters["Margin"] + $Step*$i; - $XPos = $AxisPos["R"]; - - if ( $Abscissa != NULL ) - { if ( isset($Data["Series"][$Abscissa]["Data"][$i]) ) { $Value = $this->scaleFormat($Data["Series"][$Abscissa]["Data"][$i],$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); } else { $Value = ""; } } - else - { - if ( isset($Parameters["ScaleMin"]) && isset ($Parameters["RowHeight"]) ) - $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); - else - $Value = $i; - } - - $ID++; $Skipped = TRUE; - if ( $this->isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) && !$RemoveXAxis) - { - $Bounds = $this->drawText($XPos+$OuterTickWidth+$XLabelOffset,$YPos,$Value,array("Angle"=>$LabelRotation,"Align"=>$LabelAlign)); - $TxtBox = $XPos+$OuterTickWidth+2+($Bounds[1]["X"]-$Bounds[0]["X"]); - $MaxRight = max($MaxRight,$TxtBox); - $LastValue = $Value; - $Skipped = FALSE; - } - - if ( $RemoveXAxis ) { $Skipped = FALSE; } - - if ( $Skipped ) - { - if ( $DrawXLines ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,$SkippedAxisColor); } - if ( ($SkippedInnerTickWidth != 0 || $SkippedOuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos+$SkippedOuterTickWidth,$YPos,$XPos-$SkippedInnerTickWidth,$YPos,$SkippedTickColor); } - } - else - { - if ( $DrawXLines ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } - if ( ($InnerTickWidth != 0 || $OuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos+$OuterTickWidth,$YPos,$XPos-$InnerTickWidth,$YPos,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); } - } - - } - - if ( isset($Parameters["Name"]) && !$RemoveXAxis) - { - $XPos = $MaxRight+4; - $YPos = $this->GraphAreaY1+($this->GraphAreaY2-$this->GraphAreaY1)/2; - $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE,"Angle"=>270)); - $MaxRight = $Bounds[1]["X"]; - - $this->DataSet->Data["GraphArea"]["X2"] = $MaxRight + $this->FontSize; - } - - $AxisPos["R"] = $MaxRight + $ScaleSpacing; - } - } - } - - - - if ( $Parameters["Identity"] == AXIS_Y ) - { - if ( $Pos == SCALE_POS_LEFTRIGHT ) - { - if ( $Parameters["Position"] == AXIS_POSITION_LEFT ) - { - - if ( $Floating ) - { $FloatingOffset = $XMargin; $this->drawLine($AxisPos["L"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["L"],$this->GraphAreaY2-$Parameters["Margin"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - else - { $FloatingOffset = 0; $this->drawLine($AxisPos["L"],$this->GraphAreaY1,$AxisPos["L"],$this->GraphAreaY2,array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - - if ( $DrawArrows ) { $this->drawArrow($AxisPos["L"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["L"],$this->GraphAreaY1-($ArrowSize*2),array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } - - $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Parameters["Margin"]*2; - $Step = $Height / $Parameters["Rows"]; $SubTicksSize = $Step /2; $MinLeft = $AxisPos["L"]; - $LastY = NULL; - for($i=0;$i<=$Parameters["Rows"];$i++) - { - $YPos = $this->GraphAreaY2 - $Parameters["Margin"] - $Step*$i; - $XPos = $AxisPos["L"]; - $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Parameters["Display"],$Parameters["Format"],$Parameters["Unit"]); - - if ( $i%2 == 1 ) { $BGColor = array("R"=>$BackgroundR1,"G"=>$BackgroundG1,"B"=>$BackgroundB1,"Alpha"=>$BackgroundAlpha1); } else { $BGColor = array("R"=>$BackgroundR2,"G"=>$BackgroundG2,"B"=>$BackgroundB2,"Alpha"=>$BackgroundAlpha2); } - if ( $LastY != NULL && $CycleBackground && ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) )) { $this->drawFilledRectangle($this->GraphAreaX1+$FloatingOffset,$LastY,$this->GraphAreaX2-$FloatingOffset,$YPos,$BGColor); } - - if ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } - - if ( $DrawSubTicks && $i != $Parameters["Rows"] ) - $this->drawLine($XPos-$OuterSubTickWidth,$YPos-$SubTicksSize,$XPos+$InnerSubTickWidth,$YPos-$SubTicksSize,array("R"=>$SubTickR,"G"=>$SubTickG,"B"=>$SubTickB,"Alpha"=>$SubTickAlpha)); - - $this->drawLine($XPos-$OuterTickWidth,$YPos,$XPos+$InnerTickWidth,$YPos,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); - $Bounds = $this->drawText($XPos-$OuterTickWidth-2,$YPos,$Value,array("Align"=>TEXT_ALIGN_MIDDLERIGHT)); - $TxtLeft = $XPos-$OuterTickWidth-2-($Bounds[1]["X"]-$Bounds[0]["X"]); - $MinLeft = min($MinLeft,$TxtLeft); - - $LastY = $YPos; - } - - if ( isset($Parameters["Name"]) ) - { - $XPos = $MinLeft-2; - $YPos = $this->GraphAreaY1+($this->GraphAreaY2-$this->GraphAreaY1)/2; - $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE,"Angle"=>90)); - $MinLeft = $Bounds[2]["X"]; - - $this->DataSet->Data["GraphArea"]["X1"] = $MinLeft; - } - - $AxisPos["L"] = $MinLeft - $ScaleSpacing; - } - elseif ( $Parameters["Position"] == AXIS_POSITION_RIGHT ) - { - if ( $Floating ) - { $FloatingOffset = $XMargin; $this->drawLine($AxisPos["R"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["R"],$this->GraphAreaY2-$Parameters["Margin"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - else - { $FloatingOffset = 0; $this->drawLine($AxisPos["R"],$this->GraphAreaY1,$AxisPos["R"],$this->GraphAreaY2,array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - - if ( $DrawArrows ) { $this->drawArrow($AxisPos["R"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["R"],$this->GraphAreaY1-($ArrowSize*2),array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } - - $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Parameters["Margin"]*2; - $Step = $Height / $Parameters["Rows"]; $SubTicksSize = $Step /2; $MaxLeft = $AxisPos["R"]; - $LastY = NULL; - for($i=0;$i<=$Parameters["Rows"];$i++) - { - $YPos = $this->GraphAreaY2 - $Parameters["Margin"] - $Step*$i; - $XPos = $AxisPos["R"]; - $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Parameters["Display"],$Parameters["Format"],$Parameters["Unit"]); - - if ( $i%2 == 1 ) { $BGColor = array("R"=>$BackgroundR1,"G"=>$BackgroundG1,"B"=>$BackgroundB1,"Alpha"=>$BackgroundAlpha1); } else { $BGColor = array("R"=>$BackgroundR2,"G"=>$BackgroundG2,"B"=>$BackgroundB2,"Alpha"=>$BackgroundAlpha2); } - if ( $LastY != NULL && $CycleBackground && ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) )) { $this->drawFilledRectangle($this->GraphAreaX1+$FloatingOffset,$LastY,$this->GraphAreaX2-$FloatingOffset,$YPos,$BGColor); } - - if ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } - - if ( $DrawSubTicks && $i != $Parameters["Rows"] ) - $this->drawLine($XPos-$OuterSubTickWidth,$YPos-$SubTicksSize,$XPos+$InnerSubTickWidth,$YPos-$SubTicksSize,array("R"=>$SubTickR,"G"=>$SubTickG,"B"=>$SubTickB,"Alpha"=>$SubTickAlpha)); - - $this->drawLine($XPos-$InnerTickWidth,$YPos,$XPos+$OuterTickWidth,$YPos,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); - $Bounds = $this->drawText($XPos+$OuterTickWidth+2,$YPos,$Value,array("Align"=>TEXT_ALIGN_MIDDLELEFT)); - $TxtLeft = $XPos+$OuterTickWidth+2+($Bounds[1]["X"]-$Bounds[0]["X"]); - $MaxLeft = max($MaxLeft,$TxtLeft); - - $LastY = $YPos; - } - - if ( isset($Parameters["Name"]) ) - { - $XPos = $MaxLeft+6; - $YPos = $this->GraphAreaY1+($this->GraphAreaY2-$this->GraphAreaY1)/2; - $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE,"Angle"=>270)); - $MaxLeft = $Bounds[2]["X"]; - - $this->DataSet->Data["GraphArea"]["X2"] = $MaxLeft + $this->FontSize; - } - $AxisPos["R"] = $MaxLeft + $ScaleSpacing; - } - } - elseif ( $Pos == SCALE_POS_TOPBOTTOM ) - { - if ( $Parameters["Position"] == AXIS_POSITION_TOP ) - { - if ( $Floating ) - { $FloatingOffset = $XMargin; $this->drawLine($this->GraphAreaX1+$Parameters["Margin"],$AxisPos["T"],$this->GraphAreaX2-$Parameters["Margin"],$AxisPos["T"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - else - { $FloatingOffset = 0; $this->drawLine($this->GraphAreaX1,$AxisPos["T"],$this->GraphAreaX2,$AxisPos["T"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - - if ( $DrawArrows ) { $this->drawArrow($this->GraphAreaX2-$Parameters["Margin"],$AxisPos["T"],$this->GraphAreaX2+($ArrowSize*2),$AxisPos["T"],array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } - - $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Parameters["Margin"]*2; - $Step = $Width / $Parameters["Rows"]; $SubTicksSize = $Step /2; $MinTop = $AxisPos["T"]; - $LastX = NULL; - for($i=0;$i<=$Parameters["Rows"];$i++) - { - $XPos = $this->GraphAreaX1 + $Parameters["Margin"] + $Step*$i; - $YPos = $AxisPos["T"]; - $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Parameters["Display"],$Parameters["Format"],$Parameters["Unit"]); - - if ( $i%2 == 1 ) { $BGColor = array("R"=>$BackgroundR1,"G"=>$BackgroundG1,"B"=>$BackgroundB1,"Alpha"=>$BackgroundAlpha1); } else { $BGColor = array("R"=>$BackgroundR2,"G"=>$BackgroundG2,"B"=>$BackgroundB2,"Alpha"=>$BackgroundAlpha2); } - if ( $LastX != NULL && $CycleBackground && ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) )) { $this->drawFilledRectangle($LastX,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,$BGColor); } - - if ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } - - if ( $DrawSubTicks && $i != $Parameters["Rows"] ) - $this->drawLine($XPos+$SubTicksSize,$YPos-$OuterSubTickWidth,$XPos+$SubTicksSize,$YPos+$InnerSubTickWidth,array("R"=>$SubTickR,"G"=>$SubTickG,"B"=>$SubTickB,"Alpha"=>$SubTickAlpha)); - - $this->drawLine($XPos,$YPos-$OuterTickWidth,$XPos,$YPos+$InnerTickWidth,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); - $Bounds = $this->drawText($XPos,$YPos-$OuterTickWidth-2,$Value,array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); - $TxtHeight = $YPos-$OuterTickWidth-2-($Bounds[1]["Y"]-$Bounds[2]["Y"]); - $MinTop = min($MinTop,$TxtHeight); - - $LastX = $XPos; - } - - if ( isset($Parameters["Name"]) ) - { - $YPos = $MinTop-2; - $XPos = $this->GraphAreaX1+($this->GraphAreaX2-$this->GraphAreaX1)/2; - $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); - $MinTop = $Bounds[2]["Y"]; - - $this->DataSet->Data["GraphArea"]["Y1"] = $MinTop; - } - - $AxisPos["T"] = $MinTop - $ScaleSpacing; - } - elseif ( $Parameters["Position"] == AXIS_POSITION_BOTTOM ) - { - if ( $Floating ) - { $FloatingOffset = $XMargin; $this->drawLine($this->GraphAreaX1+$Parameters["Margin"],$AxisPos["B"],$this->GraphAreaX2-$Parameters["Margin"],$AxisPos["B"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - else - { $FloatingOffset = 0; $this->drawLine($this->GraphAreaX1,$AxisPos["B"],$this->GraphAreaX2,$AxisPos["B"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } - - if ( $DrawArrows ) { $this->drawArrow($this->GraphAreaX2-$Parameters["Margin"],$AxisPos["B"],$this->GraphAreaX2+($ArrowSize*2),$AxisPos["B"],array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } - - $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Parameters["Margin"]*2; - $Step = $Width / $Parameters["Rows"]; $SubTicksSize = $Step /2; $MaxBottom = $AxisPos["B"]; - $LastX = NULL; - for($i=0;$i<=$Parameters["Rows"];$i++) - { - $XPos = $this->GraphAreaX1 + $Parameters["Margin"] + $Step*$i; - $YPos = $AxisPos["B"]; - $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Parameters["Display"],$Parameters["Format"],$Parameters["Unit"]); - - if ( $i%2 == 1 ) { $BGColor = array("R"=>$BackgroundR1,"G"=>$BackgroundG1,"B"=>$BackgroundB1,"Alpha"=>$BackgroundAlpha1); } else { $BGColor = array("R"=>$BackgroundR2,"G"=>$BackgroundG2,"B"=>$BackgroundB2,"Alpha"=>$BackgroundAlpha2); } - if ( $LastX != NULL && $CycleBackground && ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) )) { $this->drawFilledRectangle($LastX,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,$BGColor); } - - if ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } - - if ( $DrawSubTicks && $i != $Parameters["Rows"] ) - $this->drawLine($XPos+$SubTicksSize,$YPos-$OuterSubTickWidth,$XPos+$SubTicksSize,$YPos+$InnerSubTickWidth,array("R"=>$SubTickR,"G"=>$SubTickG,"B"=>$SubTickB,"Alpha"=>$SubTickAlpha)); - - $this->drawLine($XPos,$YPos-$OuterTickWidth,$XPos,$YPos+$InnerTickWidth,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); - $Bounds = $this->drawText($XPos,$YPos+$OuterTickWidth+2,$Value,array("Align"=>TEXT_ALIGN_TOPMIDDLE)); - $TxtHeight = $YPos+$OuterTickWidth+2+($Bounds[1]["Y"]-$Bounds[2]["Y"]); - $MaxBottom = max($MaxBottom,$TxtHeight); - - $LastX = $XPos; - } - - if ( isset($Parameters["Name"]) ) - { - $YPos = $MaxBottom+2; - $XPos = $this->GraphAreaX1+($this->GraphAreaX2-$this->GraphAreaX1)/2; - $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_TOPMIDDLE)); - $MaxBottom = $Bounds[0]["Y"]; - - $this->DataSet->Data["GraphArea"]["Y2"] = $MaxBottom + $this->FontSize; - } - - $AxisPos["B"] = $MaxBottom + $ScaleSpacing; - } - } - } - } - } - - function isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) - { - if ( $LabelingMethod == LABELING_DIFFERENT && $Value != $LastValue ) { return(TRUE); } - if ( $LabelingMethod == LABELING_DIFFERENT && $Value == $LastValue ) { return(FALSE); } - if ( $LabelingMethod == LABELING_ALL && $LabelSkip == 0 ) { return(TRUE); } - if ( $LabelingMethod == LABELING_ALL && ($ID+$LabelSkip) % ($LabelSkip+1) != 1 ) { return(FALSE); } - - return(TRUE); - } - - /* Compute the scale, check for the best visual factors */ - function computeScale($XMin,$XMax,$MaxDivs,$Factors,$AxisID=0) - { - /* Compute each factors */ - $Results = ""; - foreach ($Factors as $Key => $Factor) - $Results[$Factor] = $this->processScale($XMin,$XMax,$MaxDivs,array($Factor),$AxisID); - - /* Remove scales that are creating to much decimals */ - $GoodScaleFactors = ""; - foreach ($Results as $Key => $Result) - { - $Decimals = preg_split("/\./",$Result["RowHeight"]); - if ( (!isset($Decimals[1])) || (strlen($Decimals[1]) < 6) ) { $GoodScaleFactors[] = $Key; } - } - - /* Found no correct scale, shame,... returns the 1st one as default */ - if ( $GoodScaleFactors == "" ) { return($Results[$Factors[0]]); } - - /* Find the factor that cause the maximum number of Rows */ - $MaxRows = 0; $BestFactor = 0; - foreach($GoodScaleFactors as $Key => $Factor) - { if ( $Results[$Factor]["Rows"] > $MaxRows ) { $MaxRows = $Results[$Factor]["Rows"]; $BestFactor = $Factor; } } - - /* Return the best visual scale */ - return($Results[$BestFactor]); - } - - /* Compute the best matching scale based on size & factors */ - function processScale($XMin,$XMax,$MaxDivs,$Factors,$AxisID) - { - $ScaleHeight = abs(ceil($XMax)-floor($XMin)); - - if ( isset($this->DataSet->Data["Axis"][$AxisID]["Format"]) ) - $Format = $this->DataSet->Data["Axis"][$AxisID]["Format"]; - else - $Format = NULL; - - if ( isset($this->DataSet->Data["Axis"][$AxisID]["Display"]) ) - $Mode = $this->DataSet->Data["Axis"][$AxisID]["Display"]; - else - $Mode = AXIS_FORMAT_DEFAULT; - - $Scale = ""; - if ( $XMin != $XMax ) - { - $Found = FALSE; $Rescaled = FALSE; $Scaled10Factor = .0001; $Result = 0; - while(!$Found) - { - foreach($Factors as $Key => $Factor) - { - if ( !$Found ) - { - if ( !($this->modulo($XMin,$Factor*$Scaled10Factor) == 0) || ($XMin != floor($XMin))) { $XMinRescaled = floor($XMin/($Factor*$Scaled10Factor))*$Factor*$Scaled10Factor; } else { $XMinRescaled = $XMin; } - if ( !($this->modulo($XMax,$Factor*$Scaled10Factor) == 0) || ($XMax != floor($XMax))) { $XMaxRescaled = floor($XMax/($Factor*$Scaled10Factor))*$Factor*$Scaled10Factor+($Factor*$Scaled10Factor); } else { $XMaxRescaled = $XMax; } - $ScaleHeightRescaled = abs($XMaxRescaled-$XMinRescaled); - - if ( !$Found && floor($ScaleHeightRescaled/($Factor*$Scaled10Factor)) <= $MaxDivs ) { $Found = TRUE; $Rescaled = TRUE; $Result = $Factor * $Scaled10Factor; } - } - } - $Scaled10Factor = $Scaled10Factor * 10; - } - - /* ReCall Min / Max / Height */ - if ( $Rescaled ) { $XMin = $XMinRescaled; $XMax = $XMaxRescaled; $ScaleHeight = $ScaleHeightRescaled; } - - /* Compute rows size */ - $Rows = floor($ScaleHeight / $Result); if ( $Rows == 0 ) { $Rows = 1; } - $RowHeight = $ScaleHeight / $Rows; - - /* Return the results */ - $Scale["Rows"] = $Rows; $Scale["RowHeight"] = $RowHeight; $Scale["XMin"] = $XMin; $Scale["XMax"] = $XMax; - - /* Compute the needed decimals for the metric view to avoid repetition of the same X Axis labels */ - if ( $Mode == AXIS_FORMAT_METRIC && $Format == NULL ) - { - $Done = FALSE; $GoodDecimals = 0; - for($Decimals=0;$Decimals<=10;$Decimals++) - { - if ( !$Done ) - { - $LastLabel = "zob"; $ScaleOK = TRUE; - for($i=0;$i<=$Rows;$i++) - { - $Value = $XMin + $i*$RowHeight; - $Label = $this->scaleFormat($Value,AXIS_FORMAT_METRIC,$Decimals); - - if ( $LastLabel == $Label ) { $ScaleOK = FALSE; } - $LastLabel = $Label; - } - if ( $ScaleOK ) { $Done = TRUE; $GoodDecimals = $Decimals; } - } - } - - $Scale["Format"] = $GoodDecimals; - } - } - else - { - /* If all values are the same we keep a +1/-1 scale */ - $Rows = 2; $XMin = $XMax-1; $XMax = $XMax+1; $RowHeight = 1; - - /* Return the results */ - $Scale["Rows"] = $Rows; $Scale["RowHeight"] = $RowHeight; $Scale["XMin"] = $XMin; $Scale["XMax"] = $XMax; - } - - return($Scale); - } - - function modulo($Value1,$Value2) - { - if (floor($Value2) == 0) { return(0); } - if (floor($Value2) != 0) { return($Value1 % $Value2); } - - $MinValue = min($Value1,$Value2); $Factor = 10; - while ( floor($MinValue*$Factor) == 0 ) - { $Factor = $Factor * 10; } - - return(($Value1*$Factor) % ($Value2*$Factor)); - } - - /* Draw an X threshold */ - function drawXThreshold($Value,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 255; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 50; - $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : 6; - $Wide = isset($Format["Wide"]) ? $Format["Wide"] : FALSE; - $WideFactor = isset($Format["WideFactor"]) ? $Format["WideFactor"] : 5; - $WriteCaption = isset($Format["WriteCaption"]) ? $Format["WriteCaption"] : FALSE; - $Caption = isset($Format["Caption"]) ? $Format["Caption"] : NULL; - $CaptionAlign = isset($Format["CaptionAlign"]) ? $Format["CaptionAlign"] : CAPTION_LEFT_TOP; - $CaptionOffset = isset($Format["CaptionOffset"]) ? $Format["CaptionOffset"] : 5; - $CaptionR = isset($Format["CaptionR"]) ? $Format["CaptionR"] : 255; - $CaptionG = isset($Format["CaptionG"]) ? $Format["CaptionG"] : 255; - $CaptionB = isset($Format["CaptionB"]) ? $Format["CaptionB"] : 255; - $CaptionAlpha = isset($Format["CaptionAlpha"]) ? $Format["CaptionAlpha"] : 100; - $DrawBox = isset($Format["DrawBox"]) ? $Format["DrawBox"] : TRUE; - $DrawBoxBorder = isset($Format["DrawBoxBorder"]) ? $Format["DrawBoxBorder"] : FALSE; - $BorderOffset = isset($Format["BorderOffset"]) ? $Format["BorderOffset"] : 3; - $BoxRounded = isset($Format["BoxRounded"]) ? $Format["BoxRounded"] : TRUE; - $RoundedRadius = isset($Format["RoundedRadius"]) ? $Format["RoundedRadius"] : 3; - $BoxR = isset($Format["BoxR"]) ? $Format["BoxR"] : 0; - $BoxG = isset($Format["BoxG"]) ? $Format["BoxG"] : 0; - $BoxB = isset($Format["BoxB"]) ? $Format["BoxB"] : 0; - $BoxAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 30; - $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : ""; - $BoxBorderR = isset($Format["BoxBorderR"]) ? $Format["BoxBorderR"] : 255; - $BoxBorderG = isset($Format["BoxBorderG"]) ? $Format["BoxBorderG"] : 255; - $BoxBorderB = isset($Format["BoxBorderB"]) ? $Format["BoxBorderB"] : 255; - $BoxBorderAlpha = isset($Format["BoxBorderAlpha"]) ? $Format["BoxBorderAlpha"] : 100; - $ValueIsLabel = isset($Format["ValueIsLabel"]) ? $Format["ValueIsLabel"] : FALSE; - - $Data = $this->DataSet->getData(); - $AbscissaMargin = $this->getAbscissaMargin($Data); - $XScale = $this->scaleGetXSettings(); - - if ( is_array($Value) ) { foreach ($Value as $Key => $ID) { $this->drawXThreshold($ID,$Format); } return(0); } - - if ( $ValueIsLabel ) - { - $Format["ValueIsLabel"] = FALSE; - foreach($Data["Series"][$Data["Abscissa"]]["Data"] as $Key => $SerieValue) - { if ( $SerieValue == $Value ) { $this->drawXThreshold($Key,$Format); } } - - return(0); - } - - $CaptionSettings = array("DrawBox"=>$DrawBox,"DrawBoxBorder"=>$DrawBoxBorder,"BorderOffset"=>$BorderOffset,"BoxRounded"=>$BoxRounded,"RoundedRadius"=>$RoundedRadius, - "BoxR"=>$BoxR,"BoxG"=>$BoxG,"BoxB"=>$BoxB,"BoxAlpha"=>$BoxAlpha,"BoxSurrounding"=>$BoxSurrounding, - "BoxBorderR"=>$BoxBorderR,"BoxBorderG"=>$BoxBorderG,"BoxBorderB"=>$BoxBorderB,"BoxBorderAlpha"=>$BoxBorderAlpha, - "R"=>$CaptionR,"G"=>$CaptionG,"B"=>$CaptionB,"Alpha"=>$CaptionAlpha); - - if ( $Caption == NULL ) - { - if ( isset($Data["Abscissa"]) ) - { - if ( isset($Data["Series"][$Data["Abscissa"]]["Data"][$Value]) ) - $Caption = $Data["Series"][$Data["Abscissa"]]["Data"][$Value]; - else - $Caption = $Value; - } - else - $Caption = $Value; - } - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - $XStep = (($this->GraphAreaX2 - $this->GraphAreaX1) - $XScale[0] *2 ) / $XScale[1]; - $XPos = $this->GraphAreaX1 + $XScale[0] + $XStep * $Value; - $YPos1 = $this->GraphAreaY1 + $Data["YMargin"]; - $YPos2 = $this->GraphAreaY2 - $Data["YMargin"]; - - if ( $XPos >= $this->GraphAreaX1 + $AbscissaMargin && $XPos <= $this->GraphAreaX2 - $AbscissaMargin ) - { - $this->drawLine($XPos,$YPos1,$XPos,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); - - if ( $Wide ) - { - $this->drawLine($XPos-1,$YPos1,$XPos-1,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); - $this->drawLine($XPos+1,$YPos1,$XPos+1,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); - } - - if ( $WriteCaption ) - { - if ( $CaptionAlign == CAPTION_LEFT_TOP ) - { $Y = $YPos1 + $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_TOPMIDDLE; } - else - { $Y = $YPos2 - $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_BOTTOMMIDDLE; } - - $this->drawText($XPos,$Y,$Caption,$CaptionSettings); - } - - return(array("X"=>$XPos)); - } - } - elseif( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) - { - $XStep = (($this->GraphAreaY2 - $this->GraphAreaY1) - $XScale[0] *2 ) / $XScale[1]; - $XPos = $this->GraphAreaY1 + $XScale[0] + $XStep * $Value; - $YPos1 = $this->GraphAreaX1 + $Data["YMargin"]; - $YPos2 = $this->GraphAreaX2 - $Data["YMargin"]; - - if ( $XPos >= $this->GraphAreaY1 + $AbscissaMargin && $XPos <= $this->GraphAreaY2 - $AbscissaMargin ) - { - $this->drawLine($YPos1,$XPos,$YPos2,$XPos,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); - - if ( $Wide ) - { - $this->drawLine($YPos1,$XPos-1,$YPos2,$XPos-1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); - $this->drawLine($YPos1,$XPos+1,$YPos2,$XPos+1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); - } - - if ( $WriteCaption ) - { - if ( $CaptionAlign == CAPTION_LEFT_TOP ) - { $Y = $YPos1 + $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_MIDDLELEFT; } - else - { $Y = $YPos2 - $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_MIDDLERIGHT; } - - $this->drawText($Y,$XPos,$Caption,$CaptionSettings); - } - - return(array("X"=>$XPos)); - } - } - } - - /* Draw an X threshold area */ - function drawXThresholdArea($Value1,$Value2,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 255; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 20; - $Border = isset($Format["Border"]) ? $Format["Border"] : TRUE; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; - $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha + 20; - $BorderTicks = isset($Format["BorderTicks"]) ? $Format["BorderTicks"] : 2; - $AreaName = isset($Format["AreaName"]) ? $Format["AreaName"] : NULL; - $NameAngle = isset($Format["NameAngle"]) ? $Format["NameAngle"] : ZONE_NAME_ANGLE_AUTO; - $NameR = isset($Format["NameR"]) ? $Format["NameR"] : 255; - $NameG = isset($Format["NameG"]) ? $Format["NameG"] : 255; - $NameB = isset($Format["NameB"]) ? $Format["NameB"] : 255; - $NameAlpha = isset($Format["NameAlpha"]) ? $Format["NameAlpha"] : 100; - $DisableShadowOnArea = isset($Format["DisableShadowOnArea"]) ? $Format["DisableShadowOnArea"] : TRUE; - - $RestoreShadow = $this->Shadow; - if ( $DisableShadowOnArea && $this->Shadow ) { $this->Shadow = FALSE; } - - if ($BorderAlpha >100) { $BorderAlpha = 100;} - - $Data = $this->DataSet->getData(); - $XScale = $this->scaleGetXSettings(); - $AbscissaMargin = $this->getAbscissaMargin($Data); - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - $XStep = (($this->GraphAreaX2 - $this->GraphAreaX1) - $XScale[0] *2 ) / $XScale[1]; - $XPos1 = $this->GraphAreaX1 + $XScale[0] + $XStep * $Value1; - $XPos2 = $this->GraphAreaX1 + $XScale[0] + $XStep * $Value2; - $YPos1 = $this->GraphAreaY1 + $Data["YMargin"]; - $YPos2 = $this->GraphAreaY2 - $Data["YMargin"]; - - if ( $XPos1 < $this->GraphAreaX1 + $XScale[0] ) { $XPos1 = $this->GraphAreaX1 + $XScale[0]; } - if ( $XPos1 > $this->GraphAreaX2 - $XScale[0] ) { $XPos1 = $this->GraphAreaX2 - $XScale[0]; } - if ( $XPos2 < $this->GraphAreaX1 + $XScale[0] ) { $XPos2 = $this->GraphAreaX1 + $XScale[0]; } - if ( $XPos2 > $this->GraphAreaX2 - $XScale[0] ) { $XPos2 = $this->GraphAreaX2 - $XScale[0]; } - - $this->drawFilledRectangle($XPos1,$YPos1,$XPos2,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - - if ( $Border ) - { - $this->drawLine($XPos1,$YPos1,$XPos1,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); - $this->drawLine($XPos2,$YPos1,$XPos2,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); - } - - if ( $AreaName != NULL ) - { - $XPos = ($XPos2-$XPos1)/2 + $XPos1; - $YPos = ($YPos2-$YPos1)/2 + $YPos1; - - if ( $NameAngle == ZONE_NAME_ANGLE_AUTO ) - { - $TxtPos = $this->getTextBox($XPos,$YPos,$this->FontName,$this->FontSize,0,$AreaName); - $TxtWidth = $TxtPos[1]["X"] - $TxtPos[0]["X"]; - if ( abs($XPos2 - $XPos1) > $TxtWidth ) { $NameAngle = 0; } else { $NameAngle = 90; } - } - $this->Shadow = $RestoreShadow; - $this->drawText($XPos,$YPos,$AreaName,array("R"=>$NameR,"G"=>$NameG,"B"=>$NameB,"Alpha"=>$NameAlpha,"Angle"=>$NameAngle,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); - if ( $DisableShadowOnArea ) { $this->Shadow = FALSE; } - } - - $this->Shadow = $RestoreShadow; - return(array("X1"=>$XPos1,"X2"=>$XPos2)); - } - elseif ( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) - { - $XStep = (($this->GraphAreaY2 - $this->GraphAreaY1) - $XScale[0] *2 ) / $XScale[1]; - $XPos1 = $this->GraphAreaY1 + $XScale[0] + $XStep * $Value1; - $XPos2 = $this->GraphAreaY1 + $XScale[0] + $XStep * $Value2; - $YPos1 = $this->GraphAreaX1 + $Data["YMargin"]; - $YPos2 = $this->GraphAreaX2 - $Data["YMargin"]; - - if ( $XPos1 < $this->GraphAreaY1 + $XScale[0] ) { $XPos1 = $this->GraphAreaY1 + $XScale[0]; } - if ( $XPos1 > $this->GraphAreaY2 - $XScale[0] ) { $XPos1 = $this->GraphAreaY2 - $XScale[0]; } - if ( $XPos2 < $this->GraphAreaY1 + $XScale[0] ) { $XPos2 = $this->GraphAreaY1 + $XScale[0]; } - if ( $XPos2 > $this->GraphAreaY2 - $XScale[0] ) { $XPos2 = $this->GraphAreaY2 - $XScale[0]; } - - $this->drawFilledRectangle($YPos1,$XPos1,$YPos2,$XPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - - if ( $Border ) - { - $this->drawLine($YPos1,$XPos1,$YPos2,$XPos1,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); - $this->drawLine($YPos1,$XPos2,$YPos2,$XPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); - } - - if ( $AreaName != NULL ) - { - $XPos = ($XPos2-$XPos1)/2 + $XPos1; - $YPos = ($YPos2-$YPos1)/2 + $YPos1; - - $this->Shadow = $RestoreShadow; - $this->drawText($YPos,$XPos,$AreaName,array("R"=>$NameR,"G"=>$NameG,"B"=>$NameB,"Alpha"=>$NameAlpha,"Angle"=>0,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); - if ( $DisableShadowOnArea ) { $this->Shadow = FALSE; } - } - - $this->Shadow = $RestoreShadow; - return(array("X1"=>$XPos1,"X2"=>$XPos2)); - } - } - - /* Draw an Y threshold with the computed scale */ - function drawThreshold($Value,$Format="") - { - $AxisID = isset($Format["AxisID"]) ? $Format["AxisID"] : 0; - $R = isset($Format["R"]) ? $Format["R"] : 255; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 50; - $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; - $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : 6; - $Wide = isset($Format["Wide"]) ? $Format["Wide"] : FALSE; - $WideFactor = isset($Format["WideFactor"]) ? $Format["WideFactor"] : 5; - $WriteCaption = isset($Format["WriteCaption"]) ? $Format["WriteCaption"] : FALSE; - $Caption = isset($Format["Caption"]) ? $Format["Caption"] : NULL; - $CaptionAlign = isset($Format["CaptionAlign"]) ? $Format["CaptionAlign"] : CAPTION_LEFT_TOP; - $CaptionOffset = isset($Format["CaptionOffset"]) ? $Format["CaptionOffset"] : 10; - $CaptionR = isset($Format["CaptionR"]) ? $Format["CaptionR"] : 255; - $CaptionG = isset($Format["CaptionG"]) ? $Format["CaptionG"] : 255; - $CaptionB = isset($Format["CaptionB"]) ? $Format["CaptionB"] : 255; - $CaptionAlpha = isset($Format["CaptionAlpha"]) ? $Format["CaptionAlpha"] : 100; - $DrawBox = isset($Format["DrawBox"]) ? $Format["DrawBox"] : TRUE; - $DrawBoxBorder = isset($Format["DrawBoxBorder"]) ? $Format["DrawBoxBorder"] : FALSE; - $BorderOffset = isset($Format["BorderOffset"]) ? $Format["BorderOffset"] : 5; - $BoxRounded = isset($Format["BoxRounded"]) ? $Format["BoxRounded"] : TRUE; - $RoundedRadius = isset($Format["RoundedRadius"]) ? $Format["RoundedRadius"] : 3; - $BoxR = isset($Format["BoxR"]) ? $Format["BoxR"] : 0; - $BoxG = isset($Format["BoxG"]) ? $Format["BoxG"] : 0; - $BoxB = isset($Format["BoxB"]) ? $Format["BoxB"] : 0; - $BoxAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 20; - $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : ""; - $BoxBorderR = isset($Format["BoxBorderR"]) ? $Format["BoxBorderR"] : 255; - $BoxBorderG = isset($Format["BoxBorderG"]) ? $Format["BoxBorderG"] : 255; - $BoxBorderB = isset($Format["BoxBorderB"]) ? $Format["BoxBorderB"] : 255; - $BoxBorderAlpha = isset($Format["BoxBorderAlpha"]) ? $Format["BoxBorderAlpha"] : 100; - $NoMargin = isset($Format["NoMargin"]) ? $Format["NoMargin"] : FALSE; - - if ( is_array($Value) ) { foreach ($Value as $Key => $ID) { $this->drawThreshold($ID,$Format); } return(0); } - - $CaptionSettings = array("DrawBox"=>$DrawBox,"DrawBoxBorder"=>$DrawBoxBorder,"BorderOffset"=>$BorderOffset,"BoxRounded"=>$BoxRounded,"RoundedRadius"=>$RoundedRadius, - "BoxR"=>$BoxR,"BoxG"=>$BoxG,"BoxB"=>$BoxB,"BoxAlpha"=>$BoxAlpha,"BoxSurrounding"=>$BoxSurrounding, - "BoxBorderR"=>$BoxBorderR,"BoxBorderG"=>$BoxBorderG,"BoxBorderB"=>$BoxBorderB,"BoxBorderAlpha"=>$BoxBorderAlpha, - "R"=>$CaptionR,"G"=>$CaptionG,"B"=>$CaptionB,"Alpha"=>$CaptionAlpha); - - $Data = $this->DataSet->getData(); - $AbscissaMargin = $this->getAbscissaMargin($Data); - - if ( $NoMargin ) { $AbscissaMargin = 0; } - if ( !isset($Data["Axis"][$AxisID]) ) { return(-1); } - if ( $Caption == NULL ) { $Caption = $Value; } - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - $YPos = $this->scaleComputeY($Value,array("AxisID"=>$AxisID)); - if ( $YPos >= $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"] && $YPos <= $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"] ) - { - $X1 = $this->GraphAreaX1 + $AbscissaMargin; - $X2 = $this->GraphAreaX2 - $AbscissaMargin; - - $this->drawLine($X1,$YPos,$X2,$YPos,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); - - if ( $Wide ) - { - $this->drawLine($X1,$YPos-1,$X2,$YPos-1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); - $this->drawLine($X1,$YPos+1,$X2,$YPos+1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); - } - - if ( $WriteCaption ) - { - if ( $CaptionAlign == CAPTION_LEFT_TOP ) - { $X = $X1 + $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_MIDDLELEFT; } - else - { $X = $X2 - $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_MIDDLERIGHT; } - - $this->drawText($X,$YPos,$Caption,$CaptionSettings); - } - } - - return(array("Y"=>$YPos)); - } - - if ( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) - { - $XPos = $this->scaleComputeY($Value,array("AxisID"=>$AxisID)); - if ( $XPos >= $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"] && $XPos <= $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"] ) - { - $Y1 = $this->GraphAreaY1 + $AbscissaMargin; - $Y2 = $this->GraphAreaY2 - $AbscissaMargin; - - $this->drawLine($XPos,$Y1,$XPos,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); - - if ( $Wide ) - { - $this->drawLine($XPos-1,$Y1,$XPos-1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); - $this->drawLine($XPos+1,$Y1,$XPos+1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); - } - - if ( $WriteCaption ) - { - if ( $CaptionAlign == CAPTION_LEFT_TOP ) - { $Y = $Y1 + $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_TOPMIDDLE; } - else - { $Y = $Y2 - $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_BOTTOMMIDDLE; } - - $CaptionSettings["Align"] = TEXT_ALIGN_TOPMIDDLE; - $this->drawText($XPos,$Y,$Caption,$CaptionSettings); - } - } - - return(array("Y"=>$XPos)); - } - } - - /* Draw a threshold with the computed scale */ - function drawThresholdArea($Value1,$Value2,$Format="") - { - $AxisID = isset($Format["AxisID"]) ? $Format["AxisID"] : 0; - $R = isset($Format["R"]) ? $Format["R"] : 255; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 20; - $Border = isset($Format["Border"]) ? $Format["Border"] : TRUE; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; - $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha + 20; - $BorderTicks = isset($Format["BorderTicks"]) ? $Format["BorderTicks"] : 2; - $AreaName = isset($Format["AreaName"]) ? $Format["AreaName"] : NULL; - $NameAngle = isset($Format["NameAngle"]) ? $Format["NameAngle"] : ZONE_NAME_ANGLE_AUTO; - $NameR = isset($Format["NameR"]) ? $Format["NameR"] : 255; - $NameG = isset($Format["NameG"]) ? $Format["NameG"] : 255; - $NameB = isset($Format["NameB"]) ? $Format["NameB"] : 255; - $NameAlpha = isset($Format["NameAlpha"]) ? $Format["NameAlpha"] : 100; - $DisableShadowOnArea = isset($Format["DisableShadowOnArea"]) ? $Format["DisableShadowOnArea"] : TRUE; - $NoMargin = isset($Format["NoMargin"]) ? $Format["NoMargin"] : FALSE; - - if ($Value1 > $Value2) { list($Value1, $Value2) = array($Value2, $Value1); } - - $RestoreShadow = $this->Shadow; - if ( $DisableShadowOnArea && $this->Shadow ) { $this->Shadow = FALSE; } - - if ($BorderAlpha >100) { $BorderAlpha = 100;} - - $Data = $this->DataSet->getData(); - $AbscissaMargin = $this->getAbscissaMargin($Data); - - if ( $NoMargin ) { $AbscissaMargin = 0; } - if ( !isset($Data["Axis"][$AxisID]) ) { return(-1); } - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - $XPos1 = $this->GraphAreaX1 + $AbscissaMargin; - $XPos2 = $this->GraphAreaX2 - $AbscissaMargin; - $YPos1 = $this->scaleComputeY($Value1,array("AxisID"=>$AxisID)); - $YPos2 = $this->scaleComputeY($Value2,array("AxisID"=>$AxisID)); - - if ( $YPos1 < $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"] ) { $YPos1 = $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"]; } - if ( $YPos1 > $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"] ) { $YPos1 = $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"]; } - if ( $YPos2 < $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"] ) { $YPos2 = $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"]; } - if ( $YPos2 > $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"] ) { $YPos2 = $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"]; } - - $this->drawFilledRectangle($XPos1,$YPos1,$XPos2,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - if ( $Border ) - { - $this->drawLine($XPos1,$YPos1,$XPos2,$YPos1,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); - $this->drawLine($XPos1,$YPos2,$XPos2,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); - } - - if ( $AreaName != NULL ) - { - $XPos = ($XPos2-$XPos1)/2 + $XPos1; - $YPos = ($YPos2-$YPos1)/2 + $YPos1; - $this->Shadow = $RestoreShadow; - $this->drawText($XPos,$YPos,$AreaName,array("R"=>$NameR,"G"=>$NameG,"B"=>$NameB,"Alpha"=>$NameAlpha,"Angle"=>0,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); - if ( $DisableShadowOnArea ) { $this->Shadow = FALSE; } - } - - $this->Shadow = $RestoreShadow; - return(array("Y1"=>$YPos1,"Y2"=>$YPos2)); - } - elseif ( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) - { - $YPos1 = $this->GraphAreaY1 + $AbscissaMargin; - $YPos2 = $this->GraphAreaY2 - $AbscissaMargin; - $XPos1 = $this->scaleComputeY($Value1,array("AxisID"=>$AxisID)); - $XPos2 = $this->scaleComputeY($Value2,array("AxisID"=>$AxisID)); - - if ( $XPos1 < $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"] ) { $XPos1 = $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"]; } - if ( $XPos1 > $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"] ) { $XPos1 = $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"]; } - if ( $XPos2 < $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"] ) { $XPos2 = $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"]; } - if ( $XPos2 > $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"] ) { $XPos2 = $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"]; } - - $this->drawFilledRectangle($XPos1,$YPos1,$XPos2,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - if ( $Border ) - { - $this->drawLine($XPos1,$YPos1,$XPos1,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); - $this->drawLine($XPos2,$YPos1,$XPos2,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); - } - - if ( $AreaName != NULL ) - { - $XPos = ($YPos2-$YPos1)/2 + $YPos1; - $YPos = ($XPos2-$XPos1)/2 + $XPos1; - - if ( $NameAngle == ZONE_NAME_ANGLE_AUTO ) - { - $TxtPos = $this->getTextBox($XPos,$YPos,$this->FontName,$this->FontSize,0,$AreaName); - $TxtWidth = $TxtPos[1]["X"] - $TxtPos[0]["X"]; - if ( abs($XPos2 - $XPos1) > $TxtWidth ) { $NameAngle = 0; } else { $NameAngle = 90; } - } - $this->Shadow = $RestoreShadow; - $this->drawText($YPos,$XPos,$AreaName,array("R"=>$NameR,"G"=>$NameG,"B"=>$NameB,"Alpha"=>$NameAlpha,"Angle"=>$NameAngle,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); - if ( $DisableShadowOnArea ) { $this->Shadow = FALSE; } - } - - $this->Shadow = $RestoreShadow; - return(array("Y1"=>$XPos1,"Y2"=>$XPos2)); - } - } - - function scaleGetXSettings() - { - $Data = $this->DataSet->getData(); - foreach($Data["Axis"] as $AxisID => $Settings) - { - if ( $Settings["Identity"] == AXIS_X ) - { - $Rows = $Settings["Rows"]; - - return(array($Settings["Margin"],$Rows)); - } - } - } - - function scaleComputeY($Values,$Option="",$ReturnOnly0Height=FALSE) - { - $AxisID = isset($Option["AxisID"]) ? $Option["AxisID"] : 0; - $SerieName = isset($Option["SerieName"]) ? $Option["SerieName"] : NULL; - - $Data = $this->DataSet->getData(); - if ( !isset($Data["Axis"][$AxisID]) ) { return(-1); } - - if ( $SerieName != NULL ) { $AxisID = $Data["Series"][$SerieName]["Axis"]; } - if ( !is_array($Values) ) { $tmp = $Values; $Values = ""; $Values[0] = $tmp; } - - $Result = ""; - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Data["Axis"][$AxisID]["Margin"]*2; - $ScaleHeight = $Data["Axis"][$AxisID]["ScaleMax"] - $Data["Axis"][$AxisID]["ScaleMin"]; - $Step = $Height / $ScaleHeight; - - if ( $ReturnOnly0Height ) - { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $Step * $Value; } } } - else - { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $this->GraphAreaY2 - $Data["Axis"][$AxisID]["Margin"] - ($Step * ($Value-$Data["Axis"][$AxisID]["ScaleMin"])); } } } - } - else - { - $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Data["Axis"][$AxisID]["Margin"]*2; - $ScaleWidth = $Data["Axis"][$AxisID]["ScaleMax"] - $Data["Axis"][$AxisID]["ScaleMin"]; - $Step = $Width / $ScaleWidth; - - if ( $ReturnOnly0Height ) - { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $Step * $Value; } } } - else - { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $this->GraphAreaX1 + $Data["Axis"][$AxisID]["Margin"] + ($Step * ($Value-$Data["Axis"][$AxisID]["ScaleMin"])); } } } - } - - if ( count($Result) == 1 ) - return($Result[0]); - else - return($Result); - } - - /* Format the axis values */ - function scaleFormat($Value,$Mode=NULL,$Format=NULL,$Unit=NULL) - { - if ( $Value == VOID ) { return(""); } - - if ( $Mode == AXIS_FORMAT_CUSTOM ) - { if ( function_exists($Format) ) { return(call_user_func($Format,$Value)); } } - - if ( $Mode == AXIS_FORMAT_DATE ) - { if ( $Format == NULL ) { $Pattern = "d/m/Y"; } else { $Pattern = $Format; } return(date($Pattern,$Value)); } - - if ( $Mode == AXIS_FORMAT_TIME ) - { if ( $Format == NULL ) { $Pattern = "H:i:s"; } else { $Pattern = $Format; } return(date($Pattern,$Value)); } - - if ( $Mode == AXIS_FORMAT_CURRENCY ) - { return($Format.number_format($Value,2)); } - - if ( $Mode == AXIS_FORMAT_METRIC ) - { - if (abs($Value) > 1000000000) - return(round($Value/1000000000,$Format)."g".$Unit); - if (abs($Value) > 1000000) - return(round($Value/1000000,$Format)."m".$Unit); - elseif (abs($Value) >= 1000) - return(round($Value/1000,$Format)."k".$Unit); - - } - return($Value.$Unit); - } - - /* Write Max value on a chart */ - function writeBounds($Type=BOUND_BOTH,$Format=NULL) - { - $MaxLabelTxt = isset($Format["MaxLabelTxt"]) ? $Format["MaxLabelTxt"] : "max="; - $MinLabelTxt = isset($Format["MinLabelTxt"]) ? $Format["MinLabelTxt"] : "min="; - $Decimals = isset($Format["Decimals"]) ? $Format["Decimals"] : 1; - $ExcludedSeries = isset($Format["ExcludedSeries"]) ? $Format["ExcludedSeries"] : ""; - $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 4; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $MaxDisplayR = isset($Format["MaxDisplayR"]) ? $Format["MaxDisplayR"] : 0; - $MaxDisplayG = isset($Format["MaxDisplayG"]) ? $Format["MaxDisplayG"] : 0; - $MaxDisplayB = isset($Format["MaxDisplayB"]) ? $Format["MaxDisplayB"] : 0; - $MinDisplayR = isset($Format["MinDisplayR"]) ? $Format["MinDisplayR"] : 255; - $MinDisplayG = isset($Format["MinDisplayG"]) ? $Format["MinDisplayG"] : 255; - $MinDisplayB = isset($Format["MinDisplayB"]) ? $Format["MinDisplayB"] : 255; - $MinLabelPos = isset($Format["MinLabelPos"]) ? $Format["MinLabelPos"] : BOUND_LABEL_POS_AUTO; - $MaxLabelPos = isset($Format["MaxLabelPos"]) ? $Format["MaxLabelPos"] : BOUND_LABEL_POS_AUTO; - $DrawBox = isset($Format["DrawBox"]) ? $Format["DrawBox"] : TRUE; - $DrawBoxBorder = isset($Format["DrawBoxBorder"]) ? $Format["DrawBoxBorder"] : FALSE; - $BorderOffset = isset($Format["BorderOffset"]) ? $Format["BorderOffset"] : 5; - $BoxRounded = isset($Format["BoxRounded"]) ? $Format["BoxRounded"] : TRUE; - $RoundedRadius = isset($Format["RoundedRadius"]) ? $Format["RoundedRadius"] : 3; - $BoxR = isset($Format["BoxR"]) ? $Format["BoxR"] : 0; - $BoxG = isset($Format["BoxG"]) ? $Format["BoxG"] : 0; - $BoxB = isset($Format["BoxB"]) ? $Format["BoxB"] : 0; - $BoxAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 20; - $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : ""; - $BoxBorderR = isset($Format["BoxBorderR"]) ? $Format["BoxBorderR"] : 255; - $BoxBorderG = isset($Format["BoxBorderG"]) ? $Format["BoxBorderG"] : 255; - $BoxBorderB = isset($Format["BoxBorderB"]) ? $Format["BoxBorderB"] : 255; - $BoxBorderAlpha = isset($Format["BoxBorderAlpha"]) ? $Format["BoxBorderAlpha"] : 100; - - $CaptionSettings = array("DrawBox"=>$DrawBox,"DrawBoxBorder"=>$DrawBoxBorder,"BorderOffset"=>$BorderOffset,"BoxRounded"=>$BoxRounded,"RoundedRadius"=>$RoundedRadius, - "BoxR"=>$BoxR,"BoxG"=>$BoxG,"BoxB"=>$BoxB,"BoxAlpha"=>$BoxAlpha,"BoxSurrounding"=>$BoxSurrounding, - "BoxBorderR"=>$BoxBorderR,"BoxBorderG"=>$BoxBorderG,"BoxBorderB"=>$BoxBorderB,"BoxBorderAlpha"=>$BoxBorderAlpha); - - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - $Data = $this->DataSet->getData(); - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] && !isset($ExcludedSeries[$SerieName])) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } - - $MinValue = $this->DataSet->getMin($SerieName); - $MaxValue = $this->DataSet->getMax($SerieName); - - $MinPos = VOID; $MaxPos = VOID; - foreach($Serie["Data"] as $Key => $Value) - { - if ( $Value == $MinValue && $MinPos == VOID ) { $MinPos = $Key; } - if ( $Value == $MaxValue ) { $MaxPos = $Key; } - } - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; - $X = $this->GraphAreaX1 + $XMargin; - $SerieOffset = isset($Serie["XOffset"]) ? $Serie["XOffset"] : 0; - - if ( $Type == BOUND_MAX || $Type == BOUND_BOTH ) - { - if ( $MaxLabelPos == BOUND_LABEL_POS_TOP || ( $MaxLabelPos == BOUND_LABEL_POS_AUTO && $MaxValue >= 0) ) { $YPos = $PosArray[$MaxPos] - $DisplayOffset + 2; $Align = TEXT_ALIGN_BOTTOMMIDDLE; } - if ( $MaxLabelPos == BOUND_LABEL_POS_BOTTOM || ( $MaxLabelPos == BOUND_LABEL_POS_AUTO && $MaxValue < 0) ) { $YPos = $PosArray[$MaxPos] + $DisplayOffset + 2; $Align = TEXT_ALIGN_TOPMIDDLE; } - - $XPos = $X + $MaxPos*$XStep + $SerieOffset; - $Label = $MaxLabelTxt.$this->scaleFormat(round($MaxValue,$Decimals),$Mode,$Format,$Unit); - - $TxtPos = $this->getTextBox($XPos,$YPos,$this->FontName,$this->FontSize,0,$Label); - $XOffset = 0; $YOffset = 0; - if ( $TxtPos[0]["X"] < $this->GraphAreaX1 ) { $XOffset = (($this->GraphAreaX1 - $TxtPos[0]["X"])/2); } - if ( $TxtPos[1]["X"] > $this->GraphAreaX2 ) { $XOffset = -(($TxtPos[1]["X"] - $this->GraphAreaX2)/2); } - if ( $TxtPos[2]["Y"] < $this->GraphAreaY1 ) { $YOffset = $this->GraphAreaY1 - $TxtPos[2]["Y"]; } - if ( $TxtPos[0]["Y"] > $this->GraphAreaY2 ) { $YOffset = -($TxtPos[0]["Y"] - $this->GraphAreaY2); } - - $CaptionSettings["R"] = $MaxDisplayR; $CaptionSettings["G"] = $MaxDisplayG; - $CaptionSettings["B"] = $MaxDisplayB; $CaptionSettings["Align"] = $Align; - - $this->drawText($XPos+$XOffset,$YPos+$YOffset,$Label,$CaptionSettings); - } - - if ( $Type == BOUND_MIN || $Type == BOUND_BOTH ) - { - if ( $MinLabelPos == BOUND_LABEL_POS_TOP || ( $MinLabelPos == BOUND_LABEL_POS_AUTO && $MinValue >= 0) ) { $YPos = $PosArray[$MinPos] - $DisplayOffset + 2; $Align = TEXT_ALIGN_BOTTOMMIDDLE; } - if ( $MinLabelPos == BOUND_LABEL_POS_BOTTOM || ( $MinLabelPos == BOUND_LABEL_POS_AUTO && $MinValue < 0) ) { $YPos = $PosArray[$MinPos] + $DisplayOffset + 2; $Align = TEXT_ALIGN_TOPMIDDLE; } - - $XPos = $X + $MinPos*$XStep + $SerieOffset; - $Label = $MinLabelTxt.$this->scaleFormat(round($MinValue,$Decimals),$Mode,$Format,$Unit); - - $TxtPos = $this->getTextBox($XPos,$YPos,$this->FontName,$this->FontSize,0,$Label); - $XOffset = 0; $YOffset = 0; - if ( $TxtPos[0]["X"] < $this->GraphAreaX1 ) { $XOffset = (($this->GraphAreaX1 - $TxtPos[0]["X"])/2); } - if ( $TxtPos[1]["X"] > $this->GraphAreaX2 ) { $XOffset = -(($TxtPos[1]["X"] - $this->GraphAreaX2)/2); } - if ( $TxtPos[2]["Y"] < $this->GraphAreaY1 ) { $YOffset = $this->GraphAreaY1 - $TxtPos[2]["Y"]; } - if ( $TxtPos[0]["Y"] > $this->GraphAreaY2 ) { $YOffset = -($TxtPos[0]["Y"] - $this->GraphAreaY2); } - - $CaptionSettings["R"] = $MinDisplayR; $CaptionSettings["G"] = $MinDisplayG; - $CaptionSettings["B"] = $MinDisplayB; $CaptionSettings["Align"] = $Align; - - $this->drawText($XPos+$XOffset,$YPos-$DisplayOffset+$YOffset,$Label,$CaptionSettings); - } - } - else - { - $XStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; - $X = $this->GraphAreaY1 + $XMargin; - $SerieOffset = isset($Serie["XOffset"]) ? $Serie["XOffset"] : 0; - - if ( $Type == BOUND_MAX || $Type == BOUND_BOTH ) - { - if ( $MaxLabelPos == BOUND_LABEL_POS_TOP || ( $MaxLabelPos == BOUND_LABEL_POS_AUTO && $MaxValue >= 0) ) { $YPos = $PosArray[$MaxPos] + $DisplayOffset + 2; $Align = TEXT_ALIGN_MIDDLELEFT; } - if ( $MaxLabelPos == BOUND_LABEL_POS_BOTTOM || ( $MaxLabelPos == BOUND_LABEL_POS_AUTO && $MaxValue < 0) ) { $YPos = $PosArray[$MaxPos] - $DisplayOffset + 2; $Align = TEXT_ALIGN_MIDDLERIGHT; } - - $XPos = $X + $MaxPos*$XStep + $SerieOffset; - $Label = $MaxLabelTxt.$this->scaleFormat($MaxValue,$Mode,$Format,$Unit); - - $TxtPos = $this->getTextBox($YPos,$XPos,$this->FontName,$this->FontSize,0,$Label); - $XOffset = 0; $YOffset = 0; - if ( $TxtPos[0]["X"] < $this->GraphAreaX1 ) { $XOffset = $this->GraphAreaX1 - $TxtPos[0]["X"]; } - if ( $TxtPos[1]["X"] > $this->GraphAreaX2 ) { $XOffset = -($TxtPos[1]["X"] - $this->GraphAreaX2); } - if ( $TxtPos[2]["Y"] < $this->GraphAreaY1 ) { $YOffset = ($this->GraphAreaY1 - $TxtPos[2]["Y"])/2; } - if ( $TxtPos[0]["Y"] > $this->GraphAreaY2 ) { $YOffset = -(($TxtPos[0]["Y"] - $this->GraphAreaY2)/2);} - - $CaptionSettings["R"] = $MaxDisplayR; $CaptionSettings["G"] = $MaxDisplayG; - $CaptionSettings["B"] = $MaxDisplayB; $CaptionSettings["Align"] = $Align; - - $this->drawText($YPos+$XOffset,$XPos+$YOffset,$Label,$CaptionSettings); - } - - if ( $Type == BOUND_MIN || $Type == BOUND_BOTH ) - { - if ( $MinLabelPos == BOUND_LABEL_POS_TOP || ( $MinLabelPos == BOUND_LABEL_POS_AUTO && $MinValue >= 0) ) { $YPos = $PosArray[$MinPos] + $DisplayOffset + 2; $Align = TEXT_ALIGN_MIDDLELEFT; } - if ( $MinLabelPos == BOUND_LABEL_POS_BOTTOM || ( $MinLabelPos == BOUND_LABEL_POS_AUTO && $MinValue < 0) ) { $YPos = $PosArray[$MinPos] - $DisplayOffset + 2; $Align = TEXT_ALIGN_MIDDLERIGHT; } - - $XPos = $X + $MinPos*$XStep + $SerieOffset; - $Label = $MinLabelTxt.$this->scaleFormat($MinValue,$Mode,$Format,$Unit); - - $TxtPos = $this->getTextBox($YPos,$XPos,$this->FontName,$this->FontSize,0,$Label); - $XOffset = 0; $YOffset = 0; - if ( $TxtPos[0]["X"] < $this->GraphAreaX1 ) { $XOffset = $this->GraphAreaX1 - $TxtPos[0]["X"]; } - if ( $TxtPos[1]["X"] > $this->GraphAreaX2 ) { $XOffset = -($TxtPos[1]["X"] - $this->GraphAreaX2); } - if ( $TxtPos[2]["Y"] < $this->GraphAreaY1 ) { $YOffset = ($this->GraphAreaY1 - $TxtPos[2]["Y"])/2; } - if ( $TxtPos[0]["Y"] > $this->GraphAreaY2 ) { $YOffset = -(($TxtPos[0]["Y"] - $this->GraphAreaY2)/2);} - - $CaptionSettings["R"] = $MinDisplayR; $CaptionSettings["G"] = $MinDisplayG; - $CaptionSettings["B"] = $MinDisplayB; $CaptionSettings["Align"] = $Align; - - $this->drawText($YPos+$XOffset,$XPos+$YOffset,$Label,$CaptionSettings); - } - } - } - } - } - - /* Draw a plot chart */ - function drawPlotChart($Format=NULL) - { - $PlotSize = isset($Format["PlotSize"]) ? $Format["PlotSize"] : NULL; - $PlotBorder = isset($Format["PlotBorder"]) ? $Format["PlotBorder"] : FALSE; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 50; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 50; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 50; - $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : 30; - $BorderSize = isset($Format["BorderSize"]) ? $Format["BorderSize"] : 2; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; - $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 4; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; - $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; - $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - - $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - if ( isset($Serie["Weight"]) ) { $SerieWeight = $Serie["Weight"] + 2; } else { $SerieWeight = 2; } - if ( $PlotSize != NULL ) { $SerieWeight = $PlotSize; } - - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; - if ( $Surrounding != NULL ) { $BorderR = $R + $Surrounding; $BorderG = $G + $Surrounding; $BorderB = $B + $Surrounding; } - if ( isset($Serie["Picture"]) ) - { $Picture = $Serie["Picture"]; list($PicWidth,$PicHeight,$PicType) = $this->getPicInfo($Picture); } - else { $Picture = NULL; $PicOffset = 0; } - - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } - - $AxisID = $Serie["Axis"]; - $Shape = $Serie["Shape"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - if ( $Picture != NULL ) { $PicOffset = $PicHeight / 2; $SerieWeight = 0; } - $X = $this->GraphAreaX1 + $XMargin; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - foreach($PosArray as $Key => $Y) - { - if ( $DisplayValues ) - $this->drawText($X,$Y-$DisplayOffset-$SerieWeight-$BorderSize-$PicOffset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); - - if ( $Y != VOID ) - { - if ( $RecordImageMap ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$SerieWeight,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $Picture != NULL ) - { $this->drawFromPicture($PicType,$Picture,$X-$PicWidth/2,$Y-$PicHeight/2); } - else - { $this->drawShape($X,$Y,$Shape,$SerieWeight,$PlotBorder,$BorderSize,$R,$G,$B,$Alpha,$BorderR,$BorderG,$BorderB,$BorderAlpha); } - } - $X = $X + $XStep; - } - } - else - { - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - if ( $Picture != NULL ) { $PicOffset = $PicWidth / 2; $SerieWeight = 0; } - $Y = $this->GraphAreaY1 + $XMargin; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - foreach($PosArray as $Key => $X) - { - if ( $DisplayValues ) - $this->drawText($X+$DisplayOffset+$SerieWeight+$BorderSize+$PicOffset,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); - - if ( $X != VOID ) - { - if ( $RecordImageMap ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$SerieWeight,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $Picture != NULL ) - { $this->drawFromPicture($PicType,$Picture,$X-$PicWidth/2,$Y-$PicHeight/2); } - else - { $this->drawShape($X,$Y,$Shape,$SerieWeight,$PlotBorder,$BorderSize,$R,$G,$B,$Alpha,$BorderR,$BorderG,$BorderB,$BorderAlpha); } - } - $Y = $Y + $YStep; - } - } - } - } - } - - /* Draw a spline chart */ - function drawSplineChart($Format=NULL) - { - $BreakVoid = isset($Format["BreakVoid"]) ? $Format["BreakVoid"] : TRUE; - $VoidTicks = isset($Format["VoidTicks"]) ? $Format["VoidTicks"] : 4; - $BreakR = isset($Format["BreakR"]) ? $Format["BreakR"] : NULL; // 234 - $BreakG = isset($Format["BreakG"]) ? $Format["BreakG"] : NULL; // 55 - $BreakB = isset($Format["BreakB"]) ? $Format["BreakB"] : NULL; // 26 - $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; - $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; - $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; - $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - $ImageMapPlotSize = isset($Format["ImageMapPlotSize"]) ? $Format["ImageMapPlotSize"] : 5; - - $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; - - if ( $BreakR == NULL ) - $BreakSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks); - else - $BreakSettings = array("R"=>$BreakR,"G"=>$BreakG,"B"=>$BreakB,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); - - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; $WayPoints = ""; - $Force = $XStep / 5; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $LastGoodY = NULL; $LastGoodX = NULL; $LastX = 1; $LastY = 1; - foreach($PosArray as $Key => $Y) - { - if ( $DisplayValues ) - $this->drawText($X,$Y-$DisplayOffset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); - - if ( $RecordImageMap && $Y != VOID ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$ImageMapPlotSize,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $Y == VOID && $LastY != NULL ) - { $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); $WayPoints = ""; } - - if ( $Y != VOID && $LastY == NULL && $LastGoodY != NULL && !$BreakVoid ) - { $this->drawLine($LastGoodX,$LastGoodY,$X,$Y,$BreakSettings); } - - if ( $Y != VOID ) - $WayPoints[] = array($X,$Y); - - if ( $Y != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } - if ( $Y == VOID ) { $Y = NULL; } - - $LastX = $X; $LastY = $Y; - $X = $X + $XStep; - } - $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); - } - else - { - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; $WayPoints = ""; - $Force = $YStep / 5; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $LastGoodY = NULL; $LastGoodX = NULL; $LastX = 1; $LastY = 1; - foreach($PosArray as $Key => $X) - { - if ( $DisplayValues ) - $this->drawText($X+$DisplayOffset,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); - - if ( $RecordImageMap && $X != VOID ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$ImageMapPlotSize,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $X == VOID && $LastX != NULL ) - { $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); $WayPoints = ""; } - - if ( $X != VOID && $LastX == NULL && $LastGoodX != NULL && !$BreakVoid ) - { $this->drawLine($LastGoodX,$LastGoodY,$X,$Y,$BreakSettings); } - - if ( $X != VOID ) - $WayPoints[] = array($X,$Y); - - if ( $X != VOID ) { $LastGoodX = $X; $LastGoodY = $Y; } - if ( $X == VOID ) { $X = NULL; } - - $LastX = $X; $LastY = $Y; - $Y = $Y + $YStep; - } - $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); - } - } - } - } - - /* Draw a filled spline chart */ - function drawFilledSplineChart($Format=NULL) - { - $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; - $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; - $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; - $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; - $AroundZero = isset($Format["AroundZero"]) ? $Format["AroundZero"] : TRUE; - $Threshold = isset($Format["Threshold"]) ? $Format["Threshold"] : NULL; - - $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - if ( $AroundZero ) { $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); } - - if ( $Threshold != NULL ) - { - foreach($Threshold as $Key => $Params) - { - $Threshold[$Key]["MinX"] = $this->scaleComputeY($Params["Min"],array("AxisID"=>$Serie["Axis"])); - $Threshold[$Key]["MaxX"] = $this->scaleComputeY($Params["Max"],array("AxisID"=>$Serie["Axis"])); - } - } - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; $WayPoints = ""; - $Force = $XStep / 5; - - if ( !$AroundZero ) { $YZero = $this->GraphAreaY2-1; } - if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } - if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } - - $LastX = ""; $LastY = ""; - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - foreach($PosArray as $Key => $Y) - { - if ( $DisplayValues ) - $this->drawText($X,$Y-$DisplayOffset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); - - if ( $Y == VOID ) - { - $Area = $this->drawSpline($WayPoints,array("Force"=>$Force,"PathOnly"=>TRUE)); - - if ( $Area != "" ) - { - foreach ($Area as $key => $Points) - { - $Corners = ""; $Corners[] = $Area[$key][0]["X"]; $Corners[] = $YZero; - foreach($Points as $subKey => $Point) - { - if ( $subKey == count($Points)-1) { $Corners[] = $Point["X"]-1; } else { $Corners[] = $Point["X"]; } - $Corners[] = $Point["Y"]+1; - } - $Corners[] = $Points[$subKey]["X"]-1; $Corners[] = $YZero; - - $this->drawPolygonChart($Corners,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/2,"NoBorder"=>TRUE,"Threshold"=>$Threshold)); - } - $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - } - - $WayPoints = ""; - } - else - $WayPoints[] = array($X,$Y-.5); /* -.5 for AA visual fix */ - - $X = $X + $XStep; - } - $Area = $this->drawSpline($WayPoints,array("Force"=>$Force,"PathOnly"=>TRUE)); - - if ( $Area != "" ) - { - foreach ($Area as $key => $Points) - { - $Corners = ""; $Corners[] = $Area[$key][0]["X"]; $Corners[] = $YZero; - foreach($Points as $subKey => $Point) - { - if ( $subKey == count($Points)-1) { $Corners[] = $Point["X"]-1; } else { $Corners[] = $Point["X"]; } - $Corners[] = $Point["Y"]+1; - } - $Corners[] = $Points[$subKey]["X"]-1; $Corners[] = $YZero; - - $this->drawPolygonChart($Corners,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/2,"NoBorder"=>TRUE,"Threshold"=>$Threshold)); - } - $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - } - } - else - { - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; $WayPoints = ""; - $Force = $YStep / 5; - - if ( !$AroundZero ) { $YZero = $this->GraphAreaX1+1; } - if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } - if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - foreach($PosArray as $Key => $X) - { - if ( $DisplayValues ) - $this->drawText($X+$DisplayOffset,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); - - if ( $X == VOID ) - { - $Area = $this->drawSpline($WayPoints,array("Force"=>$Force,"PathOnly"=>TRUE)); - - if ( $Area != "" ) - { - foreach ($Area as $key => $Points) - { - $Corners = ""; $Corners[] = $YZero; $Corners[] = $Area[$key][0]["Y"]; - foreach($Points as $subKey => $Point) - { - if ( $subKey == count($Points)-1) { $Corners[] = $Point["X"]-1; } else { $Corners[] = $Point["X"]; } - $Corners[] = $Point["Y"]; - } - $Corners[] = $YZero; $Corners[] = $Points[$subKey]["Y"]-1; - - $this->drawPolygonChart($Corners,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/2,"NoBorder"=>TRUE,"Threshold"=>$Threshold)); - } - $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - } - - $WayPoints = ""; - } - else - $WayPoints[] = array($X,$Y); - - $Y = $Y + $YStep; - } - $Area = $this->drawSpline($WayPoints,array("Force"=>$Force,"PathOnly"=>TRUE)); - - if ( $Area != "" ) - { - foreach ($Area as $key => $Points) - { - $Corners = ""; $Corners[] = $YZero; $Corners[] = $Area[$key][0]["Y"]; - foreach($Points as $subKey => $Point) - { - if ( $subKey == count($Points)-1) { $Corners[] = $Point["X"]-1; } else { $Corners[] = $Point["X"]; } - $Corners[] = $Point["Y"]; - } - $Corners[] = $YZero; $Corners[] = $Points[$subKey]["Y"]-1; - - $this->drawPolygonChart($Corners,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/2,"NoBorder"=>TRUE,"Threshold"=>$Threshold)); - } - $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); - } - - } - } - } - } - - /* Draw a line chart */ - function drawLineChart($Format=NULL) - { - $BreakVoid = isset($Format["BreakVoid"]) ? $Format["BreakVoid"] : TRUE; - $VoidTicks = isset($Format["VoidTicks"]) ? $Format["VoidTicks"] : 4; - $BreakR = isset($Format["BreakR"]) ? $Format["BreakR"] : NULL; - $BreakG = isset($Format["BreakG"]) ? $Format["BreakG"] : NULL; - $BreakB = isset($Format["BreakB"]) ? $Format["BreakB"] : NULL; - $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; - $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; - $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; - $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - $ImageMapPlotSize = isset($Format["ImageMapPlotSize"]) ? $Format["ImageMapPlotSize"] : 5; - $ForceColor = isset($Format["ForceColor"]) ? $Format["ForceColor"] : FALSE; - $ForceR = isset($Format["ForceR"]) ? $Format["ForceR"] : 0; - $ForceG = isset($Format["ForceG"]) ? $Format["ForceG"] : 0; - $ForceB = isset($Format["ForceB"]) ? $Format["ForceB"] : 0; - $ForceAlpha = isset($Format["ForceAlpha"]) ? $Format["ForceAlpha"] : 100; - - $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; - - if ( $ForceColor ) - { $R = $ForceR; $G = $ForceG; $B = $ForceB; $Alpha = $ForceAlpha; } - - if ( $BreakR == NULL ) - $BreakSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); - else - $BreakSettings = array("R"=>$BreakR,"G"=>$BreakG,"B"=>$BreakB,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); - - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $LastGoodY = NULL; $LastGoodX = NULL; - foreach($PosArray as $Key => $Y) - { - if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) - { - if ( $Serie["Data"][$Key] > 0 ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } - $this->drawText($X,$Y-$Offset-$Weight,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); - } - - if ( $RecordImageMap && $Y != VOID ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$ImageMapPlotSize,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $Y != VOID && $LastX != NULL && $LastY != NULL ) - $this->drawLine($LastX,$LastY,$X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); - - if ( $Y != VOID && $LastY == NULL && $LastGoodY != NULL && !$BreakVoid ) - { $this->drawLine($LastGoodX,$LastGoodY,$X,$Y,$BreakSettings); $LastGoodY = NULL; } - - if ( $Y != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } - if ( $Y == VOID ) { $Y = NULL; } - - $LastX = $X; $LastY = $Y; - $X = $X + $XStep; - } - } - else - { - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $LastGoodY = NULL; $LastGoodX = NULL; - foreach($PosArray as $Key => $X) - { - if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) - { $this->drawText($X+$DisplayOffset+$Weight,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); } - - if ( $RecordImageMap && $X != VOID ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$ImageMapPlotSize,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $X != VOID && $LastX != NULL && $LastY != NULL ) - $this->drawLine($LastX,$LastY,$X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); - - if ( $X != VOID && $LastX == NULL && $LastGoodY != NULL && !$BreakVoid ) - { $this->drawLine($LastGoodX,$LastGoodY,$X,$Y,$BreakSettings); $LastGoodY = NULL; } - - if ( $X != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } - if ( $X == VOID ) { $X = NULL; } - - $LastX = $X; $LastY = $Y; - $Y = $Y + $YStep; - } - } - } - } - } - - /* Draw a line chart */ - function drawZoneChart($SerieA,$SerieB,$Format=NULL) - { - $AxisID = isset($Format["AxisID"]) ? $Format["AxisID"] : 0; - $LineR = isset($Format["LineR"]) ? $Format["LineR"] : 150; - $LineG = isset($Format["LineG"]) ? $Format["LineG"] : 150; - $LineB = isset($Format["LineB"]) ? $Format["LineB"] : 150; - $LineAlpha = isset($Format["LineAlpha"]) ? $Format["LineAlpha"] : 50; - $LineTicks = isset($Format["LineTicks"]) ? $Format["LineTicks"] : 1; - $AreaR = isset($Format["AreaR"]) ? $Format["AreaR"] : 150; - $AreaG = isset($Format["AreaG"]) ? $Format["AreaG"] : 150; - $AreaB = isset($Format["AreaB"]) ? $Format["AreaB"] : 150; - $AreaAlpha = isset($Format["AreaAlpha"]) ? $Format["AreaAlpha"] : 5; - - $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; - - $Data = $this->DataSet->getData(); - if ( !isset($Data["Series"][$SerieA]["Data"]) || !isset($Data["Series"][$SerieB]["Data"]) ) { return(0); } - $SerieAData = $Data["Series"][$SerieA]["Data"]; - $SerieBData = $Data["Series"][$SerieB]["Data"]; - - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - $PosArrayA = $this->scaleComputeY($SerieAData,array("AxisID"=>$AxisID)); - $PosArrayB = $this->scaleComputeY($SerieBData,array("AxisID"=>$AxisID)); - if ( count($PosArrayA) != count($PosArrayB) ) { return(0); } - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; - - $LastX = NULL; $LastY1 = NULL; $LastY2 = NULL; - $BoundsA = ""; $BoundsB = ""; - foreach($PosArrayA as $Key => $Y1) - { - $Y2 = $PosArrayB[$Key]; - - $BoundsA[] = $X; $BoundsA[] = $Y1; - $BoundsB[] = $X; $BoundsB[] = $Y2; - - $LastX = $X; - $LastY1 = $Y1; $LastY2 = $Y2; - - $X = $X + $XStep; - } - $Bounds = array_merge($BoundsA,$this->reversePlots($BoundsB)); - $this->drawPolygonChart($Bounds,array("R"=>$AreaR,"G"=>$AreaG,"B"=>$AreaB,"Alpha"=>$AreaAlpha)); - - for($i=0;$i<=count($BoundsA)-4;$i=$i+2) - { - $this->drawLine($BoundsA[$i],$BoundsA[$i+1],$BoundsA[$i+2],$BoundsA[$i+3],array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha,"Ticks"=>$LineTicks)); - $this->drawLine($BoundsB[$i],$BoundsB[$i+1],$BoundsB[$i+2],$BoundsB[$i+3],array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha,"Ticks"=>$LineTicks)); - } - } - else - { - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; - - $LastY = NULL; $LastX1 = NULL; $LastX2 = NULL; - $BoundsA = ""; $BoundsB = ""; - foreach($PosArrayA as $Key => $X1) - { - $X2 = $PosArrayB[$Key]; - - $BoundsA[] = $X1; $BoundsA[] = $Y; - $BoundsB[] = $X2; $BoundsB[] = $Y; - - $LastY = $Y; - $LastX1 = $X1; $LastX2 = $X2; - - $Y = $Y + $YStep; - } - $Bounds = array_merge($BoundsA,$this->reversePlots($BoundsB)); - $this->drawPolygonChart($Bounds,array("R"=>$AreaR,"G"=>$AreaG,"B"=>$AreaB,"Alpha"=>$AreaAlpha)); - - for($i=0;$i<=count($BoundsA)-4;$i=$i+2) - { - $this->drawLine($BoundsA[$i],$BoundsA[$i+1],$BoundsA[$i+2],$BoundsA[$i+3],array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha,"Ticks"=>$LineTicks)); - $this->drawLine($BoundsB[$i],$BoundsB[$i+1],$BoundsB[$i+2],$BoundsB[$i+3],array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha,"Ticks"=>$LineTicks)); - } - } - } - - /* Draw a step chart */ - function drawStepChart($Format=NULL) - { - $BreakVoid = isset($Format["BreakVoid"]) ? $Format["BreakVoid"] : FALSE; - $ReCenter = isset($Format["ReCenter"]) ? $Format["ReCenter"] : TRUE; - $VoidTicks = isset($Format["VoidTicks"]) ? $Format["VoidTicks"] : 4; - $BreakR = isset($Format["BreakR"]) ? $Format["BreakR"] : NULL; - $BreakG = isset($Format["BreakG"]) ? $Format["BreakG"] : NULL; - $BreakB = isset($Format["BreakB"]) ? $Format["BreakB"] : NULL; - $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] :FALSE; - $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; - $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; - $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - $ImageMapPlotSize = isset($Format["ImageMapPlotSize"]) ? $Format["ImageMapPlotSize"] : 5; - - $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; - - if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } - - if ( $BreakR == NULL ) - $BreakSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); - else - $BreakSettings = array("R"=>$BreakR,"G"=>$BreakG,"B"=>$BreakB,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); - - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight); - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $LastGoodY = NULL; $LastGoodX = NULL; $Init = FALSE; - foreach($PosArray as $Key => $Y) - { - if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) - { - if ( $Y <= $LastY ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } - $this->drawText($X,$Y-$Offset-$Weight,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); - } - - if ( $Y != VOID && $LastX != NULL && $LastY != NULL ) - { - $this->drawLine($LastX,$LastY,$X,$LastY,$Color); - $this->drawLine($X,$LastY,$X,$Y,$Color); - if ( $ReCenter && $X+$XStep < $this->GraphAreaX2 - $XMargin ) - { - $this->drawLine($X,$Y,$X+$XStep,$Y,$Color); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X-$ImageMapPlotSize).",".floor($Y-$ImageMapPlotSize).",".floor($X+$XStep+$ImageMapPlotSize).",".floor($Y+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - else - { if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastX-$ImageMapPlotSize).",".floor($LastY-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($LastY+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } } - } - - if ( $Y != VOID && $LastY == NULL && $LastGoodY != NULL && !$BreakVoid ) - { - if ( $ReCenter ) - { - $this->drawLine($LastGoodX+$XStep,$LastGoodY,$X,$LastGoodY,$BreakSettings); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastGoodX+$XStep-$ImageMapPlotSize).",".floor($LastGoodY-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($LastGoodY+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - else - { - $this->drawLine($LastGoodX,$LastGoodY,$X,$LastGoodY,$BreakSettings); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastGoodX-$ImageMapPlotSize).",".floor($LastGoodY-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($LastGoodY+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - - $this->drawLine($X,$LastGoodY,$X,$Y,$BreakSettings); - $LastGoodY = NULL; - } - elseif( !$BreakVoid && $LastGoodY == NULL && $Y != VOID ) - { - $this->drawLine($this->GraphAreaX1 + $XMargin,$Y,$X,$Y,$BreakSettings); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($this->GraphAreaX1+$XMargin-$ImageMapPlotSize).",".floor($Y-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($Y+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - - if ( $Y != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } - if ( $Y == VOID ) { $Y = NULL; } - - if ( !$Init && $ReCenter ) { $X = $X - $XStep/2; $Init = TRUE; } - $LastX = $X; $LastY = $Y; - if ( $LastX < $this->GraphAreaX1 + $XMargin ) { $LastX = $this->GraphAreaX1 + $XMargin; } - $X = $X + $XStep; - } - if ( $ReCenter ) - { - $this->drawLine($LastX,$LastY,$this->GraphAreaX2 - $XMargin,$LastY,$Color); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastX-$ImageMapPlotSize).",".floor($LastY-$ImageMapPlotSize).",".floor($this->GraphAreaX2-$XMargin+$ImageMapPlotSize).",".floor($LastY+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - } - else - { - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $LastGoodY = NULL; $LastGoodX = NULL; $Init = FALSE; - foreach($PosArray as $Key => $X) - { - if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) - { - if ( $X >= $LastX ) { $Align = TEXT_ALIGN_MIDDLELEFT; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_MIDDLERIGHT; $Offset = -$DisplayOffset; } - $this->drawText($X+$Offset+$Weight,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); - } - - if ( $X != VOID && $LastX != NULL && $LastY != NULL ) - { - $this->drawLine($LastX,$LastY,$LastX,$Y,$Color); - $this->drawLine($LastX,$Y,$X,$Y,$Color); - - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastX-$ImageMapPlotSize).",".floor($LastY-$ImageMapPlotSize).",".floor($LastX+$XStep+$ImageMapPlotSize).",".floor($Y+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - - if ( $X != VOID && $LastX == NULL && $LastGoodY != NULL && !$BreakVoid ) - { - $this->drawLine($LastGoodX,$LastGoodY,$LastGoodX,$LastGoodY+$YStep,$Color); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastGoodX-$ImageMapPlotSize).",".floor($LastGoodY-$ImageMapPlotSize).",".floor($LastGoodX+$ImageMapPlotSize).",".floor($LastGoodY+$YStep+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - $this->drawLine($LastGoodX,$LastGoodY+$YStep,$LastGoodX,$Y,$BreakSettings); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastGoodX-$ImageMapPlotSize).",".floor($LastGoodY+$YStep-$ImageMapPlotSize).",".floor($LastGoodX+$ImageMapPlotSize).",".floor($YStep+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - $this->drawLine($LastGoodX,$Y,$X,$Y,$BreakSettings); - $LastGoodY = NULL; - } - elseif ( $X != VOID && $LastGoodY == NULL && !$BreakVoid ) - { - $this->drawLine($X,$this->GraphAreaY1 + $XMargin,$X,$Y,$BreakSettings); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X-$ImageMapPlotSize).",".floor($this->GraphAreaY1+$XMargin-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($Y+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - - if ( $X != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } - if ( $X == VOID ) { $X = NULL; } - - if ( !$Init && $ReCenter ) { $Y = $Y - $YStep/2; $Init = TRUE; } - $LastX = $X; $LastY = $Y; - if ( $LastY < $this->GraphAreaY1 + $XMargin ) { $LastY = $this->GraphAreaY1 + $XMargin; } - $Y = $Y + $YStep; - } - if ( $ReCenter ) - { - $this->drawLine($LastX,$LastY,$LastX,$this->GraphAreaY2 - $XMargin,$Color); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastX-$ImageMapPlotSize).",".floor($LastY-$ImageMapPlotSize).",".floor($LastX+$ImageMapPlotSize).",".floor($this->GraphAreaY2-$XMargin+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - } - } - } - } - - /* Draw a step chart */ - function drawFilledStepChart($Format=NULL) - { - $ReCenter = isset($Format["ReCenter"]) ? $Format["ReCenter"] : TRUE; - $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] :FALSE; - $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $ForceTransparency = isset($Format["ForceTransparency"]) ? $Format["ForceTransparency"] : NULL; - $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; - $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; - $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; - $AroundZero = isset($Format["AroundZero"]) ? $Format["AroundZero"] : TRUE; - - $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; - - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - $Color = array("R"=>$R,"G"=>$G,"B"=>$B); - if ( $ForceTransparency != NULL ) { $Color["Alpha"] = $ForceTransparency; } else { $Color["Alpha"] = $Alpha; } - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } - if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } - - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; - - if ( !$AroundZero ) { $YZero = $this->GraphAreaY2-1; } - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $LastGoodY = NULL; $LastGoodX = NULL; $Points = ""; $Init = FALSE; - foreach($PosArray as $Key => $Y) - { - if ( $Y == VOID && $LastX != NULL && $LastY != NULL && $Points !="" ) - { - $Points[] = $LastX; $Points[] = $LastY; - $Points[] = $X; $Points[] = $LastY; - $Points[] = $X; $Points[] = $YZero; - $this->drawPolygon($Points,$Color); - $Points = ""; - } - - if ( $Y != VOID && $LastX != NULL && $LastY != NULL ) - { - if ( $Points == "") { $Points[] = $LastX; $Points[] = $YZero; } - $Points[] = $LastX; $Points[] = $LastY; - $Points[] = $X; $Points[] = $LastY; - $Points[] = $X; $Points[] = $Y; - } - - if ( $Y != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } - if ( $Y == VOID ) { $Y = NULL; } - - if ( !$Init && $ReCenter ) { $X = $X - $XStep/2; $Init = TRUE; } - $LastX = $X; $LastY = $Y; - if ( $LastX < $this->GraphAreaX1 + $XMargin ) { $LastX = $this->GraphAreaX1 + $XMargin; } - $X = $X + $XStep; - } - - if ( $ReCenter ) - { - $Points[] = $LastX+$XStep/2; $Points[] = $LastY; - $Points[] = $LastX+$XStep/2; $Points[] = $YZero; - } - else - { $Points[] = $LastX; $Points[] = $YZero; } - - $this->drawPolygon($Points,$Color); - } - else - { - if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } - if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } - - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $LastGoodY = NULL; $LastGoodX = NULL; $Points = ""; - foreach($PosArray as $Key => $X) - { - if ( $X == VOID && $LastX != NULL && $LastY != NULL && $Points !="" ) - { - $Points[] = $LastX; $Points[] = $LastY; - $Points[] = $LastX; $Points[] = $Y; - $Points[] = $YZero; $Points[] = $Y; - $this->drawPolygon($Points,$Color); - $Points = ""; - } - - if ( $X != VOID && $LastX != NULL && $LastY != NULL ) - { - if ( $Points == "") { $Points[] = $YZero; $Points[] = $LastY; } - $Points[] = $LastX; $Points[] = $LastY; - $Points[] = $LastX; $Points[] = $Y; - $Points[] = $X; $Points[] = $Y; - } - - if ( $X != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } - if ( $X == VOID ) { $X = NULL; } - - if ( $LastX == NULL && $ReCenter ) { $Y = $Y - $YStep/2; } - $LastX = $X; $LastY = $Y; - if ( $LastY < $this->GraphAreaY1 + $XMargin ) { $LastY = $this->GraphAreaY1 + $XMargin; } - $Y = $Y + $YStep; - } - - if ( $ReCenter ) - { - $Points[] = $LastX; $Points[] = $LastY+$YStep/2; - $Points[] = $YZero; $Points[] = $LastY+$YStep/2; - } - else - { $Points[] = $YZero; $Points[] = $LastY; } - - $this->drawPolygon($Points,$Color); - } - } - } - } - - /* Draw an area chart */ - function drawAreaChart($Format=NULL) - { - $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; - $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; - $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; - $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; - $ForceTransparency = isset($Format["ForceTransparency"]) ? $Format["ForceTransparency"] : 25; - $AroundZero = isset($Format["AroundZero"]) ? $Format["AroundZero"] : TRUE; - $Threshold = isset($Format["Threshold"]) ? $Format["Threshold"] : NULL; - - $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); - - if ( $Threshold != NULL ) - { - foreach($Threshold as $Key => $Params) - { - $Threshold[$Key]["MinX"] = $this->scaleComputeY($Params["Min"],array("AxisID"=>$Serie["Axis"])); - $Threshold[$Key]["MaxX"] = $this->scaleComputeY($Params["Max"],array("AxisID"=>$Serie["Axis"])); - } - } - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } - - $Areas = ""; $AreaID = 0; - $Areas[$AreaID][] = $this->GraphAreaX1 + $XMargin; - if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaY2-1; } - - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - foreach($PosArray as $Key => $Y) - { - if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) - { - if ( $Serie["Data"][$Key] > 0 ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } - $this->drawText($X,$Y-$Offset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); - } - - if ( $Y == VOID && isset($Areas[$AreaID]) ) - { - if($LastX == NULL) - { $Areas[$AreaID][] = $X; } - else - { $Areas[$AreaID][] = $LastX; } - - if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaY2-1; } - $AreaID++; - } - elseif ($Y != VOID) - { - if ( !isset($Areas[$AreaID]) ) - { - $Areas[$AreaID][] = $X; - if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaY2-1; } - } - - $Areas[$AreaID][] = $X; - $Areas[$AreaID][] = $Y; - } - - $LastX = $X; - $X = $X + $XStep; - } - $Areas[$AreaID][] = $LastX; - if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaY2-1; } - - /* Handle shadows in the areas */ - if ( $this->Shadow ) - { - $ShadowArea = ""; - foreach($Areas as $Key => $Points) - { - $ShadowArea[$Key] = ""; - foreach($Points as $Key2 => $Value) - { - if ( $Key2 % 2 == 0 ) - { $ShadowArea[$Key][] = $Value + $this->ShadowX; } - else - { $ShadowArea[$Key][] = $Value + $this->ShadowY; } - } - } - - foreach($ShadowArea as $Key => $Points) - $this->drawPolygonChart($Points,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); - } - - $Alpha = $ForceTransparency != NULL ? $ForceTransparency : $Alpha; - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Threshold"=>$Threshold); - - foreach($Areas as $Key => $Points) - $this->drawPolygonChart($Points,$Color); - } - else - { - if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } - if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } - - $Areas = ""; $AreaID = 0; - if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaX1+1; } - $Areas[$AreaID][] = $this->GraphAreaY1 + $XMargin; - - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - foreach($PosArray as $Key => $X) - { - if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) - { - if ( $Serie["Data"][$Key] > 0 ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } - $this->drawText($X+$Offset,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); - } - - if ( $X == VOID && isset($Areas[$AreaID]) ) - { - if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaX1+1; } - - if($LastY == NULL) - { $Areas[$AreaID][] = $Y; } - else - { $Areas[$AreaID][] = $LastY; } - - $AreaID++; - } - elseif ($X != VOID) - { - if ( !isset($Areas[$AreaID]) ) - { - if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaX1+1; } - $Areas[$AreaID][] = $Y; - } - - $Areas[$AreaID][] = $X; - $Areas[$AreaID][] = $Y; - } - - $LastX = $X; $LastY = $Y; - $Y = $Y + $YStep; - } - if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaX1+1; } - $Areas[$AreaID][] = $LastY; - - /* Handle shadows in the areas */ - if ( $this->Shadow ) - { - $ShadowArea = ""; - foreach($Areas as $Key => $Points) - { - $ShadowArea[$Key] = ""; - foreach($Points as $Key2 => $Value) - { - if ( $Key2 % 2 == 0 ) - { $ShadowArea[$Key][] = $Value + $this->ShadowX; } - else - { $ShadowArea[$Key][] = $Value + $this->ShadowY; } - } - } - - foreach($ShadowArea as $Key => $Points) - $this->drawPolygonChart($Points,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); - } - - $Alpha = $ForceTransparency != NULL ? $ForceTransparency : $Alpha; - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Threshold"=>$Threshold); - - foreach($Areas as $Key => $Points) - $this->drawPolygonChart($Points,$Color); - } - } - } - } - - - /* Draw a bar chart */ - function drawBarChart($Format=NULL) - { - $Floating0Serie = isset($Format["Floating0Serie"]) ? $Format["Floating0Serie"] : NULL; - $Floating0Value = isset($Format["Floating0Value"]) ? $Format["Floating0Value"] : NULL; - $Draw0Line = isset($Format["Draw0Line"]) ? $Format["Draw0Line"] : FALSE; - $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; - $DisplayOrientation = isset($Format["DisplayOrientation"]) ? $Format["DisplayOrientation"] : ORIENTATION_HORIZONTAL; - $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $DisplayFont = isset($Format["DisplaySize"]) ? $Format["DisplaySize"] : $this->FontName; - $DisplaySize = isset($Format["DisplaySize"]) ? $Format["DisplaySize"] : $this->FontSize; - $DisplayPos = isset($Format["DisplayPos"]) ? $Format["DisplayPos"] : LABEL_POS_OUTSIDE; - $DisplayShadow = isset($Format["DisplayShadow"]) ? $Format["DisplayShadow"] : TRUE; - $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; - $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; - $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; - $AroundZero = isset($Format["AroundZero"]) ? $Format["AroundZero"] : TRUE; - $Interleave = isset($Format["Interleave"]) ? $Format["Interleave"] : .5; - $Rounded = isset($Format["Rounded"]) ? $Format["Rounded"] : FALSE; - $RoundRadius = isset($Format["RoundRadius"]) ? $Format["RoundRadius"] : 4; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; - $Gradient = isset($Format["Gradient"]) ? $Format["Gradient"] : FALSE; - $GradientMode = isset($Format["GradientMode"]) ? $Format["GradientMode"] : GRADIENT_SIMPLE; - $GradientAlpha = isset($Format["GradientAlpha"]) ? $Format["GradientAlpha"] : 20; - $GradientStartR = isset($Format["GradientStartR"]) ? $Format["GradientStartR"] : 255; - $GradientStartG = isset($Format["GradientStartG"]) ? $Format["GradientStartG"] : 255; - $GradientStartB = isset($Format["GradientStartB"]) ? $Format["GradientStartB"] : 255; - $GradientEndR = isset($Format["GradientEndR"]) ? $Format["GradientEndR"] : 0; - $GradientEndG = isset($Format["GradientEndG"]) ? $Format["GradientEndG"] : 0; - $GradientEndB = isset($Format["GradientEndB"]) ? $Format["GradientEndB"] : 0; - $TxtMargin = isset($Format["TxtMargin"]) ? $Format["TxtMargin"] : 6; - $OverrideColors = isset($Format["OverrideColors"]) ? $Format["OverrideColors"] : NULL; - $OverrideSurrounding = isset($Format["OverrideSurrounding"]) ? $Format["OverrideSurrounding"] : 30; - $InnerSurrounding = isset($Format["InnerSurrounding"]) ? $Format["InnerSurrounding"] : NULL; - $InnerBorderR = isset($Format["InnerBorderR"]) ? $Format["InnerBorderR"] : -1; - $InnerBorderG = isset($Format["InnerBorderG"]) ? $Format["InnerBorderG"] : -1; - $InnerBorderB = isset($Format["InnerBorderB"]) ? $Format["InnerBorderB"] : -1; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - - $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - if ( $OverrideColors != NULL ) - { - $OverrideColors = $this->validatePalette($OverrideColors,$OverrideSurrounding); - $this->DataSet->saveExtendedData("Palette",$OverrideColors); - } - - $RestoreShadow = $this->Shadow; - - $SeriesCount = $this->countDrawableSeries(); - $CurrentSerie = 0; - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } - if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } - if ( $InnerSurrounding != NULL ) { $InnerBorderR = $R+$InnerSurrounding; $InnerBorderG = $G+$InnerSurrounding; $InnerBorderB = $B+$InnerSurrounding; } - if ( $InnerBorderR == -1 ) { $InnerColor = NULL; } else { $InnerColor = array("R"=>$InnerBorderR,"G"=>$InnerBorderG,"B"=>$InnerBorderB); } - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB); - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - - if ( $Floating0Value != NULL ) - { $YZero = $this->scaleComputeY($Floating0Value,array("AxisID"=>$Serie["Axis"])); } - else - { $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); } - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } - if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } - - if ( $XDivs == 0 ) { $XStep = 0; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; - - if ( $AroundZero ) { $Y1 = $YZero; } else { $Y1 = $this->GraphAreaY2-1; } - if ( $XDivs == 0 ) { $XSize = ($this->GraphAreaX2-$this->GraphAreaX1)/($SeriesCount+$Interleave); } else { $XSize = ($XStep / ($SeriesCount+$Interleave) ); } - - $XOffset = -($XSize*$SeriesCount)/2 + $CurrentSerie * $XSize; - if ( $X + $XOffset <= $this->GraphAreaX1 ) { $XOffset = $this->GraphAreaX1 - $X + 1 ; } - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = $XOffset + $XSize / 2; - - if ( $Rounded || $BorderR != -1) { $XSpace = 1; } else { $XSpace = 0; } - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - - $ID = 0; - foreach($PosArray as $Key => $Y2) - { - if ( $Floating0Serie != NULL ) - { - if ( isset($Data["Series"][$Floating0Serie]["Data"][$Key]) ) - { $Value = $Data["Series"][$Floating0Serie]["Data"][$Key]; } - else - { $Value = 0; } - - $YZero = $this->scaleComputeY($Value,array("AxisID"=>$Serie["Axis"])); - if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } - if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } - - if ( $AroundZero ) { $Y1 = $YZero; } else { $Y1 = $this->GraphAreaY2-1; } - } - - if ( $OverrideColors != NULL ) - { if ( isset($OverrideColors[$ID]) ) { $Color = array("R"=>$OverrideColors[$ID]["R"],"G"=>$OverrideColors[$ID]["G"],"B"=>$OverrideColors[$ID]["B"],"Alpha"=>$OverrideColors[$ID]["Alpha"],"BorderR"=>$OverrideColors[$ID]["BorderR"],"BorderG"=>$OverrideColors[$ID]["BorderG"],"BorderB"=>$OverrideColors[$ID]["BorderB"]); } else { $Color = $this->getRandomColor(); } } - - if ( $Y2 != VOID ) - { - $BarHeight = $Y1 - $Y2; - - if ( $Serie["Data"][$Key] == 0 ) - { - $this->drawLine($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y1,$Color); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X+$XOffset+$XSpace).",".floor($Y1-1).",".floor($X+$XOffset+$XSize-$XSpace).",".floor($Y1+1),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - else - { - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X+$XOffset+$XSpace).",".floor($Y1).",".floor($X+$XOffset+$XSize-$XSpace).",".floor($Y2),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $Rounded ) - $this->drawRoundedFilledRectangle($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y2,$RoundRadius,$Color); - else - { - $this->drawFilledRectangle($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y2,$Color); - - if ( $InnerColor != NULL ) { $this->drawRectangle($X+$XOffset+$XSpace+1,min($Y1,$Y2)+1,$X+$XOffset+$XSize-$XSpace-1,max($Y1,$Y2)-1,$InnerColor); } - - if ( $Gradient ) - { - $this->Shadow = FALSE; - - if ( $GradientMode == GRADIENT_SIMPLE ) - { - if ( $Serie["Data"][$Key] >= 0 ) - $GradienColor = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); - else - $GradienColor = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); - - $this->drawGradientArea($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y2,DIRECTION_VERTICAL,$GradienColor); - } - elseif ( $GradientMode == GRADIENT_EFFECT_CAN ) - { - $GradienColor1 = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); - $GradienColor2 = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); - $XSpan = floor($XSize / 3); - - $this->drawGradientArea($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSpan-$XSpace,$Y2,DIRECTION_HORIZONTAL,$GradienColor1); - $this->drawGradientArea($X+$XOffset+$XSpan+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y2,DIRECTION_HORIZONTAL,$GradienColor2); - } - $this->Shadow = $RestoreShadow; - } - } - - if ( $Draw0Line ) - { - $Line0Color = array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20); - - if ( abs($Y1 - $Y2) > 3 ) { $Line0Width = 3; } else { $Line0Width = 1; } - if ( $Y1 - $Y2 < 0 ) { $Line0Width = -$Line0Width; } - - $this->drawFilledRectangle($X+$XOffset+$XSpace,floor($Y1),$X+$XOffset+$XSize-$XSpace,floor($Y1)-$Line0Width,$Line0Color); - $this->drawLine($X+$XOffset+$XSpace,floor($Y1),$X+$XOffset+$XSize-$XSpace,floor($Y1),$Line0Color); - } - } - - if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) - { - if ( $DisplayShadow ) { $this->Shadow = TRUE; } - - $Caption = $this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit); - $TxtPos = $this->getTextBox(0,0,$DisplayFont,$DisplaySize,90,$Caption); - $TxtHeight = $TxtPos[0]["Y"] - $TxtPos[1]["Y"] + $TxtMargin; - - if ( $DisplayPos == LABEL_POS_INSIDE && abs($TxtHeight) < abs($BarHeight) ) - { - $CenterX = (($X+$XOffset+$XSize-$XSpace)-($X+$XOffset+$XSpace))/2 + $X+$XOffset+$XSpace; - $CenterY = ($Y2-$Y1)/2 + $Y1; - - $this->drawText($CenterX,$CenterY,$Caption,array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"Angle"=>90)); - } - else - { - if ( $Serie["Data"][$Key] >= 0 ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } - $this->drawText($X+$XOffset+$XSize/2,$Y2-$Offset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align,"FontSize"=>$DisplaySize)); - } - - $this->Shadow = $RestoreShadow; - } - } - - $X = $X + $XStep; - $ID++; - } - } - else - { - if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } - if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } - - if ( $XDivs == 0 ) { $YStep = 0; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - - $Y = $this->GraphAreaY1 + $XMargin; - - if ( $AroundZero ) { $X1 = $YZero; } else { $X1 = $this->GraphAreaX1+1; } - if ( $XDivs == 0 ) { $YSize = ($this->GraphAreaY2-$this->GraphAreaY1)/($SeriesCount+$Interleave); } else { $YSize = ($YStep / ($SeriesCount+$Interleave) ); } - - $YOffset = -($YSize*$SeriesCount)/2 + $CurrentSerie * $YSize; - if ( $Y + $YOffset <= $this->GraphAreaY1 ) { $YOffset = $this->GraphAreaY1 - $Y + 1 ; } - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = $YOffset + $YSize / 2; - - if ( $Rounded || $BorderR != -1 ) { $YSpace = 1; } else { $YSpace = 0; } - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - - $ID = 0 ; - foreach($PosArray as $Key => $X2) - { - if ( $Floating0Serie != NULL ) - { - if ( isset($Data["Series"][$Floating0Serie]["Data"][$Key]) ) - $Value = $Data["Series"][$Floating0Serie]["Data"][$Key]; - else { $Value = 0; } - - $YZero = $this->scaleComputeY($Value,array("AxisID"=>$Serie["Axis"])); - if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } - if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } - if ( $AroundZero ) { $X1 = $YZero; } else { $X1 = $this->GraphAreaX1+1; } - } - - if ( $OverrideColors != NULL ) - { if ( isset($OverrideColors[$ID]) ) { $Color = array("R"=>$OverrideColors[$ID]["R"],"G"=>$OverrideColors[$ID]["G"],"B"=>$OverrideColors[$ID]["B"],"Alpha"=>$OverrideColors[$ID]["Alpha"],"BorderR"=>$OverrideColors[$ID]["BorderR"],"BorderG"=>$OverrideColors[$ID]["BorderG"],"BorderB"=>$OverrideColors[$ID]["BorderB"]); } else { $Color = $this->getRandomColor(); } } - - if ( $X2 != VOID ) - { - $BarWidth = $X2 - $X1; - - if ( $Serie["Data"][$Key] == 0 ) - { - $this->drawLine($X1,$Y+$YOffset+$YSpace,$X1,$Y+$YOffset+$YSize-$YSpace,$Color); - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X1-1).",".floor($Y+$YOffset+$YSpace).",".floor($X1+1).",".floor($Y+$YOffset+$YSize-$YSpace),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - } - else - { - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X1).",".floor($Y+$YOffset+$YSpace).",".floor($X2).",".floor($Y+$YOffset+$YSize-$YSpace),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $Rounded ) - $this->drawRoundedFilledRectangle($X1+1,$Y+$YOffset+$YSpace,$X2,$Y+$YOffset+$YSize-$YSpace,$RoundRadius,$Color); - else - { - $this->drawFilledRectangle($X1,$Y+$YOffset+$YSpace,$X2,$Y+$YOffset+$YSize-$YSpace,$Color); - - if ( $InnerColor != NULL ) { $this->drawRectangle(min($X1,$X2)+1,$Y+$YOffset+$YSpace+1,max($X1,$X2)-1,$Y+$YOffset+$YSize-$YSpace-1,$InnerColor); } - - if ( $Gradient ) - { - $this->Shadow = FALSE; - - if ( $GradientMode == GRADIENT_SIMPLE ) - { - if ( $Serie["Data"][$Key] >= 0 ) - $GradienColor = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); - else - $GradienColor = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); - - $this->drawGradientArea($X1,$Y+$YOffset+$YSpace,$X2,$Y+$YOffset+$YSize-$YSpace,DIRECTION_HORIZONTAL,$GradienColor); - } - elseif ( $GradientMode == GRADIENT_EFFECT_CAN ) - { - $GradienColor1 = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); - $GradienColor2 = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); - $YSpan = floor($YSize / 3); - - $this->drawGradientArea($X1,$Y+$YOffset+$YSpace,$X2,$Y+$YOffset+$YSpan-$YSpace,DIRECTION_VERTICAL,$GradienColor1); - $this->drawGradientArea($X1,$Y+$YOffset+$YSpan,$X2,$Y+$YOffset+$YSize-$YSpace,DIRECTION_VERTICAL,$GradienColor2); - } - $this->Shadow = $RestoreShadow; - } - } - - if ( $Draw0Line ) - { - $Line0Color = array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20); - - if ( abs($X1 - $X2) > 3 ) { $Line0Width = 3; } else { $Line0Width = 1; } - if ( $X2 - $X1 < 0 ) { $Line0Width = -$Line0Width; } - - $this->drawFilledRectangle(floor($X1),$Y+$YOffset+$YSpace,floor($X1)+$Line0Width,$Y+$YOffset+$YSize-$YSpace,$Line0Color); - $this->drawLine(floor($X1),$Y+$YOffset+$YSpace,floor($X1),$Y+$YOffset+$YSize-$YSpace,$Line0Color); - } - } - - if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) - { - if ( $DisplayShadow ) { $this->Shadow = TRUE; } - - $Caption = $this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit); - $TxtPos = $this->getTextBox(0,0,$DisplayFont,$DisplaySize,0,$Caption); - $TxtWidth = $TxtPos[1]["X"] - $TxtPos[0]["X"] + $TxtMargin; - - if ( $DisplayPos == LABEL_POS_INSIDE && abs($TxtWidth) < abs($BarWidth) ) - { - $CenterX = ($X2-$X1)/2 + $X1; - $CenterY = (($Y+$YOffset+$YSize-$YSpace)-($Y+$YOffset+$YSpace))/2 + ($Y+$YOffset+$YSpace); - - $this->drawText($CenterX,$CenterY,$Caption,array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize)); - } - else - { - if ( $Serie["Data"][$Key] >= 0 ) { $Align = TEXT_ALIGN_MIDDLELEFT; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_MIDDLERIGHT; $Offset = -$DisplayOffset; } - $this->drawText($X2+$Offset,$Y+$YOffset+$YSize/2,$Caption,array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align,"FontSize"=>$DisplaySize)); - } - - $this->Shadow = $RestoreShadow; - } - } - $Y = $Y + $YStep; - $ID++; - } - } - $CurrentSerie++; - } - } - } - - /* Draw a bar chart */ - function drawStackedBarChart($Format=NULL) - { - $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; - $DisplayOrientation = isset($Format["DisplayOrientation"]) ? $Format["DisplayOrientation"] : ORIENTATION_AUTO; - $DisplayRound = isset($Format["DisplayRound"]) ? $Format["DisplayRound"] : 0; - $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; - $DisplayFont = isset($Format["DisplayFont"]) ? $Format["DisplayFont"] : $this->FontName; - $DisplaySize = isset($Format["DisplaySize"]) ? $Format["DisplaySize"] : $this->FontSize; - $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; - $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; - $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; - $Interleave = isset($Format["Interleave"]) ? $Format["Interleave"] : .5; - $Rounded = isset($Format["Rounded"]) ? $Format["Rounded"] : FALSE; - $RoundRadius = isset($Format["RoundRadius"]) ? $Format["RoundRadius"] : 4; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; - $Gradient = isset($Format["Gradient"]) ? $Format["Gradient"] : FALSE; - $GradientMode = isset($Format["GradientMode"]) ? $Format["GradientMode"] : GRADIENT_SIMPLE; - $GradientAlpha = isset($Format["GradientAlpha"]) ? $Format["GradientAlpha"] : 20; - $GradientStartR = isset($Format["GradientStartR"]) ? $Format["GradientStartR"] : 255; - $GradientStartG = isset($Format["GradientStartG"]) ? $Format["GradientStartG"] : 255; - $GradientStartB = isset($Format["GradientStartB"]) ? $Format["GradientStartB"] : 255; - $GradientEndR = isset($Format["GradientEndR"]) ? $Format["GradientEndR"] : 0; - $GradientEndG = isset($Format["GradientEndG"]) ? $Format["GradientEndG"] : 0; - $GradientEndB = isset($Format["GradientEndB"]) ? $Format["GradientEndB"] : 0; - $InnerSurrounding = isset($Format["InnerSurrounding"]) ? $Format["InnerSurrounding"] : NULL; - $InnerBorderR = isset($Format["InnerBorderR"]) ? $Format["InnerBorderR"] : -1; - $InnerBorderG = isset($Format["InnerBorderG"]) ? $Format["InnerBorderG"] : -1; - $InnerBorderB = isset($Format["InnerBorderB"]) ? $Format["InnerBorderB"] : -1; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - $FontFactor = isset($Format["FontFactor"]) ? $Format["FontFactor"] : 8; - - $this->LastChartLayout = CHART_LAST_LAYOUT_STACKED; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - $RestoreShadow = $this->Shadow; - - $LastX = ""; $LastY = ""; - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; - if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = 255; $DisplayG = 255; $DisplayB = 255; } - if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } - if ( $InnerSurrounding != NULL ) { $InnerBorderR = $R+$InnerSurrounding; $InnerBorderG = $G+$InnerSurrounding; $InnerBorderB = $B+$InnerSurrounding; } - if ( $InnerBorderR == -1 ) { $InnerColor = NULL; } else { $InnerColor = array("R"=>$InnerBorderR,"G"=>$InnerBorderG,"B"=>$InnerBorderB); } - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"]),TRUE); - $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; - - $Color = array("TransCorner"=>TRUE,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB); - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } - if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } - - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; - - $XSize = ($XStep / (1+$Interleave) ); - $XOffset = -($XSize/2); - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - foreach($PosArray as $Key => $Height) - { - if ( $Height != VOID && $Serie["Data"][$Key] != 0 ) - { - if ( $Serie["Data"][$Key] > 0 ) { $Pos = "+"; } else { $Pos = "-"; } - - if ( !isset($LastY[$Key] ) ) { $LastY[$Key] = ""; } - if ( !isset($LastY[$Key][$Pos] ) ) { $LastY[$Key][$Pos] = $YZero; } - - $Y1 = $LastY[$Key][$Pos]; - $Y2 = $Y1 - $Height; - - if ( ($Rounded || $BorderR != -1) && ($Pos == "+" && $Y1 != $YZero) ) { $YSpaceUp = 1; } else { $YSpaceUp = 0; } - if ( ($Rounded || $BorderR != -1) && ($Pos == "-" && $Y1 != $YZero) ) { $YSpaceDown = 1; } else { $YSpaceDown = 0; } - - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X+$XOffset).",".floor($Y1-$YSpaceUp+$YSpaceDown).",".floor($X+$XOffset+$XSize).",".floor($Y2),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $Rounded ) - $this->drawRoundedFilledRectangle($X+$XOffset,$Y1-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSize,$Y2,$RoundRadius,$Color); - else - { - $this->drawFilledRectangle($X+$XOffset,$Y1-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSize,$Y2,$Color); - - if ( $InnerColor != NULL ) { $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; $this->drawRectangle(min($X+$XOffset+1,$X+$XOffset+$XSize),min($Y1-$YSpaceUp+$YSpaceDown,$Y2)+1,max($X+$XOffset+1,$X+$XOffset+$XSize)-1,max($Y1-$YSpaceUp+$YSpaceDown,$Y2)-1,$InnerColor); $this->Shadow = $RestoreShadow;} - - if ( $Gradient ) - { - $this->Shadow = FALSE; - - if ( $GradientMode == GRADIENT_SIMPLE ) - { - $GradientColor = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); - $this->drawGradientArea($X+$XOffset,$Y1-1-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSize,$Y2+1,DIRECTION_VERTICAL,$GradientColor); - } - elseif ( $GradientMode == GRADIENT_EFFECT_CAN ) - { - $GradientColor1 = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); - $GradientColor2 = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); - $XSpan = floor($XSize / 3); - - $this->drawGradientArea($X+$XOffset-.5,$Y1-.5-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSpan,$Y2+.5,DIRECTION_HORIZONTAL,$GradientColor1); - $this->drawGradientArea($X+$XSpan+$XOffset-.5,$Y1-.5-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSize,$Y2+.5,DIRECTION_HORIZONTAL,$GradientColor2); - } - $this->Shadow = $RestoreShadow; - } - } - - if ( $DisplayValues ) - { - $BarHeight = abs($Y2-$Y1)-2; - $BarWidth = $XSize+($XOffset/2)-$FontFactor; - - $Caption = $this->scaleFormat(round($Serie["Data"][$Key],$DisplayRound),$Mode,$Format,$Unit); - $TxtPos = $this->getTextBox(0,0,$DisplayFont,$DisplaySize,0,$Caption); - $TxtHeight = abs($TxtPos[2]["Y"] - $TxtPos[0]["Y"]); - $TxtWidth = abs($TxtPos[1]["X"] - $TxtPos[0]["X"]); - - $XCenter = ( ($X+$XOffset+$XSize) - ($X+$XOffset) ) / 2 + $X+$XOffset; - $YCenter = ( ($Y2) - ($Y1-$YSpaceUp+$YSpaceDown) ) / 2 + $Y1-$YSpaceUp+$YSpaceDown; - - $Done = FALSE; - if ( $DisplayOrientation == ORIENTATION_HORIZONTAL || $DisplayOrientation == ORIENTATION_AUTO ) - { - if ( $TxtHeight < $BarHeight && $TxtWidth < $BarWidth ) - { - $this->drawText($XCenter,$YCenter,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"FontName"=>$DisplayFont)); - $Done = TRUE; - } - } - - if ( $DisplayOrientation == ORIENTATION_VERTICAL || ( $DisplayOrientation == ORIENTATION_AUTO && !$Done) ) - { - if ( $TxtHeight < $BarWidth && $TxtWidth < $BarHeight ) - $this->drawText($XCenter,$YCenter,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Angle"=>90,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"FontName"=>$DisplayFont)); - } - } - - $LastY[$Key][$Pos] = $Y2; - } - - $X = $X + $XStep; - } - } - else - { - if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } - if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } - - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; - - $YSize = $YStep / (1+$Interleave); - $YOffset = -($YSize/2); - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - foreach($PosArray as $Key => $Width) - { - if ( $Width != VOID && $Serie["Data"][$Key] != 0 ) - { - if ( $Serie["Data"][$Key] > 0 ) { $Pos = "+"; } else { $Pos = "-"; } - - if ( !isset($LastX[$Key] ) ) { $LastX[$Key] = ""; } - if ( !isset($LastX[$Key][$Pos] ) ) { $LastX[$Key][$Pos] = $YZero; } - - $X1 = $LastX[$Key][$Pos]; - $X2 = $X1 + $Width; - - if ( ($Rounded || $BorderR != -1) && ($Pos == "+" && $X1 != $YZero) ) { $XSpaceLeft = 2; } else { $XSpaceLeft = 0; } - if ( ($Rounded || $BorderR != -1) && ($Pos == "-" && $X1 != $YZero) ) { $XSpaceRight = 2; } else { $XSpaceRight = 0; } - - if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X1+$XSpaceLeft).",".floor($Y+$YOffset).",".floor($X2-$XSpaceRight).",".floor($Y+$YOffset+$YSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } - - if ( $Rounded ) - $this->drawRoundedFilledRectangle($X1+$XSpaceLeft,$Y+$YOffset,$X2-$XSpaceRight,$Y+$YOffset+$YSize,$RoundRadius,$Color); - else - { - $this->drawFilledRectangle($X1+$XSpaceLeft,$Y+$YOffset,$X2-$XSpaceRight,$Y+$YOffset+$YSize,$Color); - - if ( $InnerColor != NULL ) { $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; $this->drawRectangle(min($X1+$XSpaceLeft,$X2-$XSpaceRight)+1,min($Y+$YOffset,$Y+$YOffset+$YSize)+1,max($X1+$XSpaceLeft,$X2-$XSpaceRight)-1,max($Y+$YOffset,$Y+$YOffset+$YSize)-1,$InnerColor); $this->Shadow = $RestoreShadow;} - - if ( $Gradient ) - { - $this->Shadow = FALSE; - - if ( $GradientMode == GRADIENT_SIMPLE ) - { - $GradientColor = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); - $this->drawGradientArea($X1+$XSpaceLeft,$Y+$YOffset,$X2-$XSpaceRight,$Y+$YOffset+$YSize,DIRECTION_HORIZONTAL,$GradientColor); - } - elseif ( $GradientMode == GRADIENT_EFFECT_CAN ) - { - $GradientColor1 = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); - $GradientColor2 = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); - $YSpan = floor($YSize / 3); - - $this->drawGradientArea($X1+$XSpaceLeft,$Y+$YOffset,$X2-$XSpaceRight,$Y+$YOffset+$YSpan,DIRECTION_VERTICAL,$GradientColor1); - $this->drawGradientArea($X1+$XSpaceLeft,$Y+$YOffset+$YSpan,$X2-$XSpaceRight,$Y+$YOffset+$YSize,DIRECTION_VERTICAL,$GradientColor2); - } - $this->Shadow = $RestoreShadow; - } - } - - if ( $DisplayValues ) - { - $BarWidth = abs($X2-$X1)-$FontFactor; - $BarHeight = $YSize+($YOffset/2)-$FontFactor/2; - $Caption = $this->scaleFormat(round($Serie["Data"][$Key],$DisplayRound),$Mode,$Format,$Unit); - $TxtPos = $this->getTextBox(0,0,$DisplayFont,$DisplaySize,0,$Caption); - $TxtHeight = abs($TxtPos[2]["Y"] - $TxtPos[0]["Y"]); - $TxtWidth = abs($TxtPos[1]["X"] - $TxtPos[0]["X"]); - - $XCenter = ( $X2 - $X1 ) / 2 + $X1; - $YCenter = ( ($Y+$YOffset+$YSize) - ($Y+$YOffset) ) / 2 + $Y+$YOffset; - - $Done = FALSE; - if ( $DisplayOrientation == ORIENTATION_HORIZONTAL || $DisplayOrientation == ORIENTATION_AUTO ) - { - if ( $TxtHeight < $BarHeight && $TxtWidth < $BarWidth ) - { - $this->drawText($XCenter,$YCenter,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"FontName"=>$DisplayFont)); - $Done = TRUE; - } - } - - if ( $DisplayOrientation == ORIENTATION_VERTICAL || ( $DisplayOrientation == ORIENTATION_AUTO && !$Done) ) - { - if ( $TxtHeight < $BarWidth && $TxtWidth < $BarHeight ) - $this->drawText($XCenter,$YCenter,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Angle"=>90,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"FontName"=>$DisplayFont)); - } - } - - $LastX[$Key][$Pos] = $X2; - } - - $Y = $Y + $YStep; - } - } - } - } - } - - /* Draw a stacked area chart */ - function drawStackedAreaChart($Format=NULL) - { - $DrawLine = isset($Format["DrawLine"]) ? $Format["DrawLine"] : FALSE; - $LineSurrounding = isset($Format["LineSurrounding"]) ? $Format["LineSurrounding"] : NULL; - $LineR = isset($Format["LineR"]) ? $Format["LineR"] : VOID; - $LineG = isset($Format["LineG"]) ? $Format["LineG"] : VOID; - $LineB = isset($Format["LineB"]) ? $Format["LineB"] : VOID; - $LineAlpha = isset($Format["LineAlpha"]) ? $Format["LineAlpha"] : 100; - $DrawPlot = isset($Format["DrawPlot"]) ? $Format["DrawPlot"] : FALSE; - $PlotRadius = isset($Format["PlotRadius"]) ? $Format["PlotRadius"] : 2; - $PlotBorder = isset($Format["PlotBorder"]) ? $Format["PlotBorder"] : 1; - $PlotBorderSurrounding = isset($Format["PlotBorderSurrounding"]) ? $Format["PlotBorderSurrounding"] : NULL; - $PlotBorderR = isset($Format["PlotBorderR"]) ? $Format["PlotBorderR"] : 0; - $PlotBorderG = isset($Format["PlotBorderG"]) ? $Format["PlotBorderG"] : 0; - $PlotBorderB = isset($Format["PlotBorderB"]) ? $Format["PlotBorderB"] : 0; - $PlotBorderAlpha = isset($Format["PlotBorderAlpha"]) ? $Format["PlotBorderAlpha"] : 50; - $ForceTransparency = isset($Format["ForceTransparency"]) ? $Format["ForceTransparency"] : NULL; - - $this->LastChartLayout = CHART_LAST_LAYOUT_STACKED; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - $RestoreShadow = $this->Shadow; - $this->Shadow = FALSE; - - /* Build the offset data series */ - $OffsetData = ""; - $OverallOffset = ""; - $SerieOrder = ""; - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $SerieOrder[] = $SerieName; - - foreach($Serie["Data"] as $Key => $Value) - { - if ( $Value == VOID ) { $Value = 0; } - if ($Value >= 0) { $Sign = "+"; } else { $Sign = "-"; } - if ( !isset($OverallOffset[$Key]) || !isset($OverallOffset[$Key][$Sign]) ) { $OverallOffset[$Key][$Sign] = 0; } - - if ( $Sign == "+" ) - { $Data["Series"][$SerieName]["Data"][$Key] = $Value + $OverallOffset[$Key][$Sign]; } - else - { $Data["Series"][$SerieName]["Data"][$Key] = $Value - $OverallOffset[$Key][$Sign]; } - - $OverallOffset[$Key][$Sign] = $OverallOffset[$Key][$Sign] + abs($Value); - } - } - } - $SerieOrder = array_reverse($SerieOrder); - - $LastX = ""; $LastY = ""; - foreach($SerieOrder as $Key => $SerieName) - { - $Serie = $Data["Series"][$SerieName]; - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; - if ( $ForceTransparency != NULL ) { $Alpha = $ForceTransparency; } - - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); - - if ( $LineSurrounding != NULL ) - $LineColor = array("R"=>$R+$LineSurrounding,"G"=>$G+$LineSurrounding,"B"=>$B+$LineSurrounding,"Alpha"=>$Alpha); - elseif ( $LineR != VOID ) - $LineColor = array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha); - else - $LineColor = $Color; - - if ( $PlotBorderSurrounding != NULL ) - $PlotBorderColor = array("R"=>$R+$PlotBorderSurrounding,"G"=>$G+$PlotBorderSurrounding,"B"=>$B+$PlotBorderSurrounding,"Alpha"=>$PlotBorderAlpha); - else - $PlotBorderColor = array("R"=>$PlotBorderR,"G"=>$PlotBorderG,"B"=>$PlotBorderB,"Alpha"=>$PlotBorderAlpha); - - $AxisID = $Serie["Axis"]; - $Mode = $Data["Axis"][$AxisID]["Display"]; - $Format = $Data["Axis"][$AxisID]["Format"]; - $Unit = $Data["Axis"][$AxisID]["Unit"]; - - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"]),TRUE); - $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); - - $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } - if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } - - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - - $Plots = ""; $Plots[] = $X; $Plots[] = $YZero; - foreach($PosArray as $Key => $Height) - { - if ( $Height != VOID ) { $Plots[] = $X; $Plots[] = $YZero-$Height; } - $X = $X + $XStep; - } - $Plots[] = $X-$XStep; $Plots[] = $YZero; - - $this->drawPolygon($Plots,$Color); - - $this->Shadow = $RestoreShadow; - if ( $DrawLine ) { for($i=2; $i<=count($Plots)-6; $i=$i+2) { $this->drawLine($Plots[$i],$Plots[$i+1],$Plots[$i+2],$Plots[$i+3],$LineColor); } } - if ( $DrawPlot ) - { - for($i=2; $i<=count($Plots)-4; $i=$i+2) - { - if ( $PlotBorder != 0 ) - { $this->drawFilledCircle($Plots[$i],$Plots[$i+1],$PlotRadius+$PlotBorder,$PlotBorderColor); } - - $this->drawFilledCircle($Plots[$i],$Plots[$i+1],$PlotRadius,$Color); - } - } - $this->Shadow = FALSE; - } - elseif ( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) - { - if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } - if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } - - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - - $Plots = ""; $Plots[] = $YZero; $Plots[] = $Y; - foreach($PosArray as $Key => $Height) - { - if ( $Height != VOID ) { $Plots[] = $YZero+$Height; $Plots[] = $Y; } - $Y = $Y + $YStep; - } - $Plots[] = $YZero; $Plots[] = $Y-$YStep; - - $this->drawPolygon($Plots,$Color); - - $this->Shadow = $RestoreShadow; - if ( $DrawLine ) { for($i=2; $i<=count($Plots)-6; $i=$i+2) { $this->drawLine($Plots[$i],$Plots[$i+1],$Plots[$i+2],$Plots[$i+3],$LineColor); } } - if ( $DrawPlot ) - { - for($i=2; $i<=count($Plots)-4; $i=$i+2) - { - if ( $PlotBorder != 0 ) - { $this->drawFilledCircle($Plots[$i],$Plots[$i+1],$PlotRadius+$PlotBorder,$PlotBorderColor); } - - $this->drawFilledCircle($Plots[$i],$Plots[$i+1],$PlotRadius,$Color); - } - } - $this->Shadow = FALSE; - } - } - } - $this->Shadow = $RestoreShadow; - } - - /* Returns a random color */ - function getRandomColor($Alpha=100) - { return(array("R"=>rand(0,255),"G"=>rand(0,255),"B"=>rand(0,255),"Alpha"=>$Alpha)); } - - /* Validate a palette */ - function validatePalette($Colors,$Surrounding=NULL) - { - $Result = ""; - - if ( !is_array($Colors) ) { return($this->getRandomColor()); } - - foreach($Colors as $Key => $Values) - { - if ( isset($Values["R"]) ) { $Result[$Key]["R"] = $Values["R"]; } else { $Result[$Key]["R"] = rand(0,255); } - if ( isset($Values["G"]) ) { $Result[$Key]["G"] = $Values["G"]; } else { $Result[$Key]["G"] = rand(0,255); } - if ( isset($Values["B"]) ) { $Result[$Key]["B"] = $Values["B"]; } else { $Result[$Key]["B"] = rand(0,255); } - if ( isset($Values["Alpha"]) ) { $Result[$Key]["Alpha"] = $Values["Alpha"]; } else { $Result[$Key]["Alpha"] = 100; } - - if ( $Surrounding != NULL ) - { - $Result[$Key]["BorderR"] = $Result[$Key]["R"] + $Surrounding; - $Result[$Key]["BorderG"] = $Result[$Key]["G"] + $Surrounding; - $Result[$Key]["BorderB"] = $Result[$Key]["B"] + $Surrounding; - } - else - { - if ( isset($Values["BorderR"]) ) { $Result[$Key]["BorderR"] = $Values["BorderR"]; } else { $Result[$Key]["BorderR"] = $Result[$Key]["R"]; } - if ( isset($Values["BorderG"]) ) { $Result[$Key]["BorderG"] = $Values["BorderG"]; } else { $Result[$Key]["BorderG"] = $Result[$Key]["G"]; } - if ( isset($Values["BorderB"]) ) { $Result[$Key]["BorderB"] = $Values["BorderB"]; } else { $Result[$Key]["BorderB"] = $Result[$Key]["B"]; } - if ( isset($Values["BorderAlpha"]) ) { $Result[$Key]["BorderAlpha"] = $Values["BorderAlpha"]; } else { $Result[$Key]["BorderAlpha"] = $Result[$Key]["Alpha"]; } - } - } - - return($Result); - } - - /* Draw the derivative chart associated to the data series */ - function drawDerivative($Format=NULL) - { - $Offset = isset($Format["Offset"]) ? $Format["Offset"] : 10; - $SerieSpacing = isset($Format["SerieSpacing"]) ? $Format["SerieSpacing"] : 3; - $DerivativeHeight = isset($Format["DerivativeHeight"]) ? $Format["DerivativeHeight"] : 4; - $ShadedSlopeBox = isset($Format["ShadedSlopeBox"]) ? $Format["ShadedSlopeBox"] : FALSE; - $DrawBackground = isset($Format["DrawBackground"]) ? $Format["DrawBackground"] : TRUE; - $BackgroundR = isset($Format["BackgroundR"]) ? $Format["BackgroundR"] : 255; - $BackgroundG = isset($Format["BackgroundG"]) ? $Format["BackgroundG"] : 255; - $BackgroundB = isset($Format["BackgroundB"]) ? $Format["BackgroundB"] : 255; - $BackgroundAlpha = isset($Format["BackgroundAlpha"]) ? $Format["BackgroundAlpha"] : 20; - $DrawBorder = isset($Format["DrawBorder"]) ? $Format["DrawBorder"] : TRUE; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 0; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 0; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 0; - $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : 100; - $Caption = isset($Format["Caption"]) ? $Format["Caption"] : TRUE; - $CaptionHeight = isset($Format["CaptionHeight"]) ? $Format["CaptionHeight"] : 10; - $CaptionWidth = isset($Format["CaptionWidth"]) ? $Format["CaptionWidth"] : 20; - $CaptionMargin = isset($Format["CaptionMargin"]) ? $Format["CaptionMargin"] : 4; - $CaptionLine = isset($Format["CaptionLine"]) ? $Format["CaptionLine"] : FALSE; - $CaptionBox = isset($Format["CaptionBox"]) ? $Format["CaptionBox"] : FALSE; - $CaptionBorderR = isset($Format["CaptionBorderR"]) ? $Format["CaptionBorderR"] : 0; - $CaptionBorderG = isset($Format["CaptionBorderG"]) ? $Format["CaptionBorderG"] : 0; - $CaptionBorderB = isset($Format["CaptionBorderB"]) ? $Format["CaptionBorderB"] : 0; - $CaptionFillR = isset($Format["CaptionFillR"]) ? $Format["CaptionFillR"] : 255; - $CaptionFillG = isset($Format["CaptionFillG"]) ? $Format["CaptionFillG"] : 255; - $CaptionFillB = isset($Format["CaptionFillB"]) ? $Format["CaptionFillB"] : 255; - $CaptionFillAlpha = isset($Format["CaptionFillAlpha"]) ? $Format["CaptionFillAlpha"] : 80; - $PositiveSlopeStartR = isset($Format["PositiveSlopeStartR"]) ? $Format["PositiveSlopeStartR"] : 184; - $PositiveSlopeStartG = isset($Format["PositiveSlopeStartG"]) ? $Format["PositiveSlopeStartG"] : 234; - $PositiveSlopeStartB = isset($Format["PositiveSlopeStartB"]) ? $Format["PositiveSlopeStartB"] : 88; - $PositiveSlopeEndR = isset($Format["PositiveSlopeStartR"]) ? $Format["PositiveSlopeStartR"] : 239; - $PositiveSlopeEndG = isset($Format["PositiveSlopeStartG"]) ? $Format["PositiveSlopeStartG"] : 31; - $PositiveSlopeEndB = isset($Format["PositiveSlopeStartB"]) ? $Format["PositiveSlopeStartB"] : 36; - $NegativeSlopeStartR = isset($Format["NegativeSlopeStartR"]) ? $Format["NegativeSlopeStartR"] : 184; - $NegativeSlopeStartG = isset($Format["NegativeSlopeStartG"]) ? $Format["NegativeSlopeStartG"] : 234; - $NegativeSlopeStartB = isset($Format["NegativeSlopeStartB"]) ? $Format["NegativeSlopeStartB"] : 88; - $NegativeSlopeEndR = isset($Format["NegativeSlopeStartR"]) ? $Format["NegativeSlopeStartR"] : 67; - $NegativeSlopeEndG = isset($Format["NegativeSlopeStartG"]) ? $Format["NegativeSlopeStartG"] : 124; - $NegativeSlopeEndB = isset($Format["NegativeSlopeStartB"]) ? $Format["NegativeSlopeStartB"] : 227; - - $Data = $this->DataSet->getData(); - - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - $YPos = $this->DataSet->Data["GraphArea"]["Y2"] + $Offset; - else - $XPos = $this->DataSet->Data["GraphArea"]["X2"] + $Offset; - - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; - - $AxisID = $Serie["Axis"]; - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $Caption ) - { - if ( $CaptionLine ) - { - $StartX = floor($this->GraphAreaX1-$CaptionWidth+$XMargin-$CaptionMargin); - $EndX = floor($this->GraphAreaX1-$CaptionMargin+$XMargin); - - $CaptionSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight); - if ( $CaptionBox ) { $this->drawFilledRectangle($StartX,$YPos,$EndX,$YPos+$CaptionHeight,array("R"=>$CaptionFillR,"G"=>$CaptionFillG,"B"=>$CaptionFillB,"BorderR"=>$CaptionBorderR,"BorderG"=>$CaptionBorderG,"BorderB"=>$CaptionBorderB,"Alpha"=>$CaptionFillAlpha)); } - $this->drawLine($StartX+2,$YPos+($CaptionHeight/2),$EndX-2,$YPos+($CaptionHeight/2),$CaptionSettings); - } - else - $this->drawFilledRectangle($this->GraphAreaX1-$CaptionWidth+$XMargin-$CaptionMargin,$YPos,$this->GraphAreaX1-$CaptionMargin+$XMargin,$YPos+$CaptionHeight,array("R"=>$R,"G"=>$G,"B"=>$B,"BorderR"=>$CaptionBorderR,"BorderG"=>$CaptionBorderG,"BorderB"=>$CaptionBorderB)); - } - - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; - - $TopY = $YPos + ($CaptionHeight/2) - ($DerivativeHeight/2); - $BottomY = $YPos + ($CaptionHeight/2) + ($DerivativeHeight/2); - - $StartX = floor($this->GraphAreaX1+$XMargin); - $EndX = floor($this->GraphAreaX2-$XMargin); - - if ( $DrawBackground ) { $this->drawFilledRectangle($StartX-1,$TopY-1,$EndX+1,$BottomY+1,array("R"=>$BackgroundR,"G"=>$BackgroundG,"B"=>$BackgroundB,"Alpha"=>$BackgroundAlpha)); } - if ( $DrawBorder ) { $this->drawRectangle($StartX-1,$TopY-1,$EndX+1,$BottomY+1,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); } - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - - $RestoreShadow = $this->Shadow; - $this->Shadow = FALSE; - - /* Determine the Max slope index */ - $LastX = NULL; $LastY = NULL; $MinSlope = 0; $MaxSlope = 1; - foreach($PosArray as $Key => $Y) - { - if ( $Y != VOID && $LastX != NULL ) - { $Slope = ($LastY - $Y); if ( $Slope > $MaxSlope ) { $MaxSlope = $Slope; } if ( $Slope < $MinSlope ) { $MinSlope = $Slope; } } - - if ( $Y == VOID ) - { $LastX = NULL; $LastY = NULL; } - else - { $LastX = $X; $LastY = $Y; } - } - - $LastX = NULL; $LastY = NULL; $LastColor = NULL; - foreach($PosArray as $Key => $Y) - { - if ( $Y != VOID && $LastY != NULL ) - { - $Slope = ($LastY - $Y); - - if ( $Slope >= 0 ) - { - $SlopeIndex = (100 / $MaxSlope) * $Slope; - $R = (($PositiveSlopeEndR - $PositiveSlopeStartR)/100)*$SlopeIndex+$PositiveSlopeStartR; - $G = (($PositiveSlopeEndG - $PositiveSlopeStartG)/100)*$SlopeIndex+$PositiveSlopeStartG; - $B = (($PositiveSlopeEndB - $PositiveSlopeStartB)/100)*$SlopeIndex+$PositiveSlopeStartB; - } - elseif ( $Slope < 0 ) - { - $SlopeIndex = (100 / abs($MinSlope)) * abs($Slope); - $R = (($NegativeSlopeEndR - $NegativeSlopeStartR)/100)*$SlopeIndex+$NegativeSlopeStartR; - $G = (($NegativeSlopeEndG - $NegativeSlopeStartG)/100)*$SlopeIndex+$NegativeSlopeStartG; - $B = (($NegativeSlopeEndB - $NegativeSlopeStartB)/100)*$SlopeIndex+$NegativeSlopeStartB; - } - - $Color = array("R"=>$R,"G"=>$G,"B"=>$B); - - if ( $ShadedSlopeBox && $LastColor != NULL ) // && $Slope != 0 - { - $GradientSettings = array("StartR"=>$LastColor["R"],"StartG"=>$LastColor["G"],"StartB"=>$LastColor["B"],"EndR"=>$R,"EndG"=>$G,"EndB"=>$B); - $this->drawGradientArea($LastX,$TopY,$X,$BottomY,DIRECTION_HORIZONTAL,$GradientSettings); - } - elseif ( !$ShadedSlopeBox || $LastColor == NULL ) // || $Slope == 0 - $this->drawFilledRectangle(floor($LastX),$TopY,floor($X),$BottomY,$Color); - - $LastColor = $Color; - } - - if ( $Y == VOID ) - { $LastY = NULL; } - else - { $LastX = $X; $LastY = $Y; } - - $X = $X + $XStep; - } - - $YPos = $YPos + $CaptionHeight + $SerieSpacing; - } - else - { - if ( $Caption ) - { - $StartY = floor($this->GraphAreaY1-$CaptionWidth+$XMargin-$CaptionMargin); - $EndY = floor($this->GraphAreaY1-$CaptionMargin+$XMargin); - if ( $CaptionLine ) - { - $CaptionSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight); - if ( $CaptionBox ) { $this->drawFilledRectangle($XPos,$StartY,$XPos+$CaptionHeight,$EndY,array("R"=>$CaptionFillR,"G"=>$CaptionFillG,"B"=>$CaptionFillB,"BorderR"=>$CaptionBorderR,"BorderG"=>$CaptionBorderG,"BorderB"=>$CaptionBorderB,"Alpha"=>$CaptionFillAlpha)); } - $this->drawLine($XPos+($CaptionHeight/2),$StartY+2,$XPos+($CaptionHeight/2),$EndY-2,$CaptionSettings); - } - else - $this->drawFilledRectangle($XPos,$StartY,$XPos+$CaptionHeight,$EndY,array("R"=>$R,"G"=>$G,"B"=>$B,"BorderR"=>$CaptionBorderR,"BorderG"=>$CaptionBorderG,"BorderB"=>$CaptionBorderB)); - } - - - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $XStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; - - $TopX = $XPos + ($CaptionHeight/2) - ($DerivativeHeight/2); - $BottomX = $XPos + ($CaptionHeight/2) + ($DerivativeHeight/2); - - $StartY = floor($this->GraphAreaY1+$XMargin); - $EndY = floor($this->GraphAreaY2-$XMargin); - - if ( $DrawBackground ) { $this->drawFilledRectangle($TopX-1,$StartY-1,$BottomX+1,$EndY+1,array("R"=>$BackgroundR,"G"=>$BackgroundG,"B"=>$BackgroundB,"Alpha"=>$BackgroundAlpha)); } - if ( $DrawBorder ) { $this->drawRectangle($TopX-1,$StartY-1,$BottomX+1,$EndY+1,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); } - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - - $RestoreShadow = $this->Shadow; - $this->Shadow = FALSE; - - /* Determine the Max slope index */ - $LastX = NULL; $LastY = NULL; $MinSlope = 0; $MaxSlope = 1; - foreach($PosArray as $Key => $X) - { - if ( $X != VOID && $LastX != NULL ) - { $Slope = ($X - $LastX); if ( $Slope > $MaxSlope ) { $MaxSlope = $Slope; } if ( $Slope < $MinSlope ) { $MinSlope = $Slope; } } - - if ( $X == VOID ) - { $LastX = NULL; } - else - { $LastX = $X; } - } - - $LastX = NULL; $LastY = NULL; $LastColor = NULL; - foreach($PosArray as $Key => $X) - { - if ( $X != VOID && $LastX != NULL ) - { - $Slope = ($X - $LastX); - - if ( $Slope >= 0 ) - { - $SlopeIndex = (100 / $MaxSlope) * $Slope; - $R = (($PositiveSlopeEndR - $PositiveSlopeStartR)/100)*$SlopeIndex+$PositiveSlopeStartR; - $G = (($PositiveSlopeEndG - $PositiveSlopeStartG)/100)*$SlopeIndex+$PositiveSlopeStartG; - $B = (($PositiveSlopeEndB - $PositiveSlopeStartB)/100)*$SlopeIndex+$PositiveSlopeStartB; - } - elseif ( $Slope < 0 ) - { - $SlopeIndex = (100 / abs($MinSlope)) * abs($Slope); - $R = (($NegativeSlopeEndR - $NegativeSlopeStartR)/100)*$SlopeIndex+$NegativeSlopeStartR; - $G = (($NegativeSlopeEndG - $NegativeSlopeStartG)/100)*$SlopeIndex+$NegativeSlopeStartG; - $B = (($NegativeSlopeEndB - $NegativeSlopeStartB)/100)*$SlopeIndex+$NegativeSlopeStartB; - } - - $Color = array("R"=>$R,"G"=>$G,"B"=>$B); - - if ( $ShadedSlopeBox && $LastColor != NULL ) - { - $GradientSettings = array("StartR"=>$LastColor["R"],"StartG"=>$LastColor["G"],"StartB"=>$LastColor["B"],"EndR"=>$R,"EndG"=>$G,"EndB"=>$B); - - $this->drawGradientArea($TopX,$LastY,$BottomX,$Y,DIRECTION_VERTICAL,$GradientSettings); - } - elseif ( !$ShadedSlopeBox || $LastColor == NULL ) - $this->drawFilledRectangle($TopX,floor($LastY),$BottomX,floor($Y),$Color); - - $LastColor = $Color; - } - - if ( $X == VOID ) - { $LastX = NULL; } - else - { $LastX = $X; $LastY = $Y; } - - $Y = $Y + $XStep; - } - - $XPos = $XPos + $CaptionHeight + $SerieSpacing; - } - - $this->Shadow = $RestoreShadow; - } - } - } - - /* Draw the line of best fit */ - function drawBestFit($Format="") - { - $OverrideTicks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; - $OverrideR = isset($Format["R"]) ? $Format["R"] : VOID; - $OverrideG = isset($Format["G"]) ? $Format["G"] : VOID; - $OverrideB = isset($Format["B"]) ? $Format["B"] : VOID; - $OverrideAlpha = isset($Format["Alpha"]) ? $Format["Alpha"] : VOID; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - foreach($Data["Series"] as $SerieName => $Serie) - { - if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) - { - if ( $OverrideR != VOID && $OverrideG != VOID && $OverrideB != VOID ) { $R = $OverrideR; $G = $OverrideG; $B = $OverrideB; } else { $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; } - if ( $OverrideTicks == NULL ) { $Ticks = $Serie["Ticks"]; } else { $Ticks = $OverrideTicks; } - if ( $OverrideAlpha == VOID ) { $Alpha = $Serie["Color"]["Alpha"]; } else { $Alpha = $OverrideAlpha; } - - $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks); - - $AxisID = $Serie["Axis"]; - $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $Sxy = 0; $Sx = 0; $Sy = 0; $Sxx = 0; - foreach($PosArray as $Key => $Y) - { - if ( $Y != VOID ) - { - $Sxy = $Sxy + $X*$Y; - $Sx = $Sx + $X; - $Sy = $Sy + $Y; - $Sxx = $Sxx + $X*$X; - } - - $X = $X + $XStep; - } - $n = count($this->DataSet->stripVOID($PosArray)); //$n = count($PosArray); - $M = (($n*$Sxy)-($Sx*$Sy)) / (($n*$Sxx)-($Sx*$Sx)); - $B = (($Sy)-($M*$Sx))/($n); - - $X1 = $this->GraphAreaX1 + $XMargin; - $Y1 = $M * $X1 + $B; - $X2 = $this->GraphAreaX2 - $XMargin; - $Y2 = $M * $X2 + $B; - - if ( $Y1 < $this->GraphAreaY1 ) { $X1 = $X1 + ($this->GraphAreaY1-$Y1); $Y1 = $this->GraphAreaY1; } - if ( $Y1 > $this->GraphAreaY2 ) { $X1 = $X1 + ($Y1-$this->GraphAreaY2); $Y1 = $this->GraphAreaY2; } - if ( $Y2 < $this->GraphAreaY1 ) { $X2 = $X2 - ($this->GraphAreaY1-$Y2); $Y2 = $this->GraphAreaY1; } - if ( $Y2 > $this->GraphAreaY2 ) { $X2 = $X2 - ($Y2-$this->GraphAreaY2); $Y2 = $this->GraphAreaY2; } - - $this->drawLine($X1,$Y1,$X2,$Y2,$Color); - } - else - { - if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin; - - if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } - $Sxy = 0; $Sx = 0; $Sy = 0; $Sxx = 0; - foreach($PosArray as $Key => $X) - { - if ( $X != VOID ) - { - $Sxy = $Sxy + $X*$Y; - $Sx = $Sx + $Y; - $Sy = $Sy + $X; - $Sxx = $Sxx + $Y*$Y; - } - - $Y = $Y + $YStep; - } - $n = count($this->DataSet->stripVOID($PosArray)); //$n = count($PosArray); - $M = (($n*$Sxy)-($Sx*$Sy)) / (($n*$Sxx)-($Sx*$Sx)); - $B = (($Sy)-($M*$Sx))/($n); - - $Y1 = $this->GraphAreaY1 + $XMargin; - $X1 = $M * $Y1 + $B; - $Y2 = $this->GraphAreaY2 - $XMargin; - $X2 = $M * $Y2 + $B; - - if ( $X1 < $this->GraphAreaX1 ) { $Y1 = $Y1 + ($this->GraphAreaX1-$X1); $X1 = $this->GraphAreaX1; } - if ( $X1 > $this->GraphAreaX2 ) { $Y1 = $Y1 + ($X1-$this->GraphAreaX2); $X1 = $this->GraphAreaX2; } - if ( $X2 < $this->GraphAreaX1 ) { $Y2 = $Y2 - ($this->GraphAreaY1-$X2); $X2 = $this->GraphAreaX1; } - if ( $X2 > $this->GraphAreaX2 ) { $Y2 = $Y2 - ($X2-$this->GraphAreaX2); $X2 = $this->GraphAreaX2; } - - $this->drawLine($X1,$Y1,$X2,$Y2,$Color); - } - } - } - } - - /* Write labels */ - function writeLabel($SeriesName,$Indexes,$Format="") - { - $OverrideTitle = isset($Format["OverrideTitle"]) ? $Format["OverrideTitle"] : NULL; - $ForceLabels = isset($Format["ForceLabels"]) ? $Format["ForceLabels"] : NULL; - $DrawPoint = isset($Format["DrawPoint"]) ? $Format["DrawPoint"] : LABEL_POINT_BOX; - $DrawVerticalLine = isset($Format["DrawVerticalLine"]) ? $Format["DrawVerticalLine"] : FALSE; - $VerticalLineR = isset($Format["VerticalLineR"]) ? $Format["VerticalLineR"] : 0; - $VerticalLineG = isset($Format["VerticalLineG"]) ? $Format["VerticalLineG"] : 0; - $VerticalLineB = isset($Format["VerticalLineB"]) ? $Format["VerticalLineB"] : 0; - $VerticalLineAlpha = isset($Format["VerticalLineAlpha"]) ? $Format["VerticalLineAlpha"] : 40; - $VerticalLineTicks = isset($Format["VerticalLineTicks"]) ? $Format["VerticalLineTicks"] : 2; - - $Data = $this->DataSet->getData(); - list($XMargin,$XDivs) = $this->scaleGetXSettings(); - - if ( !is_array($Indexes) ) { $Index = $Indexes; $Indexes = ""; $Indexes[] = $Index; } - if ( !is_array($SeriesName) ) { $SerieName = $SeriesName; $SeriesName = ""; $SeriesName[] = $SerieName; } - if ( $ForceLabels != NULL && !is_array($ForceLabels) ) { $ForceLabel = $ForceLabels; $ForceLabels = ""; $ForceLabels[] = $ForceLabel; } - - foreach ($Indexes as $Key => $Index) - { - $Series = ""; - - if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) - { - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } - $X = $this->GraphAreaX1 + $XMargin + $Index * $XStep; - - if ( $DrawVerticalLine ) { $this->drawLine($X,$this->GraphAreaY1+$Data["YMargin"],$X,$this->GraphAreaY2-$Data["YMargin"],array("R"=>$VerticalLineR,"G"=>$VerticalLineG,"B"=>$VerticalLineB,"Alpha"=>$VerticalLineAlpha,"Ticks"=>$VerticalLineTicks)); } - - $MinY = $this->GraphAreaY2; - foreach ($SeriesName as $iKey => $SerieName) - { - if ( isset($Data["Series"][$SerieName]["Data"][$Index]) ) - { - $AxisID = $Data["Series"][$SerieName]["Axis"]; - - if ( $OverrideTitle != NULL) - $Description = $OverrideTitle; - elseif ( count($SeriesName) == 1 ) - { - if ( isset($Data["Abscissa"]) && isset($Data["Series"][$Data["Abscissa"]]["Data"][$Index]) ) - $Description = $Data["Series"][$SerieName]["Description"]." - ".$Data["Series"][$Data["Abscissa"]]["Data"][$Index]; - else - $Description = $Data["Series"][$SerieName]["Description"]; - } - elseif ( isset($Data["Abscissa"]) && isset($Data["Series"][$Data["Abscissa"]]["Data"][$Index]) ) - $Description = $Data["Series"][$Data["Abscissa"]]["Data"][$Index]; - - $AxisMode = $Data["Axis"][$AxisID]["Display"]; - $AxisFormat = $Data["Axis"][$AxisID]["Format"]; - $AxisUnit = $Data["Axis"][$AxisID]["Unit"]; - - $Serie = ""; - $Serie["R"] = $Data["Series"][$SerieName]["Color"]["R"]; - $Serie["G"] = $Data["Series"][$SerieName]["Color"]["G"]; - $Serie["B"] = $Data["Series"][$SerieName]["Color"]["B"]; - $Serie["Alpha"] = $Data["Series"][$SerieName]["Color"]["Alpha"]; - - if ( count($SeriesName) == 1 && isset($Data["Series"][$SerieName]["XOffset"]) ) - $SerieOffset = $Data["Series"][$SerieName]["XOffset"]; - else - $SerieOffset = 0; - - $Value = $Data["Series"][$SerieName]["Data"][$Index]; - if ( $Value == VOID ) { $Value = "NaN"; } - - if ( $ForceLabels != NULL ) - $Caption = isset($ForceLabels[$Key]) ? $ForceLabels[$Key] : "Not set"; - else - $Caption = $this->scaleFormat($Value,$AxisMode,$AxisFormat,$AxisUnit); - - if ( $this->LastChartLayout == CHART_LAST_LAYOUT_STACKED ) - { - if ( $Value >=0 ) { $LookFor = "+"; } else { $LookFor = "-"; } - - $Value = 0; $Done = FALSE; - foreach($Data["Series"] as $Name => $SerieLookup) - { - if ( $SerieLookup["isDrawable"] == TRUE && $Name != $Data["Abscissa"] && !$Done ) - { - if ( isset($Data["Series"][$Name]["Data"][$Index]) && $Data["Series"][$Name]["Data"][$Index] != VOID ) - { - if ($Data["Series"][$Name]["Data"][$Index] >= 0 && $LookFor == "+" ) { $Value = $Value + $Data["Series"][$Name]["Data"][$Index]; } - if ($Data["Series"][$Name]["Data"][$Index] < 0 && $LookFor == "-" ) { $Value = $Value - $Data["Series"][$Name]["Data"][$Index]; } - if ($Name == $SerieName ) { $Done = TRUE; } - } - } - } - } - - $X = floor($this->GraphAreaX1 + $XMargin + $Index * $XStep + $SerieOffset); - $Y = floor($this->scaleComputeY($Value,array("AxisID"=>$AxisID))); - - if ($Y < $MinY) { $MinY = $Y; } - - if ( $DrawPoint == LABEL_POINT_CIRCLE ) - $this->drawFilledCircle($X,$Y,3,array("R"=>255,"G"=>255,"B"=>255,"BorderR"=>0,"BorderG"=>0,"BorderB"=>0)); - elseif ( $DrawPoint == LABEL_POINT_BOX ) - $this->drawFilledRectangle($X-2,$Y-2,$X+2,$Y+2,array("R"=>255,"G"=>255,"B"=>255,"BorderR"=>0,"BorderG"=>0,"BorderB"=>0)); - - $Series[] = array("Format"=>$Serie,"Caption"=>$Caption); - } - } - $this->drawLabelBox($X,$MinY-3,$Description,$Series,$Format); - - } - else - { - if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $XStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } - $Y = $this->GraphAreaY1 + $XMargin + $Index * $XStep; - - if ( $DrawVerticalLine ) { $this->drawLine($this->GraphAreaX1+$Data["YMargin"],$Y,$this->GraphAreaX2-$Data["YMargin"],$Y,array("R"=>$VerticalLineR,"G"=>$VerticalLineG,"B"=>$VerticalLineB,"Alpha"=>$VerticalLineAlpha,"Ticks"=>$VerticalLineTicks)); } - - $MinX = $this->GraphAreaX2; - foreach ($SeriesName as $Key => $SerieName) - { - if ( isset($Data["Series"][$SerieName]["Data"][$Index]) ) - { - $AxisID = $Data["Series"][$SerieName]["Axis"]; - - if ( $OverrideTitle != NULL) - $Description = $OverrideTitle; - elseif ( count($SeriesName) == 1 ) - { - if ( isset($Data["Abscissa"]) && isset($Data["Series"][$Data["Abscissa"]]["Data"][$Index]) ) - $Description = $Data["Series"][$SerieName]["Description"]." - ".$Data["Series"][$Data["Abscissa"]]["Data"][$Index]; - else - $Description = $Data["Series"][$SerieName]["Description"]; - } - elseif ( isset($Data["Abscissa"]) && isset($Data["Series"][$Data["Abscissa"]]["Data"][$Index]) ) - $Description = $Data["Series"][$Data["Abscissa"]]["Data"][$Index]; - - $AxisMode = $Data["Axis"][$AxisID]["Display"]; - $AxisFormat = $Data["Axis"][$AxisID]["Format"]; - $AxisUnit = $Data["Axis"][$AxisID]["Unit"]; - - $Serie = ""; - if ( isset($Data["Extended"]["Palette"][$Index] ) ) - { - $Serie["R"] = $Data["Extended"]["Palette"][$Index]["R"]; - $Serie["G"] = $Data["Extended"]["Palette"][$Index]["G"]; - $Serie["B"] = $Data["Extended"]["Palette"][$Index]["B"]; - $Serie["Alpha"] = $Data["Extended"]["Palette"][$Index]["Alpha"]; - } - else - { - $Serie["R"] = $Data["Series"][$SerieName]["Color"]["R"]; - $Serie["G"] = $Data["Series"][$SerieName]["Color"]["G"]; - $Serie["B"] = $Data["Series"][$SerieName]["Color"]["B"]; - $Serie["Alpha"] = $Data["Series"][$SerieName]["Color"]["Alpha"]; - } - - if ( count($SeriesName) == 1 && isset($Data["Series"][$SerieName]["XOffset"]) ) - $SerieOffset = $Data["Series"][$SerieName]["XOffset"]; - else - $SerieOffset = 0; - - $Value = $Data["Series"][$SerieName]["Data"][$Index]; - if ( $ForceLabels != NULL ) - $Caption = isset($ForceLabels[$Key]) ? $ForceLabels[$Key] : "Not set"; - else - $Caption = $this->scaleFormat($Value,$AxisMode,$AxisFormat,$AxisUnit); - if ( $Value == VOID ) { $Value = "NaN"; } - - if ( $this->LastChartLayout == CHART_LAST_LAYOUT_STACKED ) - { - if ( $Value >=0 ) { $LookFor = "+"; } else { $LookFor = "-"; } - - $Value = 0; $Done = FALSE; - foreach($Data["Series"] as $Name => $SerieLookup) - { - if ( $SerieLookup["isDrawable"] == TRUE && $Name != $Data["Abscissa"] && !$Done ) - { - if ( isset($Data["Series"][$Name]["Data"][$Index]) && $Data["Series"][$Name]["Data"][$Index] != VOID ) - { - if ($Data["Series"][$Name]["Data"][$Index] >= 0 && $LookFor == "+" ) { $Value = $Value + $Data["Series"][$Name]["Data"][$Index]; } - if ($Data["Series"][$Name]["Data"][$Index] < 0 && $LookFor == "-" ) { $Value = $Value - $Data["Series"][$Name]["Data"][$Index]; } - if ($Name == $SerieName ) { $Done = TRUE; } - } - } - } - } - - $X = floor($this->scaleComputeY($Value,array("AxisID"=>$AxisID))); - $Y = floor($this->GraphAreaY1 + $XMargin + $Index * $XStep + $SerieOffset); - - if ($X < $MinX) { $MinX = $X; } - - if ( $DrawPoint == LABEL_POINT_CIRCLE ) - $this->drawFilledCircle($X,$Y,3,array("R"=>255,"G"=>255,"B"=>255,"BorderR"=>0,"BorderG"=>0,"BorderB"=>0)); - elseif ( $DrawPoint == LABEL_POINT_BOX ) - $this->drawFilledRectangle($X-2,$Y-2,$X+2,$Y+2,array("R"=>255,"G"=>255,"B"=>255,"BorderR"=>0,"BorderG"=>0,"BorderB"=>0)); - - $Series[] = array("Format"=>$Serie,"Caption"=>$Caption); - } - } - $this->drawLabelBox($MinX,$Y-3,$Description,$Series,$Format); - - } - } - } - - /* Draw a label box */ - function drawLabelBox($X,$Y,$Title,$Captions,$Format="") - { - $NoTitle = isset($Format["NoTitle"]) ? $Format["NoTitle"] : NULL; - $BoxWidth = isset($Format["BoxWidth"]) ? $Format["BoxWidth"] : 50; - $DrawSerieColor = isset($Format["DrawSerieColor"]) ? $Format["DrawSerieColor"] : TRUE; - $SerieR = isset($Format["SerieR"]) ? $Format["SerieR"] : NULL; - $SerieG = isset($Format["SerieG"]) ? $Format["SerieG"] : NULL; - $SerieB = isset($Format["SerieB"]) ? $Format["SerieB"] : NULL; - $SerieAlpha = isset($Format["SerieAlpha"]) ? $Format["SerieAlpha"] : NULL; - $SerieBoxSize = isset($Format["SerieBoxSize"]) ? $Format["SerieBoxSize"] : 6; - $SerieBoxSpacing = isset($Format["SerieBoxSpacing"]) ? $Format["SerieBoxSpacing"] : 4; - $VerticalMargin = isset($Format["VerticalMargin"]) ? $Format["VerticalMargin"] : 10; - $HorizontalMargin = isset($Format["HorizontalMargin"]) ? $Format["HorizontalMargin"] : 8; - $R = isset($Format["R"]) ? $Format["R"] : $this->FontColorR; - $G = isset($Format["G"]) ? $Format["G"] : $this->FontColorG; - $B = isset($Format["B"]) ? $Format["B"] : $this->FontColorB; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : $this->FontColorA; - $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; - $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; - $TitleMode = isset($Format["TitleMode"]) ? $Format["TitleMode"] : LABEL_TITLE_NOBACKGROUND; - $TitleR = isset($Format["TitleR"]) ? $Format["TitleR"] : $R; - $TitleG = isset($Format["TitleG"]) ? $Format["TitleG"] : $G; - $TitleB = isset($Format["TitleB"]) ? $Format["TitleB"] : $B; - $TitleBackgroundR = isset($Format["TitleBackgroundR"]) ? $Format["TitleBackgroundR"] : 0; - $TitleBackgroundG = isset($Format["TitleBackgroundG"]) ? $Format["TitleBackgroundG"] : 0; - $TitleBackgroundB = isset($Format["TitleBackgroundB"]) ? $Format["TitleBackgroundB"] : 0; - $GradientStartR = isset($Format["GradientStartR"]) ? $Format["GradientStartR"] : 255; - $GradientStartG = isset($Format["GradientStartG"]) ? $Format["GradientStartG"] : 255; - $GradientStartB = isset($Format["GradientStartB"]) ? $Format["GradientStartB"] : 255; - $GradientEndR = isset($Format["GradientEndR"]) ? $Format["GradientEndR"] : 220; - $GradientEndG = isset($Format["GradientEndG"]) ? $Format["GradientEndG"] : 220; - $GradientEndB = isset($Format["GradientEndB"]) ? $Format["GradientEndB"] : 220; - - if ( !$DrawSerieColor ) { $SerieBoxSize = 0; $SerieBoxSpacing = 0; } - - $TxtPos = $this->getTextBox($X,$Y,$FontName,$FontSize,0,$Title); - $TitleWidth = ($TxtPos[1]["X"] - $TxtPos[0]["X"])+$VerticalMargin*2; - $TitleHeight = ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]); - - if ( $NoTitle ) { $TitleWidth = 0; $TitleHeight = 0; } - - $CaptionWidth = 0; $CaptionHeight = -$HorizontalMargin; - foreach($Captions as $Key =>$Caption) - { - $TxtPos = $this->getTextBox($X,$Y,$FontName,$FontSize,0,$Caption["Caption"]); - $CaptionWidth = max($CaptionWidth,($TxtPos[1]["X"] - $TxtPos[0]["X"])+$VerticalMargin*2); - $CaptionHeight = $CaptionHeight + max(($TxtPos[0]["Y"] - $TxtPos[2]["Y"]),($SerieBoxSize+2)) + $HorizontalMargin; - } - - if ( $CaptionHeight <= 5 ) { $CaptionHeight = $CaptionHeight + $HorizontalMargin/2; } - - if ( $DrawSerieColor ) { $CaptionWidth = $CaptionWidth + $SerieBoxSize + $SerieBoxSpacing; } - - $BoxWidth = max($BoxWidth,$TitleWidth,$CaptionWidth); - - $XMin = $X - 5 - floor(($BoxWidth-10) / 2); - $XMax = $X + 5 + floor(($BoxWidth-10) / 2); - - $RestoreShadow = $this->Shadow; - if ( $this->Shadow == TRUE ) - { - $this->Shadow = FALSE; - - $Poly = ""; - $Poly[] = $X+$this->ShadowX; $Poly[] = $Y+$this->ShadowX; - $Poly[] = $X+5+$this->ShadowX; $Poly[] = $Y-5+$this->ShadowX; - $Poly[] = $XMax+$this->ShadowX; $Poly[] = $Y-5+$this->ShadowX; - - if ( $NoTitle ) - { - $Poly[] = $XMax+$this->ShadowX; $Poly[] = $Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2+$this->ShadowX; - $Poly[] = $XMin+$this->ShadowX; $Poly[] = $Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2+$this->ShadowX; - } - else - { - $Poly[] = $XMax+$this->ShadowX; $Poly[] = $Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3+$this->ShadowX; - $Poly[] = $XMin+$this->ShadowX; $Poly[] = $Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3+$this->ShadowX; - } - - $Poly[] = $XMin+$this->ShadowX; $Poly[] = $Y-5+$this->ShadowX; - $Poly[] = $X-5+$this->ShadowX; $Poly[] = $Y-5+$this->ShadowX; - $this->drawPolygon($Poly,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); - } - - /* Draw the background */ - $GradientSettings = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB); - if ( $NoTitle ) - $this->drawGradientArea($XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax,$Y-6,DIRECTION_VERTICAL,$GradientSettings); - else - $this->drawGradientArea($XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax,$Y-6,DIRECTION_VERTICAL,$GradientSettings); - $Poly = ""; $Poly[] = $X; $Poly[] = $Y; $Poly[] = $X-5; $Poly[] = $Y-5; $Poly[] = $X+5; $Poly[] = $Y-5; - $this->drawPolygon($Poly,array("R"=>$GradientEndR,"G"=>$GradientEndG,"B"=>$GradientEndB,"NoBorder"=>TRUE)); - - /* Outer border */ - $OuterBorderColor = $this->allocateColor($this->Picture,100,100,100,100); - imageline($this->Picture,$XMin,$Y-5,$X-5,$Y-5,$OuterBorderColor); - imageline($this->Picture,$X,$Y,$X-5,$Y-5,$OuterBorderColor); - imageline($this->Picture,$X,$Y,$X+5,$Y-5,$OuterBorderColor); - imageline($this->Picture,$X+5,$Y-5,$XMax,$Y-5,$OuterBorderColor); - if ( $NoTitle ) - { - imageline($this->Picture,$XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMin,$Y-5,$OuterBorderColor); - imageline($this->Picture,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax,$Y-5,$OuterBorderColor); - imageline($this->Picture,$XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$OuterBorderColor); - } - else - { - imageline($this->Picture,$XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMin,$Y-5,$OuterBorderColor); - imageline($this->Picture,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax,$Y-5,$OuterBorderColor); - imageline($this->Picture,$XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$OuterBorderColor); - } - - /* Inner border */ - $InnerBorderColor = $this->allocateColor($this->Picture,255,255,255,100); - imageline($this->Picture,$XMin+1,$Y-6,$X-5,$Y-6,$InnerBorderColor); - imageline($this->Picture,$X,$Y-1,$X-5,$Y-6,$InnerBorderColor); - imageline($this->Picture,$X,$Y-1,$X+5,$Y-6,$InnerBorderColor); - imageline($this->Picture,$X+5,$Y-6,$XMax-1,$Y-6,$InnerBorderColor); - if ( $NoTitle ) - { - imageline($this->Picture,$XMin+1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMin+1,$Y-6,$InnerBorderColor); - imageline($this->Picture,$XMax-1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax-1,$Y-6,$InnerBorderColor); - imageline($this->Picture,$XMin+1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax-1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$InnerBorderColor); - } - else - { - imageline($this->Picture,$XMin+1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMin+1,$Y-6,$InnerBorderColor); - imageline($this->Picture,$XMax-1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax-1,$Y-6,$InnerBorderColor); - imageline($this->Picture,$XMin+1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax-1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$InnerBorderColor); - } - - /* Draw the separator line */ - if ( $TitleMode == LABEL_TITLE_NOBACKGROUND && !$NoTitle ) - { - $YPos = $Y-7-$CaptionHeight-$HorizontalMargin-$HorizontalMargin/2; - $XMargin = $VerticalMargin / 2; - $this->drawLine($XMin+$XMargin,$YPos+1,$XMax-$XMargin,$YPos+1,array("R"=>$GradientEndR,"G"=>$GradientEndG,"B"=>$GradientEndB)); - $this->drawLine($XMin+$XMargin,$YPos,$XMax-$XMargin,$YPos,array("R"=>$GradientStartR,"G"=>$GradientStartG,"B"=>$GradientStartB)); - } - elseif ( $TitleMode == LABEL_TITLE_BACKGROUND ) - { - $this->drawFilledRectangle($XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin+$HorizontalMargin/2,array("R"=>$TitleBackgroundR,"G"=>$TitleBackgroundG,"B"=>$TitleBackgroundB)); - imageline($this->Picture,$XMin+1,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin+$HorizontalMargin/2+1,$XMax-1,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin+$HorizontalMargin/2+1,$InnerBorderColor); - } - - /* Write the description */ - if ( !$NoTitle ) - $this->drawText($XMin+$VerticalMargin,$Y-7-$CaptionHeight-$HorizontalMargin*2,$Title,array("Align"=>TEXT_ALIGN_BOTTOMLEFT,"R"=>$TitleR,"G"=>$TitleG,"B"=>$TitleB)); - - /* Write the value */ - $YPos = $Y-5-$HorizontalMargin; $XPos = $XMin+$VerticalMargin+$SerieBoxSize+$SerieBoxSpacing; - foreach($Captions as $Key => $Caption) - { - $CaptionTxt = $Caption["Caption"]; - $TxtPos = $this->getTextBox($XPos,$YPos,$FontName,$FontSize,0,$CaptionTxt); - $CaptionHeight = ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]); - - /* Write the serie color if needed */ - if ( $DrawSerieColor ) - { - $BoxSettings = array("R"=>$Caption["Format"]["R"],"G"=>$Caption["Format"]["G"],"B"=>$Caption["Format"]["B"],"Alpha"=>$Caption["Format"]["Alpha"],"BorderR"=>0,"BorderG"=>0,"BorderB"=>0); - $this->drawFilledRectangle($XMin+$VerticalMargin,$YPos-$SerieBoxSize,$XMin+$VerticalMargin+$SerieBoxSize,$YPos,$BoxSettings); - } - - $this->drawText($XPos,$YPos,$CaptionTxt,array("Align"=>TEXT_ALIGN_BOTTOMLEFT)); - - $YPos = $YPos - $CaptionHeight - $HorizontalMargin; - } - - $this->Shadow = $RestoreShadow; - } - - /* Draw a basic shape */ - function drawShape($X,$Y,$Shape,$PlotSize,$PlotBorder,$BorderSize,$R,$G,$B,$Alpha,$BorderR,$BorderG,$BorderB,$BorderAlpha) - { - if ( $Shape == SERIE_SHAPE_FILLEDCIRCLE ) - { - if ( $PlotBorder ) { $this->drawFilledCircle($X,$Y,$PlotSize+$BorderSize,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); } - $this->drawFilledCircle($X,$Y,$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - } - elseif ( $Shape == SERIE_SHAPE_FILLEDSQUARE ) - { - if ( $PlotBorder ) { $this->drawFilledRectangle($X-$PlotSize-$BorderSize,$Y-$PlotSize-$BorderSize,$X+$PlotSize+$BorderSize,$Y+$PlotSize+$BorderSize,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); } - $this->drawFilledRectangle($X-$PlotSize,$Y-$PlotSize,$X+$PlotSize,$Y+$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - } - elseif ( $Shape == SERIE_SHAPE_FILLEDTRIANGLE ) - { - if ( $PlotBorder ) - { - $Pos = ""; $Pos[]=$X; $Pos[]=$Y-$PlotSize-$BorderSize; $Pos[]=$X-$PlotSize-$BorderSize; $Pos[]=$Y+$PlotSize+$BorderSize; $Pos[]=$X+$PlotSize+$BorderSize; $Pos[]=$Y+$PlotSize+$BorderSize; - $this->drawPolygon($Pos,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); - } - - $Pos = ""; $Pos[]=$X; $Pos[]=$Y-$PlotSize; $Pos[]=$X-$PlotSize; $Pos[]=$Y+$PlotSize; $Pos[]=$X+$PlotSize; $Pos[]=$Y+$PlotSize; - $this->drawPolygon($Pos,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - } - elseif ( $Shape == SERIE_SHAPE_TRIANGLE ) - { - $this->drawLine($X,$Y-$PlotSize,$X-$PlotSize,$Y+$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - $this->drawLine($X-$PlotSize,$Y+$PlotSize,$X+$PlotSize,$Y+$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - $this->drawLine($X+$PlotSize,$Y+$PlotSize,$X,$Y-$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - } - elseif ( $Shape == SERIE_SHAPE_SQUARE ) - $this->drawRectangle($X-$PlotSize,$Y-$PlotSize,$X+$PlotSize,$Y+$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - elseif ( $Shape == SERIE_SHAPE_CIRCLE ) - $this->drawCircle($X,$Y,$PlotSize,$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - elseif ( $Shape == SERIE_SHAPE_DIAMOND ) - { - $Pos = ""; $Pos[]=$X-$PlotSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y-$PlotSize; $Pos[]=$X+$PlotSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y+$PlotSize; - $this->drawPolygon($Pos,array("NoFill"=>TRUE,"BorderR"=>$R,"BorderG"=>$G,"BorderB"=>$B,"BorderAlpha"=>$Alpha)); - } - elseif ( $Shape == SERIE_SHAPE_FILLEDDIAMOND ) - { - if ( $PlotBorder ) - { - $Pos = ""; $Pos[]=$X-$PlotSize-$BorderSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y-$PlotSize-$BorderSize; $Pos[]=$X+$PlotSize+$BorderSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y+$PlotSize+$BorderSize; - $this->drawPolygon($Pos,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); - } - - $Pos = ""; $Pos[]=$X-$PlotSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y-$PlotSize; $Pos[]=$X+$PlotSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y+$PlotSize; - $this->drawPolygon($Pos,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); - } - } - - function drawPolygonChart($Points,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $NoFill = isset($Format["NoFill"]) ? $Format["NoFill"] : FALSE; - $NoBorder = isset($Format["NoBorder"]) ? $Format["NoBorder"] : FALSE; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; - $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha / 2; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - $Threshold = isset($Format["Threshold"]) ? $Format["Threshold"] : NULL; - - if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } - - $RestoreShadow = $this->Shadow; - $this->Shadow = FALSE; - - $AllIntegers = TRUE; - for($i=0;$i<=count($Points)-2;$i=$i+2) - { if ( $this->getFirstDecimal($Points[$i+1]) != 0 ) { $AllIntegers = FALSE; } } - - /* Convert polygon to segments */ - $Segments = ""; - for($i=2;$i<=count($Points)-2;$i=$i+2) - { $Segments[] = array("X1"=>$Points[$i-2],"Y1"=>$Points[$i-1],"X2"=>$Points[$i],"Y2"=>$Points[$i+1]); } - $Segments[] = array("X1"=>$Points[$i-2],"Y1"=>$Points[$i-1],"X2"=>$Points[0],"Y2"=>$Points[1]); - - /* Simplify straight lines */ - $Result = ""; $inHorizon = FALSE; $LastX = VOID; - foreach($Segments as $Key => $Pos) - { - if ( $Pos["Y1"] != $Pos["Y2"] ) - { - if ( $inHorizon ) { $inHorizon = FALSE; $Result[] = array("X1"=>$LastX,"Y1"=>$Pos["Y1"],"X2"=>$Pos["X1"],"Y2"=>$Pos["Y1"]); } - - $Result[] = array("X1"=>$Pos["X1"],"Y1"=>$Pos["Y1"],"X2"=>$Pos["X2"],"Y2"=>$Pos["Y2"]); - } - else { if ( !$inHorizon ) { $inHorizon = TRUE; $LastX = $Pos["X1"];} } - } - $Segments = $Result; - - /* Do we have something to draw */ - if ( $Segments == "" ) { return(0); } - - /* For segments debugging purpose */ - //foreach($Segments as $Key => $Pos) - // echo $Pos["X1"].",".$Pos["Y1"].",".$Pos["X2"].",".$Pos["Y2"]."\r\n"; - - /* Find out the min & max Y boundaries */ - $MinY = OUT_OF_SIGHT; $MaxY = OUT_OF_SIGHT; - foreach($Segments as $Key => $Coords) - { - if ( $MinY == OUT_OF_SIGHT || $MinY > min($Coords["Y1"],$Coords["Y2"]) ) { $MinY = min($Coords["Y1"],$Coords["Y2"]); } - if ( $MaxY == OUT_OF_SIGHT || $MaxY < max($Coords["Y1"],$Coords["Y2"]) ) { $MaxY = max($Coords["Y1"],$Coords["Y2"]); } - } - - if ( $AllIntegers ) { $YStep = 1; } else { $YStep = .5; } - - $MinY = floor($MinY); $MaxY = floor($MaxY); - - /* Scan each Y lines */ - $DefaultColor = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - $DebugLine = 0; $DebugColor = $this->allocateColor($this->Picture,255,0,0,100); - - $MinY = floor($MinY); $MaxY = floor($MaxY); $YStep = 1; - - if ( !$NoFill ) - { - //if ( $DebugLine ) { $MinY = $DebugLine; $MaxY = $DebugLine; } - for($Y=$MinY;$Y<=$MaxY;$Y=$Y+$YStep) - { - $Intersections = ""; $LastSlope = NULL; $RestoreLast = "-"; - foreach($Segments as $Key => $Coords) - { - $X1 = $Coords["X1"]; $X2 = $Coords["X2"]; $Y1 = $Coords["Y1"]; $Y2 = $Coords["Y2"]; - - if ( min($Y1,$Y2) <= $Y && max($Y1,$Y2) >= $Y ) - { - if ( $Y1 == $Y2 ) - { $X = $X1; } - else - { $X = $X1 + ( ($Y-$Y1)*$X2 - ($Y-$Y1)*$X1 ) / ($Y2-$Y1); } - - $X = floor($X); - - if ( $X2 == $X1 ) - { $Slope = "!"; } - else - { - $SlopeC = ($Y2 - $Y1) / ($X2 - $X1); - if( $SlopeC == 0 ) - { $Slope = "="; } - elseif( $SlopeC > 0 ) - { $Slope = "+"; } - elseif ( $SlopeC < 0 ) - { $Slope = "-"; } - } - - if ( !is_array($Intersections) ) - { $Intersections[] = $X; } - elseif( !in_array($X,$Intersections) ) - { $Intersections[] = $X; } - elseif( in_array($X,$Intersections) ) - { - if ($Y == $DebugLine) { echo $Slope."/".$LastSlope."(".$X.") "; } - - if ( $Slope == "=" && $LastSlope == "-" ) { $Intersections[] = $X; } - if ( $Slope != $LastSlope && $LastSlope != "!" && $LastSlope != "=" ) { $Intersections[] = $X; } - if ( $Slope != $LastSlope && $LastSlope == "!" && $Slope == "+" ) { $Intersections[] = $X; } - } - - if ( is_array($Intersections) && in_array($X,$Intersections) && $LastSlope == "=" && ($Slope == "-" )) { $Intersections[] = $X; } - - $LastSlope = $Slope; - } - } - if ( $RestoreLast != "-" ) { $Intersections[] = $RestoreLast; echo "@".$Y."\r\n"; } - - if ( is_array($Intersections) ) - { - sort($Intersections); - - if ($Y == $DebugLine) { print_r($Intersections); } - - /* Remove NULL plots */ - $Result = ""; - for($i=0;$i<=count($Intersections)-1;$i=$i+2) - { - if ( isset($Intersections[$i+1]) ) - { if ( $Intersections[$i] != $Intersections[$i+1] ) { $Result[] = $Intersections[$i]; $Result[] = $Intersections[$i+1]; } } - } - - if ( is_array($Result) ) - { - $Intersections = $Result; - - $LastX = OUT_OF_SIGHT; - foreach($Intersections as $Key => $X) - { - if ( $LastX == OUT_OF_SIGHT ) - $LastX = $X; - elseif ( $LastX != OUT_OF_SIGHT ) - { - if ( $this->getFirstDecimal($LastX) > 1 ) { $LastX++; } - - $Color = $DefaultColor; - if ( $Threshold != NULL ) - { - foreach($Threshold as $Key => $Parameters) - { - if ( $Y <= $Parameters["MinX"] && $Y >= $Parameters["MaxX"]) - { - if ( isset($Parameters["R"]) ) { $R = $Parameters["R"]; } else { $R = 0; } - if ( isset($Parameters["G"]) ) { $G = $Parameters["G"]; } else { $G = 0; } - if ( isset($Parameters["B"]) ) { $B = $Parameters["B"]; } else { $B = 0; } - if ( isset($Parameters["Alpha"]) ) { $Alpha = $Parameters["Alpha"]; } else { $Alpha = 100; } - $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); - } - } - } - - imageline($this->Picture,$LastX,$Y,$X,$Y,$Color); - - if ( $Y == $DebugLine) { imageline($this->Picture,$LastX,$Y,$X,$Y,$DebugColor); } - - $LastX = OUT_OF_SIGHT; - } - } - } - } - } - } - - /* Draw the polygon border, if required */ - if ( !$NoBorder) - { - foreach($Segments as $Key => $Coords) - $this->drawLine($Coords["X1"],$Coords["Y1"],$Coords["X2"],$Coords["Y2"],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Threshold"=>$Threshold)); - } - - $this->Shadow = $RestoreShadow; - } - - /* Return the abscissa margin */ - function getAbscissaMargin($Data) - { - foreach($Data["Axis"] as $AxisID => $Values) { if ( $Values["Identity"] == AXIS_X ) { return($Values["Margin"]); } } - return(0); - } - - } +DataSet->getData(); + + foreach($Data["Series"] as $SerieName => $Serie) + { if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) { $Results++; } } + + return($Results); + } + + /* Fix box coordinates */ + function fixBoxCoordinates($Xa,$Ya,$Xb,$Yb) + { + $X1 = min($Xa,$Xb); $Y1 = min($Ya,$Yb); + $X2 = max($Xa,$Xb); $Y2 = max($Ya,$Yb); + + return(array($X1,$Y1,$X2,$Y2)); + } + + /* Draw a polygon */ + function drawPolygon($Points,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $NoFill = isset($Format["NoFill"]) ? $Format["NoFill"] : FALSE; + $NoBorder = isset($Format["NoBorder"]) ? $Format["NoBorder"] : FALSE; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; + $BorderAlpha = isset($Format["Alpha"]) ? $Format["Alpha"] : $Alpha / 2; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + $SkipX = isset($Format["SkipX"]) ? $Format["SkipX"] : OUT_OF_SIGHT; + $SkipY = isset($Format["SkipY"]) ? $Format["SkipY"] : OUT_OF_SIGHT; + + /* Calling the ImageFilledPolygon() function over the $Points array will round it */ + $Backup = $Points; + + if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } + + if ( $SkipX != OUT_OF_SIGHT ) { $SkipX = floor($SkipX); } + if ( $SkipY != OUT_OF_SIGHT ) { $SkipY = floor($SkipY); } + + $RestoreShadow = $this->Shadow; + if ( !$NoFill ) + { + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $this->Shadow = FALSE; + for($i=0;$i<=count($Points)-1;$i=$i+2) + { $Shadow[] = $Points[$i] + $this->ShadowX; $Shadow[] = $Points[$i+1] + $this->ShadowY; } + $this->drawPolygon($Shadow,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"NoBorder"=>TRUE)); + } + + $FillColor = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + + if ( count($Points) >= 6 ) + { ImageFilledPolygon($this->Picture,$Points,count($Points)/2,$FillColor); } + } + + if ( !$NoBorder ) + { + $Points = $Backup; + + if ( $NoFill ) + $BorderSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); + else + $BorderSettings = array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha); + + for($i=0;$i<=count($Points)-1;$i=$i+2) + { + if ( isset($Points[$i+2]) ) + { + if ( !($Points[$i] == $Points[$i+2] && $Points[$i] == $SkipX ) && !($Points[$i+1] == $Points[$i+3] && $Points[$i+1] == $SkipY ) ) + $this->drawLine($Points[$i],$Points[$i+1],$Points[$i+2],$Points[$i+3],$BorderSettings); + } + else + { + if ( !($Points[$i] == $Points[0] && $Points[$i] == $SkipX ) && !($Points[$i+1] == $Points[1] && $Points[$i+1] == $SkipY ) ) + $this->drawLine($Points[$i],$Points[$i+1],$Points[0],$Points[1],$BorderSettings); + } + } + } + + $this->Shadow = $RestoreShadow; + } + + /* Apply AALias correction to the rounded box boundaries */ + function offsetCorrection($Value,$Mode) + { + $Value = round($Value,1); + + if ( $Value == 0 && $Mode == 1 ) { return(.9); } + if ( $Value == 0 ) { return(0); } + + if ( $Mode == 1) + { if ( $Value == 1 ) { return(.9); }; if ( $Value == .1 ) { return(.9); }; if ( $Value == .2 ) { return(.8); }; if ( $Value == .3 ) { return(.8); }; if ( $Value == .4 ) { return(.7); }; if ( $Value == .5 ) { return(.5); }; if ( $Value == .6 ) { return(.8); }; if ( $Value == .7 ) { return(.7); }; if ( $Value == .8 ) { return(.6); }; if ( $Value == .9 ) { return(.9); }; } + + if ( $Mode == 2) + { if ( $Value == 1 ) { return(.9); }; if ( $Value == .1 ) { return(.1); }; if ( $Value == .2 ) { return(.2); }; if ( $Value == .3 ) { return(.3); }; if ( $Value == .4 ) { return(.4); }; if ( $Value == .5 ) { return(.5); }; if ( $Value == .6 ) { return(.8); }; if ( $Value == .7 ) { return(.7); }; if ( $Value == .8 ) { return(.8); }; if ( $Value == .9 ) { return(.9); }; } + + if ( $Mode == 3) + { if ( $Value == 1 ) { return(.1); }; if ( $Value == .1 ) { return(.1); }; if ( $Value == .2 ) { return(.2); }; if ( $Value == .3 ) { return(.3); }; if ( $Value == .4 ) { return(.4); }; if ( $Value == .5 ) { return(.9); }; if ( $Value == .6 ) { return(.6); }; if ( $Value == .7 ) { return(.7); }; if ( $Value == .8 ) { return(.4); }; if ( $Value == .9 ) { return(.5); }; } + + if ( $Mode == 4) + { if ( $Value == 1 ) { return(-1); }; if ( $Value == .1 ) { return(.1); }; if ( $Value == .2 ) { return(.2); }; if ( $Value == .3 ) { return(.3); }; if ( $Value == .4 ) { return(.1); }; if ( $Value == .5 ) { return(-.1); }; if ( $Value == .6 ) { return(.8); }; if ( $Value == .7 ) { return(.1); }; if ( $Value == .8 ) { return(.1); }; if ( $Value == .9 ) { return(.1); }; } + } + + /* Draw a rectangle with rounded corners */ + function drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + + list($X1,$Y1,$X2,$Y2) = $this->fixBoxCoordinates($X1,$Y1,$X2,$Y2); + + if ( $X2 - $X1 < $Radius ) { $Radius = floor((($X2-$X1))/2); } + if ( $Y2 - $Y1 < $Radius ) { $Radius = floor((($Y2-$Y1))/2); } + + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"NoBorder"=>TRUE); + + if ( $Radius <= 0 ) { $this->drawRectangle($X1,$Y1,$X2,$Y2,$Color); return(0); } + + if ( $this->Antialias ) + { + $this->drawLine($X1+$Radius,$Y1,$X2-$Radius,$Y1,$Color); + $this->drawLine($X2,$Y1+$Radius,$X2,$Y2-$Radius,$Color); + $this->drawLine($X2-$Radius,$Y2,$X1+$Radius,$Y2,$Color); + $this->drawLine($X1,$Y1+$Radius,$X1,$Y2-$Radius,$Color); + } + else + { + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + imageline($this->Picture,$X1+$Radius,$Y1,$X2-$Radius,$Y1,$Color); + imageline($this->Picture,$X2,$Y1+$Radius,$X2,$Y2-$Radius,$Color); + imageline($this->Picture,$X2-$Radius,$Y2,$X1+$Radius,$Y2,$Color); + imageline($this->Picture,$X1,$Y1+$Radius,$X1,$Y2-$Radius,$Color); + } + + $Step = 360 / (2 * PI * $Radius); + for($i=0;$i<=90;$i=$i+$Step) + { + $X = cos(($i+180)*PI/180) * $Radius + $X1 + $Radius; + $Y = sin(($i+180)*PI/180) * $Radius + $Y1 + $Radius; + $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + + $X = cos(($i+90)*PI/180) * $Radius + $X1 + $Radius; + $Y = sin(($i+90)*PI/180) * $Radius + $Y2 - $Radius; + $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + + $X = cos($i*PI/180) * $Radius + $X2 - $Radius; + $Y = sin($i*PI/180) * $Radius + $Y2 - $Radius; + $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + + $X = cos(($i+270)*PI/180) * $Radius + $X2 - $Radius; + $Y = sin(($i+270)*PI/180) * $Radius + $Y1 + $Radius; + $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + } + } + + /* Draw a rectangle with rounded corners */ + function drawRoundedFilledRectangle($X1,$Y1,$X2,$Y2,$Radius,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + + /* Temporary fix for AA issue */ + $Y1 = floor($Y1); $Y2 = floor($Y2); $X1 = floor($X1); $X2 = floor($X2); + + if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } + if ( $BorderR == -1 ) { $BorderR = $R; $BorderG = $G; $BorderB = $B; } + + list($X1,$Y1,$X2,$Y2) = $this->fixBoxCoordinates($X1,$Y1,$X2,$Y2); + + if ( $X2 - $X1 < $Radius*2 ) { $Radius = floor((($X2-$X1))/4); } + if ( $Y2 - $Y1 < $Radius*2 ) { $Radius = floor((($Y2-$Y1))/4); } + + $RestoreShadow = $this->Shadow; + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $this->Shadow = FALSE; + $this->drawRoundedFilledRectangle($X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,$Radius,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); + } + + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"NoBorder"=>TRUE); + + if ( $Radius <= 0 ) { $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$Color); return(0); } + + $YTop = $Y1+$Radius; + $YBottom = $Y2-$Radius; + + $Step = 360 / (2 * PI * $Radius); + $Positions = ""; $Radius--; $MinY = ""; $MaxY = ""; + for($i=0;$i<=90;$i=$i+$Step) + { + $Xp1 = cos(($i+180)*PI/180) * $Radius + $X1 + $Radius; + $Xp2 = cos(((90-$i)+270)*PI/180) * $Radius + $X2 - $Radius; + $Yp = floor(sin(($i+180)*PI/180) * $Radius + $YTop); + if ( $MinY == "" || $Yp > $MinY ) { $MinY = $Yp; } + + if ( $Xp1 <= floor($X1) ) { $Xp1++; } + if ( $Xp2 >= floor($X2) ) { $Xp2--; } + $Xp1++; + + if ( !isset($Positions[$Yp]) ) + { $Positions[$Yp]["X1"] = $Xp1; $Positions[$Yp]["X2"] = $Xp2; } + else + { $Positions[$Yp]["X1"] = ($Positions[$Yp]["X1"]+$Xp1)/2; $Positions[$Yp]["X2"] = ($Positions[$Yp]["X2"]+$Xp2)/2; } + + $Xp1 = cos(($i+90)*PI/180) * $Radius + $X1 + $Radius; + $Xp2 = cos((90-$i)*PI/180) * $Radius + $X2 - $Radius; + $Yp = floor(sin(($i+90)*PI/180) * $Radius + $YBottom); + if ( $MaxY == "" || $Yp < $MaxY ) { $MaxY = $Yp; } + + if ( $Xp1 <= floor($X1) ) { $Xp1++; } + if ( $Xp2 >= floor($X2) ) { $Xp2--; } + $Xp1++; + + if ( !isset($Positions[$Yp]) ) + { $Positions[$Yp]["X1"] = $Xp1; $Positions[$Yp]["X2"] = $Xp2; } + else + { $Positions[$Yp]["X1"] = ($Positions[$Yp]["X1"]+$Xp1)/2; $Positions[$Yp]["X2"] = ($Positions[$Yp]["X2"]+$Xp2)/2; } + } + + $ManualColor = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + foreach($Positions as $Yp => $Bounds) + { + $X1 = $Bounds["X1"]; $X1Dec = $this->getFirstDecimal($X1); if ( $X1Dec != 0 ) { $X1 = floor($X1)+1; } + $X2 = $Bounds["X2"]; $X2Dec = $this->getFirstDecimal($X2); if ( $X2Dec != 0 ) { $X2 = floor($X2)-1; } + imageline($this->Picture,$X1,$Yp,$X2,$Yp,$ManualColor); + } + $this->drawFilledRectangle($X1,$MinY+1,floor($X2),$MaxY-1,$Color); + + $Radius++; + $this->drawRoundedRectangle($X1,$Y1,$X2+1,$Y2-1,$Radius,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); + + $this->Shadow = $RestoreShadow; + } + + /* Draw a rectangle with rounded corners */ + function drawRoundedFilledRectangle_deprecated($X1,$Y1,$X2,$Y2,$Radius,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + + if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } + if ( $BorderR == -1 ) { $BorderR = $R; $BorderG = $G; $BorderB = $B; } + + list($X1,$Y1,$X2,$Y2) = $this->fixBoxCoordinates($X1,$Y1,$X2,$Y2); + + if ( $X2 - $X1 < $Radius ) { $Radius = floor((($X2-$X1)+2)/2); } + if ( $Y2 - $Y1 < $Radius ) { $Radius = floor((($Y2-$Y1)+2)/2); } + + $RestoreShadow = $this->Shadow; + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $this->Shadow = FALSE; + $this->drawRoundedFilledRectangle($X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,$Radius,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); + } + + if ( $this->getFirstDecimal($X2) >= 5 ) { $XOffset2 = 1; } else { $XOffset2 = 0; } + if ( $this->getFirstDecimal($X1) <= 5 ) { $XOffset1 = 1; } else { $XOffset1 = 0; } + + if ( !$this->Antialias ) { $XOffset1 = 1; $XOffset2 = 1; } + + $YTop = floor($Y1+$Radius); + $YBottom = floor($Y2-$Radius); + + $this->drawFilledRectangle($X1-$XOffset1,$YTop,$X2+$XOffset2,$YBottom,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"NoBorder"=>TRUE)); + + $Step = 360 / (2 * PI * $Radius); + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + $Color2 = $this->allocateColor($this->Picture,255,0,0,$Alpha); + $Drawn = ""; + + if ( $Alpha < 100 ) { $Drawn[$YTop] = FALSE; } + if ( $Alpha < 100 ) { $Drawn[$YBottom] = TRUE; } + + for($i=0;$i<=90;$i=$i+$Step) + { + $Xp1 = cos(($i+180)*PI/180) * $Radius + $X1 + $Radius; + $Xp2 = cos(((90-$i)+270)*PI/180) * $Radius + $X2 - $Radius; + $Yp = sin(($i+180)*PI/180) * $Radius + $YTop; + + if ( $this->getFirstDecimal($Xp1) > 5 ) { $XOffset1 = 1; } else { $XOffset1 = 0; } + if ( $this->getFirstDecimal($Xp2) > 5 ) { $XOffset2 = 1; } else { $XOffset2 = 0; } + if ( $this->getFirstDecimal($Yp) > 5 ) { $YOffset = 1; } else { $YOffset = 0; } + + if ( !isset($Drawn[$Yp+$YOffset]) || $Alpha == 100 ) + imageline($this->Picture,$Xp1+$XOffset1,$Yp+$YOffset,$Xp2+$XOffset2,$Yp+$YOffset,$Color); + + $Drawn[$Yp+$YOffset] = $Xp2; + + $Xp1 = cos(($i+90)*PI/180) * $Radius + $X1 + $Radius; + $Xp2 = cos((90-$i)*PI/180) * $Radius + $X2 - $Radius; + $Yp = sin(($i+90)*PI/180) * $Radius + $YBottom; + + if ( $this->getFirstDecimal($Xp1) > 7 ) { $XOffset1 = 1; } else { $XOffset1 = 0; } + if ( $this->getFirstDecimal($Xp2) > 7 ) { $XOffset2 = 1; } else { $XOffset2 = 0; } + if ( $this->getFirstDecimal($Yp) > 5 ) { $YOffset = 1; } else { $YOffset = 0; } + + if ( !isset($Drawn[$Yp+$YOffset]) || $Alpha == 100 ) + imageline($this->Picture,$Xp1+$XOffset1,$Yp+$YOffset,$Xp2+$XOffset2,$Yp+$YOffset,$Color); + + $Drawn[$Yp+$YOffset] = $Xp2; + } + + $this->drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); + + $this->Shadow = $RestoreShadow; + } + + /* Draw a rectangle */ + function drawRectangle($X1,$Y1,$X2,$Y2,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; + $NoAngle = isset($Format["NoAngle"]) ? $Format["NoAngle"] : FALSE; + + if ($X1 > $X2) { list($X1, $X2) = array($X2, $X1); } + if ($Y1 > $Y2) { list($Y1, $Y2) = array($Y2, $Y1); } + + if ( $this->Antialias ) + { + if ( $NoAngle ) + { + $this->drawLine($X1+1,$Y1,$X2-1,$Y1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + $this->drawLine($X2,$Y1+1,$X2,$Y2-1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + $this->drawLine($X2-1,$Y2,$X1+1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + $this->drawLine($X1,$Y1+1,$X1,$Y2-1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + } + else + { + $this->drawLine($X1+1,$Y1,$X2-1,$Y1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + $this->drawLine($X2,$Y1,$X2,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + $this->drawLine($X2-1,$Y2,$X1+1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + $this->drawLine($X1,$Y1,$X1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + } + } + else + { + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + imagerectangle($this->Picture,$X1,$Y1,$X2,$Y2,$Color); + } + } + + /* Draw a filled rectangle */ + function drawFilledRectangle($X1,$Y1,$X2,$Y2,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; + $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; + $NoAngle = isset($Format["NoAngle"]) ? $Format["NoAngle"] : NULL; + $Dash = isset($Format["Dash"]) ? $Format["Dash"] : FALSE; + $DashStep = isset($Format["DashStep"]) ? $Format["DashStep"] : 4; + $DashR = isset($Format["DashR"]) ? $Format["DashR"] : 0; + $DashG = isset($Format["DashG"]) ? $Format["DashG"] : 0; + $DashB = isset($Format["DashB"]) ? $Format["DashB"] : 0; + $NoBorder = isset($Format["NoBorder"]) ? $Format["NoBorder"] : FALSE; + + if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } + + if ($X1 > $X2) { list($X1, $X2) = array($X2, $X1); } + if ($Y1 > $Y2) { list($Y1, $Y2) = array($Y2, $Y1); } + + $RestoreShadow = $this->Shadow; + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $this->Shadow = FALSE; + $this->drawFilledRectangle($X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"Ticks"=>$Ticks,"NoAngle"=>$NoAngle)); + } + + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + if ( $NoAngle ) + { + imagefilledrectangle($this->Picture,ceil($X1)+1,ceil($Y1),floor($X2)-1,floor($Y2),$Color); + imageline($this->Picture,ceil($X1),ceil($Y1)+1,ceil($X1),floor($Y2)-1,$Color); + imageline($this->Picture,floor($X2),ceil($Y1)+1,floor($X2),floor($Y2)-1,$Color); + } + else + imagefilledrectangle($this->Picture,ceil($X1),ceil($Y1),floor($X2),floor($Y2),$Color); + + if ( $Dash ) + { + if ( $BorderR != -1 ) { $iX1=$X1+1; $iY1=$Y1+1; $iX2=$X2-1; $iY2=$Y2-1; } else { $iX1=$X1; $iY1=$Y1; $iX2=$X2; $iY2=$Y2; } + + $Color = $this->allocateColor($this->Picture,$DashR,$DashG,$DashB,$Alpha); + $Y=$iY1-$DashStep; + for($X=$iX1; $X<=$iX2+($iY2-$iY1); $X=$X+$DashStep) + { + $Y=$Y+$DashStep; + if ( $X > $iX2 ) { $Xa = $X-($X-$iX2); $Ya = $iY1+($X-$iX2); } else { $Xa = $X; $Ya = $iY1; } + if ( $Y > $iY2 ) { $Xb = $iX1+($Y-$iY2); $Yb = $Y-($Y-$iY2); } else { $Xb = $iX1; $Yb = $Y; } + imageline($this->Picture,$Xa,$Ya,$Xb,$Yb,$Color); + } + } + + if ( $this->Antialias && !$NoBorder ) + { + if ( $X1 < ceil($X1) ) + { + $AlphaA = $Alpha * (ceil($X1) - $X1); + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$AlphaA); + imageline($this->Picture,ceil($X1)-1,ceil($Y1),ceil($X1)-1,floor($Y2),$Color); + } + + if ( $Y1 < ceil($Y1) ) + { + $AlphaA = $Alpha * (ceil($Y1) - $Y1); + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$AlphaA); + imageline($this->Picture,ceil($X1),ceil($Y1)-1,floor($X2),ceil($Y1)-1,$Color); + } + + if ( $X2 > floor($X2) ) + { + $AlphaA = $Alpha * (.5-($X2 - floor($X2))); + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$AlphaA); + imageline($this->Picture,floor($X2)+1,ceil($Y1),floor($X2)+1,floor($Y2),$Color); + } + + if ( $Y2 > floor($Y2) ) + { + $AlphaA = $Alpha * (.5-($Y2 - floor($Y2))); + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$AlphaA); + imageline($this->Picture,ceil($X1),floor($Y2)+1,floor($X2),floor($Y2)+1,$Color); + } + } + + if ( $BorderR != -1 ) + $this->drawRectangle($X1,$Y1,$X2,$Y2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$Ticks,"NoAngle"=>$NoAngle)); + + $this->Shadow = $RestoreShadow; + } + + /* Draw a rectangular marker of the specified size */ + function drawRectangleMarker($X,$Y,$Format="") + { + $Size = isset($Format["Size"]) ? $Format["Size"] : 4; + + $HalfSize = floor($Size/2); + $this->drawFilledRectangle($X-$HalfSize,$Y-$HalfSize,$X+$HalfSize,$Y+$HalfSize,$Format); + } + + /* Drawn a spline based on the bezier function */ + function drawSpline($Coordinates,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Force = isset($Format["Force"]) ? $Format["Force"] : 30; + $Forces = isset($Format["Forces"]) ? $Format["Forces"] : NULL; + $ShowC = isset($Format["ShowControl"]) ? $Format["ShowControl"] : FALSE; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; + $PathOnly = isset($Format["PathOnly"]) ? $Format["PathOnly"] : FALSE; + $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; + + $Cpt = NULL; $Mode = NULL; $Result = ""; + for($i=1;$i<=count($Coordinates)-1;$i++) + { + $X1 = $Coordinates[$i-1][0]; $Y1 = $Coordinates[$i-1][1]; + $X2 = $Coordinates[$i][0]; $Y2 = $Coordinates[$i][1]; + + if ( $Forces != NULL ) { $Force = $Forces[$i]; } + + /* First segment */ + if ( $i == 1 ) + { $Xv1 = $X1; $Yv1 = $Y1; } + else + { + $Angle1 = $this->getAngle($XLast,$YLast,$X1,$Y1); + $Angle2 = $this->getAngle($X1,$Y1,$X2,$Y2); + $XOff = cos($Angle2 * PI / 180) * $Force + $X1; + $YOff = sin($Angle2 * PI / 180) * $Force + $Y1; + + $Xv1 = cos($Angle1 * PI / 180) * $Force + $XOff; + $Yv1 = sin($Angle1 * PI / 180) * $Force + $YOff; + } + + /* Last segment */ + if ( $i == count($Coordinates)-1 ) + { $Xv2 = $X2; $Yv2 = $Y2; } + else + { + $Angle1 = $this->getAngle($X2,$Y2,$Coordinates[$i+1][0],$Coordinates[$i+1][1]); + $Angle2 = $this->getAngle($X1,$Y1,$X2,$Y2); + $XOff = cos(($Angle2+180) * PI / 180) * $Force + $X2; + $YOff = sin(($Angle2+180) * PI / 180) * $Force + $Y2; + + $Xv2 = cos(($Angle1+180) * PI / 180) * $Force + $XOff; + $Yv2 = sin(($Angle1+180) * PI / 180) * $Force + $YOff; + } + + $Path = $this->drawBezier($X1,$Y1,$X2,$Y2,$Xv1,$Yv1,$Xv2,$Yv2,$Format); + if ($PathOnly) { $Result[] = $Path; } + + $XLast = $X1; $YLast = $Y1; + } + + return($Result); + } + + /* Draw a bezier curve with two controls points */ + function drawBezier($X1,$Y1,$X2,$Y2,$Xv1,$Yv1,$Xv2,$Yv2,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $ShowC = isset($Format["ShowControl"]) ? $Format["ShowControl"] : FALSE; + $Segments = isset($Format["Segments"]) ? $Format["Segments"] : NULL; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; + $NoDraw = isset($Format["NoDraw"]) ? $Format["NoDraw"] : FALSE; + $PathOnly = isset($Format["PathOnly"]) ? $Format["PathOnly"] : FALSE; + $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; + $DrawArrow = isset($Format["DrawArrow"]) ? $Format["DrawArrow"] : FALSE; + $ArrowSize = isset($Format["ArrowSize"]) ? $Format["ArrowSize"] : 10; + $ArrowRatio = isset($Format["ArrowRatio"]) ? $Format["ArrowRatio"] : .5; + $ArrowTwoHeads = isset($Format["ArrowTwoHeads"]) ? $Format["ArrowTwoHeads"] : FALSE; + + if ( $Segments == NULL ) + { + $Length = $this->getLength($X1,$Y1,$X2,$Y2); + $Precision = ($Length*125)/1000; + } + else + $Precision = $Segments; + + $P[0]["X"] = $X1; $P[0]["Y"] = $Y1; + $P[1]["X"] = $Xv1; $P[1]["Y"] = $Yv1; + $P[2]["X"] = $Xv2; $P[2]["Y"] = $Yv2; + $P[3]["X"] = $X2; $P[3]["Y"] = $Y2; + + /* Compute the bezier points */ + $Q = ""; $ID = 0; $Path = ""; + for($i=0;$i<=$Precision;$i=$i+1) + { + $u = $i / $Precision; + + $C = ""; + $C[0] = (1 - $u) * (1 - $u) * (1 - $u); + $C[1] = ($u * 3) * (1 - $u) * (1 - $u); + $C[2] = 3 * $u * $u * (1 - $u); + $C[3] = $u * $u * $u; + + for($j=0;$j<=3;$j++) + { + if ( !isset($Q[$ID]) ) { $Q[$ID] = ""; } + if ( !isset($Q[$ID]["X"]) ) { $Q[$ID]["X"] = 0; } + if ( !isset($Q[$ID]["Y"]) ) { $Q[$ID]["Y"] = 0; } + + $Q[$ID]["X"] = $Q[$ID]["X"] + $P[$j]["X"] * $C[$j]; + $Q[$ID]["Y"] = $Q[$ID]["Y"] + $P[$j]["Y"] * $C[$j]; + } + $ID++; + } + $Q[$ID]["X"] = $X2; $Q[$ID]["Y"] = $Y2; + + if ( !$NoDraw ) + { + /* Display the control points */ + if ( $ShowC && !$PathOnly ) + { + $Xv1 = floor($Xv1); $Yv1 = floor($Yv1); $Xv2 = floor($Xv2); $Yv2 = floor($Yv2); + + $this->drawLine($X1,$Y1,$X2,$Y2,array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>30)); + + $MyMarkerSettings = array("R"=>255,"G"=>0,"B"=>0,"BorderR"=>255,"BorderB"=>255,"BorderG"=>255,"Size"=>4); + $this->drawRectangleMarker($Xv1,$Yv1,$MyMarkerSettings); + $this->drawText($Xv1+4,$Yv1,"v1"); + $MyMarkerSettings = array("R"=>0,"G"=>0,"B"=>255,"BorderR"=>255,"BorderB"=>255,"BorderG"=>255,"Size"=>4); + $this->drawRectangleMarker($Xv2,$Yv2,$MyMarkerSettings); + $this->drawText($Xv2+4,$Yv2,"v2"); + } + + /* Draw the bezier */ + $LastX = NULL; $LastY = NULL; $Cpt = NULL; $Mode = NULL; $ArrowS = NULL; + foreach ($Q as $Key => $Point) + { + $X = $Point["X"]; $Y = $Point["Y"]; + + /* Get the first segment */ + if ( $ArrowS == NULL && $LastX != NULL && $LastY != NULL ) + { $ArrowS["X2"] = $LastX; $ArrowS["Y2"] = $LastY; $ArrowS["X1"] = $X; $ArrowS["Y1"] = $Y; } + + if ( $LastX != NULL && $LastY != NULL && !$PathOnly) + list($Cpt,$Mode) = $this->drawLine($LastX,$LastY,$X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Cpt"=>$Cpt,"Mode"=>$Mode,"Weight"=>$Weight)); + + /* Get the last segment */ + $ArrowE["X1"] = $LastX; $ArrowE["Y1"] = $LastY; $ArrowE["X2"] = $X; $ArrowE["Y2"] = $Y; + + $LastX = $X; $LastY = $Y; + } + + if ( $DrawArrow && !$PathOnly ) + { + $ArrowSettings = array("FillR"=>$R,"FillG"=>$G,"FillB"=>$B,"Alpha"=>$Alpha,"Size"=>$ArrowSize,"Ratio"=>$ArrowRatio); + if ( $ArrowTwoHeads ) + $this->drawArrow($ArrowS["X1"],$ArrowS["Y1"],$ArrowS["X2"],$ArrowS["Y2"],$ArrowSettings); + + $this->drawArrow($ArrowE["X1"],$ArrowE["Y1"],$ArrowE["X2"],$ArrowE["Y2"],$ArrowSettings); + } + } + return($Q); + } + + /* Draw a line between two points */ + function drawLine($X1,$Y1,$X2,$Y2,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; + $Cpt = isset($Format["Cpt"]) ? $Format["Cpt"] : 1; + $Mode = isset($Format["Mode"]) ? $Format["Mode"] : 1; + $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; + $Threshold = isset($Format["Threshold"]) ? $Format["Threshold"] : NULL; + + if ( $this->Antialias == FALSE && $Ticks == NULL ) + { + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $ShadowColor = $this->allocateColor($this->Picture,$this->ShadowR,$this->ShadowG,$this->ShadowB,$this->Shadowa); + imageline($this->Picture,$X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,$ShadowColor); + } + + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + imageline($this->Picture,$X1,$Y1,$X2,$Y2,$Color); + return(0); + } + + $Distance = sqrt(($X2-$X1)*($X2-$X1)+($Y2-$Y1)*($Y2-$Y1)); + if ( $Distance == 0 ) { return(-1); } + + /* Derivative algorithm for overweighted lines, re-route to polygons primitives */ + if ( $Weight != NULL ) + { + $Angle = $this->getAngle($X1,$Y1,$X2,$Y2); + $PolySettings = array ("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderAlpha"=>$Alpha); + + if ( $Ticks == NULL ) + { + $Points = ""; + $Points[] = cos(deg2rad($Angle-90)) * $Weight + $X1; $Points[] = sin(deg2rad($Angle-90)) * $Weight + $Y1; + $Points[] = cos(deg2rad($Angle+90)) * $Weight + $X1; $Points[] = sin(deg2rad($Angle+90)) * $Weight + $Y1; + $Points[] = cos(deg2rad($Angle+90)) * $Weight + $X2; $Points[] = sin(deg2rad($Angle+90)) * $Weight + $Y2; + $Points[] = cos(deg2rad($Angle-90)) * $Weight + $X2; $Points[] = sin(deg2rad($Angle-90)) * $Weight + $Y2; + + $this->drawPolygon($Points,$PolySettings); + } + else + { + for($i=0;$i<=$Distance;$i=$i+$Ticks*2) + { + $Xa = (($X2-$X1)/$Distance) * $i + $X1; $Ya = (($Y2-$Y1)/$Distance) * $i + $Y1; + $Xb = (($X2-$X1)/$Distance) * ($i+$Ticks) + $X1; $Yb = (($Y2-$Y1)/$Distance) * ($i+$Ticks) + $Y1; + + $Points = ""; + $Points[] = cos(deg2rad($Angle-90)) * $Weight + $Xa; $Points[] = sin(deg2rad($Angle-90)) * $Weight + $Ya; + $Points[] = cos(deg2rad($Angle+90)) * $Weight + $Xa; $Points[] = sin(deg2rad($Angle+90)) * $Weight + $Ya; + $Points[] = cos(deg2rad($Angle+90)) * $Weight + $Xb; $Points[] = sin(deg2rad($Angle+90)) * $Weight + $Yb; + $Points[] = cos(deg2rad($Angle-90)) * $Weight + $Xb; $Points[] = sin(deg2rad($Angle-90)) * $Weight + $Yb; + + $this->drawPolygon($Points,$PolySettings); + } + } + + return(1); + } + + $XStep = ($X2-$X1) / $Distance; + $YStep = ($Y2-$Y1) / $Distance; + + for($i=0;$i<=$Distance;$i++) + { + $X = $i * $XStep + $X1; + $Y = $i * $YStep + $Y1; + + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); + + if ( $Threshold != NULL ) + { + foreach($Threshold as $Key => $Parameters) + { + if ( $Y <= $Parameters["MinX"] && $Y >= $Parameters["MaxX"]) + { + if ( isset($Parameters["R"]) ) { $RT = $Parameters["R"]; } else { $RT = 0; } + if ( isset($Parameters["G"]) ) { $GT = $Parameters["G"]; } else { $GT = 0; } + if ( isset($Parameters["B"]) ) { $BT = $Parameters["B"]; } else { $BT = 0; } + if ( isset($Parameters["Alpha"]) ) { $AlphaT = $Parameters["Alpha"]; } else { $AlphaT = 0; } + $Color = array("R"=>$RT,"G"=>$GT,"B"=>$BT,"Alpha"=>$AlphaT); + } + } + } + + if ( $Ticks != NULL ) + { + if ( $Cpt % $Ticks == 0 ) + { $Cpt = 0; if ( $Mode == 1 ) { $Mode = 0; } else { $Mode = 1; } } + + if ( $Mode == 1 ) + $this->drawAntialiasPixel($X,$Y,$Color); + + $Cpt++; + } + else + $this->drawAntialiasPixel($X,$Y,$Color); + } + + return(array($Cpt,$Mode)); + } + + /* Draw a circle */ + function drawCircle($Xc,$Yc,$Height,$Width,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; + + $Height = abs($Height); + $Width = abs($Width); + + if ( $Height == 0 ) { $Height = 1; } + if ( $Width == 0 ) { $Width = 1; } + $Xc = floor($Xc); $Yc = floor($Yc); + + $RestoreShadow = $this->Shadow; + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $this->Shadow = FALSE; + $this->drawCircle($Xc+$this->ShadowX,$Yc+$this->ShadowY,$Height,$Width,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"Ticks"=>$Ticks)); + } + + if ( $Width == 0 ) { $Width = $Height; } + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $Step = 360 / (2 * PI * max($Width,$Height)); + $Mode = 1; $Cpt = 1; + for($i=0;$i<=360;$i=$i+$Step) + { + $X = cos($i*PI/180) * $Height + $Xc; + $Y = sin($i*PI/180) * $Width + $Yc; + + if ( $Ticks != NULL ) + { + if ( $Cpt % $Ticks == 0 ) + { $Cpt = 0; if ( $Mode == 1 ) { $Mode = 0; } else { $Mode = 1; } } + + if ( $Mode == 1 ) + $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + + $Cpt++; + } + else + $this->drawAntialiasPixel($X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + + } + $this->Shadow = $RestoreShadow; + } + + /* Draw a filled circle */ + function drawFilledCircle($X,$Y,$Radius,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; + $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + + if ( $Radius == 0 ) { $Radius = 1; } + if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } + $X = floor($X); $Y = floor($Y); + + $Radius = abs($Radius); + + $RestoreShadow = $this->Shadow; + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $this->Shadow = FALSE; + $this->drawFilledCircle($X+$this->ShadowX,$Y+$this->ShadowY,$Radius,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"Ticks"=>$Ticks)); + } + + $this->Mask = ""; + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + for ($i=0; $i<=$Radius*2; $i++) + { + $Slice = sqrt($Radius * $Radius - ($Radius - $i) * ($Radius - $i)); + $XPos = floor($Slice); + $YPos = $Y + $i - $Radius; + $AAlias = $Slice - floor($Slice); + + $this->Mask[$X-$XPos][$YPos] = TRUE; + $this->Mask[$X+$XPos][$YPos] = TRUE; + imageline($this->Picture,$X-$XPos,$YPos,$X+$XPos,$YPos,$Color); + } + if ( $this->Antialias ) + $this->drawCircle($X,$Y,$Radius,$Radius,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + + $this->Mask = ""; + + if ( $BorderR != -1 ) + $this->drawCircle($X,$Y,$Radius,$Radius,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$Ticks)); + + $this->Shadow = $RestoreShadow; + } + + /* Write text */ + function drawText($X,$Y,$Text,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : $this->FontColorR; + $G = isset($Format["G"]) ? $Format["G"] : $this->FontColorG; + $B = isset($Format["B"]) ? $Format["B"] : $this->FontColorB; + $Angle = isset($Format["Angle"]) ? $Format["Angle"] : 0; + $Align = isset($Format["Align"]) ? $Format["Align"] : TEXT_ALIGN_BOTTOMLEFT; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : $this->FontColorA; + $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; + $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; + $ShowOrigine = isset($Format["ShowOrigine"]) ? $Format["ShowOrigine"] : FALSE; + $TOffset = isset($Format["TOffset"]) ? $Format["TOffset"] : 2; + $DrawBox = isset($Format["DrawBox"]) ? $Format["DrawBox"] : FALSE; + $DrawBoxBorder = isset($Format["DrawBoxBorder"]) ? $Format["DrawBoxBorder"] : TRUE; + $BorderOffset = isset($Format["BorderOffset"]) ? $Format["BorderOffset"] : 6; + $BoxRounded = isset($Format["BoxRounded"]) ? $Format["BoxRounded"] : FALSE; + $RoundedRadius = isset($Format["RoundedRadius"]) ? $Format["RoundedRadius"] : 6; + $BoxR = isset($Format["BoxR"]) ? $Format["BoxR"] : 255; + $BoxG = isset($Format["BoxG"]) ? $Format["BoxG"] : 255; + $BoxB = isset($Format["BoxB"]) ? $Format["BoxB"] : 255; + $BoxAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 50; + $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : ""; + $BoxBorderR = isset($Format["BoxR"]) ? $Format["BoxR"] : 0; + $BoxBorderG = isset($Format["BoxG"]) ? $Format["BoxG"] : 0; + $BoxBorderB = isset($Format["BoxB"]) ? $Format["BoxB"] : 0; + $BoxBorderAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 50; + $NoShadow = isset($Format["NoShadow"]) ? $Format["NoShadow"] : FALSE; + + $Shadow = $this->Shadow; + if ( $NoShadow ) { $this->Shadow = FALSE; } + + if ( $BoxSurrounding != "" ) { $BoxBorderR = $BoxR - $BoxSurrounding; $BoxBorderG = $BoxG - $BoxSurrounding; $BoxBorderB = $BoxB - $BoxSurrounding; $BoxBorderAlpha = $BoxAlpha; } + + if ( $ShowOrigine ) + { + $MyMarkerSettings = array("R"=>255,"G"=>0,"B"=>0,"BorderR"=>255,"BorderB"=>255,"BorderG"=>255,"Size"=>4); + $this->drawRectangleMarker($X,$Y,$MyMarkerSettings); + } + + $TxtPos = $this->getTextBox($X,$Y,$FontName,$FontSize,$Angle,$Text); + + if ( $DrawBox && ($Angle == 0 || $Angle == 90 || $Angle == 180 || $Angle == 270)) + { + $T[0]["X"]=0;$T[0]["Y"]=0;$T[1]["X"]=0;$T[1]["Y"]=0;$T[2]["X"]=0;$T[2]["Y"]=0;$T[3]["X"]=0;$T[3]["Y"]=0; + if ( $Angle == 0 ) { $T[0]["X"]=-$TOffset;$T[0]["Y"]=$TOffset;$T[1]["X"]=$TOffset;$T[1]["Y"]=$TOffset;$T[2]["X"]=$TOffset;$T[2]["Y"]=-$TOffset;$T[3]["X"]=-$TOffset;$T[3]["Y"]=-$TOffset; } + + $X1 = min($TxtPos[0]["X"],$TxtPos[1]["X"],$TxtPos[2]["X"],$TxtPos[3]["X"]) - $BorderOffset + 3; + $Y1 = min($TxtPos[0]["Y"],$TxtPos[1]["Y"],$TxtPos[2]["Y"],$TxtPos[3]["Y"]) - $BorderOffset; + $X2 = max($TxtPos[0]["X"],$TxtPos[1]["X"],$TxtPos[2]["X"],$TxtPos[3]["X"]) + $BorderOffset + 3; + $Y2 = max($TxtPos[0]["Y"],$TxtPos[1]["Y"],$TxtPos[2]["Y"],$TxtPos[3]["Y"]) + $BorderOffset - 3; + + $X1 = $X1 - $TxtPos[$Align]["X"] + $X + $T[0]["X"]; + $Y1 = $Y1 - $TxtPos[$Align]["Y"] + $Y + $T[0]["Y"]; + $X2 = $X2 - $TxtPos[$Align]["X"] + $X + $T[0]["X"]; + $Y2 = $Y2 - $TxtPos[$Align]["Y"] + $Y + $T[0]["Y"]; + + $Settings = array("R"=>$BoxR,"G"=>$BoxG,"B"=>$BoxB,"Alpha"=>$BoxAlpha,"BorderR"=>$BoxBorderR,"BorderG"=>$BoxBorderG,"BorderB"=>$BoxBorderB,"BorderAlpha"=>$BoxBorderAlpha); + + if ( $BoxRounded ) + { $this->drawRoundedFilledRectangle($X1,$Y1,$X2,$Y2,$RoundedRadius,$Settings); } + else + { $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$Settings); } + } + + $X = $X - $TxtPos[$Align]["X"] + $X; + $Y = $Y - $TxtPos[$Align]["Y"] + $Y; + + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $C_ShadowColor = $this->allocateColor($this->Picture,$this->ShadowR,$this->ShadowG,$this->ShadowB,$this->Shadowa); + imagettftext($this->Picture,$FontSize,$Angle,$X+$this->ShadowX,$Y+$this->ShadowY,$C_ShadowColor,$FontName,$Text); + } + + $C_TextColor = $this->AllocateColor($this->Picture,$R,$G,$B,$Alpha); + imagettftext($this->Picture,$FontSize,$Angle,$X,$Y,$C_TextColor,$FontName,$Text); + + $this->Shadow = $Shadow; + + return($TxtPos); + } + + /* Draw a gradient within a defined area */ + function drawGradientArea($X1,$Y1,$X2,$Y2,$Direction,$Format="") + { + $StartR = isset($Format["StartR"]) ? $Format["StartR"] : 90; + $StartG = isset($Format["StartG"]) ? $Format["StartG"] : 90; + $StartB = isset($Format["StartB"]) ? $Format["StartB"] : 90; + $EndR = isset($Format["EndR"]) ? $Format["EndR"] : 0; + $EndG = isset($Format["EndG"]) ? $Format["EndG"] : 0; + $EndB = isset($Format["EndB"]) ? $Format["EndB"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Levels = isset($Format["Levels"]) ? $Format["Levels"] : NULL; + + $Shadow = $this->Shadow; + $this->Shadow = FALSE; + + if ( $StartR == $EndR && $StartG == $EndG && $StartB == $EndB ) + { + $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,array("R"=>$StartR,"G"=>$StartG,"B"=>$StartB,"Alpha"=>$Alpha)); + return(0); + } + + if ( $Levels != NULL ) + { $EndR=$StartR+$Levels; $EndG=$StartG+$Levels; $EndB=$StartB+$Levels; } + + if ($X1 > $X2) { list($X1, $X2) = array($X2, $X1); } + if ($Y1 > $Y2) { list($Y1, $Y2) = array($Y2, $Y1); } + + if ( $Direction == DIRECTION_VERTICAL ) { $Width = abs($Y2-$Y1); } + if ( $Direction == DIRECTION_HORIZONTAL ) { $Width = abs($X2-$X1); } + + $Step = max(abs($EndR-$StartR),abs($EndG-$StartG),abs($EndB-$StartB)); + $StepSize = $Width/$Step; + $RStep = ($EndR-$StartR)/$Step; + $GStep = ($EndG-$StartG)/$Step; + $BStep = ($EndB-$StartB)/$Step; + + $R=$StartR;$G=$StartG;$B=$StartB; + switch($Direction) + { + case DIRECTION_VERTICAL: + $StartY = $Y1; $EndY = floor($Y2)+1; $LastY2 = $StartY; + for($i=0;$i<=$Step;$i++) + { + $Y2 = floor($StartY + ($i * $StepSize)); + + if ($Y2 > $EndY) { $Y2 = $EndY; } + if (($Y1 != $Y2 && $Y1 < $Y2) || $Y2 == $EndY) + { + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); + $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$Color); + $LastY2 = max($LastY2,$Y2); + $Y1 = $Y2+1; + } + $R = $R + $RStep; $G = $G + $GStep; $B = $B + $BStep; + } + if ( $LastY2 < $EndY && isset($Color)) { for ($i=$LastY2+1;$i<=$EndY;$i++) { $this->drawLine($X1,$i,$X2,$i,$Color); } } + break; + + case DIRECTION_HORIZONTAL: + $StartX = $X1; $EndX = $X2; + for($i=0;$i<=$Step;$i++) + { + $X2 = floor($StartX + ($i * $StepSize)); + + if ($X2 > $EndX) { $X2 = $EndX; } + if (($X1 != $X2 && $X1 < $X2) || $X2 == $EndX) + { + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); + $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$Color); + $X1 = $X2+1; + } + $R = $R + $RStep; $G = $G + $GStep; $B = $B + $BStep; + } + if ( $X2 < $EndX && isset($Color)) { $this->drawFilledRectangle($X2,$Y1,$EndX,$Y2,$Color); } + break; + } + + $this->Shadow = $Shadow; + + } + + /* Draw an aliased pixel */ + function drawAntialiasPixel($X,$Y,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + + if ( $X < 0 || $Y < 0 || $X >= $this->XSize || $Y >= $this->YSize ) + return(-1); + + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + if ( !$this->Antialias ) + { + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $ShadowColor = $this->allocateColor($this->Picture,$this->ShadowR,$this->ShadowG,$this->ShadowB,$this->Shadowa); + imagesetpixel($this->Picture,$X+$this->ShadowX,$Y+$this->ShadowY,$ShadowColor); + } + + $PlotColor = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + imagesetpixel($this->Picture,$X,$Y,$PlotColor); + + return(0); + } + + $Plot = ""; + $Xi = floor($X); + $Yi = floor($Y); + + if ( $Xi == $X && $Yi == $Y) + { + if ( $Alpha == 100 ) + $this->drawAlphaPixel($X,$Y,100,$R,$G,$B); + else + $this->drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B); + } + else + { + $Alpha1 = (((1 - ($X - floor($X))) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha; + if ( $Alpha1 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi,$Alpha1,$R,$G,$B); } + + $Alpha2 = ((($X - floor($X)) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha; + if ( $Alpha2 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi,$Alpha2,$R,$G,$B); } + + $Alpha3 = (((1 - ($X - floor($X))) * ($Y - floor($Y)) * 100) / 100) * $Alpha; + if ( $Alpha3 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi+1,$Alpha3,$R,$G,$B); } + + $Alpha4 = ((($X - floor($X)) * ($Y - floor($Y)) * 100) / 100) * $Alpha; + if ( $Alpha4 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi+1,$Alpha4,$R,$G,$B); } + } + } + + /* Draw a semi-transparent pixel */ + function drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B) + { + if ( isset($this->Mask[$X])) { if ( isset($this->Mask[$X][$Y]) ) { return(0); } } + + if ( $X < 0 || $Y < 0 || $X >= $this->XSize || $Y >= $this->YSize ) + return(-1); + + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $AlphaFactor = floor(($Alpha / 100) * $this->Shadowa); + $ShadowColor = $this->allocateColor($this->Picture,$this->ShadowR,$this->ShadowG,$this->ShadowB,$AlphaFactor); + imagesetpixel($this->Picture,$X+$this->ShadowX,$Y+$this->ShadowY,$ShadowColor); + } + + $C_Aliased = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + imagesetpixel($this->Picture,$X,$Y,$C_Aliased); + } + + /* Convert apha to base 10 */ + function convertAlpha($AlphaValue) + { return((127/100)*(100-$AlphaValue)); } + + /* Allocate a color with transparency */ + function allocateColor($Picture,$R,$G,$B,$Alpha=100) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + if ( $Alpha < 0 ) { $Alpha = 0; } + if ( $Alpha > 100) { $Alpha = 100; } + + $Alpha = $this->convertAlpha($Alpha); + return(imagecolorallocatealpha($Picture,$R,$G,$B,$Alpha)); + } + + /* Load a PNG file and draw it over the chart */ + function drawFromPNG($X,$Y,$FileName) + { $this->drawFromPicture(1,$FileName,$X,$Y); } + + /* Load a GIF file and draw it over the chart */ + function drawFromGIF($X,$Y,$FileName) + { $this->drawFromPicture(2,$FileName,$X,$Y); } + + /* Load a JPEG file and draw it over the chart */ + function drawFromJPG($X,$Y,$FileName) + { $this->drawFromPicture(3,$FileName,$X,$Y); } + + function getPicInfo($FileName) + { + $Infos = getimagesize($FileName); + $Width = $Infos[0]; + $Height = $Infos[1]; + $Type = $Infos["mime"]; + + if ( $Type == "image/png") { $Type = 1; } + if ( $Type == "image/gif") { $Type = 2; } + if ( $Type == "image/jpeg ") { $Type = 3; } + + return(array($Width,$Height,$Type)); + } + + /* Generic loader function for external pictures */ + function drawFromPicture($PicType,$FileName,$X,$Y) + { + if ( file_exists($FileName)) + { + list($Width,$Height) = $this->getPicInfo($FileName); + + if ( $PicType == 1 ) + { $Raster = imagecreatefrompng($FileName); } + elseif ( $PicType == 2 ) + { $Raster = imagecreatefromgif($FileName); } + elseif ( $PicType == 3 ) + { $Raster = imagecreatefromjpeg($FileName); } + else + { return(0); } + + + $RestoreShadow = $this->Shadow; + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $this->Shadow = FALSE; + if ( $PicType == 3 ) + $this->drawFilledRectangle($X+$this->ShadowX,$Y+$this->ShadowY,$X+$Width+$this->ShadowX,$Y+$Height+$this->ShadowY,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); + else + { + $TranparentID = imagecolortransparent($Raster); + for ($Xc=0;$Xc<=$Width-1;$Xc++) + { + for ($Yc=0;$Yc<=$Height-1;$Yc++) + { + $RGBa = imagecolorat($Raster,$Xc,$Yc); + $Values = imagecolorsforindex($Raster,$RGBa); + if ( $Values["alpha"] < 120 ) + { + $AlphaFactor = floor(($this->Shadowa / 100) * ((100 / 127) * (127-$Values["alpha"]))); + $this->drawAlphaPixel($X+$Xc+$this->ShadowX,$Y+$Yc+$this->ShadowY,$AlphaFactor,$this->ShadowR,$this->ShadowG,$this->ShadowB); + } + } + } + } + } + $this->Shadow = $RestoreShadow; + + imagecopy($this->Picture,$Raster,$X,$Y,0,0,$Width,$Height); + imagedestroy($Raster); + } + } + + /* Draw an arrow */ + function drawArrow($X1,$Y1,$X2,$Y2,$Format="") + { + $FillR = isset($Format["FillR"]) ? $Format["FillR"] : 0; + $FillG = isset($Format["FillG"]) ? $Format["FillG"] : 0; + $FillB = isset($Format["FillB"]) ? $Format["FillB"] : 0; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $FillR; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $FillG; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $FillB; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Size = isset($Format["Size"]) ? $Format["Size"] : 10; + $Ratio = isset($Format["Ratio"]) ? $Format["Ratio"] : .5; + $TwoHeads = isset($Format["TwoHeads"]) ? $Format["TwoHeads"] : FALSE; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : FALSE; + + /* Calculate the line angle */ + $Angle = $this->getAngle($X1,$Y1,$X2,$Y2); + + /* Override Shadow support, this will be managed internally */ + $RestoreShadow = $this->Shadow; + if ( $this->Shadow && $this->ShadowX != 0 && $this->ShadowY != 0 ) + { + $this->Shadow = FALSE; + $this->drawArrow($X1+$this->ShadowX,$Y1+$this->ShadowY,$X2+$this->ShadowX,$Y2+$this->ShadowY,array("FillR"=>$this->ShadowR,"FillG"=>$this->ShadowG,"FillB"=>$this->ShadowB,"Alpha"=>$this->Shadowa,"Size"=>$Size,"Ratio"=>$Ratio,"TwoHeads"=>$TwoHeads,"Ticks"=>$Ticks)); + } + + /* Draw the 1st Head */ + $TailX = cos(($Angle-180)*PI/180)*$Size+$X2; + $TailY = sin(($Angle-180)*PI/180)*$Size+$Y2; + + $Points = ""; + $Points[] = $X2; $Points[] = $Y2; + $Points[] = cos(($Angle-90)*PI/180)*$Size*$Ratio+$TailX; $Points[] = sin(($Angle-90)*PI/180)*$Size*$Ratio+$TailY; + $Points[] = cos(($Angle-270)*PI/180)*$Size*$Ratio+$TailX; $Points[] = sin(($Angle-270)*PI/180)*$Size*$Ratio+$TailY; + $Points[] = $X2; $Points[] = $Y2; + + /* Visual correction */ + if ($Angle == 180 || $Angle == 360 ) { $Points[4] = $Points[2]; } + if ($Angle == 90 || $Angle == 270 ) { $Points[5] = $Points[3]; } + + $ArrowColor = $this->allocateColor($this->Picture,$FillR,$FillG,$FillB,$Alpha); + ImageFilledPolygon($this->Picture,$Points,4,$ArrowColor); + + $this->drawLine($Points[0],$Points[1],$Points[2],$Points[3],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); + $this->drawLine($Points[2],$Points[3],$Points[4],$Points[5],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); + $this->drawLine($Points[0],$Points[1],$Points[4],$Points[5],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); + + /* Draw the second head */ + if ( $TwoHeads ) + { + $Angle = $this->getAngle($X2,$Y2,$X1,$Y1); + + $TailX2 = cos(($Angle-180)*PI/180)*$Size+$X1; + $TailY2 = sin(($Angle-180)*PI/180)*$Size+$Y1; + + $Points = ""; + $Points[] = $X1; $Points[] = $Y1; + $Points[] = cos(($Angle-90)*PI/180)*$Size*$Ratio+$TailX2; $Points[] = sin(($Angle-90)*PI/180)*$Size*$Ratio+$TailY2; + $Points[] = cos(($Angle-270)*PI/180)*$Size*$Ratio+$TailX2; $Points[] = sin(($Angle-270)*PI/180)*$Size*$Ratio+$TailY2; + $Points[] = $X1; $Points[] = $Y1; + + /* Visual correction */ + if ($Angle == 180 || $Angle == 360 ) { $Points[4] = $Points[2]; } + if ($Angle == 90 || $Angle == 270 ) { $Points[5] = $Points[3]; } + + $ArrowColor = $this->allocateColor($this->Picture,$FillR,$FillG,$FillB,$Alpha); + ImageFilledPolygon($this->Picture,$Points,4,$ArrowColor); + + $this->drawLine($Points[0],$Points[1],$Points[2],$Points[3],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); + $this->drawLine($Points[2],$Points[3],$Points[4],$Points[5],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); + $this->drawLine($Points[0],$Points[1],$Points[4],$Points[5],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); + + $this->drawLine($TailX,$TailY,$TailX2,$TailY2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + } + else + $this->drawLine($X1,$Y1,$TailX,$TailY,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + + /* Re-enable shadows */ + $this->Shadow = $RestoreShadow; + } + + /* Draw a label with associated arrow */ + function drawArrowLabel($X1,$Y1,$Text,$Format="") + { + $FillR = isset($Format["FillR"]) ? $Format["FillR"] : 0; + $FillG = isset($Format["FillG"]) ? $Format["FillG"] : 0; + $FillB = isset($Format["FillB"]) ? $Format["FillB"] : 0; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $FillR; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $FillG; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $FillB; + $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; + $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Length = isset($Format["Length"]) ? $Format["Length"] : 50; + $Angle = isset($Format["Angle"]) ? $Format["Angle"] : 315; + $Size = isset($Format["Size"]) ? $Format["Size"] : 10; + $Position = isset($Format["Position"]) ? $Format["Position"] : POSITION_TOP; + $RoundPos = isset($Format["RoundPos"]) ? $Format["RoundPos"] : FALSE; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; + + $Angle = $Angle % 360; + + $X2 = sin(($Angle+180)*PI/180)*$Length+$X1; + $Y2 = cos(($Angle+180)*PI/180)*$Length+$Y1; + + if ( $RoundPos && $Angle > 0 && $Angle < 180 ) { $Y2 = ceil($Y2); } + if ( $RoundPos && $Angle > 180 ) { $Y2 = floor($Y2); } + + $this->drawArrow($X2,$Y2,$X1,$Y1,$Format); + + $Size = imagettfbbox($FontSize,0,$FontName,$Text); + $TxtWidth = max(abs($Size[2]-$Size[0]),abs($Size[0]-$Size[6])); + $TxtHeight = max(abs($Size[1]-$Size[7]),abs($Size[3]-$Size[1])); + + if ( $Angle > 0 && $Angle < 180 ) + { + $this->drawLine($X2,$Y2,$X2-$TxtWidth,$Y2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + if ( $Position == POSITION_TOP ) + $this->drawText($X2,$Y2-2,$Text,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Align"=>TEXT_ALIGN_BOTTOMRIGHT)); + else + $this->drawText($X2,$Y2+4,$Text,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Align"=>TEXT_ALIGN_TOPRIGHT)); + } + else + { + $this->drawLine($X2,$Y2,$X2+$TxtWidth,$Y2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + if ( $Position == POSITION_TOP ) + $this->drawText($X2,$Y2-2,$Text,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha)); + else + $this->drawText($X2,$Y2+4,$Text,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$Alpha,"Align"=>TEXT_ALIGN_TOPLEFT)); + } + } + + /* Draw a progress bar filled with specified % */ + function drawProgress($X,$Y,$Percent,$Format="") + { + if ( $Percent > 100 ) { $Percent = 100; } + if ( $Percent < 0 ) { $Percent = 0; } + + $Width = isset($Format["Width"]) ? $Format["Width"] : 200; + $Height = isset($Format["Height"]) ? $Format["Height"] : 20; + $Orientation = isset($Format["Orientation"]) ? $Format["Orientation"] : ORIENTATION_HORIZONTAL; + $ShowLabel = isset($Format["ShowLabel"]) ? $Format["ShowLabel"] : FALSE; + $LabelPos = isset($Format["LabelPos"]) ? $Format["LabelPos"] : LABEL_POS_INSIDE; + $Margin = isset($Format["Margin"]) ? $Format["Margin"] : 10; + $R = isset($Format["R"]) ? $Format["R"] : 130; + $G = isset($Format["G"]) ? $Format["G"] : 130; + $B = isset($Format["B"]) ? $Format["B"] : 130; + $RFade = isset($Format["RFade"]) ? $Format["RFade"] : -1; + $GFade = isset($Format["GFade"]) ? $Format["GFade"] : -1; + $BFade = isset($Format["BFade"]) ? $Format["BFade"] : -1; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; + $BoxBorderR = isset($Format["BoxBorderR"]) ? $Format["BoxBorderR"] : 0; + $BoxBorderG = isset($Format["BoxBorderG"]) ? $Format["BoxBorderG"] : 0; + $BoxBorderB = isset($Format["BoxBorderB"]) ? $Format["BoxBorderB"] : 0; + $BoxBackR = isset($Format["BoxBackR"]) ? $Format["BoxBackR"] : 255; + $BoxBackG = isset($Format["BoxBackG"]) ? $Format["BoxBackG"] : 255; + $BoxBackB = isset($Format["BoxBackB"]) ? $Format["BoxBackB"] : 255; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : NULL; + $NoAngle = isset($Format["NoAngle"]) ? $Format["NoAngle"] : FALSE; + + if ( $RFade != -1 && $GFade != -1 && $BFade != -1 ) + { + $RFade = (($RFade-$R)/100)*$Percent+$R; + $GFade = (($GFade-$G)/100)*$Percent+$G; + $BFade = (($BFade-$B)/100)*$Percent+$B; + } + + if ( $Surrounding != NULL ) { $BorderR = $R + $Surrounding; $BorderG = $G + $Surrounding; $BorderB = $B + $Surrounding; } + if ( $BoxSurrounding != NULL ) { $BoxBorderR = $BoxBackR + $Surrounding; $BoxBorderG = $BoxBackG + $Surrounding; $BoxBorderB = $BoxBackB + $Surrounding; } + + if ( $Orientation == ORIENTATION_VERTICAL ) + { + $InnerHeight = (($Height-2)/100)*$Percent; + $this->drawFilledRectangle($X,$Y,$X+$Width,$Y-$Height,array("R"=>$BoxBackR,"G"=>$BoxBackG,"B"=>$BoxBackB,"BorderR"=>$BoxBorderR,"BorderG"=>$BoxBorderG,"BorderB"=>$BoxBorderB,"NoAngle"=>$NoAngle)); + + $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; + if ( $RFade != -1 && $GFade != -1 && $BFade != -1 ) + { + $GradientOptions = array("StartR"=>$RFade,"StartG"=>$GFade,"StartB"=>$BFade,"EndR"=>$R,"EndG"=>$G,"EndB"=>$B); + $this->drawGradientArea($X+1,$Y-1,$X+$Width-1,$Y-$InnerHeight,DIRECTION_VERTICAL,$GradientOptions); + + if ( $Surrounding ) + $this->drawRectangle($X+1,$Y-1,$X+$Width-1,$Y-$InnerHeight,array("R"=>255,"G"=>255,"B"=>255,"Alpha"=>$Surrounding)); + } + else + $this->drawFilledRectangle($X+1,$Y-1,$X+$Width-1,$Y-$InnerHeight,array("R"=>$R,"G"=>$G,"B"=>$B,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); + + $this->Shadow = $RestoreShadow; + + if ( $ShowLabel && $LabelPos == LABEL_POS_BOTTOM ) { $this->drawText($X+($Width/2),$Y+$Margin,$Percent."%",array("Align"=>TEXT_ALIGN_TOPMIDDLE)); } + if ( $ShowLabel && $LabelPos == LABEL_POS_TOP ) { $this->drawText($X+($Width/2),$Y-$Height-$Margin,$Percent."%",array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); } + if ( $ShowLabel && $LabelPos == LABEL_POS_INSIDE ) { $this->drawText($X+($Width/2),$Y-$InnerHeight-$Margin,$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLELEFT,"Angle"=>90)); } + if ( $ShowLabel && $LabelPos == LABEL_POS_CENTER ) { $this->drawText($X+($Width/2),$Y-($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"Angle"=>90)); } + } + else + { + if ( $Percent == 100 ) + $InnerWidth = $Width-1; + else + $InnerWidth = (($Width-2)/100)*$Percent; + + $this->drawFilledRectangle($X,$Y,$X+$Width,$Y+$Height,array("R"=>$BoxBackR,"G"=>$BoxBackG,"B"=>$BoxBackB,"BorderR"=>$BoxBorderR,"BorderG"=>$BoxBorderG,"BorderB"=>$BoxBorderB,"NoAngle"=>$NoAngle)); + + $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; + if ( $RFade != -1 && $GFade != -1 && $BFade != -1 ) + { + $GradientOptions = array("StartR"=>$R,"StartG"=>$G,"StartB"=>$B,"EndR"=>$RFade,"EndG"=>$GFade,"EndB"=>$BFade); + $this->drawGradientArea($X+1,$Y+1,$X+$InnerWidth,$Y+$Height-1,DIRECTION_HORIZONTAL,$GradientOptions); + + if ( $Surrounding ) + $this->drawRectangle($X+1,$Y+1,$X+$InnerWidth,$Y+$Height-1,array("R"=>255,"G"=>255,"B"=>255,"Alpha"=>$Surrounding)); + } + else + $this->drawFilledRectangle($X+1,$Y+1,$X+$InnerWidth,$Y+$Height-1,array("R"=>$R,"G"=>$G,"B"=>$B,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); + + $this->Shadow = $RestoreShadow; + + if ( $ShowLabel && $LabelPos == LABEL_POS_LEFT ) { $this->drawText($X-$Margin,$Y+($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLERIGHT)); } + if ( $ShowLabel && $LabelPos == LABEL_POS_RIGHT ) { $this->drawText($X+$Width+$Margin,$Y+($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLELEFT)); } + if ( $ShowLabel && $LabelPos == LABEL_POS_CENTER ) { $this->drawText($X+($Width/2),$Y+($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); } + if ( $ShowLabel && $LabelPos == LABEL_POS_INSIDE ) { $this->drawText($X+$InnerWidth+$Margin,$Y+($Height/2),$Percent."%",array("Align"=>TEXT_ALIGN_MIDDLELEFT)); } + } + } + + /* Get the legend box size */ + function getLegendSize($Format="") + { + $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; + $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; + $BoxSize = isset($Format["BoxSize"]) ? $Format["BoxSize"] : 5; + $Margin = isset($Format["Margin"]) ? $Format["Margin"] : 5; + $Style = isset($Format["Style"]) ? $Format["Style"] : LEGEND_ROUND; + $Mode = isset($Format["Mode"]) ? $Format["Mode"] : LEGEND_VERTICAL; + $BoxWidth = isset($Format["BoxWidth"]) ? $Format["BoxWidth"] : 5; + $BoxHeight = isset($Format["BoxHeight"]) ? $Format["BoxHeight"] : 5; + $IconAreaWidth = isset($Format["IconAreaWidth"]) ? $Format["IconAreaWidth"] : $BoxWidth; + $IconAreaHeight = isset($Format["IconAreaHeight"]) ? $Format["IconAreaHeight"] : $BoxHeight; + $XSpacing = isset($Format["XSpacing"]) ? $Format["XSpacing"] : 5; + + $Data = $this->DataSet->getData(); + + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] && isset($Serie["Picture"])) + { + list($PicWidth,$PicHeight) = $this->getPicInfo($Serie["Picture"]); + if ( $IconAreaWidth < $PicWidth ) { $IconAreaWidth = $PicWidth; } + if ( $IconAreaHeight < $PicHeight ) { $IconAreaHeight = $PicHeight; } + } + } + + $YStep = max($this->FontSize,$IconAreaHeight) + 5; + $XStep = $IconAreaWidth + 5; + $XStep = $XSpacing; + + $X=100; $Y=100; + + $Boundaries = ""; $Boundaries["L"] = $X; $Boundaries["T"] = $Y; $Boundaries["R"] = 0; $Boundaries["B"] = 0; $vY = $Y; $vX = $X; + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + if ( $Mode == LEGEND_VERTICAL ) + { + $BoxArray = $this->getTextBox($vX+$IconAreaWidth+4,$vY+$IconAreaHeight/2,$FontName,$FontSize,0,$Serie["Description"]); + + if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$IconAreaHeight/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$IconAreaHeight/2; } + if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } + if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$IconAreaHeight/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$IconAreaHeight/2; } + + $Lines = preg_split("/\n/",$Serie["Description"]); + $vY = $vY + max($this->FontSize*count($Lines),$IconAreaHeight) + 5; + } + elseif ( $Mode == LEGEND_HORIZONTAL ) + { + $Lines = preg_split("/\n/",$Serie["Description"]); + $Width = ""; + foreach($Lines as $Key => $Value) + { + $BoxArray = $this->getTextBox($vX+$IconAreaWidth+6,$Y+$IconAreaHeight/2+(($this->FontSize+3)*$Key),$FontName,$FontSize,0,$Value); + + if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$IconAreaHeight/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$IconAreaHeight/2; } + if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } + if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$IconAreaHeight/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$IconAreaHeight/2; } + + $Width[] = $BoxArray[1]["X"]; + } + + $vX=max($Width)+$XStep; + } + } + } + $vY=$vY-$YStep; $vX=$vX-$XStep; + + $TopOffset = $Y - $Boundaries["T"]; + if ( $Boundaries["B"]-($vY+$IconAreaHeight) < $TopOffset ) { $Boundaries["B"] = $vY+$IconAreaHeight+$TopOffset; } + + $Width = ($Boundaries["R"]+$Margin) - ($Boundaries["L"]-$Margin); + $Height = ($Boundaries["B"]+$Margin) - ($Boundaries["T"]-$Margin); + + return(array("Width"=>$Width,"Height"=>$Height)); + } + + /* Draw the legend of the active series */ + function drawLegend($X,$Y,$Format="") + { + $Family = isset($Format["Family"]) ? $Format["Family"] : LEGEND_FAMILY_BOX; + $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; + $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; + $FontR = isset($Format["FontR"]) ? $Format["FontR"] : $this->FontColorR; + $FontG = isset($Format["FontG"]) ? $Format["FontG"] : $this->FontColorG; + $FontB = isset($Format["FontB"]) ? $Format["FontB"] : $this->FontColorB; + $BoxWidth = isset($Format["BoxWidth"]) ? $Format["BoxWidth"] : 5; + $BoxHeight = isset($Format["BoxHeight"]) ? $Format["BoxHeight"] : 5; + $IconAreaWidth = isset($Format["IconAreaWidth"]) ? $Format["IconAreaWidth"] : $BoxWidth; + $IconAreaHeight = isset($Format["IconAreaHeight"]) ? $Format["IconAreaHeight"] : $BoxHeight; + $XSpacing = isset($Format["XSpacing"]) ? $Format["XSpacing"] : 5; + $Margin = isset($Format["Margin"]) ? $Format["Margin"] : 5; + $R = isset($Format["R"]) ? $Format["R"] : 200; + $G = isset($Format["G"]) ? $Format["G"] : 200; + $B = isset($Format["B"]) ? $Format["B"] : 200; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 255; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 255; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 255; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + $Style = isset($Format["Style"]) ? $Format["Style"] : LEGEND_ROUND; + $Mode = isset($Format["Mode"]) ? $Format["Mode"] : LEGEND_VERTICAL; + + if ( $Surrounding != NULL ) { $BorderR = $R + $Surrounding; $BorderG = $G + $Surrounding; $BorderB = $B + $Surrounding; } + + $Data = $this->DataSet->getData(); + + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] && isset($Serie["Picture"])) + { + list($PicWidth,$PicHeight) = $this->getPicInfo($Serie["Picture"]); + if ( $IconAreaWidth < $PicWidth ) { $IconAreaWidth = $PicWidth; } + if ( $IconAreaHeight < $PicHeight ) { $IconAreaHeight = $PicHeight; } + } + } + + $YStep = max($this->FontSize,$IconAreaHeight) + 5; + $XStep = $IconAreaWidth + 5; + $XStep = $XSpacing; + + $Boundaries = ""; $Boundaries["L"] = $X; $Boundaries["T"] = $Y; $Boundaries["R"] = 0; $Boundaries["B"] = 0; $vY = $Y; $vX = $X; + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + if ( $Mode == LEGEND_VERTICAL ) + { + $BoxArray = $this->getTextBox($vX+$IconAreaWidth+4,$vY+$IconAreaHeight/2,$FontName,$FontSize,0,$Serie["Description"]); + + if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$IconAreaHeight/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$IconAreaHeight/2; } + if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } + if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$IconAreaHeight/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$IconAreaHeight/2; } + + $Lines = preg_split("/\n/",$Serie["Description"]); + $vY = $vY + max($this->FontSize*count($Lines),$IconAreaHeight) + 5; + } + elseif ( $Mode == LEGEND_HORIZONTAL ) + { + $Lines = preg_split("/\n/",$Serie["Description"]); + $Width = ""; + foreach($Lines as $Key => $Value) + { + $BoxArray = $this->getTextBox($vX+$IconAreaWidth+6,$Y+$IconAreaHeight/2+(($this->FontSize+3)*$Key),$FontName,$FontSize,0,$Value); + + if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$IconAreaHeight/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$IconAreaHeight/2; } + if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } + if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$IconAreaHeight/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$IconAreaHeight/2; } + + $Width[] = $BoxArray[1]["X"]; + } + + $vX=max($Width)+$XStep; + } + } + } + $vY=$vY-$YStep; $vX=$vX-$XStep; + + $TopOffset = $Y - $Boundaries["T"]; + if ( $Boundaries["B"]-($vY+$IconAreaHeight) < $TopOffset ) { $Boundaries["B"] = $vY+$IconAreaHeight+$TopOffset; } + + if ( $Style == LEGEND_ROUND ) + $this->drawRoundedFilledRectangle($Boundaries["L"]-$Margin,$Boundaries["T"]-$Margin,$Boundaries["R"]+$Margin,$Boundaries["B"]+$Margin,$Margin,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); + elseif ( $Style == LEGEND_BOX ) + $this->drawFilledRectangle($Boundaries["L"]-$Margin,$Boundaries["T"]-$Margin,$Boundaries["R"]+$Margin,$Boundaries["B"]+$Margin,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); + + $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; + $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; + + if ( isset($Serie["Picture"]) ) + { + $Picture = $Serie["Picture"]; + list($PicWidth,$PicHeight) = $this->getPicInfo($Picture); + $PicX = $X+$IconAreaWidth/2; $PicY = $Y+$IconAreaHeight/2; + + $this->drawFromPNG($PicX-$PicWidth/2,$PicY-$PicHeight/2,$Picture); + } + else + { + if ( $Family == LEGEND_FAMILY_BOX ) + { + if ( $BoxWidth != $IconAreaWidth ) { $XOffset = floor(($IconAreaWidth-$BoxWidth)/2); } else { $XOffset = 0; } + if ( $BoxHeight != $IconAreaHeight ) { $YOffset = floor(($IconAreaHeight-$BoxHeight)/2); } else { $YOffset = 0; } + + $this->drawFilledRectangle($X+1+$XOffset,$Y+1+$YOffset,$X+$BoxWidth+$XOffset+1,$Y+$BoxHeight+1+$YOffset,array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20)); + $this->drawFilledRectangle($X+$XOffset,$Y+$YOffset,$X+$BoxWidth+$XOffset,$Y+$BoxHeight+$YOffset,array("R"=>$R,"G"=>$G,"B"=>$B,"Surrounding"=>20)); + } + elseif ( $Family == LEGEND_FAMILY_CIRCLE ) + { + $this->drawFilledCircle($X+1+$IconAreaWidth/2,$Y+1+$IconAreaHeight/2,min($IconAreaHeight/2,$IconAreaWidth/2),array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20)); + $this->drawFilledCircle($X+$IconAreaWidth/2,$Y+$IconAreaHeight/2,min($IconAreaHeight/2,$IconAreaWidth/2),array("R"=>$R,"G"=>$G,"B"=>$B,"Surrounding"=>20)); + } + elseif ( $Family == LEGEND_FAMILY_LINE ) + { + $this->drawLine($X+1,$Y+1+$IconAreaHeight/2,$X+1+$IconAreaWidth,$Y+1+$IconAreaHeight/2,array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20,"Ticks"=>$Ticks,"Weight"=>$Weight)); + $this->drawLine($X,$Y+$IconAreaHeight/2,$X+$IconAreaWidth,$Y+$IconAreaHeight/2,array("R"=>$R,"G"=>$G,"B"=>$B,"Ticks"=>$Ticks,"Weight"=>$Weight)); + } + } + + if ( $Mode == LEGEND_VERTICAL ) + { + $Lines = preg_split("/\n/",$Serie["Description"]); + foreach($Lines as $Key => $Value) + $this->drawText($X+$IconAreaWidth+4,$Y+$IconAreaHeight/2+(($this->FontSize+3)*$Key),$Value,array("R"=>$FontR,"G"=>$FontG,"B"=>$FontB,"Align"=>TEXT_ALIGN_MIDDLELEFT,"FontSize"=>$FontSize,"FontName"=>$FontName)); + + $Y=$Y+max($this->FontSize*count($Lines),$IconAreaHeight) + 5; + } + elseif ( $Mode == LEGEND_HORIZONTAL ) + { + $Lines = preg_split("/\n/",$Serie["Description"]); + $Width = ""; + foreach($Lines as $Key => $Value) + { + $BoxArray = $this->drawText($X+$IconAreaWidth+4,$Y+$IconAreaHeight/2+(($this->FontSize+3)*$Key),$Value,array("R"=>$FontR,"G"=>$FontG,"B"=>$FontB,"Align"=>TEXT_ALIGN_MIDDLELEFT,"FontSize"=>$FontSize,"FontName"=>$FontName)); + $Width[] = $BoxArray[1]["X"]; + } + $X=max($Width)+2+$XStep; + } + } + } + + + $this->Shadow = $RestoreShadow; + } + + function drawScale($Format="") + { + $Pos = isset($Format["Pos"]) ? $Format["Pos"] : SCALE_POS_LEFTRIGHT; + $Floating = isset($Format["Floating"]) ? $Format["Floating"] : FALSE; + $Mode = isset($Format["Mode"]) ? $Format["Mode"] : SCALE_MODE_FLOATING; + $RemoveXAxis = isset($Format["RemoveXAxis"]) ? $Format["RemoveXAxis"] : FALSE; + $MinDivHeight = isset($Format["MinDivHeight"]) ? $Format["MinDivHeight"] : 20; + $Factors = isset($Format["Factors"]) ? $Format["Factors"] : array(1,2,5); + $ManualScale = isset($Format["ManualScale"]) ? $Format["ManualScale"] : array("0"=>array("Min"=>-100,"Max"=>100)); + $XMargin = isset($Format["XMargin"]) ? $Format["XMargin"] : AUTO; + $YMargin = isset($Format["YMargin"]) ? $Format["YMargin"] : 0; + $ScaleSpacing = isset($Format["ScaleSpacing"]) ? $Format["ScaleSpacing"] : 15; + $InnerTickWidth = isset($Format["InnerTickWidth"]) ? $Format["InnerTickWidth"] : 2; + $OuterTickWidth = isset($Format["OuterTickWidth"]) ? $Format["OuterTickWidth"] : 2; + $DrawXLines = isset($Format["DrawXLines"]) ? $Format["DrawXLines"] : TRUE; + $DrawYLines = isset($Format["DrawYLines"]) ? $Format["DrawYLines"] : ALL; + $GridTicks = isset($Format["GridTicks"]) ? $Format["GridTicks"] : 4; + $GridR = isset($Format["GridR"]) ? $Format["GridR"] : 255; + $GridG = isset($Format["GridG"]) ? $Format["GridG"] : 255; + $GridB = isset($Format["GridB"]) ? $Format["GridB"] : 255; + $GridAlpha = isset($Format["GridAlpha"]) ? $Format["GridAlpha"] : 40; + $AxisRo = isset($Format["AxisR"]) ? $Format["AxisR"] : 0; + $AxisGo = isset($Format["AxisG"]) ? $Format["AxisG"] : 0; + $AxisBo = isset($Format["AxisB"]) ? $Format["AxisB"] : 0; + $AxisAlpha = isset($Format["AxisAlpha"]) ? $Format["AxisAlpha"] : 100; + $TickRo = isset($Format["TickR"]) ? $Format["TickR"] : 0; + $TickGo = isset($Format["TickG"]) ? $Format["TickG"] : 0; + $TickBo = isset($Format["TickB"]) ? $Format["TickB"] : 0; + $TickAlpha = isset($Format["TickAlpha"]) ? $Format["TickAlpha"] : 100; + $DrawSubTicks = isset($Format["DrawSubTicks"]) ? $Format["DrawSubTicks"] : FALSE; + $InnerSubTickWidth = isset($Format["InnerSubTickWidth"]) ? $Format["InnerSubTickWidth"] : 0; + $OuterSubTickWidth = isset($Format["OuterSubTickWidth"]) ? $Format["OuterSubTickWidth"] : 2; + $SubTickR = isset($Format["SubTickR"]) ? $Format["SubTickR"] : 255; + $SubTickG = isset($Format["SubTickG"]) ? $Format["SubTickG"] : 0; + $SubTickB = isset($Format["SubTickB"]) ? $Format["SubTickB"] : 0; + $SubTickAlpha = isset($Format["SubTickAlpha"]) ? $Format["SubTickAlpha"] : 100; + $AutoAxisLabels = isset($Format["AutoAxisLabels"]) ? $Format["AutoAxisLabels"] : TRUE; + $XReleasePercent = isset($Format["XReleasePercent"]) ? $Format["XReleasePercent"] : 1; + $DrawArrows = isset($Format["DrawArrows"]) ? $Format["DrawArrows"] : FALSE; + $ArrowSize = isset($Format["ArrowSize"]) ? $Format["ArrowSize"] : 8; + $CycleBackground = isset($Format["CycleBackground"]) ? $Format["CycleBackground"] : FALSE; + $BackgroundR1 = isset($Format["BackgroundR1"]) ? $Format["BackgroundR1"] : 255; + $BackgroundG1 = isset($Format["BackgroundG1"]) ? $Format["BackgroundG1"] : 255; + $BackgroundB1 = isset($Format["BackgroundB1"]) ? $Format["BackgroundB1"] : 255; + $BackgroundAlpha1 = isset($Format["BackgroundAlpha1"]) ? $Format["BackgroundAlpha1"] : 20; + $BackgroundR2 = isset($Format["BackgroundR2"]) ? $Format["BackgroundR2"] : 230; + $BackgroundG2 = isset($Format["BackgroundG2"]) ? $Format["BackgroundG2"] : 230; + $BackgroundB2 = isset($Format["BackgroundB2"]) ? $Format["BackgroundB2"] : 230; + $BackgroundAlpha2 = isset($Format["BackgroundAlpha2"]) ? $Format["BackgroundAlpha2"] : 20; + $LabelingMethod = isset($Format["LabelingMethod"]) ? $Format["LabelingMethod"] : LABELING_ALL; + $LabelSkip = isset($Format["LabelSkip"]) ? $Format["LabelSkip"] : 0; + $LabelRotation = isset($Format["LabelRotation"]) ? $Format["LabelRotation"] : 0; + $SkippedAxisTicks = isset($Format["SkippedAxisTicks"]) ? $Format["SkippedAxisTicks"] : $GridTicks+2; + $SkippedAxisR = isset($Format["SkippedAxisR"]) ? $Format["SkippedAxisR"] : $GridR; + $SkippedAxisG = isset($Format["SkippedAxisG"]) ? $Format["SkippedAxisG"] : $GridG; + $SkippedAxisB = isset($Format["SkippedAxisB"]) ? $Format["SkippedAxisB"] : $GridB; + $SkippedAxisAlpha = isset($Format["SkippedAxisAlpha"]) ? $Format["SkippedAxisAlpha"] : $GridAlpha-30; + $SkippedTickR = isset($Format["SkippedTickR"]) ? $Format["SkippedTickR"] : $TickRo; + $SkippedTickG = isset($Format["SkippedTickG"]) ? $Format["SkippedTickG"] : $TickGo; + $SkippedTickB = isset($Format["SkippedTicksB"]) ? $Format["SkippedTickB"] : $TickBo; + $SkippedTickAlpha = isset($Format["SkippedTickAlpha"]) ? $Format["SkippedTickAlpha"] : $TickAlpha-80; + $SkippedInnerTickWidth = isset($Format["SkippedInnerTickWidth"]) ? $Format["SkippedInnerTickWidth"] : 0; + $SkippedOuterTickWidth = isset($Format["SkippedOuterTickWidth"]) ? $Format["SkippedOuterTickWidth"] : 2; + + /* Floating scale require X & Y margins to be set manually */ + if ( $Floating && ( $XMargin == AUTO || $YMargin == 0 ) ) { $Floating = FALSE; } + + /* Skip a NOTICE event in case of an empty array */ + if ( $DrawYLines == NONE || $DrawYLines == FALSE ) { $DrawYLines = array("zarma"=>"31"); } + + /* Define the color for the skipped elements */ + $SkippedAxisColor = array("R"=>$SkippedAxisR,"G"=>$SkippedAxisG,"B"=>$SkippedAxisB,"Alpha"=>$SkippedAxisAlpha,"Ticks"=>$SkippedAxisTicks); + $SkippedTickColor = array("R"=>$SkippedTickR,"G"=>$SkippedTickG,"B"=>$SkippedTickB,"Alpha"=>$SkippedTickAlpha); + + $Data = $this->DataSet->getData(); + if ( isset($Data["Abscissa"]) ) { $Abscissa = $Data["Abscissa"]; } else { $Abscissa = NULL; } + + /* Unset the abscissa axis, needed if we display multiple charts on the same picture */ + if ( $Abscissa != NULL ) + { + foreach($Data["Axis"] as $AxisID => $Parameters) + { if ($Parameters["Identity"] == AXIS_X) { unset($Data["Axis"][$AxisID]); } } + } + + /* Build the scale settings */ + $GotXAxis = FALSE; + foreach($Data["Axis"] as $AxisID => $AxisParameter) + { + if ( $AxisParameter["Identity"] == AXIS_X ) { $GotXAxis = TRUE; } + + if ( $Pos == SCALE_POS_LEFTRIGHT && $AxisParameter["Identity"] == AXIS_Y) + { $Height = $this->GraphAreaY2-$this->GraphAreaY1 - $YMargin*2; } + elseif ( $Pos == SCALE_POS_LEFTRIGHT && $AxisParameter["Identity"] == AXIS_X) + { $Height = $this->GraphAreaX2-$this->GraphAreaX1; } + elseif ( $Pos == SCALE_POS_TOPBOTTOM && $AxisParameter["Identity"] == AXIS_Y) + { $Height = $this->GraphAreaX2-$this->GraphAreaX1 - $YMargin*2;; } + else + { $Height = $this->GraphAreaY2-$this->GraphAreaY1; } + + $AxisMin = ABSOLUTE_MAX; $AxisMax = OUT_OF_SIGHT; + if ( $Mode == SCALE_MODE_FLOATING || $Mode == SCALE_MODE_START0 ) + { + foreach($Data["Series"] as $SerieID => $SerieParameter) + { + if ( $SerieParameter["Axis"] == $AxisID && $Data["Series"][$SerieID]["isDrawable"] && $Data["Abscissa"] != $SerieID) + { + $AxisMax = max($AxisMax,$Data["Series"][$SerieID]["Max"]); + $AxisMin = min($AxisMin,$Data["Series"][$SerieID]["Min"]); + } + } + $AutoMargin = (($AxisMax-$AxisMin)/100)*$XReleasePercent; + + $Data["Axis"][$AxisID]["Min"] = $AxisMin-$AutoMargin; $Data["Axis"][$AxisID]["Max"] = $AxisMax+$AutoMargin; + if ( $Mode == SCALE_MODE_START0 ) { $Data["Axis"][$AxisID]["Min"] = 0; } + } + elseif ( $Mode == SCALE_MODE_MANUAL ) + { + if ( isset($ManualScale[$AxisID]["Min"]) && isset($ManualScale[$AxisID]["Max"]) ) + { + $Data["Axis"][$AxisID]["Min"] = $ManualScale[$AxisID]["Min"]; + $Data["Axis"][$AxisID]["Max"] = $ManualScale[$AxisID]["Max"]; + } + else + { echo "Manual scale boundaries not set."; exit(); } + } + elseif ( $Mode == SCALE_MODE_ADDALL || $Mode == SCALE_MODE_ADDALL_START0 ) + { + $Series = ""; + foreach($Data["Series"] as $SerieID => $SerieParameter) + { if ( $SerieParameter["Axis"] == $AxisID && $SerieParameter["isDrawable"] && $Data["Abscissa"] != $SerieID ) { $Series[$SerieID] = count($Data["Series"][$SerieID]["Data"]); } } + + for ($ID=0;$ID<=max($Series)-1;$ID++) + { + $PointMin = 0; $PointMax = 0; + foreach($Series as $SerieID => $ValuesCount ) + { + if (isset($Data["Series"][$SerieID]["Data"][$ID]) && $Data["Series"][$SerieID]["Data"][$ID] != NULL ) + { + $Value = $Data["Series"][$SerieID]["Data"][$ID]; + if ( $Value > 0 ) { $PointMax = $PointMax + $Value; } else { $PointMin = $PointMin + $Value; } + } + } + $AxisMax = max($AxisMax,$PointMax); + $AxisMin = min($AxisMin,$PointMin); + } + $AutoMargin = (($AxisMax-$AxisMin)/100)*$XReleasePercent; + $Data["Axis"][$AxisID]["Min"] = $AxisMin-$AutoMargin; $Data["Axis"][$AxisID]["Max"] = $AxisMax+$AutoMargin; + } + $MaxDivs = floor($Height/$MinDivHeight); + + if ( $Mode == SCALE_MODE_ADDALL_START0 ) { $Data["Axis"][$AxisID]["Min"] = 0; } + + $Scale = $this->computeScale($Data["Axis"][$AxisID]["Min"],$Data["Axis"][$AxisID]["Max"],$MaxDivs,$Factors,$AxisID); + + $Data["Axis"][$AxisID]["Margin"] = $AxisParameter["Identity"] == AXIS_X ? $XMargin : $YMargin; + $Data["Axis"][$AxisID]["ScaleMin"] = $Scale["XMin"]; + $Data["Axis"][$AxisID]["ScaleMax"] = $Scale["XMax"]; + $Data["Axis"][$AxisID]["Rows"] = $Scale["Rows"]; + $Data["Axis"][$AxisID]["RowHeight"] = $Scale["RowHeight"]; + + if ( isset($Scale["Format"]) ) { $Data["Axis"][$AxisID]["Format"] = $Scale["Format"]; } + + if ( !isset($Data["Axis"][$AxisID]["Display"]) ) { $Data["Axis"][$AxisID]["Display"] = NULL; } + if ( !isset($Data["Axis"][$AxisID]["Format"]) ) { $Data["Axis"][$AxisID]["Format"] = NULL; } + if ( !isset($Data["Axis"][$AxisID]["Unit"]) ) { $Data["Axis"][$AxisID]["Unit"] = NULL; } + } + + /* Still no X axis */ + if ( $GotXAxis == FALSE ) + { + if ( $Abscissa != NULL ) + { + $Points = count($Data["Series"][$Abscissa]["Data"]); + if ( $AutoAxisLabels ) + $AxisName = isset($Data["Series"][$Abscissa]["Description"]) ? $Data["Series"][$Abscissa]["Description"] : NULL; + else + $AxisName = NULL; + } + else + { + $Points = 0; + $AxisName = isset($Data["XAxisName"]) ? $Data["XAxisName"] : NULL; + foreach($Data["Series"] as $SerieID => $SerieParameter) + { if ( $SerieParameter["isDrawable"] ) { $Points = max($Points,count($SerieParameter["Data"])); } } + } + + $AxisID = count($Data["Axis"]); + $Data["Axis"][$AxisID]["Identity"] = AXIS_X; + if ( $Pos == SCALE_POS_LEFTRIGHT ) { $Data["Axis"][$AxisID]["Position"] = AXIS_POSITION_BOTTOM; } else { $Data["Axis"][$AxisID]["Position"] = AXIS_POSITION_LEFT; } + if ( isset($Data["AbscissaName"]) ) { $Data["Axis"][$AxisID]["Name"] = $Data["AbscissaName"]; } + if ( $XMargin == AUTO ) + { + if ( $Pos == SCALE_POS_LEFTRIGHT ) + { $Height = $this->GraphAreaX2-$this->GraphAreaX1; } + else + { $Height = $this->GraphAreaY2-$this->GraphAreaY1; } + + if ( $Points == 1 ) + $Data["Axis"][$AxisID]["Margin"] = $Height / 2; + else + $Data["Axis"][$AxisID]["Margin"] = ($Height/$Points) / 2; + } + else + { $Data["Axis"][$AxisID]["Margin"] = $XMargin; } + $Data["Axis"][$AxisID]["Rows"] = $Points-1; + if ( !isset($Data["Axis"][$AxisID]["Display"]) ) { $Data["Axis"][$AxisID]["Display"] = NULL; } + if ( !isset($Data["Axis"][$AxisID]["Format"]) ) { $Data["Axis"][$AxisID]["Format"] = NULL; } + if ( !isset($Data["Axis"][$AxisID]["Unit"]) ) { $Data["Axis"][$AxisID]["Unit"] = NULL; } + } + + /* Do we need to reverse the abscissa position? */ + if ( $Pos != SCALE_POS_LEFTRIGHT ) + { + if ( $Data["AbsicssaPosition"] == AXIS_POSITION_BOTTOM ) + { $Data["AbsicssaPosition"] = AXIS_POSITION_LEFT; } + else + { $Data["AbsicssaPosition"] = AXIS_POSITION_RIGHT; } + } + $Data["Axis"][$AxisID]["Position"] = $Data["AbsicssaPosition"]; + + $this->DataSet->saveOrientation($Pos); + $this->DataSet->saveAxisConfig($Data["Axis"]); + $this->DataSet->saveYMargin($YMargin); + + $FontColorRo = $this->FontColorR; $FontColorGo = $this->FontColorG; $FontColorBo = $this->FontColorB; + + $AxisPos["L"] = $this->GraphAreaX1; $AxisPos["R"] = $this->GraphAreaX2; $AxisPos["T"] = $this->GraphAreaY1; $AxisPos["B"] = $this->GraphAreaY2; + foreach($Data["Axis"] as $AxisID => $Parameters) + { + if ( isset($Parameters["Color"]) ) + { + $AxisR = $Parameters["Color"]["R"]; $AxisG = $Parameters["Color"]["G"]; $AxisB = $Parameters["Color"]["B"]; + $TickR = $Parameters["Color"]["R"]; $TickG = $Parameters["Color"]["G"]; $TickB = $Parameters["Color"]["B"]; + $this->setFontProperties(array("R"=>$Parameters["Color"]["R"],"G"=>$Parameters["Color"]["G"],"B"=>$Parameters["Color"]["B"])); + } + else + { + $AxisR = $AxisRo; $AxisG = $AxisGo; $AxisB = $AxisBo; + $TickR = $TickRo; $TickG = $TickGo; $TickB = $TickBo; + $this->setFontProperties(array("R"=>$FontColorRo,"G"=>$FontColorGo,"B"=>$FontColorBo)); + } + + $LastValue = "w00t"; $ID = 1; + if ( $Parameters["Identity"] == AXIS_X ) + { + if ( $Pos == SCALE_POS_LEFTRIGHT ) + { + if ( $Parameters["Position"] == AXIS_POSITION_BOTTOM ) + { + if ( $LabelRotation == 0 ) { $LabelAlign = TEXT_ALIGN_TOPMIDDLE; $YLabelOffset = 2; } + if ( $LabelRotation > 0 && $LabelRotation < 190 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $YLabelOffset = 5; } + if ( $LabelRotation == 180 ) { $LabelAlign = TEXT_ALIGN_BOTTOMMIDDLE; $YLabelOffset = 5; } + if ( $LabelRotation > 180 && $LabelRotation < 360 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $YLabelOffset = 2; } + + if ( !$RemoveXAxis ) + { + if ( $Floating ) + { $FloatingOffset = $YMargin; $this->drawLine($this->GraphAreaX1+$Parameters["Margin"],$AxisPos["B"],$this->GraphAreaX2-$Parameters["Margin"],$AxisPos["B"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + else + { $FloatingOffset = 0; $this->drawLine($this->GraphAreaX1,$AxisPos["B"],$this->GraphAreaX2,$AxisPos["B"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + + if ( $DrawArrows ) { $this->drawArrow($this->GraphAreaX2-$Parameters["Margin"],$AxisPos["B"],$this->GraphAreaX2+($ArrowSize*2),$AxisPos["B"],array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } + } + + $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Parameters["Margin"]*2; + + if ($Parameters["Rows"] == 0 ) { $Step = $Width; } else { $Step = $Width / ($Parameters["Rows"]); } + + $MaxBottom = $AxisPos["B"]; + for($i=0;$i<=$Parameters["Rows"];$i++) + { + $XPos = $this->GraphAreaX1 + $Parameters["Margin"] + $Step*$i; + $YPos = $AxisPos["B"]; + + if ( $Abscissa != NULL ) + { if ( isset($Data["Series"][$Abscissa]["Data"][$i]) ) { $Value = $this->scaleFormat($Data["Series"][$Abscissa]["Data"][$i],$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); } else { $Value = ""; } } + else + { + if ( isset($Parameters["ScaleMin"]) && isset ($Parameters["RowHeight"]) ) + $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); + else + $Value = $i; + } + + $ID++; $Skipped = TRUE; + if ( $this->isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) && !$RemoveXAxis) + { + $Bounds = $this->drawText($XPos,$YPos+$OuterTickWidth+$YLabelOffset,$Value,array("Angle"=>$LabelRotation,"Align"=>$LabelAlign)); + $TxtBottom = $YPos+$OuterTickWidth+2+($Bounds[0]["Y"]-$Bounds[2]["Y"]); + $MaxBottom = max($MaxBottom,$TxtBottom); + $LastValue = $Value; + $Skipped = FALSE; + } + + if ( $RemoveXAxis ) { $Skipped = FALSE; } + + if ( $Skipped ) + { + if ( $DrawXLines ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,$SkippedAxisColor); } + if ( ($SkippedInnerTickWidth !=0 || $SkippedOuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos,$YPos-$SkippedInnerTickWidth,$XPos,$YPos+$SkippedOuterTickWidth,$SkippedTickColor); } + } + else + { + if ( $DrawXLines && ($XPos != $this->GraphAreaX1 && $XPos != $this->GraphAreaX2) ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } + if ( ($InnerTickWidth !=0 || $OuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos,$YPos-$InnerTickWidth,$XPos,$YPos+$OuterTickWidth,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); } + } + } + + if ( isset($Parameters["Name"]) && !$RemoveXAxis) + { + $YPos = $MaxBottom+2; + $XPos = $this->GraphAreaX1+($this->GraphAreaX2-$this->GraphAreaX1)/2; + $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_TOPMIDDLE)); + $MaxBottom = $Bounds[0]["Y"]; + + $this->DataSet->Data["GraphArea"]["Y2"] = $MaxBottom + $this->FontSize; + } + + $AxisPos["B"] = $MaxBottom + $ScaleSpacing; + } + elseif ( $Parameters["Position"] == AXIS_POSITION_TOP ) + { + if ( $LabelRotation == 0 ) { $LabelAlign = TEXT_ALIGN_BOTTOMMIDDLE; $YLabelOffset = 2; } + if ( $LabelRotation > 0 && $LabelRotation < 190 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $YLabelOffset = 2; } + if ( $LabelRotation == 180 ) { $LabelAlign = TEXT_ALIGN_TOPMIDDLE; $YLabelOffset = 5; } + if ( $LabelRotation > 180 && $LabelRotation < 360 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $YLabelOffset = 5; } + + if ( !$RemoveXAxis ) + { + if ( $Floating ) + { $FloatingOffset = $YMargin; $this->drawLine($this->GraphAreaX1+$Parameters["Margin"],$AxisPos["T"],$this->GraphAreaX2-$Parameters["Margin"],$AxisPos["T"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + else + { $FloatingOffset = 0; $this->drawLine($this->GraphAreaX1,$AxisPos["T"],$this->GraphAreaX2,$AxisPos["T"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + + if ( $DrawArrows ) { $this->drawArrow($this->GraphAreaX2-$Parameters["Margin"],$AxisPos["T"],$this->GraphAreaX2+($ArrowSize*2),$AxisPos["T"],array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } + } + + $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Parameters["Margin"]*2; + + if ($Parameters["Rows"] == 0 ) { $Step = $Width; } else { $Step = $Width / $Parameters["Rows"]; } + + $MinTop = $AxisPos["T"]; + for($i=0;$i<=$Parameters["Rows"];$i++) + { + $XPos = $this->GraphAreaX1 + $Parameters["Margin"] + $Step*$i; + $YPos = $AxisPos["T"]; + + if ( $Abscissa != NULL ) + { if ( isset($Data["Series"][$Abscissa]["Data"][$i]) ) { $Value = $this->scaleFormat($Data["Series"][$Abscissa]["Data"][$i],$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); } else { $Value = ""; } } + else + { + if ( isset($Parameters["ScaleMin"]) && isset ($Parameters["RowHeight"]) ) + $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); + else + $Value = $i; + } + + $ID++; $Skipped = TRUE; + if ( $this->isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) && !$RemoveXAxis) + { + $Bounds = $this->drawText($XPos,$YPos-$OuterTickWidth-$YLabelOffset,$Value,array("Angle"=>$LabelRotation,"Align"=>$LabelAlign)); + $TxtBox = $YPos-$OuterTickWidth-2-($Bounds[0]["Y"]-$Bounds[2]["Y"]); + $MinTop = min($MinTop,$TxtBox); + $LastValue = $Value; + $Skipped = FALSE; + } + + if ( $RemoveXAxis ) { $Skipped = FALSE; } + + if ( $Skipped ) + { + if ( $DrawXLines ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,$SkippedAxisColor); } + if ( ($SkippedInnerTickWidth !=0 || $SkippedOuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos,$YPos+$SkippedInnerTickWidth,$XPos,$YPos-$SkippedOuterTickWidth,$SkippedTickColor); } + } + else + { + if ( $DrawXLines ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } + if ( ($InnerTickWidth !=0 || $OuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos,$YPos+$InnerTickWidth,$XPos,$YPos-$OuterTickWidth,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); } + } + + } + + if ( isset($Parameters["Name"]) && !$RemoveXAxis ) + { + $YPos = $MinTop-2; + $XPos = $this->GraphAreaX1+($this->GraphAreaX2-$this->GraphAreaX1)/2; + $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); + $MinTop = $Bounds[2]["Y"]; + + $this->DataSet->Data["GraphArea"]["Y1"] = $MinTop; + } + + $AxisPos["T"] = $MinTop - $ScaleSpacing; + } + } + elseif ( $Pos == SCALE_POS_TOPBOTTOM ) + { + if ( $Parameters["Position"] == AXIS_POSITION_LEFT ) + { + if ( $LabelRotation == 0 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $XLabelOffset = -2; } + if ( $LabelRotation > 0 && $LabelRotation < 190 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $XLabelOffset = -6; } + if ( $LabelRotation == 180 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $XLabelOffset = -2; } + if ( $LabelRotation > 180 && $LabelRotation < 360 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $XLabelOffset = -5; } + + if ( !$RemoveXAxis ) + { + if ( $Floating ) + { $FloatingOffset = $YMargin; $this->drawLine($AxisPos["L"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["L"],$this->GraphAreaY2-$Parameters["Margin"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + else + { $FloatingOffset = 0; $this->drawLine($AxisPos["L"],$this->GraphAreaY1,$AxisPos["L"],$this->GraphAreaY2,array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + + if ( $DrawArrows ) { $this->drawArrow($AxisPos["L"],$this->GraphAreaY2-$Parameters["Margin"],$AxisPos["L"],$this->GraphAreaY2+($ArrowSize*2),array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } + } + + $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Parameters["Margin"]*2; + + if ($Parameters["Rows"] == 0 ) { $Step = $Height; } else { $Step = $Height / $Parameters["Rows"]; } + + $MinLeft = $AxisPos["L"]; + for($i=0;$i<=$Parameters["Rows"];$i++) + { + $YPos = $this->GraphAreaY1 + $Parameters["Margin"] + $Step*$i; + $XPos = $AxisPos["L"]; + + if ( $Abscissa != NULL ) + { if ( isset($Data["Series"][$Abscissa]["Data"][$i]) ) { $Value = $this->scaleFormat($Data["Series"][$Abscissa]["Data"][$i],$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); } else { $Value = ""; } } + else + { + if ( isset($Parameters["ScaleMin"]) && isset ($Parameters["RowHeight"]) ) + $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); + else + $Value = $i; + } + + $ID++; $Skipped = TRUE; + if ( $this->isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) && !$RemoveXAxis) + { + $Bounds = $this->drawText($XPos-$OuterTickWidth+$XLabelOffset,$YPos,$Value,array("Angle"=>$LabelRotation,"Align"=>$LabelAlign)); + $TxtBox = $XPos-$OuterTickWidth-2-($Bounds[1]["X"]-$Bounds[0]["X"]); + $MinLeft = min($MinLeft,$TxtBox); + $LastValue = $Value; + $Skipped = FALSE; + } + + if ( $RemoveXAxis ) { $Skipped = FALSE; } + + if ( $Skipped ) + { + if ( $DrawXLines ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,$SkippedAxisColor); } + if ( ($SkippedInnerTickWidth !=0 || $SkippedOuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos-$SkippedOuterTickWidth,$YPos,$XPos+$SkippedInnerTickWidth,$YPos,$SkippedTickColor); } + } + else + { + if ( $DrawXLines && ($YPos != $this->GraphAreaY1 && $YPos != $this->GraphAreaY2) ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } + if ( ($InnerTickWidth !=0 || $OuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos-$OuterTickWidth,$YPos,$XPos+$InnerTickWidth,$YPos,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); } + } + + } + if ( isset($Parameters["Name"]) && !$RemoveXAxis ) + { + $XPos = $MinLeft-2; + $YPos = $this->GraphAreaY1+($this->GraphAreaY2-$this->GraphAreaY1)/2; + $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE,"Angle"=>90)); + $MinLeft = $Bounds[0]["X"]; + + $this->DataSet->Data["GraphArea"]["X1"] = $MinLeft; + } + + $AxisPos["L"] = $MinLeft - $ScaleSpacing; + } + elseif ( $Parameters["Position"] == AXIS_POSITION_RIGHT ) + { + if ( $LabelRotation == 0 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $XLabelOffset = 2; } + if ( $LabelRotation > 0 && $LabelRotation < 190 ) { $LabelAlign = TEXT_ALIGN_MIDDLELEFT; $XLabelOffset = 6; } + if ( $LabelRotation == 180 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $XLabelOffset = 5; } + if ( $LabelRotation > 180 && $LabelRotation < 360 ) { $LabelAlign = TEXT_ALIGN_MIDDLERIGHT; $XLabelOffset = 7; } + + if ( !$RemoveXAxis ) + { + if ( $Floating ) + { $FloatingOffset = $YMargin; $this->drawLine($AxisPos["R"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["R"],$this->GraphAreaY2-$Parameters["Margin"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + else + { $FloatingOffset = 0; $this->drawLine($AxisPos["R"],$this->GraphAreaY1,$AxisPos["R"],$this->GraphAreaY2,array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + + if ( $DrawArrows ) { $this->drawArrow($AxisPos["R"],$this->GraphAreaY2-$Parameters["Margin"],$AxisPos["R"],$this->GraphAreaY2+($ArrowSize*2),array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } + } + + $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Parameters["Margin"]*2; + + if ($Parameters["Rows"] == 0 ) { $Step = $Height; } else { $Step = $Height / $Parameters["Rows"]; } + + $MaxRight = $AxisPos["R"]; + for($i=0;$i<=$Parameters["Rows"];$i++) + { + $YPos = $this->GraphAreaY1 + $Parameters["Margin"] + $Step*$i; + $XPos = $AxisPos["R"]; + + if ( $Abscissa != NULL ) + { if ( isset($Data["Series"][$Abscissa]["Data"][$i]) ) { $Value = $this->scaleFormat($Data["Series"][$Abscissa]["Data"][$i],$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); } else { $Value = ""; } } + else + { + if ( isset($Parameters["ScaleMin"]) && isset ($Parameters["RowHeight"]) ) + $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Data["XAxisDisplay"],$Data["XAxisFormat"],$Data["XAxisUnit"]); + else + $Value = $i; + } + + $ID++; $Skipped = TRUE; + if ( $this->isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) && !$RemoveXAxis) + { + $Bounds = $this->drawText($XPos+$OuterTickWidth+$XLabelOffset,$YPos,$Value,array("Angle"=>$LabelRotation,"Align"=>$LabelAlign)); + $TxtBox = $XPos+$OuterTickWidth+2+($Bounds[1]["X"]-$Bounds[0]["X"]); + $MaxRight = max($MaxRight,$TxtBox); + $LastValue = $Value; + $Skipped = FALSE; + } + + if ( $RemoveXAxis ) { $Skipped = FALSE; } + + if ( $Skipped ) + { + if ( $DrawXLines ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,$SkippedAxisColor); } + if ( ($SkippedInnerTickWidth != 0 || $SkippedOuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos+$SkippedOuterTickWidth,$YPos,$XPos-$SkippedInnerTickWidth,$YPos,$SkippedTickColor); } + } + else + { + if ( $DrawXLines ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } + if ( ($InnerTickWidth != 0 || $OuterTickWidth != 0) && !$RemoveXAxis ) { $this->drawLine($XPos+$OuterTickWidth,$YPos,$XPos-$InnerTickWidth,$YPos,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); } + } + + } + + if ( isset($Parameters["Name"]) && !$RemoveXAxis) + { + $XPos = $MaxRight+4; + $YPos = $this->GraphAreaY1+($this->GraphAreaY2-$this->GraphAreaY1)/2; + $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE,"Angle"=>270)); + $MaxRight = $Bounds[1]["X"]; + + $this->DataSet->Data["GraphArea"]["X2"] = $MaxRight + $this->FontSize; + } + + $AxisPos["R"] = $MaxRight + $ScaleSpacing; + } + } + } + + + + if ( $Parameters["Identity"] == AXIS_Y ) + { + if ( $Pos == SCALE_POS_LEFTRIGHT ) + { + if ( $Parameters["Position"] == AXIS_POSITION_LEFT ) + { + + if ( $Floating ) + { $FloatingOffset = $XMargin; $this->drawLine($AxisPos["L"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["L"],$this->GraphAreaY2-$Parameters["Margin"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + else + { $FloatingOffset = 0; $this->drawLine($AxisPos["L"],$this->GraphAreaY1,$AxisPos["L"],$this->GraphAreaY2,array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + + if ( $DrawArrows ) { $this->drawArrow($AxisPos["L"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["L"],$this->GraphAreaY1-($ArrowSize*2),array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } + + $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Parameters["Margin"]*2; + $Step = $Height / $Parameters["Rows"]; $SubTicksSize = $Step /2; $MinLeft = $AxisPos["L"]; + $LastY = NULL; + for($i=0;$i<=$Parameters["Rows"];$i++) + { + $YPos = $this->GraphAreaY2 - $Parameters["Margin"] - $Step*$i; + $XPos = $AxisPos["L"]; + $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Parameters["Display"],$Parameters["Format"],$Parameters["Unit"]); + + if ( $i%2 == 1 ) { $BGColor = array("R"=>$BackgroundR1,"G"=>$BackgroundG1,"B"=>$BackgroundB1,"Alpha"=>$BackgroundAlpha1); } else { $BGColor = array("R"=>$BackgroundR2,"G"=>$BackgroundG2,"B"=>$BackgroundB2,"Alpha"=>$BackgroundAlpha2); } + if ( $LastY != NULL && $CycleBackground && ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) )) { $this->drawFilledRectangle($this->GraphAreaX1+$FloatingOffset,$LastY,$this->GraphAreaX2-$FloatingOffset,$YPos,$BGColor); } + + if ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } + + if ( $DrawSubTicks && $i != $Parameters["Rows"] ) + $this->drawLine($XPos-$OuterSubTickWidth,$YPos-$SubTicksSize,$XPos+$InnerSubTickWidth,$YPos-$SubTicksSize,array("R"=>$SubTickR,"G"=>$SubTickG,"B"=>$SubTickB,"Alpha"=>$SubTickAlpha)); + + $this->drawLine($XPos-$OuterTickWidth,$YPos,$XPos+$InnerTickWidth,$YPos,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); + $Bounds = $this->drawText($XPos-$OuterTickWidth-2,$YPos,$Value,array("Align"=>TEXT_ALIGN_MIDDLERIGHT)); + $TxtLeft = $XPos-$OuterTickWidth-2-($Bounds[1]["X"]-$Bounds[0]["X"]); + $MinLeft = min($MinLeft,$TxtLeft); + + $LastY = $YPos; + } + + if ( isset($Parameters["Name"]) ) + { + $XPos = $MinLeft-2; + $YPos = $this->GraphAreaY1+($this->GraphAreaY2-$this->GraphAreaY1)/2; + $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE,"Angle"=>90)); + $MinLeft = $Bounds[2]["X"]; + + $this->DataSet->Data["GraphArea"]["X1"] = $MinLeft; + } + + $AxisPos["L"] = $MinLeft - $ScaleSpacing; + } + elseif ( $Parameters["Position"] == AXIS_POSITION_RIGHT ) + { + if ( $Floating ) + { $FloatingOffset = $XMargin; $this->drawLine($AxisPos["R"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["R"],$this->GraphAreaY2-$Parameters["Margin"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + else + { $FloatingOffset = 0; $this->drawLine($AxisPos["R"],$this->GraphAreaY1,$AxisPos["R"],$this->GraphAreaY2,array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + + if ( $DrawArrows ) { $this->drawArrow($AxisPos["R"],$this->GraphAreaY1+$Parameters["Margin"],$AxisPos["R"],$this->GraphAreaY1-($ArrowSize*2),array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } + + $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Parameters["Margin"]*2; + $Step = $Height / $Parameters["Rows"]; $SubTicksSize = $Step /2; $MaxLeft = $AxisPos["R"]; + $LastY = NULL; + for($i=0;$i<=$Parameters["Rows"];$i++) + { + $YPos = $this->GraphAreaY2 - $Parameters["Margin"] - $Step*$i; + $XPos = $AxisPos["R"]; + $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Parameters["Display"],$Parameters["Format"],$Parameters["Unit"]); + + if ( $i%2 == 1 ) { $BGColor = array("R"=>$BackgroundR1,"G"=>$BackgroundG1,"B"=>$BackgroundB1,"Alpha"=>$BackgroundAlpha1); } else { $BGColor = array("R"=>$BackgroundR2,"G"=>$BackgroundG2,"B"=>$BackgroundB2,"Alpha"=>$BackgroundAlpha2); } + if ( $LastY != NULL && $CycleBackground && ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) )) { $this->drawFilledRectangle($this->GraphAreaX1+$FloatingOffset,$LastY,$this->GraphAreaX2-$FloatingOffset,$YPos,$BGColor); } + + if ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) ) { $this->drawLine($this->GraphAreaX1+$FloatingOffset,$YPos,$this->GraphAreaX2-$FloatingOffset,$YPos,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } + + if ( $DrawSubTicks && $i != $Parameters["Rows"] ) + $this->drawLine($XPos-$OuterSubTickWidth,$YPos-$SubTicksSize,$XPos+$InnerSubTickWidth,$YPos-$SubTicksSize,array("R"=>$SubTickR,"G"=>$SubTickG,"B"=>$SubTickB,"Alpha"=>$SubTickAlpha)); + + $this->drawLine($XPos-$InnerTickWidth,$YPos,$XPos+$OuterTickWidth,$YPos,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); + $Bounds = $this->drawText($XPos+$OuterTickWidth+2,$YPos,$Value,array("Align"=>TEXT_ALIGN_MIDDLELEFT)); + $TxtLeft = $XPos+$OuterTickWidth+2+($Bounds[1]["X"]-$Bounds[0]["X"]); + $MaxLeft = max($MaxLeft,$TxtLeft); + + $LastY = $YPos; + } + + if ( isset($Parameters["Name"]) ) + { + $XPos = $MaxLeft+6; + $YPos = $this->GraphAreaY1+($this->GraphAreaY2-$this->GraphAreaY1)/2; + $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE,"Angle"=>270)); + $MaxLeft = $Bounds[2]["X"]; + + $this->DataSet->Data["GraphArea"]["X2"] = $MaxLeft + $this->FontSize; + } + $AxisPos["R"] = $MaxLeft + $ScaleSpacing; + } + } + elseif ( $Pos == SCALE_POS_TOPBOTTOM ) + { + if ( $Parameters["Position"] == AXIS_POSITION_TOP ) + { + if ( $Floating ) + { $FloatingOffset = $XMargin; $this->drawLine($this->GraphAreaX1+$Parameters["Margin"],$AxisPos["T"],$this->GraphAreaX2-$Parameters["Margin"],$AxisPos["T"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + else + { $FloatingOffset = 0; $this->drawLine($this->GraphAreaX1,$AxisPos["T"],$this->GraphAreaX2,$AxisPos["T"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + + if ( $DrawArrows ) { $this->drawArrow($this->GraphAreaX2-$Parameters["Margin"],$AxisPos["T"],$this->GraphAreaX2+($ArrowSize*2),$AxisPos["T"],array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } + + $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Parameters["Margin"]*2; + $Step = $Width / $Parameters["Rows"]; $SubTicksSize = $Step /2; $MinTop = $AxisPos["T"]; + $LastX = NULL; + for($i=0;$i<=$Parameters["Rows"];$i++) + { + $XPos = $this->GraphAreaX1 + $Parameters["Margin"] + $Step*$i; + $YPos = $AxisPos["T"]; + $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Parameters["Display"],$Parameters["Format"],$Parameters["Unit"]); + + if ( $i%2 == 1 ) { $BGColor = array("R"=>$BackgroundR1,"G"=>$BackgroundG1,"B"=>$BackgroundB1,"Alpha"=>$BackgroundAlpha1); } else { $BGColor = array("R"=>$BackgroundR2,"G"=>$BackgroundG2,"B"=>$BackgroundB2,"Alpha"=>$BackgroundAlpha2); } + if ( $LastX != NULL && $CycleBackground && ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) )) { $this->drawFilledRectangle($LastX,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,$BGColor); } + + if ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } + + if ( $DrawSubTicks && $i != $Parameters["Rows"] ) + $this->drawLine($XPos+$SubTicksSize,$YPos-$OuterSubTickWidth,$XPos+$SubTicksSize,$YPos+$InnerSubTickWidth,array("R"=>$SubTickR,"G"=>$SubTickG,"B"=>$SubTickB,"Alpha"=>$SubTickAlpha)); + + $this->drawLine($XPos,$YPos-$OuterTickWidth,$XPos,$YPos+$InnerTickWidth,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); + $Bounds = $this->drawText($XPos,$YPos-$OuterTickWidth-2,$Value,array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); + $TxtHeight = $YPos-$OuterTickWidth-2-($Bounds[1]["Y"]-$Bounds[2]["Y"]); + $MinTop = min($MinTop,$TxtHeight); + + $LastX = $XPos; + } + + if ( isset($Parameters["Name"]) ) + { + $YPos = $MinTop-2; + $XPos = $this->GraphAreaX1+($this->GraphAreaX2-$this->GraphAreaX1)/2; + $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); + $MinTop = $Bounds[2]["Y"]; + + $this->DataSet->Data["GraphArea"]["Y1"] = $MinTop; + } + + $AxisPos["T"] = $MinTop - $ScaleSpacing; + } + elseif ( $Parameters["Position"] == AXIS_POSITION_BOTTOM ) + { + if ( $Floating ) + { $FloatingOffset = $XMargin; $this->drawLine($this->GraphAreaX1+$Parameters["Margin"],$AxisPos["B"],$this->GraphAreaX2-$Parameters["Margin"],$AxisPos["B"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + else + { $FloatingOffset = 0; $this->drawLine($this->GraphAreaX1,$AxisPos["B"],$this->GraphAreaX2,$AxisPos["B"],array("R"=>$AxisR,"G"=>$AxisG,"B"=>$AxisB,"Alpha"=>$AxisAlpha)); } + + if ( $DrawArrows ) { $this->drawArrow($this->GraphAreaX2-$Parameters["Margin"],$AxisPos["B"],$this->GraphAreaX2+($ArrowSize*2),$AxisPos["B"],array("FillR"=>$AxisR,"FillG"=>$AxisG,"FillB"=>$AxisB,"Size"=>$ArrowSize)); } + + $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Parameters["Margin"]*2; + $Step = $Width / $Parameters["Rows"]; $SubTicksSize = $Step /2; $MaxBottom = $AxisPos["B"]; + $LastX = NULL; + for($i=0;$i<=$Parameters["Rows"];$i++) + { + $XPos = $this->GraphAreaX1 + $Parameters["Margin"] + $Step*$i; + $YPos = $AxisPos["B"]; + $Value = $this->scaleFormat($Parameters["ScaleMin"] + $Parameters["RowHeight"]*$i,$Parameters["Display"],$Parameters["Format"],$Parameters["Unit"]); + + if ( $i%2 == 1 ) { $BGColor = array("R"=>$BackgroundR1,"G"=>$BackgroundG1,"B"=>$BackgroundB1,"Alpha"=>$BackgroundAlpha1); } else { $BGColor = array("R"=>$BackgroundR2,"G"=>$BackgroundG2,"B"=>$BackgroundB2,"Alpha"=>$BackgroundAlpha2); } + if ( $LastX != NULL && $CycleBackground && ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) )) { $this->drawFilledRectangle($LastX,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,$BGColor); } + + if ( $DrawYLines == ALL || in_array($AxisID,$DrawYLines) ) { $this->drawLine($XPos,$this->GraphAreaY1+$FloatingOffset,$XPos,$this->GraphAreaY2-$FloatingOffset,array("R"=>$GridR,"G"=>$GridG,"B"=>$GridB,"Alpha"=>$GridAlpha,"Ticks"=>$GridTicks)); } + + if ( $DrawSubTicks && $i != $Parameters["Rows"] ) + $this->drawLine($XPos+$SubTicksSize,$YPos-$OuterSubTickWidth,$XPos+$SubTicksSize,$YPos+$InnerSubTickWidth,array("R"=>$SubTickR,"G"=>$SubTickG,"B"=>$SubTickB,"Alpha"=>$SubTickAlpha)); + + $this->drawLine($XPos,$YPos-$OuterTickWidth,$XPos,$YPos+$InnerTickWidth,array("R"=>$TickR,"G"=>$TickG,"B"=>$TickB,"Alpha"=>$TickAlpha)); + $Bounds = $this->drawText($XPos,$YPos+$OuterTickWidth+2,$Value,array("Align"=>TEXT_ALIGN_TOPMIDDLE)); + $TxtHeight = $YPos+$OuterTickWidth+2+($Bounds[1]["Y"]-$Bounds[2]["Y"]); + $MaxBottom = max($MaxBottom,$TxtHeight); + + $LastX = $XPos; + } + + if ( isset($Parameters["Name"]) ) + { + $YPos = $MaxBottom+2; + $XPos = $this->GraphAreaX1+($this->GraphAreaX2-$this->GraphAreaX1)/2; + $Bounds = $this->drawText($XPos,$YPos,$Parameters["Name"],array("Align"=>TEXT_ALIGN_TOPMIDDLE)); + $MaxBottom = $Bounds[0]["Y"]; + + $this->DataSet->Data["GraphArea"]["Y2"] = $MaxBottom + $this->FontSize; + } + + $AxisPos["B"] = $MaxBottom + $ScaleSpacing; + } + } + } + } + } + + function isValidLabel($Value,$LastValue,$LabelingMethod,$ID,$LabelSkip) + { + if ( $LabelingMethod == LABELING_DIFFERENT && $Value != $LastValue ) { return(TRUE); } + if ( $LabelingMethod == LABELING_DIFFERENT && $Value == $LastValue ) { return(FALSE); } + if ( $LabelingMethod == LABELING_ALL && $LabelSkip == 0 ) { return(TRUE); } + if ( $LabelingMethod == LABELING_ALL && ($ID+$LabelSkip) % ($LabelSkip+1) != 1 ) { return(FALSE); } + + return(TRUE); + } + + /* Compute the scale, check for the best visual factors */ + function computeScale($XMin,$XMax,$MaxDivs,$Factors,$AxisID=0) + { + /* Compute each factors */ + $Results = ""; + foreach ($Factors as $Key => $Factor) + $Results[$Factor] = $this->processScale($XMin,$XMax,$MaxDivs,array($Factor),$AxisID); + + /* Remove scales that are creating to much decimals */ + $GoodScaleFactors = ""; + foreach ($Results as $Key => $Result) + { + $Decimals = preg_split("/\./",$Result["RowHeight"]); + if ( (!isset($Decimals[1])) || (strlen($Decimals[1]) < 6) ) { $GoodScaleFactors[] = $Key; } + } + + /* Found no correct scale, shame,... returns the 1st one as default */ + if ( $GoodScaleFactors == "" ) { return($Results[$Factors[0]]); } + + /* Find the factor that cause the maximum number of Rows */ + $MaxRows = 0; $BestFactor = 0; + foreach($GoodScaleFactors as $Key => $Factor) + { if ( $Results[$Factor]["Rows"] > $MaxRows ) { $MaxRows = $Results[$Factor]["Rows"]; $BestFactor = $Factor; } } + + /* Return the best visual scale */ + return($Results[$BestFactor]); + } + + /* Compute the best matching scale based on size & factors */ + function processScale($XMin,$XMax,$MaxDivs,$Factors,$AxisID) + { + $ScaleHeight = abs(ceil($XMax)-floor($XMin)); + + if ( isset($this->DataSet->Data["Axis"][$AxisID]["Format"]) ) + $Format = $this->DataSet->Data["Axis"][$AxisID]["Format"]; + else + $Format = NULL; + + if ( isset($this->DataSet->Data["Axis"][$AxisID]["Display"]) ) + $Mode = $this->DataSet->Data["Axis"][$AxisID]["Display"]; + else + $Mode = AXIS_FORMAT_DEFAULT; + + $Scale = ""; + if ( $XMin != $XMax ) + { + $Found = FALSE; $Rescaled = FALSE; $Scaled10Factor = .0001; $Result = 0; + while(!$Found) + { + foreach($Factors as $Key => $Factor) + { + if ( !$Found ) + { + if ( !($this->modulo($XMin,$Factor*$Scaled10Factor) == 0) || ($XMin != floor($XMin))) { $XMinRescaled = floor($XMin/($Factor*$Scaled10Factor))*$Factor*$Scaled10Factor; } else { $XMinRescaled = $XMin; } + if ( !($this->modulo($XMax,$Factor*$Scaled10Factor) == 0) || ($XMax != floor($XMax))) { $XMaxRescaled = floor($XMax/($Factor*$Scaled10Factor))*$Factor*$Scaled10Factor+($Factor*$Scaled10Factor); } else { $XMaxRescaled = $XMax; } + $ScaleHeightRescaled = abs($XMaxRescaled-$XMinRescaled); + + if ( !$Found && floor($ScaleHeightRescaled/($Factor*$Scaled10Factor)) <= $MaxDivs ) { $Found = TRUE; $Rescaled = TRUE; $Result = $Factor * $Scaled10Factor; } + } + } + $Scaled10Factor = $Scaled10Factor * 10; + } + + /* ReCall Min / Max / Height */ + if ( $Rescaled ) { $XMin = $XMinRescaled; $XMax = $XMaxRescaled; $ScaleHeight = $ScaleHeightRescaled; } + + /* Compute rows size */ + $Rows = floor($ScaleHeight / $Result); if ( $Rows == 0 ) { $Rows = 1; } + $RowHeight = $ScaleHeight / $Rows; + + /* Return the results */ + $Scale["Rows"] = $Rows; $Scale["RowHeight"] = $RowHeight; $Scale["XMin"] = $XMin; $Scale["XMax"] = $XMax; + + /* Compute the needed decimals for the metric view to avoid repetition of the same X Axis labels */ + if ( $Mode == AXIS_FORMAT_METRIC && $Format == NULL ) + { + $Done = FALSE; $GoodDecimals = 0; + for($Decimals=0;$Decimals<=10;$Decimals++) + { + if ( !$Done ) + { + $LastLabel = "zob"; $ScaleOK = TRUE; + for($i=0;$i<=$Rows;$i++) + { + $Value = $XMin + $i*$RowHeight; + $Label = $this->scaleFormat($Value,AXIS_FORMAT_METRIC,$Decimals); + + if ( $LastLabel == $Label ) { $ScaleOK = FALSE; } + $LastLabel = $Label; + } + if ( $ScaleOK ) { $Done = TRUE; $GoodDecimals = $Decimals; } + } + } + + $Scale["Format"] = $GoodDecimals; + } + } + else + { + /* If all values are the same we keep a +1/-1 scale */ + $Rows = 2; $XMin = $XMax-1; $XMax = $XMax+1; $RowHeight = 1; + + /* Return the results */ + $Scale["Rows"] = $Rows; $Scale["RowHeight"] = $RowHeight; $Scale["XMin"] = $XMin; $Scale["XMax"] = $XMax; + } + + return($Scale); + } + + function modulo($Value1,$Value2) + { + if (floor($Value2) == 0) { return(0); } + if (floor($Value2) != 0) { return($Value1 % $Value2); } + + $MinValue = min($Value1,$Value2); $Factor = 10; + while ( floor($MinValue*$Factor) == 0 ) + { $Factor = $Factor * 10; } + + return(($Value1*$Factor) % ($Value2*$Factor)); + } + + /* Draw an X threshold */ + function drawXThreshold($Value,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 255; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 50; + $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : 6; + $Wide = isset($Format["Wide"]) ? $Format["Wide"] : FALSE; + $WideFactor = isset($Format["WideFactor"]) ? $Format["WideFactor"] : 5; + $WriteCaption = isset($Format["WriteCaption"]) ? $Format["WriteCaption"] : FALSE; + $Caption = isset($Format["Caption"]) ? $Format["Caption"] : NULL; + $CaptionAlign = isset($Format["CaptionAlign"]) ? $Format["CaptionAlign"] : CAPTION_LEFT_TOP; + $CaptionOffset = isset($Format["CaptionOffset"]) ? $Format["CaptionOffset"] : 5; + $CaptionR = isset($Format["CaptionR"]) ? $Format["CaptionR"] : 255; + $CaptionG = isset($Format["CaptionG"]) ? $Format["CaptionG"] : 255; + $CaptionB = isset($Format["CaptionB"]) ? $Format["CaptionB"] : 255; + $CaptionAlpha = isset($Format["CaptionAlpha"]) ? $Format["CaptionAlpha"] : 100; + $DrawBox = isset($Format["DrawBox"]) ? $Format["DrawBox"] : TRUE; + $DrawBoxBorder = isset($Format["DrawBoxBorder"]) ? $Format["DrawBoxBorder"] : FALSE; + $BorderOffset = isset($Format["BorderOffset"]) ? $Format["BorderOffset"] : 3; + $BoxRounded = isset($Format["BoxRounded"]) ? $Format["BoxRounded"] : TRUE; + $RoundedRadius = isset($Format["RoundedRadius"]) ? $Format["RoundedRadius"] : 3; + $BoxR = isset($Format["BoxR"]) ? $Format["BoxR"] : 0; + $BoxG = isset($Format["BoxG"]) ? $Format["BoxG"] : 0; + $BoxB = isset($Format["BoxB"]) ? $Format["BoxB"] : 0; + $BoxAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 30; + $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : ""; + $BoxBorderR = isset($Format["BoxBorderR"]) ? $Format["BoxBorderR"] : 255; + $BoxBorderG = isset($Format["BoxBorderG"]) ? $Format["BoxBorderG"] : 255; + $BoxBorderB = isset($Format["BoxBorderB"]) ? $Format["BoxBorderB"] : 255; + $BoxBorderAlpha = isset($Format["BoxBorderAlpha"]) ? $Format["BoxBorderAlpha"] : 100; + $ValueIsLabel = isset($Format["ValueIsLabel"]) ? $Format["ValueIsLabel"] : FALSE; + + $Data = $this->DataSet->getData(); + $AbscissaMargin = $this->getAbscissaMargin($Data); + $XScale = $this->scaleGetXSettings(); + + if ( is_array($Value) ) { foreach ($Value as $Key => $ID) { $this->drawXThreshold($ID,$Format); } return(0); } + + if ( $ValueIsLabel ) + { + $Format["ValueIsLabel"] = FALSE; + foreach($Data["Series"][$Data["Abscissa"]]["Data"] as $Key => $SerieValue) + { if ( $SerieValue == $Value ) { $this->drawXThreshold($Key,$Format); } } + + return(0); + } + + $CaptionSettings = array("DrawBox"=>$DrawBox,"DrawBoxBorder"=>$DrawBoxBorder,"BorderOffset"=>$BorderOffset,"BoxRounded"=>$BoxRounded,"RoundedRadius"=>$RoundedRadius, + "BoxR"=>$BoxR,"BoxG"=>$BoxG,"BoxB"=>$BoxB,"BoxAlpha"=>$BoxAlpha,"BoxSurrounding"=>$BoxSurrounding, + "BoxBorderR"=>$BoxBorderR,"BoxBorderG"=>$BoxBorderG,"BoxBorderB"=>$BoxBorderB,"BoxBorderAlpha"=>$BoxBorderAlpha, + "R"=>$CaptionR,"G"=>$CaptionG,"B"=>$CaptionB,"Alpha"=>$CaptionAlpha); + + if ( $Caption == NULL ) + { + if ( isset($Data["Abscissa"]) ) + { + if ( isset($Data["Series"][$Data["Abscissa"]]["Data"][$Value]) ) + $Caption = $Data["Series"][$Data["Abscissa"]]["Data"][$Value]; + else + $Caption = $Value; + } + else + $Caption = $Value; + } + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + $XStep = (($this->GraphAreaX2 - $this->GraphAreaX1) - $XScale[0] *2 ) / $XScale[1]; + $XPos = $this->GraphAreaX1 + $XScale[0] + $XStep * $Value; + $YPos1 = $this->GraphAreaY1 + $Data["YMargin"]; + $YPos2 = $this->GraphAreaY2 - $Data["YMargin"]; + + if ( $XPos >= $this->GraphAreaX1 + $AbscissaMargin && $XPos <= $this->GraphAreaX2 - $AbscissaMargin ) + { + $this->drawLine($XPos,$YPos1,$XPos,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); + + if ( $Wide ) + { + $this->drawLine($XPos-1,$YPos1,$XPos-1,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); + $this->drawLine($XPos+1,$YPos1,$XPos+1,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); + } + + if ( $WriteCaption ) + { + if ( $CaptionAlign == CAPTION_LEFT_TOP ) + { $Y = $YPos1 + $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_TOPMIDDLE; } + else + { $Y = $YPos2 - $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_BOTTOMMIDDLE; } + + $this->drawText($XPos,$Y,$Caption,$CaptionSettings); + } + + return(array("X"=>$XPos)); + } + } + elseif( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) + { + $XStep = (($this->GraphAreaY2 - $this->GraphAreaY1) - $XScale[0] *2 ) / $XScale[1]; + $XPos = $this->GraphAreaY1 + $XScale[0] + $XStep * $Value; + $YPos1 = $this->GraphAreaX1 + $Data["YMargin"]; + $YPos2 = $this->GraphAreaX2 - $Data["YMargin"]; + + if ( $XPos >= $this->GraphAreaY1 + $AbscissaMargin && $XPos <= $this->GraphAreaY2 - $AbscissaMargin ) + { + $this->drawLine($YPos1,$XPos,$YPos2,$XPos,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); + + if ( $Wide ) + { + $this->drawLine($YPos1,$XPos-1,$YPos2,$XPos-1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); + $this->drawLine($YPos1,$XPos+1,$YPos2,$XPos+1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); + } + + if ( $WriteCaption ) + { + if ( $CaptionAlign == CAPTION_LEFT_TOP ) + { $Y = $YPos1 + $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_MIDDLELEFT; } + else + { $Y = $YPos2 - $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_MIDDLERIGHT; } + + $this->drawText($Y,$XPos,$Caption,$CaptionSettings); + } + + return(array("X"=>$XPos)); + } + } + } + + /* Draw an X threshold area */ + function drawXThresholdArea($Value1,$Value2,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 255; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 20; + $Border = isset($Format["Border"]) ? $Format["Border"] : TRUE; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; + $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha + 20; + $BorderTicks = isset($Format["BorderTicks"]) ? $Format["BorderTicks"] : 2; + $AreaName = isset($Format["AreaName"]) ? $Format["AreaName"] : NULL; + $NameAngle = isset($Format["NameAngle"]) ? $Format["NameAngle"] : ZONE_NAME_ANGLE_AUTO; + $NameR = isset($Format["NameR"]) ? $Format["NameR"] : 255; + $NameG = isset($Format["NameG"]) ? $Format["NameG"] : 255; + $NameB = isset($Format["NameB"]) ? $Format["NameB"] : 255; + $NameAlpha = isset($Format["NameAlpha"]) ? $Format["NameAlpha"] : 100; + $DisableShadowOnArea = isset($Format["DisableShadowOnArea"]) ? $Format["DisableShadowOnArea"] : TRUE; + + $RestoreShadow = $this->Shadow; + if ( $DisableShadowOnArea && $this->Shadow ) { $this->Shadow = FALSE; } + + if ($BorderAlpha >100) { $BorderAlpha = 100;} + + $Data = $this->DataSet->getData(); + $XScale = $this->scaleGetXSettings(); + $AbscissaMargin = $this->getAbscissaMargin($Data); + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + $XStep = (($this->GraphAreaX2 - $this->GraphAreaX1) - $XScale[0] *2 ) / $XScale[1]; + $XPos1 = $this->GraphAreaX1 + $XScale[0] + $XStep * $Value1; + $XPos2 = $this->GraphAreaX1 + $XScale[0] + $XStep * $Value2; + $YPos1 = $this->GraphAreaY1 + $Data["YMargin"]; + $YPos2 = $this->GraphAreaY2 - $Data["YMargin"]; + + if ( $XPos1 < $this->GraphAreaX1 + $XScale[0] ) { $XPos1 = $this->GraphAreaX1 + $XScale[0]; } + if ( $XPos1 > $this->GraphAreaX2 - $XScale[0] ) { $XPos1 = $this->GraphAreaX2 - $XScale[0]; } + if ( $XPos2 < $this->GraphAreaX1 + $XScale[0] ) { $XPos2 = $this->GraphAreaX1 + $XScale[0]; } + if ( $XPos2 > $this->GraphAreaX2 - $XScale[0] ) { $XPos2 = $this->GraphAreaX2 - $XScale[0]; } + + $this->drawFilledRectangle($XPos1,$YPos1,$XPos2,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + + if ( $Border ) + { + $this->drawLine($XPos1,$YPos1,$XPos1,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); + $this->drawLine($XPos2,$YPos1,$XPos2,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); + } + + if ( $AreaName != NULL ) + { + $XPos = ($XPos2-$XPos1)/2 + $XPos1; + $YPos = ($YPos2-$YPos1)/2 + $YPos1; + + if ( $NameAngle == ZONE_NAME_ANGLE_AUTO ) + { + $TxtPos = $this->getTextBox($XPos,$YPos,$this->FontName,$this->FontSize,0,$AreaName); + $TxtWidth = $TxtPos[1]["X"] - $TxtPos[0]["X"]; + if ( abs($XPos2 - $XPos1) > $TxtWidth ) { $NameAngle = 0; } else { $NameAngle = 90; } + } + $this->Shadow = $RestoreShadow; + $this->drawText($XPos,$YPos,$AreaName,array("R"=>$NameR,"G"=>$NameG,"B"=>$NameB,"Alpha"=>$NameAlpha,"Angle"=>$NameAngle,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); + if ( $DisableShadowOnArea ) { $this->Shadow = FALSE; } + } + + $this->Shadow = $RestoreShadow; + return(array("X1"=>$XPos1,"X2"=>$XPos2)); + } + elseif ( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) + { + $XStep = (($this->GraphAreaY2 - $this->GraphAreaY1) - $XScale[0] *2 ) / $XScale[1]; + $XPos1 = $this->GraphAreaY1 + $XScale[0] + $XStep * $Value1; + $XPos2 = $this->GraphAreaY1 + $XScale[0] + $XStep * $Value2; + $YPos1 = $this->GraphAreaX1 + $Data["YMargin"]; + $YPos2 = $this->GraphAreaX2 - $Data["YMargin"]; + + if ( $XPos1 < $this->GraphAreaY1 + $XScale[0] ) { $XPos1 = $this->GraphAreaY1 + $XScale[0]; } + if ( $XPos1 > $this->GraphAreaY2 - $XScale[0] ) { $XPos1 = $this->GraphAreaY2 - $XScale[0]; } + if ( $XPos2 < $this->GraphAreaY1 + $XScale[0] ) { $XPos2 = $this->GraphAreaY1 + $XScale[0]; } + if ( $XPos2 > $this->GraphAreaY2 - $XScale[0] ) { $XPos2 = $this->GraphAreaY2 - $XScale[0]; } + + $this->drawFilledRectangle($YPos1,$XPos1,$YPos2,$XPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + + if ( $Border ) + { + $this->drawLine($YPos1,$XPos1,$YPos2,$XPos1,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); + $this->drawLine($YPos1,$XPos2,$YPos2,$XPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); + } + + if ( $AreaName != NULL ) + { + $XPos = ($XPos2-$XPos1)/2 + $XPos1; + $YPos = ($YPos2-$YPos1)/2 + $YPos1; + + $this->Shadow = $RestoreShadow; + $this->drawText($YPos,$XPos,$AreaName,array("R"=>$NameR,"G"=>$NameG,"B"=>$NameB,"Alpha"=>$NameAlpha,"Angle"=>0,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); + if ( $DisableShadowOnArea ) { $this->Shadow = FALSE; } + } + + $this->Shadow = $RestoreShadow; + return(array("X1"=>$XPos1,"X2"=>$XPos2)); + } + } + + /* Draw an Y threshold with the computed scale */ + function drawThreshold($Value,$Format="") + { + $AxisID = isset($Format["AxisID"]) ? $Format["AxisID"] : 0; + $R = isset($Format["R"]) ? $Format["R"] : 255; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 50; + $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL; + $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : 6; + $Wide = isset($Format["Wide"]) ? $Format["Wide"] : FALSE; + $WideFactor = isset($Format["WideFactor"]) ? $Format["WideFactor"] : 5; + $WriteCaption = isset($Format["WriteCaption"]) ? $Format["WriteCaption"] : FALSE; + $Caption = isset($Format["Caption"]) ? $Format["Caption"] : NULL; + $CaptionAlign = isset($Format["CaptionAlign"]) ? $Format["CaptionAlign"] : CAPTION_LEFT_TOP; + $CaptionOffset = isset($Format["CaptionOffset"]) ? $Format["CaptionOffset"] : 10; + $CaptionR = isset($Format["CaptionR"]) ? $Format["CaptionR"] : 255; + $CaptionG = isset($Format["CaptionG"]) ? $Format["CaptionG"] : 255; + $CaptionB = isset($Format["CaptionB"]) ? $Format["CaptionB"] : 255; + $CaptionAlpha = isset($Format["CaptionAlpha"]) ? $Format["CaptionAlpha"] : 100; + $DrawBox = isset($Format["DrawBox"]) ? $Format["DrawBox"] : TRUE; + $DrawBoxBorder = isset($Format["DrawBoxBorder"]) ? $Format["DrawBoxBorder"] : FALSE; + $BorderOffset = isset($Format["BorderOffset"]) ? $Format["BorderOffset"] : 5; + $BoxRounded = isset($Format["BoxRounded"]) ? $Format["BoxRounded"] : TRUE; + $RoundedRadius = isset($Format["RoundedRadius"]) ? $Format["RoundedRadius"] : 3; + $BoxR = isset($Format["BoxR"]) ? $Format["BoxR"] : 0; + $BoxG = isset($Format["BoxG"]) ? $Format["BoxG"] : 0; + $BoxB = isset($Format["BoxB"]) ? $Format["BoxB"] : 0; + $BoxAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 20; + $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : ""; + $BoxBorderR = isset($Format["BoxBorderR"]) ? $Format["BoxBorderR"] : 255; + $BoxBorderG = isset($Format["BoxBorderG"]) ? $Format["BoxBorderG"] : 255; + $BoxBorderB = isset($Format["BoxBorderB"]) ? $Format["BoxBorderB"] : 255; + $BoxBorderAlpha = isset($Format["BoxBorderAlpha"]) ? $Format["BoxBorderAlpha"] : 100; + $NoMargin = isset($Format["NoMargin"]) ? $Format["NoMargin"] : FALSE; + + if ( is_array($Value) ) { foreach ($Value as $Key => $ID) { $this->drawThreshold($ID,$Format); } return(0); } + + $CaptionSettings = array("DrawBox"=>$DrawBox,"DrawBoxBorder"=>$DrawBoxBorder,"BorderOffset"=>$BorderOffset,"BoxRounded"=>$BoxRounded,"RoundedRadius"=>$RoundedRadius, + "BoxR"=>$BoxR,"BoxG"=>$BoxG,"BoxB"=>$BoxB,"BoxAlpha"=>$BoxAlpha,"BoxSurrounding"=>$BoxSurrounding, + "BoxBorderR"=>$BoxBorderR,"BoxBorderG"=>$BoxBorderG,"BoxBorderB"=>$BoxBorderB,"BoxBorderAlpha"=>$BoxBorderAlpha, + "R"=>$CaptionR,"G"=>$CaptionG,"B"=>$CaptionB,"Alpha"=>$CaptionAlpha); + + $Data = $this->DataSet->getData(); + $AbscissaMargin = $this->getAbscissaMargin($Data); + + if ( $NoMargin ) { $AbscissaMargin = 0; } + if ( !isset($Data["Axis"][$AxisID]) ) { return(-1); } + if ( $Caption == NULL ) { $Caption = $Value; } + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + $YPos = $this->scaleComputeY($Value,array("AxisID"=>$AxisID)); + if ( $YPos >= $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"] && $YPos <= $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"] ) + { + $X1 = $this->GraphAreaX1 + $AbscissaMargin; + $X2 = $this->GraphAreaX2 - $AbscissaMargin; + + $this->drawLine($X1,$YPos,$X2,$YPos,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); + + if ( $Wide ) + { + $this->drawLine($X1,$YPos-1,$X2,$YPos-1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); + $this->drawLine($X1,$YPos+1,$X2,$YPos+1,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); + } + + if ( $WriteCaption ) + { + if ( $CaptionAlign == CAPTION_LEFT_TOP ) + { $X = $X1 + $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_MIDDLELEFT; } + else + { $X = $X2 - $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_MIDDLERIGHT; } + + $this->drawText($X,$YPos,$Caption,$CaptionSettings); + } + } + + return(array("Y"=>$YPos)); + } + + if ( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) + { + $XPos = $this->scaleComputeY($Value,array("AxisID"=>$AxisID)); + if ( $XPos >= $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"] && $XPos <= $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"] ) + { + $Y1 = $this->GraphAreaY1 + $AbscissaMargin; + $Y2 = $this->GraphAreaY2 - $AbscissaMargin; + + $this->drawLine($XPos,$Y1,$XPos,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); + + if ( $Wide ) + { + $this->drawLine($XPos-1,$Y1,$XPos-1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); + $this->drawLine($XPos+1,$Y1,$XPos+1,$Y2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/$WideFactor,"Ticks"=>$Ticks)); + } + + if ( $WriteCaption ) + { + if ( $CaptionAlign == CAPTION_LEFT_TOP ) + { $Y = $Y1 + $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_TOPMIDDLE; } + else + { $Y = $Y2 - $CaptionOffset; $CaptionSettings["Align"] = TEXT_ALIGN_BOTTOMMIDDLE; } + + $CaptionSettings["Align"] = TEXT_ALIGN_TOPMIDDLE; + $this->drawText($XPos,$Y,$Caption,$CaptionSettings); + } + } + + return(array("Y"=>$XPos)); + } + } + + /* Draw a threshold with the computed scale */ + function drawThresholdArea($Value1,$Value2,$Format="") + { + $AxisID = isset($Format["AxisID"]) ? $Format["AxisID"] : 0; + $R = isset($Format["R"]) ? $Format["R"] : 255; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 20; + $Border = isset($Format["Border"]) ? $Format["Border"] : TRUE; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; + $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha + 20; + $BorderTicks = isset($Format["BorderTicks"]) ? $Format["BorderTicks"] : 2; + $AreaName = isset($Format["AreaName"]) ? $Format["AreaName"] : NULL; + $NameAngle = isset($Format["NameAngle"]) ? $Format["NameAngle"] : ZONE_NAME_ANGLE_AUTO; + $NameR = isset($Format["NameR"]) ? $Format["NameR"] : 255; + $NameG = isset($Format["NameG"]) ? $Format["NameG"] : 255; + $NameB = isset($Format["NameB"]) ? $Format["NameB"] : 255; + $NameAlpha = isset($Format["NameAlpha"]) ? $Format["NameAlpha"] : 100; + $DisableShadowOnArea = isset($Format["DisableShadowOnArea"]) ? $Format["DisableShadowOnArea"] : TRUE; + $NoMargin = isset($Format["NoMargin"]) ? $Format["NoMargin"] : FALSE; + + if ($Value1 > $Value2) { list($Value1, $Value2) = array($Value2, $Value1); } + + $RestoreShadow = $this->Shadow; + if ( $DisableShadowOnArea && $this->Shadow ) { $this->Shadow = FALSE; } + + if ($BorderAlpha >100) { $BorderAlpha = 100;} + + $Data = $this->DataSet->getData(); + $AbscissaMargin = $this->getAbscissaMargin($Data); + + if ( $NoMargin ) { $AbscissaMargin = 0; } + if ( !isset($Data["Axis"][$AxisID]) ) { return(-1); } + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + $XPos1 = $this->GraphAreaX1 + $AbscissaMargin; + $XPos2 = $this->GraphAreaX2 - $AbscissaMargin; + $YPos1 = $this->scaleComputeY($Value1,array("AxisID"=>$AxisID)); + $YPos2 = $this->scaleComputeY($Value2,array("AxisID"=>$AxisID)); + + if ( $YPos1 < $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"] ) { $YPos1 = $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"]; } + if ( $YPos1 > $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"] ) { $YPos1 = $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"]; } + if ( $YPos2 < $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"] ) { $YPos2 = $this->GraphAreaY1+$Data["Axis"][$AxisID]["Margin"]; } + if ( $YPos2 > $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"] ) { $YPos2 = $this->GraphAreaY2-$Data["Axis"][$AxisID]["Margin"]; } + + $this->drawFilledRectangle($XPos1,$YPos1,$XPos2,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + if ( $Border ) + { + $this->drawLine($XPos1,$YPos1,$XPos2,$YPos1,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); + $this->drawLine($XPos1,$YPos2,$XPos2,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); + } + + if ( $AreaName != NULL ) + { + $XPos = ($XPos2-$XPos1)/2 + $XPos1; + $YPos = ($YPos2-$YPos1)/2 + $YPos1; + $this->Shadow = $RestoreShadow; + $this->drawText($XPos,$YPos,$AreaName,array("R"=>$NameR,"G"=>$NameG,"B"=>$NameB,"Alpha"=>$NameAlpha,"Angle"=>0,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); + if ( $DisableShadowOnArea ) { $this->Shadow = FALSE; } + } + + $this->Shadow = $RestoreShadow; + return(array("Y1"=>$YPos1,"Y2"=>$YPos2)); + } + elseif ( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) + { + $YPos1 = $this->GraphAreaY1 + $AbscissaMargin; + $YPos2 = $this->GraphAreaY2 - $AbscissaMargin; + $XPos1 = $this->scaleComputeY($Value1,array("AxisID"=>$AxisID)); + $XPos2 = $this->scaleComputeY($Value2,array("AxisID"=>$AxisID)); + + if ( $XPos1 < $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"] ) { $XPos1 = $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"]; } + if ( $XPos1 > $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"] ) { $XPos1 = $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"]; } + if ( $XPos2 < $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"] ) { $XPos2 = $this->GraphAreaX1+$Data["Axis"][$AxisID]["Margin"]; } + if ( $XPos2 > $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"] ) { $XPos2 = $this->GraphAreaX2-$Data["Axis"][$AxisID]["Margin"]; } + + $this->drawFilledRectangle($XPos1,$YPos1,$XPos2,$YPos2,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + if ( $Border ) + { + $this->drawLine($XPos1,$YPos1,$XPos1,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); + $this->drawLine($XPos2,$YPos1,$XPos2,$YPos2,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Ticks"=>$BorderTicks)); + } + + if ( $AreaName != NULL ) + { + $XPos = ($YPos2-$YPos1)/2 + $YPos1; + $YPos = ($XPos2-$XPos1)/2 + $XPos1; + + if ( $NameAngle == ZONE_NAME_ANGLE_AUTO ) + { + $TxtPos = $this->getTextBox($XPos,$YPos,$this->FontName,$this->FontSize,0,$AreaName); + $TxtWidth = $TxtPos[1]["X"] - $TxtPos[0]["X"]; + if ( abs($XPos2 - $XPos1) > $TxtWidth ) { $NameAngle = 0; } else { $NameAngle = 90; } + } + $this->Shadow = $RestoreShadow; + $this->drawText($YPos,$XPos,$AreaName,array("R"=>$NameR,"G"=>$NameG,"B"=>$NameB,"Alpha"=>$NameAlpha,"Angle"=>$NameAngle,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE)); + if ( $DisableShadowOnArea ) { $this->Shadow = FALSE; } + } + + $this->Shadow = $RestoreShadow; + return(array("Y1"=>$XPos1,"Y2"=>$XPos2)); + } + } + + function scaleGetXSettings() + { + $Data = $this->DataSet->getData(); + foreach($Data["Axis"] as $AxisID => $Settings) + { + if ( $Settings["Identity"] == AXIS_X ) + { + $Rows = $Settings["Rows"]; + + return(array($Settings["Margin"],$Rows)); + } + } + } + + function scaleComputeY($Values,$Option="",$ReturnOnly0Height=FALSE) + { + $AxisID = isset($Option["AxisID"]) ? $Option["AxisID"] : 0; + $SerieName = isset($Option["SerieName"]) ? $Option["SerieName"] : NULL; + + $Data = $this->DataSet->getData(); + if ( !isset($Data["Axis"][$AxisID]) ) { return(-1); } + + if ( $SerieName != NULL ) { $AxisID = $Data["Series"][$SerieName]["Axis"]; } + if ( !is_array($Values) ) { $tmp = $Values; $Values = ""; $Values[0] = $tmp; } + + $Result = ""; + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + $Height = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Data["Axis"][$AxisID]["Margin"]*2; + $ScaleHeight = $Data["Axis"][$AxisID]["ScaleMax"] - $Data["Axis"][$AxisID]["ScaleMin"]; + $Step = $Height / $ScaleHeight; + + if ( $ReturnOnly0Height ) + { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $Step * $Value; } } } + else + { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $this->GraphAreaY2 - $Data["Axis"][$AxisID]["Margin"] - ($Step * ($Value-$Data["Axis"][$AxisID]["ScaleMin"])); } } } + } + else + { + $Width = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Data["Axis"][$AxisID]["Margin"]*2; + $ScaleWidth = $Data["Axis"][$AxisID]["ScaleMax"] - $Data["Axis"][$AxisID]["ScaleMin"]; + $Step = $Width / $ScaleWidth; + + if ( $ReturnOnly0Height ) + { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $Step * $Value; } } } + else + { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $this->GraphAreaX1 + $Data["Axis"][$AxisID]["Margin"] + ($Step * ($Value-$Data["Axis"][$AxisID]["ScaleMin"])); } } } + } + + if ( count($Result) == 1 ) + return($Result[0]); + else + return($Result); + } + + /* Format the axis values */ + function scaleFormat($Value,$Mode=NULL,$Format=NULL,$Unit=NULL) + { + if ( $Value == VOID ) { return(""); } + + if ( $Mode == AXIS_FORMAT_CUSTOM ) + { if ( function_exists($Format) ) { return(call_user_func($Format,$Value)); } } + + if ( $Mode == AXIS_FORMAT_DATE ) + { if ( $Format == NULL ) { $Pattern = "d/m/Y"; } else { $Pattern = $Format; } return(date($Pattern,$Value)); } + + if ( $Mode == AXIS_FORMAT_TIME ) + { if ( $Format == NULL ) { $Pattern = "H:i:s"; } else { $Pattern = $Format; } return(date($Pattern,$Value)); } + + if ( $Mode == AXIS_FORMAT_CURRENCY ) + { return($Format.number_format($Value,2)); } + + if ( $Mode == AXIS_FORMAT_METRIC ) + { + if (abs($Value) > 1000000000) + return(round($Value/1000000000,$Format)."g".$Unit); + if (abs($Value) > 1000000) + return(round($Value/1000000,$Format)."m".$Unit); + elseif (abs($Value) >= 1000) + return(round($Value/1000,$Format)."k".$Unit); + + } + return($Value.$Unit); + } + + /* Write Max value on a chart */ + function writeBounds($Type=BOUND_BOTH,$Format=NULL) + { + $MaxLabelTxt = isset($Format["MaxLabelTxt"]) ? $Format["MaxLabelTxt"] : "max="; + $MinLabelTxt = isset($Format["MinLabelTxt"]) ? $Format["MinLabelTxt"] : "min="; + $Decimals = isset($Format["Decimals"]) ? $Format["Decimals"] : 1; + $ExcludedSeries = isset($Format["ExcludedSeries"]) ? $Format["ExcludedSeries"] : ""; + $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 4; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $MaxDisplayR = isset($Format["MaxDisplayR"]) ? $Format["MaxDisplayR"] : 0; + $MaxDisplayG = isset($Format["MaxDisplayG"]) ? $Format["MaxDisplayG"] : 0; + $MaxDisplayB = isset($Format["MaxDisplayB"]) ? $Format["MaxDisplayB"] : 0; + $MinDisplayR = isset($Format["MinDisplayR"]) ? $Format["MinDisplayR"] : 255; + $MinDisplayG = isset($Format["MinDisplayG"]) ? $Format["MinDisplayG"] : 255; + $MinDisplayB = isset($Format["MinDisplayB"]) ? $Format["MinDisplayB"] : 255; + $MinLabelPos = isset($Format["MinLabelPos"]) ? $Format["MinLabelPos"] : BOUND_LABEL_POS_AUTO; + $MaxLabelPos = isset($Format["MaxLabelPos"]) ? $Format["MaxLabelPos"] : BOUND_LABEL_POS_AUTO; + $DrawBox = isset($Format["DrawBox"]) ? $Format["DrawBox"] : TRUE; + $DrawBoxBorder = isset($Format["DrawBoxBorder"]) ? $Format["DrawBoxBorder"] : FALSE; + $BorderOffset = isset($Format["BorderOffset"]) ? $Format["BorderOffset"] : 5; + $BoxRounded = isset($Format["BoxRounded"]) ? $Format["BoxRounded"] : TRUE; + $RoundedRadius = isset($Format["RoundedRadius"]) ? $Format["RoundedRadius"] : 3; + $BoxR = isset($Format["BoxR"]) ? $Format["BoxR"] : 0; + $BoxG = isset($Format["BoxG"]) ? $Format["BoxG"] : 0; + $BoxB = isset($Format["BoxB"]) ? $Format["BoxB"] : 0; + $BoxAlpha = isset($Format["BoxAlpha"]) ? $Format["BoxAlpha"] : 20; + $BoxSurrounding = isset($Format["BoxSurrounding"]) ? $Format["BoxSurrounding"] : ""; + $BoxBorderR = isset($Format["BoxBorderR"]) ? $Format["BoxBorderR"] : 255; + $BoxBorderG = isset($Format["BoxBorderG"]) ? $Format["BoxBorderG"] : 255; + $BoxBorderB = isset($Format["BoxBorderB"]) ? $Format["BoxBorderB"] : 255; + $BoxBorderAlpha = isset($Format["BoxBorderAlpha"]) ? $Format["BoxBorderAlpha"] : 100; + + $CaptionSettings = array("DrawBox"=>$DrawBox,"DrawBoxBorder"=>$DrawBoxBorder,"BorderOffset"=>$BorderOffset,"BoxRounded"=>$BoxRounded,"RoundedRadius"=>$RoundedRadius, + "BoxR"=>$BoxR,"BoxG"=>$BoxG,"BoxB"=>$BoxB,"BoxAlpha"=>$BoxAlpha,"BoxSurrounding"=>$BoxSurrounding, + "BoxBorderR"=>$BoxBorderR,"BoxBorderG"=>$BoxBorderG,"BoxBorderB"=>$BoxBorderB,"BoxBorderAlpha"=>$BoxBorderAlpha); + + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + $Data = $this->DataSet->getData(); + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] && !isset($ExcludedSeries[$SerieName])) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } + + $MinValue = $this->DataSet->getMin($SerieName); + $MaxValue = $this->DataSet->getMax($SerieName); + + $MinPos = VOID; $MaxPos = VOID; + foreach($Serie["Data"] as $Key => $Value) + { + if ( $Value == $MinValue && $MinPos == VOID ) { $MinPos = $Key; } + if ( $Value == $MaxValue ) { $MaxPos = $Key; } + } + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; + $X = $this->GraphAreaX1 + $XMargin; + $SerieOffset = isset($Serie["XOffset"]) ? $Serie["XOffset"] : 0; + + if ( $Type == BOUND_MAX || $Type == BOUND_BOTH ) + { + if ( $MaxLabelPos == BOUND_LABEL_POS_TOP || ( $MaxLabelPos == BOUND_LABEL_POS_AUTO && $MaxValue >= 0) ) { $YPos = $PosArray[$MaxPos] - $DisplayOffset + 2; $Align = TEXT_ALIGN_BOTTOMMIDDLE; } + if ( $MaxLabelPos == BOUND_LABEL_POS_BOTTOM || ( $MaxLabelPos == BOUND_LABEL_POS_AUTO && $MaxValue < 0) ) { $YPos = $PosArray[$MaxPos] + $DisplayOffset + 2; $Align = TEXT_ALIGN_TOPMIDDLE; } + + $XPos = $X + $MaxPos*$XStep + $SerieOffset; + $Label = $MaxLabelTxt.$this->scaleFormat(round($MaxValue,$Decimals),$Mode,$Format,$Unit); + + $TxtPos = $this->getTextBox($XPos,$YPos,$this->FontName,$this->FontSize,0,$Label); + $XOffset = 0; $YOffset = 0; + if ( $TxtPos[0]["X"] < $this->GraphAreaX1 ) { $XOffset = (($this->GraphAreaX1 - $TxtPos[0]["X"])/2); } + if ( $TxtPos[1]["X"] > $this->GraphAreaX2 ) { $XOffset = -(($TxtPos[1]["X"] - $this->GraphAreaX2)/2); } + if ( $TxtPos[2]["Y"] < $this->GraphAreaY1 ) { $YOffset = $this->GraphAreaY1 - $TxtPos[2]["Y"]; } + if ( $TxtPos[0]["Y"] > $this->GraphAreaY2 ) { $YOffset = -($TxtPos[0]["Y"] - $this->GraphAreaY2); } + + $CaptionSettings["R"] = $MaxDisplayR; $CaptionSettings["G"] = $MaxDisplayG; + $CaptionSettings["B"] = $MaxDisplayB; $CaptionSettings["Align"] = $Align; + + $this->drawText($XPos+$XOffset,$YPos+$YOffset,$Label,$CaptionSettings); + } + + if ( $Type == BOUND_MIN || $Type == BOUND_BOTH ) + { + if ( $MinLabelPos == BOUND_LABEL_POS_TOP || ( $MinLabelPos == BOUND_LABEL_POS_AUTO && $MinValue >= 0) ) { $YPos = $PosArray[$MinPos] - $DisplayOffset + 2; $Align = TEXT_ALIGN_BOTTOMMIDDLE; } + if ( $MinLabelPos == BOUND_LABEL_POS_BOTTOM || ( $MinLabelPos == BOUND_LABEL_POS_AUTO && $MinValue < 0) ) { $YPos = $PosArray[$MinPos] + $DisplayOffset + 2; $Align = TEXT_ALIGN_TOPMIDDLE; } + + $XPos = $X + $MinPos*$XStep + $SerieOffset; + $Label = $MinLabelTxt.$this->scaleFormat(round($MinValue,$Decimals),$Mode,$Format,$Unit); + + $TxtPos = $this->getTextBox($XPos,$YPos,$this->FontName,$this->FontSize,0,$Label); + $XOffset = 0; $YOffset = 0; + if ( $TxtPos[0]["X"] < $this->GraphAreaX1 ) { $XOffset = (($this->GraphAreaX1 - $TxtPos[0]["X"])/2); } + if ( $TxtPos[1]["X"] > $this->GraphAreaX2 ) { $XOffset = -(($TxtPos[1]["X"] - $this->GraphAreaX2)/2); } + if ( $TxtPos[2]["Y"] < $this->GraphAreaY1 ) { $YOffset = $this->GraphAreaY1 - $TxtPos[2]["Y"]; } + if ( $TxtPos[0]["Y"] > $this->GraphAreaY2 ) { $YOffset = -($TxtPos[0]["Y"] - $this->GraphAreaY2); } + + $CaptionSettings["R"] = $MinDisplayR; $CaptionSettings["G"] = $MinDisplayG; + $CaptionSettings["B"] = $MinDisplayB; $CaptionSettings["Align"] = $Align; + + $this->drawText($XPos+$XOffset,$YPos-$DisplayOffset+$YOffset,$Label,$CaptionSettings); + } + } + else + { + $XStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; + $X = $this->GraphAreaY1 + $XMargin; + $SerieOffset = isset($Serie["XOffset"]) ? $Serie["XOffset"] : 0; + + if ( $Type == BOUND_MAX || $Type == BOUND_BOTH ) + { + if ( $MaxLabelPos == BOUND_LABEL_POS_TOP || ( $MaxLabelPos == BOUND_LABEL_POS_AUTO && $MaxValue >= 0) ) { $YPos = $PosArray[$MaxPos] + $DisplayOffset + 2; $Align = TEXT_ALIGN_MIDDLELEFT; } + if ( $MaxLabelPos == BOUND_LABEL_POS_BOTTOM || ( $MaxLabelPos == BOUND_LABEL_POS_AUTO && $MaxValue < 0) ) { $YPos = $PosArray[$MaxPos] - $DisplayOffset + 2; $Align = TEXT_ALIGN_MIDDLERIGHT; } + + $XPos = $X + $MaxPos*$XStep + $SerieOffset; + $Label = $MaxLabelTxt.$this->scaleFormat($MaxValue,$Mode,$Format,$Unit); + + $TxtPos = $this->getTextBox($YPos,$XPos,$this->FontName,$this->FontSize,0,$Label); + $XOffset = 0; $YOffset = 0; + if ( $TxtPos[0]["X"] < $this->GraphAreaX1 ) { $XOffset = $this->GraphAreaX1 - $TxtPos[0]["X"]; } + if ( $TxtPos[1]["X"] > $this->GraphAreaX2 ) { $XOffset = -($TxtPos[1]["X"] - $this->GraphAreaX2); } + if ( $TxtPos[2]["Y"] < $this->GraphAreaY1 ) { $YOffset = ($this->GraphAreaY1 - $TxtPos[2]["Y"])/2; } + if ( $TxtPos[0]["Y"] > $this->GraphAreaY2 ) { $YOffset = -(($TxtPos[0]["Y"] - $this->GraphAreaY2)/2);} + + $CaptionSettings["R"] = $MaxDisplayR; $CaptionSettings["G"] = $MaxDisplayG; + $CaptionSettings["B"] = $MaxDisplayB; $CaptionSettings["Align"] = $Align; + + $this->drawText($YPos+$XOffset,$XPos+$YOffset,$Label,$CaptionSettings); + } + + if ( $Type == BOUND_MIN || $Type == BOUND_BOTH ) + { + if ( $MinLabelPos == BOUND_LABEL_POS_TOP || ( $MinLabelPos == BOUND_LABEL_POS_AUTO && $MinValue >= 0) ) { $YPos = $PosArray[$MinPos] + $DisplayOffset + 2; $Align = TEXT_ALIGN_MIDDLELEFT; } + if ( $MinLabelPos == BOUND_LABEL_POS_BOTTOM || ( $MinLabelPos == BOUND_LABEL_POS_AUTO && $MinValue < 0) ) { $YPos = $PosArray[$MinPos] - $DisplayOffset + 2; $Align = TEXT_ALIGN_MIDDLERIGHT; } + + $XPos = $X + $MinPos*$XStep + $SerieOffset; + $Label = $MinLabelTxt.$this->scaleFormat($MinValue,$Mode,$Format,$Unit); + + $TxtPos = $this->getTextBox($YPos,$XPos,$this->FontName,$this->FontSize,0,$Label); + $XOffset = 0; $YOffset = 0; + if ( $TxtPos[0]["X"] < $this->GraphAreaX1 ) { $XOffset = $this->GraphAreaX1 - $TxtPos[0]["X"]; } + if ( $TxtPos[1]["X"] > $this->GraphAreaX2 ) { $XOffset = -($TxtPos[1]["X"] - $this->GraphAreaX2); } + if ( $TxtPos[2]["Y"] < $this->GraphAreaY1 ) { $YOffset = ($this->GraphAreaY1 - $TxtPos[2]["Y"])/2; } + if ( $TxtPos[0]["Y"] > $this->GraphAreaY2 ) { $YOffset = -(($TxtPos[0]["Y"] - $this->GraphAreaY2)/2);} + + $CaptionSettings["R"] = $MinDisplayR; $CaptionSettings["G"] = $MinDisplayG; + $CaptionSettings["B"] = $MinDisplayB; $CaptionSettings["Align"] = $Align; + + $this->drawText($YPos+$XOffset,$XPos+$YOffset,$Label,$CaptionSettings); + } + } + } + } + } + + /* Draw a plot chart */ + function drawPlotChart($Format=NULL) + { + $PlotSize = isset($Format["PlotSize"]) ? $Format["PlotSize"] : NULL; + $PlotBorder = isset($Format["PlotBorder"]) ? $Format["PlotBorder"] : FALSE; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 50; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 50; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 50; + $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : 30; + $BorderSize = isset($Format["BorderSize"]) ? $Format["BorderSize"] : 2; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; + $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 4; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; + $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; + $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + + $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + if ( isset($Serie["Weight"]) ) { $SerieWeight = $Serie["Weight"] + 2; } else { $SerieWeight = 2; } + if ( $PlotSize != NULL ) { $SerieWeight = $PlotSize; } + + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; + if ( $Surrounding != NULL ) { $BorderR = $R + $Surrounding; $BorderG = $G + $Surrounding; $BorderB = $B + $Surrounding; } + if ( isset($Serie["Picture"]) ) + { $Picture = $Serie["Picture"]; list($PicWidth,$PicHeight,$PicType) = $this->getPicInfo($Picture); } + else { $Picture = NULL; $PicOffset = 0; } + + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } + + $AxisID = $Serie["Axis"]; + $Shape = $Serie["Shape"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + if ( $Picture != NULL ) { $PicOffset = $PicHeight / 2; $SerieWeight = 0; } + $X = $this->GraphAreaX1 + $XMargin; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + foreach($PosArray as $Key => $Y) + { + if ( $DisplayValues ) + $this->drawText($X,$Y-$DisplayOffset-$SerieWeight-$BorderSize-$PicOffset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); + + if ( $Y != VOID ) + { + if ( $RecordImageMap ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$SerieWeight,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $Picture != NULL ) + { $this->drawFromPicture($PicType,$Picture,$X-$PicWidth/2,$Y-$PicHeight/2); } + else + { $this->drawShape($X,$Y,$Shape,$SerieWeight,$PlotBorder,$BorderSize,$R,$G,$B,$Alpha,$BorderR,$BorderG,$BorderB,$BorderAlpha); } + } + $X = $X + $XStep; + } + } + else + { + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + if ( $Picture != NULL ) { $PicOffset = $PicWidth / 2; $SerieWeight = 0; } + $Y = $this->GraphAreaY1 + $XMargin; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + foreach($PosArray as $Key => $X) + { + if ( $DisplayValues ) + $this->drawText($X+$DisplayOffset+$SerieWeight+$BorderSize+$PicOffset,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); + + if ( $X != VOID ) + { + if ( $RecordImageMap ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$SerieWeight,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $Picture != NULL ) + { $this->drawFromPicture($PicType,$Picture,$X-$PicWidth/2,$Y-$PicHeight/2); } + else + { $this->drawShape($X,$Y,$Shape,$SerieWeight,$PlotBorder,$BorderSize,$R,$G,$B,$Alpha,$BorderR,$BorderG,$BorderB,$BorderAlpha); } + } + $Y = $Y + $YStep; + } + } + } + } + } + + /* Draw a spline chart */ + function drawSplineChart($Format=NULL) + { + $BreakVoid = isset($Format["BreakVoid"]) ? $Format["BreakVoid"] : TRUE; + $VoidTicks = isset($Format["VoidTicks"]) ? $Format["VoidTicks"] : 4; + $BreakR = isset($Format["BreakR"]) ? $Format["BreakR"] : NULL; // 234 + $BreakG = isset($Format["BreakG"]) ? $Format["BreakG"] : NULL; // 55 + $BreakB = isset($Format["BreakB"]) ? $Format["BreakB"] : NULL; // 26 + $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; + $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; + $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; + $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + $ImageMapPlotSize = isset($Format["ImageMapPlotSize"]) ? $Format["ImageMapPlotSize"] : 5; + + $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; + + if ( $BreakR == NULL ) + $BreakSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks); + else + $BreakSettings = array("R"=>$BreakR,"G"=>$BreakG,"B"=>$BreakB,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); + + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; $WayPoints = ""; + $Force = $XStep / 5; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $LastGoodY = NULL; $LastGoodX = NULL; $LastX = 1; $LastY = 1; + foreach($PosArray as $Key => $Y) + { + if ( $DisplayValues ) + $this->drawText($X,$Y-$DisplayOffset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); + + if ( $RecordImageMap && $Y != VOID ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$ImageMapPlotSize,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $Y == VOID && $LastY != NULL ) + { $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); $WayPoints = ""; } + + if ( $Y != VOID && $LastY == NULL && $LastGoodY != NULL && !$BreakVoid ) + { $this->drawLine($LastGoodX,$LastGoodY,$X,$Y,$BreakSettings); } + + if ( $Y != VOID ) + $WayPoints[] = array($X,$Y); + + if ( $Y != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } + if ( $Y == VOID ) { $Y = NULL; } + + $LastX = $X; $LastY = $Y; + $X = $X + $XStep; + } + $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); + } + else + { + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; $WayPoints = ""; + $Force = $YStep / 5; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $LastGoodY = NULL; $LastGoodX = NULL; $LastX = 1; $LastY = 1; + foreach($PosArray as $Key => $X) + { + if ( $DisplayValues ) + $this->drawText($X+$DisplayOffset,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); + + if ( $RecordImageMap && $X != VOID ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$ImageMapPlotSize,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $X == VOID && $LastX != NULL ) + { $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); $WayPoints = ""; } + + if ( $X != VOID && $LastX == NULL && $LastGoodX != NULL && !$BreakVoid ) + { $this->drawLine($LastGoodX,$LastGoodY,$X,$Y,$BreakSettings); } + + if ( $X != VOID ) + $WayPoints[] = array($X,$Y); + + if ( $X != VOID ) { $LastGoodX = $X; $LastGoodY = $Y; } + if ( $X == VOID ) { $X = NULL; } + + $LastX = $X; $LastY = $Y; + $Y = $Y + $YStep; + } + $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); + } + } + } + } + + /* Draw a filled spline chart */ + function drawFilledSplineChart($Format=NULL) + { + $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; + $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; + $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; + $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; + $AroundZero = isset($Format["AroundZero"]) ? $Format["AroundZero"] : TRUE; + $Threshold = isset($Format["Threshold"]) ? $Format["Threshold"] : NULL; + + $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + if ( $AroundZero ) { $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); } + + if ( $Threshold != NULL ) + { + foreach($Threshold as $Key => $Params) + { + $Threshold[$Key]["MinX"] = $this->scaleComputeY($Params["Min"],array("AxisID"=>$Serie["Axis"])); + $Threshold[$Key]["MaxX"] = $this->scaleComputeY($Params["Max"],array("AxisID"=>$Serie["Axis"])); + } + } + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; $WayPoints = ""; + $Force = $XStep / 5; + + if ( !$AroundZero ) { $YZero = $this->GraphAreaY2-1; } + if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } + if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } + + $LastX = ""; $LastY = ""; + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + foreach($PosArray as $Key => $Y) + { + if ( $DisplayValues ) + $this->drawText($X,$Y-$DisplayOffset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); + + if ( $Y == VOID ) + { + $Area = $this->drawSpline($WayPoints,array("Force"=>$Force,"PathOnly"=>TRUE)); + + if ( $Area != "" ) + { + foreach ($Area as $key => $Points) + { + $Corners = ""; $Corners[] = $Area[$key][0]["X"]; $Corners[] = $YZero; + foreach($Points as $subKey => $Point) + { + if ( $subKey == count($Points)-1) { $Corners[] = $Point["X"]-1; } else { $Corners[] = $Point["X"]; } + $Corners[] = $Point["Y"]+1; + } + $Corners[] = $Points[$subKey]["X"]-1; $Corners[] = $YZero; + + $this->drawPolygonChart($Corners,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/2,"NoBorder"=>TRUE,"Threshold"=>$Threshold)); + } + $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + } + + $WayPoints = ""; + } + else + $WayPoints[] = array($X,$Y-.5); /* -.5 for AA visual fix */ + + $X = $X + $XStep; + } + $Area = $this->drawSpline($WayPoints,array("Force"=>$Force,"PathOnly"=>TRUE)); + + if ( $Area != "" ) + { + foreach ($Area as $key => $Points) + { + $Corners = ""; $Corners[] = $Area[$key][0]["X"]; $Corners[] = $YZero; + foreach($Points as $subKey => $Point) + { + if ( $subKey == count($Points)-1) { $Corners[] = $Point["X"]-1; } else { $Corners[] = $Point["X"]; } + $Corners[] = $Point["Y"]+1; + } + $Corners[] = $Points[$subKey]["X"]-1; $Corners[] = $YZero; + + $this->drawPolygonChart($Corners,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/2,"NoBorder"=>TRUE,"Threshold"=>$Threshold)); + } + $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + } + } + else + { + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; $WayPoints = ""; + $Force = $YStep / 5; + + if ( !$AroundZero ) { $YZero = $this->GraphAreaX1+1; } + if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } + if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + foreach($PosArray as $Key => $X) + { + if ( $DisplayValues ) + $this->drawText($X+$DisplayOffset,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); + + if ( $X == VOID ) + { + $Area = $this->drawSpline($WayPoints,array("Force"=>$Force,"PathOnly"=>TRUE)); + + if ( $Area != "" ) + { + foreach ($Area as $key => $Points) + { + $Corners = ""; $Corners[] = $YZero; $Corners[] = $Area[$key][0]["Y"]; + foreach($Points as $subKey => $Point) + { + if ( $subKey == count($Points)-1) { $Corners[] = $Point["X"]-1; } else { $Corners[] = $Point["X"]; } + $Corners[] = $Point["Y"]; + } + $Corners[] = $YZero; $Corners[] = $Points[$subKey]["Y"]-1; + + $this->drawPolygonChart($Corners,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/2,"NoBorder"=>TRUE,"Threshold"=>$Threshold)); + } + $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + } + + $WayPoints = ""; + } + else + $WayPoints[] = array($X,$Y); + + $Y = $Y + $YStep; + } + $Area = $this->drawSpline($WayPoints,array("Force"=>$Force,"PathOnly"=>TRUE)); + + if ( $Area != "" ) + { + foreach ($Area as $key => $Points) + { + $Corners = ""; $Corners[] = $YZero; $Corners[] = $Area[$key][0]["Y"]; + foreach($Points as $subKey => $Point) + { + if ( $subKey == count($Points)-1) { $Corners[] = $Point["X"]-1; } else { $Corners[] = $Point["X"]; } + $Corners[] = $Point["Y"]; + } + $Corners[] = $YZero; $Corners[] = $Points[$subKey]["Y"]-1; + + $this->drawPolygonChart($Corners,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha/2,"NoBorder"=>TRUE,"Threshold"=>$Threshold)); + } + $this->drawSpline($WayPoints,array("Force"=>$Force,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks)); + } + + } + } + } + } + + /* Draw a line chart */ + function drawLineChart($Format=NULL) + { + $BreakVoid = isset($Format["BreakVoid"]) ? $Format["BreakVoid"] : TRUE; + $VoidTicks = isset($Format["VoidTicks"]) ? $Format["VoidTicks"] : 4; + $BreakR = isset($Format["BreakR"]) ? $Format["BreakR"] : NULL; + $BreakG = isset($Format["BreakG"]) ? $Format["BreakG"] : NULL; + $BreakB = isset($Format["BreakB"]) ? $Format["BreakB"] : NULL; + $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; + $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; + $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; + $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + $ImageMapPlotSize = isset($Format["ImageMapPlotSize"]) ? $Format["ImageMapPlotSize"] : 5; + $ForceColor = isset($Format["ForceColor"]) ? $Format["ForceColor"] : FALSE; + $ForceR = isset($Format["ForceR"]) ? $Format["ForceR"] : 0; + $ForceG = isset($Format["ForceG"]) ? $Format["ForceG"] : 0; + $ForceB = isset($Format["ForceB"]) ? $Format["ForceB"] : 0; + $ForceAlpha = isset($Format["ForceAlpha"]) ? $Format["ForceAlpha"] : 100; + + $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; + + if ( $ForceColor ) + { $R = $ForceR; $G = $ForceG; $B = $ForceB; $Alpha = $ForceAlpha; } + + if ( $BreakR == NULL ) + $BreakSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); + else + $BreakSettings = array("R"=>$BreakR,"G"=>$BreakG,"B"=>$BreakB,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); + + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $LastGoodY = NULL; $LastGoodX = NULL; + foreach($PosArray as $Key => $Y) + { + if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) + { + if ( $Serie["Data"][$Key] > 0 ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } + $this->drawText($X,$Y-$Offset-$Weight,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); + } + + if ( $RecordImageMap && $Y != VOID ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$ImageMapPlotSize,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $Y != VOID && $LastX != NULL && $LastY != NULL ) + $this->drawLine($LastX,$LastY,$X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); + + if ( $Y != VOID && $LastY == NULL && $LastGoodY != NULL && !$BreakVoid ) + { $this->drawLine($LastGoodX,$LastGoodY,$X,$Y,$BreakSettings); $LastGoodY = NULL; } + + if ( $Y != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } + if ( $Y == VOID ) { $Y = NULL; } + + $LastX = $X; $LastY = $Y; + $X = $X + $XStep; + } + } + else + { + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $LastGoodY = NULL; $LastGoodX = NULL; + foreach($PosArray as $Key => $X) + { + if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) + { $this->drawText($X+$DisplayOffset+$Weight,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); } + + if ( $RecordImageMap && $X != VOID ) { $this->addToImageMap("CIRCLE",floor($X).",".floor($Y).",".$ImageMapPlotSize,$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $X != VOID && $LastX != NULL && $LastY != NULL ) + $this->drawLine($LastX,$LastY,$X,$Y,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight)); + + if ( $X != VOID && $LastX == NULL && $LastGoodY != NULL && !$BreakVoid ) + { $this->drawLine($LastGoodX,$LastGoodY,$X,$Y,$BreakSettings); $LastGoodY = NULL; } + + if ( $X != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } + if ( $X == VOID ) { $X = NULL; } + + $LastX = $X; $LastY = $Y; + $Y = $Y + $YStep; + } + } + } + } + } + + /* Draw a line chart */ + function drawZoneChart($SerieA,$SerieB,$Format=NULL) + { + $AxisID = isset($Format["AxisID"]) ? $Format["AxisID"] : 0; + $LineR = isset($Format["LineR"]) ? $Format["LineR"] : 150; + $LineG = isset($Format["LineG"]) ? $Format["LineG"] : 150; + $LineB = isset($Format["LineB"]) ? $Format["LineB"] : 150; + $LineAlpha = isset($Format["LineAlpha"]) ? $Format["LineAlpha"] : 50; + $LineTicks = isset($Format["LineTicks"]) ? $Format["LineTicks"] : 1; + $AreaR = isset($Format["AreaR"]) ? $Format["AreaR"] : 150; + $AreaG = isset($Format["AreaG"]) ? $Format["AreaG"] : 150; + $AreaB = isset($Format["AreaB"]) ? $Format["AreaB"] : 150; + $AreaAlpha = isset($Format["AreaAlpha"]) ? $Format["AreaAlpha"] : 5; + + $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; + + $Data = $this->DataSet->getData(); + if ( !isset($Data["Series"][$SerieA]["Data"]) || !isset($Data["Series"][$SerieB]["Data"]) ) { return(0); } + $SerieAData = $Data["Series"][$SerieA]["Data"]; + $SerieBData = $Data["Series"][$SerieB]["Data"]; + + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + $PosArrayA = $this->scaleComputeY($SerieAData,array("AxisID"=>$AxisID)); + $PosArrayB = $this->scaleComputeY($SerieBData,array("AxisID"=>$AxisID)); + if ( count($PosArrayA) != count($PosArrayB) ) { return(0); } + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; + + $LastX = NULL; $LastY1 = NULL; $LastY2 = NULL; + $BoundsA = ""; $BoundsB = ""; + foreach($PosArrayA as $Key => $Y1) + { + $Y2 = $PosArrayB[$Key]; + + $BoundsA[] = $X; $BoundsA[] = $Y1; + $BoundsB[] = $X; $BoundsB[] = $Y2; + + $LastX = $X; + $LastY1 = $Y1; $LastY2 = $Y2; + + $X = $X + $XStep; + } + $Bounds = array_merge($BoundsA,$this->reversePlots($BoundsB)); + $this->drawPolygonChart($Bounds,array("R"=>$AreaR,"G"=>$AreaG,"B"=>$AreaB,"Alpha"=>$AreaAlpha)); + + for($i=0;$i<=count($BoundsA)-4;$i=$i+2) + { + $this->drawLine($BoundsA[$i],$BoundsA[$i+1],$BoundsA[$i+2],$BoundsA[$i+3],array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha,"Ticks"=>$LineTicks)); + $this->drawLine($BoundsB[$i],$BoundsB[$i+1],$BoundsB[$i+2],$BoundsB[$i+3],array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha,"Ticks"=>$LineTicks)); + } + } + else + { + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; + + $LastY = NULL; $LastX1 = NULL; $LastX2 = NULL; + $BoundsA = ""; $BoundsB = ""; + foreach($PosArrayA as $Key => $X1) + { + $X2 = $PosArrayB[$Key]; + + $BoundsA[] = $X1; $BoundsA[] = $Y; + $BoundsB[] = $X2; $BoundsB[] = $Y; + + $LastY = $Y; + $LastX1 = $X1; $LastX2 = $X2; + + $Y = $Y + $YStep; + } + $Bounds = array_merge($BoundsA,$this->reversePlots($BoundsB)); + $this->drawPolygonChart($Bounds,array("R"=>$AreaR,"G"=>$AreaG,"B"=>$AreaB,"Alpha"=>$AreaAlpha)); + + for($i=0;$i<=count($BoundsA)-4;$i=$i+2) + { + $this->drawLine($BoundsA[$i],$BoundsA[$i+1],$BoundsA[$i+2],$BoundsA[$i+3],array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha,"Ticks"=>$LineTicks)); + $this->drawLine($BoundsB[$i],$BoundsB[$i+1],$BoundsB[$i+2],$BoundsB[$i+3],array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha,"Ticks"=>$LineTicks)); + } + } + } + + /* Draw a step chart */ + function drawStepChart($Format=NULL) + { + $BreakVoid = isset($Format["BreakVoid"]) ? $Format["BreakVoid"] : FALSE; + $ReCenter = isset($Format["ReCenter"]) ? $Format["ReCenter"] : TRUE; + $VoidTicks = isset($Format["VoidTicks"]) ? $Format["VoidTicks"] : 4; + $BreakR = isset($Format["BreakR"]) ? $Format["BreakR"] : NULL; + $BreakG = isset($Format["BreakG"]) ? $Format["BreakG"] : NULL; + $BreakB = isset($Format["BreakB"]) ? $Format["BreakB"] : NULL; + $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] :FALSE; + $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; + $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; + $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + $ImageMapPlotSize = isset($Format["ImageMapPlotSize"]) ? $Format["ImageMapPlotSize"] : 5; + + $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; + + if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } + + if ( $BreakR == NULL ) + $BreakSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); + else + $BreakSettings = array("R"=>$BreakR,"G"=>$BreakG,"B"=>$BreakB,"Alpha"=>$Alpha,"Ticks"=>$VoidTicks,"Weight"=>$Weight); + + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight); + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $LastGoodY = NULL; $LastGoodX = NULL; $Init = FALSE; + foreach($PosArray as $Key => $Y) + { + if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) + { + if ( $Y <= $LastY ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } + $this->drawText($X,$Y-$Offset-$Weight,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); + } + + if ( $Y != VOID && $LastX != NULL && $LastY != NULL ) + { + $this->drawLine($LastX,$LastY,$X,$LastY,$Color); + $this->drawLine($X,$LastY,$X,$Y,$Color); + if ( $ReCenter && $X+$XStep < $this->GraphAreaX2 - $XMargin ) + { + $this->drawLine($X,$Y,$X+$XStep,$Y,$Color); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X-$ImageMapPlotSize).",".floor($Y-$ImageMapPlotSize).",".floor($X+$XStep+$ImageMapPlotSize).",".floor($Y+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + else + { if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastX-$ImageMapPlotSize).",".floor($LastY-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($LastY+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } } + } + + if ( $Y != VOID && $LastY == NULL && $LastGoodY != NULL && !$BreakVoid ) + { + if ( $ReCenter ) + { + $this->drawLine($LastGoodX+$XStep,$LastGoodY,$X,$LastGoodY,$BreakSettings); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastGoodX+$XStep-$ImageMapPlotSize).",".floor($LastGoodY-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($LastGoodY+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + else + { + $this->drawLine($LastGoodX,$LastGoodY,$X,$LastGoodY,$BreakSettings); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastGoodX-$ImageMapPlotSize).",".floor($LastGoodY-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($LastGoodY+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + + $this->drawLine($X,$LastGoodY,$X,$Y,$BreakSettings); + $LastGoodY = NULL; + } + elseif( !$BreakVoid && $LastGoodY == NULL && $Y != VOID ) + { + $this->drawLine($this->GraphAreaX1 + $XMargin,$Y,$X,$Y,$BreakSettings); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($this->GraphAreaX1+$XMargin-$ImageMapPlotSize).",".floor($Y-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($Y+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + + if ( $Y != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } + if ( $Y == VOID ) { $Y = NULL; } + + if ( !$Init && $ReCenter ) { $X = $X - $XStep/2; $Init = TRUE; } + $LastX = $X; $LastY = $Y; + if ( $LastX < $this->GraphAreaX1 + $XMargin ) { $LastX = $this->GraphAreaX1 + $XMargin; } + $X = $X + $XStep; + } + if ( $ReCenter ) + { + $this->drawLine($LastX,$LastY,$this->GraphAreaX2 - $XMargin,$LastY,$Color); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastX-$ImageMapPlotSize).",".floor($LastY-$ImageMapPlotSize).",".floor($this->GraphAreaX2-$XMargin+$ImageMapPlotSize).",".floor($LastY+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + } + else + { + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $LastGoodY = NULL; $LastGoodX = NULL; $Init = FALSE; + foreach($PosArray as $Key => $X) + { + if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) + { + if ( $X >= $LastX ) { $Align = TEXT_ALIGN_MIDDLELEFT; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_MIDDLERIGHT; $Offset = -$DisplayOffset; } + $this->drawText($X+$Offset+$Weight,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); + } + + if ( $X != VOID && $LastX != NULL && $LastY != NULL ) + { + $this->drawLine($LastX,$LastY,$LastX,$Y,$Color); + $this->drawLine($LastX,$Y,$X,$Y,$Color); + + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastX-$ImageMapPlotSize).",".floor($LastY-$ImageMapPlotSize).",".floor($LastX+$XStep+$ImageMapPlotSize).",".floor($Y+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + + if ( $X != VOID && $LastX == NULL && $LastGoodY != NULL && !$BreakVoid ) + { + $this->drawLine($LastGoodX,$LastGoodY,$LastGoodX,$LastGoodY+$YStep,$Color); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastGoodX-$ImageMapPlotSize).",".floor($LastGoodY-$ImageMapPlotSize).",".floor($LastGoodX+$ImageMapPlotSize).",".floor($LastGoodY+$YStep+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + $this->drawLine($LastGoodX,$LastGoodY+$YStep,$LastGoodX,$Y,$BreakSettings); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastGoodX-$ImageMapPlotSize).",".floor($LastGoodY+$YStep-$ImageMapPlotSize).",".floor($LastGoodX+$ImageMapPlotSize).",".floor($YStep+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + $this->drawLine($LastGoodX,$Y,$X,$Y,$BreakSettings); + $LastGoodY = NULL; + } + elseif ( $X != VOID && $LastGoodY == NULL && !$BreakVoid ) + { + $this->drawLine($X,$this->GraphAreaY1 + $XMargin,$X,$Y,$BreakSettings); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X-$ImageMapPlotSize).",".floor($this->GraphAreaY1+$XMargin-$ImageMapPlotSize).",".floor($X+$ImageMapPlotSize).",".floor($Y+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + + if ( $X != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } + if ( $X == VOID ) { $X = NULL; } + + if ( !$Init && $ReCenter ) { $Y = $Y - $YStep/2; $Init = TRUE; } + $LastX = $X; $LastY = $Y; + if ( $LastY < $this->GraphAreaY1 + $XMargin ) { $LastY = $this->GraphAreaY1 + $XMargin; } + $Y = $Y + $YStep; + } + if ( $ReCenter ) + { + $this->drawLine($LastX,$LastY,$LastX,$this->GraphAreaY2 - $XMargin,$Color); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($LastX-$ImageMapPlotSize).",".floor($LastY-$ImageMapPlotSize).",".floor($LastX+$ImageMapPlotSize).",".floor($this->GraphAreaY2-$XMargin+$ImageMapPlotSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + } + } + } + } + + /* Draw a step chart */ + function drawFilledStepChart($Format=NULL) + { + $ReCenter = isset($Format["ReCenter"]) ? $Format["ReCenter"] : TRUE; + $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] :FALSE; + $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $ForceTransparency = isset($Format["ForceTransparency"]) ? $Format["ForceTransparency"] : NULL; + $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; + $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; + $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; + $AroundZero = isset($Format["AroundZero"]) ? $Format["AroundZero"] : TRUE; + + $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; + + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + $Color = array("R"=>$R,"G"=>$G,"B"=>$B); + if ( $ForceTransparency != NULL ) { $Color["Alpha"] = $ForceTransparency; } else { $Color["Alpha"] = $Alpha; } + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } + if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } + + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; + + if ( !$AroundZero ) { $YZero = $this->GraphAreaY2-1; } + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $LastGoodY = NULL; $LastGoodX = NULL; $Points = ""; $Init = FALSE; + foreach($PosArray as $Key => $Y) + { + if ( $Y == VOID && $LastX != NULL && $LastY != NULL && $Points !="" ) + { + $Points[] = $LastX; $Points[] = $LastY; + $Points[] = $X; $Points[] = $LastY; + $Points[] = $X; $Points[] = $YZero; + $this->drawPolygon($Points,$Color); + $Points = ""; + } + + if ( $Y != VOID && $LastX != NULL && $LastY != NULL ) + { + if ( $Points == "") { $Points[] = $LastX; $Points[] = $YZero; } + $Points[] = $LastX; $Points[] = $LastY; + $Points[] = $X; $Points[] = $LastY; + $Points[] = $X; $Points[] = $Y; + } + + if ( $Y != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } + if ( $Y == VOID ) { $Y = NULL; } + + if ( !$Init && $ReCenter ) { $X = $X - $XStep/2; $Init = TRUE; } + $LastX = $X; $LastY = $Y; + if ( $LastX < $this->GraphAreaX1 + $XMargin ) { $LastX = $this->GraphAreaX1 + $XMargin; } + $X = $X + $XStep; + } + + if ( $ReCenter ) + { + $Points[] = $LastX+$XStep/2; $Points[] = $LastY; + $Points[] = $LastX+$XStep/2; $Points[] = $YZero; + } + else + { $Points[] = $LastX; $Points[] = $YZero; } + + $this->drawPolygon($Points,$Color); + } + else + { + if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } + if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } + + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $LastGoodY = NULL; $LastGoodX = NULL; $Points = ""; + foreach($PosArray as $Key => $X) + { + if ( $X == VOID && $LastX != NULL && $LastY != NULL && $Points !="" ) + { + $Points[] = $LastX; $Points[] = $LastY; + $Points[] = $LastX; $Points[] = $Y; + $Points[] = $YZero; $Points[] = $Y; + $this->drawPolygon($Points,$Color); + $Points = ""; + } + + if ( $X != VOID && $LastX != NULL && $LastY != NULL ) + { + if ( $Points == "") { $Points[] = $YZero; $Points[] = $LastY; } + $Points[] = $LastX; $Points[] = $LastY; + $Points[] = $LastX; $Points[] = $Y; + $Points[] = $X; $Points[] = $Y; + } + + if ( $X != VOID ) { $LastGoodY = $Y; $LastGoodX = $X; } + if ( $X == VOID ) { $X = NULL; } + + if ( $LastX == NULL && $ReCenter ) { $Y = $Y - $YStep/2; } + $LastX = $X; $LastY = $Y; + if ( $LastY < $this->GraphAreaY1 + $XMargin ) { $LastY = $this->GraphAreaY1 + $XMargin; } + $Y = $Y + $YStep; + } + + if ( $ReCenter ) + { + $Points[] = $LastX; $Points[] = $LastY+$YStep/2; + $Points[] = $YZero; $Points[] = $LastY+$YStep/2; + } + else + { $Points[] = $YZero; $Points[] = $LastY; } + + $this->drawPolygon($Points,$Color); + } + } + } + } + + /* Draw an area chart */ + function drawAreaChart($Format=NULL) + { + $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; + $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; + $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; + $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; + $ForceTransparency = isset($Format["ForceTransparency"]) ? $Format["ForceTransparency"] : 25; + $AroundZero = isset($Format["AroundZero"]) ? $Format["AroundZero"] : TRUE; + $Threshold = isset($Format["Threshold"]) ? $Format["Threshold"] : NULL; + + $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); + + if ( $Threshold != NULL ) + { + foreach($Threshold as $Key => $Params) + { + $Threshold[$Key]["MinX"] = $this->scaleComputeY($Params["Min"],array("AxisID"=>$Serie["Axis"])); + $Threshold[$Key]["MaxX"] = $this->scaleComputeY($Params["Max"],array("AxisID"=>$Serie["Axis"])); + } + } + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } + + $Areas = ""; $AreaID = 0; + $Areas[$AreaID][] = $this->GraphAreaX1 + $XMargin; + if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaY2-1; } + + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; $LastX = NULL; $LastY = NULL; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + foreach($PosArray as $Key => $Y) + { + if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) + { + if ( $Serie["Data"][$Key] > 0 ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } + $this->drawText($X,$Y-$Offset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); + } + + if ( $Y == VOID && isset($Areas[$AreaID]) ) + { + if($LastX == NULL) + { $Areas[$AreaID][] = $X; } + else + { $Areas[$AreaID][] = $LastX; } + + if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaY2-1; } + $AreaID++; + } + elseif ($Y != VOID) + { + if ( !isset($Areas[$AreaID]) ) + { + $Areas[$AreaID][] = $X; + if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaY2-1; } + } + + $Areas[$AreaID][] = $X; + $Areas[$AreaID][] = $Y; + } + + $LastX = $X; + $X = $X + $XStep; + } + $Areas[$AreaID][] = $LastX; + if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaY2-1; } + + /* Handle shadows in the areas */ + if ( $this->Shadow ) + { + $ShadowArea = ""; + foreach($Areas as $Key => $Points) + { + $ShadowArea[$Key] = ""; + foreach($Points as $Key2 => $Value) + { + if ( $Key2 % 2 == 0 ) + { $ShadowArea[$Key][] = $Value + $this->ShadowX; } + else + { $ShadowArea[$Key][] = $Value + $this->ShadowY; } + } + } + + foreach($ShadowArea as $Key => $Points) + $this->drawPolygonChart($Points,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); + } + + $Alpha = $ForceTransparency != NULL ? $ForceTransparency : $Alpha; + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Threshold"=>$Threshold); + + foreach($Areas as $Key => $Points) + $this->drawPolygonChart($Points,$Color); + } + else + { + if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } + if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } + + $Areas = ""; $AreaID = 0; + if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaX1+1; } + $Areas[$AreaID][] = $this->GraphAreaY1 + $XMargin; + + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; $LastX = NULL; $LastY = NULL; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + foreach($PosArray as $Key => $X) + { + if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) + { + if ( $Serie["Data"][$Key] > 0 ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } + $this->drawText($X+$Offset,$Y,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("Angle"=>270,"R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align)); + } + + if ( $X == VOID && isset($Areas[$AreaID]) ) + { + if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaX1+1; } + + if($LastY == NULL) + { $Areas[$AreaID][] = $Y; } + else + { $Areas[$AreaID][] = $LastY; } + + $AreaID++; + } + elseif ($X != VOID) + { + if ( !isset($Areas[$AreaID]) ) + { + if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaX1+1; } + $Areas[$AreaID][] = $Y; + } + + $Areas[$AreaID][] = $X; + $Areas[$AreaID][] = $Y; + } + + $LastX = $X; $LastY = $Y; + $Y = $Y + $YStep; + } + if ( $AroundZero ) { $Areas[$AreaID][] = $YZero; } else { $Areas[$AreaID][] = $this->GraphAreaX1+1; } + $Areas[$AreaID][] = $LastY; + + /* Handle shadows in the areas */ + if ( $this->Shadow ) + { + $ShadowArea = ""; + foreach($Areas as $Key => $Points) + { + $ShadowArea[$Key] = ""; + foreach($Points as $Key2 => $Value) + { + if ( $Key2 % 2 == 0 ) + { $ShadowArea[$Key][] = $Value + $this->ShadowX; } + else + { $ShadowArea[$Key][] = $Value + $this->ShadowY; } + } + } + + foreach($ShadowArea as $Key => $Points) + $this->drawPolygonChart($Points,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); + } + + $Alpha = $ForceTransparency != NULL ? $ForceTransparency : $Alpha; + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Threshold"=>$Threshold); + + foreach($Areas as $Key => $Points) + $this->drawPolygonChart($Points,$Color); + } + } + } + } + + + /* Draw a bar chart */ + function drawBarChart($Format=NULL) + { + $Floating0Serie = isset($Format["Floating0Serie"]) ? $Format["Floating0Serie"] : NULL; + $Floating0Value = isset($Format["Floating0Value"]) ? $Format["Floating0Value"] : NULL; + $Draw0Line = isset($Format["Draw0Line"]) ? $Format["Draw0Line"] : FALSE; + $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; + $DisplayOrientation = isset($Format["DisplayOrientation"]) ? $Format["DisplayOrientation"] : ORIENTATION_HORIZONTAL; + $DisplayOffset = isset($Format["DisplayOffset"]) ? $Format["DisplayOffset"] : 2; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $DisplayFont = isset($Format["DisplaySize"]) ? $Format["DisplaySize"] : $this->FontName; + $DisplaySize = isset($Format["DisplaySize"]) ? $Format["DisplaySize"] : $this->FontSize; + $DisplayPos = isset($Format["DisplayPos"]) ? $Format["DisplayPos"] : LABEL_POS_OUTSIDE; + $DisplayShadow = isset($Format["DisplayShadow"]) ? $Format["DisplayShadow"] : TRUE; + $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; + $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; + $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; + $AroundZero = isset($Format["AroundZero"]) ? $Format["AroundZero"] : TRUE; + $Interleave = isset($Format["Interleave"]) ? $Format["Interleave"] : .5; + $Rounded = isset($Format["Rounded"]) ? $Format["Rounded"] : FALSE; + $RoundRadius = isset($Format["RoundRadius"]) ? $Format["RoundRadius"] : 4; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; + $Gradient = isset($Format["Gradient"]) ? $Format["Gradient"] : FALSE; + $GradientMode = isset($Format["GradientMode"]) ? $Format["GradientMode"] : GRADIENT_SIMPLE; + $GradientAlpha = isset($Format["GradientAlpha"]) ? $Format["GradientAlpha"] : 20; + $GradientStartR = isset($Format["GradientStartR"]) ? $Format["GradientStartR"] : 255; + $GradientStartG = isset($Format["GradientStartG"]) ? $Format["GradientStartG"] : 255; + $GradientStartB = isset($Format["GradientStartB"]) ? $Format["GradientStartB"] : 255; + $GradientEndR = isset($Format["GradientEndR"]) ? $Format["GradientEndR"] : 0; + $GradientEndG = isset($Format["GradientEndG"]) ? $Format["GradientEndG"] : 0; + $GradientEndB = isset($Format["GradientEndB"]) ? $Format["GradientEndB"] : 0; + $TxtMargin = isset($Format["TxtMargin"]) ? $Format["TxtMargin"] : 6; + $OverrideColors = isset($Format["OverrideColors"]) ? $Format["OverrideColors"] : NULL; + $OverrideSurrounding = isset($Format["OverrideSurrounding"]) ? $Format["OverrideSurrounding"] : 30; + $InnerSurrounding = isset($Format["InnerSurrounding"]) ? $Format["InnerSurrounding"] : NULL; + $InnerBorderR = isset($Format["InnerBorderR"]) ? $Format["InnerBorderR"] : -1; + $InnerBorderG = isset($Format["InnerBorderG"]) ? $Format["InnerBorderG"] : -1; + $InnerBorderB = isset($Format["InnerBorderB"]) ? $Format["InnerBorderB"] : -1; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + + $this->LastChartLayout = CHART_LAST_LAYOUT_REGULAR; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + if ( $OverrideColors != NULL ) + { + $OverrideColors = $this->validatePalette($OverrideColors,$OverrideSurrounding); + $this->DataSet->saveExtendedData("Palette",$OverrideColors); + } + + $RestoreShadow = $this->Shadow; + + $SeriesCount = $this->countDrawableSeries(); + $CurrentSerie = 0; + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = $R; $DisplayG = $G; $DisplayB = $B; } + if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } + if ( $InnerSurrounding != NULL ) { $InnerBorderR = $R+$InnerSurrounding; $InnerBorderG = $G+$InnerSurrounding; $InnerBorderB = $B+$InnerSurrounding; } + if ( $InnerBorderR == -1 ) { $InnerColor = NULL; } else { $InnerColor = array("R"=>$InnerBorderR,"G"=>$InnerBorderG,"B"=>$InnerBorderB); } + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB); + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + + if ( $Floating0Value != NULL ) + { $YZero = $this->scaleComputeY($Floating0Value,array("AxisID"=>$Serie["Axis"])); } + else + { $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); } + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } + if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } + + if ( $XDivs == 0 ) { $XStep = 0; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; + + if ( $AroundZero ) { $Y1 = $YZero; } else { $Y1 = $this->GraphAreaY2-1; } + if ( $XDivs == 0 ) { $XSize = ($this->GraphAreaX2-$this->GraphAreaX1)/($SeriesCount+$Interleave); } else { $XSize = ($XStep / ($SeriesCount+$Interleave) ); } + + $XOffset = -($XSize*$SeriesCount)/2 + $CurrentSerie * $XSize; + if ( $X + $XOffset <= $this->GraphAreaX1 ) { $XOffset = $this->GraphAreaX1 - $X + 1 ; } + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = $XOffset + $XSize / 2; + + if ( $Rounded || $BorderR != -1) { $XSpace = 1; } else { $XSpace = 0; } + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + + $ID = 0; + foreach($PosArray as $Key => $Y2) + { + if ( $Floating0Serie != NULL ) + { + if ( isset($Data["Series"][$Floating0Serie]["Data"][$Key]) ) + { $Value = $Data["Series"][$Floating0Serie]["Data"][$Key]; } + else + { $Value = 0; } + + $YZero = $this->scaleComputeY($Value,array("AxisID"=>$Serie["Axis"])); + if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } + if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } + + if ( $AroundZero ) { $Y1 = $YZero; } else { $Y1 = $this->GraphAreaY2-1; } + } + + if ( $OverrideColors != NULL ) + { if ( isset($OverrideColors[$ID]) ) { $Color = array("R"=>$OverrideColors[$ID]["R"],"G"=>$OverrideColors[$ID]["G"],"B"=>$OverrideColors[$ID]["B"],"Alpha"=>$OverrideColors[$ID]["Alpha"],"BorderR"=>$OverrideColors[$ID]["BorderR"],"BorderG"=>$OverrideColors[$ID]["BorderG"],"BorderB"=>$OverrideColors[$ID]["BorderB"]); } else { $Color = $this->getRandomColor(); } } + + if ( $Y2 != VOID ) + { + $BarHeight = $Y1 - $Y2; + + if ( $Serie["Data"][$Key] == 0 ) + { + $this->drawLine($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y1,$Color); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X+$XOffset+$XSpace).",".floor($Y1-1).",".floor($X+$XOffset+$XSize-$XSpace).",".floor($Y1+1),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + else + { + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X+$XOffset+$XSpace).",".floor($Y1).",".floor($X+$XOffset+$XSize-$XSpace).",".floor($Y2),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $Rounded ) + $this->drawRoundedFilledRectangle($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y2,$RoundRadius,$Color); + else + { + $this->drawFilledRectangle($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y2,$Color); + + if ( $InnerColor != NULL ) { $this->drawRectangle($X+$XOffset+$XSpace+1,min($Y1,$Y2)+1,$X+$XOffset+$XSize-$XSpace-1,max($Y1,$Y2)-1,$InnerColor); } + + if ( $Gradient ) + { + $this->Shadow = FALSE; + + if ( $GradientMode == GRADIENT_SIMPLE ) + { + if ( $Serie["Data"][$Key] >= 0 ) + $GradienColor = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); + else + $GradienColor = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); + + $this->drawGradientArea($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y2,DIRECTION_VERTICAL,$GradienColor); + } + elseif ( $GradientMode == GRADIENT_EFFECT_CAN ) + { + $GradienColor1 = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); + $GradienColor2 = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); + $XSpan = floor($XSize / 3); + + $this->drawGradientArea($X+$XOffset+$XSpace,$Y1,$X+$XOffset+$XSpan-$XSpace,$Y2,DIRECTION_HORIZONTAL,$GradienColor1); + $this->drawGradientArea($X+$XOffset+$XSpan+$XSpace,$Y1,$X+$XOffset+$XSize-$XSpace,$Y2,DIRECTION_HORIZONTAL,$GradienColor2); + } + $this->Shadow = $RestoreShadow; + } + } + + if ( $Draw0Line ) + { + $Line0Color = array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20); + + if ( abs($Y1 - $Y2) > 3 ) { $Line0Width = 3; } else { $Line0Width = 1; } + if ( $Y1 - $Y2 < 0 ) { $Line0Width = -$Line0Width; } + + $this->drawFilledRectangle($X+$XOffset+$XSpace,floor($Y1),$X+$XOffset+$XSize-$XSpace,floor($Y1)-$Line0Width,$Line0Color); + $this->drawLine($X+$XOffset+$XSpace,floor($Y1),$X+$XOffset+$XSize-$XSpace,floor($Y1),$Line0Color); + } + } + + if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) + { + if ( $DisplayShadow ) { $this->Shadow = TRUE; } + + $Caption = $this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit); + $TxtPos = $this->getTextBox(0,0,$DisplayFont,$DisplaySize,90,$Caption); + $TxtHeight = $TxtPos[0]["Y"] - $TxtPos[1]["Y"] + $TxtMargin; + + if ( $DisplayPos == LABEL_POS_INSIDE && abs($TxtHeight) < abs($BarHeight) ) + { + $CenterX = (($X+$XOffset+$XSize-$XSpace)-($X+$XOffset+$XSpace))/2 + $X+$XOffset+$XSpace; + $CenterY = ($Y2-$Y1)/2 + $Y1; + + $this->drawText($CenterX,$CenterY,$Caption,array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"Angle"=>90)); + } + else + { + if ( $Serie["Data"][$Key] >= 0 ) { $Align = TEXT_ALIGN_BOTTOMMIDDLE; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_TOPMIDDLE; $Offset = -$DisplayOffset; } + $this->drawText($X+$XOffset+$XSize/2,$Y2-$Offset,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align,"FontSize"=>$DisplaySize)); + } + + $this->Shadow = $RestoreShadow; + } + } + + $X = $X + $XStep; + $ID++; + } + } + else + { + if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } + if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } + + if ( $XDivs == 0 ) { $YStep = 0; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + + $Y = $this->GraphAreaY1 + $XMargin; + + if ( $AroundZero ) { $X1 = $YZero; } else { $X1 = $this->GraphAreaX1+1; } + if ( $XDivs == 0 ) { $YSize = ($this->GraphAreaY2-$this->GraphAreaY1)/($SeriesCount+$Interleave); } else { $YSize = ($YStep / ($SeriesCount+$Interleave) ); } + + $YOffset = -($YSize*$SeriesCount)/2 + $CurrentSerie * $YSize; + if ( $Y + $YOffset <= $this->GraphAreaY1 ) { $YOffset = $this->GraphAreaY1 - $Y + 1 ; } + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = $YOffset + $YSize / 2; + + if ( $Rounded || $BorderR != -1 ) { $YSpace = 1; } else { $YSpace = 0; } + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + + $ID = 0 ; + foreach($PosArray as $Key => $X2) + { + if ( $Floating0Serie != NULL ) + { + if ( isset($Data["Series"][$Floating0Serie]["Data"][$Key]) ) + $Value = $Data["Series"][$Floating0Serie]["Data"][$Key]; + else { $Value = 0; } + + $YZero = $this->scaleComputeY($Value,array("AxisID"=>$Serie["Axis"])); + if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } + if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } + if ( $AroundZero ) { $X1 = $YZero; } else { $X1 = $this->GraphAreaX1+1; } + } + + if ( $OverrideColors != NULL ) + { if ( isset($OverrideColors[$ID]) ) { $Color = array("R"=>$OverrideColors[$ID]["R"],"G"=>$OverrideColors[$ID]["G"],"B"=>$OverrideColors[$ID]["B"],"Alpha"=>$OverrideColors[$ID]["Alpha"],"BorderR"=>$OverrideColors[$ID]["BorderR"],"BorderG"=>$OverrideColors[$ID]["BorderG"],"BorderB"=>$OverrideColors[$ID]["BorderB"]); } else { $Color = $this->getRandomColor(); } } + + if ( $X2 != VOID ) + { + $BarWidth = $X2 - $X1; + + if ( $Serie["Data"][$Key] == 0 ) + { + $this->drawLine($X1,$Y+$YOffset+$YSpace,$X1,$Y+$YOffset+$YSize-$YSpace,$Color); + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X1-1).",".floor($Y+$YOffset+$YSpace).",".floor($X1+1).",".floor($Y+$YOffset+$YSize-$YSpace),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + } + else + { + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X1).",".floor($Y+$YOffset+$YSpace).",".floor($X2).",".floor($Y+$YOffset+$YSize-$YSpace),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $Rounded ) + $this->drawRoundedFilledRectangle($X1+1,$Y+$YOffset+$YSpace,$X2,$Y+$YOffset+$YSize-$YSpace,$RoundRadius,$Color); + else + { + $this->drawFilledRectangle($X1,$Y+$YOffset+$YSpace,$X2,$Y+$YOffset+$YSize-$YSpace,$Color); + + if ( $InnerColor != NULL ) { $this->drawRectangle(min($X1,$X2)+1,$Y+$YOffset+$YSpace+1,max($X1,$X2)-1,$Y+$YOffset+$YSize-$YSpace-1,$InnerColor); } + + if ( $Gradient ) + { + $this->Shadow = FALSE; + + if ( $GradientMode == GRADIENT_SIMPLE ) + { + if ( $Serie["Data"][$Key] >= 0 ) + $GradienColor = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); + else + $GradienColor = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); + + $this->drawGradientArea($X1,$Y+$YOffset+$YSpace,$X2,$Y+$YOffset+$YSize-$YSpace,DIRECTION_HORIZONTAL,$GradienColor); + } + elseif ( $GradientMode == GRADIENT_EFFECT_CAN ) + { + $GradienColor1 = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); + $GradienColor2 = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); + $YSpan = floor($YSize / 3); + + $this->drawGradientArea($X1,$Y+$YOffset+$YSpace,$X2,$Y+$YOffset+$YSpan-$YSpace,DIRECTION_VERTICAL,$GradienColor1); + $this->drawGradientArea($X1,$Y+$YOffset+$YSpan,$X2,$Y+$YOffset+$YSize-$YSpace,DIRECTION_VERTICAL,$GradienColor2); + } + $this->Shadow = $RestoreShadow; + } + } + + if ( $Draw0Line ) + { + $Line0Color = array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20); + + if ( abs($X1 - $X2) > 3 ) { $Line0Width = 3; } else { $Line0Width = 1; } + if ( $X2 - $X1 < 0 ) { $Line0Width = -$Line0Width; } + + $this->drawFilledRectangle(floor($X1),$Y+$YOffset+$YSpace,floor($X1)+$Line0Width,$Y+$YOffset+$YSize-$YSpace,$Line0Color); + $this->drawLine(floor($X1),$Y+$YOffset+$YSpace,floor($X1),$Y+$YOffset+$YSize-$YSpace,$Line0Color); + } + } + + if ( $DisplayValues && $Serie["Data"][$Key] != VOID ) + { + if ( $DisplayShadow ) { $this->Shadow = TRUE; } + + $Caption = $this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit); + $TxtPos = $this->getTextBox(0,0,$DisplayFont,$DisplaySize,0,$Caption); + $TxtWidth = $TxtPos[1]["X"] - $TxtPos[0]["X"] + $TxtMargin; + + if ( $DisplayPos == LABEL_POS_INSIDE && abs($TxtWidth) < abs($BarWidth) ) + { + $CenterX = ($X2-$X1)/2 + $X1; + $CenterY = (($Y+$YOffset+$YSize-$YSpace)-($Y+$YOffset+$YSpace))/2 + ($Y+$YOffset+$YSpace); + + $this->drawText($CenterX,$CenterY,$Caption,array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize)); + } + else + { + if ( $Serie["Data"][$Key] >= 0 ) { $Align = TEXT_ALIGN_MIDDLELEFT; $Offset = $DisplayOffset; } else { $Align = TEXT_ALIGN_MIDDLERIGHT; $Offset = -$DisplayOffset; } + $this->drawText($X2+$Offset,$Y+$YOffset+$YSize/2,$Caption,array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>$Align,"FontSize"=>$DisplaySize)); + } + + $this->Shadow = $RestoreShadow; + } + } + $Y = $Y + $YStep; + $ID++; + } + } + $CurrentSerie++; + } + } + } + + /* Draw a bar chart */ + function drawStackedBarChart($Format=NULL) + { + $DisplayValues = isset($Format["DisplayValues"]) ? $Format["DisplayValues"] : FALSE; + $DisplayOrientation = isset($Format["DisplayOrientation"]) ? $Format["DisplayOrientation"] : ORIENTATION_AUTO; + $DisplayRound = isset($Format["DisplayRound"]) ? $Format["DisplayRound"] : 0; + $DisplayColor = isset($Format["DisplayColor"]) ? $Format["DisplayColor"] : DISPLAY_MANUAL; + $DisplayFont = isset($Format["DisplayFont"]) ? $Format["DisplayFont"] : $this->FontName; + $DisplaySize = isset($Format["DisplaySize"]) ? $Format["DisplaySize"] : $this->FontSize; + $DisplayR = isset($Format["DisplayR"]) ? $Format["DisplayR"] : 0; + $DisplayG = isset($Format["DisplayG"]) ? $Format["DisplayG"] : 0; + $DisplayB = isset($Format["DisplayB"]) ? $Format["DisplayB"] : 0; + $Interleave = isset($Format["Interleave"]) ? $Format["Interleave"] : .5; + $Rounded = isset($Format["Rounded"]) ? $Format["Rounded"] : FALSE; + $RoundRadius = isset($Format["RoundRadius"]) ? $Format["RoundRadius"] : 4; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : -1; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : -1; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : -1; + $Gradient = isset($Format["Gradient"]) ? $Format["Gradient"] : FALSE; + $GradientMode = isset($Format["GradientMode"]) ? $Format["GradientMode"] : GRADIENT_SIMPLE; + $GradientAlpha = isset($Format["GradientAlpha"]) ? $Format["GradientAlpha"] : 20; + $GradientStartR = isset($Format["GradientStartR"]) ? $Format["GradientStartR"] : 255; + $GradientStartG = isset($Format["GradientStartG"]) ? $Format["GradientStartG"] : 255; + $GradientStartB = isset($Format["GradientStartB"]) ? $Format["GradientStartB"] : 255; + $GradientEndR = isset($Format["GradientEndR"]) ? $Format["GradientEndR"] : 0; + $GradientEndG = isset($Format["GradientEndG"]) ? $Format["GradientEndG"] : 0; + $GradientEndB = isset($Format["GradientEndB"]) ? $Format["GradientEndB"] : 0; + $InnerSurrounding = isset($Format["InnerSurrounding"]) ? $Format["InnerSurrounding"] : NULL; + $InnerBorderR = isset($Format["InnerBorderR"]) ? $Format["InnerBorderR"] : -1; + $InnerBorderG = isset($Format["InnerBorderG"]) ? $Format["InnerBorderG"] : -1; + $InnerBorderB = isset($Format["InnerBorderB"]) ? $Format["InnerBorderB"] : -1; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + $FontFactor = isset($Format["FontFactor"]) ? $Format["FontFactor"] : 8; + + $this->LastChartLayout = CHART_LAST_LAYOUT_STACKED; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + $RestoreShadow = $this->Shadow; + + $LastX = ""; $LastY = ""; + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; + if ( $DisplayColor == DISPLAY_AUTO ) { $DisplayR = 255; $DisplayG = 255; $DisplayB = 255; } + if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } + if ( $InnerSurrounding != NULL ) { $InnerBorderR = $R+$InnerSurrounding; $InnerBorderG = $G+$InnerSurrounding; $InnerBorderB = $B+$InnerSurrounding; } + if ( $InnerBorderR == -1 ) { $InnerColor = NULL; } else { $InnerColor = array("R"=>$InnerBorderR,"G"=>$InnerBorderG,"B"=>$InnerBorderB); } + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + if (isset($Serie["Description"])) { $SerieDescription = $Serie["Description"]; } else { $SerieDescription = $SerieName; } + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"]),TRUE); + $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; + + $Color = array("TransCorner"=>TRUE,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB); + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } + if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } + + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; + + $XSize = ($XStep / (1+$Interleave) ); + $XOffset = -($XSize/2); + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + foreach($PosArray as $Key => $Height) + { + if ( $Height != VOID && $Serie["Data"][$Key] != 0 ) + { + if ( $Serie["Data"][$Key] > 0 ) { $Pos = "+"; } else { $Pos = "-"; } + + if ( !isset($LastY[$Key] ) ) { $LastY[$Key] = ""; } + if ( !isset($LastY[$Key][$Pos] ) ) { $LastY[$Key][$Pos] = $YZero; } + + $Y1 = $LastY[$Key][$Pos]; + $Y2 = $Y1 - $Height; + + if ( ($Rounded || $BorderR != -1) && ($Pos == "+" && $Y1 != $YZero) ) { $YSpaceUp = 1; } else { $YSpaceUp = 0; } + if ( ($Rounded || $BorderR != -1) && ($Pos == "-" && $Y1 != $YZero) ) { $YSpaceDown = 1; } else { $YSpaceDown = 0; } + + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X+$XOffset).",".floor($Y1-$YSpaceUp+$YSpaceDown).",".floor($X+$XOffset+$XSize).",".floor($Y2),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $Rounded ) + $this->drawRoundedFilledRectangle($X+$XOffset,$Y1-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSize,$Y2,$RoundRadius,$Color); + else + { + $this->drawFilledRectangle($X+$XOffset,$Y1-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSize,$Y2,$Color); + + if ( $InnerColor != NULL ) { $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; $this->drawRectangle(min($X+$XOffset+1,$X+$XOffset+$XSize),min($Y1-$YSpaceUp+$YSpaceDown,$Y2)+1,max($X+$XOffset+1,$X+$XOffset+$XSize)-1,max($Y1-$YSpaceUp+$YSpaceDown,$Y2)-1,$InnerColor); $this->Shadow = $RestoreShadow;} + + if ( $Gradient ) + { + $this->Shadow = FALSE; + + if ( $GradientMode == GRADIENT_SIMPLE ) + { + $GradientColor = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); + $this->drawGradientArea($X+$XOffset,$Y1-1-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSize,$Y2+1,DIRECTION_VERTICAL,$GradientColor); + } + elseif ( $GradientMode == GRADIENT_EFFECT_CAN ) + { + $GradientColor1 = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); + $GradientColor2 = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); + $XSpan = floor($XSize / 3); + + $this->drawGradientArea($X+$XOffset-.5,$Y1-.5-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSpan,$Y2+.5,DIRECTION_HORIZONTAL,$GradientColor1); + $this->drawGradientArea($X+$XSpan+$XOffset-.5,$Y1-.5-$YSpaceUp+$YSpaceDown,$X+$XOffset+$XSize,$Y2+.5,DIRECTION_HORIZONTAL,$GradientColor2); + } + $this->Shadow = $RestoreShadow; + } + } + + if ( $DisplayValues ) + { + $BarHeight = abs($Y2-$Y1)-2; + $BarWidth = $XSize+($XOffset/2)-$FontFactor; + + $Caption = $this->scaleFormat(round($Serie["Data"][$Key],$DisplayRound),$Mode,$Format,$Unit); + $TxtPos = $this->getTextBox(0,0,$DisplayFont,$DisplaySize,0,$Caption); + $TxtHeight = abs($TxtPos[2]["Y"] - $TxtPos[0]["Y"]); + $TxtWidth = abs($TxtPos[1]["X"] - $TxtPos[0]["X"]); + + $XCenter = ( ($X+$XOffset+$XSize) - ($X+$XOffset) ) / 2 + $X+$XOffset; + $YCenter = ( ($Y2) - ($Y1-$YSpaceUp+$YSpaceDown) ) / 2 + $Y1-$YSpaceUp+$YSpaceDown; + + $Done = FALSE; + if ( $DisplayOrientation == ORIENTATION_HORIZONTAL || $DisplayOrientation == ORIENTATION_AUTO ) + { + if ( $TxtHeight < $BarHeight && $TxtWidth < $BarWidth ) + { + $this->drawText($XCenter,$YCenter,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"FontName"=>$DisplayFont)); + $Done = TRUE; + } + } + + if ( $DisplayOrientation == ORIENTATION_VERTICAL || ( $DisplayOrientation == ORIENTATION_AUTO && !$Done) ) + { + if ( $TxtHeight < $BarWidth && $TxtWidth < $BarHeight ) + $this->drawText($XCenter,$YCenter,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Angle"=>90,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"FontName"=>$DisplayFont)); + } + } + + $LastY[$Key][$Pos] = $Y2; + } + + $X = $X + $XStep; + } + } + else + { + if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } + if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } + + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; + + $YSize = $YStep / (1+$Interleave); + $YOffset = -($YSize/2); + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + foreach($PosArray as $Key => $Width) + { + if ( $Width != VOID && $Serie["Data"][$Key] != 0 ) + { + if ( $Serie["Data"][$Key] > 0 ) { $Pos = "+"; } else { $Pos = "-"; } + + if ( !isset($LastX[$Key] ) ) { $LastX[$Key] = ""; } + if ( !isset($LastX[$Key][$Pos] ) ) { $LastX[$Key][$Pos] = $YZero; } + + $X1 = $LastX[$Key][$Pos]; + $X2 = $X1 + $Width; + + if ( ($Rounded || $BorderR != -1) && ($Pos == "+" && $X1 != $YZero) ) { $XSpaceLeft = 2; } else { $XSpaceLeft = 0; } + if ( ($Rounded || $BorderR != -1) && ($Pos == "-" && $X1 != $YZero) ) { $XSpaceRight = 2; } else { $XSpaceRight = 0; } + + if ( $RecordImageMap ) { $this->addToImageMap("RECT",floor($X1+$XSpaceLeft).",".floor($Y+$YOffset).",".floor($X2-$XSpaceRight).",".floor($Y+$YOffset+$YSize),$this->toHTMLColor($R,$G,$B),$SerieDescription,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit)); } + + if ( $Rounded ) + $this->drawRoundedFilledRectangle($X1+$XSpaceLeft,$Y+$YOffset,$X2-$XSpaceRight,$Y+$YOffset+$YSize,$RoundRadius,$Color); + else + { + $this->drawFilledRectangle($X1+$XSpaceLeft,$Y+$YOffset,$X2-$XSpaceRight,$Y+$YOffset+$YSize,$Color); + + if ( $InnerColor != NULL ) { $RestoreShadow = $this->Shadow; $this->Shadow = FALSE; $this->drawRectangle(min($X1+$XSpaceLeft,$X2-$XSpaceRight)+1,min($Y+$YOffset,$Y+$YOffset+$YSize)+1,max($X1+$XSpaceLeft,$X2-$XSpaceRight)-1,max($Y+$YOffset,$Y+$YOffset+$YSize)-1,$InnerColor); $this->Shadow = $RestoreShadow;} + + if ( $Gradient ) + { + $this->Shadow = FALSE; + + if ( $GradientMode == GRADIENT_SIMPLE ) + { + $GradientColor = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); + $this->drawGradientArea($X1+$XSpaceLeft,$Y+$YOffset,$X2-$XSpaceRight,$Y+$YOffset+$YSize,DIRECTION_HORIZONTAL,$GradientColor); + } + elseif ( $GradientMode == GRADIENT_EFFECT_CAN ) + { + $GradientColor1 = array("StartR"=>$GradientEndR,"StartG"=>$GradientEndG,"StartB"=>$GradientEndB,"EndR"=>$GradientStartR,"EndG"=>$GradientStartG,"EndB"=>$GradientStartB,"Alpha"=>$GradientAlpha); + $GradientColor2 = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB,"Alpha"=>$GradientAlpha); + $YSpan = floor($YSize / 3); + + $this->drawGradientArea($X1+$XSpaceLeft,$Y+$YOffset,$X2-$XSpaceRight,$Y+$YOffset+$YSpan,DIRECTION_VERTICAL,$GradientColor1); + $this->drawGradientArea($X1+$XSpaceLeft,$Y+$YOffset+$YSpan,$X2-$XSpaceRight,$Y+$YOffset+$YSize,DIRECTION_VERTICAL,$GradientColor2); + } + $this->Shadow = $RestoreShadow; + } + } + + if ( $DisplayValues ) + { + $BarWidth = abs($X2-$X1)-$FontFactor; + $BarHeight = $YSize+($YOffset/2)-$FontFactor/2; + $Caption = $this->scaleFormat(round($Serie["Data"][$Key],$DisplayRound),$Mode,$Format,$Unit); + $TxtPos = $this->getTextBox(0,0,$DisplayFont,$DisplaySize,0,$Caption); + $TxtHeight = abs($TxtPos[2]["Y"] - $TxtPos[0]["Y"]); + $TxtWidth = abs($TxtPos[1]["X"] - $TxtPos[0]["X"]); + + $XCenter = ( $X2 - $X1 ) / 2 + $X1; + $YCenter = ( ($Y+$YOffset+$YSize) - ($Y+$YOffset) ) / 2 + $Y+$YOffset; + + $Done = FALSE; + if ( $DisplayOrientation == ORIENTATION_HORIZONTAL || $DisplayOrientation == ORIENTATION_AUTO ) + { + if ( $TxtHeight < $BarHeight && $TxtWidth < $BarWidth ) + { + $this->drawText($XCenter,$YCenter,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"FontName"=>$DisplayFont)); + $Done = TRUE; + } + } + + if ( $DisplayOrientation == ORIENTATION_VERTICAL || ( $DisplayOrientation == ORIENTATION_AUTO && !$Done) ) + { + if ( $TxtHeight < $BarWidth && $TxtWidth < $BarHeight ) + $this->drawText($XCenter,$YCenter,$this->scaleFormat($Serie["Data"][$Key],$Mode,$Format,$Unit),array("R"=>$DisplayR,"G"=>$DisplayG,"B"=>$DisplayB,"Angle"=>90,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontSize"=>$DisplaySize,"FontName"=>$DisplayFont)); + } + } + + $LastX[$Key][$Pos] = $X2; + } + + $Y = $Y + $YStep; + } + } + } + } + } + + /* Draw a stacked area chart */ + function drawStackedAreaChart($Format=NULL) + { + $DrawLine = isset($Format["DrawLine"]) ? $Format["DrawLine"] : FALSE; + $LineSurrounding = isset($Format["LineSurrounding"]) ? $Format["LineSurrounding"] : NULL; + $LineR = isset($Format["LineR"]) ? $Format["LineR"] : VOID; + $LineG = isset($Format["LineG"]) ? $Format["LineG"] : VOID; + $LineB = isset($Format["LineB"]) ? $Format["LineB"] : VOID; + $LineAlpha = isset($Format["LineAlpha"]) ? $Format["LineAlpha"] : 100; + $DrawPlot = isset($Format["DrawPlot"]) ? $Format["DrawPlot"] : FALSE; + $PlotRadius = isset($Format["PlotRadius"]) ? $Format["PlotRadius"] : 2; + $PlotBorder = isset($Format["PlotBorder"]) ? $Format["PlotBorder"] : 1; + $PlotBorderSurrounding = isset($Format["PlotBorderSurrounding"]) ? $Format["PlotBorderSurrounding"] : NULL; + $PlotBorderR = isset($Format["PlotBorderR"]) ? $Format["PlotBorderR"] : 0; + $PlotBorderG = isset($Format["PlotBorderG"]) ? $Format["PlotBorderG"] : 0; + $PlotBorderB = isset($Format["PlotBorderB"]) ? $Format["PlotBorderB"] : 0; + $PlotBorderAlpha = isset($Format["PlotBorderAlpha"]) ? $Format["PlotBorderAlpha"] : 50; + $ForceTransparency = isset($Format["ForceTransparency"]) ? $Format["ForceTransparency"] : NULL; + + $this->LastChartLayout = CHART_LAST_LAYOUT_STACKED; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + $RestoreShadow = $this->Shadow; + $this->Shadow = FALSE; + + /* Build the offset data series */ + $OffsetData = ""; + $OverallOffset = ""; + $SerieOrder = ""; + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $SerieOrder[] = $SerieName; + + foreach($Serie["Data"] as $Key => $Value) + { + if ( $Value == VOID ) { $Value = 0; } + if ($Value >= 0) { $Sign = "+"; } else { $Sign = "-"; } + if ( !isset($OverallOffset[$Key]) || !isset($OverallOffset[$Key][$Sign]) ) { $OverallOffset[$Key][$Sign] = 0; } + + if ( $Sign == "+" ) + { $Data["Series"][$SerieName]["Data"][$Key] = $Value + $OverallOffset[$Key][$Sign]; } + else + { $Data["Series"][$SerieName]["Data"][$Key] = $Value - $OverallOffset[$Key][$Sign]; } + + $OverallOffset[$Key][$Sign] = $OverallOffset[$Key][$Sign] + abs($Value); + } + } + } + $SerieOrder = array_reverse($SerieOrder); + + $LastX = ""; $LastY = ""; + foreach($SerieOrder as $Key => $SerieName) + { + $Serie = $Data["Series"][$SerieName]; + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; + if ( $ForceTransparency != NULL ) { $Alpha = $ForceTransparency; } + + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha); + + if ( $LineSurrounding != NULL ) + $LineColor = array("R"=>$R+$LineSurrounding,"G"=>$G+$LineSurrounding,"B"=>$B+$LineSurrounding,"Alpha"=>$Alpha); + elseif ( $LineR != VOID ) + $LineColor = array("R"=>$LineR,"G"=>$LineG,"B"=>$LineB,"Alpha"=>$LineAlpha); + else + $LineColor = $Color; + + if ( $PlotBorderSurrounding != NULL ) + $PlotBorderColor = array("R"=>$R+$PlotBorderSurrounding,"G"=>$G+$PlotBorderSurrounding,"B"=>$B+$PlotBorderSurrounding,"Alpha"=>$PlotBorderAlpha); + else + $PlotBorderColor = array("R"=>$PlotBorderR,"G"=>$PlotBorderG,"B"=>$PlotBorderB,"Alpha"=>$PlotBorderAlpha); + + $AxisID = $Serie["Axis"]; + $Mode = $Data["Axis"][$AxisID]["Display"]; + $Format = $Data["Axis"][$AxisID]["Format"]; + $Unit = $Data["Axis"][$AxisID]["Unit"]; + + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"]),TRUE); + $YZero = $this->scaleComputeY(0,array("AxisID"=>$Serie["Axis"])); + + $this->DataSet->Data["Series"][$SerieName]["XOffset"] = 0; + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $YZero < $this->GraphAreaY1+1 ) { $YZero = $this->GraphAreaY1+1; } + if ( $YZero > $this->GraphAreaY2-1 ) { $YZero = $this->GraphAreaY2-1; } + + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + + $Plots = ""; $Plots[] = $X; $Plots[] = $YZero; + foreach($PosArray as $Key => $Height) + { + if ( $Height != VOID ) { $Plots[] = $X; $Plots[] = $YZero-$Height; } + $X = $X + $XStep; + } + $Plots[] = $X-$XStep; $Plots[] = $YZero; + + $this->drawPolygon($Plots,$Color); + + $this->Shadow = $RestoreShadow; + if ( $DrawLine ) { for($i=2; $i<=count($Plots)-6; $i=$i+2) { $this->drawLine($Plots[$i],$Plots[$i+1],$Plots[$i+2],$Plots[$i+3],$LineColor); } } + if ( $DrawPlot ) + { + for($i=2; $i<=count($Plots)-4; $i=$i+2) + { + if ( $PlotBorder != 0 ) + { $this->drawFilledCircle($Plots[$i],$Plots[$i+1],$PlotRadius+$PlotBorder,$PlotBorderColor); } + + $this->drawFilledCircle($Plots[$i],$Plots[$i+1],$PlotRadius,$Color); + } + } + $this->Shadow = FALSE; + } + elseif ( $Data["Orientation"] == SCALE_POS_TOPBOTTOM ) + { + if ( $YZero < $this->GraphAreaX1+1 ) { $YZero = $this->GraphAreaX1+1; } + if ( $YZero > $this->GraphAreaX2-1 ) { $YZero = $this->GraphAreaX2-1; } + + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + + $Plots = ""; $Plots[] = $YZero; $Plots[] = $Y; + foreach($PosArray as $Key => $Height) + { + if ( $Height != VOID ) { $Plots[] = $YZero+$Height; $Plots[] = $Y; } + $Y = $Y + $YStep; + } + $Plots[] = $YZero; $Plots[] = $Y-$YStep; + + $this->drawPolygon($Plots,$Color); + + $this->Shadow = $RestoreShadow; + if ( $DrawLine ) { for($i=2; $i<=count($Plots)-6; $i=$i+2) { $this->drawLine($Plots[$i],$Plots[$i+1],$Plots[$i+2],$Plots[$i+3],$LineColor); } } + if ( $DrawPlot ) + { + for($i=2; $i<=count($Plots)-4; $i=$i+2) + { + if ( $PlotBorder != 0 ) + { $this->drawFilledCircle($Plots[$i],$Plots[$i+1],$PlotRadius+$PlotBorder,$PlotBorderColor); } + + $this->drawFilledCircle($Plots[$i],$Plots[$i+1],$PlotRadius,$Color); + } + } + $this->Shadow = FALSE; + } + } + } + $this->Shadow = $RestoreShadow; + } + + /* Returns a random color */ + function getRandomColor($Alpha=100) + { return(array("R"=>rand(0,255),"G"=>rand(0,255),"B"=>rand(0,255),"Alpha"=>$Alpha)); } + + /* Validate a palette */ + function validatePalette($Colors,$Surrounding=NULL) + { + $Result = ""; + + if ( !is_array($Colors) ) { return($this->getRandomColor()); } + + foreach($Colors as $Key => $Values) + { + if ( isset($Values["R"]) ) { $Result[$Key]["R"] = $Values["R"]; } else { $Result[$Key]["R"] = rand(0,255); } + if ( isset($Values["G"]) ) { $Result[$Key]["G"] = $Values["G"]; } else { $Result[$Key]["G"] = rand(0,255); } + if ( isset($Values["B"]) ) { $Result[$Key]["B"] = $Values["B"]; } else { $Result[$Key]["B"] = rand(0,255); } + if ( isset($Values["Alpha"]) ) { $Result[$Key]["Alpha"] = $Values["Alpha"]; } else { $Result[$Key]["Alpha"] = 100; } + + if ( $Surrounding != NULL ) + { + $Result[$Key]["BorderR"] = $Result[$Key]["R"] + $Surrounding; + $Result[$Key]["BorderG"] = $Result[$Key]["G"] + $Surrounding; + $Result[$Key]["BorderB"] = $Result[$Key]["B"] + $Surrounding; + } + else + { + if ( isset($Values["BorderR"]) ) { $Result[$Key]["BorderR"] = $Values["BorderR"]; } else { $Result[$Key]["BorderR"] = $Result[$Key]["R"]; } + if ( isset($Values["BorderG"]) ) { $Result[$Key]["BorderG"] = $Values["BorderG"]; } else { $Result[$Key]["BorderG"] = $Result[$Key]["G"]; } + if ( isset($Values["BorderB"]) ) { $Result[$Key]["BorderB"] = $Values["BorderB"]; } else { $Result[$Key]["BorderB"] = $Result[$Key]["B"]; } + if ( isset($Values["BorderAlpha"]) ) { $Result[$Key]["BorderAlpha"] = $Values["BorderAlpha"]; } else { $Result[$Key]["BorderAlpha"] = $Result[$Key]["Alpha"]; } + } + } + + return($Result); + } + + /* Draw the derivative chart associated to the data series */ + function drawDerivative($Format=NULL) + { + $Offset = isset($Format["Offset"]) ? $Format["Offset"] : 10; + $SerieSpacing = isset($Format["SerieSpacing"]) ? $Format["SerieSpacing"] : 3; + $DerivativeHeight = isset($Format["DerivativeHeight"]) ? $Format["DerivativeHeight"] : 4; + $ShadedSlopeBox = isset($Format["ShadedSlopeBox"]) ? $Format["ShadedSlopeBox"] : FALSE; + $DrawBackground = isset($Format["DrawBackground"]) ? $Format["DrawBackground"] : TRUE; + $BackgroundR = isset($Format["BackgroundR"]) ? $Format["BackgroundR"] : 255; + $BackgroundG = isset($Format["BackgroundG"]) ? $Format["BackgroundG"] : 255; + $BackgroundB = isset($Format["BackgroundB"]) ? $Format["BackgroundB"] : 255; + $BackgroundAlpha = isset($Format["BackgroundAlpha"]) ? $Format["BackgroundAlpha"] : 20; + $DrawBorder = isset($Format["DrawBorder"]) ? $Format["DrawBorder"] : TRUE; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 0; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 0; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 0; + $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : 100; + $Caption = isset($Format["Caption"]) ? $Format["Caption"] : TRUE; + $CaptionHeight = isset($Format["CaptionHeight"]) ? $Format["CaptionHeight"] : 10; + $CaptionWidth = isset($Format["CaptionWidth"]) ? $Format["CaptionWidth"] : 20; + $CaptionMargin = isset($Format["CaptionMargin"]) ? $Format["CaptionMargin"] : 4; + $CaptionLine = isset($Format["CaptionLine"]) ? $Format["CaptionLine"] : FALSE; + $CaptionBox = isset($Format["CaptionBox"]) ? $Format["CaptionBox"] : FALSE; + $CaptionBorderR = isset($Format["CaptionBorderR"]) ? $Format["CaptionBorderR"] : 0; + $CaptionBorderG = isset($Format["CaptionBorderG"]) ? $Format["CaptionBorderG"] : 0; + $CaptionBorderB = isset($Format["CaptionBorderB"]) ? $Format["CaptionBorderB"] : 0; + $CaptionFillR = isset($Format["CaptionFillR"]) ? $Format["CaptionFillR"] : 255; + $CaptionFillG = isset($Format["CaptionFillG"]) ? $Format["CaptionFillG"] : 255; + $CaptionFillB = isset($Format["CaptionFillB"]) ? $Format["CaptionFillB"] : 255; + $CaptionFillAlpha = isset($Format["CaptionFillAlpha"]) ? $Format["CaptionFillAlpha"] : 80; + $PositiveSlopeStartR = isset($Format["PositiveSlopeStartR"]) ? $Format["PositiveSlopeStartR"] : 184; + $PositiveSlopeStartG = isset($Format["PositiveSlopeStartG"]) ? $Format["PositiveSlopeStartG"] : 234; + $PositiveSlopeStartB = isset($Format["PositiveSlopeStartB"]) ? $Format["PositiveSlopeStartB"] : 88; + $PositiveSlopeEndR = isset($Format["PositiveSlopeStartR"]) ? $Format["PositiveSlopeStartR"] : 239; + $PositiveSlopeEndG = isset($Format["PositiveSlopeStartG"]) ? $Format["PositiveSlopeStartG"] : 31; + $PositiveSlopeEndB = isset($Format["PositiveSlopeStartB"]) ? $Format["PositiveSlopeStartB"] : 36; + $NegativeSlopeStartR = isset($Format["NegativeSlopeStartR"]) ? $Format["NegativeSlopeStartR"] : 184; + $NegativeSlopeStartG = isset($Format["NegativeSlopeStartG"]) ? $Format["NegativeSlopeStartG"] : 234; + $NegativeSlopeStartB = isset($Format["NegativeSlopeStartB"]) ? $Format["NegativeSlopeStartB"] : 88; + $NegativeSlopeEndR = isset($Format["NegativeSlopeStartR"]) ? $Format["NegativeSlopeStartR"] : 67; + $NegativeSlopeEndG = isset($Format["NegativeSlopeStartG"]) ? $Format["NegativeSlopeStartG"] : 124; + $NegativeSlopeEndB = isset($Format["NegativeSlopeStartB"]) ? $Format["NegativeSlopeStartB"] : 227; + + $Data = $this->DataSet->getData(); + + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + $YPos = $this->DataSet->Data["GraphArea"]["Y2"] + $Offset; + else + $XPos = $this->DataSet->Data["GraphArea"]["X2"] + $Offset; + + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; $Alpha = $Serie["Color"]["Alpha"]; $Ticks = $Serie["Ticks"]; $Weight = $Serie["Weight"]; + + $AxisID = $Serie["Axis"]; + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $Caption ) + { + if ( $CaptionLine ) + { + $StartX = floor($this->GraphAreaX1-$CaptionWidth+$XMargin-$CaptionMargin); + $EndX = floor($this->GraphAreaX1-$CaptionMargin+$XMargin); + + $CaptionSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight); + if ( $CaptionBox ) { $this->drawFilledRectangle($StartX,$YPos,$EndX,$YPos+$CaptionHeight,array("R"=>$CaptionFillR,"G"=>$CaptionFillG,"B"=>$CaptionFillB,"BorderR"=>$CaptionBorderR,"BorderG"=>$CaptionBorderG,"BorderB"=>$CaptionBorderB,"Alpha"=>$CaptionFillAlpha)); } + $this->drawLine($StartX+2,$YPos+($CaptionHeight/2),$EndX-2,$YPos+($CaptionHeight/2),$CaptionSettings); + } + else + $this->drawFilledRectangle($this->GraphAreaX1-$CaptionWidth+$XMargin-$CaptionMargin,$YPos,$this->GraphAreaX1-$CaptionMargin+$XMargin,$YPos+$CaptionHeight,array("R"=>$R,"G"=>$G,"B"=>$B,"BorderR"=>$CaptionBorderR,"BorderG"=>$CaptionBorderG,"BorderB"=>$CaptionBorderB)); + } + + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; + + $TopY = $YPos + ($CaptionHeight/2) - ($DerivativeHeight/2); + $BottomY = $YPos + ($CaptionHeight/2) + ($DerivativeHeight/2); + + $StartX = floor($this->GraphAreaX1+$XMargin); + $EndX = floor($this->GraphAreaX2-$XMargin); + + if ( $DrawBackground ) { $this->drawFilledRectangle($StartX-1,$TopY-1,$EndX+1,$BottomY+1,array("R"=>$BackgroundR,"G"=>$BackgroundG,"B"=>$BackgroundB,"Alpha"=>$BackgroundAlpha)); } + if ( $DrawBorder ) { $this->drawRectangle($StartX-1,$TopY-1,$EndX+1,$BottomY+1,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); } + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + + $RestoreShadow = $this->Shadow; + $this->Shadow = FALSE; + + /* Determine the Max slope index */ + $LastX = NULL; $LastY = NULL; $MinSlope = 0; $MaxSlope = 1; + foreach($PosArray as $Key => $Y) + { + if ( $Y != VOID && $LastX != NULL ) + { $Slope = ($LastY - $Y); if ( $Slope > $MaxSlope ) { $MaxSlope = $Slope; } if ( $Slope < $MinSlope ) { $MinSlope = $Slope; } } + + if ( $Y == VOID ) + { $LastX = NULL; $LastY = NULL; } + else + { $LastX = $X; $LastY = $Y; } + } + + $LastX = NULL; $LastY = NULL; $LastColor = NULL; + foreach($PosArray as $Key => $Y) + { + if ( $Y != VOID && $LastY != NULL ) + { + $Slope = ($LastY - $Y); + + if ( $Slope >= 0 ) + { + $SlopeIndex = (100 / $MaxSlope) * $Slope; + $R = (($PositiveSlopeEndR - $PositiveSlopeStartR)/100)*$SlopeIndex+$PositiveSlopeStartR; + $G = (($PositiveSlopeEndG - $PositiveSlopeStartG)/100)*$SlopeIndex+$PositiveSlopeStartG; + $B = (($PositiveSlopeEndB - $PositiveSlopeStartB)/100)*$SlopeIndex+$PositiveSlopeStartB; + } + elseif ( $Slope < 0 ) + { + $SlopeIndex = (100 / abs($MinSlope)) * abs($Slope); + $R = (($NegativeSlopeEndR - $NegativeSlopeStartR)/100)*$SlopeIndex+$NegativeSlopeStartR; + $G = (($NegativeSlopeEndG - $NegativeSlopeStartG)/100)*$SlopeIndex+$NegativeSlopeStartG; + $B = (($NegativeSlopeEndB - $NegativeSlopeStartB)/100)*$SlopeIndex+$NegativeSlopeStartB; + } + + $Color = array("R"=>$R,"G"=>$G,"B"=>$B); + + if ( $ShadedSlopeBox && $LastColor != NULL ) // && $Slope != 0 + { + $GradientSettings = array("StartR"=>$LastColor["R"],"StartG"=>$LastColor["G"],"StartB"=>$LastColor["B"],"EndR"=>$R,"EndG"=>$G,"EndB"=>$B); + $this->drawGradientArea($LastX,$TopY,$X,$BottomY,DIRECTION_HORIZONTAL,$GradientSettings); + } + elseif ( !$ShadedSlopeBox || $LastColor == NULL ) // || $Slope == 0 + $this->drawFilledRectangle(floor($LastX),$TopY,floor($X),$BottomY,$Color); + + $LastColor = $Color; + } + + if ( $Y == VOID ) + { $LastY = NULL; } + else + { $LastX = $X; $LastY = $Y; } + + $X = $X + $XStep; + } + + $YPos = $YPos + $CaptionHeight + $SerieSpacing; + } + else + { + if ( $Caption ) + { + $StartY = floor($this->GraphAreaY1-$CaptionWidth+$XMargin-$CaptionMargin); + $EndY = floor($this->GraphAreaY1-$CaptionMargin+$XMargin); + if ( $CaptionLine ) + { + $CaptionSettings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks,"Weight"=>$Weight); + if ( $CaptionBox ) { $this->drawFilledRectangle($XPos,$StartY,$XPos+$CaptionHeight,$EndY,array("R"=>$CaptionFillR,"G"=>$CaptionFillG,"B"=>$CaptionFillB,"BorderR"=>$CaptionBorderR,"BorderG"=>$CaptionBorderG,"BorderB"=>$CaptionBorderB,"Alpha"=>$CaptionFillAlpha)); } + $this->drawLine($XPos+($CaptionHeight/2),$StartY+2,$XPos+($CaptionHeight/2),$EndY-2,$CaptionSettings); + } + else + $this->drawFilledRectangle($XPos,$StartY,$XPos+$CaptionHeight,$EndY,array("R"=>$R,"G"=>$G,"B"=>$B,"BorderR"=>$CaptionBorderR,"BorderG"=>$CaptionBorderG,"BorderB"=>$CaptionBorderB)); + } + + + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $XStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; + + $TopX = $XPos + ($CaptionHeight/2) - ($DerivativeHeight/2); + $BottomX = $XPos + ($CaptionHeight/2) + ($DerivativeHeight/2); + + $StartY = floor($this->GraphAreaY1+$XMargin); + $EndY = floor($this->GraphAreaY2-$XMargin); + + if ( $DrawBackground ) { $this->drawFilledRectangle($TopX-1,$StartY-1,$BottomX+1,$EndY+1,array("R"=>$BackgroundR,"G"=>$BackgroundG,"B"=>$BackgroundB,"Alpha"=>$BackgroundAlpha)); } + if ( $DrawBorder ) { $this->drawRectangle($TopX-1,$StartY-1,$BottomX+1,$EndY+1,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); } + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + + $RestoreShadow = $this->Shadow; + $this->Shadow = FALSE; + + /* Determine the Max slope index */ + $LastX = NULL; $LastY = NULL; $MinSlope = 0; $MaxSlope = 1; + foreach($PosArray as $Key => $X) + { + if ( $X != VOID && $LastX != NULL ) + { $Slope = ($X - $LastX); if ( $Slope > $MaxSlope ) { $MaxSlope = $Slope; } if ( $Slope < $MinSlope ) { $MinSlope = $Slope; } } + + if ( $X == VOID ) + { $LastX = NULL; } + else + { $LastX = $X; } + } + + $LastX = NULL; $LastY = NULL; $LastColor = NULL; + foreach($PosArray as $Key => $X) + { + if ( $X != VOID && $LastX != NULL ) + { + $Slope = ($X - $LastX); + + if ( $Slope >= 0 ) + { + $SlopeIndex = (100 / $MaxSlope) * $Slope; + $R = (($PositiveSlopeEndR - $PositiveSlopeStartR)/100)*$SlopeIndex+$PositiveSlopeStartR; + $G = (($PositiveSlopeEndG - $PositiveSlopeStartG)/100)*$SlopeIndex+$PositiveSlopeStartG; + $B = (($PositiveSlopeEndB - $PositiveSlopeStartB)/100)*$SlopeIndex+$PositiveSlopeStartB; + } + elseif ( $Slope < 0 ) + { + $SlopeIndex = (100 / abs($MinSlope)) * abs($Slope); + $R = (($NegativeSlopeEndR - $NegativeSlopeStartR)/100)*$SlopeIndex+$NegativeSlopeStartR; + $G = (($NegativeSlopeEndG - $NegativeSlopeStartG)/100)*$SlopeIndex+$NegativeSlopeStartG; + $B = (($NegativeSlopeEndB - $NegativeSlopeStartB)/100)*$SlopeIndex+$NegativeSlopeStartB; + } + + $Color = array("R"=>$R,"G"=>$G,"B"=>$B); + + if ( $ShadedSlopeBox && $LastColor != NULL ) + { + $GradientSettings = array("StartR"=>$LastColor["R"],"StartG"=>$LastColor["G"],"StartB"=>$LastColor["B"],"EndR"=>$R,"EndG"=>$G,"EndB"=>$B); + + $this->drawGradientArea($TopX,$LastY,$BottomX,$Y,DIRECTION_VERTICAL,$GradientSettings); + } + elseif ( !$ShadedSlopeBox || $LastColor == NULL ) + $this->drawFilledRectangle($TopX,floor($LastY),$BottomX,floor($Y),$Color); + + $LastColor = $Color; + } + + if ( $X == VOID ) + { $LastX = NULL; } + else + { $LastX = $X; $LastY = $Y; } + + $Y = $Y + $XStep; + } + + $XPos = $XPos + $CaptionHeight + $SerieSpacing; + } + + $this->Shadow = $RestoreShadow; + } + } + } + + /* Draw the line of best fit */ + function drawBestFit($Format="") + { + $OverrideTicks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL; + $OverrideR = isset($Format["R"]) ? $Format["R"] : VOID; + $OverrideG = isset($Format["G"]) ? $Format["G"] : VOID; + $OverrideB = isset($Format["B"]) ? $Format["B"] : VOID; + $OverrideAlpha = isset($Format["Alpha"]) ? $Format["Alpha"] : VOID; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + foreach($Data["Series"] as $SerieName => $Serie) + { + if ( $Serie["isDrawable"] == TRUE && $SerieName != $Data["Abscissa"] ) + { + if ( $OverrideR != VOID && $OverrideG != VOID && $OverrideB != VOID ) { $R = $OverrideR; $G = $OverrideG; $B = $OverrideB; } else { $R = $Serie["Color"]["R"]; $G = $Serie["Color"]["G"]; $B = $Serie["Color"]["B"]; } + if ( $OverrideTicks == NULL ) { $Ticks = $Serie["Ticks"]; } else { $Ticks = $OverrideTicks; } + if ( $OverrideAlpha == VOID ) { $Alpha = $Serie["Color"]["Alpha"]; } else { $Alpha = $OverrideAlpha; } + + $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks); + + $AxisID = $Serie["Axis"]; + $PosArray = $this->scaleComputeY($Serie["Data"],array("AxisID"=>$Serie["Axis"])); + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $Sxy = 0; $Sx = 0; $Sy = 0; $Sxx = 0; + foreach($PosArray as $Key => $Y) + { + if ( $Y != VOID ) + { + $Sxy = $Sxy + $X*$Y; + $Sx = $Sx + $X; + $Sy = $Sy + $Y; + $Sxx = $Sxx + $X*$X; + } + + $X = $X + $XStep; + } + $n = count($this->DataSet->stripVOID($PosArray)); //$n = count($PosArray); + $M = (($n*$Sxy)-($Sx*$Sy)) / (($n*$Sxx)-($Sx*$Sx)); + $B = (($Sy)-($M*$Sx))/($n); + + $X1 = $this->GraphAreaX1 + $XMargin; + $Y1 = $M * $X1 + $B; + $X2 = $this->GraphAreaX2 - $XMargin; + $Y2 = $M * $X2 + $B; + + if ( $Y1 < $this->GraphAreaY1 ) { $X1 = $X1 + ($this->GraphAreaY1-$Y1); $Y1 = $this->GraphAreaY1; } + if ( $Y1 > $this->GraphAreaY2 ) { $X1 = $X1 + ($Y1-$this->GraphAreaY2); $Y1 = $this->GraphAreaY2; } + if ( $Y2 < $this->GraphAreaY1 ) { $X2 = $X2 - ($this->GraphAreaY1-$Y2); $Y2 = $this->GraphAreaY1; } + if ( $Y2 > $this->GraphAreaY2 ) { $X2 = $X2 - ($Y2-$this->GraphAreaY2); $Y2 = $this->GraphAreaY2; } + + $this->drawLine($X1,$Y1,$X2,$Y2,$Color); + } + else + { + if ( $XDivs == 0 ) { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $YStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin; + + if ( !is_array($PosArray) ) { $Value = $PosArray; $PosArray = ""; $PosArray[0] = $Value; } + $Sxy = 0; $Sx = 0; $Sy = 0; $Sxx = 0; + foreach($PosArray as $Key => $X) + { + if ( $X != VOID ) + { + $Sxy = $Sxy + $X*$Y; + $Sx = $Sx + $Y; + $Sy = $Sy + $X; + $Sxx = $Sxx + $Y*$Y; + } + + $Y = $Y + $YStep; + } + $n = count($this->DataSet->stripVOID($PosArray)); //$n = count($PosArray); + $M = (($n*$Sxy)-($Sx*$Sy)) / (($n*$Sxx)-($Sx*$Sx)); + $B = (($Sy)-($M*$Sx))/($n); + + $Y1 = $this->GraphAreaY1 + $XMargin; + $X1 = $M * $Y1 + $B; + $Y2 = $this->GraphAreaY2 - $XMargin; + $X2 = $M * $Y2 + $B; + + if ( $X1 < $this->GraphAreaX1 ) { $Y1 = $Y1 + ($this->GraphAreaX1-$X1); $X1 = $this->GraphAreaX1; } + if ( $X1 > $this->GraphAreaX2 ) { $Y1 = $Y1 + ($X1-$this->GraphAreaX2); $X1 = $this->GraphAreaX2; } + if ( $X2 < $this->GraphAreaX1 ) { $Y2 = $Y2 - ($this->GraphAreaY1-$X2); $X2 = $this->GraphAreaX1; } + if ( $X2 > $this->GraphAreaX2 ) { $Y2 = $Y2 - ($X2-$this->GraphAreaX2); $X2 = $this->GraphAreaX2; } + + $this->drawLine($X1,$Y1,$X2,$Y2,$Color); + } + } + } + } + + /* Write labels */ + function writeLabel($SeriesName,$Indexes,$Format="") + { + $OverrideTitle = isset($Format["OverrideTitle"]) ? $Format["OverrideTitle"] : NULL; + $ForceLabels = isset($Format["ForceLabels"]) ? $Format["ForceLabels"] : NULL; + $DrawPoint = isset($Format["DrawPoint"]) ? $Format["DrawPoint"] : LABEL_POINT_BOX; + $DrawVerticalLine = isset($Format["DrawVerticalLine"]) ? $Format["DrawVerticalLine"] : FALSE; + $VerticalLineR = isset($Format["VerticalLineR"]) ? $Format["VerticalLineR"] : 0; + $VerticalLineG = isset($Format["VerticalLineG"]) ? $Format["VerticalLineG"] : 0; + $VerticalLineB = isset($Format["VerticalLineB"]) ? $Format["VerticalLineB"] : 0; + $VerticalLineAlpha = isset($Format["VerticalLineAlpha"]) ? $Format["VerticalLineAlpha"] : 40; + $VerticalLineTicks = isset($Format["VerticalLineTicks"]) ? $Format["VerticalLineTicks"] : 2; + + $Data = $this->DataSet->getData(); + list($XMargin,$XDivs) = $this->scaleGetXSettings(); + + if ( !is_array($Indexes) ) { $Index = $Indexes; $Indexes = ""; $Indexes[] = $Index; } + if ( !is_array($SeriesName) ) { $SerieName = $SeriesName; $SeriesName = ""; $SeriesName[] = $SerieName; } + if ( $ForceLabels != NULL && !is_array($ForceLabels) ) { $ForceLabel = $ForceLabels; $ForceLabels = ""; $ForceLabels[] = $ForceLabel; } + + foreach ($Indexes as $Key => $Index) + { + $Series = ""; + + if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT ) + { + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1)/4; } else { $XStep = ($this->GraphAreaX2-$this->GraphAreaX1-$XMargin*2)/$XDivs; } + $X = $this->GraphAreaX1 + $XMargin + $Index * $XStep; + + if ( $DrawVerticalLine ) { $this->drawLine($X,$this->GraphAreaY1+$Data["YMargin"],$X,$this->GraphAreaY2-$Data["YMargin"],array("R"=>$VerticalLineR,"G"=>$VerticalLineG,"B"=>$VerticalLineB,"Alpha"=>$VerticalLineAlpha,"Ticks"=>$VerticalLineTicks)); } + + $MinY = $this->GraphAreaY2; + foreach ($SeriesName as $iKey => $SerieName) + { + if ( isset($Data["Series"][$SerieName]["Data"][$Index]) ) + { + $AxisID = $Data["Series"][$SerieName]["Axis"]; + + if ( $OverrideTitle != NULL) + $Description = $OverrideTitle; + elseif ( count($SeriesName) == 1 ) + { + if ( isset($Data["Abscissa"]) && isset($Data["Series"][$Data["Abscissa"]]["Data"][$Index]) ) + $Description = $Data["Series"][$SerieName]["Description"]." - ".$Data["Series"][$Data["Abscissa"]]["Data"][$Index]; + else + $Description = $Data["Series"][$SerieName]["Description"]; + } + elseif ( isset($Data["Abscissa"]) && isset($Data["Series"][$Data["Abscissa"]]["Data"][$Index]) ) + $Description = $Data["Series"][$Data["Abscissa"]]["Data"][$Index]; + + $AxisMode = $Data["Axis"][$AxisID]["Display"]; + $AxisFormat = $Data["Axis"][$AxisID]["Format"]; + $AxisUnit = $Data["Axis"][$AxisID]["Unit"]; + + $Serie = ""; + $Serie["R"] = $Data["Series"][$SerieName]["Color"]["R"]; + $Serie["G"] = $Data["Series"][$SerieName]["Color"]["G"]; + $Serie["B"] = $Data["Series"][$SerieName]["Color"]["B"]; + $Serie["Alpha"] = $Data["Series"][$SerieName]["Color"]["Alpha"]; + + if ( count($SeriesName) == 1 && isset($Data["Series"][$SerieName]["XOffset"]) ) + $SerieOffset = $Data["Series"][$SerieName]["XOffset"]; + else + $SerieOffset = 0; + + $Value = $Data["Series"][$SerieName]["Data"][$Index]; + if ( $Value == VOID ) { $Value = "NaN"; } + + if ( $ForceLabels != NULL ) + $Caption = isset($ForceLabels[$Key]) ? $ForceLabels[$Key] : "Not set"; + else + $Caption = $this->scaleFormat($Value,$AxisMode,$AxisFormat,$AxisUnit); + + if ( $this->LastChartLayout == CHART_LAST_LAYOUT_STACKED ) + { + if ( $Value >=0 ) { $LookFor = "+"; } else { $LookFor = "-"; } + + $Value = 0; $Done = FALSE; + foreach($Data["Series"] as $Name => $SerieLookup) + { + if ( $SerieLookup["isDrawable"] == TRUE && $Name != $Data["Abscissa"] && !$Done ) + { + if ( isset($Data["Series"][$Name]["Data"][$Index]) && $Data["Series"][$Name]["Data"][$Index] != VOID ) + { + if ($Data["Series"][$Name]["Data"][$Index] >= 0 && $LookFor == "+" ) { $Value = $Value + $Data["Series"][$Name]["Data"][$Index]; } + if ($Data["Series"][$Name]["Data"][$Index] < 0 && $LookFor == "-" ) { $Value = $Value - $Data["Series"][$Name]["Data"][$Index]; } + if ($Name == $SerieName ) { $Done = TRUE; } + } + } + } + } + + $X = floor($this->GraphAreaX1 + $XMargin + $Index * $XStep + $SerieOffset); + $Y = floor($this->scaleComputeY($Value,array("AxisID"=>$AxisID))); + + if ($Y < $MinY) { $MinY = $Y; } + + if ( $DrawPoint == LABEL_POINT_CIRCLE ) + $this->drawFilledCircle($X,$Y,3,array("R"=>255,"G"=>255,"B"=>255,"BorderR"=>0,"BorderG"=>0,"BorderB"=>0)); + elseif ( $DrawPoint == LABEL_POINT_BOX ) + $this->drawFilledRectangle($X-2,$Y-2,$X+2,$Y+2,array("R"=>255,"G"=>255,"B"=>255,"BorderR"=>0,"BorderG"=>0,"BorderB"=>0)); + + $Series[] = array("Format"=>$Serie,"Caption"=>$Caption); + } + } + $this->drawLabelBox($X,$MinY-3,$Description,$Series,$Format); + + } + else + { + if ( $XDivs == 0 ) { $XStep = ($this->GraphAreaY2-$this->GraphAreaY1)/4; } else { $XStep = ($this->GraphAreaY2-$this->GraphAreaY1-$XMargin*2)/$XDivs; } + $Y = $this->GraphAreaY1 + $XMargin + $Index * $XStep; + + if ( $DrawVerticalLine ) { $this->drawLine($this->GraphAreaX1+$Data["YMargin"],$Y,$this->GraphAreaX2-$Data["YMargin"],$Y,array("R"=>$VerticalLineR,"G"=>$VerticalLineG,"B"=>$VerticalLineB,"Alpha"=>$VerticalLineAlpha,"Ticks"=>$VerticalLineTicks)); } + + $MinX = $this->GraphAreaX2; + foreach ($SeriesName as $Key => $SerieName) + { + if ( isset($Data["Series"][$SerieName]["Data"][$Index]) ) + { + $AxisID = $Data["Series"][$SerieName]["Axis"]; + + if ( $OverrideTitle != NULL) + $Description = $OverrideTitle; + elseif ( count($SeriesName) == 1 ) + { + if ( isset($Data["Abscissa"]) && isset($Data["Series"][$Data["Abscissa"]]["Data"][$Index]) ) + $Description = $Data["Series"][$SerieName]["Description"]." - ".$Data["Series"][$Data["Abscissa"]]["Data"][$Index]; + else + $Description = $Data["Series"][$SerieName]["Description"]; + } + elseif ( isset($Data["Abscissa"]) && isset($Data["Series"][$Data["Abscissa"]]["Data"][$Index]) ) + $Description = $Data["Series"][$Data["Abscissa"]]["Data"][$Index]; + + $AxisMode = $Data["Axis"][$AxisID]["Display"]; + $AxisFormat = $Data["Axis"][$AxisID]["Format"]; + $AxisUnit = $Data["Axis"][$AxisID]["Unit"]; + + $Serie = ""; + if ( isset($Data["Extended"]["Palette"][$Index] ) ) + { + $Serie["R"] = $Data["Extended"]["Palette"][$Index]["R"]; + $Serie["G"] = $Data["Extended"]["Palette"][$Index]["G"]; + $Serie["B"] = $Data["Extended"]["Palette"][$Index]["B"]; + $Serie["Alpha"] = $Data["Extended"]["Palette"][$Index]["Alpha"]; + } + else + { + $Serie["R"] = $Data["Series"][$SerieName]["Color"]["R"]; + $Serie["G"] = $Data["Series"][$SerieName]["Color"]["G"]; + $Serie["B"] = $Data["Series"][$SerieName]["Color"]["B"]; + $Serie["Alpha"] = $Data["Series"][$SerieName]["Color"]["Alpha"]; + } + + if ( count($SeriesName) == 1 && isset($Data["Series"][$SerieName]["XOffset"]) ) + $SerieOffset = $Data["Series"][$SerieName]["XOffset"]; + else + $SerieOffset = 0; + + $Value = $Data["Series"][$SerieName]["Data"][$Index]; + if ( $ForceLabels != NULL ) + $Caption = isset($ForceLabels[$Key]) ? $ForceLabels[$Key] : "Not set"; + else + $Caption = $this->scaleFormat($Value,$AxisMode,$AxisFormat,$AxisUnit); + if ( $Value == VOID ) { $Value = "NaN"; } + + if ( $this->LastChartLayout == CHART_LAST_LAYOUT_STACKED ) + { + if ( $Value >=0 ) { $LookFor = "+"; } else { $LookFor = "-"; } + + $Value = 0; $Done = FALSE; + foreach($Data["Series"] as $Name => $SerieLookup) + { + if ( $SerieLookup["isDrawable"] == TRUE && $Name != $Data["Abscissa"] && !$Done ) + { + if ( isset($Data["Series"][$Name]["Data"][$Index]) && $Data["Series"][$Name]["Data"][$Index] != VOID ) + { + if ($Data["Series"][$Name]["Data"][$Index] >= 0 && $LookFor == "+" ) { $Value = $Value + $Data["Series"][$Name]["Data"][$Index]; } + if ($Data["Series"][$Name]["Data"][$Index] < 0 && $LookFor == "-" ) { $Value = $Value - $Data["Series"][$Name]["Data"][$Index]; } + if ($Name == $SerieName ) { $Done = TRUE; } + } + } + } + } + + $X = floor($this->scaleComputeY($Value,array("AxisID"=>$AxisID))); + $Y = floor($this->GraphAreaY1 + $XMargin + $Index * $XStep + $SerieOffset); + + if ($X < $MinX) { $MinX = $X; } + + if ( $DrawPoint == LABEL_POINT_CIRCLE ) + $this->drawFilledCircle($X,$Y,3,array("R"=>255,"G"=>255,"B"=>255,"BorderR"=>0,"BorderG"=>0,"BorderB"=>0)); + elseif ( $DrawPoint == LABEL_POINT_BOX ) + $this->drawFilledRectangle($X-2,$Y-2,$X+2,$Y+2,array("R"=>255,"G"=>255,"B"=>255,"BorderR"=>0,"BorderG"=>0,"BorderB"=>0)); + + $Series[] = array("Format"=>$Serie,"Caption"=>$Caption); + } + } + $this->drawLabelBox($MinX,$Y-3,$Description,$Series,$Format); + + } + } + } + + /* Draw a label box */ + function drawLabelBox($X,$Y,$Title,$Captions,$Format="") + { + $NoTitle = isset($Format["NoTitle"]) ? $Format["NoTitle"] : NULL; + $BoxWidth = isset($Format["BoxWidth"]) ? $Format["BoxWidth"] : 50; + $DrawSerieColor = isset($Format["DrawSerieColor"]) ? $Format["DrawSerieColor"] : TRUE; + $SerieR = isset($Format["SerieR"]) ? $Format["SerieR"] : NULL; + $SerieG = isset($Format["SerieG"]) ? $Format["SerieG"] : NULL; + $SerieB = isset($Format["SerieB"]) ? $Format["SerieB"] : NULL; + $SerieAlpha = isset($Format["SerieAlpha"]) ? $Format["SerieAlpha"] : NULL; + $SerieBoxSize = isset($Format["SerieBoxSize"]) ? $Format["SerieBoxSize"] : 6; + $SerieBoxSpacing = isset($Format["SerieBoxSpacing"]) ? $Format["SerieBoxSpacing"] : 4; + $VerticalMargin = isset($Format["VerticalMargin"]) ? $Format["VerticalMargin"] : 10; + $HorizontalMargin = isset($Format["HorizontalMargin"]) ? $Format["HorizontalMargin"] : 8; + $R = isset($Format["R"]) ? $Format["R"] : $this->FontColorR; + $G = isset($Format["G"]) ? $Format["G"] : $this->FontColorG; + $B = isset($Format["B"]) ? $Format["B"] : $this->FontColorB; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : $this->FontColorA; + $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->FontName; + $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->FontSize; + $TitleMode = isset($Format["TitleMode"]) ? $Format["TitleMode"] : LABEL_TITLE_NOBACKGROUND; + $TitleR = isset($Format["TitleR"]) ? $Format["TitleR"] : $R; + $TitleG = isset($Format["TitleG"]) ? $Format["TitleG"] : $G; + $TitleB = isset($Format["TitleB"]) ? $Format["TitleB"] : $B; + $TitleBackgroundR = isset($Format["TitleBackgroundR"]) ? $Format["TitleBackgroundR"] : 0; + $TitleBackgroundG = isset($Format["TitleBackgroundG"]) ? $Format["TitleBackgroundG"] : 0; + $TitleBackgroundB = isset($Format["TitleBackgroundB"]) ? $Format["TitleBackgroundB"] : 0; + $GradientStartR = isset($Format["GradientStartR"]) ? $Format["GradientStartR"] : 255; + $GradientStartG = isset($Format["GradientStartG"]) ? $Format["GradientStartG"] : 255; + $GradientStartB = isset($Format["GradientStartB"]) ? $Format["GradientStartB"] : 255; + $GradientEndR = isset($Format["GradientEndR"]) ? $Format["GradientEndR"] : 220; + $GradientEndG = isset($Format["GradientEndG"]) ? $Format["GradientEndG"] : 220; + $GradientEndB = isset($Format["GradientEndB"]) ? $Format["GradientEndB"] : 220; + + if ( !$DrawSerieColor ) { $SerieBoxSize = 0; $SerieBoxSpacing = 0; } + + $TxtPos = $this->getTextBox($X,$Y,$FontName,$FontSize,0,$Title); + $TitleWidth = ($TxtPos[1]["X"] - $TxtPos[0]["X"])+$VerticalMargin*2; + $TitleHeight = ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]); + + if ( $NoTitle ) { $TitleWidth = 0; $TitleHeight = 0; } + + $CaptionWidth = 0; $CaptionHeight = -$HorizontalMargin; + foreach($Captions as $Key =>$Caption) + { + $TxtPos = $this->getTextBox($X,$Y,$FontName,$FontSize,0,$Caption["Caption"]); + $CaptionWidth = max($CaptionWidth,($TxtPos[1]["X"] - $TxtPos[0]["X"])+$VerticalMargin*2); + $CaptionHeight = $CaptionHeight + max(($TxtPos[0]["Y"] - $TxtPos[2]["Y"]),($SerieBoxSize+2)) + $HorizontalMargin; + } + + if ( $CaptionHeight <= 5 ) { $CaptionHeight = $CaptionHeight + $HorizontalMargin/2; } + + if ( $DrawSerieColor ) { $CaptionWidth = $CaptionWidth + $SerieBoxSize + $SerieBoxSpacing; } + + $BoxWidth = max($BoxWidth,$TitleWidth,$CaptionWidth); + + $XMin = $X - 5 - floor(($BoxWidth-10) / 2); + $XMax = $X + 5 + floor(($BoxWidth-10) / 2); + + $RestoreShadow = $this->Shadow; + if ( $this->Shadow == TRUE ) + { + $this->Shadow = FALSE; + + $Poly = ""; + $Poly[] = $X+$this->ShadowX; $Poly[] = $Y+$this->ShadowX; + $Poly[] = $X+5+$this->ShadowX; $Poly[] = $Y-5+$this->ShadowX; + $Poly[] = $XMax+$this->ShadowX; $Poly[] = $Y-5+$this->ShadowX; + + if ( $NoTitle ) + { + $Poly[] = $XMax+$this->ShadowX; $Poly[] = $Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2+$this->ShadowX; + $Poly[] = $XMin+$this->ShadowX; $Poly[] = $Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2+$this->ShadowX; + } + else + { + $Poly[] = $XMax+$this->ShadowX; $Poly[] = $Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3+$this->ShadowX; + $Poly[] = $XMin+$this->ShadowX; $Poly[] = $Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3+$this->ShadowX; + } + + $Poly[] = $XMin+$this->ShadowX; $Poly[] = $Y-5+$this->ShadowX; + $Poly[] = $X-5+$this->ShadowX; $Poly[] = $Y-5+$this->ShadowX; + $this->drawPolygon($Poly,array("R"=>$this->ShadowR,"G"=>$this->ShadowG,"B"=>$this->ShadowB,"Alpha"=>$this->Shadowa)); + } + + /* Draw the background */ + $GradientSettings = array("StartR"=>$GradientStartR,"StartG"=>$GradientStartG,"StartB"=>$GradientStartB,"EndR"=>$GradientEndR,"EndG"=>$GradientEndG,"EndB"=>$GradientEndB); + if ( $NoTitle ) + $this->drawGradientArea($XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax,$Y-6,DIRECTION_VERTICAL,$GradientSettings); + else + $this->drawGradientArea($XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax,$Y-6,DIRECTION_VERTICAL,$GradientSettings); + $Poly = ""; $Poly[] = $X; $Poly[] = $Y; $Poly[] = $X-5; $Poly[] = $Y-5; $Poly[] = $X+5; $Poly[] = $Y-5; + $this->drawPolygon($Poly,array("R"=>$GradientEndR,"G"=>$GradientEndG,"B"=>$GradientEndB,"NoBorder"=>TRUE)); + + /* Outer border */ + $OuterBorderColor = $this->allocateColor($this->Picture,100,100,100,100); + imageline($this->Picture,$XMin,$Y-5,$X-5,$Y-5,$OuterBorderColor); + imageline($this->Picture,$X,$Y,$X-5,$Y-5,$OuterBorderColor); + imageline($this->Picture,$X,$Y,$X+5,$Y-5,$OuterBorderColor); + imageline($this->Picture,$X+5,$Y-5,$XMax,$Y-5,$OuterBorderColor); + if ( $NoTitle ) + { + imageline($this->Picture,$XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMin,$Y-5,$OuterBorderColor); + imageline($this->Picture,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax,$Y-5,$OuterBorderColor); + imageline($this->Picture,$XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$OuterBorderColor); + } + else + { + imageline($this->Picture,$XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMin,$Y-5,$OuterBorderColor); + imageline($this->Picture,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax,$Y-5,$OuterBorderColor); + imageline($this->Picture,$XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$OuterBorderColor); + } + + /* Inner border */ + $InnerBorderColor = $this->allocateColor($this->Picture,255,255,255,100); + imageline($this->Picture,$XMin+1,$Y-6,$X-5,$Y-6,$InnerBorderColor); + imageline($this->Picture,$X,$Y-1,$X-5,$Y-6,$InnerBorderColor); + imageline($this->Picture,$X,$Y-1,$X+5,$Y-6,$InnerBorderColor); + imageline($this->Picture,$X+5,$Y-6,$XMax-1,$Y-6,$InnerBorderColor); + if ( $NoTitle ) + { + imageline($this->Picture,$XMin+1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMin+1,$Y-6,$InnerBorderColor); + imageline($this->Picture,$XMax-1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax-1,$Y-6,$InnerBorderColor); + imageline($this->Picture,$XMin+1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$XMax-1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*2,$InnerBorderColor); + } + else + { + imageline($this->Picture,$XMin+1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMin+1,$Y-6,$InnerBorderColor); + imageline($this->Picture,$XMax-1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax-1,$Y-6,$InnerBorderColor); + imageline($this->Picture,$XMin+1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax-1,$Y-4-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$InnerBorderColor); + } + + /* Draw the separator line */ + if ( $TitleMode == LABEL_TITLE_NOBACKGROUND && !$NoTitle ) + { + $YPos = $Y-7-$CaptionHeight-$HorizontalMargin-$HorizontalMargin/2; + $XMargin = $VerticalMargin / 2; + $this->drawLine($XMin+$XMargin,$YPos+1,$XMax-$XMargin,$YPos+1,array("R"=>$GradientEndR,"G"=>$GradientEndG,"B"=>$GradientEndB)); + $this->drawLine($XMin+$XMargin,$YPos,$XMax-$XMargin,$YPos,array("R"=>$GradientStartR,"G"=>$GradientStartG,"B"=>$GradientStartB)); + } + elseif ( $TitleMode == LABEL_TITLE_BACKGROUND ) + { + $this->drawFilledRectangle($XMin,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin*3,$XMax,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin+$HorizontalMargin/2,array("R"=>$TitleBackgroundR,"G"=>$TitleBackgroundG,"B"=>$TitleBackgroundB)); + imageline($this->Picture,$XMin+1,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin+$HorizontalMargin/2+1,$XMax-1,$Y-5-$TitleHeight-$CaptionHeight-$HorizontalMargin+$HorizontalMargin/2+1,$InnerBorderColor); + } + + /* Write the description */ + if ( !$NoTitle ) + $this->drawText($XMin+$VerticalMargin,$Y-7-$CaptionHeight-$HorizontalMargin*2,$Title,array("Align"=>TEXT_ALIGN_BOTTOMLEFT,"R"=>$TitleR,"G"=>$TitleG,"B"=>$TitleB)); + + /* Write the value */ + $YPos = $Y-5-$HorizontalMargin; $XPos = $XMin+$VerticalMargin+$SerieBoxSize+$SerieBoxSpacing; + foreach($Captions as $Key => $Caption) + { + $CaptionTxt = $Caption["Caption"]; + $TxtPos = $this->getTextBox($XPos,$YPos,$FontName,$FontSize,0,$CaptionTxt); + $CaptionHeight = ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]); + + /* Write the serie color if needed */ + if ( $DrawSerieColor ) + { + $BoxSettings = array("R"=>$Caption["Format"]["R"],"G"=>$Caption["Format"]["G"],"B"=>$Caption["Format"]["B"],"Alpha"=>$Caption["Format"]["Alpha"],"BorderR"=>0,"BorderG"=>0,"BorderB"=>0); + $this->drawFilledRectangle($XMin+$VerticalMargin,$YPos-$SerieBoxSize,$XMin+$VerticalMargin+$SerieBoxSize,$YPos,$BoxSettings); + } + + $this->drawText($XPos,$YPos,$CaptionTxt,array("Align"=>TEXT_ALIGN_BOTTOMLEFT)); + + $YPos = $YPos - $CaptionHeight - $HorizontalMargin; + } + + $this->Shadow = $RestoreShadow; + } + + /* Draw a basic shape */ + function drawShape($X,$Y,$Shape,$PlotSize,$PlotBorder,$BorderSize,$R,$G,$B,$Alpha,$BorderR,$BorderG,$BorderB,$BorderAlpha) + { + if ( $Shape == SERIE_SHAPE_FILLEDCIRCLE ) + { + if ( $PlotBorder ) { $this->drawFilledCircle($X,$Y,$PlotSize+$BorderSize,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); } + $this->drawFilledCircle($X,$Y,$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + } + elseif ( $Shape == SERIE_SHAPE_FILLEDSQUARE ) + { + if ( $PlotBorder ) { $this->drawFilledRectangle($X-$PlotSize-$BorderSize,$Y-$PlotSize-$BorderSize,$X+$PlotSize+$BorderSize,$Y+$PlotSize+$BorderSize,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); } + $this->drawFilledRectangle($X-$PlotSize,$Y-$PlotSize,$X+$PlotSize,$Y+$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + } + elseif ( $Shape == SERIE_SHAPE_FILLEDTRIANGLE ) + { + if ( $PlotBorder ) + { + $Pos = ""; $Pos[]=$X; $Pos[]=$Y-$PlotSize-$BorderSize; $Pos[]=$X-$PlotSize-$BorderSize; $Pos[]=$Y+$PlotSize+$BorderSize; $Pos[]=$X+$PlotSize+$BorderSize; $Pos[]=$Y+$PlotSize+$BorderSize; + $this->drawPolygon($Pos,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); + } + + $Pos = ""; $Pos[]=$X; $Pos[]=$Y-$PlotSize; $Pos[]=$X-$PlotSize; $Pos[]=$Y+$PlotSize; $Pos[]=$X+$PlotSize; $Pos[]=$Y+$PlotSize; + $this->drawPolygon($Pos,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + } + elseif ( $Shape == SERIE_SHAPE_TRIANGLE ) + { + $this->drawLine($X,$Y-$PlotSize,$X-$PlotSize,$Y+$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + $this->drawLine($X-$PlotSize,$Y+$PlotSize,$X+$PlotSize,$Y+$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + $this->drawLine($X+$PlotSize,$Y+$PlotSize,$X,$Y-$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + } + elseif ( $Shape == SERIE_SHAPE_SQUARE ) + $this->drawRectangle($X-$PlotSize,$Y-$PlotSize,$X+$PlotSize,$Y+$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + elseif ( $Shape == SERIE_SHAPE_CIRCLE ) + $this->drawCircle($X,$Y,$PlotSize,$PlotSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + elseif ( $Shape == SERIE_SHAPE_DIAMOND ) + { + $Pos = ""; $Pos[]=$X-$PlotSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y-$PlotSize; $Pos[]=$X+$PlotSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y+$PlotSize; + $this->drawPolygon($Pos,array("NoFill"=>TRUE,"BorderR"=>$R,"BorderG"=>$G,"BorderB"=>$B,"BorderAlpha"=>$Alpha)); + } + elseif ( $Shape == SERIE_SHAPE_FILLEDDIAMOND ) + { + if ( $PlotBorder ) + { + $Pos = ""; $Pos[]=$X-$PlotSize-$BorderSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y-$PlotSize-$BorderSize; $Pos[]=$X+$PlotSize+$BorderSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y+$PlotSize+$BorderSize; + $this->drawPolygon($Pos,array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha)); + } + + $Pos = ""; $Pos[]=$X-$PlotSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y-$PlotSize; $Pos[]=$X+$PlotSize; $Pos[]=$Y; $Pos[]=$X; $Pos[]=$Y+$PlotSize; + $this->drawPolygon($Pos,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha)); + } + } + + function drawPolygonChart($Points,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $NoFill = isset($Format["NoFill"]) ? $Format["NoFill"] : FALSE; + $NoBorder = isset($Format["NoBorder"]) ? $Format["NoBorder"] : FALSE; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : $R; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : $G; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : $B; + $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : $Alpha / 2; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + $Threshold = isset($Format["Threshold"]) ? $Format["Threshold"] : NULL; + + if ( $Surrounding != NULL ) { $BorderR = $R+$Surrounding; $BorderG = $G+$Surrounding; $BorderB = $B+$Surrounding; } + + $RestoreShadow = $this->Shadow; + $this->Shadow = FALSE; + + $AllIntegers = TRUE; + for($i=0;$i<=count($Points)-2;$i=$i+2) + { if ( $this->getFirstDecimal($Points[$i+1]) != 0 ) { $AllIntegers = FALSE; } } + + /* Convert polygon to segments */ + $Segments = ""; + for($i=2;$i<=count($Points)-2;$i=$i+2) + { $Segments[] = array("X1"=>$Points[$i-2],"Y1"=>$Points[$i-1],"X2"=>$Points[$i],"Y2"=>$Points[$i+1]); } + $Segments[] = array("X1"=>$Points[$i-2],"Y1"=>$Points[$i-1],"X2"=>$Points[0],"Y2"=>$Points[1]); + + /* Simplify straight lines */ + $Result = ""; $inHorizon = FALSE; $LastX = VOID; + foreach($Segments as $Key => $Pos) + { + if ( $Pos["Y1"] != $Pos["Y2"] ) + { + if ( $inHorizon ) { $inHorizon = FALSE; $Result[] = array("X1"=>$LastX,"Y1"=>$Pos["Y1"],"X2"=>$Pos["X1"],"Y2"=>$Pos["Y1"]); } + + $Result[] = array("X1"=>$Pos["X1"],"Y1"=>$Pos["Y1"],"X2"=>$Pos["X2"],"Y2"=>$Pos["Y2"]); + } + else { if ( !$inHorizon ) { $inHorizon = TRUE; $LastX = $Pos["X1"];} } + } + $Segments = $Result; + + /* Do we have something to draw */ + if ( $Segments == "" ) { return(0); } + + /* For segments debugging purpose */ + //foreach($Segments as $Key => $Pos) + // echo $Pos["X1"].",".$Pos["Y1"].",".$Pos["X2"].",".$Pos["Y2"]."\r\n"; + + /* Find out the min & max Y boundaries */ + $MinY = OUT_OF_SIGHT; $MaxY = OUT_OF_SIGHT; + foreach($Segments as $Key => $Coords) + { + if ( $MinY == OUT_OF_SIGHT || $MinY > min($Coords["Y1"],$Coords["Y2"]) ) { $MinY = min($Coords["Y1"],$Coords["Y2"]); } + if ( $MaxY == OUT_OF_SIGHT || $MaxY < max($Coords["Y1"],$Coords["Y2"]) ) { $MaxY = max($Coords["Y1"],$Coords["Y2"]); } + } + + if ( $AllIntegers ) { $YStep = 1; } else { $YStep = .5; } + + $MinY = floor($MinY); $MaxY = floor($MaxY); + + /* Scan each Y lines */ + $DefaultColor = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + $DebugLine = 0; $DebugColor = $this->allocateColor($this->Picture,255,0,0,100); + + $MinY = floor($MinY); $MaxY = floor($MaxY); $YStep = 1; + + if ( !$NoFill ) + { + //if ( $DebugLine ) { $MinY = $DebugLine; $MaxY = $DebugLine; } + for($Y=$MinY;$Y<=$MaxY;$Y=$Y+$YStep) + { + $Intersections = ""; $LastSlope = NULL; $RestoreLast = "-"; + foreach($Segments as $Key => $Coords) + { + $X1 = $Coords["X1"]; $X2 = $Coords["X2"]; $Y1 = $Coords["Y1"]; $Y2 = $Coords["Y2"]; + + if ( min($Y1,$Y2) <= $Y && max($Y1,$Y2) >= $Y ) + { + if ( $Y1 == $Y2 ) + { $X = $X1; } + else + { $X = $X1 + ( ($Y-$Y1)*$X2 - ($Y-$Y1)*$X1 ) / ($Y2-$Y1); } + + $X = floor($X); + + if ( $X2 == $X1 ) + { $Slope = "!"; } + else + { + $SlopeC = ($Y2 - $Y1) / ($X2 - $X1); + if( $SlopeC == 0 ) + { $Slope = "="; } + elseif( $SlopeC > 0 ) + { $Slope = "+"; } + elseif ( $SlopeC < 0 ) + { $Slope = "-"; } + } + + if ( !is_array($Intersections) ) + { $Intersections[] = $X; } + elseif( !in_array($X,$Intersections) ) + { $Intersections[] = $X; } + elseif( in_array($X,$Intersections) ) + { + if ($Y == $DebugLine) { echo $Slope."/".$LastSlope."(".$X.") "; } + + if ( $Slope == "=" && $LastSlope == "-" ) { $Intersections[] = $X; } + if ( $Slope != $LastSlope && $LastSlope != "!" && $LastSlope != "=" ) { $Intersections[] = $X; } + if ( $Slope != $LastSlope && $LastSlope == "!" && $Slope == "+" ) { $Intersections[] = $X; } + } + + if ( is_array($Intersections) && in_array($X,$Intersections) && $LastSlope == "=" && ($Slope == "-" )) { $Intersections[] = $X; } + + $LastSlope = $Slope; + } + } + if ( $RestoreLast != "-" ) { $Intersections[] = $RestoreLast; echo "@".$Y."\r\n"; } + + if ( is_array($Intersections) ) + { + sort($Intersections); + + if ($Y == $DebugLine) { print_r($Intersections); } + + /* Remove NULL plots */ + $Result = ""; + for($i=0;$i<=count($Intersections)-1;$i=$i+2) + { + if ( isset($Intersections[$i+1]) ) + { if ( $Intersections[$i] != $Intersections[$i+1] ) { $Result[] = $Intersections[$i]; $Result[] = $Intersections[$i+1]; } } + } + + if ( is_array($Result) ) + { + $Intersections = $Result; + + $LastX = OUT_OF_SIGHT; + foreach($Intersections as $Key => $X) + { + if ( $LastX == OUT_OF_SIGHT ) + $LastX = $X; + elseif ( $LastX != OUT_OF_SIGHT ) + { + if ( $this->getFirstDecimal($LastX) > 1 ) { $LastX++; } + + $Color = $DefaultColor; + if ( $Threshold != NULL ) + { + foreach($Threshold as $Key => $Parameters) + { + if ( $Y <= $Parameters["MinX"] && $Y >= $Parameters["MaxX"]) + { + if ( isset($Parameters["R"]) ) { $R = $Parameters["R"]; } else { $R = 0; } + if ( isset($Parameters["G"]) ) { $G = $Parameters["G"]; } else { $G = 0; } + if ( isset($Parameters["B"]) ) { $B = $Parameters["B"]; } else { $B = 0; } + if ( isset($Parameters["Alpha"]) ) { $Alpha = $Parameters["Alpha"]; } else { $Alpha = 100; } + $Color = $this->allocateColor($this->Picture,$R,$G,$B,$Alpha); + } + } + } + + imageline($this->Picture,$LastX,$Y,$X,$Y,$Color); + + if ( $Y == $DebugLine) { imageline($this->Picture,$LastX,$Y,$X,$Y,$DebugColor); } + + $LastX = OUT_OF_SIGHT; + } + } + } + } + } + } + + /* Draw the polygon border, if required */ + if ( !$NoBorder) + { + foreach($Segments as $Key => $Coords) + $this->drawLine($Coords["X1"],$Coords["Y1"],$Coords["X2"],$Coords["Y2"],array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha,"Threshold"=>$Threshold)); + } + + $this->Shadow = $RestoreShadow; + } + + /* Return the abscissa margin */ + function getAbscissaMargin($Data) + { + foreach($Data["Axis"] as $AxisID => $Values) { if ( $Values["Identity"] == AXIS_X ) { return($Values["Margin"]); } } + return(0); + } + + } ?> \ No newline at end of file diff --git a/vendor/pchart/pImage.class.php b/vendor/pchart/pImage.class.php index f0f97bae7..27b262e52 100755 --- a/vendor/pchart/pImage.class.php +++ b/vendor/pchart/pImage.class.php @@ -1,472 +1,472 @@ -TransparentBackground = $TransparentBackground; - - if ( $DataSet != NULL ) { $this->DataSet = $DataSet; } - - $this->XSize = $XSize; - $this->YSize = $YSize; - $this->Picture = imagecreatetruecolor($XSize,$YSize); - - if ( $this->TransparentBackground ) - { - imagealphablending($this->Picture,FALSE); - imagefilledrectangle($this->Picture, 0,0,$XSize, $YSize, imagecolorallocatealpha($this->Picture, 255, 255, 255, 127)); - imagealphablending($this->Picture,TRUE); - imagesavealpha($this->Picture,true); - } - else - { - $C_White = $this->AllocateColor($this->Picture,255,255,255); - imagefilledrectangle($this->Picture,0,0,$XSize,$YSize,$C_White); - } - } - - /* Enable / Disable and set shadow properties */ - function setShadow($Enabled=TRUE,$Format="") - { - $X = isset($Format["X"]) ? $Format["X"] : 2; - $Y = isset($Format["Y"]) ? $Format["Y"] : 2; - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 10; - - $this->Shadow = $Enabled; - $this->ShadowX = $X; - $this->ShadowY = $Y; - $this->ShadowR = $R; - $this->ShadowG = $G; - $this->ShadowB = $B; - $this->Shadowa = $Alpha; - } - - /* Set the graph area position */ - function setGraphArea($X1,$Y1,$X2,$Y2) - { - if ( $X2 < $X1 || $X1 == $X2 || $Y2 < $Y1 || $Y1 == $Y2 ) { return(-1); } - - $this->GraphAreaX1 = $X1; $this->DataSet->Data["GraphArea"]["X1"] = $X1; - $this->GraphAreaY1 = $Y1; $this->DataSet->Data["GraphArea"]["Y1"] = $Y1; - $this->GraphAreaX2 = $X2; $this->DataSet->Data["GraphArea"]["X2"] = $X2; - $this->GraphAreaY2 = $Y2; $this->DataSet->Data["GraphArea"]["Y2"] = $Y2; - } - - /* Return the width of the picture */ - function getWidth() - { return($this->XSize); } - - /* Return the heigth of the picture */ - function getHeight() - { return($this->YSize); } - - /* Render the picture to a file */ - function render($FileName) - { - if ( $this->TransparentBackground ) { imagealphablending($this->Picture,false); imagesavealpha($this->Picture,true); } - imagepng($this->Picture,$FileName); - } - - /* Render the picture to a web browser stream */ - function stroke($BrowserExpire=FALSE) - { - if ( $this->TransparentBackground ) { imagealphablending($this->Picture,false); imagesavealpha($this->Picture,true); } - - if ( $BrowserExpire ) - { - header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); - header("Cache-Control: no-cache"); - header("Pragma: no-cache"); - } - - header('Content-type: image/png'); - imagepng($this->Picture); - } - - /* Automatic output method based on the calling interface */ - function autoOutput($FileName="output.png") - { - if (php_sapi_name() == "cli") - $this->Render($FileName); - else - $this->Stroke(); - } - - /* Return the length between two points */ - function getLength($X1,$Y1,$X2,$Y2) - { return(sqrt(pow(max($X1,$X2)-min($X1,$X2),2)+pow(max($Y1,$Y2)-min($Y1,$Y2),2))); } - - /* Return the orientation of a line */ - function getAngle($X1,$Y1,$X2,$Y2) - { - $Opposite = $Y2 - $Y1; $Adjacent = $X2 - $X1;$Angle = rad2deg(atan2($Opposite,$Adjacent)); - if ($Angle > 0) { return($Angle); } else { return(360-abs($Angle)); } - } - - /* Return the surrounding box of text area */ - function getTextBox_deprecated($X,$Y,$FontName,$FontSize,$Angle,$Text) - { - $Size = imagettfbbox($FontSize,$Angle,$FontName,$Text); - $Width = $this->getLength($Size[0],$Size[1],$Size[2],$Size[3])+1; - $Height = $this->getLength($Size[2],$Size[3],$Size[4],$Size[5])+1; - - $RealPos[0]["X"] = $X; $RealPos[0]["Y"] = $Y; - $RealPos[1]["X"] = cos((360-$Angle)*PI/180)*$Width + $RealPos[0]["X"]; $RealPos[1]["Y"] = sin((360-$Angle)*PI/180)*$Width + $RealPos[0]["Y"]; - $RealPos[2]["X"] = cos((270-$Angle)*PI/180)*$Height + $RealPos[1]["X"]; $RealPos[2]["Y"] = sin((270-$Angle)*PI/180)*$Height + $RealPos[1]["Y"]; - $RealPos[3]["X"] = cos((180-$Angle)*PI/180)*$Width + $RealPos[2]["X"]; $RealPos[3]["Y"] = sin((180-$Angle)*PI/180)*$Width + $RealPos[2]["Y"]; - - $RealPos[TEXT_ALIGN_BOTTOMLEFT]["X"] = $RealPos[0]["X"]; $RealPos[TEXT_ALIGN_BOTTOMLEFT]["Y"] = $RealPos[0]["Y"]; - $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["X"] = $RealPos[1]["X"]; $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["Y"] = $RealPos[1]["Y"]; - - return($RealPos); - } - - /* Return the surrounding box of text area */ - function getTextBox($X,$Y,$FontName,$FontSize,$Angle,$Text) - { - $coords = imagettfbbox($FontSize, 0, $FontName, $Text); - - $a = deg2rad($Angle); $ca = cos($a); $sa = sin($a); $RealPos = array(); - for($i = 0; $i < 7; $i += 2) - { - $RealPos[$i/2]["X"] = $X + round($coords[$i] * $ca + $coords[$i+1] * $sa); - $RealPos[$i/2]["Y"] = $Y + round($coords[$i+1] * $ca - $coords[$i] * $sa); - } - - $RealPos[TEXT_ALIGN_BOTTOMLEFT]["X"] = $RealPos[0]["X"]; $RealPos[TEXT_ALIGN_BOTTOMLEFT]["Y"] = $RealPos[0]["Y"]; - $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["X"] = $RealPos[1]["X"]; $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["Y"] = $RealPos[1]["Y"]; - $RealPos[TEXT_ALIGN_TOPLEFT]["X"] = $RealPos[3]["X"]; $RealPos[TEXT_ALIGN_TOPLEFT]["Y"] = $RealPos[3]["Y"]; - $RealPos[TEXT_ALIGN_TOPRIGHT]["X"] = $RealPos[2]["X"]; $RealPos[TEXT_ALIGN_TOPRIGHT]["Y"] = $RealPos[2]["Y"]; - $RealPos[TEXT_ALIGN_BOTTOMMIDDLE]["X"] = ($RealPos[1]["X"]-$RealPos[0]["X"])/2+$RealPos[0]["X"]; $RealPos[TEXT_ALIGN_BOTTOMMIDDLE]["Y"] = ($RealPos[0]["Y"]-$RealPos[1]["Y"])/2+$RealPos[1]["Y"]; - $RealPos[TEXT_ALIGN_TOPMIDDLE]["X"] = ($RealPos[2]["X"]-$RealPos[3]["X"])/2+$RealPos[3]["X"]; $RealPos[TEXT_ALIGN_TOPMIDDLE]["Y"] = ($RealPos[3]["Y"]-$RealPos[2]["Y"])/2+$RealPos[2]["Y"]; - $RealPos[TEXT_ALIGN_MIDDLELEFT]["X"] = ($RealPos[0]["X"]-$RealPos[3]["X"])/2+$RealPos[3]["X"]; $RealPos[TEXT_ALIGN_MIDDLELEFT]["Y"] = ($RealPos[0]["Y"]-$RealPos[3]["Y"])/2+$RealPos[3]["Y"]; - $RealPos[TEXT_ALIGN_MIDDLERIGHT]["X"] = ($RealPos[1]["X"]-$RealPos[2]["X"])/2+$RealPos[2]["X"]; $RealPos[TEXT_ALIGN_MIDDLERIGHT]["Y"] = ($RealPos[1]["Y"]-$RealPos[2]["Y"])/2+$RealPos[2]["Y"]; - $RealPos[TEXT_ALIGN_MIDDLEMIDDLE]["X"] = ($RealPos[1]["X"]-$RealPos[3]["X"])/2+$RealPos[3]["X"]; $RealPos[TEXT_ALIGN_MIDDLEMIDDLE]["Y"] = ($RealPos[0]["Y"]-$RealPos[2]["Y"])/2+$RealPos[2]["Y"]; - - return($RealPos); - } - - /* Set current font properties */ - function setFontProperties($Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : -1; - $G = isset($Format["G"]) ? $Format["G"] : -1; - $B = isset($Format["B"]) ? $Format["B"] : -1; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $FontName = isset($Format["FontName"]) ? $Format["FontName"] : NULL; - $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : NULL; - - if ( $R != -1) { $this->FontColorR = $R; } - if ( $G != -1) { $this->FontColorG = $G; } - if ( $B != -1) { $this->FontColorB = $B; } - if ( $Alpha != NULL) { $this->FontColorA = $Alpha; } - - if ( $FontName != NULL ) - $this->FontName = $FontName; - - if ( $FontSize != NULL ) - $this->FontSize = $FontSize; - } - - /* Returns the 1st decimal values (used to correct AA bugs) */ - function getFirstDecimal($Value) - { - $Values = preg_split("/\./",$Value); - if ( isset($Values[1]) ) { return(substr($Values[1],0,1)); } else { return(0); } - } - - /* Attach a dataset to your pChart Object */ - function setDataSet(&$DataSet) - { $this->DataSet = $DataSet; } - - /* Print attached dataset contents to STDOUT */ - function printDataSet() - { print_r($this->DataSet); } - - /* Initialise the image map methods */ - function initialiseImageMap($Name="pChart",$StorageMode=IMAGE_MAP_STORAGE_SESSION,$UniqueID="imageMap",$StorageFolder="tmp") - { - $this->ImageMapIndex = $Name; - $this->ImageMapStorageMode = $StorageMode; - - if ($StorageMode == IMAGE_MAP_STORAGE_SESSION) - { - if(!isset($_SESSION)) { session_start(); } - $_SESSION[$this->ImageMapIndex] = NULL; - } - elseif($StorageMode == IMAGE_MAP_STORAGE_FILE) - { - $this->ImageMapFileName = $UniqueID; - $this->ImageMapStorageFolder = $StorageFolder; - - if (file_exists($StorageFolder."/".$UniqueID.".map")) { unlink($StorageFolder."/".$UniqueID.".map"); } - } - } - - /* Add a zone to the image map */ - function addToImageMap($Type,$Plots,$Color=NULL,$Title=NULL,$Message=NULL,$HTMLEncode=FALSE) - { - if ( $this->ImageMapStorageMode == NULL ) { $this->initialiseImageMap(); } - - /* Encode the characters in the imagemap in HTML standards */ - $Title = str_replace("€","\u20AC",$Title); - $Title = htmlentities($Title,ENT_QUOTES,"ISO-8859-15"); - if ( $HTMLEncode ) - { - $Message = htmlentities($Message,ENT_QUOTES,"ISO-8859-15"); - $Message = str_replace("<","<",$Message); - $Message = str_replace(">",">",$Message); - } - - if ( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION ) - { - if(!isset($_SESSION)) { $this->initialiseImageMap(); } - $_SESSION[$this->ImageMapIndex][] = array($Type,$Plots,$Color,$Title,$Message); - } - elseif($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE) - { - $Handle = fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", 'a'); - fwrite($Handle, $Type.IMAGE_MAP_DELIMITER.$Plots.IMAGE_MAP_DELIMITER.$Color.IMAGE_MAP_DELIMITER.$Title.IMAGE_MAP_DELIMITER.$Message."\r\n"); - fclose($Handle); - } - } - - /* Remove VOID values from an imagemap custom values array */ - function removeVOIDFromArray($SerieName, $Values) - { - if ( !isset($this->DataSet->Data["Series"][$SerieName]) ) { return(-1); } - - $Result = ""; - foreach($this->DataSet->Data["Series"][$SerieName]["Data"] as $Key => $Value) - { if ( $Value != VOID && isset($Values[$Key]) ) { $Result[] = $Values[$Key]; } } - return($Result); - } - - /* Replace the title of one image map serie */ - function replaceImageMapTitle($OldTitle, $NewTitle) - { - if ( $this->ImageMapStorageMode == NULL ) { return(-1); } - - if ( is_array($NewTitle) ) { $NewTitle = $this->removeVOIDFromArray($OldTitle, $NewTitle); } - - if ( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION ) - { - if(!isset($_SESSION)) { return(-1); } - if ( is_array($NewTitle) ) - { $ID = 0; foreach($_SESSION[$this->ImageMapIndex] as $Key => $Settings) { if ( $Settings[3] == $OldTitle && isset($NewTitle[$ID])) { $_SESSION[$this->ImageMapIndex][$Key][3] = $NewTitle[$ID]; $ID++; } } } - else - { foreach($_SESSION[$this->ImageMapIndex] as $Key => $Settings) { if ( $Settings[3] == $OldTitle ) { $_SESSION[$this->ImageMapIndex][$Key][3] = $NewTitle; } } } - } - elseif( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE ) - { - $TempArray = ""; - $Handle = @fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", "r"); - if ($Handle) - { - while (($Buffer = fgets($Handle, 4096)) !== false) - { - $Fields = split(IMAGE_MAP_DELIMITER,str_replace(array(chr(10),chr(13)),"",$Buffer)); - $TempArray[] = array($Fields[0],$Fields[1],$Fields[2],$Fields[3],$Fields[4]); - } - fclose($Handle); - - if ( is_array($NewTitle) ) - { $ID = 0; foreach($TempArray as $Key => $Settings) { if ( $Settings[3] == $OldTitle && isset($NewTitle[$ID]) ) { $TempArray[$Key][3] = $NewTitle[$ID]; $ID++; } } } - else - { foreach($TempArray as $Key => $Settings) { if ( $Settings[3] == $OldTitle ) { $TempArray[$Key][3] = $NewTitle; } } } - - $Handle = fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", 'w'); - foreach($TempArray as $Key => $Settings) - { fwrite($Handle, $Settings[0].IMAGE_MAP_DELIMITER.$Settings[1].IMAGE_MAP_DELIMITER.$Settings[2].IMAGE_MAP_DELIMITER.$Settings[3].IMAGE_MAP_DELIMITER.$Settings[4]."\r\n"); } - fclose($Handle); - } - } - } - - /* Replace the values of the image map contents */ - function replaceImageMapValues($Title, $Values) - { - if ( $this->ImageMapStorageMode == NULL ) { return(-1); } - - $Values = $this->removeVOIDFromArray($Title, $Values); - $ID = 0; - if ( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION ) - { - if(!isset($_SESSION)) { return(-1); } - foreach($_SESSION[$this->ImageMapIndex] as $Key => $Settings) { if ( $Settings[3] == $Title ) { if ( isset($Values[$ID]) ) { $_SESSION[$this->ImageMapIndex][$Key][4] = $Values[$ID]; } $ID++; } } - } - elseif( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE ) - { - $TempArray = ""; - $Handle = @fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", "r"); - if ($Handle) - { - while (($Buffer = fgets($Handle, 4096)) !== false) - { - $Fields = split(IMAGE_MAP_DELIMITER,str_replace(array(chr(10),chr(13)),"",$Buffer)); - $TempArray[] = array($Fields[0],$Fields[1],$Fields[2],$Fields[3],$Fields[4]); - } - fclose($Handle); - - foreach($TempArray as $Key => $Settings) { if ( $Settings[3] == $Title ) { if ( isset($Values[$ID]) ) { $TempArray[$Key][4] = $Values[$ID]; } $ID++; } } - - $Handle = fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", 'w'); - foreach($TempArray as $Key => $Settings) - { fwrite($Handle, $Settings[0].IMAGE_MAP_DELIMITER.$Settings[1].IMAGE_MAP_DELIMITER.$Settings[2].IMAGE_MAP_DELIMITER.$Settings[3].IMAGE_MAP_DELIMITER.$Settings[4]."\r\n"); } - fclose($Handle); - } - } - } - - /* Dump the image map */ - function dumpImageMap($Name="pChart",$StorageMode=IMAGE_MAP_STORAGE_SESSION,$UniqueID="imageMap",$StorageFolder="tmp") - { - $this->ImageMapIndex = $Name; - $this->ImageMapStorageMode = $StorageMode; - - if ( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION ) - { - if(!isset($_SESSION)) { session_start(); } - if ( $_SESSION[$Name] != NULL ) - { - foreach($_SESSION[$Name] as $Key => $Params) - { echo $Params[0].IMAGE_MAP_DELIMITER.$Params[1].IMAGE_MAP_DELIMITER.$Params[2].IMAGE_MAP_DELIMITER.$Params[3].IMAGE_MAP_DELIMITER.$Params[4]."\r\n"; } - } - } - elseif( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE ) - { - if (file_exists($StorageFolder."/".$UniqueID.".map")) - { - $Handle = @fopen($StorageFolder."/".$UniqueID.".map", "r"); - if ($Handle) { while (($Buffer = fgets($Handle, 4096)) !== false) { echo $Buffer; } } - fclose($Handle); - - if ( $this->ImageMapAutoDelete ) { unlink($StorageFolder."/".$UniqueID.".map"); } - } - } - - /* When the image map is returned to the client, the script ends */ - exit(); - } - - /* Return the HTML converted color from the RGB composite values */ - function toHTMLColor($R,$G,$B) - { - $R=intval($R); $G=intval($G); $B=intval($B); - $R=dechex($R<0?0:($R>255?255:$R)); $G=dechex($G<0?0:($G>255?255:$G));$B=dechex($B<0?0:($B>255?255:$B)); - $Color="#".(strlen($R) < 2?'0':'').$R; $Color.=(strlen($G) < 2?'0':'').$G; $Color.= (strlen($B) < 2?'0':'').$B; - return($Color); - } - - /* Reverse an array of points */ - function reversePlots($Plots) - { - $Result = ""; - for($i=count($Plots)-2;$i>=0;$i=$i-2) { $Result[] = $Plots[$i]; $Result[] = $Plots[$i+1]; } - return($Result); - } - - /* Mirror Effect */ - function drawAreaMirror($X,$Y,$Width,$Height,$Format="") - { - $StartAlpha = isset($Format["StartAlpha"]) ? $Format["StartAlpha"] : 80; - $EndAlpha = isset($Format["EndAlpha"]) ? $Format["EndAlpha"] : 0; - - $AlphaStep = ($StartAlpha-$EndAlpha)/$Height; - - $Picture = imagecreatetruecolor($this->XSize,$this->YSize); - imagecopy($Picture,$this->Picture,0,0,0,0,$this->XSize,$this->YSize); - - for($i=1;$i<=$Height;$i++) - { - if ( $Y+($i-1) < $this->YSize && $Y-$i > 0 ) { imagecopymerge($Picture,$this->Picture,$X,$Y+($i-1),$X,$Y-$i,$Width,1,$StartAlpha-$AlphaStep*$i); } - } - - imagecopy($this->Picture,$Picture,0,0,0,0,$this->XSize,$this->YSize); - } - } +TransparentBackground = $TransparentBackground; + + if ( $DataSet != NULL ) { $this->DataSet = $DataSet; } + + $this->XSize = $XSize; + $this->YSize = $YSize; + $this->Picture = imagecreatetruecolor($XSize,$YSize); + + if ( $this->TransparentBackground ) + { + imagealphablending($this->Picture,FALSE); + imagefilledrectangle($this->Picture, 0,0,$XSize, $YSize, imagecolorallocatealpha($this->Picture, 255, 255, 255, 127)); + imagealphablending($this->Picture,TRUE); + imagesavealpha($this->Picture,true); + } + else + { + $C_White = $this->AllocateColor($this->Picture,255,255,255); + imagefilledrectangle($this->Picture,0,0,$XSize,$YSize,$C_White); + } + } + + /* Enable / Disable and set shadow properties */ + function setShadow($Enabled=TRUE,$Format="") + { + $X = isset($Format["X"]) ? $Format["X"] : 2; + $Y = isset($Format["Y"]) ? $Format["Y"] : 2; + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 10; + + $this->Shadow = $Enabled; + $this->ShadowX = $X; + $this->ShadowY = $Y; + $this->ShadowR = $R; + $this->ShadowG = $G; + $this->ShadowB = $B; + $this->Shadowa = $Alpha; + } + + /* Set the graph area position */ + function setGraphArea($X1,$Y1,$X2,$Y2) + { + if ( $X2 < $X1 || $X1 == $X2 || $Y2 < $Y1 || $Y1 == $Y2 ) { return(-1); } + + $this->GraphAreaX1 = $X1; $this->DataSet->Data["GraphArea"]["X1"] = $X1; + $this->GraphAreaY1 = $Y1; $this->DataSet->Data["GraphArea"]["Y1"] = $Y1; + $this->GraphAreaX2 = $X2; $this->DataSet->Data["GraphArea"]["X2"] = $X2; + $this->GraphAreaY2 = $Y2; $this->DataSet->Data["GraphArea"]["Y2"] = $Y2; + } + + /* Return the width of the picture */ + function getWidth() + { return($this->XSize); } + + /* Return the heigth of the picture */ + function getHeight() + { return($this->YSize); } + + /* Render the picture to a file */ + function render($FileName) + { + if ( $this->TransparentBackground ) { imagealphablending($this->Picture,false); imagesavealpha($this->Picture,true); } + imagepng($this->Picture,$FileName); + } + + /* Render the picture to a web browser stream */ + function stroke($BrowserExpire=FALSE) + { + if ( $this->TransparentBackground ) { imagealphablending($this->Picture,false); imagesavealpha($this->Picture,true); } + + if ( $BrowserExpire ) + { + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); + header("Cache-Control: no-cache"); + header("Pragma: no-cache"); + } + + header('Content-type: image/png'); + imagepng($this->Picture); + } + + /* Automatic output method based on the calling interface */ + function autoOutput($FileName="output.png") + { + if (php_sapi_name() == "cli") + $this->Render($FileName); + else + $this->Stroke(); + } + + /* Return the length between two points */ + function getLength($X1,$Y1,$X2,$Y2) + { return(sqrt(pow(max($X1,$X2)-min($X1,$X2),2)+pow(max($Y1,$Y2)-min($Y1,$Y2),2))); } + + /* Return the orientation of a line */ + function getAngle($X1,$Y1,$X2,$Y2) + { + $Opposite = $Y2 - $Y1; $Adjacent = $X2 - $X1;$Angle = rad2deg(atan2($Opposite,$Adjacent)); + if ($Angle > 0) { return($Angle); } else { return(360-abs($Angle)); } + } + + /* Return the surrounding box of text area */ + function getTextBox_deprecated($X,$Y,$FontName,$FontSize,$Angle,$Text) + { + $Size = imagettfbbox($FontSize,$Angle,$FontName,$Text); + $Width = $this->getLength($Size[0],$Size[1],$Size[2],$Size[3])+1; + $Height = $this->getLength($Size[2],$Size[3],$Size[4],$Size[5])+1; + + $RealPos[0]["X"] = $X; $RealPos[0]["Y"] = $Y; + $RealPos[1]["X"] = cos((360-$Angle)*PI/180)*$Width + $RealPos[0]["X"]; $RealPos[1]["Y"] = sin((360-$Angle)*PI/180)*$Width + $RealPos[0]["Y"]; + $RealPos[2]["X"] = cos((270-$Angle)*PI/180)*$Height + $RealPos[1]["X"]; $RealPos[2]["Y"] = sin((270-$Angle)*PI/180)*$Height + $RealPos[1]["Y"]; + $RealPos[3]["X"] = cos((180-$Angle)*PI/180)*$Width + $RealPos[2]["X"]; $RealPos[3]["Y"] = sin((180-$Angle)*PI/180)*$Width + $RealPos[2]["Y"]; + + $RealPos[TEXT_ALIGN_BOTTOMLEFT]["X"] = $RealPos[0]["X"]; $RealPos[TEXT_ALIGN_BOTTOMLEFT]["Y"] = $RealPos[0]["Y"]; + $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["X"] = $RealPos[1]["X"]; $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["Y"] = $RealPos[1]["Y"]; + + return($RealPos); + } + + /* Return the surrounding box of text area */ + function getTextBox($X,$Y,$FontName,$FontSize,$Angle,$Text) + { + $coords = imagettfbbox($FontSize, 0, $FontName, $Text); + + $a = deg2rad($Angle); $ca = cos($a); $sa = sin($a); $RealPos = array(); + for($i = 0; $i < 7; $i += 2) + { + $RealPos[$i/2]["X"] = $X + round($coords[$i] * $ca + $coords[$i+1] * $sa); + $RealPos[$i/2]["Y"] = $Y + round($coords[$i+1] * $ca - $coords[$i] * $sa); + } + + $RealPos[TEXT_ALIGN_BOTTOMLEFT]["X"] = $RealPos[0]["X"]; $RealPos[TEXT_ALIGN_BOTTOMLEFT]["Y"] = $RealPos[0]["Y"]; + $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["X"] = $RealPos[1]["X"]; $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["Y"] = $RealPos[1]["Y"]; + $RealPos[TEXT_ALIGN_TOPLEFT]["X"] = $RealPos[3]["X"]; $RealPos[TEXT_ALIGN_TOPLEFT]["Y"] = $RealPos[3]["Y"]; + $RealPos[TEXT_ALIGN_TOPRIGHT]["X"] = $RealPos[2]["X"]; $RealPos[TEXT_ALIGN_TOPRIGHT]["Y"] = $RealPos[2]["Y"]; + $RealPos[TEXT_ALIGN_BOTTOMMIDDLE]["X"] = ($RealPos[1]["X"]-$RealPos[0]["X"])/2+$RealPos[0]["X"]; $RealPos[TEXT_ALIGN_BOTTOMMIDDLE]["Y"] = ($RealPos[0]["Y"]-$RealPos[1]["Y"])/2+$RealPos[1]["Y"]; + $RealPos[TEXT_ALIGN_TOPMIDDLE]["X"] = ($RealPos[2]["X"]-$RealPos[3]["X"])/2+$RealPos[3]["X"]; $RealPos[TEXT_ALIGN_TOPMIDDLE]["Y"] = ($RealPos[3]["Y"]-$RealPos[2]["Y"])/2+$RealPos[2]["Y"]; + $RealPos[TEXT_ALIGN_MIDDLELEFT]["X"] = ($RealPos[0]["X"]-$RealPos[3]["X"])/2+$RealPos[3]["X"]; $RealPos[TEXT_ALIGN_MIDDLELEFT]["Y"] = ($RealPos[0]["Y"]-$RealPos[3]["Y"])/2+$RealPos[3]["Y"]; + $RealPos[TEXT_ALIGN_MIDDLERIGHT]["X"] = ($RealPos[1]["X"]-$RealPos[2]["X"])/2+$RealPos[2]["X"]; $RealPos[TEXT_ALIGN_MIDDLERIGHT]["Y"] = ($RealPos[1]["Y"]-$RealPos[2]["Y"])/2+$RealPos[2]["Y"]; + $RealPos[TEXT_ALIGN_MIDDLEMIDDLE]["X"] = ($RealPos[1]["X"]-$RealPos[3]["X"])/2+$RealPos[3]["X"]; $RealPos[TEXT_ALIGN_MIDDLEMIDDLE]["Y"] = ($RealPos[0]["Y"]-$RealPos[2]["Y"])/2+$RealPos[2]["Y"]; + + return($RealPos); + } + + /* Set current font properties */ + function setFontProperties($Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : -1; + $G = isset($Format["G"]) ? $Format["G"] : -1; + $B = isset($Format["B"]) ? $Format["B"] : -1; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $FontName = isset($Format["FontName"]) ? $Format["FontName"] : NULL; + $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : NULL; + + if ( $R != -1) { $this->FontColorR = $R; } + if ( $G != -1) { $this->FontColorG = $G; } + if ( $B != -1) { $this->FontColorB = $B; } + if ( $Alpha != NULL) { $this->FontColorA = $Alpha; } + + if ( $FontName != NULL ) + $this->FontName = $FontName; + + if ( $FontSize != NULL ) + $this->FontSize = $FontSize; + } + + /* Returns the 1st decimal values (used to correct AA bugs) */ + function getFirstDecimal($Value) + { + $Values = preg_split("/\./",$Value); + if ( isset($Values[1]) ) { return(substr($Values[1],0,1)); } else { return(0); } + } + + /* Attach a dataset to your pChart Object */ + function setDataSet(&$DataSet) + { $this->DataSet = $DataSet; } + + /* Print attached dataset contents to STDOUT */ + function printDataSet() + { print_r($this->DataSet); } + + /* Initialise the image map methods */ + function initialiseImageMap($Name="pChart",$StorageMode=IMAGE_MAP_STORAGE_SESSION,$UniqueID="imageMap",$StorageFolder="tmp") + { + $this->ImageMapIndex = $Name; + $this->ImageMapStorageMode = $StorageMode; + + if ($StorageMode == IMAGE_MAP_STORAGE_SESSION) + { + if(!isset($_SESSION)) { session_start(); } + $_SESSION[$this->ImageMapIndex] = NULL; + } + elseif($StorageMode == IMAGE_MAP_STORAGE_FILE) + { + $this->ImageMapFileName = $UniqueID; + $this->ImageMapStorageFolder = $StorageFolder; + + if (file_exists($StorageFolder."/".$UniqueID.".map")) { unlink($StorageFolder."/".$UniqueID.".map"); } + } + } + + /* Add a zone to the image map */ + function addToImageMap($Type,$Plots,$Color=NULL,$Title=NULL,$Message=NULL,$HTMLEncode=FALSE) + { + if ( $this->ImageMapStorageMode == NULL ) { $this->initialiseImageMap(); } + + /* Encode the characters in the imagemap in HTML standards */ + $Title = str_replace("€","\u20AC",$Title); + $Title = htmlentities($Title,ENT_QUOTES,"ISO-8859-15"); + if ( $HTMLEncode ) + { + $Message = htmlentities($Message,ENT_QUOTES,"ISO-8859-15"); + $Message = str_replace("<","<",$Message); + $Message = str_replace(">",">",$Message); + } + + if ( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION ) + { + if(!isset($_SESSION)) { $this->initialiseImageMap(); } + $_SESSION[$this->ImageMapIndex][] = array($Type,$Plots,$Color,$Title,$Message); + } + elseif($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE) + { + $Handle = fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", 'a'); + fwrite($Handle, $Type.IMAGE_MAP_DELIMITER.$Plots.IMAGE_MAP_DELIMITER.$Color.IMAGE_MAP_DELIMITER.$Title.IMAGE_MAP_DELIMITER.$Message."\r\n"); + fclose($Handle); + } + } + + /* Remove VOID values from an imagemap custom values array */ + function removeVOIDFromArray($SerieName, $Values) + { + if ( !isset($this->DataSet->Data["Series"][$SerieName]) ) { return(-1); } + + $Result = ""; + foreach($this->DataSet->Data["Series"][$SerieName]["Data"] as $Key => $Value) + { if ( $Value != VOID && isset($Values[$Key]) ) { $Result[] = $Values[$Key]; } } + return($Result); + } + + /* Replace the title of one image map serie */ + function replaceImageMapTitle($OldTitle, $NewTitle) + { + if ( $this->ImageMapStorageMode == NULL ) { return(-1); } + + if ( is_array($NewTitle) ) { $NewTitle = $this->removeVOIDFromArray($OldTitle, $NewTitle); } + + if ( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION ) + { + if(!isset($_SESSION)) { return(-1); } + if ( is_array($NewTitle) ) + { $ID = 0; foreach($_SESSION[$this->ImageMapIndex] as $Key => $Settings) { if ( $Settings[3] == $OldTitle && isset($NewTitle[$ID])) { $_SESSION[$this->ImageMapIndex][$Key][3] = $NewTitle[$ID]; $ID++; } } } + else + { foreach($_SESSION[$this->ImageMapIndex] as $Key => $Settings) { if ( $Settings[3] == $OldTitle ) { $_SESSION[$this->ImageMapIndex][$Key][3] = $NewTitle; } } } + } + elseif( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE ) + { + $TempArray = ""; + $Handle = @fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", "r"); + if ($Handle) + { + while (($Buffer = fgets($Handle, 4096)) !== false) + { + $Fields = split(IMAGE_MAP_DELIMITER,str_replace(array(chr(10),chr(13)),"",$Buffer)); + $TempArray[] = array($Fields[0],$Fields[1],$Fields[2],$Fields[3],$Fields[4]); + } + fclose($Handle); + + if ( is_array($NewTitle) ) + { $ID = 0; foreach($TempArray as $Key => $Settings) { if ( $Settings[3] == $OldTitle && isset($NewTitle[$ID]) ) { $TempArray[$Key][3] = $NewTitle[$ID]; $ID++; } } } + else + { foreach($TempArray as $Key => $Settings) { if ( $Settings[3] == $OldTitle ) { $TempArray[$Key][3] = $NewTitle; } } } + + $Handle = fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", 'w'); + foreach($TempArray as $Key => $Settings) + { fwrite($Handle, $Settings[0].IMAGE_MAP_DELIMITER.$Settings[1].IMAGE_MAP_DELIMITER.$Settings[2].IMAGE_MAP_DELIMITER.$Settings[3].IMAGE_MAP_DELIMITER.$Settings[4]."\r\n"); } + fclose($Handle); + } + } + } + + /* Replace the values of the image map contents */ + function replaceImageMapValues($Title, $Values) + { + if ( $this->ImageMapStorageMode == NULL ) { return(-1); } + + $Values = $this->removeVOIDFromArray($Title, $Values); + $ID = 0; + if ( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION ) + { + if(!isset($_SESSION)) { return(-1); } + foreach($_SESSION[$this->ImageMapIndex] as $Key => $Settings) { if ( $Settings[3] == $Title ) { if ( isset($Values[$ID]) ) { $_SESSION[$this->ImageMapIndex][$Key][4] = $Values[$ID]; } $ID++; } } + } + elseif( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE ) + { + $TempArray = ""; + $Handle = @fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", "r"); + if ($Handle) + { + while (($Buffer = fgets($Handle, 4096)) !== false) + { + $Fields = split(IMAGE_MAP_DELIMITER,str_replace(array(chr(10),chr(13)),"",$Buffer)); + $TempArray[] = array($Fields[0],$Fields[1],$Fields[2],$Fields[3],$Fields[4]); + } + fclose($Handle); + + foreach($TempArray as $Key => $Settings) { if ( $Settings[3] == $Title ) { if ( isset($Values[$ID]) ) { $TempArray[$Key][4] = $Values[$ID]; } $ID++; } } + + $Handle = fopen($this->ImageMapStorageFolder."/".$this->ImageMapFileName.".map", 'w'); + foreach($TempArray as $Key => $Settings) + { fwrite($Handle, $Settings[0].IMAGE_MAP_DELIMITER.$Settings[1].IMAGE_MAP_DELIMITER.$Settings[2].IMAGE_MAP_DELIMITER.$Settings[3].IMAGE_MAP_DELIMITER.$Settings[4]."\r\n"); } + fclose($Handle); + } + } + } + + /* Dump the image map */ + function dumpImageMap($Name="pChart",$StorageMode=IMAGE_MAP_STORAGE_SESSION,$UniqueID="imageMap",$StorageFolder="tmp") + { + $this->ImageMapIndex = $Name; + $this->ImageMapStorageMode = $StorageMode; + + if ( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION ) + { + if(!isset($_SESSION)) { session_start(); } + if ( $_SESSION[$Name] != NULL ) + { + foreach($_SESSION[$Name] as $Key => $Params) + { echo $Params[0].IMAGE_MAP_DELIMITER.$Params[1].IMAGE_MAP_DELIMITER.$Params[2].IMAGE_MAP_DELIMITER.$Params[3].IMAGE_MAP_DELIMITER.$Params[4]."\r\n"; } + } + } + elseif( $this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE ) + { + if (file_exists($StorageFolder."/".$UniqueID.".map")) + { + $Handle = @fopen($StorageFolder."/".$UniqueID.".map", "r"); + if ($Handle) { while (($Buffer = fgets($Handle, 4096)) !== false) { echo $Buffer; } } + fclose($Handle); + + if ( $this->ImageMapAutoDelete ) { unlink($StorageFolder."/".$UniqueID.".map"); } + } + } + + /* When the image map is returned to the client, the script ends */ + exit(); + } + + /* Return the HTML converted color from the RGB composite values */ + function toHTMLColor($R,$G,$B) + { + $R=intval($R); $G=intval($G); $B=intval($B); + $R=dechex($R<0?0:($R>255?255:$R)); $G=dechex($G<0?0:($G>255?255:$G));$B=dechex($B<0?0:($B>255?255:$B)); + $Color="#".(strlen($R) < 2?'0':'').$R; $Color.=(strlen($G) < 2?'0':'').$G; $Color.= (strlen($B) < 2?'0':'').$B; + return($Color); + } + + /* Reverse an array of points */ + function reversePlots($Plots) + { + $Result = ""; + for($i=count($Plots)-2;$i>=0;$i=$i-2) { $Result[] = $Plots[$i]; $Result[] = $Plots[$i+1]; } + return($Result); + } + + /* Mirror Effect */ + function drawAreaMirror($X,$Y,$Width,$Height,$Format="") + { + $StartAlpha = isset($Format["StartAlpha"]) ? $Format["StartAlpha"] : 80; + $EndAlpha = isset($Format["EndAlpha"]) ? $Format["EndAlpha"] : 0; + + $AlphaStep = ($StartAlpha-$EndAlpha)/$Height; + + $Picture = imagecreatetruecolor($this->XSize,$this->YSize); + imagecopy($Picture,$this->Picture,0,0,0,0,$this->XSize,$this->YSize); + + for($i=1;$i<=$Height;$i++) + { + if ( $Y+($i-1) < $this->YSize && $Y-$i > 0 ) { imagecopymerge($Picture,$this->Picture,$X,$Y+($i-1),$X,$Y-$i,$Width,1,$StartAlpha-$AlphaStep*$i); } + } + + imagecopy($this->Picture,$Picture,0,0,0,0,$this->XSize,$this->YSize); + } + } ?> \ No newline at end of file diff --git a/vendor/pchart/pPie.class.php b/vendor/pchart/pPie.class.php index 30d2d3daa..06db3cf28 100755 --- a/vendor/pchart/pPie.class.php +++ b/vendor/pchart/pPie.class.php @@ -1,1500 +1,1500 @@ -pChartObject = $Object; - - /* Cache the pData object reference */ - $this->pDataObject = $pDataObject; - } - - /* Draw a pie chart */ - function draw2DPie($X,$Y,$Format="") - { - $Radius = isset($Format["Radius"]) ? $Format["Radius"] : 60; - $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0; - $DataGapAngle = isset($Format["DataGapAngle"]) ? $Format["DataGapAngle"] : 0; - $DataGapRadius = isset($Format["DataGapRadius"]) ? $Format["DataGapRadius"] : 0; - $SecondPass = isset($Format["SecondPass"]) ? $Format["SecondPass"] : TRUE; - $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 255; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 255; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 255; - $Shadow = isset($Format["Shadow"]) ? $Format["Shadow"] : FALSE; - $DrawLabels = isset($Format["DrawLabels"]) ? $Format["DrawLabels"] : FALSE; - $LabelStacked = isset($Format["LabelStacked"]) ? $Format["LabelStacked"] : FALSE; - $LabelColor = isset($Format["LabelColor"]) ? $Format["LabelColor"] : PIE_LABEL_COLOR_MANUAL; - $LabelR = isset($Format["LabelR"]) ? $Format["LabelR"] : 0; - $LabelG = isset($Format["LabelG"]) ? $Format["LabelG"] : 0; - $LabelB = isset($Format["LabelB"]) ? $Format["LabelB"] : 0; - $LabelAlpha = isset($Format["LabelAlpha"]) ? $Format["LabelAlpha"] : 100; - $WriteValues = isset($Format["WriteValues"]) ? $Format["WriteValues"] : NULL; - $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_OUTSIDE; - $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : 15; - $ValueSuffix = isset($Format["ValueSuffix"]) ? $Format["ValueSuffix"] : ""; - $ValueR = isset($Format["ValueR"]) ? $Format["ValueR"] : 255; - $ValueG = isset($Format["ValueG"]) ? $Format["ValueG"] : 255; - $ValueB = isset($Format["ValueB"]) ? $Format["ValueB"] : 255; - $ValueAlpha = isset($Format["ValueAlpha"]) ? $Format["ValueAlpha"] : 100; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - - /* Data Processing */ - $Data = $this->pDataObject->getData(); - $Palette = $this->pDataObject->getPalette(); - - /* Do we have an abscissa serie defined? */ - if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } - - /* Try to find the data serie */ - $DataSerie = ""; - foreach ($Data["Series"] as $SerieName => $SerieData) - { if ( $SerieName != $Data["Abscissa"]) { $DataSerie = $SerieName; } } - - /* Do we have data to compute? */ - if ( $DataSerie == "" ) { return(PIE_NO_DATASERIE); } - - /* Remove unused data */ - list($Data,$Palette) = $this->clean0Values($Data,$Palette,$DataSerie,$Data["Abscissa"]); - - /* Compute the pie sum */ - $SerieSum = $this->pDataObject->getSum($DataSerie); - - /* Do we have data to draw? */ - if ( $SerieSum == 0 ) { return(PIE_SUMISNULL); } - - /* Dump the real number of data to draw */ - $Values = ""; - foreach ($Data["Series"][$DataSerie]["Data"] as $Key => $Value) - { if ($Value != 0) { $Values[] = $Value; } } - - /* Compute the wasted angular space between series */ - if (count($Values)==1) { $WastedAngular = 0; } else { $WastedAngular = count($Values) * $DataGapAngle; } - - /* Compute the scale */ - $ScaleFactor = (360 - $WastedAngular) / $SerieSum; - - $RestoreShadow = $this->pChartObject->Shadow; - if ( $this->pChartObject->Shadow ) - { - $this->pChartObject->Shadow = FALSE; - - $ShadowFormat = $Format; $ShadowFormat["Shadow"] = TRUE; - $this->draw2DPie($X+$this->pChartObject->ShadowX,$Y+$this->pChartObject->ShadowY,$ShadowFormat); - } - - /* Draw the polygon pie elements */ - $Step = 360 / (2 * PI * $Radius); - $Offset = 0; $ID = 0; - foreach($Values as $Key => $Value) - { - if ( $Shadow ) - $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa); - else - { - if ( !isset($Palette[$ID]["R"]) ) { $Color = $this->pChartObject->getRandomColor(); $Palette[$ID] = $Color; $this->pDataObject->savePalette($ID,$Color); } - $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); - } - - if ( !$SecondPass && !$Shadow ) - { - if ( !$Border ) - $Settings["Surrounding"] = 10; - else - { $Settings["BorderR"] = $BorderR; $Settings["BorderG"] = $BorderG; $Settings["BorderB"] = $BorderB; } - } - - $Plots = ""; - $EndAngle = $Offset+($Value*$ScaleFactor); if ( $EndAngle > 360 ) { $EndAngle = 360; } - - $Angle = ($EndAngle - $Offset)/2 + $Offset; - if ($DataGapAngle == 0) - { $X0 = $X; $Y0 = $Y; } - else - { - $X0 = cos(($Angle-90)*PI/180) * $DataGapRadius + $X; - $Y0 = sin(($Angle-90)*PI/180) * $DataGapRadius + $Y; - } - - $Plots[] = $X0; $Plots[] = $Y0; - - - for($i=$Offset;$i<=$EndAngle;$i=$i+$Step) - { - $Xc = cos(($i-90)*PI/180) * $Radius + $X; - $Yc = sin(($i-90)*PI/180) * $Radius + $Y; - - if ( $SecondPass && ( $i<90 )) { $Yc++; } - if ( $SecondPass && ( $i>180 && $i<270 )) { $Xc++; } - if ( $SecondPass && ( $i>=270 )) { $Xc++; $Yc++; } - - $Plots[] = $Xc; $Plots[] = $Yc; - } - - $this->pChartObject->drawPolygon($Plots,$Settings); - if ( $RecordImageMap && !$Shadow ) { $this->pChartObject->addToImageMap("POLY",$this->arraySerialize($Plots),$this->pChartObject->toHTMLColor($Palette[$ID]["R"],$Palette[$ID]["G"],$Palette[$ID]["B"]),$Data["Series"][$Data["Abscissa"]]["Data"][$Key],$Value); } - - if ( $DrawLabels && !$Shadow && !$SecondPass ) - { - if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) - { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} - else - { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } - - $Angle = ($EndAngle - $Offset)/2 + $Offset; - $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; - $Yc = sin(($Angle-90)*PI/180) * $Radius + $Y; - - $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key]; - - if ( $LabelStacked ) - $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,TRUE,$X,$Y,$Radius); - else - $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,FALSE); - } - - $Offset = $i + $DataGapAngle; $ID++; - } - - /* Second pass to smooth the angles */ - if ( $SecondPass ) - { - $Step = 360 / (2 * PI * $Radius); - $Offset = 0; $ID = 0; - foreach($Values as $Key => $Value) - { - $FirstPoint = TRUE; - if ( $Shadow ) - $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa); - else - { - if ( $Border ) - $Settings = array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB); - else - $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); - } - - $EndAngle = $Offset+($Value*$ScaleFactor); if ( $EndAngle > 360 ) { $EndAngle = 360; } - - if ($DataGapAngle == 0) - { $X0 = $X; $Y0 = $Y; } - else - { - $Angle = ($EndAngle - $Offset)/2 + $Offset; - $X0 = cos(($Angle-90)*PI/180) * $DataGapRadius + $X; - $Y0 = sin(($Angle-90)*PI/180) * $DataGapRadius + $Y; - } - $Plots[] = $X0; $Plots[] = $Y0; - - for($i=$Offset;$i<=$EndAngle;$i=$i+$Step) - { - $Xc = cos(($i-90)*PI/180) * $Radius + $X; - $Yc = sin(($i-90)*PI/180) * $Radius + $Y; - - if ( $FirstPoint ) { $this->pChartObject->drawLine($Xc,$Yc,$X0,$Y0,$Settings); } { $FirstPoint = FALSE; } - - $this->pChartObject->drawAntialiasPixel($Xc,$Yc,$Settings); - } - $this->pChartObject->drawLine($Xc,$Yc,$X0,$Y0,$Settings); - - if ( $DrawLabels && !$Shadow ) - { - if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) - { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} - else - { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } - - $Angle = ($EndAngle - $Offset)/2 + $Offset; - $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; - $Yc = sin(($Angle-90)*PI/180) * $Radius + $Y; - - $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key]; - - if ( $LabelStacked ) - $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,TRUE,$X,$Y,$Radius); - else - $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,FALSE); - } - - $Offset = $i + $DataGapAngle; $ID++; - } - } - - if ( $WriteValues != NULL && !$Shadow ) - { - $Step = 360 / (2 * PI * $Radius); - $Offset = 0; $ID = count($Values)-1; - $Settings = array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"R"=>$ValueR,"G"=>$ValueG,"B"=>$ValueB,"Alpha"=>$ValueAlpha); - foreach($Values as $Key => $Value) - { - $EndAngle = ($Value*$ScaleFactor) + $Offset; if ( $EndAngle > 360 ) { $EndAngle = 0; } - $Angle = ($EndAngle - $Offset)/2 + $Offset; - - if ( $ValuePosition == PIE_VALUE_OUTSIDE ) - { - $Xc = cos(($Angle-90)*PI/180) * ($Radius+$ValuePadding) + $X; - $Yc = sin(($Angle-90)*PI/180) * ($Radius+$ValuePadding) + $Y; - } - else - { - $Xc = cos(($Angle-90)*PI/180) * ($Radius)/2 + $X; - $Yc = sin(($Angle-90)*PI/180) * ($Radius)/2 + $Y; - } - - if ( $WriteValues == PIE_VALUE_PERCENTAGE ) - $Display = round(( 100 / $SerieSum ) * $Value,$Precision)."%"; - elseif ( $WriteValues == PIE_VALUE_NATURAL ) - $Display = $Value.$ValueSuffix; - - $this->pChartObject->drawText($Xc,$Yc,$Display,$Settings); - - $Offset = $EndAngle + $DataGapAngle; $ID--; - } - } - - if ( $DrawLabels && $LabelStacked ) { $this->writeShiftedLabels(); } - - $this->pChartObject->Shadow = $RestoreShadow; - - return(PIE_RENDERED); - } - - /* Draw a 3D pie chart */ - function draw3DPie($X,$Y,$Format="") - { - /* Rendering layout */ - $Radius = isset($Format["Radius"]) ? $Format["Radius"] : 80; - $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0; - $SkewFactor = isset($Format["SkewFactor"]) ? $Format["SkewFactor"] : .5; - $SliceHeight = isset($Format["SliceHeight"]) ? $Format["SliceHeight"] : 20; - $DataGapAngle = isset($Format["DataGapAngle"]) ? $Format["DataGapAngle"] : 0; - $DataGapRadius = isset($Format["DataGapRadius"]) ? $Format["DataGapRadius"] : 0; - $SecondPass = isset($Format["SecondPass"]) ? $Format["SecondPass"] : TRUE; - $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE; - $Shadow = isset($Format["Shadow"]) ? $Format["Shadow"] : FALSE; - $DrawLabels = isset($Format["DrawLabels"]) ? $Format["DrawLabels"] : FALSE; - $LabelStacked = isset($Format["LabelStacked"]) ? $Format["LabelStacked"] : FALSE; - $LabelColor = isset($Format["LabelColor"]) ? $Format["LabelColor"] : PIE_LABEL_COLOR_MANUAL; - $LabelR = isset($Format["LabelR"]) ? $Format["LabelR"] : 0; - $LabelG = isset($Format["LabelG"]) ? $Format["LabelG"] : 0; - $LabelB = isset($Format["LabelB"]) ? $Format["LabelB"] : 0; - $LabelAlpha = isset($Format["LabelAlpha"]) ? $Format["LabelAlpha"] : 100; - $WriteValues = isset($Format["WriteValues"]) ? $Format["WriteValues"] : NULL; //PIE_VALUE_PERCENTAGE - $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_INSIDE; - $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : 15; - $ValueSuffix = isset($Format["ValueSuffix"]) ? $Format["ValueSuffix"] : ""; - $ValueR = isset($Format["ValueR"]) ? $Format["ValueR"] : 255; - $ValueG = isset($Format["ValueG"]) ? $Format["ValueG"] : 255; - $ValueB = isset($Format["ValueB"]) ? $Format["ValueB"] : 255; - $ValueAlpha = isset($Format["ValueAlpha"]) ? $Format["ValueAlpha"] : 100; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - - /* Error correction for overlaying rounded corners */ - if ( $SkewFactor < .5 ) { $SkewFactor = .5; } - - /* Data Processing */ - $Data = $this->pDataObject->getData(); - $Palette = $this->pDataObject->getPalette(); - - /* Do we have an abscissa serie defined? */ - if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } - - /* Try to find the data serie */ - $DataSerie = ""; - foreach ($Data["Series"] as $SerieName => $SerieData) - { if ( $SerieName != $Data["Abscissa"]) { $DataSerie = $SerieName; } } - - /* Do we have data to compute? */ - if ( $DataSerie == "" ) { return(PIE_NO_DATASERIE); } - - /* Remove unused data */ - list($Data,$Palette) = $this->clean0Values($Data,$Palette,$DataSerie,$Data["Abscissa"]); - - /* Compute the pie sum */ - $SerieSum = $this->pDataObject->getSum($DataSerie); - - /* Do we have data to draw? */ - if ( $SerieSum == 0 ) { return(PIE_SUMISNULL); } - - /* Dump the real number of data to draw */ - $Values = ""; - foreach ($Data["Series"][$DataSerie]["Data"] as $Key => $Value) - { if ($Value != 0) { $Values[] = $Value; } } - - /* Compute the wasted angular space between series */ - if (count($Values)==1) { $WastedAngular = 0; } else { $WastedAngular = count($Values) * $DataGapAngle; } - - /* Compute the scale */ - $ScaleFactor = (360 - $WastedAngular) / $SerieSum; - - $RestoreShadow = $this->pChartObject->Shadow; - if ( $this->pChartObject->Shadow ) { $this->pChartObject->Shadow = FALSE; } - - /* Draw the polygon pie elements */ - $Step = 360 / (2 * PI * $Radius); - $Offset = 360; $ID = count($Values)-1; - $Values = array_reverse($Values); - $Slice = 0; $Slices = ""; $SliceColors = ""; $Visible = ""; $SliceAngle = ""; - foreach($Values as $Key => $Value) - { - if ( !isset($Palette[$ID]["R"]) ) { $Color = $this->pChartObject->getRandomColor(); $Palette[$ID] = $Color; $this->pDataObject->savePalette($ID,$Color); } - $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); - - $SliceColors[$Slice] = $Settings; - - $StartAngle = $Offset; - $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } - - if ( $StartAngle > 180 ) { $Visible[$Slice]["Start"] = TRUE; } else { $Visible[$Slice]["Start"] = TRUE; } - if ( $EndAngle < 180 ) { $Visible[$Slice]["End"] = FALSE; } else { $Visible[$Slice]["End"] = TRUE; } - - if ($DataGapAngle == 0) - { $X0 = $X; $Y0 = $Y; } - else - { - $Angle = ($EndAngle - $Offset)/2 + $Offset; - $X0 = cos(($Angle-90)*PI/180) * $DataGapRadius + $X; - $Y0 = sin(($Angle-90)*PI/180) * $DataGapRadius*$SkewFactor + $Y; - } - $Slices[$Slice][] = $X0; $Slices[$Slice][] = $Y0; $SliceAngle[$Slice][] = 0; - - for($i=$Offset;$i>=$EndAngle;$i=$i-$Step) - { - $Xc = cos(($i-90)*PI/180) * $Radius + $X; - $Yc = sin(($i-90)*PI/180) * $Radius*$SkewFactor + $Y; - - if ( ($SecondPass || $RestoreShadow ) && ( $i<90 )) { $Yc++; } - if ( ($SecondPass || $RestoreShadow ) && ( $i>90 && $i<180 )) { $Xc++; } - if ( ($SecondPass || $RestoreShadow ) && ( $i>180 && $i<270 )) { $Xc++; } - if ( ($SecondPass || $RestoreShadow ) && ( $i>=270 )) { $Xc++; $Yc++; } - - $Slices[$Slice][] = $Xc; $Slices[$Slice][] = $Yc; $SliceAngle[$Slice][] = $i; - } - - $Offset = $i - $DataGapAngle; $ID--; $Slice++; - } - - /* Draw the bottom shadow if needed */ - if ( $RestoreShadow && ($this->pChartObject->ShadowX != 0 || $this->pChartObject->ShadowY !=0 )) - { - foreach($Slices as $SliceID => $Plots) - { - $ShadowPie = ""; - for($i=0;$ipChartObject->ShadowX; $ShadowPie[] = $Plots[$i+1]+$this->pChartObject->ShadowY; } - - $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa,"NoBorder"=>TRUE); - $this->pChartObject->drawPolygon($ShadowPie,$Settings); - } - - $Step = 360 / (2 * PI * $Radius); - $Offset = 360; - foreach($Values as $Key => $Value) - { - $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } - - for($i=$Offset;$i>=$EndAngle;$i=$i-$Step) - { - $Xc = cos(($i-90)*PI/180) * $Radius + $X + $this->pChartObject->ShadowX; - $Yc = sin(($i-90)*PI/180) * $Radius*$SkewFactor + $Y + $this->pChartObject->ShadowY; - - $this->pChartObject->drawAntialiasPixel($Xc,$Yc,$Settings); - } - - $Offset = $i - $DataGapAngle; $ID--; - } - } - - /* Draw the bottom pie splice */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; - $this->pChartObject->drawPolygon($Plots,$Settings); - - if ( $SecondPass ) - { - $Settings = $SliceColors[$SliceID]; - if ( $Border ) - { $Settings["R"]+= 30; $Settings["G"]+= 30; $Settings["B"]+= 30;; } - - if ( isset($SliceAngle[$SliceID][1]) ) /* Empty error handling */ - { - $Angle = $SliceAngle[$SliceID][1]; - $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; - $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y; - $this->pChartObject->drawLine($Plots[0],$Plots[1],$Xc,$Yc,$Settings); - - $Angle = $SliceAngle[$SliceID][count($SliceAngle[$SliceID])-1]; - $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; - $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y; - $this->pChartObject->drawLine($Plots[0],$Plots[1],$Xc,$Yc,$Settings); - } - } - } - - /* Draw the two vertical edges */ - $Slices = array_reverse($Slices); - $SliceColors = array_reverse($SliceColors); - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; - $Settings["R"]+= 10; $Settings["G"]+= 10; $Settings["B"]+= 10; $Settings["NoBorder"] = TRUE; - - if ( $Visible[$SliceID]["Start"] && isset($Plots[2])) /* Empty error handling */ - { - $this->pChartObject->drawLine($Plots[2],$Plots[3],$Plots[2],$Plots[3]- $SliceHeight,array("R"=>$Settings["R"],"G"=>$Settings["G"],"B"=>$Settings["B"])); - $Border = ""; - $Border[] = $Plots[0]; $Border[] = $Plots[1]; $Border[] = $Plots[0]; $Border[] = $Plots[1] - $SliceHeight; - $Border[] = $Plots[2]; $Border[] = $Plots[3] - $SliceHeight; $Border[] = $Plots[2]; $Border[] = $Plots[3]; - $this->pChartObject->drawPolygon($Border,$Settings); - } - } - - $Slices = array_reverse($Slices); - $SliceColors = array_reverse($SliceColors); - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; - $Settings["R"]+= 10; $Settings["G"]+= 10; $Settings["B"]+= 10; $Settings["NoBorder"] = TRUE; - if ( $Visible[$SliceID]["End"] ) - { - $this->pChartObject->drawLine($Plots[count($Plots)-2],$Plots[count($Plots)-1],$Plots[count($Plots)-2],$Plots[count($Plots)-1]- $SliceHeight,array("R"=>$Settings["R"],"G"=>$Settings["G"],"B"=>$Settings["B"])); - - $Border = ""; - $Border[] = $Plots[0]; $Border[] = $Plots[1]; $Border[] = $Plots[0]; $Border[] = $Plots[1] - $SliceHeight; - $Border[] = $Plots[count($Plots)-2]; $Border[] = $Plots[count($Plots)-1] - $SliceHeight; $Border[] = $Plots[count($Plots)-2]; $Border[] = $Plots[count($Plots)-1]; - $this->pChartObject->drawPolygon($Border,$Settings); - } - } - - /* Draw the rounded edges */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; - $Settings["R"]+= 10; $Settings["G"]+= 10; $Settings["B"]+= 10; $Settings["NoBorder"] = TRUE; - - for ($j=2;$j 90 ) - { - $Border = ""; - $Border[] = $Plots[$j]; $Border[] = $Plots[$j+1]; - $Border[] = $Plots[$j+2]; $Border[] = $Plots[$j+3]; - $Border[] = $Plots[$j+2]; $Border[] = $Plots[$j+3] - $SliceHeight; - $Border[] = $Plots[$j]; $Border[] = $Plots[$j+1] - $SliceHeight; - $this->pChartObject->drawPolygon($Border,$Settings); - } - } - - if ( $SecondPass ) - { - $Settings = $SliceColors[$SliceID]; - if ( $Border ) - { $Settings["R"]+= 30; $Settings["G"]+= 30; $Settings["B"]+= 30; } - - if ( isset($SliceAngle[$SliceID][1]) ) /* Empty error handling */ - { - $Angle = $SliceAngle[$SliceID][1]; - if ( $Angle < 270 && $Angle > 90 ) - { - $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; - $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y; - $this->pChartObject->drawLine($Xc,$Yc,$Xc,$Yc-$SliceHeight,$Settings); - } - } - - $Angle = $SliceAngle[$SliceID][count($SliceAngle[$SliceID])-1]; - if ( $Angle < 270 && $Angle > 90 ) - { - $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; - $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y; - $this->pChartObject->drawLine($Xc,$Yc,$Xc,$Yc-$SliceHeight,$Settings); - } - - if ( isset($SliceAngle[$SliceID][1]) && $SliceAngle[$SliceID][1] > 270 && $SliceAngle[$SliceID][count($SliceAngle[$SliceID])-1] < 270 ) - { - $Xc = cos((270-90)*PI/180) * $Radius + $X; - $Yc = sin((270-90)*PI/180) * $Radius*$SkewFactor + $Y; - $this->pChartObject->drawLine($Xc,$Yc,$Xc,$Yc-$SliceHeight,$Settings); - } - - if ( isset($SliceAngle[$SliceID][1]) && $SliceAngle[$SliceID][1] > 90 && $SliceAngle[$SliceID][count($SliceAngle[$SliceID])-1] < 90 ) - { - $Xc = cos((0)*PI/180) * $Radius + $X; - $Yc = sin((0)*PI/180) * $Radius*$SkewFactor + $Y; - $this->pChartObject->drawLine($Xc,$Yc,$Xc,$Yc-$SliceHeight,$Settings); - } - - } - } - - /* Draw the top splice */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; - $Settings["R"]+= 20; $Settings["G"]+= 20; $Settings["B"]+= 20; - - $Top = ""; - for($j=0;$jpChartObject->drawPolygon($Top,$Settings); - - if ( $RecordImageMap && !$Shadow ) { $this->pChartObject->addToImageMap("POLY",$this->arraySerialize($Top),$this->pChartObject->toHTMLColor($Settings["R"],$Settings["G"],$Settings["B"]),$Data["Series"][$Data["Abscissa"]]["Data"][count($Slices)-$SliceID-1],$Values[$SliceID]); } - } - - - /* Second pass to smooth the angles */ - if ( $SecondPass ) - { - $Step = 360 / (2 * PI * $Radius); - $Offset = 360; $ID = count($Values)-1; - foreach($Values as $Key => $Value) - { - $FirstPoint = TRUE; - if ( $Shadow ) - $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa); - else - { - if ( $Border ) - { $Settings = array("R"=>$Palette[$ID]["R"]+30,"G"=>$Palette[$ID]["G"]+30,"B"=>$Palette[$ID]["B"]+30,"Alpha"=>$Palette[$ID]["Alpha"]); } - else - $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); - } - - $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } - - if ($DataGapAngle == 0) - { $X0 = $X; $Y0 = $Y- $SliceHeight; } - else - { - $Angle = ($EndAngle - $Offset)/2 + $Offset; - $X0 = cos(($Angle-90)*PI/180) * $DataGapRadius + $X; - $Y0 = sin(($Angle-90)*PI/180) * $DataGapRadius*$SkewFactor + $Y - $SliceHeight; - } - $Plots[] = $X0; $Plots[] = $Y0; - - for($i=$Offset;$i>=$EndAngle;$i=$i-$Step) - { - $Xc = cos(($i-90)*PI/180) * $Radius + $X; - $Yc = sin(($i-90)*PI/180) * $Radius*$SkewFactor + $Y - $SliceHeight; - - if ( $FirstPoint ) { $this->pChartObject->drawLine($Xc,$Yc,$X0,$Y0,$Settings); } { $FirstPoint = FALSE; } - - $this->pChartObject->drawAntialiasPixel($Xc,$Yc,$Settings); - if ($i < 270 && $i > 90 ) { $this->pChartObject->drawAntialiasPixel($Xc,$Yc+$SliceHeight,$Settings); } - } - $this->pChartObject->drawLine($Xc,$Yc,$X0,$Y0,$Settings); - - $Offset = $i - $DataGapAngle; $ID--; - } - } - - if ( $WriteValues != NULL ) - { - $Step = 360 / (2 * PI * $Radius); - $Offset = 360; $ID = count($Values)-1; - $Settings = array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"R"=>$ValueR,"G"=>$ValueG,"B"=>$ValueB,"Alpha"=>$ValueAlpha); - foreach($Values as $Key => $Value) - { - $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } - - $Angle = ($EndAngle - $Offset)/2 + $Offset; - - if ( $ValuePosition == PIE_VALUE_OUTSIDE ) - { - $Xc = cos(($Angle-90)*PI/180) * ($Radius+$ValuePadding) + $X; - $Yc = sin(($Angle-90)*PI/180) * (($Radius*$SkewFactor)+$ValuePadding) + $Y - $SliceHeight; - } - else - { - $Xc = cos(($Angle-90)*PI/180) * ($Radius)/2 + $X; - $Yc = sin(($Angle-90)*PI/180) * ($Radius*$SkewFactor)/2 + $Y - $SliceHeight; - } - - if ( $WriteValues == PIE_VALUE_PERCENTAGE ) - $Display = round(( 100 / $SerieSum ) * $Value,$Precision)."%"; - elseif ( $WriteValues == PIE_VALUE_NATURAL ) - $Display = $Value.$ValueSuffix; - - $this->pChartObject->drawText($Xc,$Yc,$Display,$Settings); - - $Offset = $EndAngle - $DataGapAngle; $ID--; - } - } - - if ( $DrawLabels ) - { - $Step = 360 / (2 * PI * $Radius); - $Offset = 360; $ID = count($Values)-1; - foreach($Values as $Key => $Value) - { - if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) - { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} - else - { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } - - $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } - - $Angle = ($EndAngle - $Offset)/2 + $Offset; - $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; - $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y - $SliceHeight; - - if ( isset($Data["Series"][$Data["Abscissa"]]["Data"][$ID]) ) - { - $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$ID]; - - if ( $LabelStacked ) - $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,TRUE,$X,$Y,$Radius,TRUE); - else - $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,FALSE); - } - - $Offset = $EndAngle - $DataGapAngle; $ID--; - } - } - - if ( $DrawLabels && $LabelStacked ) { $this->writeShiftedLabels(); } - - $this->pChartObject->Shadow = $RestoreShadow; - - return(PIE_RENDERED); - } - - /* Draw the legend of pie chart */ - function drawPieLegend($X,$Y,$Format="") - { - $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->pChartObject->FontName; - $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->pChartObject->FontSize; - $FontR = isset($Format["FontR"]) ? $Format["FontR"] : $this->pChartObject->FontColorR; - $FontG = isset($Format["FontG"]) ? $Format["FontG"] : $this->pChartObject->FontColorG; - $FontB = isset($Format["FontB"]) ? $Format["FontB"] : $this->pChartObject->FontColorB; - $BoxSize = isset($Format["BoxSize"]) ? $Format["BoxSize"] : 5; - $Margin = isset($Format["Margin"]) ? $Format["Margin"] : 5; - $R = isset($Format["R"]) ? $Format["R"] : 200; - $G = isset($Format["G"]) ? $Format["G"] : 200; - $B = isset($Format["B"]) ? $Format["B"] : 200; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 255; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 255; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 255; - $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; - $Style = isset($Format["Style"]) ? $Format["Style"] : LEGEND_ROUND; - $Mode = isset($Format["Mode"]) ? $Format["Mode"] : LEGEND_VERTICAL; - - if ( $Surrounding != NULL ) { $BorderR = $R + $Surrounding; $BorderG = $G + $Surrounding; $BorderB = $B + $Surrounding; } - - $YStep = max($this->pChartObject->FontSize,$BoxSize) + 5; - $XStep = $BoxSize + 5; - - /* Data Processing */ - $Data = $this->pDataObject->getData(); - $Palette = $this->pDataObject->getPalette(); - - /* Do we have an abscissa serie defined? */ - if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } - - $Boundaries = ""; $Boundaries["L"] = $X; $Boundaries["T"] = $Y; $Boundaries["R"] = 0; $Boundaries["B"] = 0; $vY = $Y; $vX = $X; - foreach($Data["Series"][$Data["Abscissa"]]["Data"] as $Key => $Value) - { - $BoxArray = $this->pChartObject->getTextBox($vX+$BoxSize+4,$vY+$BoxSize/2,$FontName,$FontSize,0,$Value); - - if ( $Mode == LEGEND_VERTICAL ) - { - if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$BoxSize/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$BoxSize/2; } - if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } - if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$BoxSize/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$BoxSize/2; } - $vY=$vY+$YStep; - } - elseif ( $Mode == LEGEND_HORIZONTAL ) - { - if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$BoxSize/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$BoxSize/2; } - if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } - if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$BoxSize/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$BoxSize/2; } - $vX=$Boundaries["R"]+$XStep; - } - } - $vY=$vY-$YStep; $vX=$vX-$XStep; - - $TopOffset = $Y - $Boundaries["T"]; - if ( $Boundaries["B"]-($vY+$BoxSize) < $TopOffset ) { $Boundaries["B"] = $vY+$BoxSize+$TopOffset; } - - if ( $Style == LEGEND_ROUND ) - $this->pChartObject->drawRoundedFilledRectangle($Boundaries["L"]-$Margin,$Boundaries["T"]-$Margin,$Boundaries["R"]+$Margin,$Boundaries["B"]+$Margin,$Margin,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); - elseif ( $Style == LEGEND_BOX ) - $this->pChartObject->drawFilledRectangle($Boundaries["L"]-$Margin,$Boundaries["T"]-$Margin,$Boundaries["R"]+$Margin,$Boundaries["B"]+$Margin,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); - - $RestoreShadow = $this->pChartObject->Shadow; $this->pChartObject->Shadow = FALSE; - foreach($Data["Series"][$Data["Abscissa"]]["Data"] as $Key => $Value) - { - $R = $Palette[$Key]["R"]; $G = $Palette[$Key]["G"]; $B = $Palette[$Key]["B"]; - - $this->pChartObject->drawFilledRectangle($X+1,$Y+1,$X+$BoxSize+1,$Y+$BoxSize+1,array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20)); - $this->pChartObject->drawFilledRectangle($X,$Y,$X+$BoxSize,$Y+$BoxSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Surrounding"=>20)); - if ( $Mode == LEGEND_VERTICAL ) - { - $this->pChartObject->drawText($X+$BoxSize+4,$Y+$BoxSize/2,$Value,array("R"=>$FontR,"G"=>$FontG,"B"=>$FontB,"Align"=>TEXT_ALIGN_MIDDLELEFT,"FontName"=>$FontName,"FontSize"=>$FontSize)); - $Y=$Y+$YStep; - } - elseif ( $Mode == LEGEND_HORIZONTAL ) - { - $BoxArray = $this->pChartObject->drawText($X+$BoxSize+4,$Y+$BoxSize/2,$Value,array("R"=>$FontR,"G"=>$FontG,"B"=>$FontB,"Align"=>TEXT_ALIGN_MIDDLELEFT,"FontName"=>$FontName,"FontSize"=>$FontSize)); - $X=$BoxArray[1]["X"]+2+$XStep; - } - } - - $this->Shadow = $RestoreShadow; - } - - /* Set the color of the specified slice */ - function setSliceColor($SliceID,$Format="") - { - $R = isset($Format["R"]) ? $Format["R"] : 0; - $G = isset($Format["G"]) ? $Format["G"] : 0; - $B = isset($Format["B"]) ? $Format["B"] : 0; - $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; - - $this->pDataObject->Palette[$SliceID]["R"] = $R; - $this->pDataObject->Palette[$SliceID]["G"] = $G; - $this->pDataObject->Palette[$SliceID]["B"] = $B; - $this->pDataObject->Palette[$SliceID]["Alpha"] = $Alpha; - } - - /* Internally used compute the label positions */ - function writePieLabel($X,$Y,$Label,$Angle,$Settings,$Stacked,$Xc=0,$Yc=0,$Radius=0,$Reversed=FALSE) - { - $LabelOffset = 30; - $FontName = $this->pChartObject->FontName; - $FontSize = $this->pChartObject->FontSize; - - if ( !$Stacked ) - { - $Settings["Angle"] = 360-$Angle; - $Settings["Length"] = 25; - $Settings["Size"] = 8; - - $this->pChartObject->drawArrowLabel($X,$Y," ".$Label." ",$Settings); - } - else - { - $X2 = cos(deg2rad($Angle-90))*20+$X; - $Y2 = sin(deg2rad($Angle-90))*20+$Y; - - $TxtPos = $this->pChartObject->getTextBox($X,$Y,$FontName,$FontSize,0,$Label); - $Height = $TxtPos[0]["Y"] - $TxtPos[2]["Y"]; - $YTop = $Y2 - $Height/2 - 2; - $YBottom = $Y2 + $Height/2 + 2; - - if ( $this->LabelPos != "" ) - { - $Done = FALSE; - foreach($this->LabelPos as $Key => $Settings) - { - if ( !$Done ) - { - if ( $Angle <= 90 && (($YTop >= $Settings["YTop"] && $YTop <= $Settings["YBottom"]) || ($YBottom >= $Settings["YTop"] && $YBottom <= $Settings["YBottom"]))) - { $this->shift(0,180,-($Height+2),$Reversed); $Done = TRUE; } - if ( $Angle > 90 && $Angle <= 180 && (($YTop >= $Settings["YTop"] && $YTop <= $Settings["YBottom"]) || ($YBottom >= $Settings["YTop"] && $YBottom <= $Settings["YBottom"]))) - { $this->shift(0,180,-($Height+2),$Reversed); $Done = TRUE; } - if ( $Angle > 180 && $Angle <= 270 && (($YTop >= $Settings["YTop"] && $YTop <= $Settings["YBottom"]) || ($YBottom >= $Settings["YTop"] && $YBottom <= $Settings["YBottom"]))) - { $this->shift(180,360,($Height+2),$Reversed); $Done = TRUE; } - if ( $Angle > 270 && $Angle <= 360 && (($YTop >= $Settings["YTop"] && $YTop <= $Settings["YBottom"]) || ($YBottom >= $Settings["YTop"] && $YBottom <= $Settings["YBottom"]))) - { $this->shift(180,360,($Height+2),$Reversed); $Done = TRUE; } - } - } - } - - $LabelSettings = array("YTop"=>$YTop,"YBottom"=>$YBottom,"Label"=>$Label,"Angle"=>$Angle,"X1"=>$X,"Y1"=>$Y,"X2"=>$X2,"Y2"=>$Y2); - if ( $Angle <= 180 ) { $LabelSettings["X3"] = $Xc+$Radius+$LabelOffset; } - if ( $Angle > 180 ) { $LabelSettings["X3"] = $Xc-$Radius-$LabelOffset; } - $this->LabelPos[] = $LabelSettings; - } - } - - /* Internally used to shift label positions */ - function shift($StartAngle,$EndAngle,$Offset,$Reversed) - { - if ( $Reversed ) { $Offset = -$Offset; } - foreach($this->LabelPos as $Key => $Settings) - { - if ( $Settings["Angle"] > $StartAngle && $Settings["Angle"] <= $EndAngle ) { $this->LabelPos[$Key]["YTop"] = $Settings["YTop"] + $Offset; $this->LabelPos[$Key]["YBottom"] = $Settings["YBottom"] + $Offset; $this->LabelPos[$Key]["Y2"] = $Settings["Y2"] + $Offset; } - } - } - - /* Internally used to write the re-computed labels */ - function writeShiftedLabels() - { - if ( $this->LabelPos == "" ) { return(0); } - foreach($this->LabelPos as $Key => $Settings) - { - $X1 = $Settings["X1"]; $Y1 = $Settings["Y1"]; - $X2 = $Settings["X2"]; $Y2 = $Settings["Y2"]; - $X3 = $Settings["X3"]; - $Angle = $Settings["Angle"]; - $Label = $Settings["Label"]; - - $this->pChartObject->drawArrow($X2,$Y2,$X1,$Y1,array("Size"=>8)); - if ( $Angle <= 180 ) - { - $this->pChartObject->drawLine($X2,$Y2,$X3,$Y2); - $this->pChartObject->drawText($X3+2,$Y2,$Label,array("Align"=>TEXT_ALIGN_MIDDLELEFT)); - } - else - { - $this->pChartObject->drawLine($X2,$Y2,$X3,$Y2); - $this->pChartObject->drawText($X3-2,$Y2,$Label,array("Align"=>TEXT_ALIGN_MIDDLERIGHT)); - } - } - } - - /* Draw a ring chart */ - function draw2DRing($X,$Y,$Format="") - { - $OuterRadius = isset($Format["Radius"]) ? $Format["Radius"] : 60; - $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0; - $InnerRadius = isset($Format["Radius"]) ? $Format["Radius"] : 30; - $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE; - $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 255; - $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 255; - $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 255; - $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : 100; - $Shadow = isset($Format["Shadow"]) ? $Format["Shadow"] : FALSE; - $DrawLabels = isset($Format["DrawLabels"]) ? $Format["DrawLabels"] : FALSE; - $LabelStacked = isset($Format["LabelStacked"]) ? $Format["LabelStacked"] : FALSE; - $LabelColor = isset($Format["LabelColor"]) ? $Format["LabelColor"] : PIE_LABEL_COLOR_MANUAL; - $LabelR = isset($Format["LabelR"]) ? $Format["LabelR"] : 0; - $LabelG = isset($Format["LabelG"]) ? $Format["LabelG"] : 0; - $LabelB = isset($Format["LabelB"]) ? $Format["LabelB"] : 0; - $LabelAlpha = isset($Format["LabelAlpha"]) ? $Format["LabelAlpha"] : 100; - $WriteValues = isset($Format["WriteValues"]) ? $Format["WriteValues"] : NULL; //PIE_VALUE_PERCENTAGE - $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : 5; - $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_OUTSIDE; - $ValueSuffix = isset($Format["ValueSuffix"]) ? $Format["ValueSuffix"] : ""; - $ValueR = isset($Format["ValueR"]) ? $Format["ValueR"] : 255; - $ValueG = isset($Format["ValueG"]) ? $Format["ValueG"] : 255; - $ValueB = isset($Format["ValueB"]) ? $Format["ValueB"] : 255; - $ValueAlpha = isset($Format["ValueAlpha"]) ? $Format["ValueAlpha"] : 100; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - - /* Data Processing */ - $Data = $this->pDataObject->getData(); - $Palette = $this->pDataObject->getPalette(); - - /* Do we have an abscissa serie defined? */ - if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } - - /* Try to find the data serie */ - $DataSerie = ""; - foreach ($Data["Series"] as $SerieName => $SerieData) - { if ( $SerieName != $Data["Abscissa"]) { $DataSerie = $SerieName; } } - - /* Do we have data to compute? */ - if ( $DataSerie == "" ) { return(PIE_NO_DATASERIE); } - - /* Remove unused data */ - list($Data,$Palette) = $this->clean0Values($Data,$Palette,$DataSerie,$Data["Abscissa"]); - - /* Compute the pie sum */ - $SerieSum = $this->pDataObject->getSum($DataSerie); - - /* Do we have data to draw? */ - if ( $SerieSum == 0 ) { return(PIE_SUMISNULL); } - - /* Dump the real number of data to draw */ - $Values = ""; - foreach ($Data["Series"][$DataSerie]["Data"] as $Key => $Value) - { if ($Value != 0) { $Values[] = $Value; } } - - /* Compute the wasted angular space between series */ - if (count($Values)==1) { $WastedAngular = 0; } else { $WastedAngular = 0; } // count($Values) - - /* Compute the scale */ - $ScaleFactor = (360 - $WastedAngular) / $SerieSum; - - $RestoreShadow = $this->pChartObject->Shadow; - if ( $this->pChartObject->Shadow ) - { - $this->pChartObject->Shadow = FALSE; - - $ShadowFormat = $Format; $ShadowFormat["Shadow"] = TRUE; - $this->draw2DRing($X+$this->pChartObject->ShadowX,$Y+$this->pChartObject->ShadowY,$ShadowFormat); - } - - /* Draw the polygon pie elements */ - $Step = 360 / (2 * PI * $OuterRadius); - $Offset = 0; $ID = 0; - foreach($Values as $Key => $Value) - { - if ( $Shadow ) - { - $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa); - $BorderColor = $Settings; - } - else - { - if ( !isset($Palette[$ID]["R"]) ) { $Color = $this->pChartObject->getRandomColor(); $Palette[$ID] = $Color; $this->pDataObject->savePalette($ID,$Color); } - $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); - - if ( $Border ) - $BorderColor = array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha); - else - $BorderColor = $Settings; - } - - $Plots = ""; $Boundaries = ""; $AAPixels = ""; - $EndAngle = $Offset+($Value*$ScaleFactor); if ( $EndAngle > 360 ) { $EndAngle = 360; } - for($i=$Offset;$i<=$EndAngle;$i=$i+$Step) - { - $Xc = cos(($i-90)*PI/180) * $OuterRadius + $X; - $Yc = sin(($i-90)*PI/180) * $OuterRadius + $Y; - - if ( !isset($Boundaries[0]["X1"]) ) { $Boundaries[0]["X1"] = $Xc; $Boundaries[0]["Y1"] = $Yc; } - $AAPixels[] = array($Xc,$Yc); - - if ( $i<90 ) { $Yc++; } - if ( $i>180 && $i<270 ) { $Xc++; } - if ( $i>=270 ) { $Xc++; $Yc++; } - - $Plots[] = $Xc; $Plots[] = $Yc; - } - $Boundaries[1]["X1"] = $Xc; $Boundaries[1]["Y1"] = $Yc; - $Lasti = $EndAngle; - - for($i=$EndAngle;$i>=$Offset;$i=$i-$Step) - { - $Xc = cos(($i-90)*PI/180) * ($InnerRadius-1) + $X; - $Yc = sin(($i-90)*PI/180) * ($InnerRadius-1) + $Y; - - if ( !isset($Boundaries[1]["X2"]) ) { $Boundaries[1]["X2"] = $Xc; $Boundaries[1]["Y2"] = $Yc; } - $AAPixels[] = array($Xc,$Yc); - - $Xc = cos(($i-90)*PI/180) * $InnerRadius + $X; - $Yc = sin(($i-90)*PI/180) * $InnerRadius + $Y; - - if ( $i<90 ) { $Yc++; } - if ( $i>180 && $i<270 ) { $Xc++; } - if ( $i>=270 ) { $Xc++; $Yc++; } - - $Plots[] = $Xc; $Plots[] = $Yc; - } - $Boundaries[0]["X2"] = $Xc; $Boundaries[0]["Y2"] = $Yc; - - /* Draw the polygon */ - $this->pChartObject->drawPolygon($Plots,$Settings); - if ( $RecordImageMap && !$Shadow ) { $this->pChartObject->addToImageMap("POLY",$this->arraySerialize($Plots),$this->pChartObject->toHTMLColor($Palette[$ID]["R"],$Palette[$ID]["G"],$Palette[$ID]["B"]),$Data["Series"][$Data["Abscissa"]]["Data"][$Key],$Value); } - - /* Smooth the edges using AA */ - foreach($AAPixels as $iKey => $Pos ) { $this->pChartObject->drawAntialiasPixel($Pos[0],$Pos[1],$BorderColor); } - $this->pChartObject->drawLine($Boundaries[0]["X1"],$Boundaries[0]["Y1"],$Boundaries[0]["X2"],$Boundaries[0]["Y2"],$BorderColor); - $this->pChartObject->drawLine($Boundaries[1]["X1"],$Boundaries[1]["Y1"],$Boundaries[1]["X2"],$Boundaries[1]["Y2"],$BorderColor); - - if ( $DrawLabels && !$Shadow ) - { - if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) - { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} - else - { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } - - $Angle = ($EndAngle - $Offset)/2 + $Offset; - $Xc = cos(($Angle-90)*PI/180) * $OuterRadius + $X; - $Yc = sin(($Angle-90)*PI/180) * $OuterRadius + $Y; - - $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key]; - - if ( $LabelStacked ) - $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,TRUE,$X,$Y,$OuterRadius); - else - $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,FALSE); - } - - $Offset = $Lasti; $ID++; - } - - if ( $DrawLabels && $LabelStacked ) { $this->writeShiftedLabels(); } - - if ( $WriteValues && !$Shadow ) - { - $Step = 360 / (2 * PI * $OuterRadius); - $Offset = 0; - foreach($Values as $Key => $Value) - { - $EndAngle = $Offset+($Value*$ScaleFactor); - if ( $EndAngle > 360 ) { $EndAngle = 360; } - - $Angle = $Offset+($Value*$ScaleFactor)/2; - if ( $ValuePosition == PIE_VALUE_OUTSIDE ) - { - $Xc = cos(($Angle-90)*PI/180) * ($OuterRadius+$ValuePadding) + $X; - $Yc = sin(($Angle-90)*PI/180) * ($OuterRadius+$ValuePadding) + $Y; - if ( $Angle >=0 && $Angle <= 90 ) { $Align = TEXT_ALIGN_BOTTOMLEFT; } - if ( $Angle > 90 && $Angle <= 180 ) { $Align = TEXT_ALIGN_TOPLEFT; } - if ( $Angle > 180 && $Angle <= 270 ) { $Align = TEXT_ALIGN_TOPRIGHT; } - if ( $Angle > 270 ) { $Align = TEXT_ALIGN_BOTTOMRIGHT; } - } - else - { - $Xc = cos(($Angle-90)*PI/180) * (($OuterRadius-$InnerRadius)/2+$InnerRadius) + $X; - $Yc = sin(($Angle-90)*PI/180) * (($OuterRadius-$InnerRadius)/2+$InnerRadius) + $Y; - $Align = TEXT_ALIGN_MIDDLEMIDDLE; - } - - if ( $WriteValues == PIE_VALUE_PERCENTAGE ) - $Display = round(( 100 / $SerieSum ) * $Value,$Precision)."%"; - elseif ( $WriteValues == PIE_VALUE_NATURAL ) - $Display = $Value.$ValueSuffix; - else - $Label = ""; - - $this->pChartObject->drawText($Xc,$Yc,$Display,array("Align"=>$Align,"R"=>$ValueR,"G"=>$ValueG,"B"=>$ValueB)); - $Offset = $EndAngle; - } - } - - $this->pChartObject->Shadow = $RestoreShadow; - - return(PIE_RENDERED); - } - - /* Draw a 3D ring chart */ - function draw3DRing($X,$Y,$Format="") - { - $OuterRadius = isset($Format["OuterRadius"]) ? $Format["OuterRadius"] : 100; - $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0; - $InnerRadius = isset($Format["InnerRadius"]) ? $Format["InnerRadius"] : 30; - $SkewFactor = isset($Format["SkewFactor"]) ? $Format["SkewFactor"] : .6; - $SliceHeight = isset($Format["SliceHeight"]) ? $Format["SliceHeight"] : 10; - $DataGapAngle = isset($Format["DataGapAngle"]) ? $Format["DataGapAngle"] : 10; - $DataGapRadius = isset($Format["DataGapRadius"]) ? $Format["DataGapRadius"] : 10; - $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE; - $Shadow = isset($Format["Shadow"]) ? $Format["Shadow"] : FALSE; - $DrawLabels = isset($Format["DrawLabels"]) ? $Format["DrawLabels"] : FALSE; - $LabelStacked = isset($Format["LabelStacked"]) ? $Format["LabelStacked"] : FALSE; - $LabelColor = isset($Format["LabelColor"]) ? $Format["LabelColor"] : PIE_LABEL_COLOR_MANUAL; - $LabelR = isset($Format["LabelR"]) ? $Format["LabelR"] : 0; - $LabelG = isset($Format["LabelG"]) ? $Format["LabelG"] : 0; - $LabelB = isset($Format["LabelB"]) ? $Format["LabelB"] : 0; - $LabelAlpha = isset($Format["LabelAlpha"]) ? $Format["LabelAlpha"] : 100; - $Cf = isset($Format["Cf"]) ? $Format["Cf"] : 20; - $WriteValues = isset($Format["WriteValues"]) ? $Format["WriteValues"] : PIE_VALUE_NATURAL; - $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : $SliceHeight + 15; - $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_OUTSIDE; - $ValueSuffix = isset($Format["ValueSuffix"]) ? $Format["ValueSuffix"] : ""; - $ValueR = isset($Format["ValueR"]) ? $Format["ValueR"] : 255; - $ValueG = isset($Format["ValueG"]) ? $Format["ValueG"] : 255; - $ValueB = isset($Format["ValueB"]) ? $Format["ValueB"] : 255; - $ValueAlpha = isset($Format["ValueAlpha"]) ? $Format["ValueAlpha"] : 100; - $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; - - /* Error correction for overlaying rounded corners */ - if ( $SkewFactor < .5 ) { $SkewFactor = .5; } - - /* Data Processing */ - $Data = $this->pDataObject->getData(); - $Palette = $this->pDataObject->getPalette(); - - /* Do we have an abscissa serie defined? */ - if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } - - /* Try to find the data serie */ - $DataSerie = ""; - foreach ($Data["Series"] as $SerieName => $SerieData) - { if ( $SerieName != $Data["Abscissa"]) { $DataSerie = $SerieName; } } - - /* Do we have data to compute? */ - if ( $DataSerie == "" ) { return(PIE_NO_DATASERIE); } - - /* Remove unused data */ - list($Data,$Palette) = $this->clean0Values($Data,$Palette,$DataSerie,$Data["Abscissa"]); - - /* Compute the pie sum */ - $SerieSum = $this->pDataObject->getSum($DataSerie); - - /* Do we have data to draw? */ - if ( $SerieSum == 0 ) { return(PIE_SUMISNULL); } - - /* Dump the real number of data to draw */ - $Values = ""; - foreach ($Data["Series"][$DataSerie]["Data"] as $Key => $Value) - { if ($Value != 0) { $Values[] = $Value; } } - - /* Compute the wasted angular space between series */ - if (count($Values)==1) { $WastedAngular = 0; } else { $WastedAngular = count($Values) * $DataGapAngle; } - - /* Compute the scale */ - $ScaleFactor = (360 - $WastedAngular) / $SerieSum; - - $RestoreShadow = $this->pChartObject->Shadow; - if ( $this->pChartObject->Shadow ) { $this->pChartObject->Shadow = FALSE; } - - /* Draw the polygon ring elements */ - $Offset = 360; $ID = count($Values)-1; - $Values = array_reverse($Values); - $Slice = 0; $Slices = ""; $SliceColors = ""; $Visible = ""; $SliceAngle = ""; - foreach($Values as $Key => $Value) - { - if ( !isset($Palette[$ID]["R"]) ) { $Color = $this->pChartObject->getRandomColor(); $Palette[$ID] = $Color; $this->pDataObject->savePalette($ID,$Color); } - $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); - - $SliceColors[$Slice] = $Settings; - - $StartAngle = $Offset; - $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } - - if ( $StartAngle > 180 ) { $Visible[$Slice]["Start"] = TRUE; } else { $Visible[$Slice]["Start"] = TRUE; } - if ( $EndAngle < 180 ) { $Visible[$Slice]["End"] = FALSE; } else { $Visible[$Slice]["End"] = TRUE; } - - $Step = (360 / (2 * PI * $OuterRadius))/2; - $OutX1 = VOID; $OutY1 = VOID; - for($i=$Offset;$i>=$EndAngle;$i=$i-$Step) - { - $Xc = cos(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius-2) + $X; - $Yc = sin(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius-2)*$SkewFactor + $Y; - $Slices[$Slice]["AA"][] = array($Xc,$Yc); - - $Xc = cos(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius-1) + $X; - $Yc = sin(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius-1)*$SkewFactor + $Y; - $Slices[$Slice]["AA"][] = array($Xc,$Yc); - - $Xc = cos(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius) + $X; - $Yc = sin(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius)*$SkewFactor + $Y; - $this->pChartObject->drawAntialiasPixel($Xc,$Yc,$Settings); - - if ( $OutX1 == VOID ) { $OutX1 = $Xc; $OutY1 = $Yc; } - - if ( $i<90 ) { $Yc++; } - if ( $i>90 && $i<180 ) { $Xc++; } - if ( $i>180 && $i<270 ) { $Xc++; } - if ( $i>=270 ) { $Xc++; $Yc++; } - - $Slices[$Slice]["BottomPoly"][] = floor($Xc); $Slices[$Slice]["BottomPoly"][] = floor($Yc); - $Slices[$Slice]["TopPoly"][] = floor($Xc); $Slices[$Slice]["TopPoly"][] = floor($Yc)-$SliceHeight; - $Slices[$Slice]["Angle"][] = $i; - } - $OutX2 = $Xc; $OutY2 = $Yc; - - $Slices[$Slice]["Angle"][] = VOID; - $Lasti = $i; - - $Step = (360 / (2 * PI * $InnerRadius))/2; - $InX1 = VOID; $InY1 = VOID; - for($i=$EndAngle;$i<=$Offset;$i=$i+$Step) - { - $Xc = cos(($i-90)*PI/180) * ($InnerRadius+$DataGapRadius-1) + $X; - $Yc = sin(($i-90)*PI/180) * ($InnerRadius+$DataGapRadius-1)*$SkewFactor + $Y; - $Slices[$Slice]["AA"][] = array($Xc,$Yc); - - $Xc = cos(($i-90)*PI/180) * ($InnerRadius+$DataGapRadius) + $X; - $Yc = sin(($i-90)*PI/180) * ($InnerRadius+$DataGapRadius)*$SkewFactor + $Y; - $Slices[$Slice]["AA"][] = array($Xc,$Yc); - - if ( $InX1 == VOID ) { $InX1 = $Xc; $InY1 = $Yc; } - - if ( $i<90 ) { $Yc++; } - if ( $i>90 && $i<180 ) { $Xc++; } - if ( $i>180 && $i<270 ) { $Xc++; } - if ( $i>=270 ) { $Xc++; $Yc++; } - - $Slices[$Slice]["BottomPoly"][] = floor($Xc); $Slices[$Slice]["BottomPoly"][] = floor($Yc); - $Slices[$Slice]["TopPoly"][] = floor($Xc); $Slices[$Slice]["TopPoly"][] = floor($Yc)-$SliceHeight; - $Slices[$Slice]["Angle"][] = $i; - } - $InX2 = $Xc; $InY2 = $Yc; - - $Slices[$Slice]["InX1"] = $InX1; $Slices[$Slice]["InY1"] = $InY1; - $Slices[$Slice]["InX2"] = $InX2; $Slices[$Slice]["InY2"] = $InY2; - $Slices[$Slice]["OutX1"] = $OutX1; $Slices[$Slice]["OutY1"] = $OutY1; - $Slices[$Slice]["OutX2"] = $OutX2; $Slices[$Slice]["OutY2"] = $OutY2; - - $Offset = $Lasti - $DataGapAngle; $ID--; $Slice++; - } - - /* Draw the bottom pie splice */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; - $this->pChartObject->drawPolygon($Plots["BottomPoly"],$Settings); - - foreach($Plots["AA"] as $Key => $Pos) - $this->pChartObject->drawAntialiasPixel($Pos[0],$Pos[1],$Settings); - - $this->pChartObject->drawLine($Plots["InX1"],$Plots["InY1"],$Plots["OutX2"],$Plots["OutY2"],$Settings); - $this->pChartObject->drawLine($Plots["InX2"],$Plots["InY2"],$Plots["OutX1"],$Plots["OutY1"],$Settings); - } - - $Slices = array_reverse($Slices); - $SliceColors = array_reverse($SliceColors); - - /* Draw the vertical edges (semi-visible) */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; - $Settings["R"] = $Settings["R"]+$Cf; $Settings["G"] = $Settings["G"]+$Cf; $Settings["B"] = $Settings["B"]+$Cf; - - $StartAngle = $Plots["Angle"][0]; - foreach($Plots["Angle"] as $Key =>$Angle) { if ($Angle == VOID) { $EndAngle = $Plots["Angle"][$Key-1]; } } - - if ( $StartAngle >= 270 || $StartAngle <= 90 ) - $this->pChartObject->drawLine($Plots["OutX1"],$Plots["OutY1"],$Plots["OutX1"],$Plots["OutY1"]-$SliceHeight,$Settings); - if ( $StartAngle >= 270 || $StartAngle <= 90 ) - $this->pChartObject->drawLine($Plots["OutX2"],$Plots["OutY2"],$Plots["OutX2"],$Plots["OutY2"]-$SliceHeight,$Settings); - - $this->pChartObject->drawLine($Plots["InX1"],$Plots["InY1"],$Plots["InX1"],$Plots["InY1"]-$SliceHeight,$Settings); - $this->pChartObject->drawLine($Plots["InX2"],$Plots["InY2"],$Plots["InX2"],$Plots["InY2"]-$SliceHeight,$Settings); - } - - /* Draw the inner vertical slices */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; - $Settings["R"] = $Settings["R"]+$Cf; $Settings["G"] = $Settings["G"]+$Cf; $Settings["B"] = $Settings["B"]+$Cf; - - $Outer = TRUE; $Inner = FALSE; - $InnerPlotsA = ""; $InnerPlotsB = ""; - foreach($Plots["Angle"] as $ID => $Angle) - { - if ( $Angle == VOID ) - { $Outer = FALSE; $Inner = TRUE; } - elseif( $Inner ) - { - if (( $Angle < 90 || $Angle > 270 ) && isset($Plots["BottomPoly"][$ID*2]) ) - { - $Xo = $Plots["BottomPoly"][$ID*2]; - $Yo = $Plots["BottomPoly"][$ID*2+1]; - - $InnerPlotsA[] = $Xo; $InnerPlotsA[] = $Yo; - $InnerPlotsB[] = $Xo; $InnerPlotsB[] = $Yo-$SliceHeight; - } - } - } - - if ( $InnerPlotsA != "" ) - { $InnerPlots = array_merge($InnerPlotsA,$this->arrayReverse($InnerPlotsB)); $this->pChartObject->drawPolygon($InnerPlots,$Settings); } - } - - /* Draw the splice top and left poly */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; - $Settings["R"] = $Settings["R"]+$Cf*1.5; $Settings["G"] = $Settings["G"]+$Cf*1.5; $Settings["B"] = $Settings["B"]+$Cf*1.5; - - $StartAngle = $Plots["Angle"][0]; - foreach($Plots["Angle"] as $Key =>$Angle) { if ($Angle == VOID) { $EndAngle = $Plots["Angle"][$Key-1]; } } - - if ( $StartAngle < 180 ) - { - $Points = ""; - $Points[] = $Plots["InX2"]; - $Points[] = $Plots["InY2"]; - $Points[] = $Plots["InX2"]; - $Points[] = $Plots["InY2"]-$SliceHeight; - $Points[] = $Plots["OutX1"]; - $Points[] = $Plots["OutY1"]-$SliceHeight; - $Points[] = $Plots["OutX1"]; - $Points[] = $Plots["OutY1"]; - - $this->pChartObject->drawPolygon($Points,$Settings); - } - - if ( $EndAngle > 180 ) - { - $Points = ""; - $Points[] = $Plots["InX1"]; - $Points[] = $Plots["InY1"]; - $Points[] = $Plots["InX1"]; - $Points[] = $Plots["InY1"]-$SliceHeight; - $Points[] = $Plots["OutX2"]; - $Points[] = $Plots["OutY2"]-$SliceHeight; - $Points[] = $Plots["OutX2"]; - $Points[] = $Plots["OutY2"]; - - $this->pChartObject->drawPolygon($Points,$Settings); - } - } - - - /* Draw the vertical edges (visible) */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; - $Settings["R"] = $Settings["R"]+$Cf; $Settings["G"] = $Settings["G"]+$Cf; $Settings["B"] = $Settings["B"]+$Cf; - - $StartAngle = $Plots["Angle"][0]; - foreach($Plots["Angle"] as $Key =>$Angle) { if ($Angle == VOID) { $EndAngle = $Plots["Angle"][$Key-1]; } } - - if ( $StartAngle <= 270 && $StartAngle >= 90 ) - $this->pChartObject->drawLine($Plots["OutX1"],$Plots["OutY1"],$Plots["OutX1"],$Plots["OutY1"]-$SliceHeight,$Settings); - if ( $EndAngle <= 270 && $EndAngle >= 90 ) - $this->pChartObject->drawLine($Plots["OutX2"],$Plots["OutY2"],$Plots["OutX2"],$Plots["OutY2"]-$SliceHeight,$Settings); - } - - - /* Draw the outer vertical slices */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; - $Settings["R"] = $Settings["R"]+$Cf; $Settings["G"] = $Settings["G"]+$Cf; $Settings["B"] = $Settings["B"]+$Cf; - - $Outer = TRUE; $Inner = FALSE; - $OuterPlotsA = ""; $OuterPlotsB = ""; $InnerPlotsA = ""; $InnerPlotsB = ""; - foreach($Plots["Angle"] as $ID => $Angle) - { - if ( $Angle == VOID ) - { $Outer = FALSE; $Inner = TRUE; } - elseif( $Outer ) - { - if ( ( $Angle > 90 && $Angle < 270 ) && isset($Plots["BottomPoly"][$ID*2]) ) - { - $Xo = $Plots["BottomPoly"][$ID*2]; - $Yo = $Plots["BottomPoly"][$ID*2+1]; - - $OuterPlotsA[] = $Xo; $OuterPlotsA[] = $Yo; - $OuterPlotsB[] = $Xo; $OuterPlotsB[] = $Yo-$SliceHeight; - } - } - } - if ( $OuterPlotsA != "" ) - { $OuterPlots = array_merge($OuterPlotsA,$this->arrayReverse($OuterPlotsB)); $this->pChartObject->drawPolygon($OuterPlots,$Settings); } - } - - $Slices = array_reverse($Slices); - $SliceColors = array_reverse($SliceColors); - - - /* Draw the top pie splice */ - foreach($Slices as $SliceID => $Plots) - { - $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; - $Settings["R"] = $Settings["R"]+$Cf*2; $Settings["G"] = $Settings["G"]+$Cf*2; $Settings["B"] = $Settings["B"]+$Cf*2; - - $this->pChartObject->drawPolygon($Plots["TopPoly"],$Settings); - - if ( $RecordImageMap ) { $this->pChartObject->addToImageMap("POLY",$this->arraySerialize($Plots["TopPoly"]),$this->pChartObject->toHTMLColor($Settings["R"],$Settings["G"],$Settings["B"]),$Data["Series"][$Data["Abscissa"]]["Data"][$SliceID],$Data["Series"][$DataSerie]["Data"][count($Slices)-$SliceID-1]); } - - foreach($Plots["AA"] as $Key => $Pos) - $this->pChartObject->drawAntialiasPixel($Pos[0],$Pos[1]-$SliceHeight,$Settings); - - $this->pChartObject->drawLine($Plots["InX1"],$Plots["InY1"]-$SliceHeight,$Plots["OutX2"],$Plots["OutY2"]-$SliceHeight,$Settings); - $this->pChartObject->drawLine($Plots["InX2"],$Plots["InY2"]-$SliceHeight,$Plots["OutX1"],$Plots["OutY1"]-$SliceHeight,$Settings); - } - - if ( $DrawLabels ) - { - $Offset = 360; - foreach($Values as $Key => $Value) - { - $StartAngle = $Offset; - $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } - - if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) - { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} - else - { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } - - $Angle = ($EndAngle - $Offset)/2 + $Offset; - $Xc = cos(($Angle-90)*PI/180) * ($OuterRadius+$DataGapRadius) + $X; - $Yc = sin(($Angle-90)*PI/180) * ($OuterRadius+$DataGapRadius)*$SkewFactor + $Y; - - if ( $WriteValues == PIE_VALUE_PERCENTAGE ) - $Label = $Display = round(( 100 / $SerieSum ) * $Value,$Precision)."%"; - elseif ( $WriteValues == PIE_VALUE_NATURAL ) - $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key]; - else - $Label = ""; - - if ( $LabelStacked ) - $this->writePieLabel($Xc,$Yc-$SliceHeight,$Label,$Angle,$Settings,TRUE,$X,$Y,$OuterRadius); - else - $this->writePieLabel($Xc,$Yc-$SliceHeight,$Label,$Angle,$Settings,FALSE); - - $Offset = $EndAngle - $DataGapAngle; $ID--; $Slice++; - } - } - if ( $DrawLabels && $LabelStacked ) { $this->writeShiftedLabels(); } - - $this->pChartObject->Shadow = $RestoreShadow; - - return(PIE_RENDERED); - } - - /* Serialize an array */ - function arraySerialize($Data) - { - $Result = ""; - foreach($Data as $Key => $Value) - { if ($Result == "") { $Result = floor($Value); } else { $Result = $Result.",".floor($Value); } } - - return($Result); - } - - /* Reverse an array */ - function arrayReverse($Plots) - { - $Result = ""; - - for($i=count($Plots)-1;$i>=0;$i=$i-2) - { $Result[] = $Plots[$i-1]; $Result[] = $Plots[$i]; } - - return($Result); - } - - /* Remove unused series & values */ - function clean0Values($Data,$Palette,$DataSerie,$AbscissaSerie) - { - $NewPalette = ""; $NewData = ""; $NewAbscissa = ""; - - /* Remove unused series */ - foreach($Data["Series"] as $SerieName => $SerieSettings) - { if ( $SerieName != $DataSerie && $SerieName != $AbscissaSerie ) { unset($Data["Series"][$SerieName]); } } - - /* Remove NULL values */ - foreach($Data["Series"][$DataSerie]["Data"] as $Key => $Value) - { - if ($Value != 0 ) - { - $NewData[] = $Value; - $NewAbscissa[] = $Data["Series"][$AbscissaSerie]["Data"][$Key]; - if ( isset($Palette[$Key]) ) { $NewPalette[] = $Palette[$Key]; } - } - } - $Data["Series"][$DataSerie]["Data"] = $NewData; - $Data["Series"][$AbscissaSerie]["Data"] = $NewAbscissa; - - return(array($Data,$NewPalette)); - } - } +pChartObject = $Object; + + /* Cache the pData object reference */ + $this->pDataObject = $pDataObject; + } + + /* Draw a pie chart */ + function draw2DPie($X,$Y,$Format="") + { + $Radius = isset($Format["Radius"]) ? $Format["Radius"] : 60; + $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0; + $DataGapAngle = isset($Format["DataGapAngle"]) ? $Format["DataGapAngle"] : 0; + $DataGapRadius = isset($Format["DataGapRadius"]) ? $Format["DataGapRadius"] : 0; + $SecondPass = isset($Format["SecondPass"]) ? $Format["SecondPass"] : TRUE; + $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 255; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 255; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 255; + $Shadow = isset($Format["Shadow"]) ? $Format["Shadow"] : FALSE; + $DrawLabels = isset($Format["DrawLabels"]) ? $Format["DrawLabels"] : FALSE; + $LabelStacked = isset($Format["LabelStacked"]) ? $Format["LabelStacked"] : FALSE; + $LabelColor = isset($Format["LabelColor"]) ? $Format["LabelColor"] : PIE_LABEL_COLOR_MANUAL; + $LabelR = isset($Format["LabelR"]) ? $Format["LabelR"] : 0; + $LabelG = isset($Format["LabelG"]) ? $Format["LabelG"] : 0; + $LabelB = isset($Format["LabelB"]) ? $Format["LabelB"] : 0; + $LabelAlpha = isset($Format["LabelAlpha"]) ? $Format["LabelAlpha"] : 100; + $WriteValues = isset($Format["WriteValues"]) ? $Format["WriteValues"] : NULL; + $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_OUTSIDE; + $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : 15; + $ValueSuffix = isset($Format["ValueSuffix"]) ? $Format["ValueSuffix"] : ""; + $ValueR = isset($Format["ValueR"]) ? $Format["ValueR"] : 255; + $ValueG = isset($Format["ValueG"]) ? $Format["ValueG"] : 255; + $ValueB = isset($Format["ValueB"]) ? $Format["ValueB"] : 255; + $ValueAlpha = isset($Format["ValueAlpha"]) ? $Format["ValueAlpha"] : 100; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + + /* Data Processing */ + $Data = $this->pDataObject->getData(); + $Palette = $this->pDataObject->getPalette(); + + /* Do we have an abscissa serie defined? */ + if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } + + /* Try to find the data serie */ + $DataSerie = ""; + foreach ($Data["Series"] as $SerieName => $SerieData) + { if ( $SerieName != $Data["Abscissa"]) { $DataSerie = $SerieName; } } + + /* Do we have data to compute? */ + if ( $DataSerie == "" ) { return(PIE_NO_DATASERIE); } + + /* Remove unused data */ + list($Data,$Palette) = $this->clean0Values($Data,$Palette,$DataSerie,$Data["Abscissa"]); + + /* Compute the pie sum */ + $SerieSum = $this->pDataObject->getSum($DataSerie); + + /* Do we have data to draw? */ + if ( $SerieSum == 0 ) { return(PIE_SUMISNULL); } + + /* Dump the real number of data to draw */ + $Values = ""; + foreach ($Data["Series"][$DataSerie]["Data"] as $Key => $Value) + { if ($Value != 0) { $Values[] = $Value; } } + + /* Compute the wasted angular space between series */ + if (count($Values)==1) { $WastedAngular = 0; } else { $WastedAngular = count($Values) * $DataGapAngle; } + + /* Compute the scale */ + $ScaleFactor = (360 - $WastedAngular) / $SerieSum; + + $RestoreShadow = $this->pChartObject->Shadow; + if ( $this->pChartObject->Shadow ) + { + $this->pChartObject->Shadow = FALSE; + + $ShadowFormat = $Format; $ShadowFormat["Shadow"] = TRUE; + $this->draw2DPie($X+$this->pChartObject->ShadowX,$Y+$this->pChartObject->ShadowY,$ShadowFormat); + } + + /* Draw the polygon pie elements */ + $Step = 360 / (2 * PI * $Radius); + $Offset = 0; $ID = 0; + foreach($Values as $Key => $Value) + { + if ( $Shadow ) + $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa); + else + { + if ( !isset($Palette[$ID]["R"]) ) { $Color = $this->pChartObject->getRandomColor(); $Palette[$ID] = $Color; $this->pDataObject->savePalette($ID,$Color); } + $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); + } + + if ( !$SecondPass && !$Shadow ) + { + if ( !$Border ) + $Settings["Surrounding"] = 10; + else + { $Settings["BorderR"] = $BorderR; $Settings["BorderG"] = $BorderG; $Settings["BorderB"] = $BorderB; } + } + + $Plots = ""; + $EndAngle = $Offset+($Value*$ScaleFactor); if ( $EndAngle > 360 ) { $EndAngle = 360; } + + $Angle = ($EndAngle - $Offset)/2 + $Offset; + if ($DataGapAngle == 0) + { $X0 = $X; $Y0 = $Y; } + else + { + $X0 = cos(($Angle-90)*PI/180) * $DataGapRadius + $X; + $Y0 = sin(($Angle-90)*PI/180) * $DataGapRadius + $Y; + } + + $Plots[] = $X0; $Plots[] = $Y0; + + + for($i=$Offset;$i<=$EndAngle;$i=$i+$Step) + { + $Xc = cos(($i-90)*PI/180) * $Radius + $X; + $Yc = sin(($i-90)*PI/180) * $Radius + $Y; + + if ( $SecondPass && ( $i<90 )) { $Yc++; } + if ( $SecondPass && ( $i>180 && $i<270 )) { $Xc++; } + if ( $SecondPass && ( $i>=270 )) { $Xc++; $Yc++; } + + $Plots[] = $Xc; $Plots[] = $Yc; + } + + $this->pChartObject->drawPolygon($Plots,$Settings); + if ( $RecordImageMap && !$Shadow ) { $this->pChartObject->addToImageMap("POLY",$this->arraySerialize($Plots),$this->pChartObject->toHTMLColor($Palette[$ID]["R"],$Palette[$ID]["G"],$Palette[$ID]["B"]),$Data["Series"][$Data["Abscissa"]]["Data"][$Key],$Value); } + + if ( $DrawLabels && !$Shadow && !$SecondPass ) + { + if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) + { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} + else + { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } + + $Angle = ($EndAngle - $Offset)/2 + $Offset; + $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; + $Yc = sin(($Angle-90)*PI/180) * $Radius + $Y; + + $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key]; + + if ( $LabelStacked ) + $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,TRUE,$X,$Y,$Radius); + else + $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,FALSE); + } + + $Offset = $i + $DataGapAngle; $ID++; + } + + /* Second pass to smooth the angles */ + if ( $SecondPass ) + { + $Step = 360 / (2 * PI * $Radius); + $Offset = 0; $ID = 0; + foreach($Values as $Key => $Value) + { + $FirstPoint = TRUE; + if ( $Shadow ) + $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa); + else + { + if ( $Border ) + $Settings = array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB); + else + $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); + } + + $EndAngle = $Offset+($Value*$ScaleFactor); if ( $EndAngle > 360 ) { $EndAngle = 360; } + + if ($DataGapAngle == 0) + { $X0 = $X; $Y0 = $Y; } + else + { + $Angle = ($EndAngle - $Offset)/2 + $Offset; + $X0 = cos(($Angle-90)*PI/180) * $DataGapRadius + $X; + $Y0 = sin(($Angle-90)*PI/180) * $DataGapRadius + $Y; + } + $Plots[] = $X0; $Plots[] = $Y0; + + for($i=$Offset;$i<=$EndAngle;$i=$i+$Step) + { + $Xc = cos(($i-90)*PI/180) * $Radius + $X; + $Yc = sin(($i-90)*PI/180) * $Radius + $Y; + + if ( $FirstPoint ) { $this->pChartObject->drawLine($Xc,$Yc,$X0,$Y0,$Settings); } { $FirstPoint = FALSE; } + + $this->pChartObject->drawAntialiasPixel($Xc,$Yc,$Settings); + } + $this->pChartObject->drawLine($Xc,$Yc,$X0,$Y0,$Settings); + + if ( $DrawLabels && !$Shadow ) + { + if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) + { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} + else + { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } + + $Angle = ($EndAngle - $Offset)/2 + $Offset; + $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; + $Yc = sin(($Angle-90)*PI/180) * $Radius + $Y; + + $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key]; + + if ( $LabelStacked ) + $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,TRUE,$X,$Y,$Radius); + else + $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,FALSE); + } + + $Offset = $i + $DataGapAngle; $ID++; + } + } + + if ( $WriteValues != NULL && !$Shadow ) + { + $Step = 360 / (2 * PI * $Radius); + $Offset = 0; $ID = count($Values)-1; + $Settings = array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"R"=>$ValueR,"G"=>$ValueG,"B"=>$ValueB,"Alpha"=>$ValueAlpha); + foreach($Values as $Key => $Value) + { + $EndAngle = ($Value*$ScaleFactor) + $Offset; if ( $EndAngle > 360 ) { $EndAngle = 0; } + $Angle = ($EndAngle - $Offset)/2 + $Offset; + + if ( $ValuePosition == PIE_VALUE_OUTSIDE ) + { + $Xc = cos(($Angle-90)*PI/180) * ($Radius+$ValuePadding) + $X; + $Yc = sin(($Angle-90)*PI/180) * ($Radius+$ValuePadding) + $Y; + } + else + { + $Xc = cos(($Angle-90)*PI/180) * ($Radius)/2 + $X; + $Yc = sin(($Angle-90)*PI/180) * ($Radius)/2 + $Y; + } + + if ( $WriteValues == PIE_VALUE_PERCENTAGE ) + $Display = round(( 100 / $SerieSum ) * $Value,$Precision)."%"; + elseif ( $WriteValues == PIE_VALUE_NATURAL ) + $Display = $Value.$ValueSuffix; + + $this->pChartObject->drawText($Xc,$Yc,$Display,$Settings); + + $Offset = $EndAngle + $DataGapAngle; $ID--; + } + } + + if ( $DrawLabels && $LabelStacked ) { $this->writeShiftedLabels(); } + + $this->pChartObject->Shadow = $RestoreShadow; + + return(PIE_RENDERED); + } + + /* Draw a 3D pie chart */ + function draw3DPie($X,$Y,$Format="") + { + /* Rendering layout */ + $Radius = isset($Format["Radius"]) ? $Format["Radius"] : 80; + $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0; + $SkewFactor = isset($Format["SkewFactor"]) ? $Format["SkewFactor"] : .5; + $SliceHeight = isset($Format["SliceHeight"]) ? $Format["SliceHeight"] : 20; + $DataGapAngle = isset($Format["DataGapAngle"]) ? $Format["DataGapAngle"] : 0; + $DataGapRadius = isset($Format["DataGapRadius"]) ? $Format["DataGapRadius"] : 0; + $SecondPass = isset($Format["SecondPass"]) ? $Format["SecondPass"] : TRUE; + $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE; + $Shadow = isset($Format["Shadow"]) ? $Format["Shadow"] : FALSE; + $DrawLabels = isset($Format["DrawLabels"]) ? $Format["DrawLabels"] : FALSE; + $LabelStacked = isset($Format["LabelStacked"]) ? $Format["LabelStacked"] : FALSE; + $LabelColor = isset($Format["LabelColor"]) ? $Format["LabelColor"] : PIE_LABEL_COLOR_MANUAL; + $LabelR = isset($Format["LabelR"]) ? $Format["LabelR"] : 0; + $LabelG = isset($Format["LabelG"]) ? $Format["LabelG"] : 0; + $LabelB = isset($Format["LabelB"]) ? $Format["LabelB"] : 0; + $LabelAlpha = isset($Format["LabelAlpha"]) ? $Format["LabelAlpha"] : 100; + $WriteValues = isset($Format["WriteValues"]) ? $Format["WriteValues"] : NULL; //PIE_VALUE_PERCENTAGE + $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_INSIDE; + $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : 15; + $ValueSuffix = isset($Format["ValueSuffix"]) ? $Format["ValueSuffix"] : ""; + $ValueR = isset($Format["ValueR"]) ? $Format["ValueR"] : 255; + $ValueG = isset($Format["ValueG"]) ? $Format["ValueG"] : 255; + $ValueB = isset($Format["ValueB"]) ? $Format["ValueB"] : 255; + $ValueAlpha = isset($Format["ValueAlpha"]) ? $Format["ValueAlpha"] : 100; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + + /* Error correction for overlaying rounded corners */ + if ( $SkewFactor < .5 ) { $SkewFactor = .5; } + + /* Data Processing */ + $Data = $this->pDataObject->getData(); + $Palette = $this->pDataObject->getPalette(); + + /* Do we have an abscissa serie defined? */ + if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } + + /* Try to find the data serie */ + $DataSerie = ""; + foreach ($Data["Series"] as $SerieName => $SerieData) + { if ( $SerieName != $Data["Abscissa"]) { $DataSerie = $SerieName; } } + + /* Do we have data to compute? */ + if ( $DataSerie == "" ) { return(PIE_NO_DATASERIE); } + + /* Remove unused data */ + list($Data,$Palette) = $this->clean0Values($Data,$Palette,$DataSerie,$Data["Abscissa"]); + + /* Compute the pie sum */ + $SerieSum = $this->pDataObject->getSum($DataSerie); + + /* Do we have data to draw? */ + if ( $SerieSum == 0 ) { return(PIE_SUMISNULL); } + + /* Dump the real number of data to draw */ + $Values = ""; + foreach ($Data["Series"][$DataSerie]["Data"] as $Key => $Value) + { if ($Value != 0) { $Values[] = $Value; } } + + /* Compute the wasted angular space between series */ + if (count($Values)==1) { $WastedAngular = 0; } else { $WastedAngular = count($Values) * $DataGapAngle; } + + /* Compute the scale */ + $ScaleFactor = (360 - $WastedAngular) / $SerieSum; + + $RestoreShadow = $this->pChartObject->Shadow; + if ( $this->pChartObject->Shadow ) { $this->pChartObject->Shadow = FALSE; } + + /* Draw the polygon pie elements */ + $Step = 360 / (2 * PI * $Radius); + $Offset = 360; $ID = count($Values)-1; + $Values = array_reverse($Values); + $Slice = 0; $Slices = ""; $SliceColors = ""; $Visible = ""; $SliceAngle = ""; + foreach($Values as $Key => $Value) + { + if ( !isset($Palette[$ID]["R"]) ) { $Color = $this->pChartObject->getRandomColor(); $Palette[$ID] = $Color; $this->pDataObject->savePalette($ID,$Color); } + $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); + + $SliceColors[$Slice] = $Settings; + + $StartAngle = $Offset; + $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } + + if ( $StartAngle > 180 ) { $Visible[$Slice]["Start"] = TRUE; } else { $Visible[$Slice]["Start"] = TRUE; } + if ( $EndAngle < 180 ) { $Visible[$Slice]["End"] = FALSE; } else { $Visible[$Slice]["End"] = TRUE; } + + if ($DataGapAngle == 0) + { $X0 = $X; $Y0 = $Y; } + else + { + $Angle = ($EndAngle - $Offset)/2 + $Offset; + $X0 = cos(($Angle-90)*PI/180) * $DataGapRadius + $X; + $Y0 = sin(($Angle-90)*PI/180) * $DataGapRadius*$SkewFactor + $Y; + } + $Slices[$Slice][] = $X0; $Slices[$Slice][] = $Y0; $SliceAngle[$Slice][] = 0; + + for($i=$Offset;$i>=$EndAngle;$i=$i-$Step) + { + $Xc = cos(($i-90)*PI/180) * $Radius + $X; + $Yc = sin(($i-90)*PI/180) * $Radius*$SkewFactor + $Y; + + if ( ($SecondPass || $RestoreShadow ) && ( $i<90 )) { $Yc++; } + if ( ($SecondPass || $RestoreShadow ) && ( $i>90 && $i<180 )) { $Xc++; } + if ( ($SecondPass || $RestoreShadow ) && ( $i>180 && $i<270 )) { $Xc++; } + if ( ($SecondPass || $RestoreShadow ) && ( $i>=270 )) { $Xc++; $Yc++; } + + $Slices[$Slice][] = $Xc; $Slices[$Slice][] = $Yc; $SliceAngle[$Slice][] = $i; + } + + $Offset = $i - $DataGapAngle; $ID--; $Slice++; + } + + /* Draw the bottom shadow if needed */ + if ( $RestoreShadow && ($this->pChartObject->ShadowX != 0 || $this->pChartObject->ShadowY !=0 )) + { + foreach($Slices as $SliceID => $Plots) + { + $ShadowPie = ""; + for($i=0;$ipChartObject->ShadowX; $ShadowPie[] = $Plots[$i+1]+$this->pChartObject->ShadowY; } + + $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa,"NoBorder"=>TRUE); + $this->pChartObject->drawPolygon($ShadowPie,$Settings); + } + + $Step = 360 / (2 * PI * $Radius); + $Offset = 360; + foreach($Values as $Key => $Value) + { + $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } + + for($i=$Offset;$i>=$EndAngle;$i=$i-$Step) + { + $Xc = cos(($i-90)*PI/180) * $Radius + $X + $this->pChartObject->ShadowX; + $Yc = sin(($i-90)*PI/180) * $Radius*$SkewFactor + $Y + $this->pChartObject->ShadowY; + + $this->pChartObject->drawAntialiasPixel($Xc,$Yc,$Settings); + } + + $Offset = $i - $DataGapAngle; $ID--; + } + } + + /* Draw the bottom pie splice */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; + $this->pChartObject->drawPolygon($Plots,$Settings); + + if ( $SecondPass ) + { + $Settings = $SliceColors[$SliceID]; + if ( $Border ) + { $Settings["R"]+= 30; $Settings["G"]+= 30; $Settings["B"]+= 30;; } + + if ( isset($SliceAngle[$SliceID][1]) ) /* Empty error handling */ + { + $Angle = $SliceAngle[$SliceID][1]; + $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; + $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y; + $this->pChartObject->drawLine($Plots[0],$Plots[1],$Xc,$Yc,$Settings); + + $Angle = $SliceAngle[$SliceID][count($SliceAngle[$SliceID])-1]; + $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; + $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y; + $this->pChartObject->drawLine($Plots[0],$Plots[1],$Xc,$Yc,$Settings); + } + } + } + + /* Draw the two vertical edges */ + $Slices = array_reverse($Slices); + $SliceColors = array_reverse($SliceColors); + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; + $Settings["R"]+= 10; $Settings["G"]+= 10; $Settings["B"]+= 10; $Settings["NoBorder"] = TRUE; + + if ( $Visible[$SliceID]["Start"] && isset($Plots[2])) /* Empty error handling */ + { + $this->pChartObject->drawLine($Plots[2],$Plots[3],$Plots[2],$Plots[3]- $SliceHeight,array("R"=>$Settings["R"],"G"=>$Settings["G"],"B"=>$Settings["B"])); + $Border = ""; + $Border[] = $Plots[0]; $Border[] = $Plots[1]; $Border[] = $Plots[0]; $Border[] = $Plots[1] - $SliceHeight; + $Border[] = $Plots[2]; $Border[] = $Plots[3] - $SliceHeight; $Border[] = $Plots[2]; $Border[] = $Plots[3]; + $this->pChartObject->drawPolygon($Border,$Settings); + } + } + + $Slices = array_reverse($Slices); + $SliceColors = array_reverse($SliceColors); + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; + $Settings["R"]+= 10; $Settings["G"]+= 10; $Settings["B"]+= 10; $Settings["NoBorder"] = TRUE; + if ( $Visible[$SliceID]["End"] ) + { + $this->pChartObject->drawLine($Plots[count($Plots)-2],$Plots[count($Plots)-1],$Plots[count($Plots)-2],$Plots[count($Plots)-1]- $SliceHeight,array("R"=>$Settings["R"],"G"=>$Settings["G"],"B"=>$Settings["B"])); + + $Border = ""; + $Border[] = $Plots[0]; $Border[] = $Plots[1]; $Border[] = $Plots[0]; $Border[] = $Plots[1] - $SliceHeight; + $Border[] = $Plots[count($Plots)-2]; $Border[] = $Plots[count($Plots)-1] - $SliceHeight; $Border[] = $Plots[count($Plots)-2]; $Border[] = $Plots[count($Plots)-1]; + $this->pChartObject->drawPolygon($Border,$Settings); + } + } + + /* Draw the rounded edges */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; + $Settings["R"]+= 10; $Settings["G"]+= 10; $Settings["B"]+= 10; $Settings["NoBorder"] = TRUE; + + for ($j=2;$j 90 ) + { + $Border = ""; + $Border[] = $Plots[$j]; $Border[] = $Plots[$j+1]; + $Border[] = $Plots[$j+2]; $Border[] = $Plots[$j+3]; + $Border[] = $Plots[$j+2]; $Border[] = $Plots[$j+3] - $SliceHeight; + $Border[] = $Plots[$j]; $Border[] = $Plots[$j+1] - $SliceHeight; + $this->pChartObject->drawPolygon($Border,$Settings); + } + } + + if ( $SecondPass ) + { + $Settings = $SliceColors[$SliceID]; + if ( $Border ) + { $Settings["R"]+= 30; $Settings["G"]+= 30; $Settings["B"]+= 30; } + + if ( isset($SliceAngle[$SliceID][1]) ) /* Empty error handling */ + { + $Angle = $SliceAngle[$SliceID][1]; + if ( $Angle < 270 && $Angle > 90 ) + { + $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; + $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y; + $this->pChartObject->drawLine($Xc,$Yc,$Xc,$Yc-$SliceHeight,$Settings); + } + } + + $Angle = $SliceAngle[$SliceID][count($SliceAngle[$SliceID])-1]; + if ( $Angle < 270 && $Angle > 90 ) + { + $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; + $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y; + $this->pChartObject->drawLine($Xc,$Yc,$Xc,$Yc-$SliceHeight,$Settings); + } + + if ( isset($SliceAngle[$SliceID][1]) && $SliceAngle[$SliceID][1] > 270 && $SliceAngle[$SliceID][count($SliceAngle[$SliceID])-1] < 270 ) + { + $Xc = cos((270-90)*PI/180) * $Radius + $X; + $Yc = sin((270-90)*PI/180) * $Radius*$SkewFactor + $Y; + $this->pChartObject->drawLine($Xc,$Yc,$Xc,$Yc-$SliceHeight,$Settings); + } + + if ( isset($SliceAngle[$SliceID][1]) && $SliceAngle[$SliceID][1] > 90 && $SliceAngle[$SliceID][count($SliceAngle[$SliceID])-1] < 90 ) + { + $Xc = cos((0)*PI/180) * $Radius + $X; + $Yc = sin((0)*PI/180) * $Radius*$SkewFactor + $Y; + $this->pChartObject->drawLine($Xc,$Yc,$Xc,$Yc-$SliceHeight,$Settings); + } + + } + } + + /* Draw the top splice */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; + $Settings["R"]+= 20; $Settings["G"]+= 20; $Settings["B"]+= 20; + + $Top = ""; + for($j=0;$jpChartObject->drawPolygon($Top,$Settings); + + if ( $RecordImageMap && !$Shadow ) { $this->pChartObject->addToImageMap("POLY",$this->arraySerialize($Top),$this->pChartObject->toHTMLColor($Settings["R"],$Settings["G"],$Settings["B"]),$Data["Series"][$Data["Abscissa"]]["Data"][count($Slices)-$SliceID-1],$Values[$SliceID]); } + } + + + /* Second pass to smooth the angles */ + if ( $SecondPass ) + { + $Step = 360 / (2 * PI * $Radius); + $Offset = 360; $ID = count($Values)-1; + foreach($Values as $Key => $Value) + { + $FirstPoint = TRUE; + if ( $Shadow ) + $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa); + else + { + if ( $Border ) + { $Settings = array("R"=>$Palette[$ID]["R"]+30,"G"=>$Palette[$ID]["G"]+30,"B"=>$Palette[$ID]["B"]+30,"Alpha"=>$Palette[$ID]["Alpha"]); } + else + $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); + } + + $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } + + if ($DataGapAngle == 0) + { $X0 = $X; $Y0 = $Y- $SliceHeight; } + else + { + $Angle = ($EndAngle - $Offset)/2 + $Offset; + $X0 = cos(($Angle-90)*PI/180) * $DataGapRadius + $X; + $Y0 = sin(($Angle-90)*PI/180) * $DataGapRadius*$SkewFactor + $Y - $SliceHeight; + } + $Plots[] = $X0; $Plots[] = $Y0; + + for($i=$Offset;$i>=$EndAngle;$i=$i-$Step) + { + $Xc = cos(($i-90)*PI/180) * $Radius + $X; + $Yc = sin(($i-90)*PI/180) * $Radius*$SkewFactor + $Y - $SliceHeight; + + if ( $FirstPoint ) { $this->pChartObject->drawLine($Xc,$Yc,$X0,$Y0,$Settings); } { $FirstPoint = FALSE; } + + $this->pChartObject->drawAntialiasPixel($Xc,$Yc,$Settings); + if ($i < 270 && $i > 90 ) { $this->pChartObject->drawAntialiasPixel($Xc,$Yc+$SliceHeight,$Settings); } + } + $this->pChartObject->drawLine($Xc,$Yc,$X0,$Y0,$Settings); + + $Offset = $i - $DataGapAngle; $ID--; + } + } + + if ( $WriteValues != NULL ) + { + $Step = 360 / (2 * PI * $Radius); + $Offset = 360; $ID = count($Values)-1; + $Settings = array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"R"=>$ValueR,"G"=>$ValueG,"B"=>$ValueB,"Alpha"=>$ValueAlpha); + foreach($Values as $Key => $Value) + { + $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } + + $Angle = ($EndAngle - $Offset)/2 + $Offset; + + if ( $ValuePosition == PIE_VALUE_OUTSIDE ) + { + $Xc = cos(($Angle-90)*PI/180) * ($Radius+$ValuePadding) + $X; + $Yc = sin(($Angle-90)*PI/180) * (($Radius*$SkewFactor)+$ValuePadding) + $Y - $SliceHeight; + } + else + { + $Xc = cos(($Angle-90)*PI/180) * ($Radius)/2 + $X; + $Yc = sin(($Angle-90)*PI/180) * ($Radius*$SkewFactor)/2 + $Y - $SliceHeight; + } + + if ( $WriteValues == PIE_VALUE_PERCENTAGE ) + $Display = round(( 100 / $SerieSum ) * $Value,$Precision)."%"; + elseif ( $WriteValues == PIE_VALUE_NATURAL ) + $Display = $Value.$ValueSuffix; + + $this->pChartObject->drawText($Xc,$Yc,$Display,$Settings); + + $Offset = $EndAngle - $DataGapAngle; $ID--; + } + } + + if ( $DrawLabels ) + { + $Step = 360 / (2 * PI * $Radius); + $Offset = 360; $ID = count($Values)-1; + foreach($Values as $Key => $Value) + { + if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) + { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} + else + { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } + + $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } + + $Angle = ($EndAngle - $Offset)/2 + $Offset; + $Xc = cos(($Angle-90)*PI/180) * $Radius + $X; + $Yc = sin(($Angle-90)*PI/180) * $Radius*$SkewFactor + $Y - $SliceHeight; + + if ( isset($Data["Series"][$Data["Abscissa"]]["Data"][$ID]) ) + { + $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$ID]; + + if ( $LabelStacked ) + $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,TRUE,$X,$Y,$Radius,TRUE); + else + $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,FALSE); + } + + $Offset = $EndAngle - $DataGapAngle; $ID--; + } + } + + if ( $DrawLabels && $LabelStacked ) { $this->writeShiftedLabels(); } + + $this->pChartObject->Shadow = $RestoreShadow; + + return(PIE_RENDERED); + } + + /* Draw the legend of pie chart */ + function drawPieLegend($X,$Y,$Format="") + { + $FontName = isset($Format["FontName"]) ? $Format["FontName"] : $this->pChartObject->FontName; + $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : $this->pChartObject->FontSize; + $FontR = isset($Format["FontR"]) ? $Format["FontR"] : $this->pChartObject->FontColorR; + $FontG = isset($Format["FontG"]) ? $Format["FontG"] : $this->pChartObject->FontColorG; + $FontB = isset($Format["FontB"]) ? $Format["FontB"] : $this->pChartObject->FontColorB; + $BoxSize = isset($Format["BoxSize"]) ? $Format["BoxSize"] : 5; + $Margin = isset($Format["Margin"]) ? $Format["Margin"] : 5; + $R = isset($Format["R"]) ? $Format["R"] : 200; + $G = isset($Format["G"]) ? $Format["G"] : 200; + $B = isset($Format["B"]) ? $Format["B"] : 200; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 255; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 255; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 255; + $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : NULL; + $Style = isset($Format["Style"]) ? $Format["Style"] : LEGEND_ROUND; + $Mode = isset($Format["Mode"]) ? $Format["Mode"] : LEGEND_VERTICAL; + + if ( $Surrounding != NULL ) { $BorderR = $R + $Surrounding; $BorderG = $G + $Surrounding; $BorderB = $B + $Surrounding; } + + $YStep = max($this->pChartObject->FontSize,$BoxSize) + 5; + $XStep = $BoxSize + 5; + + /* Data Processing */ + $Data = $this->pDataObject->getData(); + $Palette = $this->pDataObject->getPalette(); + + /* Do we have an abscissa serie defined? */ + if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } + + $Boundaries = ""; $Boundaries["L"] = $X; $Boundaries["T"] = $Y; $Boundaries["R"] = 0; $Boundaries["B"] = 0; $vY = $Y; $vX = $X; + foreach($Data["Series"][$Data["Abscissa"]]["Data"] as $Key => $Value) + { + $BoxArray = $this->pChartObject->getTextBox($vX+$BoxSize+4,$vY+$BoxSize/2,$FontName,$FontSize,0,$Value); + + if ( $Mode == LEGEND_VERTICAL ) + { + if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$BoxSize/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$BoxSize/2; } + if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } + if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$BoxSize/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$BoxSize/2; } + $vY=$vY+$YStep; + } + elseif ( $Mode == LEGEND_HORIZONTAL ) + { + if ( $Boundaries["T"] > $BoxArray[2]["Y"]+$BoxSize/2 ) { $Boundaries["T"] = $BoxArray[2]["Y"]+$BoxSize/2; } + if ( $Boundaries["R"] < $BoxArray[1]["X"]+2 ) { $Boundaries["R"] = $BoxArray[1]["X"]+2; } + if ( $Boundaries["B"] < $BoxArray[1]["Y"]+2+$BoxSize/2 ) { $Boundaries["B"] = $BoxArray[1]["Y"]+2+$BoxSize/2; } + $vX=$Boundaries["R"]+$XStep; + } + } + $vY=$vY-$YStep; $vX=$vX-$XStep; + + $TopOffset = $Y - $Boundaries["T"]; + if ( $Boundaries["B"]-($vY+$BoxSize) < $TopOffset ) { $Boundaries["B"] = $vY+$BoxSize+$TopOffset; } + + if ( $Style == LEGEND_ROUND ) + $this->pChartObject->drawRoundedFilledRectangle($Boundaries["L"]-$Margin,$Boundaries["T"]-$Margin,$Boundaries["R"]+$Margin,$Boundaries["B"]+$Margin,$Margin,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); + elseif ( $Style == LEGEND_BOX ) + $this->pChartObject->drawFilledRectangle($Boundaries["L"]-$Margin,$Boundaries["T"]-$Margin,$Boundaries["R"]+$Margin,$Boundaries["B"]+$Margin,array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"BorderR"=>$BorderR,"BorderG"=>$BorderG,"BorderB"=>$BorderB)); + + $RestoreShadow = $this->pChartObject->Shadow; $this->pChartObject->Shadow = FALSE; + foreach($Data["Series"][$Data["Abscissa"]]["Data"] as $Key => $Value) + { + $R = $Palette[$Key]["R"]; $G = $Palette[$Key]["G"]; $B = $Palette[$Key]["B"]; + + $this->pChartObject->drawFilledRectangle($X+1,$Y+1,$X+$BoxSize+1,$Y+$BoxSize+1,array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>20)); + $this->pChartObject->drawFilledRectangle($X,$Y,$X+$BoxSize,$Y+$BoxSize,array("R"=>$R,"G"=>$G,"B"=>$B,"Surrounding"=>20)); + if ( $Mode == LEGEND_VERTICAL ) + { + $this->pChartObject->drawText($X+$BoxSize+4,$Y+$BoxSize/2,$Value,array("R"=>$FontR,"G"=>$FontG,"B"=>$FontB,"Align"=>TEXT_ALIGN_MIDDLELEFT,"FontName"=>$FontName,"FontSize"=>$FontSize)); + $Y=$Y+$YStep; + } + elseif ( $Mode == LEGEND_HORIZONTAL ) + { + $BoxArray = $this->pChartObject->drawText($X+$BoxSize+4,$Y+$BoxSize/2,$Value,array("R"=>$FontR,"G"=>$FontG,"B"=>$FontB,"Align"=>TEXT_ALIGN_MIDDLELEFT,"FontName"=>$FontName,"FontSize"=>$FontSize)); + $X=$BoxArray[1]["X"]+2+$XStep; + } + } + + $this->Shadow = $RestoreShadow; + } + + /* Set the color of the specified slice */ + function setSliceColor($SliceID,$Format="") + { + $R = isset($Format["R"]) ? $Format["R"] : 0; + $G = isset($Format["G"]) ? $Format["G"] : 0; + $B = isset($Format["B"]) ? $Format["B"] : 0; + $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; + + $this->pDataObject->Palette[$SliceID]["R"] = $R; + $this->pDataObject->Palette[$SliceID]["G"] = $G; + $this->pDataObject->Palette[$SliceID]["B"] = $B; + $this->pDataObject->Palette[$SliceID]["Alpha"] = $Alpha; + } + + /* Internally used compute the label positions */ + function writePieLabel($X,$Y,$Label,$Angle,$Settings,$Stacked,$Xc=0,$Yc=0,$Radius=0,$Reversed=FALSE) + { + $LabelOffset = 30; + $FontName = $this->pChartObject->FontName; + $FontSize = $this->pChartObject->FontSize; + + if ( !$Stacked ) + { + $Settings["Angle"] = 360-$Angle; + $Settings["Length"] = 25; + $Settings["Size"] = 8; + + $this->pChartObject->drawArrowLabel($X,$Y," ".$Label." ",$Settings); + } + else + { + $X2 = cos(deg2rad($Angle-90))*20+$X; + $Y2 = sin(deg2rad($Angle-90))*20+$Y; + + $TxtPos = $this->pChartObject->getTextBox($X,$Y,$FontName,$FontSize,0,$Label); + $Height = $TxtPos[0]["Y"] - $TxtPos[2]["Y"]; + $YTop = $Y2 - $Height/2 - 2; + $YBottom = $Y2 + $Height/2 + 2; + + if ( $this->LabelPos != "" ) + { + $Done = FALSE; + foreach($this->LabelPos as $Key => $Settings) + { + if ( !$Done ) + { + if ( $Angle <= 90 && (($YTop >= $Settings["YTop"] && $YTop <= $Settings["YBottom"]) || ($YBottom >= $Settings["YTop"] && $YBottom <= $Settings["YBottom"]))) + { $this->shift(0,180,-($Height+2),$Reversed); $Done = TRUE; } + if ( $Angle > 90 && $Angle <= 180 && (($YTop >= $Settings["YTop"] && $YTop <= $Settings["YBottom"]) || ($YBottom >= $Settings["YTop"] && $YBottom <= $Settings["YBottom"]))) + { $this->shift(0,180,-($Height+2),$Reversed); $Done = TRUE; } + if ( $Angle > 180 && $Angle <= 270 && (($YTop >= $Settings["YTop"] && $YTop <= $Settings["YBottom"]) || ($YBottom >= $Settings["YTop"] && $YBottom <= $Settings["YBottom"]))) + { $this->shift(180,360,($Height+2),$Reversed); $Done = TRUE; } + if ( $Angle > 270 && $Angle <= 360 && (($YTop >= $Settings["YTop"] && $YTop <= $Settings["YBottom"]) || ($YBottom >= $Settings["YTop"] && $YBottom <= $Settings["YBottom"]))) + { $this->shift(180,360,($Height+2),$Reversed); $Done = TRUE; } + } + } + } + + $LabelSettings = array("YTop"=>$YTop,"YBottom"=>$YBottom,"Label"=>$Label,"Angle"=>$Angle,"X1"=>$X,"Y1"=>$Y,"X2"=>$X2,"Y2"=>$Y2); + if ( $Angle <= 180 ) { $LabelSettings["X3"] = $Xc+$Radius+$LabelOffset; } + if ( $Angle > 180 ) { $LabelSettings["X3"] = $Xc-$Radius-$LabelOffset; } + $this->LabelPos[] = $LabelSettings; + } + } + + /* Internally used to shift label positions */ + function shift($StartAngle,$EndAngle,$Offset,$Reversed) + { + if ( $Reversed ) { $Offset = -$Offset; } + foreach($this->LabelPos as $Key => $Settings) + { + if ( $Settings["Angle"] > $StartAngle && $Settings["Angle"] <= $EndAngle ) { $this->LabelPos[$Key]["YTop"] = $Settings["YTop"] + $Offset; $this->LabelPos[$Key]["YBottom"] = $Settings["YBottom"] + $Offset; $this->LabelPos[$Key]["Y2"] = $Settings["Y2"] + $Offset; } + } + } + + /* Internally used to write the re-computed labels */ + function writeShiftedLabels() + { + if ( $this->LabelPos == "" ) { return(0); } + foreach($this->LabelPos as $Key => $Settings) + { + $X1 = $Settings["X1"]; $Y1 = $Settings["Y1"]; + $X2 = $Settings["X2"]; $Y2 = $Settings["Y2"]; + $X3 = $Settings["X3"]; + $Angle = $Settings["Angle"]; + $Label = $Settings["Label"]; + + $this->pChartObject->drawArrow($X2,$Y2,$X1,$Y1,array("Size"=>8)); + if ( $Angle <= 180 ) + { + $this->pChartObject->drawLine($X2,$Y2,$X3,$Y2); + $this->pChartObject->drawText($X3+2,$Y2,$Label,array("Align"=>TEXT_ALIGN_MIDDLELEFT)); + } + else + { + $this->pChartObject->drawLine($X2,$Y2,$X3,$Y2); + $this->pChartObject->drawText($X3-2,$Y2,$Label,array("Align"=>TEXT_ALIGN_MIDDLERIGHT)); + } + } + } + + /* Draw a ring chart */ + function draw2DRing($X,$Y,$Format="") + { + $OuterRadius = isset($Format["Radius"]) ? $Format["Radius"] : 60; + $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0; + $InnerRadius = isset($Format["Radius"]) ? $Format["Radius"] : 30; + $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE; + $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 255; + $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 255; + $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 255; + $BorderAlpha = isset($Format["BorderAlpha"]) ? $Format["BorderAlpha"] : 100; + $Shadow = isset($Format["Shadow"]) ? $Format["Shadow"] : FALSE; + $DrawLabels = isset($Format["DrawLabels"]) ? $Format["DrawLabels"] : FALSE; + $LabelStacked = isset($Format["LabelStacked"]) ? $Format["LabelStacked"] : FALSE; + $LabelColor = isset($Format["LabelColor"]) ? $Format["LabelColor"] : PIE_LABEL_COLOR_MANUAL; + $LabelR = isset($Format["LabelR"]) ? $Format["LabelR"] : 0; + $LabelG = isset($Format["LabelG"]) ? $Format["LabelG"] : 0; + $LabelB = isset($Format["LabelB"]) ? $Format["LabelB"] : 0; + $LabelAlpha = isset($Format["LabelAlpha"]) ? $Format["LabelAlpha"] : 100; + $WriteValues = isset($Format["WriteValues"]) ? $Format["WriteValues"] : NULL; //PIE_VALUE_PERCENTAGE + $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : 5; + $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_OUTSIDE; + $ValueSuffix = isset($Format["ValueSuffix"]) ? $Format["ValueSuffix"] : ""; + $ValueR = isset($Format["ValueR"]) ? $Format["ValueR"] : 255; + $ValueG = isset($Format["ValueG"]) ? $Format["ValueG"] : 255; + $ValueB = isset($Format["ValueB"]) ? $Format["ValueB"] : 255; + $ValueAlpha = isset($Format["ValueAlpha"]) ? $Format["ValueAlpha"] : 100; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + + /* Data Processing */ + $Data = $this->pDataObject->getData(); + $Palette = $this->pDataObject->getPalette(); + + /* Do we have an abscissa serie defined? */ + if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } + + /* Try to find the data serie */ + $DataSerie = ""; + foreach ($Data["Series"] as $SerieName => $SerieData) + { if ( $SerieName != $Data["Abscissa"]) { $DataSerie = $SerieName; } } + + /* Do we have data to compute? */ + if ( $DataSerie == "" ) { return(PIE_NO_DATASERIE); } + + /* Remove unused data */ + list($Data,$Palette) = $this->clean0Values($Data,$Palette,$DataSerie,$Data["Abscissa"]); + + /* Compute the pie sum */ + $SerieSum = $this->pDataObject->getSum($DataSerie); + + /* Do we have data to draw? */ + if ( $SerieSum == 0 ) { return(PIE_SUMISNULL); } + + /* Dump the real number of data to draw */ + $Values = ""; + foreach ($Data["Series"][$DataSerie]["Data"] as $Key => $Value) + { if ($Value != 0) { $Values[] = $Value; } } + + /* Compute the wasted angular space between series */ + if (count($Values)==1) { $WastedAngular = 0; } else { $WastedAngular = 0; } // count($Values) + + /* Compute the scale */ + $ScaleFactor = (360 - $WastedAngular) / $SerieSum; + + $RestoreShadow = $this->pChartObject->Shadow; + if ( $this->pChartObject->Shadow ) + { + $this->pChartObject->Shadow = FALSE; + + $ShadowFormat = $Format; $ShadowFormat["Shadow"] = TRUE; + $this->draw2DRing($X+$this->pChartObject->ShadowX,$Y+$this->pChartObject->ShadowY,$ShadowFormat); + } + + /* Draw the polygon pie elements */ + $Step = 360 / (2 * PI * $OuterRadius); + $Offset = 0; $ID = 0; + foreach($Values as $Key => $Value) + { + if ( $Shadow ) + { + $Settings = array("R"=>$this->pChartObject->ShadowR,"G"=>$this->pChartObject->ShadowG,"B"=>$this->pChartObject->ShadowB,"Alpha"=>$this->pChartObject->Shadowa); + $BorderColor = $Settings; + } + else + { + if ( !isset($Palette[$ID]["R"]) ) { $Color = $this->pChartObject->getRandomColor(); $Palette[$ID] = $Color; $this->pDataObject->savePalette($ID,$Color); } + $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); + + if ( $Border ) + $BorderColor = array("R"=>$BorderR,"G"=>$BorderG,"B"=>$BorderB,"Alpha"=>$BorderAlpha); + else + $BorderColor = $Settings; + } + + $Plots = ""; $Boundaries = ""; $AAPixels = ""; + $EndAngle = $Offset+($Value*$ScaleFactor); if ( $EndAngle > 360 ) { $EndAngle = 360; } + for($i=$Offset;$i<=$EndAngle;$i=$i+$Step) + { + $Xc = cos(($i-90)*PI/180) * $OuterRadius + $X; + $Yc = sin(($i-90)*PI/180) * $OuterRadius + $Y; + + if ( !isset($Boundaries[0]["X1"]) ) { $Boundaries[0]["X1"] = $Xc; $Boundaries[0]["Y1"] = $Yc; } + $AAPixels[] = array($Xc,$Yc); + + if ( $i<90 ) { $Yc++; } + if ( $i>180 && $i<270 ) { $Xc++; } + if ( $i>=270 ) { $Xc++; $Yc++; } + + $Plots[] = $Xc; $Plots[] = $Yc; + } + $Boundaries[1]["X1"] = $Xc; $Boundaries[1]["Y1"] = $Yc; + $Lasti = $EndAngle; + + for($i=$EndAngle;$i>=$Offset;$i=$i-$Step) + { + $Xc = cos(($i-90)*PI/180) * ($InnerRadius-1) + $X; + $Yc = sin(($i-90)*PI/180) * ($InnerRadius-1) + $Y; + + if ( !isset($Boundaries[1]["X2"]) ) { $Boundaries[1]["X2"] = $Xc; $Boundaries[1]["Y2"] = $Yc; } + $AAPixels[] = array($Xc,$Yc); + + $Xc = cos(($i-90)*PI/180) * $InnerRadius + $X; + $Yc = sin(($i-90)*PI/180) * $InnerRadius + $Y; + + if ( $i<90 ) { $Yc++; } + if ( $i>180 && $i<270 ) { $Xc++; } + if ( $i>=270 ) { $Xc++; $Yc++; } + + $Plots[] = $Xc; $Plots[] = $Yc; + } + $Boundaries[0]["X2"] = $Xc; $Boundaries[0]["Y2"] = $Yc; + + /* Draw the polygon */ + $this->pChartObject->drawPolygon($Plots,$Settings); + if ( $RecordImageMap && !$Shadow ) { $this->pChartObject->addToImageMap("POLY",$this->arraySerialize($Plots),$this->pChartObject->toHTMLColor($Palette[$ID]["R"],$Palette[$ID]["G"],$Palette[$ID]["B"]),$Data["Series"][$Data["Abscissa"]]["Data"][$Key],$Value); } + + /* Smooth the edges using AA */ + foreach($AAPixels as $iKey => $Pos ) { $this->pChartObject->drawAntialiasPixel($Pos[0],$Pos[1],$BorderColor); } + $this->pChartObject->drawLine($Boundaries[0]["X1"],$Boundaries[0]["Y1"],$Boundaries[0]["X2"],$Boundaries[0]["Y2"],$BorderColor); + $this->pChartObject->drawLine($Boundaries[1]["X1"],$Boundaries[1]["Y1"],$Boundaries[1]["X2"],$Boundaries[1]["Y2"],$BorderColor); + + if ( $DrawLabels && !$Shadow ) + { + if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) + { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} + else + { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } + + $Angle = ($EndAngle - $Offset)/2 + $Offset; + $Xc = cos(($Angle-90)*PI/180) * $OuterRadius + $X; + $Yc = sin(($Angle-90)*PI/180) * $OuterRadius + $Y; + + $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key]; + + if ( $LabelStacked ) + $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,TRUE,$X,$Y,$OuterRadius); + else + $this->writePieLabel($Xc,$Yc,$Label,$Angle,$Settings,FALSE); + } + + $Offset = $Lasti; $ID++; + } + + if ( $DrawLabels && $LabelStacked ) { $this->writeShiftedLabels(); } + + if ( $WriteValues && !$Shadow ) + { + $Step = 360 / (2 * PI * $OuterRadius); + $Offset = 0; + foreach($Values as $Key => $Value) + { + $EndAngle = $Offset+($Value*$ScaleFactor); + if ( $EndAngle > 360 ) { $EndAngle = 360; } + + $Angle = $Offset+($Value*$ScaleFactor)/2; + if ( $ValuePosition == PIE_VALUE_OUTSIDE ) + { + $Xc = cos(($Angle-90)*PI/180) * ($OuterRadius+$ValuePadding) + $X; + $Yc = sin(($Angle-90)*PI/180) * ($OuterRadius+$ValuePadding) + $Y; + if ( $Angle >=0 && $Angle <= 90 ) { $Align = TEXT_ALIGN_BOTTOMLEFT; } + if ( $Angle > 90 && $Angle <= 180 ) { $Align = TEXT_ALIGN_TOPLEFT; } + if ( $Angle > 180 && $Angle <= 270 ) { $Align = TEXT_ALIGN_TOPRIGHT; } + if ( $Angle > 270 ) { $Align = TEXT_ALIGN_BOTTOMRIGHT; } + } + else + { + $Xc = cos(($Angle-90)*PI/180) * (($OuterRadius-$InnerRadius)/2+$InnerRadius) + $X; + $Yc = sin(($Angle-90)*PI/180) * (($OuterRadius-$InnerRadius)/2+$InnerRadius) + $Y; + $Align = TEXT_ALIGN_MIDDLEMIDDLE; + } + + if ( $WriteValues == PIE_VALUE_PERCENTAGE ) + $Display = round(( 100 / $SerieSum ) * $Value,$Precision)."%"; + elseif ( $WriteValues == PIE_VALUE_NATURAL ) + $Display = $Value.$ValueSuffix; + else + $Label = ""; + + $this->pChartObject->drawText($Xc,$Yc,$Display,array("Align"=>$Align,"R"=>$ValueR,"G"=>$ValueG,"B"=>$ValueB)); + $Offset = $EndAngle; + } + } + + $this->pChartObject->Shadow = $RestoreShadow; + + return(PIE_RENDERED); + } + + /* Draw a 3D ring chart */ + function draw3DRing($X,$Y,$Format="") + { + $OuterRadius = isset($Format["OuterRadius"]) ? $Format["OuterRadius"] : 100; + $Precision = isset($Format["Precision"]) ? $Format["Precision"] : 0; + $InnerRadius = isset($Format["InnerRadius"]) ? $Format["InnerRadius"] : 30; + $SkewFactor = isset($Format["SkewFactor"]) ? $Format["SkewFactor"] : .6; + $SliceHeight = isset($Format["SliceHeight"]) ? $Format["SliceHeight"] : 10; + $DataGapAngle = isset($Format["DataGapAngle"]) ? $Format["DataGapAngle"] : 10; + $DataGapRadius = isset($Format["DataGapRadius"]) ? $Format["DataGapRadius"] : 10; + $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE; + $Shadow = isset($Format["Shadow"]) ? $Format["Shadow"] : FALSE; + $DrawLabels = isset($Format["DrawLabels"]) ? $Format["DrawLabels"] : FALSE; + $LabelStacked = isset($Format["LabelStacked"]) ? $Format["LabelStacked"] : FALSE; + $LabelColor = isset($Format["LabelColor"]) ? $Format["LabelColor"] : PIE_LABEL_COLOR_MANUAL; + $LabelR = isset($Format["LabelR"]) ? $Format["LabelR"] : 0; + $LabelG = isset($Format["LabelG"]) ? $Format["LabelG"] : 0; + $LabelB = isset($Format["LabelB"]) ? $Format["LabelB"] : 0; + $LabelAlpha = isset($Format["LabelAlpha"]) ? $Format["LabelAlpha"] : 100; + $Cf = isset($Format["Cf"]) ? $Format["Cf"] : 20; + $WriteValues = isset($Format["WriteValues"]) ? $Format["WriteValues"] : PIE_VALUE_NATURAL; + $ValuePadding = isset($Format["ValuePadding"]) ? $Format["ValuePadding"] : $SliceHeight + 15; + $ValuePosition = isset($Format["ValuePosition"]) ? $Format["ValuePosition"] : PIE_VALUE_OUTSIDE; + $ValueSuffix = isset($Format["ValueSuffix"]) ? $Format["ValueSuffix"] : ""; + $ValueR = isset($Format["ValueR"]) ? $Format["ValueR"] : 255; + $ValueG = isset($Format["ValueG"]) ? $Format["ValueG"] : 255; + $ValueB = isset($Format["ValueB"]) ? $Format["ValueB"] : 255; + $ValueAlpha = isset($Format["ValueAlpha"]) ? $Format["ValueAlpha"] : 100; + $RecordImageMap = isset($Format["RecordImageMap"]) ? $Format["RecordImageMap"] : FALSE; + + /* Error correction for overlaying rounded corners */ + if ( $SkewFactor < .5 ) { $SkewFactor = .5; } + + /* Data Processing */ + $Data = $this->pDataObject->getData(); + $Palette = $this->pDataObject->getPalette(); + + /* Do we have an abscissa serie defined? */ + if ( $Data["Abscissa"] == "" ) { return(PIE_NO_ABSCISSA); } + + /* Try to find the data serie */ + $DataSerie = ""; + foreach ($Data["Series"] as $SerieName => $SerieData) + { if ( $SerieName != $Data["Abscissa"]) { $DataSerie = $SerieName; } } + + /* Do we have data to compute? */ + if ( $DataSerie == "" ) { return(PIE_NO_DATASERIE); } + + /* Remove unused data */ + list($Data,$Palette) = $this->clean0Values($Data,$Palette,$DataSerie,$Data["Abscissa"]); + + /* Compute the pie sum */ + $SerieSum = $this->pDataObject->getSum($DataSerie); + + /* Do we have data to draw? */ + if ( $SerieSum == 0 ) { return(PIE_SUMISNULL); } + + /* Dump the real number of data to draw */ + $Values = ""; + foreach ($Data["Series"][$DataSerie]["Data"] as $Key => $Value) + { if ($Value != 0) { $Values[] = $Value; } } + + /* Compute the wasted angular space between series */ + if (count($Values)==1) { $WastedAngular = 0; } else { $WastedAngular = count($Values) * $DataGapAngle; } + + /* Compute the scale */ + $ScaleFactor = (360 - $WastedAngular) / $SerieSum; + + $RestoreShadow = $this->pChartObject->Shadow; + if ( $this->pChartObject->Shadow ) { $this->pChartObject->Shadow = FALSE; } + + /* Draw the polygon ring elements */ + $Offset = 360; $ID = count($Values)-1; + $Values = array_reverse($Values); + $Slice = 0; $Slices = ""; $SliceColors = ""; $Visible = ""; $SliceAngle = ""; + foreach($Values as $Key => $Value) + { + if ( !isset($Palette[$ID]["R"]) ) { $Color = $this->pChartObject->getRandomColor(); $Palette[$ID] = $Color; $this->pDataObject->savePalette($ID,$Color); } + $Settings = array("R"=>$Palette[$ID]["R"],"G"=>$Palette[$ID]["G"],"B"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]); + + $SliceColors[$Slice] = $Settings; + + $StartAngle = $Offset; + $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } + + if ( $StartAngle > 180 ) { $Visible[$Slice]["Start"] = TRUE; } else { $Visible[$Slice]["Start"] = TRUE; } + if ( $EndAngle < 180 ) { $Visible[$Slice]["End"] = FALSE; } else { $Visible[$Slice]["End"] = TRUE; } + + $Step = (360 / (2 * PI * $OuterRadius))/2; + $OutX1 = VOID; $OutY1 = VOID; + for($i=$Offset;$i>=$EndAngle;$i=$i-$Step) + { + $Xc = cos(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius-2) + $X; + $Yc = sin(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius-2)*$SkewFactor + $Y; + $Slices[$Slice]["AA"][] = array($Xc,$Yc); + + $Xc = cos(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius-1) + $X; + $Yc = sin(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius-1)*$SkewFactor + $Y; + $Slices[$Slice]["AA"][] = array($Xc,$Yc); + + $Xc = cos(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius) + $X; + $Yc = sin(($i-90)*PI/180) * ($OuterRadius+$DataGapRadius)*$SkewFactor + $Y; + $this->pChartObject->drawAntialiasPixel($Xc,$Yc,$Settings); + + if ( $OutX1 == VOID ) { $OutX1 = $Xc; $OutY1 = $Yc; } + + if ( $i<90 ) { $Yc++; } + if ( $i>90 && $i<180 ) { $Xc++; } + if ( $i>180 && $i<270 ) { $Xc++; } + if ( $i>=270 ) { $Xc++; $Yc++; } + + $Slices[$Slice]["BottomPoly"][] = floor($Xc); $Slices[$Slice]["BottomPoly"][] = floor($Yc); + $Slices[$Slice]["TopPoly"][] = floor($Xc); $Slices[$Slice]["TopPoly"][] = floor($Yc)-$SliceHeight; + $Slices[$Slice]["Angle"][] = $i; + } + $OutX2 = $Xc; $OutY2 = $Yc; + + $Slices[$Slice]["Angle"][] = VOID; + $Lasti = $i; + + $Step = (360 / (2 * PI * $InnerRadius))/2; + $InX1 = VOID; $InY1 = VOID; + for($i=$EndAngle;$i<=$Offset;$i=$i+$Step) + { + $Xc = cos(($i-90)*PI/180) * ($InnerRadius+$DataGapRadius-1) + $X; + $Yc = sin(($i-90)*PI/180) * ($InnerRadius+$DataGapRadius-1)*$SkewFactor + $Y; + $Slices[$Slice]["AA"][] = array($Xc,$Yc); + + $Xc = cos(($i-90)*PI/180) * ($InnerRadius+$DataGapRadius) + $X; + $Yc = sin(($i-90)*PI/180) * ($InnerRadius+$DataGapRadius)*$SkewFactor + $Y; + $Slices[$Slice]["AA"][] = array($Xc,$Yc); + + if ( $InX1 == VOID ) { $InX1 = $Xc; $InY1 = $Yc; } + + if ( $i<90 ) { $Yc++; } + if ( $i>90 && $i<180 ) { $Xc++; } + if ( $i>180 && $i<270 ) { $Xc++; } + if ( $i>=270 ) { $Xc++; $Yc++; } + + $Slices[$Slice]["BottomPoly"][] = floor($Xc); $Slices[$Slice]["BottomPoly"][] = floor($Yc); + $Slices[$Slice]["TopPoly"][] = floor($Xc); $Slices[$Slice]["TopPoly"][] = floor($Yc)-$SliceHeight; + $Slices[$Slice]["Angle"][] = $i; + } + $InX2 = $Xc; $InY2 = $Yc; + + $Slices[$Slice]["InX1"] = $InX1; $Slices[$Slice]["InY1"] = $InY1; + $Slices[$Slice]["InX2"] = $InX2; $Slices[$Slice]["InY2"] = $InY2; + $Slices[$Slice]["OutX1"] = $OutX1; $Slices[$Slice]["OutY1"] = $OutY1; + $Slices[$Slice]["OutX2"] = $OutX2; $Slices[$Slice]["OutY2"] = $OutY2; + + $Offset = $Lasti - $DataGapAngle; $ID--; $Slice++; + } + + /* Draw the bottom pie splice */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; + $this->pChartObject->drawPolygon($Plots["BottomPoly"],$Settings); + + foreach($Plots["AA"] as $Key => $Pos) + $this->pChartObject->drawAntialiasPixel($Pos[0],$Pos[1],$Settings); + + $this->pChartObject->drawLine($Plots["InX1"],$Plots["InY1"],$Plots["OutX2"],$Plots["OutY2"],$Settings); + $this->pChartObject->drawLine($Plots["InX2"],$Plots["InY2"],$Plots["OutX1"],$Plots["OutY1"],$Settings); + } + + $Slices = array_reverse($Slices); + $SliceColors = array_reverse($SliceColors); + + /* Draw the vertical edges (semi-visible) */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; + $Settings["R"] = $Settings["R"]+$Cf; $Settings["G"] = $Settings["G"]+$Cf; $Settings["B"] = $Settings["B"]+$Cf; + + $StartAngle = $Plots["Angle"][0]; + foreach($Plots["Angle"] as $Key =>$Angle) { if ($Angle == VOID) { $EndAngle = $Plots["Angle"][$Key-1]; } } + + if ( $StartAngle >= 270 || $StartAngle <= 90 ) + $this->pChartObject->drawLine($Plots["OutX1"],$Plots["OutY1"],$Plots["OutX1"],$Plots["OutY1"]-$SliceHeight,$Settings); + if ( $StartAngle >= 270 || $StartAngle <= 90 ) + $this->pChartObject->drawLine($Plots["OutX2"],$Plots["OutY2"],$Plots["OutX2"],$Plots["OutY2"]-$SliceHeight,$Settings); + + $this->pChartObject->drawLine($Plots["InX1"],$Plots["InY1"],$Plots["InX1"],$Plots["InY1"]-$SliceHeight,$Settings); + $this->pChartObject->drawLine($Plots["InX2"],$Plots["InY2"],$Plots["InX2"],$Plots["InY2"]-$SliceHeight,$Settings); + } + + /* Draw the inner vertical slices */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; + $Settings["R"] = $Settings["R"]+$Cf; $Settings["G"] = $Settings["G"]+$Cf; $Settings["B"] = $Settings["B"]+$Cf; + + $Outer = TRUE; $Inner = FALSE; + $InnerPlotsA = ""; $InnerPlotsB = ""; + foreach($Plots["Angle"] as $ID => $Angle) + { + if ( $Angle == VOID ) + { $Outer = FALSE; $Inner = TRUE; } + elseif( $Inner ) + { + if (( $Angle < 90 || $Angle > 270 ) && isset($Plots["BottomPoly"][$ID*2]) ) + { + $Xo = $Plots["BottomPoly"][$ID*2]; + $Yo = $Plots["BottomPoly"][$ID*2+1]; + + $InnerPlotsA[] = $Xo; $InnerPlotsA[] = $Yo; + $InnerPlotsB[] = $Xo; $InnerPlotsB[] = $Yo-$SliceHeight; + } + } + } + + if ( $InnerPlotsA != "" ) + { $InnerPlots = array_merge($InnerPlotsA,$this->arrayReverse($InnerPlotsB)); $this->pChartObject->drawPolygon($InnerPlots,$Settings); } + } + + /* Draw the splice top and left poly */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; + $Settings["R"] = $Settings["R"]+$Cf*1.5; $Settings["G"] = $Settings["G"]+$Cf*1.5; $Settings["B"] = $Settings["B"]+$Cf*1.5; + + $StartAngle = $Plots["Angle"][0]; + foreach($Plots["Angle"] as $Key =>$Angle) { if ($Angle == VOID) { $EndAngle = $Plots["Angle"][$Key-1]; } } + + if ( $StartAngle < 180 ) + { + $Points = ""; + $Points[] = $Plots["InX2"]; + $Points[] = $Plots["InY2"]; + $Points[] = $Plots["InX2"]; + $Points[] = $Plots["InY2"]-$SliceHeight; + $Points[] = $Plots["OutX1"]; + $Points[] = $Plots["OutY1"]-$SliceHeight; + $Points[] = $Plots["OutX1"]; + $Points[] = $Plots["OutY1"]; + + $this->pChartObject->drawPolygon($Points,$Settings); + } + + if ( $EndAngle > 180 ) + { + $Points = ""; + $Points[] = $Plots["InX1"]; + $Points[] = $Plots["InY1"]; + $Points[] = $Plots["InX1"]; + $Points[] = $Plots["InY1"]-$SliceHeight; + $Points[] = $Plots["OutX2"]; + $Points[] = $Plots["OutY2"]-$SliceHeight; + $Points[] = $Plots["OutX2"]; + $Points[] = $Plots["OutY2"]; + + $this->pChartObject->drawPolygon($Points,$Settings); + } + } + + + /* Draw the vertical edges (visible) */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; + $Settings["R"] = $Settings["R"]+$Cf; $Settings["G"] = $Settings["G"]+$Cf; $Settings["B"] = $Settings["B"]+$Cf; + + $StartAngle = $Plots["Angle"][0]; + foreach($Plots["Angle"] as $Key =>$Angle) { if ($Angle == VOID) { $EndAngle = $Plots["Angle"][$Key-1]; } } + + if ( $StartAngle <= 270 && $StartAngle >= 90 ) + $this->pChartObject->drawLine($Plots["OutX1"],$Plots["OutY1"],$Plots["OutX1"],$Plots["OutY1"]-$SliceHeight,$Settings); + if ( $EndAngle <= 270 && $EndAngle >= 90 ) + $this->pChartObject->drawLine($Plots["OutX2"],$Plots["OutY2"],$Plots["OutX2"],$Plots["OutY2"]-$SliceHeight,$Settings); + } + + + /* Draw the outer vertical slices */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; + $Settings["R"] = $Settings["R"]+$Cf; $Settings["G"] = $Settings["G"]+$Cf; $Settings["B"] = $Settings["B"]+$Cf; + + $Outer = TRUE; $Inner = FALSE; + $OuterPlotsA = ""; $OuterPlotsB = ""; $InnerPlotsA = ""; $InnerPlotsB = ""; + foreach($Plots["Angle"] as $ID => $Angle) + { + if ( $Angle == VOID ) + { $Outer = FALSE; $Inner = TRUE; } + elseif( $Outer ) + { + if ( ( $Angle > 90 && $Angle < 270 ) && isset($Plots["BottomPoly"][$ID*2]) ) + { + $Xo = $Plots["BottomPoly"][$ID*2]; + $Yo = $Plots["BottomPoly"][$ID*2+1]; + + $OuterPlotsA[] = $Xo; $OuterPlotsA[] = $Yo; + $OuterPlotsB[] = $Xo; $OuterPlotsB[] = $Yo-$SliceHeight; + } + } + } + if ( $OuterPlotsA != "" ) + { $OuterPlots = array_merge($OuterPlotsA,$this->arrayReverse($OuterPlotsB)); $this->pChartObject->drawPolygon($OuterPlots,$Settings); } + } + + $Slices = array_reverse($Slices); + $SliceColors = array_reverse($SliceColors); + + + /* Draw the top pie splice */ + foreach($Slices as $SliceID => $Plots) + { + $Settings = $SliceColors[$SliceID]; $Settings["NoBorder"] = TRUE; + $Settings["R"] = $Settings["R"]+$Cf*2; $Settings["G"] = $Settings["G"]+$Cf*2; $Settings["B"] = $Settings["B"]+$Cf*2; + + $this->pChartObject->drawPolygon($Plots["TopPoly"],$Settings); + + if ( $RecordImageMap ) { $this->pChartObject->addToImageMap("POLY",$this->arraySerialize($Plots["TopPoly"]),$this->pChartObject->toHTMLColor($Settings["R"],$Settings["G"],$Settings["B"]),$Data["Series"][$Data["Abscissa"]]["Data"][$SliceID],$Data["Series"][$DataSerie]["Data"][count($Slices)-$SliceID-1]); } + + foreach($Plots["AA"] as $Key => $Pos) + $this->pChartObject->drawAntialiasPixel($Pos[0],$Pos[1]-$SliceHeight,$Settings); + + $this->pChartObject->drawLine($Plots["InX1"],$Plots["InY1"]-$SliceHeight,$Plots["OutX2"],$Plots["OutY2"]-$SliceHeight,$Settings); + $this->pChartObject->drawLine($Plots["InX2"],$Plots["InY2"]-$SliceHeight,$Plots["OutX1"],$Plots["OutY1"]-$SliceHeight,$Settings); + } + + if ( $DrawLabels ) + { + $Offset = 360; + foreach($Values as $Key => $Value) + { + $StartAngle = $Offset; + $EndAngle = $Offset-($Value*$ScaleFactor); if ( $EndAngle < 0 ) { $EndAngle = 0; } + + if ( $LabelColor == PIE_LABEL_COLOR_AUTO ) + { $Settings = array("FillR"=>$Palette[$ID]["R"],"FillG"=>$Palette[$ID]["G"],"FillB"=>$Palette[$ID]["B"],"Alpha"=>$Palette[$ID]["Alpha"]);} + else + { $Settings = array("FillR"=>$LabelR,"FillG"=>$LabelG,"FillB"=>$LabelB,"Alpha"=>$LabelAlpha); } + + $Angle = ($EndAngle - $Offset)/2 + $Offset; + $Xc = cos(($Angle-90)*PI/180) * ($OuterRadius+$DataGapRadius) + $X; + $Yc = sin(($Angle-90)*PI/180) * ($OuterRadius+$DataGapRadius)*$SkewFactor + $Y; + + if ( $WriteValues == PIE_VALUE_PERCENTAGE ) + $Label = $Display = round(( 100 / $SerieSum ) * $Value,$Precision)."%"; + elseif ( $WriteValues == PIE_VALUE_NATURAL ) + $Label = $Data["Series"][$Data["Abscissa"]]["Data"][$Key]; + else + $Label = ""; + + if ( $LabelStacked ) + $this->writePieLabel($Xc,$Yc-$SliceHeight,$Label,$Angle,$Settings,TRUE,$X,$Y,$OuterRadius); + else + $this->writePieLabel($Xc,$Yc-$SliceHeight,$Label,$Angle,$Settings,FALSE); + + $Offset = $EndAngle - $DataGapAngle; $ID--; $Slice++; + } + } + if ( $DrawLabels && $LabelStacked ) { $this->writeShiftedLabels(); } + + $this->pChartObject->Shadow = $RestoreShadow; + + return(PIE_RENDERED); + } + + /* Serialize an array */ + function arraySerialize($Data) + { + $Result = ""; + foreach($Data as $Key => $Value) + { if ($Result == "") { $Result = floor($Value); } else { $Result = $Result.",".floor($Value); } } + + return($Result); + } + + /* Reverse an array */ + function arrayReverse($Plots) + { + $Result = ""; + + for($i=count($Plots)-1;$i>=0;$i=$i-2) + { $Result[] = $Plots[$i-1]; $Result[] = $Plots[$i]; } + + return($Result); + } + + /* Remove unused series & values */ + function clean0Values($Data,$Palette,$DataSerie,$AbscissaSerie) + { + $NewPalette = ""; $NewData = ""; $NewAbscissa = ""; + + /* Remove unused series */ + foreach($Data["Series"] as $SerieName => $SerieSettings) + { if ( $SerieName != $DataSerie && $SerieName != $AbscissaSerie ) { unset($Data["Series"][$SerieName]); } } + + /* Remove NULL values */ + foreach($Data["Series"][$DataSerie]["Data"] as $Key => $Value) + { + if ($Value != 0 ) + { + $NewData[] = $Value; + $NewAbscissa[] = $Data["Series"][$AbscissaSerie]["Data"][$Key]; + if ( isset($Palette[$Key]) ) { $NewPalette[] = $Palette[$Key]; } + } + } + $Data["Series"][$DataSerie]["Data"] = $NewData; + $Data["Series"][$AbscissaSerie]["Data"] = $NewAbscissa; + + return(array($Data,$NewPalette)); + } + } ?> \ No newline at end of file