📄 graph_lib.php
字号:
<?php/** * Graphing class * * SourceForge: Breaking Down the Barriers to Open Source Development * Copyright 1999-2001 (c) VA Linux Systems * http://sourceforge.net * * @version $Id: graph_lib.php,v 1.16 2001/06/18 20:17:55 dbrogdon Exp $ */class Graph { /** * x coordinate title * * @var string $xtitle */ var $xtitle; /** * y coordinate title * * @var string $ytitle */ var $ytitle; /** * @var ? $im */ var $im; /** * @var string $color */ var $color; /** * x coordinate minimum * * @var int $xmin */ var $xmin; /** * x coordinate maximum * * @var int $xmax */ var $xmax; /** * y coordinate minimum * * @var int $ymin */ var $ymin; /** * y coordinate maximum * * @var int $ymax */ var $ymax; /** * x coordinate data diff * * @var ? $xdata_diff */ var $xdata_diff; /** * y coordinate data diff * * @var ? $ydata_diff */ var $ydata_diff; /** * Overall graph height * * @var int $graph_height */ var $graph_height; /** * Overall graph width * * @var int $graph_width */ var $graph_width; /** * x coordinate pad * * @var ? $xpad */ var $xpad; /** * y coordinate pad * * @var ? $ypad */ var $ypad; /** * Image height * * @var int $image_height */ var $image_height; /** * Image width * * @var int $image_width */ var $image_width; /** * Data set * * @var ? $data_set */ var $data_set; /** * Number of data sets * * @var int $num_data_sets */ var $num_data_sets; /** * Number of points * * @var int $num_points */ var $num_points; /** * Debug string * * @var string $strDebug */ var $strDebug; /** * Graph() - Constructor * * The function constructor sets up the basic vars needed to draw a graph. * It sets up the geometry for the graph * as well as any data extents that need to be set. * * @param int Width of the graph * @param int Height of the graph */ function Graph( $width = 640, $height = 480 ) { $this->xpad = 50; $this->ypad = 40; $this->graph_height = $height - (2 * $this->ypad); $this->graph_width = $width - (2 * $this->xpad); $this->image_height = $height; $this->image_width = $width; $this->im = ImageCreate($this->image_width, $this->image_height); $this->color = array(); $this->color['white'] = ImageColorAllocate($this->im,255,255,255); $this->color['black'] = ImageColorAllocate($this->im,0,0,0); $this->color['red'] = ImageColorAllocate($this->im,180,0,0); $this->color['darkred'] = ImageColorAllocate($this->im,120,0,0); $this->color['green'] = ImageColorAllocate($this->im,0,180,0); $this->color['darkgreen'] = ImageColorAllocate($this->im,0,120,0); $this->color['blue'] = ImageColorAllocate($this->im,0,0,180); $this->color['darkblue'] = ImageColorAllocate($this->im,0,0,120); $this->color['gray'] = ImageColorAllocate($this->im,180,180,180); $this->color['darkgray'] = ImageColorAllocate($this->im,64,64,64); $this->color['magenta'] = ImageColorAllocate($this->im,240,0,240); $this->color['darkmagenta'] = ImageColorAllocate($this->im,180,0,180); } // function Graph Constructor /** * SetPad() - Redefines the x and y padding distances on the object. * * @param int The x distance on either side reserved for markings. * @param int The y distance on the top and bottom reserved for markings. */ function SetPads( $xpad = 50, $ypad = 40 ) { $this->xpad = $xpad; $this->ypad = $ypad; $this->graph_height = $this->image_height - (2 * $this->ypad); $this->graph_width = $this->image_width - (2 * $this->xpad); } /** * AddData() - Adds an array of prefetched data to this object. * * @param string The x data to add * @param string The y data to add * @param string The x label to add */ function AddData( $xdata, $ydata, $xlabel = 0 ) { $this->num_data_sets++; //asort( $xdata ); $i = 0; $this->strDebug[] = "Adding dataset " . $this->num_data_sets . " " . ($xlabel ? "with a label" : "without a label") . " to the datasets."; while (list($index,$val) = each($xdata)) { $this->data_set[$this->num_data_sets]['x'][$i] = $xdata[$index]; $this->data_set[$this->num_data_sets]['y'][$i] = $ydata[$index]; if ( $xlabel ) { $this->data_set[$this->num_data_sets]['xlabel'][$i] = $xlabel[$index]; } else { $this->data_set[$this->num_data_sets]['xlabel'][$i] = $xdata[$index]; } ++$i; } $this->num_points[$this->num_data_sets] = min(sizeof($xdata),sizeof($ydata)); if ($this->num_data_sets == 1) { $this->xmax = max($xdata); $this->xmin = min($xdata); $this->ymax = max($ydata); $this->ymin = (min($ydata) < 0) ? min($ydata) : 0; } else { $tmp_xmax = max($xdata); $tmp_xmin = min($xdata); $tmp_ymax = max($ydata); $tmp_ymin = min($ydata); $this->xmax = max($this->xmax,$tmp_xmax); $this->xmin = min($this->xmin,$tmp_xmin); $this->ymax = max($this->ymax,$tmp_ymax); $this->ymin = (min($this->ymin,$tmp_ymin) < 0 ) ? min($this->ymin,$tmp_ymin) : 0; } $this->xdata_diff = (($this->xmax) - ($this->xmin)); $this->ydata_diff = (($this->ymax) - ($this->ymin)); return $this->num_data_sets; } // function AddData /** * translate() - Translate shifts the $x and $y arguments from points in the world space to * pixels in graph plane space. * * @param int The x position in world coordinates * @param int The y position in world coordinates * @param int The x position in screen pixels * @param int The y position in screen pixels */ function translate( &$x, &$y, &$xpos, &$ypos ) { $xpos = $this->xdata_diff ? ($this->graph_width / $this->xdata_diff) * ($x - $this->xmin) + $this->xpad : 0 + $this->xpad; $ypos = $this->ydata_diff ? ((($this->ymax - $y) / $this->ydata_diff) * $this->graph_height) + $this->ypad : 0 + $this->ypad; } /** * adjustNum() - Makes a number better for axis spacing. * Instead of having something like .12452314 it should be closer to .12 for presentability * * @param int This is the number you want to adjust * @param int This is the total range of values that are spanned by this axis * @param int How many divisions will there be? */ function adjustNum ( $num, $data_diff, $num_divisions ) { $data_diff = abs($data_diff); $adjusted = $data_diff / $num_divisions; if ( $num == 0 ) { return $num; } elseif ($adjusted >= 1) { $decimals = strlen(floor($adjusted)."") - 1; $divisor = pow(10,$decimals); $num = floor($num / $divisor) * $divisor; } else { list($zero,$adjusted) = explode(".",$adjusted); $decimals = strlen(floor(1/$num) . "") + 1; $divisor = pow(10,$decimals); $num = round($num * $divisor) / $divisor; } return $num; } // function adjustNum /** * DrawLine() - Draws a line with point value inputs. * This translates the input coordinates into screen space and draws a line * * @param int The first point's x coordinate (in the world coordinate view) * @param int The first point's y coordinate (in the world coordinate view) * @param int The second point's x coordinate (in the world coordinate view) * @param int The second point's y coordinate (in the world coordinate view) * @param string The color to draw the line */ function DrawLine ( $x1, $y1, $x2, $y2, $color ) { $this->translate( $x1, $y1, $x1pos, $y1pos ); $this->translate( $x2, $y2, $x2pos, $y2pos ); ImageLine( $this->im, $x1pos, $y1pos, $x2pos, $y2pos, $color ); } /** * DrawFilledPolygon() - Is a wrapper to the imageFilledPolygon GD function. * * @param array The vertices for the polygon * @param string The color you want the polygon to be */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -