6 * Copyright (C) 1998, 1999, 2000, 2001, 2002 Afan Ottenheimer. Released under
7 * the GPL and PHP licenses as stated in the the README file which
8 * should have been included with this document.
10 * Recent (2003-2004) work by Miguel de Benito Delgado <nonick AT 8027 DOT org>
15 // PHPLOT Version 5.0.rc1
16 // Requires PHP 4.1.0 or later (CHECK THIS)
18 if (! defined(__FUNCTION__))
19 define(__FUNCTION__, '__FUNCTION__ Requires at least PHP 4.3.0.');
21 define ('MINY', -1); // Indexes in $data (for DrawXDataLine())
24 error_reporting(E_ALL);
29 /* I have removed internal variable declarations, some isset() checking was required,
30 * but now the variables left are those which can be tweaked by the user. This is intended to
31 * be the first step towards moving most of the Set...() methods into a subclass which will be
32 * used only when strictly necessary. Many users will be able to put default values here in the
33 * class and thus avoid memory overhead and reduce parsing times.
35 //////////////// CONFIG PARAMETERS //////////////////////
36 var $reduction = 8; // BW Patch
37 var $is_inline = FALSE; // FALSE = Sends headers, TRUE = sends just raw image data
38 var $browser_cache = FALSE; // FALSE = Sends headers for browser to not cache the image,
39 // (only if is_inline = FALSE also)
41 var $safe_margin = 5; // Extra margin used in several places. In pixels
43 var $x_axis_position = ''; // Where to draw both axis (world coordinates),
44 var $y_axis_position = ''; // leave blank for X axis at 0 and Y axis at left of plot.
46 var $xscale_type = 'linear'; // linear, log
47 var $yscale_type = 'linear';
50 var $use_ttf = FALSE; // Use True Type Fonts?
51 var $ttf_path = '.'; // Default path to look in for TT Fonts.
52 var $default_ttfont = 'benjamingothic.ttf';
53 var $line_spacing = 4; // Pixels between lines.
55 // Font angles: 0 or 90 degrees for fixed fonts, any for TTF
56 var $x_label_angle = 0; // For labels on X axis (tick and data)
57 var $y_label_angle = 0; // For labels on Y axis (tick and data)
58 var $x_title_angle = 0; // Don't change this if you don't want to screw things up!
59 var $y_title_angle = 90; // Nor this.
60 var $title_angle = 0; // Or this.
63 var $file_format = 'png';
64 var $output_file = ''; // For output to a file instead of stdout
67 var $data_type = 'text-data'; // text-data, data-data-error, data-data, text-data-pie
68 var $plot_type= 'linepoints'; // bars, lines, linepoints, area, points, pie, thinbarline, squared
70 var $label_scale_position = 0.5; // Shifts data labes in pie charts. 1 = top, 0 = bottom
71 var $group_frac_width = 0.7; // value from 0 to 1 = width of bar groups
72 var $bar_width_adjust = 1; // 1 = bars of normal width, must be > 0
77 var $data_units_text = ''; // Units text for 'data' labels (i.e: 'ยค', '$', etc.)
82 var $x_title_txt = '';
83 var $x_title_pos = 'plotdown'; // plotdown, plotup, both, none
85 var $y_title_txt = '';
86 var $y_title_pos = 'plotleft'; // plotleft, plotright, both, none
90 // There are two types of labels in PHPlot:
91 // Tick labels: they follow the grid, next to ticks in axis. (DONE)
92 // they are drawn at grid drawing time, by _DrawXTicks() and _DrawYTicks()
93 // Data labels: they follow the data points, and can be placed on the axis or the plot (x/y) (TODO)
94 // they are drawn at graph plotting time, by DrawDataLabel(), called by DrawLines(), etc.
95 // Draw*DataLabel() also draws H/V lines to datapoints depending on draw_*_data_label_line
98 var $x_tick_label_pos = 'plotdown'; // plotdown, plotup, both, xaxis, none
99 var $y_tick_label_pos = 'plotleft'; // plotleft, plotright, both, yaxis, none
102 var $x_data_label_pos = 'plotdown'; // plotdown, plotup, both, plot, all, none
103 var $y_data_label_pos = 'plotleft'; // plotleft, plotright, both, plot, all, none
105 var $draw_x_data_label_lines = FALSE; // Draw a line from the data point to the axis?
106 var $draw_y_data_label_lines = FALSE; // TODO
108 // Label types: (for tick, data and plot labels)
109 var $x_label_type = ''; // data, time. Leave blank for no formatting.
110 var $y_label_type = ''; // data, time. Leave blank for no formatting.
111 var $x_time_format = '%H:%m:%s'; // See http://www.php.net/manual/html/function.strftime.html
112 var $y_time_format = '%H:%m:%s'; // SetYTimeFormat() too...
115 var $x_label_inc = 1; // Draw a label every this many (1 = all) (TODO)
116 var $y_label_inc = 1;
119 var $legend = ''; // An array with legend titles
120 var $legend_x_pos = '';
121 var $legend_y_pos = '';
125 var $x_tick_length = 5; // tick length in pixels for upper/lower axis
126 var $y_tick_length = 5; // tick length in pixels for left/right axis
128 var $x_tick_cross = 3; // ticks cross x axis this many pixels
129 var $y_tick_cross = 3; // ticks cross y axis this many pixels
131 var $x_tick_pos = 'plotdown'; // plotdown, plotup, both, xaxis, none
132 var $y_tick_pos = 'plotleft'; // plotright, plotleft, both, yaxis, none
134 var $num_x_ticks = '';
135 var $num_y_ticks = '';
137 var $x_tick_increment = ''; // Set num_x_ticks or x_tick_increment, not both.
138 var $y_tick_increment = ''; // Set num_y_ticks or y_tick_increment, not both.
140 var $skip_top_tick = FALSE;
141 var $skip_bottom_tick = FALSE;
142 var $skip_left_tick = FALSE;
143 var $skip_right_tick = FALSE;
146 var $draw_x_grid = FALSE;
147 var $draw_y_grid = TRUE;
149 var $dashed_grid = TRUE;
151 //Colors and styles (all colors can be array (R,G,B) or named color)
152 var $color_array = 'small'; // 'small', 'large' or array (define your own colors)
153 // See rgb.inc.php and SetRGBArray()
154 var $i_border = array(194, 194, 194);
155 var $plot_bg_color = 'white';
156 var $bg_color = 'white';
157 var $label_color = 'black';
158 var $text_color = 'black';
159 var $grid_color = 'black';
160 var $light_grid_color = 'gray';
161 var $tick_color = 'black';
162 var $title_color = 'black';
163 var $data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'orange', 'red', 'violet', 'azure1');
164 var $error_bar_colors = array('SkyBlue', 'green', 'orange', 'blue', 'orange', 'red', 'violet', 'azure1');
165 var $data_border_colors = array('black');
167 var $line_widths = 1; // single value or array
168 var $line_styles = array('solid', 'solid', 'dashed'); // single value or array
169 var $dashed_style = '2-4'; // colored dots-transparent dots
172 var $point_shape = 'diamond'; // rect, circle, diamond, triangle, dot, line, halfline, cross
174 var $error_bar_size = 5; // right and left size of tee
175 var $error_bar_shape = 'tee'; // 'tee' or 'line'
176 var $error_bar_line_width = 1; // single value (or array TODO)
178 var $plot_border_type = 'sides'; // left, sides, none, full
179 var $image_border_type = 'none'; // 'raised', 'plain', 'none'
181 var $shading = 5; // 0 for no shading, > 0 is size of shadows in pixels
183 var $draw_plot_area_background = FALSE;
184 var $draw_broken_lines = FALSE; // Tells not to draw lines for missing Y data.
187 //////////////////////////////////////////////////////
189 //////////////////////////////////////////////////////
192 * Constructor: Setup img resource, colors and size of the image, and font sizes.
194 * \param which_width int Image width in pixels.
195 * \param which_height int Image height in pixels.
196 * \param which_output_file string Filename for output.
197 * \param which_input_fule string Path to a file to be used as background.
199 function PHPlot($which_width=600, $which_height=400, $which_output_file=NULL, $which_input_file=NULL)
202 * Please see http://www.php.net/register_shutdown_function
203 * PLEASE NOTE: register_shutdown_function() will take a copy of the object rather than a reference
204 * so we put an ampersand. However, the function registered will work on the object as it
205 * was upon registration. To solve this, one of two methods can be used:
206 * $obj = new object();
207 * register_shutdown_function(array(&$obj,'shutdown'));
209 * $obj = &new object();
210 * HOWEVER, as the second statement assigns $obj a reference to the current object, it might be that
211 * several instances mess things up... (CHECK THIS)
214 * as $this->img is set upon construction of the object, problems will not arise for us (for the
215 * moment maybe, so I put all this here just in case)
217 register_shutdown_function(array(&$this, '_PHPlot'));
219 $this->SetRGBArray($this->color_array);
221 $this->background_done = FALSE; // Set to TRUE after background image first drawn
223 if ($which_output_file)
224 $this->SetOutputFile($which_output_file);
226 if ($which_input_file)
227 $this->SetInputFile($which_input_file);
229 $this->image_width = $which_width;
230 $this->image_height = $which_height;
232 $this->img = ImageCreate($this->image_width, $this->image_height);
234 $this->PrintError('PHPlot(): Could not create image resource.');
238 $this->SetDefaultStyles();
239 $this->SetDefaultFonts();
242 $this->SetXTitle('');
243 $this->SetYTitle('');
245 $this->print_image = TRUE; // Use for multiple plots per image (TODO: automatic)
249 * Destructor. Image resources not deallocated can be memory hogs, I think
250 * it is safer to automatically call imagedestroy upon script termination than
252 * See notes in the constructor code.
256 ImageDestroy($this->img);
261 /////////////////////////////////////////////
262 ////////////// COLORS
263 /////////////////////////////////////////////
266 * Returns an index to a color passed in as anything (string, hex, rgb)
268 * \param which_color * Color (can be '#AABBCC', 'Colorname', or array(r,g,b))
270 function SetIndexColor($which_color)
272 list ($r, $g, $b) = $this->SetRGBColor($which_color); //Translate to RGB
273 $index = ImageColorExact($this->img, $r, $g, $b);
275 return ImageColorResolve($this->img, $r, $g, $b);
283 * Returns an index to a slightly darker color than the one requested.
285 function SetIndexDarkColor($which_color)
287 list ($r, $g, $b) = $this->SetRGBColor($which_color);
289 $r -= 0x30; $r = ($r < 0) ? 0 : $r;
290 $g -= 0x30; $g = ($g < 0) ? 0 : $g;
291 $b -= 0x30; $b = ($b < 0) ? 0 : $b;
293 $index = ImageColorExact($this->img, $r, $g, $b);
295 return ImageColorResolve($this->img, $r, $g, $b);
302 * Sets/reverts all colors and styles to their defaults. If session is set, then only updates indices,
303 * as they are lost with every script execution, else, sets the default colors by name or value and
304 * then updates indices too.
306 * FIXME Isn't this too slow?
309 function SetDefaultStyles()
311 /* Some of the Set*() functions use default values when they get no parameters. */
313 if (! isset($this->session_set)) {
314 // If sessions are enabled, this variable will be preserved, so upon future executions, we
315 // will have it set, as well as color names (though not color indices, that's why we
316 // need to rebuild them)
317 $this->session_set = TRUE;
319 // These only need to be set once
320 $this->SetLineWidths();
321 $this->SetLineStyles();
322 $this->SetDefaultDashedStyle($this->dashed_style);
323 $this->SetPointSize($this->point_size);
326 $this->SetImageBorderColor($this->i_border);
327 $this->SetPlotBgColor($this->plot_bg_color);
328 $this->SetBackgroundColor($this->bg_color);
329 $this->SetLabelColor($this->label_color);
330 $this->SetTextColor($this->text_color);
331 $this->SetGridColor($this->grid_color);
332 $this->SetLightGridColor($this->light_grid_color);
333 $this->SetTickColor($this->tick_color);
334 $this->SetTitleColor($this->title_color);
335 $this->SetDataColors();
336 $this->SetErrorBarColors();
337 $this->SetDataBorderColors();
344 function SetBackgroundColor($which_color)
346 $this->bg_color= $which_color;
347 $this->ndx_bg_color= $this->SetIndexColor($this->bg_color);
354 function SetPlotBgColor($which_color)
356 $this->plot_bg_color= $which_color;
357 $this->ndx_plot_bg_color= $this->SetIndexColor($this->plot_bg_color);
364 function SetTitleColor($which_color)
366 $this->title_color= $which_color;
367 $this->ndx_title_color= $this->SetIndexColor($this->title_color);
374 function SetTickColor ($which_color)
376 $this->tick_color= $which_color;
377 $this->ndx_tick_color= $this->SetIndexColor($this->tick_color);
385 function SetLabelColor ($which_color)
387 $this->label_color = $which_color;
388 $this->ndx_title_color= $this->SetIndexColor($this->label_color);
396 function SetTextColor ($which_color)
398 $this->text_color= $which_color;
399 $this->ndx_text_color= $this->SetIndexColor($this->text_color);
407 function SetLightGridColor ($which_color)
409 $this->light_grid_color= $which_color;
410 $this->ndx_light_grid_color= $this->SetIndexColor($this->light_grid_color);
418 function SetGridColor ($which_color)
420 $this->grid_color = $which_color;
421 $this->ndx_grid_color= $this->SetIndexColor($this->grid_color);
429 function SetImageBorderColor($which_color)
431 $this->i_border = $which_color;
432 $this->ndx_i_border = $this->SetIndexColor($this->i_border);
433 $this->ndx_i_border_dark = $this->SetIndexDarkColor($this->i_border);
441 function SetTransparentColor($which_color)
443 ImageColorTransparent($this->img, $this->SetIndexColor($which_color));
449 * Sets the array of colors to be used. It can be user defined, a small predefined one
450 * or a large one included from 'rgb.inc.php'.
452 * \param which_color_array If an array, the used as color array. If a string can
453 * be one of 'small' or 'large'.
455 function SetRGBArray ($which_color_array)
457 if ( is_array($which_color_array) ) { // User defined array
458 $this->rgb_array = $which_color_array;
460 } elseif ($which_color_array == 'small') { // Small predefined color array
461 $this->rgb_array = array(
462 'white' => array(255, 255, 255),
463 'snow' => array(255, 250, 250),
464 'PeachPuff' => array(255, 218, 185),
465 'ivory' => array(255, 255, 240),
466 'lavender' => array(230, 230, 250),
467 'black' => array( 0, 0, 0),
468 'DimGrey' => array(105, 105, 105),
469 'gray' => array(190, 190, 190),
470 'grey' => array(190, 190, 190),
471 'navy' => array( 0, 0, 128),
472 'SlateBlue' => array(106, 90, 205),
473 'blue' => array( 0, 0, 255),
474 'SkyBlue' => array(135, 206, 235),
475 'cyan' => array( 0, 255, 255),
476 'DarkGreen' => array( 0, 100, 0),
477 'green' => array( 0, 255, 0),
478 'YellowGreen' => array(154, 205, 50),
479 'yellow' => array(255, 255, 0),
480 'orange' => array(255, 165, 0),
481 'gold' => array(255, 215, 0),
482 'peru' => array(205, 133, 63),
483 'beige' => array(245, 245, 220),
484 'wheat' => array(245, 222, 179),
485 'tan' => array(210, 180, 140),
486 'brown' => array(165, 42, 42),
487 'salmon' => array(250, 128, 114),
488 'red' => array(255, 0, 0),
489 'pink' => array(255, 192, 203),
490 'maroon' => array(176, 48, 96),
491 'magenta' => array(255, 0, 255),
492 'violet' => array(238, 130, 238),
493 'plum' => array(221, 160, 221),
494 'orchid' => array(218, 112, 214),
495 'purple' => array(160, 32, 240),
496 'azure1' => array(240, 255, 255),
497 'aquamarine1' => array(127, 255, 212)
500 } elseif ($which_color_array === 'large') { // Large color array
501 include("./rgb.inc.php");
502 $this->rgb_array = $RGBArray;
503 } else { // Default to black and white only.
504 $this->rgb_array = array('white' => array(255, 255, 255), 'black' => array(0, 0, 0));
511 * Returns an array in R, G, B format 0-255
513 * \param color_asked array(R,G,B) or string (named color or '#AABBCC')
515 function SetRGBColor($color_asked)
517 if ($color_asked == '') { $color_asked = array(0, 0, 0); };
519 if ( count($color_asked) == 3 ) { // already array of 3 rgb
520 $ret_val = $color_asked;
521 } else { // asking for a color by string
522 if(substr($color_asked, 0, 1) == '#') { // asking in #FFFFFF format.
523 $ret_val = array(hexdec(substr($color_asked, 1, 2)), hexdec(substr($color_asked, 3, 2)),
524 hexdec(substr($color_asked, 5, 2)));
525 } else { // asking by color name
526 $ret_val = $this->rgb_array[$color_asked];
534 * Sets the colors for the data.
536 function SetDataColors($which_data = NULL, $which_border = NULL)
538 if (is_null($which_data) && is_array($this->data_colors)) {
539 // use already set data_colors
540 } else if (! is_array($which_data)) {
541 $this->data_colors = ($which_data) ? array($which_data) : array('blue', 'red', 'green', 'orange');
543 $this->data_colors = $which_data;
547 foreach ($this->data_colors as $col) {
548 $this->ndx_data_colors[$i] = $this->SetIndexColor($col);
549 $this->ndx_data_dark_colors[$i] = $this->SetIndexDarkColor($col);
553 // For past compatibility:
554 $this->SetDataBorderColors($which_border);
555 } // function SetDataColors()
561 function SetDataBorderColors($which_br = NULL)
563 if (is_null($which_br) && is_array($this->data_border_colors)) {
564 // use already set data_border_colors
565 } else if (! is_array($which_br)) {
566 // Create new array with specified color
567 $this->data_border_colors = ($which_br) ? array($which_br) : array('black');
569 $this->data_border_colors = $which_br;
573 foreach($this->data_border_colors as $col) {
574 $this->ndx_data_border_colors[$i] = $this->SetIndexColor($col);
577 } // function SetDataBorderColors()
581 * Sets the colors for the data error bars.
583 function SetErrorBarColors($which_err = NULL)
585 if (is_null($which_err) && is_array($this->error_bar_colors)) {
586 // use already set error_bar_colors
587 } else if (! is_array($which_err)) {
588 $this->error_bar_colors = ($which_err) ? array($which_err) : array('black');
590 $this->error_bar_colors = $which_err;
594 foreach($this->error_bar_colors as $col) {
595 $this->ndx_error_bar_colors[$i] = $this->SetIndexColor($col);
600 } // function SetErrorBarColors()
604 * Sets the default dashed style.
605 * \param which_style A string specifying order of colored and transparent dots,
606 * i.e: '4-3' means 4 colored, 3 transparent;
607 * '2-3-1-2' means 2 colored, 3 transparent, 1 colored, 2 transparent.
609 function SetDefaultDashedStyle($which_style)
611 // String: "numcol-numtrans-numcol-numtrans..."
612 $asked = explode('-', $which_style);
614 if (count($asked) < 2) {
615 $this->DrawError("SetDefaultDashedStyle(): Wrong parameter '$which_style'.");
619 // Build the string to be eval()uated later by SetDashedStyle()
620 $this->default_dashed_style = 'array( ';
623 foreach($asked as $s) {
625 $this->default_dashed_style .= str_repeat('$which_ndxcol,', $s);
627 $this->default_dashed_style .= str_repeat('IMG_COLOR_TRANSPARENT,', $s);
631 // Remove trailing comma and add closing parenthesis
632 $this->default_dashed_style = substr($this->default_dashed_style, 0, -1);
633 $this->default_dashed_style .= ')';
640 * Sets the style before drawing a dashed line. Defaults to $this->default_dashed_style
641 * \param which_ndxcol Color index to be used.
643 function SetDashedStyle($which_ndxcol)
645 // See SetDefaultDashedStyle() to understand this.
646 eval ("\$style = $this->default_dashed_style;");
647 return imagesetstyle($this->img, $style);
652 * Sets line widths on a per-line basis.
654 function SetLineWidths($which_lw=NULL)
656 if (is_null($which_lw)) {
657 // Do nothing, use default value.
658 } else if (is_array($which_lw)) {
659 // Did we get an array with line widths?
660 $this->line_widths = $which_lw;
662 $this->line_widths = array($which_lw);
670 function SetLineStyles($which_ls=NULL)
672 if (is_null($which_ls)) {
673 // Do nothing, use default value.
674 } else if (! is_array($which_ls)) {
675 // Did we get an array with line styles?
676 $this->line_styles = $which_ls;
678 $this->line_styles = ($which_ls) ? array($which_ls) : array('solid');
684 /////////////////////////////////////////////
686 /////////////////////////////////////////////
690 * Sets number of pixels between lines of the same text.
692 function SetLineSpacing($which_spc)
694 $this->line_spacing = $which_spc;
699 * Enables use of TrueType fonts in the graph. Font initialisation methods
700 * depend on this setting, so when called, SetUseTTF() resets the font
703 function SetUseTTF($which_ttf)
705 $this->use_ttf = $which_ttf;
707 $this->SetDefaultFonts();
712 * Sets the directory name to look into for TrueType fonts.
714 function SetTTFPath($which_path)
716 // Maybe someone needs really dynamic config. He'll need this:
719 if (is_dir($which_path) && is_readable($which_path)) {
720 $this->ttf_path = $which_path;
723 $this->PrintError("SetTTFPath(): $which_path is not a valid path.");
729 * Sets the default TrueType font and updates all fonts to that.
731 function SetDefaultTTFont($which_font)
733 if (is_file($which_font) && is_readable($which_font)) {
734 $this->default_ttfont = $which_font;
735 return $this->SetDefaultFonts();
737 $this->PrintError("SetDefaultTTFont(): $which_font is not a valid font file.");
743 * Sets fonts to their defaults
745 function SetDefaultFonts()
748 if ($this->use_ttf) {
749 //$this->SetTTFPath(dirname($_SERVER['PHP_SELF']));
750 $this->SetTTFPath(getcwd());
751 $this->SetFont('generic', $this->default_ttfont, 8);
752 $this->SetFont('title', $this->default_ttfont, 14);
753 $this->SetFont('legend', $this->default_ttfont, 8);
754 $this->SetFont('x_label', $this->default_ttfont, 6);
755 $this->SetFont('y_label', $this->default_ttfont, 6);
756 $this->SetFont('x_title', $this->default_ttfont, 10);
757 $this->SetFont('y_title', $this->default_ttfont, 10);
761 $this->SetFont('generic', 2);
762 $this->SetFont('title', 5);
763 $this->SetFont('legend', 2);
764 $this->SetFont('x_label', 1);
765 $this->SetFont('y_label', 1);
766 $this->SetFont('x_title', 3);
767 $this->SetFont('y_title', 3);
774 * Sets Fixed/Truetype font parameters.
775 * \param $which_elem Is the element whose font is to be changed.
776 * It can be one of 'title', 'legend', 'generic',
777 * 'x_label', 'y_label', x_title' or 'y_title'
778 * \param $which_font Can be a number (for fixed font sizes) or
779 * a string with the filename when using TTFonts.
780 * \param $which_size Point size (TTF only)
781 * Calculates and updates internal height and width variables.
783 function SetFont($which_elem, $which_font, $which_size = 12)
786 if ($this->use_ttf) {
787 $path = $this->ttf_path.'/'.$which_font;
789 if (! is_file($path) || ! is_readable($path) ) {
790 $this->DrawError("SetFont(): True Type font $path doesn't exist");
794 switch ($which_elem) {
796 $this->generic_font['font'] = $path;
797 $this->generic_font['size'] = $which_size;
800 $this->title_font['font'] = $path;
801 $this->title_font['size'] = $which_size;
804 $this->legend_font['font'] = $path;
805 $this->legend_font['size'] = $which_size;
808 $this->x_label_font['font'] = $path;
809 $this->x_label_font['size'] = $which_size;
812 $this->y_label_font['font'] = $path;
813 $this->y_label_font['size'] = $which_size;
816 $this->x_title_font['font'] = $path;
817 $this->x_title_font['size'] = $which_size;
820 $this->y_title_font['font'] = $path;
821 $this->y_title_font['size'] = $which_size;
824 $this->DrawError("SetFont(): Unknown element '$which_elem' specified.");
832 if ($which_font > 5 || $which_font < 0) {
833 $this->DrawError('SetFont(): Non-TTF font size must be 1, 2, 3, 4 or 5');
837 switch ($which_elem) {
839 $this->generic_font['font'] = $which_font;
840 $this->generic_font['height'] = ImageFontHeight($which_font);
841 $this->generic_font['width'] = ImageFontWidth($which_font);
844 $this->title_font['font'] = $which_font;
845 $this->title_font['height'] = ImageFontHeight($which_font);
846 $this->title_font['width'] = ImageFontWidth($which_font);
849 $this->legend_font['font'] = $which_font;
850 $this->legend_font['height'] = ImageFontHeight($which_font)-$this->reduction; // BW Patch
851 $this->legend_font['width'] = ImageFontWidth($which_font);
854 $this->x_label_font['font'] = $which_font;
855 $this->x_label_font['height'] = ImageFontHeight($which_font);
856 $this->x_label_font['width'] = ImageFontWidth($which_font);
859 $this->y_label_font['font'] = $which_font;
860 $this->y_label_font['height'] = ImageFontHeight($which_font);
861 $this->y_label_font['width'] = ImageFontWidth($which_font);
864 $this->x_title_font['font'] = $which_font;
865 $this->x_title_font['height'] = ImageFontHeight($which_font);
866 $this->x_title_font['width'] = ImageFontWidth($which_font);
869 $this->y_title_font['font'] = $which_font;
870 $this->y_title_font['height'] = ImageFontHeight($which_font);
871 $this->y_title_font['width'] = ImageFontWidth($which_font);
874 $this->DrawError("SetFont(): Unknown element '$which_elem' specified.");
882 * Returns an array with the size of the bounding box of an
883 * arbitrarily placed (rotated) TrueType text string.
885 function TTFBBoxSize($size, $angle, $font, $string)
887 // First, assume angle < 90
888 $arr = ImageTTFBBox($size, 0, $font, $string);
889 $flat_width = $arr[2] - $arr[0];
890 $flat_height = abs($arr[3] - $arr[5]);
892 // Now the bounding box
893 $angle = deg2rad($angle);
894 $width = ceil(abs($flat_width*cos($angle) + $flat_height*sin($angle))); //Must be integer
895 $height = ceil(abs($flat_width*sin($angle) + $flat_height*cos($angle))); //Must be integer
897 return array($width, $height);
902 * Draws a string of text. Horizontal and vertical alignment are relative to
903 * to the drawing. That is: vertical text (90 deg) gets centered along y-axis
904 * with v_align = 'center', and adjusted to the left of x-axis with h_align = 'right',
906 * \note Original multiple lines code submitted by Remi Ricard.
907 * \note Original vertical code submitted by Marlin Viss.
909 function DrawText($which_font, $which_angle, $which_xpos, $which_ypos, $which_color, $which_text,
910 $which_halign = 'left', $which_valign = 'bottom')
913 if ($this->use_ttf) {
914 $size = $this->TTFBBoxSize($which_font['size'], $which_angle, $which_font['font'], $which_text);
915 $rads = deg2rad($which_angle);
917 if ($which_valign == 'center')
918 $which_ypos += $size[1]/2;
920 if ($which_valign == 'bottom')
921 $which_ypos += $size[1];
923 if ($which_halign == 'center')
924 $which_xpos -= ($size[0]/2) * cos($rads);
926 if ($which_halign == 'left')
927 $which_xpos += $size[0] * sin($rads);
929 if ($which_halign == 'right')
930 $which_xpos -= $size[0] * cos($rads);
932 ImageTTFText($this->img, $which_font['size'], $which_angle,
933 $which_xpos, $which_ypos, $which_color, $which_font['font'], $which_text);
937 // Split the text by its lines, and count them
938 $which_text = ereg_replace("\r", "", $which_text);
939 $str = split("\n", $which_text);
940 $nlines = count($str);
941 $spacing = $this->line_spacing * ($nlines - 1);
944 // (Remember the alignment convention with vertical text)
945 if ($which_angle == 90) {
946 // The text goes around $which_xpos.
947 if ($which_halign == 'center')
948 $which_xpos -= ($nlines * ($which_font['height'] + $spacing))/2;
950 // Left alignment requires no modification to $xpos...
951 // Right-align it. $which_xpos designated the rightmost x coordinate.
952 else if ($which_halign == 'right')
953 $which_xpos += ($nlines * ($which_font['height'] + $spacing));
956 for($i = 0; $i < $nlines; $i++) {
957 // Center the text vertically around $which_ypos (each line)
958 if ($which_valign == 'center')
959 $ypos = $which_ypos + (strlen($str[$i]) * $which_font['width']) / 2;
960 // Make the text finish (vertically) at $which_ypos
961 if ($which_valign == 'bottom')
962 $ypos = $which_ypos + strlen($str[$i]) * $which_font['width'];
964 ImageStringUp($this->img, $which_font['font'],
965 $i * ($which_font['height'] + $spacing) + $which_xpos,
966 $ypos, $str[$i], $which_color);
971 // The text goes above $which_ypos
972 if ($which_valign == 'top')
973 $which_ypos -= $nlines * ($which_font['height'] + $spacing);
974 // The text is centered around $which_ypos
975 if ($which_valign == 'center')
976 $which_ypos -= ($nlines * ($which_font['height'] + $spacing))/2;
977 // valign = 'bottom' requires no modification
980 for($i = 0; $i < $nlines; $i++) {
981 // center the text around $which_xpos
982 if ($which_halign == 'center')
983 $xpos = $which_xpos - (strlen($str[$i]) * $which_font['width'])/2;
984 // make the text finish at $which_xpos
985 if ($which_halign == 'right')
986 $xpos = $which_xpos - strlen($str[$i]) * $which_font['width'];
988 ImageString($this->img, $which_font['font'], $xpos,
989 $i * ($which_font['height'] + $spacing) + $which_ypos,
990 $str[$i], $which_color);
995 } // function DrawText()
998 /////////////////////////////////////////////
999 /////////// INPUT / OUTPUT CONTROL
1000 /////////////////////////////////////////////
1003 * Sets output format.
1004 * inline condition, cacheable, etc.
1006 function SetFileFormat($which_file_format)
1008 // I know rewriting this was unnecessary, but it didn't work for me, I don't
1010 $asked = strtolower($which_file_format);
1013 if (imagetypes() & IMG_JPG)
1017 if (imagetypes() & IMG_PNG)
1021 if (imagetypes() & IMG_GIF)
1025 if (imagetypes() & IMG_WBMP)
1029 $this->PrintError("SetFileFormat(): Unrecognized option '$which_file_format'");
1032 $this->PrintError("SetFileFormat():File format '$which_file_format' not supported");
1038 * Selects an input file to be used as background for the whole graph.
1040 function SetInputFile($which_input_file)
1042 $size = GetImageSize($which_input_file);
1043 $input_type = $size[2];
1045 switch($input_type) {
1047 $im = @ImageCreateFromGIF ($which_input_file);
1048 if (!$im) { // See if it failed
1049 $this->PrintError("Unable to open $which_input_file as a GIF");
1054 $im = @ImageCreateFromPNG ($which_input_file);
1055 if (!$im) { // See if it failed
1056 $this->PrintError("Unable to open $which_input_file as a PNG");
1061 $im = @ImageCreateFromJPEG ($which_input_file);
1062 if (!$im) { // See if it failed
1063 $this->PrintError("Unable to open $which_input_file as a JPG");
1068 $this->PrintError('SetInputFile(): Please select gif, jpg, or png for image type!');
1073 // Set Width and Height of Image
1074 $this->image_width = $size[0];
1075 $this->image_height = $size[1];
1077 // Deallocate any resources previously allocated
1079 imagedestroy($this->img);
1087 function SetOutputFile($which_output_file)
1089 $this->output_file = $which_output_file;
1094 * Sets the output image as 'inline', ie. no Content-Type headers are sent
1095 * to the browser. Very useful if you want to embed the images.
1097 function SetIsInline($which_ii)
1099 $this->is_inline = $which_ii;
1105 * Performs the actual outputting of the generated graph, and
1106 * destroys the image resource.
1108 function PrintImage()
1110 // Browser cache stuff submitted by Thiemo Nagel
1111 if ( (! $this->browser_cache) && (! $this->is_inline)) {
1112 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
1113 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
1114 header('Cache-Control: no-cache, must-revalidate');
1115 header('Pragma: no-cache');
1118 switch($this->file_format) {
1120 if (! $this->is_inline) {
1121 Header('Content-type: image/png');
1123 if ($this->is_inline && $this->output_file != '') {
1124 ImagePng($this->img, $this->output_file);
1126 ImagePng($this->img);
1130 if (! $this->is_inline) {
1131 Header('Content-type: image/jpeg');
1133 if ($this->is_inline && $this->output_file != '') {
1134 ImageJPEG($this->img, $this->output_file);
1136 ImageJPEG($this->img);
1140 if (! $this->is_inline) {
1141 Header('Content-type: image/gif');
1143 if ($this->is_inline && $this->output_file != '') {
1144 ImageGIF($this->img, $this->output_file);
1146 ImageGIF($this->img);
1150 case 'wbmp': // wireless bitmap, 2 bit.
1151 if (! $this->is_inline) {
1152 Header('Content-type: image/wbmp');
1154 if ($this->is_inline && $this->output_file != '') {
1155 ImageWBMP($this->img, $this->output_file);
1157 ImageWBMP($this->img);
1162 $this->PrintError('PrintImage(): Please select an image type!');
1169 * Prints an error message to stdout and dies
1171 function PrintError($error_message)
1173 echo "<p><b>Fatal error</b>: $error_message<p>";
1178 * Prints an error message inline into the generated image and draws it centered
1179 * around the given coordinates (defaults to center of the image)
1180 * \param error_message Message to be drawn
1181 * \param where_x X coordinate
1182 * \param where_y Y coordinate
1184 function DrawError($error_message, $where_x = NULL, $where_y = NULL)
1187 $this->PrintError('_DrawError(): Warning, no image resource allocated. '.
1188 'The message to be written was: '.$error_message);
1190 $ypos = (! $where_y) ? $this->image_height/2 : $where_y;
1191 $xpos = (! $where_x) ? $this->image_width/2 : $where_x;
1192 ImageRectangle($this->img, 0, 0, $this->image_width, $this->image_height,
1193 ImageColorAllocate($this->img, 255, 255, 255));
1195 $this->DrawText($this->generic_font, 0, $xpos, $ypos, ImageColorAllocate($this->img, 0, 0, 0),
1196 $error_message, 'center', 'center');
1198 $this->PrintImage();
1203 /////////////////////////////////////////////
1205 /////////////////////////////////////////////
1209 * Sets position for X labels following data points.
1211 function SetXDataLabelPos($which_xdlp)
1213 $this->x_data_label_pos = $this->CheckOption($which_xdlp, 'plotdown, plotup, both, xaxis, all, none',
1215 if ($which_xdlp != 'none')
1216 $this->x_tick_label_pos == 'none';
1222 * Sets position for Y labels following data points.
1224 function SetYDataLabelPos($which_ydlp)
1226 $this->y_data_label_pos = $this->CheckOption($which_ydlp, 'plotleft, plotright, both, yaxis, all, none',
1228 if ($which_ydlp != 'none')
1229 $this->y_tick_label_pos == 'none';
1236 * Sets position for X labels following ticks (hence grid lines)
1238 function SetXTickLabelPos($which_xtlp)
1240 $this->x_tick_label_pos = $this->CheckOption($which_xtlp, 'plotdown, plotup, both, xaxis, all, none',
1242 if ($which_xtlp != 'none')
1243 $this->x_data_label_pos == 'none';
1249 * Sets position for Y labels following ticks (hence grid lines)
1251 function SetYTickLabelPos($which_ytlp)
1253 $this->y_tick_label_pos = $this->CheckOption($which_ytlp, 'plotleft, plotright, both, yaxis, all, none',
1255 if ($which_ytlp != 'none')
1256 $this->y_data_label_pos == 'none';
1262 * Sets type for tick and data labels on X axis.
1263 * \note 'title' type left for backwards compatibility.
1265 function SetXLabelType($which_xlt)
1267 $this->x_label_type = $this->CheckOption($which_xlt, 'data, time, title', __FUNCTION__);
1272 * Sets type for tick and data labels on Y axis.
1274 function SetYLabelType($which_ylt)
1276 $this->y_label_type = $this->CheckOption($which_ylt, 'data, time', __FUNCTION__);
1280 function SetXTimeFormat($which_xtf)
1282 $this->x_time_format = $which_xtf;
1285 function SetYTimeFormat($which_ytf)
1287 $this->y_time_format = $which_ytf;
1291 function SetXLabelAngle($which_xla)
1293 $this->x_label_angle = $which_xla;
1297 function SetYLabelAngle($which_yla)
1299 $this->y_label_angle = $which_yla;
1303 /////////////////////////////////////////////
1305 /////////////////////////////////////////////
1308 * ON THE WORKS: XXX XXX XXX XXX
1310 ***************** __FUNCTION__ needs PHP 4.3.0
1312 * Checks the valididy of an option.
1313 * \param which_opt String to check.
1314 * \param which_acc String of accepted choices.
1315 * \param which_func Name of the calling function, for error messages.
1316 * \note If checking everywhere for correctness slows things down, we could provide a
1317 * child class overriding every Set...() method which uses CheckOption(). Those new
1318 * methods could proceed in the unsafe but faster way.
1320 function CheckOption($which_opt, $which_acc, $which_func)
1322 $asked = trim($which_opt);
1324 // FIXME: this for backward compatibility, as eregi() fails with empty strings.
1328 if (@ eregi($asked, $which_acc)) {
1331 $this->DrawError("$which_func(): '$which_opt' not in available choices: '$which_acc'.");
1338 * \note Submitted by Thiemo Nagel
1340 function SetBrowserCache($which_browser_cache)
1342 $this->browser_cache = $which_browser_cache;
1347 * Whether to show the final image or not
1349 function SetPrintImage($which_pi)
1351 $this->print_image = $which_pi;
1356 * Sets the graph's legend. If argument is not an array, appends it to the legend.
1358 function SetLegend($which_leg)
1360 if (is_array($which_leg)) { // use array
1361 $this->legend = $which_leg;
1363 } else if (! is_null($which_leg)) { // append string
1364 $this->legend[] = $which_leg;
1367 $this->DrawError("SetLegend(): argument must not be null.");
1373 * Specifies the absolute (relative to image's up/left corner) position
1374 * of the legend's upper/leftmost corner.
1375 * $which_type not yet used (TODO)
1377 function SetLegendPixels($which_x, $which_y, $which_type=NULL)
1379 $this->legend_x_pos = $which_x;
1380 $this->legend_y_pos = $which_y;
1386 * Specifies the relative (to graph's origin) position of the legend's
1387 * upper/leftmost corner. MUST be called after scales are set up.
1388 * $which_type not yet used (TODO)
1390 function SetLegendWorld($which_x, $which_y, $which_type=NULL)
1392 if (! $this->scale_is_set)
1393 $this->CalcTranslation();
1395 $this->legend_x_pos = $this->xtr($which_x);
1396 $this->legend_y_pos = $this->ytr($which_y);
1402 * Accepted values are: left, sides, none, full
1404 function SetPlotBorderType($pbt)
1406 $this->plot_border_type = $this->CheckOption($pbt, 'left, sides, none, full', __FUNCTION__);
1410 * Accepted values are: raised, plain
1412 function SetImageBorderType($sibt)
1414 $this->image_border_type = $this->CheckOption($sibt, 'raised, plain', __FUNCTION__);
1421 function SetDrawPlotAreaBackground($dpab)
1423 $this->draw_plot_area_background = (bool)$dpab;
1430 function SetDrawYGrid($dyg)
1432 $this->draw_y_grid = (bool)$dyg;
1440 function SetDrawXGrid($dxg)
1442 $this->draw_x_grid = (bool)$dxg;
1450 function SetDrawDashedGrid($ddg)
1452 $this->dashed_grid = (bool)$ddg;
1460 function SetDrawXDataLabelLines($dxdl)
1462 $this->draw_x_data_label_lines = (bool)$dxdl;
1468 * TODO: draw_y_data_label_lines not implemented.
1471 function SetDrawYDataLabelLines($dydl)
1473 $this->draw_y_data_label_lines = $dydl;
1477 * Sets the graph's title.
1479 function SetTitle($which_title)
1481 $this->title_txt = $which_title;
1483 if ($which_title == '') {
1484 $this->title_height = 0;
1488 $str = split("\n", $which_title);
1489 $lines = count($str);
1490 $spacing = $this->line_spacing * ($lines - 1);
1492 if ($this->use_ttf) {
1493 $size = $this->TTFBBoxSize($this->title_font['size'], 0, $this->title_font['font'], $which_title);
1494 $this->title_height = $size[1] * $lines;
1496 $this->title_height = ($this->title_font['height'] + $spacing) * $lines;
1502 * Sets the X axis title and position.
1504 function SetXTitle($which_xtitle, $which_xpos = 'plotdown')
1506 if ($which_xtitle == '')
1507 $which_xpos = 'none';
1509 $this->x_title_pos = $this->CheckOption($which_xpos, 'plotdown, plotup, both, none', __FUNCTION__);
1511 $this->x_title_txt = $which_xtitle;
1513 $str = split("\n", $which_xtitle);
1514 $lines = count($str);
1515 $spacing = $this->line_spacing * ($lines - 1);
1517 if ($this->use_ttf) {
1518 $size = $this->TTFBBoxSize($this->x_title_font['size'], 0, $this->x_title_font['font'], $which_xtitle);
1519 $this->x_title_height = $size[1] * $lines;
1521 $this->x_title_height = ($this->y_title_font['height'] + $spacing) * $lines;
1529 * Sets the Y axis title and position.
1531 function SetYTitle($which_ytitle, $which_ypos = 'plotleft')
1533 if ($which_ytitle == '')
1534 $which_ypos = 'none';
1536 $this->y_title_pos = $this->CheckOption($which_ypos, 'plotleft, plotright, both, none', __FUNCTION__);
1538 $this->y_title_txt = $which_ytitle;
1540 $str = split("\n", $which_ytitle);
1541 $lines = count($str);
1542 $spacing = $this->line_spacing * ($lines - 1);
1544 if ($this->use_ttf) {
1545 $size = $this->TTFBBoxSize($this->y_title_font['size'], 90, $this->y_title_font['font'],
1547 $this->y_title_width = $size[0] * $lines;
1549 $this->y_title_width = ($this->y_title_font['height'] + $spacing) * $lines;
1556 * Sets the size of the drop shadow for bar and pie charts.
1557 * \param which_s int Size in pixels.
1559 function SetShading($which_s)
1561 $this->shading = (int)$which_s;
1565 function SetPlotType($which_pt)
1567 $this->plot_type = $this->CheckOption($which_pt,
1568 'bars, lines, linepoints, area, points, pie, thinbarline, squared',
1573 * Sets the position of Y axis.
1574 * \param pos int Position in world coordinates.
1576 function SetYAxisPosition($pos)
1578 $this->y_axis_position = (int)$pos;
1579 if (isset($this->scale_is_set)) {
1580 $this->CalcTranslation();
1586 * Sets the position of X axis.
1587 * \param pos int Position in world coordinates.
1589 function SetXAxisPosition($pos)
1591 $this->x_axis_position = (int)$pos;
1592 if (isset($this->scale_is_set)) {
1593 $this->CalcTranslation();
1599 function SetXScaleType($which_xst)
1601 $this->xscale_type = $this->CheckOption($which_xst, 'linear, log', __FUNCTION__);
1605 function SetYScaleType($which_yst)
1607 $this->yscale_type = $this->CheckOption($which_yst, 'linear, log', __FUNCTION__);
1611 function SetPrecisionX($which_prec)
1613 $this->x_precision = $which_prec;
1616 function SetPrecisionY($which_prec)
1618 $this->y_precision = $which_prec;
1622 function SetErrorBarLineWidth($which_seblw)
1624 $this->error_bar_line_width = $which_seblw;
1628 function SetLabelScalePosition($which_blp)
1631 $this->label_scale_position = $which_blp;
1635 function SetErrorBarSize($which_ebs)
1638 $this->error_bar_size = $which_ebs;
1643 * Can be one of: 'tee', 'line'
1645 function SetErrorBarShape($which_ebs)
1647 $this->error_bar_shape = $this->CheckOption($which_ebs, 'tee, line', __FUNCTION__);
1651 * Can be one of: 'halfline', 'line', 'plus', 'cross', 'rect', 'circle', 'dot',
1652 * 'diamond', 'triangle', 'trianglemid'
1654 function SetPointShape($which_pt)
1656 $this->point_shape = $this->CheckOption($which_pt,
1657 'halfline, line, plus, cross, rect, circle, dot, diamond, triangle, trianglemid',
1662 * Sets the point size for point plots.
1663 * \param ps int Size in pixels.
1665 function SetPointSize($ps)
1667 $this->point_size = (int)$ps;
1669 if ($this->point_shape == 'diamond' or $this->point_shape == 'triangle') {
1670 if ($this->point_size % 2 != 0) {
1671 $this->point_size++;
1679 * Tells not to draw lines for missing Y data. Only works with 'lines' and 'squared' plots.
1682 function SetDrawBrokenLines($bl)
1684 $this->draw_broken_lines = (bool)$bl;
1689 * text-data: ('label', y1, y2, y3, ...)
1690 * text-data-pie: ('label', y1), for pie charts. See DrawPieChart()
1691 * data-data: ('label', x, y1, y2, y3, ...)
1692 * data-data-error: ('label', x1, y1, e1+, e2-, y2, e2+, e2-, y3, e3+, e3-, ...)
1694 function SetDataType($which_dt)
1696 //The next three lines are for past compatibility.
1697 if ($which_dt == 'text-linear') { $which_dt = 'text-data'; };
1698 if ($which_dt == 'linear-linear') { $which_dt = 'data-data'; };
1699 if ($which_dt == 'linear-linear-error') { $which_dt = 'data-data-error'; };
1701 $this->data_type = $this->CheckOption($which_dt, 'text-data, text-data-pie, data-data, data-data-error',
1707 * Copy the array passed as data values. We convert to numerical indexes, for its
1708 * use for (or while) loops, which sometimes are faster. Performance improvements
1709 * vary from 28% in DrawLines() to 49% in DrawArea() for plot drawing functions.
1711 function SetDataValues(&$which_dv)
1713 $this->num_data_rows = count($which_dv);
1714 $this->total_records = 0; // Perform some useful calculations.
1715 $this->records_per_group = 1;
1716 for ($i = 0, $recs = 0; $i < $this->num_data_rows; $i++) {
1718 $this->data[$i] = array_values($which_dv[$i]); // convert to numerical indices.
1720 // Compute some values
1721 $recs = count($this->data[$i]);
1722 $this->total_records += $recs;
1724 if ($recs > $this->records_per_group)
1725 $this->records_per_group = $recs;
1727 $this->num_recs[$i] = $recs;
1732 * Pad styles arrays for later use by plot drawing functions:
1733 * This removes the need for $max_data_colors, etc. and $color_index = $color_index % $max_data_colors
1734 * in DrawBars(), DrawLines(), etc.
1736 function PadArrays()
1738 array_pad_array($this->line_widths, $this->records_per_group);
1739 array_pad_array($this->line_styles, $this->records_per_group);
1741 array_pad_array($this->data_colors, $this->records_per_group);
1742 array_pad_array($this->data_border_colors, $this->records_per_group);
1743 array_pad_array($this->error_bar_colors, $this->records_per_group);
1745 $this->SetDataColors();
1746 $this->SetDataBorderColors();
1747 $this->SetErrorBarColors();
1753 //////////////////////////////////////////////////////////
1754 /////////// DATA ANALYSIS, SCALING AND TRANSLATION
1755 //////////////////////////////////////////////////////////
1758 * Analizes data and sets up internal maxima and minima
1759 * Needed by: CalcMargins(), ...
1760 * Text-Data is different than data-data graphs. For them what
1761 * we have, instead of X values, is # of records equally spaced on data.
1762 * text-data is passed in as $data[] = (title, y1, y2, y3, y4, ...)
1763 * data-data is passed in as $data[] = (title, x, y1, y2, y3, y4, ...)
1765 function FindDataLimits()
1767 // Set some default min and max values before running through the data
1768 switch ($this->data_type) {
1771 $maxx = $this->num_data_rows - 1 ;
1772 $miny = $this->data[0][1];
1775 default: //Everything else: data-data, etc, take first value
1776 $minx = $this->data[0][1];
1778 $miny = $this->data[0][2];
1783 $mine = 0; // Maximum value for the -error bar (assume error bars always > 0)
1784 $maxe = 0; // Maximum value for the +error bar (assume error bars always > 0)
1785 $maxt = 0; // Maximum number of characters in text labels
1789 // Process each row of data
1790 for ($i=0; $i < $this->num_data_rows; $i++) {
1792 // Extract maximum text label length
1793 $val = @ strlen($this->data[$i][$j++]);
1794 $maxt = ($val > $maxt) ? $val : $maxt;
1796 switch ($this->data_type) {
1797 case 'text-data': // Data is passed in as (title, y1, y2, y3, ...)
1798 case 'text-data-pie': // This one is for some pie charts, see DrawPieChart()
1799 // $numrecs = @ count($this->data[$i]);
1800 $miny = $maxy = (double)$this->data[$i][$j];
1801 for (; $j < $this->num_recs[$i]; $j++) {
1802 $val = (double)$this->data[$i][$j];
1803 $maxy = ($val > $maxy) ? $val : $maxy;
1804 $miny = ($val < $miny) ? $val : $miny;
1807 case 'data-data': // Data is passed in as (title, x, y, y2, y3, ...)
1809 $val = (double)$this->data[$i][$j++];
1810 $maxx = ($val > $maxx) ? $val : $maxx;
1811 $minx = ($val < $minx) ? $val : $minx;
1813 $miny = $maxy = (double)$this->data[$i][$j];
1814 // $numrecs = @ count($this->data[$i]);
1815 for (; $j < $this->num_recs[$i]; $j++) {
1816 $val = (double)$this->data[$i][$j];
1817 $maxy = ($val > $maxy) ? $val : $maxy;
1818 $miny = ($val < $miny) ? $val : $miny;
1821 case 'data-data-error': // Data is passed in as (title, x, y, err+, err-, y2, err2+, err2-,...)
1823 $val = (double)$this->data[$i][$j++];
1824 $maxx = ($val > $maxx) ? $val : $maxx;
1825 $minx = ($val < $minx) ? $val : $minx;
1827 $miny = $maxy = (double)$this->data[$i][$j];
1828 // $numrecs = @ count($this->data[$i]);
1829 for (; $j < $this->num_recs[$i];) {
1831 $val = (double)$this->data[$i][$j++];
1832 $maxy = ($val > $maxy) ? $val : $maxy;
1833 $miny = ($val < $miny) ? $val : $miny;
1835 $val = (double)$this->data[$i][$j++];
1836 $maxe = ($val > $maxe) ? $val : $maxe;
1838 $val = (double)$this->data[$i][$j++];
1839 $mine = ($val > $mine) ? $val : $mine;
1841 $maxy = $maxy + $maxe;
1842 $miny = $miny - $mine; // assume error bars are always > 0
1845 $this->PrintError("FindDataLimits(): Unknown data type '$data_type'.");
1848 $this->data[$i][MINY] = $miny; // This row's min Y, for DrawXDataLine()
1849 $this->data[$i][MAXY] = $maxy; // This row's max Y, for DrawXDataLine()
1850 $minminy = ($miny < $minminy) ? $miny : $minminy; // global min
1851 $maxmaxy = ($maxy > $maxmaxy) ? $maxy : $maxmaxy; // global max
1854 $this->min_x = $minx;
1855 $this->max_x = $maxx;
1856 $this->min_y = $minminy;
1857 $this->max_y = $maxmaxy;
1858 $this->max_t = $maxt;
1860 $this->data_limits_done = TRUE;
1867 * Calculates image margins on the fly from title positions and sizes,
1868 * and tick labels positions and sizes.
1870 * FIXME: fix x_data_label_pos behaviour. Now we are leaving room for it AND x_tick_label_pos
1871 * maybe it shouldn't be so...
1873 * TODO: add x_tick_label_width and y_tick_label_height and use them to calculate
1874 * max_x_labels and max_y_labels, to be used by drawing functions t avoid overlapping.
1876 function CalcMargins()
1878 // Temporary variables for label size calculation
1879 $xlab = $this->FormatLabel('x', $this->max_x);
1880 $ylab = $this->FormatLabel('y', $this->max_y);
1882 //////// Calculate maximum X/Y axis label height and width:
1885 if ($this->use_ttf) {
1886 // Maximum X axis label height
1887 $size = $this->TTFBBoxSize($this->x_label_font['size'], $this->x_label_angle,
1888 $this->x_label_font['font'], $xlab);
1889 $this->x_tick_label_height = $size[1];
1891 // Maximum Y axis label width
1892 $size = $this->TTFBBoxSize($this->y_label_font['size'], $this->y_label_angle,
1893 $this->y_label_font['font'], $ylab);
1894 $this->y_tick_label_width = $size[0];
1898 // Maximum X axis label height
1899 if ($this->x_label_angle == 90)
1900 $this->x_tick_label_height = strlen($xlab) * $this->x_label_font['width'];
1902 $this->x_tick_label_height = $this->x_label_font['height'];
1904 // Maximum Y axis label width
1905 $this->y_tick_label_width = strlen($ylab) * $this->y_label_font['width'];
1909 ///////// Calculate margins:
1911 // Upper title, ticks and tick labels, and data labels:
1912 $this->y_top_margin = $this->title_height + $this->safe_margin * 2;
1914 if ($this->x_title_pos == 'plotup' || $this->x_title_pos == 'both')
1915 $this->y_top_margin += $this->x_title_height + $this->safe_margin;
1917 if ($this->x_tick_label_pos == 'plotup' || $this->x_tick_label_pos == 'both')
1918 $this->y_top_margin += $this->x_tick_label_height;
1920 if ($this->x_tick_pos == 'plotup' || $this->x_tick_pos == 'both')
1921 $this->y_top_margin += $this->x_tick_length * 2;
1923 if ($this->x_data_label_pos == 'plotup' || $this->x_data_label_pos == 'both')
1924 $this->y_top_margin += $this->x_tick_label_height;
1926 // Lower title, ticks and tick labels, and data labels:
1927 $this->y_bot_margin = $this->safe_margin * 2;
1929 if ($this->x_title_pos == 'plotdown' || $this->x_title_pos == 'both')
1930 $this->y_bot_margin += $this->x_title_height;
1932 if ($this->x_tick_pos == 'plotdown' || $this->x_tick_pos == 'both')
1933 $this->y_bot_margin += $this->x_tick_length * 2;
1935 if ($this->x_tick_pos == 'xaxis' && ($this->x_axis_position == '' || $this->x_axis_position == 0))
1936 $this->y_bot_margin += $this->x_tick_length * 2;
1938 if ($this->x_tick_label_pos == 'plotdown' || $this->x_tick_label_pos == 'both')
1939 $this->y_bot_margin += $this->x_tick_label_height;
1941 if ($this->x_tick_label_pos == 'xaxis' && ($this->x_axis_position == '' || $this->x_axis_position == 0))
1942 $this->y_bot_margin += $this->x_tick_label_height;
1944 if ($this->x_data_label_pos == 'plotdown' || $this->x_data_label_pos == 'both')
1945 $this->y_bot_margin += $this->x_tick_label_height;
1947 // Left title, ticks and tick labels:
1948 $this->x_left_margin = $this->safe_margin * 2;
1950 if ($this->y_title_pos == 'plotleft' || $this->y_title_pos == 'both')
1951 $this->x_left_margin += $this->y_title_width + $this->safe_margin;
1953 if ($this->y_tick_label_pos == 'plotleft' || $this->y_tick_label_pos == 'both')
1954 $this->x_left_margin += $this->y_tick_label_width;
1956 if ($this->y_tick_pos == 'plotleft' || $this->y_tick_pos == 'both')
1957 $this->x_left_margin += $this->y_tick_length * 2 ;
1959 // Right title, ticks and tick labels:
1960 $this->x_right_margin = $this->safe_margin * 2;
1962 if ($this->y_title_pos == 'plotright' || $this->y_title_pos == 'both')
1963 $this->x_right_margin += $this->y_title_width + $this->safe_margin;
1965 if ($this->y_tick_label_pos == 'plotright' || $this->y_tick_label_pos == 'both')
1966 $this->x_right_margin += $this->y_tick_label_width;
1968 if ($this->y_tick_pos == 'plotright' || $this->y_tick_pos == 'both')
1969 $this->x_right_margin += $this->y_tick_length * 2;
1972 $this->x_tot_margin = $this->x_left_margin + $this->x_right_margin;
1973 $this->y_tot_margin = $this->y_top_margin + $this->y_bot_margin;
1980 * Set the margins in pixels (left, right, top, bottom)
1982 function SetMarginsPixels($which_lm, $which_rm, $which_tm, $which_bm)
1985 $this->x_left_margin = $which_lm;
1986 $this->x_right_margin = $which_rm;
1987 $this->x_tot_margin = $which_lm + $which_rm;
1989 $this->y_top_margin = $which_tm;
1990 $this->y_bot_margin = $which_bm;
1991 $this->y_tot_margin = $which_tm + $which_bm;
1993 $this->SetPlotAreaPixels();
2000 * Sets the limits for the plot area. If no arguments are supplied, uses
2001 * values calculated from _CalcMargins();
2002 * Like in GD, (0,0) is upper left
2004 * This resets the scale if SetPlotAreaWorld() was already called
2006 function SetPlotAreaPixels($x1=NULL, $y1=NULL, $x2=NULL, $y2=NULL)
2009 $this->plot_area = array($x1, $y1, $x2, $y2);
2011 if (! isset($this->x_tot_margin))
2012 $this->CalcMargins();
2014 $this->plot_area = array($this->x_left_margin, $this->y_top_margin,
2015 $this->image_width - $this->x_right_margin,
2016 $this->image_height - $this->y_bot_margin);
2018 $this->plot_area_width = $this->plot_area[2] - $this->plot_area[0];
2019 $this->plot_area_height = $this->plot_area[3] - $this->plot_area[1];
2021 // Reset the scale with the new plot area.
2022 if (isset($this->plot_max_x))
2023 $this->CalcTranslation();
2031 * Sets minimum and maximum x and y values in the plot using FindDataLimits()
2032 * or from the supplied parameters, if any.
2034 * This resets the scale if SetPlotAreaPixels() was already called
2036 function SetPlotAreaWorld($xmin=NULL, $ymin=NULL, $xmax=NULL, $ymax=NULL)
2038 if ((! $xmin) && (! $xmax) ) {
2039 // For automatic setting of data we need data limits
2040 if (! isset($this->data_limits_done)) {
2041 $this->FindDataLimits() ;
2043 if ($this->data_type == 'text-data') {
2044 $xmax = $this->max_x + 1 ; // valid for BAR CHART TYPE GRAPHS ONLY
2045 $xmin = 0 ; // valid for BAR CHART TYPE GRAPHS ONLY
2047 $xmax = $this->max_x;
2048 $xmin = $this->min_x;
2051 $ymax = ceil($this->max_y * 1.1);
2052 if ($this->min_y < 0) {
2053 $ymin = floor($this->min_y * 1.1);
2059 $this->plot_min_x = $xmin;
2060 $this->plot_max_x = $xmax;
2062 if ($ymin == $ymax) {
2065 if ($this->yscale_type == 'log') {
2066 //extra error checking
2071 $this->PrintError('SetPlotAreaWorld(): Log plots need data greater than 0');
2076 $this->plot_min_y = $ymin;
2077 $this->plot_max_y = $ymax;
2079 if ($ymax <= $ymin) {
2080 $this->DrawError('SetPlotAreaWorld(): Error in data - max not greater than min');
2084 // Reset the scale with the new maxs and mins
2085 if (isset($this->plot_area_width)) {
2086 $this->CalcTranslation();
2090 } //function SetPlotAreaWorld
2094 * For plots that have equally spaced x variables and multiple bars per x-point.
2096 function SetEqualXCoord()
2098 $space = ($this->plot_area[2] - $this->plot_area[0]) /
2099 ($this->num_data_rows * 2) * $this->group_frac_width;
2100 $group_width = $space * 2;
2101 $bar_width = $group_width / $this->records_per_group;
2102 //I think that eventually this space variable will be replaced by just graphing x.
2103 $this->data_group_space = $space;
2104 $this->record_bar_width = $bar_width;
2109 * Calculates scaling stuff...
2111 function CalcTranslation()
2113 if ($this->xscale_type == 'log') {
2114 $this->xscale = ($this->plot_area_width)/(log10($this->plot_max_x) - log10($this->plot_min_x));
2116 $this->xscale = ($this->plot_area_width)/($this->plot_max_x - $this->plot_min_x);
2118 if ($this->yscale_type == 'log') {
2119 $this->yscale = ($this->plot_area_height)/(log10($this->plot_max_y) - log10($this->plot_min_y));
2121 $this->yscale = ($this->plot_area_height)/($this->plot_max_y - $this->plot_min_y);
2124 // GD defines x = 0 at left and y = 0 at TOP so -/+ respectively
2125 if ($this->xscale_type == 'log') {
2126 $this->plot_origin_x = $this->plot_area[0] - ($this->xscale * log10($this->plot_min_x) );
2128 $this->plot_origin_x = $this->plot_area[0] - ($this->xscale * $this->plot_min_x);
2130 if ($this->yscale_type == 'log') {
2131 $this->plot_origin_y = $this->plot_area[3] + ($this->yscale * log10($this->plot_min_y));
2133 $this->plot_origin_y = $this->plot_area[3] + ($this->yscale * $this->plot_min_y);
2136 $this->scale_is_set = TRUE;
2138 /************** FIXME?? *************/
2139 // There should be a better place for this.
2141 // User provided y axis position?
2142 if ($this->y_axis_position != '') {
2143 // Make sure we draw our axis inside the plot
2144 $this->y_axis_position = ($this->y_axis_position < $this->plot_min_x)
2145 ? $this->plot_min_x : $this->y_axis_position;
2146 $this->y_axis_position = ($this->y_axis_position > $this->plot_max_x)
2147 ? $this->plot_max_x : $this->y_axis_position;
2148 $this->y_axis_x_pixels = $this->xtr($this->y_axis_position);
2150 // Default to left axis
2151 $this->y_axis_x_pixels = $this->xtr($this->plot_min_x);
2153 // User provided x axis position?
2154 if ($this->x_axis_position != '') {
2155 // Make sure we draw our axis inside the plot
2156 $this->x_axis_position = ($this->x_axis_position < $this->plot_min_y)
2157 ? $this->plot_min_y : $this->x_axis_position;
2158 $this->x_axis_position = ($this->x_axis_position > $this->plot_max_y)
2159 ? $this->plot_max_y : $this->x_axis_position;
2160 $this->x_axis_y_pixels = $this->ytr($this->x_axis_position);
2162 if ($this->yscale_type == 'log')
2163 $this->x_axis_y_pixels = $this->ytr(1);
2165 // Default to axis at 0 or plot_min_y (should be 0 anyway, from SetPlotAreaWorld())
2166 $this->x_axis_y_pixels = ($this->plot_min_y <= 0) && (0 <= $this->plot_max_y)
2167 ? $this->ytr(0) : $this->ytr($this->plot_min_y);
2170 } // function CalcTranslation()
2174 * Translate X world coordinate into pixel coordinate
2175 * Needs values calculated by _CalcTranslation()
2177 function xtr($x_world)
2179 //$x_pixels = $this->x_left_margin + ($this->image_width - $this->x_tot_margin)*
2180 // (($x_world - $this->plot_min_x) / ($this->plot_max_x - $this->plot_min_x)) ;
2181 //which with a little bit of math reduces to ...
2182 if ($this->xscale_type == 'log') {
2183 $x_pixels = $this->plot_origin_x + log10($x_world) * $this->xscale ;
2185 $x_pixels = $this->plot_origin_x + $x_world * $this->xscale ;
2187 return round($x_pixels);
2192 * Translate Y world coordinate into pixel coordinate.
2193 * Needs values calculated by _CalcTranslation()
2195 function ytr($y_world)
2197 if ($this->yscale_type == 'log') {
2198 //minus because GD defines y = 0 at top. doh!
2199 $y_pixels = $this->plot_origin_y - log10($y_world) * $this->yscale ;
2201 $y_pixels = $this->plot_origin_y - $y_world * $this->yscale ;
2203 return round($y_pixels);
2207 * Formats a tick or data label.
2209 * \note Time formatting suggested by Marlin Viss
2211 function FormatLabel($which_pos, $which_lab)
2213 switch ($which_pos) {
2216 switch ($this->x_label_type) {
2218 $lab = $this->data[$which_lab][0];
2221 $lab = number_format($which_lab, $this->x_precision, '.', ', ').$this->data_units_text;
2224 $lab = strftime($this->x_time_format, $which_lab);
2227 // Unchanged from whatever format it is passed in
2234 switch ($this->y_label_type) {
2236 $lab = number_format($which_lab, $this->y_precision, '.', ', ').$this->data_units_text;
2239 $lab = strftime($this->y_time_format, $which_lab);
2242 // Unchanged from whatever format it is passed in
2248 $this->PrintError("FormatLabel(): Unknown label type $which_type");
2253 } //function FormatLabel
2257 /////////////////////////////////////////////
2258 /////////////// TICKS
2259 /////////////////////////////////////////////
2262 * Use either this or SetNumXTicks() to set where to place x tick marks
2264 function SetXTickIncrement($which_ti=NULL)
2267 $this->x_tick_increment = $which_ti; //world coordinates
2269 if (! isset($this->data_limits_done)) {
2270 $this->FindDataLimits(); //Get maxima and minima for scaling
2272 //$this->x_tick_increment = ( ceil($this->max_x * 1.2) - floor($this->min_x * 1.2) )/10;
2273 $this->x_tick_increment = ($this->plot_max_x - $this->plot_min_x )/10;
2275 $this->num_x_ticks = ''; //either use num_y_ticks or y_tick_increment, not both
2280 * Use either this or SetNumYTicks() to set where to place y tick marks
2282 function SetYTickIncrement($which_ti=NULL)
2285 $this->y_tick_increment = $which_ti; //world coordinates
2287 if (! isset($this->data_limits_done)) {
2288 $this->FindDataLimits(); //Get maxima and minima for scaling
2290 if (! isset($this->plot_max_y))
2291 $this->SetPlotAreaWorld();
2293 //$this->y_tick_increment = ( ceil($this->max_y * 1.2) - floor($this->min_y * 1.2) )/10;
2294 $this->y_tick_increment = ($this->plot_max_y - $this->plot_min_y )/10;
2296 $this->num_y_ticks = ''; //either use num_y_ticks or y_tick_increment, not both
2301 function SetNumXTicks($which_nt)
2303 $this->num_x_ticks = $which_nt;
2304 $this->x_tick_increment = ''; //either use num_x_ticks or x_tick_increment, not both
2308 function SetNumYTicks($which_nt)
2310 $this->num_y_ticks = $which_nt;
2311 $this->y_tick_increment = ''; //either use num_y_ticks or y_tick_increment, not both
2318 function SetYTickPos($which_tp)
2320 $this->y_tick_pos = $this->CheckOption($which_tp, 'plotleft, plotright, both, yaxis, none', __FUNCTION__);
2326 function SetXTickPos($which_tp)
2328 $this->x_tick_pos = $this->CheckOption($which_tp, 'plotdown, plotup, both, xaxis, none', __FUNCTION__);
2335 function SetSkipTopTick($skip)
2337 $this->skip_top_tick = (bool)$skip;
2344 function SetSkipBottomTick($skip)
2346 $this->skip_bottom_tick = (bool)$skip;
2350 function SetXTickLength($which_xln)
2352 $this->x_tick_length = $which_xln;
2356 function SetYTickLength($which_yln)
2358 $this->y_tick_length = $which_yln;
2362 function SetXTickCrossing($which_xc)
2364 $this->x_tick_cross = $which_xc;
2368 function SetYTickCrossing($which_yc)
2370 $this->y_tick_cross = $which_yc;
2375 /////////////////////////////////////////////
2376 //////////////////// GENERIC DRAWING
2377 /////////////////////////////////////////////
2380 * Fills the background of the image with a solid color
2382 function DrawBackground()
2384 if (! $this->background_done) { //Don't draw it twice if drawing two plots on one image
2385 ImageFilledRectangle($this->img, 0, 0, $this->image_width, $this->image_height,
2386 $this->ndx_bg_color);
2387 $this->background_done = TRUE;
2393 * Draws a border around the final image.
2395 function DrawImageBorder()
2397 switch ($this->image_border_type) {
2399 ImageLine($this->img, 0, 0, $this->image_width-1, 0, $this->ndx_i_border);
2400 ImageLine($this->img, 1, 1, $this->image_width-2, 1, $this->ndx_i_border);
2401 ImageLine($this->img, 0, 0, 0, $this->image_height-1, $this->ndx_i_border);
2402 ImageLine($this->img, 1, 1, 1, $this->image_height-2, $this->ndx_i_border);
2403 ImageLine($this->img, $this->image_width-1, 0, $this->image_width-1,
2404 $this->image_height-1, $this->ndx_i_border_dark);
2405 ImageLine($this->img, 0, $this->image_height-1, $this->image_width-1,
2406 $this->image_height-1, $this->ndx_i_border_dark);
2407 ImageLine($this->img, $this->image_width-2, 1, $this->image_width-2,
2408 $this->image_height-2, $this->ndx_i_border_dark);
2409 ImageLine($this->img, 1, $this->image_height-2, $this->image_width-2,
2410 $this->image_height-2, $this->ndx_i_border_dark);
2413 ImageLine($this->img, 0, 0, $this->image_width, 0, $this->ndx_i_border_dark);
2414 ImageLine($this->img, $this->image_width-1, 0, $this->image_width-1,
2415 $this->image_height, $this->ndx_i_border_dark);
2416 ImageLine($this->img, $this->image_width-1, $this->image_height-1, 0, $this->image_height-1,
2417 $this->ndx_i_border_dark);
2418 ImageLine($this->img, 0, 0, 0, $this->image_height, $this->ndx_i_border_dark);
2423 $this->DrawError("DrawImageBorder(): unknown image_border_type: '$this->image_border_type'");
2431 * Adds the title to the graph.
2433 function DrawTitle()
2435 // Center of the plot area
2436 //$xpos = ($this->plot_area[0] + $this->plot_area_width )/ 2;
2438 // Center of the image:
2439 $xpos = $this->image_width / 2;
2441 // Place it at almost at the top
2442 $ypos = $this->safe_margin;
2444 $this->DrawText($this->title_font, $this->title_angle, $xpos, $ypos,
2445 $this->ndx_title_color, $this->title_txt, 'center', 'bottom');
2453 * Draws the X-Axis Title
2455 function DrawXTitle()
2457 if ($this->x_title_pos == 'none')
2460 // Center of the plot
2461 $xpos = ($this->plot_area[2] + $this->plot_area[0]) / 2;
2464 if ($this->x_title_pos == 'plotup' || $this->x_title_pos == 'both') {
2465 $ypos = $this->safe_margin + $this->title_height + $this->safe_margin;
2466 $this->DrawText($this->x_title_font, $this->x_title_angle,
2467 $xpos, $ypos, $this->ndx_title_color, $this->x_title_txt, 'center');
2470 if ($this->x_title_pos == 'plotdown' || $this->x_title_pos == 'both') {
2471 $ypos = $this->image_height - $this->x_title_height - $this->safe_margin;
2472 $this->DrawText($this->x_title_font, $this->x_title_angle,
2473 $xpos, $ypos, $this->ndx_title_color, $this->x_title_txt, 'center');
2479 * Draws the Y-Axis Title
2481 function DrawYTitle()
2483 if ($this->y_title_pos == 'none')
2486 // Center the title vertically to the plot
2487 $ypos = ($this->plot_area[3] + $this->plot_area[1]) / 2;
2489 if ($this->y_title_pos == 'plotleft' || $this->y_title_pos == 'both') {
2490 $xpos = $this->safe_margin;
2491 $this->DrawText($this->y_title_font, 90, $xpos, $ypos, $this->ndx_title_color,
2492 $this->y_title_txt, 'left', 'center');
2494 if ($this->y_title_pos == 'plotright' || $this->y_title_pos == 'both') {
2495 $xpos = $this->image_width - $this->safe_margin - $this->y_title_width - $this->safe_margin;
2496 $this->DrawText($this->y_title_font, 90, $xpos, $ypos, $this->ndx_title_color,
2497 $this->y_title_txt, 'left', 'center');
2505 * Fills the plot area with a solid color
2507 function DrawPlotAreaBackground()
2509 ImageFilledRectangle($this->img, $this->plot_area[0], $this->plot_area[1],
2510 $this->plot_area[2], $this->plot_area[3],
2511 $this->ndx_plot_bg_color);
2516 * \note Horizontal grid lines overwrite horizontal axis with y=0, so call this first, then DrawXAxis()
2518 function DrawYAxis()
2520 // Draw ticks, labels and grid, if any
2521 $this->DrawYTicks();
2523 // Draw Y axis at X = y_axis_x_pixels
2524 ImageLine($this->img, $this->y_axis_x_pixels, $this->plot_area[1],
2525 $this->y_axis_x_pixels, $this->plot_area[3], $this->ndx_grid_color);
2533 function DrawXAxis()
2535 // Draw ticks, labels and grid
2536 $this->DrawXTicks();
2538 //Draw Tick and Label for Y axis
2539 if (! $this->skip_bottom_tick) {
2540 $ylab =$this->FormatLabel('y', $this->x_axis_position);
2541 $this->DrawYTick($ylab, $this->x_axis_y_pixels);
2544 //Draw X Axis at Y = x_axis_y_pixels
2545 ImageLine($this->img, $this->plot_area[0]+1, $this->x_axis_y_pixels,
2546 $this->plot_area[2]-1, $this->x_axis_y_pixels, $this->ndx_grid_color);
2552 * Draw Just one Tick, called from DrawYTicks() and DrawXAxis()
2553 * TODO? Move this inside DrawYTicks() and Modify DrawXAxis() ?
2555 function DrawYTick($which_ylab, $which_ypix)
2558 if ($this->y_tick_pos == 'yaxis') {
2559 ImageLine($this->img, $this->y_axis_x_pixels - $this->y_tick_length, $which_ypix,
2560 $this->y_axis_x_pixels + $this->y_tick_cross, $which_ypix,
2561 $this->ndx_tick_color);
2565 if ($this->y_tick_label_pos == 'yaxis') {
2566 $this->DrawText($this->y_label_font, $this->y_label_angle,
2567 $this->y_axis_x_pixels - $this->y_tick_length * 1.5, $which_ypix,
2568 $this->ndx_text_color, $which_ylab, 'right', 'center');
2571 // Ticks to the left of the Plot Area
2572 if (($this->y_tick_pos == 'plotleft') || ($this->y_tick_pos == 'both') ) {
2573 ImageLine($this->img, $this->plot_area[0] - $this->y_tick_length,
2574 $which_ypix, $this->plot_area[0] + $this->y_tick_cross,
2575 $which_ypix, $this->ndx_tick_color);
2578 // Ticks to the right of the Plot Area
2579 if (($this->y_tick_pos == 'plotright') || ($this->y_tick_pos == 'both') ) {
2580 ImageLine($this->img, ($this->plot_area[2] + $this->y_tick_length),
2581 $which_ypix, $this->plot_area[2] - $this->y_tick_cross,
2582 $which_ypix, $this->ndx_tick_color);
2585 // Labels to the left of the plot area
2586 if ($this->y_tick_label_pos == 'plotleft' || $this->y_tick_label_pos == 'both') {
2587 $this->DrawText($this->y_label_font, $this->y_label_angle,
2588 $this->plot_area[0] - $this->y_tick_length * 1.5, $which_ypix,
2589 $this->ndx_text_color, $which_ylab, 'right', 'center');
2591 // Labels to the right of the plot area
2592 if ($this->y_tick_label_pos == 'plotright' || $this->y_tick_label_pos == 'both') {
2593 $this->DrawText($this->y_label_font, $this->y_label_angle,
2594 $this->plot_area[2] + $this->y_tick_length * 1.5, $which_ypix,
2595 $this->ndx_text_color, $which_ylab, 'left', 'center');
2597 } // Function DrawYTick()
2601 * Draws Grid, Ticks and Tick Labels along Y-Axis
2602 * Ticks and ticklabels can be left of plot only, right of plot only,
2603 * both on the left and right of plot, or crossing a user defined Y-axis
2605 function DrawYTicks()
2607 // Sets the line style for IMG_COLOR_STYLED lines (grid)
2608 if ($this->dashed_grid) {
2609 $this->SetDashedStyle($this->ndx_light_grid_color);
2610 $style = IMG_COLOR_STYLED;
2612 $style = $this->ndx_light_grid_color;
2615 // maxy is always > miny so delta_y is always positive
2616 if ($this->y_tick_increment) {
2617 $delta_y = $this->y_tick_increment;
2618 } elseif ($this->num_y_ticks) {
2619 $delta_y = ($this->plot_max_y - $this->plot_min_y) / $this->num_y_ticks;
2621 $delta_y = ($this->plot_max_y - $this->plot_min_y) / 10 ;
2624 // NOTE: When working with floats, because of approximations when adding $delta_y,
2625 // $y_tmp never equals $y_end at the for loop, so one spurious line would get drawn where
2626 // not for the substraction to $y_end here.
2627 $y_tmp = (double)$this->plot_min_y;
2628 $y_end = (double)$this->plot_max_y - ($delta_y/2);
2630 if ($this->skip_bottom_tick)
2633 if ($this->skip_top_tick)
2636 for (;$y_tmp < $y_end; $y_tmp += $delta_y) {
2637 $ylab = $this->FormatLabel('y', $y_tmp);
2638 $y_pixels = $this->ytr($y_tmp);
2640 // Horizontal grid line
2641 if ($this->draw_y_grid) {
2642 ImageLine($this->img, $this->plot_area[0]+1, $y_pixels, $this->plot_area[2]-1, $y_pixels, $style);
2646 $this->DrawYTick($ylab, $y_pixels);
2649 } // function DrawYTicks
2653 * Draws Grid, Ticks and Tick Labels along X-Axis
2654 * Ticks and tick labels can be down of plot only, up of plot only,
2655 * both on up and down of plot, or crossing a user defined X-axis
2657 * \note Original vertical code submitted by Marlin Viss
2659 function DrawXTicks()
2661 // Sets the line style for IMG_COLOR_STYLED lines (grid)
2662 if ($this->dashed_grid) {
2663 $this->SetDashedStyle($this->ndx_light_grid_color);
2664 $style = IMG_COLOR_STYLED;
2666 $style = $this->ndx_light_grid_color;
2669 // Calculate x increment between ticks
2670 if ($this->x_tick_increment) {
2671 $delta_x = $this->x_tick_increment;
2672 } elseif ($this->num_x_ticks) {
2673 $delta_x = ($this->plot_max_x - $this->plot_min_x) / $this->num_x_ticks;
2675 $delta_x =($this->plot_max_x - $this->plot_min_x) / 10 ;
2678 // NOTE: When working with decimals, because of approximations when adding $delta_x,
2679 // $x_tmp never equals $x_end at the for loop, so one spurious line would get drawn where
2680 // not for the substraction to $x_end here.
2681 $x_tmp = (double)$this->plot_min_x;
2682 $x_end = (double)$this->plot_max_x - ($delta_x/2);
2684 // Should the leftmost tick be drawn?
2685 if ($this->skip_left_tick)
2688 // And the rightmost?
2689 if (! $this->skip_right_tick)
2692 for (;$x_tmp < $x_end; $x_tmp += $delta_x) {
2693 $xlab = $this->FormatLabel('x', $x_tmp);
2694 $x_pixels = $this->xtr($x_tmp);
2696 // Vertical grid lines
2697 if ($this->draw_x_grid) {
2698 ImageLine($this->img, $x_pixels, $this->plot_area[1], $x_pixels, $this->plot_area[3], $style);
2702 if ($this->x_tick_pos == 'xaxis') {
2703 ImageLine($this->img, $x_pixels, $this->x_axis_y_pixels - $this->x_tick_cross,
2704 $x_pixels, $this->x_axis_y_pixels + $this->x_tick_length, $this->ndx_tick_color);
2708 if ($this->x_tick_label_pos == 'xaxis') {
2709 $this->DrawText($this->x_label_font, $this->x_label_angle, $x_pixels,
2710 $this->x_axis_y_pixels + $this->x_tick_length*1.5, $this->ndx_text_color,
2711 $xlab, 'center', 'bottom');
2714 // Top of the plot area tick
2715 if ($this->x_tick_pos == 'plotup' || $this->x_tick_pos == 'both') {
2716 ImageLine($this->img, $x_pixels, $this->plot_area[1] - $this->x_tick_length,
2717 $x_pixels, $this->plot_area[1] + $this->x_tick_cross, $this->ndx_tick_color);
2719 // Bottom of the plot area tick
2720 if ($this->x_tick_pos == 'plotdown' || $this->x_tick_pos == 'both') {
2721 ImageLine($this->img, $x_pixels, $this->plot_area[3] + $this->x_tick_length,
2722 $x_pixels, $this->plot_area[3] - $this->x_tick_cross, $this->ndx_tick_color);
2725 // Top of the plot area tick label
2726 if ($this->x_tick_label_pos == 'plotup' || $this->x_tick_label_pos == 'both') {
2727 $this->DrawText($this->x_label_font, $this->x_label_angle, $x_pixels,
2728 $this->plot_area[1] - $this->x_tick_length*1.5, $this->ndx_text_color,
2729 $xlab, 'center', 'top');
2732 // Bottom of the plot area tick label
2733 if ($this->x_tick_label_pos == 'plotdown' || $this->x_tick_label_pos == 'both') {
2734 $this->DrawText($this->x_label_font, $this->x_label_angle, $x_pixels,
2735 $this->plot_area[3] + $this->x_tick_length*1.5, $this->ndx_text_color,
2736 $xlab, 'center', 'bottom');
2740 } // function DrawXTicks
2746 function DrawPlotBorder()
2748 switch ($this->plot_border_type) {
2749 case 'left': // for past compatibility
2751 ImageLine($this->img, $this->plot_area[0], $this->ytr($this->plot_min_y),
2752 $this->plot_area[0], $this->ytr($this->plot_max_y), $this->ndx_grid_color);
2756 ImageLine($this->img, $this->plot_area[2], $this->ytr($this->plot_min_y),
2757 $this->plot_area[2], $this->ytr($this->plot_max_y), $this->ndx_grid_color);
2761 ImageLine($this->img, $this->plot_area[0], $this->ytr($this->plot_min_y),
2762 $this->plot_area[0], $this->ytr($this->plot_max_y), $this->ndx_grid_color);
2763 ImageLine($this->img, $this->plot_area[2], $this->ytr($this->plot_min_y),
2764 $this->plot_area[2], $this->ytr($this->plot_max_y), $this->ndx_grid_color);
2771 ImageRectangle($this->img, $this->plot_area[0], $this->ytr($this->plot_min_y),
2772 $this->plot_area[2], $this->ytr($this->plot_max_y), $this->ndx_grid_color);
2780 * Draws the data label associated with a point in the plot.
2781 * This is different from x_labels drawn by DrawXTicks() and care
2782 * should be taken not to draw both, as they'd probably overlap.
2783 * Calling of this function in DrawLines(), etc is decided after x_data_label_pos value.
2784 * Leave the last parameter out, to avoid the drawing of vertical lines, no matter
2785 * what the setting is (for plots that need it, like DrawSquared())
2787 function DrawXDataLabel($xlab, $xpos, $row=FALSE)
2789 $xlab = $this->FormatLabel('x', $xlab);
2791 // Labels below the plot area
2792 if ($this->x_data_label_pos == 'plotdown' || $this->x_data_label_pos == 'both')
2793 $this->DrawText($this->x_label_font, $this->x_label_angle, $xpos,
2794 $this->plot_area[3] + $this->x_tick_length,
2795 $this->ndx_text_color, $xlab, 'center', 'bottom');
2797 // Labels above the plot area
2798 if ($this->x_data_label_pos == 'plotup' || $this->x_data_label_pos == 'both')
2799 $this->DrawText($this->x_label_font, $this->x_label_angle, $xpos,
2800 $this->plot_area[1] - $this->x_tick_length ,
2801 $this->ndx_text_color, $xlab, 'center', 'top');
2803 if ($row && $this->draw_x_data_label_lines)
2804 $this->DrawXDataLine($xpos, $row);
2808 * Draws Vertical lines from data points up and down.
2809 * Which lines are drawn depends on the value of x_data_label_pos,
2810 * and whether this is at all done or not, on draw_x_data_label_lines
2812 * \param xpos int position in pixels of the line.
2813 * \param row int index of the data row being drawn.
2815 function DrawXDataLine($xpos, $row)
2817 // Sets the line style for IMG_COLOR_STYLED lines (grid)
2818 if($this->dashed_grid) {
2819 $this->SetDashedStyle($this->ndx_light_grid_color);
2820 $style = IMG_COLOR_STYLED;
2822 $style = $this->ndx_light_grid_color;
2825 // Lines from the bottom up
2826 if ($this->x_data_label_pos == 'both') {
2827 ImageLine($this->img, $xpos, $this->plot_area[3], $xpos, $this->plot_area[1], $style);
2829 // Lines coming from the bottom of the plot
2830 else if ($this->x_data_label_pos == 'plotdown') {
2831 // See FindDataLimits() to see why 'MAXY' index.
2832 $ypos = $this->ytr($this->data[$row][MAXY]);
2833 ImageLine($this->img, $xpos, $ypos, $xpos, $this->plot_area[3], $style);
2835 // Lines coming from the top of the plot
2836 else if ($this->x_data_label_pos == 'plotup') {
2837 // See FindDataLimits() to see why 'MINY' index.
2838 $ypos = $this->ytr($this->data[$row][MINY]);
2839 ImageLine($this->img, $xpos, $this->plot_area[1], $xpos, $ypos, $style);
2844 function DrawPlotLabel($xlab, $xpos, $ypos)
2846 $this->DrawText($this->x_label_font, $this->x_label_angle, $xpos, $this
2850 * Draws the graph legend
2852 * \note Base code submitted by Marlin Viss
2853 * FIXME: maximum label length should be calculated more accurately for TT fonts
2854 * Performing a BBox calculation for every legend element, for example.
2856 function DrawLegend($which_x1, $which_y1, $which_boxtype)
2858 // Find maximum legend label length
2860 foreach ($this->legend as $leg) {
2861 $len = strlen($leg);
2862 $max_len = ($len > $max_len) ? $len : $max_len;
2864 $max_len += 5; // Leave room for the boxes and margins
2866 /////// Calculate legend labels sizes: FIXME - dirty hack - FIXME
2868 if ($this->use_ttf) {
2869 $size = $this->TTFBBoxSize($this->legend_font['size'], 0,
2870 $this->legend_font['font'], '_');
2873 $size = $this->TTFBBoxSize($this->legend_font['size'], 0,
2874 $this->legend_font['font'], '|');
2879 $char_w = $this->legend_font['width'];
2880 $char_h = $this->legend_font['height'];
2883 $v_margin = $char_h/2; // Between vertical borders and labels
2884 $dot_height = $char_h + $this->line_spacing; // Height of the small colored boxes
2885 $width = $char_w * $max_len;
2887 //////// Calculate box size
2889 if ( (! $which_x1) || (! $which_y1) ) {
2890 $box_start_x = $this->plot_area[2] - $width;
2891 $box_start_y = $this->plot_area[1] + 5;
2893 $box_start_x = $which_x1;
2894 $box_start_y = $which_y1;
2897 // Lower right corner
2898 $box_end_y = $box_start_y + $dot_height*(count($this->legend)) + 2*$v_margin;
2899 $box_end_x = $box_start_x + $width - 5;
2903 ImageFilledRectangle($this->img, $box_start_x, $box_start_y, $box_end_x, $box_end_y, $this->ndx_bg_color);
2904 ImageRectangle($this->img, $box_start_x, $box_start_y, $box_end_x, $box_end_y, $this->ndx_grid_color);
2907 $max_color_index = count($this->ndx_data_colors) - 1;
2909 $dot_left_x = $box_end_x - $char_w * 2;
2910 $dot_right_x = $box_end_x - $char_w;
2911 $y_pos = $box_start_y + $v_margin;
2913 foreach ($this->legend as $leg) {
2914 // Text right aligned to the little box
2915 $this->DrawText($this->legend_font, 0, $dot_left_x - $char_w, $y_pos,
2916 $this->ndx_text_color, $leg, 'right');
2917 // Draw a box in the data color
2918 ImageFilledRectangle($this->img, $dot_left_x, $y_pos + 1, $dot_right_x,
2919 $y_pos + $dot_height-1, $this->ndx_data_colors[$color_index]);
2920 // Draw a rectangle around the box
2921 ImageRectangle($this->img, $dot_left_x, $y_pos + 1, $dot_right_x,
2922 $y_pos + $dot_height-1, $this->ndx_text_color);
2924 $y_pos += $char_h + $this->line_spacing;
2927 if ($color_index > $max_color_index)
2930 } // Function DrawLegend()
2934 * TODO Draws a legend over (or below) an axis of the plot.
2936 function DrawAxisLegend()
2938 // Calculate available room
2939 // Calculate length of all items (boxes included)
2940 // Calculate number of lines and room it would take. FIXME: this should be known in CalcMargins()
2944 /////////////////////////////////////////////
2945 //////////////////// PLOT DRAWING
2946 /////////////////////////////////////////////
2950 * Draws a pie chart. Data has to be 'text-data' type.
2952 * This can work in two ways: the classical, with a column for each sector
2953 * (computes the column totals and draws the pie with that)
2955 * Takes each row as a sector and uses it's first value. This has the added
2956 * advantage of using the labels provided, which is not the case with the
2957 * former method. This might prove useful for pie charts from GROUP BY sql queries
2959 function DrawPieChart()
2961 $xpos = $this->plot_area[0] + $this->plot_area_width/2;
2962 $ypos = $this->plot_area[1] + $this->plot_area_height/2;
2963 $diameter = min($this->plot_area_width, $this->plot_area_height);
2964 $radius = $diameter/2;
2966 // Get sum of each column? One pie slice per column
2967 if ($this->data_type === 'text-data') {
2968 for ($i = 0; $i < $this->num_data_rows; $i++) {
2969 for ($j = 1; $j < $this->num_recs[$i]; $j++) { // Label ($row[0]) unused in these pie charts
2970 @ $sumarr[$j] += abs($this->data[$i][$j]); // NOTE! sum > 0 to make pie charts
2974 // Or only one column per row, one pie slice per row?
2975 else if ($this->data_type == 'text-data-pie') {
2976 for ($i = 0; $i < $this->num_data_rows; $i++) {
2977 $legend[$i] = $this->data[$i][0]; // Set the legend to column labels
2978 $sumarr[$i] = $this->data[$i][1];
2981 else if ($this->data_type == 'data-data') {
2982 for ($i = 0; $i < $this->num_data_rows; $i++) {
2983 for ($j = 2; $j < $this->num_recs[$i]; $j++) {
2984 @ $sumarr[$j] += abs($this->data[$i][$j]);
2989 $this->DrawError("DrawPieChart(): Data type '$this->data_type' not supported.");
2993 $total = array_sum($sumarr);
2996 $this->DrawError('DrawPieChart(): Empty data set');
3000 if ($this->shading) {
3001 $diam2 = $diameter / 2;
3005 $max_data_colors = count ($this->data_colors);
3007 for ($h = $this->shading; $h >= 0; $h--) {
3011 foreach ($sumarr as $val) {
3012 // For shaded pies: the last one (at the top of the "stack") has a brighter color:
3014 $slicecol = $this->ndx_data_colors[$color_index];
3016 $slicecol = $this->ndx_data_dark_colors[$color_index];
3018 $label_txt = number_format(($val / $total * 100), $this->y_precision, '.', ', ') . '%';
3019 $val = 360 * ($val / $total);
3021 // NOTE that imagefilledarc measures angles CLOCKWISE (go figure why),
3022 // so the pie chart would start clockwise from 3 o'clock, would it not be
3023 // for the reversal of start and end angles in imagefilledarc()
3024 $start_angle = $end_angle;
3026 $mid_angle = deg2rad($end_angle - ($val / 2));
3029 ImageFilledArc($this->img, $xpos, $ypos+$h, $diameter, $diam2,
3030 360-$end_angle, 360-$start_angle,
3031 $slicecol, IMG_ARC_PIE);
3033 // Draw the labels only once
3036 if (! $this->shading)
3037 ImageFilledArc($this->img, $xpos, $ypos+$h, $diameter, $diam2,
3038 360-$end_angle, 360-$start_angle,
3039 $this->ndx_grid_color, IMG_ARC_PIE | IMG_ARC_EDGED |IMG_ARC_NOFILL);
3042 // The '* 1.2' trick is to get labels out of the pie chart so there are more
3043 // chances they can be seen in small sectors.
3044 $label_x = $xpos + ($diameter * 1.2 * cos($mid_angle)) * $this->label_scale_position;
3045 $label_y = $ypos+$h - ($diam2 * 1.2 * sin($mid_angle)) * $this->label_scale_position;
3047 $this->DrawText($this->generic_font, 0, $label_x, $label_y, $this->ndx_grid_color,
3048 $label_txt, 'center', 'center');
3051 $color_index = $color_index % $max_data_colors;
3058 * Supported data formats: data-data-error, text-data-error (doesn't exist yet)
3059 * ( data comes in as array("title", x, y, error+, error-, y2, error2+, error2-, ...) )
3061 function DrawDotsError()
3063 $this->CheckOption($this->data_type, 'data-data-error', __FUNCTION__);
3065 for($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
3066 $record = 1; // Skip record #0 (title)
3068 // Do we have a value for X?
3069 if ($this->data_type == 'data-data-error')
3070 $x_now = $this->data[$row][$record++]; // Read it, advance record index
3072 $x_now = 0.5 + $cnt++; // Place text-data at X = 0.5, 1.5, 2.5, etc...
3074 // Draw X Data labels?
3075 if ($this->x_data_label_pos != 'none')
3076 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels, $row);
3078 while ($record < $this->num_recs[$row]) {
3080 $y_now = $this->data[$row][$record++];
3081 $this->DrawDot($x_now, $y_now, $this->point_shape, $this->ndx_data_colors[$record]);
3084 $val = $this->data[$row][$record++];
3085 $this->DrawYErrorBar($x_now, $y_now, $val, $this->error_bar_shape,
3086 $this->ndx_error_bar_colors[$record]);
3088 $val = $this->data[$row][$record];
3089 $this->DrawYErrorBar($x_now, $y_now, -$val, $this->error_bar_shape,
3090 $this->ndx_error_bar_colors[$record++]);
3093 } // function DrawDotsError()
3097 * Supported data types:
3098 * - data-data ("title", x, y1, y2, y3, ...)
3099 * - text-data ("title", y1, y2, y3, ...)
3103 $this->CheckOption($this->data_type, 'text-data, data-data', __FUNCTION__);
3105 for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
3106 $rec = 1; // Skip record #0 (data label)
3108 // Do we have a value for X?
3109 if ($this->data_type == 'data-data')
3110 $x_now = $this->data[$row][$rec++]; // Read it, advance record index
3112 $x_now = 0.5 + $cnt++; // Place text-data at X = 0.5, 1.5, 2.5, etc...
3114 $x_now_pixels = $this->xtr($x_now);
3116 // Draw X Data labels?
3117 if ($this->x_data_label_pos != 'none')
3118 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels, $row);
3120 // Proceed with Y values
3121 for($idx = 0;$rec < $this->num_recs[$row]; $rec++, $idx++) {
3122 if (is_numeric($this->data[$row][$rec])) { // Allow for missing Y data
3123 $this->DrawDot($x_now, $this->data[$row][$rec],
3124 $this->point_shape, $this->ndx_data_colors[$idx]);
3128 } //function DrawDots
3132 * A clean, fast routine for when you just want charts like stock volume charts
3134 function DrawThinBarLines()
3136 $this->CheckOption($this->data_type, 'text-data, data-data', __FUNCTION__);
3138 for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
3139 $rec = 1; // Skip record #0 (data label)
3141 // Do we have a value for X?
3142 if ($this->data_type == 'data-data')
3143 $x_now = $this->data[$row][$rec++]; // Read it, advance record index
3145 $x_now = 0.5 + $cnt++; // Place text-data at X = 0.5, 1.5, 2.5, etc...
3147 $x_now_pixels = $this->xtr($x_now);
3149 // Draw X Data labels?
3150 if ($this->x_data_label_pos != 'none')
3151 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels);
3153 // Proceed with Y values
3154 for($idx = 0;$rec < $this->num_recs[$row]; $rec++, $idx++) {
3155 if (is_numeric($this->data[$row][$rec])) { // Allow for missing Y data
3156 ImageSetThickness($this->img, $this->line_widths[$idx]);
3157 // Draws a line from user defined x axis position up to ytr($val)
3158 ImageLine($this->img, $x_now_pixels, $this->x_axis_y_pixels, $x_now_pixels,
3159 $this->ytr($this->data[$row][$rec]), $this->ndx_data_colors[$idx]);
3164 ImageSetThickness($this->img, 1);
3165 } //function DrawThinBarLines
3170 function DrawYErrorBar($x_world, $y_world, $error_height, $error_bar_type, $color)
3173 // TODO: add a parameter to show datalabels next to error bars?
3174 // something like this:
3175 if ($this->x_data_label_pos == 'plot') {
3176 $this->DrawText($this->error_font, 90, $x1, $y2,
3177 $color, $label, 'center', 'top');
3180 $x1 = $this->xtr($x_world);
3181 $y1 = $this->ytr($y_world);
3182 $y2 = $this->ytr($y_world+$error_height) ;
3184 ImageSetThickness($this->img, $this->error_bar_line_width);
3185 ImageLine($this->img, $x1, $y1 , $x1, $y2, $color);
3187 switch ($error_bar_type) {
3191 ImageLine($this->img, $x1-$this->error_bar_size, $y2, $x1+$this->error_bar_size, $y2, $color);
3194 ImageLine($this->img, $x1-$this->error_bar_size, $y2, $x1+$this->error_bar_size, $y2, $color);
3198 ImageSetThickness($this->img, 1);
3203 * Draws a styled dot. Uses world coordinates.
3204 * Supported types: 'halfline', 'line', 'plus', 'cross', 'rect', 'circle', 'dot',
3205 * 'diamond', 'triangle', 'trianglemid'
3207 function DrawDot($x_world, $y_world, $dot_type, $color)
3209 $half_point = $this->point_size / 2;
3211 $x_mid = $this->xtr($x_world);
3212 $y_mid = $this->ytr($y_world);
3214 $x1 = $x_mid - $half_point;
3215 $x2 = $x_mid + $half_point;
3216 $y1 = $y_mid - $half_point;
3217 $y2 = $y_mid + $half_point;
3219 switch ($dot_type) {
3221 ImageLine($this->img, $x1, $y_mid, $x_mid, $y_mid, $color);
3224 ImageLine($this->img, $x1, $y_mid, $x2, $y_mid, $color);
3227 ImageLine($this->img, $x1, $y_mid, $x2, $y_mid, $color);
3228 ImageLine($this->img, $x_mid, $y1, $x_mid, $y2, $color);
3231 ImageLine($this->img, $x1, $y1, $x2, $y2, $color);
3232 ImageLine($this->img, $x1, $y2, $x2, $y1, $color);
3235 ImageFilledRectangle($this->img, $x1, $y1, $x2, $y2, $color);
3238 ImageArc($this->img, $x_mid, $y_mid, $this->point_size, $this->point_size, 0, 360, $color);
3241 ImageFilledArc($this->img, $x_mid, $y_mid, $this->point_size, $this->point_size, 0, 360,
3242 $color, IMG_ARC_PIE);
3245 $arrpoints = array( $x1, $y_mid, $x_mid, $y1, $x2, $y_mid, $x_mid, $y2);
3246 ImageFilledPolygon($this->img, $arrpoints, 4, $color);
3249 $arrpoints = array( $x1, $y_mid, $x2, $y_mid, $x_mid, $y2);
3250 ImageFilledPolygon($this->img, $arrpoints, 3, $color);
3253 $arrpoints = array( $x1, $y1, $x2, $y1, $x_mid, $y_mid);
3254 ImageFilledPolygon($this->img, $arrpoints, 3, $color);
3257 ImageFilledRectangle($this->img, $x1, $y1, $x2, $y2, $color);
3264 * Draw an area plot. Supported data types:
3267 * NOTE: This function used to add first and last data values even on incomplete
3268 * sets. That is not the behaviour now. As for missing data in between,
3269 * there are two posibilities: replace the point with one on the X axis (previous
3270 * way), or forget about it and use the preceding and following ones to draw the polygon.
3271 * There is the possibility to use both, we just need to add the method to set
3272 * it. Something like SetMissingDataBehaviour(), for example.
3276 $incomplete_data_defaults_to_x_axis = FALSE; // TODO: make this configurable
3278 for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
3279 $rec = 1; // Skip record #0 (data label)
3281 if ($this->data_type == 'data-data') // Do we have a value for X?
3282 $x_now = $this->data[$row][$rec++]; // Read it, advance record index
3284 $x_now = 0.5 + $cnt++; // Place text-data at X = 0.5, 1.5, 2.5, etc...
3286 $x_now_pixels = $this->xtr($x_now); // Absolute coordinates
3289 if ($this->x_data_label_pos != 'none') // Draw X Data labels?
3290 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels);
3292 // Proceed with Y values
3293 // Create array of points for imagefilledpolygon()
3294 for($idx = 0; $rec < $this->num_recs[$row]; $rec++, $idx++) {
3295 if (is_numeric($this->data[$row][$rec])) { // Allow for missing Y data
3296 $y_now_pixels = $this->ytr($this->data[$row][$rec]);
3298 $posarr[$idx][] = $x_now_pixels;
3299 $posarr[$idx][] = $y_now_pixels;
3301 $num_points[$idx] = isset($num_points[$idx]) ? $num_points[$idx]+1 : 1;
3303 // If there's missing data...
3305 if (isset ($incomplete_data_defaults_to_x_axis)) {
3306 $posarr[$idx][] = $x_now_pixels;
3307 $posarr[$idx][] = $this->x_axis_y_pixels;
3308 $num_points[$idx] = isset($num_points[$idx]) ? $num_points[$idx]+1 : 1;
3314 $end = count($posarr);
3315 for ($i = 0; $i < $end; $i++) {
3316 // Prepend initial points. X = first point's X, Y = x_axis_y_pixels
3317 $x = $posarr[$i][0];
3318 array_unshift($posarr[$i], $x, $this->x_axis_y_pixels);
3320 // Append final points. X = last point's X, Y = x_axis_y_pixels
3321 $x = $posarr[$i][count($posarr[$i])-2];
3322 array_push($posarr[$i], $x, $this->x_axis_y_pixels);
3324 $num_points[$i] += 2;
3327 ImageFilledPolygon($this->img, $posarr[$i], $num_points[$i], $this->ndx_data_colors[$i]);
3330 } // function DrawArea()
3334 * Draw Lines. Supported data-types:
3337 * NOTE: Please see the note regarding incomplete data sets on DrawArea()
3339 function DrawLines()
3341 // This will tell us if lines have already begun to be drawn.
3342 // It is an array to keep separate information for every line, with a single
3343 // variable we would sometimes get "undefined offset" errors and no plot...
3344 $start_lines = array_fill(0, $this->records_per_group, FALSE);
3346 if ($this->data_type == 'text-data') {
3347 $lastx[0] = $this->xtr(0);
3348 $lasty[0] = $this->xtr(0);
3351 for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
3352 $record = 1; // Skip record #0 (data label)
3354 if ($this->data_type == 'data-data') // Do we have a value for X?
3355 $x_now = $this->data[$row][$record++]; // Read it, advance record index
3357 $x_now = 0.5 + $cnt++; // Place text-data at X = 0.5, 1.5, 2.5, etc...
3359 $x_now_pixels = $this->xtr($x_now); // Absolute coordinates
3361 if ($this->x_data_label_pos != 'none') // Draw X Data labels?
3362 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels, $row);
3364 for ($idx = 0; $record < $this->num_recs[$row]; $record++, $idx++) {
3365 if (is_numeric($this->data[$row][$record])) { //Allow for missing Y data
3366 $y_now_pixels = $this->ytr($this->data[$row][$record]);
3368 if ($start_lines[$idx] == TRUE) {
3369 // Set line width, revert it to normal at the end
3370 ImageSetThickness($this->img, $this->line_widths[$idx]);
3372 if ($this->line_styles[$idx] == 'dashed') {
3373 $this->SetDashedStyle($this->ndx_data_colors[$idx]);
3374 ImageLine($this->img, $x_now_pixels, $y_now_pixels, $lastx[$idx], $lasty[$idx],
3377 ImageLine($this->img, $x_now_pixels, $y_now_pixels, $lastx[$idx], $lasty[$idx],
3378 $this->ndx_data_colors[$idx]);
3382 $lasty[$idx] = $y_now_pixels;
3383 $lastx[$idx] = $x_now_pixels;
3384 $start_lines[$idx] = TRUE;
3386 // Y data missing... should we leave a blank or not?
3387 else if ($this->draw_broken_lines) {
3388 $start_lines[$idx] = FALSE;
3393 ImageSetThickness($this->img, 1); // Revert to original state for lines to be drawn later.
3394 } // function DrawLines()
3398 * Draw lines with error bars - data comes in as
3399 * array("label", x, y, error+, error-, y2, error2+, error2-, ...);
3401 function DrawLinesError()
3403 if ($this->data_type != 'data-data-error') {
3404 $this->DrawError("DrawLinesError(): Data type '$this->data_type' not supported.");
3408 $start_lines = array_fill(0, $this->records_per_group, FALSE);
3410 for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
3411 $record = 1; // Skip record #0 (data label)
3413 $x_now = $this->data[$row][$record++]; // Read X value, advance record index
3415 $x_now_pixels = $this->xtr($x_now); // Absolute coordinates.
3418 if ($this->x_data_label_pos != 'none') // Draw X Data labels?
3419 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels, $row);
3421 // Now go for Y, E+, E-
3422 for ($idx = 0; $record < $this->num_recs[$row]; $idx++) {
3424 $y_now = $this->data[$row][$record++];
3425 $y_now_pixels = $this->ytr($y_now);
3427 if ($start_lines[$idx] == TRUE) {
3428 ImageSetThickness($this->img, $this->line_widths[$idx]);
3430 if ($this->line_styles[$idx] == 'dashed') {
3431 $this->SetDashedStyle($this->ndx_data_colors[$idx]);
3432 ImageLine($this->img, $x_now_pixels, $y_now_pixels, $lastx[$idx], $lasty[$idx],
3435 ImageLine($this->img, $x_now_pixels, $y_now_pixels, $lastx[$idx], $lasty[$idx],
3436 $this->ndx_data_colors[$idx]);
3441 $val = $this->data[$row][$record++];
3442 $this->DrawYErrorBar($x_now, $y_now, $val, $this->error_bar_shape,
3443 $this->ndx_error_bar_colors[$idx]);
3446 $val = $this->data[$row][$record++];
3447 $this->DrawYErrorBar($x_now, $y_now, -$val, $this->error_bar_shape,
3448 $this->ndx_error_bar_colors[$idx]);
3451 $start_lines[$idx] = TRUE; // Tells us if we already drew the first column of points,
3452 // thus having $lastx and $lasty ready for the next column.
3453 $lastx[$idx] = $x_now_pixels;
3454 $lasty[$idx] = $y_now_pixels;
3458 ImageSetThickness($this->img, 1); // Revert to original state for lines to be drawn later.
3459 } // function DrawErrorLines()
3464 * This is a mere copy of DrawLines() with one more line drawn for each point
3466 function DrawSquared()
3468 // This will tell us if lines have already begun to be drawn.
3469 // It is an array to keep separate information for every line, for with a single
3470 // variable we could sometimes get "undefined offset" errors and no plot...
3471 $start_lines = array_fill(0, $this->records_per_group, FALSE);
3473 if ($this->data_type == 'text-data') {
3474 $lastx[0] = $this->xtr(0);
3475 $lasty[0] = $this->xtr(0);
3478 for ($row = 0, $cnt = 0; $row < $this->num_data_rows; $row++) {
3479 $record = 1; // Skip record #0 (data label)
3481 if ($this->data_type == 'data-data') // Do we have a value for X?
3482 $x_now = $this->data[$row][$record++]; // Read it, advance record index
3484 $x_now = 0.5 + $cnt++; // Place text-data at X = 0.5, 1.5, 2.5, etc...
3486 $x_now_pixels = $this->xtr($x_now); // Absolute coordinates
3488 if ($this->x_data_label_pos != 'none') // Draw X Data labels?
3489 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels); // notice there is no last param.
3492 for ($idx = 0; $record < $this->num_recs[$row]; $record++, $idx++) {
3493 if (is_numeric($this->data[$row][$record])) { // Allow for missing Y data
3494 $y_now_pixels = $this->ytr($this->data[$row][$record]);
3496 if ($start_lines[$idx] == TRUE) {
3497 // Set line width, revert it to normal at the end
3498 ImageSetThickness($this->img, $this->line_widths[$idx]);
3500 if ($this->line_styles[$idx] == 'dashed') {
3501 $this->SetDashedStyle($this->ndx_data_colors[$idx]);
3502 ImageLine($this->img, $lastx[$idx], $lasty[$idx], $x_now_pixels, $lasty[$idx],
3504 ImageLine($this->img, $x_now_pixels, $lasty[$idx], $x_now_pixels, $y_now_pixels,
3507 ImageLine($this->img, $lastx[$idx], $lasty[$idx], $x_now_pixels, $lasty[$idx],
3508 $this->ndx_data_colors[$idx]);
3509 ImageLine($this->img, $x_now_pixels, $lasty[$idx], $x_now_pixels, $y_now_pixels,
3510 $this->ndx_data_colors[$idx]);
3513 $lastx[$idx] = $x_now_pixels;
3514 $lasty[$idx] = $y_now_pixels;
3515 $start_lines[$idx] = TRUE;
3517 // Y data missing... should we leave a blank or not?
3518 else if ($this->draw_broken_lines) {
3519 $start_lines[$idx] = FALSE;
3524 ImageSetThickness($this->img, 1);
3525 } // function DrawSquared()
3529 * Data comes in as array("title", x, y, y2, y3, ...)
3533 if ($this->data_type != 'text-data') {
3534 $this->DrawError('DrawBars(): Bar plots must be text-data: use function SetDataType("text-data")');
3538 for ($row = 0; $row < $this->num_data_rows; $row++) {
3539 $record = 1; // Skip record #0 (data label)
3541 $x_now_pixels = $this->xtr(0.5 + $row); // Place text-data at X = 0.5, 1.5, 2.5, etc...
3543 if ($this->x_data_label_pos != 'none') // Draw X Data labels?
3544 $this->DrawXDataLabel($this->data[$row][0], $x_now_pixels);
3547 for ($idx = 0; $record < $this->num_recs[$row]; $record++, $idx++) {
3548 if (is_numeric($this->data[$row][$record])) { // Allow for missing Y data
3549 $x1 = $x_now_pixels - $this->data_group_space + ($idx * $this->record_bar_width);
3550 $x2 = $x1 + ($this->bar_width_adjust * $this->record_bar_width);
3552 if ($this->data[$row][$record] < $this->x_axis_position) {
3553 $y1 = $this->x_axis_y_pixels;
3554 $y2 = $this->ytr($this->data[$row][$record]);
3556 $y1 = $this->ytr($this->data[$row][$record]);
3557 $y2 = $this->x_axis_y_pixels;
3560 if ($this->shading) { // Draw the shade?
3561 ImageFilledPolygon($this->img, array($x1, $y1,
3562 $x1 + $this->shading, $y1 - $this->shading,
3563 $x2 + $this->shading, $y1 - $this->shading,
3564 $x2 + $this->shading, $y2 - $this->shading,
3567 6, $this->ndx_data_dark_colors[$idx]);
3569 // Or draw a border?
3571 ImageRectangle($this->img, $x1, $y1, $x2,$y2, $this->ndx_data_border_colors[$idx]);
3574 ImageFilledRectangle($this->img, $x1, $y1, $x2, $y2, $this->ndx_data_colors[$idx]);
3578 } //function DrawBars
3584 function DrawGraph()
3587 $this->DrawError('DrawGraph(): No image resource allocated');
3591 if (! is_array($this->data)) {
3592 $this->DrawError("DrawGraph(): No array of data in \$data");
3596 if (! isset($this->data_limits_done))
3597 $this->FindDataLimits(); // Get maxima and minima for scaling
3599 if ($this->total_records == 0) { // Check for empty data sets
3600 $this->DrawError('Empty data set');
3604 $this->CalcMargins(); // Calculate margins
3606 if (! isset($this->plot_area_width)) // Set plot area pixel values (plot_area[])
3607 $this->SetPlotAreaPixels();
3609 if (! isset($this->plot_max_y)) // Set plot area world values (plot_max_x, etc.)
3610 $this->SetPlotAreaWorld();
3612 if ($this->data_type == 'text-data')
3613 $this->SetEqualXCoord();
3615 if ($this->x_data_label_pos != 'none') { // Default: do not draw tick stuff if
3616 $this->x_tick_label_pos = 'none'; // there are data labels.
3617 $this->x_tick_pos = 'none';
3620 $this->PadArrays(); // Pad color and style arrays to fit records per group.
3622 $this->DrawBackground();
3624 $this->DrawImageBorder();
3626 if ($this->draw_plot_area_background)
3627 $this->DrawPlotAreaBackground();
3630 $this->DrawXTitle();
3631 $this->DrawYTitle();
3633 switch ($this->plot_type) {
3635 $this->DrawThinBarLines();
3641 $this->DrawSquared();
3644 if ( $this->data_type == 'data-data-error') {
3645 $this->DrawLinesError();
3650 case 'linepoints': // FIXME !!! DrawXDataLabel gets called in DrawLines() and DrawDots()
3651 if ( $this->data_type == 'data-data-error') {
3652 $this->DrawLinesError();
3653 $this->DrawDotsError();
3660 if ( $this->data_type == 'data-data-error') {
3661 $this->DrawDotsError();
3667 // Pie charts can maximize image space usage.
3668 $this->SetPlotAreaPixels($this->safe_margin, $this->title_height,
3669 $this->image_width - $this->safe_margin,
3670 $this->image_height - $this->safe_margin);
3671 $this->DrawPieChart();
3675 $this->plot_type = 'bars'; // Set it if it wasn't already set.
3676 $this->DrawYAxis(); // We don't want the grid to overwrite bar charts
3677 $this->DrawXAxis(); // so we draw it first. Also, Y must be drawn before X (see DrawYAxis())
3679 $this->DrawPlotBorder();
3683 if ($this->plot_type != 'pie' && $this->plot_type != 'bars') {
3686 $this->DrawPlotBorder();
3689 $this->DrawLegend($this->legend_x_pos, $this->legend_y_pos, '');
3691 if ($this->print_image)
3692 $this->PrintImage();
3694 } //function DrawGraph()
3696 /////////////////////////////////////////////
3697 ////////////////// DEPRECATED METHODS
3698 /////////////////////////////////////////////
3701 * Deprecated, use SetYTickPos()
3703 function SetDrawVertTicks($which_dvt)
3705 if ($which_dvt != 1)
3706 $this->SetYTickPos('none');
3711 * Deprecated, use SetXTickPos()
3713 function SetDrawHorizTicks($which_dht)
3715 if ($which_dht != 1)
3716 $this->SetXTickPos('none');
3721 * \deprecated Use SetNumXTicks()
3723 function SetNumHorizTicks($n)
3725 return $this->SetNumXTicks($n);
3729 * \deprecated Use SetNumYTicks()
3731 function SetNumVertTicks($n)
3733 return $this->SetNumYTicks($n);
3737 * \deprecated Use SetXTickIncrement()
3739 function SetHorizTickIncrement($inc)
3741 return $this->SetXTickIncrement($inc);
3746 * \deprecated Use SetYTickIncrement()
3748 function SetVertTickIncrement($inc)
3750 return $this->SetYTickIncrement($inc);
3754 * \deprecated Use SetYTickPos()
3756 function SetVertTickPosition($which_tp)
3758 return $this->SetYTickPos($which_tp);
3762 * \deprecated Use SetXTickPos()
3764 function SetHorizTickPosition($which_tp)
3766 return $this->SetXTickPos($which_tp);
3770 * \deprecated Use SetFont()
3772 function SetTitleFontSize($which_size)
3774 return $this->SetFont('title', $which_size);
3778 * \deprecated Use SetFont()
3780 function SetAxisFontSize($which_size)
3782 $this->SetFont('x_label', $which_size);
3783 $this->SetFont('y_label', $whic_size);
3787 * \deprecated Use SetFont()
3789 function SetSmallFontSize($which_size)
3791 return $this->SetFont('generic', $which_size);
3795 * \deprecated Use SetFont()
3797 function SetXLabelFontSize($which_size)
3799 return $this->SetFont('x_title', $which_size);
3803 * \deprecated Use SetFont()
3805 function SetYLabelFontSize($which_size)
3807 return $this->SetFont('y_title', $which_size);
3811 * \deprecated Use SetXTitle()
3813 function SetXLabel($which_xlab)
3815 return $this->SetXTitle($which_xlab);
3819 * \deprecated Use SetYTitle()
3821 function SetYLabel($which_ylab)
3823 return $this->SetYTitle($which_ylab);
3827 * \deprecated This is now an Internal function - please set width and
3828 * height via PHPlot() upon object construction
3830 function SetImageArea($which_iw, $which_ih)
3832 $this->image_width = $which_iw;
3833 $this->image_height = $which_ih;
3839 * \deprecated Use SetXTickLength() and SetYTickLength() instead.
3841 function SetTickLength($which_tl)
3843 $this->SetXTickLength($which_tl);
3844 $this->SetYTickLength($which_tl);
3849 * \deprecated Use SetYLabelType()
3851 function SetYGridLabelType($which_yglt)
3853 return $this->SetYLabelType($which_yglt);
3857 * \deprecated Use SetXLabelType()
3859 function SetXGridLabelType($which_xglt)
3861 return $this->SetXLabelType($which_xglt);
3864 * \deprecated Use SetYTickLabelPos()
3866 function SetYGridLabelPos($which_yglp)
3868 return $this->SetYTickLabelPos($which_yglp);
3871 * \deprecated Use SetXTickLabelPos()
3873 function SetXGridLabelPos($which_xglp)
3875 return $this->SetXTickLabelPos($which_xglp);
3880 * \deprecated Use SetXtitle()
3882 function SetXTitlePos($xpos)
3884 $this->x_title_pos = $xpos;
3889 * \deprecated Use SetYTitle()
3891 function SetYTitlePos($xpos)
3893 $this->y_title_pos = $xpos;
3898 * \deprecated Use DrawDots()
3900 function DrawDotSeries()
3906 * \deprecated Use SetXLabelAngle()
3908 function SetXDataLabelAngle($which_xdla)
3910 return $this->SetXLabelAngle($which_xdla);
3914 * Draw Labels (not grid labels) on X Axis, following data points. Default position is
3915 * down of plot. Care must be taken not to draw these and x_tick_labels as they'd probably overlap.
3917 * \deprecated Use SetXDataLabelPos()
3919 function SetDrawXDataLabels($which_dxdl)
3921 if ($which_dxdl == '1' )
3922 $this->SetXDataLabelPos('plotdown');
3924 $this->SetXDataLabelPos('none');
3928 * \deprecated This method was intended to improve performance by being specially
3929 * written for 'data-data'. However, the improvement didn't pay. Use DrawLines() instead
3931 function DrawLineSeries()
3933 return $this->DrawLines();
3937 * \deprecated Calculates maximum X-Axis label height. Now inside _CalcMargins()
3939 function CalcXHeights()
3942 if ($this->use_ttf) {
3943 $xstr = str_repeat('.', $this->max_t);
3944 $size = $this->TTFBBoxSize($this->x_label_font['size'], $this->x_label_angle,
3945 $this->x_label_font['font'], $xstr);
3946 $this->x_tick_label_height = $size[1];
3949 else { // For Non-TTF fonts we can have only angles 0 or 90
3950 if ($this->x_label_angle == 90)
3951 $this->x_tick_label_height = $this->max_t * $this->x_label_font['width'];
3953 $this->x_tick_label_height = $this->x_label_font['height'];
3961 * \deprecated Calculates Maximum Y-Axis tick label width. Now inside CalcMargins()
3963 function CalcYWidths()
3965 //the "." is for space. It isn't actually printed
3966 $ylab = number_format($this->max_y, $this->y_precision, '.', ', ') . $this->data_units_text . '.';
3969 if ($this->use_ttf) {
3970 // Maximum Y tick label width
3971 $size = $this->TTFBBoxSize($this->y_label_font['size'], 0, $this->y_label_font['font'], $ylab);
3972 $this->y_tick_label_width = $size[0];
3977 // Y axis title width
3978 $this->y_tick_label_width = strlen($ylab) * $this->y_label_font['width'];
3985 * \deprecated Superfluous.
3987 function DrawLabels()
3990 $this->DrawXTitle();
3991 $this->DrawYTitle();
3995 * Set up the image resource 'img'
3996 * \deprecated The constructor should init 'img'
3998 function InitImage()
4000 $this->img = ImageCreate($this->image_width, $this->image_height);
4003 $this->PrintError('InitImage(): Could not create image resource');
4010 function SetNewPlotAreaPixels($x1, $y1, $x2, $y2)
4012 //Like in GD 0, 0 is upper left set via pixel Coordinates
4013 $this->plot_area = array($x1, $y1, $x2, $y2);
4014 $this->plot_area_width = $this->plot_area[2] - $this->plot_area[0];
4015 $this->plot_area_height = $this->plot_area[3] - $this->plot_area[1];
4016 $this->y_top_margin = $this->plot_area[1];
4018 if (isset($this->plot_max_x))
4019 $this->CalcTranslation();
4025 * \deprecated Use _SetRGBColor()
4027 function SetColor($which_color)
4029 $this->SetRGBColor($which_color);
4034 * \deprecated Use SetLineWidths().
4036 function SetLineWidth($which_lw)
4039 $this->SetLineWidths($which_lw);
4041 if (!$this->error_bar_line_width) {
4042 $this->SetErrorBarLineWidth($which_lw);
4050 function DrawDashedLine($x1, $y1, $x2, $y2 , $dash_length, $dash_space, $color)
4053 $dashes = array_fill(0, $dash_length, $color);
4057 $spaces = array_fill(0, $dash_space, IMG_COLOR_TRANSPARENT);
4061 $style = array_merge($dashes, $spaces);
4062 ImageSetStyle($this->img, $style);
4063 ImageLine($this->img, $x1, $y1, $x2, $y2, IMG_COLOR_STYLED);
4069 ////////////////////////
4073 * Pads an array with another or with itself.
4074 * \param arr array Original array (reference)
4075 * \param size int Size of the resulting array.
4076 * \param arr2 array If specified, array to use for padding. If unspecified, pad with $arr.
4078 function array_pad_array(&$arr, $size, $arr2=NULL)
4080 if (! is_array($arr2)) {
4081 $arr2 = $arr; // copy the original array
4083 while (count($arr) < $size)
4084 $arr = array_merge($arr, $arr2); // append until done