PHPGraphLib.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. <?php
  2. ///////////////////////////////////////////////////////////
  3. //PHPGraphLib - PHP Graphing Library v2.31
  4. //Author: Elliott Brueggeman
  5. //PHP v4.04 + compatible
  6. //Please visit www.ebrueggeman.com for usage policy
  7. //and full documentation + examples
  8. ///////////////////////////////////////////////////////////
  9. class PHPGraphLib {
  10. //---------------user changeable defaults----------------/
  11. var $height = 300;
  12. var $width = 400;
  13. var $data_max_allowable = 9999999999999999;
  14. var $data_min_allowable = -9999999999999999;
  15. var $data_set_count = 0;
  16. //set to actual font heights and widths used
  17. var $title_char_width = 6;
  18. var $title_char_height = 12;
  19. var $text_width = 6;
  20. var $text_height = 12;
  21. var $data_value_text_width = 6;
  22. var $data_value_text_height = 12;
  23. //padding between axis and value displayed
  24. var $axis_value_padding = 5;
  25. //space b/t top of bar or center of point and data value
  26. var $data_value_padding = 5;
  27. //default margin % of width / height
  28. var $x_axis_default_percent = 12;
  29. var $y_axis_default_percent = 8;
  30. //data point diameter in px
  31. var $data_point_width = 6;
  32. //user changeable default booleans (show element by default?)
  33. var $bool_bar_outline = true;
  34. var $bool_x_axis = true;
  35. var $bool_y_axis = true;
  36. var $bool_x_axis_values = true;
  37. var $bool_y_axis_values = true;
  38. var $bool_grid = true;
  39. var $bool_line = false;
  40. var $bool_data_values = false;
  41. var $bool_x_axis_values_vert = true;
  42. var $bool_data_points = false;
  43. var $bool_title_left = false;
  44. var $bool_title_right = false;
  45. var $bool_title_center = true;
  46. //----------internal variables (do not change)------------/
  47. var $image;
  48. var $output_file;
  49. var $error;
  50. var $bool_x_axis_setup = false;
  51. var $bool_y_axis_setup = false;
  52. var $bool_data = false;
  53. var $bool_bars_generate = true;
  54. var $bool_bars = true;
  55. var $bool_background = false;
  56. var $bool_title = false;
  57. var $bool_all_negative = false;
  58. var $bool_all_positive = false;
  59. var $bool_gradient = false;
  60. var $bool_user_data_range = false;
  61. var $all_zero_data = false;
  62. var $bool_gradient_colors_found; //init as array
  63. //color vars
  64. var $background_color;
  65. var $grid_color;
  66. var $bar_color;
  67. var $outline_color;
  68. var $x_axis_text_color;
  69. var $y_axis_text_color;
  70. var $title_color;
  71. var $x_axis_color;
  72. var $y_axis_color;
  73. var $data_point_color;
  74. var $data_value_color;
  75. var $line_color;
  76. var $line_color_default;
  77. var $goal_line_color;
  78. var $goal_line_color_custom;
  79. //gradient colors stored as arrays, not allocated color
  80. var $gradient_color_1;
  81. var $gradient_color_2;
  82. var $gradient_color_array;
  83. var $gradient_max = 200;
  84. var $gradient_handicap;
  85. //data vars
  86. var $data_array;
  87. var $data_count;
  88. var $data_min;
  89. var $data_max;
  90. var $true_displayed_max_value;
  91. var $true_displayed_min_value;
  92. var $data_currency;
  93. var $data_format_array;
  94. var $data_additional_length = 0;
  95. var $data_format_generic;
  96. //bar vars / scale
  97. var $bar_spaces;
  98. var $bar_width;
  99. var $space_width;
  100. var $unit_scale;
  101. var $goal_line_array;
  102. var $horiz_grid_lines;
  103. var $vert_grid_lines;
  104. var $horiz_grid_values;
  105. var $vert_grid_values;
  106. //text / font
  107. var $title_x;
  108. var $title_y;
  109. //axis points
  110. var $x_axis_x1;
  111. var $x_axis_y1;
  112. var $x_axis_x2;
  113. var $x_axis_y2;
  114. var $y_axis_x1;
  115. var $y_axis_y1;
  116. var $y_axis_x2;
  117. var $y_axis_y2;
  118. var $x_axis_margin; //aka bottom margin
  119. var $y_axis_margin; //aka left margin
  120. var $data_range_max;
  121. var $data_range_min;
  122. var $top_margin = 0;
  123. var $right_margin = 0;
  124. var $range_divisor_factor = 25; //controls auto-adjusting grid interval
  125. var $data_point_array;
  126. //multiple dataset variables
  127. var $bool_multi_offset = true;
  128. var $multi_offset_two = 24; //percent of bar width
  129. var $multi_offset_three = 15; //percent of bar width
  130. var $multi_offset_four = 10; //percent of bar width
  131. var $multi_offset_five = 9; //percent of bar width
  132. var $multi_gradient_colors_1;
  133. var $multi_gradient_colors_2;
  134. var $multi_bar_colors;
  135. var $color_darken_factor = 30;//percent decrease
  136. //legend variables
  137. var $bool_legend = false;
  138. var $legend_text_width = 6; //in px...
  139. var $legend_max_chars = 15;
  140. var $legend_total_chars;
  141. var $legend_text_height = 12;
  142. var $legend_padding=4; //padding inside legend box
  143. var $legend_width;
  144. var $legend_height;
  145. var $legend_x;
  146. var $legend_y;
  147. var $legend_text_color;
  148. var $legend_outline_color;
  149. var $legend_swatch_outline_color;
  150. var $legend_color;
  151. var $legend_titles = array();
  152. function PHPGraphLib($width = '', $height = '', $output_file = NULL) {
  153. if (!empty($width) && !empty($height)) {
  154. $this->width = $width;
  155. $this->height = $height;
  156. }
  157. $this->output_file = $output_file;
  158. $this->initialize();
  159. $this->allocateColors(); //sets default colors
  160. }
  161. function initialize() {
  162. //header must be sent before any html or blank space output
  163. if (!$this->output_file){ header("Content-type: image/png"); }
  164. $this->image = @imagecreate($this->width, $this->height)
  165. or die("Cannot Initialize new GD image stream - Check your PHP setup");
  166. $this->data_point_array = array();
  167. $this->goal_line_array = array();
  168. $this->multi_bar_colors = array();
  169. $this->multi_gradient_colors_1 = array();
  170. $this->multi_gradient_colors_2 = array();
  171. //these values are now arrays
  172. $this->gradient_handicap = array();
  173. $this->bool_gradient_colors_found = array();
  174. $this->legend_total_chars = array();
  175. $this->line_color = array();
  176. $this->data_format_array = array();
  177. $this->horiz_grid_lines = array();
  178. $this->vert_grid_lines = array();
  179. $this->horiz_grid_values = array();
  180. $this->vert_grid_values = array();
  181. }
  182. function createGraph() {
  183. //main class method - called last
  184. //setup axis if not already setup by user
  185. if ($this->bool_data){
  186. if (!$this->bool_x_axis_setup){ $this->setupXAxis(); }
  187. if (!$this->bool_y_axis_setup){ $this->setupYAxis(); }
  188. //calculations
  189. $this->calcTopMargin();
  190. $this->calcRightMargin();
  191. $this->calcCoords();
  192. $this->setupData();
  193. //start creating actual image elements
  194. if ($this->bool_background){ $this->generateBackgound(); }
  195. //always gen grid values, even if not displayed
  196. $this->generateGrid();
  197. if ($this->bool_bars_generate){ $this->generateBars(); }
  198. if ($this->bool_data_points){ $this->generateDataPoints(); }
  199. if ($this->bool_legend){ $this->generateLegend(); }
  200. if ($this->bool_title){ $this->generateTitle(); }
  201. if ($this->bool_x_axis){ $this->generateXAxis(); }
  202. if ($this->bool_y_axis){ $this->generateYAxis(); }
  203. }
  204. else {
  205. $this->error[] = "No valid data added to graph. Add data with the addData() function.";
  206. }
  207. //display errors
  208. $this->displayErrors();
  209. //output to browser
  210. if ($this->output_file) {
  211. imagepng($this->image, $this->output_file);
  212. }
  213. else {
  214. imagepng($this->image);
  215. }
  216. imagedestroy($this->image);
  217. }
  218. function setupData() {
  219. $this->bar_spaces = ($this->data_count * 2) + 1;
  220. $unit_width = ($this->width - $this->y_axis_margin - $this->right_margin) / (($this->data_count * 2) + $this->data_count);
  221. if ($unit_width < 1) {
  222. //error units too small, too many data points or not large enough graph
  223. $this->bool_bars_generate = false;
  224. $this->error[] = "Graph too small or too many data points.";
  225. }
  226. else {
  227. //default space between bars is 1/2 the width of the bar
  228. //find bar and space widths. bar = 2 units, space = 1 unit
  229. $this->bar_width= 2 * $unit_width;
  230. $this->space_width = $unit_width;
  231. //now calculate height (scale) units
  232. $availVertSpace = $this->height - $this->x_axis_margin - $this->top_margin;
  233. if ($availVertSpace<1) {
  234. $this->bool_bars_generate=false;
  235. $this->error[] = "Graph height not tall enough.";
  236. //error scale units too small, x axis margin too big or graph height not tall enough
  237. }
  238. else {
  239. if ($this->bool_user_data_range) {
  240. //if all zero data, set fake max and min to range boundaries
  241. if ($this->all_zero_data) {
  242. if ($this->data_range_min > $this->data_min) { $this->data_min = $this->data_range_min; }
  243. if ($this->data_range_max < $this->data_max) { $this->data_max = $this->data_range_max; }
  244. }
  245. $graphTopScale = $this->data_range_max;
  246. $graphBottomScale = $this->data_range_min;
  247. $graphScaleRange = $graphTopScale - $graphBottomScale;
  248. $this->unit_scale = $availVertSpace / $graphScaleRange;
  249. $this->data_max = $this->data_range_max;
  250. $this->data_min = $this->data_range_min;
  251. if ($this->data_min < 0) {
  252. $this->x_axis_y1 -= round($this->unit_scale * abs($this->data_min));
  253. $this->x_axis_y2 -= round($this->unit_scale * abs($this->data_min));
  254. }
  255. //all data identical
  256. if ($graphScaleRange == 0) {
  257. $graphScaleRange = 100;
  258. }
  259. }
  260. else {
  261. //start at y value 0 or data min, whichever is less
  262. $graphBottomScale = ($this->data_min<0) ? $this->data_min : 0;
  263. $graphTopScale = ($this->data_max<0) ? 0 : $this->data_max ;
  264. $graphScaleRange = $graphTopScale - $graphBottomScale;
  265. //all data identical
  266. if ($graphScaleRange == 0) {
  267. $graphScaleRange = 100;
  268. }
  269. $this->unit_scale = $availVertSpace / $graphScaleRange;
  270. //now adjust x axis in y value if negative values
  271. if ($this->data_min < 0) {
  272. $this->x_axis_y1 -= round($this->unit_scale * abs($this->data_min));
  273. $this->x_axis_y2 -= round($this->unit_scale * abs($this->data_min));
  274. }
  275. }
  276. $this->bool_bars_generate = true;
  277. }
  278. }
  279. }
  280. //always port changes to generatebars() to extensions
  281. function generateBars() {
  282. $this->finalizeColors();
  283. $barCount = 0;
  284. $adjustment = 0;
  285. if ($this->bool_user_data_range && $this->data_min >= 0) {
  286. $adjustment = $this->data_min * $this->unit_scale;
  287. }
  288. //reverse array to order data sets in order of priority
  289. $this->data_array = array_reverse($this->data_array);
  290. $dataset_offset=0;
  291. if ($this->bool_multi_offset) {
  292. //set different offsets based on number of data sets
  293. switch ($this->data_set_count) {
  294. case 2: $dataset_offset = $this->bar_width * ($this->multi_offset_two / 100); break;
  295. case 3: $dataset_offset = $this->bar_width * ($this->multi_offset_three / 100); break;
  296. case 4: $dataset_offset = $this->bar_width * ($this->multi_offset_four / 100); break;
  297. case 5: $dataset_offset = $this->bar_width * ($this->multi_offset_five / 100); break;
  298. }
  299. }
  300. foreach ($this->data_array as $data_set_num => $data_set) {
  301. $lineX2 = NULL;
  302. $xStart = $this->y_axis_x1 + ($this->space_width / 2);
  303. foreach ($data_set as $key => $item) {
  304. $hideBarOutline = false;
  305. $x1=round($xStart + ($dataset_offset * $data_set_num));
  306. $x2=round(($xStart + $this->bar_width) + ($dataset_offset * $data_set_num));
  307. $y1=round($this->x_axis_y1 - ($item * $this->unit_scale) + $adjustment);
  308. $y2=round($this->x_axis_y1);
  309. //if we are using a user specified data range, need to limit what's displayed
  310. if ($this->bool_user_data_range) {
  311. if ($item <= $this->data_range_min) {
  312. //don't display, we are out of our allowed display range!
  313. $y1 = $y2;
  314. $hideBarOutline = true;
  315. }
  316. else if ($item >= $this->data_range_max) {
  317. //display, but cut off display above range max
  318. $y1 = $this->x_axis_y1 - ($this->true_displayed_max_value * $this->unit_scale) + $adjustment;
  319. }
  320. }
  321. //draw bar
  322. if ($this->bool_bars) {
  323. if ($this->bool_gradient) {
  324. //draw gradient if desired
  325. $this->drawGradientBar($x1, $y1, $x2, $y2, $this->multi_gradient_colors_1[$data_set_num], $this->multi_gradient_colors_2[$data_set_num], $data_set_num);
  326. }
  327. else {
  328. imagefilledrectangle($this->image, $x1, $y1,$x2, $y2, $this->multi_bar_colors[$data_set_num]);
  329. }
  330. //draw bar outline
  331. if ($this->bool_bar_outline && !$hideBarOutline) {
  332. imagerectangle($this->image, $x1, $y2, $x2, $y1, $this->outline_color);
  333. }
  334. }
  335. // draw line
  336. if ($this->bool_line) {
  337. $lineX1 = $x1 + ($this->bar_width / 2); //MIDPOINT OF BARS, IF SHOWN
  338. $lineY1 = $y1;
  339. if (isset($lineX2)) {
  340. imageline($this->image, $lineX2, $lineY2, $lineX1, $lineY1, $this->line_color[$data_set_num]);
  341. $lineX2 = $lineX1;
  342. $lineY2 = $lineY1;
  343. }
  344. else {
  345. $lineX2 = $lineX1;
  346. $lineY2 = $lineY1;
  347. }
  348. }
  349. // display data points
  350. if ($this->bool_data_points) {
  351. //dont draw datapoints here or will overlap poorly with line
  352. //instead collect coordinates
  353. $pointX = $x1 + ($this->bar_width / 2); //MIDPOINT OF BARS, IF SHOWN
  354. $this->data_point_array[] = array($pointX, $y1);
  355. }
  356. // display data values
  357. if ($this->bool_data_values) {
  358. $dataX = ($x1 + ($this->bar_width / 2)) - ((strlen($item) * $this->data_value_text_width) / 2);
  359. //value to be graphed is equal/over 0
  360. if ($item >= 0) {
  361. $dataY = $y1 - $this->data_value_padding - $this->data_value_text_height;
  362. }
  363. else {
  364. //check for item values below user spec'd range
  365. if ($this->bool_user_data_range && $item <= $this->data_range_min) {
  366. $dataY = $y1 - $this->data_value_padding - $this->data_value_text_height;
  367. }
  368. else {
  369. $dataY = $y1 + $this->data_value_padding;
  370. }
  371. }
  372. //add currency sign, formatting etc
  373. if ($this->data_format_array) {
  374. $item = $this->applyDataFormats($item);
  375. }
  376. if ($this->data_currency) {
  377. $item = $this->applyDataCurrency($item);
  378. }
  379. //recenter data position if necessary
  380. $dataX -= ($this->data_additional_length * $this->data_value_text_width) / 2;
  381. imagestring($this->image, 2, $dataX, $dataY, $item, $this->data_value_color);
  382. }
  383. //write x axis value
  384. if ($this->bool_x_axis_values) {
  385. if ($data_set_num == $this->data_set_count - 1) {
  386. if ($this->bool_x_axis_values_vert) {
  387. if ($this->bool_all_negative) {
  388. //we must put values above 0 line
  389. $textVertPos = round($this->y_axis_y2 - $this->axis_value_padding);
  390. }
  391. else {
  392. //mix of both pos and neg numbers
  393. //write value y axis bottom value (will be under bottom of grid even if x axis is floating due to
  394. $textVertPos = round($this->y_axis_y1 + (strlen($key) * $this->text_width) + $this->axis_value_padding);
  395. }
  396. $textHorizPos = round($xStart + ($this->bar_width / 2) - ($this->text_height / 2));
  397. imagestringup($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color);
  398. }
  399. else {
  400. if ($this->bool_all_negative) {
  401. //we must put values above 0 line
  402. $textVertPos = round($this->y_axis_y2 - $this->text_height - $this->axis_value_padding);
  403. }
  404. else {
  405. //mix of both pos and neg numbers
  406. //write value y axis bottom value (will be under bottom of grid even if x axis is floating due to
  407. $textVertPos = round($this->y_axis_y1 + ($this->text_height * 2 / 3) - $this->axis_value_padding);
  408. }
  409. //horizontal data keys
  410. $textHorizPos = round($xStart + ($this->bar_width / 2) - ((strlen($key) * $this->text_width) / 2));
  411. imagestring($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color);
  412. }
  413. }
  414. }
  415. $xStart += $this->bar_width + $this->space_width;
  416. }
  417. }
  418. }
  419. function finalizeColors() {
  420. if ($this->bool_gradient) {
  421. $num_set=count($this->multi_gradient_colors_1);
  422. //loop through set colors and add backing colors if necessary
  423. if ($num_set != $this->data_set_count) {
  424. $color_darken_decimal = (100 - $this->color_darken_factor) / 100;
  425. while ($num_set < $this->data_set_count) {
  426. $color_ref_1 = $this->multi_gradient_colors_1[$num_set - 1];
  427. $color_ref_2 = $this->multi_gradient_colors_2[$num_set - 1];
  428. $this->multi_gradient_colors_1[] = array(
  429. round($color_ref_1[0] * $color_darken_decimal),
  430. round($color_ref_1[1] * $color_darken_decimal),
  431. round($color_ref_1[2] * $color_darken_decimal));
  432. $this->multi_gradient_colors_2[]=array(
  433. round($color_ref_2[0] * $color_darken_decimal),
  434. round($color_ref_2[1] * $color_darken_decimal),
  435. round($color_ref_2[2] * $color_darken_decimal));
  436. $num_set++;
  437. }
  438. }
  439. while (count($this->multi_gradient_colors_1) > $this->data_set_count) {
  440. $temp = array_pop($this->multi_gradient_colors_1);
  441. }
  442. while (count($this->multi_gradient_colors_2) > $this->data_set_count) {
  443. $temp = array_pop($this->multi_gradient_colors_2);
  444. }
  445. $this->multi_gradient_colors_1 = array_reverse($this->multi_gradient_colors_1);
  446. $this->multi_gradient_colors_2 = array_reverse($this->multi_gradient_colors_2);
  447. }
  448. else if (!$this->bool_gradient) {
  449. $num_set = count($this->multi_bar_colors);
  450. if ($num_set == 0) {
  451. $this->multi_bar_colors[0] = $this->bar_color;
  452. $num_set = 1;
  453. }
  454. //loop through set colors and add backing colors if necessary
  455. while ($num_set < $this->data_set_count) {
  456. $color_ref = $this->multi_bar_colors[$num_set - 1];
  457. $color_parts = imagecolorsforindex($this->image, $color_ref);
  458. $color_darken_decimal = (100 - $this->color_darken_factor) / 100;
  459. $this->multi_bar_colors[$num_set] = imagecolorallocate($this->image,
  460. round($color_parts['red'] * $color_darken_decimal),
  461. round($color_parts['green'] * $color_darken_decimal),
  462. round($color_parts['blue'] * $color_darken_decimal));
  463. $num_set++;
  464. }
  465. while (count($this->multi_bar_colors) > $this->data_set_count) {
  466. $temp = array_pop($this->multi_bar_colors);
  467. }
  468. $this->multi_bar_colors = array_reverse($this->multi_bar_colors);
  469. }
  470. if ($this->bool_line) {
  471. if (!$this->bool_bars) {
  472. $num_set = count($this->line_color);
  473. if ($num_set == 0) {
  474. $this->line_color[0] = $this->line_color_default;
  475. $num_set = 1;
  476. }
  477. //only darken each data set's lines when no bars present
  478. while ($num_set < $this->data_set_count) {
  479. $color_ref = $this->line_color[$num_set - 1];
  480. $color_parts = imagecolorsforindex($this->image, $color_ref);
  481. $color_darken_decimal = (100 - $this->color_darken_factor) / 100;
  482. $this->line_color[$num_set] = imagecolorallocate($this->image,
  483. round($color_parts['red'] * $color_darken_decimal),
  484. round($color_parts['green'] * $color_darken_decimal),
  485. round($color_parts['blue'] * $color_darken_decimal));
  486. $num_set++;
  487. }
  488. }
  489. else {
  490. $num_set = count($this->line_color);
  491. while ($num_set < $this->data_set_count) {
  492. $this->line_color[$num_set] = $this->line_color_default;
  493. $num_set++;
  494. }
  495. }
  496. while (count($this->line_color) > $this->data_set_count) {
  497. $temp = array_pop($this->line_color);
  498. }
  499. $this->line_color = array_reverse($this->line_color);
  500. }
  501. }
  502. function drawGradientBar($x1, $y1, $x2, $y2, $colorArr1, $colorArr2, $data_set_num) {
  503. if (!isset($this->bool_gradient_colors_found[$data_set_num]) || $this->bool_gradient_colors_found[$data_set_num] == false) {
  504. $this->gradient_handicap[$data_set_num] = 0;
  505. $numLines = abs($x1 - $x2) + 1;
  506. while ($numLines * $this->data_set_count > $this->gradient_max) {
  507. //we have more lines than allowable colors
  508. //use handicap to record this
  509. $numLines /= 2;
  510. $this->gradient_handicap[$data_set_num]++;
  511. }
  512. $color1R = $colorArr1[0];
  513. $color1G = $colorArr1[1];
  514. $color1B = $colorArr1[2];
  515. $color2R = $colorArr2[0];
  516. $color2G = $colorArr2[1];
  517. $color2B = $colorArr2[2];
  518. $rScale = ($color1R-$color2R) / $numLines;
  519. $gScale = ($color1G-$color2G) / $numLines;
  520. $bScale = ($color1B-$color2B) / $numLines;
  521. $this->allocateGradientColors($color1R, $color1G, $color1B, $rScale, $gScale, $bScale, $numLines, $data_set_num);
  522. }
  523. $numLines = abs($x1 - $x2) + 1;
  524. if ($this->gradient_handicap[$data_set_num] > 0) {
  525. //if handicap is used, it will allow us to move through the array more slowly, depending on the set value
  526. $interval = $this->gradient_handicap[$data_set_num];
  527. for($i = 0; $i < $numLines; $i++) {
  528. $adjusted_index = ceil($i / pow(2, $interval)) - 1;
  529. if ($adjusted_index < 0) {
  530. $adjusted_index = 0;
  531. }
  532. imageline($this->image, $x1+$i, $y1, $x1 + $i, $y2, $this->gradient_color_array[$data_set_num][$adjusted_index]);
  533. }
  534. }
  535. else {
  536. //normal gradients with colors < $this->gradient_max
  537. for ($i = 0; $i < $numLines; $i++) {
  538. imageline($this->image, $x1+$i, $y1, $x1+$i, $y2, $this->gradient_color_array[$data_set_num][$i]);
  539. }
  540. }
  541. }
  542. function generateGrid() {
  543. if ($this->bool_user_data_range && $this->data_min >= 0) {
  544. $adjustment = $this->data_min * $this->unit_scale;
  545. }
  546. else {
  547. $adjustment = 0;
  548. }
  549. //calculate both values
  550. $this->calculateGridHoriz($adjustment);
  551. $this->calculateGridVert();
  552. //output values - separation from calc is b/c of an extension related need
  553. $this->generateGrids();
  554. $this->generateGoalLines($adjustment);
  555. }
  556. function calculateGridHoriz($adjustment = 0) {
  557. //determine horizontal grid lines
  558. $horizGridArray = array();
  559. if ($this->bool_user_data_range) {
  560. $min = $this->data_min;
  561. }
  562. else {
  563. $min = 0;
  564. }
  565. $horizGridArray[] = $min;
  566. //use our function to determine ideal y axis scale interval
  567. $intervalFromZero = $this->determineAxisMarkerScale($this->data_max, $this->data_min);
  568. //if we have positive values, add grid values to array
  569. //until we reach the max needed (we will go 1 over)
  570. $cur = $min;
  571. while ($cur < $this->data_max) {
  572. $cur += $intervalFromZero;
  573. $horizGridArray[] = $cur;
  574. }
  575. //if we have negative values, add grid values to array
  576. //until we reach the min needed (we will go 1 over)
  577. $cur = $min;
  578. while ($cur > $this->data_min) {
  579. $cur -= $intervalFromZero;
  580. $horizGridArray[] = $cur;
  581. }
  582. //sort needed b/c we will use last value later (max)
  583. sort($horizGridArray);
  584. $this->true_displayed_max_value = $horizGridArray[count($horizGridArray) - 1];
  585. $this->true_displayed_min_value = $horizGridArray[0];
  586. //loop through each horizontal line
  587. foreach ($horizGridArray as $value) {
  588. $yValue = round($this->x_axis_y1 - ($value * $this->unit_scale) + $adjustment);
  589. if ($this->bool_grid) {
  590. //imageline($this->image, $this->y_axis_x1, $yValue, $this->x_axis_x2 , $yValue, $this->grid_color);
  591. $this->horiz_grid_lines[] = array('x1' => $this->y_axis_x1, 'y1' => $yValue,
  592. 'x2' => $this->x_axis_x2, 'y2' => $yValue, 'color' => $this->grid_color);
  593. }
  594. //display value on y axis if desired using calc'd grid values
  595. if ($this->bool_y_axis_values) {
  596. $adjustedYValue = $yValue - ($this->text_height / 2);
  597. $adjustedXValue = $this->y_axis_x1 - ((strlen($value) + $this->data_additional_length) * $this->text_width) - $this->axis_value_padding;
  598. //add currency sign, formatting etc
  599. if ($this->data_format_array) {
  600. $value = $this->applyDataFormats($value);
  601. }
  602. if ($this->data_currency) {
  603. $value = $this->applyDataCurrency($value);
  604. }
  605. //imagestring($this->image, 2, $adjustedXValue, $adjustedYValue, $value, $this->y_axis_text_color);
  606. $this->horiz_grid_values[] = array('size' => 2, 'x' => $adjustedXValue, 'y' => $adjustedYValue,
  607. 'value' => $value, 'color' => $this->y_axis_text_color);
  608. }
  609. }
  610. if (!$this->bool_all_positive && !$this->bool_user_data_range) {
  611. //reset with better value based on grid min value calculations, not data min
  612. $this->y_axis_y1 = $this->x_axis_y1 - ($horizGridArray[0] * $this->unit_scale);
  613. }
  614. //reset with better value based on grid value calculations, not data min
  615. $this->y_axis_y2 = $yValue;
  616. }
  617. function calculateGridVert() {
  618. //determine vertical grid lines
  619. $vertGridArray = array();
  620. $vertGrids = $this->data_count + 1;
  621. $interval = $this->bar_width + $this->space_width;
  622. //assemble vert gridline array
  623. for ($i = 1; $i < $vertGrids; $i++) {
  624. $vertGridArray[] = $this->y_axis_x1 + ($interval * $i);
  625. }
  626. //loop through each vertical line
  627. if ($this->bool_grid) {
  628. $xValue = $this->y_axis_y1;
  629. foreach ($vertGridArray as $value) {
  630. //imageline($this->image, $value, $this->y_axis_y2, $value, $xValue , $this->grid_color);
  631. $this->vert_grid_lines[] = array('x1' => $value, 'y1' => $this->y_axis_y2,
  632. 'x2' => $value, 'y2' => $xValue, 'color' => $this->grid_color);
  633. }
  634. }
  635. }
  636. function imagelinedashed(&$image_handle, $x_axis_x1, $yLocation, $x_axis_x2 , $yLocation, $color) {
  637. $step = 3;
  638. for ($i = $x_axis_x1; $i < $x_axis_x2 -1; $i += ($step*2)) {
  639. imageline($this->image, $i, $yLocation, $i + $step - 1, $yLocation, $color);
  640. }
  641. }
  642. function generateGrids() {
  643. //loop through and display values
  644. foreach ($this->horiz_grid_lines as $line) {
  645. imageline($this->image, $line['x1'], $line['y1'], $line['x2'], $line['y2'] , $line['color']);
  646. }
  647. foreach ($this->vert_grid_lines as $line) {
  648. imageline($this->image, $line['x1'], $line['y1'], $line['x2'], $line['y2'] , $line['color']);
  649. }
  650. foreach ($this->horiz_grid_values as $value) {
  651. imagestring($this->image, $value['size'], $value['x'], $value['y'], $value['value'], $value['color']);
  652. }
  653. //not implemented in the base library, but used in extensions
  654. foreach ($this->vert_grid_values as $value) {
  655. imagestring($this->image, $value['size'], $value['x'], $value['y'], $value['value'], $value['color']);
  656. }
  657. }
  658. function generateGoalLines($adjustment = 0) {
  659. //draw goal lines if present (after grid) - doesn't get executed if array empty
  660. foreach ($this->goal_line_array as $goal_line_data) {
  661. $yLocation = $goal_line_data['yValue'];
  662. $style = $goal_line_data['style'];
  663. $color = $goal_line_data['color'] ? $goal_line_data['color'] : $this->goal_line_color;
  664. $yLocation = round(($this->x_axis_y1 - ($yLocation * $this->unit_scale) + $adjustment));
  665. if ($style == 'dashed') {
  666. $this->imagelinedashed($this->image, $this->x_axis_x1, $yLocation, $this->x_axis_x2 , $yLocation, $color);
  667. }
  668. else {
  669. //a solid line is the default if a style condition is not matched
  670. imageline($this->image, $this->x_axis_x1, $yLocation, $this->x_axis_x2 , $yLocation, $color);
  671. }
  672. }
  673. }
  674. function generateDataPoints() {
  675. foreach ($this->data_point_array as $pointArray) {
  676. imagefilledellipse($this->image, $pointArray[0], $pointArray[1], $this->data_point_width, $this->data_point_width, $this->data_point_color);
  677. }
  678. }
  679. function generateXAxis() {
  680. imageline($this->image, $this->x_axis_x1, $this->x_axis_y1, $this->x_axis_x2, $this->x_axis_y2, $this->x_axis_color);
  681. }
  682. function generateYAxis() {
  683. imageline($this->image, $this->y_axis_x1, $this->y_axis_y1, $this->y_axis_x2, $this->y_axis_y2, $this->y_axis_color);
  684. }
  685. function generateBackgound() {
  686. imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $this->background_color);
  687. }
  688. function generateTitle() {
  689. //spacing may have changed since earlier
  690. //use top margin or grid top y, whichever less
  691. $highestElement = ($this->top_margin < $this->y_axis_y2) ? $this->top_margin : $this->y_axis_y2;
  692. $textVertPos = ($highestElement / 2) - ($this->title_char_height / 2); //centered
  693. $titleLength = strlen($this->title_text);
  694. if ($this->bool_title_center) {
  695. $this->title_x = ($this->width / 2) - (($titleLength * $this->title_char_width) / 2);
  696. $this->title_y = $textVertPos;
  697. }
  698. else if ($this->bool_title_left) {
  699. $this->title_x = $this->y_axis_x1;
  700. $this->title_y = $textVertPos;
  701. }
  702. else if ($this->bool_title_right) {
  703. $this->title_x = $this->x_axis_x2 - ($titleLength * $this->title_char_width);
  704. $this->title_y = $textVertPos;
  705. }
  706. imagestring($this->image, 2, $this->title_x , $this->title_y , $this->title_text, $this->title_color);
  707. }
  708. function calcTopMargin() {
  709. if ($this->bool_title) {
  710. //include space for title, approx margin + 3*title height
  711. $this->top_margin = ($this->height * ($this->x_axis_default_percent / 100)) + $this->title_char_height;
  712. }
  713. else {
  714. //just use default spacing
  715. $this->top_margin = $this->height * ($this->x_axis_default_percent / 100);
  716. }
  717. }
  718. function calcRightMargin() {
  719. //just use default spacing
  720. $this->right_margin = $this->width * ($this->y_axis_default_percent / 100);
  721. }
  722. function calcCoords() {
  723. //calculate axis points, also used for other calculations
  724. $this->x_axis_x1 = $this->y_axis_margin;
  725. $this->x_axis_y1 = $this->height - $this->x_axis_margin;
  726. $this->x_axis_x2 = $this->width - $this->right_margin;
  727. $this->x_axis_y2 = $this->height - $this->x_axis_margin;
  728. $this->y_axis_x1 = $this->y_axis_margin;
  729. $this->y_axis_y1 = $this->height - $this->x_axis_margin;
  730. $this->y_axis_x2 = $this->y_axis_margin;
  731. $this->y_axis_y2 = $this->top_margin;
  732. }
  733. function determineAxisMarkerScale($max, $min, $axis = 'y') {
  734. switch ($axis) {
  735. case 'y' : $space = $this->height; break;
  736. case 'x' : $space = $this->width; break;
  737. }
  738. //for calclation, take range or max-0
  739. if ($this->bool_user_data_range) {
  740. $range = abs($max - $min);
  741. }
  742. else {
  743. $range = (abs($max-$min) > abs($max - 0)) ? abs($max - $min) : abs($max - 0);
  744. }
  745. //handle all zero data
  746. if ($range == 0) {
  747. $range = 10;
  748. }
  749. //multiply up to over 100, to better figure interval
  750. $count = 0;
  751. while (abs($range) < 100) {
  752. $range *= 10;
  753. $count++;
  754. }
  755. //divide into intervals based on height / preset constant - after rounding will be approx
  756. $divisor = round($space / $this->range_divisor_factor);
  757. $divided = round($range / $divisor);
  758. $result = $this->roundUpOneExtraDigit($divided);
  759. //if rounded up w/ extra digit is more than 200% of divided value,
  760. //round up to next sig number with same num of digits
  761. if ($result / $divided >= 2) {
  762. $result = $this->roundUpSameDigits($divided);
  763. }
  764. //divide back down, if needed
  765. for ($i = 0; $i < $count; $i++) {
  766. $result /= 10;
  767. }
  768. return $result;
  769. }
  770. function roundUpSameDigits($num) {
  771. $len = strlen($num);
  772. if (round($num, -1 * ($len - 1)) == $num) {
  773. //we already have a sig number
  774. return $num;
  775. }
  776. else {
  777. $firstDig = substr($num, 0,1);
  778. $secondDig = substr($num, 1,1);
  779. $rest = substr($num, 2);
  780. $secondDig = 5;
  781. $altered = $firstDig.$secondDig.$rest;
  782. //after reassembly, round up to next sig number, same # of digits
  783. return round((int)$altered, -1 * ($len - 1));
  784. }
  785. }
  786. function roundUpOneExtraDigit($num) {
  787. $len = strlen($num);
  788. $firstDig = substr($num, 0, 1);
  789. $rest = substr($num, 1);
  790. $firstDig = 5;
  791. $altered = $firstDig . $rest;
  792. //after reassembly, round up to next sig number, one extra # of digits
  793. return round((int)$altered, -1 * ($len));
  794. }
  795. function displayErrors() {
  796. if (count($this->error) > 0) {
  797. $lineHeight = 12;
  798. $errorColor = imagecolorallocate($this->image, 0, 0, 0);
  799. $errorBackColor = imagecolorallocate($this->image, 255, 204, 0);
  800. imagefilledrectangle($this->image, 0, 0, $this->width - 1, 2 * $lineHeight, $errorBackColor);
  801. imagestring($this->image, 3, 2, 0, "!!----- PHPGraphLib Error -----!!", $errorColor);
  802. foreach($this->error as $key => $errorText) {
  803. imagefilledrectangle($this->image, 0, ($key * $lineHeight) + $lineHeight, $this->width - 1, ($key * $lineHeight) + 2 * $lineHeight, $errorBackColor);
  804. imagestring($this->image, 2, 2, ($key * $lineHeight) + $lineHeight, "[". ($key + 1) . "] ". $errorText, $errorColor);
  805. }
  806. $errorOutlineColor = imagecolorallocate($this->image, 255, 0, 0);
  807. imagerectangle($this->image, 0, 0, $this->width-1,($key * $lineHeight) + 2 * $lineHeight, $errorOutlineColor);
  808. }
  809. }
  810. function addData($data, $data2 = '', $data3 = '', $data4 = '', $data5 = '') {
  811. if (is_array($data)){ $this->data_array[]=$data; }
  812. if (is_array($data2)){ $this->data_array[]=$data2; }
  813. if (is_array($data3)){ $this->data_array[]=$data3; }
  814. if (is_array($data4)){ $this->data_array[]=$data4; }
  815. if (is_array($data5)){ $this->data_array[]=$data5; }
  816. //assess data
  817. $min = $this->data_max_allowable;
  818. $max = $this->data_min_allowable;
  819. $this->data_count = 0;
  820. //get rid of bad data, find max, min
  821. foreach ($this->data_array as $data_set_num => $data_set) {
  822. foreach ($data_set as $key => $item) {
  823. if (!is_numeric($item)) {
  824. unset($this->data_array[$data_set_num][$key]);
  825. }
  826. else {
  827. if ($item < $min){ $min = $item; }
  828. if ($item > $max){ $max = $item; }
  829. }
  830. }
  831. //find the count of the dataset with the most data points
  832. $count = count($this->data_array[$data_set_num]);
  833. $count > $this->data_count ? ($this->data_count = $count) : NULL;
  834. }
  835. //number of valid data sets
  836. $this->data_set_count = count($this->data_array);
  837. if ($this->data_set_count == 0) {
  838. $this->error[] = "No valid datasets added in adddata() function.";
  839. }
  840. else {
  841. $this->calcMinMax($min, $max);
  842. }
  843. }
  844. function calcMinMax($min, $max) {
  845. $this->bool_data = true;
  846. //min and max for all datasets
  847. $this->data_min = $min;
  848. $this->data_max = $max;
  849. if ($this->data_min >= 0) {
  850. $this->bool_all_positive = true;
  851. }
  852. else if ($this->data_max<=0) {
  853. $this->bool_all_negative=true;
  854. }
  855. //setup fake max and min for all zero data
  856. if ($this->data_min >= 0 && $this->data_max == 0 ) {
  857. $this->data_min = 0;
  858. $this->data_max = 10;
  859. $this->all_zero_data = true;
  860. }
  861. }
  862. function setupXAxis($percent = '', $color = '') {
  863. if ($percent === false) {
  864. $this->bool_x_axis = false;
  865. }
  866. else {
  867. $this->bool_x_axis = true;
  868. }
  869. $this->bool_x_axis_setup = true;
  870. if (!empty($color) && $arr = $this->returnColorArray($color)) {
  871. $this->x_axis_color = imagecolorallocate($this->image, $arr[0], $arr[1], $arr[2]);
  872. }
  873. if (is_numeric($percent) && $percent>0) {
  874. $percent = $percent / 100;
  875. $this->x_axis_margin = round($this->height * $percent);
  876. }
  877. else {
  878. $percent = $this->x_axis_default_percent / 100;
  879. $this->x_axis_margin = round($this->height * $percent);
  880. }
  881. }
  882. function setupYAxis($percent = '', $color = '') {
  883. if ($percent === false) {
  884. $this->bool_y_axis = false;
  885. }
  886. else {
  887. $this->bool_y_axis = true;
  888. }
  889. $this->bool_y_axis_setup = true;
  890. if (!empty($color) && $arr = $this->returnColorArray($color)) {
  891. $this->y_axis_color = imagecolorallocate($this->image, $arr[0], $arr[1], $arr[2]);
  892. }
  893. if (is_numeric($percent) && $percent>0) {
  894. $this->y_axis_margin = round($this->width * ($percent / 100));
  895. }
  896. else {
  897. $percent = $this->y_axis_default_percent / 100;
  898. $this->y_axis_margin = round($this->width * $percent);
  899. }
  900. }
  901. function setRange($min, $max) {
  902. //because of deprecated use of this function as($max, $min)
  903. if ($min > $max) {
  904. $this->data_range_max = $min;
  905. $this->data_range_min = $max;
  906. }
  907. else {
  908. $this->data_range_max = $max;
  909. $this->data_range_min = $min;
  910. }
  911. $this->bool_user_data_range = true;
  912. }
  913. function setTitle($title) {
  914. if (!empty($title)) {
  915. $this->title_text = $title;
  916. $this->bool_title = true;
  917. }
  918. else{ $this->error[] = "String arg for setTitle() not specified properly."; }
  919. }
  920. function setTitleLocation($location) {
  921. $this->bool_title_left = false;
  922. $this->bool_title_right = false;
  923. $this->bool_title_center = false;
  924. switch (strtolower($location)) {
  925. case 'left': $this->bool_title_left = true; break;
  926. case 'right': $this->bool_title_right = true; break;
  927. case 'center': $this->bool_title_center = true; break;
  928. default: $this->error[] = "String arg for setTitleLocation() not specified properly.";
  929. }
  930. }
  931. function setBars($bool) {
  932. if (is_bool($bool)) { $this->bool_bars = $bool;}
  933. else { $this->error[] = "Boolean arg for setBars() not specified properly."; }
  934. }
  935. function setGrid($bool) {
  936. if (is_bool($bool)) { $this->bool_grid = $bool;}
  937. else { $this->error[] = "Boolean arg for setGrid() not specified properly."; }
  938. }
  939. function setXValues($bool) {
  940. if (is_bool($bool)) { $this->bool_x_axis_values = $bool; }
  941. else { $this->error[] = "Boolean arg for setXValues() not specified properly."; }
  942. }
  943. function setYValues($bool) {
  944. if (is_bool($bool)) { $this->bool_y_axis_values = $bool; }
  945. else { $this->error[] = "Boolean arg for setYValues() not specified properly."; }
  946. }
  947. function setXValuesHorizontal($bool) {
  948. if (is_bool($bool)) {
  949. ($bool) ? $this->bool_x_axis_values_vert = false : $this->bool_x_axis_values_vert = true;
  950. }
  951. else{ $this->error[] = "Boolean arg for setXValuesHorizontal() not specified properly."; }
  952. }
  953. function setXValuesVertical($bool) {
  954. if (is_bool($bool)) { $this->bool_x_axis_values_vert = $bool; }
  955. else { $this->error[] = "Boolean arg for setXValuesVertical() not specified properly."; }
  956. }
  957. function setBarOutline($bool) {
  958. if (is_bool($bool)) { $this->bool_bar_outline = $bool; }
  959. else { $this->error[] = "Boolean arg for setBarOutline() not specified properly."; }
  960. }
  961. function setDataPoints($bool) {
  962. if (is_bool($bool)) { $this->bool_data_points = $bool; }
  963. else { $this->error[] = "Boolean arg for setDataPoints() not specified properly."; }
  964. }
  965. function setDataPointSize($size) {
  966. if (is_numeric($size)) { $this->data_point_width = $size; }
  967. else { $this->error[] = "Data point size in setDataPointSize() not specified properly."; }
  968. }
  969. function setDataValues($bool) {
  970. if (is_bool($bool)) { $this->bool_data_values = $bool; }
  971. else{ $this->error[] = "Boolean arg for setDataValues() not specified properly."; }
  972. }
  973. function setDataCurrency($currency_type = 'dollar') {
  974. switch (strtolower($currency_type)) {
  975. case 'dollar': $this->data_currency = '$'; break;
  976. case 'yen': $this->data_currency = '¥'; break;
  977. case 'pound': $this->data_currency = '£'; break;
  978. case 'lira': $this->data_currency = '£'; break;
  979. /* Euro doesn't display properly...
  980. case 'euro': $this->data_currency = '€'; break; */
  981. /* Franc doesn't display properly
  982. case 'franc': $this->data_currency = '?'; break; */
  983. default: $this->data_currency = $currency_type; break;
  984. }
  985. $this->data_additional_length += strlen($this->data_currency);
  986. }
  987. function applyDataCurrency($input) {
  988. return $this->data_currency . $input;
  989. }
  990. function setDataFormat($format) {
  991. //setup structure for future additional data formats - specify callback functions
  992. switch ($format) {
  993. case 'comma':
  994. $this->data_format_array[] = 'formatDataAsComma';
  995. $this->data_additional_length += floor(strlen($this->data_max) / 3);
  996. break;
  997. case 'percent':
  998. $this->data_format_array[] = 'formatDataAsPercent';
  999. $this->data_additional_length++;
  1000. break;
  1001. case 'degrees':
  1002. $this->data_format_array[] = 'formatDataAsDegrees';
  1003. $this->data_additional_length++;
  1004. break;
  1005. default:
  1006. $this->data_format_array[] = 'formatDataAsGeneric';
  1007. $this->data_format_generic = $format;
  1008. $this->data_additional_length += strlen($format);
  1009. break;
  1010. }
  1011. }
  1012. function applyDataFormats($input) {
  1013. //comma formatting must be done first
  1014. if ($pos = array_search('formatDataAsComma', $this->data_format_array)) {
  1015. unset($this->data_format_array[$pos]);
  1016. array_unshift($this->data_format_array, 'formatDataAsComma');
  1017. }
  1018. //loop through each formatting function
  1019. foreach ($this->data_format_array as $format_type_callback) {
  1020. eval('$input=$this->' . $format_type_callback . '($input);');
  1021. }
  1022. return $input;
  1023. }
  1024. function formatDataAsComma($input) {
  1025. //check for negative sign
  1026. $sign_part = '';
  1027. if (substr($input, 0, 1) == '-') {
  1028. $input = substr($input, 1);
  1029. $sign_part = '-';
  1030. }
  1031. //handle decimals
  1032. $decimal_part = '';
  1033. if (($pos = strpos($input, '.')) !== false) {
  1034. $decimal_part = substr($input, $pos);
  1035. $input = substr($input, 0, $pos);
  1036. }
  1037. //turn data into format 12,234...
  1038. $parts = '';
  1039. while (strlen($input)>3) {
  1040. $parts = ',' . substr($input, -3) . $parts;
  1041. $input = substr($input, 0, strlen($input) - 3);
  1042. }
  1043. $input = $sign_part . $currency_part . $input . $parts . $decimal_part;
  1044. return $input;
  1045. }
  1046. function formatDataAsPercent($input) {
  1047. return $input . '%';
  1048. }
  1049. function formatDataAsDegrees($input) {
  1050. return $input . '°';
  1051. }
  1052. function formatDataAsGeneric($input) {
  1053. return $input . $this->data_format_generic;
  1054. }
  1055. function setLine($bool) {
  1056. if (is_bool($bool)) { $this->bool_line = $bool; }
  1057. else { $this->error[] = "Boolean arg for setLine() not specified properly."; }
  1058. }
  1059. function setGoalLine($yValue, $color = NULL, $style = 'solid') {
  1060. if (is_numeric($yValue)) {
  1061. if ($color) {
  1062. $this->setGenericColor($color, '$this->goal_line_custom_color', "Goal line color not specified properly.");
  1063. $color = $this->goal_line_custom_color;
  1064. }
  1065. $this->goal_line_array[] = array('yValue' => $yValue, 'color' => $color, 'style' => $style);
  1066. }
  1067. else {
  1068. $this->error[] = "Goal line Y axis value not specified properly.";
  1069. }
  1070. }
  1071. function allocateColors() {
  1072. $this->background_color = imagecolorallocate($this->image, 255, 255, 255);
  1073. $this->grid_color = imagecolorallocate($this->image, 220, 220, 220);
  1074. $this->bar_color = imagecolorallocate($this->image, 200, 200, 200);
  1075. $this->line_color_default = imagecolorallocate($this->image, 100, 100, 100);
  1076. $this->x_axis_text_color = $this->line_color_default;
  1077. $this->y_axis_text_color = $this->line_color_default;
  1078. $this->data_value_color = $this->line_color_default;
  1079. $this->title_color = imagecolorallocate($this->image, 0, 0, 0);
  1080. $this->outline_color = $this->title_color;
  1081. $this->data_point_color = $this->title_color;
  1082. $this->x_axis_color = $this->title_color;
  1083. $this->y_axis_color = $this->title_color;
  1084. $this->goal_line_color = $this->title_color;
  1085. //New Legend Colors
  1086. $this->legend_outline_color = $this->grid_color;
  1087. $this->legend_color = $this->background_color;
  1088. $this->legend_text_color = $this->line_color_default;
  1089. $this->legend_swatch_outline_color = $this->line_color_default;
  1090. }
  1091. function returnColorArray($color) {
  1092. //check to see if numeric color passed through in form '128,128,128'
  1093. if (strpos($color,',') !== false) {
  1094. return explode(',', $color);
  1095. }
  1096. //check to see if in hex format
  1097. else if (substr($color, 0, 1) == '#') {
  1098. if (strlen($color) == 7) {
  1099. $hex1 = hexdec(substr($color, 1, 2));
  1100. $hex2 = hexdec(substr($color, 3, 2));
  1101. $hex3 = hexdec(substr($color, 5, 2));
  1102. return array($hex1, $hex2, $hex3);
  1103. }
  1104. else if (strlen($color) == 4) {
  1105. $hex1 = hexdec(substr($color, 1, 1) . substr($color, 1, 1));
  1106. $hex2 = hexdec(substr($color, 2, 1) . substr($color, 2, 1));
  1107. $hex3 = hexdec(substr($color, 3, 1) . substr($color, 3, 1));
  1108. return array($hex1, $hex2, $hex3);
  1109. }
  1110. }
  1111. switch (strtolower($color)) {
  1112. //NAMED COLORS BASED ON W3C's RECOMMENDED HTML COLORS
  1113. case 'black': return array(0,0,0); break;
  1114. case 'silver': return array(192,192,192); break;
  1115. case 'gray': return array(128,128,128); break;
  1116. case 'white': return array(255,255,255); break;
  1117. case 'maroon': return array(128,0,0); break;
  1118. case 'red': return array(255,0,0); break;
  1119. case 'purple': return array(128,0,128); break;
  1120. case 'fuscia': return array(255,0,255); break;
  1121. case 'green': return array(0,128,0); break;
  1122. case 'lime': return array(0,255,0); break;
  1123. case 'olive': return array(128,128,0); break;
  1124. case 'yellow': return array(255,255,0); break;
  1125. case 'navy': return array(0,0,128); break;
  1126. case 'blue': return array(0,0,255); break;
  1127. case 'teal': return array(0,128,128); break;
  1128. case 'aqua': return array(0,255,255); break;
  1129. }
  1130. $this->error[] = "Color name \"$color\" not recogized.";
  1131. return false;
  1132. }
  1133. function allocateGradientColors($color1R, $color1G, $color1B, $rScale, $gScale, $bScale, $num, $data_set_num) {
  1134. //caluclate the colors used in our gradient and store them in array
  1135. $this->gradient_color_array[$data_set_num] = array();
  1136. for ($i = 0; $i <= $num + 1; $i++) {
  1137. $this->gradient_color_array[$data_set_num][$i] = imagecolorallocate($this->image, $color1R - ($rScale * $i), $color1G - ($gScale * $i), $color1B - ($bScale * $i));
  1138. }
  1139. $this->bool_gradient_colors_found[$data_set_num] = true;
  1140. }
  1141. function setGenericColor($inputColor, $var, $errorMsg) {
  1142. //can be used for most color setting options
  1143. if (!empty($inputColor) && ($arr = $this->returnColorArray($inputColor))) {
  1144. eval($var . ' = imagecolorallocate($this->image, $arr[0], $arr[1], $arr[2]);');
  1145. return true;
  1146. }
  1147. else {
  1148. $this->error[] = $errorMsg;
  1149. return false;
  1150. }
  1151. }
  1152. function setBackgroundColor($color) {
  1153. if ($this->setGenericColor($color, '$this->background_color', "Background color not specified properly.")) {
  1154. $this->bool_background = true;
  1155. }
  1156. }
  1157. function setTitleColor($color) {
  1158. $this->setGenericColor($color, '$this->title_color', "Title color not specified properly.");
  1159. }
  1160. function setTextColor($color) {
  1161. $this->setGenericColor($color, '$this->x_axis_text_color', "X axis text color not specified properly.");
  1162. $this->setGenericColor($color, '$this->y_axis_text_color', "Y axis Text color not specified properly.");
  1163. }
  1164. function setXAxisTextColor($color) {
  1165. $this->setGenericColor($color, '$this->x_axis_text_color', "X axis text color not specified properly.");
  1166. }
  1167. function setYAxisTextColor($color) {
  1168. $this->setGenericColor($color, '$this->y_axis_text_color', "Y axis Text color not specified properly.");
  1169. }
  1170. function setBarColor($color, $color2 = '', $color3 = '', $color4 = '', $color5 = '') {
  1171. $this->setGenericColor($color, '$this->multi_bar_colors[]', "Bar color not specified properly.");
  1172. if (!empty($color2)) {
  1173. $this->setGenericColor($color2, '$this->multi_bar_colors[]', "Bar color 2 not specified properly.");
  1174. }
  1175. if (!empty($color3)) {
  1176. $this->setGenericColor($color3, '$this->multi_bar_colors[]', "Bar color 3 not specified properly.");
  1177. }
  1178. if (!empty($color4)) {
  1179. $this->setGenericColor($color4, '$this->multi_bar_colors[]', "Bar color 4 not specified properly.");
  1180. }
  1181. if (!empty($color5)) {
  1182. $this->setGenericColor($color5, '$this->multi_bar_colors[]', "Bar color 5 not specified properly.");
  1183. }
  1184. }
  1185. function setGridColor($color) {
  1186. $this->setGenericColor($color, '$this->grid_color', "Grid color not specified properly.");
  1187. }
  1188. function setBarOutlineColor($color) {
  1189. $this->setGenericColor($color, '$this->outline_color', "Bar outline color not specified properly.");
  1190. }
  1191. function setDataPointColor($color) {
  1192. $this->setGenericColor($color, '$this->data_point_color', "Data point color not specified properly.");
  1193. }
  1194. function setDataValueColor($color) {
  1195. $this->setGenericColor($color, '$this->data_value_color', "Data value color not specified properly.");
  1196. }
  1197. function setLineColor($color, $color2 = '', $color3 = '', $color4 = '', $color5 = '') {
  1198. $this->setGenericColor($color, '$this->line_color[]', "Line color not specified properly.");
  1199. if (!empty($color2)) {
  1200. $this->setGenericColor($color2, '$this->line_color[]', "Line color 2 not specified properly.");
  1201. }
  1202. if (!empty($color3)) {
  1203. $this->setGenericColor($color3, '$this->line_color[]', "Line color 3 not specified properly.");
  1204. }
  1205. if (!empty($color4)) {
  1206. $this->setGenericColor($color4, '$this->line_color[]', "Line color 4 not specified properly.");
  1207. }
  1208. if (!empty($color5)) {
  1209. $this->setGenericColor($color5, '$this->line_color[]', "Line color 5 not specified properly.");
  1210. }
  1211. }
  1212. /* @deprecated - setGoalLineColor() replaced with 2nd parameter of setGoalLine() */
  1213. function setGoalLineColor($color) {
  1214. $this->setGenericColor($color, '$this->goal_line_color', "Goal line color not specified properly.");
  1215. }
  1216. function setGradient($color1, $color2, $color3='', $color4='', $color5='', $color6='', $color7='', $color8='', $color9='', $color10='') {
  1217. if (!empty($color1) && !empty($color2) && ($arr1 = $this->returnColorArray($color1)) && ($arr2 = $this->returnColorArray($color2))) {
  1218. $this->bool_gradient=true;
  1219. $this->multi_gradient_colors_1[] = $arr1;
  1220. $this->multi_gradient_colors_2[] = $arr2;
  1221. }
  1222. else {
  1223. $this->error[] = "Gradient color(s) not specified properly.";
  1224. }
  1225. //Optional gradients
  1226. if (!empty($color3) && !empty($color4) && ($arr1=$this->returnColorArray($color3)) && ($arr2 = $this->returnColorArray($color4))) {
  1227. $this->bool_gradient=true;
  1228. $this->multi_gradient_colors_1[]=$arr1;
  1229. $this->multi_gradient_colors_2[]=$arr2;
  1230. }
  1231. if (!empty($color5) && !empty($color6) && ($arr1 = $this->returnColorArray($color5)) && ($arr2 = $this->returnColorArray($color6))) {
  1232. $this->bool_gradient = true;
  1233. $this->multi_gradient_colors_1[] = $arr1;
  1234. $this->multi_gradient_colors_2[] = $arr2;
  1235. }
  1236. if (!empty($color7) && !empty($color8) && ($arr1 = $this->returnColorArray($color7)) && ($arr2 = $this->returnColorArray($color8))) {
  1237. $this->bool_gradient = true;
  1238. $this->multi_gradient_colors_1[] = $arr1;
  1239. $this->multi_gradient_colors_2[] = $arr2;
  1240. }
  1241. if (!empty($color9) && !empty($color10) && ($arr1 = $this->returnColorArray($color9)) && ($arr2 = $this->returnColorArray($color10))) {
  1242. $this->bool_gradient = true;
  1243. $this->multi_gradient_colors_1[] = $arr1;
  1244. $this->multi_gradient_colors_2[] = $arr2;
  1245. }
  1246. }
  1247. //Legend Related Functions
  1248. function setLegend($bool) {
  1249. if (is_bool($bool)) { $this->bool_legend = $bool;}
  1250. else { $this->error[] = "Boolean arg for setLegend() not specified properly."; }
  1251. }
  1252. function setLegendColor($color) {
  1253. $this->setGenericColor($color, '$this->legend_color', "Legend color not specified properly.");
  1254. }
  1255. function setLegendTextColor($color) {
  1256. $this->setGenericColor($color, '$this->legend_text_color', "Legend text color not specified properly.");
  1257. }
  1258. function setLegendOutlineColor($color) {
  1259. $this->setGenericColor($color, '$this->legend_outline_color', "Legend outline color not specified properly.");
  1260. }
  1261. function setSwatchOutlineColor($color) {
  1262. $this->setGenericColor($color, '$this->legend_swatch_outline_color', "Swatch outline color not specified properly.");
  1263. }
  1264. function setLegendTitle($title, $title2 = '', $title3 = '', $title4 = '', $title5 = '') {
  1265. if (!empty($title)) {
  1266. $len = strlen($title);
  1267. if ($len > $this->legend_max_chars){
  1268. $title = substr($title, 0, $this->legend_max_chars);
  1269. $this->legend_total_chars[] = $this->legend_max_chars;
  1270. }
  1271. else {$this->legend_total_chars[] = $len;}
  1272. $this->legend_titles[] = $title;
  1273. }
  1274. else {$this->error[] = "String arg 1 for setLegendTitles() not specified properly."; }
  1275. if (!empty($title2)) {
  1276. $len = strlen($title2);
  1277. if ($len > $this->legend_max_chars) {
  1278. $title2 = substr($title2, 0, $this->legend_max_chars);
  1279. $this->legend_total_chars[] = $this->legend_max_chars;
  1280. }
  1281. else {$this->legend_total_chars[] = $len;}
  1282. $this->legend_titles[] = $title2;
  1283. }
  1284. if (!empty($title3)) {
  1285. $len = strlen($title3);
  1286. if ($len > $this->legend_max_chars){
  1287. $title3 = substr($title3, 0, $this->legend_max_chars);
  1288. $this->legend_total_chars[] = $this->legend_max_chars;
  1289. }
  1290. else {$this->legend_total_chars[] = $len;}
  1291. $this->legend_titles[] = $title3;
  1292. }
  1293. if (!empty($title4)) {
  1294. $len = strlen($title4);
  1295. if ($len > $this->legend_max_chars){
  1296. $title4 = substr($title4, 0, $this->legend_max_chars);
  1297. $this->legend_total_chars[] = $this->legend_max_chars;
  1298. }
  1299. else {$this->legend_total_chars[] = $len;}
  1300. $this->legend_titles[] = $title4;
  1301. }
  1302. if (!empty($title5)) {
  1303. $len = strlen($title5);
  1304. if ($len > $this->legend_max_chars){
  1305. $title5 = substr($title5, 0, $this->legend_max_chars);
  1306. $this->legend_total_chars[] = $this->legend_max_chars;
  1307. }
  1308. else {$this->legend_total_chars[] = $len;}
  1309. $this->legend_titles[] = $title5;
  1310. }
  1311. }
  1312. function generateLegend() {
  1313. $swatchToTextOffset = ($this->legend_text_height - 6) / 2;
  1314. $swatchSize = $this->legend_text_height - (2 * $swatchToTextOffset);
  1315. //calc height / width based on # of data sets
  1316. $this->legend_height = $this->legend_text_height + (2 * $this->legend_padding);
  1317. $totalChars = 0;
  1318. for ($i = 0; $i < $this->data_set_count; $i++) {
  1319. //could have more titles than data sets - check for this
  1320. if (isset($this->legend_total_chars[$i])){ $totalChars += $this->legend_total_chars[$i]; }
  1321. }
  1322. $this->legend_width = $totalChars * $this->legend_text_width + ($this->legend_padding * 2.2) +
  1323. ($this->data_set_count * ($swatchSize + ($this->legend_padding * 2)));
  1324. $this->legend_x = $this->x_axis_x2 - $this->legend_width;
  1325. $highestElement = ($this->top_margin < $this->y_axis_y2) ? $this->top_margin : $this->y_axis_y2;
  1326. $this->legend_y = ($highestElement / 2) - ($this->legend_height / 2); //centered
  1327. //background
  1328. imagefilledrectangle($this->image, $this->legend_x, $this->legend_y, $this->legend_x+$this->legend_width,
  1329. $this->legend_y + $this->legend_height, $this->legend_color);
  1330. //border
  1331. imagerectangle($this->image, $this->legend_x, $this->legend_y, $this->legend_x+$this->legend_width,
  1332. $this->legend_y+$this->legend_height, $this->legend_outline_color);
  1333. $length_covered = 0;
  1334. for($i = 0; $i < $this->data_set_count; $i++) {
  1335. $data_label = '';
  1336. if (isset($this->legend_titles[$i])) {
  1337. $data_label = $this->legend_titles[$i];
  1338. }
  1339. $yValue = $this->legend_y + $this->legend_padding;
  1340. $xValue = $this->legend_x + $this->legend_padding + ($length_covered * $this->legend_text_width) + ($i * 4 * $this->legend_padding);
  1341. $length_covered += strlen($data_label);
  1342. //draw color boxes
  1343. if ($this->bool_bars) {
  1344. if ($this->bool_gradient) {
  1345. $color = $this->gradient_color_array[$this->data_set_count - $i - 1][0];
  1346. }
  1347. else{
  1348. $color = $this->multi_bar_colors[$this->data_set_count - $i - 1];
  1349. }
  1350. }
  1351. else if ($this->bool_line&&!$this->bool_bars)
  1352. {
  1353. $color = $this->line_color[$this->data_set_count - $i - 1];
  1354. }
  1355. imagefilledrectangle($this->image, $xValue, $yValue+$swatchToTextOffset, $xValue + $swatchSize, $yValue + $swatchToTextOffset + $swatchSize, $color);
  1356. imagerectangle($this->image, $xValue, $yValue + $swatchToTextOffset, $xValue + $swatchSize, $yValue + $swatchToTextOffset + $swatchSize, $this->legend_swatch_outline_color);
  1357. imagestring($this->image, 2, $xValue + (2 * $this->legend_padding + 2), $yValue, $data_label, $this->legend_text_color);
  1358. }
  1359. }
  1360. }