forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphlib.php
1771 lines (1532 loc) · 82.2 KB
/
graphlib.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
/**
* Graph Class. PHP Class to draw line, point, bar, and area graphs, including numeric x-axis and double y-axis.
* Version: 1.6.3
* Copyright (C) 2000 Herman Veluwenkamp
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Copy of GNU Lesser General Public License at: http://www.gnu.org/copyleft/lesser.txt
* Contact author at: [email protected]
*
* @package core
* @subpackage lib
*/
defined('MOODLE_INTERNAL') || die();
/* This file contains modifications by Martin Dougiamas
* as part of Moodle (http://moodle.com). Modified lines
* are marked with "Moodle".
*/
/**
* @package moodlecore
*/
class graph {
var $image;
var $debug = FALSE; // be careful!!
var $calculated = array(); // array of computed values for chart
var $parameter = array( // input parameters
'width' => 320, // default width of image
'height' => 240, // default height of image
'file_name' => 'none', // name of file for file to be saved as.
// NOTE: no suffix required. this is determined from output_format below.
'output_format' => 'PNG', // image output format. 'GIF', 'PNG', 'JPEG'. default 'PNG'.
'seconds_to_live' => 0, // expiry time in seconds (for HTTP header)
'hours_to_live' => 0, // expiry time in hours (for HTTP header)
'path_to_fonts' => 'fonts/', // path to fonts folder. don't forget *trailing* slash!!
// for WINDOZE this may need to be the full path, not relative.
'title' => 'Graph Title', // text for graph title
'title_font' => 'default.ttf', // title text font. don't forget to set 'path_to_fonts' above.
'title_size' => 16, // title text point size
'title_colour' => 'black', // colour for title text
'x_label' => '', // if this is set then this text is printed on bottom axis of graph.
'y_label_left' => '', // if this is set then this text is printed on left axis of graph.
'y_label_right' => '', // if this is set then this text is printed on right axis of graph.
'label_size' => 8, // label text point size
'label_font' => 'default.ttf', // label text font. don't forget to set 'path_to_fonts' above.
'label_colour' => 'gray33', // label text colour
'y_label_angle' => 90, // rotation of y axis label
'x_label_angle' => 90, // rotation of y axis label
'outer_padding' => 5, // padding around outer text. i.e. title, y label, and x label.
'inner_padding' => 0, // padding beteen axis text and graph.
'x_inner_padding' => 5, // padding beteen axis text and graph.
'y_inner_padding' => 6, // padding beteen axis text and graph.
'outer_border' => 'none', // colour of border aound image, or 'none'.
'inner_border' => 'black', // colour of border around actual graph, or 'none'.
'inner_border_type' => 'box', // 'box' for all four sides, 'axis' for x/y axis only,
// 'y' or 'y-left' for y axis only, 'y-right' for right y axis only,
// 'x' for x axis only, 'u' for both left and right y axis and x axis.
'outer_background' => 'none', // background colour of entire image.
'inner_background' => 'none', // background colour of plot area.
'y_min_left' => 0, // this will be reset to minimum value if there is a value lower than this.
'y_max_left' => 0, // this will be reset to maximum value if there is a value higher than this.
'y_min_right' => 0, // this will be reset to minimum value if there is a value lower than this.
'y_max_right' => 0, // this will be reset to maximum value if there is a value higher than this.
'x_min' => 0, // only used if x axis is numeric.
'x_max' => 0, // only used if x axis is numeric.
'y_resolution_left' => 1, // scaling for rounding of y axis max value.
// if max y value is 8645 then
// if y_resolution is 0, then y_max becomes 9000.
// if y_resolution is 1, then y_max becomes 8700.
// if y_resolution is 2, then y_max becomes 8650.
// if y_resolution is 3, then y_max becomes 8645.
// get it?
'y_decimal_left' => 0, // number of decimal places for y_axis text.
'y_resolution_right' => 2, // ... same for right hand side
'y_decimal_right' => 0, // ... same for right hand side
'x_resolution' => 2, // only used if x axis is numeric.
'x_decimal' => 0, // only used if x axis is numeric.
'point_size' => 4, // default point size. use even number for diamond or triangle to get nice look.
'brush_size' => 4, // default brush size for brush line.
'brush_type' => 'circle', // type of brush to use to draw line. choose from the following
// 'circle', 'square', 'horizontal', 'vertical', 'slash', 'backslash'
'bar_size' => 0.8, // size of bar to draw. <1 bars won't touch
// 1 is full width - i.e. bars will touch.
// >1 means bars will overlap.
'bar_spacing' => 10, // space in pixels between group of bars for each x value.
'shadow_offset' => 3, // draw shadow at this offset, unless overidden by data parameter.
'shadow' => 'grayCC', // 'none' or colour of shadow.
'shadow_below_axis' => true, // whether to draw shadows of bars and areas below the x/zero axis.
'x_axis_gridlines' => 'auto', // if set to a number then x axis is treated as numeric.
'y_axis_gridlines' => 6, // number of gridlines on y axis.
'zero_axis' => 'none', // colour to draw zero-axis, or 'none'.
'axis_font' => 'default.ttf', // axis text font. don't forget to set 'path_to_fonts' above.
'axis_size' => 8, // axis text font size in points
'axis_colour' => 'gray33', // colour of axis text.
'y_axis_angle' => 0, // rotation of axis text.
'x_axis_angle' => 0, // rotation of axis text.
'y_axis_text_left' => 1, // whether to print left hand y axis text. if 0 no text, if 1 all ticks have text,
'x_axis_text' => 1, // if 4 then print every 4th tick and text, etc...
'y_axis_text_right' => 0, // behaviour same as above for right hand y axis.
'x_offset' => 0.5, // x axis tick offset from y axis as fraction of tick spacing.
'y_ticks_colour' => 'black', // colour to draw y ticks, or 'none'
'x_ticks_colour' => 'black', // colour to draw x ticks, or 'none'
'y_grid' => 'line', // grid lines. set to 'line' or 'dash'...
'x_grid' => 'line', // or if set to 'none' print nothing.
'grid_colour' => 'grayEE', // default grid colour.
'tick_length' => 4, // length of ticks in pixels. can be negative. i.e. outside data drawing area.
'legend' => 'none', // default. no legend.
// otherwise: 'top-left', 'top-right', 'bottom-left', 'bottom-right',
// 'outside-top', 'outside-bottom', 'outside-left', or 'outside-right'.
'legend_offset' => 10, // offset in pixels from graph or outside border.
'legend_padding' => 5, // padding around legend text.
'legend_font' => 'default.ttf', // legend text font. don't forget to set 'path_to_fonts' above.
'legend_size' => 8, // legend text point size.
'legend_colour' => 'black', // legend text colour.
'legend_border' => 'none', // legend border colour, or 'none'.
'decimal_point' => '.', // symbol for decimal separation '.' or ',' *european support.
'thousand_sep' => ',', // symbol for thousand separation ',' or ''
);
var $y_tick_labels = null; // array of text values for y-axis tick labels
var $offset_relation = null; // array of offsets for different sets of data
// init all text - title, labels, and axis text.
function init() {
/// Moodle mods: overrides the font path and encodings
global $CFG;
/// A default.ttf is searched for in this order:
/// dataroot/lang/xx_local/fonts
/// dataroot/lang/xx/fonts
/// dirroot/lang/xx/fonts
/// dataroot/lang
/// lib/
$currlang = current_language();
if (file_exists("$CFG->dataroot/lang/".$currlang."_local/fonts/default.ttf")) {
$fontpath = "$CFG->dataroot/lang/".$currlang."_local/fonts/";
} else if (file_exists("$CFG->dataroot/lang/$currlang/fonts/default.ttf")) {
$fontpath = "$CFG->dataroot/lang/$currlang/fonts/";
} else if (file_exists("$CFG->dirroot/lang/$currlang/fonts/default.ttf")) {
$fontpath = "$CFG->dirroot/lang/$currlang/fonts/";
} else if (file_exists("$CFG->dataroot/lang/default.ttf")) {
$fontpath = "$CFG->dataroot/lang/";
} else {
$fontpath = "$CFG->libdir/";
}
$this->parameter['path_to_fonts'] = $fontpath;
/// End Moodle mods
$this->calculated['outer_border'] = $this->calculated['boundary_box'];
// outer padding
$this->calculated['boundary_box']['left'] += $this->parameter['outer_padding'];
$this->calculated['boundary_box']['top'] += $this->parameter['outer_padding'];
$this->calculated['boundary_box']['right'] -= $this->parameter['outer_padding'];
$this->calculated['boundary_box']['bottom'] -= $this->parameter['outer_padding'];
$this->init_x_axis();
$this->init_y_axis();
$this->init_legend();
$this->init_labels();
// take into account tick lengths
$this->calculated['bottom_inner_padding'] = $this->parameter['x_inner_padding'];
if (($this->parameter['x_ticks_colour'] != 'none') && ($this->parameter['tick_length'] < 0))
$this->calculated['bottom_inner_padding'] -= $this->parameter['tick_length'];
$this->calculated['boundary_box']['bottom'] -= $this->calculated['bottom_inner_padding'];
$this->calculated['left_inner_padding'] = $this->parameter['y_inner_padding'];
if ($this->parameter['y_axis_text_left']) {
if (($this->parameter['y_ticks_colour'] != 'none') && ($this->parameter['tick_length'] < 0))
$this->calculated['left_inner_padding'] -= $this->parameter['tick_length'];
}
$this->calculated['boundary_box']['left'] += $this->calculated['left_inner_padding'];
$this->calculated['right_inner_padding'] = $this->parameter['y_inner_padding'];
if ($this->parameter['y_axis_text_right']) {
if (($this->parameter['y_ticks_colour'] != 'none') && ($this->parameter['tick_length'] < 0))
$this->calculated['right_inner_padding'] -= $this->parameter['tick_length'];
}
$this->calculated['boundary_box']['right'] -= $this->calculated['right_inner_padding'];
// boundaryBox now has coords for plotting area.
$this->calculated['inner_border'] = $this->calculated['boundary_box'];
$this->init_data();
$this->init_x_ticks();
$this->init_y_ticks();
}
function draw_text() {
$colour = $this->parameter['outer_background'];
if ($colour != 'none') $this->draw_rectangle($this->calculated['outer_border'], $colour, 'fill'); // graph background
// draw border around image
$colour = $this->parameter['outer_border'];
if ($colour != 'none') $this->draw_rectangle($this->calculated['outer_border'], $colour, 'box'); // graph border
$this->draw_title();
$this->draw_x_label();
$this->draw_y_label_left();
$this->draw_y_label_right();
$this->draw_x_axis();
$this->draw_y_axis();
if ($this->calculated['y_axis_left']['has_data']) $this->draw_zero_axis_left(); // either draw zero axis on left
else if ($this->calculated['y_axis_right']['has_data']) $this->draw_zero_axis_right(); // ... or right.
$this->draw_legend();
// draw border around plot area
$colour = $this->parameter['inner_background'];
if ($colour != 'none') $this->draw_rectangle($this->calculated['inner_border'], $colour, 'fill'); // graph background
// draw border around image
$colour = $this->parameter['inner_border'];
if ($colour != 'none') $this->draw_rectangle($this->calculated['inner_border'], $colour, $this->parameter['inner_border_type']); // graph border
}
function draw_stack() {
$this->init();
$this->draw_text();
$yOrder = $this->y_order; // save y_order data.
// iterate over each data set. order is very important if you want to see data correctly. remember shadows!!
foreach ($yOrder as $set) {
$this->y_order = array($set);
$this->init_data();
$this->draw_data();
}
$this->y_order = $yOrder; // revert y_order data.
$this->output();
}
function draw() {
$this->init();
$this->draw_text();
$this->draw_data();
$this->output();
}
// draw a data set
function draw_set($order, $set, $offset) {
if ($offset) @$this->init_variable($colour, $this->y_format[$set]['shadow'], $this->parameter['shadow']);
else $colour = $this->y_format[$set]['colour'];
@$this->init_variable($point, $this->y_format[$set]['point'], 'none');
@$this->init_variable($pointSize, $this->y_format[$set]['point_size'], $this->parameter['point_size']);
@$this->init_variable($line, $this->y_format[$set]['line'], 'none');
@$this->init_variable($brushType, $this->y_format[$set]['brush_type'], $this->parameter['brush_type']);
@$this->init_variable($brushSize, $this->y_format[$set]['brush_size'], $this->parameter['brush_size']);
@$this->init_variable($bar, $this->y_format[$set]['bar'], 'none');
@$this->init_variable($barSize, $this->y_format[$set]['bar_size'], $this->parameter['bar_size']);
@$this->init_variable($area, $this->y_format[$set]['area'], 'none');
$lastX = 0;
$lastY = 'none';
$fromX = 0;
$fromY = 'none';
//print "set $set<br />";
//expand_pre($this->calculated['y_plot']);
foreach ($this->x_data as $index => $x) {
//print "index $index<br />";
$thisY = $this->calculated['y_plot'][$set][$index];
$thisX = $this->calculated['x_plot'][$index];
//print "$thisX, $thisY <br />";
if (($bar!='none') && (string)$thisY != 'none') {
if ($relatedset = $this->offset_relation[$set]) { // Moodle
$yoffset = $this->calculated['y_plot'][$relatedset][$index]; // Moodle
} else { // Moodle
$yoffset = 0; // Moodle
} // Moodle
//$this->bar($thisX, $thisY, $bar, $barSize, $colour, $offset, $set); // Moodle
$this->bar($thisX, $thisY, $bar, $barSize, $colour, $offset, $set, $yoffset); // Moodle
}
if (($area!='none') && (((string)$lastY != 'none') && ((string)$thisY != 'none')))
$this->area($lastX, $lastY, $thisX, $thisY, $area, $colour, $offset);
if (($point!='none') && (string)$thisY != 'none') $this->plot($thisX, $thisY, $point, $pointSize, $colour, $offset);
if (($line!='none') && ((string)$thisY != 'none')) {
if ((string)$fromY != 'none')
$this->line($fromX, $fromY, $thisX, $thisY, $line, $brushType, $brushSize, $colour, $offset);
$fromY = $thisY; // start next line from here
$fromX = $thisX; // ...
} else {
$fromY = 'none';
$fromX = 'none';
}
$lastX = $thisX;
$lastY = $thisY;
}
}
function draw_data() {
// cycle thru y data to be plotted
// first check for drop shadows...
foreach ($this->y_order as $order => $set) {
@$this->init_variable($offset, $this->y_format[$set]['shadow_offset'], $this->parameter['shadow_offset']);
@$this->init_variable($colour, $this->y_format[$set]['shadow'], $this->parameter['shadow']);
if ($colour != 'none') $this->draw_set($order, $set, $offset);
}
// then draw data
foreach ($this->y_order as $order => $set) {
$this->draw_set($order, $set, 0);
}
}
function draw_legend() {
$position = $this->parameter['legend'];
if ($position == 'none') return; // abort if no border
$borderColour = $this->parameter['legend_border'];
$offset = $this->parameter['legend_offset'];
$padding = $this->parameter['legend_padding'];
$height = $this->calculated['legend']['boundary_box_all']['height'];
$width = $this->calculated['legend']['boundary_box_all']['width'];
$graphTop = $this->calculated['boundary_box']['top'];
$graphBottom = $this->calculated['boundary_box']['bottom'];
$graphLeft = $this->calculated['boundary_box']['left'];
$graphRight = $this->calculated['boundary_box']['right'];
$outsideRight = $this->calculated['outer_border']['right'];
$outsideBottom = $this->calculated['outer_border']['bottom'];
switch ($position) {
case 'top-left':
$top = $graphTop + $offset;
$bottom = $graphTop + $height + $offset;
$left = $graphLeft + $offset;
$right = $graphLeft + $width + $offset;
break;
case 'top-right':
$top = $graphTop + $offset;
$bottom = $graphTop + $height + $offset;
$left = $graphRight - $width - $offset;
$right = $graphRight - $offset;
break;
case 'bottom-left':
$top = $graphBottom - $height - $offset;
$bottom = $graphBottom - $offset;
$left = $graphLeft + $offset;
$right = $graphLeft + $width + $offset;
break;
case 'bottom-right':
$top = $graphBottom - $height - $offset;
$bottom = $graphBottom - $offset;
$left = $graphRight - $width - $offset;
$right = $graphRight - $offset;
break;
case 'outside-top' :
$top = $graphTop;
$bottom = $graphTop + $height;
$left = $outsideRight - $width - $offset;
$right = $outsideRight - $offset;
break;
case 'outside-bottom' :
$top = $graphBottom - $height;
$bottom = $graphBottom;
$left = $outsideRight - $width - $offset;
$right = $outsideRight - $offset;
break;
case 'outside-left' :
$top = $outsideBottom - $height - $offset;
$bottom = $outsideBottom - $offset;
$left = $graphLeft;
$right = $graphLeft + $width;
break;
case 'outside-right' :
$top = $outsideBottom - $height - $offset;
$bottom = $outsideBottom - $offset;
$left = $graphRight - $width;
$right = $graphRight;
break;
default: // default is top left. no particular reason.
$top = $this->calculated['boundary_box']['top'];
$bottom = $this->calculated['boundary_box']['top'] + $this->calculated['legend']['boundary_box_all']['height'];
$left = $this->calculated['boundary_box']['left'];
$right = $this->calculated['boundary_box']['right'] + $this->calculated['legend']['boundary_box_all']['width'];
}
// legend border
if($borderColour!='none') $this->draw_rectangle(array('top' => $top,
'left' => $left,
'bottom' => $bottom,
'right' => $right), $this->parameter['legend_border'], 'box');
// legend text
$legendText = array('points' => $this->parameter['legend_size'],
'angle' => 0,
'font' => $this->parameter['legend_font'],
'colour' => $this->parameter['legend_colour']);
$box = $this->calculated['legend']['boundary_box_max']['height']; // use max height for legend square size.
$x = $left + $padding;
$x_text = $x + $box * 2;
$y = $top + $padding;
foreach ($this->y_order as $set) {
$legendText['text'] = $this->calculated['legend']['text'][$set];
if ($legendText['text'] != 'none') {
// if text exists then draw box and text
$boxColour = $this->colour[$this->y_format[$set]['colour']];
// draw box
ImageFilledRectangle($this->image, $x, $y, $x + $box, $y + $box, $boxColour);
// draw text
$coords = array('x' => $x + $box * 2, 'y' => $y, 'reference' => 'top-left');
$legendText['boundary_box'] = $this->calculated['legend']['boundary_box'][$set];
$this->update_boundaryBox($legendText['boundary_box'], $coords);
$this->print_TTF($legendText);
$y += $padding + $box;
}
}
}
function draw_y_label_right() {
if (!$this->parameter['y_label_right']) return;
$x = $this->calculated['boundary_box']['right'] + $this->parameter['y_inner_padding'];
if ($this->parameter['y_axis_text_right']) $x += $this->calculated['y_axis_right']['boundary_box_max']['width']
+ $this->calculated['right_inner_padding'];
$y = ($this->calculated['boundary_box']['bottom'] + $this->calculated['boundary_box']['top']) / 2;
$label = $this->calculated['y_label_right'];
$coords = array('x' => $x, 'y' => $y, 'reference' => 'left-center');
$this->update_boundaryBox($label['boundary_box'], $coords);
$this->print_TTF($label);
}
function draw_y_label_left() {
if (!$this->parameter['y_label_left']) return;
$x = $this->calculated['boundary_box']['left'] - $this->parameter['y_inner_padding'];
if ($this->parameter['y_axis_text_left']) $x -= $this->calculated['y_axis_left']['boundary_box_max']['width']
+ $this->calculated['left_inner_padding'];
$y = ($this->calculated['boundary_box']['bottom'] + $this->calculated['boundary_box']['top']) / 2;
$label = $this->calculated['y_label_left'];
$coords = array('x' => $x, 'y' => $y, 'reference' => 'right-center');
$this->update_boundaryBox($label['boundary_box'], $coords);
$this->print_TTF($label);
}
function draw_title() {
if (!$this->parameter['title']) return;
//$y = $this->calculated['outside_border']['top'] + $this->parameter['outer_padding'];
$y = $this->calculated['boundary_box']['top'] - $this->parameter['outer_padding'];
$x = ($this->calculated['boundary_box']['right'] + $this->calculated['boundary_box']['left']) / 2;
$label = $this->calculated['title'];
$coords = array('x' => $x, 'y' => $y, 'reference' => 'bottom-center');
$this->update_boundaryBox($label['boundary_box'], $coords);
$this->print_TTF($label);
}
function draw_x_label() {
if (!$this->parameter['x_label']) return;
$y = $this->calculated['boundary_box']['bottom'] + $this->parameter['x_inner_padding'];
if ($this->parameter['x_axis_text']) $y += $this->calculated['x_axis']['boundary_box_max']['height']
+ $this->calculated['bottom_inner_padding'];
$x = ($this->calculated['boundary_box']['right'] + $this->calculated['boundary_box']['left']) / 2;
$label = $this->calculated['x_label'];
$coords = array('x' => $x, 'y' => $y, 'reference' => 'top-center');
$this->update_boundaryBox($label['boundary_box'], $coords);
$this->print_TTF($label);
}
function draw_zero_axis_left() {
$colour = $this->parameter['zero_axis'];
if ($colour == 'none') return;
// draw zero axis on left hand side
$this->calculated['zero_axis'] = round($this->calculated['boundary_box']['top'] + ($this->calculated['y_axis_left']['max'] * $this->calculated['y_axis_left']['factor']));
ImageLine($this->image, $this->calculated['boundary_box']['left'], $this->calculated['zero_axis'], $this->calculated['boundary_box']['right'], $this->calculated['zero_axis'], $this->colour[$colour]);
}
function draw_zero_axis_right() {
$colour = $this->parameter['zero_axis'];
if ($colour == 'none') return;
// draw zero axis on right hand side
$this->calculated['zero_axis'] = round($this->calculated['boundary_box']['top'] + ($this->calculated['y_axis_right']['max'] * $this->calculated['y_axis_right']['factor']));
ImageLine($this->image, $this->calculated['boundary_box']['left'], $this->calculated['zero_axis'], $this->calculated['boundary_box']['right'], $this->calculated['zero_axis'], $this->colour[$colour]);
}
function draw_x_axis() {
$gridColour = $this->colour[$this->parameter['grid_colour']];
$tickColour = $this->colour[$this->parameter['x_ticks_colour']];
$axis_colour = $this->parameter['axis_colour'];
$xGrid = $this->parameter['x_grid'];
$gridTop = $this->calculated['boundary_box']['top'];
$gridBottom = $this->calculated['boundary_box']['bottom'];
if ($this->parameter['tick_length'] >= 0) {
$tickTop = $this->calculated['boundary_box']['bottom'] - $this->parameter['tick_length'];
$tickBottom = $this->calculated['boundary_box']['bottom'];
$textBottom = $tickBottom + $this->calculated['bottom_inner_padding'];
} else {
$tickTop = $this->calculated['boundary_box']['bottom'];
$tickBottom = $this->calculated['boundary_box']['bottom'] - $this->parameter['tick_length'];
$textBottom = $tickBottom + $this->calculated['bottom_inner_padding'];
}
$axis_font = $this->parameter['axis_font'];
$axis_size = $this->parameter['axis_size'];
$axis_angle = $this->parameter['x_axis_angle'];
if ($axis_angle == 0) $reference = 'top-center';
if ($axis_angle > 0) $reference = 'top-right';
if ($axis_angle < 0) $reference = 'top-left';
if ($axis_angle == 90) $reference = 'top-center';
//generic tag information. applies to all axis text.
$axisTag = array('points' => $axis_size, 'angle' => $axis_angle, 'font' => $axis_font, 'colour' => $axis_colour);
foreach ($this->calculated['x_axis']['tick_x'] as $set => $tickX) {
// draw x grid if colour specified
if ($xGrid != 'none') {
switch ($xGrid) {
case 'line':
ImageLine($this->image, round($tickX), round($gridTop), round($tickX), round($gridBottom), $gridColour);
break;
case 'dash':
ImageDashedLine($this->image, round($tickX), round($gridTop), round($tickX), round($gridBottom), $gridColour);
break;
}
}
if ($this->parameter['x_axis_text'] && !($set % $this->parameter['x_axis_text'])) { // test if tick should be displayed
// draw tick
if ($tickColour != 'none')
ImageLine($this->image, round($tickX), round($tickTop), round($tickX), round($tickBottom), $tickColour);
// draw axis text
$coords = array('x' => $tickX, 'y' => $textBottom, 'reference' => $reference);
$axisTag['text'] = $this->calculated['x_axis']['text'][$set];
$axisTag['boundary_box'] = $this->calculated['x_axis']['boundary_box'][$set];
$this->update_boundaryBox($axisTag['boundary_box'], $coords);
$this->print_TTF($axisTag);
}
}
}
function draw_y_axis() {
$gridColour = $this->colour[$this->parameter['grid_colour']];
$tickColour = $this->colour[$this->parameter['y_ticks_colour']];
$axis_colour = $this->parameter['axis_colour'];
$yGrid = $this->parameter['y_grid'];
$gridLeft = $this->calculated['boundary_box']['left'];
$gridRight = $this->calculated['boundary_box']['right'];
// axis font information
$axis_font = $this->parameter['axis_font'];
$axis_size = $this->parameter['axis_size'];
$axis_angle = $this->parameter['y_axis_angle'];
$axisTag = array('points' => $axis_size, 'angle' => $axis_angle, 'font' => $axis_font, 'colour' => $axis_colour);
if ($this->calculated['y_axis_left']['has_data']) {
// LEFT HAND SIDE
// left and right coords for ticks
if ($this->parameter['tick_length'] >= 0) {
$tickLeft = $this->calculated['boundary_box']['left'];
$tickRight = $this->calculated['boundary_box']['left'] + $this->parameter['tick_length'];
} else {
$tickLeft = $this->calculated['boundary_box']['left'] + $this->parameter['tick_length'];
$tickRight = $this->calculated['boundary_box']['left'];
}
$textRight = $tickLeft - $this->calculated['left_inner_padding'];
if ($axis_angle == 0) $reference = 'right-center';
if ($axis_angle > 0) $reference = 'right-top';
if ($axis_angle < 0) $reference = 'right-bottom';
if ($axis_angle == 90) $reference = 'right-center';
foreach ($this->calculated['y_axis']['tick_y'] as $set => $tickY) {
// draw y grid if colour specified
if ($yGrid != 'none') {
switch ($yGrid) {
case 'line':
ImageLine($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour);
break;
case 'dash':
ImageDashedLine($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour);
break;
}
}
// y axis text
if ($this->parameter['y_axis_text_left'] && !($set % $this->parameter['y_axis_text_left'])) { // test if tick should be displayed
// draw tick
if ($tickColour != 'none')
ImageLine($this->image, round($tickLeft), round($tickY), round($tickRight), round($tickY), $tickColour);
// draw axis text...
$coords = array('x' => $textRight, 'y' => $tickY, 'reference' => $reference);
$axisTag['text'] = $this->calculated['y_axis_left']['text'][$set];
$axisTag['boundary_box'] = $this->calculated['y_axis_left']['boundary_box'][$set];
$this->update_boundaryBox($axisTag['boundary_box'], $coords);
$this->print_TTF($axisTag);
}
}
}
if ($this->calculated['y_axis_right']['has_data']) {
// RIGHT HAND SIDE
// left and right coords for ticks
if ($this->parameter['tick_length'] >= 0) {
$tickLeft = $this->calculated['boundary_box']['right'] - $this->parameter['tick_length'];
$tickRight = $this->calculated['boundary_box']['right'];
} else {
$tickLeft = $this->calculated['boundary_box']['right'];
$tickRight = $this->calculated['boundary_box']['right'] - $this->parameter['tick_length'];
}
$textLeft = $tickRight+ $this->calculated['left_inner_padding'];
if ($axis_angle == 0) $reference = 'left-center';
if ($axis_angle > 0) $reference = 'left-bottom';
if ($axis_angle < 0) $reference = 'left-top';
if ($axis_angle == 90) $reference = 'left-center';
foreach ($this->calculated['y_axis']['tick_y'] as $set => $tickY) {
if (!$this->calculated['y_axis_left']['has_data'] && $yGrid != 'none') { // draw grid if not drawn already (above)
switch ($yGrid) {
case 'line':
ImageLine($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour);
break;
case 'dash':
ImageDashedLine($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour);
break;
}
}
if ($this->parameter['y_axis_text_right'] && !($set % $this->parameter['y_axis_text_right'])) { // test if tick should be displayed
// draw tick
if ($tickColour != 'none')
ImageLine($this->image, round($tickLeft), round($tickY), round($tickRight), round($tickY), $tickColour);
// draw axis text...
$coords = array('x' => $textLeft, 'y' => $tickY, 'reference' => $reference);
$axisTag['text'] = $this->calculated['y_axis_right']['text'][$set];
$axisTag['boundary_box'] = $this->calculated['y_axis_left']['boundary_box'][$set];
$this->update_boundaryBox($axisTag['boundary_box'], $coords);
$this->print_TTF($axisTag);
}
}
}
}
function init_data() {
$this->calculated['y_plot'] = array(); // array to hold pixel plotting coords for y axis
$height = $this->calculated['boundary_box']['bottom'] - $this->calculated['boundary_box']['top'];
$width = $this->calculated['boundary_box']['right'] - $this->calculated['boundary_box']['left'];
// calculate pixel steps between axis ticks.
$this->calculated['y_axis']['step'] = $height / ($this->parameter['y_axis_gridlines'] - 1);
// calculate x ticks spacing taking into account x offset for ticks.
$extraTick = 2 * $this->parameter['x_offset']; // extra tick to account for padding
$numTicks = $this->calculated['x_axis']['num_ticks'] - 1; // number of x ticks
// Hack by rodger to avoid division by zero, see bug 1231
if ($numTicks==0) $numTicks=1;
$this->calculated['x_axis']['step'] = $width / ($numTicks + $extraTick);
$widthPlot = $width - ($this->calculated['x_axis']['step'] * $extraTick);
$this->calculated['x_axis']['step'] = $widthPlot / $numTicks;
//calculate factor for transforming x,y physical coords to logical coords for right hand y_axis.
$y_range = $this->calculated['y_axis_right']['max'] - $this->calculated['y_axis_right']['min'];
$y_range = ($y_range ? $y_range : 1);
$this->calculated['y_axis_right']['factor'] = $height / $y_range;
//calculate factor for transforming x,y physical coords to logical coords for left hand axis.
$yRange = $this->calculated['y_axis_left']['max'] - $this->calculated['y_axis_left']['min'];
$yRange = ($yRange ? $yRange : 1);
$this->calculated['y_axis_left']['factor'] = $height / $yRange;
if ($this->parameter['x_axis_gridlines'] != 'auto') {
$xRange = $this->calculated['x_axis']['max'] - $this->calculated['x_axis']['min'];
$xRange = ($xRange ? $xRange : 1);
$this->calculated['x_axis']['factor'] = $widthPlot / $xRange;
}
//expand_pre($this->calculated['boundary_box']);
// cycle thru all data sets...
$this->calculated['num_bars'] = 0;
foreach ($this->y_order as $order => $set) {
// determine how many bars there are
if (isset($this->y_format[$set]['bar']) && ($this->y_format[$set]['bar'] != 'none')) {
$this->calculated['bar_offset_index'][$set] = $this->calculated['num_bars']; // index to relate bar with data set.
$this->calculated['num_bars']++;
}
// calculate y coords for plotting data
foreach ($this->x_data as $index => $x) {
$this->calculated['y_plot'][$set][$index] = $this->y_data[$set][$index];
if ((string)$this->y_data[$set][$index] != 'none') {
if (isset($this->y_format[$set]['y_axis']) && $this->y_format[$set]['y_axis'] == 'right') {
$this->calculated['y_plot'][$set][$index] =
round(($this->y_data[$set][$index] - $this->calculated['y_axis_right']['min'])
* $this->calculated['y_axis_right']['factor']);
} else {
//print "$set $index<br />";
$this->calculated['y_plot'][$set][$index] =
round(($this->y_data[$set][$index] - $this->calculated['y_axis_left']['min'])
* $this->calculated['y_axis_left']['factor']);
}
}
}
}
//print "factor ".$this->calculated['x_axis']['factor']."<br />";
//expand_pre($this->calculated['x_plot']);
// calculate bar parameters if bars are to be drawn.
if ($this->calculated['num_bars']) {
$xStep = $this->calculated['x_axis']['step'];
$totalWidth = $this->calculated['x_axis']['step'] - $this->parameter['bar_spacing'];
$barWidth = $totalWidth / $this->calculated['num_bars'];
$barX = ($barWidth - $totalWidth) / 2; // starting x offset
for ($i=0; $i < $this->calculated['num_bars']; $i++) {
$this->calculated['bar_offset_x'][$i] = $barX;
$barX += $barWidth; // add width of bar to x offset.
}
$this->calculated['bar_width'] = $barWidth;
}
}
function init_x_ticks() {
// get coords for x axis ticks and data plots
//$xGrid = $this->parameter['x_grid'];
$xStep = $this->calculated['x_axis']['step'];
$ticksOffset = $this->parameter['x_offset']; // where to start drawing ticks relative to y axis.
$gridLeft = $this->calculated['boundary_box']['left'] + ($xStep * $ticksOffset); // grid x start
$tickX = $gridLeft; // tick x coord
foreach ($this->calculated['x_axis']['text'] as $set => $value) {
//print "index: $set<br />";
// x tick value
$this->calculated['x_axis']['tick_x'][$set] = $tickX;
// if num ticks is auto then x plot value is same as x tick
if ($this->parameter['x_axis_gridlines'] == 'auto') $this->calculated['x_plot'][$set] = round($tickX);
//print $this->calculated['x_plot'][$set].'<br />';
$tickX += $xStep;
}
//print "xStep: $xStep <br />";
// if numeric x axis then calculate x coords for each data point. this is seperate from x ticks.
$gridX = $gridLeft;
if (empty($this->calculated['x_axis']['factor'])) {
$this->calculated['x_axis']['factor'] = 0;
}
if (empty($this->calculated['x_axis']['min'])) {
$this->calculated['x_axis']['min'] = 0;
}
$factor = $this->calculated['x_axis']['factor'];
$min = $this->calculated['x_axis']['min'];
if ($this->parameter['x_axis_gridlines'] != 'auto') {
foreach ($this->x_data as $index => $x) {
//print "index: $index, x: $x<br />";
$offset = $x - $this->calculated['x_axis']['min'];
//$gridX = ($offset * $this->calculated['x_axis']['factor']);
//print "offset: $offset <br />";
//$this->calculated['x_plot'][$set] = $gridLeft + ($offset * $this->calculated['x_axis']['factor']);
$this->calculated['x_plot'][$index] = $gridLeft + ($x - $min) * $factor;
//print $this->calculated['x_plot'][$set].'<br />';
}
}
//expand_pre($this->calculated['boundary_box']);
//print "factor ".$this->calculated['x_axis']['factor']."<br />";
//expand_pre($this->calculated['x_plot']);
}
function init_y_ticks() {
// get coords for y axis ticks
$yStep = $this->calculated['y_axis']['step'];
$gridBottom = $this->calculated['boundary_box']['bottom'];
$tickY = $gridBottom; // tick y coord
for ($i = 0; $i < $this->parameter['y_axis_gridlines']; $i++) {
$this->calculated['y_axis']['tick_y'][$i] = $tickY;
$tickY -= $yStep;
}
}
function init_labels() {
if ($this->parameter['title']) {
$size = $this->get_boundaryBox(
array('points' => $this->parameter['title_size'],
'angle' => 0,
'font' => $this->parameter['title_font'],
'text' => $this->parameter['title']));
$this->calculated['title']['boundary_box'] = $size;
$this->calculated['title']['text'] = $this->parameter['title'];
$this->calculated['title']['font'] = $this->parameter['title_font'];
$this->calculated['title']['points'] = $this->parameter['title_size'];
$this->calculated['title']['colour'] = $this->parameter['title_colour'];
$this->calculated['title']['angle'] = 0;
$this->calculated['boundary_box']['top'] += $size['height'] + $this->parameter['outer_padding'];
//$this->calculated['boundary_box']['top'] += $size['height'];
} else $this->calculated['title']['boundary_box'] = $this->get_null_size();
if ($this->parameter['y_label_left']) {
$this->calculated['y_label_left']['text'] = $this->parameter['y_label_left'];
$this->calculated['y_label_left']['angle'] = $this->parameter['y_label_angle'];
$this->calculated['y_label_left']['font'] = $this->parameter['label_font'];
$this->calculated['y_label_left']['points'] = $this->parameter['label_size'];
$this->calculated['y_label_left']['colour'] = $this->parameter['label_colour'];
$size = $this->get_boundaryBox($this->calculated['y_label_left']);
$this->calculated['y_label_left']['boundary_box'] = $size;
//$this->calculated['boundary_box']['left'] += $size['width'] + $this->parameter['inner_padding'];
$this->calculated['boundary_box']['left'] += $size['width'];
} else $this->calculated['y_label_left']['boundary_box'] = $this->get_null_size();
if ($this->parameter['y_label_right']) {
$this->calculated['y_label_right']['text'] = $this->parameter['y_label_right'];
$this->calculated['y_label_right']['angle'] = $this->parameter['y_label_angle'];
$this->calculated['y_label_right']['font'] = $this->parameter['label_font'];
$this->calculated['y_label_right']['points'] = $this->parameter['label_size'];
$this->calculated['y_label_right']['colour'] = $this->parameter['label_colour'];
$size = $this->get_boundaryBox($this->calculated['y_label_right']);
$this->calculated['y_label_right']['boundary_box'] = $size;
//$this->calculated['boundary_box']['right'] -= $size['width'] + $this->parameter['inner_padding'];
$this->calculated['boundary_box']['right'] -= $size['width'];
} else $this->calculated['y_label_right']['boundary_box'] = $this->get_null_size();
if ($this->parameter['x_label']) {
$this->calculated['x_label']['text'] = $this->parameter['x_label'];
$this->calculated['x_label']['angle'] = $this->parameter['x_label_angle'];
$this->calculated['x_label']['font'] = $this->parameter['label_font'];
$this->calculated['x_label']['points'] = $this->parameter['label_size'];
$this->calculated['x_label']['colour'] = $this->parameter['label_colour'];
$size = $this->get_boundaryBox($this->calculated['x_label']);
$this->calculated['x_label']['boundary_box'] = $size;
//$this->calculated['boundary_box']['bottom'] -= $size['height'] + $this->parameter['inner_padding'];
$this->calculated['boundary_box']['bottom'] -= $size['height'];
} else $this->calculated['x_label']['boundary_box'] = $this->get_null_size();
}
function init_legend() {
$this->calculated['legend'] = array(); // array to hold calculated values for legend.
//$this->calculated['legend']['boundary_box_max'] = array('height' => 0, 'width' => 0);
$this->calculated['legend']['boundary_box_max'] = $this->get_null_size();
if ($this->parameter['legend'] == 'none') return;
$position = $this->parameter['legend'];
$numSets = 0; // number of data sets with legends.
$sumTextHeight = 0; // total of height of all legend text items.
$width = 0;
$height = 0;
foreach ($this->y_order as $set) {
$text = isset($this->y_format[$set]['legend']) ? $this->y_format[$set]['legend'] : 'none';
$size = $this->get_boundaryBox(
array('points' => $this->parameter['legend_size'],
'angle' => 0,
'font' => $this->parameter['legend_font'],
'text' => $text));
$this->calculated['legend']['boundary_box'][$set] = $size;
$this->calculated['legend']['text'][$set] = $text;
//$this->calculated['legend']['font'][$set] = $this->parameter['legend_font'];
//$this->calculated['legend']['points'][$set] = $this->parameter['legend_size'];
//$this->calculated['legend']['angle'][$set] = 0;
if ($text && $text!='none') {
$numSets++;
$sumTextHeight += $size['height'];
}
if ($size['width'] > $this->calculated['legend']['boundary_box_max']['width'])
$this->calculated['legend']['boundary_box_max'] = $size;
}
$offset = $this->parameter['legend_offset']; // offset in pixels of legend box from graph border.
$padding = $this->parameter['legend_padding']; // padding in pixels around legend text.
$textWidth = $this->calculated['legend']['boundary_box_max']['width']; // width of largest legend item.
$textHeight = $this->calculated['legend']['boundary_box_max']['height']; // use height as size to use for colour square in legend.
$width = $padding * 2 + $textWidth + $textHeight * 2; // left and right padding + maximum text width + space for square
$height = ($padding + $textHeight) * $numSets + $padding; // top and bottom padding + padding between text + text.
$this->calculated['legend']['boundary_box_all'] = array('width' => $width,
'height' => $height,
'offset' => $offset,
'reference' => $position);
switch ($position) { // move in right or bottom if legend is outside data plotting area.
case 'outside-top' :
$this->calculated['boundary_box']['right'] -= $offset + $width; // move in right hand side
break;
case 'outside-bottom' :
$this->calculated['boundary_box']['right'] -= $offset + $width; // move in right hand side
break;
case 'outside-left' :
$this->calculated['boundary_box']['bottom'] -= $offset + $height; // move in right hand side
break;
case 'outside-right' :
$this->calculated['boundary_box']['bottom'] -= $offset + $height; // move in right hand side
break;
}
}
function init_y_axis() {
$this->calculated['y_axis_left'] = array(); // array to hold calculated values for y_axis on left.
$this->calculated['y_axis_left']['boundary_box_max'] = $this->get_null_size();
$this->calculated['y_axis_right'] = array(); // array to hold calculated values for y_axis on right.
$this->calculated['y_axis_right']['boundary_box_max'] = $this->get_null_size();
$axis_font = $this->parameter['axis_font'];
$axis_size = $this->parameter['axis_size'];
$axis_colour = $this->parameter['axis_colour'];
$axis_angle = $this->parameter['y_axis_angle'];
$y_tick_labels = $this->y_tick_labels;
$this->calculated['y_axis_left']['has_data'] = FALSE;
$this->calculated['y_axis_right']['has_data'] = FALSE;
// find min and max y values.
$minLeft = $this->parameter['y_min_left'];
$maxLeft = $this->parameter['y_max_left'];
$minRight = $this->parameter['y_min_right'];
$maxRight = $this->parameter['y_max_right'];
$dataLeft = array();
$dataRight = array();
foreach ($this->y_order as $order => $set) {