/
phplot.php
5052 lines (4392 loc) · 188 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
/* $Id: phplot.php,v 1.141 2008/01/14 02:09:41 lbayuk Exp $ */
/*
* PHPLOT Version 5.0.5
* Copyright (C) 1998-2008 Afan Ottenheimer. Released under
* the GPL and PHP licenses as stated in the the README file which should
* have been included with this document.
*
* Co-author and maintainer (2003-2005)
* Miguel de Benito Delgado <nonick AT vodafone DOT es>
*
* Maintainer (2006-present)
* <lbayuk AT users DOT sourceforge DOT net>
*
* Visit http://sourceforge.net/projects/phplot/
* for PHPlot documentation, downloads, and discussions.
*
* Requires PHP 5.2.x or later. (PHP 4 is unsupported as of Jan 2008)
*/
class PHPlot {
/* I have removed internal variable declarations, some isset() checking was required,
* but now the variables left are those which can be tweaked by the user. This is intended to
* be the first step towards moving most of the Set...() methods into a subclass which will be
* used only when strictly necessary. Many users will be able to put default values here in the
* class and thus avoid memory overhead and reduce parsing times.
*/
//////////////// CONFIG PARAMETERS //////////////////////
var $is_inline = FALSE; // FALSE = Sends headers, TRUE = sends just raw image data
var $browser_cache = FALSE; // FALSE = Sends headers for browser to not cache the image,
// (only if is_inline = FALSE also)
var $safe_margin = 5; // Extra margin used in several places. In pixels
var $x_axis_position = ''; // Where to draw both axis (world coordinates),
var $y_axis_position = ''; // leave blank for X axis at 0 and Y axis at left of plot.
var $xscale_type = 'linear'; // linear, log
var $yscale_type = 'linear';
//Fonts
var $use_ttf = FALSE; // Use True Type Fonts?
var $ttf_path = '.'; // Default path to look in for TT Fonts.
var $default_ttfont = 'benjamingothic.ttf';
var $line_spacing = 4; // Pixels between lines.
// Font angles: 0 or 90 degrees for fixed fonts, any for TTF
var $x_label_angle = 0; // For labels on X axis (tick and data)
var $y_label_angle = 0; // For labels on Y axis (tick and data)
//Formats
var $file_format = 'png';
var $output_file = ''; // For output to a file instead of stdout
//Data
var $data_type = 'text-data'; // text-data, data-data-error, data-data, text-data-single
var $plot_type= 'linepoints'; // bars, lines, linepoints, area, points, pie, thinbarline, squared
var $label_scale_position = 0.5; // Shifts data labes in pie charts. 1 = top, 0 = bottom
var $group_frac_width = 0.7; // Bars use this fraction (0 to 1) of a group's space
var $bar_extra_space = 0.5; // Number of extra bar's worth of space in a group
var $bar_width_adjust = 1; // 1 = bars of normal width, must be > 0
var $y_precision = 1;
var $x_precision = 1;
var $data_units_text = ''; // Units text for 'data' labels (i.e: '�', '$', etc.)
// Titles
var $title_txt = '';
var $x_title_txt = '';
var $x_title_pos = 'plotdown'; // plotdown, plotup, both, none
var $y_title_txt = '';
var $y_title_pos = 'plotleft'; // plotleft, plotright, both, none
//Labels
// There are two types of labels in PHPlot:
// Tick labels: they follow the grid, next to ticks in axis. (DONE)
// they are drawn at grid drawing time, by DrawXTicks() and DrawYTicks()
// Data labels: they follow the data points, and can be placed on the axis or the plot (x/y) (TODO)
// they are drawn at graph plotting time, by Draw*DataLabel(), called by DrawLines(), etc.
// Draw*DataLabel() also draws H/V lines to datapoints depending on draw_*_data_label_lines
// Tick Labels
var $x_tick_label_pos = 'plotdown'; // plotdown, plotup, both, xaxis, none
var $y_tick_label_pos = 'plotleft'; // plotleft, plotright, both, yaxis, none
// Data Labels:
var $x_data_label_pos = 'plotdown'; // plotdown, plotup, both, plot, all, none
var $y_data_label_pos = 'plotleft'; // plotleft, plotright, both, plot, all, plotin, none
var $draw_x_data_label_lines = FALSE; // Draw a line from the data point to the axis?
var $draw_y_data_label_lines = FALSE; // TODO
// Label types: (for tick, data and plot labels)
var $x_label_type = ''; // data, time. Leave blank for no formatting.
var $y_label_type = ''; // data, time. Leave blank for no formatting.
var $x_time_format = '%H:%M:%S'; // See http://www.php.net/manual/html/function.strftime.html
var $y_time_format = '%H:%M:%S'; // SetYTimeFormat() too...
// Skipping labels
// var $x_label_inc = 1; // Draw a label every this many (1 = all) (TODO)
// var $y_label_inc = 1;
// var $_x_label_cnt = 0; // internal count FIXME: work in progress
// Legend
var $legend = ''; // An array with legend titles
// These variables are unset to take default values:
// var $legend_x_pos; // User-specified upper left coordinates of legend box
// var $legend_y_pos;
// var $legend_xy_world; // If set, legend_x/y_pos are world coords, else pixel coords
// var $legend_text_align; // left or right, Unset means right
// var $legend_colorbox_align; // left, right, or none; Unset means same as text_align
//Ticks
var $x_tick_length = 5; // tick length in pixels for upper/lower axis
var $y_tick_length = 5; // tick length in pixels for left/right axis
var $x_tick_cross = 3; // ticks cross x axis this many pixels
var $y_tick_cross = 3; // ticks cross y axis this many pixels
var $x_tick_pos = 'plotdown'; // plotdown, plotup, both, xaxis, none
var $y_tick_pos = 'plotleft'; // plotright, plotleft, both, yaxis, none
var $num_x_ticks = '';
var $num_y_ticks = '';
var $x_tick_inc = ''; // Set num_x_ticks or x_tick_inc, not both.
var $y_tick_inc = ''; // Set num_y_ticks or y_tick_inc, not both.
var $skip_top_tick = FALSE;
var $skip_bottom_tick = FALSE;
var $skip_left_tick = FALSE;
var $skip_right_tick = FALSE;
//Grid Formatting
var $draw_x_grid = FALSE;
var $draw_y_grid = TRUE;
var $dashed_grid = TRUE;
var $grid_at_foreground = FALSE; // Chooses whether to draw the grid below or above the graph
//Colors and styles (all colors can be array (R,G,B) or named color)
var $color_array = 'small'; // 'small', 'large' or array (define your own colors)
// See rgb.inc.php and SetRGBArray()
var $i_border = array(194, 194, 194);
var $plot_bg_color = 'white';
var $bg_color = 'white';
var $label_color = 'black';
var $text_color = 'black';
var $grid_color = 'black';
var $light_grid_color = 'gray';
var $tick_color = 'black';
var $title_color = 'black';
var $data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'orange', 'red', 'violet', 'azure1');
var $error_bar_colors = array('SkyBlue', 'green', 'orange', 'blue', 'orange', 'red', 'violet', 'azure1');
var $data_border_colors = array('black');
var $line_widths = 1; // single value or array
var $line_styles = array('solid', 'solid', 'dashed'); // single value or array
var $dashed_style = '2-4'; // colored dots-transparent dots
var $point_sizes = array(5,5,3); // single value or array
var $point_shapes = array('diamond'); // rect, circle, diamond, triangle, dot, line, halfline, cross
var $error_bar_size = 5; // right and left size of tee
var $error_bar_shape = 'tee'; // 'tee' or 'line'
var $error_bar_line_width = 1; // single value (or array TODO)
var $plot_border_type = 'sides'; // left, sides, none, full
var $image_border_type = 'none'; // 'raised', 'plain', 'none'
var $shading = 5; // 0 for no shading, > 0 is size of shadows in pixels
var $draw_plot_area_background = FALSE;
var $draw_broken_lines = FALSE; // Tells not to draw lines for missing Y data.
//Miscellaneous
var $callbacks = array( // Valid callback reasons (see SetCallBack)
'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,
'debug_textbox' => null, // For testing/debugging text box alignment
'debug_scale' => null, // For testing/debugging scale setup
);
//////////////////////////////////////////////////////
//BEGIN CODE
//////////////////////////////////////////////////////
/*!
* Constructor: Setup img resource, colors and size of the image, and font sizes.
*
* \param which_width int Image width in pixels.
* \param which_height int Image height in pixels.
* \param which_output_file string Filename for output.
* \param which_input_fule string Path to a file to be used as background.
*/
function __construct($which_width=600, $which_height=400, $which_output_file=null, $which_input_file=null)
{
$this->SetRGBArray($this->color_array);
$this->background_done = FALSE; // TRUE after background image is drawn once
$this->plot_margins_set = FALSE; // TRUE with user-set plot area or plot margins.
if ($which_output_file)
$this->SetOutputFile($which_output_file);
if ($which_input_file)
$this->SetInputFile($which_input_file);
else {
$this->image_width = $which_width;
$this->image_height = $which_height;
$this->img = ImageCreate($this->image_width, $this->image_height);
if (! $this->img)
return $this->PrintError('PHPlot(): Could not create image resource.');
}
$this->SetDefaultStyles();
$this->SetDefaultFonts();
$this->SetTitle('');
$this->SetXTitle('');
$this->SetYTitle('');
$this->print_image = TRUE; // Use for multiple plots per image (TODO: automatic)
}
/*!
* Reads an image file. Stores width and height, and returns the image
* resource. On error, calls PrintError and returns False.
* This is used by the constructor via SetInputFile, and by tile_img().
*/
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.
* This resets the graph size to the image's size.
* Note: This is used by the constructor. It is deprecated for direct use.
*/
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->background_done = TRUE;
return TRUE;
}
/////////////////////////////////////////////
////////////// COLORS
/////////////////////////////////////////////
/*!
* Returns an index to a color passed in as anything (string, hex, rgb)
*
* \param which_color * Color (can be '#AABBCC', 'Colorname', or array(r,g,b))
* Returns a GD color index (integer >= 0), or NULL on error.
*/
function SetIndexColor($which_color)
{
list ($r, $g, $b) = $this->SetRGBColor($which_color); //Translate to RGB
if (!isset($r)) return null;
return ImageColorResolve($this->img, $r, $g, $b);
}
/*!
* Returns an index to a slightly darker color than the one requested.
* Returns a GD color index (integer >= 0), or NULL on error.
*/
function SetIndexDarkColor($which_color)
{
list ($r, $g, $b) = $this->SetRGBColor($which_color);
if (!isset($r)) return null;
$r = max(0, $r - 0x30);
$g = max(0, $g - 0x30);
$b = max(0, $b - 0x30);
return ImageColorResolve($this->img, $r, $g, $b);
}
/*!
* Sets/reverts all colors and styles to their defaults. If session is set, then only updates indices,
* as they are lost with every script execution, else, sets the default colors by name or value and
* then updates indices too.
*
* FIXME Isn't this too slow?
*
*/
function SetDefaultStyles()
{
/* Some of the Set*() functions use default values when they get no parameters. */
if (! isset($this->session_set)) {
// If sessions are enabled, this variable will be preserved, so upon future executions, we
// will have it set, as well as color names (though not color indices, that's why we
// need to rebuild them)
$this->session_set = TRUE;
// These only need to be set once
$this->SetLineWidths();
$this->SetLineStyles();
$this->SetDefaultDashedStyle($this->dashed_style);
$this->SetPointSizes($this->point_sizes);
}
$this->SetImageBorderColor($this->i_border);
$this->SetPlotBgColor($this->plot_bg_color);
$this->SetBackgroundColor($this->bg_color);
$this->SetLabelColor($this->label_color);
$this->SetTextColor($this->text_color);
$this->SetGridColor($this->grid_color);
$this->SetLightGridColor($this->light_grid_color);
$this->SetTickColor($this->tick_color);
$this->SetTitleColor($this->title_color);
$this->SetDataColors();
$this->SetErrorBarColors();
$this->SetDataBorderColors();
return TRUE;
}
/*
*
*/
function SetBackgroundColor($which_color)
{
$this->bg_color= $which_color;
$this->ndx_bg_color= $this->SetIndexColor($this->bg_color);
return isset($this->ndx_bg_color);
}
/*
*
*/
function SetPlotBgColor($which_color)
{
$this->plot_bg_color= $which_color;
$this->ndx_plot_bg_color= $this->SetIndexColor($this->plot_bg_color);
return isset($this->ndx_plot_bg_color);
}
/*
*
*/
function SetTitleColor($which_color)
{
$this->title_color= $which_color;
$this->ndx_title_color= $this->SetIndexColor($this->title_color);
return isset($this->ndx_title_color);
}
/*
*
*/
function SetTickColor ($which_color)
{
$this->tick_color= $which_color;
$this->ndx_tick_color= $this->SetIndexColor($this->tick_color);
return isset($this->ndx_tick_color);
}
/*
* Do not use. Use SetTitleColor instead.
*/
function SetLabelColor ($which_color)
{
$this->label_color = $which_color;
$this->ndx_title_color= $this->SetIndexColor($this->label_color);
return isset($this->ndx_title_color);
}
/*
*
*/
function SetTextColor ($which_color)
{
$this->text_color= $which_color;
$this->ndx_text_color= $this->SetIndexColor($this->text_color);
return isset($this->ndx_text_color);
}
/*
*
*/
function SetLightGridColor ($which_color)
{
$this->light_grid_color= $which_color;
$this->ndx_light_grid_color= $this->SetIndexColor($this->light_grid_color);
return isset($this->ndx_light_grid_color);
}
/*
*
*/
function SetGridColor ($which_color)
{
$this->grid_color = $which_color;
$this->ndx_grid_color= $this->SetIndexColor($this->grid_color);
return isset($this->ndx_grid_color);
}
/*
*
*/
function SetImageBorderColor($which_color)
{
$this->i_border = $which_color;
$this->ndx_i_border = $this->SetIndexColor($this->i_border);
$this->ndx_i_border_dark = $this->SetIndexDarkColor($this->i_border);
return isset($this->ndx_i_border);
}
/*
*
*/
function SetTransparentColor($which_color)
{
$ndx = $this->SetIndexColor($which_color);
if (!isset($ndx))
return FALSE;
ImageColorTransparent($this->img, $ndx);
return TRUE;
}
/*!
* Sets the array of colors to be used. It can be user defined, a small predefined one
* or a large one included from 'rgb.inc.php'.
*
* \param which_color_array If an array, the used as color array. If a string can
* be one of 'small' or 'large'.
*/
function SetRGBArray ($which_color_array)
{
if ( is_array($which_color_array) ) { // User defined array
$this->rgb_array = $which_color_array;
return TRUE;
} elseif ($which_color_array == 'small') { // Small predefined color array
$this->rgb_array = array(
'white' => array(255, 255, 255),
'snow' => array(255, 250, 250),
'PeachPuff' => array(255, 218, 185),
'ivory' => array(255, 255, 240),
'lavender' => array(230, 230, 250),
'black' => array( 0, 0, 0),
'DimGrey' => array(105, 105, 105),
'gray' => array(190, 190, 190),
'grey' => array(190, 190, 190),
'navy' => array( 0, 0, 128),
'SlateBlue' => array(106, 90, 205),
'blue' => array( 0, 0, 255),
'SkyBlue' => array(135, 206, 235),
'cyan' => array( 0, 255, 255),
'DarkGreen' => array( 0, 100, 0),
'green' => array( 0, 255, 0),
'YellowGreen' => array(154, 205, 50),
'yellow' => array(255, 255, 0),
'orange' => array(255, 165, 0),
'gold' => array(255, 215, 0),
'peru' => array(205, 133, 63),
'beige' => array(245, 245, 220),
'wheat' => array(245, 222, 179),
'tan' => array(210, 180, 140),
'brown' => array(165, 42, 42),
'salmon' => array(250, 128, 114),
'red' => array(255, 0, 0),
'pink' => array(255, 192, 203),
'maroon' => array(176, 48, 96),
'magenta' => array(255, 0, 255),
'violet' => array(238, 130, 238),
'plum' => array(221, 160, 221),
'orchid' => array(218, 112, 214),
'purple' => array(160, 32, 240),
'azure1' => array(240, 255, 255),
'aquamarine1' => array(127, 255, 212)
);
return TRUE;
} elseif ($which_color_array === 'large') { // Large color array
include("./rgb.inc.php");
$this->rgb_array = $RGBArray;
} else { // Default to black and white only.
$this->rgb_array = array('white' => array(255, 255, 255), 'black' => array(0, 0, 0));
}
return TRUE;
}
/*!
* Returns an array in R, G, B format 0-255
*
* \param color_asked array(R,G,B) or string (named color or '#AABBCC')
*/
function SetRGBColor($color_asked)
{
if (empty($color_asked)) {
$ret_val = array(0, 0, 0);
} elseif (count($color_asked) == 3 ) { // already array of 3 rgb
$ret_val = $color_asked;
} elseif ($color_asked[0] == '#') { // Hex RGB notation #RRGGBB
$ret_val = array(hexdec(substr($color_asked, 1, 2)),
hexdec(substr($color_asked, 3, 2)),
hexdec(substr($color_asked, 5, 2)));
} elseif (isset($this->rgb_array[$color_asked])) { // Color by name
$ret_val = $this->rgb_array[$color_asked];
} else {
return $this->PrintError("SetRGBColor(): Color '$color_asked' is not valid.");
}
return $ret_val;
}
/*!
* Sets the colors for the data.
*/
function SetDataColors($which_data = null, $which_border = null)
{
if (is_null($which_data) && is_array($this->data_colors)) {
// use already set data_colors
} else if (! is_array($which_data)) {
$this->data_colors = ($which_data) ? array($which_data) : array('blue', 'red', 'green', 'orange');
} else {
$this->data_colors = $which_data;
}
$i = 0;
foreach ($this->data_colors as $col) {
$ndx = $this->SetIndexColor($col);
if (!isset($ndx))
return FALSE;
$this->ndx_data_colors[$i] = $ndx;
$this->ndx_data_dark_colors[$i] = $this->SetIndexDarkColor($col);
$i++;
}
// For past compatibility:
return $this->SetDataBorderColors($which_border);
} // function SetDataColors()
/*!
*
*/
function SetDataBorderColors($which_br = null)
{
if (is_null($which_br) && is_array($this->data_border_colors)) {
// use already set data_border_colors
} else if (! is_array($which_br)) {
// Create new array with specified color
$this->data_border_colors = ($which_br) ? array($which_br) : array('black');
} else {
$this->data_border_colors = $which_br;
}
$i = 0;
foreach($this->data_border_colors as $col) {
$ndx = $this->SetIndexColor($col);
if (!isset($ndx))
return FALSE;
$this->ndx_data_border_colors[$i] = $ndx;
$i++;
}
return TRUE;
} // function SetDataBorderColors()
/*!
* Sets the colors for the data error bars.
*/
function SetErrorBarColors($which_err = null)
{
if (is_null($which_err) && is_array($this->error_bar_colors)) {
// use already set error_bar_colors
} else if (! is_array($which_err)) {
$this->error_bar_colors = ($which_err) ? array($which_err) : array('black');
} else {
$this->error_bar_colors = $which_err;
}
$i = 0;
foreach($this->error_bar_colors as $col) {
$ndx = $this->SetIndexColor($col);
if (!isset($ndx))
return FALSE;
$this->ndx_error_bar_colors[$i] = $ndx;
$i++;
}
return TRUE;
} // function SetErrorBarColors()
/*!
* Sets the default dashed style.
* \param which_style A string specifying order of colored and transparent dots,
* i.e: '4-3' means 4 colored, 3 transparent;
* '2-3-1-2' means 2 colored, 3 transparent, 1 colored, 2 transparent.
*/
function SetDefaultDashedStyle($which_style)
{
// String: "numcol-numtrans-numcol-numtrans..."
$asked = explode('-', $which_style);
if (count($asked) < 2) {
return $this->PrintError("SetDefaultDashedStyle(): Wrong parameter '$which_style'.");
}
// Build the string to be eval()uated later by SetDashedStyle()
$this->default_dashed_style = 'array( ';
$t = 0;
foreach($asked as $s) {
if ($t % 2 == 0) {
$this->default_dashed_style .= str_repeat('$which_ndxcol,', $s);
} else {
$this->default_dashed_style .= str_repeat('IMG_COLOR_TRANSPARENT,', $s);
}
$t++;
}
// Remove trailing comma and add closing parenthesis
$this->default_dashed_style = substr($this->default_dashed_style, 0, -1);
$this->default_dashed_style .= ')';
return TRUE;
}
/*!
* Sets the style before drawing a dashed line. Defaults to $this->default_dashed_style
* \param which_ndxcol Color index to be used.
*/
function SetDashedStyle($which_ndxcol)
{
// See SetDefaultDashedStyle() to understand this.
eval ("\$style = $this->default_dashed_style;");
return imagesetstyle($this->img, $style);
}
/*!
* Sets line widths on a per-line basis.
*/
function SetLineWidths($which_lw=null)
{
if (is_null($which_lw)) {
// Do nothing, use default value.
} else if (is_array($which_lw)) {
// Did we get an array with line widths?
$this->line_widths = $which_lw;
} else {
$this->line_widths = array($which_lw);
}
return TRUE;
}
/*!
*
*/
function SetLineStyles($which_ls=null)
{
if (is_null($which_ls)) {
// Do nothing, use default value.
} else if ( is_array($which_ls)) {
// Did we get an array with line styles?
$this->line_styles = $which_ls;
} else {
$this->line_styles = ($which_ls) ? array($which_ls) : array('solid');
}
return TRUE;
}
/////////////////////////////////////////////
////////////// TEXT and FONTS
/////////////////////////////////////////////
/*!
* Sets number of pixels between lines of the same text.
*/
function SetLineSpacing($which_spc)
{
$this->line_spacing = $which_spc;
return TRUE;
}
/*!
* Enables use of TrueType fonts in the graph. Font initialisation methods
* depend on this setting, so when called, SetUseTTF() resets the font
* settings
*/
function SetUseTTF($which_ttf)
{
$this->use_ttf = $which_ttf;
return $this->SetDefaultFonts();
}
/*!
* Sets the directory name to look into for TrueType fonts.
*/
function SetTTFPath($which_path)
{
// Maybe someone needs really dynamic config. He'll need this:
// clearstatcache();
if (is_dir($which_path) && is_readable($which_path)) {
$this->ttf_path = $which_path;
return TRUE;
}
return $this->PrintError("SetTTFPath(): $which_path is not a valid path.");
}
/*!
* Sets the default TrueType font and updates all fonts to that.
* The default font might be a full path, or relative to the TTFPath,
* so let SetFont check that it exists.
* Side effect: enable use of TrueType fonts.
*/
function SetDefaultTTFont($which_font)
{
$this->default_ttfont = $which_font;
return $this->SetUseTTF(TRUE);
}
/*!
* Sets fonts to their defaults
*/
function SetDefaultFonts()
{
// TTF:
if ($this->use_ttf) {
return $this->SetFont('generic', '', 8)
&& $this->SetFont('title', '', 14)
&& $this->SetFont('legend', '', 8)
&& $this->SetFont('x_label', '', 6)
&& $this->SetFont('y_label', '', 6)
&& $this->SetFont('x_title', '', 10)
&& $this->SetFont('y_title', '', 10);
}
// Fixed GD Fonts:
return $this->SetFont('generic', 2)
&& $this->SetFont('title', 5)
&& $this->SetFont('legend', 2)
&& $this->SetFont('x_label', 1)
&& $this->SetFont('y_label', 1)
&& $this->SetFont('x_title', 3)
&& $this->SetFont('y_title', 3);
}
/*!
* Sets Fixed/Truetype font parameters.
* \param $which_elem Is the element whose font is to be changed.
* It can be one of 'title', 'legend', 'generic',
* 'x_label', 'y_label', x_title' or 'y_title'
* \param $which_font Can be a number (for fixed font sizes) or
* a string with the font pathname or filename when using TTFonts.
* For TTFonts, an empty string means use the default font.
* \param $which_size Point size (TTF only)
* Calculates and updates internal height and width variables.
*/
function SetFont($which_elem, $which_font, $which_size = 12)
{
// TTF:
if ($this->use_ttf) {
// Empty font name means use the default font.
if (empty($which_font))
$which_font = $this->default_ttfont;
$path = $which_font;
// First try the font name directly, if not then try with path.
if (!is_file($path) || !is_readable($path)) {
$path = $this->ttf_path . '/' . $which_font;
if (!is_file($path) || !is_readable($path)) {
return $this->PrintError("SetFont(): Can't find TrueType font $which_font");
}
}
switch ($which_elem) {
case 'generic':
$this->generic_font['font'] = $path;
$this->generic_font['size'] = $which_size;
break;
case 'title':
$this->title_font['font'] = $path;
$this->title_font['size'] = $which_size;
break;
case 'legend':
$this->legend_font['font'] = $path;
$this->legend_font['size'] = $which_size;
break;
case 'x_label':
$this->x_label_font['font'] = $path;
$this->x_label_font['size'] = $which_size;
break;
case 'y_label':
$this->y_label_font['font'] = $path;
$this->y_label_font['size'] = $which_size;
break;
case 'x_title':
$this->x_title_font['font'] = $path;
$this->x_title_font['size'] = $which_size;
break;
case 'y_title':
$this->y_title_font['font'] = $path;
$this->y_title_font['size'] = $which_size;
break;
default:
return $this->PrintError("SetFont(): Unknown element '$which_elem' specified.");
}
return TRUE;
}
// Fixed fonts:
if ($which_font > 5 || $which_font < 0) {
return $this->PrintError('SetFont(): Non-TTF font size must be 1, 2, 3, 4 or 5');
}
switch ($which_elem) {
case 'generic':
$this->generic_font['font'] = $which_font;
$this->generic_font['height'] = ImageFontHeight($which_font);
$this->generic_font['width'] = ImageFontWidth($which_font);
break;
case 'title':
$this->title_font['font'] = $which_font;
$this->title_font['height'] = ImageFontHeight($which_font);
$this->title_font['width'] = ImageFontWidth($which_font);
break;
case 'legend':
$this->legend_font['font'] = $which_font;
$this->legend_font['height'] = ImageFontHeight($which_font);
$this->legend_font['width'] = ImageFontWidth($which_font);
break;
case 'x_label':
$this->x_label_font['font'] = $which_font;
$this->x_label_font['height'] = ImageFontHeight($which_font);
$this->x_label_font['width'] = ImageFontWidth($which_font);
break;
case 'y_label':
$this->y_label_font['font'] = $which_font;
$this->y_label_font['height'] = ImageFontHeight($which_font);
$this->y_label_font['width'] = ImageFontWidth($which_font);
break;
case 'x_title':
$this->x_title_font['font'] = $which_font;
$this->x_title_font['height'] = ImageFontHeight($which_font);
$this->x_title_font['width'] = ImageFontWidth($which_font);
break;
case 'y_title':
$this->y_title_font['font'] = $which_font;
$this->y_title_font['height'] = ImageFontHeight($which_font);
$this->y_title_font['width'] = ImageFontWidth($which_font);
break;
default:
return $this->PrintError("SetFont(): Unknown element '$which_elem' specified.");
}
return TRUE;
}
/*!
* Text drawing and sizing functions:
* ProcessText is meant for use only by DrawText and SizeText.
* ProcessText(True, ...) - Draw a block of text
* ProcessText(False, ...) - Just return ($width, $height) of
* the orthogonal bounding box containing the text.
* ProcessText is further split into separate functions for GD and TTF
* text, due to the size of the code.
*
* Horizontal and vertical alignment are relative to the drawing. That is:
* vertical text (90 deg) gets centered along Y postition with
* v_align = 'center', and adjusted to the right of X position with
* h_align = 'right'. Another way to look at this is to say
* that text rotation happens first, then alignment.
*
* Original multiple lines code submitted by Remi Ricard.
* Original vertical code submitted by Marlin Viss.
*
* Text routines rewritten by ljb to fix alignment and position problems.
* Here is my explanation and notes. More information and pictures will be
* placed in the PHPlot Reference Manual.
*
* + Process TTF text one line at a time, not as a block. (See below)
* + Flipped top vs bottom vertical alignment. The usual interpretation
* is: bottom align means bottom of the text is at the specified Y
* coordinate. For some reason, PHPlot did left/right the correct way,
* but had top/bottom reversed. I fixed it, and left the default valign
* argument as bottom, but the meaning of the default value changed.
*
* For GD font text, only single-line text is handled by GD, and the
* basepoint is the upper left corner of each text line.
* For TTF text, multi-line text could be handled by GD, with the text
* basepoint at the lower left corner of the first line of text.
* (Behavior of TTF drawing routines on multi-line text is not documented.)
* But you cannot do left/center/right alignment on each line that way,
* or proper line spacing.
* Therefore, for either text type, we have to break up the text into
* lines and position each line independently.
*
* There are 9 alignment modes: Horizontal = left, center, or right, and
* Vertical = top, center, or bottom. Alignment is interpreted relative to
* the image, not as the text is read. This makes sense when you consider
* for example X axis labels. They need to be centered below the marks
* (center, top alignment) regardless of the text angle.
*
* GD font text is supported (by libgd) at 0 degrees and 90 degrees only.
* Multi-line or single line text works with any of the 9 alignment modes.
*
* TTF text can be at any angle. The 9 aligment modes work for all angles,
* but the results might not be what you expect for multi-line text. See
* the PHPlot Reference Manual for pictures and details. In short, alignment
* applies to the orthogonal (aligned with X and Y axes) bounding box that
* contains the text, and to each line in the multi-line text box. Since
* alignment is relative to the image, 45 degree multi-line text aligns
* differently from 46 degree text.
*
* Note that PHPlot allows multi-line text for the 3 titles, and they
* are only drawn at 0 degrees (main and X titles) or 90 degrees (Y title).
* Data labels can also be multi-line, and they can be drawn at any angle.
* -ljb 2007-11-03
*
*/
/*
* ProcessTextGD() - Draw or size GD fixed-font text.
* This is intended for use only by ProcessText().
* $draw_it : True to draw the text, False to just return the orthogonal width and height.
* $font_number : GD font number, 1-5.
* $font_width : Fixed character cell width for the font
* $font_height : Fixed character cell height for the font
* $angle : Text angle in degrees. GD only supports 0 and 90. We treat >= 45 as 90, else 0.
* $x, $y : Reference point for the text (ignored if !$draw_it)
* $color : GD color index to use for drawing the text (ignored if !$draw_it)
* $text : The text to draw or size. Put a newline between lines.
* $h_factor : Horizontal alignment factor: 0(left), .5(center), or 1(right) (ignored if !$draw_it)
* $v_factor : Vertical alignment factor: 0(top), .5(center), or 1(bottom) (ignored if !$draw_it)