]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/external_packages/phplot/phplot.php
bacula-web: Upgraded phplot from version 5.2.0 to 5.3.1
[bacula/bacula] / gui / bacula-web / external_packages / phplot / phplot.php
1 <?php
2 /* $Id: phplot.php,v 1.216 2011/01/16 01:19:55 lbayuk Exp $ */
3 /*
4  * PHPLOT Version 5.3.1
5  *
6  * A PHP class for creating scientific and business charts
7  * Visit http://sourceforge.net/projects/phplot/
8  * for PHPlot documentation, downloads, and discussions.
9  * ---------------------------------------------------------------------
10  * Copyright (C) 1998-2011 Afan Ottenheimer
11  *
12  * This is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation;
15  * version 2.1 of the License.
16  *
17  * This software is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this software; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  * ---------------------------------------------------------------------
26  *
27  * Co-author and maintainer (2003-2005)
28  * Miguel de Benito Delgado <nonick AT vodafone DOT es>
29  *
30  * Maintainer (2006-present)
31  * <lbayuk AT users DOT sourceforge DOT net>
32  *
33  * Requires PHP 5.2.x or later. (PHP 4 is unsupported as of Jan 2008)
34  */
35
36 class PHPlot
37 {
38     /* Declare class variables which are initialized to static values. Many more class variables
39      * are used, defined as needed, but are unset by default.
40      * All these are declared as public. While it is tempting to make them private or protected, this
41      * is avoided for two reasons. First, it will break existing code, since all member variables
42      * were public in PHP4 and who knows what internal variables people used. Second, it makes
43      * testing harder and less effective. Nevertheless, your code should not modify these.
44      */
45
46     public $is_inline = FALSE;             // FALSE = Sends headers, TRUE = sends just raw image data
47     public $browser_cache = FALSE;         // FALSE = Sends headers for browser to not cache the image,
48                                            // (only if is_inline = FALSE also)
49     public $print_image = TRUE;            // DrawGraph calls PrintImage. See SetPrintImage
50
51     public $safe_margin = 5;               // Extra margin used in several places, in pixels
52
53     public $x_axis_position = '';          // X axis position in Y world coordinates, blank for default.
54     public $y_axis_position = '';          // Y axis position in X world coordinates, blank for default.
55
56     public $xscale_type = 'linear';        // linear, log
57     public $yscale_type = 'linear';
58
59 //Fonts
60     public $use_ttf  = FALSE;              // Use True Type Fonts by default?
61     public $ttf_path = '.';                // Default path to look in for TT Fonts.
62     // public $default_ttfont;             // Initialized in GetDefaultTTFont
63     public $line_spacing = 4;              // Controls line spacing of multi-line labels
64
65     // Label angles: 0 or 90 degrees for fixed fonts, any for TTF
66     public $x_label_angle = 0;             // For X tick labels
67     // public $x_data_label_angle;         // For X data labels; defaults to x_label_angle - see CheckLabels()
68     public $y_label_angle = 0;             // For Y tick labels
69     public $y_data_label_angle = 0;        // For Y data labels
70
71 //Formats
72     public $file_format = 'png';
73     public $output_file = '';              // For output to a file instead of stdout
74
75 //Data
76     public $data_type = 'text-data';       // Structure of the data array
77     public $plot_type = 'linepoints';      // See $plots[] below
78
79     public $label_scale_position = 0.5;    // Shifts data labels in pie charts. 1 = top, 0 = bottom
80     public $group_frac_width = 0.7;        // Bars use this fraction (0 to 1) of a group's space
81     public $bar_extra_space = 0.5;         // Number of extra bar's worth of space in a group
82     public $bar_width_adjust = 1;          // 1 = bars of normal width, must be > 0
83
84 // Titles
85     public $title_txt = '';
86
87     public $x_title_txt = '';
88     public $x_title_pos = 'none';          // plotdown, plotup, both, none
89
90     public $y_title_txt = '';
91     public $y_title_pos = 'none';          // plotleft, plotright, both, none
92
93 //Labels
94     // There are two types of labels in PHPlot:
95     //    Tick labels: Follow the grid, next to ticks in axis.
96     //                 Are drawn at grid drawing time, by DrawXTicks() and DrawYTicks()
97     //    Data labels: Follow the data points, and can be placed on the axis or the plot (x/y)
98     //                 Are drawn at graph plotting time, by Draw*DataLabel(), called by DrawLines(), etc.
99     //                 DrawXDataLabel() also draws vertical lines to data points, depending on
100     //                 draw_x_data_label_lines.
101     // Tick Labels
102     // Tick and Data label positions are not initialized, because PHPlot needs to tell if they
103     // defaulted or are set by the user. See CheckLabels() for details. The variables and
104     // effective defaults are shown here in comments (but CheckLabels adjusts the defaults).
105     // public $x_tick_label_pos = 'plotdown';     // X tick label position
106     // public $y_tick_label_pos = 'plotleft';     // Y tick label position
107     // public $x_data_label_pos = 'plotdown';     // X data label position
108     // public $y_data_label_pos = 'none';         // Y data label position
109
110     public $draw_x_data_label_lines = FALSE;   // Draw a line from the data point to the axis?
111
112     // Label format controls: (for tick, data and plot labels)
113     // Unset by default, these array members are used as needed for 'x' (x tick labels), 'xd' (x data
114     // labels), 'y' (y tick labels), and 'yd' (y data labels).
115     //    type, precision, prefix, suffix, time_format, printf_format, custom_callback, custom_arg.
116     // These replace the former: x_label_type, x_time_format, x_precision (similar for y), data_units_text.
117     public $label_format = array('x' => array(), 'xd' => array(), 'y' => array(), 'yd' => array());
118     // data_units_text is retained for backward compatibility, because there was never a function
119     // to set it. Use the 'suffix' argument to Set[XY]LabelType instead.
120     public $data_units_text = '';              // Units text for 'data' labels (i.e: 'ยค', '$', etc.)
121
122 // Legend
123     public $legend = '';                       // An array with legend titles
124     // These variables are unset to take default values:
125     // public $legend_x_pos;                   // User-specified upper left coordinates of legend box
126     // public $legend_y_pos;
127     // public $legend_xy_world;                // If set, legend_x/y_pos are world coords, else pixel coords
128     // public $legend_text_align;              // left or right, Unset means right
129     // public $legend_colorbox_align;          // left, right, or none; Unset means same as text_align
130
131 //Ticks
132     public $x_tick_length = 5;                 // tick length in pixels for upper/lower axis
133     public $y_tick_length = 5;                 // tick length in pixels for left/right axis
134
135     public $x_tick_cross = 3;                  // ticks cross x axis this many pixels
136     public $y_tick_cross = 3;                  // ticks cross y axis this many pixels
137
138     public $x_tick_pos = 'plotdown';           // plotdown, plotup, both, xaxis, none
139     public $y_tick_pos = 'plotleft';           // plotright, plotleft, both, yaxis, none
140
141     public $num_x_ticks = '';
142     public $num_y_ticks = '';
143
144     public $x_tick_inc = '';                   // Set num_x_ticks or x_tick_inc, not both.
145     public $y_tick_inc = '';                   // Set num_y_ticks or y_tick_inc, not both.
146
147     public $skip_top_tick = FALSE;
148     public $skip_bottom_tick = FALSE;
149     public $skip_left_tick = FALSE;
150     public $skip_right_tick = FALSE;
151
152 //Grid Formatting
153     // public $draw_x_grid = FALSE;            // Default is False except for swapped data type
154     // public $draw_y_grid = TRUE;             // Default is True except for swapped data type
155
156     public $dashed_grid = TRUE;
157     public $grid_at_foreground = FALSE;        // Chooses whether to draw the grid below or above the graph
158
159 //Colors and styles       (all colors can be array (R,G,B) or named color)
160     public $color_array = 'small';             // 'small', 'large' or array (define your own colors)
161                                             // See rgb.inc.php and SetRGBArray()
162     public $default_colors = array(       // The default colors for data and error bars
163         'SkyBlue', 'green', 'orange', 'blue', 'red', 'DarkGreen', 'purple', 'peru',
164         'cyan', 'salmon', 'SlateBlue', 'YellowGreen', 'magenta', 'aquamarine1', 'gold', 'violet');
165
166     // See SetDefaultStyles() for default colors for PHPlot elements.
167
168     public $line_widths = 1;                  // single value or array
169     public $line_styles = array('solid', 'solid', 'dashed');   // single value or array
170     public $dashed_style = '2-4';              // colored dots-transparent dots
171
172     public $point_sizes = array(6);            // Array of sizes for points. See CheckPointParams()
173     public $point_shapes = array(              // Array of point shapes. See SetPointShapes() and DrawDot()
174           'diamond', 'dot', 'delta', 'home', 'yield', 'box', 'circle', 'up', 'down', 'cross'
175        );
176
177     public $error_bar_size = 5;                // right and left size of tee
178     public $error_bar_shape = 'tee';           // 'tee' or 'line'
179     public $error_bar_line_width = 1;          // single value (or array TODO)
180
181     public $plot_border_type = 'sides';        // left, right, top, bottom, sides, none, full; or array
182     public $image_border_type = 'none';        // 'raised', 'plain', 'none'
183     // public $image_border_width;             // NULL, 0, or unset for default. Default depends on type.
184
185     public $shading = 5;                       // 0 for no shading, > 0 is size of shadows in pixels
186
187     public $draw_plot_area_background = FALSE;
188     public $draw_broken_lines = FALSE;          // Tells not to draw lines for missing Y data.
189
190 //Miscellaneous
191     public $callbacks = array(                  // Valid callback reasons (see SetCallBack)
192         'draw_setup' => NULL,
193         'draw_image_background' => NULL,
194         'draw_plotarea_background' => NULL,
195         'draw_titles' => NULL,
196         'draw_axes' => NULL,
197         'draw_graph' => NULL,
198         'draw_border' => NULL,
199         'draw_legend' => NULL,
200         'draw_all' => NULL,
201         'data_color' => NULL,
202         'debug_textbox' => NULL,  // For testing/debugging text box alignment
203         'debug_scale' => NULL,    // For testing/debugging scale setup
204     );
205
206     // Defined plot types static array:
207     // Array key is the plot type. (Upper case letters are not allowed due to CheckOption)
208     // Value is an array with these keys:
209     //   draw_method (required) : Class method to call to draw the plot.
210     //   draw_arg : Optional array of arguments to pass to draw_method.
211     //   draw_axes : If FALSE, do not draw X/Y axis lines, labels, ticks, grid, titles.
212     //   abs_vals, sum_vals : Data array processing flags. See FindDataLimits().
213     static protected $plots = array(
214         'area' => array(
215             'draw_method' => 'DrawArea',
216             'abs_vals' => TRUE,
217         ),
218         'bars' => array(
219             'draw_method' => 'DrawBars',
220         ),
221         'candlesticks' => array(
222             'draw_method' => 'DrawOHLC',
223             'draw_arg' => array(TRUE, FALSE), // Draw candlesticks, only fill if "closed down"
224         ),
225         'candlesticks2' => array(
226             'draw_method' => 'DrawOHLC',
227             'draw_arg' => array(TRUE, TRUE), // Draw candlesticks, fill always
228         ),
229         'linepoints' => array(
230             'draw_method' => 'DrawLinePoints',
231         ),
232         'lines' => array(
233             'draw_method' => 'DrawLines',
234         ),
235         'ohlc' => array(
236             'draw_method' => 'DrawOHLC',
237             'draw_arg' => array(FALSE), // Don't draw candlesticks
238         ),
239         'pie' => array(
240             'draw_method' => 'DrawPieChart',
241             'draw_axes' => FALSE,
242             'abs_vals' => TRUE,
243         ),
244         'points' => array(
245             'draw_method' => 'DrawDots',
246         ),
247         'squared' => array(
248             'draw_method' => 'DrawSquared',
249         ),
250         'stackedarea' => array(
251             'draw_method' => 'DrawArea',
252             'draw_arg' => array(TRUE), // Tells DrawArea to draw stacked area plot
253             'sum_vals' => TRUE,
254             'abs_vals' => TRUE,
255         ),
256         'stackedbars' => array(
257             'draw_method' => 'DrawStackedBars',
258             'sum_vals' => TRUE,
259         ),
260         'thinbarline' => array(
261             'draw_method' => 'DrawThinBarLines',
262         ),
263     );
264
265 //////////////////////////////////////////////////////
266 //BEGIN CODE
267 //////////////////////////////////////////////////////
268
269     /*
270      * Constructor: Setup img resource, colors and size of the image, and font sizes.
271      *
272      *   $which_width : Image width in pixels.
273      *   $which_height : Image height in pixels.
274      *   $which_output_file : Filename for output.
275      *   $which_input_file : Path to a file to be used as background.
276      */
277     function PHPlot($which_width=600, $which_height=400, $which_output_file=NULL, $which_input_file=NULL)
278     {
279         $this->SetRGBArray($this->color_array);
280
281         if ($which_output_file)
282             $this->SetOutputFile($which_output_file);
283
284         if ($which_input_file) {
285             $this->SetInputFile($which_input_file);
286         } else {
287             $this->image_width = $which_width;
288             $this->image_height = $which_height;
289
290             $this->img = ImageCreate($this->image_width, $this->image_height);
291             if (! $this->img)
292                 return $this->PrintError('PHPlot(): Could not create image resource.');
293         }
294
295         $this->SetDefaultStyles();
296         $this->SetDefaultFonts();
297     }
298
299     /*
300      * Reads an image file. Stores width and height, and returns the image
301      * resource. On error, calls PrintError and returns False.
302      * This is used by the constructor via SetInputFile, and by tile_img().
303      */
304     protected function GetImage($image_filename, &$width, &$height)
305     {
306         $error = '';
307         $size = getimagesize($image_filename);
308         if (!$size) {
309             $error = "Unable to query image file $image_filename";
310         } else {
311             $image_type = $size[2];
312             switch ($image_type) {
313             case IMAGETYPE_GIF:
314                 $img = @ ImageCreateFromGIF ($image_filename);
315                 break;
316             case IMAGETYPE_PNG:
317                 $img = @ ImageCreateFromPNG ($image_filename);
318                 break;
319             case IMAGETYPE_JPEG:
320                 $img = @ ImageCreateFromJPEG ($image_filename);
321                 break;
322             default:
323                 $error = "Unknown image type ($image_type) for image file $image_filename";
324                 break;
325             }
326         }
327         if (empty($error) && !$img) {
328             // getimagesize is OK, but GD won't read it. Maybe unsupported format.
329             $error = "Failed to read image file $image_filename";
330         }
331         if (!empty($error)) {
332             return $this->PrintError("GetImage(): $error");
333         }
334         $width = $size[0];
335         $height = $size[1];
336         return $img;
337     }
338
339     /*
340      * Selects an input file to be used as background for the whole graph.
341      * This resets the graph size to the image's size.
342      * Note: This is used by the constructor. It is deprecated for direct use.
343      */
344     function SetInputFile($which_input_file)
345     {
346         $im = $this->GetImage($which_input_file, $this->image_width, $this->image_height);
347         if (!$im)
348             return FALSE;  // GetImage already produced an error message.
349
350         // Deallocate any resources previously allocated
351         if (isset($this->img))
352             imagedestroy($this->img);
353
354         $this->img = $im;
355
356         // Do not overwrite the input file with the background color.
357         $this->done['background'] = TRUE;
358
359         return TRUE;
360     }
361
362 /////////////////////////////////////////////
363 //////////////                         COLORS
364 /////////////////////////////////////////////
365
366     /*
367      * Allocate a GD color index for a color specified by a 4 component array.
368      * When a color is requested, it is parsed and checked by SetRGBColor, and then saved as an array
369      * of (R,G,B,A) components. At graph drawing time, this function is used to allocate the color.
370      *   $color : The color specification as a 4 component array: R, G, B, A.
371      * Returns: A GD color index that can be used when drawing.
372      */
373     protected function GetColorIndex($color)
374     {
375         list($r, $g, $b, $a) = $color;
376         return imagecolorresolvealpha($this->img, $r, $g, $b, $a);
377     }
378
379     /*
380      * Allocate an array of GD color indexes for an array of color specifications.
381      * This is used for the data_colors array, for example.
382      *  $color_array : Array of color specifications, each an array of R,G,B,A components.
383      *     This must use 0-based sequential integer indexes.
384      *  $max_colors : Limit color allocation to no more than this.
385      * Returns an array of GD color indexes.
386      */
387     protected function GetColorIndexArray($color_array, $max_colors)
388     {
389         $n = min(count($color_array), $max_colors);
390         $result = array();
391         for ($i = 0; $i < $n; $i++)
392             $result[] = $this->GetColorIndex($color_array[$i]);
393         return $result;
394     }
395
396     /*
397      * Allocate an array of GD color indexes for darker shades of an array of color specifications.
398      *  $color_array : Array of color specifications, each an array of R,G,B,A components.
399      *  $max_colors : Limit color allocation to this many colors from the array.
400      * Returns an array of GD color indexes.
401      */
402     protected function GetDarkColorIndexArray($color_array, $max_colors)
403     {
404         $n = min(count($color_array), $max_colors);
405         $result = array();
406         for ($i = 0; $i < $n; $i++)
407             $result[] = $this->GetDarkColorIndex($color_array[$i]);
408         return $result;
409     }
410
411     /*
412      * Allocate a GD color index for a darker shade of a color specified by a 4 component array.
413      * See notes for GetColorIndex() above.
414      *   $color : The color specification as a 4 component array: R, G, B, A.
415      * Returns: A GD color index that can be used when drawing.
416      */
417     protected function GetDarkColorIndex($color)
418     {
419         list ($r, $g, $b, $a) = $color;
420         $r = max(0, $r - 0x30);
421         $g = max(0, $g - 0x30);
422         $b = max(0, $b - 0x30);
423         return imagecolorresolvealpha($this->img, $r, $g, $b, $a);
424     }
425
426     /*
427      * Sets/reverts all colors and styles to their defaults.
428      */
429     protected function SetDefaultStyles()
430     {
431         $this->SetDefaultDashedStyle($this->dashed_style);
432         $this->SetImageBorderColor(array(194, 194, 194));
433         $this->SetPlotBgColor('white');
434         $this->SetBackgroundColor('white');
435         $this->SetTextColor('black');
436         $this->SetGridColor('black');
437         $this->SetLightGridColor('gray');
438         $this->SetTickColor('black');
439         $this->SetTitleColor('black');
440         // These functions set up the default colors when called without parameters
441         $this->SetDataColors();
442         $this->SetErrorBarColors();
443         $this->SetDataBorderColors();
444         return TRUE;
445     }
446
447     /*
448      * Set the image background color to $which_color.
449      */
450     function SetBackgroundColor($which_color)
451     {
452         return (bool)($this->bg_color = $this->SetRGBColor($which_color));
453     }
454
455     /*
456      * Set the plot area background color (if enabled) to $which_color.
457      */
458     function SetPlotBgColor($which_color)
459     {
460         return (bool)($this->plot_bg_color = $this->SetRGBColor($which_color));
461     }
462
463     /*
464      * Set the color of the titles (main, X, and Y) to $which_color.
465      * See also SetXTitleColor and SetYTitleColor.
466      */
467     function SetTitleColor($which_color)
468     {
469         return (bool)($this->title_color = $this->SetRGBColor($which_color));
470     }
471
472     /*
473      * Set the color of the X title to $which_color.
474      * This overrides the color set with SetTitleColor.
475      */
476     function SetXTitleColor($which_color)
477     {
478         return (bool)($this->x_title_color = $this->SetRGBColor($which_color));
479     }
480
481     /*
482      * Set the color of the Y title to $which_color.
483      * This overrides the color set with SetTitleColor.
484      */
485     function SetYTitleColor($which_color)
486     {
487         return (bool)($this->y_title_color = $this->SetRGBColor($which_color));
488     }
489
490     /*
491      * Set the color of the axis tick marks to $which_color.
492      */
493     function SetTickColor($which_color)
494     {
495         return (bool)($this->tick_color = $this->SetRGBColor($which_color));
496     }
497
498     /*
499      * Deprecated. Use SetTitleColor()
500      */
501     function SetLabelColor($which_color)
502     {
503         return $this->SetTitleColor($which_color);
504     }
505
506     /*
507      * Set the general text color (tick and data labels, legend, etc) to $which_color.
508      */
509     function SetTextColor($which_color)
510     {
511         return (bool)($this->text_color = $this->SetRGBColor($which_color));
512     }
513
514     /*
515      * Set the X and Y grid colors to $which_color. Also sets the data label line color.
516      */
517     function SetLightGridColor($which_color)
518     {
519         return (bool)($this->light_grid_color = $this->SetRGBColor($which_color));
520     }
521
522     /*
523      * Set the color used for the X and Y axis, plot border, legend border to $which_color.
524      * Note: This has nothing to do with the grid, and we don't recall where this name came from.
525      */
526     function SetGridColor($which_color)
527     {
528         return (bool)($this->grid_color = $this->SetRGBColor($which_color));
529     }
530
531     /*
532      * Set the color used for the image border to $which_color.
533      */
534     function SetImageBorderColor($which_color)
535     {
536         return (bool)($this->i_border = $this->SetRGBColor($which_color));
537     }
538
539     /*
540      * Designate color $which_color to be transparent, if supported by the image format.
541      */
542     function SetTransparentColor($which_color)
543     {
544         return (bool)($this->transparent_color = $this->SetRGBColor($which_color));
545     }
546
547     /*
548      * Sets the array of colors to be used. It can be user defined, a small predefined one
549      * or a large one included from 'rgb.inc.php'.
550      *
551      *    $which_color_array : A color array, or 'small' or 'large'.
552      * Color arrays map color names into arrays of R, G, B and optionally A values.
553      */
554     function SetRGBArray($which_color_array)
555     {
556         if (is_array($which_color_array)) {           // User defined array
557             $this->rgb_array = $which_color_array;
558         } elseif ($which_color_array == 'small') {      // Small predefined color array
559             $this->rgb_array = array(
560                 'white'          => array(255, 255, 255),
561                 'snow'           => array(255, 250, 250),
562                 'PeachPuff'      => array(255, 218, 185),
563                 'ivory'          => array(255, 255, 240),
564                 'lavender'       => array(230, 230, 250),
565                 'black'          => array(  0,   0,   0),
566                 'DimGrey'        => array(105, 105, 105),
567                 'gray'           => array(190, 190, 190),
568                 'grey'           => array(190, 190, 190),
569                 'navy'           => array(  0,   0, 128),
570                 'SlateBlue'      => array(106,  90, 205),
571                 'blue'           => array(  0,   0, 255),
572                 'SkyBlue'        => array(135, 206, 235),
573                 'cyan'           => array(  0, 255, 255),
574                 'DarkGreen'      => array(  0, 100,   0),
575                 'green'          => array(  0, 255,   0),
576                 'YellowGreen'    => array(154, 205,  50),
577                 'yellow'         => array(255, 255,   0),
578                 'orange'         => array(255, 165,   0),
579                 'gold'           => array(255, 215,   0),
580                 'peru'           => array(205, 133,  63),
581                 'beige'          => array(245, 245, 220),
582                 'wheat'          => array(245, 222, 179),
583                 'tan'            => array(210, 180, 140),
584                 'brown'          => array(165,  42,  42),
585                 'salmon'         => array(250, 128, 114),
586                 'red'            => array(255,   0,   0),
587                 'pink'           => array(255, 192, 203),
588                 'maroon'         => array(176,  48,  96),
589                 'magenta'        => array(255,   0, 255),
590                 'violet'         => array(238, 130, 238),
591                 'plum'           => array(221, 160, 221),
592                 'orchid'         => array(218, 112, 214),
593                 'purple'         => array(160,  32, 240),
594                 'azure1'         => array(240, 255, 255),
595                 'aquamarine1'    => array(127, 255, 212)
596                 );
597         } elseif ($which_color_array == 'large')  {    // Large color array
598             if (!@include('rgb.inc.php')) {
599                 return $this->PrintError("SetRGBArray(): Large color map could not be loaded\n"
600                                        . "from 'rgb.inc.php'.");
601             }
602             $this->rgb_array = $ColorArray;
603         } else {                                        // Default to black and white only.
604             $this->rgb_array = array('white' => array(255, 255, 255), 'black' => array(0, 0, 0));
605         }
606
607         return TRUE;
608     }
609
610     /*
611      * Parse a color description and return the color component values.
612      * Arguments:
613      *   $color_asked : The desired color description, in one of these forms:
614      *       Component notation: array(R, G, B) or array(R, G, B, A) with each
615      *          in the range described below for the return value.
616      *          Examples: (255,255,0)  (204,0,0,30)
617      *       Hex notation: "#RRGGBB" or "#RRGGBBAA" where each pair is a 2 digit hex number.
618      *          Examples: #FF00FF (magenta)   #0000FF40 (Blue with alpha=64/127)
619      *       Named color in the current colormap, with optional suffix ":alpha" for alpha value.
620      *          Examples:  blue   red:60  yellow:20
621      *   $alpha : optional default alpha value. This is applied to the color if it doesn't
622      *       already have an alpha value. If not supplied, colors are opaque (alpha=0) by default.
623      *
624      * Returns an array describing a color as (R, G, B, Alpha).
625      * R, G, and B are integers 0-255, and Alpha is 0 (opaque) to 127 (transparent).
626      * Note: This function should be considered 'protected', and is not documented for public use.
627      */
628     function SetRGBColor($color_asked, $alpha = 0)
629     {
630         if (empty($color_asked)) {
631             $ret_val = array(0, 0, 0);
632
633         } elseif (is_array($color_asked) && (($n = count($color_asked)) == 3 || $n == 4) ) {
634             // Already an array of 3 or 4 elements:
635             $ret_val = $color_asked;
636
637         } elseif (preg_match('/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i',
638                              $color_asked, $ss)) {
639             // #RRGGBB or #RRGGBBAA notation:
640             $ret_val = array(hexdec($ss[1]), hexdec($ss[2]), hexdec($ss[3]));
641             if (isset($ss[4])) $ret_val[] = hexdec($ss[4]);
642
643         } elseif (isset($this->rgb_array[$color_asked])) {
644             // Color by name:
645             $ret_val = $this->rgb_array[$color_asked];
646
647         } elseif (preg_match('/(.+):([\d]+)$/', $color_asked, $ss)
648                   && isset($this->rgb_array[$ss[1]])) {
649             // Color by name with ":alpha" suffix, alpha is a decimal number:
650             $ret_val = $this->rgb_array[$ss[1]];
651             $ret_val[3] = (int)$ss[2];
652
653         } else {
654             return $this->PrintError("SetRGBColor(): Color '$color_asked' is not valid.");
655         }
656
657         // Append alpha if not already provided for:
658         if (count($ret_val) == 3)
659             $ret_val[] = $alpha;
660         return $ret_val;
661     }
662
663     /*
664      * Sets the colors for the data, with optional default alpha value (for PHPlot_truecolor only)
665      * Cases are:
666      *    SetDataColors(array(...))  : Use the supplied array as the color map.
667      *    SetDataColors(colorname)   : Use an array of just colorname as the color map.
668      *    SetDataColors() or SetDataColors(NULL) : Load default color map if no color map is already set.
669      *    SetDataColors('') or SetDataColors(False) : Load default color map (even if one is already set).
670      *  $which_border is passed to SetDataBorderColors, for backward compatibility.
671      *  $alpha is a default Alpha to apply to all data colors that do not have alpha.
672      *    The default for this is NULL, not 0, so we can tell if it was defaulted. But the effective
673      *    default value is 0 (opaque).
674      */
675     function SetDataColors($which_data = NULL, $which_border = NULL, $alpha = NULL)
676     {
677         if (is_array($which_data)) {
678             $colors = $which_data;  // Use supplied array
679         } elseif (!empty($which_data)) {
680             $colors = array($which_data);  // Use supplied single color
681         } elseif (empty($this->data_colors) || !is_null($which_data)) {
682             $colors = $this->default_colors;  // Use default color array
683         } else {
684             // which_data is NULL or missing and a color array is already set.
685             // The existing color array is left alone, except that if $alpha is
686             // given this will replace the alpha value of each existing color.
687             // This makes SetDataColors(NULL, NULL, $alpha) work.
688             if (isset($alpha)) {
689                 $n_colors = count($this->data_colors);
690                 for ($i = 0; $i < $n_colors; $i++) {
691                     $this->data_colors[$i][3] = $alpha; // Component 3 = alpha value
692                 }
693             }
694             // No need to reparse the colors or anything else.
695             return TRUE;
696         }
697
698         if (!isset($alpha))
699             $alpha = 0; // Actual default is opaque colors.
700
701         // Check each color and convert to array (r,g,b,a) form.
702         // Use the $alpha argument as a default for the alpha value of each color.
703         $this->data_colors = array();
704         foreach ($colors as $color) {
705             $color_array = $this->SetRGBColor($color, $alpha);
706             if (!$color_array) return FALSE; // SetRGBColor already did an error message.
707             $this->data_colors[] = $color_array;
708         }
709
710         // For past compatibility:
711         return $this->SetDataBorderColors($which_border);
712     }
713
714     /*
715      * Set the colors for the bars and stacked bars outlines.
716      * Argument usage is similar to SetDataColors(), except the default is just black.
717      */
718     function SetDataBorderColors($which_br = NULL)
719     {
720         if (is_array($which_br)) {
721             $colors = $which_br; // Use supplied array
722         } elseif (!empty($which_br)) {
723             $colors = array($which_br);  // Use supplied single color
724         } elseif (empty($this->data_border_colors) || !is_null($which_br)) {
725             $colors = array('black'); // Use default
726         } else {
727             return TRUE; // Do nothing: which_br is NULL or missing and a color array is already set.
728         }
729
730         // Check each color and convert to array (r,g,b,a) form.
731         $this->data_border_colors = array();
732         foreach ($colors as $color) {
733             $color_array = $this->SetRGBColor($color);
734             if (!$color_array) return FALSE; // SetRGBColor already did an error message.
735             $this->data_border_colors[] = $color_array;
736         }
737         return TRUE;
738     }
739
740     /*
741      * Sets the colors for the data error bars.
742      * Argument usage is the same as SetDataColors().
743      */
744     function SetErrorBarColors($which_err = NULL)
745     {
746         if (is_array($which_err)) {
747             $colors = $which_err;  // Use supplied array
748         } elseif (!empty($which_err)) {
749             $colors = array($which_err);  // Use supplied single color
750         } elseif (empty($this->error_bar_colors) || !is_null($which_err)) {
751             $colors = $this->default_colors;  // Use default color array
752         } else {
753             return TRUE; // Do nothing: which_err is NULL or missing and a color array is already set.
754         }
755
756         // Check each color and convert to array (r,g,b,a) form.
757         $this->error_bar_colors = array();
758         foreach ($colors as $color) {
759             $color_array = $this->SetRGBColor($color);
760             if (!$color_array) return FALSE; // SetRGBColor already did an error message.
761             $this->error_bar_colors[] = $color_array;
762         }
763         return TRUE;
764     }
765
766     /*
767      * Sets the default dashed line style.
768      *   $which_style : A string specifying the dashed line style, as alternating numbers
769      *         of the length (in pixels) of lines and spaces, separated by dashes.
770      *   For example: '2-3-1-2' means 2 dots of color, 3 transparent, 1 color, then 2 transparent.
771      *   This builds a string which will evaluate to an array of integers. Each colored dot
772      *   is  '$which_ndxcol' and each transparent dot is 'IMG_COLOR_TRANSPARENT'. When SetDashedStyle()
773      *   eval's this with $which_ndxcol set, the result is a GD line style array.
774      */
775     function SetDefaultDashedStyle($which_style)
776     {
777         // Explode "numcol-numtrans-numcol-numtrans..." into segment counts:
778         $asked = explode('-', $which_style);
779
780         if (count($asked) < 2) {
781             return $this->PrintError("SetDefaultDashedStyle(): Wrong parameter '$which_style'.");
782         }
783
784         // Build the string to be evaluated later by SetDashedStyle() with $which_ndxcolor set.
785         $result = '';
786         $vals = array('$which_ndxcol,', 'IMG_COLOR_TRANSPARENT,');
787         $index = 0;
788         foreach ($asked as $n) {
789             $result .= str_repeat($vals[$index], $n);
790             $index = 1 - $index;
791         }
792         $this->default_dashed_style = "array($result)";
793
794         return TRUE;
795     }
796
797     /*
798      * Sets the style before drawing a dashed line. Defaults to $this->default_dashed_style
799      *    $which_ndxcol : Color index to be used.
800      */
801     protected function SetDashedStyle($which_ndxcol)
802     {
803         // See SetDefaultDashedStyle() to understand this.
804         eval ("\$style = $this->default_dashed_style;");
805         return imagesetstyle($this->img, $style);
806     }
807
808     /*
809      * Set line widths for each data set.
810      *   $which_lw : Array of line widths in pixels, or a single value to use for all data sets.
811      */
812     function SetLineWidths($which_lw=NULL)
813     {
814         if (is_array($which_lw)) {
815             $this->line_widths = $which_lw; // Use provided array
816         } elseif (!is_null($which_lw)) {
817             $this->line_widths = array($which_lw); // Convert value to array
818         }
819         return TRUE;
820     }
821
822     /*
823      * Set line style ('solid' or 'dashed') for each data set.
824      *   $which_ls : Array of keywords, or a single keyword to use for all data sets.
825      */
826     function SetLineStyles($which_ls=NULL)
827     {
828         if (is_array($which_ls)) {
829             $this->line_styles = $which_ls; // Use provided array
830         } elseif (!is_null($which_ls)) {
831             $this->line_styles = ($which_ls) ? array($which_ls) : array('solid');
832         }
833         return TRUE;
834     }
835
836 /////////////////////////////////////////////
837 //////////////                 TEXT and FONTS
838 /////////////////////////////////////////////
839
840     /*
841      * Controls the line spacing of multi-line labels.
842      *   $which_spc : Line spacing factor for text
843      * For GD text, this is the number of pixels between lines.
844      * For TTF text, it controls line spacing in proportion to the normal
845      * spacing defined by the font.
846      */
847     function SetLineSpacing($which_spc)
848     {
849         $this->line_spacing = $which_spc;
850         return TRUE;
851     }
852
853     /*
854      * Select the default font type to use.
855      *   $which_ttf : True to default to TrueType, False to default to GD (fixed) fonts.
856      * This also resets all font settings to the defaults.
857      */
858     function SetUseTTF($which_ttf)
859     {
860         $this->use_ttf = $which_ttf;
861         return $this->SetDefaultFonts();
862     }
863
864     /*
865      * Sets the directory name to look into for TrueType fonts.
866      */
867     function SetTTFPath($which_path)
868     {
869         if (!is_dir($which_path) || !is_readable($which_path)) {
870             return $this->PrintError("SetTTFPath(): $which_path is not a valid path.");
871         }
872         $this->ttf_path = $which_path;
873         return TRUE;
874     }
875
876     /*
877      * Sets the default TrueType font and updates all fonts to that.
878      * The default font might be a full path, or relative to the TTFPath,
879      * so let SetFont check that it exists.
880      * Side effects: Enables use of TrueType fonts as the default font type,
881      * and resets all font settings.
882      */
883     function SetDefaultTTFont($which_font)
884     {
885         $this->default_ttfont = $which_font;
886         return $this->SetUseTTF(TRUE);
887     }
888
889     /*
890      * Return the default TrueType font name. If no default has been set,
891      * this tries some likely candidates for a font which can be loaded.
892      * If it finds one that works, that becomes the default TT font.
893      * If there is no default and it cannot find a working font, it falls
894      * back to the original PHPlot default (which will not likely work either).
895      */
896     protected function GetDefaultTTFont()
897     {
898         if (!isset($this->default_ttfont)) {
899             // No default font yet. Try some common sans-serif fonts.
900             $fonts = array('LiberationSans-Regular.ttf',  // For Linux with a correct GD font search path
901                            'Verdana.ttf', 'Arial.ttf', 'Helvetica.ttf', // For Windows, maybe others
902                            'ttf-liberation/LiberationSans-Regular.ttf', // For Debian, Ubuntu, and friends
903                            'benjamingothic.ttf',  // Original PHPlot default 
904                           );
905             foreach ($fonts as $font) {
906                 // First try the font name alone, to see if GD can find and load it.
907                 if (@imagettfbbox(10, 0, $font, "1") !== False)
908                     break;
909                 // If the font wasn't found, try it with the default TTF path in front.
910                 $font_with_path = $this->ttf_path . DIRECTORY_SEPARATOR . $font;
911                 if (@imagettfbbox(10, 0, $font_with_path, "1") !== False) {
912                     $font = $font_with_path;
913                     break;
914                 }
915             }
916             // We either have a working font, or are using the last one regardless.
917             $this->default_ttfont = $font;
918         }
919         return $this->default_ttfont;
920     }
921
922     /*
923      * Sets fonts to their defaults
924      */
925     protected function SetDefaultFonts()
926     {
927         // TTF:
928         if ($this->use_ttf) {
929             return $this->SetFont('generic', '', 8)
930                 && $this->SetFont('title', '', 14)
931                 && $this->SetFont('legend', '', 8)
932                 && $this->SetFont('x_label', '', 6)
933                 && $this->SetFont('y_label', '', 6)
934                 && $this->SetFont('x_title', '', 10)
935                 && $this->SetFont('y_title', '', 10);
936         }
937         // Fixed GD Fonts:
938         return $this->SetFont('generic', 2)
939             && $this->SetFont('title', 5)
940             && $this->SetFont('legend', 2)
941             && $this->SetFont('x_label', 1)
942             && $this->SetFont('y_label', 1)
943             && $this->SetFont('x_title', 3)
944             && $this->SetFont('y_title', 3);
945     }
946
947     /*
948      * Select a fixed (GD) font for an element.
949      * This allows using a fixed font, even with SetUseTTF(True).
950      *    $which_elem : The element whose font is to be changed.
951      *       One of: title legend generic x_label y_label x_title y_title
952      *    $which_font : A GD font number 1-5
953      *    $which_spacing (optional) : Line spacing factor
954      */
955     function SetFontGD($which_elem, $which_font, $which_spacing = NULL)
956     {
957         if ($which_font < 1 || 5 < $which_font) {
958             return $this->PrintError(__FUNCTION__ . ': Font size must be 1, 2, 3, 4 or 5');
959         }
960         if (!$this->CheckOption($which_elem,
961                                 'generic, title, legend, x_label, y_label, x_title, y_title',
962                                 __FUNCTION__)) {
963             return FALSE;
964         }
965
966         // Store the font parameters: name/size, char cell height and width.
967         $this->fonts[$which_elem] = array('ttf' => FALSE,
968                                           'font' => $which_font,
969                                           'height' => ImageFontHeight($which_font),
970                                           'width' => ImageFontWidth($which_font),
971                                           'line_spacing' => $which_spacing);
972         return TRUE;
973     }
974
975     /*
976      * Select a TrueType font for an element.
977      * This allows using a TrueType font, even with SetUseTTF(False).
978      *    $which_elem : The element whose font is to be changed.
979      *       One of: title legend generic x_label y_label x_title y_title
980      *    $which_font : A TrueType font filename or pathname.
981      *    $which_size : Font point size.
982      *    $which_spacing (optional) : Line spacing factor
983      */
984     function SetFontTTF($which_elem, $which_font, $which_size = 12, $which_spacing = NULL)
985     {
986         if (!$this->CheckOption($which_elem,
987                                 'generic, title, legend, x_label, y_label, x_title, y_title',
988                                 __FUNCTION__)) {
989             return FALSE;
990         }
991
992         // Empty font name means use the default font.
993         if (empty($which_font))
994             $which_font = $this->GetDefaultTTFont();
995         $path = $which_font;
996
997         // First try the font name directly, if not then try with path.
998         // Use GD imagettfbbox() to determine if this is a valid font.
999         // The return $bbox is used below, if valid.
1000         if (($bbox = @imagettfbbox($which_size, 0, $path, "E")) === False) {
1001             $path = $this->ttf_path . DIRECTORY_SEPARATOR . $which_font;
1002             if (($bbox = @imagettfbbox($which_size, 0, $path, "E")) === False) {
1003                 return $this->PrintError(__FUNCTION__ . ": Can't find TrueType font $which_font");
1004             }
1005         }
1006
1007         // Calculate the font height and inherent line spacing. TrueType fonts have this information
1008         // internally, but PHP/GD has no way to directly access it. So get the bounding box size of
1009         // an upper-case character without descenders, and the baseline-to-baseline height.
1010         // Note: In practice, $which_size = $height, maybe +/-1 . But which_size is in points,
1011         // and height is in pixels, and someday GD may be able to tell the difference.
1012         // The character width is saved too, but not used by the normal text drawing routines - it
1013         // isn't necessarily a fixed-space font. It is used in DrawLegend.
1014         $height = $bbox[1] - $bbox[5];
1015         $width = $bbox[2] - $bbox[0];
1016         $bbox = ImageTTFBBox($which_size, 0, $path, "E\nE");
1017         $spacing = $bbox[1] - $bbox[5] - 2 * $height;
1018
1019         // Store the font parameters:
1020         $this->fonts[$which_elem] = array('ttf' => TRUE,
1021                                           'font' => $path,
1022                                           'size' => $which_size,
1023                                           'height' => $height,
1024                                           'width' => $width,
1025                                           'spacing' => $spacing,
1026                                           'line_spacing' => $which_spacing);
1027         return TRUE;
1028     }
1029
1030     /*
1031      * Select Fixed/TrueType font for an element. Which type of font is
1032      * selected depends on the $use_ttf class variable (see SetUseTTF()).
1033      * Before PHPlot supported mixing font types, only this function and
1034      * SetUseTTF were available to select an overall font type, but now
1035      * SetFontGD() and SetFontTTF() can be used for mixing font types.
1036      *    $which_elem : The element whose font is to be changed.
1037      *       One of: title legend generic x_label y_label x_title y_title
1038      *    $which_font : A number 1-5 for fixed fonts, or a TrueType font.
1039      *    $which_size : Ignored for Fixed fonts, point size for TrueType.
1040      *    $which_spacing (optional) : Line spacing factor
1041      */
1042     function SetFont($which_elem, $which_font, $which_size = 12, $line_spacing = NULL)
1043     {
1044         if ($this->use_ttf)
1045             return $this->SetFontTTF($which_elem, $which_font, $which_size, $line_spacing);
1046         return $this->SetFontGD($which_elem, $which_font, $line_spacing);
1047     }
1048
1049     /*
1050      * Return the inter-line spacing for a font.
1051      * This is an internal function, used by ProcessText* and DrawLegend.
1052      *   $font : A font array variable.
1053      * Returns: Spacing, in pixels, between text lines.
1054      */
1055     protected function GetLineSpacing($font)
1056     {
1057         // Use the per-font line spacing preference, if set, else the global value:
1058         if (isset($font['line_spacing']))
1059             $line_spacing = $font['line_spacing'];
1060         else
1061             $line_spacing = $this->line_spacing;
1062
1063         // For GD fonts, that is the spacing in pixels.
1064         // For TTF, adjust based on the 'natural' font spacing (see SetFontTTF):
1065         if ($font['ttf']) {
1066             $line_spacing = (int)($line_spacing * $font['spacing'] / 6.0);
1067         }
1068         return $line_spacing;
1069     }
1070
1071     /*
1072      * Text drawing and sizing functions:
1073      * ProcessText is meant for use only by DrawText and SizeText.
1074      *    ProcessText(True, ...)  - Draw a block of text
1075      *    ProcessText(False, ...) - Just return ($width, $height) of
1076      *       the orthogonal bounding box containing the text.
1077      * ProcessText is further split into separate functions for GD and TTF
1078      * text, due to the size of the code.
1079      *
1080      * Horizontal and vertical alignment are relative to the drawing. That is:
1081      * vertical text (90 deg) gets centered along Y position with
1082      * v_align = 'center', and adjusted to the right of X position with
1083      * h_align = 'right'.  Another way to look at this is to say
1084      * that text rotation happens first, then alignment.
1085      *
1086      * Original multiple lines code submitted by Remi Ricard.
1087      * Original vertical code submitted by Marlin Viss.
1088      *
1089      * Text routines rewritten by ljb to fix alignment and position problems.
1090      * Here is my explanation and notes. More information and pictures will be
1091      * placed in the PHPlot Reference Manual.
1092      *
1093      *    + Process TTF text one line at a time, not as a block. (See below)
1094      *    + Flipped top vs bottom vertical alignment. The usual interpretation
1095      *  is: bottom align means bottom of the text is at the specified Y
1096      *  coordinate. For some reason, PHPlot did left/right the correct way,
1097      *  but had top/bottom reversed. I fixed it, and left the default valign
1098      *  argument as bottom, but the meaning of the default value changed.
1099      *
1100      *    For GD font text, only single-line text is handled by GD, and the
1101      *  basepoint is the upper left corner of each text line.
1102      *    For TTF text, multi-line text could be handled by GD, with the text
1103      *  basepoint at the lower left corner of the first line of text.
1104      *  (Behavior of TTF drawing routines on multi-line text is not documented.)
1105      *  But you cannot do left/center/right alignment on each line that way,
1106      *  or proper line spacing.
1107      *    Therefore, for either text type, we have to break up the text into
1108      *  lines and position each line independently.
1109      *
1110      *    There are 9 alignment modes: Horizontal = left, center, or right, and
1111      *  Vertical = top, center, or bottom. Alignment is interpreted relative to
1112      *  the image, not as the text is read. This makes sense when you consider
1113      *  for example X axis labels. They need to be centered below the marks
1114      *  (center, top alignment) regardless of the text angle.
1115      *  'Bottom' alignment really means baseline alignment.
1116      *
1117      *    GD font text is supported (by libgd) at 0 degrees and 90 degrees only.
1118      *  Multi-line or single line text works with any of the 9 alignment modes.
1119      *
1120      *    TTF text can be at any angle. The 9 alignment modes work for all angles,
1121      *  but the results might not be what you expect for multi-line text. See
1122      *  the PHPlot Reference Manual for pictures and details. In short, alignment
1123      *  applies to the orthogonal (aligned with X and Y axes) bounding box that
1124      *  contains the text, and to each line in the multi-line text box. Since
1125      *  alignment is relative to the image, 45 degree multi-line text aligns
1126      *  differently from 46 degree text.
1127      *
1128      *    Note that PHPlot allows multi-line text for the 3 titles, and they
1129      *  are only drawn at 0 degrees (main and X titles) or 90 degrees (Y title).
1130      *  Data labels can also be multi-line, and they can be drawn at any angle.
1131      *  -ljb 2007-11-03
1132      *
1133      */
1134
1135     /*
1136      * ProcessTextGD() - Draw or size GD fixed-font text.
1137      * This is intended for use only by ProcessText().
1138      *    $draw_it : True to draw the text, False to just return the orthogonal width and height.
1139      *    $font : PHPlot font array (with 'ttf' = False) - see SetFontGD()
1140      *    $angle : Text angle in degrees. GD only supports 0 and 90. We treat >= 45 as 90, else 0.
1141      *    $x, $y : Reference point for the text (ignored if !$draw_it)
1142      *    $color : GD color index to use for drawing the text (ignored if !$draw_it)
1143      *    $text : The text to draw or size. Put a newline between lines.
1144      *    $h_factor : Horizontal alignment factor: 0(left), .5(center), or 1(right) (ignored if !$draw_it)
1145      *    $v_factor : Vertical alignment factor: 0(top), .5(center), or 1(bottom) (ignored if !$draw_it)
1146      * Returns: True, if drawing text, or an array of ($width, $height) if not.
1147      */
1148     protected function ProcessTextGD($draw_it, $font, $angle, $x, $y, $color, $text, $h_factor, $v_factor)
1149     {
1150         // Extract font parameters:
1151         $font_number = $font['font'];
1152         $font_width = $font['width'];
1153         $font_height = $font['height'];
1154         $line_spacing = $this->GetLineSpacing($font);
1155
1156         // Break up the text into lines, trim whitespace, find longest line.
1157         // Save the lines and length for drawing below.
1158         $longest = 0;
1159         foreach (explode("\n", $text) as $each_line) {
1160             $lines[] = $line = trim($each_line);
1161             $line_lens[] = $line_len = strlen($line);
1162             if ($line_len > $longest) $longest = $line_len;
1163         }
1164         $n_lines = count($lines);
1165
1166         // Width, height are based on font size and longest line, line count respectively.
1167         // These are relative to the text angle.
1168         $total_width = $longest * $font_width;
1169         $total_height = $n_lines * $font_height + ($n_lines - 1) * $line_spacing;
1170
1171         if (!$draw_it) {
1172             if ($angle < 45) return array($total_width, $total_height);
1173             return array($total_height, $total_width);
1174         }
1175
1176         $interline_step = $font_height + $line_spacing; // Line-to-line step
1177
1178         if ($angle >= 45) {
1179             // Vertical text (90 degrees):
1180             // (Remember the alignment convention with vertical text)
1181             // For 90 degree text, alignment factors change like this:
1182             $temp = $v_factor;
1183             $v_factor = $h_factor;
1184             $h_factor = 1 - $temp;
1185
1186             $draw_func = 'ImageStringUp';
1187
1188             // Rotation matrix "R" for 90 degrees (with Y pointing down):
1189             $r00 = 0;  $r01 = 1;
1190             $r10 = -1; $r11 = 0;
1191
1192         } else {
1193             // Horizontal text (0 degrees):
1194             $draw_func = 'ImageString';
1195
1196             // Rotation matrix "R" for 0 degrees:
1197             $r00 = 1; $r01 = 0;
1198             $r10 = 0; $r11 = 1;
1199         }
1200
1201         // Adjust for vertical alignment (horizontal text) or horizontal alignment (vertical text):
1202         $factor = (int)($total_height * $v_factor);
1203         $xpos = $x - $r01 * $factor;
1204         $ypos = $y - $r11 * $factor;
1205
1206         // Debug callback provides the bounding box:
1207         if ($this->GetCallback('debug_textbox')) {
1208             if ($angle >= 45) {
1209                 $bbox_width  = $total_height;
1210                 $bbox_height = $total_width;
1211                 $px = $xpos;
1212                 $py = $ypos - (1 - $h_factor) * $total_width;
1213             } else {
1214                 $bbox_width  = $total_width;
1215                 $bbox_height = $total_height;
1216                 $px = $xpos - $h_factor * $total_width;
1217                 $py = $ypos;
1218             }
1219             $this->DoCallback('debug_textbox', $px, $py, $bbox_width, $bbox_height);
1220         }
1221
1222         for ($i = 0; $i < $n_lines; $i++) {
1223
1224             // Adjust for alignment of this line within the text block:
1225             $factor = (int)($line_lens[$i] * $font_width * $h_factor);
1226             $x = $xpos - $r00 * $factor;
1227             $y = $ypos - $r10 * $factor;
1228
1229             // Call ImageString or ImageStringUp:
1230             $draw_func($this->img, $font_number, $x, $y, $lines[$i], $color);
1231
1232             // Step to the next line of text. This is a rotation of (x=0, y=interline_spacing)
1233             $xpos += $r01 * $interline_step;
1234             $ypos += $r11 * $interline_step;
1235         }
1236         return TRUE;
1237     }
1238
1239     /*
1240      * ProcessTextTTF() - Draw or size TTF text.
1241      * This is intended for use only by ProcessText().
1242      *    $draw_it : True to draw the text, False to just return the orthogonal width and height.
1243      *    $font : PHPlot font array (with 'ttf' = True) - see SetFontTTF()
1244      *    $angle : Text angle in degrees.
1245      *    $x, $y : Reference point for the text (ignored if !$draw_it)
1246      *    $color : GD color index to use for drawing the text (ignored if !$draw_it)
1247      *    $text : The text to draw or size. Put a newline between lines.
1248      *    $h_factor : Horizontal alignment factor: 0(left), .5(center), or 1(right) (ignored if !$draw_it)
1249      *    $v_factor : Vertical alignment factor: 0(top), .5(center), or 1(bottom) (ignored if !$draw_it)
1250      * Returns: True, if drawing text, or an array of ($width, $height) if not.
1251      */
1252     protected function ProcessTextTTF($draw_it, $font, $angle, $x, $y, $color, $text, $h_factor, $v_factor)
1253     {
1254         // Extract font parameters (see SetFontTTF):
1255         $font_file = $font['font'];
1256         $font_size = $font['size'];
1257         $font_height = $font['height'];
1258         $line_spacing = $this->GetLineSpacing($font);
1259
1260         // Break up the text into lines, trim whitespace.
1261         // Calculate the total width and height of the text box at 0 degrees.
1262         // Save the trimmed lines and their widths for later when drawing.
1263         // To get uniform spacing, don't use the actual line heights.
1264         // Total height = Font-specific line heights plus inter-line spacing.
1265         // Total width = width of widest line.
1266         // Last Line Descent is the offset from the bottom to the text baseline.
1267         // Note: For some reason, ImageTTFBBox uses (-1,-1) as the reference point.
1268         //   So 1+bbox[1] is the baseline to bottom distance.
1269         $total_width = 0;
1270         $lastline_descent = 0;
1271         foreach (explode("\n", $text) as $each_line) {
1272             $lines[] = $line = trim($each_line);
1273             $bbox = ImageTTFBBox($font_size, 0, $font_file, $line);
1274             $line_widths[] = $width = $bbox[2] - $bbox[0];
1275             if ($width > $total_width) $total_width = $width;
1276             $lastline_descent = 1 + $bbox[1];
1277         }
1278         $n_lines = count($lines);
1279         $total_height = $n_lines * $font_height + ($n_lines - 1) * $line_spacing;
1280
1281         // Calculate the rotation matrix for the text's angle. Remember that GD points Y down,
1282         // so the sin() terms change sign.
1283         $theta = deg2rad($angle);
1284         $cos_t = cos($theta);
1285         $sin_t = sin($theta);
1286         $r00 = $cos_t;    $r01 = $sin_t;
1287         $r10 = -$sin_t;   $r11 = $cos_t;
1288
1289         // Make a bounding box of the right size, with upper left corner at (0,0).
1290         // By convention, the point order is: LL, LR, UR, UL.
1291         // Note this is still working with the text at 0 degrees.
1292         // When sizing text (SizeText), use the overall size with descenders.
1293         //   This tells the caller how much room to leave for the text.
1294         // When drawing text (DrawText), use the size without descenders - that
1295         //   is, down to the baseline. This is for accurate positioning.
1296         $b[0] = 0;
1297         if ($draw_it) {
1298             $b[1] = $total_height;
1299         } else {
1300             $b[1] = $total_height + $lastline_descent;
1301         }
1302         $b[2] = $total_width;  $b[3] = $b[1];
1303         $b[4] = $total_width;  $b[5] = 0;
1304         $b[6] = 0;             $b[7] = 0;
1305
1306         // Rotate the bounding box, then offset to the reference point:
1307         for ($i = 0; $i < 8; $i += 2) {
1308             $x_b = $b[$i];
1309             $y_b = $b[$i+1];
1310             $c[$i]   = $x + $r00 * $x_b + $r01 * $y_b;
1311             $c[$i+1] = $y + $r10 * $x_b + $r11 * $y_b;
1312         }
1313
1314         // Get an orthogonal (aligned with X and Y axes) bounding box around it, by
1315         // finding the min and max X and Y:
1316         $bbox_ref_x = $bbox_max_x = $c[0];
1317         $bbox_ref_y = $bbox_max_y = $c[1];
1318         for ($i = 2; $i < 8; $i += 2) {
1319             $x_b = $c[$i];
1320             if ($x_b < $bbox_ref_x) $bbox_ref_x = $x_b;
1321             elseif ($bbox_max_x < $x_b) $bbox_max_x = $x_b;
1322             $y_b = $c[$i+1];
1323             if ($y_b < $bbox_ref_y) $bbox_ref_y = $y_b;
1324             elseif ($bbox_max_y < $y_b) $bbox_max_y = $y_b;
1325         }
1326         $bbox_width = $bbox_max_x - $bbox_ref_x;
1327         $bbox_height = $bbox_max_y - $bbox_ref_y;
1328
1329         if (!$draw_it) {
1330             // Return the bounding box, rounded up (so it always contains the text):
1331             return array((int)ceil($bbox_width), (int)ceil($bbox_height));
1332         }
1333
1334         $interline_step = $font_height + $line_spacing; // Line-to-line step
1335
1336         // Calculate the offsets from the supplied reference point to the
1337         // upper-left corner of the text.
1338         // Start at the reference point at the upper left corner of the bounding
1339         // box (bbox_ref_x, bbox_ref_y) then adjust it for the 9 point alignment.
1340         // h,v_factor are 0,0 for top,left, .5,.5 for center,center, 1,1 for bottom,right.
1341         //    $off_x = $bbox_ref_x + $bbox_width * $h_factor - $x;
1342         //    $off_y = $bbox_ref_y + $bbox_height * $v_factor - $y;
1343         // Then use that offset to calculate back to the supplied reference point x, y
1344         // to get the text base point.
1345         //    $qx = $x - $off_x;
1346         //    $qy = $y - $off_y;
1347         // Reduces to:
1348         $qx = 2 * $x - $bbox_ref_x - $bbox_width * $h_factor;
1349         $qy = 2 * $y - $bbox_ref_y - $bbox_height * $v_factor;
1350
1351         // Check for debug callback. Don't calculate bounding box unless it is wanted.
1352         if ($this->GetCallback('debug_textbox')) {
1353             // Calculate the orthogonal bounding box coordinates for debug testing.
1354
1355             // qx, qy is upper left corner relative to the text.
1356             // Calculate px,py: upper left corner (absolute) of the bounding box.
1357             // There are 4 equation sets for this, depending on the quadrant:
1358             if ($sin_t > 0) {
1359                 if ($cos_t > 0) {
1360                     // Quadrant: 0d - 90d:
1361                     $px = $qx; $py = $qy - $total_width * $sin_t;
1362                 } else {
1363                     // Quadrant: 90d - 180d:
1364                    $px = $qx + $total_width * $cos_t; $py = $qy - $bbox_height;
1365                 }
1366             } else {
1367                 if ($cos_t < 0) {
1368                     // Quadrant: 180d - 270d:
1369                     $px = $qx - $bbox_width; $py = $qy + $total_height * $cos_t;
1370                 } else {
1371                     // Quadrant: 270d - 360d:
1372                     $px = $qx + $total_height * $sin_t; $py = $qy;
1373                 }
1374             }
1375             $this->DoCallback('debug_textbox', $px, $py, $bbox_width, $bbox_height);
1376         }
1377
1378         // Since alignment is applied after rotation, which parameter is used
1379         // to control alignment of each line within the text box varies with
1380         // the angle.
1381         //   Angle (degrees):       Line alignment controlled by:
1382         //  -45 < angle <= 45          h_align
1383         //   45 < angle <= 135         reversed v_align
1384         //  135 < angle <= 225         reversed h_align
1385         //  225 < angle <= 315         v_align
1386         if ($cos_t >= $sin_t) {
1387             if ($cos_t >= -$sin_t) $line_align_factor = $h_factor;
1388             else $line_align_factor = $v_factor;
1389         } else {
1390             if ($cos_t >= -$sin_t) $line_align_factor = 1-$v_factor;
1391             else $line_align_factor = 1-$h_factor;
1392         }
1393
1394         // Now we have the start point, spacing and in-line alignment factor.
1395         // We are finally ready to start drawing the text, line by line.
1396         for ($i = 0; $i < $n_lines; $i++) {
1397
1398             // For drawing TTF text, the reference point is the left edge of the
1399             // text baseline (not the lower left corner of the bounding box).
1400             // The following also adjusts for horizontal (relative to
1401             // the text) alignment of the current line within the box.
1402             // What is happening is rotation of this vector by the text angle:
1403             //    (x = (total_width - line_width) * factor, y = font_height)
1404
1405             $width_factor = ($total_width - $line_widths[$i]) * $line_align_factor;
1406             $rx = $qx + $r00 * $width_factor + $r01 * $font_height;
1407             $ry = $qy + $r10 * $width_factor + $r11 * $font_height;
1408
1409             // Finally, draw the text:
1410             ImageTTFText($this->img, $font_size, $angle, $rx, $ry, $color, $font_file, $lines[$i]);
1411
1412             // Step to position of next line.
1413             // This is a rotation of (x=0,y=height+line_spacing) by $angle:
1414             $qx += $r01 * $interline_step;
1415             $qy += $r11 * $interline_step;
1416         }
1417         return TRUE;
1418     }
1419
1420     /*
1421      * ProcessText() - Wrapper for ProcessTextTTF() and ProcessTextGD(). See notes above.
1422      * This is intended for use from within PHPlot only, and only by DrawText() and SizeText().
1423      *    $draw_it : True to draw the text, False to just return the orthogonal width and height.
1424      *    $font : PHPlot font array, or NULL or empty string to use 'generic'
1425      *    $angle : Text angle in degrees
1426      *    $x, $y : Reference point for the text (ignored if !$draw_it)
1427      *    $color : GD color index to use for drawing the text (ignored if !$draw_it)
1428      *    $text : The text to draw or size. Put a newline between lines.
1429      *    $halign : Horizontal alignment: left, center, or right (ignored if !$draw_it)
1430      *    $valign : Vertical alignment: top, center, or bottom (ignored if !$draw_it)
1431      *      Note: Alignment is relative to the image, not the text.
1432      * Returns: True, if drawing text, or an array of ($width, $height) if not.
1433      */
1434     protected function ProcessText($draw_it, $font, $angle, $x, $y, $color, $text, $halign, $valign)
1435     {
1436         // Empty text case:
1437         if ($text === '') {
1438             if ($draw_it) return TRUE;
1439             return array(0, 0);
1440         }
1441
1442         // Calculate width and height offset factors using the alignment args:
1443         if ($valign == 'top') $v_factor = 0;
1444         elseif ($valign == 'center') $v_factor = 0.5;
1445         else $v_factor = 1.0; // 'bottom'
1446         if ($halign == 'left') $h_factor = 0;
1447         elseif ($halign == 'center') $h_factor = 0.5;
1448         else $h_factor = 1.0; // 'right'
1449
1450         // Apply a default font. This is mostly for external (callback) users.
1451         if (empty($font)) $font = $this->fonts['generic'];
1452
1453         if ($font['ttf']) {
1454             return $this->ProcessTextTTF($draw_it, $font, $angle, $x, $y, $color, $text,
1455                                          $h_factor, $v_factor);
1456         }
1457         return $this->ProcessTextGD($draw_it, $font, $angle, $x, $y, $color, $text, $h_factor, $v_factor);
1458     }
1459
1460     /*
1461      * Draws a block of text. See comments above before ProcessText().
1462      *    $which_font : PHPlot font array, or NULL or empty string to use 'generic'
1463      *    $which_angle : Text angle in degrees
1464      *    $which_xpos, $which_ypos: Reference point for the text
1465      *    $which_color : GD color index to use for drawing the text
1466      *    $which_text :  The text to draw, with newlines (\n) between lines.
1467      *    $which_halign : Horizontal (relative to the image) alignment: left, center, or right.
1468      *    $which_valign : Vertical (relative to the image) alignment: top, center, or bottom.
1469      * Note: This function should be considered 'protected', and is not documented for public use.
1470      */
1471     function DrawText($which_font, $which_angle, $which_xpos, $which_ypos, $which_color, $which_text,
1472                       $which_halign = 'left', $which_valign = 'bottom')
1473     {
1474         return $this->ProcessText(TRUE,
1475                            $which_font, $which_angle, $which_xpos, $which_ypos,
1476                            $which_color, $which_text, $which_halign, $which_valign);
1477     }
1478
1479     /*
1480      * Returns the size of block of text. This is the orthogonal width and height of a bounding
1481      * box aligned with the X and Y axes of the text. Only for angle=0 is this the actual
1482      * width and height of the text block, but for any angle it is the amount of space needed
1483      * to contain the text.
1484      *    $which_font : PHPlot font array, or NULL or empty string to use 'generic'
1485      *    $which_angle : Text angle in degrees
1486      *    $which_text :  The text to draw, with newlines (\n) between lines.
1487      * Returns a two element array with: $width, $height.
1488      * This is just a wrapper for ProcessText() - see above.
1489      * Note: This function should be considered 'protected', and is not documented for public use.
1490      */
1491     function SizeText($which_font, $which_angle, $which_text)
1492     {
1493         // Color, position, and alignment are not used when calculating the size.
1494         return $this->ProcessText(FALSE,
1495                            $which_font, $which_angle, 0, 0, 1, $which_text, '', '');
1496     }
1497
1498 /////////////////////////////////////////////
1499 ///////////            INPUT / OUTPUT CONTROL
1500 /////////////////////////////////////////////
1501
1502     /*
1503      * Sets output file format to $format (jpg, png, ...)
1504      */
1505     function SetFileFormat($format)
1506     {
1507         $asked = $this->CheckOption($format, 'jpg, png, gif, wbmp', __FUNCTION__);
1508         if (!$asked) return FALSE;
1509         switch ($asked) {
1510         case 'jpg':
1511             $format_test = IMG_JPG;
1512             break;
1513         case 'png':
1514             $format_test = IMG_PNG;
1515             break;
1516         case 'gif':
1517             $format_test = IMG_GIF;
1518             break;
1519         case 'wbmp':
1520             $format_test = IMG_WBMP;
1521             break;
1522         }
1523         if (!(imagetypes() & $format_test)) {
1524             return $this->PrintError("SetFileFormat(): File format '$format' not supported");
1525         }
1526         $this->file_format = $asked;
1527         return TRUE;
1528     }
1529
1530     /*
1531      * Selects an input file to be used as graph background and scales or tiles this image
1532      * to fit the sizes.
1533      *   $input_file : Path to the file to be used (jpeg, png and gif accepted)
1534      *   $mode : 'centeredtile', 'tile', or 'scale' (the image to the graph's size)
1535      */
1536     function SetBgImage($input_file, $mode='centeredtile')
1537     {
1538         $this->bgmode = $this->CheckOption($mode, 'tile, centeredtile, scale', __FUNCTION__);
1539         $this->bgimg  = $input_file;
1540         return (boolean)$this->bgmode;
1541     }
1542
1543     /*
1544      * Selects an input file to be used as plot area background and scales or tiles this image
1545      * to fit the sizes.
1546      *   $input_file : Path to the file to be used (jpeg, png and gif accepted)
1547      *   $mode : 'centeredtile', 'tile', or 'scale' (the image to the graph's size)
1548      */
1549     function SetPlotAreaBgImage($input_file, $mode='tile')
1550     {
1551         $this->plotbgmode = $this->CheckOption($mode, 'tile, centeredtile, scale', __FUNCTION__);
1552         $this->plotbgimg  = $input_file;
1553         return (boolean)$this->plotbgmode;
1554     }
1555
1556     /*
1557      * Sets the name of the file to be used as output file.
1558      */
1559     function SetOutputFile($which_output_file)
1560     {
1561         $this->output_file = $which_output_file;
1562         return TRUE;
1563     }
1564
1565     /*
1566      * Sets the output image as 'inline', that is: no Content-Type headers are sent
1567      * to the browser. Needed if you want to embed the images.
1568      */
1569     function SetIsInline($which_ii)
1570     {
1571         $this->is_inline = (bool)$which_ii;
1572         return TRUE;
1573     }
1574
1575     /*
1576      * Performs the actual outputting of the generated graph.
1577      */
1578     function PrintImage()
1579     {
1580         // Browser cache stuff submitted by Thiemo Nagel
1581         if ( (! $this->browser_cache) && (! $this->is_inline)) {
1582             header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
1583             header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
1584             header('Cache-Control: no-cache, must-revalidate');
1585             header('Pragma: no-cache');
1586         }
1587
1588         switch ($this->file_format) {
1589         case 'png':
1590             $mime_type = 'image/png';
1591             $output_f = 'imagepng';
1592             break;
1593         case 'jpg':
1594             $mime_type = 'image/jpeg';
1595             $output_f = 'imagejpeg';
1596             break;
1597         case 'gif':
1598             $mime_type = 'image/gif';
1599             $output_f = 'imagegif';
1600             break;
1601         case 'wbmp':
1602             $mime_type = 'image/wbmp';
1603             $output_f = 'imagewbmp';
1604             break;
1605         default:
1606             return $this->PrintError('PrintImage(): Please select an image type!');
1607         }
1608         if (!$this->is_inline) {
1609             Header("Content-type: $mime_type");
1610         }
1611         if ($this->is_inline && $this->output_file != '') {
1612             $output_f($this->img, $this->output_file);
1613         } else {
1614             $output_f($this->img);
1615         }
1616         return TRUE;
1617     }
1618
1619     /*
1620      *  Error handling for 'fatal' errors:
1621      *   $error_message       Text of the error message
1622      *  Standard output from PHPlot is expected to be an image file, such as
1623      *  when handling an <img> tag browser request. So it is not permitted to
1624      *  output text to standard output. (You should have display_errors=off)
1625      *  Here is how PHPlot handles fatal errors:
1626      *    + Write the error message into an image, and output the image.
1627      *    + If no image can be output, write nothing and produce an HTTP
1628      *      error header.
1629      *    + Trigger a user-level error containing the error message.
1630      *      If no error handler was set up, the script will log the
1631      *      error and exit with non-zero status.
1632      *
1633      *  PrintError() and DrawError() are now equivalent. Both are provided for
1634      *  compatibility. (In earlier releases, PrintError sent the message to
1635      *  stdout only, and DrawError sent it in an image only.)
1636      *
1637      *  This function does not return, unless the calling script has set up
1638      *  an error handler which does not exit. In that case, PrintError will
1639      *  return False. But not all of PHPlot will handle this correctly, so
1640      *  it is probably a bad idea for an error handler to return.
1641      */
1642     protected function PrintError($error_message)
1643     {
1644         // Be sure not to loop recursively, e.g. PrintError - PrintImage - PrintError.
1645         if (isset($this->in_error)) return FALSE;
1646         $this->in_error = TRUE;
1647
1648         // Output an image containing the error message:
1649         if (!empty($this->img)) {
1650             $ypos = $this->image_height/2;
1651             $xpos = $this->image_width/2;
1652             $bgcolor = ImageColorResolve($this->img, 255, 255, 255);
1653             $fgcolor = ImageColorResolve($this->img, 0, 0, 0);
1654             ImageFilledRectangle($this->img, 0, 0, $this->image_width, $this->image_height, $bgcolor);
1655
1656             // Switch to built-in fonts, in case of error with TrueType fonts:
1657             $this->SetUseTTF(FALSE);
1658
1659             $this->DrawText($this->fonts['generic'], 0, $xpos, $ypos, $fgcolor,
1660                             wordwrap($error_message), 'center', 'center');
1661
1662             $this->PrintImage();
1663         } elseif (! $this->is_inline) {
1664             Header('HTTP/1.0 500 Internal Server Error');
1665         }
1666         trigger_error($error_message, E_USER_ERROR);
1667         unset($this->in_error);
1668         return FALSE;  // In case error handler returns, rather than doing exit().
1669     }
1670
1671     /*
1672      * Display an error message and exit.
1673      * This is provided for backward compatibility only. Use PrintError() instead.
1674      *   $error_message       Text of the error message
1675      *   $where_x, $where_y   Ignored, provided for compatibility.
1676      */
1677     protected function DrawError($error_message, $where_x = NULL, $where_y = NULL)
1678     {
1679         return $this->PrintError($error_message);
1680     }
1681
1682 /////////////////////////////////////////////
1683 ///////////                            LABELS
1684 /////////////////////////////////////////////
1685
1686     /*
1687      * Sets position for X data labels.
1688      * For vertical plots, these are X axis data labels, showing label strings from the data array.
1689      *    Accepted positions are: plotdown, plotup, both, none.
1690      * For horizontal plots (bar, stackedbar only), these are X data value labels, show the data values.
1691      *    Accepted positions are: plotin, plotstack, none.
1692      */
1693     function SetXDataLabelPos($which_xdlp)
1694     {
1695         $which_xdlp = $this->CheckOption($which_xdlp, 'plotdown, plotup, both, none, plotin, plotstack',
1696                                          __FUNCTION__);
1697         if (!$which_xdlp) return FALSE;
1698         $this->x_data_label_pos = $which_xdlp;
1699
1700         return TRUE;
1701     }
1702
1703     /*
1704      * Sets position for Y data labels.
1705      * For vertical plots (where available), these are Y data value labels, showing the data values.
1706      *    Accepted positions are: plotin, plotstack, none.
1707      * For horizontal plots, these are Y axis data labels, showing label strings from the data array.
1708      *    Accepted positions are: plotleft, plotright, both, none.
1709      */
1710     function SetYDataLabelPos($which_ydlp)
1711     {
1712         $which_ydlp = $this->CheckOption($which_ydlp, 'plotleft, plotright, both, none, plotin, plotstack',
1713                                           __FUNCTION__);
1714         if (!$which_ydlp) return FALSE;
1715         $this->y_data_label_pos = $which_ydlp;
1716
1717         return TRUE;
1718     }
1719
1720     /*
1721      * Set position for X tick labels.
1722      */
1723     function SetXTickLabelPos($which_xtlp)
1724     {
1725         $which_xtlp = $this->CheckOption($which_xtlp, 'plotdown, plotup, both, xaxis, none',
1726                                          __FUNCTION__);
1727         if (!$which_xtlp) return FALSE;
1728         $this->x_tick_label_pos = $which_xtlp;
1729
1730         return TRUE;
1731     }
1732
1733     /*
1734      * Set position for Y tick labels.
1735      */
1736     function SetYTickLabelPos($which_ytlp)
1737     {
1738         $which_ytlp = $this->CheckOption($which_ytlp, 'plotleft, plotright, both, yaxis, none',
1739                                          __FUNCTION__);
1740         if (!$which_ytlp) return FALSE;
1741         $this->y_tick_label_pos = $which_ytlp;
1742
1743         return TRUE;
1744     }
1745
1746     /*
1747      * Set formatting type for tick and data labels on X or Y axis.
1748      * This implements the 4 functions Set[XY]LabelType() and Set[XY]DataLabelType().
1749      *    $mode  : 'x', 'y', 'xd', or 'yd' - which type of label to configure.
1750      *        'x' and 'y' set the type for tick labels, and the default type for data labels
1751      *        if they are not separately configured. 'xd' and 'yd' set the type for data labels.
1752      *    $args  : Variable arguments, passed as an array.
1753      *       [0] = $type (required) : Label type. 'data', 'time', 'printf', or 'custom'.
1754      *     For type 'data':
1755      *       [1] = $precision (optional). Numeric precision. Can also be set by SetPrecision[XY]().
1756      *       [2] = $prefix (optional) - prefix string for labels.
1757      *       [3] = $suffix (optional) - suffix string for labels. This replaces data_units_text.
1758      *     For type 'time':
1759      *       [1] = $format for strftime (optional). Can also be set by Set[XY]TimeFormat().
1760      *     For type 'printf':
1761      *       [1] = $format (optional) for sprintf.
1762      *     For type 'custom':
1763      *       [1] = $callback (required) - Custom function or array of (instance,method) to call.
1764      *       [2] = $argument (optional) - Pass-through argument for the formatting function.
1765      */
1766     protected function SetLabelType($mode, $args)
1767     {
1768         if (!$this->CheckOption($mode, 'x, y, xd, yd', __FUNCTION__))
1769             return FALSE;
1770
1771         $type = isset($args[0]) ? $args[0] : '';
1772         $format =& $this->label_format[$mode];  // Shorthand reference to format storage variables
1773         switch ($type) {
1774         case 'data':
1775             if (isset($args[1]))
1776                 $format['precision'] = $args[1];
1777             elseif (!isset($format['precision']))
1778                 $format['precision'] = 1;
1779             $format['prefix'] = isset($args[2]) ? $args[2] : '';
1780             $format['suffix'] = isset($args[3]) ? $args[3] : '';
1781             break;
1782
1783         case 'time':
1784             if (isset($args[1]))
1785                 $format['time_format'] = $args[1];
1786             elseif (!isset($format['time_format']))
1787                 $format['time_format'] = '%H:%M:%S';
1788             break;
1789
1790         case 'printf':
1791             if (isset($args[1]))
1792                 $format['printf_format'] = $args[1];
1793             elseif (!isset($format['printf_format']))
1794                 $format['printf_format'] = '%e';
1795             break;
1796
1797         case 'custom':
1798             if (isset($args[1])) {
1799                 $format['custom_callback'] = $args[1];
1800                 $format['custom_arg'] = isset($args[2]) ? $args[2] : NULL;
1801             } else {
1802                 $type = ''; // Error, 'custom' without a function, set to no-format mode.
1803             }
1804             break;
1805
1806         case '':
1807         case 'title':   // Retained for backwards compatibility?
1808             break;
1809
1810         default:
1811             $this->CheckOption($type, 'data, time, printf, custom', __FUNCTION__);
1812             $type = '';
1813         }
1814         $format['type'] = $type;
1815         return (boolean)$type;
1816     }
1817
1818     /*
1819      * Select label formating for X tick labels, and for X data labels
1820      * (unless SetXDataLabelType was called).
1821      * See SetLabelType() for details.
1822      */
1823     function SetXLabelType()  // Variable arguments: $type, ...
1824     {
1825         $args = func_get_args();
1826         return $this->SetLabelType('x', $args);
1827     }
1828
1829     /*
1830      * Select label formatting for X data labels, overriding SetXLabelType.
1831      */
1832     function SetXDataLabelType()  // Variable arguments: $type, ...
1833     {
1834         $args = func_get_args();
1835         return $this->SetLabelType('xd', $args);
1836     }
1837
1838     /*
1839      * Select label formating for Y tick labels, and for Y data labels
1840      * (unless SetYDataLabelType was called).
1841      * See SetLabelType() for details.
1842      */
1843     function SetYLabelType()  // Variable arguments: $type, ...
1844     {
1845         $args = func_get_args();
1846         return $this->SetLabelType('y', $args);
1847     }
1848
1849     /*
1850      * Select label formatting for Y data labels, overriding SetYLabelType.
1851      */
1852     function SetYDataLabelType()  // Variable arguments: $type, ...
1853     {
1854         $args = func_get_args();
1855         return $this->SetLabelType('yd', $args);
1856     }
1857
1858     /*
1859      * Set the date/time format code for X labels.
1860      * Note: Use of SetXLabelType('time', $which_xtf) is preferred, because
1861      * SetXTimeFormat does not also enable date/time formatting.
1862      */
1863     function SetXTimeFormat($which_xtf)
1864     {
1865         $this->label_format['x']['time_format'] = $which_xtf;
1866         return TRUE;
1867     }
1868
1869     /*
1870      * Set the date/time format code for Y labels.
1871      * Note: Use of SetYLabelType('time', $which_ytf) is preferred, because
1872      * SetYTimeFormat does not also enable date/time formatting.
1873      */
1874     function SetYTimeFormat($which_ytf)
1875     {
1876         $this->label_format['y']['time_format'] = $which_ytf;
1877         return TRUE;
1878     }
1879
1880     /*
1881      * Set number format parameters (decimal point and thousands separator) for
1882      * 'data' mode label formatting, overriding the locale-defaults.
1883      */
1884     function SetNumberFormat($decimal_point, $thousands_sep)
1885     {
1886         $this->decimal_point = $decimal_point;
1887         $this->thousands_sep = $thousands_sep;
1888         return TRUE;
1889     }
1890
1891     /*
1892      * Set the text angle for X labels to $which_xla degrees.
1893      */
1894     function SetXLabelAngle($which_xla)
1895     {
1896         $this->x_label_angle = $which_xla;
1897         return TRUE;
1898     }
1899
1900     /*
1901      * Set the text angle for Y labels to $which_xla degrees.
1902      */
1903     function SetYLabelAngle($which_yla)
1904     {
1905         $this->y_label_angle = $which_yla;
1906         return TRUE;
1907     }
1908
1909     /*
1910      * Set the angle for X Data Labels to $which_xdla degrees.
1911      * If not used, this defaults to the value set with SetXLabelAngle.
1912      */
1913     function SetXDataLabelAngle($which_xdla)
1914     {
1915         $this->x_data_label_angle = $which_xdla;
1916         return TRUE;
1917     }
1918
1919     /*
1920      * Set the angle for Y Data Labels to $which_ydla degrees.
1921      * If not used, this defaults to zero (unlike X data labels).
1922      */
1923     function SetYDataLabelAngle($which_ydla)
1924     {
1925         $this->y_data_label_angle = $which_ydla;
1926         return TRUE;
1927     }
1928
1929 /////////////////////////////////////////////
1930 ///////////                              MISC
1931 /////////////////////////////////////////////
1932
1933     /*
1934      * Checks the validity of an option.
1935      *   $which_opt  String to check, such as the provided value of a function argument.
1936      *   $which_acc  String of accepted choices. Must be lower-case, and separated
1937      *               by exactly ', ' (comma, space).
1938      *   $which_func Name of the calling function, for error messages.
1939      * Returns the supplied option value, downcased and trimmed, if it is valid.
1940      * Reports an error if the supplied option is not valid.
1941      */
1942     protected function CheckOption($which_opt, $which_acc, $which_func)
1943     {
1944         $asked = strtolower(trim($which_opt));
1945
1946         // Look for the supplied value in a comma/space separated list.
1947         if (strpos(", $which_acc,", ", $asked,") !== FALSE)
1948             return $asked;
1949
1950         $this->PrintError("$which_func(): '$which_opt' not in available choices: '$which_acc'.");
1951         return NULL;
1952     }
1953
1954     /*
1955      * Checks the validity of an array of options.
1956      *   $opt  Array or string to check.
1957      *   $acc  String of accepted choices. Must be lower-case, and separated
1958      *               by exactly ', ' (comma, space).
1959      *   $func Name of the calling function, for error messages.
1960      * Returns a array option value(s), downcased and trimmed, if all entries in $opt are valid.
1961      * Reports an error if any supplied option is not valid. Returns NULL if the error handler returns.
1962      */
1963     protected function CheckOptionArray($opt, $acc, $func)
1964     {
1965         $opt_array = (array)$opt;
1966         $result = array();
1967         foreach ($opt_array as $option) {
1968             $choice = $this->CheckOption($option, $acc, $func);
1969             if (is_null($choice)) return NULL; // In case CheckOption error handler returns
1970             $result[] = $choice;
1971         }
1972         return $result;
1973     }
1974
1975     /*
1976      * Check compatibility of a plot type and data type.
1977      * This is called by the plot-type-specific drawing functions.
1978      *   $valid_types  String of supported data types. Multiple values must be
1979      *      separated by exactly ', ' (comma, space).
1980      * Returns True if the type is valid for this plot.
1981      * Reports an error if the data type is not value. If the error is handled and
1982      *   the handler returns, this returns False.
1983      */
1984     protected function CheckDataType($valid_types)
1985     {
1986         if (strpos(", $valid_types,", ", $this->data_type,") !== FALSE)
1987             return TRUE;
1988
1989         $this->PrintError("Data type '$this->data_type' is not valid for '$this->plot_type' plots."
1990                . " Supported data type(s): '$valid_types'");
1991         return FALSE;
1992     }
1993
1994     /*
1995      * Decode the data type into variables used to determine how to process a data array.
1996      * The goal is minimize which functions understand the actual data type values.
1997      * This sets the datatype_* variables for use by other member functions.
1998      *   datatype_implied : Implicit independent variable (e.g. text-data vs data-data)
1999      *   datatype_swapped_xy : Swapped X/Y (horizontal plot)
2000      *   datatype_error_bars : Data array has error bar data
2001      *   datatype_pie_single : Data array is for a pie chart with one row per slice
2002      */
2003     protected function DecodeDataType()
2004     {
2005         $dt = $this->data_type;
2006
2007         $this->datatype_implied = ($dt == 'text-data' || $dt == 'text-data-single'
2008                                 || $dt == 'text-data-yx');
2009         $this->datatype_swapped_xy = ($dt == 'text-data-yx' || $dt == 'data-data-yx');
2010         $this->datatype_error_bars = ($dt == 'data-data-error');
2011         $this->datatype_pie_single = ($dt == 'text-data-single');
2012     }
2013
2014     /*
2015      * Make sure the data array is populated, and calculate the number of columns.
2016      * This is called from DrawGraph. Calculates data_columns, which is the
2017      * maximum number of dependent variable values (usually Y) in the data array rows.
2018      * (For pie charts, this is the number of slices.)
2019      * This depends on the data_type, unlike records_per_group (which was
2020      * previously used to pad style arrays, but is not accurate).
2021      * Returns True if the data array is OK, else reports an error (and may return False).
2022      * Note error messages refer to the caller, the public DrawGraph().
2023      */
2024     protected function CheckDataArray()
2025     {
2026         // Test for missing image, which really should never happen.
2027         if (!$this->img) {
2028             return $this->PrintError('DrawGraph(): No image resource allocated');
2029         }
2030
2031         // Test for missing or empty data array:
2032         if (empty($this->data) || !is_array($this->data)) {
2033             return $this->PrintError("DrawGraph(): No data array");
2034         }
2035         if ($this->total_records == 0) {
2036             return $this->PrintError('DrawGraph(): Empty data set');
2037         }
2038
2039         // Decode the data type into functional flags.
2040         $this->DecodeDataType();
2041
2042         // Calculate the maximum number of dependent values per independent value
2043         // (e.g. Y for each X), or the number of pie slices.
2044         if ($this->datatype_pie_single) {
2045             $this->data_columns = $this->num_data_rows; // Special case for 1 type of pie chart.
2046         } else {
2047             $skip = $this->datatype_implied ? 1 : 2; // Skip data label and independent variable if used
2048             $this->data_columns = $this->records_per_group - $skip;
2049             if ($this->datatype_error_bars) // Each Y has +err and -err along with it
2050                 $this->data_columns = (int)($this->data_columns / 3);
2051         }
2052         return TRUE;
2053     }
2054
2055     /*
2056      * Control headers for browser-side image caching.
2057      *   $which_browser_cache : True to allow browsers to cache the image.
2058      */
2059     function SetBrowserCache($which_browser_cache)
2060     {
2061         $this->browser_cache = $which_browser_cache;
2062         return TRUE;
2063     }
2064
2065     /*
2066      * Set whether DrawGraph automatically outputs the image too.
2067      *   $which_pi : True to have DrawGraph call PrintImage at the end.
2068      */
2069     function SetPrintImage($which_pi)
2070     {
2071         $this->print_image = $which_pi;
2072         return TRUE;
2073     }
2074
2075     /*
2076      * Set text to display in the graph's legend.
2077      *   $which_leg : Array of strings for the complete legend, or a single string
2078      *                to be appended to the legend.
2079      *                Or NULL (or an empty array) to cancel the legend.
2080      */
2081     function SetLegend($which_leg)
2082     {
2083         if (is_array($which_leg)) {           // use array (or cancel, if empty array)
2084             $this->legend = $which_leg;
2085         } elseif (!is_null($which_leg)) {     // append string
2086             $this->legend[] = $which_leg;
2087         } else {
2088             $this->legend = '';  // Reinitialize to empty, meaning no legend.
2089         }
2090         return TRUE;
2091     }
2092
2093     /*
2094      * Specifies the position of the legend's upper/leftmost corner,
2095      * in pixel (device) coordinates.
2096      * Both X and Y must be provided, or both omitted (or use NULL) to restore auto-positioning.
2097      */
2098     function SetLegendPixels($which_x=NULL, $which_y=NULL)
2099     {
2100         $this->legend_x_pos = $which_x;
2101         $this->legend_y_pos = $which_y;
2102         // Make sure this is unset, meaning we have pixel coords:
2103         unset($this->legend_xy_world);
2104
2105         return TRUE;
2106     }
2107
2108     /*
2109      * Specifies the position of the legend's upper/leftmost corner,
2110      * in world (data space) coordinates.
2111      */
2112     function SetLegendWorld($which_x, $which_y)
2113     {
2114         // Since conversion from world to pixel coordinates is not yet available, just
2115         // remember the coordinates and set a flag to indicate conversion is needed.
2116         $this->legend_x_pos = $which_x;
2117         $this->legend_y_pos = $which_y;
2118         $this->legend_xy_world = TRUE;
2119
2120         return TRUE;
2121     }
2122
2123     /*
2124      * Set legend text alignment, color box alignment, and style options.
2125      *   $text_align : Alignment of the text, 'left' or 'right'.
2126      *   $colorbox_align : Alignment of the color boxes, 'left', 'right', 'none', or missing/empty.
2127      *       If missing or empty, the same alignment as $text_align is used. Color box is positioned first.
2128      *   $style : reserved for future use.
2129      */
2130     function SetLegendStyle($text_align, $colorbox_align = '', $style = '')
2131     {
2132         $this->legend_text_align = $this->CheckOption($text_align, 'left, right', __FUNCTION__);
2133         if (empty($colorbox_align))
2134             $this->legend_colorbox_align = $this->legend_text_align;
2135         else
2136             $this->legend_colorbox_align = $this->CheckOption($colorbox_align, 'left, right, none',
2137                                                               __FUNCTION__);
2138         return ((boolean)$this->legend_text_align && (boolean)$this->legend_colorbox_align);
2139     }
2140
2141     /*
2142      * Set border for the plot area.
2143      * Accepted values are: left, right, top, bottom, sides, none, full or an array of those.
2144      */
2145     function SetPlotBorderType($pbt)
2146     {
2147         $this->plot_border_type = $this->CheckOptionArray($pbt, 'left, right, top, bottom, sides, none, full',
2148                                                           __FUNCTION__);
2149         return !empty($this->plot_border_type);
2150     }
2151
2152     /*
2153      * Set border style for the image.
2154      * Accepted values are: raised, plain, solid, none
2155      *  'solid' is the same as 'plain' except it fixes the color (see DrawImageBorder)
2156      */
2157     function SetImageBorderType($sibt)
2158     {
2159         $this->image_border_type = $this->CheckOption($sibt, 'raised, plain, solid, none', __FUNCTION__);
2160         return (boolean)$this->image_border_type;
2161     }
2162
2163     /*
2164      * Set border width for the image to $width in pixels.
2165      */
2166     function SetImageBorderWidth($width)
2167     {
2168         $this->image_border_width = $width;
2169         return TRUE;
2170     }
2171
2172     /*
2173      * Enable or disable drawing of the plot area background color.
2174      */
2175     function SetDrawPlotAreaBackground($dpab)
2176     {
2177         $this->draw_plot_area_background = (bool)$dpab;
2178         return TRUE;
2179     }
2180
2181     /*
2182      * Enable or disable drawing of the X grid lines.
2183      */
2184     function SetDrawXGrid($dxg)
2185     {
2186         $this->draw_x_grid = (bool)$dxg;
2187         return TRUE;
2188     }
2189
2190     /*
2191      * Enable or disable drawing of the Y grid lines.
2192      */
2193     function SetDrawYGrid($dyg)
2194     {
2195         $this->draw_y_grid = (bool)$dyg;
2196         return TRUE;
2197     }
2198
2199     /*
2200      * Select dashed or solid grid lines.
2201      *   $ddg : True for dashed grid lines, false for solid grid lines.
2202      */
2203     function SetDrawDashedGrid($ddg)
2204     {
2205         $this->dashed_grid = (bool)$ddg;
2206         return TRUE;
2207     }
2208
2209     /*
2210      * Enable or disable drawing of X Data Label Lines.
2211      */
2212     function SetDrawXDataLabelLines($dxdl)
2213     {
2214         $this->draw_x_data_label_lines = (bool)$dxdl;
2215         return TRUE;
2216     }
2217
2218     /*
2219      * Set the main title text for the plot.
2220      */
2221     function SetTitle($which_title)
2222     {
2223         $this->title_txt = $which_title;
2224         return TRUE;
2225     }
2226
2227     /*
2228      * Set the X axis title and position.
2229      */
2230     function SetXTitle($which_xtitle, $which_xpos = 'plotdown')
2231     {
2232         if ($which_xtitle == '')
2233             $which_xpos = 'none';
2234
2235         $this->x_title_pos = $this->CheckOption($which_xpos, 'plotdown, plotup, both, none', __FUNCTION__);
2236         if (!$this->x_title_pos) return FALSE;
2237         $this->x_title_txt = $which_xtitle;
2238         return TRUE;
2239     }
2240
2241     /*
2242      * Set the Y axis title and position.
2243      */
2244     function SetYTitle($which_ytitle, $which_ypos = 'plotleft')
2245     {
2246         if ($which_ytitle == '')
2247             $which_ypos = 'none';
2248
2249         $this->y_title_pos = $this->CheckOption($which_ypos, 'plotleft, plotright, both, none', __FUNCTION__);
2250         if (!$this->y_title_pos) return FALSE;
2251         $this->y_title_txt = $which_ytitle;
2252         return TRUE;
2253     }
2254
2255     /*
2256      * Set the size of the drop shadow for bar and pie charts.
2257      *   $which_s : Size of the drop shadow in pixels.
2258      */
2259     function SetShading($which_s)
2260     {
2261         $this->shading = (int)$which_s;
2262         return TRUE;
2263     }
2264
2265     /*
2266      * Set the plot type (bars, points, ...)
2267      */
2268     function SetPlotType($which_pt)
2269     {
2270         $avail_plot_types = implode(', ', array_keys(PHPlot::$plots)); // List of known plot types
2271         $this->plot_type = $this->CheckOption($which_pt, $avail_plot_types, __FUNCTION__);
2272         return (boolean)$this->plot_type;
2273     }
2274
2275     /*
2276      * Set the position of the X axis.
2277      *  $pos : Axis position in world coordinates (as an integer).
2278      */
2279     function SetXAxisPosition($pos='')
2280     {
2281         $this->x_axis_position = ($pos === '') ? $pos : (int)$pos;
2282         return TRUE;
2283     }
2284
2285     /*
2286      * Set the position of the Y axis.
2287      *  $pos : Axis position in world coordinates (as an integer).
2288      */
2289     function SetYAxisPosition($pos='')
2290     {
2291         $this->y_axis_position = ($pos === '') ? $pos : (int)$pos;
2292         return TRUE;
2293     }
2294
2295     /*
2296      * Enable or disable drawing of the X axis line.
2297      *  $draw : True to draw the axis (default if not called), False to suppress it.
2298      * This controls drawing of the axis line only, and not the ticks, labels, or grid.
2299      */
2300     function SetDrawXAxis($draw)
2301     {
2302         $this->suppress_x_axis = !$draw; // See DrawXAxis()
2303         return TRUE;
2304     }
2305
2306     /*
2307      * Enable or disable drawing of the Y axis line.
2308      *  $draw : True to draw the axis (default if not called), False to suppress it.
2309      * This controls drawing of the axis line only, and not the ticks, labels, or grid.
2310      */
2311     function SetDrawYAxis($draw)
2312     {
2313         $this->suppress_y_axis = !$draw; // See DrawYAxis()
2314         return TRUE;
2315     }
2316
2317     /*
2318      * Select linear or log scale for the X axis.
2319      */
2320     function SetXScaleType($which_xst)
2321     {
2322         $this->xscale_type = $this->CheckOption($which_xst, 'linear, log', __FUNCTION__);
2323         return (boolean)$this->xscale_type;
2324     }
2325
2326     /*
2327      * Select linear or log scale for the Y axis.
2328      */
2329     function SetYScaleType($which_yst)
2330     {
2331         $this->yscale_type = $this->CheckOption($which_yst, 'linear, log',  __FUNCTION__);
2332         return (boolean)$this->yscale_type;
2333     }
2334
2335     /*
2336      * Set the precision for numerically formatted X labels.
2337      *   $which_prec : Number of digits to display.
2338      * Note: This is equivalent to: SetXLabelType('data', $which_prec)
2339      */
2340     function SetPrecisionX($which_prec)
2341     {
2342         return $this->SetXLabelType('data', $which_prec);
2343     }
2344
2345     /*
2346      * Set the precision for numerically formatted Y labels.
2347      *   $which_prec : Number of digits to display.
2348      * Note: This is equivalent to: SetYLabelType('data', $which_prec)
2349      */
2350     function SetPrecisionY($which_prec)
2351     {
2352         return $this->SetYLabelType('data', $which_prec);
2353     }
2354
2355     /*
2356      * Set the line width (in pixels) for error bars.
2357      */
2358     function SetErrorBarLineWidth($which_seblw)
2359     {
2360         $this->error_bar_line_width = $which_seblw;
2361         return TRUE;
2362     }
2363
2364     /*
2365      * Set the position for pie chart percentage labels.
2366      *   $which_blb : Real number between 0 and 1.
2367      *      Smaller values move the labels in towards the center.
2368      */
2369     function SetLabelScalePosition($which_blp)
2370     {
2371         $this->label_scale_position = $which_blp;
2372         return TRUE;
2373     }
2374
2375     /*
2376      * Set the size (in pixels) of the "T" in error bars.
2377      */
2378     function SetErrorBarSize($which_ebs)
2379     {
2380         $this->error_bar_size = $which_ebs;
2381         return TRUE;
2382     }
2383
2384     /*
2385      * Set the shape of the in error bars.
2386      *   $which_ebs : Error bar shape, 'tee' or 'line'.
2387      */
2388     function SetErrorBarShape($which_ebs)
2389     {
2390         $this->error_bar_shape = $this->CheckOption($which_ebs, 'tee, line', __FUNCTION__);
2391         return (boolean)$this->error_bar_shape;
2392     }
2393
2394     /*
2395      * Synchronize the point shape and point size arrays.
2396      * This is called just before drawing any plot that needs 'points'.
2397      */
2398     protected function CheckPointParams()
2399     {
2400         // Make both point_shapes and point_sizes the same size, by padding the smaller.
2401         $ps = count($this->point_sizes);
2402         $pt = count($this->point_shapes);
2403
2404         if ($ps < $pt) {
2405             $this->pad_array($this->point_sizes, $pt);
2406             $this->point_counts = $pt;
2407         } elseif ($ps > $pt) {
2408             $this->pad_array($this->point_shapes, $ps);
2409             $this->point_counts = $ps;
2410         } else {
2411             $this->point_counts = $ps;
2412         }
2413
2414         // Note: PHPlot used to check and adjust point_sizes to be an even number here,
2415         // for all 'diamond' and 'triangle' shapes. The reason for this having been
2416         // lost, and the current maintainer seeing no sense it doing this for only
2417         // some shapes, the code has been removed. But see what DrawDot() does.
2418     }
2419
2420     /*
2421      * Set the point shape for each data set.
2422      *   $which_pt : Array (or single value) of valid point shapes. See also DrawDot() for valid shapes.
2423      * The point shape and point sizes arrays are synchronized before drawing a graph
2424      * that uses points. See CheckPointParams()
2425      */
2426     function SetPointShapes($which_pt)
2427     {
2428         $this->point_shapes = $this->CheckOptionArray($which_pt, 'halfline, line, plus, cross, rect,'
2429                        . ' circle, dot, diamond, triangle, trianglemid, delta, yield, star, hourglass,'
2430                        . ' bowtie, target, box, home, up, down, none', __FUNCTION__);
2431         return !empty($this->point_shapes);
2432     }
2433
2434     /*
2435      * Set the point size for point plots.
2436      *   $which_ps : Array (or single value) of point sizes in pixels.
2437      * The point shape and point sizes arrays are synchronized before drawing a graph
2438      * that uses points. See CheckPointParams()
2439      */
2440     function SetPointSizes($which_ps)
2441     {
2442         if (is_array($which_ps)) {
2443             // Use provided array:
2444             $this->point_sizes = $which_ps;
2445         } elseif (!is_null($which_ps)) {
2446             // Make the single value into an array:
2447             $this->point_sizes = array($which_ps);
2448         }
2449         return TRUE;
2450     }
2451
2452     /*
2453      * Sets whether lines should be broken at missing data.
2454      *   $bl : True to break the lines, false to connect around missing data.
2455      * This only works with 'lines' and 'squared' plots.
2456      */
2457     function SetDrawBrokenLines($bl)
2458     {
2459         $this->draw_broken_lines = (bool)$bl;
2460         return TRUE;
2461     }
2462
2463     /*
2464      * Set the data type, which defines the structure of the data array
2465      *  text-data: ('label', y1, y2, y3, ...)
2466      *  text-data-single: ('label', data), for some pie charts.
2467      *  data-data: ('label', x, y1, y2, y3, ...)
2468      *  data-data-error: ('label', x1, y1, e1+, e2-, y2, e2+, e2-, y3, e3+, e3-, ...)
2469      *  data-data-yx: ('label', y, x1, x2, x3, ..)
2470      *  text-data-yx: ('label', x1, x2, x3, ...)
2471      */
2472     function SetDataType($which_dt)
2473     {
2474         //The next four lines are for past compatibility.
2475         if ($which_dt == 'text-linear') $which_dt = 'text-data';
2476         elseif ($which_dt == 'linear-linear') $which_dt = 'data-data';
2477         elseif ($which_dt == 'linear-linear-error') $which_dt = 'data-data-error';
2478         elseif ($which_dt == 'text-data-pie') $which_dt = 'text-data-single';
2479
2480         $this->data_type = $this->CheckOption($which_dt, 'text-data, text-data-single, '.
2481                                                          'data-data, data-data-error, '.
2482                                                          'data-data-yx, text-data-yx',
2483                                                          __FUNCTION__);
2484         return (boolean)$this->data_type;
2485     }
2486
2487     /*
2488      * Copy the array passed as data values. We convert to numerical indexes, for its
2489      * use for (or while) loops, which sometimes are faster. Performance improvements
2490      * vary from 28% in DrawLines() to 49% in DrawArea() for plot drawing functions.
2491      */
2492     function SetDataValues($which_dv)
2493     {
2494         $this->num_data_rows = count($which_dv);
2495         $this->total_records = 0;
2496         $this->data = array();
2497         $this->num_recs = array();
2498         for ($i = 0; $i < $this->num_data_rows; $i++) {
2499             $this->data[$i] = array_values($which_dv[$i]);   // convert to numerical indices.
2500
2501             // Count size of each row, and total for the array.
2502             $recs = count($this->data[$i]);
2503             $this->total_records += $recs;
2504             $this->num_recs[$i] = $recs;
2505         }
2506         // This is the size of the widest row in the data array
2507         // Note records_per_group isn't used much anymore. See data_columns in CheckDataArray()
2508         $this->records_per_group = max($this->num_recs);
2509         return TRUE;
2510     }
2511
2512     /*
2513      * Pad styles arrays for later use by plot drawing functions:
2514      * This removes the need for $max_data_colors, etc. and $color_index = $color_index % $max_data_colors
2515      * in DrawBars(), DrawLines(), etc.
2516      * The arrays are padded to data_columns which is the maximum number of data sets.
2517      * See CheckDataArray() for the calculation.
2518      */
2519     protected function PadArrays()
2520     {
2521         $this->pad_array($this->line_widths, $this->data_columns);
2522         $this->pad_array($this->line_styles, $this->data_columns);
2523         $this->pad_array($this->ndx_data_colors, $this->data_columns);
2524         $this->pad_array($this->ndx_data_border_colors, $this->data_columns);
2525         // Other data color arrays are handled in the Need*Colors() functions.
2526
2527         return TRUE;
2528     }
2529
2530     /*
2531      * Pads an array with itself. This only works on 0-based sequential integer indexed arrays.
2532      *    $arr : The array (or scalar) to pad. This argument is modified.
2533      *    $size : Minimum size of the resulting array.
2534      * If $arr is a scalar, it will be converted first to a single element array.
2535      * If $arr has at least $size elements, it is unchanged.
2536      * Otherwise, append elements of $arr to itself until it reaches $size elements.
2537      */
2538     protected function pad_array(&$arr, $size)
2539     {
2540         if (! is_array($arr)) {
2541             $arr = array($arr);
2542         }
2543         $n = count($arr);
2544         $base = 0;
2545         while ($n < $size) $arr[$n++] = $arr[$base++];
2546     }
2547
2548     /*
2549      * Format a floating-point number.
2550      *   $number : A floating point number to format
2551      *   $decimals : Number of decimal places in the result
2552      *   Returns the formatted result.
2553      * This is like PHP's number_format, but uses class variables for separators.
2554      * The separators will default to locale-specific values, if available.
2555      */
2556     protected function number_format($number, $decimals=0)
2557     {
2558         if (!isset($this->decimal_point) || !isset($this->thousands_sep)) {
2559             // Load locale-specific values from environment, unless disabled:
2560             if (empty($this->locale_override))
2561                 @setlocale(LC_ALL, '');
2562             // Fetch locale settings:
2563             $locale = @localeconv();
2564             if (isset($locale['decimal_point']) && isset($locale['thousands_sep'])) {
2565                 $this->decimal_point = $locale['decimal_point'];
2566                 $this->thousands_sep = $locale['thousands_sep'];
2567             } else {
2568                 // Locale information not available.
2569                 $this->decimal_point = '.';
2570                 $this->thousands_sep = ',';
2571             }
2572         }
2573         return number_format($number, $decimals, $this->decimal_point, $this->thousands_sep);
2574     }
2575
2576     /*
2577      * Register a callback (hook) function
2578      *   $reason : A pre-defined name where a callback can be defined.
2579      *   $function : The name of a function to register for callback, or an instance/method
2580      *      pair in an array (see 'callbacks' in the PHP reference manual).
2581      *   $arg : Optional argument to supply to the callback function when it is triggered.
2582      *      (Often called "clientData")
2583      *   Returns True if the callback reason is valid, else False.
2584      */
2585     function SetCallback($reason, $function, $arg = NULL)
2586     {
2587         // Use array_key_exists because valid reason keys have NULL as value.
2588         if (!array_key_exists($reason, $this->callbacks))
2589             return FALSE;
2590         $this->callbacks[$reason] = array($function, $arg);
2591         return TRUE;
2592     }
2593
2594     /*
2595      * Return the name of a function registered for callback. See SetCallBack.
2596      *   $reason - A pre-defined name where a callback can be defined.
2597      *   Returns the current callback function (name or array) for the given reason,
2598      *   or False if there was no active callback or the reason is not valid.
2599      * Note you can safely test the return value with a simple 'if', as
2600      * no valid function name evaluates to false.
2601      */
2602     function GetCallback($reason)
2603     {
2604         if (isset($this->callbacks[$reason]))
2605             return $this->callbacks[$reason][0];
2606         return FALSE;
2607     }
2608
2609     /*
2610      * Un-register (remove) a function registered for callback.
2611      *   $reason - A pre-defined name where a callback can be defined.
2612      *   Returns: True if it was a valid callback reason, else False.
2613      * Note: Returns True whether or not there was a callback registered.
2614      */
2615     function RemoveCallback($reason)
2616     {
2617         if (!array_key_exists($reason, $this->callbacks))
2618             return FALSE;
2619         $this->callbacks[$reason] = NULL;
2620         return TRUE;
2621     }
2622
2623     /*
2624      * Invoke a callback, if one is registered.
2625      * Accepts a variable number of arguments >= 1:
2626      *   $reason : A string naming the callback.
2627      *   ... : Zero or more additional arguments to be passed to the
2628      *         callback function, after the passthru argument:
2629      *           callback_function($image, $passthru, ...)
2630      *   Returns: whatever value (if any) was returned by the callback.
2631      */
2632     protected function DoCallback() // Note: Variable arguments
2633     {
2634         $args = func_get_args();
2635         $reason = $args[0];
2636         if (!isset($this->callbacks[$reason]))
2637             return;
2638         list($function, $args[0]) = $this->callbacks[$reason];
2639         array_unshift($args, $this->img);
2640         // Now args[] looks like: img, passthru, extra args...
2641         return call_user_func_array($function, $args);
2642     }
2643
2644     /*
2645      * Allocate colors for the plot.
2646      * This is called by DrawGraph to allocate the colors needed for the plot.  Each selectable
2647      * color has already been validated, parsed into an array (r,g,b,a), and stored into a member
2648      * variable. Now the GD color indexes are assigned and stored into the ndx_*_color variables.
2649      * This is deferred here to avoid allocating unneeded colors and to avoid order dependencies,
2650      * especially with the transparent color.
2651      *
2652      * For drawing data elements, only the main data colors and border colors are allocated here.
2653      * Dark colors and error bar colors are allocated by Need*Color() functions.
2654      * (Data border colors default to just black, so there is no cost to always allocating.)
2655      *
2656      * Data color allocation works as follows. If there is a data_color callback, then allocate all
2657      * defined data colors (because the callback can use them however it wants). Otherwise, only allocate
2658      * the number of colors that will be used. This is the larger of the number of data sets and the
2659      * number of legend lines.
2660      */
2661     protected function SetColorIndexes()
2662     {
2663         $this->ndx_bg_color         = $this->GetColorIndex($this->bg_color); // Background first
2664         $this->ndx_plot_bg_color    = $this->GetColorIndex($this->plot_bg_color);
2665         if ($this->image_border_type != 'none') {
2666             $this->ndx_i_border         = $this->GetColorIndex($this->i_border);
2667             $this->ndx_i_border_dark    = $this->GetDarkColorIndex($this->i_border);
2668         }
2669
2670         // Handle defaults for X and Y title colors.
2671         $this->ndx_title_color      = $this->GetColorIndex($this->title_color);
2672         if (empty($this->x_title_color)) {
2673             $this->ndx_x_title_color = $this->ndx_title_color;
2674         } else {
2675             $this->ndx_x_title_color = $this->GetColorIndex($this->x_title_color);
2676         }
2677         if (empty($this->y_title_color)) {
2678             $this->ndx_y_title_color = $this->ndx_title_color;
2679         } else {
2680             $this->ndx_y_title_color = $this->GetColorIndex($this->y_title_color);
2681         }
2682
2683         $this->ndx_text_color       = $this->GetColorIndex($this->text_color);
2684         $this->ndx_grid_color       = $this->GetColorIndex($this->grid_color);
2685         $this->ndx_light_grid_color = $this->GetColorIndex($this->light_grid_color);
2686         $this->ndx_tick_color       = $this->GetColorIndex($this->tick_color);
2687
2688         // Maximum number of data & border colors to allocate:
2689         if ($this->GetCallback('data_color')) {
2690             $n_data = count($this->data_colors); // Need all of them
2691             $n_border = count($this->data_border_colors);
2692         } else {
2693             $n_data = max($this->data_columns, empty($this->legend) ? 0 : count($this->legend));
2694             $n_border = $n_data; // One border color per data color
2695         }
2696
2697         // Allocate main data colors. For other colors used for data, see the functions which follow.
2698         $this->ndx_data_colors = $this->GetColorIndexArray($this->data_colors, $n_data);
2699         $this->ndx_data_border_colors = $this->GetColorIndexArray($this->data_border_colors, $n_border);
2700
2701         // Set up a color as transparent, if SetTransparentColor was used.
2702         if (!empty($this->transparent_color)) {
2703             imagecolortransparent($this->img, $this->GetColorIndex($this->transparent_color));
2704         }
2705     }
2706
2707     /*
2708      * Allocate dark-shade data colors. Called if needed by graph drawing functions.
2709      */
2710     protected function NeedDataDarkColors()
2711     {
2712         // This duplicates the calculation in SetColorIndexes() for number of data colors to allocate.
2713         if ($this->GetCallback('data_color')) {
2714             $n_data = count($this->data_colors);
2715         } else {
2716             $n_data = max($this->data_columns, empty($this->legend) ? 0 : count($this->legend));
2717         }
2718         $this->ndx_data_dark_colors = $this->GetDarkColorIndexArray($this->data_colors, $n_data);
2719         $this->pad_array($this->ndx_data_dark_colors, $this->data_columns);
2720     }
2721
2722     /*
2723      * Allocate error bar colors. Called if needed by graph drawing functions.
2724      */
2725     protected function NeedErrorBarColors()
2726     {
2727         // This is similar to the calculation in SetColorIndexes() for number of data colors to allocate.
2728         if ($this->GetCallback('data_color')) {
2729             $n_err = count($this->error_bar_colors);
2730         } else {
2731             $n_err = max($this->data_columns, empty($this->legend) ? 0 : count($this->legend));
2732         }
2733         $this->ndx_error_bar_colors = $this->GetColorIndexArray($this->error_bar_colors, $n_err);
2734         $this->pad_array($this->ndx_error_bar_colors, $this->data_columns);
2735     }
2736
2737     /*
2738      * Determine if, and where, to draw Data Value Labels.
2739      *   $label_control : Label position control. Either x_data_label_pos or y_data_label_pos.
2740      *   &$x_adj, &$y_adj : Returns X,Y adjustments (offset in pixels) to the text position.
2741      *   &$h_align, &$v_align : Returns horizontal and vertical alignment for the label.
2742      *      The above 4 argument values should be passed to DrawDataValueLabel()
2743      * Returns True if data value labels should be drawn (based on $label_control), else False.
2744      * This is used for plot types other than bars/stackedbars (which have their own way of doing it).
2745      * It uses two member variables (unset by default): data_value_label_angle and data_value_label_distance
2746      * to define the vector to the label. Default is 90 degrees at 5 pixels.
2747      */
2748     protected function CheckDataValueLabels($label_control, &$x_adj, &$y_adj, &$h_align, &$v_align)
2749     {
2750         if ($label_control != 'plotin')
2751             return FALSE; // No data value labels
2752         $angle = deg2rad(isset($this->data_value_label_angle) ? $this->data_value_label_angle : 90);
2753         $radius = isset($this->data_value_label_distance) ? $this->data_value_label_distance : 5;
2754         $cos = cos($angle);
2755         $sin = sin($angle);
2756         $x_adj = (int)($radius * $cos);
2757         $y_adj = -(int)($radius * $sin); // Y is reversed in device coordinates
2758
2759         // Choose from 8 (not 9, center/center can't happen) text alignments based on angle:
2760         if ($sin >= 0.383) $v_align = 'bottom'; // 0.383 = sin(360deg / 16)
2761         elseif ($sin >= -0.383) $v_align = 'center';
2762         else $v_align = 'top';
2763         if ($cos >= 0.383) $h_align = 'left';
2764         elseif ($cos >= -0.383) $h_align = 'center';
2765         else $h_align = 'right';
2766         return TRUE;
2767     }
2768
2769 //////////////////////////////////////////////////////////
2770 ///////////         DATA ANALYSIS, SCALING AND TRANSLATION
2771 //////////////////////////////////////////////////////////
2772
2773     /*
2774      * Analyzes the data array and calculates the minimum and maximum values.
2775      * In this function, IV refers to the independent variable, and DV the dependent variable.
2776      * For most plots, IV is X and DV is Y. For swapped X/Y plots, IV is Y and DV is X.
2777      * At the end of the function, IV and DV ranges get assigned into X or Y.
2778      *
2779      * The data type mostly determines the data array structure, but some plot types do special
2780      * things such as sum the values in a row. This information is in the plots[] array.
2781      *
2782      * This calculates min_x, max_x, min_y, and max_y. It also calculates two arrays
2783      * data_min[] and data_max[] with per-row min and max values. These are used for
2784      * data label lines. For normal (unswapped) data, these are the Y range for each X.
2785      * For swapped X/Y data, they are the X range for each Y.
2786      */
2787     protected function FindDataLimits()
2788     {
2789         // Does this plot type need special processing of the data values?
2790         $sum_vals = !empty(PHPlot::$plots[$this->plot_type]['sum_vals']); // Add up values in each row
2791         $abs_vals = !empty(PHPlot::$plots[$this->plot_type]['abs_vals']); // Take absolute values
2792
2793         // These need to be initialized in case there are multiple plots and missing data points.
2794         $this->data_min = array();
2795         $this->data_max = array();
2796
2797         // Independent values are in the data array or assumed?
2798         if ($this->datatype_implied) {
2799             $all_iv = array(0, $this->num_data_rows - 1);
2800         } else {
2801             $all_iv = array();
2802         }
2803
2804         // Process all rows of data:
2805         for ($i = 0; $i < $this->num_data_rows; $i++) {
2806             $n_vals = $this->num_recs[$i];
2807             $j = 1; // Skips label at [0]
2808
2809             if (!$this->datatype_implied) {
2810                 $all_iv[] = (double)$this->data[$i][$j++];
2811             }
2812
2813             if ($sum_vals) {
2814                 $all_dv = array(0, 0); // One limit is 0, other calculated below
2815             } else {
2816                 $all_dv = array();
2817             }
2818             while ($j < $n_vals) {
2819                 if (is_numeric($this->data[$i][$j])) {
2820                     $val = (double)$this->data[$i][$j++];
2821
2822                     if ($this->datatype_error_bars) {
2823                         $all_dv[] = $val + (double)$this->data[$i][$j++];
2824                         $all_dv[] = $val - (double)$this->data[$i][$j++];
2825                     } else {
2826                         if ($abs_vals) {
2827                             $val = abs($val); // Use absolute values
2828                         }
2829                         if ($sum_vals) {
2830                             $all_dv[1] += $val;  // Sum of values
2831                         } else {
2832                             $all_dv[] = $val; // List of all values
2833                         }
2834                     }
2835                 } else {    // Missing DV value
2836                   $j++;
2837                   if ($this->datatype_error_bars) $j += 2;
2838                 }
2839             }
2840             if (!empty($all_dv)) {
2841                 $this->data_min[$i] = min($all_dv);  // Store per-row DV range
2842                 $this->data_max[$i] = max($all_dv);
2843             }
2844         }
2845
2846         if ($this->datatype_swapped_xy) {
2847             // Assign min and max for swapped X/Y plots: IV=Y and DV=X
2848             $this->min_y = min($all_iv);
2849             $this->max_y = max($all_iv);
2850             if (empty($this->data_min)) { // Guard against regressive case: No X at all
2851                 $this->min_x = 0;
2852                 $this->max_x = 0;
2853             } else {
2854                 $this->min_x = min($this->data_min);  // Store global X range
2855                 $this->max_x = max($this->data_max);
2856             }
2857         } else {
2858             // Assign min and max for normal plots: IV=X and DV=Y
2859             $this->min_x = min($all_iv);
2860             $this->max_x = max($all_iv);
2861             if (empty($this->data_min)) { // Guard against regressive case: No Y at all
2862                 $this->min_y = 0;
2863                 $this->max_y = 0;
2864             } else {
2865                 $this->min_y = min($this->data_min);  // Store global Y range
2866                 $this->max_y = max($this->data_max);
2867             }
2868         }
2869
2870         if ($this->GetCallback('debug_scale')) {
2871             $this->DoCallback('debug_scale', __FUNCTION__, array(
2872                 'min_x' => $this->min_x, 'min_y' => $this->min_y,
2873                 'max_x' => $this->max_x, 'max_y' => $this->max_y));
2874         }
2875         return TRUE;
2876     }
2877
2878     /*
2879      * Calculates image margins on the fly from title positions and sizes,
2880      * and tick labels positions and sizes.
2881      *
2882      * A picture of the locations of elements and spacing can be found in the
2883      * PHPlot Reference Manual.
2884      *
2885      * Calculates the following (class variables unless noted):
2886      *
2887      * Plot area margins (see note below):
2888      *     y_top_margin
2889      *     y_bot_margin
2890      *     x_left_margin
2891      *     x_right_margin
2892      *
2893      * Title sizes (these are now local, not class variables, since they are not used elsewhere):
2894      *     title_height : Height of main title
2895      *     x_title_height : Height of X axis title, 0 if no X title
2896      *     y_title_width : Width of Y axis title, 0 if no Y title
2897      *
2898      * Tick/Data label offsets, relative to plot_area:
2899      *     x_label_top_offset, x_label_bot_offset, x_label_axis_offset
2900      *     y_label_left_offset, y_label_right_offset, y_label_axis_offset
2901      *
2902      * Title offsets, relative to plot area:
2903      *     x_title_top_offset, x_title_bot_offset
2904      *     y_title_left_offset, y_title_left_offset
2905      *     title_offset (for main title, relative to image edge)
2906      *
2907      *  Note: The margins are calculated, but not stored, if margins or plot area were
2908      *  set by the user with SetPlotAreaPixels or SetMarginsPixels. The margin
2909      *  calculation is mixed in with the offset variables, so it doesn't seem worth the
2910      *  trouble to separate them.
2911      *
2912      * If the $maximize argument is true, we use the full image size, minus safe_margin
2913      * and main title, for the plot. This is for pie charts which have no axes or X/Y titles.
2914      */
2915     protected function CalcMargins($maximize)
2916     {
2917         // This is the line-to-line or line-to-text spacing:
2918         $gap = $this->safe_margin;
2919         // Initial margin on each side takes into account a possible image border.
2920         // For compatibility, if border is 1 or 2, don't increase the margins.
2921         $base_margin = max($gap, $this->GetImageBorderWidth() + 3);
2922         $this->title_offset = $base_margin;  // For use in DrawTitle
2923
2924         // Minimum margin on each side. This reduces the chance that the
2925         // right-most tick label (for example) will run off the image edge
2926         // if there are no titles on that side.
2927         $min_margin = 2 * $gap + $base_margin;
2928
2929         // Calculate the title sizes:
2930         list($unused, $title_height) = $this->SizeText($this->fonts['title'], 0, $this->title_txt);
2931         list($unused, $x_title_height) = $this->SizeText($this->fonts['x_title'], 0, $this->x_title_txt);
2932         list($y_title_width, $unused) = $this->SizeText($this->fonts['y_title'], 90, $this->y_title_txt);
2933
2934         // Special case for maximum area usage with no X/Y titles or labels, only main title:
2935         if ($maximize) {
2936             if (!isset($this->x_left_margin))
2937                 $this->x_left_margin = $base_margin;
2938             if (!isset($this->x_right_margin))
2939                 $this->x_right_margin = $base_margin;
2940             if (!isset($this->y_top_margin)) {
2941                 $this->y_top_margin = $base_margin;
2942                 if ($title_height > 0)
2943                     $this->y_top_margin += $title_height + $gap;
2944             }
2945             if (!isset($this->y_bot_margin))
2946                 $this->y_bot_margin = $base_margin;
2947
2948             return TRUE;
2949         }
2950
2951         // Make local variables for these. (They get used a lot and I'm tired of this, this, this.)
2952         $x_tick_label_pos = $this->x_tick_label_pos;
2953         $x_data_label_pos = $this->x_data_label_pos;
2954         $x_tick_pos       = $this->x_tick_pos;
2955         $x_tick_len       = $this->x_tick_length;
2956         $y_tick_label_pos = $this->y_tick_label_pos;
2957         $y_tick_pos       = $this->y_tick_pos;
2958         $y_tick_len       = $this->y_tick_length;
2959         $y_data_label_pos = $this->y_data_label_pos;
2960
2961         // For X/Y tick and label position of 'xaxis' or 'yaxis', determine if the axis happens to be
2962         // on an edge of a plot. If it is, we need to account for the margins there.
2963         if ($this->x_axis_position <= $this->plot_min_y)
2964             $x_axis_pos = 'bottom';
2965         elseif ($this->x_axis_position >= $this->plot_max_y)
2966             $x_axis_pos = 'top';
2967         else
2968             $x_axis_pos = 'none';
2969         if ($this->y_axis_position <= $this->plot_min_x)
2970             $y_axis_pos = 'left';
2971         elseif ($this->y_axis_position >= $this->plot_max_x)
2972             $y_axis_pos = 'right';
2973         else
2974             $y_axis_pos = 'none';
2975
2976         // Calculate the heights for X tick and data labels, and the max (used if they are overlaid):
2977         $x_data_label_height = ($x_data_label_pos == 'none') ? 0 : $this->CalcMaxDataLabelSize('x');
2978         $x_tick_label_height = ($x_tick_label_pos == 'none') ? 0 : $this->CalcMaxTickLabelSize('x');
2979         $x_max_label_height = max($x_data_label_height, $x_tick_label_height);
2980
2981         // Calculate the space needed above and below the plot for X tick and X data labels:
2982
2983         // Above the plot:
2984         $tick_labels_above = ($x_tick_label_pos == 'plotup' || $x_tick_label_pos == 'both'
2985                           || ($x_tick_label_pos == 'xaxis' && $x_axis_pos == 'top'));
2986         $data_labels_above = ($x_data_label_pos == 'plotup' || $x_data_label_pos == 'both');
2987         if ($tick_labels_above) {
2988             if ($data_labels_above) {
2989                 $label_height_above = $x_max_label_height;
2990             } else {
2991                 $label_height_above = $x_tick_label_height;
2992             }
2993         } elseif ($data_labels_above) {
2994             $label_height_above = $x_data_label_height;
2995         } else {
2996             $label_height_above = 0;
2997         }
2998
2999         // Below the plot:
3000         $tick_labels_below = ($x_tick_label_pos == 'plotdown' || $x_tick_label_pos == 'both'
3001                           || ($x_tick_label_pos == 'xaxis' && $x_axis_pos == 'bottom'));
3002         $data_labels_below = ($x_data_label_pos == 'plotdown' || $x_data_label_pos == 'both');
3003         if ($tick_labels_below) {
3004             if ($data_labels_below) {
3005                 $label_height_below = $x_max_label_height;
3006             } else {
3007                 $label_height_below = $x_tick_label_height;
3008             }
3009         } elseif ($data_labels_below) {
3010             $label_height_below = $x_data_label_height;
3011         } else {
3012             $label_height_below = 0;
3013         }
3014
3015         // Calculate the width for Y tick and data labels, if on, and the max:
3016         // Note CalcMaxDataLabelSize('y') returns 0 except for swapped X/Y plots.
3017         $y_data_label_width = ($y_data_label_pos == 'none') ? 0 : $this->CalcMaxDataLabelSize('y');
3018         $y_tick_label_width = ($y_tick_label_pos == 'none') ? 0 : $this->CalcMaxTickLabelSize('y');
3019         $y_max_label_width = max($y_data_label_width, $y_tick_label_width);
3020
3021         // Calculate the space needed left and right of the plot for Y tick and Y data labels:
3022         // (Y data labels here are for swapped X/Y plots such has horizontal bars)
3023
3024         // Left of the plot:
3025         $tick_labels_left = ($y_tick_label_pos == 'plotleft' || $y_tick_label_pos == 'both'
3026                          || ($y_tick_label_pos == 'yaxis' && $y_axis_pos == 'left'));
3027         $data_labels_left = ($y_data_label_pos == 'plotleft' || $y_data_label_pos == 'both');
3028         if ($tick_labels_left) {
3029             if ($data_labels_left) {
3030                 $label_width_left = $y_max_label_width;
3031             } else {
3032                 $label_width_left = $y_tick_label_width;
3033             }
3034         } elseif ($data_labels_left) {
3035             $label_width_left = $y_data_label_width;
3036         } else {
3037             $label_width_left = 0;
3038         }
3039
3040         // Right of the plot:
3041         $tick_labels_right = ($y_tick_label_pos == 'plotright' || $y_tick_label_pos == 'both'
3042                           || ($y_tick_label_pos == 'yaxis' && $y_axis_pos == 'right'));
3043         $data_labels_right = ($y_data_label_pos == 'plotright' || $y_data_label_pos == 'both');
3044         if ($tick_labels_right) {
3045             if ($data_labels_right) {
3046                 $label_width_right = $y_max_label_width;
3047             } else {
3048                 $label_width_right = $y_tick_label_width;
3049             }
3050         } elseif ($data_labels_right) {
3051             $label_width_right = $y_data_label_width;
3052         } else {
3053             $label_width_right = 0;
3054         }
3055
3056         ///////// Calculate margins:
3057
3058         // Calculating Top and Bottom margins:
3059         // y_top_margin: Main title, Upper X title, X ticks and tick labels, and X data labels:
3060         // y_bot_margin: Lower title, ticks and tick labels, and data labels:
3061         $top_margin = $base_margin;
3062         $bot_margin = $base_margin;
3063         $this->x_title_top_offset = $gap;
3064         $this->x_title_bot_offset = $gap;
3065
3066         // Space for main title?
3067         if ($title_height > 0)
3068             $top_margin += $title_height + $gap;
3069
3070         // Space for X Title?
3071         if ($x_title_height > 0) {
3072             $pos = $this->x_title_pos;
3073             if ($pos == 'plotup' || $pos == 'both')
3074                 $top_margin += $x_title_height + $gap;
3075             if ($pos == 'plotdown' || $pos == 'both')
3076                 $bot_margin += $x_title_height + $gap;
3077         }
3078
3079         // Space for X Labels above the plot?
3080         if ($label_height_above > 0) {
3081             $top_margin += $label_height_above + $gap;
3082             $this->x_title_top_offset += $label_height_above + $gap;
3083         }
3084
3085         // Space for X Labels below the plot?
3086         if ($label_height_below > 0) {
3087             $bot_margin += $label_height_below + $gap;
3088             $this->x_title_bot_offset += $label_height_below + $gap;
3089         }
3090
3091         // Space for X Ticks above the plot?
3092         if ($x_tick_pos == 'plotup' || $x_tick_pos == 'both'
3093            || ($x_tick_pos == 'xaxis' && $x_axis_pos == 'top')) {
3094             $top_margin += $x_tick_len;
3095             $this->x_label_top_offset = $x_tick_len + $gap;
3096             $this->x_title_top_offset += $x_tick_len;
3097         } else {
3098             // No X Ticks above the plot:
3099             $this->x_label_top_offset = $gap;
3100         }
3101
3102         // Space for X Ticks below the plot?
3103         if ($x_tick_pos == 'plotdown' || $x_tick_pos == 'both'
3104            || ($x_tick_pos == 'xaxis' && $x_axis_pos == 'bottom')) {
3105             $bot_margin += $x_tick_len;
3106             $this->x_label_bot_offset = $x_tick_len + $gap;
3107             $this->x_title_bot_offset += $x_tick_len;
3108         } else {
3109             // No X Ticks below the plot:
3110             $this->x_label_bot_offset = $gap;
3111         }
3112         // Label offsets for on-axis ticks:
3113         if ($x_tick_pos == 'xaxis') {
3114             $this->x_label_axis_offset = $x_tick_len + $gap;
3115         } else {
3116             $this->x_label_axis_offset = $gap;
3117         }
3118
3119         // Calculating Left and Right margins:
3120         // x_left_margin: Left Y title, Y ticks and tick labels:
3121         // x_right_margin: Right Y title, Y ticks and tick labels:
3122         $left_margin = $base_margin;
3123         $right_margin = $base_margin;
3124         $this->y_title_left_offset = $gap;
3125         $this->y_title_right_offset = $gap;
3126
3127         // Space for Y Title?
3128         if ($y_title_width > 0) {
3129             $pos = $this->y_title_pos;
3130             if ($pos == 'plotleft' || $pos == 'both')
3131                 $left_margin += $y_title_width + $gap;
3132             if ($pos == 'plotright' || $pos == 'both')
3133                 $right_margin += $y_title_width + $gap;
3134         }
3135
3136         // Space for Y Labels left of the plot?
3137         if ($label_width_left > 0) {
3138             $left_margin += $label_width_left + $gap;
3139             $this->y_title_left_offset += $label_width_left + $gap;
3140         }
3141
3142         // Space for Y Labels right of the plot?
3143         if ($label_width_right > 0) {
3144             $right_margin += $label_width_right + $gap;
3145             $this->y_title_right_offset += $label_width_right + $gap;
3146         }
3147
3148         // Space for Y Ticks left of plot?
3149         if ($y_tick_pos == 'plotleft' || $y_tick_pos == 'both'
3150            || ($y_tick_pos == 'yaxis' && $y_axis_pos == 'left')) {
3151             $left_margin += $y_tick_len;
3152             $this->y_label_left_offset = $y_tick_len + $gap;
3153             $this->y_title_left_offset += $y_tick_len;
3154         } else {
3155             // No Y Ticks left of plot:
3156             $this->y_label_left_offset = $gap;
3157         }
3158
3159         // Space for Y Ticks right of plot?
3160         if ($y_tick_pos == 'plotright' || $y_tick_pos == 'both'
3161            || ($y_tick_pos == 'yaxis' && $y_axis_pos == 'right')) {
3162             $right_margin += $y_tick_len;
3163             $this->y_label_right_offset = $y_tick_len + $gap;
3164             $this->y_title_right_offset += $y_tick_len;
3165         } else {
3166             // No Y Ticks right of plot:
3167             $this->y_label_right_offset = $gap;
3168         }
3169
3170         // Label offsets for on-axis ticks:
3171         if ($x_tick_pos == 'yaxis') {
3172             $this->y_label_axis_offset = $y_tick_len + $gap;
3173         } else {
3174             $this->y_label_axis_offset = $gap;
3175         }
3176
3177         // Apply the minimum margins and store in the object.
3178         // Do not set margins which were user-defined (see note at top of function).
3179         if (!isset($this->y_top_margin))
3180             $this->y_top_margin = max($min_margin, $top_margin);
3181         if (!isset($this->y_bot_margin))
3182             $this->y_bot_margin = max($min_margin, $bot_margin);
3183         if (!isset($this->x_left_margin))
3184             $this->x_left_margin = max($min_margin, $left_margin);
3185         if (!isset($this->x_right_margin))
3186             $this->x_right_margin = max($min_margin, $right_margin);
3187
3188         if ($this->GetCallback('debug_scale')) {
3189             // (Too bad compact() doesn't work on class member variables...)
3190             $this->DoCallback('debug_scale', __FUNCTION__, array(
3191                 'label_height_above' => $label_height_above,
3192                 'label_height_below' => $label_height_below,
3193                 'label_width_left' => $label_width_left,
3194                 'label_width_right' => $label_width_right,
3195                 'x_tick_len' => $x_tick_len,
3196                 'y_tick_len' => $y_tick_len,
3197                 'x_left_margin' => $this->x_left_margin,
3198                 'x_right_margin' => $this->x_right_margin,
3199                 'y_top_margin' => $this->y_top_margin,
3200                 'y_bot_margin' => $this->y_bot_margin,
3201                 'x_label_top_offset' => $this->x_label_top_offset,
3202                 'x_label_bot_offset' => $this->x_label_bot_offset,
3203                 'y_label_left_offset' => $this->y_label_left_offset,
3204                 'y_label_right_offset' => $this->y_label_right_offset,
3205                 'x_title_top_offset' => $this->x_title_top_offset,
3206                 'x_title_bot_offset' => $this->x_title_bot_offset,
3207                 'y_title_left_offset' => $this->y_title_left_offset,
3208                 'y_title_right_offset' => $this->y_title_right_offset));
3209         }
3210
3211         return TRUE;
3212     }
3213
3214     /*
3215      * Calculate the plot area (device coordinates) from the margins.
3216      * (This used to be part of SetPlotAreaPixels.)
3217      * The margins might come from SetMarginsPixels, SetPlotAreaPixels,
3218      * or CalcMargins.
3219      */
3220     protected function CalcPlotAreaPixels()
3221     {
3222         $this->plot_area = array($this->x_left_margin, $this->y_top_margin,
3223                                  $this->image_width - $this->x_right_margin,
3224                                  $this->image_height - $this->y_bot_margin);
3225         $this->plot_area_width = $this->plot_area[2] - $this->plot_area[0];
3226         $this->plot_area_height = $this->plot_area[3] - $this->plot_area[1];
3227
3228         $this->DoCallback('debug_scale', __FUNCTION__, $this->plot_area);
3229         return TRUE;
3230     }
3231
3232     /*
3233      * Set the margins in pixels (left, right, top, bottom)
3234      * This determines the plot area, equivalent to SetPlotAreaPixels().
3235      * Deferred calculations now occur in CalcPlotAreaPixels().
3236      */
3237     function SetMarginsPixels($which_lm = NULL, $which_rm = NULL, $which_tm = NULL, $which_bm = NULL)
3238     {
3239         $this->x_left_margin = $which_lm;
3240         $this->x_right_margin = $which_rm;
3241         $this->y_top_margin = $which_tm;
3242         $this->y_bot_margin = $which_bm;
3243
3244         return TRUE;
3245     }
3246
3247     /*
3248      * Sets the limits for the plot area.
3249      * This stores the margins, not the area. That may seem odd, but
3250      * the idea is to make SetPlotAreaPixels and SetMarginsPixels two
3251      * ways to accomplish the same thing, and the deferred calculations
3252      * in CalcMargins and CalcPlotAreaPixels don't need to know which
3253      * was used.
3254      *   (x1, y1) - Upper left corner of the plot area
3255      *   (x2, y2) - Lower right corner of the plot area
3256      */
3257     function SetPlotAreaPixels($x1 = NULL, $y1 = NULL, $x2 = NULL, $y2 = NULL)
3258     {
3259         $this->x_left_margin = $x1;
3260         if (isset($x2)) $this->x_right_margin = $this->image_width - $x2;
3261         else unset($this->x_right_margin);
3262         $this->y_top_margin = $y1;
3263         if (isset($y2)) $this->y_bot_margin = $this->image_height - $y2;
3264         else unset($this->y_bot_margin);
3265
3266         return TRUE;
3267     }
3268
3269     /*
3270      * Calculate the World Coordinate limits of the plot area.
3271      * This goes with SetPlotAreaWorld, but the calculations are
3272      * deferred until the graph is being drawn.
3273      * Uses and sets: plot_min_x, plot_max_x, plot_min_y, plot_max_y
3274      * These can be user-supplied or NULL to auto-calculate.
3275      * Pre-requisites: FindDataLimits() calculates min_x, max_x, min_y, max_y
3276      * which are the limits of the data to be plotted.
3277      *
3278      * The general method is this:
3279      *   If any part of the range is user-defined (via SetPlotAreaWorld),
3280      *      use the user-defined value.
3281      *   Else, if this is an implicitly-defined independent variable,
3282      *      use the fixed range of 0 to (max+1).
3283      *   Else, if this is an explicitly-defined independent variable,
3284      *      use the exact data range (min to max).
3285      *   Else, this is the dependent variable, so define a range which
3286      *      includes and exceeds the data range by a bit.
3287      */
3288     protected function CalcPlotAreaWorld()
3289     {
3290         // Data array omits X or Y?
3291         $implied_x = $this->datatype_implied && !$this->datatype_swapped_xy;
3292         $implied_y = $this->datatype_implied && $this->datatype_swapped_xy;
3293
3294         if (isset($this->plot_min_x) && $this->plot_min_x !== '')
3295             $xmin = $this->plot_min_x; // Use user-provided value
3296         elseif ($implied_x)
3297             $xmin = 0;          // Implied X starts at zero
3298         elseif ($this->datatype_swapped_xy)
3299             // If X is the dependent variable, leave some room below.
3300             $xmin = floor($this->min_x - abs($this->min_x) * 0.1);
3301         else
3302             $xmin = $this->min_x;  // Otherwise just start at the min data X
3303
3304         if (isset($this->plot_max_x) && $this->plot_max_x !== '')
3305             $xmax = $this->plot_max_x; // Use user-provided value
3306         elseif ($implied_x)
3307             $xmax = $this->max_x + 1; // Implied X ends after last value
3308         elseif ($this->datatype_swapped_xy)
3309             // If X is the dependent variable, leave some room above.
3310             $xmax = ceil($this->max_x + abs($this->max_x) * 0.1);
3311         else
3312             $xmax = $this->max_x; // Otherwise just end at the max data X
3313
3314         if (isset($this->plot_min_y) && $this->plot_min_y !== '')
3315             $ymin = $this->plot_min_y;  // Use user-provided value
3316         elseif ($implied_y)
3317             $ymin = 0;    // Implied Y starts at zero
3318         elseif ($this->datatype_swapped_xy)
3319             $ymin = $this->min_y; // Start at min data Y
3320         else
3321             // If Y is the dependent variable, leave some room below.
3322             $ymin = floor($this->min_y - abs($this->min_y) * 0.1);
3323
3324         if (isset($this->plot_max_y) && $this->plot_max_y !== '')
3325             $ymax = $this->plot_max_y; // Use user-provided value
3326         elseif ($implied_y)
3327             $ymax = $this->max_y + 1; // Implied Y ends after last value
3328         elseif ($this->datatype_swapped_xy)
3329             $ymax = $this->max_y;  // End at max data Y
3330         else
3331             // If Y is the dependent variable, leave some room above.
3332             $ymax = ceil($this->max_y + abs($this->max_y) * 0.1);
3333
3334         // Error checking
3335
3336         if ($ymin == $ymax)
3337             $ymax++;
3338         if ($xmin == $xmax)
3339             $xmax++;
3340
3341         if ($this->yscale_type == 'log') {
3342             if ($ymin <= 0) {
3343                 $ymin = 1;
3344             }
3345             if ($ymax <= 0) {
3346                 // Note: Error messages reference the user function, not this function.
3347                 return $this->PrintError('SetPlotAreaWorld(): Log plots need data greater than 0');
3348             }
3349         }
3350
3351         if ($ymax <= $ymin) {
3352             return $this->PrintError('SetPlotAreaWorld(): Error in data - max not greater than min');
3353         }
3354
3355         $this->plot_min_x = $xmin;
3356         $this->plot_max_x = $xmax;
3357         $this->plot_min_y = $ymin;
3358         $this->plot_max_y = $ymax;
3359         if ($this->GetCallback('debug_scale')) {
3360             $this->DoCallback('debug_scale', __FUNCTION__, array(
3361                 'plot_min_x' => $this->plot_min_x, 'plot_min_y' => $this->plot_min_y,
3362                 'plot_max_x' => $this->plot_max_x, 'plot_max_y' => $this->plot_max_y));
3363         }
3364         return TRUE;
3365     }
3366
3367     /*
3368      * Stores the desired World Coordinate range of the plot.
3369      * The user calls this to force one or more of the range limits to
3370      * specific values. Anything not set will be calculated in CalcPlotAreaWorld().
3371      */
3372     function SetPlotAreaWorld($xmin=NULL, $ymin=NULL, $xmax=NULL, $ymax=NULL)
3373     {
3374         $this->plot_min_x = $xmin;
3375         $this->plot_max_x = $xmax;
3376         $this->plot_min_y = $ymin;
3377         $this->plot_max_y = $ymax;
3378         return TRUE;
3379     }
3380
3381     /*
3382      * Calculate the width (or height) of bars for bar plots.
3383      *   $stacked : If true, this is a stacked bar plot (1 bar per group).
3384      *   $verticals : If false, this is a horizontal bar plot.
3385      * This calculates:
3386      *    record_bar_width : Allocated width for each bar (including gaps)
3387      *    actual_bar_width : Actual drawn width of each bar
3388      *    bar_adjust_gap  : Gap on each side of each bar (0 if they touch)
3389      * For the case $verticals=False, horizontal bars are being drawn,
3390      * but the same variable names are used. Think of "bar_width" as being
3391      * the width if you are standing on the Y axis looking towards positive X.
3392      */
3393     protected function CalcBarWidths($stacked, $verticals)
3394     {
3395         // group_width is the width of a group, including padding
3396         if ($verticals) {
3397             $group_width = $this->plot_area_width / $this->num_data_rows;
3398         } else {
3399             $group_width = $this->plot_area_height / $this->num_data_rows;
3400         }
3401
3402         // Actual number of bar spaces in the group. This includes the drawn bars, and
3403         // 'bar_extra_space'-worth of extra bars.
3404         if ($stacked) {
3405             $num_spots = 1 + $this->bar_extra_space;
3406         } else {
3407             $num_spots = $this->data_columns + $this->bar_extra_space;
3408         }
3409
3410         // record_bar_width is the width of each bar's allocated area.
3411         // If bar_width_adjust=1 this is the width of the bar, otherwise
3412         // the bar is centered inside record_bar_width.
3413         // The equation is:
3414         //   group_frac_width * group_width = record_bar_width * num_spots
3415         $this->record_bar_width = $this->group_frac_width * $group_width / $num_spots;
3416
3417         // Note that the extra space due to group_frac_width and bar_extra_space will be
3418         // evenly divided on each side of the group: the drawn bars are centered in the group.
3419
3420         // Within each bar's allocated space, if bar_width_adjust=1 the bar fills the
3421         // space, otherwise it is centered.
3422         // This is the actual drawn bar width:
3423         $this->actual_bar_width = $this->record_bar_width * $this->bar_width_adjust;
3424         // This is the gap on each side of the bar (0 if bar_width_adjust=1):
3425         $this->bar_adjust_gap = ($this->record_bar_width - $this->actual_bar_width) / 2;
3426
3427         if ($this->GetCallback('debug_scale')) {
3428             $this->DoCallback('debug_scale', __FUNCTION__, array(
3429                 'record_bar_width' => $this->record_bar_width,
3430                 'actual_bar_width' => $this->actual_bar_width,
3431                 'bar_adjust_gap' => $this->bar_adjust_gap));
3432         }
3433         return TRUE;
3434     }
3435
3436     /*
3437      * Calculate X and Y Axis Positions, world coordinates.
3438      * This needs the min/max x/y range set by CalcPlotAreaWorld.
3439      * It adjusts or sets x_axis_position and y_axis_position per the data.
3440      * Empty string means the values need to be calculated; otherwise they
3441      * are supplied but need to be validated against the World area.
3442      *
3443      * Note: This used to be in CalcTranslation, but CalcMargins needs it too.
3444      * This does not calculate the pixel values of the axes. That happens in
3445      * CalcTranslation, after scaling is set up (which has to happen after
3446      * margins are set up).
3447      *
3448      * For vertical plots, the X axis defaults to Y=0 if that is inside the plot range, else whichever
3449      * of the top or bottom that has the smallest absolute value (that is, the value closest to 0).
3450      * The Y axis defaults to the left edge. For horizontal plots, the axis roles and defaults are switched.
3451      */
3452     protected function CalcAxisPositions()
3453     {
3454         // Validate user-provided X axis position, or calculate a default if not provided:
3455         if ($this->x_axis_position !== '') {
3456             // Force user-provided X axis position to be within the plot range:
3457             $this->x_axis_position = min(max($this->plot_min_y, $this->x_axis_position), $this->plot_max_y);
3458         } elseif ($this->yscale_type == 'log') {
3459             // Always use 1 for X axis position on log scale plots.
3460             $this->x_axis_position = 1;
3461         } elseif ($this->datatype_swapped_xy || $this->plot_min_y > 0) {
3462             // Horizontal plot, or Vertical Plot with all Y > 0: Place X axis on the bottom.
3463             $this->x_axis_position = $this->plot_min_y;
3464         } elseif ($this->plot_max_y < 0) {
3465             // Vertical plot with all Y < 0, so place the X axis at the top.
3466             $this->x_axis_position = $this->plot_max_y;
3467         } else {
3468             // Vertical plot range includes Y=0, so place X axis at 0.
3469             $this->x_axis_position = 0;
3470         }
3471
3472         // Validate user-provided Y axis position, or calculate a default if not provided:
3473         if ($this->y_axis_position !== '') {
3474             // Force user-provided Y axis position to be within the plot range:
3475             $this->y_axis_position = min(max($this->plot_min_x, $this->y_axis_position), $this->plot_max_x);
3476         } elseif ($this->xscale_type == 'log') {
3477             // Always use 1 for Y axis position on log scale plots.
3478             $this->y_axis_position = 1;
3479         } elseif (!$this->datatype_swapped_xy || $this->plot_min_x > 0) {
3480             // Vertical plot, or Horizontal Plot with all X > 0: Place Y axis on left side.
3481             $this->y_axis_position = $this->plot_min_x;
3482         } elseif ($this->plot_max_x < 0) {
3483             // Horizontal plot with all X < 0, so place the Y axis on the right side.
3484             $this->y_axis_position = $this->plot_max_x;
3485         } else {
3486             // Horizontal plot range includes X=0: place Y axis at 0.
3487             $this->y_axis_position = 0;
3488         }
3489
3490         if ($this->GetCallback('debug_scale')) {
3491             $this->DoCallback('debug_scale', __FUNCTION__, array(
3492                 'x_axis_position' => $this->x_axis_position,
3493                 'y_axis_position' => $this->y_axis_position));
3494         }
3495
3496         return TRUE;
3497     }
3498
3499     /*
3500      * Calculates scaling stuff...
3501      */
3502     protected function CalcTranslation()
3503     {
3504         if ($this->plot_max_x - $this->plot_min_x == 0) { // Check for div by 0
3505             $this->xscale = 0;
3506         } else {
3507             if ($this->xscale_type == 'log') {
3508                 $this->xscale = $this->plot_area_width /
3509                                 (log10($this->plot_max_x) - log10($this->plot_min_x));
3510             } else {
3511                 $this->xscale = $this->plot_area_width / ($this->plot_max_x - $this->plot_min_x);
3512             }
3513         }
3514
3515         if ($this->plot_max_y - $this->plot_min_y == 0) { // Check for div by 0
3516             $this->yscale = 0;
3517         } else {
3518             if ($this->yscale_type == 'log') {
3519                 $this->yscale = $this->plot_area_height /
3520                                 (log10($this->plot_max_y) - log10($this->plot_min_y));
3521             } else {
3522                 $this->yscale = $this->plot_area_height / ($this->plot_max_y - $this->plot_min_y);
3523             }
3524         }
3525         // GD defines x = 0 at left and y = 0 at TOP so -/+ respectively
3526         if ($this->xscale_type == 'log') {
3527             $this->plot_origin_x = $this->plot_area[0] - ($this->xscale * log10($this->plot_min_x) );
3528         } else {
3529             $this->plot_origin_x = $this->plot_area[0] - ($this->xscale * $this->plot_min_x);
3530         }
3531         if ($this->yscale_type == 'log') {
3532             $this->plot_origin_y = $this->plot_area[3] + ($this->yscale * log10($this->plot_min_y));
3533         } else {
3534             $this->plot_origin_y = $this->plot_area[3] + ($this->yscale * $this->plot_min_y);
3535         }
3536
3537         // Convert axis positions to device coordinates:
3538         $this->y_axis_x_pixels = $this->xtr($this->y_axis_position);
3539         $this->x_axis_y_pixels = $this->ytr($this->x_axis_position);
3540
3541         if ($this->GetCallback('debug_scale')) {
3542             $this->DoCallback('debug_scale', __FUNCTION__, array(
3543                 'xscale' => $this->xscale, 'yscale' => $this->yscale,
3544                 'plot_origin_x' => $this->plot_origin_x, 'plot_origin_y' => $this->plot_origin_y,
3545                 'y_axis_x_pixels' => $this->y_axis_x_pixels,
3546                 'x_axis_y_pixels' => $this->x_axis_y_pixels));
3547         }
3548
3549         return TRUE;
3550     }
3551
3552     /*
3553      * Translate X world coordinate into pixel coordinate
3554      * See CalcTranslation() for calculation of xscale.
3555      * Note: This function should be 'protected', but is left public for historical reasons.
3556      * See GetDeviceXY() for a preferred public method.
3557      */
3558     function xtr($x_world)
3559     {
3560         if ($this->xscale_type == 'log') {
3561             $x_pixels = $this->plot_origin_x + log10($x_world) * $this->xscale ;
3562         } else {
3563             $x_pixels = $this->plot_origin_x + $x_world * $this->xscale ;
3564         }
3565         return round($x_pixels);
3566     }
3567
3568     /*
3569      * Translate Y world coordinate into pixel coordinate.
3570      * See CalcTranslation() for calculation of yscale.
3571      * Note: This function should be 'protected', but is left public for historical reasons.
3572      * See GetDeviceXY() for a preferred public method.
3573      */
3574     function ytr($y_world)
3575     {
3576         if ($this->yscale_type == 'log') {
3577             //minus because GD defines y = 0 at top. doh!
3578             $y_pixels =  $this->plot_origin_y - log10($y_world) * $this->yscale ;
3579         } else {
3580             $y_pixels =  $this->plot_origin_y - $y_world * $this->yscale ;
3581         }
3582         return round($y_pixels);
3583     }
3584
3585     /* A public interface to xtr and ytr. Translates (x,y) in world coordinates
3586      * to (x,y) in device coordinates and returns them as an array.
3587      * Usage is: list($x_pixel, $y_pixel) = $plot->GetDeviceXY($x_world, $y_world)
3588      */
3589     function GetDeviceXY($x_world, $y_world)
3590     {
3591         if (!isset($this->xscale)) {
3592             return $this->PrintError("GetDeviceXY() was called before translation factors were calculated");
3593         }
3594         return array($this->xtr($x_world), $this->ytr($y_world));
3595     }
3596
3597     /*
3598      * Calculate tick parameters: Start, end, and delta values. This is used
3599      * by both DrawXTicks() and DrawYTicks().
3600      * This currently uses the same simplistic method previously used by
3601      * PHPlot (basically just range/10), but splitting this out into its
3602      * own function is the first step in replacing the method.
3603      * This is also used by CalcMaxTickSize() for CalcMargins().
3604      *
3605      *   $which : 'x' or 'y' : Which tick parameters to calculate
3606      *
3607      * Returns an array of 3 elements: tick_start, tick_end, tick_step
3608      */
3609     protected function CalcTicks($which)
3610     {
3611         if ($which == 'x') {
3612             $num_ticks = $this->num_x_ticks;
3613             $tick_inc = $this->x_tick_inc;
3614             $data_max = $this->plot_max_x;
3615             $data_min = $this->plot_min_x;
3616             $skip_lo = $this->skip_left_tick;
3617             $skip_hi = $this->skip_right_tick;
3618         } elseif ($which == 'y') {
3619             $num_ticks = $this->num_y_ticks;
3620             $tick_inc = $this->y_tick_inc;
3621             $data_max = $this->plot_max_y;
3622             $data_min = $this->plot_min_y;
3623             $skip_lo = $this->skip_bottom_tick;
3624             $skip_hi = $this->skip_top_tick;
3625         } else {
3626             return $this->PrintError("CalcTicks: Invalid usage ($which)");
3627         }
3628
3629         if (!empty($tick_inc)) {
3630             $tick_step = $tick_inc;
3631         } elseif (!empty($num_ticks)) {
3632             $tick_step = ($data_max - $data_min) / $num_ticks;
3633         } else {
3634             $tick_step = ($data_max - $data_min) / 10;
3635         }
3636
3637         // NOTE: When working with floats, because of approximations when adding $tick_step,
3638         // the value may not quite reach the end, or may exceed it very slightly.
3639         // So apply a "fudge" factor.
3640         $tick_start = (double)$data_min;
3641         $tick_end = (double)$data_max + ($data_max - $data_min) / 10000.0;
3642
3643         if ($skip_lo)
3644             $tick_start += $tick_step;
3645
3646         if ($skip_hi)
3647             $tick_end -= $tick_step;
3648
3649         return array($tick_start, $tick_end, $tick_step);
3650     }
3651
3652     /*
3653      * Calculate the size of the biggest tick label. This is used by CalcMargins().
3654      * For 'x' ticks, it returns the height . For 'y' ticks, it returns the width.
3655      * This means height along Y, or width along X - not relative to the text angle.
3656      * That is what we need to calculate the needed margin space.
3657      * (Previous versions of PHPlot estimated this, using the maximum X or Y value,
3658      * or maybe the longest string. That doesn't work. -10 is longer than 9, etc.
3659      * So this gets the actual size of each label, slow as that may be.
3660      */
3661     protected function CalcMaxTickLabelSize($which)
3662     {
3663         list($tick_start, $tick_end, $tick_step) = $this->CalcTicks($which);
3664
3665         if ($which == 'x') {
3666             $font = $this->fonts['x_label'];
3667             $angle = $this->x_label_angle;
3668         } elseif ($which == 'y') {
3669             $font = $this->fonts['y_label'];
3670             $angle = $this->y_label_angle;
3671         } else {
3672             return $this->PrintError("CalcMaxTickLabelSize: Invalid usage ($which)");
3673         }
3674
3675         $max_width = 0;
3676         $max_height = 0;
3677
3678         // Loop over ticks, same as DrawXTicks and DrawYTicks:
3679         // Avoid cumulative round-off errors from $val += $delta
3680         $n = 0;
3681         $tick_val = $tick_start;
3682         while ($tick_val <= $tick_end) {
3683             $tick_label = $this->FormatLabel($which, $tick_val);
3684             list($width, $height) = $this->SizeText($font, $angle, $tick_label);
3685             if ($width > $max_width) $max_width = $width;
3686             if ($height > $max_height) $max_height = $height;
3687             $tick_val = $tick_start + ++$n * $tick_step;
3688         }
3689         if ($this->GetCallback('debug_scale')) {
3690             $this->DoCallback('debug_scale', __FUNCTION__, array(
3691                 'which' => $which, 'height' => $max_height, 'width' => $max_width));
3692         }
3693
3694         if ($which == 'x')
3695             return $max_height;
3696         return $max_width;
3697     }
3698
3699     /*
3700      * Calculate the size of the biggest data label. This is used by CalcMargins().
3701      * For $which='x', it returns the height of labels along the top or bottom.
3702      * For $which='y', it returns the width of labels along the left or right sides.
3703      * There is only one set of data labels (the first position in each data record).
3704      * They normally go along the top or bottom (or both). If the data type indicates
3705      * X/Y swapping (which is used for horizontal bar charts), the data labels go
3706      * along the sides instead. So CalcMaxDataLabelSize('x') returns 0 if the
3707      * data is X/Y swapped, and CalcMaxDataLabelSize('y') returns 0 if the data is
3708      * is not X/Y swapped.
3709      */
3710     protected function CalcMaxDataLabelSize($which = 'x')
3711     {
3712         if ($which == 'x') {
3713           if ($this->datatype_swapped_xy)
3714               return 0; // Shortcut: labels aren't on top/bottom.
3715           $font = $this->fonts['x_label'];
3716           $angle = $this->x_data_label_angle;
3717           $format_code = 'xd';
3718         } elseif ($which == 'y') {
3719           if (!$this->datatype_swapped_xy)
3720               return 0; // Shortcut: labels aren't on left/right.
3721           $font = $this->fonts['y_label'];
3722           $angle = $this->y_data_label_angle;
3723           $format_code = 'yd';
3724         } else {
3725           return $this->PrintError("CalcMaxDataLabelSize: Invalid usage ($which)");
3726         }
3727         $max_width = 0;
3728         $max_height = 0;
3729
3730         // Loop over all data labels and find the biggest:
3731         for ($i = 0; $i < $this->num_data_rows; $i++) {
3732             $label = $this->FormatLabel($format_code, $this->data[$i][0]);
3733             list($width, $height) = $this->SizeText($font, $angle, $label);
3734             if ($width > $max_width) $max_width = $width;
3735             if ($height > $max_height) $max_height = $height;
3736         }
3737         if ($this->GetCallback('debug_scale')) {
3738             $this->DoCallback('debug_scale', __FUNCTION__, array(
3739                 'height' => $max_height, 'width' => $max_width));
3740         }
3741
3742         if ($this->datatype_swapped_xy)
3743             return $max_width;
3744         return $max_height;
3745     }
3746
3747     /*
3748      * Set grid control defaults.
3749      * X grid defaults off, Y grid defaults on, except the reverse is true
3750      * with swapped graphs such as horizontal bars.
3751      */
3752     protected function CalcGridSettings()
3753     {
3754         if (!isset($this->draw_x_grid))
3755             $this->draw_x_grid = $this->datatype_swapped_xy;
3756         if (!isset($this->draw_y_grid))
3757             $this->draw_y_grid = !$this->datatype_swapped_xy;
3758     }
3759
3760     /*
3761      * Helper for CheckLabels() - determine if there are any non-empty labels.
3762      * Returns True if all data labels are empty, else False.
3763      */
3764     protected function CheckLabelsAllEmpty()
3765     {
3766         for ($i = 0; $i < $this->num_data_rows; $i++)
3767             if ($this->data[$i][0] !== '') return FALSE;
3768         return TRUE;
3769     }
3770
3771     /*
3772      * Check and set label parameters. This handles deferred processing for label
3773      * positioning and other label-related parameters.
3774      *   Copy label_format from 'x' to 'xd', and 'y' to 'yd', if not already set.
3775      *   Set x_data_label_angle from x_label_angle, if not already set.
3776      *   Apply defaults to X and Y tick and data label positions.
3777      * Note: the label strings in the data array are used as X data labels in
3778      * the normal case, but as Y data labels in the swapped X/Y case.
3779      */
3780     protected function CheckLabels()
3781     {
3782         // The X and Y data labels are formatted the same as X and Y tick labels,
3783         // unless overridden. Check and apply defaults for FormatLabel here:
3784         if (empty($this->label_format['xd']) && !empty($this->label_format['x']))
3785             $this->label_format['xd'] = $this->label_format['x'];
3786         if (empty($this->label_format['yd']) && !empty($this->label_format['y']))
3787             $this->label_format['yd'] = $this->label_format['y'];
3788
3789         // The X tick label angle setting controls X data label angles too,
3790         // unless overridden. Check and apply the default here:
3791         if (!isset($this->x_data_label_angle))
3792             $this->x_data_label_angle = $this->x_label_angle;
3793         // Note: Y data label angle defaults to zero, unlike X,
3794         // for compatibility with older releases.
3795
3796         // X Label position fixups, for x_data_label_pos and x_tick_label_pos:
3797         if ($this->datatype_swapped_xy) {
3798             // Just apply defaults - there is no position conflict for X labels.
3799             if (!isset($this->x_tick_label_pos))
3800                 $this->x_tick_label_pos = 'plotdown';
3801             if (!isset($this->x_data_label_pos))
3802                 $this->x_data_label_pos = 'none';
3803         } else {
3804             // Apply defaults but do not allow conflict between tick and data labels.
3805             if (isset($this->x_data_label_pos)) {
3806                 if (!isset($this->x_tick_label_pos)) {
3807                     // Case: data_label_pos is set, tick_label_pos needs a default:
3808                     if ($this->x_data_label_pos == 'none')
3809                         $this->x_tick_label_pos = 'plotdown';
3810                     else
3811                         $this->x_tick_label_pos = 'none';
3812                 }
3813             } elseif (isset($this->x_tick_label_pos)) {
3814                 // Case: tick_label_pos is set, data_label_pos needs a default:
3815                 if ($this->x_tick_label_pos == 'none')
3816                     $this->x_data_label_pos = 'plotdown';
3817                 else
3818                     $this->x_data_label_pos = 'none';
3819             } else {
3820                 // Case: Neither tick_label_pos nor data_label_pos is set.
3821                 // We do not want them to be both on (as PHPlot used to do in this case).
3822                 // Turn on data labels if any were supplied, else tick labels.
3823                 if ($this->CheckLabelsAllEmpty()) {
3824                     $this->x_data_label_pos = 'none';
3825                     $this->x_tick_label_pos = 'plotdown';
3826                 } else {
3827                     $this->x_data_label_pos = 'plotdown';
3828                     $this->x_tick_label_pos = 'none';
3829                 }
3830             }
3831         }
3832
3833         // Y Label position fixups, for y_data_label_pos and y_tick_label_pos:
3834         if (!$this->datatype_swapped_xy) {
3835             // Just apply defaults - there is no position conflict.
3836             if (!isset($this->y_tick_label_pos))
3837                 $this->y_tick_label_pos = 'plotleft';
3838             if (!isset($this->y_data_label_pos))
3839                 $this->y_data_label_pos = 'none';
3840         } else {
3841             // Apply defaults but do not allow conflict between tick and data labels.
3842             if (isset($this->y_data_label_pos)) {
3843                 if (!isset($this->y_tick_label_pos)) {
3844                     // Case: data_label_pos is set, tick_label_pos needs a default:
3845                     if ($this->y_data_label_pos == 'none')
3846                         $this->y_tick_label_pos = 'plotleft';
3847                     else
3848                         $this->y_tick_label_pos = 'none';
3849                 }
3850             } elseif (isset($this->y_tick_label_pos)) {
3851                 // Case: tick_label_pos is set, data_label_pos needs a default:
3852                 if ($this->y_tick_label_pos == 'none')
3853                     $this->y_data_label_pos = 'plotleft';
3854                 else
3855                     $this->y_data_label_pos = 'none';
3856             } else {
3857                 // Case: Neither tick_label_pos nor data_label_pos is set.
3858                 // Turn on data labels if any were supplied, else tick labels.
3859                 if ($this->CheckLabelsAllEmpty()) {
3860                     $this->y_data_label_pos = 'none';
3861                     $this->y_tick_label_pos = 'plotleft';
3862                 } else {
3863                     $this->y_data_label_pos = 'plotleft';
3864                     $this->y_tick_label_pos = 'none';
3865                 }
3866             }
3867         }
3868         return TRUE;
3869     }
3870
3871     /*
3872      * Formats a tick or data label.
3873      *    which_pos - 'x', 'xd', 'y', or 'yd', selects formatting controls.
3874      *        x, y are for tick labels; xd, yd are for data labels.
3875      *    which_lab - String to format as a label.
3876      * Credits: Time formatting suggested by Marlin Viss
3877      *          Custom formatting suggested by zer0x333
3878      * Notes:
3879      *   Type 'title' is obsolete and retained for compatibility.
3880      *   Class variable 'data_units_text' is retained as a suffix for 'data' type formatting for
3881      *      backward compatibility. Since there was never a function/method to set it, there
3882      *      could be somebody out there who sets it directly in the object.
3883      */
3884     protected function FormatLabel($which_pos, $which_lab)
3885     {
3886         // Assign a reference shortcut to the label format controls.
3887         // Note CheckLabels() made sure the 'xd' and 'yd' arrays are set.
3888         $format =& $this->label_format[$which_pos];
3889
3890         // Don't format empty strings (especially as time or numbers), or if no type was set.
3891         if ($which_lab !== '' && !empty($format['type'])) {
3892             switch ($format['type']) {
3893             case 'title':  // Note: This is obsolete
3894                 $which_lab = @ $this->data[$which_lab][0];
3895                 break;
3896             case 'data':
3897                 $which_lab = $format['prefix']
3898                            . $this->number_format($which_lab, $format['precision'])
3899                            . $this->data_units_text  // Obsolete
3900                            . $format['suffix'];
3901                 break;
3902             case 'time':
3903                 $which_lab = strftime($format['time_format'], $which_lab);
3904                 break;
3905             case 'printf':
3906                 $which_lab = sprintf($format['printf_format'], $which_lab);
3907                 break;
3908             case 'custom':
3909                 $which_lab = call_user_func($format['custom_callback'], $which_lab, $format['custom_arg']);
3910                 break;
3911
3912             }
3913         }
3914         return $which_lab;
3915     }
3916
3917 /////////////////////////////////////////////
3918 ///////////////                         TICKS
3919 /////////////////////////////////////////////
3920
3921     /*
3922      * Set the step (interval) between X ticks.
3923      * Use either this or SetNumXTicks(), not both, to control the X tick marks.
3924      */
3925     function SetXTickIncrement($which_ti='')
3926     {
3927         $this->x_tick_inc = $which_ti;
3928         if (!empty($which_ti)) {
3929             $this->num_x_ticks = '';
3930         }
3931         return TRUE;
3932     }
3933
3934     /*
3935      * Set the step (interval) between Y ticks.
3936      * Use either this or SetNumYTicks(), not both, to control the Y tick marks.
3937      */
3938     function SetYTickIncrement($which_ti='')
3939     {
3940         $this->y_tick_inc = $which_ti;
3941         if (!empty($which_ti)) {
3942             $this->num_y_ticks = '';
3943         }
3944         return TRUE;
3945     }
3946
3947     /*
3948      * Set the number of X tick marks.
3949      * Use either this or SetXTickIncrement(), not both, to control the X tick marks.
3950      */
3951     function SetNumXTicks($which_nt='')
3952     {
3953         $this->num_x_ticks = $which_nt;
3954         if (!empty($which_nt)) {
3955             $this->x_tick_inc = '';
3956         }
3957         return TRUE;
3958     }
3959
3960     /*
3961      * Set the number of Y tick marks.
3962      * Use either this or SetYTickIncrement(), not both, to control the Y tick marks.
3963      */
3964     function SetNumYTicks($which_nt='')
3965     {
3966         $this->num_y_ticks = $which_nt;
3967         if (!empty($which_nt)) {
3968             $this->y_tick_inc = '';  //either use num_y_ticks or y_tick_inc, not both
3969         }
3970         return TRUE;
3971     }
3972
3973     /*
3974      * Set the position for the X tick marks.
3975      * These can be above the plot, below, both positions, at the X axis, or suppressed.
3976      */
3977     function SetXTickPos($which_tp)
3978     {
3979         $this->x_tick_pos = $this->CheckOption($which_tp, 'plotdown, plotup, both, xaxis, none',
3980                                                __FUNCTION__);
3981         return (boolean)$this->x_tick_pos;
3982     }
3983
3984     /*
3985      * Set the position for the Y tick marks.
3986      * These can be left of the plot, right, both positions, at the Y axis, or suppressed.
3987      */
3988     function SetYTickPos($which_tp)
3989     {
3990         $this->y_tick_pos = $this->CheckOption($which_tp, 'plotleft, plotright, both, yaxis, none',
3991                                               __FUNCTION__);
3992         return (boolean)$this->y_tick_pos;
3993     }
3994
3995     /*
3996      * Skip the top-most Y axis tick mark and label if $skip is true.
3997      */
3998     function SetSkipTopTick($skip)
3999     {
4000         $this->skip_top_tick = (bool)$skip;
4001         return TRUE;
4002     }
4003
4004     /*
4005      * Skip the bottom-most Y axis tick mark and label if $skip is true.
4006      */
4007     function SetSkipBottomTick($skip)
4008     {
4009         $this->skip_bottom_tick = (bool)$skip;
4010         return TRUE;
4011     }
4012
4013     /*
4014      * Skip the left-most X axis tick mark and label if $skip is true.
4015      */
4016     function SetSkipLeftTick($skip)
4017     {
4018         $this->skip_left_tick = (bool)$skip;
4019         return TRUE;
4020     }
4021
4022     /*
4023      * Skip the right-most X axis tick mark and label if $skip is true.
4024      */
4025     function SetSkipRightTick($skip)
4026     {
4027         $this->skip_right_tick = (bool)$skip;
4028         return TRUE;
4029     }
4030
4031     /*
4032      * Set the outer length of X tick marks to $which_xln pixels.
4033      * This is the part of the tick mark that sticks out from the plot area.
4034      */
4035     function SetXTickLength($which_xln)
4036     {
4037         $this->x_tick_length = $which_xln;
4038         return TRUE;
4039     }
4040
4041     /*
4042      * Set the outer length of Y tick marks to $which_yln pixels.
4043      * This is the part of the tick mark that sticks out from the plot area.
4044      */
4045     function SetYTickLength($which_yln)
4046     {
4047         $this->y_tick_length = $which_yln;
4048         return TRUE;
4049     }
4050
4051     /*
4052      * Set the crossing length of X tick marks to $which_xc pixels.
4053      * This is the part of the tick mark that sticks into the plot area.
4054      */
4055     function SetXTickCrossing($which_xc)
4056     {
4057         $this->x_tick_cross = $which_xc;
4058         return TRUE;
4059     }
4060
4061     /*
4062      * Set the crossing length of Y tick marks to $which_yc pixels.
4063      * This is the part of the tick mark that sticks into the plot area.
4064      */
4065     function SetYTickCrossing($which_yc)
4066     {
4067         $this->y_tick_cross = $which_yc;
4068         return TRUE;
4069     }
4070
4071 /////////////////////////////////////////////
4072 ////////////////////          GENERIC DRAWING
4073 /////////////////////////////////////////////
4074
4075     /*
4076      * Fill the image background, with a tiled image file or solid color.
4077      */
4078     protected function DrawBackground()
4079     {
4080         // Don't draw this twice if drawing two plots on one image
4081         if (empty($this->done['background'])) {
4082             if (isset($this->bgimg)) {    // If bgimg is defined, use it
4083                 $this->tile_img($this->bgimg, 0, 0, $this->image_width, $this->image_height, $this->bgmode);
4084             } else {                        // Else use solid color
4085                 ImageFilledRectangle($this->img, 0, 0, $this->image_width, $this->image_height,
4086                                      $this->ndx_bg_color);
4087             }
4088             $this->done['background'] = TRUE;
4089         }
4090         return TRUE;
4091     }
4092
4093     /*
4094      * Fill the plot area background, with a tiled image file or solid color.
4095      */
4096     protected function DrawPlotAreaBackground()
4097     {
4098         if (isset($this->plotbgimg)) {
4099             $this->tile_img($this->plotbgimg, $this->plot_area[0], $this->plot_area[1],
4100                             $this->plot_area_width, $this->plot_area_height, $this->plotbgmode);
4101         } elseif ($this->draw_plot_area_background) {
4102             ImageFilledRectangle($this->img, $this->plot_area[0], $this->plot_area[1],
4103                                  $this->plot_area[2], $this->plot_area[3], $this->ndx_plot_bg_color);
4104         }
4105         return TRUE;
4106     }
4107
4108     /*
4109      * Tiles an image at some given coordinates.
4110      *   $file : Filename of the picture to be used as tile.
4111      *   $xorig : X device coordinate of where the tile is to begin.
4112      *   $yorig : Y device coordinate of where the tile is to begin.
4113      *   $width : Width of the area to be tiled.
4114      *   $height : Height of the area to be tiled.
4115      *   $mode : Tiling mode. One of 'centeredtile', 'tile', 'scale'.
4116      */
4117     protected function tile_img($file, $xorig, $yorig, $width, $height, $mode)
4118     {
4119         $im = $this->GetImage($file, $tile_width, $tile_height);
4120         if (!$im)
4121             return FALSE;  // GetImage already produced an error message.
4122
4123         if ($mode == 'scale') {
4124             imagecopyresampled($this->img, $im, $xorig, $yorig, 0, 0, $width, $height,
4125                                $tile_width, $tile_height);
4126             return TRUE;
4127         }
4128
4129         if ($mode == 'centeredtile') {
4130             $x0 = - floor($tile_width/2);   // Make the tile look better
4131             $y0 = - floor($tile_height/2);
4132         } else {      // Accept anything else as $mode == 'tile'
4133             $x0 = 0;
4134             $y0 = 0;
4135         }
4136
4137         // Draw the tile onto a temporary image first.
4138         $tmp = imagecreate($width, $height);
4139         if (! $tmp)
4140             return $this->PrintError('tile_img(): Could not create image resource.');
4141
4142         for ($x = $x0; $x < $width; $x += $tile_width)
4143             for ($y = $y0; $y < $height; $y += $tile_height)
4144                 imagecopy($tmp, $im, $x, $y, 0, 0, $tile_width, $tile_height);
4145
4146         // Copy the temporary image onto the final one.
4147         imagecopy($this->img, $tmp, $xorig, $yorig, 0,0, $width, $height);
4148
4149         // Free resources
4150         imagedestroy($tmp);
4151         imagedestroy($im);
4152
4153         return TRUE;
4154     }
4155
4156     /*
4157      * Return the image border width.
4158      * This is used by CalcMargins() and DrawImageBorder().
4159      */
4160     protected function GetImageBorderWidth()
4161     {
4162         if ($this->image_border_type == 'none')
4163             return 0; // No border
4164         if (!empty($this->image_border_width))
4165             return $this->image_border_width; // Specified border width
4166         if ($this->image_border_type == 'raised')
4167             return 2; // Default for raised border is 2 pixels.
4168         return 1; // Default for other border types is 1 pixel.
4169     }
4170
4171     /*
4172      * Draws a border around the final image.
4173      * Note: 'plain' draws a flat border using the dark shade of the border color.
4174      * This probably should have been written to use the actual border color, but
4175      * it is too late to fix it without changing plot appearances. Therefore a
4176      * new type 'solid' was added to use the SetImageBorderColor color.
4177      */
4178     protected function DrawImageBorder()
4179     {
4180         // Do nothing if already drawn, or if no border has been set.
4181         if ($this->image_border_type == 'none' || !empty($this->done['border']))
4182             return TRUE;
4183         $width = $this->GetImageBorderWidth();
4184         $color1 = $this->ndx_i_border;
4185         $color2 = $this->ndx_i_border_dark;
4186         $ex = $this->image_width - 1;
4187         $ey = $this->image_height - 1;
4188         switch ($this->image_border_type) {
4189         case 'raised':
4190             // Top and left lines use border color, right and bottom use the darker shade.
4191             // Drawing order matters in the upper right and lower left corners.
4192             for ($i = 0; $i < $width; $i++, $ex--, $ey--) {
4193                 imageline($this->img, $i,  $i,  $ex, $i,  $color1); // Top
4194                 imageline($this->img, $ex, $i,  $ex, $ey, $color2); // Right
4195                 imageline($this->img, $i,  $i,  $i,  $ey, $color1); // Left
4196                 imageline($this->img, $i,  $ey, $ex, $ey, $color2); // Bottom
4197             }
4198             break;
4199         case 'plain': // See note above re colors
4200             $color1 = $color2;
4201             // Fall through
4202         case 'solid':
4203             for ($i = 0; $i < $width; $i++, $ex--, $ey--) {
4204                 imagerectangle($this->img, $i, $i, $ex, $ey, $color1);
4205             }
4206             break;
4207         default:
4208             return $this->PrintError(
4209                           "DrawImageBorder(): unknown image_border_type: '$this->image_border_type'");
4210         }
4211         $this->done['border'] = TRUE; // Border should only be drawn once per image.
4212         return TRUE;
4213     }
4214
4215     /*
4216      * Draws the main title on the graph.
4217      * The title must not be drawn more than once (in the case of multiple plots
4218      * on the image), because TTF text antialiasing makes it look bad.
4219      */
4220     protected function DrawTitle()
4221     {
4222         if (!empty($this->done['title']) || $this->title_txt === '')
4223             return TRUE;
4224
4225         // Center of the image:
4226         $xpos = $this->image_width / 2;
4227
4228         // Place it at almost at the top
4229         $ypos = $this->title_offset;
4230
4231         $this->DrawText($this->fonts['title'], 0, $xpos, $ypos,
4232                         $this->ndx_title_color, $this->title_txt, 'center', 'top');
4233
4234         $this->done['title'] = TRUE;
4235         return TRUE;
4236     }
4237
4238     /*
4239      * Draws the X-Axis Title
4240      */
4241     protected function DrawXTitle()
4242     {
4243         if ($this->x_title_pos == 'none')
4244             return TRUE;
4245
4246         // Center of the plot
4247         $xpos = ($this->plot_area[2] + $this->plot_area[0]) / 2;
4248
4249         // Upper title
4250         if ($this->x_title_pos == 'plotup' || $this->x_title_pos == 'both') {
4251             $ypos = $this->plot_area[1] - $this->x_title_top_offset;
4252             $this->DrawText($this->fonts['x_title'], 0, $xpos, $ypos, $this->ndx_x_title_color,
4253                             $this->x_title_txt, 'center', 'bottom');
4254         }
4255         // Lower title
4256         if ($this->x_title_pos == 'plotdown' || $this->x_title_pos == 'both') {
4257             $ypos = $this->plot_area[3] + $this->x_title_bot_offset;
4258             $this->DrawText($this->fonts['x_title'], 0, $xpos, $ypos, $this->ndx_x_title_color,
4259                             $this->x_title_txt, 'center', 'top');
4260         }
4261         return TRUE;
4262     }
4263
4264     /*
4265      * Draws the Y-Axis Title
4266      */
4267     protected function DrawYTitle()
4268     {
4269         if ($this->y_title_pos == 'none')
4270             return TRUE;
4271
4272         // Center the title vertically to the plot area
4273         $ypos = ($this->plot_area[3] + $this->plot_area[1]) / 2;
4274
4275         if ($this->y_title_pos == 'plotleft' || $this->y_title_pos == 'both') {
4276             $xpos = $this->plot_area[0] - $this->y_title_left_offset;
4277             $this->DrawText($this->fonts['y_title'], 90, $xpos, $ypos, $this->ndx_y_title_color,
4278                             $this->y_title_txt, 'right', 'center');
4279         }
4280         if ($this->y_title_pos == 'plotright' || $this->y_title_pos == 'both') {
4281             $xpos = $this->plot_area[2] + $this->y_title_right_offset;
4282             $this->DrawText($this->fonts['y_title'], 90, $xpos, $ypos, $this->ndx_y_title_color,
4283                             $this->y_title_txt, 'left', 'center');
4284         }
4285
4286         return TRUE;
4287     }
4288
4289     /*
4290      * Draw the X axis, including ticks and labels, and X (vertical) grid lines.
4291      */
4292     protected function DrawXAxis()
4293     {
4294         // Draw ticks, labels and grid
4295         $this->DrawXTicks();
4296
4297         //Draw X Axis at Y = x_axis_y_pixels, unless suppressed (See SetXAxisPosition)
4298         if (empty($this->suppress_x_axis)) {
4299             ImageLine($this->img, $this->plot_area[0]+1, $this->x_axis_y_pixels,
4300                       $this->plot_area[2]-1, $this->x_axis_y_pixels, $this->ndx_grid_color);
4301         }
4302         return TRUE;
4303     }
4304
4305     /*
4306      * Draw the Y axis, including ticks and labels, and Y (horizontal) grid lines.
4307      * Horizontal grid lines overwrite horizontal axis with y=0, so call this first, then DrawXAxis()
4308      */
4309     protected function DrawYAxis()
4310     {
4311         // Draw ticks, labels and grid
4312         $this->DrawYTicks();
4313
4314         // Draw Y axis at X = y_axis_x_pixels, unless suppressed (See SetYAxisPosition)
4315         if (empty($this->suppress_y_axis)) {
4316             ImageLine($this->img, $this->y_axis_x_pixels, $this->plot_area[1],
4317                       $this->y_axis_x_pixels, $this->plot_area[3], $this->ndx_grid_color);
4318         }
4319         return TRUE;
4320     }
4321
4322     /*
4323      * Draw one X tick mark and its tick label.
4324      *   $which_xlab : Formatted X value for the label.
4325      *   $which_xpix : X device coordinate for this tick mark.
4326      */
4327     protected function DrawXTick($which_xlab, $which_xpix)
4328     {
4329         // Ticks on X axis
4330         if ($this->x_tick_pos == 'xaxis') {
4331             ImageLine($this->img, $which_xpix, $this->x_axis_y_pixels - $this->x_tick_cross,
4332                       $which_xpix, $this->x_axis_y_pixels + $this->x_tick_length, $this->ndx_tick_color);
4333         }
4334
4335         // Ticks on top of the Plot Area
4336         if ($this->x_tick_pos == 'plotup' || $this->x_tick_pos == 'both') {
4337             ImageLine($this->img, $which_xpix, $this->plot_area[1] - $this->x_tick_length,
4338                       $which_xpix, $this->plot_area[1] + $this->x_tick_cross, $this->ndx_tick_color);
4339         }
4340
4341         // Ticks on bottom of Plot Area
4342         if ($this->x_tick_pos == 'plotdown' || $this->x_tick_pos == 'both') {
4343             ImageLine($this->img, $which_xpix, $this->plot_area[3] + $this->x_tick_length,
4344                       $which_xpix, $this->plot_area[3] - $this->x_tick_cross, $this->ndx_tick_color);
4345         }
4346
4347         // Label on X axis
4348         if ($this->x_tick_label_pos == 'xaxis') {
4349             $this->DrawText($this->fonts['x_label'], $this->x_label_angle,
4350                             $which_xpix, $this->x_axis_y_pixels + $this->x_label_axis_offset,
4351                             $this->ndx_text_color, $which_xlab, 'center', 'top');
4352         }
4353
4354         // Label on top of the Plot Area
4355         if ($this->x_tick_label_pos == 'plotup' || $this->x_tick_label_pos == 'both') {
4356             $this->DrawText($this->fonts['x_label'], $this->x_label_angle,
4357                             $which_xpix, $this->plot_area[1] - $this->x_label_top_offset,
4358                             $this->ndx_text_color, $which_xlab, 'center', 'bottom');
4359         }
4360
4361         // Label on bottom of the Plot Area
4362         if ($this->x_tick_label_pos == 'plotdown' || $this->x_tick_label_pos == 'both') {
4363             $this->DrawText($this->fonts['x_label'], $this->x_label_angle,
4364                             $which_xpix, $this->plot_area[3] + $this->x_label_bot_offset,
4365                             $this->ndx_text_color, $which_xlab, 'center', 'top');
4366         }
4367         return TRUE;
4368     }
4369
4370     /*
4371      * Draw one Y tick mark and its tick label. Called from DrawYTicks() and DrawXAxis()
4372      *   $which_ylab : Formatted Y value for the label.
4373      *   $which_ypix : Y device coordinate for this tick mark.
4374      */
4375     protected function DrawYTick($which_ylab, $which_ypix)
4376     {
4377         // Ticks on Y axis
4378         if ($this->y_tick_pos == 'yaxis') {
4379             ImageLine($this->img, $this->y_axis_x_pixels - $this->y_tick_length, $which_ypix,
4380                       $this->y_axis_x_pixels + $this->y_tick_cross, $which_ypix, $this->ndx_tick_color);
4381         }
4382
4383         // Ticks to the left of the Plot Area
4384         if (($this->y_tick_pos == 'plotleft') || ($this->y_tick_pos == 'both') ) {
4385             ImageLine($this->img, $this->plot_area[0] - $this->y_tick_length, $which_ypix,
4386                       $this->plot_area[0] + $this->y_tick_cross, $which_ypix, $this->ndx_tick_color);
4387         }
4388
4389         // Ticks to the right of the Plot Area
4390         if (($this->y_tick_pos == 'plotright') || ($this->y_tick_pos == 'both') ) {
4391             ImageLine($this->img, $this->plot_area[2] + $this->y_tick_length, $which_ypix,
4392                       $this->plot_area[2] - $this->y_tick_cross, $which_ypix, $this->ndx_tick_color);
4393         }
4394
4395         // Labels on Y axis
4396         if ($this->y_tick_label_pos == 'yaxis') {
4397             $this->DrawText($this->fonts['y_label'], $this->y_label_angle,
4398                             $this->y_axis_x_pixels - $this->y_label_axis_offset, $which_ypix,
4399                             $this->ndx_text_color, $which_ylab, 'right', 'center');
4400         }
4401
4402         // Labels to the left of the plot area
4403         if ($this->y_tick_label_pos == 'plotleft' || $this->y_tick_label_pos == 'both') {
4404             $this->DrawText($this->fonts['y_label'], $this->y_label_angle,
4405                             $this->plot_area[0] - $this->y_label_left_offset, $which_ypix,
4406                             $this->ndx_text_color, $which_ylab, 'right', 'center');
4407         }
4408         // Labels to the right of the plot area
4409         if ($this->y_tick_label_pos == 'plotright' || $this->y_tick_label_pos == 'both') {
4410             $this->DrawText($this->fonts['y_label'], $this->y_label_angle,
4411                             $this->plot_area[2] + $this->y_label_right_offset, $which_ypix,
4412                             $this->ndx_text_color, $which_ylab, 'left', 'center');
4413         }
4414         return TRUE;
4415     }
4416
4417     /*
4418      * Draws Grid, Ticks and Tick Labels along X-Axis
4419      * Ticks and tick labels can be down of plot only, up of plot only,
4420      * both on up and down of plot, or crossing a user defined X-axis
4421      *
4422      * Original vertical code submitted by Marlin Viss
4423      */
4424     protected function DrawXTicks()
4425     {
4426         // Sets the line style for IMG_COLOR_STYLED lines (grid)
4427         if ($this->dashed_grid) {
4428             $this->SetDashedStyle($this->ndx_light_grid_color);
4429             $style = IMG_COLOR_STYLED;
4430         } else {
4431             $style = $this->ndx_light_grid_color;
4432         }
4433
4434         // Calculate the tick start, end, and step:
4435         list($x_start, $x_end, $delta_x) = $this->CalcTicks('x');
4436
4437         // Loop, avoiding cumulative round-off errors from $x_tmp += $delta_x
4438         $n = 0;
4439         $x_tmp = $x_start;
4440         while ($x_tmp <= $x_end) {
4441             $xlab = $this->FormatLabel('x', $x_tmp);
4442             $x_pixels = $this->xtr($x_tmp);
4443
4444             // Vertical grid lines
4445             if ($this->draw_x_grid) {
4446                 ImageLine($this->img, $x_pixels, $this->plot_area[1], $x_pixels, $this->plot_area[3], $style);
4447             }
4448
4449             // Draw tick mark(s)
4450             $this->DrawXTick($xlab, $x_pixels);
4451
4452             // Step to next X, without accumulating error
4453             $x_tmp = $x_start + ++$n * $delta_x;
4454         }
4455         return TRUE;
4456     }
4457
4458     /*
4459      * Draw the grid, ticks, and tick labels along the Y axis.
4460      * Ticks and tick labels can be left of plot only, right of plot only,
4461      * both on the left and right of plot, or crossing a user defined Y-axis
4462      */
4463     protected function DrawYTicks()
4464     {
4465         // Sets the line style for IMG_COLOR_STYLED lines (grid)
4466         if ($this->dashed_grid) {
4467             $this->SetDashedStyle($this->ndx_light_grid_color);
4468             $style = IMG_COLOR_STYLED;
4469         } else {
4470             $style = $this->ndx_light_grid_color;
4471         }
4472
4473         // Calculate the tick start, end, and step:
4474         list($y_start, $y_end, $delta_y) = $this->CalcTicks('y');
4475
4476         // Loop, avoiding cumulative round-off errors from $y_tmp += $delta_y
4477         $n = 0;
4478         $y_tmp = $y_start;
4479         while ($y_tmp <= $y_end) {
4480             $ylab = $this->FormatLabel('y', $y_tmp);
4481             $y_pixels = $this->ytr($y_tmp);
4482
4483             // Horizontal grid line
4484             if ($this->draw_y_grid) {
4485                 ImageLine($this->img, $this->plot_area[0]+1, $y_pixels, $this->plot_area[2]-1,
4486                           $y_pixels, $style);
4487             }
4488
4489             // Draw tick mark(s)
4490             $this->DrawYTick($ylab, $y_pixels);
4491
4492             // Step to next Y, without accumulating error
4493             $y_tmp = $y_start + ++$n * $delta_y;
4494         }
4495         return TRUE;
4496     }
4497
4498     /*
4499      *  Draw a border around the plot area. See SetPlotBorderType.
4500      *  Note: SetPlotBorderType sets plot_border_type to an array, but
4501      *  it won't be an array if it defaults or is set directly (backward compatibility).
4502      */
4503     protected function DrawPlotBorder()
4504     {
4505         $pbt = (array)$this->plot_border_type;
4506         $sides = 0;  // Bitmap: 1=left 2=top 4=right 8=bottom
4507         $map = array('left' => 1, 'plotleft' => 1, 'right' => 4, 'plotright' => 4, 'top' => 2,
4508                       'bottom' => 8, 'both' => 5, 'sides' => 5, 'full' => 15, 'none' => 0);
4509         foreach ($pbt as $option) $sides |= $map[$option];
4510         if ($sides == 15) { // Border on all 4 sides
4511             imagerectangle($this->img, $this->plot_area[0], $this->plot_area[1],
4512                            $this->plot_area[2], $this->plot_area[3], $this->ndx_grid_color);
4513         } else {
4514             if ($sides & 1) // Left
4515                 imageline($this->img, $this->plot_area[0], $this->plot_area[1],
4516                                       $this->plot_area[0], $this->plot_area[3], $this->ndx_grid_color);
4517             if ($sides & 2) // Top
4518                 imageline($this->img, $this->plot_area[0], $this->plot_area[1],
4519                                       $this->plot_area[2], $this->plot_area[1], $this->ndx_grid_color);
4520             if ($sides & 4) // Right
4521                 imageline($this->img, $this->plot_area[2], $this->plot_area[1],
4522                                       $this->plot_area[2], $this->plot_area[3], $this->ndx_grid_color);
4523             if ($sides & 8) // Bottom
4524                 imageline($this->img, $this->plot_area[0], $this->plot_area[3],
4525                                       $this->plot_area[2], $this->plot_area[3], $this->ndx_grid_color);
4526         }
4527         return TRUE;
4528     }
4529
4530     /*
4531      * Draw the data value label associated with a point in the plot.
4532      * These are labels that show the value (dependent variable, usually Y) of the data point,
4533      * and are drawn within the plot area (not to be confused with axis data labels).
4534      *
4535      *    $x_or_y : Specify 'x' or 'y' labels. This selects font, angle, and formatting.
4536      *    $x_world, $y_world : World coordinates of the text (see also x/y_adjustment).
4537      *    $text : The text to draw, after formatting with FormatLabel().
4538      *    $halign, $valign : Selects from 9-point text alignment.
4539      *    $x_adjustment, $y_adjustment : Text position offsets, in device coordinates.
4540      *    $min_width, $min_height : If supplied, suppress the text if it will not fit.
4541      * Returns True, if the text was drawn, or False, if it will not fit.
4542      */
4543     protected function DrawDataValueLabel($x_or_y, $x_world, $y_world, $text, $halign, $valign,
4544                       $x_adjustment=0, $y_adjustment=0, $min_width=NULL, $min_height=NULL)
4545     {
4546         if ($x_or_y == 'x') {
4547             $angle = $this->x_data_label_angle;
4548             $font = $this->fonts['x_label'];
4549             $formatted_text = $this->FormatLabel('xd', $text);
4550         } else { // Assumed 'y'
4551             $angle = $this->y_data_label_angle;
4552             $font = $this->fonts['y_label'];
4553             $formatted_text = $this->FormatLabel('yd', $text);
4554         }
4555         $color = $this->ndx_title_color; // Currently this is the same for X and Y labels
4556
4557         // Check to see if the text fits in the available space, if requested.
4558         if (isset($min_width) || isset($min_height)) {
4559             list($width, $height) = $this->SizeText($font, $angle, $formatted_text);
4560             if ((isset($min_width) && ($min_width - $width)  < 2)
4561                 || (isset($min_height) && ($min_height - $height) < 2))
4562                 return FALSE;
4563         }
4564
4565         $this->DrawText($font, $angle, $this->xtr($x_world) + $x_adjustment,
4566                         $this->ytr($y_world) + $y_adjustment,
4567                         $color, $formatted_text, $halign, $valign);
4568         return TRUE;
4569     }
4570
4571     /*
4572      * Draws the axis data label associated with a point in the plot.
4573      * This is different from x_labels drawn by DrawXTicks() and care
4574      * should be taken not to draw both, as they'd probably overlap.
4575      * Calling of this function in DrawLines(), etc is decided after x_data_label_pos value.
4576      * Leave the last parameter out, to avoid the drawing of vertical lines, no matter
4577      * what the setting is (for plots that need it, like DrawSquared())
4578      */
4579     protected function DrawXDataLabel($xlab, $xpos, $row=FALSE)
4580     {
4581         $xlab = $this->FormatLabel('xd', $xlab);
4582
4583         // Labels below the plot area
4584         if ($this->x_data_label_pos == 'plotdown' || $this->x_data_label_pos == 'both')
4585             $this->DrawText($this->fonts['x_label'], $this->x_data_label_angle,
4586                             $xpos, $this->plot_area[3] + $this->x_label_bot_offset,
4587                             $this->ndx_text_color, $xlab, 'center', 'top');
4588
4589         // Labels above the plot area
4590         if ($this->x_data_label_pos == 'plotup' || $this->x_data_label_pos == 'both')
4591             $this->DrawText($this->fonts['x_label'], $this->x_data_label_angle,
4592                             $xpos, $this->plot_area[1] - $this->x_label_top_offset,
4593                             $this->ndx_text_color, $xlab, 'center', 'bottom');
4594
4595         // $row=0 means this is the first row. $row=FALSE means don't do any rows.
4596         if ($row !== FALSE && $this->draw_x_data_label_lines)
4597             $this->DrawXDataLine($xpos, $row);
4598         return TRUE;
4599     }
4600
4601     /*
4602      * Draw a data label along the Y axis or side.
4603      * This is used by horizontal plots.
4604      */
4605     protected function DrawYDataLabel($ylab, $ypos)
4606     {
4607         $ylab = $this->FormatLabel('yd', $ylab);
4608
4609         // Labels left of the plot area
4610         if ($this->y_data_label_pos == 'plotleft' || $this->y_data_label_pos == 'both')
4611             $this->DrawText($this->fonts['y_label'], $this->y_data_label_angle,
4612                             $this->plot_area[0] - $this->y_label_left_offset, $ypos,
4613                             $this->ndx_text_color, $ylab, 'right', 'center');
4614
4615         // Labels right of the plot area
4616         if ($this->y_data_label_pos == 'plotright' || $this->y_data_label_pos == 'both')
4617             $this->DrawText($this->fonts['y_label'], $this->y_data_label_angle,
4618                             $this->plot_area[2] + $this->y_label_right_offset, $ypos,
4619                             $this->ndx_text_color, $ylab, 'left', 'center');
4620         return TRUE;
4621     }
4622
4623     /*
4624      * Draws Vertical lines from data points up and down.
4625      * Which lines are drawn depends on the value of x_data_label_pos,
4626      * and whether this is at all done or not, on draw_x_data_label_lines
4627      *
4628      *   $xpos : position in pixels of the line.
4629      *   $row : index of the data row being drawn.
4630      */
4631     protected function DrawXDataLine($xpos, $row)
4632     {
4633         // Sets the line style for IMG_COLOR_STYLED lines (grid)
4634         if ($this->dashed_grid) {
4635             $this->SetDashedStyle($this->ndx_light_grid_color);
4636             $style = IMG_COLOR_STYLED;
4637         } else {
4638             $style = $this->ndx_light_grid_color;
4639         }
4640
4641         if ($this->x_data_label_pos == 'both') {
4642             // Lines from the bottom up
4643             ImageLine($this->img, $xpos, $this->plot_area[3], $xpos, $this->plot_area[1], $style);
4644         } elseif ($this->x_data_label_pos == 'plotdown' && isset($this->data_max[$row])) {
4645             // Lines from the bottom of the plot up to the max Y value at this X:
4646             $ypos = $this->ytr($this->data_max[$row]);
4647             ImageLine($this->img, $xpos, $ypos, $xpos, $this->plot_area[3], $style);
4648         } elseif ($this->x_data_label_pos == 'plotup' && isset($this->data_min[$row])) {
4649             // Lines from the top of the plot down to the min Y value at this X:
4650             $ypos = $this->ytr($this->data_min[$row]);
4651             ImageLine($this->img, $xpos, $this->plot_area[1], $xpos, $ypos, $style);
4652         }
4653         return TRUE;
4654     }
4655
4656     /*
4657      * Draws the graph legend
4658      * This is called by DrawGraph only if $this->legend is not empty.
4659      * Base code submitted by Marlin Viss
4660      */
4661     protected function DrawLegend()
4662     {
4663         $font = &$this->fonts['legend'];
4664
4665         // Find maximum legend label line width.
4666         $max_width = 0;
4667         foreach ($this->legend as $line) {
4668             list($width, $unused) = $this->SizeText($font, 0, $line);
4669             if ($width > $max_width) $max_width = $width;
4670         }
4671
4672         // Use the font parameters to size the color boxes:
4673         $char_w = $font['width'];
4674         $char_h = $font['height'];
4675         $line_spacing = $this->GetLineSpacing($font);
4676
4677         // Normalize text alignment and colorbox alignment variables:
4678         $text_align = isset($this->legend_text_align) ? $this->legend_text_align : 'right';
4679         $colorbox_align = isset($this->legend_colorbox_align) ? $this->legend_colorbox_align : 'right';
4680
4681         // Sizing parameters:
4682         $v_margin = $char_h/2;                   // Between vertical borders and labels
4683         $dot_height = $char_h + $line_spacing;   // Height of the small colored boxes
4684         // Color boxes are $char_w wide, but can be adjusted using legend_colorbox_width:
4685         $colorbox_width = $char_w;
4686         if (isset($this->legend_colorbox_width))
4687             $colorbox_width *= $this->legend_colorbox_width;
4688
4689         // Overall legend box width e.g.: | space colorbox space text space |
4690         // where each space adds $char_w, and colorbox adds $char_w * its width adjustment.
4691         if (($draw_colorbox = ($colorbox_align != 'none'))) {
4692             $width = $max_width + 3 * $char_w + $colorbox_width;
4693         } else {
4694             $width = $max_width + 2 * $char_w;
4695         }
4696
4697         //////// Calculate box position
4698         // User-defined position specified?
4699         if ( !isset($this->legend_x_pos) || !isset($this->legend_y_pos)) {
4700             // No, use default
4701             $box_start_x = $this->plot_area[2] - $width - $this->safe_margin;
4702             $box_start_y = $this->plot_area[1] + $this->safe_margin;
4703         } elseif (isset($this->legend_xy_world)) {
4704             // User-defined position in world-coordinates (See SetLegendWorld).
4705             $box_start_x = $this->xtr($this->legend_x_pos);
4706             $box_start_y = $this->ytr($this->legend_y_pos);
4707         } else {
4708             // User-defined position in pixel coordinates.
4709             $box_start_x = $this->legend_x_pos;
4710             $box_start_y = $this->legend_y_pos;
4711         }
4712
4713         // Lower right corner
4714         $box_end_y = $box_start_y + $dot_height*(count($this->legend)) + 2*$v_margin;
4715         $box_end_x = $box_start_x + $width;
4716
4717         // Draw outer box
4718         ImageFilledRectangle($this->img, $box_start_x, $box_start_y, $box_end_x, $box_end_y,
4719                              $this->ndx_bg_color);
4720         ImageRectangle($this->img, $box_start_x, $box_start_y, $box_end_x, $box_end_y,
4721                        $this->ndx_grid_color);
4722
4723         $color_index = 0;
4724         $max_color_index = count($this->ndx_data_colors) - 1;
4725
4726         // Calculate color box and text horizontal positions.
4727         if (!$draw_colorbox) {
4728             if ($text_align == 'left')
4729                 $x_pos = $box_start_x + $char_w;
4730             else
4731                 $x_pos = $box_end_x - $char_w;
4732         } elseif ($colorbox_align == 'left') {
4733             $dot_left_x = $box_start_x + $char_w;
4734             $dot_right_x = $dot_left_x + $colorbox_width;
4735             if ($text_align == 'left')
4736                 $x_pos = $dot_right_x + $char_w;
4737             else
4738                 $x_pos = $box_end_x - $char_w;
4739         } else {      // $colorbox_align == 'right'
4740             $dot_right_x = $box_end_x - $char_w;
4741             $dot_left_x = $dot_right_x - $colorbox_width;
4742             if ($text_align == 'left')
4743                 $x_pos = $box_start_x + $char_w;
4744             else
4745                 $x_pos = $dot_left_x - $char_w;
4746         }
4747
4748         // Calculate starting position of first text line.  The bottom of each color box
4749         // lines up with the bottom (baseline) of its text line.
4750         $y_pos = $box_start_y + $v_margin + $dot_height;
4751
4752         foreach ($this->legend as $leg) {
4753             // Draw text with requested alignment:
4754             $this->DrawText($font, 0, $x_pos, $y_pos, $this->ndx_text_color, $leg, $text_align, 'bottom');
4755             if ($draw_colorbox) {
4756                 // Draw a box in the data color
4757                 $y1 = $y_pos - $dot_height + 1;
4758                 $y2 = $y_pos - 1;
4759                 ImageFilledRectangle($this->img, $dot_left_x, $y1, $dot_right_x, $y2,
4760                                      $this->ndx_data_colors[$color_index]);
4761                 // Draw a rectangle around the box
4762                 ImageRectangle($this->img, $dot_left_x, $y1, $dot_right_x, $y2,
4763                                $this->ndx_text_color);
4764             }
4765             $y_pos += $dot_height;
4766
4767             $color_index++;
4768             if ($color_index > $max_color_index)
4769                 $color_index = 0;
4770         }
4771         return TRUE;
4772     }
4773
4774 /////////////////////////////////////////////
4775 ////////////////////     PLOT DRAWING HELPERS
4776 /////////////////////////////////////////////
4777
4778     /*
4779      * Get data color to use for plotting.
4780      *   $row, $idx : Index arguments for the current data point.
4781      *   &$vars : Variable storage. Caller makes an empty array, and this function uses it.
4782      *   &$data_color : Returned result - Color index for the data point.
4783      *   $extra : Extra info flag passed through to data color callback.
4784      */
4785     protected function GetDataColor($row, $idx, &$vars, &$data_color, $extra = 0)
4786     {
4787         // Initialize or extract variables:
4788         if (empty($vars)) {
4789             $custom_color = (bool)$this->GetCallback('data_color');
4790             $num_data_colors = count($this->ndx_data_colors);
4791             $vars = compact('custom_color', 'num_data_colors');
4792         } else {
4793             extract($vars);
4794         }
4795
4796         // Select the colors.
4797         if ($custom_color) {
4798             $col_i = $this->DoCallback('data_color', $row, $idx, $extra); // Custom color index
4799             $data_color = $this->ndx_data_colors[$col_i % $num_data_colors];
4800         } else {
4801             $data_color = $this->ndx_data_colors[$idx];
4802         }
4803     }
4804
4805     /*
4806      * Get data color and error bar color to use for plotting.
4807      *   $row, $idx : Index arguments for the current bar.
4808      *   &$vars : Variable storage. Caller makes an empty array, and this function uses it.
4809      *   &$data_color : Returned result - Color index for the data (bar fill)
4810      *   &$error_color : Returned result - Color index for the error bars
4811      *   $extra : Extra info flag passed through to data color callback.
4812      */
4813     protected function GetDataErrorColors($row, $idx, &$vars, &$data_color, &$error_color, $extra = 0)
4814     {
4815         // Initialize or extract variables:
4816         if (empty($vars)) {
4817             $this->NeedErrorBarColors();   // This plot needs error bar colors.
4818             $custom_color = (bool)$this->GetCallback('data_color');
4819             $num_data_colors = count($this->ndx_data_colors);
4820             $num_error_colors = count($this->ndx_error_bar_colors);
4821             $vars = compact('custom_color', 'num_data_colors', 'num_error_colors');
4822         } else {
4823             extract($vars);
4824         }
4825
4826         // Select the colors.
4827         if ($custom_color) {
4828             $col_i = $this->DoCallback('data_color', $row, $idx, $extra); // Custom color index
4829             $data_color = $this->ndx_data_colors[$col_i % $num_data_colors];
4830             $error_color = $this->ndx_error_bar_colors[$col_i % $num_error_colors];
4831         } else {
4832             $data_color = $this->ndx_data_colors[$idx];
4833             $error_color = $this->ndx_error_bar_colors[$idx];
4834         }
4835     }
4836
4837     /*
4838      * Get colors to use for a bar chart. There is a data color, and either a border color
4839      * or a shading color (data dark color).
4840      *   $row, $idx : Index arguments for the current bar.
4841      *   &$vars : Variable storage. Caller makes an empty array, and this function uses it.
4842      *   &$data_color : Returned result - Color index for the data (bar fill).
4843      *   &$alt_color : Returned result - Color index for the shading or outline.
4844      */
4845     protected function GetBarColors($row, $idx, &$vars, &$data_color, &$alt_color)
4846     {
4847         // Initialize or extract variables:
4848         if (empty($vars)) {
4849             if ($this->shading > 0)    // This plot needs dark colors if shading is on.
4850                 $this->NeedDataDarkColors();
4851             $custom_color = (bool)$this->GetCallback('data_color');
4852             $num_data_colors = count($this->ndx_data_colors);
4853             $num_border_colors = count($this->ndx_data_border_colors);
4854             $vars = compact('custom_color', 'num_data_colors', 'num_border_colors');
4855         } else {
4856             extract($vars);
4857         }
4858
4859         // Select the colors.
4860         if ($custom_color) {
4861             $col_i = $this->DoCallback('data_color', $row, $idx); // Custom color index
4862             $i_data = $col_i % $num_data_colors; // Index for data colors and dark colors
4863             $i_border = $col_i % $num_border_colors; // Index for data borders (if used)
4864         } else {
4865             $i_data = $i_border = $idx;
4866         }
4867         $data_color = $this->ndx_data_colors[$i_data];
4868         if ($this->shading > 0) {
4869             $alt_color = $this->ndx_data_dark_colors[$i_data];
4870         } else {
4871             $alt_color = $this->ndx_data_border_colors[$i_border];
4872         }
4873     }
4874
4875     /*
4876      * Draws a styled dot. Uses world coordinates.
4877      * The list of supported shapes can also be found in SetPointShapes().
4878      * All shapes are drawn using a 3x3 grid, centered on the data point.
4879      * The center is (x_mid, y_mid) and the corners are (x1, y1) and (x2, y2).
4880      *   $record is the 0-based index that selects the shape and size.
4881      */
4882     protected function DrawDot($x_world, $y_world, $record, $color)
4883     {
4884         $index = $record % $this->point_counts;
4885         $point_size = $this->point_sizes[$index];
4886
4887         $half_point = (int)($point_size / 2);
4888
4889         $x_mid = $this->xtr($x_world);
4890         $y_mid = $this->ytr($y_world);
4891
4892         $x1 = $x_mid - $half_point;
4893         $x2 = $x_mid + $half_point;
4894         $y1 = $y_mid - $half_point;
4895         $y2 = $y_mid + $half_point;
4896
4897         switch ($this->point_shapes[$index]) {
4898         case 'halfline':
4899             ImageLine($this->img, $x1, $y_mid, $x_mid, $y_mid, $color);
4900             break;
4901         case 'line':
4902             ImageLine($this->img, $x1, $y_mid, $x2, $y_mid, $color);
4903             break;
4904         case 'plus':
4905             ImageLine($this->img, $x1, $y_mid, $x2, $y_mid, $color);
4906             ImageLine($this->img, $x_mid, $y1, $x_mid, $y2, $color);
4907             break;
4908         case 'cross':
4909             ImageLine($this->img, $x1, $y1, $x2, $y2, $color);
4910             ImageLine($this->img, $x1, $y2, $x2, $y1, $color);
4911             break;
4912         case 'circle':
4913             ImageArc($this->img, $x_mid, $y_mid, $point_size, $point_size, 0, 360, $color);
4914             break;
4915         case 'dot':
4916             ImageFilledEllipse($this->img, $x_mid, $y_mid, $point_size, $point_size, $color);
4917             break;
4918         case 'diamond':
4919             $arrpoints = array( $x1, $y_mid, $x_mid, $y1, $x2, $y_mid, $x_mid, $y2);
4920             ImageFilledPolygon($this->img, $arrpoints, 4, $color);
4921             break;
4922         case 'triangle':
4923             $arrpoints = array( $x1, $y_mid, $x2, $y_mid, $x_mid, $y2);
4924             ImageFilledPolygon($this->img, $arrpoints, 3, $color);
4925             break;
4926         case 'trianglemid':
4927             $arrpoints = array( $x1, $y1, $x2, $y1, $x_mid, $y_mid);
4928             ImageFilledPolygon($this->img, $arrpoints, 3, $color);
4929             break;
4930         case 'yield':
4931             $arrpoints = array( $x1, $y1, $x2, $y1, $x_mid, $y2);
4932             ImageFilledPolygon($this->img, $arrpoints, 3, $color);
4933             break;
4934         case 'delta':
4935             $arrpoints = array( $x1, $y2, $x2, $y2, $x_mid, $y1);
4936             ImageFilledPolygon($this->img, $arrpoints, 3, $color);
4937             break;
4938         case 'star':
4939             ImageLine($this->img, $x1, $y_mid, $x2, $y_mid, $color);
4940             ImageLine($this->img, $x_mid, $y1, $x_mid, $y2, $color);
4941             ImageLine($this->img, $x1, $y1, $x2, $y2, $color);
4942             ImageLine($this->img, $x1, $y2, $x2, $y1, $color);
4943             break;
4944         case 'hourglass':
4945             $arrpoints = array( $x1, $y1, $x2, $y1, $x1, $y2, $x2, $y2);
4946             ImageFilledPolygon($this->img, $arrpoints, 4, $color);
4947             break;
4948         case 'bowtie':
4949             $arrpoints = array( $x1, $y1, $x1, $y2, $x2, $y1, $x2, $y2);
4950             ImageFilledPolygon($this->img, $arrpoints, 4, $color);
4951             break;
4952         case 'target':
4953             ImageFilledRectangle($this->img, $x1, $y1, $x_mid, $y_mid, $color);
4954             ImageFilledRectangle($this->img, $x_mid, $y_mid, $x2, $y2, $color);
4955             ImageRectangle($this->img, $x1, $y1, $x2, $y2, $color);
4956             break;
4957         case 'box':
4958             ImageRectangle($this->img, $x1, $y1, $x2, $y2, $color);
4959             break;
4960         case 'home': /* As in: "home plate" (baseball), also looks sort of like a house. */
4961             $arrpoints = array( $x1, $y2, $x2, $y2, $x2, $y_mid, $x_mid, $y1, $x1, $y_mid);
4962             ImageFilledPolygon($this->img, $arrpoints, 5, $color);
4963             break;
4964         case 'up':
4965             ImagePolygon($this->img, array($x_mid, $y1, $x2, $y2, $x1, $y2), 3, $color);
4966             break;
4967         case 'down':
4968             ImagePolygon($this->img, array($x_mid, $y2, $x1, $y1, $x2, $y1), 3, $color);
4969             break;
4970         case 'none': /* Special case, no point shape here */
4971             break;
4972         default: /* Also 'rect' */
4973             ImageFilledRectangle($this->img, $x1, $y1, $x2, $y2, $color);
4974             break;
4975         }
4976         return TRUE;
4977     }
4978
4979     /*
4980      * Draw a bar (or segment of a bar), with optional shading or border.
4981      * This is used by the bar and stackedbar plots, vertical and horizontal.
4982      *   $x1, $y1 : One corner of the bar.
4983      *   $x2, $y2 : Other corner of the bar.
4984      *   $data_color : Color index to use for the bar fill.
4985      *   $alt_color : Color index to use for the shading (if shading is on), else for the border.
4986      *      Note the same color is NOT used for shading and border - just the same argument.
4987      *      See GetBarColors() for where these arguments come from.
4988      *   $shade_top : Shade the top? (Suppressed for downward stack segments except first.)
4989      *   $shade_side : Shade the right side? (Suppressed for leftward stack segments except first.)
4990      *      Only one of $shade_top or $shade_side can be FALSE. Both default to TRUE.
4991      */
4992     protected function DrawBar($x1, $y1, $x2, $y2, $data_color, $alt_color,
4993             $shade_top = TRUE, $shade_side = TRUE)
4994     {
4995         // Sort the points so x1,y1 is upper left and x2,y2 is lower right. This
4996         // is needed in order to get the shading right, and imagerectangle may require it.
4997         if ($x1 > $x2) {
4998             $t = $x1; $x1 = $x2; $x2 = $t;
4999         }
5000         if ($y1 > $y2) {
5001             $t = $y1; $y1 = $y2; $y2 = $t;
5002         }
5003
5004         // Draw the bar
5005         ImageFilledRectangle($this->img, $x1, $y1, $x2, $y2, $data_color);
5006
5007         // Draw a shade, or a border.
5008         if (($shade = $this->shading) > 0) {
5009             if ($shade_top && $shade_side) {
5010                 $npts = 6;
5011                 $pts = array($x1, $y1, $x1 + $shade, $y1 - $shade, $x2 + $shade, $y1 - $shade,
5012                              $x2 + $shade, $y2 - $shade, $x2, $y2, $x2, $y1);
5013             } else {
5014                 $npts = 4;
5015                 if ($shade_top) { // Suppress side shading
5016                     $pts = array($x1, $y1, $x1 + $shade, $y1 - $shade, $x2 + $shade, $y1 - $shade, $x2, $y1);
5017                 } else { // Suppress top shading
5018                     $pts = array($x2, $y2, $x2, $y1, $x2 + $shade, $y1 - $shade, $x2 + $shade, $y2 - $shade);
5019                 }
5020             }
5021             ImageFilledPolygon($this->img, $pts, $npts, $alt_color);
5022         } else {
5023             ImageRectangle($this->img, $x1, $y1, $x2,$y2, $alt_color);
5024         }
5025     }
5026
5027     /*
5028      *  Draw an Error Bar set. Used by DrawDotsError and DrawLinesError
5029      */
5030     protected function DrawYErrorBar($x_world, $y_world, $error_height, $error_bar_type, $color)
5031     {
5032         $x1 = $this->xtr($x_world);
5033         $y1 = $this->ytr($y_world);
5034         $y2 = $this->ytr($y_world+$error_height) ;
5035
5036         ImageSetThickness($this->img, $this->error_bar_line_width);
5037         ImageLine($this->img, $x1, $y1 , $x1, $y2, $color);
5038         if ($error_bar_type == 'tee') {
5039             ImageLine($this->img, $x1-$this->error_bar_size, $y2, $x1+$this->error_bar_size, $y2, $color);
5040         }
5041         ImageSetThickness($this->img, 1);
5042         return TRUE;
5043     }
5044
5045 /////////////////////////////////////////////
5046 ////////////////////             PLOT DRAWING
5047 /////////////////////////////////////////////
5048
5049     /*
5050      * Draws a pie chart. Data is 'text-data', 'data-data', or 'text-data-single'.
5051      *
5052      *  For text-data-single, the data array contains records with an ignored label,
5053      *  and one Y value. Each record defines a sector of the pie, as a portion of
5054      *  the sum of all Y values.
5055      *
5056      *  For text-data and data-data, the data array contains records with an ignored label,
5057      *  an ignored X value (for data-data only), and N (N>=1) Y values per record.
5058      *  The pie chart will be produced with N segments. The relative size of the first
5059      *  sector of the pie is the sum of the first Y data value in each record, etc.
5060      *
5061      *  Note: With text-data-single, the data labels could be used, but are not currently.
5062      *
5063      *  If there are no valid data points > 0 at all, just draw nothing. It may seem more correct to
5064      *  raise an error, but all of the other plot types handle it this way implicitly. DrawGraph
5065      *  checks for an empty data array, but this is different: a non-empty data array with no Y values,
5066      *  or all Y=0.
5067      */
5068     protected function DrawPieChart()
5069     {
5070         if (!$this->CheckDataType('text-data, text-data-single, data-data'))
5071             return FALSE;
5072
5073         // Allocate dark colors only if they will be used for shading.
5074         if ($this->shading > 0)
5075             $this->NeedDataDarkColors();
5076
5077         $xpos = $this->plot_area[0] + $this->plot_area_width/2;
5078         $ypos = $this->plot_area[1] + $this->plot_area_height/2;
5079         $diameter = min($this->plot_area_width, $this->plot_area_height);
5080         $radius = $diameter/2;
5081
5082         $num_slices = $this->data_columns;  // See CheckDataArray which calculates this for us.
5083         if ($num_slices < 1) return TRUE;   // Give up early if there is no data at all.
5084         $sumarr = array_fill(0, $num_slices, 0);
5085
5086         if ($this->datatype_pie_single) {
5087             // text-data-single: One data column per row, one pie slice per row.
5088             for ($i = 0; $i < $num_slices; $i++) {
5089                 // $legend[$i] = $this->data[$i][0];                // Note: Labels are not used yet
5090                 if (is_numeric($this->data[$i][1]))
5091                     $sumarr[$i] = abs($this->data[$i][1]);
5092             }
5093         } else {
5094             // text-data: Sum each column (skipping label), one pie slice per column.
5095             // data-data: Sum each column (skipping X value and label), one pie slice per column.
5096             $skip = ($this->datatype_implied) ? 1 : 2; // Leading values to skip in each row.
5097             for ($i = 0; $i < $this->num_data_rows; $i++) {
5098                 for ($j = $skip; $j < $this->num_recs[$i]; $j++) {
5099                     if (is_numeric($this->data[$i][$j]))
5100                         $sumarr[$j-$skip] += abs($this->data[$i][$j]);
5101                 }
5102             }
5103         }
5104
5105         $total = array_sum($sumarr);
5106
5107         if ($total == 0) {
5108             // There are either no valid data points, or all are 0.
5109             // See top comment about why not to make this an error.
5110             return TRUE;
5111         }
5112
5113         if ($this->shading) {
5114             $diam2 = $diameter / 2;
5115         } else {
5116             $diam2 = $diameter;
5117         }
5118         $max_data_colors = count($this->ndx_data_colors);
5119
5120         // Use the Y label format precision, with default value:
5121         if (isset($this->label_format['y']['precision']))
5122             $precision = $this->label_format['y']['precision'];
5123         else
5124             $precision = 1;
5125
5126         for ($h = $this->shading; $h >= 0; $h--) {
5127             $color_index = 0;
5128             $start_angle = 0;
5129             $end_angle = 0;
5130             for ($j = 0; $j < $num_slices; $j++) {
5131                 $val = $sumarr[$j];
5132
5133                 // For shaded pies: the last one (at the top of the "stack") has a brighter color:
5134                 if ($h == 0)
5135                     $slicecol = $this->ndx_data_colors[$color_index];
5136                 else
5137                     $slicecol = $this->ndx_data_dark_colors[$color_index];
5138
5139                 $label_txt = $this->number_format(($val / $total * 100), $precision) . '%';
5140                 $val = 360 * ($val / $total);
5141
5142                 // NOTE that imagefilledarc measures angles CLOCKWISE (go figure why),
5143                 // so the pie chart would start clockwise from 3 o'clock, would it not be
5144                 // for the reversal of start and end angles in imagefilledarc()
5145                 // Also note ImageFilledArc only takes angles in integer degrees, and if the
5146                 // the start and end angles match then you get a full circle not a zero-width
5147                 // pie. This is bad. So skip any zero-size wedge. On the other hand, we cannot
5148                 // let cumulative error from rounding to integer result in missing wedges. So
5149                 // keep the running total as a float, and round the angles. It should not
5150                 // be necessary to check that the last wedge ends at 360 degrees.
5151                 $start_angle = $end_angle;
5152                 $end_angle += $val;
5153                 // This method of conversion to integer - truncate after reversing it - was
5154                 // chosen to match the implicit method of PHPlot<=5.0.4 to get the same slices.
5155                 $arc_start_angle = (int)(360 - $start_angle);
5156                 $arc_end_angle = (int)(360 - $end_angle);
5157
5158                 if ($arc_start_angle > $arc_end_angle) {
5159                     $mid_angle = deg2rad($end_angle - ($val / 2));
5160
5161                     // Draw the slice
5162                     ImageFilledArc($this->img, $xpos, $ypos+$h, $diameter, $diam2,
5163                                    $arc_end_angle, $arc_start_angle,
5164                                    $slicecol, IMG_ARC_PIE);
5165
5166                     // Draw the labels only once
5167                     if ($h == 0) {
5168                         // Draw the outline
5169                         if (! $this->shading)
5170                             ImageFilledArc($this->img, $xpos, $ypos+$h, $diameter, $diam2,
5171                                            $arc_end_angle, $arc_start_angle, $this->ndx_grid_color,
5172                                            IMG_ARC_PIE | IMG_ARC_EDGED |IMG_ARC_NOFILL);
5173
5174                         // The '* 1.2' trick is to get labels out of the pie chart so there are more
5175                         // chances they can be seen in small sectors.
5176                         $label_x = $xpos + ($diameter * 1.2 * cos($mid_angle)) * $this->label_scale_position;
5177                         $label_y = $ypos+$h - ($diam2 * 1.2 * sin($mid_angle)) * $this->label_scale_position;
5178
5179                         $this->DrawText($this->fonts['generic'], 0, $label_x, $label_y, $this->ndx_grid_color,
5180                                         $label_txt, 'center', 'center');
5181                     }
5182                 }
5183                 if (++$color_index >= $max_data_colors)
5184                     $color_index = 0;
5185             }   // end for
5186         }   // end for
5187         return TRUE;
5188     }
5189
5190     /*
5191      * Draw the points and errors bars for an error plot of types points and linepoints
5192      * Supports only data-data-error format, with each row of the form
5193      *   array("title", x, y1, error1+, error1-, y2, error2+, error2-, ...)
5194      * This is called from DrawDots, with data type already checked.
5195      *   $paired is true for linepoints error plots, to make sure elements are
5196      *       only drawn once.  If true, data labels are drawn by DrawLinesError, and error
5197      *       bars are drawn by DrawDotsError. (This choice is for backwards compatibility.)
5198      */
5199     protected function DrawDotsError($paired = FALSE)
5200     {
5201         // Adjust the point shapes and point sizes arrays:
5202         $this->CheckPointParams();
5203
5204         $gcvars = array(); // For GetDataErrorColors, which initializes and uses this.
5205         // Special flag for data color callback to indicate the 'points' part of 'linepoints':
5206         $alt_flag = $paired ? 1 : 0;
5207
5208         for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
5209             $record = 1;                                // Skip record #0 (title)
5210
5211             $x_now = $this->data[$row][$record++];  // Read it, advance record index
5212
5213             $x_now_pixels = $this->xtr($x_now);             // Absolute coordinates.
5214
5215             // Draw X Data labels?
5216             if ($this->x_data_label_pos != 'none' && !$paired)
5217                 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels, $row);
5218
5219             // Now go for Y, E+, E-
5220             for ($idx = 0; $record < $this->num_recs[$row]; $idx++) {
5221                 if (is_numeric($this->data[$row][$record])) {         // Allow for missing Y data
5222
5223                     // Select the colors:
5224                     $this->GetDataErrorColors($row, $idx, $gcvars, $data_color, $error_color, $alt_flag);
5225
5226                     // Y:
5227                     $y_now = $this->data[$row][$record++];
5228                     $this->DrawDot($x_now, $y_now, $idx, $data_color);
5229
5230                     // Error +
5231                     $val = $this->data[$row][$record++];
5232                     $this->DrawYErrorBar($x_now, $y_now, $val, $this->error_bar_shape, $error_color);
5233                     // Error -
5234                     $val = $this->data[$row][$record++];
5235                     $this->DrawYErrorBar($x_now, $y_now, -$val, $this->error_bar_shape, $error_color);
5236                 } else {
5237                     $record += 3;  // Skip over missing Y and its error values
5238                 }
5239             }
5240         }
5241         return TRUE;
5242     }
5243
5244     /*
5245      * Draw a points plot, or the points for a linepoints plot
5246      * Data format can be text-data (label, y1, y2, ...) or data-data (label, x, y1, y2, ...)
5247      * Points plot with error bars (data-data-error format) is redirected to DrawDotsError.
5248      *   $paired is true for linepoints plots, to make sure elements are only drawn once.
5249      */
5250     protected function DrawDots($paired = FALSE)
5251     {
5252         if (!$this->CheckDataType('text-data, data-data, data-data-error'))
5253             return FALSE;
5254         if ($this->datatype_error_bars)
5255             return $this->DrawDotsError($paired); // Redirect for points+errorbars plot
5256
5257         // Adjust the point shapes and point sizes arrays:
5258         $this->CheckPointParams();
5259
5260         $gcvars = array(); // For GetDataColor, which initializes and uses this.
5261         // Special flag for data color callback to indicate the 'points' part of 'linepoints':
5262         $alt_flag = $paired ? 1 : 0;
5263
5264         // Data Value Labels? (Skip if doing the points from a linepoints plot)
5265         $do_dvls = !$paired && $this->CheckDataValueLabels($this->y_data_label_pos,
5266                       $dvl_x_off, $dvl_y_off, $dvl_h_align, $dvl_v_align);
5267
5268         for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
5269             $rec = 1;                    // Skip record #0 (data label)
5270
5271             if ($this->datatype_implied)                    // Implied X values?
5272                 $x_now = 0.5 + $cnt++;                      // Place text-data at X = 0.5, 1.5, 2.5, etc...
5273             else
5274                 $x_now = $this->data[$row][$rec++];         // Read it, advance record index
5275
5276             $x_now_pixels = $this->xtr($x_now);
5277
5278             // Draw X Data labels?
5279             if (!$paired && $this->x_data_label_pos != 'none')
5280                 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels, $row);
5281
5282             // Proceed with Y values
5283             for ($idx = 0;$rec < $this->num_recs[$row]; $rec++, $idx++) {
5284                 if (is_numeric($this->data[$row][$rec])) {              // Allow for missing Y data
5285                     $y_now = (double)$this->data[$row][$rec];
5286
5287                     // Select the color:
5288                     $this->GetDataColor($row, $idx, $gcvars, $data_color, $alt_flag);
5289                     // Draw the marker:
5290                     $this->DrawDot($x_now, $y_now, $idx, $data_color);
5291
5292                     // Draw data value labels?
5293                     if ($do_dvls) {
5294                         $this->DrawDataValueLabel('y', $x_now, $y_now, $y_now, $dvl_h_align, $dvl_v_align,
5295                                                   $dvl_x_off, $dvl_y_off);
5296                     }
5297                 }
5298             }
5299         }
5300         return TRUE;
5301     }
5302
5303     /*
5304      * Draw a Thin Bar Line plot, also known as an Impulse plot.
5305      * A clean, fast routine for when you just want charts like stock volume charts.
5306      * Supports data-data and text-data formats for vertical plots,
5307      * and data-data-yx and text-data-yx for horizontal plots.
5308      * Note that although this plot type supports multiple data sets, it rarely makes
5309      * sense to have more than 1, because the lines will overlay.
5310      * This one function does both vertical and horizontal plots. "iv" is used for the
5311      * independent variable (X for vertical plots, Y for horizontal) and "dv" is used
5312      * for the dependent variable (Y for vertical plots, X for horizontal).
5313      */
5314     protected function DrawThinBarLines()
5315     {
5316         if (!$this->CheckDataType('text-data, data-data, text-data-yx, data-data-yx'))
5317             return FALSE;
5318
5319         $gcvars = array(); // For GetDataColor, which initializes and uses this.
5320
5321         for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
5322             $rec = 1;                    // Skip record #0 (data label)
5323
5324             if ($this->datatype_implied)                    // Implied independent variable values?
5325                 $iv_now = 0.5 + $cnt++;                     // Place text-data at 0.5, 1.5, 2.5, etc...
5326             else
5327                 $iv_now = $this->data[$row][$rec++];        // Read it, advance record index
5328
5329             if ($this->datatype_swapped_xy) {
5330                 $y_now_pixels = $this->ytr($iv_now);
5331                 // Draw Y Data labels?
5332                 if ($this->y_data_label_pos != 'none')
5333                     $this->DrawYDataLabel($this->data[$row][0], $y_now_pixels);
5334             } else {
5335                 $x_now_pixels = $this->xtr($iv_now);
5336                 // Draw X Data labels?
5337                 if ($this->x_data_label_pos != 'none')
5338                     $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels);
5339             }
5340
5341             // Proceed with dependent values
5342             for ($idx = 0; $rec < $this->num_recs[$row]; $rec++, $idx++) {
5343                 if (is_numeric($this->data[$row][$rec])) {              // Allow for missing data
5344                     $dv = $this->data[$row][$rec];
5345                     ImageSetThickness($this->img, $this->line_widths[$idx]);
5346
5347                     // Select the color:
5348                     $this->GetDataColor($row, $idx, $gcvars, $data_color);
5349
5350                     if ($this->datatype_swapped_xy) {
5351                         // Draw a line from user defined y axis position right (or left) to xtr($dv)
5352                         ImageLine($this->img, $this->y_axis_x_pixels, $y_now_pixels,
5353                                               $this->xtr($dv), $y_now_pixels, $data_color);
5354                     } else {
5355                         // Draw a line from user defined x axis position up (or down) to ytr($dv)
5356                         ImageLine($this->img, $x_now_pixels, $this->x_axis_y_pixels,
5357                                               $x_now_pixels, $this->ytr($dv), $data_color);
5358                    }
5359                 }
5360             }
5361         }
5362
5363         ImageSetThickness($this->img, 1);
5364         return TRUE;
5365     }
5366
5367     /*
5368      * Draw an 'area' or 'stacked area' plot.
5369      * Both of these fill the area between lines, but in the stacked area graph the Y values
5370      * are accumulated for each X, same as stacked bars. In the regular area graph, the areas
5371      * are filled in order from the X axis up to each Y (so the Y values for each X need to be
5372      * in decreasing order in this case).
5373      * Data format can be text-data (label, y1, y2, ...) or data-data (label, x, y1, y2, ...)
5374      * Notes:
5375      *   All Y values must be >= 0. (If any Y<0 the absolute value is used.)
5376      *   Missing data points are NOT handled. (They are counted as 0.)
5377      *   All rows must have the same number of Y points, or an error image will be produced.
5378      */
5379     protected function DrawArea($do_stacked = FALSE)
5380     {
5381         if (!$this->CheckDataType('text-data, data-data'))
5382             return FALSE;
5383
5384         $n = $this->num_data_rows;  // Number of X values
5385
5386         // These arrays store the device X and Y coordinates for all lines:
5387         $xd = array();
5388         $yd = array();
5389
5390         // Make sure each row has the same number of values. Note records_per_group is max(num_recs).
5391         if ($this->records_per_group != min($this->num_recs)) {
5392             return $this->PrintError("DrawArea(): Data array must contain the same number"
5393                       . " of Y values for each X");
5394         }
5395
5396         // Calculate the Y value for each X, and store the device
5397         // coordinates into the xd and yd arrays.
5398         // For stacked area plots, the Y values accumulate.
5399         for ($row = 0; $row < $n; $row++) {
5400             $rec = 1;                                       // Skip record #0 (data label)
5401
5402             if ($this->datatype_implied)                    // Implied X values?
5403                 $x_now = 0.5 + $row;                        // Place text-data at X = 0.5, 1.5, 2.5, etc...
5404             else
5405                 $x_now = $this->data[$row][$rec++];         // Read it, advance record index
5406
5407             $x_now_pixels = $this->xtr($x_now);
5408
5409             if ($this->x_data_label_pos != 'none')          // Draw X Data labels?
5410                 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels);
5411
5412             // Store the X value.
5413             // There is an artificial Y value at the axis. For 'area' it goes
5414             // at the end; for stackedarea it goes before the start.
5415             $xd[$row] = $x_now_pixels;
5416             $yd[$row] = array();
5417             if ($do_stacked)
5418                 $yd[$row][] = $this->x_axis_y_pixels;
5419
5420             // Store the Y values for this X.
5421             // All Y values are clipped to the x axis which should be zero but can be moved.
5422             $y = 0;
5423             while ($rec < $this->records_per_group) {
5424                 if (is_numeric($this->data[$row][$rec])) {  // Treat missing values as 0.
5425                     $y += abs($this->data[$row][$rec]);
5426                 }
5427                 $yd[$row][] = $this->ytr(max($this->x_axis_position, $y));
5428                 if (!$do_stacked) $y = 0;
5429                 $rec++;
5430             }
5431
5432             if (!$do_stacked)
5433                 $yd[$row][] = $this->x_axis_y_pixels;
5434         }
5435
5436         // Now draw the filled polygons.
5437         // Note data_columns is the number of Y points (columns excluding label and X), and the
5438         // number of entries in the yd[] arrays is data_columns+1.
5439         $prev_row = 0;
5440         for ($row = 1; $row <= $this->data_columns; $row++) { // 1 extra for X axis artificial row
5441             $pts = array();
5442             // Previous data set forms top (for area) or bottom (for stackedarea):
5443             for ($j = 0; $j < $n; $j++) {
5444                 $pts[] = $xd[$j];
5445                 $pts[] = $yd[$j][$prev_row];
5446             }
5447             // Current data set forms bottom (for area) or top (for stackedarea):
5448             for ($j = $n- 1; $j >= 0; $j--) {
5449                 $pts[] = $xd[$j];
5450                 $pts[] = $yd[$j][$row];
5451             }
5452             // Draw it:
5453             ImageFilledPolygon($this->img, $pts, $n * 2, $this->ndx_data_colors[$prev_row]);
5454
5455             $prev_row = $row;
5456         }
5457         return TRUE;
5458     }
5459
5460     /*
5461      * Draw a line plot, or the lines part of a linepoints plot
5462      * Data format can be text-data (label, y1, y2, ...) or data-data (label, x, y1, y2, ...)
5463      * Line plot with error bars (data-data-error format) is redirected to DrawLinesError.
5464      *   $paired is true for linepoints plots, to make sure elements are only drawn once.
5465      */
5466     protected function DrawLines($paired = FALSE)
5467     {
5468         if (!$this->CheckDataType('text-data, data-data, data-data-error'))
5469             return FALSE;
5470         if ($this->datatype_error_bars)
5471             return $this->DrawLinesError($paired); // Redirect for lines+errorbar plot
5472
5473         // Flag array telling if the current point is valid, one element per plot line.
5474         // If start_lines[i] is true, then (lastx[i], lasty[i]) is the previous point.
5475         $start_lines = array_fill(0, $this->data_columns, FALSE);
5476
5477         $gcvars = array(); // For GetDataColor, which initializes and uses this.
5478
5479         // Data Value Labels?
5480         $do_dvls = $this->CheckDataValueLabels($this->y_data_label_pos,
5481                       $dvl_x_off, $dvl_y_off, $dvl_h_align, $dvl_v_align);
5482
5483         for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
5484             $record = 1;                                    // Skip record #0 (data label)
5485
5486             if ($this->datatype_implied)                    // Implied X values?
5487                 $x_now = 0.5 + $cnt++;                      // Place text-data at X = 0.5, 1.5, 2.5, etc...
5488             else
5489                 $x_now = $this->data[$row][$record++];      // Read it, advance record index
5490
5491             $x_now_pixels = $this->xtr($x_now);             // Absolute coordinates
5492
5493             if ($this->x_data_label_pos != 'none')          // Draw X Data labels?
5494                 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels, $row);
5495
5496             for ($idx = 0; $record < $this->num_recs[$row]; $record++, $idx++) {
5497                 if (($line_style = $this->line_styles[$idx]) == 'none')
5498                     continue; //Allow suppressing entire line, useful with linepoints
5499                 if (is_numeric($this->data[$row][$record])) {           //Allow for missing Y data
5500                     $y_now = (double)$this->data[$row][$record];
5501                     $y_now_pixels = $this->ytr($y_now);
5502
5503                     if ($start_lines[$idx]) {
5504                         // Set line width, revert it to normal at the end
5505                         ImageSetThickness($this->img, $this->line_widths[$idx]);
5506
5507                         // Select the color:
5508                         $this->GetDataColor($row, $idx, $gcvars, $data_color);
5509
5510                         if ($line_style == 'dashed') {
5511                             $this->SetDashedStyle($data_color);
5512                             $data_color = IMG_COLOR_STYLED;
5513                         }
5514                         ImageLine($this->img, $x_now_pixels, $y_now_pixels,
5515                                   $lastx[$idx], $lasty[$idx], $data_color);
5516                     }
5517
5518                     // Draw data value labels?
5519                     if ($do_dvls) {
5520                         $this->DrawDataValueLabel('y', $x_now, $y_now, $y_now, $dvl_h_align, $dvl_v_align,
5521                                                   $dvl_x_off, $dvl_y_off);
5522                     }
5523
5524                     $lasty[$idx] = $y_now_pixels;
5525                     $lastx[$idx] = $x_now_pixels;
5526                     $start_lines[$idx] = TRUE;
5527                 } elseif ($this->draw_broken_lines) {  // Y data missing, leave a gap.
5528                     $start_lines[$idx] = FALSE;
5529                 }
5530             }   // end for
5531         }   // end for
5532
5533         ImageSetThickness($this->img, 1);       // Revert to original state for lines to be drawn later.
5534         return TRUE;
5535     }
5536
5537     /*
5538      * Draw lines with error bars for an error plot of types lines and linepoints
5539      * Supports only data-data-error format, with each row of the form
5540      *   array("title", x, y1, error1+, error1-, y2, error2+, error2-, ...)
5541      * This is called from DrawLines, with data type already checked.
5542      *   $paired is true for linepoints error plots, to make sure elements are
5543      *       only drawn once.  If true, data labels are drawn by DrawLinesError, and error
5544      *       bars are drawn by DrawDotsError. (This choice is for backwards compatibility.)
5545      */
5546     protected function DrawLinesError($paired = FALSE)
5547     {
5548         $start_lines = array_fill(0, $this->data_columns, FALSE);
5549
5550         $gcvars = array(); // For GetDataErrorColors, which initializes and uses this.
5551
5552         for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
5553             $record = 1;                                    // Skip record #0 (data label)
5554
5555             $x_now = $this->data[$row][$record++];          // Read X value, advance record index
5556
5557             $x_now_pixels = $this->xtr($x_now);             // Absolute coordinates.
5558
5559             if ($this->x_data_label_pos != 'none')          // Draw X Data labels?
5560                 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels, $row);
5561
5562             // Now go for Y, E+, E-
5563             for ($idx = 0; $record < $this->num_recs[$row]; $idx++) {
5564                 if (($line_style = $this->line_styles[$idx]) == 'none')
5565                     continue; //Allow suppressing entire line, useful with linepoints
5566                 if (is_numeric($this->data[$row][$record])) {    // Allow for missing Y data
5567
5568                     // Select the colors:
5569                     $this->GetDataErrorColors($row, $idx, $gcvars, $data_color, $error_color);
5570
5571                     // Y
5572                     $y_now = $this->data[$row][$record++];
5573                     $y_now_pixels = $this->ytr($y_now);
5574
5575                     if ($start_lines[$idx]) {
5576                         ImageSetThickness($this->img, $this->line_widths[$idx]);
5577
5578                         if ($line_style == 'dashed') {
5579                             $this->SetDashedStyle($data_color);
5580                             $data_color = IMG_COLOR_STYLED;
5581                         }
5582                         ImageLine($this->img, $x_now_pixels, $y_now_pixels,
5583                                   $lastx[$idx], $lasty[$idx], $data_color);
5584                     }
5585
5586                     if ($paired) {
5587                         $record += 2; // Skip error bars - done in the 'points' part of 'linepoints'.
5588                     } else {
5589                         // Error+
5590                         $val = $this->data[$row][$record++];
5591                         $this->DrawYErrorBar($x_now, $y_now, $val, $this->error_bar_shape, $error_color);
5592
5593                         // Error-
5594                         $val = $this->data[$row][$record++];
5595                         $this->DrawYErrorBar($x_now, $y_now, -$val, $this->error_bar_shape, $error_color);
5596                     }
5597
5598                     // Update indexes:
5599                     $start_lines[$idx] = TRUE;   // Tells us if we already drew the first column of points,
5600                                              // thus having $lastx and $lasty ready for the next column.
5601                     $lastx[$idx] = $x_now_pixels;
5602                     $lasty[$idx] = $y_now_pixels;
5603
5604                 } else {
5605                     $record += 3;  // Skip over missing Y and its error values
5606                     if ($this->draw_broken_lines) {
5607                         $start_lines[$idx] = FALSE;
5608                     }
5609                 }
5610             }   // end for
5611         }   // end for
5612
5613         ImageSetThickness($this->img, 1);   // Revert to original state for lines to be drawn later.
5614         return TRUE;
5615     }
5616
5617     /*
5618      * Draw a Lines+Points plot (linepoints).
5619      * This just uses DrawLines and DrawDots. They handle the error-bar case themselves.
5620      */
5621     protected function DrawLinePoints()
5622     {
5623         // This check is redundant, as DrawLines and DrawDots do it, but left here as insurance.
5624         if (!$this->CheckDataType('text-data, data-data, data-data-error'))
5625             return FALSE;
5626         $this->DrawLines(TRUE);
5627         $this->DrawDots(TRUE);
5628         return TRUE;
5629     }
5630
5631     /*
5632      * Draw a Squared Line plot.
5633      * Data format can be text-data (label, y1, y2, ...) or data-data (label, x, y1, y2, ...)
5634      * This is based on DrawLines(), with one more line drawn for each point.
5635      */
5636     protected function DrawSquared()
5637     {
5638         if (!$this->CheckDataType('text-data, data-data'))
5639             return FALSE;
5640
5641         // Flag array telling if the current point is valid, one element per plot line.
5642         // If start_lines[i] is true, then (lastx[i], lasty[i]) is the previous point.
5643         $start_lines = array_fill(0, $this->data_columns, FALSE);
5644
5645         $gcvars = array(); // For GetDataColor, which initializes and uses this.
5646
5647         // Data Value Labels?
5648         $do_dvls = $this->CheckDataValueLabels($this->y_data_label_pos,
5649                       $dvl_x_off, $dvl_y_off, $dvl_h_align, $dvl_v_align);
5650
5651         for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
5652             $record = 1;                                    // Skip record #0 (data label)
5653
5654             if ($this->datatype_implied)                    // Implied X values?
5655                 $x_now = 0.5 + $cnt++;                      // Place text-data at X = 0.5, 1.5, 2.5, etc...
5656             else
5657                 $x_now = $this->data[$row][$record++];      // Read it, advance record index
5658
5659             $x_now_pixels = $this->xtr($x_now);             // Absolute coordinates
5660
5661             if ($this->x_data_label_pos != 'none')          // Draw X Data labels?
5662                 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels); // notice there is no last param.
5663
5664             // Draw Lines
5665             for ($idx = 0; $record < $this->num_recs[$row]; $record++, $idx++) {
5666                 if (is_numeric($this->data[$row][$record])) {               // Allow for missing Y data
5667                     $y_now = (double)$this->data[$row][$record];
5668                     $y_now_pixels = $this->ytr($y_now);
5669
5670                     if ($start_lines[$idx]) {
5671                         // Set line width, revert it to normal at the end
5672                         ImageSetThickness($this->img, $this->line_widths[$idx]);
5673
5674                         // Select the color:
5675                         $this->GetDataColor($row, $idx, $gcvars, $data_color);
5676
5677                         if ($this->line_styles[$idx] == 'dashed') {
5678                             $this->SetDashedStyle($data_color);
5679                             $data_color = IMG_COLOR_STYLED;
5680                         }
5681                         ImageLine($this->img, $lastx[$idx], $lasty[$idx],
5682                                   $x_now_pixels, $lasty[$idx], $data_color);
5683                         ImageLine($this->img, $x_now_pixels, $lasty[$idx],
5684                                   $x_now_pixels, $y_now_pixels, $data_color);
5685                     }
5686
5687                     // Draw data value labels?
5688                     if ($do_dvls) {
5689                         $this->DrawDataValueLabel('y', $x_now, $y_now, $y_now, $dvl_h_align, $dvl_v_align,
5690                                                   $dvl_x_off, $dvl_y_off);
5691                     }
5692
5693                     $lastx[$idx] = $x_now_pixels;
5694                     $lasty[$idx] = $y_now_pixels;
5695                     $start_lines[$idx] = TRUE;
5696                 } elseif ($this->draw_broken_lines) {  // Y data missing, leave a gap.
5697                     $start_lines[$idx] = FALSE;
5698                 }
5699             }
5700         }   // end while
5701
5702         ImageSetThickness($this->img, 1);
5703         return TRUE;
5704     }
5705
5706     /*
5707      * Draw a Bar chart
5708      * Supports text-data format, with each row in the form array(label, y1, y2, y3, ...)
5709      * Horizontal bars (text-data-yx format) are sent to DrawHorizBars() instead.
5710      */
5711     protected function DrawBars()
5712     {
5713         if (!$this->CheckDataType('text-data, text-data-yx'))
5714             return FALSE;
5715         if ($this->datatype_swapped_xy)
5716             return $this->DrawHorizBars();
5717         $this->CalcBarWidths(FALSE, TRUE); // Calculate bar widths for unstacked, vertical
5718
5719         // This is the X offset from the bar group's label center point to the left side of the first bar
5720         // in the group. See also CalcBarWidths above.
5721         $x_first_bar = ($this->data_columns * $this->record_bar_width) / 2 - $this->bar_adjust_gap;
5722
5723         $gcvars = array(); // For GetBarColors, which initializes and uses this.
5724
5725         for ($row = 0; $row < $this->num_data_rows; $row++) {
5726             $record = 1;                                    // Skip record #0 (data label)
5727
5728             $x_now_pixels = $this->xtr(0.5 + $row);         // Place text-data at X = 0.5, 1.5, 2.5, etc...
5729
5730             if ($this->x_data_label_pos != 'none')          // Draw X Data labels?
5731                 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels);
5732
5733             // Lower left X of first bar in the group:
5734             $x1 = $x_now_pixels - $x_first_bar;
5735
5736             // Draw the bars in the group:
5737             for ($idx = 0; $record < $this->num_recs[$row]; $record++, $idx++) {
5738                 if (is_numeric($this->data[$row][$record])) {       // Allow for missing Y data
5739                     $y = $this->data[$row][$record];
5740                     $x2 = $x1 + $this->actual_bar_width;
5741
5742                     if (($upgoing_bar = $y >= $this->x_axis_position)) {
5743                         $y1 = $this->ytr($y);
5744                         $y2 = $this->x_axis_y_pixels;
5745                     } else {
5746                         $y1 = $this->x_axis_y_pixels;
5747                         $y2 = $this->ytr($y);
5748                     }
5749
5750                     // Select the colors:
5751                     $this->GetBarColors($row, $idx, $gcvars, $data_color, $alt_color);
5752
5753                     // Draw the bar, and the shade or border:
5754                     $this->DrawBar($x1, $y1, $x2, $y2, $data_color, $alt_color);
5755
5756                     // Draw optional data labels above the bars (or below, for negative values).
5757                     if ( $this->y_data_label_pos == 'plotin') {
5758                         if ($upgoing_bar) {
5759                           $v_align = 'bottom';
5760                           $y_offset = -5 - $this->shading;
5761                         } else {
5762                           $v_align = 'top';
5763                           $y_offset = 2;
5764                         }
5765                         $this->DrawDataValueLabel('y', $row+0.5, $y, $y, 'center', $v_align,
5766                                 ($idx + 0.5) * $this->record_bar_width - $x_first_bar, $y_offset);
5767                     }
5768                 }
5769                 // Step to next bar in group:
5770                 $x1 += $this->record_bar_width;
5771             }   // end for
5772         }   // end for
5773         return TRUE;
5774     }
5775
5776     /*
5777      * Draw a Horizontal Bar chart
5778      * Supports only text-data-yx format, with each row in the form array(label, x1, x2, x3, ...)
5779      * Note that the data values are X not Y, and the bars are drawn horizontally.
5780      * This is called from DrawBars, which has already checked the data type.
5781      */
5782     protected function DrawHorizBars()
5783     {
5784         $this->CalcBarWidths(FALSE, FALSE); // Calculate bar widths for unstacked, vertical
5785
5786         // This is the Y offset from the bar group's label center point to the bottom of the first bar
5787         // in the group. See also CalcBarWidths above.
5788         $y_first_bar = ($this->data_columns * $this->record_bar_width) / 2 - $this->bar_adjust_gap;
5789
5790         $gcvars = array(); // For GetBarColors, which initializes and uses this.
5791
5792         for ($row = 0; $row < $this->num_data_rows; $row++) {
5793             $record = 1;                                    // Skip record #0 (data label)
5794
5795             $y_now_pixels = $this->ytr(0.5 + $row);         // Place bars at Y=0.5, 1.5, 2.5, etc...
5796
5797             if ($this->y_data_label_pos != 'none')          // Draw Y Data Labels?
5798                 $this->DrawYDataLabel($this->data[$row][0], $y_now_pixels);
5799
5800             // Lower left Y of first bar in the group:
5801             $y1 = $y_now_pixels + $y_first_bar;
5802
5803             // Draw the bars in the group:
5804             for ($idx = 0; $record < $this->num_recs[$row]; $record++, $idx++) {
5805                 if (is_numeric($this->data[$row][$record])) {       // Allow for missing X data
5806                     $x = $this->data[$row][$record];
5807                     $y2 = $y1 - $this->actual_bar_width;
5808
5809                     if (($rightwards_bar = $x >= $this->y_axis_position)) {
5810                         $x1 = $this->xtr($x);
5811                         $x2 = $this->y_axis_x_pixels;
5812                     } else {
5813                         $x1 = $this->y_axis_x_pixels;
5814                         $x2 = $this->xtr($x);
5815                     }
5816
5817                     // Select the colors:
5818                     $this->GetBarColors($row, $idx, $gcvars, $data_color, $alt_color);
5819
5820                     // Draw the bar, and the shade or border:
5821                     $this->DrawBar($x1, $y1, $x2, $y2, $data_color, $alt_color);
5822
5823                     // Draw optional data labels to the right of the bars (or left, if the bar
5824                     // goes left of the Y axis line).
5825                     if ($this->x_data_label_pos == 'plotin') {
5826                         if ($rightwards_bar) {
5827                           $h_align = 'left';
5828                           $x_offset = 5 + $this->shading;
5829                         } else {
5830                           $h_align = 'right';
5831                           $x_offset = -2;
5832                         }
5833                         $this->DrawDataValueLabel('x', $x, $row+0.5, $x, $h_align, 'center',
5834                                 $x_offset, $y_first_bar - ($idx + 0.5) * $this->record_bar_width);
5835                     }
5836
5837                 }
5838                 // Step to next bar in group:
5839                 $y1 -= $this->record_bar_width;
5840             }   // end for
5841         }   // end for
5842
5843         return TRUE;
5844     }
5845
5846     /*
5847      * Draw a Stacked Bar chart
5848      * Supports text-data format, with each row in the form array(label, y1, y2, y3, ...)
5849      * Horizontal stacked bars (text-data-yx format) are sent to DrawHorizStackedBars() instead.
5850      * Original stacked bars idea by Laurent Kruk < lolok at users.sourceforge.net >
5851      */
5852     protected function DrawStackedBars()
5853     {
5854         if (!$this->CheckDataType('text-data, text-data-yx'))
5855             return FALSE;
5856         if ($this->datatype_swapped_xy)
5857             return $this->DrawHorizStackedBars();
5858         $this->CalcBarWidths(TRUE, TRUE); // Calculate bar widths for stacked, vertical
5859
5860         // This is the X offset from the bar's label center point to the left side of the bar.
5861         $x_first_bar = $this->record_bar_width / 2 - $this->bar_adjust_gap;
5862
5863         $gcvars = array(); // For GetBarColors, which initializes and uses this.
5864
5865         // Determine if any data labels are on:
5866         $data_labels_within = ($this->y_data_label_pos == 'plotstack');
5867         $data_labels_end = $data_labels_within || ($this->y_data_label_pos == 'plotin');
5868         $data_label_y_offset = -5 - $this->shading; // For upward labels only.
5869
5870         for ($row = 0; $row < $this->num_data_rows; $row++) {
5871             $record = 1;                                    // Skip record #0 (data label)
5872
5873             $x_now_pixels = $this->xtr(0.5 + $row);         // Place text-data at X = 0.5, 1.5, 2.5, etc...
5874
5875             if ($this->x_data_label_pos != 'none')          // Draw X Data labels?
5876                 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels);
5877
5878             // Lower left and lower right X of the bars in this stack:
5879             $x1 = $x_now_pixels - $x_first_bar;
5880             $x2 = $x1 + $this->actual_bar_width;
5881
5882             // Draw the bar segments in this stack.
5883             $wy1 = 0;                       // World coordinates Y1, current sum of values
5884             $wy2 = $this->x_axis_position;  // World coordinates Y2, last drawn value
5885             $first = TRUE;
5886
5887             for ($idx = 0; $record < $this->num_recs[$row]; $record++, $idx++) {
5888
5889                 // Skip missing Y values, and ignore Y=0 values.
5890                 if (is_numeric($this->data[$row][$record])
5891                     && ($this_y = $this->data[$row][$record]) != 0) {
5892
5893                     // First non-zero value sets the direction, $upward. Note this compares to 0,
5894                     // not the axis position. Segments are based at 0 but clip to the axis.
5895                     if ($first)
5896                         $upward = ($this_y > 0);
5897
5898                     $wy1 += $this_y;    // Keep the running total for this bar stack
5899
5900                     // Draw nothing if this segment would not increase the bar height.
5901                     // Upward bars: draw if wy1>wy2.  Downward bars: Draw if wy1<wy2.
5902                     if (($wy1 < $wy2) XOR $upward) {
5903
5904                         $y1 = $this->ytr($wy1); // Convert to device coordinates. $y1 is outermost value.
5905                         $y2 = $this->ytr($wy2); // $y2 is innermost (closest to axis).
5906
5907                         // Select the colors:
5908                         $this->GetBarColors($row, $idx, $gcvars, $data_color, $alt_color);
5909
5910                         // Draw the bar, and the shade or border:
5911                         $this->DrawBar($x1, $y1, $x2, $y2, $data_color, $alt_color,
5912                             // Only shade the top for upward bars, or the first segment of downward bars:
5913                             $upward || $first, TRUE);
5914
5915                         // Draw optional data label for this bar segment just inside the end.
5916                         // Text value is the current Y, but position is the cumulative Y.
5917                         // The label is only drawn if it fits in the segment height |y2-y1|.
5918                         if ($data_labels_within) {
5919                             $this->DrawDataValueLabel('y', $row+0.5, $wy1, $this_y,
5920                                                       'center', $upward ? 'top' : 'bottom',
5921                                                       0, $upward ? 3 : -3, NULL, abs($y1 - $y2));
5922                         }
5923                         // Mark the new end of the bar, conditional on segment height > 0.
5924                         $wy2 = $wy1;
5925                     }
5926                     $first = FALSE;
5927                 }
5928             }   // end for
5929
5930             // Draw optional data label above the bar with the total value.
5931             // Value is wy1 (total value), but position is wy2 (end of the bar stack).
5932             // These differ only with wrong-direction segments, or a stack completely clipped by the axis.
5933             if ($data_labels_end) {
5934                 $this->DrawDataValueLabel('y', $row+0.5, $wy2, $wy1, 'center', $upward ? 'bottom' : 'top',
5935                                           0, $upward ? $data_label_y_offset : 5);
5936             }
5937         }   // end for
5938         return TRUE;
5939     }
5940
5941     /*
5942      * Draw a Horizontal Stacked Bar chart
5943      * Supports only text-data-yx format, with each row in the form array(label, x1, x2, x3, ...)
5944      * Note that the data values are X not Y, and the bars are drawn horizontally.
5945      * This is called from DrawStackedBars, which has already checked the data type.
5946      */
5947     protected function DrawHorizStackedBars()
5948     {
5949         $this->CalcBarWidths(TRUE, FALSE); // Calculate bar widths for stacked, horizontal
5950
5951         // This is the Y offset from the bar's label center point to the bottom of the bar
5952         $y_first_bar = $this->record_bar_width / 2 - $this->bar_adjust_gap;
5953
5954         $gcvars = array(); // For GetBarColors, which initializes and uses this.
5955
5956         // Determine if any data labels are on:
5957         $data_labels_within = ($this->x_data_label_pos == 'plotstack');
5958         $data_labels_end = $data_labels_within || ($this->x_data_label_pos == 'plotin');
5959         $data_label_x_offset = 5 + $this->shading; // For rightward labels only
5960
5961         for ($row = 0; $row < $this->num_data_rows; $row++) {
5962             $record = 1;                                    // Skip record #0 (data label)
5963
5964             $y_now_pixels = $this->ytr(0.5 + $row);         // Place bars at Y=0.5, 1.5, 2.5, etc...
5965
5966             if ($this->y_data_label_pos != 'none')          // Draw Y Data labels?
5967                 $this->DrawYDataLabel($this->data[$row][0], $y_now_pixels);
5968
5969             // Lower left and upper left Y of the bars in this stack:
5970             $y1 = $y_now_pixels + $y_first_bar;
5971             $y2 = $y1 - $this->actual_bar_width;
5972
5973             // Draw the bar segments in this stack:
5974             $wx1 = 0;                       // World coordinates X1, current sum of values
5975             $wx2 = $this->y_axis_position;  // World coordinates X2, last drawn value
5976             $first = TRUE;
5977
5978             for ($idx = 0; $record < $this->num_recs[$row]; $record++, $idx++) {
5979
5980                 // Skip missing X values, and ignore X<0 values.
5981                 if (is_numeric($this->data[$row][$record])
5982                     && ($this_x = $this->data[$row][$record]) != 0) {
5983
5984                     // First non-zero value sets the direction, $rightward. Note this compares to 0,
5985                     // not the axis position. Segments are based at 0 but clip to the axis.
5986                     if ($first)
5987                         $rightward = ($this_x > 0);
5988
5989                     $wx1 += $this_x;  // Keep the running total for this bar stack
5990
5991                     // Draw nothing if this segment would not increase the bar length.
5992                     // Rightward bars: draw if wx1>wx2. Leftward bars: Draw if wx1<wx2.
5993                     if (($wx1 < $wx2) XOR $rightward) {
5994
5995                         $x1 = $this->xtr($wx1); // Convert to device coordinates. $x1 is outermost value.
5996                         $x2 = $this->xtr($wx2); // $x2 is innermost (closest to axis).
5997
5998                         // Select the colors:
5999                         $this->GetBarColors($row, $idx, $gcvars, $data_color, $alt_color);
6000
6001                         // Draw the bar, and the shade or border:
6002                         $this->DrawBar($x1, $y1, $x2, $y2, $data_color, $alt_color,
6003                             // Only shade the side for rightward bars, or the first segment of leftward bars:
6004                             TRUE, $rightward || $first);
6005                         // Draw optional data label for this bar segment just inside the end.
6006                         // Text value is the current X, but position is the cumulative X.
6007                         // The label is only drawn if it fits in the segment width |x2-x1|.
6008                         if ($data_labels_within) {
6009                             $this->DrawDataValueLabel('x', $wx1, $row+0.5, $this_x,
6010                                                       $rightward ? 'right' : 'left', 'center',
6011                                                       $rightward ? -3 : 3, 0, abs($x1 - $x2), NULL);
6012                         }
6013                         // Mark the new end of the bar, conditional on segment width > 0.
6014                         $wx2 = $wx1;
6015                     }
6016                     $first = FALSE;
6017                 }
6018             }   // end for
6019
6020             // Draw optional data label right of the bar with the total value.
6021             // Value is wx1 (total value), but position is wx2 (end of the bar stack).
6022             // These differ only with wrong-direction segments, or a stack completely clipped by the axis.
6023             if ($data_labels_end) {
6024                 $this->DrawDataValueLabel('x', $wx2, $row+0.5, $wx1, $rightward ? 'left' : 'right', 'center',
6025                                           $rightward ? $data_label_x_offset : -5, 0);
6026             }
6027         }   // end for
6028         return TRUE;
6029     }
6030
6031     /*
6032      * Draw a financial "Open/High/Low/Close" (OHLC) plot, including candlestick plots.
6033      * Data format can be text-data (label, Yo, Yh, Yl, Yc) or data-data (label, X, Yo, Yh, Yl, Yc).
6034      * Yo="Opening price", Yc="Closing price", Yl="Low price", Yh="High price".
6035      * Each row must have exactly 4 Y values. No multiple data sets, no missing values.
6036      * There are 3 subtypes, selected by $draw_candles and $always_fill.
6037      *   $draw_candles  $always_fill  Description:
6038      *    FALSE          N/A          A basic OHLC chart with a vertical line for price range, horizontal
6039      *                                tick marks on left for opening price and right for closing price.
6040      *    TRUE           FALSE        A candlestick plot with filled body indicating close down, outline
6041      *                                for closing up, and vertical wicks for low and high prices.
6042      *    TRUE           TRUE         A candlestick plot where the candle bodies are always filled.
6043      * These map to 3 plot types per the $plots[] array.
6044      *
6045      * Data color usage:                        If closes down:   If closes up or unchanged:
6046      *    Candlestick body, ohlc range line:      color 0           color 1
6047      *    Candlestick wicks, ohlc tick marks:     color 2           color 3
6048      * There are three member variables that control the width (candlestick body or tick marks):
6049      *     ohlc_max_width, ohlc_min_width, ohlc_frac_width
6050      * (There is no API to change them at this time.)
6051      */
6052     protected function DrawOHLC($draw_candles, $always_fill = FALSE)
6053     {
6054         if (!$this->CheckDataType('text-data, data-data'))
6055             return FALSE;
6056
6057         // Assign name of GD function to draw candlestick bodies for stocks that close up.
6058         $draw_body_close_up = $always_fill ? 'imagefilledrectangle' : 'imagerectangle';
6059
6060         // These 3 variables control the calculation of the half-width of the candle body, or length of
6061         // the tick marks. This is scaled based on the plot density, but within tight limits.
6062         $min_width = isset($this->ohlc_min_width) ? $this->ohlc_min_width : 2;
6063         $max_width = isset($this->ohlc_max_width) ? $this->ohlc_max_width : 8;
6064         $width_factor = isset($this->ohlc_frac_width) ? $this->ohlc_frac_width : 0.3;
6065         $dw = max($min_width, min($max_width,
6066                      (int)($width_factor * $this->plot_area_width / $this->num_data_rows)));
6067
6068         // Get line widths to use: index 0 for body/stroke, 1 for wick/tick.
6069         list($body_thickness, $wick_thickness) = $this->line_widths;
6070
6071         $gcvars = array(); // For GetDataColor, which initializes and uses this.
6072
6073         for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
6074             $record = 1;                                    // Skip record #0 (data label)
6075
6076             if ($this->datatype_implied)                    // Implied X values?
6077                 $x_now = 0.5 + $cnt++;                      // Place text-data at X = 0.5, 1.5, 2.5, etc...
6078             else
6079                 $x_now = $this->data[$row][$record++];      // Read it, advance record index
6080
6081             $x_now_pixels = $this->xtr($x_now);             // Convert X to device coordinates
6082             $x_left = $x_now_pixels - $dw;
6083             $x_right = $x_now_pixels + $dw;
6084
6085             if ($this->x_data_label_pos != 'none')          // Draw X Data labels?
6086                 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels, $row);
6087
6088             // Require and use 4 numeric values in each row.
6089             if ($this->num_recs[$row] - $record != 4
6090                     || !is_numeric($yo = $this->data[$row][$record++])
6091                     || !is_numeric($yh = $this->data[$row][$record++])
6092                     || !is_numeric($yl = $this->data[$row][$record++])
6093                     || !is_numeric($yc = $this->data[$row][$record++])) {
6094                 return $this->PrintError("DrawOHLC: row $row must have 4 values.");
6095             }
6096
6097             // Set device coordinates for each value and direction flag:
6098             $yh_pixels = $this->ytr($yh);
6099             $yl_pixels = $this->ytr($yl);
6100             $yc_pixels = $this->ytr($yc);
6101             $yo_pixels = $this->ytr($yo);
6102             $closed_up = $yc >= $yo;
6103
6104             // Get data colors and line thicknesses:
6105             if ($closed_up) {
6106                 $this->GetDataColor($row, 1, $gcvars, $body_color); // Color 1 for body, closing up
6107                 $this->GetDataColor($row, 3, $gcvars, $ext_color);  // Color 3 for wicks/ticks
6108             } else {
6109                 $this->GetDataColor($row, 0, $gcvars, $body_color); // Color 0 for body, closing down
6110                 $this->GetDataColor($row, 2, $gcvars, $ext_color);  // Color 2 for wicks/ticks
6111             }
6112             imagesetthickness($this->img, $body_thickness);
6113
6114             if ($draw_candles) {
6115                 // Note: Unlike ImageFilledRectangle, ImageRectangle 'requires' its arguments in
6116                 // order with upper left corner first.
6117                 if ($closed_up) {
6118                     $yb1_pixels = $yc_pixels; // Upper body Y
6119                     $yb2_pixels = $yo_pixels; // Lower body Y
6120                     $draw_body = $draw_body_close_up;
6121                     // Avoid a PHP/GD bug resulting in "T"-shaped ends to zero height unfilled rectangle:
6122                     if ($yb1_pixels == $yb2_pixels)
6123                         $draw_body = 'imagefilledrectangle';
6124                 } else {
6125                     $yb1_pixels = $yo_pixels;
6126                     $yb2_pixels = $yc_pixels;
6127                     $draw_body = 'imagefilledrectangle';
6128                 }
6129
6130                 // Draw candle body
6131                 $draw_body($this->img, $x_left, $yb1_pixels, $x_right, $yb2_pixels, $body_color);
6132
6133                 // Draw upper and lower wicks, if they have height. (In device coords, that's dY<0)
6134                 imagesetthickness($this->img, $wick_thickness);
6135                 if ($yh_pixels < $yb1_pixels) {
6136                     imageline($this->img, $x_now_pixels, $yb1_pixels, $x_now_pixels, $yh_pixels, $ext_color);
6137                 }
6138                 if ($yl_pixels > $yb2_pixels) {
6139                     imageline($this->img, $x_now_pixels, $yb2_pixels, $x_now_pixels, $yl_pixels, $ext_color);
6140                 }
6141             } else {
6142                 // Basic OHLC
6143                 imageline($this->img, $x_now_pixels, $yl_pixels, $x_now_pixels, $yh_pixels, $body_color);
6144                 imagesetthickness($this->img, $wick_thickness);
6145                 imageline($this->img, $x_left, $yo_pixels, $x_now_pixels, $yo_pixels, $ext_color);
6146                 imageline($this->img, $x_right, $yc_pixels, $x_now_pixels, $yc_pixels, $ext_color);
6147             }
6148             imagesetthickness($this->img, 1);
6149         }
6150         return TRUE;
6151     }
6152
6153     /*
6154      * Draw the graph.
6155      * This is the function that performs the actual drawing, after all
6156      * the parameters and data are set up.
6157      * It also outputs the finished image, unless told not to.
6158      * Note: It is possible for this to be called multiple times.
6159      */
6160     function DrawGraph()
6161     {
6162         // Test for missing image, missing data, empty data:
6163         if (!$this->CheckDataArray())
6164             return FALSE; // Error message already reported.
6165
6166         // Set defaults then import plot type configuration:
6167         $draw_axes = TRUE;
6168         $draw_arg = array(); // Default is: no arguments to the drawing function
6169         extract(PHPlot::$plots[$this->plot_type]);
6170
6171         // Allocate colors for the plot:
6172         $this->SetColorIndexes();
6173
6174         // Get maxima and minima for scaling:
6175         if (!$this->FindDataLimits())
6176             return FALSE;
6177
6178         // Set plot area world values (plot_max_x, etc.):
6179         if (!$this->CalcPlotAreaWorld())
6180             return FALSE;
6181
6182         // Calculate X and Y axis positions in World Coordinates:
6183         $this->CalcAxisPositions();
6184
6185         // Process label-related parameters:
6186         $this->CheckLabels();
6187
6188         // Apply grid defaults:
6189         $this->CalcGridSettings();
6190
6191         // Calculate the plot margins, if needed.
6192         // For pie charts, set the $maximize argument to maximize space usage.
6193         $this->CalcMargins(!$draw_axes);
6194
6195         // Calculate the actual plot area in device coordinates:
6196         $this->CalcPlotAreaPixels();
6197
6198         // Calculate the mapping between world and device coordinates:
6199         $this->CalcTranslation();
6200
6201         // Pad color and style arrays to fit records per group:
6202         $this->PadArrays();
6203         $this->DoCallback('draw_setup');
6204
6205         $this->DrawBackground();
6206         $this->DrawImageBorder();
6207         $this->DoCallback('draw_image_background');
6208
6209         $this->DrawPlotAreaBackground();
6210         $this->DoCallback('draw_plotarea_background', $this->plot_area);
6211
6212         $this->DrawTitle();
6213         if ($draw_axes) {  // If no axes (pie chart), no axis titles either
6214             $this->DrawXTitle();
6215             $this->DrawYTitle();
6216         }
6217         $this->DoCallback('draw_titles');
6218
6219         if ($draw_axes && ! $this->grid_at_foreground) {   // Usually one wants grids to go back, but...
6220             $this->DrawYAxis();     // Y axis must be drawn before X axis (see DrawYAxis())
6221             $this->DrawXAxis();
6222             $this->DoCallback('draw_axes');
6223         }
6224
6225         // Call the plot-type drawing method:
6226         call_user_func_array(array($this, $draw_method), $draw_arg);
6227         $this->DoCallback('draw_graph', $this->plot_area);
6228
6229         if ($draw_axes && $this->grid_at_foreground) {   // Usually one wants grids to go back, but...
6230             $this->DrawYAxis();     // Y axis must be drawn before X axis (see DrawYAxis())
6231             $this->DrawXAxis();
6232             $this->DoCallback('draw_axes');
6233         }
6234
6235         if ($draw_axes) {
6236             $this->DrawPlotBorder();
6237             $this->DoCallback('draw_border');
6238         }
6239
6240         if ($this->legend) {
6241             $this->DrawLegend();
6242             $this->DoCallback('draw_legend');
6243         }
6244         $this->DoCallback('draw_all', $this->plot_area);
6245
6246         if ($this->print_image && !$this->PrintImage())
6247             return FALSE;
6248
6249         return TRUE;
6250     }
6251
6252 /////////////////////////////////////////////
6253 //////////////////         DEPRECATED METHODS
6254 /////////////////////////////////////////////
6255
6256     /*
6257      * Note on deprecated methods - as these pre-date the PHPlot Reference
6258      * Manual, and there is minimal documentation about them, I have neither
6259      * removed them nor changed them. They are not tested or documented, and
6260      * should not be used.
6261      */
6262
6263     /*
6264      * Deprecated, use SetYTickPos()
6265      */
6266     function SetDrawVertTicks($which_dvt)
6267     {
6268         if ($which_dvt != 1)
6269             $this->SetYTickPos('none');
6270         return TRUE;
6271     }
6272
6273     /*
6274      * Deprecated, use SetXTickPos()
6275      */
6276     function SetDrawHorizTicks($which_dht)
6277     {
6278         if ($which_dht != 1)
6279            $this->SetXTickPos('none');
6280         return TRUE;
6281     }
6282
6283     /*
6284      * Deprecated - use SetNumXTicks()
6285      */
6286     function SetNumHorizTicks($n)
6287     {
6288         return $this->SetNumXTicks($n);
6289     }
6290
6291     /*
6292      * Deprecated - use SetNumYTicks()
6293      */
6294     function SetNumVertTicks($n)
6295     {
6296         return $this->SetNumYTicks($n);
6297     }
6298
6299     /*
6300      * Deprecated - use SetXTickIncrement()
6301      */
6302     function SetHorizTickIncrement($inc)
6303     {
6304         return $this->SetXTickIncrement($inc);
6305     }
6306
6307     /*
6308      * Deprecated - use SetYTickIncrement()
6309      */
6310     function SetVertTickIncrement($inc)
6311     {
6312         return $this->SetYTickIncrement($inc);
6313     }
6314
6315     /*
6316      * Deprecated - use SetYTickPos()
6317      */
6318     function SetVertTickPosition($which_tp)
6319     {
6320         return $this->SetYTickPos($which_tp);
6321     }
6322
6323     /*
6324      * Deprecated - use SetXTickPos()
6325      */
6326     function SetHorizTickPosition($which_tp)
6327     {
6328         return $this->SetXTickPos($which_tp);
6329     }
6330
6331     /*
6332      * Deprecated - use SetFont()
6333      */
6334     function SetTitleFontSize($which_size)
6335     {
6336         return $this->SetFont('title', $which_size);
6337     }
6338
6339     /*
6340      * Deprecated - use SetFont()
6341      */
6342     function SetAxisFontSize($which_size)
6343     {
6344         $this->SetFont('x_label', $which_size);
6345         $this->SetFont('y_label', $which_size);
6346     }
6347
6348     /*
6349      * Deprecated - use SetFont()
6350      */
6351     function SetSmallFontSize($which_size)
6352     {
6353         return $this->SetFont('generic', $which_size);
6354     }
6355
6356     /*
6357      * Deprecated - use SetFont()
6358      */
6359     function SetXLabelFontSize($which_size)
6360     {
6361         return $this->SetFont('x_title', $which_size);
6362     }
6363
6364     /*
6365      * Deprecated - use SetFont()
6366      */
6367     function SetYLabelFontSize($which_size)
6368     {
6369         return $this->SetFont('y_title', $which_size);
6370     }
6371
6372     /*
6373      * Deprecated - use SetXTitle()
6374      */
6375     function SetXLabel($which_xlab)
6376     {
6377         return $this->SetXTitle($which_xlab);
6378     }
6379
6380     /*
6381      * Deprecated - use SetYTitle()
6382      */
6383     function SetYLabel($which_ylab)
6384     {
6385         return $this->SetYTitle($which_ylab);
6386     }
6387
6388     /*
6389      * Deprecated - use SetXTickLength() and SetYTickLength() instead.
6390      */
6391     function SetTickLength($which_tl)
6392     {
6393         $this->SetXTickLength($which_tl);
6394         $this->SetYTickLength($which_tl);
6395         return TRUE;
6396     }
6397
6398     /*
6399      * Deprecated - use SetYLabelType()
6400      */
6401     function SetYGridLabelType($which_yglt)
6402     {
6403         return $this->SetYLabelType($which_yglt);
6404     }
6405
6406     /*
6407      * Deprecated - use SetXLabelType()
6408      */
6409     function SetXGridLabelType($which_xglt)
6410     {
6411         return $this->SetXLabelType($which_xglt);
6412     }
6413     /*
6414      * Deprecated - use SetYTickLabelPos()
6415      */
6416     function SetYGridLabelPos($which_yglp)
6417     {
6418         return $this->SetYTickLabelPos($which_yglp);
6419     }
6420     /*
6421      * Deprecated - use SetXTickLabelPos()
6422      */
6423     function SetXGridLabelPos($which_xglp)
6424     {
6425         return $this->SetXTickLabelPos($which_xglp);
6426     }
6427
6428     /*
6429      * Deprecated - use SetXtitle()
6430      */
6431     function SetXTitlePos($xpos)
6432     {
6433         $this->x_title_pos = $xpos;
6434         return TRUE;
6435     }
6436
6437     /*
6438      * Deprecated - use SetYTitle()
6439      */
6440     function SetYTitlePos($xpos)
6441     {
6442         $this->y_title_pos = $xpos;
6443         return TRUE;
6444     }
6445
6446     /*
6447      * Deprecated - use SetXDataLabelPos()
6448      */
6449     function SetDrawXDataLabels($which_dxdl)
6450     {
6451         if ($which_dxdl == '1' )
6452             $this->SetXDataLabelPos('plotdown');
6453         else
6454             $this->SetXDataLabelPos('none');
6455     }
6456
6457     /*
6458      * Deprecated - use SetPlotAreaPixels()
6459      */
6460     function SetNewPlotAreaPixels($x1, $y1, $x2, $y2)
6461     {
6462         return $this->SetPlotAreaPixels($x1, $y1, $x2, $y2);
6463     }
6464
6465     /*
6466      * Deprecated - use SetLineWidths().
6467      */
6468     function SetLineWidth($which_lw)
6469     {
6470
6471         $this->SetLineWidths($which_lw);
6472
6473         if (!$this->error_bar_line_width) {
6474             $this->SetErrorBarLineWidth($which_lw);
6475         }
6476         return TRUE;
6477     }
6478
6479     /*
6480      * Deprecated - use SetPointShapes().
6481      */
6482     function SetPointShape($which_pt)
6483     {
6484         $this->SetPointShapes($which_pt);
6485         return TRUE;
6486     }
6487
6488     /*
6489      * Deprecated - use SetPointSizes().
6490      */
6491     function SetPointSize($which_ps)
6492     {
6493         $this->SetPointSizes($which_ps);
6494         return TRUE;
6495     }
6496 }
6497
6498 /*
6499  * The PHPlot_truecolor class extends PHPlot to use GD truecolor images.
6500  */
6501
6502 class PHPlot_truecolor extends PHPlot
6503 {
6504     /*
6505      * PHPlot Truecolor variation constructor: Create a PHPlot_truecolor object and initialize it.
6506      * Note this does NOT call the parent (PHPlot) constructor. It duplicates the code here.
6507      * Everything is the same as the PHPlot constructor except for imagecreatetruecolor.
6508      *
6509      * Parameters are the same as PHPlot:
6510      *   $which_width : Image width in pixels.
6511      *   $which_height : Image height in pixels.
6512      *   $which_output_file : Filename for output.
6513      *   $which_input_file : Path to a file to be used as background.
6514      */
6515     function __construct($which_width=600, $which_height=400, $which_output_file=NULL, $which_input_file=NULL)
6516     {
6517         $this->SetRGBArray($this->color_array);
6518
6519         if ($which_output_file)
6520             $this->SetOutputFile($which_output_file);
6521
6522         if ($which_input_file) {
6523             $this->SetInputFile($which_input_file);
6524         } else {
6525             $this->image_width = $which_width;
6526             $this->image_height = $which_height;
6527
6528             $this->img = imagecreatetruecolor($this->image_width, $this->image_height);
6529             if (! $this->img)
6530                 return $this->PrintError('PHPlot_truecolor(): Could not create image resource.');
6531         }
6532
6533         $this->SetDefaultStyles();
6534         $this->SetDefaultFonts();
6535     }
6536 }