PageRenderTime 78ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/www/advgraph/advgraph5.class.php

https://github.com/jonathankhoo/pulsar-distance
PHP | 2199 lines | 1705 code | 445 blank | 49 comment | 385 complexity | e0da3c3fceb073e3adee92a8bb7f6d0f MD5 | raw file

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

  1. <?php
  2. /*
  3. +-------------------------------------------------------------------------+
  4. | Copyright (C) 2006-2007 Zack Bloom |
  5. | |
  6. | This program is free software; you can redistribute it and/or |
  7. | modify it under the terms of the GNU Lesser General Public |
  8. | License as published by the Free Software Foundation; either |
  9. | version 2.1 of the License, or (at your option) any later version. |
  10. | |
  11. | This program is distributed in the hope that it will be useful, |
  12. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  13. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  14. | GNU Lesser General Public License for more details. |
  15. | |
  16. | You should have received a copy of the GNU Lesser General Public |
  17. | License along with this library; if not, write to the Free Software |
  18. | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
  19. | 02110-1301, USA |
  20. | |
  21. +-------------------------------------------------------------------------+
  22. | Version 1.8.0 - April 7th, 2007 |
  23. +-------------------------------------------------------------------------+
  24. | Special Thanks to: |
  25. | Miles Kaufmann - EvalMath Class Library |
  26. | Walter Zorn - Javascript Graph Library |
  27. | Andreas Gorh - PHP4 Backport |
  28. | All Those Who Love PHP :) |
  29. +-------------------------------------------------------------------------+
  30. | Code updates and additional features are released frequently, the most |
  31. | updated version can always be found at: http://www.zackbloom.org. |
  32. | |
  33. | Email me at: zackbloom@gmail.com with any comments, questions and bugs |
  34. | |
  35. | Works with PHP 4 & 5 with GD and TTF support. |
  36. +-------------------------------------------------------------------------+
  37. | - Advanced Graph Class Library - http://www.zackbloom.org/ |
  38. +-------------------------------------------------------------------------+
  39. */
  40. class graph {
  41. public static $numGraphs;
  42. public $width, $height, $numPoints, $xPts, $yPts, $iVars, $dVars, $ids, $props, $time, $xMax, $yMax, $xMin, $yMin;
  43. private $img, $imgCache, $fileCache, $imgCacheFile, $lines, $errors, $div, $evalmath, $mode;
  44. private $font_name, $keyfont_name;
  45. public function __construct($x_width=400, $x_height=200, $xScale=10, $yScale=6, $mode='image', $div='jg') {
  46. $this->mode = $mode;
  47. /* set defaults */
  48. $this->props = array();
  49. $this->xPts = array();
  50. $this->yPts = array();
  51. $this->iVars[0] = array();
  52. $this->dVars[0] = array();
  53. $this->errors = array();
  54. $this->width = $x_width;
  55. $this->height = $x_height;
  56. $this->font_name = "arial.ttf" ;
  57. $this->keyfont_name = "arial.ttf";
  58. $this->setProp( 'font', dirname(__FILE__) . '/fonts/' . $this-> font_name);
  59. $this->setProp( 'keyfont', dirname(__FILE__) . '/fonts/' . $this-> keyfont_name);
  60. if ($this->mode == 'div')
  61. $this->div = new jgwrap($div, str_replace (".ttf", "", str_replace("fonts/", "" , $this->font_name )), self::$numGraphs);
  62. $this->numPoints[0] = 0;
  63. $this->xMax = 1;
  64. $this->yMax = 1;
  65. $this->xMin = 0;
  66. $this->yMin = 0;
  67. $this->lines = 1;
  68. $this->evalmath = new EvalMath;
  69. $this->setBulkProps("xsclpts:$xScale, ysclpts:$yScale, xsclpts:10, ysclpts:6, xsclmax:1, xsclmin:0,
  70. ysclmax:1, ysclmin:0, xSclInc:1, ySclInc:.165, sclline:5, onfreq:.2, actwidth:".($x_width+30).",
  71. actheight:".($x_height+30).", xincpts:10, yincpts:6, autoscl:true");
  72. $this->setProp("color", array(0,0,255,0));
  73. $this->setProp("backcolor", array(255,255,255));
  74. $this->img = imagecreatetruecolor($this->width,$this->height);
  75. }
  76. /* remove cached files */
  77. public function __destruct() {
  78. if (isset($this->fileCache) && !$this->getProp("keepcache",true)) {
  79. unlink($this->fileCache);
  80. }
  81. if (isset($this->imgCache) && !$this->getProp("keepcache",true)) {
  82. unlink($this->imgCache);
  83. }
  84. }
  85. public function setProp($name, $value, $l=-1) {
  86. if ($l+1 > $this->lines) $this->lines++;
  87. $this->props[$l][strtolower($this->strim($name))] = $this->strim($value);
  88. }
  89. public function __set($name,$value) {
  90. $this->setProp($name,$value);
  91. }
  92. public function storeGraph($file="graphCache.data", $image=false) {
  93. $this->fileCache = $file;
  94. if ($image) {
  95. $this->imgCacheFile = $image;
  96. ob_start();
  97. imagepng($this->img);
  98. unset($this->img);
  99. $img = ob_get_flush();
  100. file_put_contents($image,$img);
  101. }
  102. file_put_contents($file,serialize($this));
  103. if ($image)
  104. return array($file, $image);
  105. else
  106. return $file;
  107. }
  108. public function retriveGraph($file = "graphCache.data") {
  109. return unserialize(file_get_contents($file));
  110. }
  111. public function retriveImage($image) {
  112. $this->img = imagecreatefrompng($image);
  113. }
  114. public function setBulkProps($str) {
  115. if ($str=="") return false;
  116. if (func_num_args() == 1) {
  117. $str = str_replace(
  118. array(":red", ":green", ":blue", ":orange", ":yellow", ":pink", ":purple", ":teal", ":black", ":white", ":lightgray", ":darkgray"),
  119. array(":230-60-60", ":100-200-100", ":60-60-230", ":255-160-15", ":255-255-0", ":255-0-170", ":216-0-255", ":0-255-255", ":0-0-0", ":255-255-255", ":150-150-150", ":80-80-80"),
  120. $str);
  121. foreach (explode(",",$str) as $e) {
  122. $d = explode(":",$e);
  123. if (strpos($d[0],"|")){
  124. $g = explode("|",$d[0]);
  125. $d[0] = $g[1];
  126. $d[2] = $g[0];
  127. } else {
  128. $d[2] = -1;
  129. }
  130. if (strpos($d[0],"color")!==false) {
  131. $d[1] = explode("-",$d[1]);
  132. }
  133. $this->setProp($d[0],$d[1],$d[2]);
  134. }
  135. } else {
  136. $arg = func_get_args();
  137. foreach ($arg as $e) {
  138. if (is_string($e)) {
  139. $e = explode(":",$e);
  140. if (strpos("|",$e[0])) {
  141. $g = explode("|",$e[0]);
  142. $e[0] = $e[1];
  143. $e[2] = $e[0];
  144. } else {
  145. $e[2] = -1;
  146. }
  147. } else {
  148. if (isset($e[2])==false) {
  149. $e[2] = -1;
  150. }
  151. }
  152. $this->setProp($e[0],$e[1],$e[2]);
  153. }
  154. }
  155. return true;
  156. }
  157. private function strim($a) {
  158. if (is_string($a)) {
  159. return trim($a);
  160. } else {
  161. return $a;
  162. }
  163. }
  164. public function storePropArr($arr) {
  165. $this->props = $arr;
  166. }
  167. public function getPropArr() {
  168. return $this->props;
  169. }
  170. /* getProp - retrieves the values from the properties array. Otherwise
  171. returns the default value.
  172. @arg $name - the property name to be searched
  173. @arg $asu - the default value if not found
  174. @arg $l - the property name array offset */
  175. public function getProp($name, $asu = false, $l=0) {
  176. $name = strtolower($this->strim($name));
  177. if (isset($this->props[$l][$name])) {
  178. return $this->props[$l][$name];
  179. } else {
  180. if (isset($this->props[-1][$name])) {
  181. return $this->props[-1][$name];
  182. } else {
  183. if ($asu===-9) {
  184. $this->error('The required property '.$name.' was not set');
  185. }elseif ($asu===-8) {
  186. $this->error('The property '.$name.' was not found');
  187. } else {
  188. return $this->strim($asu);
  189. }
  190. }
  191. }
  192. return false;
  193. }
  194. public function __get($name) {
  195. return $this->getProp($name,-8);
  196. }
  197. public function __call($name, $args) {
  198. if (count($args)!=2) {
  199. $this->error("Method $name not found.",true);
  200. }
  201. return $this->getProp($name, $args[0], $args[1]);
  202. }
  203. public function setColor($obj, $l, $r, $g=-1, $b=-1, $a=0) {
  204. if (substr($obj,-5)!="color") {
  205. $obj.="color";
  206. }
  207. if (is_array($r)){
  208. $g = $r[1];
  209. $b = $r[2];
  210. $r = $r[0];
  211. }
  212. if (is_string($r) && $g==-1 && $b==-1) {
  213. $colors = array("red","green","blue","orange","yellow","pink","purple","teal","black","white","lightgray","darkgray");
  214. $cid = array(array(230,60,60),array(100,200,100),array(60,60,230),array(255,160,15),array(255,255,0),array(255,0,170),array(216,0,255),array(0,255,255),array(0,0,0),array(255,255,255),array(150,150,150),array(80,80,80));
  215. $x=array_search(strtolower($r),$colors);
  216. if ($x!==false) {
  217. $this->setColor($obj,$l,$cid[$x][0],$cid[$x][1],$cid[$x][2],0);
  218. } else {
  219. $this->error("Color not found");
  220. }
  221. } else {
  222. $this->setProp($obj,array($r,$g,$b,$a),$l);
  223. }
  224. }
  225. private function xScale($x) {
  226. if (($this->xMax-$this->xMin)>0) {
  227. return $this->width * (($x-$this->xMin)/($this->xMax-$this->xMin));
  228. } else {
  229. return 0;
  230. }
  231. }
  232. private function yScale($y) {
  233. if ((($this->yMax-$this->yMin))>0) {
  234. return $this->height * (($y-$this->yMin)/($this->yMax-$this->yMin));
  235. } else {
  236. return 0;
  237. }
  238. }
  239. public function addPoint($d,$i=-5,$l=0) {
  240. if ($l+1>$this->lines) {
  241. $this->lines++;
  242. $this->numPoints[$l] = 0;
  243. $this->iVars[$l] = array();
  244. $this->dVars[$l] = array();
  245. }
  246. if (is_array($d)) {
  247. if (count($d)==3)
  248. return $this->addPoint($d[0],$d[1],$d[2]);
  249. elseif (count($d)==2 && isset($d[1]))
  250. return $this->addPoint($d[0],$d[1]);
  251. elseif (count($d)==2)
  252. return $this->addPoint($d[0],-5,$d[2]);
  253. else
  254. return $this->addPoint($d[0]);
  255. }
  256. if ($i==-5) {
  257. $i=(count($this->iVars[$l])>0?max($this->iVars[$l])+1:0);
  258. }
  259. $this->iVars[$l][] = $i;
  260. $this->dVars[$l][] = $d;
  261. $id = rand(1,10000000);
  262. $this->ids[$l][] = $id;
  263. $this->numPoints[$l]++;
  264. return $id;
  265. }
  266. public function addBulkPoints($a,$sep=":") {
  267. if (is_string($a)) {
  268. $cDataSet = 0;
  269. foreach (explode(",",$a) as $e) {
  270. $e = explode($sep,$e);
  271. if (strpos($e[0],"|")) {
  272. $d = explode('|',$e[0]);
  273. $e[0] = $d[1];
  274. $cDataSet = $d[0];
  275. }
  276. $e[2] = $cDataSet;
  277. if (isset($e[1])) {
  278. if (strpos($e[1],'-')) {
  279. $e[1] = strtotime($e[1]);
  280. }
  281. }
  282. $ids[] = $this->addPoint($e);
  283. }
  284. }elseif (func_num_args()>1) {
  285. $arg = func_get_args();
  286. foreach ($arg as $e)
  287. $ids[] = $this->addPoint($e);
  288. }elseif (is_array($a)) {
  289. foreach ($a as $e)
  290. $ids[] = $this->addPoint($e);
  291. } else {
  292. return false;
  293. }
  294. return $ids;
  295. }
  296. public function delBulkPoints($a) {
  297. if (is_string($a)) {
  298. foreach (explode(",",$a) as $e) {
  299. $ids[] = $this->delPoint($e);
  300. }
  301. }elseif (is_array($a)){
  302. foreach ($a as $e)
  303. $ids[] = $this->delPoint($e);
  304. } else {
  305. $arg = func_get_args();
  306. foreach ($arg as $e) {
  307. $ids[] = $this->delPoint($e);
  308. }
  309. }
  310. return $ids;
  311. }
  312. private function idSearch($b) {
  313. foreach ($this->ids as $k => $v) {
  314. $e = array_search($b,$v);
  315. if ($e!==false) {
  316. return array($e,$k);
  317. }
  318. }
  319. return false;
  320. }
  321. public function delPoint($id,$k=-1) {
  322. if ($k==-1) {
  323. $k = $this->idSearch($id);
  324. }
  325. if ($k === false) {
  326. return $k;
  327. }
  328. unset($this->dVars[$k[1]][$k[0]],$this->iVars[$k[1]][$k[0]],$this->ids[$k[1]][$k[0]]);
  329. $this->numPoints[$k[1]]--;
  330. return true;
  331. }
  332. public function clearPoints() {
  333. $this->dVars = array();
  334. $this->iVars = array();
  335. $this->ids = array();
  336. foreach ($this->numPoints as $k => $e) {
  337. $this->numPoints[$k]=0;
  338. }
  339. }
  340. public function demoData($i=10,$mi=0,$ma=10) {
  341. for($j=0;$j<$i;$j++) {
  342. $this->addPoint(rand($mi,$ma));
  343. }
  344. }
  345. private function error($x="Error",$ext=false) {
  346. $log = $this->getProp("logfile",false);
  347. if ($x!==true) {
  348. $this->errors[] = $x;
  349. } else {
  350. if (!$this->getProp("production",false)) {
  351. foreach ($this->errors as $k => $x) {
  352. imagestring($this->img,2,10,10+$k*12,$x,imagecolorallocate($this->img,255,0,0));
  353. }
  354. }
  355. if ($log) {
  356. $l = fopen($log,'a');
  357. if ($this->errors) {
  358. fwrite($l,"\nSESSION ".date("D M j G:i:s T Y").', '.$_SERVER['REMOTE_ADDR']."\n");
  359. }
  360. foreach ($this->errors as $k => $x) {
  361. fwrite($l,$x."\n");
  362. }
  363. fclose($l);
  364. }
  365. }
  366. if ($log && $ext) {
  367. $l = fopen($log,'a');
  368. fwrite($l,'FATAL ERROR: '.$x."\n".date("D M j G:i:s T Y").', '.$_SERVER['REMOTE_ADDR']."\n\n");
  369. }
  370. if (!$this->getProp("production",false)) {
  371. if ($ext==true) {
  372. trigger_error($x,E_USER_ERROR);
  373. }
  374. } else {
  375. trigger_error("Their was an error, please contact the webmaster",E_USER_ERROR);
  376. }
  377. }
  378. public function importMySQL($table,$field,$rcodb=null,$user=null,$pass=null,$server="localhost",$freq=true){
  379. if (!is_resource($rcodb)) {
  380. $m = mysql_connect($server,$user,$pass);
  381. if (!$m) {
  382. $this->error("Could not connect to MySQL server.",true);
  383. }
  384. $d = mysql_select_db($rcodb,$m);
  385. if (!$d) {
  386. $this->error("Could not select MySQL database.",true);
  387. }
  388. $rcodb = $m;
  389. }
  390. $q = mysql_query("SELECT `$field` FROM $table");
  391. if (!$q) {
  392. $this->error("Error querying MySQL database, check table and field.",true);
  393. }
  394. while ($r = mysql_fetch_array($q, MYSQL_NUM)) {
  395. $a[] = $r[0];
  396. }
  397. if ($freq==true) {
  398. $s = array_count_values($a);
  399. foreach ($s as $e) {
  400. $this->addPoint($e);
  401. }
  402. $this->setProp("key",array_keys($s));
  403. $this->setProp("showkey",true);
  404. } else {
  405. $this->addBulkPoints($a);
  406. }
  407. }
  408. public function importCSV($file, $format='l,d,i', $dl=0) { //Read the readme
  409. @$handle = fopen($file,"r");
  410. if (!$handle) {
  411. $this->error("Error opening CSV!", E_USER_ERROR);
  412. return false;
  413. }
  414. foreach (explode(',',$format) as $k => $s) {
  415. $pos[$s] = $k;
  416. }
  417. while (($data[] = fgetcsv($handle, 1000)) !== FALSE) {
  418. /* load data array from file contents */
  419. }
  420. foreach ($data as $k => $d) {
  421. if (isset($pos['i'])) {
  422. $i = $d[$pos['i']];
  423. } else {
  424. $i = -5;
  425. }
  426. if (isset($pos['l'])) {
  427. $l = $d[$pos['l']];
  428. } else {
  429. $l = $dl;
  430. }
  431. if ($d[$pos['d']] && $i) {
  432. $idArr[] = $this->addPoint($d[$pos['d']],$i,$l);
  433. }
  434. }
  435. return $idArr;
  436. }
  437. public function importXML($file,$ind="i",$dep="d",$block="",$dl=0) { //Read the readme
  438. if ($block!="") {
  439. $eblock = "</".$block.">";
  440. $block = "<".$block."( $dl=([0-9])*)>";
  441. }
  442. $str = file_get_contents($file);
  443. $str = str_replace(array("\n","\t"),'',$str);
  444. if ($str==false) {
  445. $this->error("Error opening XML File!",E_USER_ERROR);
  446. return false;
  447. }
  448. $str = trim($str);
  449. $arr = array();
  450. if (preg_match_all("[$block<([$ind|$dep])>([0-9])*</[($ind|$dep)]><([$ind|$dep])>([0-9])*</[($ind|$dep)]>$eblock]",$str,$arr)==0) {
  451. $this->error("No data found in XML file, check format",true);
  452. }
  453. $i=($arr[3][0]==$ind?4:6);
  454. $d=($arr[3][0]==$ind?6:4);
  455. foreach ($arr[$d] as $k => $r) {
  456. $idArr[] = $this->addPoint($arr[$d][$k],$arr[$i][$k],(is_string($dl)?$arr[2][$k]:$dl));
  457. }
  458. return $idArr;
  459. }
  460. public function graphFunction($func, $minX, $maxX, $l=0) {
  461. $inv = $this->getProp('funcinterval',.0625);
  462. $inv += .005;
  463. $this->evalmath->evaluate('graph(x)='.str_replace('$x','x',$func));
  464. for($x=$minX;$x<=$maxX;$x+=$inv) {
  465. $pts[] = $this->addPoint($this->evalmath->evaluate("graph($x)"),$x,$l);
  466. }
  467. $this->setProp('sort',false);
  468. return $pts;
  469. }
  470. public function evaluate($f) {
  471. return $this->evalmath->evaluate($f);
  472. }
  473. public function graph() { // Allows internalGraph to return false if it needs to restart
  474. do
  475. $x = $this->internalGraph();
  476. while ($x==false);
  477. }
  478. private function cacheImg($img=0) {
  479. if (isset($this->imgCache)) {
  480. return $this->imgCache;
  481. } else {
  482. $this->imgCache = $img;
  483. }
  484. return false;
  485. }
  486. /* determine the maximum value in a single dimensional array */
  487. private function multiMax($a) {
  488. $max = -1000000000;
  489. foreach ($a as $e) {
  490. if (max($e)>$max) {
  491. $max = max($e);
  492. }
  493. }
  494. return $max;
  495. }
  496. /* determine the mimimum value in a single dimensional array */
  497. private function multiMin($a) {
  498. $min = 100000000;
  499. foreach ($a as $e) {
  500. if (min($e)<$min) {
  501. $min = min($e);
  502. }
  503. }
  504. return $min;
  505. }
  506. /* interesting array index value checker */
  507. function actSort($a,$b) {
  508. return ($a[0] == $b[0] ? 0 : ($a[0] < $b[0] ? -1 : 1) );
  509. }
  510. private function indSort($e) {
  511. for($i=0; $i<$this->numPoints[$e]; $i++) {
  512. $tiVars[] = array($this->iVars[$e][$i], $this->dVars[$e][$i], $this->ids[$e][$i]);
  513. }
  514. usort($tiVars, array("graph", "actSort"));
  515. foreach ($tiVars as $k => $g) {
  516. $this->iVars[$e][$k] = $g[0];
  517. $this->dVars[$e][$k] = $g[1];
  518. $this->ids[$e][$k] = $g[2];
  519. }
  520. }
  521. private function drawBar($x1, $y1, $x2, $y2, $ccol) {
  522. $mode = $this->getProp("barstyle",0);
  523. $colorlist = $this->getProp("colorlist",false);
  524. if ($colorlist === true || $colorlist === 1 || $colorlist === "true") {
  525. $colorlist = array(array(255, 203, 3),array(220, 101, 29),array(189, 24, 51),array(214, 0, 127),array(98, 1, 96),array(0, 62, 136),array(0, 102, 179),array(0, 145, 195),array(0, 115, 106),array(178, 210, 52),array(137, 91, 74),array(82, 56, 47));
  526. }
  527. if ($colorlist) {
  528. $color = $colorlist[$ccol];
  529. }
  530. if (!isset($color[3])) {
  531. $color[3] = 0;
  532. } else {
  533. $color = $this->getProp("color", array(0,0,255,0));
  534. }
  535. $bord = $this->getProp("bordercolor",array(0,0,0,0));
  536. switch ($mode) {
  537. case 0:
  538. if ($this->mode!="div") {
  539. imagefilledrectangle($this->img,$x1,$y1,$x2,$y2,imagecolorallocatealpha($this->img,$color[0],$color[1],$color[2],$color[3]));
  540. imagerectangle($this->img,$x1,$y1,$x2,$y2,imagecolorallocatealpha($this->img,$bord[0],$bord[1],$bord[2],$bord[3]));
  541. } else {
  542. $this->div->setColor($color[0],$color[1],$color[2]);
  543. $this->div->drawFilledRect($x1,$y1,$x2-$x1,$y2-$y1);
  544. $this->div->setColor($bord[0],$bord[1],$bord[2]);
  545. $this->div->drawRect($x1,$y1,$x2-$x1,$y2-$y1);
  546. }
  547. break;
  548. case 1:
  549. $ge = $this->getProp("gendcolor",array(0,0,0,0));
  550. $gs = $this->getProp("gstartcolor",array(255,255,255,0));
  551. for($i=0;$i<4;$i++) {
  552. $c[$i] = ($ge[$i]-$gs[$i])/($y2-$y1);
  553. $cc[$i] = $gs[$i];
  554. }
  555. for($y=$y1;$y<$y2;$y++) {
  556. for($i=0;$i<4;$i++) {
  557. $cc[$i] += $c[$i];
  558. }
  559. if ($this->mode!="div") {
  560. imageline($this->img,$x1,$y,$x2,$y,imagecolorallocatealpha($this->img,$cc[0],$cc[1],$cc[2],$cc[3]));
  561. } else {
  562. $this->div->setColor($cc[0],$cc[1],$cc[2]);
  563. $this->div->drawLine($x1+1,$y,$x2,$y);
  564. }
  565. }
  566. break;
  567. default:
  568. $this->error("Bar style not understood.");
  569. break;
  570. }
  571. }
  572. private function flipMulti(&$arr) {
  573. foreach ($arr as $i => $e) {
  574. foreach ($e as $j => $k) {
  575. $n[$j][$i] = $k;
  576. }
  577. }
  578. $arr = $n;
  579. }
  580. function pieSort($a,$b) {
  581. return ($a==$b ? 0 : (abs($a-90) > abs($b-90) ? -1 : 1));
  582. }
  583. private function drawPie($ex) {
  584. $cx = $this->width/2;
  585. $cy = $this->height/2+20;
  586. $ang = $this->getProp("pieangle",35);
  587. if ($ang>90) {
  588. $this->error("Angles over 90 cannot be properly graphed.");
  589. }
  590. $h = min($this->width,$this->height-10);
  591. $h = ($h*((90-$ang)/90))+3;
  592. $w = min(min($this->width,$this->height-10)+$ang*1.5,$this->width);
  593. $colorlist = $this->getProp('colorlist',array(array(125, 203, 3),array(220, 101, 29),array(189, 24, 51),array(34, 78, 120),array(120, 1, 60),array(0, 62, 136),array(0, 102, 179),array(0, 145, 195),array(0, 115, 106),array(178, 210, 52),array(137, 91, 74),array(82, 56, 47)));
  594. $da['data'] = $this->dVars[$ex];
  595. $da['key'] = $this->iVars[$ex];
  596. foreach ($da['data'] as $i => $e){ //Allocate colors
  597. $rcolor[$i] = imagecolorallocate($this->img,$colorlist[$i][0],$colorlist[$i][1],$colorlist[$i][2]);
  598. $dcolor[$i] = imagecolorallocate($this->img,abs($colorlist[$i][0]-30),abs($colorlist[$i][1]-30),abs($colorlist[$i][2]-30));
  599. }
  600. $datasum = array_sum($da['data']);
  601. for($i=0;$i<count($da['data']);$i++){
  602. $da['part'][$i] = $da['data'][$i] / $datasum; //Get percents
  603. }
  604. if (($fd=array_sum($da['part']))<1) {
  605. $da['part'][count($da['part'])-1] += 1-$fd;
  606. }
  607. for($i=0;$i<count($da['data']);$i++) {
  608. $da['angle'][$i] = $da['part'][$i] * 360; //Get angles
  609. }
  610. for($i=0;$i<count($da['data']);$i++) {
  611. @$da['ansum'][$i] = array_sum(array_slice($da['angle'],0,$i+1)); //Get sums from 0 to each angle
  612. }
  613. for($i=1;$i<count($da['ansum'])+1;$i++) {
  614. $sortkeys[] = $da['ansum'][$i-1]; //Create sort array to make sure pie is graphed back to front
  615. }
  616. for($i=1;$i<count($sortkeys)+1;$i++) {
  617. if ($sortkeys[$i-1]<90 && $sortkeys[$i]>90) { // Make sure the one that actually crosses 90 is last
  618. $sortkeys[$i] = 90;
  619. }
  620. }
  621. uasort($sortkeys,array('graph','piesort'));
  622. $sk = array_keys($sortkeys);
  623. for($p=0;$p<=count($da['data'])-1;$p++) {
  624. $n = $sk[$p];
  625. $f = $n - 1;
  626. if ($da['angle'][$n] != 0) {
  627. for ($i = 0; $i < $ang; $i++) {
  628. if (($da['ansum'][$n]<=180 || $da['ansum'][$f]<=180) || ($n == count($da['data'])-1)) { //Draw 3d
  629. @imagefilledarc($this->img, $cx, $cy+$i, $w, $h, $da['ansum'][$f], $da['ansum'][$n], ($n==count($da['data'])-1?$rcolor[$n]:$dcolor[$n]), IMG_ARC_PIE);
  630. }
  631. }
  632. }
  633. }
  634. for($i=0;$i<=count($da['data'])-1;$i++) {
  635. $n = $i - 1;
  636. if ($da['angle'][$i] != 0) { //Draw top
  637. @imagefilledarc($this->img, $cx, $cy, $w, $h, $da['ansum'][$n], $da['ansum'][$i], $rcolor[$i], IMG_ARC_PIE);
  638. }
  639. }
  640. for($i=0;$i<count($da['data']);$i++) {
  641. @$da['ansum'][$i] = array_sum(array_slice($da['angle'],0,$i+1));
  642. }
  643. for($i=0;$i<count($da['data']);$i++) { //Draw keys
  644. $text = ($this->getProp("useval",false)? $da['data'][$i] : round($da['part'][$i]*100,0).'%');
  645. $size = imagettfbbox($this->getProp("textsize",8),$this->getProp("textAngle",0),$this->getProp("font",realpath($this->font_name)),$text);
  646. if (!isset($da['ansum'][$i-1])) {
  647. $valuea = 0;
  648. } else {
  649. $valuea = $da['ansum'][$i-1];
  650. }
  651. if (!isset($da['ansum'][$i])) {
  652. $valueb = 0;
  653. } else {
  654. $valueb = $da['ansum'][$i];
  655. }
  656. $avang = ($valuea+$valueb)/2;
  657. imagettftext($this->img,
  658. $this->getProp("textsize",8),
  659. $this->getProp("textAngle",0),
  660. cos(deg2rad($avang))*$w/2+$cx+($avang<90||$avang>270?$this->getProp("numspace",5):-$size[2]-$this->getProp("numspace",5)),
  661. sin(deg2rad($avang))*$h/2+$cy+($avang<180&&$avang>0?-$size[5]+$ang:$size[5]),imagecolorallocate($this->img,0,0,0),
  662. $this->getProp("font",realpath($this->font_name)),
  663. $text);
  664. }
  665. }
  666. private function internalGraph() {
  667. if ($this->getProp("scale","numeric")=="date") {
  668. $start = $this->getProp("startdate",-9);
  669. $end = $this->getProp("enddate",time());
  670. if (is_string($start)) {
  671. $start = strtotime($start);
  672. }
  673. if (is_string($end)) {
  674. $end = strtotime($end);
  675. }
  676. }
  677. foreach ($this->iVars as $e => $g) {
  678. foreach ($g as $k => $i) {
  679. if (substr($this->iVars[$e][$k],0,2)=='d:') {
  680. $this->iVars[$e][$k] = strtotime(substr($this->iVars[$e][$k],2))/($end-$start);
  681. }
  682. }
  683. }
  684. for($i=0;$i<count($this->numPoints);$i++) {
  685. if ($this->getProp("reverse",0,$i)==1) {
  686. $this->dVars = array_reverse($this->dVars[$i]);
  687. }
  688. if ($this->getProp("flip",0,$i)==1) {
  689. $this->dVars = array_reverse($this->dVars[$i]);
  690. $this->iVars = array_reverse($this->iVars[$i]);
  691. }
  692. if ($this->getProp("sort",true,$i)==true) {
  693. $this->indSort($i);
  694. }
  695. }
  696. $time = microtime(true);
  697. $nu = ($this->getProp('type','line') != 'line'?1:2);
  698. for($k=0;$k<$this->lines;$k++) {
  699. if (isset($this->numPoints[$k])==false || $this->numPoints[$k]<$nu) {
  700. $this->error("Not enough points in dataset ".$k);
  701. }
  702. }
  703. if ($this->getProp("autoSize",true)) {
  704. if ($this->getProp("autoSizeX",true)) {
  705. $this->xMax = $this->multiMax($this->iVars);
  706. $this->xMin = $this->multiMin($this->iVars);
  707. }
  708. if ($this->getProp("autoSizeY",true)) {
  709. $this->yMax = $this->multiMax($this->dVars);
  710. $this->yMin = $this->multiMin($this->dVars);
  711. }
  712. }
  713. if ($this->getProp("type","line")=="bar" && $this->getProp("barnone",true)) {
  714. $this->yMin = 0;
  715. $this->setProp("xincpts",$this->numPoints[0]);
  716. }
  717. if ($this->getProp("autoScl",true)) {
  718. if ($this->getProp("autoSclX",true)) {
  719. round($this->setProp("xsclmax",$this->xMax),1);
  720. round($this->setProp("xsclmin",$this->xMin),1);
  721. }
  722. if ($this->getProp("autoSclY",true)) {
  723. round($this->setProp("ysclmax",$this->yMax),1);
  724. round($this->setProp("ysclmin",$this->yMin),1);
  725. }
  726. }
  727. @($this->setProp("xSclInc",(($this->getProp("xsclmax")-$this->getProp("xsclmin"))/(float)$this->getProp("xsclpts"))*$this->width/(float)($this->getProp("xsclmax")-$this->getProp("xsclmin"))));
  728. @($this->setProp("ySclInc",(($this->getProp("ysclmax")-$this->getProp("ysclmin"))/(float)$this->getProp("ysclpts"))*$this->height/(float)($this->getProp("ysclmax")-$this->getProp("ysclmin"))));
  729. $this->xPts = array();
  730. $this->yPts = array();
  731. foreach ($this->iVars as $e => $g) {
  732. foreach ($g as $k => $i) {
  733. $f = $this->xScale($i);
  734. $this->xPts[$e][] = $f;
  735. }
  736. }
  737. foreach ($this->dVars as $e => $g) {
  738. foreach ($g as $k => $i) {
  739. $f = $this->yScale($i);
  740. $this->yPts[$e][] = $f;
  741. }
  742. }
  743. $backcolor = $this->getProp("backcolor");
  744. $grids = $this->getProp("gridcolor",array(80,80,80));
  745. if ($this->getProp("showvertscale",true)) {
  746. for($i=0;$i<$this->getProp("yincpts")+1;$i++) { //Generate scale information for use in the graph image creation
  747. $vertScaleText[$i] = trim(round((($i/$this->getProp("yincpts"))*($this->yMax-$this->yMin))+$this->yMin,1));
  748. $vertScaleSize[$i] = imagettfbbox($this->getProp("textsize",8),
  749. $this->getProp("textAngle",0),
  750. $this->getProp("font",realpath($this->font_name)),
  751. $vertScaleText[$i]);
  752. $cSize[$i] = $vertScaleSize[$i][4];
  753. }
  754. $max = max($cSize);
  755. $this->setProp('actwidth',$this->width+10+$max);
  756. }
  757. $this->img = imagecreatetruecolor($this->getProp("actwidth"),$this->getProp("actheight")); // Create image.
  758. $back = imagecolorallocate($this->img,$backcolor[0],$backcolor[1],$backcolor[2]);
  759. $grid = imagecolorallocate($this->img,$grids[0],$grids[1],$grids[2]);
  760. imagefill($this->img,0,0,$back); //Fill with back color
  761. if ($this->getProp("showgrid",true)) {
  762. for($i=0;$i<round($this->getProp("sclline")*$this->getProp("onfreq"),0);$i++) { //Create grid line style
  763. $style[] = $grid;
  764. }
  765. for($i=0;$i<round($this->getProp("sclline")*(1-$this->getProp("onfreq")),0);$i++) {
  766. $style[] = IMG_COLOR_TRANSPARENT;
  767. }
  768. imagesetstyle($this->img, $style);
  769. if ($this->getProp("showxgrid",true)) {
  770. for($i=1;$i<$this->getProp("xsclpts");$i++) { //Create grid
  771. if ($this->mode=='image') {
  772. imageline($this->img, round($i*$this->getProp("xSclInc"),0), 0, round($i*$this->getProp("xSclInc"),0), $this->height, IMG_COLOR_STYLED);
  773. } else {
  774. $this->div->setColor($grids[0],$grids[1],$grids[2]);
  775. $this->div->setStyle('dotted');
  776. $this->div->drawLine(round($i*$this->getProp("xSclInc"),0),0,round($i*$this->getProp("xSclInc"),0),$this->height);
  777. }
  778. }
  779. }
  780. if ($this->getProp("showygrid",true)) {
  781. for($i=1;$i<$this->getProp("ysclpts");$i++) {
  782. if ($this->mode=='image') {
  783. imageline($this->img, 0, round($i*$this->getProp("ySclInc"),0), $this->width, round($i*$this->getProp("ySclInc"),0), IMG_COLOR_STYLED);
  784. } else {
  785. $this->div->drawLine(0,round($i*$this->getProp("ySclInc"),0),$this->width,round($i*$this->getProp("ySclInc"),0));
  786. }
  787. }
  788. }
  789. if ($this->mode=='image') {
  790. imageline($this->img, 0, $this->height-1, $this->width-1, $this->height-1, IMG_COLOR_STYLED); //Last lines
  791. imageline($this->img, $this->width-1, $this->height-1, $this->width-1, 0, IMG_COLOR_STYLED);
  792. imageline($this->img, 0, $this->height, 0, 0, IMG_COLOR_STYLED);
  793. imageline($this->img, 0, 0, $this->width, 0, IMG_COLOR_STYLED);
  794. } else {
  795. $this->div->drawLine(0,$this->height-1,$this->width-1,$this->height-1);
  796. $this->div->drawLine($this->width-1,$this->height-1,$this->width-1,0);
  797. $this->div->drawLine(0,$this->height,0,0);
  798. $this->div->drawLine(0,0,$this->width,0);
  799. $this->div->setStyle(1);
  800. }
  801. }
  802. if ($this->getProp("type","line")!="pie") {
  803. if ($this->getProp("showhorizscale",true)) {
  804. if ($this->getProp("scale","numeric")=="date") {
  805. $start = $this->getProp("startdate",-9);
  806. $end = $this->getProp("enddate",time());
  807. if (is_string($start)) {
  808. $start = strtotime($start);
  809. }
  810. if (is_string($end)) {
  811. $end = strtotime($end);
  812. }
  813. $start = getdate($start);
  814. $end = getdate($end);
  815. $dDate = ($end[0] - $start[0])/$this->getProp("xincpts");
  816. $showyr = $this->getProp("showyear",($start['year']!=$end['year']));
  817. $format = $this->getProp("dateformat",1);
  818. }elseif (is_array($this->getProp("scale","numeric"))) {
  819. $scale = $this->getProp("scale");
  820. $this->setProp("xincpts",count($scale)-1);
  821. }
  822. for($i=0;$i<$this->getProp("xincpts")+1;$i++) { //Create horiz scale
  823. if ($this->getProp("scale","numeric")=="numeric") {
  824. $text = ($this->getProp("type","line")=="bar"?$i:trim(round((($i/$this->getProp("xincpts"))*($this->xMax-$this->xMin))+$this->xMin,6)));
  825. }elseif ($this->getProp("scale","numeric")=="date") {
  826. $date = getdate($start[0]+($i*$dDate));
  827. if ($date['mday']>15 && $this->getProp('rounddateto',false)=='month') {
  828. $date = getdate($start[0]+(($i+0.5)*$dDate));
  829. }
  830. $text = ($format<=2?substr($date['month'],0,3):$date['month']);
  831. if ($showyr) {
  832. $text .= " ".substr($date['year'],($format==4 || $format==2?0:2),($format==4 || $format==2?4:2));
  833. }
  834. }elseif (is_array($this->getProp("scale","numeric"))) {
  835. $text = $scale[$i];
  836. } else {
  837. $this->error("Scale format not understood.");
  838. }
  839. $size = imagettfbbox($this->getProp("textsize",8),
  840. $this->getProp("textAngle",0),
  841. $this->getProp("font",realpath($this->font_name)),
  842. $text);
  843. if ($this->mode=="image") {
  844. imagettftext($this->img,$this->getProp("textsize",8),
  845. $this->getProp("textAngle",0),
  846. round(($i*($this->width/$this->getProp("xincpts")))-($i!=$this->getProp("xincpts")?($i!=0?(.5*$size[2]):0):$size[2]),0)-($this->getProp("type","line")=="bar"?.5*($this->width/count($this->xPts[0])):0),
  847. $this->height+abs($size[5])+3,
  848. $grid,
  849. $this->getProp("font",realpath($this->font_name)),
  850. $text);
  851. } else {
  852. $this->div->setFontSize(8);
  853. $this->div->drawString(round(($i*($this->width/$this->getProp("xincpts")))-($i!=$this->getProp("xincpts")?($i!=0?(.5*$size[2]):0):$size[2]),0)-($this->getProp("type","line")=="bar"?.5*($this->width/count($this->xPts[0])):0),$this->height,$text);
  854. }
  855. }
  856. }
  857. if ($this->getProp("showvertscale",true)) {
  858. for($i=0;$i<$this->getProp("yincpts")+1;$i++) { //Create vert scale
  859. $text = $vertScaleText[$i];
  860. $size = $vertScaleSize[$i];
  861. if ($this->mode=="image") {
  862. imagettftext($this->img,
  863. $this->getProp("textsize",8),
  864. $this->getProp("textAngle",0),
  865. $this->width+3,round((($this->getProp("yincpts")-$i)*($this->height/$this->getProp("yincpts")))-($text!=$this->yMax?($text!=$this->yMin?(.5*$size[5]):0):$size[5]),0),
  866. $grid,
  867. $this->getProp("font",realpath($this->font_name)),
  868. $text);
  869. } else {
  870. $this->div->setFontSize(8);
  871. $this->div->drawString($this->width+3,
  872. round((($this->getProp("yincpts")-$i)*($this->height/$this->getProp("yincpts")))-($i==0?abs($size[5]):0),0),
  873. $text);
  874. }
  875. }
  876. }
  877. }
  878. foreach ($this->xPts as $ex => $ind) {
  879. $xPts = $this->xPts[$ex];
  880. $yPts = $this->yPts[$ex];
  881. $g = $this->width/count($xPts);
  882. $color = $this->getProp("color",array(0,0,255,0),$ex);
  883. $fore = imagecolorallocatealpha($this->img,$color[0],$color[1],$color[2],$color[3]);
  884. if ($this->getProp("type","line")=="line") { //Draw graph
  885. for($i=1;$i<$this->numPoints[$ex];$i++) {
  886. $this->imageSmoothAlphaLine($this->img,$xPts[$i-1],$this->height-$yPts[$i-1],$xPts[$i],$this->height-$yPts[$i],$color[0],$color[1],$color[2],$color[3]);
  887. }
  888. }elseif ($this->getProp("type","line")=="bar") {
  889. for($i=0;$i<$this->numPoints[$ex];$i++) {
  890. $this->drawBar($g*$i+($i==0?$g*(1-$this->getProp("barwidth",1)):0),$this->height-$yPts[$i],$g*$i+$this->getProp("barwidth",1)*$g,$this->height-1,$i);
  891. }
  892. }elseif ($this->getProp("type","line")=="pie"){
  893. $this->drawPie($ex);
  894. }elseif ($this->getProp("type","line")!="dot") {
  895. $this->error("Type property not understood.",E_USER_ERROR);
  896. }
  897. $width = $this->getProp("pointwidth",5,$ex);
  898. $height = $this->getProp("pointheight",$width,$ex);
  899. $point = $this->getProp("pointcolor",$color,$ex);
  900. $col = imagecolorallocatealpha($this->img,$point[0],$point[1],$point[2],$point[3]);
  901. if ($this->getProp("pointstyle",0,$ex)!=0) {
  902. foreach ($xPts as $k => $xpt) {
  903. $x = $xPts[$k];
  904. $y = $this->height-$yPts[$k];
  905. if ($this->getProp("endstyle",0,$ex)!=0 && $k==count($xPts)-1) {
  906. continue;
  907. }
  908. switch ($this->getProp("pointstyle",0,$ex)) { //Draw points
  909. case 1: //Filled rectangle
  910. imagefilledrectangle($this->img,$x-.5*$width,$y-.5*$height,$x+.5*$width,$y+.5*$height,$col);
  911. break;
  912. case 2: //Open rectangle
  913. if (!$this->getProp("clearback",false,$ex)) {
  914. imagefilledrectangle($this->img,$x-.5*$width,$y-.5*$height,$x+.5*$width,$y+.5*$height,$back);
  915. }
  916. imagerectangle($this->img,$x-.5*$width,$y-.5*$height,$x+.5*$width,$y+.5*$height,$col);
  917. break;
  918. case 3: //Filled Triangle
  919. imagefilledpolygon($this->img,array($x-.5*$width,$y+.5*$height,$x+.5*$width,$y+.5*$height,$x,$y-.5*$height),3,$col);
  920. break;
  921. case 4: //Open Triangle
  922. if (!$this->getProp("clearback",false,$ex)) {
  923. imagefilledpolygon($this->img,array($x-.5*$width,$y+.5*$height,$x+.5*$width,$y+.5*$height,$x,$y-.5*$height),3,$back);
  924. }
  925. imagepolygon($this->img,array($x-.5*$width,$y+.5*$height,$x+.5*$width,$y+.5*$height,$x,$y-.5*$height),3,$col);
  926. break;
  927. case 5: //Filled n-gon, for testing only!
  928. $n = $this->getProp("pointsides",7);
  929. if ($n<7) {
  930. $this->error("Point shape must be 7 or more sides!");
  931. }elseif ($n>30){
  932. $this->error("Just use a circle ;)");
  933. }
  934. $s = $width;
  935. unset($points);
  936. for($i=1;$i<$n+1;$i++) {
  937. $o=($i)*(360/$n);
  938. $points[] = ($s*cos($o))+$x;
  939. $points[] = ($s*sin($o))+$y;
  940. }
  941. imagefilledpolygon($this->img,$points,$n,$col);
  942. break;
  943. case 6: //Open n-gon, for testing only!
  944. $n = $this->getProp("pointsides",7,$ex);
  945. if ($n<7) {
  946. $this->error("Point shape must be 7 or more sides!");
  947. }elseif ($n>30) {
  948. $this->error("Just use a circle ;)");
  949. }
  950. $s = $width;
  951. unset($points);
  952. for($i=1;$i<$n+1;$i++) {
  953. $o=($i)*(360/$n);
  954. $points[] = ($s*cos($o))+$x;
  955. $points[] = ($s*sin($o))+$y;
  956. }
  957. if (!$this->getProp("clearback",false,$ex)) {
  958. imagefilledpolygon($this->img,$points,$n,$back);
  959. }
  960. imagepolygon($this->img,$points,$n,$col);
  961. break;
  962. case 7: //Filled ellipse, make width = height for a circle
  963. imagefilledellipse($this->img,$x,$y,$width,$height,$col);
  964. break;
  965. case 8: //Open ellipse
  966. if (!$this->getProp("clearback",false,$ex)) {
  967. imagefilledellipse($this->img,$x,$y,$width,$height,$back);
  968. }
  969. imageellipse($this->img,$x,$y,$width,$height,$col);
  970. break;
  971. case 9: //Image
  972. if ($this->getProp("pointimgsrc",false,$ex)===false) {
  973. $this->error("You must set the pointimgsrc property before using the image point style",true);
  974. }
  975. if (!isset($this->imgCache)) {
  976. $im = $this->loadimg($this->getProp("pointimgsrc",false,$ex));
  977. if ($this->getProp("pointimgscale",false,$ex)) {
  978. $height = round($width*(ImageSX($im)/ImageSY($im)),0);
  979. }
  980. $tmp = imagecreatetruecolor($width,$height);
  981. if (!imagecopyresized($tmp,$im,0,0,0,0,$width,$height,ImageSX($im),ImageSY($im))) {
  982. $this->error("Error resizing point image");
  983. }
  984. $this->cacheImg($tmp);
  985. }
  986. $tmp = $this->cacheImg();
  987. if (!imagecopy($this->img,$tmp,$x-.5*$width,$y-.5*$height,0,0,$width,$height)) {
  988. $this->error("Error inserting point image");
  989. }
  990. break;
  991. }
  992. }
  993. unset($this->imgCache);
  994. }
  995. $arrow = $this->getProp("arrowcolor",$color,$ex);
  996. $col = imagecolorallocatealpha($this->img,$arrow[0],$arrow[1],$arrow[2],$arrow[3]);
  997. $x2 = $xPts[count($xPts)-1];
  998. $y2 = $this->height-$yPts[count($yPts)-1];
  999. $x1 = $xPts[count($xPts)-2];
  1000. $y1 = $this->height-$yPts[count($yPts)-2];
  1001. switch ($this->getProp("endstyle",0,$ex)){ //Draw end
  1002. case 1: //Open arrow
  1003. $arrhead = $this->getProp("arrowwidth",25,$ex);
  1004. $arrang = $this->getProp("arrowangle",14,$ex);
  1005. $arrow = $this->drawArrowheads ($x1, $y1, $x2, $y2, $arrhead, $arrang);
  1006. if (!$this->getProp("clearback",false,$ex)) {
  1007. imagefilledpolygon($this->img,array (
  1008. $arrow['x1'], $arrow['y1'],
  1009. $arrow['x2'], $arrow['y2'],
  1010. $x2, $y2
  1011. ),3,$back);
  1012. }
  1013. imageline($this->img, $arrow['x1'], $arrow['y1'], $arrow['x2'], $arrow['y2'], $col);
  1014. imageline($this->img, $x2, $y2, $arrow['x1'], $arrow['y1'], $col);
  1015. imageline($this->img, $x2, $y2, $arrow['x2'], $arrow['y2'], $col);
  1016. break;
  1017. case 2: //Filled arrow
  1018. $arrhead = $this->getProp("arrowwidth",25,$ex);
  1019. $arrang = $this->getProp("arrowangle",14,$ex);
  1020. $arrow = $this->drawArrowheads ($x1, $y1, $x2, $y2, $arrhead, $arrang);
  1021. imagefilledpolygon($this->img,array (
  1022. $arrow['x1'], $arrow['y1'],
  1023. $arrow['x2'], $arrow['y2'],
  1024. $x2, $y2
  1025. ),3,$col);
  1026. break;
  1027. }
  1028. }
  1029. $sX = ImageSX($this->img);
  1030. $sY = ImageSY($this->img);
  1031. $font = $this->getProp("font", realpath($this->font_name));
  1032. $labels = $this->getProp("labelcolor", array(0,0,0,0));
  1033. $label = imagecolorallocatealpha($this->img, $labels[0], $labels[1], $labels[2], $labels[3]);
  1034. $fX = $sX;
  1035. $fY = $sY;
  1036. $tB = 0;
  1037. if ($this->getProp("title",false)!==false) {
  1038. $title = $this->getProp("title");
  1039. $tsize = imagettfbbox($this->getProp("titlesize",24),0,$font,$title);
  1040. $tB = abs($tsize[5])+10;
  1041. $fY += $tB + 5;
  1042. }
  1043. if ($this->getProp("xlabel",false)!==false || $this->getProp("xllabel",false)!==false || $this->getProp("xrlabel",false)!==false) {
  1044. if($this->getProp("xlabel",false)!==false){
  1045. $xlabel = $this->getProp("xlabel");
  1046. $xsize = imagettfbbox($this->getProp("labelsize",14),0,$font,$xlabel);
  1047. }
  1048. if($this->getProp("xllabel",false)!==false){
  1049. $xllabel = $this->getProp("xllabel");
  1050. $xlsize = imagettfbbox($this->getProp("xllabelsize",10),0,$font,$xllabel);
  1051. }
  1052. if($this->getProp("xrlabel",false)!==false){
  1053. $xrlabel = $this->getProp("xrlabel");
  1054. $xrsize = imagettfbbox($this->getProp("xrlabelsize",10),0,$font,$xrlabel);
  1055. }
  1056. @$fY += max(array(abs($xsize[5]),abs($xlsize[5]),abs($xrsize[5])))+5+$this->getProp('xlabeloffset',5);
  1057. }
  1058. if ($this->getProp("ylabel",false)!==false) {
  1059. $ylabel = $this->getProp("ylabel");
  1060. $ysize = imagettfbbox($this->getProp("labelsize",14),90,$font,$ylabel);
  1061. $fX += abs($ysize[2])+15;
  1062. }
  1063. if(!isset($xsize)) $xsize[5] = 30;
  1064. if ($fX != $sX || $fY != $sY) {
  1065. $timg = imagecreatetruecolor($fX,$fY);
  1066. imagecopy($timg,$this->img,0,$tB,0,0,$sX,$sY);
  1067. imagefill($timg,0,0,$back);
  1068. if ($this->getProp("ylabel",false)!==false) {
  1069. imagettftext($timg,$this->getProp("labelsize",14),90,$sX+10,round(($fY-$ysize[5])/2,0),$label,$font,$ylabel);
  1070. }
  1071. if ($this->getProp("xlabel",false)!==false) {
  1072. imagettftext($timg,$this->getProp("labelsize",14),0,round(($fX-abs($xsize[2]))/2,0),$sY+$tB+$this->getProp('xlabeloffset',5),$label,$font,$xlabel);
  1073. }
  1074. if ($this->getProp("xllabel",false)!==false) {
  1075. imagettftext($timg,$this->getProp("xllabelsize",10),0,$this->getProp('xrlabeloffset',10),floor($sY+$tB+(abs($xsize[5])-abs($xlsize[5]))/2)+$this->getProp('xlabeloffset',5),$label,$font,$xllabel);
  1076. }
  1077. if ($this->getProp("xrlabel",false)!==false) {
  1078. imagettftext($timg,$this->getProp("xrlabelsize",10),0,round(($fX-abs($xrsize[2])-20),0)+$this->getProp('xllabeloffset',0),floor($sY+$tB+(abs($xsize[5])-abs($xrsize[5]))/2)+$this->getProp('xlabeloffset',5),$label,$font,$xrlabel);
  1079. }
  1080. if ($this->getProp("title",false)!==false) {
  1081. imagettftext($timg,$this->getProp("titlesize",24),0,round(($fX-abs($tsize[2]))/2,0),abs($tsize[5]),$label,$font,$title);
  1082. }
  1083. $this->img = $timg;
  1084. }
  1085. if ($this->getProp("benchmark",false)) {
  1086. echo (round(microtime(true) - $time,3)*1000)."ms";
  1087. }
  1088. if ($this->getProp("showkey",false)) {
  1089. $acthei = $this->getProp("actheight",$this->height);
  1090. $actwid = $this->getProp("actwidth",$this->width);
  1091. $size = $this->getProp("keysize", 10);
  1092. $font = $this->getProp("keyfont", realpath($this->keyfont_name));
  1093. $indhei = 0;
  1094. $indwid = 0;
  1095. $type = $this->getProp("type",'line');
  1096. if ($type!='line') {
  1097. $keys = $this->getProp('key',false);
  1098. $num = $this->numPoints[0];
  1099. $dis = $this->getProp("keyinfo",0);
  1100. if ($dis>3 || $dis<0) {
  1101. $this->error("keyinfo not understood.");
  1102. }
  1103. if ($dis!=0) {
  1104. $d = $this->dVars[0];
  1105. $g = array_sum($d);
  1106. foreach ($d as $i => $e) {
  1107. $pr[] = ($e/$g)*100;
  1108. }
  1109. if (($fd=array_sum($pr))<1) {
  1110. $pr[count($pr)-1] += 1-$fd;
  1111. }
  1112. foreach ($d as $i => $e) {
  1113. $p[] = round($pr[$i],0).'%';
  1114. }
  1115. if ($dis==1 || $dis==3) {
  1116. foreach ($keys as $i => $k) {
  1117. $keys[$i] = $p[$i].' '.$k;
  1118. }
  1119. }
  1120. if ($dis==2 || $dis==3) {
  1121. foreach ($keys as $i => $k) {
  1122. $keys[$i] = $d[$i].' '.$k;
  1123. }
  1124. }
  1125. }
  1126. } else {
  1127. $num = $this->lines;
  1128. }
  1129. for($i=0;$i<$num;$i++) {
  1130. if ($type=='line') {
  1131. $keys[$i] = $this->getProp("key",false,$i);
  1132. if ($keys[$i]==false) {
  1133. $this->error("You must set the keys property for dataset $i.");
  1134. }
  1135. }
  1136. $ksize[$i]['size'] = imagettfbbox($size, 0, $font, $keys[$i]);
  1137. $indhei = max($indhei,$ksize[$i]['height'] = abs($ksize[$i]['size'][5]));
  1138. $indwid = max($indwid,$ksize[$i]['width'] = abs($ksize[$i]['size'][2])+$indhei+5);
  1139. }
  1140. $indhei += 4;
  1141. $indwid += 10;
  1142. $wspc = $this->getProp("keywidspc",10);
  1143. $oldwid = $actwid;
  1144. $oldhei = $acthei;
  1145. if($num<12)
  1146. $khei = $indhei*$num+14;
  1147. else
  1148. $khei = $indhei*($num+1)+14;
  1149. $actwid = $actwid+$indwid+$wspc;
  1150. $acthei = max($acthei,$khei);
  1151. $timg = imagecreatetruecolor($actwid,$acthei);
  1152. $backcolor = $this->getProp("backcolor");
  1153. $back = imagecolorallocate($this->img,$backcolor[0],$backcolor[1],$backcolor[2]);
  1154. imagefill($timg,0,0,$back);
  1155. imagecopy($timg,$this->img,0,0,0,0,$oldwid,$oldhei);
  1156. $this->img = $timg;
  1157. $y = ($acthei-$khei)/2;
  1158. $x = $oldwid+$wspc+4;
  1159. imagerectangle($this->img,$x-4,$y,$actwid-1,$khei+($acthei-$khei)/2+4,imagecolorallocate($this->img,0,0,0));
  1160. $black = imagecolorallocate($this->img,0,0,0);
  1161. if ($this->getProp('type','line')=='bar') {
  1162. $colorlist = $this->getProp("colorlist",false);
  1163. if ($colorlist === true || $colorlist === 1 || $colorlist === "true") {
  1164. $colorlist = array(array(255, 203, 3),array(220, 101, 29),array(189, 24, 51),array(214, 0, 127),array(98, 1, 96),array(0, 62, 136),array(0, 102, 179),array(0, 145, 195),array(0, 115, 106),array(178, 210, 52),array(137, 91, 74),array(82, 56, 47));
  1165. }
  1166. if ($colorlist) {
  1167. $col = $colorlist[$i-1];
  1168. } else {
  1169. $this->error("colorlist must be set for keys to work.",E_USER_ERROR);
  1170. }
  1171. } else {
  1172. $colorlist = $this->getProp('colorlist',array(array(125, 203, 3),array(220, 101, 29),array(189, 24, 51),array(34, 78, 120),array(120, 1, 60),array(0, 62, 136),array(0, 102, 179),array(0, 145, 195),array(0, 115, 106),array(178, 210, 52),array(137, 91, 74),array(82, 56, 47)));
  1173. }
  1174. foreach ($keys as $i => $k) {
  1175. if ($type=='line') {
  1176. $col = $this->getProp("color",array(0,0,255,0),$i);
  1177. } else {
  1178. $col = $colorlist[$i];
  1179. }
  1180. if (isset($col[3])) {
  1181. $alpha = $col[3];
  1182. } else {
  1183. $alpha = 0;
  1184. }
  1185. $fco = imagecolorallocatealpha($this->img, $col[0], $col[1], $col[2], $alpha);
  1186. imagefilledrectangle($this->img,$x,$y+=4,$x+$indhei-4,$y+=$indhei-2,$fco);
  1187. $y-=$indhei-2+4;
  1188. imagerectangle($this->img,$x,$y+=4,$x+$indhei-4,$y+=$indhei-2,$black);
  1189. imagettftext($this->img,$size,0,$x+$indhei,$y-1,$black,$font,$k);
  1190. }
  1191. }
  1192. $this->error(true);
  1193. return true;
  1194. }
  1195. private function loadimg($file) {
  1196. if (substr($file,-4)==".png") {
  1197. $im = imagecreatefrompng($file);
  1198. }elseif (substr($file,-4)==".gif") {
  1199. $im = imagecreatefromgif ($file);
  1200. }elseif (substr($file,-4)==".jpg" || substr($file,-5)==".jpeg") {
  1201. $im = imagecreatefromjpeg($file);
  1202. }elseif (substr($file,-4)==".bmp") {
  1203. $im = imagecreatefromwbmp($file);
  1204. } else {
  1205. $this->error("Image format not understood",true);
  1206. }
  1207. return $im;
  1208. }
  1209. private function imageSmoothAlphaLine ($image, $x1, $y1, $x2, $y2, $r, $g, $b, $alpha=0) { //Thanks php.net poster
  1210. if ($this->mode=='image') {
  1211. if ($x2==$x1) {
  1212. $x2++;
  1213. }
  1214. if ($y2==$y1) {
  1215. $y2++;
  1216. }
  1217. $icr = $r;$icg = $g;$icb = $b;$dcol = imagecolorallocatealpha($image, $icr, $icg, $icb, $alpha);$m = ($y2 - $y1) / ($x2 - $x1);$b = $y1 - $m * $x1;
  1218. if (abs ($m) <2) {
  1219. $x = min($x1, $x2);$endx = max($x1, $x2) + 1;
  1220. while ($x < $endx) {
  1221. $y = $m * $x + $b;$ya = ($y == floor($y) ? 1: $y - floor($y));$yb = ceil($y) - $y;$trgb = ImageColorAt($image, $x, floor(abs($y)));$tcr = ($trgb >> 16) & 0xFF;$tcg = ($trgb >> 8) & 0xFF;$tcb = $trgb & 0xFF;imagesetpixel($image, $x, floor($y), imagecolorallocatealpha($image, ($tcr * $ya + $icr * $yb), ($tcg * $ya + $icg * $yb), ($tcb * $ya + $icb * $yb), $alpha));$trgb = ImageColorAt($image, $x, ceil(abs($y)));$tcr = ($trgb >> 16) & 0xFF;$tcg = ($trgb >> 8) & 0xFF;$tcb = $trgb & 0xFF;imagesetpixel($image, $x, ceil($y), imagecolorallocatealpha($image, ($tcr * $yb + $icr * $ya), ($tcg * $yb + $icg * $ya), ($tcb * $yb + $icb * $ya), $alpha));$x++;
  1222. }
  1223. } else {
  1224. $y = min($y1, $y2);$endy = max($y1, $y2) + 1;
  1225. while ($y < $endy) {
  1226. @$x = ($y - $b) / $m;
  1227. $xa = ($x == floor($x) ? 1: $x - floor($x));$xb = ceil($x) - $x;$trgb = ImageColorAt($image, floor(abs($x)), $y);$tcr = ($trgb >> 16) & 0xFF;$tcg = ($trgb >> 8) & 0xFF;$tcb = $trgb & 0xFF;
  1228. imagesetpixel($image, floor($x), $y, imagecolorallocatealpha($image, ($tcr * $xa + $icr * $xb), ($tcg * $xa + $icg * $xb), ($tcb * $xa + $icb * $xb), $alpha));$trgb = ImageColorAt($image, ceil(abs($x)), $y);$tcr = ($trgb >> 16) & 0xFF;$tcg = ($trgb >> 8) & 0xFF;$tcb = $trgb & 0xFF;imagesetpixel ($image, ceil(abs($x)), $y, imagecolorallocatealpha($image, ($tcr * $xb + $icr * $xa), ($tcg * $xb + $icg * $xa), ($tcb * $xb + $icb * $xa), $alpha));$y++;
  1229. }
  1230. }
  1231. } else {
  1232. $this->div->setColor($r,$g,$b);
  1233. $this->div->drawLine($x1,$y1,$x2,$y2);
  1234. }
  1235. }
  1236. private function drawArrowheads ($x1, $y1, $x2, $y2, $arrhead, $arrang) { //Thanks php.net poster (tried to do the math myself, not fun :()
  1237. $debug = false;
  1238. if (($x2-$x1)==0) {
  1239. if ($y1 == 0) {
  1240. $slope = 0;
  1241. } else {
  1242. $slope = 'INFINITE';
  1243. }
  1244. } else {
  1245. $slope = -($y2-$y1)/($x2-$x1);
  1246. }
  1247. if ($debug) {
  1248. echo ("Values of xy.. before add/sub</br>");
  1249. echo ("$x1, $y1 $x2, $y2</br>");
  1250. }
  1251. if ($slope == 'INFINITE') {
  1252. $ang = 90;
  1253. } else {
  1254. $ang = atan ($slope);
  1255. $ang = ($ang * 180)/pi();
  1256. }
  1257. $arrang1 = ($ang - $arrang);
  1258. $arrang1 = ($arrang1*pi())/180;
  1259. $arrang2 = ($ang + $arrang);
  1260. $arrang2 = ($arrang2*pi())/180;
  1261. $arx1 = (floor(cos($arrang1)*$arrhead));
  1262. $ary1 = (floor(sin($arrang1)*$arrhead));
  1263. $arx2 = (floor(cos($arrang2)*$arrhead));
  1264. $ary2 = (floor(sin($arrang2)*$arrhead));
  1265. if ($ang==0) {
  1266. if ($x2>$x1) {
  1267. $arx1=$x2-$arx1;$ary1=$y2-$ary1;$arx2=$x2-$arx2;$ary2=$y2-$ary2;
  1268. }elseif ($x2<$x1) {
  1269. $arx1=$x2+$arx1;$ary1=$y2-$ary1;$arx2=$x2+$arx2;$ary2=$y2-$ary2;
  1270. }
  1271. }
  1272. if ($ang>0&&$ang<90) {
  1273. if (($x2>$x1)&&($y2<$y1)) {
  1274. $arx1=$x2-$arx1;$ary1=$y2+$ary1;$arx2=$x2-$arx2;$ary2=

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