-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpChart4mw.class.php
executable file
·1442 lines (1212 loc) · 47.4 KB
/
pChart4mw.class.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
class pChart4mw {
// **************************************************************************************
//
// Properties used for a pChart
//
// **************************************************************************************
// Unique filename for the chart that is generated
var $filename = null;
// pChart object containing the chart itself
var $pChart = null;
// Array with the data for generating the chart
// See also: http://pchart.sourceforge.net/documentation.php?topic=datastructure
var $pData = array();
// Array with the data descriptions for generating the chart
// See also: http://pchart.sourceforge.net/documentation.php?topic=datastructure
var $pDataDescription = array();
// Size and location of the graph area within the whole image. These values are
// computed in the setGraphArea method
var $graphArea = array();
// Width of the Y labels and height of the X labels. These values are used to compute
// the grapharea
var $widthYLabel = null;
var $heightXLabel = null;
// Type of the chart. This field is filled in subclasses.
var $type = "";
// Flag that is set when the chart contains no data.
var $empty = false;
// **************************************************************************************
//
// Methods for parsing data and analyzing the set parameters
//
// **************************************************************************************
/**
* Parses the data from the charts
*
* @param $rawData String Raw user-given data for the graph
* @returns pData pData object containing the entered data
*/
public function parseData( $rawData ) {
// Create empty data object
$pData = $this->pData;
$pDataDescription = $this->pDataDescription;
$pDataDescription[ "Position" ] = "Name";
$pDataDescription[ "Values" ] = array();
$pDataDescription[ "Description" ] = array();
// If the data is empty, no processing has to be done
if( trim( $rawData ) == "" ) {
$this->empty = true;
$this->pData = $pData;
$this->pDataDescription = $pDataDescription;
return $pData;
}
// Parse the raw data. This raw data is in CSV format, with lines separated by a \n
$lines = explode( "\n", $rawData );
$serieNames = array();
$xlabels = false;
// Remove empty lines from the array and split it into elements. Also count the number
// of rows and columns
$csvData = array();
$numberColumns = 0;
$numberRows = 0;
foreach( $lines as $line ) {
// Remove leading and trailing spaces
$line = trim( $line );
if( $line != "" ) {
$csvData[] = explode( ",", $line );
// Increase the number of rows
$numberRows++;
// Take the maximum number of columns in a row as the grand number of columns
$numberColumns = max( $numberColumns, count( $csvData[ $numberRows - 1 ] ) );
}
}
// Check whether there are labels for columns or for rows
$xlabels = false;
$ylabels = false;
// To do that, we check whether there are non-numeric values on the first row or column
$A1 = false; // Flag whether the contents of cell A1 is non-numeric
$c1 = false; // Flag whether the contents of column 1 (without A1) is non-numeric
$r1 = false; // Flag whether the contents of row 1 (without A1) is non-numeric
// We check three different positions for their contents:
// A1: top left cell
// R1: top row, except for the left cell
// C1: left column, except for the top cell
//
$A1 = !is_numeric( trim( $csvData[ 0 ][ 0 ] ) );
for( $i = 0; $i < $numberRows; $i++ ) {
if( $i == 0 ) {
if( $numberColumns > 1 ) {
for( $j = 1; $j < $numberColumns; $j++ ) {
if( count( $csvData[ $i ] ) > $j && !is_numeric( $csvData[ $i ][ $j ] ) ) {
$r1 = true;
break;
}
}
}
} else {
if( !is_numeric( $csvData[ $i ][ 0 ] ) ) {
$c1 = true;
break;
}
}
}
// Now, we have several cases:
// A1 = empty, row 1 and column 1 contain non-numerics: both x and y labels
// A1 or row 1 contains non-numerics, column 1 does not: only y labels
// A1 or column 1 contains non-numerics, row 1 does not: only x labels
// Both A1, column 1 and row 1 do not contain non-numerics: no labels at all
if( $r1 && $c1 ) {
$xlabels = true;
$ylabels = true;
// Adjust the number of data columns and rows
$numberColumns--;
$numberRows--;
} elseif( ( $r1 && !$c1 ) || ( $A1 && $numberColumns == 1 ) ) {
$xlabels = false;
$ylabels = true;
// Adjust the number of data rows
$numberRows--;
} elseif( ( $c1 && !$r1 ) || ( $A1 && $numberRows == 1 ) ) {
$xlabels = true;
$ylabels = false;
// Adjust the number of data columns
$numberColumns--;
} elseif( !$A1 && !$r1 && !$c1 ) {
$xlabels = false;
$ylabels = false;
}
// The automatic detection of labels can be overridden by the user
//
if( $this->chartArgs[ "xlabels" ] && !$xlabels ) {
$xlabels = true;
$numberColumns--;
}
if( $this->chartArgs[ "ylabels" ] && !$ylabels ) {
$ylabels = true;
$numberRows--;
}
// Check whether the first line contains labels. This is true when one or more of the
// elements on the line is non-numeric
$serieNames = array();
if( $ylabels ) {
// Take the first row as Serienames
$serieNames = array_shift( $csvData );
// Remove the first entry if xlabels exist
// That entry is empty or otherwise has no meaning
if( $xlabels ) {
array_shift( $serieNames );
}
}
// Match the number of serie names with the number of series
$serieNames = array_pad( $serieNames, $numberColumns, "" );
// Check whether any data is entered. If not, no further action has to be done
if( $numberRows == 0 || $numberColumns == 0 ) {
$this->pData = $pData;
$this->pDataDescription = $pDataDescription;
$this->empty = true;
return $pData;
}
// Create the pData array. This array has 1 element per row
// That element contains a associative array, like this;
//
// Array
// (
// [0] => Array
// (
// [Name] => January
// [Serie1] => 0
// [Serie2] => 2
// )
// [1] => Array
// (
// [Name] => February
// [Serie1] => 1
// [Serie2] => 4
// )
// )
// First create an array with keynames
$keyNames = array( "Name" );
for( $i = 0; $i < $numberColumns; $i++ ) {
$keyNames[] = "Serie" . strval( $i + 1 );
}
// Save all lines to the pData structure
for( $i = 0; $i < $numberRows; $i++ ) {
$data = $csvData[ $i ];
// If the xlabels are not set, determine the xlabels automatically
if( !$xlabels ) {
array_unshift( $data, $i + 1 );
}
// Make sure there are enough entries on a line
$data = array_pad( $data, $numberColumns + 1, "" );
// Save the data with the serienames as keys and values as data.
$pData[ $i ] = array_combine( $keyNames, $data );
}
// Create the pDataStructure array
// This array has the structure like:
//
// Array
// (
// [Position] => Name
// [Values] => Array
// (
// [0] => Serie1
// [1] => Serie2
// )
// [Description] => Array
// (
// [Serie1] => Year 2007
// [Serie2] => Year 2008
// )
// )
// Set names and descriptions for the series into the array
for( $i = 0; $i < $numberColumns; $i++ ) {
$name = "Serie" . strval( $i + 1 );
$pDataDescription[ "Values" ][ $i ] = $name;
$pDataDescription[ "Description" ][ $name ] = $serieNames[ $i ];
}
// Set the format, unit and axis names
$pDataDescription[ "Axis" ] = array( "X" => $this->chartArgs[ "xtitle" ], "Y" => $this->chartArgs[ "ytitle" ] );
$pDataDescription[ "Format" ] = array( "X" => $this->chartArgs[ "xformat" ], "Y" => $this->chartArgs[ "yformat" ] );
$pDataDescription[ "Unit" ] = array( "X" => $this->chartArgs[ "xunit" ], "Y" => $this->chartArgs[ "yunit" ] );
// Save the data into the object
$this->pData = $pData;
$this->pDataDescription = $pDataDescription;
return $this->pData;
}
/**
* Parses the parameters for the chart and sets them to the pChart object
*
* @param $args Array Associative array with arguments given by the user
* @param $default Array Default parameters (optional)
* @returns pChart pChart object with parameters set
*/
public function parseArgs( $args, $default = false ) {
// Initialize default arguments
if( !$default ) {
$chartArgs = $this->getDefaultArgs();
} else {
$chartArgs = $default;
}
// Check whether several TITLE parameters are set by the user
if( array_key_exists( "title", $args ) ) {
$chartArgs[ "title" ] = $args[ "title" ];
}
if( array_key_exists( "titlefont", $args ) && preg_match( '/\/|\\|\.\./', $args[ "titlefont" ] ) == 0) {
$chartArgs[ "titlefont" ] = $args[ "titlefont" ];
}
if( array_key_exists( "titlesize", $args ) ) {
$chartArgs[ "titlesize" ] = $args[ "titlesize" ];
}
// If the color is not correctly specified (HTML-style), the default color is used
if( array_key_exists( "titlecolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "titlecolor" ] );
if( $cArray ) {
$chartArgs[ "titlecolor" ] = $cArray;
}
}
// Check whether several TEXT parameters are set by the user
if( array_key_exists( "textfont", $args ) && preg_match( '/\/|\\|\.\./', $args[ "textfont" ] ) == 0) {
$chartArgs[ "textfont" ] = $args[ "textfont" ];
}
if( array_key_exists( "textsize", $args ) ) {
$chartArgs[ "textsize" ] = $args[ "textsize" ];
}
// Determine the size of the chart
if( array_key_exists( "size", $args ) && trim( $args[ "size" ] ) != "" ) {
// Size can be set as one number, or 00x00 for width x height
$sizes = explode( "x", strtolower( $args[ "size" ] ) );
// If only one number is given, it is taken as the height and width
// so the chart will be a square
if( count( $sizes ) == 1 && is_numeric( $sizes[ 0 ] ) ) {
$chartArgs[ "sizeX" ] = $sizes[ 0 ];
$chartArgs[ "sizeY" ] = $sizes[ 0 ];
}
if( count( $sizes ) > 1 ) {
if( is_numeric( $sizes[ 0 ] ) ) {
$chartArgs[ "sizeX" ] = $sizes[ 0 ];
}
if( is_numeric( $sizes[ 1 ] ) ) {
$chartArgs[ "sizeY" ] = $sizes[ 1 ];
}
}
}
// Are the colors set? If so, set the colors into the color palette
if( array_key_exists( "colors", $args ) ) {
$colors = explode( ",", $args[ "colors" ] );
$chartArgs[ "colors" ] = array();
// Set all colors into the palette, if it is a correct RGB color
// If it is not a correct RGB color, black is used
foreach( $colors as $color ) {
$cArray = wfPChart4mwhtml2rgb( $color );
if( !$cArray ) {
$cArray = array( 0, 0, 0 );
}
$chartArgs[ "colors" ][] = $cArray;
}
}
// Has the user specified a colorscheme to be used?
if( array_key_exists( "colorscheme", $args ) ) {
global $wgPChart4mwDefaultColorSchemeDir;
$filename = $wgPChart4mwDefaultColorSchemeDir . '/' . $args[ "colorscheme" ] . '.txt';
if( file_exists( $filename ) ) {
$chartArgs[ "colorscheme" ] = $args[ "colorscheme" ];
}
}
// What color should be used for the image background
// If no correct color is specified, the default color is used
if( array_key_exists( "bgcolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "bgcolor" ] );
if( $cArray ) {
$chartArgs[ "bgcolor" ] = $cArray;
}
}
// Set the margins on left and right side
if( array_key_exists( "marginx", $args ) && is_numeric( $args[ "marginx" ] ) ) {
$chartArgs[ "marginX" ] = $args[ "marginx" ];
}
// Set the margins on top and bottom
if( array_key_exists( "marginy", $args ) && is_numeric( $args[ "marginy" ] ) ) {
$chartArgs[ "marginY" ] = $args[ "marginy" ];
}
// Should the labels be printed?
if( array_key_exists( "labels", $args ) ) {
$chartArgs[ "labels" ] = ( strtolower( $args[ "labels" ] ) != "false" );
}
// Should every label be printed, or should some be skipped
// See skiplabels parameter of pChart->drawScale()
if( array_key_exists( "skiplabels", $args ) && is_numeric( $args[ "skiplabels" ] ) ) {
$chartArgs[ "skiplabels" ] = $args[ "skiplabels" ];
}
// How many decimals should be shown on the Y-axis
// See decimals parameter of pChart->drawScale()
if( array_key_exists( "decimals", $args ) && is_numeric( $args[ "decimals" ] ) ) {
$chartArgs[ "decimals" ] = $args[ "decimals" ];
}
// Should the labels be printed with an angle?
// The angle should be between 0 and 180 degrees
if( array_key_exists( "angle", $args ) ) {
$angle = (int) $args[ "angle" ];
if( $angle >= 0 && $angle <= 180 ) {
$chartArgs[ "angle" ] = $angle;
}
}
// What color should be used for the graph background
if( array_key_exists( "axiscolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "axiscolor" ] );
if( $cArray ) {
$chartArgs[ "axiscolor" ] = $cArray;
}
}
// What color should be used for the graph background
if( array_key_exists( "axis", $args ) ) {
if( strtolower( $args[ "axis" ] ) == "false" ) {
$chartArgs[ "axiscolor" ] = $chartArgs[ "bgcolor" ];
}
}
// Show or hide the grid in the chart
if( array_key_exists( "box", $args ) ) {
$chartArgs[ "box" ] = ( $args[ "box" ] != "false" );
}
// What color should be used for the box
// If the color is not correctly specified (HTML-style), the default color is used
if( array_key_exists( "boxcolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "boxcolor" ] );
if( $cArray ) {
$chartArgs[ "boxcolor" ] = $cArray;
}
}
// Are the max and min values for the Y axis given?
if( array_key_exists( "ymax", $args ) ) {
$chartArgs[ "autoscaling" ] = false;
$chartArgs[ "ymax" ] = $args[ "ymax" ];
if( !array_key_exists( "ymin", $args ) ) {
$chartArgs[ "ymin" ] = 0;
}
}
// If the ymin value is given, save it. If it is zero, it can be used to fix the y-axis min value to zero
// other values are only used in combination with ymax
if( array_key_exists( "ymin", $args ) ) {
$chartArgs[ "ymin" ] = $args[ "ymin" ];
}
// Show or hide the grid in the chart
if( array_key_exists( "grid", $args ) ) {
$chartArgs[ "grid" ] = ( $args[ "grid" ] != "false" );
}
// What color should be used for the grid
// If the color is not correctly specified (HTML-style), the default color is used
if( array_key_exists( "gridcolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "gridcolor" ] );
if( $cArray ) {
$chartArgs[ "gridcolor" ] = $cArray;
}
}
// Use a background gradient or not
if( array_key_exists( "bgtype", $args ) ) {
if( $args[ "bgtype" ] == "gradient" ) {
$chartArgs[ "bgtype" ] = "gradient";
}
}
// What color should be used for the graph background
// If the color is not correctly specified (HTML-style), the default color is used
if( array_key_exists( "graphbgcolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "graphbgcolor" ] );
if( $cArray ) {
$chartArgs[ "graphbgcolor" ] = $cArray;
}
}
// Use a background gradient or not
if( array_key_exists( "graphbgtype", $args ) ) {
if( $args[ "graphbgtype" ] == "gradient" || $args[ "graphbgtype" ] == "normal" || $args[ "graphbgtype" ] == "transparent") {
$chartArgs[ "graphbgtype" ] = $args[ "graphbgtype" ];
}
}
// Should the legend be set?
if( array_key_exists( "legend", $args ) ) {
$chartArgs[ "legend" ] = ( $args[ "legend" ] != "none" );
$chartArgs[ "legendpos" ] = $args[ "legend" ];
}
// What color should be used for the text in the legend
// If the color is not correctly specified (HTML-style), the default color is used
if( array_key_exists( "legendcolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "legendcolor" ] );
if( $cArray ) {
$chartArgs[ "legendcolor" ] = $cArray;
}
}
// What color should be used for the legend background
// If the color is not correctly specified (HTML-style), the default color is used
if( array_key_exists( "legendbgcolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "legendbgcolor" ] );
if( $cArray ) {
$chartArgs[ "legendbgcolor" ] = $cArray;
}
}
// What color should be used for the legend border
// If the color is not correctly specified (HTML-style), the default color is used
if( array_key_exists( "legendbordercolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "legendbordercolor" ] );
if( $cArray ) {
$chartArgs[ "legendbordercolor" ] = $cArray;
}
}
// Check how transparent the bars or graph area should be
if( array_key_exists( "opacity", $args ) ) {
if( $args[ "opacity" ] >= 0 && $args[ "opacity" ] <= 100 ) {
$chartArgs[ "opacity" ] = $args[ "opacity" ];
}
}
// Title for the X-axis
if( array_key_exists( "xtitle", $args ) ) {
$chartArgs[ "xtitle" ] = $args[ "xtitle" ];
}
// Title for the Y-axis
if( array_key_exists( "ytitle", $args ) ) {
$chartArgs[ "ytitle" ] = $args[ "ytitle" ];
}
// Unit for the X-axis
if( array_key_exists( "xunit", $args ) ) {
$chartArgs[ "xunit" ] = $args[ "xunit" ];
}
// Unit for the Y-axis
if( array_key_exists( "yunit", $args ) ) {
$chartArgs[ "yunit" ] = $args[ "yunit" ];
}
// Format for values on the X-axis
if( array_key_exists( "xformat", $args ) ) {
$chartArgs[ "xformat" ] = $args[ "xformat" ];
}
// Format for values on the Y-axis
if( array_key_exists( "yformat", $args ) ) {
$chartArgs[ "yformat" ] = $args[ "yformat" ];
}
// Are there non-numeric labels in the data
if( array_key_exists( "xlabels", $args ) ) {
$chartArgs[ "xlabels" ] = ( strtolower( $args[ "xlabels" ] ) != "false" );
}
// Are there non-numeric labels in the data
if( array_key_exists( "ylabels", $args ) ) {
$chartArgs[ "ylabels" ] = ( strtolower( $args[ "ylabels" ] ) != "false" );
}
// Threshold line
if( array_key_exists( "threshold", $args ) ) {
if ( is_numeric($args[ "threshold" ]) ) {
$chartArgs[ "threshold" ] = $args[ "threshold" ];
}
}
// What color should be used for the grid
// If the color is not correctly specified (HTML-style), the default color is used
if( array_key_exists( "thcolor", $args ) ) {
$cArray = wfPChart4mwhtml2rgb( $args[ "thcolor" ] );
if( $cArray ) {
$chartArgs[ "thcolor" ] = $cArray;
}
}
// Save the arguments
$this->chartArgs = $chartArgs;
return $this->chartArgs;
}
/**
* The arguments are parsed before the data is parsed. This function updates the
* arguments based on the data structure
*/
public function updateArgsBasedOnData() {
// If the size is too small for the margins, adjust the margins.
// 60x60 seems to be the smallest graph area
if( $this->chartArgs[ "sizeX" ] < 60 + ( $this->chartArgs[ "labels" ] ? $this->getWidthYLabel() : 0 ) + 2 * $this->chartArgs[ "marginX" ] ) {
$this->chartArgs[ "sizeX" ] = 60 + ( $this->chartArgs[ "labels" ] ? $this->getWidthYLabel() : 0 ) + 2 * $this->chartArgs[ "marginX" ];
}
$titlesize = wfPChart4mwtextboxSize( $this->chartArgs[ "titlefont" ], $this->chartArgs[ "title" ], 0, $this->chartArgs[ "titlesize" ] );
if( $this->chartArgs[ "sizeY" ] < 60 + ( $this->chartArgs[ "labels" ] ? $this->getHeightXLabel() : 0 ) + ( $this->chartArgs[ "title" ] != "" ? $titlesize[ 1 ] : 0 ) + 2 * $this->chartArgs[ "marginY" ] ) {
$this->chartArgs[ "sizeY" ] = 60 + ( $this->chartArgs[ "labels" ] ? $this->getHeightXLabel() : 0 ) + ( $this->chartArgs[ "title" ] != "" ? $titlesize[ 1 ] : 0 ) + 2 * $this->chartArgs[ "marginY" ];
}
}
/**
* Returns the default properties for all graphs. This method contains all hard-coded
* defaults for all parameters. These parameters can be overridden by setting global
* variables $wgPChart4mwDefaults and $wgPChart4mw<type>Defaults in LocalSettings.php
*
* @returns Array Associative array with default properties.
*/
public function getDefaultArgs() {
$args = array(
// Default chart size in pixels
"sizeX" => 500,
"sizeY" => 300,
// Default margin in pixels
"marginX" => 10,
"marginY" => 10,
// Fontsize used to write text into the chart (labels and legend)
"textsize" => 8,
"textfont" => "tahoma.ttf",
// Default title, color for the title and font size
"title" => "",
"titlecolor" => array( 119, 119, 119 ),
"titlesize" => 10,
"titlefont" => "tahoma.ttf",
// Default title for the axes, unit (shown after the number, e.g. 150kg) and format (number, time, date)
"xtitle" => "",
"ytitle" => "",
"xunit" => "",
"yunit" => "",
"xformat" => "number",
"yformat" => "number",
// Whether the data section contains a row or column with labels. If these values are non-numeric,
// the script automatically detects these labels. If the values are all numeric, the script has no
// method of distinguishing the labels from the data. In that case, these parameters should be set
// in the wiki-tag.
"xlabels" => false,
"ylabels" => false,
// Whether the labels should be printed or not.
"labels" => true,
// Should any labels be skipped?
"skiplabels" => 1,
// How many decimals should be shown on the Y-axis?
"decimals" => 0,
"angle" => 0,
"axis" => true,
"axiscolor" => array( 119, 119, 119 ), // #777777
"autoscaling" => true,
"ymin" => -1,
"ymax" => -1,
"box" => false,
"boxcolor" => array( 227, 227, 227 ),
"grid" => false,
"gridcolor" => array( 227, 227, 227 ),
"gridlinesize" => 5,
"gridalpha" => 50,
"legend" => false,
"legendpos" => "right",
"legendcolor" => array( 119, 119, 119 ),
"legendbgcolor" => array( 255, 255, 255 ),
"legendbordercolor" => array( 119, 119, 119 ),
"colors" => array(),
"colorscheme" => "accent",
"bgcolor" => array( 255, 255, 255 ),
"bgtype" => "normal",
"graphbgcolor" => array( 255, 255, 255 ),
"graphbgtype" => "transparent",
"stacked" => false,
"opacity" => 100,
"threshold" => false,
"thcolor" => array( 227, 227, 227 )
);
// Check whether the user has set defaults in the LocalSettings.php file
if( array_key_exists( "wgPChart4mwDefaults", $GLOBALS ) ) {
$args = $this->parseArgs( $GLOBALS[ "wgPChart4mwDefaults" ], $args );
}
return $args;
}
// **************************************************************************************
//
// High-level methods for generating the chart and showing it to the user
//
// **************************************************************************************
/**
* Creates the chart with the given parameters
*
* @param $input String Input between the <pBar> and </pBar> tags or null is the tag is closed (<pBar />)
* @param $args Array Tag arguments, which are entered like HTML tag attributes; this is an associative
* array indexed by attribute name
* @return String HTML code to show the chart
*/
public function renderChart( $input, $args ) {
global $wgPChart4mwCacheEnabled, $wgPChart4mwWebservice;
// If the chart must be rendered on an external location
// call the method 'callWebservice'
if( $wgPChart4mwWebservice != "" ) {
return $this->callWebservice( $input, $args );
}
// Create a unique filename for this chart
$this->setUniqueFileName( $this->type, $input, $args );
// Check whether the file exists in cache
if( $wgPChart4mwCacheEnabled && $this->existsInCache() ) {
return $this->htmlCode();
}
// Parse arguments and data
$this->parseArgs( $args );
$this->parseData( $input );
$this->updateArgsBasedOnData();
// Draw the graph
$this->drawChart();
// Save chart
$this->saveChart();
// Return htmlCode for this chart
return $this->htmlCode();
}
/**
* Creates the chart with the given parameters and outputs it to the screen
*
* @param $input String Input between the <pBar> and </pBar> tags or null is the tag is closed (<pBar />)
* @param $args Array Tag arguments, which are entered like HTML tag attributes; this is an associative
* array indexed by attribute name
* @return void
*/
public function showChart( $input, $args ) {
global $wgPChart4mwCacheEnabled, $wgPChart4mwWebservice;
global $wgPChart4mwImageFormat;
// Create a unique filename for this chart
$this->setUniqueFileName( $this->type, $input, $args );
// Check whether the file exists in cache
if( $wgPChart4mwCacheEnabled && $this->existsInCache() ) {
header( "Content-type: image/" . $wgPChart4mwImageFormat );
readfile( $this->getAbsoluteUploadDir() . DIRECTORY_SEPARATOR . $this->filename );
}
// Parse arguments and data
$this->parseData( $input );
$this->parseArgs( $args );
$this->updateDataBasedOnArgs();
// Draw the graph
$this->drawChart();
if( $wgPChart4mwCacheEnabled ) {
// Save chart
$this->saveChart();
// Output the contents
header( "Content-type: image/" . $wgPChart4mwImageFormat );
readfile( $this->getAbsoluteUploadDir() . DIRECTORY_SEPARATOR . $this->filename );
} else {
// Output the contents of the chart directly to the screen
$this->pChart->stroke();
}
}
/**
* Calls a webservice to create the charts. The webservice is determined in the configuration
*
* @param $input String Input between the <pBar> and </pBar> tags or null is the tag is closed (<pBar />)
* @param $args Array Tag arguments, which are entered like HTML tag attributes; this is an associative
* array indexed by attribute name
* @return String HTML code to show the chart
*/
public function callWebservice( $input, $args ) {
global $wgPChart4mwWebservice;
// Put the data into a string. The newlines are converted to literal '|'
$_data = str_replace( "\n", '|', $input );
// Create an array with parameters
$params = $args;
$params[ "_data" ] = $_data;
$params[ "_type" ] = $this->type;
// Create a sound URL
$url = $wgPChart4mwWebservice . "?" . http_build_query( $params );
// Return correct HTML code
return $this->htmlCode( $url );
}
/**
* Returns the HTML code to show this graph on screen
*
* @param @imgURL String URL of the generated chart
* @return String HTML code to show this graph on screen in a wiki page
*/
public function htmlCode( $imgURL = "" ) {
// If no image url is given, an url to the saved image is used.
if( $imgURL == "" ) {
$imgURL = htmlspecialchars($this->getUploadDir() . "/" . $this->filename );
}
return '<p><b><img src="' . $imgURL . '" alt="pChart" /></b></p>';
}
// **************************************************************************************
//
// More detailed methods for generating the chart. The generation of a chart is divided
// into three steps:
//
// 1. Initialization: determine size of graph area and plot axes and grid
// 2. Show the graph itself
// 3. Finishing chart: printing titles and plotting legend
//
// **************************************************************************************
/**
* Draws the chart, based on the type of chart
*/
public function drawChart() {
// Initialize the chart with the given parameters
$this->initializeChart();
// If no data is entered, the chart can be returned without an further actions
if( $this->empty ) {
return;
}
// Draw the chart of a specific type.
$this->drawChartSpecific();
// Finish the chart with legend and title etc.
$this->finishChart();
}
/**
* Initializes the chart, sets the properties
*/
public function initializeChart() {
global $wgPChart4mwFontPath;
// Retrieve the parameters for the chart
$args = $this->chartArgs;
// Create a chart object
$pChart = new pChart( $args["sizeX"], $args[ "sizeY" ] );
// Draw background colors. If needed, a gradient should be drawn
if( $args[ "bgtype" ] == "normal" ) {
$pChart->drawFilledRectangle( 0, 0, $args[ "sizeX" ], $args[ "sizeY" ], $args[ "bgcolor" ][ 0 ], $args[ "bgcolor" ][ 1 ], $args[ "bgcolor" ][ 2 ] );
} else {
$pChart->drawGraphAreaGradient( $args[ "bgcolor" ][ 0 ], $args[ "bgcolor" ][ 1 ], $args[ "bgcolor" ][ 2 ], 50, TARGET_BACKGROUND );
}
// Set default font properties
$pChart->setFontProperties( $wgPChart4mwFontPath . "/". $args[ "textfont" ], $args[ "textsize" ] );
// Define the graph area, by computing the margins, legend size, title size etc.
$pChart = $this->setGraphArea($pChart);
if( $args[ "graphbgtype" ] == "normal" ) {
$pChart->drawGraphArea( $args[ "graphbgcolor" ][ 0 ], $args[ "graphbgcolor" ][ 1 ], $args[ "graphbgcolor" ][ 2 ] );
} elseif( $args[ "graphbgtype" ] == "gradient" ) {
$pChart->drawGraphAreaGradient( $args[ "graphbgcolor" ][ 0 ], $args[ "graphbgcolor" ][ 1 ], $args[ "graphbgcolor" ][ 2 ], 50 );
}
$this->pChart = $pChart;
if( !$this->empty ) {
$this->drawScaleAndGrid();
$this->setColorPalette();
}
return $this->pChart;
}
/**
* Sets the boundaries for the graph area, taking into account the margins and legend size and position
*
* @param $pChart pChart The pChart object to set the graph area for
* @return pChart The updated pChart object
*/
public function setGraphArea( $pChart ) {
// Get all parameters
$args = $this->chartArgs;
// We have to take into account: label size, legend size and position and title size
// Title size and position is fixed: top, height is specified in arguments
// Without a legend and labels we only have to worry about the margins and textsize.
$top = $args[ "marginY" ] ;
$left = $args[ "marginX" ];
$bottom = $args[ "sizeY" ] - $args[ "marginY" ];
$right = $args[ "sizeX" ] - $args[ "marginX" ];
// Add the title size if it is used
if( $args[ "title" ] != "" ) {
$size = wfPChart4mwtextboxSize( $args[ "titlefont" ], $args[ "title" ], 0, $args[ "titlesize" ] );
$top += $size[ 1 ] + $args[ "marginY" ];
}
if( $args[ "xtitle" ] != "" ) {
$size = wfPChart4mwtextboxSize( $args[ "textfont" ], $args[ "xtitle" ], 0, $args[ "textsize" ] );
$bottom -= ( $size[ 1 ] );
}
if( $args[ "ytitle" ] != "" ) {
$size = wfPChart4mwtextboxSize( $args[ "textfont" ], $args[ "ytitle" ], 90, $args[ "textsize" ] );
$left += $size[ 0 ] + $args[ "marginX" ];
}
// If labels are shown, take their size into account
if( $args[ "labels" ] ) {
$left += $this->getWidthYLabel() + $args[ "marginX" ];
$bottom -= ( $this->getHeightXLabel() + $args[ "marginY" ] );
}
// If the legend is shown, we have to add some to the margins
if( $args[ "legend" ] ) {
// Determine the size of the legend box
$legendsize = $this->getLegendBoxSize( $pChart, $this->pDataDescription );
$legendmargin = 10;
// Determine what margin has to be increased
switch( $args[ "legendpos" ] ) {
case "top":
$top += $legendsize[ 1 ] + $legendmargin;
break;
case "bottom":
$bottom -= ( $legendsize[ 1 ] + $legendmargin );
break;
case "left":
$left += $legendsize[ 0 ] + $legendmargin;
break;
case "right":
default:
$right -= ( $legendsize[ 0 ] + $legendmargin );
break;
}
}
// Actually set the graph area
$pChart->setGraphArea( $left, $top, $right, $bottom );
$this->graphArea = array( $left, $top, $right, $bottom );
return $pChart;
}
/**
* Set the scale to the right values and draws the axes and grid
*/
public function drawScaleAndGrid() {
$args = $this->chartArgs;
// Set scale, depending on the user preferences
if( !$args[ "autoscaling" ] ) {
$this->pChart->setFixedScale( $args[ "ymin" ], $args[ "ymax" ] );
}
// Determine type of scaling
if( $args[ "autoscaling" ] && $args[ "ymin" ] == 0 ) {
$scaleType = $args[ "stacked" ] ? SCALE_ADDALLSTART0 : SCALE_START0;
} else {
$scaleType = $args[ "stacked" ] ? SCALE_ADDALL : SCALE_NORMAL;
}
// If needed, draw a box around the graph Area
if( $args[ "box" ] ) {
$this->pChart->drawRectangle(
$this->graphArea[ 0 ], $this->graphArea[ 1 ], $this->graphArea[ 2 ], $this->graphArea[ 3 ],
$args[ "boxcolor" ][ 0 ], $args[ "boxcolor" ][ 1 ], $args[ "boxcolor" ][ 2 ]