forked from jeroenst/phpgraphlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpgraphlib.php
1435 lines (1411 loc) · 54.8 KB
/
phpgraphlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
PHPGraphLib Graphing Library
The first version PHPGraphLib was written in 2007 by Elliott Brueggeman to
deliver PHP generated graphs quickly and easily. It has grown in both features
and maturity since its inception, but remains PHP 4.04+ compatible. Originally
available only for paid commerial use, PHPGraphLib was open-sourced in 2013
under the MIT License. Please visit http://www.ebrueggeman.com/phpgraphlib
for more information.
---
The MIT License (MIT)
Copyright (c) 2013 Elliott Brueggeman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class PHPGraphLib {
//---------------user changeable defaults----------------/
var $height = 300;
var $width = 400;
var $data_max_allowable = 9999999999999999;
var $data_min_allowable = -9999999999999999;
var $data_set_count = 0;
//set to actual font heights and widths used
var $title_char_width = 6;
var $title_char_height = 12;
var $text_width = 6;
var $text_height = 12;
var $data_value_text_width = 6;
var $data_value_text_height = 12;
//padding between axis and value displayed
var $axis_value_padding = 5;
//space b/t top of bar or center of point and data value
var $data_value_padding = 5;
//default margin % of width / height
var $x_axis_default_percent = 12;
var $y_axis_default_percent = 8;
//data point diameter in px
var $data_point_width = 6;
//user changeable default booleans (show element by default?)
var $bool_bar_outline = true;
var $bool_x_axis = true;
var $bool_y_axis = true;
var $bool_x_axis_values = true;
var $bool_y_axis_values = true;
var $bool_grid = true;
var $bool_line = false;
var $bool_data_values = false;
var $bool_x_axis_values_vert = true;
var $bool_data_points = false;
var $bool_title_left = false;
var $bool_title_right = false;
var $bool_title_center = true;
var $x_axis_value_interval = false;
//----------internal variables (do not change)------------/
var $image;
var $output_file;
var $error;
var $bool_x_axis_setup = false;
var $bool_y_axis_setup = false;
var $bool_data = false;
var $bool_bars_generate = true;
var $bool_bars = true;
var $bool_background = false;
var $bool_title = false;
var $bool_all_negative = false;
var $bool_all_positive = false;
var $bool_gradient = false;
var $bool_user_data_range = false;
var $all_zero_data = false;
var $bool_gradient_colors_found; //init as array
var $x_axis_value_interval_counter = 0;
//color vars
var $background_color;
var $grid_color;
var $bar_color;
var $outline_color;
var $x_axis_text_color;
var $y_axis_text_color;
var $title_color;
var $x_axis_color;
var $y_axis_color;
var $data_point_color;
var $data_value_color;
var $line_color;
var $line_color_default;
var $goal_line_color;
var $goal_line_color_custom;
//gradient colors stored as arrays, not allocated color
var $gradient_color_1;
var $gradient_color_2;
var $gradient_color_array;
var $gradient_max = 200;
var $gradient_handicap;
//data vars
var $data_array;
var $data_count;
var $data_min;
var $data_max;
var $true_displayed_max_value;
var $true_displayed_min_value;
var $data_currency;
var $data_format_array;
var $data_additional_length = 0;
var $data_format_generic;
//bar vars / scale
var $bar_spaces;
var $bar_width;
var $space_width;
var $unit_scale;
var $goal_line_array;
var $horiz_grid_lines;
var $vert_grid_lines;
var $horiz_grid_values;
var $vert_grid_values;
//text / font
var $title_x;
var $title_y;
//axis points
var $x_axis_x1;
var $x_axis_y1;
var $x_axis_x2;
var $x_axis_y2;
var $y_axis_x1;
var $y_axis_y1;
var $y_axis_x2;
var $y_axis_y2;
var $x_axis_margin; //aka bottom margin
var $y_axis_margin; //aka left margin
var $data_range_max;
var $data_range_min;
var $top_margin = 0;
var $right_margin = 0;
var $range_divisor_factor = 25; //controls auto-adjusting grid interval
var $data_point_array;
//multiple dataset variables
var $bool_multi_offset = true;
var $multi_offset_two = 24; //percent of bar width
var $multi_offset_three = 15; //percent of bar width
var $multi_offset_four = 10; //percent of bar width
var $multi_offset_five = 9; //percent of bar width
var $multi_gradient_colors_1;
var $multi_gradient_colors_2;
var $multi_bar_colors;
var $color_darken_factor = 30;//percent decrease
//legend variables
var $bool_legend = false;
var $legend_text_width = 6; //in px...
var $legend_max_chars = 15;
var $legend_total_chars;
var $legend_text_height = 12;
var $legend_padding=4; //padding inside legend box
var $legend_width;
var $legend_height;
var $legend_x;
var $legend_y;
var $legend_text_color;
var $legend_outline_color;
var $legend_swatch_outline_color;
var $legend_color;
var $legend_titles = array();
function PHPGraphLib($width = '', $height = '', $output_file = NULL) {
if (!empty($width) && !empty($height)) {
$this->width = $width;
$this->height = $height;
}
$this->output_file = $output_file;
$this->initialize();
$this->allocateColors(); //sets default colors
}
function initialize() {
//header must be sent before any html or blank space output
if (!$this->output_file){ header("Content-type: image/png"); }
$this->image = @imagecreate($this->width, $this->height)
or die("Cannot Initialize new GD image stream - Check your PHP setup");
$this->data_point_array = array();
$this->goal_line_array = array();
$this->multi_bar_colors = array();
$this->multi_gradient_colors_1 = array();
$this->multi_gradient_colors_2 = array();
//these values are now arrays
$this->gradient_handicap = array();
$this->bool_gradient_colors_found = array();
$this->legend_total_chars = array();
$this->line_color = array();
$this->data_format_array = array();
$this->horiz_grid_lines = array();
$this->vert_grid_lines = array();
$this->horiz_grid_values = array();
$this->vert_grid_values = array();
}
function createGraph() {
//main class method - called last
//setup axis if not already setup by user
if ($this->bool_data){
if (!$this->bool_x_axis_setup){ $this->setupXAxis(); }
if (!$this->bool_y_axis_setup){ $this->setupYAxis(); }
//calculations
$this->calcTopMargin();
$this->calcRightMargin();
$this->calcCoords();
$this->setupData();
//start creating actual image elements
if ($this->bool_background){ $this->generateBackgound(); }
//always gen grid values, even if not displayed
$this->generateGrid();
if ($this->bool_bars_generate){ $this->generateBars(); }
if ($this->bool_data_points){ $this->generateDataPoints(); }
if ($this->bool_legend){ $this->generateLegend(); }
if ($this->bool_title){ $this->generateTitle(); }
if ($this->bool_x_axis){ $this->generateXAxis(); }
if ($this->bool_y_axis){ $this->generateYAxis(); }
}
else {
$this->error[] = "No valid data added to graph. Add data with the addData() function.";
}
//display errors
$this->displayErrors();
//output to browser
if ($this->output_file) {
imagepng($this->image, $this->output_file);
}
else {
imagepng($this->image);
}
imagedestroy($this->image);
}
function setupData() {
$this->bar_spaces = ($this->data_count * 2) + 1;
$unit_width = ($this->width - $this->y_axis_margin - $this->right_margin) / (($this->data_count * 2) + $this->data_count);
if ($unit_width < 1) {
//error units too small, too many data points or not large enough graph
$this->bool_bars_generate = false;
$this->error[] = "Graph too small or too many data points.";
}
else {
//default space between bars is 1/2 the width of the bar
//find bar and space widths. bar = 2 units, space = 1 unit
$this->bar_width= 2 * $unit_width;
$this->space_width = $unit_width;
//now calculate height (scale) units
$availVertSpace = $this->height - $this->x_axis_margin - $this->top_margin;
if ($availVertSpace<1) {
$this->bool_bars_generate=false;
$this->error[] = "Graph height not tall enough.";
//error scale units too small, x axis margin too big or graph height not tall enough
}
else {
if ($this->bool_user_data_range) {
//if all zero data, set fake max and min to range boundaries
if ($this->all_zero_data) {
if ($this->data_range_min > $this->data_min) { $this->data_min = $this->data_range_min; }
if ($this->data_range_max < $this->data_max) { $this->data_max = $this->data_range_max; }
}
$graphTopScale = $this->data_range_max;
$graphBottomScale = $this->data_range_min;
$graphScaleRange = $graphTopScale - $graphBottomScale;
$this->unit_scale = $availVertSpace / $graphScaleRange;
$this->data_max = $this->data_range_max;
$this->data_min = $this->data_range_min;
if ($this->data_min < 0) {
$this->x_axis_y1 -= round($this->unit_scale * abs($this->data_min));
$this->x_axis_y2 -= round($this->unit_scale * abs($this->data_min));
}
//all data identical
if ($graphScaleRange == 0) {
$graphScaleRange = 100;
}
}
else {
//start at y value 0 or data min, whichever is less
$graphBottomScale = ($this->data_min<0) ? $this->data_min : 0;
$graphTopScale = ($this->data_max<0) ? 0 : $this->data_max ;
$graphScaleRange = $graphTopScale - $graphBottomScale;
//all data identical
if ($graphScaleRange == 0) {
$graphScaleRange = 100;
}
$this->unit_scale = $availVertSpace / $graphScaleRange;
//now adjust x axis in y value if negative values
if ($this->data_min < 0) {
$this->x_axis_y1 -= round($this->unit_scale * abs($this->data_min));
$this->x_axis_y2 -= round($this->unit_scale * abs($this->data_min));
}
}
$this->bool_bars_generate = true;
}
}
}
//always port changes to generatebars() to extensions
function generateBars() {
$this->finalizeColors();
$barCount = 0;
$adjustment = 0;
if ($this->bool_user_data_range && $this->data_min >= 0) {
$adjustment = $this->data_min * $this->unit_scale;
}
//reverse array to order data sets in order of priority
$this->data_array = array_reverse($this->data_array);
$dataset_offset=0;
if ($this->bool_multi_offset) {
//set different offsets based on number of data sets
switch ($this->data_set_count) {
case 2: $dataset_offset = $this->bar_width * ($this->multi_offset_two / 100); break;
case 3: $dataset_offset = $this->bar_width * ($this->multi_offset_three / 100); break;
case 4: $dataset_offset = $this->bar_width * ($this->multi_offset_four / 100); break;
case 5: $dataset_offset = $this->bar_width * ($this->multi_offset_five / 100); break;
}
}
foreach ($this->data_array as $data_set_num => $data_set) {
$lineX2 = NULL;
$xStart = $this->y_axis_x1 + ($this->space_width / 2);
foreach ($data_set as $key => $item) {
$hideBarOutline = false;
$x1=round($xStart + ($dataset_offset * $data_set_num));
$x2=round(($xStart + $this->bar_width) + ($dataset_offset * $data_set_num));
$y1=round($this->x_axis_y1 - ($item * $this->unit_scale) + $adjustment);
$y2=round($this->x_axis_y1);
//if we are using a user specified data range, need to limit what's displayed
if ($this->bool_user_data_range) {
if ($item <= $this->data_range_min) {
//don't display, we are out of our allowed display range!
$y1 = $y2;
$hideBarOutline = true;
}
else if ($item >= $this->data_range_max) {
//display, but cut off display above range max
$y1 = $this->x_axis_y1 - ($this->true_displayed_max_value * $this->unit_scale) + $adjustment;
}
}
//draw bar
if ($this->bool_bars) {
if ($this->bool_gradient) {
//draw gradient if desired
$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);
}
else {
imagefilledrectangle($this->image, $x1, $y1,$x2, $y2, $this->multi_bar_colors[$data_set_num]);
}
//draw bar outline
if ($this->bool_bar_outline && !$hideBarOutline) {
imagerectangle($this->image, $x1, $y2, $x2, $y1, $this->outline_color);
}
}
// draw line
if ($this->bool_line) {
$lineX1 = $x1 + ($this->bar_width / 2); //MIDPOINT OF BARS, IF SHOWN
$lineY1 = $y1;
if (isset($lineX2)) {
imageline($this->image, $lineX2, $lineY2, $lineX1, $lineY1, $this->line_color[$data_set_num]);
$lineX2 = $lineX1;
$lineY2 = $lineY1;
}
else {
$lineX2 = $lineX1;
$lineY2 = $lineY1;
}
}
// display data points
if ($this->bool_data_points) {
//dont draw datapoints here or will overlap poorly with line
//instead collect coordinates
$pointX = $x1 + ($this->bar_width / 2); //MIDPOINT OF BARS, IF SHOWN
$this->data_point_array[] = array($pointX, $y1);
}
// display data values
if ($this->bool_data_values) {
$dataX = ($x1 + ($this->bar_width / 2)) - ((strlen($item) * $this->data_value_text_width) / 2);
//value to be graphed is equal/over 0
if ($item >= 0) {
$dataY = $y1 - $this->data_value_padding - $this->data_value_text_height;
}
else {
//check for item values below user spec'd range
if ($this->bool_user_data_range && $item <= $this->data_range_min) {
$dataY = $y1 - $this->data_value_padding - $this->data_value_text_height;
}
else {
$dataY = $y1 + $this->data_value_padding;
}
}
//add currency sign, formatting etc
if ($this->data_format_array) {
$item = $this->applyDataFormats($item);
}
if ($this->data_currency) {
$item = $this->applyDataCurrency($item);
}
//recenter data position if necessary
$dataX -= ($this->data_additional_length * $this->data_value_text_width) / 2;
imagestring($this->image, 2, $dataX, $dataY, $item, $this->data_value_color);
}
//write x axis value
if ($this->bool_x_axis_values) {
if ($data_set_num == $this->data_set_count - 1) {
if ($this->bool_x_axis_values_vert) {
if ($this->bool_all_negative) {
//we must put values above 0 line
$textVertPos = round($this->y_axis_y2 - $this->axis_value_padding);
}
else {
//mix of both pos and neg numbers
//write value y axis bottom value (will be under bottom of grid even if x axis is floating due to
$textVertPos = round($this->y_axis_y1 + (strlen($key) * $this->text_width) + $this->axis_value_padding);
}
$textHorizPos = round($xStart + ($this->bar_width / 2) - ($this->text_height / 2));
//skip and dispplay every x intervals
if ($this->x_axis_value_interval) {
if ($this->x_axis_value_interval_counter < $this->x_axis_value_interval) {
$this->x_axis_value_interval_counter++;
}
else {
imagestringup($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color);
$this->x_axis_value_interval_counter = 0;
}
}
else {
imagestringup($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color);
}
}
else {
if ($this->bool_all_negative) {
//we must put values above 0 line
$textVertPos = round($this->y_axis_y2 - $this->text_height - $this->axis_value_padding);
}
else {
//mix of both pos and neg numbers
//write value y axis bottom value (will be under bottom of grid even if x axis is floating due to
$textVertPos = round($this->y_axis_y1 + ($this->text_height * 2 / 3) - $this->axis_value_padding);
}
//horizontal data keys
$textHorizPos = round($xStart + ($this->bar_width / 2) - ((strlen($key) * $this->text_width) / 2));
//skip and dispplay every x intervals
if ($this->x_axis_value_interval) {
if ($this->x_axis_value_interval_counter < $this->x_axis_value_interval) {
$this->x_axis_value_interval_counter++;
}
else {
imagestring($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color);
$this->x_axis_value_interval_counter = 0;
}
}
else {
imagestring($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color);
}
}
}
}
$xStart += $this->bar_width + $this->space_width;
}
}
}
function finalizeColors() {
if ($this->bool_gradient) {
$num_set=count($this->multi_gradient_colors_1);
//loop through set colors and add backing colors if necessary
if ($num_set != $this->data_set_count) {
$color_darken_decimal = (100 - $this->color_darken_factor) / 100;
while ($num_set < $this->data_set_count) {
$color_ref_1 = $this->multi_gradient_colors_1[$num_set - 1];
$color_ref_2 = $this->multi_gradient_colors_2[$num_set - 1];
$this->multi_gradient_colors_1[] = array(
round($color_ref_1[0] * $color_darken_decimal),
round($color_ref_1[1] * $color_darken_decimal),
round($color_ref_1[2] * $color_darken_decimal));
$this->multi_gradient_colors_2[]=array(
round($color_ref_2[0] * $color_darken_decimal),
round($color_ref_2[1] * $color_darken_decimal),
round($color_ref_2[2] * $color_darken_decimal));
$num_set++;
}
}
while (count($this->multi_gradient_colors_1) > $this->data_set_count) {
$temp = array_pop($this->multi_gradient_colors_1);
}
while (count($this->multi_gradient_colors_2) > $this->data_set_count) {
$temp = array_pop($this->multi_gradient_colors_2);
}
$this->multi_gradient_colors_1 = array_reverse($this->multi_gradient_colors_1);
$this->multi_gradient_colors_2 = array_reverse($this->multi_gradient_colors_2);
}
else if (!$this->bool_gradient) {
$num_set = count($this->multi_bar_colors);
if ($num_set == 0) {
$this->multi_bar_colors[0] = $this->bar_color;
$num_set = 1;
}
//loop through set colors and add backing colors if necessary
while ($num_set < $this->data_set_count) {
$color_ref = $this->multi_bar_colors[$num_set - 1];
$color_parts = imagecolorsforindex($this->image, $color_ref);
$color_darken_decimal = (100 - $this->color_darken_factor) / 100;
$this->multi_bar_colors[$num_set] = imagecolorallocate($this->image,
round($color_parts['red'] * $color_darken_decimal),
round($color_parts['green'] * $color_darken_decimal),
round($color_parts['blue'] * $color_darken_decimal));
$num_set++;
}
while (count($this->multi_bar_colors) > $this->data_set_count) {
$temp = array_pop($this->multi_bar_colors);
}
$this->multi_bar_colors = array_reverse($this->multi_bar_colors);
}
if ($this->bool_line) {
if (!$this->bool_bars) {
$num_set = count($this->line_color);
if ($num_set == 0) {
$this->line_color[0] = $this->line_color_default;
$num_set = 1;
}
//only darken each data set's lines when no bars present
while ($num_set < $this->data_set_count) {
$color_ref = $this->line_color[$num_set - 1];
$color_parts = imagecolorsforindex($this->image, $color_ref);
$color_darken_decimal = (100 - $this->color_darken_factor) / 100;
$this->line_color[$num_set] = imagecolorallocate($this->image,
round($color_parts['red'] * $color_darken_decimal),
round($color_parts['green'] * $color_darken_decimal),
round($color_parts['blue'] * $color_darken_decimal));
$num_set++;
}
}
else {
$num_set = count($this->line_color);
while ($num_set < $this->data_set_count) {
$this->line_color[$num_set] = $this->line_color_default;
$num_set++;
}
}
while (count($this->line_color) > $this->data_set_count) {
$temp = array_pop($this->line_color);
}
$this->line_color = array_reverse($this->line_color);
}
}
function drawGradientBar($x1, $y1, $x2, $y2, $colorArr1, $colorArr2, $data_set_num) {
if (!isset($this->bool_gradient_colors_found[$data_set_num]) || $this->bool_gradient_colors_found[$data_set_num] == false) {
$this->gradient_handicap[$data_set_num] = 0;
$numLines = abs($x1 - $x2) + 1;
while ($numLines * $this->data_set_count > $this->gradient_max) {
//we have more lines than allowable colors
//use handicap to record this
$numLines /= 2;
$this->gradient_handicap[$data_set_num]++;
}
$color1R = $colorArr1[0];
$color1G = $colorArr1[1];
$color1B = $colorArr1[2];
$color2R = $colorArr2[0];
$color2G = $colorArr2[1];
$color2B = $colorArr2[2];
$rScale = ($color1R-$color2R) / $numLines;
$gScale = ($color1G-$color2G) / $numLines;
$bScale = ($color1B-$color2B) / $numLines;
$this->allocateGradientColors($color1R, $color1G, $color1B, $rScale, $gScale, $bScale, $numLines, $data_set_num);
}
$numLines = abs($x1 - $x2) + 1;
if ($this->gradient_handicap[$data_set_num] > 0) {
//if handicap is used, it will allow us to move through the array more slowly, depending on the set value
$interval = $this->gradient_handicap[$data_set_num];
for($i = 0; $i < $numLines; $i++) {
$adjusted_index = ceil($i / pow(2, $interval)) - 1;
if ($adjusted_index < 0) {
$adjusted_index = 0;
}
imageline($this->image, $x1+$i, $y1, $x1 + $i, $y2, $this->gradient_color_array[$data_set_num][$adjusted_index]);
}
}
else {
//normal gradients with colors < $this->gradient_max
for ($i = 0; $i < $numLines; $i++) {
imageline($this->image, $x1+$i, $y1, $x1+$i, $y2, $this->gradient_color_array[$data_set_num][$i]);
}
}
}
function generateGrid() {
if ($this->bool_user_data_range && $this->data_min >= 0) {
$adjustment = $this->data_min * $this->unit_scale;
}
else {
$adjustment = 0;
}
//calculate both values
$this->calculateGridHoriz($adjustment);
$this->calculateGridVert();
//output values - separation from calc is b/c of an extension related need
$this->generateGrids();
$this->generateGoalLines($adjustment);
}
function calculateGridHoriz($adjustment = 0) {
//determine horizontal grid lines
$horizGridArray = array();
if ($this->bool_user_data_range) {
$min = $this->data_min;
}
else {
$min = 0;
}
$horizGridArray[] = $min;
//use our function to determine ideal y axis scale interval
$intervalFromZero = $this->determineAxisMarkerScale($this->data_max, $this->data_min);
//if we have positive values, add grid values to array
//until we reach the max needed (we will go 1 over)
$cur = $min;
while ($cur < $this->data_max) {
$cur += $intervalFromZero;
$horizGridArray[] = $cur;
}
//if we have negative values, add grid values to array
//until we reach the min needed (we will go 1 over)
$cur = $min;
while ($cur > $this->data_min) {
$cur -= $intervalFromZero;
$horizGridArray[] = $cur;
}
//sort needed b/c we will use last value later (max)
sort($horizGridArray);
$this->true_displayed_max_value = $horizGridArray[count($horizGridArray) - 1];
$this->true_displayed_min_value = $horizGridArray[0];
//loop through each horizontal line
foreach ($horizGridArray as $value) {
$yValue = round($this->x_axis_y1 - ($value * $this->unit_scale) + $adjustment);
if ($this->bool_grid) {
//imageline($this->image, $this->y_axis_x1, $yValue, $this->x_axis_x2 , $yValue, $this->grid_color);
$this->horiz_grid_lines[] = array('x1' => $this->y_axis_x1, 'y1' => $yValue,
'x2' => $this->x_axis_x2, 'y2' => $yValue, 'color' => $this->grid_color);
}
//display value on y axis if desired using calc'd grid values
if ($this->bool_y_axis_values) {
$adjustedYValue = $yValue - ($this->text_height / 2);
$adjustedXValue = $this->y_axis_x1 - ((strlen($value) + $this->data_additional_length) * $this->text_width) - $this->axis_value_padding;
//add currency sign, formatting etc
if ($this->data_format_array) {
$value = $this->applyDataFormats($value);
}
if ($this->data_currency) {
$value = $this->applyDataCurrency($value);
}
//imagestring($this->image, 2, $adjustedXValue, $adjustedYValue, $value, $this->y_axis_text_color);
$this->horiz_grid_values[] = array('size' => 2, 'x' => $adjustedXValue, 'y' => $adjustedYValue,
'value' => $value, 'color' => $this->y_axis_text_color);
}
}
if (!$this->bool_all_positive && !$this->bool_user_data_range) {
//reset with better value based on grid min value calculations, not data min
$this->y_axis_y1 = $this->x_axis_y1 - ($horizGridArray[0] * $this->unit_scale);
}
//reset with better value based on grid value calculations, not data min
$this->y_axis_y2 = $yValue;
}
function calculateGridVert() {
//determine vertical grid lines
$vertGridArray = array();
$vertGrids = $this->data_count + 1;
$interval = $this->bar_width + $this->space_width;
//assemble vert gridline array
for ($i = 1; $i < $vertGrids; $i++) {
$vertGridArray[] = $this->y_axis_x1 + ($interval * $i);
}
//loop through each vertical line
if ($this->bool_grid) {
$xValue = $this->y_axis_y1;
foreach ($vertGridArray as $value) {
//imageline($this->image, $value, $this->y_axis_y2, $value, $xValue , $this->grid_color);
$this->vert_grid_lines[] = array('x1' => $value, 'y1' => $this->y_axis_y2,
'x2' => $value, 'y2' => $xValue, 'color' => $this->grid_color);
}
}
}
function imagelinedashed(&$image_handle, $x_axis_x1, $yLocation, $x_axis_x2 , $yLocation, $color) {
$step = 3;
for ($i = $x_axis_x1; $i < $x_axis_x2 -1; $i += ($step*2)) {
imageline($this->image, $i, $yLocation, $i + $step - 1, $yLocation, $color);
}
}
function generateGrids() {
//loop through and display values
foreach ($this->horiz_grid_lines as $line) {
imageline($this->image, $line['x1'], $line['y1'], $line['x2'], $line['y2'] , $line['color']);
}
foreach ($this->vert_grid_lines as $line) {
imageline($this->image, $line['x1'], $line['y1'], $line['x2'], $line['y2'] , $line['color']);
}
foreach ($this->horiz_grid_values as $value) {
imagestring($this->image, $value['size'], $value['x'], $value['y'], $value['value'], $value['color']);
}
//not implemented in the base library, but used in extensions
foreach ($this->vert_grid_values as $value) {
imagestring($this->image, $value['size'], $value['x'], $value['y'], $value['value'], $value['color']);
}
}
function generateGoalLines($adjustment = 0) {
//draw goal lines if present (after grid) - doesn't get executed if array empty
foreach ($this->goal_line_array as $goal_line_data) {
$yLocation = $goal_line_data['yValue'];
$style = $goal_line_data['style'];
$color = $goal_line_data['color'] ? $goal_line_data['color'] : $this->goal_line_color;
$yLocation = round(($this->x_axis_y1 - ($yLocation * $this->unit_scale) + $adjustment));
if ($style == 'dashed') {
$this->imagelinedashed($this->image, $this->x_axis_x1, $yLocation, $this->x_axis_x2 , $yLocation, $color);
}
else {
//a solid line is the default if a style condition is not matched
imageline($this->image, $this->x_axis_x1, $yLocation, $this->x_axis_x2 , $yLocation, $color);
}
}
}
function generateDataPoints() {
foreach ($this->data_point_array as $pointArray) {
imagefilledellipse($this->image, $pointArray[0], $pointArray[1], $this->data_point_width, $this->data_point_width, $this->data_point_color);
}
}
function generateXAxis() {
imageline($this->image, $this->x_axis_x1, $this->x_axis_y1, $this->x_axis_x2, $this->x_axis_y2, $this->x_axis_color);
}
function generateYAxis() {
imageline($this->image, $this->y_axis_x1, $this->y_axis_y1, $this->y_axis_x2, $this->y_axis_y2, $this->y_axis_color);
}
function generateBackgound() {
imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $this->background_color);
}
function generateTitle() {
//spacing may have changed since earlier
//use top margin or grid top y, whichever less
$highestElement = ($this->top_margin < $this->y_axis_y2) ? $this->top_margin : $this->y_axis_y2;
$textVertPos = ($highestElement / 2) - ($this->title_char_height / 2); //centered
$titleLength = strlen($this->title_text);
if ($this->bool_title_center) {
$this->title_x = ($this->width / 2) - (($titleLength * $this->title_char_width) / 2);
$this->title_y = $textVertPos;
}
else if ($this->bool_title_left) {
$this->title_x = $this->y_axis_x1;
$this->title_y = $textVertPos;
}
else if ($this->bool_title_right) {
$this->title_x = $this->x_axis_x2 - ($titleLength * $this->title_char_width);
$this->title_y = $textVertPos;
}
imagestring($this->image, 2, $this->title_x , $this->title_y , $this->title_text, $this->title_color);
}
function calcTopMargin() {
if ($this->bool_title) {
//include space for title, approx margin + 3*title height
$this->top_margin = ($this->height * ($this->x_axis_default_percent / 100)) + $this->title_char_height;
}
else {
//just use default spacing
$this->top_margin = $this->height * ($this->x_axis_default_percent / 100);
}
}
function calcRightMargin() {
//just use default spacing
$this->right_margin = $this->width * ($this->y_axis_default_percent / 100);
}
function calcCoords() {
//calculate axis points, also used for other calculations
$this->x_axis_x1 = $this->y_axis_margin;
$this->x_axis_y1 = $this->height - $this->x_axis_margin;
$this->x_axis_x2 = $this->width - $this->right_margin;
$this->x_axis_y2 = $this->height - $this->x_axis_margin;
$this->y_axis_x1 = $this->y_axis_margin;
$this->y_axis_y1 = $this->height - $this->x_axis_margin;
$this->y_axis_x2 = $this->y_axis_margin;
$this->y_axis_y2 = $this->top_margin;
}
function determineAxisMarkerScale($max, $min, $axis = 'y') {
switch ($axis) {
case 'y' : $space = $this->height; break;
case 'x' : $space = $this->width; break;
}
//for calclation, take range or max-0
if ($this->bool_user_data_range) {
$range = abs($max - $min);
}
else {
$range = (abs($max-$min) > abs($max - 0)) ? abs($max - $min) : abs($max - 0);
}
//handle all zero data
if ($range == 0) {
$range = 10;
}
//multiply up to over 100, to better figure interval
$count = 0;
while (abs($range) < 100) {
$range *= 10;
$count++;
}
//divide into intervals based on height / preset constant - after rounding will be approx
$divisor = round($space / $this->range_divisor_factor);
$divided = round($range / $divisor);
$result = $this->roundUpOneExtraDigit($divided);
//if rounded up w/ extra digit is more than 200% of divided value,
//round up to next sig number with same num of digits
if ($result / $divided >= 2) {
$result = $this->roundUpSameDigits($divided);
}
//divide back down, if needed
for ($i = 0; $i < $count; $i++) {
$result /= 10;
}
return $result;
}
function roundUpSameDigits($num) {
$len = strlen($num);
if (round($num, -1 * ($len - 1)) == $num) {
//we already have a sig number
return $num;
}
else {
$firstDig = substr($num, 0,1);
$secondDig = substr($num, 1,1);
$rest = substr($num, 2);
$secondDig = 5;
$altered = $firstDig.$secondDig.$rest;
//after reassembly, round up to next sig number, same # of digits
return round((int)$altered, -1 * ($len - 1));
}
}
function roundUpOneExtraDigit($num) {
$len = strlen($num);
$firstDig = substr($num, 0, 1);
$rest = substr($num, 1);
$firstDig = 5;
$altered = $firstDig . $rest;
//after reassembly, round up to next sig number, one extra # of digits
return round((int)$altered, -1 * ($len));
}
function displayErrors() {
if (count($this->error) > 0) {
$lineHeight = 12;
$errorColor = imagecolorallocate($this->image, 0, 0, 0);
$errorBackColor = imagecolorallocate($this->image, 255, 204, 0);
imagefilledrectangle($this->image, 0, 0, $this->width - 1, 2 * $lineHeight, $errorBackColor);
imagestring($this->image, 3, 2, 0, "!!----- PHPGraphLib Error -----!!", $errorColor);
foreach($this->error as $key => $errorText) {
imagefilledrectangle($this->image, 0, ($key * $lineHeight) + $lineHeight, $this->width - 1, ($key * $lineHeight) + 2 * $lineHeight, $errorBackColor);
imagestring($this->image, 2, 2, ($key * $lineHeight) + $lineHeight, "[". ($key + 1) . "] ". $errorText, $errorColor);
}
$errorOutlineColor = imagecolorallocate($this->image, 255, 0, 0);
imagerectangle($this->image, 0, 0, $this->width-1,($key * $lineHeight) + 2 * $lineHeight, $errorOutlineColor);
}
}
function addData($data, $data2 = '', $data3 = '', $data4 = '', $data5 = '') {
if (is_array($data)){ $this->data_array[]=$data; }
if (is_array($data2)){ $this->data_array[]=$data2; }
if (is_array($data3)){ $this->data_array[]=$data3; }
if (is_array($data4)){ $this->data_array[]=$data4; }
if (is_array($data5)){ $this->data_array[]=$data5; }
//assess data
$min = $this->data_max_allowable;
$max = $this->data_min_allowable;
$this->data_count = 0;
//get rid of bad data, find max, min
foreach ($this->data_array as $data_set_num => $data_set) {
foreach ($data_set as $key => $item) {
if (!is_numeric($item)) {
unset($this->data_array[$data_set_num][$key]);
}
else {
if ($item < $min){ $min = $item; }
if ($item > $max){ $max = $item; }
}
}
//find the count of the dataset with the most data points
$count = count($this->data_array[$data_set_num]);
$count > $this->data_count ? ($this->data_count = $count) : NULL;
}
//number of valid data sets
$this->data_set_count = count($this->data_array);
if ($this->data_set_count == 0) {
$this->error[] = "No valid datasets added in adddata() function.";
}
else {
$this->calcMinMax($min, $max);
}
}
function calcMinMax($min, $max) {
$this->bool_data = true;
//min and max for all datasets
$this->data_min = $min;
$this->data_max = $max;
if ($this->data_min >= 0) {
$this->bool_all_positive = true;
}
else if ($this->data_max<=0) {
$this->bool_all_negative=true;
}
//setup fake max and min for all zero data
if ($this->data_min >= 0 && $this->data_max == 0 ) {
$this->data_min = 0;
$this->data_max = 10;
$this->all_zero_data = true;
}
}
function setupXAxis($percent = '', $color = '') {
if ($percent === false) {
$this->bool_x_axis = false;
}
else {
$this->bool_x_axis = true;
}
$this->bool_x_axis_setup = true;
if (!empty($color) && $arr = $this->returnColorArray($color)) {
$this->x_axis_color = imagecolorallocate($this->image, $arr[0], $arr[1], $arr[2]);
}
if (is_numeric($percent) && $percent>0) {
$percent = $percent / 100;
$this->x_axis_margin = round($this->height * $percent);
}
else {
$percent = $this->x_axis_default_percent / 100;
$this->x_axis_margin = round($this->height * $percent);
}
}
function setupYAxis($percent = '', $color = '') {
if ($percent === false) {
$this->bool_y_axis = false;
}
else {
$this->bool_y_axis = true;
}
$this->bool_y_axis_setup = true;
if (!empty($color) && $arr = $this->returnColorArray($color)) {
$this->y_axis_color = imagecolorallocate($this->image, $arr[0], $arr[1], $arr[2]);
}
if (is_numeric($percent) && $percent>0) {
$this->y_axis_margin = round($this->width * ($percent / 100));
}
else {
$percent = $this->y_axis_default_percent / 100;
$this->y_axis_margin = round($this->width * $percent);
}
}
function setRange($min, $max) {
//because of deprecated use of this function as($max, $min)
if ($min > $max) {
$this->data_range_max = $min;
$this->data_range_min = $max;
}
else {
$this->data_range_max = $max;
$this->data_range_min = $min;
}
$this->bool_user_data_range = true;
}
function setTitle($title) {
if (!empty($title)) {
$this->title_text = $title;
$this->bool_title = true;
}
else{ $this->error[] = "String arg for setTitle() not specified properly."; }
}
function setTitleLocation($location) {
$this->bool_title_left = false;
$this->bool_title_right = false;
$this->bool_title_center = false;
switch (strtolower($location)) {
case 'left': $this->bool_title_left = true; break;
case 'right': $this->bool_title_right = true; break;
case 'center': $this->bool_title_center = true; break;
default: $this->error[] = "String arg for setTitleLocation() not specified properly.";
}
}
function setBars($bool) {
if (is_bool($bool)) { $this->bool_bars = $bool;}