[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Graph Class. PHP Class to draw line, point, bar, and area graphs, including numeric x-axis and double y-axis. 5 * Version: 1.6.3 6 * Copyright (C) 2000 Herman Veluwenkamp 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 * 22 * Copy of GNU Lesser General Public License at: http://www.gnu.org/copyleft/lesser.txt 23 * Contact author at: [email protected] 24 * 25 * @package core 26 * @subpackage lib 27 */ 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 /* This file contains modifications by Martin Dougiamas 32 * as part of Moodle (http://moodle.com). Modified lines 33 * are marked with "Moodle". 34 */ 35 36 /** 37 * @package moodlecore 38 */ 39 class graph { 40 var $image; 41 var $debug = FALSE; // be careful!! 42 var $calculated = array(); // array of computed values for chart 43 var $parameter = array( // input parameters 44 'width' => 320, // default width of image 45 'height' => 240, // default height of image 46 'file_name' => 'none', // name of file for file to be saved as. 47 // NOTE: no suffix required. this is determined from output_format below. 48 'output_format' => 'PNG', // image output format. 'GIF', 'PNG', 'JPEG'. default 'PNG'. 49 50 'seconds_to_live' => 0, // expiry time in seconds (for HTTP header) 51 'hours_to_live' => 0, // expiry time in hours (for HTTP header) 52 'path_to_fonts' => 'fonts/', // path to fonts folder. don't forget *trailing* slash!! 53 // for WINDOZE this may need to be the full path, not relative. 54 55 'title' => 'Graph Title', // text for graph title 56 'title_font' => 'default.ttf', // title text font. don't forget to set 'path_to_fonts' above. 57 'title_size' => 16, // title text point size 58 'title_colour' => 'black', // colour for title text 59 60 'x_label' => '', // if this is set then this text is printed on bottom axis of graph. 61 'y_label_left' => '', // if this is set then this text is printed on left axis of graph. 62 'y_label_right' => '', // if this is set then this text is printed on right axis of graph. 63 64 'label_size' => 8, // label text point size 65 'label_font' => 'default.ttf', // label text font. don't forget to set 'path_to_fonts' above. 66 'label_colour' => 'gray33', // label text colour 67 'y_label_angle' => 90, // rotation of y axis label 68 69 'x_label_angle' => 90, // rotation of y axis label 70 71 'outer_padding' => 5, // padding around outer text. i.e. title, y label, and x label. 72 'inner_padding' => 0, // padding beteen axis text and graph. 73 'x_inner_padding' => 5, // padding beteen axis text and graph. 74 'y_inner_padding' => 6, // padding beteen axis text and graph. 75 'outer_border' => 'none', // colour of border aound image, or 'none'. 76 'inner_border' => 'black', // colour of border around actual graph, or 'none'. 77 'inner_border_type' => 'box', // 'box' for all four sides, 'axis' for x/y axis only, 78 // 'y' or 'y-left' for y axis only, 'y-right' for right y axis only, 79 // 'x' for x axis only, 'u' for both left and right y axis and x axis. 80 'outer_background' => 'none', // background colour of entire image. 81 'inner_background' => 'none', // background colour of plot area. 82 83 'y_min_left' => 0, // this will be reset to minimum value if there is a value lower than this. 84 'y_max_left' => 0, // this will be reset to maximum value if there is a value higher than this. 85 'y_min_right' => 0, // this will be reset to minimum value if there is a value lower than this. 86 'y_max_right' => 0, // this will be reset to maximum value if there is a value higher than this. 87 'x_min' => 0, // only used if x axis is numeric. 88 'x_max' => 0, // only used if x axis is numeric. 89 90 'y_resolution_left' => 1, // scaling for rounding of y axis max value. 91 // if max y value is 8645 then 92 // if y_resolution is 0, then y_max becomes 9000. 93 // if y_resolution is 1, then y_max becomes 8700. 94 // if y_resolution is 2, then y_max becomes 8650. 95 // if y_resolution is 3, then y_max becomes 8645. 96 // get it? 97 'y_decimal_left' => 0, // number of decimal places for y_axis text. 98 'y_resolution_right' => 2, // ... same for right hand side 99 'y_decimal_right' => 0, // ... same for right hand side 100 'x_resolution' => 2, // only used if x axis is numeric. 101 'x_decimal' => 0, // only used if x axis is numeric. 102 103 'point_size' => 4, // default point size. use even number for diamond or triangle to get nice look. 104 'brush_size' => 4, // default brush size for brush line. 105 'brush_type' => 'circle', // type of brush to use to draw line. choose from the following 106 // 'circle', 'square', 'horizontal', 'vertical', 'slash', 'backslash' 107 'bar_size' => 0.8, // size of bar to draw. <1 bars won't touch 108 // 1 is full width - i.e. bars will touch. 109 // >1 means bars will overlap. 110 'bar_spacing' => 10, // space in pixels between group of bars for each x value. 111 'shadow_offset' => 3, // draw shadow at this offset, unless overidden by data parameter. 112 'shadow' => 'grayCC', // 'none' or colour of shadow. 113 'shadow_below_axis' => true, // whether to draw shadows of bars and areas below the x/zero axis. 114 115 116 'x_axis_gridlines' => 'auto', // if set to a number then x axis is treated as numeric. 117 'y_axis_gridlines' => 6, // number of gridlines on y axis. 118 'zero_axis' => 'none', // colour to draw zero-axis, or 'none'. 119 120 121 'axis_font' => 'default.ttf', // axis text font. don't forget to set 'path_to_fonts' above. 122 'axis_size' => 8, // axis text font size in points 123 'axis_colour' => 'gray33', // colour of axis text. 124 'y_axis_angle' => 0, // rotation of axis text. 125 'x_axis_angle' => 0, // rotation of axis text. 126 127 'y_axis_text_left' => 1, // whether to print left hand y axis text. if 0 no text, if 1 all ticks have text, 128 'x_axis_text' => 1, // if 4 then print every 4th tick and text, etc... 129 'y_axis_text_right' => 0, // behaviour same as above for right hand y axis. 130 131 'x_offset' => 0.5, // x axis tick offset from y axis as fraction of tick spacing. 132 'y_ticks_colour' => 'black', // colour to draw y ticks, or 'none' 133 'x_ticks_colour' => 'black', // colour to draw x ticks, or 'none' 134 'y_grid' => 'line', // grid lines. set to 'line' or 'dash'... 135 'x_grid' => 'line', // or if set to 'none' print nothing. 136 'grid_colour' => 'grayEE', // default grid colour. 137 'tick_length' => 4, // length of ticks in pixels. can be negative. i.e. outside data drawing area. 138 139 'legend' => 'none', // default. no legend. 140 // otherwise: 'top-left', 'top-right', 'bottom-left', 'bottom-right', 141 // 'outside-top', 'outside-bottom', 'outside-left', or 'outside-right'. 142 'legend_offset' => 10, // offset in pixels from graph or outside border. 143 'legend_padding' => 5, // padding around legend text. 144 'legend_font' => 'default.ttf', // legend text font. don't forget to set 'path_to_fonts' above. 145 'legend_size' => 8, // legend text point size. 146 'legend_colour' => 'black', // legend text colour. 147 'legend_border' => 'none', // legend border colour, or 'none'. 148 149 'decimal_point' => '.', // symbol for decimal separation '.' or ',' *european support. 150 'thousand_sep' => ',', // symbol for thousand separation ',' or '' 151 152 ); 153 var $y_tick_labels = null; // array of text values for y-axis tick labels 154 var $offset_relation = null; // array of offsets for different sets of data 155 156 157 // init all text - title, labels, and axis text. 158 function init() { 159 160 /// Moodle mods: overrides the font path and encodings 161 162 global $CFG; 163 164 /// A default.ttf is searched for in this order: 165 /// dataroot/lang/xx_local/fonts 166 /// dataroot/lang/xx/fonts 167 /// dirroot/lang/xx/fonts 168 /// dataroot/lang 169 /// lib/ 170 171 $currlang = current_language(); 172 if (file_exists("$CFG->dataroot/lang/".$currlang."_local/fonts/default.ttf")) { 173 $fontpath = "$CFG->dataroot/lang/".$currlang."_local/fonts/"; 174 } else if (file_exists("$CFG->dataroot/lang/$currlang/fonts/default.ttf")) { 175 $fontpath = "$CFG->dataroot/lang/$currlang/fonts/"; 176 } else if (file_exists("$CFG->dirroot/lang/$currlang/fonts/default.ttf")) { 177 $fontpath = "$CFG->dirroot/lang/$currlang/fonts/"; 178 } else if (file_exists("$CFG->dataroot/lang/default.ttf")) { 179 $fontpath = "$CFG->dataroot/lang/"; 180 } else { 181 $fontpath = "$CFG->libdir/"; 182 } 183 184 $this->parameter['path_to_fonts'] = $fontpath; 185 186 /// End Moodle mods 187 188 189 190 $this->calculated['outer_border'] = $this->calculated['boundary_box']; 191 192 // outer padding 193 $this->calculated['boundary_box']['left'] += $this->parameter['outer_padding']; 194 $this->calculated['boundary_box']['top'] += $this->parameter['outer_padding']; 195 $this->calculated['boundary_box']['right'] -= $this->parameter['outer_padding']; 196 $this->calculated['boundary_box']['bottom'] -= $this->parameter['outer_padding']; 197 198 $this->init_x_axis(); 199 $this->init_y_axis(); 200 $this->init_legend(); 201 $this->init_labels(); 202 203 // take into account tick lengths 204 $this->calculated['bottom_inner_padding'] = $this->parameter['x_inner_padding']; 205 if (($this->parameter['x_ticks_colour'] != 'none') && ($this->parameter['tick_length'] < 0)) 206 $this->calculated['bottom_inner_padding'] -= $this->parameter['tick_length']; 207 $this->calculated['boundary_box']['bottom'] -= $this->calculated['bottom_inner_padding']; 208 209 $this->calculated['left_inner_padding'] = $this->parameter['y_inner_padding']; 210 if ($this->parameter['y_axis_text_left']) { 211 if (($this->parameter['y_ticks_colour'] != 'none') && ($this->parameter['tick_length'] < 0)) 212 $this->calculated['left_inner_padding'] -= $this->parameter['tick_length']; 213 } 214 $this->calculated['boundary_box']['left'] += $this->calculated['left_inner_padding']; 215 216 $this->calculated['right_inner_padding'] = $this->parameter['y_inner_padding']; 217 if ($this->parameter['y_axis_text_right']) { 218 if (($this->parameter['y_ticks_colour'] != 'none') && ($this->parameter['tick_length'] < 0)) 219 $this->calculated['right_inner_padding'] -= $this->parameter['tick_length']; 220 } 221 $this->calculated['boundary_box']['right'] -= $this->calculated['right_inner_padding']; 222 223 // boundaryBox now has coords for plotting area. 224 $this->calculated['inner_border'] = $this->calculated['boundary_box']; 225 226 $this->init_data(); 227 $this->init_x_ticks(); 228 $this->init_y_ticks(); 229 } 230 231 function draw_text() { 232 $colour = $this->parameter['outer_background']; 233 if ($colour != 'none') $this->draw_rectangle($this->calculated['outer_border'], $colour, 'fill'); // graph background 234 235 // draw border around image 236 $colour = $this->parameter['outer_border']; 237 if ($colour != 'none') $this->draw_rectangle($this->calculated['outer_border'], $colour, 'box'); // graph border 238 239 $this->draw_title(); 240 $this->draw_x_label(); 241 $this->draw_y_label_left(); 242 $this->draw_y_label_right(); 243 $this->draw_x_axis(); 244 $this->draw_y_axis(); 245 if ($this->calculated['y_axis_left']['has_data']) $this->draw_zero_axis_left(); // either draw zero axis on left 246 else if ($this->calculated['y_axis_right']['has_data']) $this->draw_zero_axis_right(); // ... or right. 247 $this->draw_legend(); 248 249 // draw border around plot area 250 $colour = $this->parameter['inner_background']; 251 if ($colour != 'none') $this->draw_rectangle($this->calculated['inner_border'], $colour, 'fill'); // graph background 252 253 // draw border around image 254 $colour = $this->parameter['inner_border']; 255 if ($colour != 'none') $this->draw_rectangle($this->calculated['inner_border'], $colour, $this->parameter['inner_border_type']); // graph border 256 } 257 258 function draw_stack() { 259 $this->init(); 260 $this->draw_text(); 261 262 $yOrder = $this->y_order; // save y_order data. 263 // iterate over each data set. order is very important if you want to see data correctly. remember shadows!! 264 foreach ($yOrder as $set) { 265 $this->y_order = array($set); 266 $this->init_data(); 267 $this->draw_data(); 268 } 269 $this->y_order = $yOrder; // revert y_order data. 270 271 $this->output(); 272 } 273 274 function draw() { 275 $this->init(); 276 $this->draw_text(); 277 $this->draw_data(); 278 $this->output(); 279 } 280 281 // draw a data set 282 function draw_set($order, $set, $offset) { 283 if ($offset) @$this->init_variable($colour, $this->y_format[$set]['shadow'], $this->parameter['shadow']); 284 else $colour = $this->y_format[$set]['colour']; 285 @$this->init_variable($point, $this->y_format[$set]['point'], 'none'); 286 @$this->init_variable($pointSize, $this->y_format[$set]['point_size'], $this->parameter['point_size']); 287 @$this->init_variable($line, $this->y_format[$set]['line'], 'none'); 288 @$this->init_variable($brushType, $this->y_format[$set]['brush_type'], $this->parameter['brush_type']); 289 @$this->init_variable($brushSize, $this->y_format[$set]['brush_size'], $this->parameter['brush_size']); 290 @$this->init_variable($bar, $this->y_format[$set]['bar'], 'none'); 291 @$this->init_variable($barSize, $this->y_format[$set]['bar_size'], $this->parameter['bar_size']); 292 @$this->init_variable($area, $this->y_format[$set]['area'], 'none'); 293 294 $lastX = 0; 295 $lastY = 'none'; 296 $fromX = 0; 297 $fromY = 'none'; 298 299 //print "set $set<br />"; 300 //expand_pre($this->calculated['y_plot']); 301 302 foreach ($this->x_data as $index => $x) { 303 //print "index $index<br />"; 304 $thisY = $this->calculated['y_plot'][$set][$index]; 305 $thisX = $this->calculated['x_plot'][$index]; 306 307 //print "$thisX, $thisY <br />"; 308 309 if (($bar!='none') && (string)$thisY != 'none') { 310 if ($relatedset = $this->offset_relation[$set]) { // Moodle 311 $yoffset = $this->calculated['y_plot'][$relatedset][$index]; // Moodle 312 } else { // Moodle 313 $yoffset = 0; // Moodle 314 } // Moodle 315 //$this->bar($thisX, $thisY, $bar, $barSize, $colour, $offset, $set); // Moodle 316 $this->bar($thisX, $thisY, $bar, $barSize, $colour, $offset, $set, $yoffset); // Moodle 317 } 318 319 if (($area!='none') && (((string)$lastY != 'none') && ((string)$thisY != 'none'))) 320 $this->area($lastX, $lastY, $thisX, $thisY, $area, $colour, $offset); 321 322 if (($point!='none') && (string)$thisY != 'none') $this->plot($thisX, $thisY, $point, $pointSize, $colour, $offset); 323 324 if (($line!='none') && ((string)$thisY != 'none')) { 325 if ((string)$fromY != 'none') 326 $this->line($fromX, $fromY, $thisX, $thisY, $line, $brushType, $brushSize, $colour, $offset); 327 328 $fromY = $thisY; // start next line from here 329 $fromX = $thisX; // ... 330 } else { 331 $fromY = 'none'; 332 $fromX = 'none'; 333 } 334 335 $lastX = $thisX; 336 $lastY = $thisY; 337 } 338 } 339 340 function draw_data() { 341 // cycle thru y data to be plotted 342 // first check for drop shadows... 343 foreach ($this->y_order as $order => $set) { 344 @$this->init_variable($offset, $this->y_format[$set]['shadow_offset'], $this->parameter['shadow_offset']); 345 @$this->init_variable($colour, $this->y_format[$set]['shadow'], $this->parameter['shadow']); 346 if ($colour != 'none') $this->draw_set($order, $set, $offset); 347 348 } 349 350 // then draw data 351 foreach ($this->y_order as $order => $set) { 352 $this->draw_set($order, $set, 0); 353 } 354 } 355 356 function draw_legend() { 357 $position = $this->parameter['legend']; 358 if ($position == 'none') return; // abort if no border 359 360 $borderColour = $this->parameter['legend_border']; 361 $offset = $this->parameter['legend_offset']; 362 $padding = $this->parameter['legend_padding']; 363 $height = $this->calculated['legend']['boundary_box_all']['height']; 364 $width = $this->calculated['legend']['boundary_box_all']['width']; 365 $graphTop = $this->calculated['boundary_box']['top']; 366 $graphBottom = $this->calculated['boundary_box']['bottom']; 367 $graphLeft = $this->calculated['boundary_box']['left']; 368 $graphRight = $this->calculated['boundary_box']['right']; 369 $outsideRight = $this->calculated['outer_border']['right']; 370 $outsideBottom = $this->calculated['outer_border']['bottom']; 371 switch ($position) { 372 case 'top-left': 373 $top = $graphTop + $offset; 374 $bottom = $graphTop + $height + $offset; 375 $left = $graphLeft + $offset; 376 $right = $graphLeft + $width + $offset; 377 378 break; 379 case 'top-right': 380 $top = $graphTop + $offset; 381 $bottom = $graphTop + $height + $offset; 382 $left = $graphRight - $width - $offset; 383 $right = $graphRight - $offset; 384 385 break; 386 case 'bottom-left': 387 $top = $graphBottom - $height - $offset; 388 $bottom = $graphBottom - $offset; 389 $left = $graphLeft + $offset; 390 $right = $graphLeft + $width + $offset; 391 392 break; 393 case 'bottom-right': 394 $top = $graphBottom - $height - $offset; 395 $bottom = $graphBottom - $offset; 396 $left = $graphRight - $width - $offset; 397 $right = $graphRight - $offset; 398 break; 399 400 case 'outside-top' : 401 $top = $graphTop; 402 $bottom = $graphTop + $height; 403 $left = $outsideRight - $width - $offset; 404 $right = $outsideRight - $offset; 405 break; 406 407 case 'outside-bottom' : 408 $top = $graphBottom - $height; 409 $bottom = $graphBottom; 410 $left = $outsideRight - $width - $offset; 411 $right = $outsideRight - $offset; 412 break; 413 414 case 'outside-left' : 415 $top = $outsideBottom - $height - $offset; 416 $bottom = $outsideBottom - $offset; 417 $left = $graphLeft; 418 $right = $graphLeft + $width; 419 break; 420 421 case 'outside-right' : 422 $top = $outsideBottom - $height - $offset; 423 $bottom = $outsideBottom - $offset; 424 $left = $graphRight - $width; 425 $right = $graphRight; 426 break; 427 default: // default is top left. no particular reason. 428 $top = $this->calculated['boundary_box']['top']; 429 $bottom = $this->calculated['boundary_box']['top'] + $this->calculated['legend']['boundary_box_all']['height']; 430 $left = $this->calculated['boundary_box']['left']; 431 $right = $this->calculated['boundary_box']['right'] + $this->calculated['legend']['boundary_box_all']['width']; 432 433 } 434 // legend border 435 if($borderColour!='none') $this->draw_rectangle(array('top' => $top, 436 'left' => $left, 437 'bottom' => $bottom, 438 'right' => $right), $this->parameter['legend_border'], 'box'); 439 440 // legend text 441 $legendText = array('points' => $this->parameter['legend_size'], 442 'angle' => 0, 443 'font' => $this->parameter['legend_font'], 444 'colour' => $this->parameter['legend_colour']); 445 446 $box = $this->calculated['legend']['boundary_box_max']['height']; // use max height for legend square size. 447 $x = $left + $padding; 448 $x_text = $x + $box * 2; 449 $y = $top + $padding; 450 451 foreach ($this->y_order as $set) { 452 $legendText['text'] = $this->calculated['legend']['text'][$set]; 453 if ($legendText['text'] != 'none') { 454 // if text exists then draw box and text 455 $boxColour = $this->colour[$this->y_format[$set]['colour']]; 456 457 // draw box 458 ImageFilledRectangle($this->image, $x, $y, $x + $box, $y + $box, $boxColour); 459 460 // draw text 461 $coords = array('x' => $x + $box * 2, 'y' => $y, 'reference' => 'top-left'); 462 $legendText['boundary_box'] = $this->calculated['legend']['boundary_box'][$set]; 463 $this->update_boundaryBox($legendText['boundary_box'], $coords); 464 $this->print_TTF($legendText); 465 $y += $padding + $box; 466 } 467 } 468 469 } 470 471 function draw_y_label_right() { 472 if (!$this->parameter['y_label_right']) return; 473 $x = $this->calculated['boundary_box']['right'] + $this->parameter['y_inner_padding']; 474 if ($this->parameter['y_axis_text_right']) $x += $this->calculated['y_axis_right']['boundary_box_max']['width'] 475 + $this->calculated['right_inner_padding']; 476 $y = ($this->calculated['boundary_box']['bottom'] + $this->calculated['boundary_box']['top']) / 2; 477 478 $label = $this->calculated['y_label_right']; 479 $coords = array('x' => $x, 'y' => $y, 'reference' => 'left-center'); 480 $this->update_boundaryBox($label['boundary_box'], $coords); 481 $this->print_TTF($label); 482 } 483 484 485 function draw_y_label_left() { 486 if (!$this->parameter['y_label_left']) return; 487 $x = $this->calculated['boundary_box']['left'] - $this->parameter['y_inner_padding']; 488 if ($this->parameter['y_axis_text_left']) $x -= $this->calculated['y_axis_left']['boundary_box_max']['width'] 489 + $this->calculated['left_inner_padding']; 490 $y = ($this->calculated['boundary_box']['bottom'] + $this->calculated['boundary_box']['top']) / 2; 491 492 $label = $this->calculated['y_label_left']; 493 $coords = array('x' => $x, 'y' => $y, 'reference' => 'right-center'); 494 $this->update_boundaryBox($label['boundary_box'], $coords); 495 $this->print_TTF($label); 496 } 497 498 function draw_title() { 499 if (!$this->parameter['title']) return; 500 //$y = $this->calculated['outside_border']['top'] + $this->parameter['outer_padding']; 501 $y = $this->calculated['boundary_box']['top'] - $this->parameter['outer_padding']; 502 $x = ($this->calculated['boundary_box']['right'] + $this->calculated['boundary_box']['left']) / 2; 503 $label = $this->calculated['title']; 504 $coords = array('x' => $x, 'y' => $y, 'reference' => 'bottom-center'); 505 $this->update_boundaryBox($label['boundary_box'], $coords); 506 $this->print_TTF($label); 507 } 508 509 function draw_x_label() { 510 if (!$this->parameter['x_label']) return; 511 $y = $this->calculated['boundary_box']['bottom'] + $this->parameter['x_inner_padding']; 512 if ($this->parameter['x_axis_text']) $y += $this->calculated['x_axis']['boundary_box_max']['height'] 513 + $this->calculated['bottom_inner_padding']; 514 $x = ($this->calculated['boundary_box']['right'] + $this->calculated['boundary_box']['left']) / 2; 515 $label = $this->calculated['x_label']; 516 $coords = array('x' => $x, 'y' => $y, 'reference' => 'top-center'); 517 $this->update_boundaryBox($label['boundary_box'], $coords); 518 $this->print_TTF($label); 519 } 520 521 function draw_zero_axis_left() { 522 $colour = $this->parameter['zero_axis']; 523 if ($colour == 'none') return; 524 // draw zero axis on left hand side 525 $this->calculated['zero_axis'] = round($this->calculated['boundary_box']['top'] + ($this->calculated['y_axis_left']['max'] * $this->calculated['y_axis_left']['factor'])); 526 ImageLine($this->image, $this->calculated['boundary_box']['left'], $this->calculated['zero_axis'], $this->calculated['boundary_box']['right'], $this->calculated['zero_axis'], $this->colour[$colour]); 527 } 528 529 function draw_zero_axis_right() { 530 $colour = $this->parameter['zero_axis']; 531 if ($colour == 'none') return; 532 // draw zero axis on right hand side 533 $this->calculated['zero_axis'] = round($this->calculated['boundary_box']['top'] + ($this->calculated['y_axis_right']['max'] * $this->calculated['y_axis_right']['factor'])); 534 ImageLine($this->image, $this->calculated['boundary_box']['left'], $this->calculated['zero_axis'], $this->calculated['boundary_box']['right'], $this->calculated['zero_axis'], $this->colour[$colour]); 535 } 536 537 function draw_x_axis() { 538 $gridColour = $this->colour[$this->parameter['grid_colour']]; 539 $tickColour = $this->colour[$this->parameter['x_ticks_colour']]; 540 $axis_colour = $this->parameter['axis_colour']; 541 $xGrid = $this->parameter['x_grid']; 542 $gridTop = $this->calculated['boundary_box']['top']; 543 $gridBottom = $this->calculated['boundary_box']['bottom']; 544 545 if ($this->parameter['tick_length'] >= 0) { 546 $tickTop = $this->calculated['boundary_box']['bottom'] - $this->parameter['tick_length']; 547 $tickBottom = $this->calculated['boundary_box']['bottom']; 548 $textBottom = $tickBottom + $this->calculated['bottom_inner_padding']; 549 } else { 550 $tickTop = $this->calculated['boundary_box']['bottom']; 551 $tickBottom = $this->calculated['boundary_box']['bottom'] - $this->parameter['tick_length']; 552 $textBottom = $tickBottom + $this->calculated['bottom_inner_padding']; 553 } 554 555 $axis_font = $this->parameter['axis_font']; 556 $axis_size = $this->parameter['axis_size']; 557 $axis_angle = $this->parameter['x_axis_angle']; 558 559 if ($axis_angle == 0) $reference = 'top-center'; 560 if ($axis_angle > 0) $reference = 'top-right'; 561 if ($axis_angle < 0) $reference = 'top-left'; 562 if ($axis_angle == 90) $reference = 'top-center'; 563 564 //generic tag information. applies to all axis text. 565 $axisTag = array('points' => $axis_size, 'angle' => $axis_angle, 'font' => $axis_font, 'colour' => $axis_colour); 566 567 foreach ($this->calculated['x_axis']['tick_x'] as $set => $tickX) { 568 // draw x grid if colour specified 569 if ($xGrid != 'none') { 570 switch ($xGrid) { 571 case 'line': 572 ImageLine($this->image, round($tickX), round($gridTop), round($tickX), round($gridBottom), $gridColour); 573 break; 574 case 'dash': 575 ImageDashedLine($this->image, round($tickX), round($gridTop), round($tickX), round($gridBottom), $gridColour); 576 break; 577 } 578 } 579 580 if ($this->parameter['x_axis_text'] && !($set % $this->parameter['x_axis_text'])) { // test if tick should be displayed 581 // draw tick 582 if ($tickColour != 'none') 583 ImageLine($this->image, round($tickX), round($tickTop), round($tickX), round($tickBottom), $tickColour); 584 585 // draw axis text 586 $coords = array('x' => $tickX, 'y' => $textBottom, 'reference' => $reference); 587 $axisTag['text'] = $this->calculated['x_axis']['text'][$set]; 588 $axisTag['boundary_box'] = $this->calculated['x_axis']['boundary_box'][$set]; 589 $this->update_boundaryBox($axisTag['boundary_box'], $coords); 590 $this->print_TTF($axisTag); 591 } 592 } 593 } 594 595 function draw_y_axis() { 596 $gridColour = $this->colour[$this->parameter['grid_colour']]; 597 $tickColour = $this->colour[$this->parameter['y_ticks_colour']]; 598 $axis_colour = $this->parameter['axis_colour']; 599 $yGrid = $this->parameter['y_grid']; 600 $gridLeft = $this->calculated['boundary_box']['left']; 601 $gridRight = $this->calculated['boundary_box']['right']; 602 603 // axis font information 604 $axis_font = $this->parameter['axis_font']; 605 $axis_size = $this->parameter['axis_size']; 606 $axis_angle = $this->parameter['y_axis_angle']; 607 $axisTag = array('points' => $axis_size, 'angle' => $axis_angle, 'font' => $axis_font, 'colour' => $axis_colour); 608 609 610 if ($this->calculated['y_axis_left']['has_data']) { 611 // LEFT HAND SIDE 612 // left and right coords for ticks 613 if ($this->parameter['tick_length'] >= 0) { 614 $tickLeft = $this->calculated['boundary_box']['left']; 615 $tickRight = $this->calculated['boundary_box']['left'] + $this->parameter['tick_length']; 616 } else { 617 $tickLeft = $this->calculated['boundary_box']['left'] + $this->parameter['tick_length']; 618 $tickRight = $this->calculated['boundary_box']['left']; 619 } 620 $textRight = $tickLeft - $this->calculated['left_inner_padding']; 621 622 if ($axis_angle == 0) $reference = 'right-center'; 623 if ($axis_angle > 0) $reference = 'right-top'; 624 if ($axis_angle < 0) $reference = 'right-bottom'; 625 if ($axis_angle == 90) $reference = 'right-center'; 626 627 foreach ($this->calculated['y_axis']['tick_y'] as $set => $tickY) { 628 // draw y grid if colour specified 629 if ($yGrid != 'none') { 630 switch ($yGrid) { 631 case 'line': 632 ImageLine($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour); 633 break; 634 case 'dash': 635 ImageDashedLine($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour); 636 break; 637 } 638 } 639 640 // y axis text 641 if ($this->parameter['y_axis_text_left'] && !($set % $this->parameter['y_axis_text_left'])) { // test if tick should be displayed 642 // draw tick 643 if ($tickColour != 'none') 644 ImageLine($this->image, round($tickLeft), round($tickY), round($tickRight), round($tickY), $tickColour); 645 646 // draw axis text... 647 $coords = array('x' => $textRight, 'y' => $tickY, 'reference' => $reference); 648 $axisTag['text'] = $this->calculated['y_axis_left']['text'][$set]; 649 $axisTag['boundary_box'] = $this->calculated['y_axis_left']['boundary_box'][$set]; 650 $this->update_boundaryBox($axisTag['boundary_box'], $coords); 651 $this->print_TTF($axisTag); 652 } 653 } 654 } 655 656 if ($this->calculated['y_axis_right']['has_data']) { 657 // RIGHT HAND SIDE 658 // left and right coords for ticks 659 if ($this->parameter['tick_length'] >= 0) { 660 $tickLeft = $this->calculated['boundary_box']['right'] - $this->parameter['tick_length']; 661 $tickRight = $this->calculated['boundary_box']['right']; 662 } else { 663 $tickLeft = $this->calculated['boundary_box']['right']; 664 $tickRight = $this->calculated['boundary_box']['right'] - $this->parameter['tick_length']; 665 } 666 $textLeft = $tickRight+ $this->calculated['left_inner_padding']; 667 668 if ($axis_angle == 0) $reference = 'left-center'; 669 if ($axis_angle > 0) $reference = 'left-bottom'; 670 if ($axis_angle < 0) $reference = 'left-top'; 671 if ($axis_angle == 90) $reference = 'left-center'; 672 673 foreach ($this->calculated['y_axis']['tick_y'] as $set => $tickY) { 674 if (!$this->calculated['y_axis_left']['has_data'] && $yGrid != 'none') { // draw grid if not drawn already (above) 675 switch ($yGrid) { 676 case 'line': 677 ImageLine($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour); 678 break; 679 case 'dash': 680 ImageDashedLine($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour); 681 break; 682 } 683 } 684 685 if ($this->parameter['y_axis_text_right'] && !($set % $this->parameter['y_axis_text_right'])) { // test if tick should be displayed 686 // draw tick 687 if ($tickColour != 'none') 688 ImageLine($this->image, round($tickLeft), round($tickY), round($tickRight), round($tickY), $tickColour); 689 690 // draw axis text... 691 $coords = array('x' => $textLeft, 'y' => $tickY, 'reference' => $reference); 692 $axisTag['text'] = $this->calculated['y_axis_right']['text'][$set]; 693 $axisTag['boundary_box'] = $this->calculated['y_axis_left']['boundary_box'][$set]; 694 $this->update_boundaryBox($axisTag['boundary_box'], $coords); 695 $this->print_TTF($axisTag); 696 } 697 } 698 } 699 } 700 701 function init_data() { 702 $this->calculated['y_plot'] = array(); // array to hold pixel plotting coords for y axis 703 $height = $this->calculated['boundary_box']['bottom'] - $this->calculated['boundary_box']['top']; 704 $width = $this->calculated['boundary_box']['right'] - $this->calculated['boundary_box']['left']; 705 706 // calculate pixel steps between axis ticks. 707 $this->calculated['y_axis']['step'] = $height / ($this->parameter['y_axis_gridlines'] - 1); 708 709 // calculate x ticks spacing taking into account x offset for ticks. 710 $extraTick = 2 * $this->parameter['x_offset']; // extra tick to account for padding 711 $numTicks = $this->calculated['x_axis']['num_ticks'] - 1; // number of x ticks 712 713 // Hack by rodger to avoid division by zero, see bug 1231 714 if ($numTicks==0) $numTicks=1; 715 716 $this->calculated['x_axis']['step'] = $width / ($numTicks + $extraTick); 717 $widthPlot = $width - ($this->calculated['x_axis']['step'] * $extraTick); 718 $this->calculated['x_axis']['step'] = $widthPlot / $numTicks; 719 720 //calculate factor for transforming x,y physical coords to logical coords for right hand y_axis. 721 $y_range = $this->calculated['y_axis_right']['max'] - $this->calculated['y_axis_right']['min']; 722 $y_range = ($y_range ? $y_range : 1); 723 $this->calculated['y_axis_right']['factor'] = $height / $y_range; 724 725 //calculate factor for transforming x,y physical coords to logical coords for left hand axis. 726 $yRange = $this->calculated['y_axis_left']['max'] - $this->calculated['y_axis_left']['min']; 727 $yRange = ($yRange ? $yRange : 1); 728 $this->calculated['y_axis_left']['factor'] = $height / $yRange; 729 if ($this->parameter['x_axis_gridlines'] != 'auto') { 730 $xRange = $this->calculated['x_axis']['max'] - $this->calculated['x_axis']['min']; 731 $xRange = ($xRange ? $xRange : 1); 732 $this->calculated['x_axis']['factor'] = $widthPlot / $xRange; 733 } 734 735 //expand_pre($this->calculated['boundary_box']); 736 // cycle thru all data sets... 737 $this->calculated['num_bars'] = 0; 738 foreach ($this->y_order as $order => $set) { 739 // determine how many bars there are 740 if (isset($this->y_format[$set]['bar']) && ($this->y_format[$set]['bar'] != 'none')) { 741 $this->calculated['bar_offset_index'][$set] = $this->calculated['num_bars']; // index to relate bar with data set. 742 $this->calculated['num_bars']++; 743 } 744 745 // calculate y coords for plotting data 746 foreach ($this->x_data as $index => $x) { 747 $this->calculated['y_plot'][$set][$index] = $this->y_data[$set][$index]; 748 749 if ((string)$this->y_data[$set][$index] != 'none') { 750 751 if (isset($this->y_format[$set]['y_axis']) && $this->y_format[$set]['y_axis'] == 'right') { 752 $this->calculated['y_plot'][$set][$index] = 753 round(($this->y_data[$set][$index] - $this->calculated['y_axis_right']['min']) 754 * $this->calculated['y_axis_right']['factor']); 755 } else { 756 //print "$set $index<br />"; 757 $this->calculated['y_plot'][$set][$index] = 758 round(($this->y_data[$set][$index] - $this->calculated['y_axis_left']['min']) 759 * $this->calculated['y_axis_left']['factor']); 760 } 761 762 } 763 } 764 } 765 //print "factor ".$this->calculated['x_axis']['factor']."<br />"; 766 //expand_pre($this->calculated['x_plot']); 767 768 // calculate bar parameters if bars are to be drawn. 769 if ($this->calculated['num_bars']) { 770 $xStep = $this->calculated['x_axis']['step']; 771 $totalWidth = $this->calculated['x_axis']['step'] - $this->parameter['bar_spacing']; 772 $barWidth = $totalWidth / $this->calculated['num_bars']; 773 774 $barX = ($barWidth - $totalWidth) / 2; // starting x offset 775 for ($i=0; $i < $this->calculated['num_bars']; $i++) { 776 $this->calculated['bar_offset_x'][$i] = $barX; 777 $barX += $barWidth; // add width of bar to x offset. 778 } 779 $this->calculated['bar_width'] = $barWidth; 780 } 781 782 783 } 784 785 function init_x_ticks() { 786 // get coords for x axis ticks and data plots 787 //$xGrid = $this->parameter['x_grid']; 788 $xStep = $this->calculated['x_axis']['step']; 789 $ticksOffset = $this->parameter['x_offset']; // where to start drawing ticks relative to y axis. 790 $gridLeft = $this->calculated['boundary_box']['left'] + ($xStep * $ticksOffset); // grid x start 791 $tickX = $gridLeft; // tick x coord 792 793 foreach ($this->calculated['x_axis']['text'] as $set => $value) { 794 //print "index: $set<br />"; 795 // x tick value 796 $this->calculated['x_axis']['tick_x'][$set] = $tickX; 797 // if num ticks is auto then x plot value is same as x tick 798 if ($this->parameter['x_axis_gridlines'] == 'auto') $this->calculated['x_plot'][$set] = round($tickX); 799 //print $this->calculated['x_plot'][$set].'<br />'; 800 $tickX += $xStep; 801 } 802 803 //print "xStep: $xStep <br />"; 804 // if numeric x axis then calculate x coords for each data point. this is seperate from x ticks. 805 $gridX = $gridLeft; 806 if (empty($this->calculated['x_axis']['factor'])) { 807 $this->calculated['x_axis']['factor'] = 0; 808 } 809 if (empty($this->calculated['x_axis']['min'])) { 810 $this->calculated['x_axis']['min'] = 0; 811 } 812 $factor = $this->calculated['x_axis']['factor']; 813 $min = $this->calculated['x_axis']['min']; 814 815 if ($this->parameter['x_axis_gridlines'] != 'auto') { 816 foreach ($this->x_data as $index => $x) { 817 //print "index: $index, x: $x<br />"; 818 $offset = $x - $this->calculated['x_axis']['min']; 819 820 //$gridX = ($offset * $this->calculated['x_axis']['factor']); 821 //print "offset: $offset <br />"; 822 //$this->calculated['x_plot'][$set] = $gridLeft + ($offset * $this->calculated['x_axis']['factor']); 823 824 $this->calculated['x_plot'][$index] = $gridLeft + ($x - $min) * $factor; 825 826 //print $this->calculated['x_plot'][$set].'<br />'; 827 } 828 } 829 //expand_pre($this->calculated['boundary_box']); 830 //print "factor ".$this->calculated['x_axis']['factor']."<br />"; 831 //expand_pre($this->calculated['x_plot']); 832 } 833 834 function init_y_ticks() { 835 // get coords for y axis ticks 836 837 $yStep = $this->calculated['y_axis']['step']; 838 $gridBottom = $this->calculated['boundary_box']['bottom']; 839 $tickY = $gridBottom; // tick y coord 840 841 for ($i = 0; $i < $this->parameter['y_axis_gridlines']; $i++) { 842 $this->calculated['y_axis']['tick_y'][$i] = $tickY; 843 $tickY -= $yStep; 844 } 845 846 } 847 848 function init_labels() { 849 if ($this->parameter['title']) { 850 $size = $this->get_boundaryBox( 851 array('points' => $this->parameter['title_size'], 852 'angle' => 0, 853 'font' => $this->parameter['title_font'], 854 'text' => $this->parameter['title'])); 855 $this->calculated['title']['boundary_box'] = $size; 856 $this->calculated['title']['text'] = $this->parameter['title']; 857 $this->calculated['title']['font'] = $this->parameter['title_font']; 858 $this->calculated['title']['points'] = $this->parameter['title_size']; 859 $this->calculated['title']['colour'] = $this->parameter['title_colour']; 860 $this->calculated['title']['angle'] = 0; 861 862 $this->calculated['boundary_box']['top'] += $size['height'] + $this->parameter['outer_padding']; 863 //$this->calculated['boundary_box']['top'] += $size['height']; 864 865 } else $this->calculated['title']['boundary_box'] = $this->get_null_size(); 866 867 if ($this->parameter['y_label_left']) { 868 $this->calculated['y_label_left']['text'] = $this->parameter['y_label_left']; 869 $this->calculated['y_label_left']['angle'] = $this->parameter['y_label_angle']; 870 $this->calculated['y_label_left']['font'] = $this->parameter['label_font']; 871 $this->calculated['y_label_left']['points'] = $this->parameter['label_size']; 872 $this->calculated['y_label_left']['colour'] = $this->parameter['label_colour']; 873 874 $size = $this->get_boundaryBox($this->calculated['y_label_left']); 875 $this->calculated['y_label_left']['boundary_box'] = $size; 876 //$this->calculated['boundary_box']['left'] += $size['width'] + $this->parameter['inner_padding']; 877 $this->calculated['boundary_box']['left'] += $size['width']; 878 879 } else $this->calculated['y_label_left']['boundary_box'] = $this->get_null_size(); 880 881 if ($this->parameter['y_label_right']) { 882 $this->calculated['y_label_right']['text'] = $this->parameter['y_label_right']; 883 $this->calculated['y_label_right']['angle'] = $this->parameter['y_label_angle']; 884 $this->calculated['y_label_right']['font'] = $this->parameter['label_font']; 885 $this->calculated['y_label_right']['points'] = $this->parameter['label_size']; 886 $this->calculated['y_label_right']['colour'] = $this->parameter['label_colour']; 887 888 $size = $this->get_boundaryBox($this->calculated['y_label_right']); 889 $this->calculated['y_label_right']['boundary_box'] = $size; 890 //$this->calculated['boundary_box']['right'] -= $size['width'] + $this->parameter['inner_padding']; 891 $this->calculated['boundary_box']['right'] -= $size['width']; 892 893 } else $this->calculated['y_label_right']['boundary_box'] = $this->get_null_size(); 894 895 if ($this->parameter['x_label']) { 896 $this->calculated['x_label']['text'] = $this->parameter['x_label']; 897 $this->calculated['x_label']['angle'] = $this->parameter['x_label_angle']; 898 $this->calculated['x_label']['font'] = $this->parameter['label_font']; 899 $this->calculated['x_label']['points'] = $this->parameter['label_size']; 900 $this->calculated['x_label']['colour'] = $this->parameter['label_colour']; 901 902 $size = $this->get_boundaryBox($this->calculated['x_label']); 903 $this->calculated['x_label']['boundary_box'] = $size; 904 //$this->calculated['boundary_box']['bottom'] -= $size['height'] + $this->parameter['inner_padding']; 905 $this->calculated['boundary_box']['bottom'] -= $size['height']; 906 907 } else $this->calculated['x_label']['boundary_box'] = $this->get_null_size(); 908 909 } 910 911 912 function init_legend() { 913 $this->calculated['legend'] = array(); // array to hold calculated values for legend. 914 //$this->calculated['legend']['boundary_box_max'] = array('height' => 0, 'width' => 0); 915 $this->calculated['legend']['boundary_box_max'] = $this->get_null_size(); 916 if ($this->parameter['legend'] == 'none') return; 917 918 $position = $this->parameter['legend']; 919 $numSets = 0; // number of data sets with legends. 920 $sumTextHeight = 0; // total of height of all legend text items. 921 $width = 0; 922 $height = 0; 923 924 foreach ($this->y_order as $set) { 925 $text = isset($this->y_format[$set]['legend']) ? $this->y_format[$set]['legend'] : 'none'; 926 $size = $this->get_boundaryBox( 927 array('points' => $this->parameter['legend_size'], 928 'angle' => 0, 929 'font' => $this->parameter['legend_font'], 930 'text' => $text)); 931 932 $this->calculated['legend']['boundary_box'][$set] = $size; 933 $this->calculated['legend']['text'][$set] = $text; 934 //$this->calculated['legend']['font'][$set] = $this->parameter['legend_font']; 935 //$this->calculated['legend']['points'][$set] = $this->parameter['legend_size']; 936 //$this->calculated['legend']['angle'][$set] = 0; 937 938 if ($text && $text!='none') { 939 $numSets++; 940 $sumTextHeight += $size['height']; 941 } 942 943 if ($size['width'] > $this->calculated['legend']['boundary_box_max']['width']) 944 $this->calculated['legend']['boundary_box_max'] = $size; 945 } 946 947 $offset = $this->parameter['legend_offset']; // offset in pixels of legend box from graph border. 948 $padding = $this->parameter['legend_padding']; // padding in pixels around legend text. 949 $textWidth = $this->calculated['legend']['boundary_box_max']['width']; // width of largest legend item. 950 $textHeight = $this->calculated['legend']['boundary_box_max']['height']; // use height as size to use for colour square in legend. 951 $width = $padding * 2 + $textWidth + $textHeight * 2; // left and right padding + maximum text width + space for square 952 $height = ($padding + $textHeight) * $numSets + $padding; // top and bottom padding + padding between text + text. 953 954 $this->calculated['legend']['boundary_box_all'] = array('width' => $width, 955 'height' => $height, 956 'offset' => $offset, 957 'reference' => $position); 958 959 switch ($position) { // move in right or bottom if legend is outside data plotting area. 960 case 'outside-top' : 961 $this->calculated['boundary_box']['right'] -= $offset + $width; // move in right hand side 962 break; 963 964 case 'outside-bottom' : 965 $this->calculated['boundary_box']['right'] -= $offset + $width; // move in right hand side 966 break; 967 968 case 'outside-left' : 969 $this->calculated['boundary_box']['bottom'] -= $offset + $height; // move in right hand side 970 break; 971 972 case 'outside-right' : 973 $this->calculated['boundary_box']['bottom'] -= $offset + $height; // move in right hand side 974 break; 975 } 976 } 977 978 function init_y_axis() { 979 $this->calculated['y_axis_left'] = array(); // array to hold calculated values for y_axis on left. 980 $this->calculated['y_axis_left']['boundary_box_max'] = $this->get_null_size(); 981 $this->calculated['y_axis_right'] = array(); // array to hold calculated values for y_axis on right. 982 $this->calculated['y_axis_right']['boundary_box_max'] = $this->get_null_size(); 983 984 $axis_font = $this->parameter['axis_font']; 985 $axis_size = $this->parameter['axis_size']; 986 $axis_colour = $this->parameter['axis_colour']; 987 $axis_angle = $this->parameter['y_axis_angle']; 988 $y_tick_labels = $this->y_tick_labels; 989 990 $this->calculated['y_axis_left']['has_data'] = FALSE; 991 $this->calculated['y_axis_right']['has_data'] = FALSE; 992 993 // find min and max y values. 994 $minLeft = $this->parameter['y_min_left']; 995 $maxLeft = $this->parameter['y_max_left']; 996 $minRight = $this->parameter['y_min_right']; 997 $maxRight = $this->parameter['y_max_right']; 998 $dataLeft = array(); 999 $dataRight = array(); 1000 foreach ($this->y_order as $order => $set) { 1001 if (isset($this->y_format[$set]['y_axis']) && $this->y_format[$set]['y_axis'] == 'right') { 1002 $this->calculated['y_axis_right']['has_data'] = TRUE; 1003 $dataRight = array_merge($dataRight, $this->y_data[$set]); 1004 } else { 1005 $this->calculated['y_axis_left']['has_data'] = TRUE; 1006 $dataLeft = array_merge($dataLeft, $this->y_data[$set]); 1007 } 1008 } 1009 $dataLeftRange = $this->find_range($dataLeft, $minLeft, $maxLeft, $this->parameter['y_resolution_left']); 1010 $dataRightRange = $this->find_range($dataRight, $minRight, $maxRight, $this->parameter['y_resolution_right']); 1011 $minLeft = $dataLeftRange['min']; 1012 $maxLeft = $dataLeftRange['max']; 1013 $minRight = $dataRightRange['min']; 1014 $maxRight = $dataRightRange['max']; 1015 1016 $this->calculated['y_axis_left']['min'] = $minLeft; 1017 $this->calculated['y_axis_left']['max'] = $maxLeft; 1018 $this->calculated['y_axis_right']['min'] = $minRight; 1019 $this->calculated['y_axis_right']['max'] = $maxRight; 1020 1021 $stepLeft = ($maxLeft - $minLeft) / ($this->parameter['y_axis_gridlines'] - 1); 1022 $startLeft = $minLeft; 1023 $step_right = ($maxRight - $minRight) / ($this->parameter['y_axis_gridlines'] - 1); 1024 $start_right = $minRight; 1025 1026 if ($this->parameter['y_axis_text_left']) { 1027 for ($i = 0; $i < $this->parameter['y_axis_gridlines']; $i++) { // calculate y axis text sizes 1028 // left y axis 1029 if ($y_tick_labels) { 1030 $value = $y_tick_labels[$i]; 1031 } else { 1032 $value = number_format($startLeft, $this->parameter['y_decimal_left'], $this->parameter['decimal_point'], $this->parameter['thousand_sep']); 1033 } 1034 $this->calculated['y_axis_left']['data'][$i] = $startLeft; 1035 $this->calculated['y_axis_left']['text'][$i] = $value; // text is formatted raw data 1036 1037 $size = $this->get_boundaryBox( 1038 array('points' => $axis_size, 1039 'font' => $axis_font, 1040 'angle' => $axis_angle, 1041 'colour' => $axis_colour, 1042 'text' => $value)); 1043 $this->calculated['y_axis_left']['boundary_box'][$i] = $size; 1044 1045 if ($size['height'] > $this->calculated['y_axis_left']['boundary_box_max']['height']) 1046 $this->calculated['y_axis_left']['boundary_box_max']['height'] = $size['height']; 1047 if ($size['width'] > $this->calculated['y_axis_left']['boundary_box_max']['width']) 1048 $this->calculated['y_axis_left']['boundary_box_max']['width'] = $size['width']; 1049 1050 $startLeft += $stepLeft; 1051 } 1052 $this->calculated['boundary_box']['left'] += $this->calculated['y_axis_left']['boundary_box_max']['width'] 1053 + $this->parameter['y_inner_padding']; 1054 } 1055 1056 if ($this->parameter['y_axis_text_right']) { 1057 for ($i = 0; $i < $this->parameter['y_axis_gridlines']; $i++) { // calculate y axis text sizes 1058 // right y axis 1059 $value = number_format($start_right, $this->parameter['y_decimal_right'], $this->parameter['decimal_point'], $this->parameter['thousand_sep']); 1060 $this->calculated['y_axis_right']['data'][$i] = $start_right; 1061 $this->calculated['y_axis_right']['text'][$i] = $value; // text is formatted raw data 1062 $size = $this->get_boundaryBox( 1063 array('points' => $axis_size, 1064 'font' => $axis_font, 1065 'angle' => $axis_angle, 1066 'colour' => $axis_colour, 1067 'text' => $value)); 1068 $this->calculated['y_axis_right']['boundary_box'][$i] = $size; 1069 1070 if ($size['height'] > $this->calculated['y_axis_right']['boundary_box_max']['height']) 1071 $this->calculated['y_axis_right']['boundary_box_max'] = $size; 1072 if ($size['width'] > $this->calculated['y_axis_right']['boundary_box_max']['width']) 1073 $this->calculated['y_axis_right']['boundary_box_max']['width'] = $size['width']; 1074 1075 $start_right += $step_right; 1076 } 1077 $this->calculated['boundary_box']['right'] -= $this->calculated['y_axis_right']['boundary_box_max']['width'] 1078 + $this->parameter['y_inner_padding']; 1079 } 1080 } 1081 1082 function init_x_axis() { 1083 $this->calculated['x_axis'] = array(); // array to hold calculated values for x_axis. 1084 $this->calculated['x_axis']['boundary_box_max'] = array('height' => 0, 'width' => 0); 1085 1086 $axis_font = $this->parameter['axis_font']; 1087 $axis_size = $this->parameter['axis_size']; 1088 $axis_colour = $this->parameter['axis_colour']; 1089 $axis_angle = $this->parameter['x_axis_angle']; 1090 1091 // check whether to treat x axis as numeric 1092 if ($this->parameter['x_axis_gridlines'] == 'auto') { // auto means text based x_axis, not numeric... 1093 $this->calculated['x_axis']['num_ticks'] = sizeof($this->x_data); 1094 $data = $this->x_data; 1095 for ($i=0; $i < $this->calculated['x_axis']['num_ticks']; $i++) { 1096 $value = array_shift($data); // grab value from begin of array 1097 $this->calculated['x_axis']['data'][$i] = $value; 1098 $this->calculated['x_axis']['text'][$i] = $value; // raw data and text are both the same in this case 1099 $size = $this->get_boundaryBox( 1100 array('points' => $axis_size, 1101 'font' => $axis_font, 1102 'angle' => $axis_angle, 1103 'colour' => $axis_colour, 1104 'text' => $value)); 1105 $this->calculated['x_axis']['boundary_box'][$i] = $size; 1106 if ($size['height'] > $this->calculated['x_axis']['boundary_box_max']['height']) 1107 $this->calculated['x_axis']['boundary_box_max'] = $size; 1108 } 1109 1110 } else { // x axis is numeric so find max min values... 1111 $this->calculated['x_axis']['num_ticks'] = $this->parameter['x_axis_gridlines']; 1112 1113 $min = $this->parameter['x_min']; 1114 $max = $this->parameter['x_max']; 1115 $data = array(); 1116 $data = $this->find_range($this->x_data, $min, $max, $this->parameter['x_resolution']); 1117 $min = $data['min']; 1118 $max = $data['max']; 1119 $this->calculated['x_axis']['min'] = $min; 1120 $this->calculated['x_axis']['max'] = $max; 1121 1122 $step = ($max - $min) / ($this->calculated['x_axis']['num_ticks'] - 1); 1123 $start = $min; 1124 1125 for ($i = 0; $i < $this->calculated['x_axis']['num_ticks']; $i++) { // calculate x axis text sizes 1126 $value = number_format($start, $this->parameter['xDecimal'], $this->parameter['decimal_point'], $this->parameter['thousand_sep']); 1127 $this->calculated['x_axis']['data'][$i] = $start; 1128 $this->calculated['x_axis']['text'][$i] = $value; // text is formatted raw data 1129 1130 $size = $this->get_boundaryBox( 1131 array('points' => $axis_size, 1132 'font' => $axis_font, 1133 'angle' => $axis_angle, 1134 'colour' => $axis_colour, 1135 'text' => $value)); 1136 $this->calculated['x_axis']['boundary_box'][$i] = $size; 1137 1138 if ($size['height'] > $this->calculated['x_axis']['boundary_box_max']['height']) 1139 $this->calculated['x_axis']['boundary_box_max'] = $size; 1140 1141 $start += $step; 1142 } 1143 } 1144 if ($this->parameter['x_axis_text']) 1145 $this->calculated['boundary_box']['bottom'] -= $this->calculated['x_axis']['boundary_box_max']['height'] 1146 + $this->parameter['x_inner_padding']; 1147 } 1148 1149 // find max and min values for a data array given the resolution. 1150 function find_range($data, $min, $max, $resolution) { 1151 if (sizeof($data) == 0 ) return array('min' => 0, 'max' => 0); 1152 foreach ($data as $key => $value) { 1153 if ($value=='none') continue; 1154 if ($value > $max) $max = $value; 1155 if ($value < $min) $min = $value; 1156 } 1157 1158 if ($max == 0) { 1159 $factor = 1; 1160 } else { 1161 if ($max < 0) $factor = - pow(10, (floor(log10(abs($max))) + $resolution) ); 1162 else $factor = pow(10, (floor(log10(abs($max))) - $resolution) ); 1163 } 1164 if ($factor > 0.1) { // To avoid some wierd rounding errors (Moodle) 1165 $factor = round($factor * 1000.0) / 1000.0; // To avoid some wierd rounding errors (Moodle) 1166 } // To avoid some wierd rounding errors (Moodle) 1167 1168 $max = $factor * @ceil($max / $factor); 1169 $min = $factor * @floor($min / $factor); 1170 1171 //print "max=$max, min=$min<br />"; 1172 1173 return array('min' => $min, 'max' => $max); 1174 } 1175 1176 function graph() { 1177 if (func_num_args() == 2) { 1178 $this->parameter['width'] = func_get_arg(0); 1179 $this->parameter['height'] = func_get_arg(1); 1180 } 1181 //$this->boundaryBox = array( 1182 $this->calculated['boundary_box'] = array( 1183 'left' => 0, 1184 'top' => 0, 1185 'right' => $this->parameter['width'] - 1, 1186 'bottom' => $this->parameter['height'] - 1); 1187 1188 $this->init_colours(); 1189 1190 //ImageColorTransparent($this->image, $this->colour['white']); // colour for transparency 1191 } 1192 1193 /** 1194 * Prepare label's text for GD output. 1195 * 1196 * @param string $label string to be prepared. 1197 * @return string Reversed input string, if we are in RTL mode and has no numbers. 1198 * Otherwise, returns the string as is. 1199 */ 1200 private function prepare_label_text($label) { 1201 if (right_to_left() and !preg_match('/[0-9]/i', $label)) { 1202 return core_text::strrev($label); 1203 } else { 1204 return $label; 1205 } 1206 } 1207 1208 function print_TTF($message) { 1209 $points = $message['points']; 1210 $angle = $message['angle']; 1211 // We have to manually reverse the label, since php GD cannot handle RTL characters properly in UTF8 strings. 1212 $text = $this->prepare_label_text($message['text']); 1213 $colour = $this->colour[$message['colour']]; 1214 $font = $this->parameter['path_to_fonts'].$message['font']; 1215 1216 $x = $message['boundary_box']['x']; 1217 $y = $message['boundary_box']['y']; 1218 $offsetX = $message['boundary_box']['offsetX']; 1219 $offsetY = $message['boundary_box']['offsetY']; 1220 $height = $message['boundary_box']['height']; 1221 $width = $message['boundary_box']['width']; 1222 $reference = $message['boundary_box']['reference']; 1223 1224 switch ($reference) { 1225 case 'top-left': 1226 case 'left-top': 1227 $y += $height - $offsetY; 1228 //$y += $offsetY; 1229 $x += $offsetX; 1230 break; 1231 case 'left-center': 1232 $y += ($height / 2) - $offsetY; 1233 $x += $offsetX; 1234 break; 1235 case 'left-bottom': 1236 $y -= $offsetY; 1237 $x += $offsetX; 1238 break; 1239 case 'top-center': 1240 $y += $height - $offsetY; 1241 $x -= ($width / 2) - $offsetX; 1242 break; 1243 case 'top-right': 1244 case 'right-top': 1245 $y += $height - $offsetY; 1246 $x -= $width - $offsetX; 1247 break; 1248 case 'right-center': 1249 $y += ($height / 2) - $offsetY; 1250 $x -= $width - $offsetX; 1251 break; 1252 case 'right-bottom': 1253 $y -= $offsetY; 1254 $x -= $width - $offsetX; 1255 break; 1256 case 'bottom-center': 1257 $y -= $offsetY; 1258 $x -= ($width / 2) - $offsetX; 1259 break; 1260 default: 1261 $y = 0; 1262 $x = 0; 1263 break; 1264 } 1265 // start of Moodle addition 1266 $text = core_text::utf8_to_entities($text, true, true); //does not work with hex entities! 1267 // end of Moodle addition 1268 ImageTTFText($this->image, $points, $angle, $x, $y, $colour, $font, $text); 1269 } 1270 1271 // move boundaryBox to coordinates specified 1272 function update_boundaryBox(&$boundaryBox, $coords) { 1273 $width = $boundaryBox['width']; 1274 $height = $boundaryBox['height']; 1275 $x = $coords['x']; 1276 $y = $coords['y']; 1277 $reference = $coords['reference']; 1278 switch ($reference) { 1279 case 'top-left': 1280 case 'left-top': 1281 $top = $y; 1282 $bottom = $y + $height; 1283 $left = $x; 1284 $right = $x + $width; 1285 break; 1286 case 'left-center': 1287 $top = $y - ($height / 2); 1288 $bottom = $y + ($height / 2); 1289 $left = $x; 1290 $right = $x + $width; 1291 break; 1292 case 'left-bottom': 1293 $top = $y - $height; 1294 $bottom = $y; 1295 $left = $x; 1296 $right = $x + $width; 1297 break; 1298 case 'top-center': 1299 $top = $y; 1300 $bottom = $y + $height; 1301 $left = $x - ($width / 2); 1302 $right = $x + ($width / 2); 1303 break; 1304 case 'right-top': 1305 case 'top-right': 1306 $top = $y; 1307 $bottom = $y + $height; 1308 $left = $x - $width; 1309 $right = $x; 1310 break; 1311 case 'right-center': 1312 $top = $y - ($height / 2); 1313 $bottom = $y + ($height / 2); 1314 $left = $x - $width; 1315 $right = $x; 1316 break; 1317 case 'bottom=right': 1318 case 'right-bottom': 1319 $top = $y - $height; 1320 $bottom = $y; 1321 $left = $x - $width; 1322 $right = $x; 1323 break; 1324 default: 1325 $top = 0; 1326 $bottom = $height; 1327 $left = 0; 1328 $right = $width; 1329 break; 1330 } 1331 1332 $boundaryBox = array_merge($boundaryBox, array('top' => $top, 1333 'bottom' => $bottom, 1334 'left' => $left, 1335 'right' => $right, 1336 'x' => $x, 1337 'y' => $y, 1338 'reference' => $reference)); 1339 } 1340 1341 function get_null_size() { 1342 return array('width' => 0, 1343 'height' => 0, 1344 'offsetX' => 0, 1345 'offsetY' => 0, 1346 //'fontHeight' => 0 1347 ); 1348 } 1349 1350 function get_boundaryBox($message) { 1351 $points = $message['points']; 1352 $angle = $message['angle']; 1353 $font = $this->parameter['path_to_fonts'].$message['font']; 1354 $text = $message['text']; 1355 1356 //print ('get_boundaryBox'); 1357 //expandPre($message); 1358 1359 // get font size 1360 $bounds = ImageTTFBBox($points, $angle, $font, "W"); 1361 if ($angle < 0) { 1362 $fontHeight = abs($bounds[7]-$bounds[1]); 1363 } else if ($angle > 0) { 1364 $fontHeight = abs($bounds[1]-$bounds[7]); 1365 } else { 1366 $fontHeight = abs($bounds[7]-$bounds[1]); 1367 } 1368 1369 // get boundary box and offsets for printing at an angle 1370 // start of Moodle addition 1371 $text = core_text::utf8_to_entities($text, true, true); //gd does not work with hex entities! 1372 // end of Moodle addition 1373 $bounds = ImageTTFBBox($points, $angle, $font, $text); 1374 1375 if ($angle < 0) { 1376 $width = abs($bounds[4]-$bounds[0]); 1377 $height = abs($bounds[3]-$bounds[7]); 1378 $offsetY = abs($bounds[3]-$bounds[1]); 1379 $offsetX = 0; 1380 1381 } else if ($angle > 0) { 1382 $width = abs($bounds[2]-$bounds[6]); 1383 $height = abs($bounds[1]-$bounds[5]); 1384 $offsetY = 0; 1385 $offsetX = abs($bounds[0]-$bounds[6]); 1386 1387 } else { 1388 $width = abs($bounds[4]-$bounds[6]); 1389 $height = abs($bounds[7]-$bounds[1]); 1390 $offsetY = $bounds[1]; 1391 $offsetX = 0; 1392 } 1393 1394 //return values 1395 return array('width' => $width, 1396 'height' => $height, 1397 'offsetX' => $offsetX, 1398 'offsetY' => $offsetY, 1399 //'fontHeight' => $fontHeight 1400 ); 1401 } 1402 1403 function draw_rectangle($border, $colour, $type) { 1404 $colour = $this->colour[$colour]; 1405 switch ($type) { 1406 case 'fill': // fill the rectangle 1407 ImageFilledRectangle($this->image, $border['left'], $border['top'], $border['right'], $border['bottom'], $colour); 1408 break; 1409 case 'box': // all sides 1410 ImageRectangle($this->image, $border['left'], $border['top'], $border['right'], $border['bottom'], $colour); 1411 break; 1412 case 'axis': // bottom x axis and left y axis 1413 ImageLine($this->image, $border['left'], $border['top'], $border['left'], $border['bottom'], $colour); 1414 ImageLine($this->image, $border['left'], $border['bottom'], $border['right'], $border['bottom'], $colour); 1415 break; 1416 case 'y': // left y axis only 1417 case 'y-left': 1418 ImageLine($this->image, $border['left'], $border['top'], $border['left'], $border['bottom'], $colour); 1419 break; 1420 case 'y-right': // right y axis only 1421 ImageLine($this->image, $border['right'], $border['top'], $border['right'], $border['bottom'], $colour); 1422 break; 1423 case 'x': // bottom x axis only 1424 ImageLine($this->image, $border['left'], $border['bottom'], $border['right'], $border['bottom'], $colour); 1425 break; 1426 case 'u': // u shaped. bottom x axis and both left and right y axis. 1427 ImageLine($this->image, $border['left'], $border['top'], $border['left'], $border['bottom'], $colour); 1428 ImageLine($this->image, $border['right'], $border['top'], $border['right'], $border['bottom'], $colour); 1429 ImageLine($this->image, $border['left'], $border['bottom'], $border['right'], $border['bottom'], $colour); 1430 break; 1431 1432 } 1433 } 1434 1435 function init_colours() { 1436 $this->image = ImageCreate($this->parameter['width'], $this->parameter['height']); 1437 // standard colours 1438 $this->colour['white'] = ImageColorAllocate ($this->image, 0xFF, 0xFF, 0xFF); // first colour is background colour. 1439 $this->colour['black'] = ImageColorAllocate ($this->image, 0x00, 0x00, 0x00); 1440 $this->colour['maroon'] = ImageColorAllocate ($this->image, 0x80, 0x00, 0x00); 1441 $this->colour['green'] = ImageColorAllocate ($this->image, 0x00, 0x80, 0x00); 1442 $this->colour['ltgreen'] = ImageColorAllocate ($this->image, 0x52, 0xF1, 0x7F); 1443 $this->colour['ltltgreen']= ImageColorAllocate ($this->image, 0x99, 0xFF, 0x99); 1444 $this->colour['olive'] = ImageColorAllocate ($this->image, 0x80, 0x80, 0x00); 1445 $this->colour['navy'] = ImageColorAllocate ($this->image, 0x00, 0x00, 0x80); 1446 $this->colour['purple'] = ImageColorAllocate ($this->image, 0x80, 0x00, 0x80); 1447 $this->colour['gray'] = ImageColorAllocate ($this->image, 0x80, 0x80, 0x80); 1448 $this->colour['red'] = ImageColorAllocate ($this->image, 0xFF, 0x00, 0x00); 1449 $this->colour['ltred'] = ImageColorAllocate ($this->image, 0xFF, 0x99, 0x99); 1450 $this->colour['ltltred'] = ImageColorAllocate ($this->image, 0xFF, 0xCC, 0xCC); 1451 $this->colour['orange'] = ImageColorAllocate ($this->image, 0xFF, 0x66, 0x00); 1452 $this->colour['ltorange'] = ImageColorAllocate ($this->image, 0xFF, 0x99, 0x66); 1453 $this->colour['ltltorange'] = ImageColorAllocate ($this->image, 0xFF, 0xcc, 0x99); 1454 $this->colour['lime'] = ImageColorAllocate ($this->image, 0x00, 0xFF, 0x00); 1455 $this->colour['yellow'] = ImageColorAllocate ($this->image, 0xFF, 0xFF, 0x00); 1456 $this->colour['blue'] = ImageColorAllocate ($this->image, 0x00, 0x00, 0xFF); 1457 $this->colour['ltblue'] = ImageColorAllocate ($this->image, 0x00, 0xCC, 0xFF); 1458 $this->colour['ltltblue'] = ImageColorAllocate ($this->image, 0x99, 0xFF, 0xFF); 1459 $this->colour['fuchsia'] = ImageColorAllocate ($this->image, 0xFF, 0x00, 0xFF); 1460 $this->colour['aqua'] = ImageColorAllocate ($this->image, 0x00, 0xFF, 0xFF); 1461 //$this->colour['white'] = ImageColorAllocate ($this->image, 0xFF, 0xFF, 0xFF); 1462 // shades of gray 1463 $this->colour['grayF0'] = ImageColorAllocate ($this->image, 0xF0, 0xF0, 0xF0); 1464 $this->colour['grayEE'] = ImageColorAllocate ($this->image, 0xEE, 0xEE, 0xEE); 1465 $this->colour['grayDD'] = ImageColorAllocate ($this->image, 0xDD, 0xDD, 0xDD); 1466 $this->colour['grayCC'] = ImageColorAllocate ($this->image, 0xCC, 0xCC, 0xCC); 1467 $this->colour['gray33'] = ImageColorAllocate ($this->image, 0x33, 0x33, 0x33); 1468 $this->colour['gray66'] = ImageColorAllocate ($this->image, 0x66, 0x66, 0x66); 1469 $this->colour['gray99'] = ImageColorAllocate ($this->image, 0x99, 0x99, 0x99); 1470 1471 $this->colour['none'] = 'none'; 1472 return true; 1473 } 1474 1475 function output() { 1476 if ($this->debug) { // for debugging purposes. 1477 //expandPre($this->graph); 1478 //expandPre($this->y_data); 1479 //expandPre($this->x_data); 1480 //expandPre($this->parameter); 1481 } else { 1482 1483 $expiresSeconds = $this->parameter['seconds_to_live']; 1484 $expiresHours = $this->parameter['hours_to_live']; 1485 1486 if ($expiresHours || $expiresSeconds) { 1487 $now = mktime (date("H"),date("i"),date("s"),date("m"),date("d"),date("Y")); 1488 $expires = mktime (date("H")+$expiresHours,date("i"),date("s")+$expiresSeconds,date("m"),date("d"),date("Y")); 1489 $expiresGMT = gmdate('D, d M Y H:i:s', $expires).' GMT'; 1490 $lastModifiedGMT = gmdate('D, d M Y H:i:s', $now).' GMT'; 1491 1492 Header('Last-modified: '.$lastModifiedGMT); 1493 Header('Expires: '.$expiresGMT); 1494 } 1495 1496 if ($this->parameter['file_name'] == 'none') { 1497 switch ($this->parameter['output_format']) { 1498 case 'GIF': 1499 Header("Content-type: image/gif"); // GIF??. switch to PNG guys!! 1500 ImageGIF($this->image); 1501 break; 1502 case 'JPEG': 1503 Header("Content-type: image/jpeg"); // JPEG for line art??. included for completeness. 1504 ImageJPEG($this->image); 1505 break; 1506 default: 1507 Header("Content-type: image/png"); // preferred output format 1508 ImagePNG($this->image); 1509 break; 1510 } 1511 } else { 1512 switch ($this->parameter['output_format']) { 1513 case 'GIF': 1514 ImageGIF($this->image, $this->parameter['file_name'].'.gif'); 1515 break; 1516 case 'JPEG': 1517 ImageJPEG($this->image, $this->parameter['file_name'].'.jpg'); 1518 break; 1519 default: 1520 ImagePNG($this->image, $this->parameter['file_name'].'.png'); 1521 break; 1522 } 1523 } 1524 1525 ImageDestroy($this->image); 1526 } 1527 } // function output 1528 1529 function init_variable(&$variable, $value, $default) { 1530 if (!empty($value)) $variable = $value; 1531 else if (isset($default)) $variable = $default; 1532 else unset($variable); 1533 } 1534 1535 // plot a point. options include square, circle, diamond, triangle, and dot. offset is used for drawing shadows. 1536 // for diamonds and triangles the size should be an even number to get nice look. if odd the points are crooked. 1537 function plot($x, $y, $type, $size, $colour, $offset) { 1538 //print("drawing point of type: $type, at offset: $offset"); 1539 $u = $x + $offset; 1540 $v = $this->calculated['inner_border']['bottom'] - $y + $offset; 1541 $half = $size / 2; 1542 1543 switch ($type) { 1544 case 'square': 1545 ImageFilledRectangle($this->image, $u-$half, $v-$half, $u+$half, $v+$half, $this->colour[$colour]); 1546 break; 1547 case 'square-open': 1548 ImageRectangle($this->image, $u-$half, $v-$half, $u+$half, $v+$half, $this->colour[$colour]); 1549 break; 1550 case 'circle': 1551 ImageArc($this->image, $u, $v, $size, $size, 0, 360, $this->colour[$colour]); 1552 ImageFillToBorder($this->image, $u, $v, $this->colour[$colour], $this->colour[$colour]); 1553 break; 1554 case 'circle-open': 1555 ImageArc($this->image, $u, $v, $size, $size, 0, 360, $this->colour[$colour]); 1556 break; 1557 case 'diamond': 1558 ImageFilledPolygon($this->image, array($u, $v-$half, $u+$half, $v, $u, $v+$half, $u-$half, $v), 4, $this->colour[$colour]); 1559 break; 1560 case 'diamond-open': 1561 ImagePolygon($this->image, array($u, $v-$half, $u+$half, $v, $u, $v+$half, $u-$half, $v), 4, $this->colour[$colour]); 1562 break; 1563 case 'triangle': 1564 ImageFilledPolygon($this->image, array($u, $v-$half, $u+$half, $v+$half, $u-$half, $v+$half), 3, $this->colour[$colour]); 1565 break; 1566 case 'triangle-open': 1567 ImagePolygon($this->image, array($u, $v-$half, $u+$half, $v+$half, $u-$half, $v+$half), 3, $this->colour[$colour]); 1568 break; 1569 case 'dot': 1570 ImageSetPixel($this->image, $u, $v, $this->colour[$colour]); 1571 break; 1572 } 1573 } 1574 1575 function bar($x, $y, $type, $size, $colour, $offset, $index, $yoffset) { 1576 $index_offset = $this->calculated['bar_offset_index'][$index]; 1577 if ( $yoffset ) { 1578 $bar_offsetx = 0; 1579 } else { 1580 $bar_offsetx = $this->calculated['bar_offset_x'][$index_offset]; 1581 } 1582 //$this->dbug("drawing bar at offset = $offset : index = $index: bar_offsetx = $bar_offsetx"); 1583 1584 $span = ($this->calculated['bar_width'] * $size) / 2; 1585 $x_left = $x + $bar_offsetx - $span; 1586 $x_right = $x + $bar_offsetx + $span; 1587 1588 if ($this->parameter['zero_axis'] != 'none') { 1589 $zero = $this->calculated['zero_axis']; 1590 if ($this->parameter['shadow_below_axis'] ) $zero += $offset; 1591 $u_left = $x_left + $offset; 1592 $u_right = $x_right + $offset - 1; 1593 $v = $this->calculated['boundary_box']['bottom'] - $y + $offset; 1594 1595 if ($v > $zero) { 1596 $top = $zero +1; 1597 $bottom = $v; 1598 } else { 1599 $top = $v; 1600 $bottom = $zero - 1; 1601 } 1602 1603 switch ($type) { 1604 case 'open': 1605 //ImageRectangle($this->image, round($u_left), $top, round($u_right), $bottom, $this->colour[$colour]); 1606 if ($v > $zero) 1607 ImageRectangle($this->image, round($u_left), $bottom, round($u_right), $bottom, $this->colour[$colour]); 1608 else 1609 ImageRectangle($this->image, round($u_left), $top, round($u_right), $top, $this->colour[$colour]); 1610 ImageRectangle($this->image, round($u_left), $top, round($u_left), $bottom, $this->colour[$colour]); 1611 ImageRectangle($this->image, round($u_right), $top, round($u_right), $bottom, $this->colour[$colour]); 1612 break; 1613 case 'fill': 1614 ImageFilledRectangle($this->image, round($u_left), $top, round($u_right), $bottom, $this->colour[$colour]); 1615 break; 1616 } 1617 1618 } else { 1619 1620 $bottom = $this->calculated['boundary_box']['bottom']; 1621 if ($this->parameter['shadow_below_axis'] ) $bottom += $offset; 1622 if ($this->parameter['inner_border'] != 'none') $bottom -= 1; // 1 pixel above bottom if border is to be drawn. 1623 $u_left = $x_left + $offset; 1624 $u_right = $x_right + $offset - 1; 1625 $v = $this->calculated['boundary_box']['bottom'] - $y + $offset; 1626 1627 // Moodle addition, plus the function parameter yoffset 1628 if ($yoffset) { // Moodle 1629 $yoffset = $yoffset - round(($bottom - $v) / 2.0); // Moodle 1630 $bottom -= $yoffset; // Moodle 1631 $v -= $yoffset; // Moodle 1632 } // Moodle 1633 1634 switch ($type) { 1635 case 'open': 1636 ImageRectangle($this->image, round($u_left), $v, round($u_right), $bottom, $this->colour[$colour]); 1637 break; 1638 case 'fill': 1639 ImageFilledRectangle($this->image, round($u_left), $v, round($u_right), $bottom, $this->colour[$colour]); 1640 break; 1641 } 1642 } 1643 } 1644 1645 function area($x_start, $y_start, $x_end, $y_end, $type, $colour, $offset) { 1646 //dbug("drawing area type: $type, at offset: $offset"); 1647 if ($this->parameter['zero_axis'] != 'none') { 1648 $bottom = $this->calculated['boundary_box']['bottom']; 1649 $zero = $this->calculated['zero_axis']; 1650 if ($this->parameter['shadow_below_axis'] ) $zero += $offset; 1651 $u_start = $x_start + $offset; 1652 $u_end = $x_end + $offset; 1653 $v_start = $bottom - $y_start + $offset; 1654 $v_end = $bottom - $y_end + $offset; 1655 switch ($type) { 1656 case 'fill': 1657 // draw it this way 'cos the FilledPolygon routine seems a bit buggy. 1658 ImageFilledPolygon($this->image, array($u_start, $v_start, $u_end, $v_end, $u_end, $zero, $u_start, $zero), 4, $this->colour[$colour]); 1659 ImagePolygon($this->image, array($u_start, $v_start, $u_end, $v_end, $u_end, $zero, $u_start, $zero), 4, $this->colour[$colour]); 1660 break; 1661 case 'open': 1662 //ImagePolygon($this->image, array($u_start, $v_start, $u_end, $v_end, $u_end, $zero, $u_start, $zero), 4, $this->colour[$colour]); 1663 ImageLine($this->image, $u_start, $v_start, $u_end, $v_end, $this->colour[$colour]); 1664 ImageLine($this->image, $u_start, $v_start, $u_start, $zero, $this->colour[$colour]); 1665 ImageLine($this->image, $u_end, $v_end, $u_end, $zero, $this->colour[$colour]); 1666 break; 1667 } 1668 } else { 1669 $bottom = $this->calculated['boundary_box']['bottom']; 1670 $u_start = $x_start + $offset; 1671 $u_end = $x_end + $offset; 1672 $v_start = $bottom - $y_start + $offset; 1673 $v_end = $bottom - $y_end + $offset; 1674 1675 if ($this->parameter['shadow_below_axis'] ) $bottom += $offset; 1676 if ($this->parameter['inner_border'] != 'none') $bottom -= 1; // 1 pixel above bottom if border is to be drawn. 1677 switch ($type) { 1678 case 'fill': 1679 ImageFilledPolygon($this->image, array($u_start, $v_start, $u_end, $v_end, $u_end, $bottom, $u_start, $bottom), 4, $this->colour[$colour]); 1680 break; 1681 case 'open': 1682 ImagePolygon($this->image, array($u_start, $v_start, $u_end, $v_end, $u_end, $bottom, $u_start, $bottom), 4, $this->colour[$colour]); 1683 break; 1684 } 1685 } 1686 } 1687 1688 function line($x_start, $y_start, $x_end, $y_end, $type, $brush_type, $brush_size, $colour, $offset) { 1689 //dbug("drawing line of type: $type, at offset: $offset"); 1690 $u_start = $x_start + $offset; 1691 $v_start = $this->calculated['boundary_box']['bottom'] - $y_start + $offset; 1692 $u_end = $x_end + $offset; 1693 $v_end = $this->calculated['boundary_box']['bottom'] - $y_end + $offset; 1694 1695 switch ($type) { 1696 case 'brush': 1697 $this->draw_brush_line($u_start, $v_start, $u_end, $v_end, $brush_size, $brush_type, $colour); 1698 break; 1699 case 'line' : 1700 ImageLine($this->image, $u_start, $v_start, $u_end, $v_end, $this->colour[$colour]); 1701 break; 1702 case 'dash': 1703 ImageDashedLine($this->image, $u_start, $v_start, $u_end, $v_end, $this->colour[$colour]); 1704 break; 1705 } 1706 } 1707 1708 // function to draw line. would prefer to use gdBrush but this is not supported yet. 1709 function draw_brush_line($x0, $y0, $x1, $y1, $size, $type, $colour) { 1710 //$this->dbug("line: $x0, $y0, $x1, $y1"); 1711 $dy = $y1 - $y0; 1712 $dx = $x1 - $x0; 1713 $t = 0; 1714 $watchdog = 1024; // precaution to prevent infinite loops. 1715 1716 $this->draw_brush($x0, $y0, $size, $type, $colour); 1717 if (abs($dx) > abs($dy)) { // slope < 1 1718 //$this->dbug("slope < 1"); 1719 $m = $dy / $dx; // compute slope 1720 $t += $y0; 1721 $dx = ($dx < 0) ? -1 : 1; 1722 $m *= $dx; 1723 while (round($x0) != round($x1)) { 1724 if (!$watchdog--) break; 1725 $x0 += $dx; // step to next x value 1726 $t += $m; // add slope to y value 1727 $y = round($t); 1728 //$this->dbug("x0=$x0, x1=$x1, y=$y watchdog=$watchdog"); 1729 $this->draw_brush($x0, $y, $size, $type, $colour); 1730 1731 } 1732 } else { // slope >= 1 1733 //$this->dbug("slope >= 1"); 1734 $m = $dx / $dy; // compute slope 1735 $t += $x0; 1736 $dy = ($dy < 0) ? -1 : 1; 1737 $m *= $dy; 1738 while (round($y0) != round($y1)) { 1739 if (!$watchdog--) break; 1740 $y0 += $dy; // step to next y value 1741 $t += $m; // add slope to x value 1742 $x = round($t); 1743 //$this->dbug("x=$x, y0=$y0, y1=$y1 watchdog=$watchdog"); 1744 $this->draw_brush($x, $y0, $size, $type, $colour); 1745 1746 } 1747 } 1748 } 1749 1750 function draw_brush($x, $y, $size, $type, $colour) { 1751 $x = round($x); 1752 $y = round($y); 1753 $half = round($size / 2); 1754 switch ($type) { 1755 case 'circle': 1756 ImageArc($this->image, $x, $y, $size, $size, 0, 360, $this->colour[$colour]); 1757 ImageFillToBorder($this->image, $x, $y, $this->colour[$colour], $this->colour[$colour]); 1758 break; 1759 case 'square': 1760 ImageFilledRectangle($this->image, $x-$half, $y-$half, $x+$half, $y+$half, $this->colour[$colour]); 1761 break; 1762 case 'vertical': 1763 ImageFilledRectangle($this->image, $x, $y-$half, $x+1, $y+$half, $this->colour[$colour]); 1764 break; 1765 case 'horizontal': 1766 ImageFilledRectangle($this->image, $x-$half, $y, $x+$half, $y+1, $this->colour[$colour]); 1767 break; 1768 case 'slash': 1769 ImageFilledPolygon($this->image, array($x+$half, $y-$half, 1770 $x+$half+1, $y-$half, 1771 $x-$half+1, $y+$half, 1772 $x-$half, $y+$half 1773 ), 4, $this->colour[$colour]); 1774 break; 1775 case 'backslash': 1776 ImageFilledPolygon($this->image, array($x-$half, $y-$half, 1777 $x-$half+1, $y-$half, 1778 $x+$half+1, $y+$half, 1779 $x+$half, $y+$half 1780 ), 4, $this->colour[$colour]); 1781 break; 1782 default: 1783 @eval($type); // user can create own brush script. 1784 } 1785 } 1786 1787 } // class graph
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |