| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file holds the base Graph class for doing |
| 4 |
* graphs on an XY coordinate system. |
| 5 |
* |
| 6 |
* $Id: SVGXYGraph.inc,v 1.7 2003/03/04 22:18:27 hemna Exp $ |
| 7 |
* |
| 8 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
| 9 |
* @package phpHtmlLib |
| 10 |
* @subpackage SVG |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* we need the SVGGraph class |
| 15 |
*/ |
| 16 |
require_once($phphtmllib."/widgets/svg/SVGGraph.inc"); |
| 17 |
|
| 18 |
/** |
| 19 |
* This is the base Graph class for doing XY coordinate |
| 20 |
* system based graphs in SVG. |
| 21 |
* |
| 22 |
* $Id: SVGXYGraph.inc,v 1.7 2003/03/04 22:18:27 hemna Exp $ |
| 23 |
* |
| 24 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
| 25 |
* @package phpHtmlLib |
| 26 |
* @subpackage SVG |
| 27 |
*/ |
| 28 |
class SVGXYGraph extends SVGGraph { |
| 29 |
|
| 30 |
/** |
| 31 |
* this holds the X axis data |
| 32 |
*/ |
| 33 |
var $_x_data = array(); |
| 34 |
var $_min_x = 0; |
| 35 |
var $_max_x = 0; |
| 36 |
|
| 37 |
/** |
| 38 |
* this holds the Y axis data |
| 39 |
*/ |
| 40 |
var $_y_data = array(); |
| 41 |
var $_min_y = 0; |
| 42 |
var $_max_y = 0; |
| 43 |
|
| 44 |
/** |
| 45 |
* holds the colors for each of |
| 46 |
* the lines |
| 47 |
*/ |
| 48 |
var $_colors = array(); |
| 49 |
|
| 50 |
|
| 51 |
function SVGXYGraph($title, $width, $height) { |
| 52 |
|
| 53 |
//set up the options. |
| 54 |
$this->_add_options(); |
| 55 |
|
| 56 |
$this->set_title($title); |
| 57 |
$this->set_width($width); |
| 58 |
$this->set_height($height); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* This is a stub for building the title(s) |
| 64 |
* for the graph |
| 65 |
* |
| 66 |
* @return Container |
| 67 |
*/ |
| 68 |
function _build_titles() { |
| 69 |
$container = container( |
| 70 |
$this->_build_title(), |
| 71 |
$this->_x_axis_title(), |
| 72 |
$this->_y_axis_title()); |
| 73 |
|
| 74 |
return $container; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* This function does the work of |
| 80 |
* building the graph itself |
| 81 |
* |
| 82 |
* @return Container |
| 83 |
*/ |
| 84 |
function _build_graph() { |
| 85 |
$container = container(); |
| 86 |
|
| 87 |
//now build the background gradient? |
| 88 |
$container->add( $this->_background() ); |
| 89 |
|
| 90 |
//now render the lines |
| 91 |
$container->add( $this->_build_x_axis() ); |
| 92 |
$container->add( $this->_build_y_axis() ); |
| 93 |
|
| 94 |
$container->add( $this->graph_data() ); |
| 95 |
return $container; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* this method adds our own options that |
| 100 |
* are different from the parent class |
| 101 |
*/ |
| 102 |
function _add_options() { |
| 103 |
|
| 104 |
//the style for the text for the |
| 105 |
//x/y/z axis titles |
| 106 |
$this->set_option("axis_title_style", |
| 107 |
"font-size:12px;font-weight:bold;". |
| 108 |
"font-family: geneva, trebuchet ms"); |
| 109 |
|
| 110 |
$this->set_option("graph_background", "#EEEEEE"); |
| 111 |
|
| 112 |
//the fill color for the axis grid lines |
| 113 |
$this->set_option("axis_grid_line_color", "#999999"); |
| 114 |
$this->set_option("border_color", "#000000"); |
| 115 |
|
| 116 |
//the title for the axis |
| 117 |
$this->set_option("x_title", ""); |
| 118 |
$this->set_option("y_title", ""); |
| 119 |
|
| 120 |
//the # of divisions/lines for the x axis |
| 121 |
$this->set_option("x_grid_lines", 0); |
| 122 |
$this->set_option("x_grid_lines_on", FALSE); |
| 123 |
$this->set_option("y_grid_lines", 0); |
| 124 |
$this->set_option("y_grid_lines_on", FALSE); |
| 125 |
|
| 126 |
$this->set_option("x_tickmark_length", 3); |
| 127 |
$this->set_option("y_tickmark_length", 3); |
| 128 |
|
| 129 |
$this->set_option("x1_margin", 0); |
| 130 |
$this->set_option("x2_margin", 0); |
| 131 |
$this->set_option("y1_margin", 5); |
| 132 |
$this->set_option("y2_margin", 0); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* This function is used to set the |
| 137 |
* title for the X Axis |
| 138 |
* |
| 139 |
* @param string - the title |
| 140 |
*/ |
| 141 |
function set_x_title( $title ) { |
| 142 |
$this->set_option("x_title", $title); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* This function is used to set the |
| 147 |
* title for the Y Axis |
| 148 |
* |
| 149 |
* @param string - the title |
| 150 |
*/ |
| 151 |
function set_y_title( $title ) { |
| 152 |
$this->set_option("y_title", $title); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* This function is used to add |
| 157 |
* a new set of x values for the graph. |
| 158 |
* |
| 159 |
* NOTE: this must be a comma delimited |
| 160 |
* list of values. |
| 161 |
* |
| 162 |
* @param string - the values |
| 163 |
*/ |
| 164 |
function add_x_values($x_values) { |
| 165 |
$this->_x_data[] = $x_values; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* This function is used to add |
| 170 |
* a new set of y values for the graph. |
| 171 |
* |
| 172 |
* NOTE: this must be a comma delimited |
| 173 |
* list of values. |
| 174 |
* |
| 175 |
* @param string - the values |
| 176 |
*/ |
| 177 |
function add_y_values($y_values) { |
| 178 |
$this->_y_data[] = $y_values; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* This enables/disables the rendering |
| 184 |
* of the grid lines for an axis |
| 185 |
* |
| 186 |
* @param string - the axis, x/y only |
| 187 |
* @param boolean - TRUE = Enabled |
| 188 |
*/ |
| 189 |
function set_grid_line_flag($axis, $flag=TRUE) { |
| 190 |
$axis = strtolower($axis); |
| 191 |
switch ($axis) { |
| 192 |
case "x": |
| 193 |
case "y": |
| 194 |
$this->set_option($axis."_grid_lines_on", $flag); |
| 195 |
break; |
| 196 |
default: |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* This is used to set how many grid lines |
| 203 |
* to draw for an axis |
| 204 |
* |
| 205 |
* @param string - the axis, x/y only |
| 206 |
* @param int - how many lines |
| 207 |
*/ |
| 208 |
function set_axis_grid_points($axis, $points) { |
| 209 |
$axis = strtolower($axis); |
| 210 |
switch ($axis) { |
| 211 |
case "x": |
| 212 |
case "y": |
| 213 |
$this->set_option($axis."_grid_lines",$points); |
| 214 |
break; |
| 215 |
default: |
| 216 |
break; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* This allows you to set the color of the |
| 222 |
* axis grid lines |
| 223 |
* |
| 224 |
* @param string - the color name |
| 225 |
*/ |
| 226 |
function set_grid_line_color( $color ) { |
| 227 |
$this->set_option("axis_grid_line_color", $color); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* We need this to calculate the min,max values for |
| 232 |
* both all x points and all y data points, so we |
| 233 |
* can scale the graph correctly |
| 234 |
*/ |
| 235 |
function _prepare_data() { |
| 236 |
$_min_x = array(); |
| 237 |
$_min_y = array(); |
| 238 |
|
| 239 |
$_max_x = array(); |
| 240 |
$_max_y = array(); |
| 241 |
|
| 242 |
$num_datas = count($this->_x_data); |
| 243 |
|
| 244 |
for( $i=0; $i<=$num_datas-1; $i++) { |
| 245 |
$x_data = explode(",", $this->_x_data[$i] ); |
| 246 |
$y_data = explode(",", $this->_y_data[$i] ); |
| 247 |
$_min_x[] = min($x_data); |
| 248 |
$_min_y[] = min($y_data); |
| 249 |
$_max_x[] = max($x_data); |
| 250 |
$_max_y[] = max($y_data); |
| 251 |
} |
| 252 |
|
| 253 |
$this->_min_x = min($_min_x); |
| 254 |
$this->_min_y = min($_min_y); |
| 255 |
|
| 256 |
$this->_max_x = max($_max_x); |
| 257 |
$this->_max_y = max($_max_y); |
| 258 |
|
| 259 |
// calculate distance from the left border to the drawing area |
| 260 |
$longest_number = @max($this->y_data); |
| 261 |
|
| 262 |
$this->_draw["x1"] = 2*$this->_options["axis_label_font_height"] + |
| 263 |
(strlen($this->format_label($this->_max_y))+3)*$this->_options["axis_font_width"]; |
| 264 |
|
| 265 |
$this->_draw["x2"] = $this->_options["width"]-1; |
| 266 |
|
| 267 |
$this->_draw["y1"] = $this->_options["title_font_height"] + $this->_options["top_margin"]; |
| 268 |
|
| 269 |
$this->_draw["y2"] = $this->_options["height"] - (2*$this->_options["title_font_height"] + $this->_options["top_margin"]) + |
| 270 |
3*$this->_options["axis_font_height"]; |
| 271 |
|
| 272 |
$this->_draw["draw_width"] = $this->_draw["x2"] - $this->_draw["x1"]; |
| 273 |
$this->_draw["draw_height"] = $this->_draw["y2"] - $this->_draw["y1"]; |
| 274 |
|
| 275 |
// how many ticks will fit |
| 276 |
if (!$this->_options["y_grid_lines"]) { |
| 277 |
$this->_options["y_grid_lines"] = round(($this->_draw["draw_height"])/($this->_options["axis_font_height"]+10)); |
| 278 |
} |
| 279 |
|
| 280 |
if (!$this->_options["x_grid_lines"]) { |
| 281 |
$this->_options["x_grid_lines"] = round(($this->_draw["draw_width"])/($this->_options["axis_font_width"]+10)); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
//calculate the distance between lines |
| 286 |
$this->_draw["x_line_space"] = ($this->_draw["draw_width"]-2)/$this->_options["x_grid_lines"]; |
| 287 |
$this->_draw["y_line_space"] = ($this->_draw["draw_height"]-2)/$this->_options["y_grid_lines"]; |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* This method builds the text title area |
| 293 |
* that lives in the X axis |
| 294 |
* |
| 295 |
* @return TEXTsvgtag |
| 296 |
*/ |
| 297 |
function _x_axis_title() { |
| 298 |
$x = $this->_draw["x2"] - (strlen($this->_options["x_title"])*$this->_options["axis_label_font_width"])/2; |
| 299 |
$y = $this->_options["height"]-$this->_options["bottom_margin"]; |
| 300 |
|
| 301 |
$text = svg_text($x,$y); |
| 302 |
$text->set_style($this->_options["axis_title_style"]); |
| 303 |
$text->add( $this->_options["x_title"] ); |
| 304 |
$text->set_collapse(); |
| 305 |
return $text; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* This method builds the text title area |
| 310 |
* that lives in the X axis |
| 311 |
* |
| 312 |
* @return TEXTsvgtag |
| 313 |
*/ |
| 314 |
function _y_axis_title() { |
| 315 |
$x = 10+$this->_options["left_margin"]; |
| 316 |
$y = $this->_draw["y1"] + (strlen($this->_options["y_title"])*$this->_options["axis_label_font_width"])/2; |
| 317 |
$text = svg_text($x,$y); |
| 318 |
$text->set_style($this->_options["axis_title_style"]); |
| 319 |
$text->set_tag_attribute("transform", "rotate(270,$x,$y)"); |
| 320 |
$text->add( $this->_options["y_title"] ); |
| 321 |
$text->set_collapse(); |
| 322 |
return $text; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
function _background() { |
| 327 |
|
| 328 |
$rect = svg_rect($this->_draw["x1"],$this->_draw["y1"], |
| 329 |
$this->_draw["draw_width"],$this->_draw["draw_height"], |
| 330 |
$this->_options["graph_background"], "black", 2); |
| 331 |
$container = container( $rect ); |
| 332 |
|
| 333 |
return $container; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* This function returns the pixel positions |
| 338 |
* of a data point |
| 339 |
* |
| 340 |
* @param float data point |
| 341 |
* |
| 342 |
* @return int position in pixels |
| 343 |
*/ |
| 344 |
function get_y_position($y) { |
| 345 |
$pos = ($this->_draw["y2"] - $this->_options["y2_margin"]) - ($y - $this->_min_y) * |
| 346 |
$this->_draw["draw_height"] / ($this->_max_y - $this->_min_y); |
| 347 |
|
| 348 |
return $pos; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
/** |
| 353 |
* This function returns the pixel positions |
| 354 |
* of a data point |
| 355 |
* |
| 356 |
* @param float data point |
| 357 |
* |
| 358 |
* @return int position in pixels |
| 359 |
*/ |
| 360 |
function get_x_position($x) { |
| 361 |
$pos = ($this->_draw["x1"] + $this->_options["x1_margin"]) + ($x - $this->_min_x) * |
| 362 |
$this->_draw["draw_width"] / ($this->_max_x - $this->_min_x); |
| 363 |
|
| 364 |
return $pos; |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* This function builds the X axis |
| 369 |
* and its measurement lines/marks |
| 370 |
* |
| 371 |
* @return Container |
| 372 |
*/ |
| 373 |
function _build_x_axis() { |
| 374 |
|
| 375 |
// what is the tick step |
| 376 |
$tick_step = round(($this->_max_x - $this->_min_x)/$this->_options["x_grid_lines"]); |
| 377 |
if ($tick_step<1) $tick_step=1; |
| 378 |
|
| 379 |
$container = container(); |
| 380 |
|
| 381 |
$x = $this->_min_x; |
| 382 |
while ($x <= $this->_max_x) { |
| 383 |
$x_pos = $this->get_x_position($x); |
| 384 |
$y_pos = $this->_draw["y2"] - $this->_options["x_tickmark_length"]; |
| 385 |
|
| 386 |
if ($x > $this->_min_x) { |
| 387 |
$tick = svg_line($x_pos, $this->_draw["y2"]+1, |
| 388 |
$x_pos, $this->_draw["y2"] + $this->_options["x_tickmark_length"]+1, |
| 389 |
$this->_options["border_color"], 1); |
| 390 |
|
| 391 |
$line = svg_line($x_pos, $this->_draw["y2"]-1, |
| 392 |
$x_pos, $this->_draw["y1"]+1, |
| 393 |
$this->_options["axis_grid_line_color"], 1); |
| 394 |
|
| 395 |
$container->add( $tick, $line ); |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
$value = $this->format_label($x); |
| 400 |
|
| 401 |
$text = svg_text($x_pos - (strlen($value)*$this->_options["axis_font_width"]/2), |
| 402 |
$this->_draw["y2"] + $this->_options["x_tickmark_length"] + |
| 403 |
$this->_options["axis_font_height"]*2); |
| 404 |
$text->set_collapse(); |
| 405 |
$text->set_style( "font-size:9;font-family: arial, geneva, trebuchet ms" ); |
| 406 |
$text->add( $value ); |
| 407 |
|
| 408 |
$container->add( $text ); |
| 409 |
|
| 410 |
$x += $tick_step; |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
$rect = svg_rect($this->_draw["x1"],$this->_draw["y1"], |
| 415 |
$this->_draw["draw_width"],$this->_draw["draw_height"], |
| 416 |
"none", "black", 2); |
| 417 |
$container->add( $rect ); |
| 418 |
|
| 419 |
return $container; |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* This function builds the Y axis |
| 424 |
* and its measurement lines/marks |
| 425 |
* |
| 426 |
* @return Container |
| 427 |
*/ |
| 428 |
function _build_y_axis() { |
| 429 |
|
| 430 |
// what is the tick step |
| 431 |
$tick_step = round(($this->_max_y - $this->_min_y)/$this->_options["y_grid_lines"]); |
| 432 |
if ($tick_step<1) $tick_step=1; |
| 433 |
|
| 434 |
$container = container(); |
| 435 |
|
| 436 |
$y = $this->_min_y; |
| 437 |
while( $y <= $this->_max_y ) { |
| 438 |
$y_pos = $this->get_y_position($y); |
| 439 |
$x_pos = $this->_draw["x1"] - $this->_options["y_tickmark_length"]; |
| 440 |
|
| 441 |
if ($y > $this->_min_y) { |
| 442 |
$tick = svg_line($this->_draw["x1"] - $this->_options["y_tickmark_length"], |
| 443 |
$y_pos, $this->_draw["x1"] - 1, $y_pos, |
| 444 |
$this->_options["border_color"],1); |
| 445 |
|
| 446 |
$line = svg_line($this->_draw["x1"] + 1, $y_pos, |
| 447 |
$this->_draw["x2"] - 1, $y_pos, |
| 448 |
$this->_options["axis_grid_line_color"],1); |
| 449 |
|
| 450 |
$container->add( $tick, $line ); |
| 451 |
} |
| 452 |
|
| 453 |
$value = $this->format_label($y); |
| 454 |
|
| 455 |
$text = svg_text($x_pos - (strlen($value) * $this->_options["axis_font_width"]) - 2, |
| 456 |
$y_pos + $this->_options["axis_font_height"]/2); |
| 457 |
$text->set_collapse(); |
| 458 |
$text->set_style( "font-size:9;font-family: arial, geneva, trebuchet ms" ); |
| 459 |
$text->add( $value ); |
| 460 |
|
| 461 |
$container->add( $text ); |
| 462 |
|
| 463 |
$y += $tick_step; |
| 464 |
} |
| 465 |
|
| 466 |
return $container; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
} |
| 471 |
?> |