-
Notifications
You must be signed in to change notification settings - Fork 0
/
phplot.php
9204 lines (8416 loc) · 392 KB
/
phplot.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
/**
* PHPlot - A class for creating scientific and business graphs, charts, plots
*
* This file contains two PHP classes which are used to create graphs,
* charts, and plots. The PHPlot class is the basic class which creates
* indexed-color images, and the extended PHPlot_truecolor class creates
* full-color (24-bit) images.
* PHPlot currently requires PHP 5.3 or later.
*
* $Id: phplot.php 1774 2015-11-03 00:18:50Z lbayuk $
*
* @version 6.2.0
* @copyright 1998-2015 Afan Ottenheimer
* @license GNU Lesser General Public License, version 2.1
* @link http://sourceforge.net/projects/phplot/ PHPlot Web Site with downloads, tracker, discussion
* @link http://phplot.sourceforge.net PHPlot Project Web Site with links to documentation
* @author lbayuk (2006-present) <[email protected]>
* @author Miguel de Benito Delgado (co-author and maintainer, 2003-2005)
* @author Afan Ottenheimer (original author)
*/
/*
* This 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;
* version 2.1 of the License.
*
* This software 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 software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ---------------------------------------------------------------------
*/
/**
* Class for creating a plot
*
* The PHPlot class represents a plot (chart, graph) with all associated
* parameters. This creates a palette (indexed) color image which is limited
* to 256 total colors. For truecolor images (24 bit R, G, B), see the
* PHPlot_truecolor class.
*
* In most cases, methods of PHPlot just change the internal properties, and
* nothing actually happens until the DrawGraph() method is used. Therefore
* methods can be used in any order up until DrawGraph(); the order should not
* affect the results.
*
* Note: Without a background image, the PHPlot class creates a palette
* (indexed) color image, and the PHPlot_truecolor class creates a truecolor
* image. If a background image is used with the constructor of either class,
* the type of image produced matches the type of the background image.
*
*/
class PHPlot
{
/** PHPlot version constant as a string */
const version = '6.2.0';
/** PHPlot version constant as a number = major * 10000 + minor * 100 + patch */
const version_id = 60200;
// All class variables are declared here, and initialized (if applicable).
// Starting with PHPlot-6.0, most variables have 'protected' visibility
// For more information on these variables, see the Reference Manual, Developer's Guide, List
// of Member Variables. The list below is in alphabetical order, matching the manual.
/** Calculated width of bars for bar charts */
protected $actual_bar_width;
/** Calculated bar gap */
protected $bar_adjust_gap;
/** Extra space between groups of bars */
public $bar_extra_space = 0.5;
/** Width of bar relative to space for one bar */
public $bar_width_adjust = 1;
/** Color (R,G,B,A) for image background */
protected $bg_color;
/** Background image filename */
protected $bgimg;
/** Background image tiling mode */
protected $bgmode;
/** Scale factor for box widths in box plots */
public $boxes_frac_width = 0.3;
/** Maximum half-width for boxes in box plots */
public $boxes_max_width = 8;
/** Minimum half-width for boxes in box plots */
public $boxes_min_width = 2;
/** Ratio of the width of the 'T' ends of box plot whiskers to the width of the boxes */
public $boxes_t_width = 0.6;
/** Flag: Don't send cache suppression headers */
protected $browser_cache = FALSE;
/** Max bubble size for bubbles plots */
public $bubbles_max_size;
/** Min bubbles size for bubble plots */
public $bubbles_min_size = 6;
/** Callback (hook) function information, indexed by callback reason */
protected $callbacks = array(
'data_points' => NULL,
'draw_setup' => NULL,
'draw_image_background' => NULL,
'draw_plotarea_background' => NULL,
'draw_titles' => NULL,
'draw_axes' => NULL,
'draw_graph' => NULL,
'draw_border' => NULL,
'draw_legend' => NULL,
'draw_all' => NULL,
'data_color' => NULL,
'debug_textbox' => NULL,
'debug_scale' => NULL,
);
/** Flag: Draw dashed or solid grid lines? */
protected $dashed_grid = TRUE;
/** Initial dashed pattern code */
protected $dashed_style = '2-4';
/** The (converted) data array */
protected $data;
/** Array of colors (R,G,B,A) for data borders available with some plot types */
protected $data_border_colors;
/** Array of colors (R,G,B,A) for data lines/marks/bars/etc. */
protected $data_colors;
/** Maximum number of dependent variable values */
protected $data_columns;
/** Array: Per row maximum Y value */
protected $data_max;
/** Array: Per row minimum Y value */
protected $data_min;
/** Format of the data array */
protected $data_type = 'text-data';
/** Obsolete - suffix for 'data'-formatted labels */
public $data_units_text = '';
/** Angle (in degrees) for data value labels */
public $data_value_label_angle = 90;
/** Distance (in pixels) for data value labels */
public $data_value_label_distance = 5;
/** Color (R,G,B,A) to use for axis data labels */
protected $datalabel_color;
/** Flag: data type has error bars */
protected $datatype_error_bars;
/** Flag: data type has implied X or Y */
protected $datatype_implied;
/** Flag: data type is one-column data for pie chart */
protected $datatype_pie_single;
/** Flag: data type has swapped X and Y values (horizontal plot) */
protected $datatype_swapped_xy;
/** Flag: data type includes Y and Z value pairs */
protected $datatype_yz;
/** Static array of data type information */
static protected $datatypes = array( // See DecodeDataType() and $datatype_* flags
'text-data' => array('implied' => TRUE),
'text-data-single' => array('implied' => TRUE, 'pie_single' => TRUE),
'data-data' => array(),
'data-data-error' => array('error_bars' => TRUE),
'data-data-yx' => array('swapped_xy' => TRUE),
'text-data-yx' => array('implied' => TRUE, 'swapped_xy' => TRUE),
'data-data-xyz' => array('yz' => TRUE),
'data-data-yx-error' => array('swapped_xy' => TRUE, 'error_bars' => TRUE),
);
/** Static array of data type aliases => primary name */
static protected $datatypes_map = array(
'text-linear' => 'text-data',
'linear-linear' => 'data-data',
'linear-linear-error' => 'data-data-error',
'text-data-pie' => 'text-data-single',
'data-data-error-yx' => 'data-data-yx-error',
);
/** Character to use for decimal point in formatted numbers */
protected $decimal_point;
/** The default color array, used to initialize data_colors and error_bar_colors */
protected $default_colors = array(
'SkyBlue', 'green', 'orange', 'blue', 'red', 'DarkGreen', 'purple', 'peru',
'cyan', 'salmon', 'SlateBlue', 'YellowGreen', 'magenta', 'aquamarine1', 'gold', 'violet'
);
/** Dashed-line template, as a string of space-separated markers (see SetDefaultDashedStyle) */
protected $default_dashed_style;
/** Default TrueType font file */
protected $default_ttfont;
/** Array of flags for elements that must be drawn at most once */
protected $done = array();
/** Flag: How to handle missing Y values */
protected $draw_broken_lines = FALSE;
/** Flag: Draw data borders, available with some plot types */
protected $draw_data_borders;
/** Flag: Draw borders on pie chart segments */
protected $draw_pie_borders;
/** Flag: Draw the background of the plot area */
protected $draw_plot_area_background = FALSE;
/** Flag: Draw X data label lines */
protected $draw_x_data_label_lines = FALSE;
/** Flag: Draw X grid lines? */
protected $draw_x_grid;
/** Flag: Draw Y data label lines */
protected $draw_y_data_label_lines = FALSE;
/** Flag: Draw Y grid lines? */
protected $draw_y_grid;
/** Color (R,G,B,A) to use for data value labels */
protected $dvlabel_color;
/** Array of colors (R,G,B,A) for error bars */
protected $error_bar_colors;
/** Thickness of error bar lines */
protected $error_bar_line_width = 1;
/** Shape (style) of error bars: line or tee */
protected $error_bar_shape = 'tee';
/** Size of error bars */
protected $error_bar_size = 5;
/** Image format: png, gif, jpg, wbmp */
protected $file_format = 'png';
/** Array of font information (should be protected, but public for possible callback use) */
public $fonts;
/** Flag: Draw grid on top of or behind the plot */
public $grid_at_foreground = FALSE;
/** Color (R,G,B,A) to use for axes, plot area border, legend border, pie chart lines and text */
protected $grid_color;
/** Controls fraction of bar group space used for bar */
public $group_frac_width = 0.7;
/** Color (R,G,B,A) for image border, if drawn */
protected $i_border;
/** Image border type */
protected $image_border_type = 'none';
/** Width of image border in pixels */
protected $image_border_width;
/** Image height */
protected $image_height;
/** Image width */
protected $image_width;
/** Image resource (should be protected, but public to reduce breakage) */
public $img;
/** Prevent recursion in error message image production */
protected $in_error;
/** Flag: don't send headers */
protected $is_inline = FALSE;
/** Label format info */
protected $label_format = array('x' => array(), 'xd' => array(), 'y' => array(), 'yd' => array());
/** Pie chart label position factor */
protected $label_scale_position = 0.5;
/** Legend text array */
protected $legend;
/** Color (R,G,B,A) for the legend background */
protected $legend_bg_color;
/** Alignment of color boxes or shape markers in the legend: left, right, or none */
protected $legend_colorbox_align = 'right';
/** Color control for colorbox borders in legend */
protected $legend_colorbox_borders = 'textcolor';
/** Adjusts width of color boxes in the legend */
public $legend_colorbox_width = 1;
/** Array holding legend position information */
protected $legend_pos;
/** Flag: reverse the order of lines in the legend box, bottom to top */
protected $legend_reverse_order = FALSE;
/** Legend style setting, left or right */
protected $legend_text_align = 'right';
/** Color (R,G,B,A) for the legend text */
protected $legend_text_color;
/** Draw color boxes (if false or unset) or shape markers (if true) in the legend */
protected $legend_use_shapes = FALSE;
/** Color (R,G,B,A) for grid lines and X data lines */
protected $light_grid_color;
/** Controls inter-line spacing of text */
protected $line_spacing = 4;
/** Plot line style(s) */
protected $line_styles = array('solid', 'solid', 'dashed');
/** Plot line width(s) */
protected $line_widths = 1;
/** Flag to avoid importing locale info */
public $locale_override = FALSE;
/** Overall max X value in the data array */
protected $max_x;
/** Overall max Y value in the data array */
protected $max_y;
/** Overall max Z value in the data array (for X/Y/Z data type only) */
protected $max_z;
/** Overall min X value in the data array */
protected $min_x;
/** Overall min Y value in the data array */
protected $min_y;
/** Overall min Z value in the data array (for X/Y/Z data type only) */
protected $min_z;
/** Color index of image background */
protected $ndx_bg_color;
/** Color index array for data borders */
protected $ndx_data_border_colors;
/** Color index array for plot data lines/marks/bars/etc. */
protected $ndx_data_colors;
/** Color index array for plot data, darker shade */
protected $ndx_data_dark_colors;
/** Color index for axis data labels */
protected $ndx_datalabel_color;
/** Color index for data value labels */
protected $ndx_dvlabel_color;
/** Color index array for error bars */
protected $ndx_error_bar_colors;
/** Color index for axes, plot area border, legend border, pie chart lines and text */
protected $ndx_grid_color;
/** Color index for image border lines */
protected $ndx_i_border;
/** Color index for image border lines, darker shade */
protected $ndx_i_border_dark;
/** Color index for the legend background */
protected $ndx_legend_bg_color;
/** Color index for the legend text */
protected $ndx_legend_text_color;
/** Color index for grid lines and X data lines */
protected $ndx_light_grid_color;
/** Color index for unshaded pie chart segment borders */
protected $ndx_pieborder_color;
/** Color index for pie chart data labels */
protected $ndx_pielabel_color;
/** Color index of plot area background */
protected $ndx_plot_bg_color;
/** Color index for labels and legend text */
protected $ndx_text_color;
/** Color index for tick marks */
protected $ndx_tick_color;
/** Color index for tick labels */
protected $ndx_ticklabel_color;
/** Color index for main title */
protected $ndx_title_color;
/** Color index for X title */
protected $ndx_x_title_color;
/** Color index for Y title */
protected $ndx_y_title_color;
/** Number of rows in the data array (number of points along X, or number of bar groups, for example) */
protected $num_data_rows;
/** Array with number of entries in each data row (including label and X if present) */
protected $num_recs;
/** Forced number of X tick marks */
protected $num_x_ticks = '';
/** Forced number of Y tick marks */
protected $num_y_ticks = '';
/** Scale factor for element widths in OHLC plots. */
public $ohlc_frac_width = 0.3;
/** Maximum half-width for elements in OHLC plots */
public $ohlc_max_width = 8;
/** Minimum half-width for elements in OHLC plots */
public $ohlc_min_width = 2;
/** Redirect to output file */
protected $output_file;
/** Aspect ratio for shaded pie charts */
public $pie_diam_factor = 0.5;
/** Flag: True to draw pie chart segments clockwise, false or unset for counter-clockwise. */
protected $pie_direction_cw = FALSE;
/** Flag: If true, do not include label sizes when calculating pie size. */
protected $pie_full_size = FALSE;
/** Source of label text for pie charts (percent, value, label, or index) */
protected $pie_label_source;
/** Minimum amount of the plot area that will be reserved for the pie */
public $pie_min_size_factor = 0.5;
/** Starting angle in degrees for the first segment in a pie chart */
protected $pie_start_angle = 0;
/** Color (R,G,B,A) to use for unshaded pie chart segment borders */
protected $pieborder_color;
/** Color (R,G,B,A) to use for pie chart data labels */
protected $pielabel_color;
/** Calculated plot area array: ([0],[1]) is top left, ([2],[3]) is bottom right */
protected $plot_area;
/** Height of the plot area */
protected $plot_area_height;
/** Width of the plot area */
protected $plot_area_width;
/** Color (R,G,B,A) for plot area background */
protected $plot_bg_color;
/** Where to draw plot borders. Can be scalar or array of choices. */
protected $plot_border_type;
/** Max X of the plot area in world coordinates */
protected $plot_max_x;
/** Max Y of the plot area in world coordinates */
protected $plot_max_y;
/** Min X of the plot area in world coordinates */
protected $plot_min_x;
/** Min Y of the plot area in world coordinates */
protected $plot_min_y;
/** X device coordinate of the plot area origin */
protected $plot_origin_x;
/** Y device coordinate of the plot area origin */
protected $plot_origin_y;
/** Selected plot type */
protected $plot_type = 'linepoints';
/** Plot area background image filename */
protected $plotbgimg;
/** Plot area background image tiling mode */
protected $plotbgmode;
/** Array of plot type information, indexed by plot type */
static protected $plots = array(
'area' => array(
'draw_method' => 'DrawArea',
'abs_vals' => TRUE,
),
'bars' => array(
'draw_method' => 'DrawBars',
),
'boxes' => array(
'draw_method' => 'DrawBoxes',
'adjust_type' => 1, // See GetRangeEndAdjust()
),
'bubbles' => array(
'draw_method' => 'DrawBubbles',
'adjust_type' => 1, // See GetRangeEndAdjust()
),
'candlesticks' => array(
'draw_method' => 'DrawOHLC',
'draw_arg' => array(TRUE, FALSE), // Draw candlesticks, only fill if "closed down"
'adjust_type' => 2, // See GetRangeEndAdjust()
),
'candlesticks2' => array(
'draw_method' => 'DrawOHLC',
'draw_arg' => array(TRUE, TRUE), // Draw candlesticks, fill always
'adjust_type' => 2, // See GetRangeEndAdjust()
),
'linepoints' => array(
'draw_method' => 'DrawLinePoints',
'legend_alt_marker' => 'shape',
),
'lines' => array(
'draw_method' => 'DrawLines',
'legend_alt_marker' => 'line',
),
'ohlc' => array(
'draw_method' => 'DrawOHLC',
'draw_arg' => array(FALSE), // Don't draw candlesticks
'adjust_type' => 2, // See GetRangeEndAdjust()
),
'pie' => array(
'draw_method' => 'DrawPieChart',
'suppress_axes' => TRUE,
'abs_vals' => TRUE,
),
'points' => array(
'draw_method' => 'DrawDots',
'legend_alt_marker' => 'shape',
),
'squared' => array(
'draw_method' => 'DrawSquared',
'legend_alt_marker' => 'line',
),
'squaredarea' => array(
'draw_method' => 'DrawSquaredArea',
'abs_vals' => TRUE,
),
'stackedarea' => array(
'draw_method' => 'DrawArea',
'draw_arg' => array(TRUE), // Tells DrawArea to draw stacked area plot
'sum_vals' => TRUE,
'abs_vals' => TRUE,
),
'stackedbars' => array(
'draw_method' => 'DrawStackedBars',
'sum_vals' => TRUE,
),
'stackedsquaredarea' => array(
'draw_method' => 'DrawSquaredArea',
'draw_arg' => array(TRUE), // Tells DrawSquaredArea the data is cumulative
'sum_vals' => TRUE,
'abs_vals' => TRUE,
),
'thinbarline' => array(
'draw_method' => 'DrawThinBarLines',
),
);
/** Size of point_shapes and point_sizes arrays */
protected $point_counts;
/** Marker shapes for point plots */
protected $point_shapes = array(
'diamond', 'dot', 'delta', 'home', 'yield', 'box', 'circle', 'up', 'down', 'cross'
);
/** Marker sizes for point plots */
protected $point_sizes = array(6);
/** Flag: Automatic PrintImage after DrawGraph? */
protected $print_image = TRUE;
/** Tuning parameters for plot range calculation */
protected $rangectl = array( 'x' => array(
'adjust_mode' => 'T', // T=adjust to next tick
'adjust_amount' => NULL, // See GetRangeEndAdjust()
'zero_magnet' => 0.857142, // Value is 6/7
),
'y' => array(
'adjust_mode' => 'T', // T=adjust to next tick
'adjust_amount' => NULL, // See GetRangeEndAdjust()
'zero_magnet' => 0.857142, // Value is 6/7
));
/** Area for each bar in a bar chart */
protected $record_bar_width;
/** Maximum of num_recs[], max number of entries (including label and X if present) for all data rows */
protected $records_per_group;
/** Array mapping color names to array of R, G, B values */
protected $rgb_array;
/** Fixed extra margin used in multiple places */
public $safe_margin = 5;
/** Stores PHPlot version when object was serialized */
protected $saved_version;
/** Drop shadow size for pie and bar charts */
protected $shading = 5;
/** Skip bottom tick mark */
protected $skip_bottom_tick = FALSE;
/** Skip left tick mark */
protected $skip_left_tick = FALSE;
/** Skip right tick mark */
protected $skip_right_tick = FALSE;
/** Skip top tick mark */
protected $skip_top_tick = FALSE;
/** MIME boundary sequence used with streaming plots */
protected $stream_boundary;
/** Boundary and MIME header, output before each frame in a plot stream */
protected $stream_frame_header;
/** Name of the GD output function for this image type, used with streaming plots */
protected $stream_output_f;
/** Flag: Don't produce an error image on fatal error */
protected $suppress_error_image = FALSE;
/** Flag: Don't draw the X axis line */
protected $suppress_x_axis = FALSE;
/** Flag: Don't draw the Y axis line */
protected $suppress_y_axis = FALSE;
/** Color (R,G,B,A) for labels and legend text */
protected $text_color;
/** Character to use to group 1000s in formatted numbers */
protected $thousands_sep;
/** Color (R,G,B,A) for tick marks */
protected $tick_color;
/** Tuning parameters for tick increment calculation */
protected $tickctl = array( 'x' => array(
'tick_mode' => NULL,
'min_ticks' => 8,
'tick_inc_integer' => FALSE,
),
'y' => array(
'tick_mode' => NULL,
'min_ticks' => 8,
'tick_inc_integer' => FALSE,
));
/** Color (R,G,B,A) to use for tick labels */
protected $ticklabel_color;
/** Color (R,G,B,A) for main title (and default for X and Y titles) */
protected $title_color;
/** Y offset of main title position */
protected $title_offset;
/** Main title text */
protected $title_txt = '';
/** Total number of entries (rows times columns in each row) in the data array. */
protected $total_records;
/** Color (R,G,B,A) designated as transparent */
protected $transparent_color;
/** Flag: True if serialized object had a truecolor image */
protected $truecolor;
/** TrueType font directory */
protected $ttf_path = '.';
/** Default font type, True for TrueType, False for GD */
protected $use_ttf = FALSE;
/** Position of X axis (in world coordinates) */
protected $x_axis_position;
/** Device coordinate for the X axis */
protected $x_axis_y_pixels;
/** Effective X data label text angle */
protected $x_data_label_angle;
/** X data label text angle (see also x_data_label_angle) */
protected $x_data_label_angle_u = '';
/** Position of X data labels */
protected $x_data_label_pos;
/** X tick label text angle (and default for x_data_label_angle) */
protected $x_label_angle = 0;
/** Label offset relative to plot area */
protected $x_label_axis_offset;
/** Label offset relative to plot area */
protected $x_label_bot_offset;
/** Label offset relative to plot area */
protected $x_label_top_offset;
/** Calculated plot area margin - left side */
protected $x_left_margin;
/** Calculated plot area margin - right side */
protected $x_right_margin;
/** X tick anchor point */
protected $x_tick_anchor;
/** Length of X tick marks (inside plot area) */
protected $x_tick_cross = 3;
/** Effective step between X tick marks */
protected $x_tick_inc;
/** Step between X tick marks (see also x_tick_inc) */
protected $x_tick_inc_u = '';
/** Position of X tick labels */
protected $x_tick_label_pos;
/** Length of X tick marks (outside plot area) */
protected $x_tick_length = 5;
/** Position of X tick marks */
protected $x_tick_pos = 'plotdown';
/** Title offset relative to plot area */
protected $x_title_bot_offset;
/** Color (R,G,B,A) for X title */
protected $x_title_color;
/** X Axis title position */
protected $x_title_pos = 'none';
/** Title offset relative to plot area */
protected $x_title_top_offset;
/** X Axis title text */
protected $x_title_txt = '';
/** X scale factor for converting World to Device coordinates */
protected $xscale;
/** Linear or log scale on X */
protected $xscale_type = 'linear';
/** Position of Y axis (in world coordinates) */
protected $y_axis_position;
/** Device coordinate for the Y axis */
protected $y_axis_x_pixels;
/** Calculated plot area margin - bottom */
protected $y_bot_margin;
/** Y data label text angle */
protected $y_data_label_angle = 0;
/** Position of Y data labels */
protected $y_data_label_pos;
/** Y tick label text angle */
protected $y_label_angle = 0;
/** Label offset relative to plot area */
protected $y_label_axis_offset;
/** Label offset relative to plot area */
protected $y_label_left_offset;
/** Label offset relative to plot area */
protected $y_label_right_offset;
/** Y tick anchor point */
protected $y_tick_anchor;
/** Length of Y tick marks (inside plot area) */
protected $y_tick_cross = 3;
/** Effective step between Y tick marks */
protected $y_tick_inc;
/** Step between Y tick marks (see also y_tick_inc) */
protected $y_tick_inc_u = '';
/** Position of Y tick labels */
protected $y_tick_label_pos;
/** Length of Y tick marks (outside plot area) */
protected $y_tick_length = 5;
/** Position of Y tick marks */
protected $y_tick_pos = 'plotleft';
/** Color (R,G,B,A) for Y title */
protected $y_title_color;
/** Title offset relative to plot area */
protected $y_title_left_offset;
/** Y Axis title position */
protected $y_title_pos = 'none';
/** Title offset relative to plot area */
protected $y_title_right_offset;
/** Y Axis title text */
protected $y_title_txt = '';
/** Calculated plot area margin - top */
protected $y_top_margin;
/** Y scale factor for converting World to Device coordinates */
protected $yscale;
/** Linear or log scale on Y */
protected $yscale_type = 'linear';
/**
* Constructor: Sets up GD palette image resource, and initializes plot style controls
*
* @param int $width Image width in pixels
* @param int $height Image height in pixels
* @param string $output_file Path for output file. Omit, or NULL, or '' to mean no output file
* @param string $input_file Path to a file to be used as background. Omit, NULL, or '' for none
*/
function __construct($width=600, $height=400, $output_file=NULL, $input_file=NULL)
{
$this->initialize('imagecreate', $width, $height, $output_file, $input_file);
}
/**
* Initializes a PHPlot object (used by PHPlot and PHPlot_truecolor constructors)
*
* @param string $imagecreate_function GD function to use: imagecreate or imagecreatetruecolor
* @param int $width Image width in pixels
* @param int $height Image height in pixels
* @param string $output_file Path for output file. Omit, or NULL, or '' to mean no output file
* @param string $input_file Path to a file to be used as background. Omit, NULL, or '' for none
* @since 5.6.0
*/
protected function initialize($imagecreate_function, $width, $height, $output_file, $input_file)
{
$this->SetRGBArray('small');
if (isset($output_file) && $output_file !== '')
$this->SetOutputFile($output_file);
if (isset($input_file) && $input_file !== '') {
$this->SetInputFile($input_file);
} else {
$this->image_width = $width;
$this->image_height = $height;
$this->img = call_user_func($imagecreate_function, $width, $height);
if (!$this->img)
return $this->PrintError(get_class($this) . '(): Could not create image resource.');
}
$this->SetDefaultStyles();
$this->SetDefaultFonts();
}
/**
* Prepares object for serialization
*
* The image resource cannot be serialized. But rather than try to filter it out from the other
* properties, just let PHP serialize it (it will become an integer=0), and then fix it in __wakeup.
* This way the object is still usable after serialize().
* Note: This does not work if an input file was provided to the constructor.
*
* @return string[] Array of object property names, as required by PHP spec for __sleep()
* @since 5.8.0
*/
function __sleep()
{
$this->truecolor = imageistruecolor($this->img); // Remember image type
$this->saved_version = self::version; // Remember version of PHPlot, for checking on unserialize
return array_keys(get_object_vars($this));
}
/**
* Cleans up object after unserialization
*
* Recreates the image resource (which is not serializable), after validating the PHPlot version.
* @since 5.8.0
*/
function __wakeup()
{
if (strcmp($this->saved_version, self::version) != 0)
$this->PrintError(get_class($this) . '(): Unserialize version mismatch');
$imagecreate_function = $this->truecolor ? 'imagecreatetruecolor' : 'imagecreate';
$this->img = call_user_func($imagecreate_function, $this->image_width, $this->image_height);
if (!$this->img)
$this->PrintError(get_class($this) . '(): Could not create image resource.');
unset($this->truecolor, $this->saved_version);
}
/**
* Reads an image file (used by constructor via SetInput file, and by tile_img for backgrounds)
*
* @param string $image_filename Filename of the image file to read
* @param int $width Reference variable for width of the image in pixels
* @param int $height Reference variable for height of the image in pixels
* @return resource Image resource (False on error if an error handler returns True)
* @since 5.0.4
*/
protected function GetImage($image_filename, &$width, &$height)
{
$error = '';
$size = getimagesize($image_filename);
if (!$size) {
$error = "Unable to query image file $image_filename";
} else {
$image_type = $size[2];
switch ($image_type) {
case IMAGETYPE_GIF:
$img = @ ImageCreateFromGIF ($image_filename);
break;
case IMAGETYPE_PNG:
$img = @ ImageCreateFromPNG ($image_filename);
break;
case IMAGETYPE_JPEG:
$img = @ ImageCreateFromJPEG ($image_filename);
break;
default:
$error = "Unknown image type ($image_type) for image file $image_filename";
break;
}
}
if (empty($error) && !$img) {
// getimagesize is OK, but GD won't read it. Maybe unsupported format.
$error = "Failed to read image file $image_filename";
}
if (!empty($error)) {
return $this->PrintError("GetImage(): $error");
}
$width = $size[0];
$height = $size[1];
return $img;
}
/**
* Selects an input file to be used as background for the whole graph
*
* @param string $which_input_file Pathname to the image file to use as a background
* @deprecated Public use discouraged; intended for use by class constructor
* @return bool True (False on error if an error handler returns True)
*/
function SetInputFile($which_input_file)
{
$im = $this->GetImage($which_input_file, $this->image_width, $this->image_height);
if (!$im)
return FALSE; // GetImage already produced an error message.
// Deallocate any resources previously allocated
if (isset($this->img))
imagedestroy($this->img);
$this->img = $im;
// Do not overwrite the input file with the background color.
$this->done['background'] = TRUE;
return TRUE;
}
/////////////////////////////////////////////
////////////// COLORS
/////////////////////////////////////////////
/**
* Allocates a GD color index for a color specified as an array (R,G,B,A)
*
* At drawing time, this allocates a GD color index for the specified color, which
* is specified as a 4 component array. Earlier, when a color is specified,
* SetRGBColor() parsed and checked it and converted it to this component array form.
*
* @param int[] $color Color specification as (R, G, B, A), or unset variable
* @param int $default_color_index An already-allocated GD color index to use if $color is unset
* @return int A GD color index that can be used when drawing
* @since 5.2.0
*/
protected function GetColorIndex(&$color, $default_color_index = 0)
{
if (empty($color)) return $default_color_index;
list($r, $g, $b, $a) = $color;
return imagecolorresolvealpha($this->img, $r, $g, $b, $a);
}
/**
* Allocates an array of GD color indexes from an array of color specification arrays
*
* This is used for the data_colors array, for example.
* Note: $color_array must use 0-based sequential integer indexes.
*
* @param array $color_array Array of color specifications, each an array (R,G,B,A)
* @param int $max_colors Limit color allocation to no more than this number of colors
* @return int[] Array of GD color indexes that can be used when drawing
* @since 5.3.1
*/
protected function GetColorIndexArray($color_array, $max_colors)
{
$n = min(count($color_array), $max_colors);
$result = array();
for ($i = 0; $i < $n; $i++)
$result[] = $this->GetColorIndex($color_array[$i]);
return $result;
}
/**
* Allocates an array of GD color indexes for darker shades from an array of color specifications
*
* This is used for shadow colors such as those in bar charts with shading.
*
* @param array $color_array Array of color specifications, each an array (R,G,B,A)
* @param int $max_colors Limit color allocation to no more than this number of colors
* @return int[] Array of GD color indexes that can be used when drawing shadow colors
* @since 5.3.1
*/
protected function GetDarkColorIndexArray($color_array, $max_colors)
{
$n = min(count($color_array), $max_colors);
$result = array();
for ($i = 0; $i < $n; $i++)
$result[] = $this->GetDarkColorIndex($color_array[$i]);
return $result;
}
/**
* Allocates a GD color index for a darker shade of a color specified as an array (R,G,B,A)
*
* See notes on GetColorIndex() above.
*
* @param int[] $color Color specification as (R, G, B, A)
* @return int A GD color index that can be used when drawing a shadow color
* @since 5.2.0
*/
protected function GetDarkColorIndex($color)
{
list ($r, $g, $b, $a) = $color;
$r = max(0, $r - 0x30);
$g = max(0, $g - 0x30);
$b = max(0, $b - 0x30);
return imagecolorresolvealpha($this->img, $r, $g, $b, $a);
}
/**
* Sets or reverts all colors and styles to their defaults
*
* @return bool True always
*/
protected function SetDefaultStyles()
{
$this->SetDefaultDashedStyle($this->dashed_style);
$this->SetImageBorderColor(array(194, 194, 194));
$this->SetPlotBgColor('white');
$this->SetBackgroundColor('white');
$this->SetTextColor('black');
$this->SetGridColor('black');
$this->SetLightGridColor('gray');
$this->SetTickColor('black');
$this->SetTitleColor('black');
// These functions set up the default colors when called without parameters
$this->SetDataColors();
$this->SetErrorBarColors();
$this->SetDataBorderColors();
return TRUE;
}
/**
* Sets the overall image background color
*
* @param string|int[] $which_color Color name or spec (#rrggbb, (r,g,b) array, etc)
* @return bool True (False on error if an error handler returns True)
*/
function SetBackgroundColor($which_color)
{
return (bool)($this->bg_color = $this->SetRGBColor($which_color));
}
/**
* Sets the plot area background color, which is only drawn if SetDrawPlotAreaBackground is used.
*
* @param string|int[] $which_color Color name or spec (#rrggbb, (r,g,b) array, etc)
* @return bool True (False on error if an error handler returns True)
*/
function SetPlotBgColor($which_color)
{
return (bool)($this->plot_bg_color = $this->SetRGBColor($which_color));
}
/**
* Sets the color of the plot title, and the default color of the X and Y titles.
*
* @param string|int[] $which_color Color name or spec (#rrggbb, (r,g,b) array, etc)
* @return bool True (False on error if an error handler returns True)
*/
function SetTitleColor($which_color)
{
return (bool)($this->title_color = $this->SetRGBColor($which_color));
}
/**
* Sets the color of the X title, overriding the color set with SetTitleColor()
*
* @param string|int[] $which_color Color name or spec (#rrggbb, (r,g,b) array, etc)
* @return bool True (False on error if an error handler returns True)
* @since 5.2.0
*/
function SetXTitleColor($which_color)
{
return (bool)($this->x_title_color = $this->SetRGBColor($which_color));
}
/**
* Sets the color of the Y title, overriding the color set with SetTitleColor()
*
* @param string|int[] $which_color Color name or spec (#rrggbb, (r,g,b) array, etc)
* @return bool True (False on error if an error handler returns True)
* @since 5.2.0
*/
function SetYTitleColor($which_color)
{
return (bool)($this->y_title_color = $this->SetRGBColor($which_color));
}
/**
* Sets the color of the axis tick marks
*
* @param string|int[] $which_color Color name or spec (#rrggbb, (r,g,b) array, etc)
* @return bool True (False on error if an error handler returns True)
*/
function SetTickColor($which_color)
{
return (bool)($this->tick_color = $this->SetRGBColor($which_color));
}
/**
* @deprecated Use SetTitleColor() instead
*/
function SetLabelColor($which_color)
{
return $this->SetTitleColor($which_color);
}
/**
* Sets the general text color, which is the default color for legend text, tick and data labels
*
* @param string|int[] $which_color Color name or spec (#rrggbb, (r,g,b) array, etc)
* @return bool True (False on error if an error handler returns True)
*/
function SetTextColor($which_color)
{
return (bool)($this->text_color = $this->SetRGBColor($which_color));
}
/**
* Sets the color for data labels, overriding the default set with SetTextColor()
*
* @param string|int[] $which_color Color name or spec (#rrggbb, (r,g,b) array, etc)
* @return bool True (False on error if an error handler returns True)
* @since 5.7.0
*/
function SetDataLabelColor($which_color)
{
return (bool)($this->datalabel_color = $this->SetRGBColor($which_color));
}
/**
* Sets the color for data value labels, overriding SetTextColor() and SetDataLabelColor()
*