/administrator/components/com_sm2emailmarketing/includes/phplot/phplot.php
PHP | 4450 lines | 2834 code | 647 blank | 969 comment | 605 complexity | f4e2a07677533cfe435dcdd5f5a533ce MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
- /* $Id: phplot.php,v 1.3 2007/07/20 21:47:24 sm2tony Exp $ */
- /*
- * PHPLOT Version 5.0.rc1
- * Copyright (C) 1998, 1999, 2000, 2001 Afan Ottenheimer. Released under
- * the GPL and PHP licenses as stated in the the README file which should
- * have been included with this document.
- *
- * Recent (2003-2004) work by Miguel de Benito Delgado <nonick AT vodafone DOT es>
- *
- * Requires PHP 4.2.0 or later (CHECK THIS)
- */
- defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');
- if (! defined(__FUNCTION__))
- define(__FUNCTION__, '__FUNCTION__ Requires at least PHP 4.3.0.');
- define ('MINY', -1); // Indexes in $data (for DrawXDataLine())
- define ('MAXY', -2);
- define ('TOTY', -3);
- error_reporting(E_ALL);
- class PHPlot {
- /* I have removed internal variable declarations, some isset() checking was required,
- * but now the variables left are those which can be tweaked by the user. This is intended to
- * be the first step towards moving most of the Set...() methods into a subclass which will be
- * used only when strictly necessary. Many users will be able to put default values here in the
- * class and thus avoid memory overhead and reduce parsing times.
- */
- //////////////// CONFIG PARAMETERS //////////////////////
- var $is_inline = FALSE; // FALSE = Sends headers, TRUE = sends just raw image data
- var $browser_cache = FALSE; // FALSE = Sends headers for browser to not cache the image,
- // (only if is_inline = FALSE also)
- var $safe_margin = 5; // Extra margin used in several places. In pixels
- var $x_axis_position = ''; // Where to draw both axis (world coordinates),
- var $y_axis_position = ''; // leave blank for X axis at 0 and Y axis at left of plot.
- var $xscale_type = 'linear'; // linear, log
- var $yscale_type = 'linear';
- //Fonts
- var $use_ttf = FALSE; // Use True Type Fonts?
- var $ttf_path = '.'; // Default path to look in for TT Fonts.
- var $default_ttfont = 'benjamingothic.ttf';
- var $line_spacing = 4; // Pixels between lines.
- // Font angles: 0 or 90 degrees for fixed fonts, any for TTF
- var $x_label_angle = 0; // For labels on X axis (tick and data)
- var $y_label_angle = 0; // For labels on Y axis (tick and data)
- var $x_title_angle = 0; // Don't change this if you don't want to screw things up!
- var $y_title_angle = 90; // Nor this.
- var $title_angle = 0; // Or this.
- //Formats
- var $file_format = 'png';
- var $output_file = ''; // For output to a file instead of stdout
- //Data
- var $data_type = 'text-data'; // text-data, data-data-error, data-data, text-data-single
- var $plot_type= 'linepoints'; // bars, lines, linepoints, area, points, pie, thinbarline, squared
- var $label_scale_position = 0.5; // Shifts data labes in pie charts. 1 = top, 0 = bottom
- var $group_frac_width = 0.7; // value from 0 to 1 = width of bar groups
- var $bar_width_adjust = 1; // 1 = bars of normal width, must be > 0
- var $y_precision = 1;
- var $x_precision = 1;
- var $data_units_text = ''; // Units text for 'data' labels (i.e: '�', '$', etc.)
- // Titles
- var $title_txt = '';
- var $x_title_txt = '';
- var $x_title_pos = 'plotdown'; // plotdown, plotup, both, none
- var $y_title_txt = '';
- var $y_title_pos = 'plotleft'; // plotleft, plotright, both, none
- //Labels
- // There are two types of labels in PHPlot:
- // Tick labels: they follow the grid, next to ticks in axis. (DONE)
- // they are drawn at grid drawing time, by DrawXTicks() and DrawYTicks()
- // Data labels: they follow the data points, and can be placed on the axis or the plot (x/y) (TODO)
- // they are drawn at graph plotting time, by Draw*DataLabel(), called by DrawLines(), etc.
- // Draw*DataLabel() also draws H/V lines to datapoints depending on draw_*_data_label_lines
- // Tick Labels
- var $x_tick_label_pos = 'plotdown'; // plotdown, plotup, both, xaxis, none
- var $y_tick_label_pos = 'plotleft'; // plotleft, plotright, both, yaxis, none
- // Data Labels:
- var $x_data_label_pos = 'plotdown'; // plotdown, plotup, both, plot, all, none
- var $y_data_label_pos = 'plotleft'; // plotleft, plotright, both, plot, all, none
- var $draw_x_data_label_lines = FALSE; // Draw a line from the data point to the axis?
- var $draw_y_data_label_lines = FALSE; // TODO
- // Label types: (for tick, data and plot labels)
- var $x_label_type = ''; // data, time. Leave blank for no formatting.
- var $y_label_type = ''; // data, time. Leave blank for no formatting.
- var $x_time_format = '%H:%m:%s'; // See http://www.php.net/manual/html/function.strftime.html
- var $y_time_format = '%H:%m:%s'; // SetYTimeFormat() too...
- // Skipping labels
- var $x_label_inc = 1; // Draw a label every this many (1 = all) (TODO)
- var $y_label_inc = 1;
- var $_x_label_cnt = 0; // internal count FIXME: work in progress
- // Legend
- var $legend = ''; // An array with legend titles
- var $legend_x_pos = '';
- var $legend_y_pos = '';
- //Ticks
- var $x_tick_length = 5; // tick length in pixels for upper/lower axis
- var $y_tick_length = 5; // tick length in pixels for left/right axis
- var $x_tick_cross = 3; // ticks cross x axis this many pixels
- var $y_tick_cross = 3; // ticks cross y axis this many pixels
- var $x_tick_pos = 'plotdown'; // plotdown, plotup, both, xaxis, none
- var $y_tick_pos = 'plotleft'; // plotright, plotleft, both, yaxis, none
- var $num_x_ticks = '';
- var $num_y_ticks = '';
- var $x_tick_inc = ''; // Set num_x_ticks or x_tick_inc, not both.
- var $y_tick_inc = ''; // Set num_y_ticks or y_tick_inc, not both.
- var $skip_top_tick = FALSE;
- var $skip_bottom_tick = FALSE;
- var $skip_left_tick = FALSE;
- var $skip_right_tick = FALSE;
- //Grid Formatting
- var $draw_x_grid = FALSE;
- var $draw_y_grid = TRUE;
- var $dashed_grid = TRUE;
- var $grid_at_foreground = FALSE; // Chooses whether to draw the grid below or above the graph
- //Colors and styles (all colors can be array (R,G,B) or named color)
- var $color_array = 'small'; // 'small', 'large' or array (define your own colors)
- // See rgb.inc.php and SetRGBArray()
- var $i_border = array(194, 194, 194);
- var $plot_bg_color = 'white';
- var $bg_color = 'white';
- var $label_color = 'black';
- var $text_color = 'black';
- var $grid_color = 'black';
- var $light_grid_color = 'gray';
- var $tick_color = 'black';
- var $title_color = 'black';
- var $data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'orange', 'red', 'violet', 'azure1');
- var $error_bar_colors = array('SkyBlue', 'green', 'orange', 'blue', 'orange', 'red', 'violet', 'azure1');
- var $data_border_colors = array('black');
- var $line_widths = 1; // single value or array
- var $line_styles = array('solid', 'solid', 'dashed'); // single value or array
- var $dashed_style = '2-4'; // colored dots-transparent dots
- var $point_sizes = array(5,5,3); // single value or array
- var $point_shapes = array('diamond'); // rect, circle, diamond, triangle, dot, line, halfline, cross
- var $error_bar_size = 5; // right and left size of tee
- var $error_bar_shape = 'tee'; // 'tee' or 'line'
- var $error_bar_line_width = 1; // single value (or array TODO)
- var $plot_border_type = 'sides'; // left, sides, none, full
- var $image_border_type = 'none'; // 'raised', 'plain', 'none'
- var $shading = 5; // 0 for no shading, > 0 is size of shadows in pixels
- var $draw_plot_area_background = FALSE;
- var $draw_broken_lines = FALSE; // Tells not to draw lines for missing Y data.
- //////////////////////////////////////////////////////
- //BEGIN CODE
- //////////////////////////////////////////////////////
- /*!
- * Constructor: Setup img resource, colors and size of the image, and font sizes.
- *
- * \param which_width int Image width in pixels.
- * \param which_height int Image height in pixels.
- * \param which_output_file string Filename for output.
- * \param which_input_fule string Path to a file to be used as background.
- */
- function PHPlot($which_width=600, $which_height=400, $which_output_file=NULL, $which_input_file=NULL)
- {
- /*
- * Please see http://www.php.net/register_shutdown_function
- * PLEASE NOTE: register_shutdown_function() will take a copy of the object rather than a reference
- * so we put an ampersand. However, the function registered will work on the object as it
- * was upon registration. To solve this, one of two methods can be used:
- * $obj = new object();
- * register_shutdown_function(array(&$obj,'shutdown'));
- * OR
- * $obj = &new object();
- * HOWEVER, as the second statement assigns $obj a reference to the current object, it might be that
- * several instances mess things up... (CHECK THIS)
- *
- * AND
- * as $this->img is set upon construction of the object, problems will not arise for us (for the
- * moment maybe, so I put all this here just in case)
- */
- register_shutdown_function(array(&$this, '_PHPlot'));
- $this->SetRGBArray($this->color_array);
- $this->background_done = FALSE; // Set to TRUE after background image is drawn once
- if ($which_output_file)
- $this->SetOutputFile($which_output_file);
- if ($which_input_file)
- $this->SetInputFile($which_input_file);
- else {
- $this->image_width = $which_width;
- $this->image_height = $which_height;
- $this->img = ImageCreate($this->image_width, $this->image_height);
- if (! $this->img)
- $this->PrintError('PHPlot(): Could not create image resource.');
- }
- $this->SetDefaultStyles();
- $this->SetDefaultFonts();
- $this->SetTitle('');
- $this->SetXTitle('');
- $this->SetYTitle('');
- $this->print_image = TRUE; // Use for multiple plots per image (TODO: automatic)
- }
- /*!
- * Destructor. Image resources not deallocated can be memory hogs, I think
- * it is safer to automatically call imagedestroy upon script termination than
- * do it ourselves.
- * See notes in the constructor code.
- */
- function _PHPlot ()
- {
- imagedestroy($this->img);
- return;
- }
- /////////////////////////////////////////////
- ////////////// COLORS
- /////////////////////////////////////////////
- /*!
- * Returns an index to a color passed in as anything (string, hex, rgb)
- *
- * \param which_color * Color (can be '#AABBCC', 'Colorname', or array(r,g,b))
- */
- function SetIndexColor($which_color)
- {
- list ($r, $g, $b) = $this->SetRGBColor($which_color); //Translate to RGB
- $index = ImageColorExact($this->img, $r, $g, $b);
- if ($index == -1) {
- return ImageColorResolve($this->img, $r, $g, $b);
- } else {
- return $index;
- }
- }
- /*!
- * Returns an index to a slightly darker color than the one requested.
- */
- function SetIndexDarkColor($which_color)
- {
- list ($r, $g, $b) = $this->SetRGBColor($which_color);
- $r -= 0x30; $r = ($r < 0) ? 0 : $r;
- $g -= 0x30; $g = ($g < 0) ? 0 : $g;
- $b -= 0x30; $b = ($b < 0) ? 0 : $b;
- $index = ImageColorExact($this->img, $r, $g, $b);
- if ($index == -1) {
- return ImageColorResolve($this->img, $r, $g, $b);
- } else {
- return $index;
- }
- }
- /*!
- * Sets/reverts all colors and styles to their defaults. If session is set, then only updates indices,
- * as they are lost with every script execution, else, sets the default colors by name or value and
- * then updates indices too.
- *
- * FIXME Isn't this too slow?
- *
- */
- function SetDefaultStyles()
- {
- /* Some of the Set*() functions use default values when they get no parameters. */
- if (! isset($this->session_set)) {
- // If sessions are enabled, this variable will be preserved, so upon future executions, we
- // will have it set, as well as color names (though not color indices, that's why we
- // need to rebuild them)
- $this->session_set = TRUE;
- // These only need to be set once
- $this->SetLineWidths();
- $this->SetLineStyles();
- $this->SetDefaultDashedStyle($this->dashed_style);
- $this->SetPointSizes($this->point_sizes);
- }
- $this->SetImageBorderColor($this->i_border);
- $this->SetPlotBgColor($this->plot_bg_color);
- $this->SetBackgroundColor($this->bg_color);
- $this->SetLabelColor($this->label_color);
- $this->SetTextColor($this->text_color);
- $this->SetGridColor($this->grid_color);
- $this->SetLightGridColor($this->light_grid_color);
- $this->SetTickColor($this->tick_color);
- $this->SetTitleColor($this->title_color);
- $this->SetDataColors();
- $this->SetErrorBarColors();
- $this->SetDataBorderColors();
- }
- /*
- *
- */
- function SetBackgroundColor($which_color)
- {
- $this->bg_color= $which_color;
- $this->ndx_bg_color= $this->SetIndexColor($this->bg_color);
- return TRUE;
- }
- /*
- *
- */
- function SetPlotBgColor($which_color)
- {
- $this->plot_bg_color= $which_color;
- $this->ndx_plot_bg_color= $this->SetIndexColor($this->plot_bg_color);
- return TRUE;
- }
- /*
- *
- */
- function SetTitleColor($which_color)
- {
- $this->title_color= $which_color;
- $this->ndx_title_color= $this->SetIndexColor($this->title_color);
- return TRUE;
- }
- /*
- *
- */
- function SetTickColor ($which_color)
- {
- $this->tick_color= $which_color;
- $this->ndx_tick_color= $this->SetIndexColor($this->tick_color);
- return TRUE;
- }
- /*
- *
- */
- function SetLabelColor ($which_color)
- {
- $this->label_color = $which_color;
- $this->ndx_title_color= $this->SetIndexColor($this->label_color);
- return TRUE;
- }
- /*
- *
- */
- function SetTextColor ($which_color)
- {
- $this->text_color= $which_color;
- $this->ndx_text_color= $this->SetIndexColor($this->text_color);
- return TRUE;
- }
- /*
- *
- */
- function SetLightGridColor ($which_color)
- {
- $this->light_grid_color= $which_color;
- $this->ndx_light_grid_color= $this->SetIndexColor($this->light_grid_color);
- return TRUE;
- }
- /*
- *
- */
- function SetGridColor ($which_color)
- {
- $this->grid_color = $which_color;
- $this->ndx_grid_color= $this->SetIndexColor($this->grid_color);
- return TRUE;
- }
- /*
- *
- */
- function SetImageBorderColor($which_color)
- {
- $this->i_border = $which_color;
- $this->ndx_i_border = $this->SetIndexColor($this->i_border);
- $this->ndx_i_border_dark = $this->SetIndexDarkColor($this->i_border);
- return TRUE;
- }
- /*
- *
- */
- function SetTransparentColor($which_color)
- {
- ImageColorTransparent($this->img, $this->SetIndexColor($which_color));
- return TRUE;
- }
- /*!
- * Sets the array of colors to be used. It can be user defined, a small predefined one
- * or a large one included from 'rgb.inc.php'.
- *
- * \param which_color_array If an array, the used as color array. If a string can
- * be one of 'small' or 'large'.
- */
- function SetRGBArray ($which_color_array)
- {
- if ( is_array($which_color_array) ) { // User defined array
- $this->rgb_array = $which_color_array;
- return TRUE;
- } elseif ($which_color_array == 'small') { // Small predefined color array
- $this->rgb_array = array(
- 'white' => array(255, 255, 255),
- 'snow' => array(255, 250, 250),
- 'PeachPuff' => array(255, 218, 185),
- 'ivory' => array(255, 255, 240),
- 'lavender' => array(230, 230, 250),
- 'black' => array( 0, 0, 0),
- 'DimGrey' => array(105, 105, 105),
- 'gray' => array(190, 190, 190),
- 'grey' => array(190, 190, 190),
- 'navy' => array( 0, 0, 128),
- 'SlateBlue' => array(106, 90, 205),
- 'blue' => array( 0, 0, 255),
- 'SkyBlue' => array(135, 206, 235),
- 'cyan' => array( 0, 255, 255),
- 'DarkGreen' => array( 0, 100, 0),
- 'green' => array( 0, 255, 0),
- 'YellowGreen' => array(154, 205, 50),
- 'yellow' => array(255, 255, 0),
- 'orange' => array(255, 165, 0),
- 'gold' => array(255, 215, 0),
- 'peru' => array(205, 133, 63),
- 'beige' => array(245, 245, 220),
- 'wheat' => array(245, 222, 179),
- 'tan' => array(210, 180, 140),
- 'brown' => array(165, 42, 42),
- 'salmon' => array(250, 128, 114),
- 'red' => array(255, 0, 0),
- 'pink' => array(255, 192, 203),
- 'maroon' => array(176, 48, 96),
- 'magenta' => array(255, 0, 255),
- 'violet' => array(238, 130, 238),
- 'plum' => array(221, 160, 221),
- 'orchid' => array(218, 112, 214),
- 'purple' => array(160, 32, 240),
- 'azure1' => array(240, 255, 255),
- 'aquamarine1' => array(127, 255, 212)
- );
- return TRUE;
- } elseif ($which_color_array === 'large') { // Large color array
- global $sm2emPHPLotPath;
- include($sm2emPHPLotPath."/rgb.inc.php");
- $this->rgb_array = $RGBArray;
- } else { // Default to black and white only.
- $this->rgb_array = array('white' => array(255, 255, 255), 'black' => array(0, 0, 0));
- }
- return TRUE;
- }
- /*!
- * Returns an array in R, G, B format 0-255
- *
- * \param color_asked array(R,G,B) or string (named color or '#AABBCC')
- */
- function SetRGBColor($color_asked)
- {
- if ($color_asked == '') { $color_asked = array(0, 0, 0); };
- if ( count($color_asked) == 3 ) { // already array of 3 rgb
- $ret_val = $color_asked;
- } else { // asking for a color by string
- if(substr($color_asked, 0, 1) == '#') { // asking in #FFFFFF format.
- $ret_val = array(hexdec(substr($color_asked, 1, 2)), hexdec(substr($color_asked, 3, 2)),
- hexdec(substr($color_asked, 5, 2)));
- } else { // asking by color name
- $ret_val = $this->rgb_array[$color_asked];
- }
- }
- return $ret_val;
- }
- /*!
- * Sets the colors for the data.
- */
- function SetDataColors($which_data = NULL, $which_border = NULL)
- {
- if (is_null($which_data) && is_array($this->data_colors)) {
- // use already set data_colors
- } else if (! is_array($which_data)) {
- $this->data_colors = ($which_data) ? array($which_data) : array('blue', 'red', 'green', 'orange');
- } else {
- $this->data_colors = $which_data;
- }
- $i = 0;
- foreach ($this->data_colors as $col) {
- $this->ndx_data_colors[$i] = $this->SetIndexColor($col);
- $this->ndx_data_dark_colors[$i] = $this->SetIndexDarkColor($col);
- $i++;
- }
- // For past compatibility:
- $this->SetDataBorderColors($which_border);
- } // function SetDataColors()
- /*!
- *
- */
- function SetDataBorderColors($which_br = NULL)
- {
- if (is_null($which_br) && is_array($this->data_border_colors)) {
- // use already set data_border_colors
- } else if (! is_array($which_br)) {
- // Create new array with specified color
- $this->data_border_colors = ($which_br) ? array($which_br) : array('black');
- } else {
- $this->data_border_colors = $which_br;
- }
- $i = 0;
- foreach($this->data_border_colors as $col) {
- $this->ndx_data_border_colors[$i] = $this->SetIndexColor($col);
- $i++;
- }
- } // function SetDataBorderColors()
- /*!
- * Sets the colors for the data error bars.
- */
- function SetErrorBarColors($which_err = NULL)
- {
- if (is_null($which_err) && is_array($this->error_bar_colors)) {
- // use already set error_bar_colors
- } else if (! is_array($which_err)) {
- $this->error_bar_colors = ($which_err) ? array($which_err) : array('black');
- } else {
- $this->error_bar_colors = $which_err;
- }
- $i = 0;
- foreach($this->error_bar_colors as $col) {
- $this->ndx_error_bar_colors[$i] = $this->SetIndexColor($col);
- $i++;
- }
- return TRUE;
- } // function SetErrorBarColors()
- /*!
- * Sets the default dashed style.
- * \param which_style A string specifying order of colored and transparent dots,
- * i.e: '4-3' means 4 colored, 3 transparent;
- * '2-3-1-2' means 2 colored, 3 transparent, 1 colored, 2 transparent.
- */
- function SetDefaultDashedStyle($which_style)
- {
- // String: "numcol-numtrans-numcol-numtrans..."
- $asked = explode('-', $which_style);
- if (count($asked) < 2) {
- $this->DrawError("SetDefaultDashedStyle(): Wrong parameter '$which_style'.");
- return FALSE;
- }
- // Build the string to be eval()uated later by SetDashedStyle()
- $this->default_dashed_style = 'array( ';
- $t = 0;
- foreach($asked as $s) {
- if ($t % 2 == 0) {
- $this->default_dashed_style .= str_repeat('$which_ndxcol,', $s);
- } else {
- $this->default_dashed_style .= str_repeat('IMG_COLOR_TRANSPARENT,', $s);
- }
- $t++;
- }
- // Remove trailing comma and add closing parenthesis
- $this->default_dashed_style = substr($this->default_dashed_style, 0, -1);
- $this->default_dashed_style .= ')';
- return TRUE;
- }
- /*!
- * Sets the style before drawing a dashed line. Defaults to $this->default_dashed_style
- * \param which_ndxcol Color index to be used.
- */
- function SetDashedStyle($which_ndxcol)
- {
- // See SetDefaultDashedStyle() to understand this.
- eval ("\$style = $this->default_dashed_style;");
- return imagesetstyle($this->img, $style);
- }
- /*!
- * Sets line widths on a per-line basis.
- */
- function SetLineWidths($which_lw=NULL)
- {
- if (is_null($which_lw)) {
- // Do nothing, use default value.
- } else if (is_array($which_lw)) {
- // Did we get an array with line widths?
- $this->line_widths = $which_lw;
- } else {
- $this->line_widths = array($which_lw);
- }
- return TRUE;
- }
- /*!
- *
- */
- function SetLineStyles($which_ls=NULL)
- {
- if (is_null($which_ls)) {
- // Do nothing, use default value.
- } else if (! is_array($which_ls)) {
- // Did we get an array with line styles?
- $this->line_styles = $which_ls;
- } else {
- $this->line_styles = ($which_ls) ? array($which_ls) : array('solid');
- }
- return TRUE;
- }
- /////////////////////////////////////////////
- ////////////// FONTS
- /////////////////////////////////////////////
- /*!
- * Sets number of pixels between lines of the same text.
- */
- function SetLineSpacing($which_spc)
- {
- $this->line_spacing = $which_spc;
- }
- /*!
- * Enables use of TrueType fonts in the graph. Font initialisation methods
- * depend on this setting, so when called, SetUseTTF() resets the font
- * settings
- */
- function SetUseTTF($which_ttf)
- {
- $this->use_ttf = $which_ttf;
- if ($which_ttf)
- $this->SetDefaultFonts();
- return TRUE;
- }
- /*!
- * Sets the directory name to look into for TrueType fonts.
- */
- function SetTTFPath($which_path)
- {
- // Maybe someone needs really dynamic config. He'll need this:
- // clearstatcache();
- if (is_dir($which_path) && is_readable($which_path)) {
- $this->ttf_path = $which_path;
- return TRUE;
- } else {
- $this->PrintError("SetTTFPath(): $which_path is not a valid path.");
- return FALSE;
- }
- }
- /*!
- * Sets the default TrueType font and updates all fonts to that.
- */
- function SetDefaultTTFont($which_font)
- {
- if (is_file($which_font) && is_readable($which_font)) {
- $this->default_ttfont = $which_font;
- return $this->SetDefaultFonts();
- } else {
- $this->PrintError("SetDefaultTTFont(): $which_font is not a valid font file.");
- return FALSE;
- }
- }
- /*!
- * Sets fonts to their defaults
- */
- function SetDefaultFonts()
- {
- // TTF:
- if ($this->use_ttf) {
- //$this->SetTTFPath(dirname($_SERVER['PHP_SELF']));
- $this->SetTTFPath(getcwd());
- $this->SetFont('generic', $this->default_ttfont, 8);
- $this->SetFont('title', $this->default_ttfont, 14);
- $this->SetFont('legend', $this->default_ttfont, 8);
- $this->SetFont('x_label', $this->default_ttfont, 6);
- $this->SetFont('y_label', $this->default_ttfont, 6);
- $this->SetFont('x_title', $this->default_ttfont, 10);
- $this->SetFont('y_title', $this->default_ttfont, 10);
- }
- // Fixed:
- else {
- $this->SetFont('generic', 2);
- $this->SetFont('title', 5);
- $this->SetFont('legend', 2);
- $this->SetFont('x_label', 1);
- $this->SetFont('y_label', 1);
- $this->SetFont('x_title', 3);
- $this->SetFont('y_title', 3);
- }
- return TRUE;
- }
- /*!
- * Sets Fixed/Truetype font parameters.
- * \param $which_elem Is the element whose font is to be changed.
- * It can be one of 'title', 'legend', 'generic',
- * 'x_label', 'y_label', x_title' or 'y_title'
- * \param $which_font Can be a number (for fixed font sizes) or
- * a string with the filename when using TTFonts.
- * \param $which_size Point size (TTF only)
- * Calculates and updates internal height and width variables.
- */
- function SetFont($which_elem, $which_font, $which_size = 12)
- {
- // TTF:
- if ($this->use_ttf) {
- $path = $this->ttf_path.'/'.$which_font;
- if (! is_file($path) || ! is_readable($path) ) {
- $this->DrawError("SetFont(): True Type font $path doesn't exist");
- return FALSE;
- }
- switch ($which_elem) {
- case 'generic':
- $this->generic_font['font'] = $path;
- $this->generic_font['size'] = $which_size;
- break;
- case 'title':
- $this->title_font['font'] = $path;
- $this->title_font['size'] = $which_size;
- break;
- case 'legend':
- $this->legend_font['font'] = $path;
- $this->legend_font['size'] = $which_size;
- break;
- case 'x_label':
- $this->x_label_font['font'] = $path;
- $this->x_label_font['size'] = $which_size;
- break;
- case 'y_label':
- $this->y_label_font['font'] = $path;
- $this->y_label_font['size'] = $which_size;
- break;
- case 'x_title':
- $this->x_title_font['font'] = $path;
- $this->x_title_font['size'] = $which_size;
- break;
- case 'y_title':
- $this->y_title_font['font'] = $path;
- $this->y_title_font['size'] = $which_size;
- break;
- default:
- $this->DrawError("SetFont(): Unknown element '$which_elem' specified.");
- return FALSE;
- }
- return TRUE;
- }
- // Fixed fonts:
- if ($which_font > 5 || $which_font < 0) {
- $this->DrawError('SetFont(): Non-TTF font size must be 1, 2, 3, 4 or 5');
- return FALSE;
- }
- switch ($which_elem) {
- case 'generic':
- $this->generic_font['font'] = $which_font;
- $this->generic_font['height'] = ImageFontHeight($which_font);
- $this->generic_font['width'] = ImageFontWidth($which_font);
- break;
- case 'title':
- $this->title_font['font'] = $which_font;
- $this->title_font['height'] = ImageFontHeight($which_font);
- $this->title_font['width'] = ImageFontWidth($which_font);
- break;
- case 'legend':
- $this->legend_font['font'] = $which_font;
- $this->legend_font['height'] = ImageFontHeight($which_font);
- $this->legend_font['width'] = ImageFontWidth($which_font);
- break;
- case 'x_label':
- $this->x_label_font['font'] = $which_font;
- $this->x_label_font['height'] = ImageFontHeight($which_font);
- $this->x_label_font['width'] = ImageFontWidth($which_font);
- break;
- case 'y_label':
- $this->y_label_font['font'] = $which_font;
- $this->y_label_font['height'] = ImageFontHeight($which_font);
- $this->y_label_font['width'] = ImageFontWidth($which_font);
- break;
- case 'x_title':
- $this->x_title_font['font'] = $which_font;
- $this->x_title_font['height'] = ImageFontHeight($which_font);
- $this->x_title_font['width'] = ImageFontWidth($which_font);
- break;
- case 'y_title':
- $this->y_title_font['font'] = $which_font;
- $this->y_title_font['height'] = ImageFontHeight($which_font);
- $this->y_title_font['width'] = ImageFontWidth($which_font);
- break;
- default:
- $this->DrawError("SetFont(): Unknown element '$which_elem' specified.");
- return FALSE;
- }
- return TRUE;
- }
- /*!
- * Returns an array with the size of the bounding box of an
- * arbitrarily placed (rotated) TrueType text string.
- */
- function TTFBBoxSize($size, $angle, $font, $string)
- {
- // First, assume angle < 90
- $arr = ImageTTFBBox($size, 0, $font, $string);
- $flat_width = $arr[2] - $arr[0];
- $flat_height = abs($arr[3] - $arr[5]);
- // Now the bounding box
- $angle = deg2rad($angle);
- $width = ceil(abs($flat_width*cos($angle) + $flat_height*sin($angle))); //Must be integer
- $height = ceil(abs($flat_width*sin($angle) + $flat_height*cos($angle))); //Must be integer
- return array($width, $height);
- }
- /*!
- * Draws a string of text. Horizontal and vertical alignment are relative to
- * to the drawing. That is: vertical text (90 deg) gets centered along y-axis
- * with v_align = 'center', and adjusted to the left of x-axis with h_align = 'right',
- *
- * \note Original multiple lines code submitted by Remi Ricard.
- * \note Original vertical code submitted by Marlin Viss.
- */
- function DrawText($which_font, $which_angle, $which_xpos, $which_ypos, $which_color, $which_text,
- $which_halign = 'left', $which_valign = 'bottom')
- {
- // TTF:
- if ($this->use_ttf) {
- $size = $this->TTFBBoxSize($which_font['size'], $which_angle, $which_font['font'], $which_text);
- $rads = deg2rad($which_angle);
- if ($which_valign == 'center')
- $which_ypos += $size[1]/2;
- if ($which_valign == 'bottom')
- $which_ypos += $size[1];
- if ($which_halign == 'center')
- $which_xpos -= ($size[0]/2) * cos($rads);
- if ($which_halign == 'left')
- $which_xpos += $size[0] * sin($rads);
- if ($which_halign == 'right')
- $which_xpos -= $size[0] * cos($rads);
- ImageTTFText($this->img, $which_font['size'], $which_angle,
- $which_xpos, $which_ypos, $which_color, $which_font['font'], $which_text);
- }
- // Fixed fonts:
- else {
- // Split the text by its lines, and count them
- $which_text = ereg_replace("\r", "", $which_text);
- $str = split("\n", $which_text);
- $nlines = count($str);
- $spacing = $this->line_spacing * ($nlines - 1);
- // Vertical text:
- // (Remember the alignment convention with vertical text)
- if ($which_angle == 90) {
- // The text goes around $which_xpos.
- if ($which_halign == 'center')
- $which_xpos -= ($nlines * ($which_font['height'] + $spacing))/2;
- // Left alignment requires no modification to $xpos...
- // Right-align it. $which_xpos designated the rightmost x coordinate.
- else if ($which_halign == 'right')
- $which_xpos += ($nlines * ($which_font['height'] + $spacing));
- $ypos = $which_ypos;
- for($i = 0; $i < $nlines; $i++) {
- // Center the text vertically around $which_ypos (each line)
- if ($which_valign == 'center')
- $ypos = $which_ypos + (strlen($str[$i]) * $which_font['width']) / 2;
- // Make the text finish (vertically) at $which_ypos
- if ($which_valign == 'bottom')
- $ypos = $which_ypos + strlen($str[$i]) * $which_font['width'];
- ImageStringUp($this->img, $which_font['font'],
- $i * ($which_font['height'] + $spacing) + $which_xpos,
- $ypos, $str[$i], $which_color);
- }
- }
- // Horizontal text:
- else {
- // The text goes above $which_ypos
- if ($which_valign == 'top')
- $which_ypos -= $nlines * ($which_font['height'] + $spacing);
- // The text is centered around $which_ypos
- if ($which_valign == 'center')
- $which_ypos -= ($nlines * ($which_font['height'] + $spacing))/2;
- // valign = 'bottom' requires no modification
- $xpos = $which_xpos;
- for($i = 0; $i < $nlines; $i++) {
- // center the text around $which_xpos
- if ($which_halign == 'center')
- $xpos = $which_xpos - (strlen($str[$i]) * $which_font['width'])/2;
- // make the text finish at $which_xpos
- if ($which_halign == 'right')
- $xpos = $which_xpos - strlen($str[$i]) * $which_font['width'];
- ImageString($this->img, $which_font['font'], $xpos,
- $i * ($which_font['height'] + $spacing) + $which_ypos,
- $str[$i], $which_color);
- }
- }
- }
- return TRUE;
- } // function DrawText()
- /////////////////////////////////////////////
- /////////// INPUT / OUTPUT CONTROL
- /////////////////////////////////////////////
- /*!
- * Sets output file format.
- */
- function SetFileFormat($format)
- {
- $asked = $this->CheckOption($format, 'jpg, png, gif, wbmp', __FUNCTION__);
- switch ($asked) {
- case 'jpg':
- if (imagetypes() & IMG_JPG)
- $this->file_format = 'jpg';
- return TRUE;
- break;
- case 'png':
- if (imagetypes() & IMG_PNG)
- $this->file_format = 'png';
- return TRUE;
- break;
- case 'gif':
- if (imagetypes() & IMG_GIF)
- $this->file_format = 'gif';
- return TRUE;
- break;
- case 'wbmp':
- if (imagetypes() & IMG_WBMP)
- $this->file_format = 'wbmp';
- return TRUE;
- break;
- default:
- $this->PrintError("SetFileFormat():File format '$format' not supported");
- return FALSE;
- }
- }
- /*!
- * Selects an input file to be used as graph background and scales or tiles this image
- * to fit the sizes.
- * \param input_file string Path to the file to be used (jpeg, png and gif accepted)
- * \param mode string 'centeredtile', 'tile', 'scale' (the image to the graph's size)
- */
- function SetBgImage($input_file, $mode='centeredtile')
- {
- $this->bgmode = $this->CheckOption($mode, 'tile, centeredtile, scale', __FUNCTION__);
- $this->bgimg = $input_file;
- }
- /*!
- * Selects an input file to be used as plot area background and scales or tiles this image
- * to fit the sizes.
- * \param input_file string Path to the file to be used (jpeg, png and gif accepted)
- * \param mode string 'centeredtile', 'tile', 'scale' (the image to the graph's size)
- */
- function SetPlotAreaBgImage($input_file, $mode='tile')
- {
- $this->plotbgmode = $this->CheckOption($mode, 'tile, centeredtile, scale', __FUNCTION__);
- $this->plotbgimg = $input_file;
- }
- /*!
- * Sets the name of the file to be used as output file.
- */
- function SetOutputFile($which_output_file)
- {
- $this->output_file = $which_output_file;
- return TRUE;
- }
- /*!
- * Sets the output image as 'inline', that is: no Content-Type headers are sent
- * to the browser. Needed if you want to embed the images.
- */
- function SetIsInline($which_ii)
- {
- $this->is_inline = (bool)$which_ii;
- return TRUE;
- }
- /*!
- * Performs the actual outputting of the generated graph, and
- * destroys the image resource.
- */
- function PrintImage()
- {
- // Browser cache stuff submitted by Thiemo Nagel
- if ( (! $this->browser_cache) && (! $this->is_inline)) {
- header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
- header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
- header('Cache-Control: no-cache, must-revalidate');
- header('Pragma: no-cache');
- }
- switch($this->file_format) {
- case 'png':
- if (! $this->is_inline) {
- Header('Content-type: image/png');
- }
- if ($this->is_inline && $this->output_file != '') {
- ImagePng($this->img, $this->output_file);
- } else {
- ImagePng($this->img);
- }
- break;
- case 'jpg':
- if (! $this->is_inline) {
- Header('Content-type: image/jpeg');
- }
- if ($this->is_inline && $this->output_file != '') {
- ImageJPEG($this->img, $this->output_file);
- } else {
- ImageJPEG($this->img);
- }
- break;
- case 'gif':
- if (! $this->is_inline) {
- Header('Content-type: image/gif');
- }
- if ($this->is_inline && $this->output_file != '') {
- ImageGIF($this->img, $this->output_file);
- } else {
- ImageGIF($this->img);
- }
- break;
- case 'wbmp': // wireless bitmap, 2 bit.
- if (! $this->is_inline) {
- Header('Content-type: image/wbmp');
- }
- if ($this->is_inline && $this->output_file != '') {
- ImageWBMP($this->img, $this->output_file);
- } else {
- ImageWBMP($this->img);
- }
- break;
- default:
- $this->PrintError('PrintImage(): Please select an image type!');
- break;
- }
- return TRUE;
- }
- /*!
- * Prints an error message to stdout and dies
- */
- function PrintError($error_message)
- {
- echo "<p><b>Fatal error</b>: $error_message<p>";
- die;
- }
- /*!
- * Prints an error message inline into the generated image and draws it centered
- * around the given coordinates (defaults to center of the image)
- * \param error_message Message to be drawn
- * \param where_x X coordinate
- * \param where_y Y coordinate
- */
- function DrawError($error_message, $where_x = NULL, $where_y = NULL)
- {
- if (! $this->img)
- $this->PrintError('_DrawError(): Warning, no image resource allocated. '.
- 'The message to be written was: '.$error_message);
- $ypos = (! $where_y) ? $this->image_height/2 : $where_y;
- $xpos = (! $where_x) ? $this->image_width/2 : $where_x;
- ImageRectangle($this->img, 0, 0, $this->image_width, $this->image_height,
- ImageColorAllocate($this->img, 255, 255, 255));
- $this->DrawText($this->generic_font, 0, $xpos, $ypos, ImageColorAllocate($this->img, 0, 0, 0),
- $error_message, 'center', 'center');
- $this->PrintImage();
- exit;
- // return TRUE;
- }
- /////////////////////////////////////////////
- /////////// LABELS
- /////////////////////////////////////////////
- /*!
- * Sets position for X labels following data points.
- */
- function SetXDataLabelPos($which_xdlp)
- {
- $this->x_data_label_pos = $this->CheckOption($which_xdlp, 'plotdown, plotup, both, xaxis, all, none',
- __FUNCTION__);
- if ($which_xdlp != 'none')
- $this->x_tick_label_pos = 'none';
- return TRUE;
- }
- /*!
- * Sets position for Y labels following data points.
- */
- function SetYDataLabelPos($which_ydlp)
- {
- $this->y_data_label_pos = $this->CheckOption($which_ydlp, 'plotleft, plotright, both, yaxis, all, none',
- __FUNCTION__);
- if ($which_ydlp != 'none')
- $this->y_tick_label_pos = 'none';
- return TRUE;
- }
- /*!
- * Sets position for X labels following ticks (hence grid lines)
- */
- function SetXTickLabelPos($which_xtlp)
- {
- $this->x_tick_label_pos = $this->CheckOption($which_xtlp, 'plotdown, plotup, both, xaxis, all, none',
- __FUNCTION__);
- if ($which_xtlp != 'none')
- $this->x_data_label_pos = 'none';
- return TRUE;
- }
- /*!
- * Sets position for Y labels following ticks (hence grid lines)
- */
- function SetYTickLabelPos($which_ytlp)
- {
- $this->y_tick_label_pos = $this->CheckOption($which_ytlp, 'plotleft, plotright, both, yaxis, all, none',
- __FUNCTION__);
- if ($which_ytlp != 'none')
- $this->y_data_label_pos = 'none';
- return TRUE;
- }
- /*!
- * Sets type for tick and data labels on X axis.
- * \note 'title' type left for backwards compatibility.
- */
- function SetXLabelType($which_xlt)
- {
- $this->x_label_type = $this->CheckOption($which_xlt, 'data, time, title', __FUNCTION__);
- return TRUE;
- }
- /*!
- * Sets type for tick and data labels on Y axis.
- */
- function SetYLabelType($which_ylt)
- {
- $this->y_label_type = $this->CheckOption($which_ylt, 'data, time', __FUNCTION__);
- return TRUE;
- }
- function SetXTimeFormat($which_xtf)
- {
- $this->x_time_format = $which_xtf;
- return TRUE;
- }
- function SetYTimeFormat($which_ytf)
- {
- $this->y_time_format = $which_ytf;
- return TRUE;
- }
- function SetXLabelAngle($which_xla)
- {
- $this->x_label_angle = $which_xla;
- return TRUE;
- }
- function SetYLabelAngle($which_yla)
- {
- $this->y_label_angle = $which_yla;
- return TRUE;
- }
- /////////////////////////////////////////////
- /////////// MISC
- /////////////////////////////////////////////
- /*!
- * Checks the valididy of an option.
- * \param which_opt String to check.
- * \param which_acc String of accepted choices.
- * \param which_func Name of the calling function, for error messages.
- * \note If checking everywhere for correctness slows things down, we could provide a
- * child class overriding every Set...() method which uses CheckOption(). Those new
- * methods could proceed in the unsafe but faster way.
- */
- function CheckOption($which_opt, $which_acc, $which_func)
- {
- $asked = trim($which_opt);
- // FIXME: this for backward compatibility, as eregi() fails with empty strings.
- if ($asked == '')
- return '';
- $asked = strtolower($asked);
- if (@ eregi($asked, $which_acc)) {
- return $asked;
- } else {
- $this->DrawError("$which_func(): '$which_opt' not in available choices: '$which_acc'.");
- return NULL;
- }
- }
- /*!
- * \note Submitted by Thiemo Nagel
- */
- function SetBrowserCache($which_browser_cache)
- {
- $this->browser_cache = $which_browser_cache;
- return TRUE;
- }
- /*!
- * Whether to show the final image or not
- */
- function SetPrintImage($which_pi)
- {
- $this->print_image = $which_pi;
- return TRUE;
- }
- /*!
- * Sets the graph's legend. If argument is not an array, appends it to the legend.
- */
- function SetLegend($which_leg)
- {
- if (is_array($which_leg)) { // use array
- $this->legend = $which_leg;
- return TRUE;
- } else if (! is_null($which_leg)) { // append string
- $this->legend[] = $which_leg;
- return TRUE;
- } else {
- $this->DrawError("SetLegend(): argument must not be null.");
- return FALSE;
- }
- }
- /*!
- * Specifies the absolute (relative to image's up/left corner) position
- * of the legend's upper/leftmost corner.
- * $which_type not yet used (TODO)
- */
- function SetLegendPixels($which_x, $which_y, $which_type=NULL)
- {
- $this->legend_x_pos = $which_x;
- $this->legend_y_pos = $which_y;
- return TRUE;
- }
- /*!
- * Specifies the relative (to graph's origin) position of the legend's
- * upper/leftmost corner. MUST be called after scales are set up.
- * $which_type not yet used (TODO)
- */
- function SetLegendWorld($which_x, $which_y, $which_type=NULL)
- {
- if (! isset($this->scale_is_set))
- $this->CalcTranslation();
- $this->legend_x_pos = $this->xtr($which_x);
- $this->legend_y_pos = $this->ytr($which_y);
- return TRUE;
- }
- /*!
- * Accepted values are: left, sides, none, full
- */
- function SetPlotBorderType($pbt)
- {
- $this->plot_border_type = $this->CheckOption($pbt, 'left, sides, none, full', __FUNCTION__);
- }
- /*!
- * Accepted values are: raised, plain
- */
- function SetImageBorderType($sibt)
- {
- $this->image_border_type = $this->CheckOption($sibt, 'raised, plain', __FUNCTION__);
- }
- /*!
- * \param dpab bool
- */
- function SetDrawPlotAreaBackground($dpab)
- {
- $this->draw_plot_area_background = (bool)$dpab;
- }
- /*!
- * \param dyg bool
- */
- function SetDrawYGrid($dyg)
- {
- $this->draw_y_grid = (bool)$dyg;
- return TRUE;
- }
- /*!
- * \param dxg bool
- */
- function SetDrawXGrid($dxg)
- {
- $this->draw_x_grid = (bool)$dxg;
- return TRUE;
- }
- /*!
- * \param ddg bool
- */
- function SetDrawDashedGrid($ddg)
- {
- $this->dashed_grid = (bool)$ddg;
- return TRUE;
- }
- /*!
- * \param dxdl bool
- */
- function SetDrawXDataLabelLine…
Large files files are truncated, but you can click here to view the full file