3 * Graph Class. PHP Class to draw line, point, bar, and area graphs, including numeric x-axis and double y-axis.
5 * Copyright (C) 2000 Herman Veluwenkamp
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Copy of GNU Lesser General Public License at: http://www.gnu.org/copyleft/lesser.txt
22 * Contact author at: hermanV@mindless.com
28 declare(strict_types
=1);
30 defined('MOODLE_INTERNAL') ||
die();
32 /* This file contains modifications by Martin Dougiamas
33 * as part of Moodle (http://moodle.com). Modified lines
34 * are marked with "Moodle".
42 var $debug = FALSE; // be careful!!
43 var $calculated = array(); // array of computed values for chart
44 var $parameter = array( // input parameters
45 'width' => 320, // default width of image
46 'height' => 240, // default height of image
47 'file_name' => 'none', // name of file for file to be saved as.
48 // NOTE: no suffix required. this is determined from output_format below.
49 'output_format' => 'PNG', // image output format. 'GIF', 'PNG', 'JPEG'. default 'PNG'.
51 'seconds_to_live' => 0, // expiry time in seconds (for HTTP header)
52 'hours_to_live' => 0, // expiry time in hours (for HTTP header)
53 'path_to_fonts' => 'fonts/', // path to fonts folder. don't forget *trailing* slash!!
54 // for WINDOZE this may need to be the full path, not relative.
56 'title' => 'Graph Title', // text for graph title
57 'title_font' => 'default.ttf', // title text font. don't forget to set 'path_to_fonts' above.
58 'title_size' => 16, // title text point size
59 'title_colour' => 'black', // colour for title text
61 'x_label' => '', // if this is set then this text is printed on bottom axis of graph.
62 'y_label_left' => '', // if this is set then this text is printed on left axis of graph.
63 'y_label_right' => '', // if this is set then this text is printed on right axis of graph.
65 'label_size' => 8, // label text point size
66 'label_font' => 'default.ttf', // label text font. don't forget to set 'path_to_fonts' above.
67 'label_colour' => 'gray33', // label text colour
68 'y_label_angle' => 90, // rotation of y axis label
70 'x_label_angle' => 90, // rotation of y axis label
72 'outer_padding' => 5, // padding around outer text. i.e. title, y label, and x label.
73 'inner_padding' => 0, // padding beteen axis text and graph.
74 'x_inner_padding' => 5, // padding beteen axis text and graph.
75 'y_inner_padding' => 6, // padding beteen axis text and graph.
76 'outer_border' => 'none', // colour of border aound image, or 'none'.
77 'inner_border' => 'black', // colour of border around actual graph, or 'none'.
78 'inner_border_type' => 'box', // 'box' for all four sides, 'axis' for x/y axis only,
79 // 'y' or 'y-left' for y axis only, 'y-right' for right y axis only,
80 // 'x' for x axis only, 'u' for both left and right y axis and x axis.
81 'outer_background' => 'none', // background colour of entire image.
82 'inner_background' => 'none', // background colour of plot area.
84 'y_min_left' => 0, // this will be reset to minimum value if there is a value lower than this.
85 'y_max_left' => 0, // this will be reset to maximum value if there is a value higher than this.
86 'y_min_right' => 0, // this will be reset to minimum value if there is a value lower than this.
87 'y_max_right' => 0, // this will be reset to maximum value if there is a value higher than this.
88 'x_min' => 0, // only used if x axis is numeric.
89 'x_max' => 0, // only used if x axis is numeric.
91 'y_resolution_left' => 1, // scaling for rounding of y axis max value.
92 // if max y value is 8645 then
93 // if y_resolution is 0, then y_max becomes 9000.
94 // if y_resolution is 1, then y_max becomes 8700.
95 // if y_resolution is 2, then y_max becomes 8650.
96 // if y_resolution is 3, then y_max becomes 8645.
98 'y_decimal_left' => 0, // number of decimal places for y_axis text.
99 'y_resolution_right' => 2, // ... same for right hand side
100 'y_decimal_right' => 0, // ... same for right hand side
101 'x_resolution' => 2, // only used if x axis is numeric.
102 'x_decimal' => 0, // only used if x axis is numeric.
104 'point_size' => 4, // default point size. use even number for diamond or triangle to get nice look.
105 'brush_size' => 4, // default brush size for brush line.
106 'brush_type' => 'circle', // type of brush to use to draw line. choose from the following
107 // 'circle', 'square', 'horizontal', 'vertical', 'slash', 'backslash'
108 'bar_size' => 0.8, // size of bar to draw. <1 bars won't touch
109 // 1 is full width - i.e. bars will touch.
110 // >1 means bars will overlap.
111 'bar_spacing' => 10, // space in pixels between group of bars for each x value.
112 'shadow_offset' => 3, // draw shadow at this offset, unless overidden by data parameter.
113 'shadow' => 'grayCC', // 'none' or colour of shadow.
114 'shadow_below_axis' => true, // whether to draw shadows of bars and areas below the x/zero axis.
117 'x_axis_gridlines' => 'auto', // if set to a number then x axis is treated as numeric.
118 'y_axis_gridlines' => 6, // number of gridlines on y axis.
119 'zero_axis' => 'none', // colour to draw zero-axis, or 'none'.
122 'axis_font' => 'default.ttf', // axis text font. don't forget to set 'path_to_fonts' above.
123 'axis_size' => 8, // axis text font size in points
124 'axis_colour' => 'gray33', // colour of axis text.
125 'y_axis_angle' => 0, // rotation of axis text.
126 'x_axis_angle' => 0, // rotation of axis text.
128 'y_axis_text_left' => 1, // whether to print left hand y axis text. if 0 no text, if 1 all ticks have text,
129 'x_axis_text' => 1, // if 4 then print every 4th tick and text, etc...
130 'y_axis_text_right' => 0, // behaviour same as above for right hand y axis.
132 'x_offset' => 0.5, // x axis tick offset from y axis as fraction of tick spacing.
133 'y_ticks_colour' => 'black', // colour to draw y ticks, or 'none'
134 'x_ticks_colour' => 'black', // colour to draw x ticks, or 'none'
135 'y_grid' => 'line', // grid lines. set to 'line' or 'dash'...
136 'x_grid' => 'line', // or if set to 'none' print nothing.
137 'grid_colour' => 'grayEE', // default grid colour.
138 'tick_length' => 4, // length of ticks in pixels. can be negative. i.e. outside data drawing area.
140 'legend' => 'none', // default. no legend.
141 // otherwise: 'top-left', 'top-right', 'bottom-left', 'bottom-right',
142 // 'outside-top', 'outside-bottom', 'outside-left', or 'outside-right'.
143 'legend_offset' => 10, // offset in pixels from graph or outside border.
144 'legend_padding' => 5, // padding around legend text.
145 'legend_font' => 'default.ttf', // legend text font. don't forget to set 'path_to_fonts' above.
146 'legend_size' => 8, // legend text point size.
147 'legend_colour' => 'black', // legend text colour.
148 'legend_border' => 'none', // legend border colour, or 'none'.
150 'decimal_point' => '.', // symbol for decimal separation '.' or ',' *european support.
151 'thousand_sep' => ',', // symbol for thousand separation ',' or ''
154 var $y_tick_labels = null; // array of text values for y-axis tick labels
155 var $offset_relation = null; // array of offsets for different sets of data
157 /** @var array y_order data. */
158 public $y_order = [];
160 /** @var array y_format data. */
161 public $y_format = [];
163 /** @var array x_data data. */
166 /** @var array colour. */
169 /** @var array y_data data. */
172 // init all text - title, labels, and axis text.
175 /// Moodle mods: overrides the font path and encodings
179 /// A default.ttf is searched for in this order:
180 /// dataroot/lang/xx_local/fonts
181 /// dataroot/lang/xx/fonts
182 /// dirroot/lang/xx/fonts
186 $currlang = current_language();
187 if (file_exists("$CFG->dataroot/lang/".$currlang."_local/fonts/default.ttf")) {
188 $fontpath = "$CFG->dataroot/lang/".$currlang."_local/fonts/";
189 } else if (file_exists("$CFG->dataroot/lang/$currlang/fonts/default.ttf")) {
190 $fontpath = "$CFG->dataroot/lang/$currlang/fonts/";
191 } else if (file_exists("$CFG->dirroot/lang/$currlang/fonts/default.ttf")) {
192 $fontpath = "$CFG->dirroot/lang/$currlang/fonts/";
193 } else if (file_exists("$CFG->dataroot/lang/default.ttf")) {
194 $fontpath = "$CFG->dataroot/lang/";
196 $fontpath = "$CFG->libdir/";
199 $this->parameter
['path_to_fonts'] = $fontpath;
205 $this->calculated
['outer_border'] = $this->calculated
['boundary_box'];
208 $this->calculated
['boundary_box']['left'] +
= $this->parameter
['outer_padding'];
209 $this->calculated
['boundary_box']['top'] +
= $this->parameter
['outer_padding'];
210 $this->calculated
['boundary_box']['right'] -= $this->parameter
['outer_padding'];
211 $this->calculated
['boundary_box']['bottom'] -= $this->parameter
['outer_padding'];
213 $this->init_x_axis();
214 $this->init_y_axis();
215 $this->init_legend();
216 $this->init_labels();
218 // take into account tick lengths
219 $this->calculated
['bottom_inner_padding'] = $this->parameter
['x_inner_padding'];
220 if (($this->parameter
['x_ticks_colour'] != 'none') && ($this->parameter
['tick_length'] < 0))
221 $this->calculated
['bottom_inner_padding'] -= $this->parameter
['tick_length'];
222 $this->calculated
['boundary_box']['bottom'] -= $this->calculated
['bottom_inner_padding'];
224 $this->calculated
['left_inner_padding'] = $this->parameter
['y_inner_padding'];
225 if ($this->parameter
['y_axis_text_left']) {
226 if (($this->parameter
['y_ticks_colour'] != 'none') && ($this->parameter
['tick_length'] < 0))
227 $this->calculated
['left_inner_padding'] -= $this->parameter
['tick_length'];
229 $this->calculated
['boundary_box']['left'] +
= $this->calculated
['left_inner_padding'];
231 $this->calculated
['right_inner_padding'] = $this->parameter
['y_inner_padding'];
232 if ($this->parameter
['y_axis_text_right']) {
233 if (($this->parameter
['y_ticks_colour'] != 'none') && ($this->parameter
['tick_length'] < 0))
234 $this->calculated
['right_inner_padding'] -= $this->parameter
['tick_length'];
236 $this->calculated
['boundary_box']['right'] -= $this->calculated
['right_inner_padding'];
238 // boundaryBox now has coords for plotting area.
239 $this->calculated
['inner_border'] = $this->calculated
['boundary_box'];
242 $this->init_x_ticks();
243 $this->init_y_ticks();
246 function draw_text() {
247 $colour = $this->parameter
['outer_background'];
248 if ($colour != 'none') $this->draw_rectangle($this->calculated
['outer_border'], $colour, 'fill'); // graph background
250 // draw border around image
251 $colour = $this->parameter
['outer_border'];
252 if ($colour != 'none') $this->draw_rectangle($this->calculated
['outer_border'], $colour, 'box'); // graph border
255 $this->draw_x_label();
256 $this->draw_y_label_left();
257 $this->draw_y_label_right();
258 $this->draw_x_axis();
259 $this->draw_y_axis();
260 if ($this->calculated
['y_axis_left']['has_data']) $this->draw_zero_axis_left(); // either draw zero axis on left
261 else if ($this->calculated
['y_axis_right']['has_data']) $this->draw_zero_axis_right(); // ... or right.
262 $this->draw_legend();
264 // draw border around plot area
265 $colour = $this->parameter
['inner_background'];
266 if ($colour != 'none') $this->draw_rectangle($this->calculated
['inner_border'], $colour, 'fill'); // graph background
268 // draw border around image
269 $colour = $this->parameter
['inner_border'];
270 if ($colour != 'none') $this->draw_rectangle($this->calculated
['inner_border'], $colour, $this->parameter
['inner_border_type']); // graph border
273 function draw_stack() {
277 $yOrder = $this->y_order
; // save y_order data.
278 // iterate over each data set. order is very important if you want to see data correctly. remember shadows!!
279 foreach ($yOrder as $set) {
280 $this->y_order
= array($set);
284 $this->y_order
= $yOrder; // revert y_order data.
297 function draw_set($order, $set, $offset) {
298 if ($offset) @$this->init_variable($colour, $this->y_format
[$set]['shadow'], $this->parameter
['shadow']);
299 else $colour = $this->y_format
[$set]['colour'];
300 @$this->init_variable($point, $this->y_format
[$set]['point'], 'none');
301 @$this->init_variable($pointSize, $this->y_format
[$set]['point_size'], $this->parameter
['point_size']);
302 @$this->init_variable($line, $this->y_format
[$set]['line'], 'none');
303 @$this->init_variable($brushType, $this->y_format
[$set]['brush_type'], $this->parameter
['brush_type']);
304 @$this->init_variable($brushSize, $this->y_format
[$set]['brush_size'], $this->parameter
['brush_size']);
305 @$this->init_variable($bar, $this->y_format
[$set]['bar'], 'none');
306 @$this->init_variable($barSize, $this->y_format
[$set]['bar_size'], $this->parameter
['bar_size']);
307 @$this->init_variable($area, $this->y_format
[$set]['area'], 'none');
314 //print "set $set<br />";
315 //expand_pre($this->calculated['y_plot']);
317 foreach ($this->x_data
as $index => $x) {
318 //print "index $index<br />";
319 $thisY = $this->calculated
['y_plot'][$set][$index];
320 $thisX = $this->calculated
['x_plot'][$index];
322 //print "$thisX, $thisY <br />";
324 if (($bar!='none') && (string)$thisY != 'none') {
325 if (isset($this->offset_relation
[$set]) && $relatedset = $this->offset_relation
[$set]) {
326 $yoffset = $this->calculated
['y_plot'][$relatedset][$index]; // Moodle
328 $yoffset = 0; // Moodle
330 //$this->bar($thisX, $thisY, $bar, $barSize, $colour, $offset, $set); // Moodle
331 $this->bar($thisX, $thisY, $bar, $barSize, $colour, $offset, $set, $yoffset); // Moodle
334 if (($area!='none') && (((string)$lastY != 'none') && ((string)$thisY != 'none')))
335 $this->area($lastX, $lastY, $thisX, $thisY, $area, $colour, $offset);
337 if (($point!='none') && (string)$thisY != 'none') $this->plot($thisX, $thisY, $point, $pointSize, $colour, $offset);
339 if (($line!='none') && ((string)$thisY != 'none')) {
340 if ((string)$fromY != 'none')
341 $this->line($fromX, $fromY, $thisX, $thisY, $line, $brushType, $brushSize, $colour, $offset);
343 $fromY = $thisY; // start next line from here
344 $fromX = $thisX; // ...
355 function draw_data() {
356 // cycle thru y data to be plotted
357 // first check for drop shadows...
358 foreach ($this->y_order
as $order => $set) {
359 @$this->init_variable($offset, $this->y_format
[$set]['shadow_offset'], $this->parameter
['shadow_offset']);
360 @$this->init_variable($colour, $this->y_format
[$set]['shadow'], $this->parameter
['shadow']);
361 if ($colour != 'none') $this->draw_set($order, $set, $offset);
366 foreach ($this->y_order
as $order => $set) {
367 $this->draw_set($order, $set, 0);
371 function draw_legend() {
372 $position = $this->parameter
['legend'];
373 if ($position == 'none') return; // abort if no border
375 $borderColour = $this->parameter
['legend_border'];
376 $offset = $this->parameter
['legend_offset'];
377 $padding = $this->parameter
['legend_padding'];
378 $height = $this->calculated
['legend']['boundary_box_all']['height'];
379 $width = $this->calculated
['legend']['boundary_box_all']['width'];
380 $graphTop = $this->calculated
['boundary_box']['top'];
381 $graphBottom = $this->calculated
['boundary_box']['bottom'];
382 $graphLeft = $this->calculated
['boundary_box']['left'];
383 $graphRight = $this->calculated
['boundary_box']['right'];
384 $outsideRight = $this->calculated
['outer_border']['right'];
385 $outsideBottom = $this->calculated
['outer_border']['bottom'];
388 $top = $graphTop +
$offset;
389 $bottom = $graphTop +
$height +
$offset;
390 $left = $graphLeft +
$offset;
391 $right = $graphLeft +
$width +
$offset;
395 $top = $graphTop +
$offset;
396 $bottom = $graphTop +
$height +
$offset;
397 $left = $graphRight - $width - $offset;
398 $right = $graphRight - $offset;
402 $top = $graphBottom - $height - $offset;
403 $bottom = $graphBottom - $offset;
404 $left = $graphLeft +
$offset;
405 $right = $graphLeft +
$width +
$offset;
409 $top = $graphBottom - $height - $offset;
410 $bottom = $graphBottom - $offset;
411 $left = $graphRight - $width - $offset;
412 $right = $graphRight - $offset;
417 $bottom = $graphTop +
$height;
418 $left = $outsideRight - $width - $offset;
419 $right = $outsideRight - $offset;
422 case 'outside-bottom' :
423 $top = $graphBottom - $height;
424 $bottom = $graphBottom;
425 $left = $outsideRight - $width - $offset;
426 $right = $outsideRight - $offset;
429 case 'outside-left' :
430 $top = $outsideBottom - $height - $offset;
431 $bottom = $outsideBottom - $offset;
433 $right = $graphLeft +
$width;
436 case 'outside-right' :
437 $top = $outsideBottom - $height - $offset;
438 $bottom = $outsideBottom - $offset;
439 $left = $graphRight - $width;
440 $right = $graphRight;
442 default: // default is top left. no particular reason.
443 $top = $this->calculated
['boundary_box']['top'];
444 $bottom = $this->calculated
['boundary_box']['top'] +
$this->calculated
['legend']['boundary_box_all']['height'];
445 $left = $this->calculated
['boundary_box']['left'];
446 $right = $this->calculated
['boundary_box']['right'] +
$this->calculated
['legend']['boundary_box_all']['width'];
450 if($borderColour!='none') $this->draw_rectangle(array('top' => $top,
453 'right' => $right), $this->parameter
['legend_border'], 'box');
456 $legendText = array('points' => $this->parameter
['legend_size'],
458 'font' => $this->parameter
['legend_font'],
459 'colour' => $this->parameter
['legend_colour']);
461 $box = $this->calculated
['legend']['boundary_box_max']['height']; // use max height for legend square size.
462 $x = $left +
$padding;
463 $x_text = $x +
$box * 2;
464 $y = $top +
$padding;
466 foreach ($this->y_order
as $set) {
467 $legendText['text'] = $this->calculated
['legend']['text'][$set];
468 if ($legendText['text'] != 'none') {
469 // if text exists then draw box and text
470 $boxColour = $this->colour
[$this->y_format
[$set]['colour']];
473 ImageFilledRectangle($this->image
, $x, $y, $x +
$box, $y +
$box, $boxColour);
476 $coords = array('x' => $x +
$box * 2, 'y' => $y, 'reference' => 'top-left');
477 $legendText['boundary_box'] = $this->calculated
['legend']['boundary_box'][$set];
478 $this->update_boundaryBox($legendText['boundary_box'], $coords);
479 $this->print_TTF($legendText);
480 $y +
= $padding +
$box;
486 function draw_y_label_right() {
487 if (!$this->parameter
['y_label_right']) return;
488 $x = $this->calculated
['boundary_box']['right'] +
$this->parameter
['y_inner_padding'];
489 if ($this->parameter
['y_axis_text_right']) $x +
= $this->calculated
['y_axis_right']['boundary_box_max']['width']
490 +
$this->calculated
['right_inner_padding'];
491 $y = ($this->calculated
['boundary_box']['bottom'] +
$this->calculated
['boundary_box']['top']) / 2;
493 $label = $this->calculated
['y_label_right'];
494 $coords = array('x' => $x, 'y' => $y, 'reference' => 'left-center');
495 $this->update_boundaryBox($label['boundary_box'], $coords);
496 $this->print_TTF($label);
500 function draw_y_label_left() {
501 if (!$this->parameter
['y_label_left']) return;
502 $x = $this->calculated
['boundary_box']['left'] - $this->parameter
['y_inner_padding'];
503 if ($this->parameter
['y_axis_text_left']) $x -= $this->calculated
['y_axis_left']['boundary_box_max']['width']
504 +
$this->calculated
['left_inner_padding'];
505 $y = ($this->calculated
['boundary_box']['bottom'] +
$this->calculated
['boundary_box']['top']) / 2;
507 $label = $this->calculated
['y_label_left'];
508 $coords = array('x' => $x, 'y' => $y, 'reference' => 'right-center');
509 $this->update_boundaryBox($label['boundary_box'], $coords);
510 $this->print_TTF($label);
513 function draw_title() {
514 if (!$this->parameter
['title']) return;
515 //$y = $this->calculated['outside_border']['top'] + $this->parameter['outer_padding'];
516 $y = $this->calculated
['boundary_box']['top'] - $this->parameter
['outer_padding'];
517 $x = ($this->calculated
['boundary_box']['right'] +
$this->calculated
['boundary_box']['left']) / 2;
518 $label = $this->calculated
['title'];
519 $coords = array('x' => $x, 'y' => $y, 'reference' => 'bottom-center');
520 $this->update_boundaryBox($label['boundary_box'], $coords);
521 $this->print_TTF($label);
524 function draw_x_label() {
525 if (!$this->parameter
['x_label']) return;
526 $y = $this->calculated
['boundary_box']['bottom'] +
$this->parameter
['x_inner_padding'];
527 if ($this->parameter
['x_axis_text']) $y +
= $this->calculated
['x_axis']['boundary_box_max']['height']
528 +
$this->calculated
['bottom_inner_padding'];
529 $x = ($this->calculated
['boundary_box']['right'] +
$this->calculated
['boundary_box']['left']) / 2;
530 $label = $this->calculated
['x_label'];
531 $coords = array('x' => $x, 'y' => $y, 'reference' => 'top-center');
532 $this->update_boundaryBox($label['boundary_box'], $coords);
533 $this->print_TTF($label);
536 function draw_zero_axis_left() {
537 $colour = $this->parameter
['zero_axis'];
538 if ($colour == 'none') return;
539 // draw zero axis on left hand side
540 $this->calculated
['zero_axis'] = round($this->calculated
['boundary_box']['top'] +
($this->calculated
['y_axis_left']['max'] * $this->calculated
['y_axis_left']['factor']));
541 ImageLine($this->image
, $this->calculated
['boundary_box']['left'], $this->calculated
['zero_axis'], $this->calculated
['boundary_box']['right'], $this->calculated
['zero_axis'], $this->colour
[$colour]);
544 function draw_zero_axis_right() {
545 $colour = $this->parameter
['zero_axis'];
546 if ($colour == 'none') return;
547 // draw zero axis on right hand side
548 $this->calculated
['zero_axis'] = round($this->calculated
['boundary_box']['top'] +
($this->calculated
['y_axis_right']['max'] * $this->calculated
['y_axis_right']['factor']));
549 ImageLine($this->image
, $this->calculated
['boundary_box']['left'], $this->calculated
['zero_axis'], $this->calculated
['boundary_box']['right'], $this->calculated
['zero_axis'], $this->colour
[$colour]);
552 function draw_x_axis() {
553 $gridColour = $this->colour
[$this->parameter
['grid_colour']];
554 $tickColour = $this->colour
[$this->parameter
['x_ticks_colour']];
555 $axis_colour = $this->parameter
['axis_colour'];
556 $xGrid = $this->parameter
['x_grid'];
557 $gridTop = (int) round($this->calculated
['boundary_box']['top']);
558 $gridBottom = (int) round($this->calculated
['boundary_box']['bottom']);
560 if ($this->parameter
['tick_length'] >= 0) {
561 $tickTop = $this->calculated
['boundary_box']['bottom'] - $this->parameter
['tick_length'];
562 $tickBottom = $this->calculated
['boundary_box']['bottom'];
563 $textBottom = $tickBottom +
$this->calculated
['bottom_inner_padding'];
565 $tickTop = $this->calculated
['boundary_box']['bottom'];
566 $tickBottom = $this->calculated
['boundary_box']['bottom'] - $this->parameter
['tick_length'];
567 $textBottom = $tickBottom +
$this->calculated
['bottom_inner_padding'];
570 $axis_font = $this->parameter
['axis_font'];
571 $axis_size = $this->parameter
['axis_size'];
572 $axis_angle = $this->parameter
['x_axis_angle'];
574 if ($axis_angle == 0) $reference = 'top-center';
575 if ($axis_angle > 0) $reference = 'top-right';
576 if ($axis_angle < 0) $reference = 'top-left';
577 if ($axis_angle == 90) $reference = 'top-center';
579 //generic tag information. applies to all axis text.
580 $axisTag = array('points' => $axis_size, 'angle' => $axis_angle, 'font' => $axis_font, 'colour' => $axis_colour);
582 foreach ($this->calculated
['x_axis']['tick_x'] as $set => $tickX) {
583 $tickX = (int) round($tickX);
584 // draw x grid if colour specified
585 if ($xGrid != 'none') {
588 ImageLine($this->image
, $tickX, $gridTop, $tickX, $gridBottom, $gridColour);
591 $this->image_dashed_line($this->image
, $tickX, $gridTop, $tickX, $gridBottom, $gridColour); // Moodle
596 if ($this->parameter
['x_axis_text'] && !($set %
$this->parameter
['x_axis_text'])) { // test if tick should be displayed
598 if ($tickColour != 'none')
599 ImageLine($this->image
, $tickX, $tickTop, $tickX, $tickBottom, $tickColour);
602 $coords = array('x' => $tickX, 'y' => $textBottom, 'reference' => $reference);
603 $axisTag['text'] = $this->calculated
['x_axis']['text'][$set];
604 $axisTag['boundary_box'] = $this->calculated
['x_axis']['boundary_box'][$set];
605 $this->update_boundaryBox($axisTag['boundary_box'], $coords);
606 $this->print_TTF($axisTag);
611 function draw_y_axis() {
612 $gridColour = $this->colour
[$this->parameter
['grid_colour']];
613 $tickColour = $this->colour
[$this->parameter
['y_ticks_colour']];
614 $axis_colour = $this->parameter
['axis_colour'];
615 $yGrid = $this->parameter
['y_grid'];
616 $gridLeft = (int) round($this->calculated
['boundary_box']['left']);
617 $gridRight = (int) round($this->calculated
['boundary_box']['right']);
619 // axis font information
620 $axis_font = $this->parameter
['axis_font'];
621 $axis_size = $this->parameter
['axis_size'];
622 $axis_angle = $this->parameter
['y_axis_angle'];
623 $axisTag = array('points' => $axis_size, 'angle' => $axis_angle, 'font' => $axis_font, 'colour' => $axis_colour);
626 if ($this->calculated
['y_axis_left']['has_data']) {
628 // left and right coords for ticks
629 if ($this->parameter
['tick_length'] >= 0) {
630 $tickLeft = $this->calculated
['boundary_box']['left'];
631 $tickRight = $this->calculated
['boundary_box']['left'] +
$this->parameter
['tick_length'];
633 $tickLeft = $this->calculated
['boundary_box']['left'] +
$this->parameter
['tick_length'];
634 $tickRight = $this->calculated
['boundary_box']['left'];
636 $textRight = $tickLeft - $this->calculated
['left_inner_padding'];
638 if ($axis_angle == 0) $reference = 'right-center';
639 if ($axis_angle > 0) $reference = 'right-top';
640 if ($axis_angle < 0) $reference = 'right-bottom';
641 if ($axis_angle == 90) $reference = 'right-center';
643 foreach ($this->calculated
['y_axis']['tick_y'] as $set => $tickY) {
644 $tickY = (int) round($tickY);
645 // draw y grid if colour specified
646 if ($yGrid != 'none') {
649 ImageLine($this->image
, $gridLeft, $tickY, $gridRight, $tickY, $gridColour);
652 $this->image_dashed_line($this->image
, $gridLeft, $tickY, $gridRight, $tickY, $gridColour); // Moodle
658 if ($this->parameter
['y_axis_text_left'] && !($set %
$this->parameter
['y_axis_text_left'])) { // test if tick should be displayed
660 if ($tickColour != 'none')
661 ImageLine($this->image
, $tickLeft, $tickY, $tickRight, $tickY, $tickColour);
664 $coords = array('x' => $textRight, 'y' => $tickY, 'reference' => $reference);
665 $axisTag['text'] = $this->calculated
['y_axis_left']['text'][$set];
666 $axisTag['boundary_box'] = $this->calculated
['y_axis_left']['boundary_box'][$set];
667 $this->update_boundaryBox($axisTag['boundary_box'], $coords);
668 $this->print_TTF($axisTag);
673 if ($this->calculated
['y_axis_right']['has_data']) {
675 // left and right coords for ticks
676 if ($this->parameter
['tick_length'] >= 0) {
677 $tickLeft = $this->calculated
['boundary_box']['right'] - $this->parameter
['tick_length'];
678 $tickRight = $this->calculated
['boundary_box']['right'];
680 $tickLeft = $this->calculated
['boundary_box']['right'];
681 $tickRight = $this->calculated
['boundary_box']['right'] - $this->parameter
['tick_length'];
683 $textLeft = $tickRight+
$this->calculated
['left_inner_padding'];
685 if ($axis_angle == 0) $reference = 'left-center';
686 if ($axis_angle > 0) $reference = 'left-bottom';
687 if ($axis_angle < 0) $reference = 'left-top';
688 if ($axis_angle == 90) $reference = 'left-center';
690 foreach ($this->calculated
['y_axis']['tick_y'] as $set => $tickY) {
691 if (!$this->calculated
['y_axis_left']['has_data'] && $yGrid != 'none') { // draw grid if not drawn already (above)
694 ImageLine($this->image
, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour);
697 $this->image_dashed_line($this->image
, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour); // Moodle
702 if ($this->parameter
['y_axis_text_right'] && !($set %
$this->parameter
['y_axis_text_right'])) { // test if tick should be displayed
704 if ($tickColour != 'none')
705 ImageLine($this->image
, round($tickLeft), round($tickY), round($tickRight), round($tickY), $tickColour);
708 $coords = array('x' => $textLeft, 'y' => $tickY, 'reference' => $reference);
709 $axisTag['text'] = $this->calculated
['y_axis_right']['text'][$set];
710 $axisTag['boundary_box'] = $this->calculated
['y_axis_left']['boundary_box'][$set];
711 $this->update_boundaryBox($axisTag['boundary_box'], $coords);
712 $this->print_TTF($axisTag);
718 function init_data() {
719 $this->calculated
['y_plot'] = array(); // array to hold pixel plotting coords for y axis
720 $height = $this->calculated
['boundary_box']['bottom'] - $this->calculated
['boundary_box']['top'];
721 $width = $this->calculated
['boundary_box']['right'] - $this->calculated
['boundary_box']['left'];
723 // calculate pixel steps between axis ticks.
724 $this->calculated
['y_axis']['step'] = $height / ($this->parameter
['y_axis_gridlines'] - 1);
726 // calculate x ticks spacing taking into account x offset for ticks.
727 $extraTick = 2 * $this->parameter
['x_offset']; // extra tick to account for padding
728 $numTicks = $this->calculated
['x_axis']['num_ticks'] - 1; // number of x ticks
730 // Hack by rodger to avoid division by zero, see bug 1231
731 if ($numTicks==0) $numTicks=1;
733 $this->calculated
['x_axis']['step'] = $width / ($numTicks +
$extraTick);
734 $widthPlot = $width - ($this->calculated
['x_axis']['step'] * $extraTick);
735 $this->calculated
['x_axis']['step'] = $widthPlot / $numTicks;
737 //calculate factor for transforming x,y physical coords to logical coords for right hand y_axis.
738 $y_range = $this->calculated
['y_axis_right']['max'] - $this->calculated
['y_axis_right']['min'];
739 $y_range = ($y_range ?
$y_range : 1);
740 $this->calculated
['y_axis_right']['factor'] = $height / $y_range;
742 //calculate factor for transforming x,y physical coords to logical coords for left hand axis.
743 $yRange = $this->calculated
['y_axis_left']['max'] - $this->calculated
['y_axis_left']['min'];
744 $yRange = ($yRange ?
$yRange : 1);
745 $this->calculated
['y_axis_left']['factor'] = $height / $yRange;
746 if ($this->parameter
['x_axis_gridlines'] != 'auto') {
747 $xRange = $this->calculated
['x_axis']['max'] - $this->calculated
['x_axis']['min'];
748 $xRange = ($xRange ?
$xRange : 1);
749 $this->calculated
['x_axis']['factor'] = $widthPlot / $xRange;
752 //expand_pre($this->calculated['boundary_box']);
753 // cycle thru all data sets...
754 $this->calculated
['num_bars'] = 0;
755 foreach ($this->y_order
as $order => $set) {
756 // determine how many bars there are
757 if (isset($this->y_format
[$set]['bar']) && ($this->y_format
[$set]['bar'] != 'none')) {
758 $this->calculated
['bar_offset_index'][$set] = $this->calculated
['num_bars']; // index to relate bar with data set.
759 $this->calculated
['num_bars']++
;
762 // calculate y coords for plotting data
763 foreach ($this->x_data
as $index => $x) {
764 $this->calculated
['y_plot'][$set][$index] = $this->y_data
[$set][$index];
766 if ((string)$this->y_data
[$set][$index] != 'none') {
768 if (isset($this->y_format
[$set]['y_axis']) && $this->y_format
[$set]['y_axis'] == 'right') {
769 $this->calculated
['y_plot'][$set][$index] =
770 round(($this->y_data
[$set][$index] - $this->calculated
['y_axis_right']['min'])
771 * $this->calculated
['y_axis_right']['factor']);
773 //print "$set $index<br />";
774 $this->calculated
['y_plot'][$set][$index] =
775 round(($this->y_data
[$set][$index] - $this->calculated
['y_axis_left']['min'])
776 * $this->calculated
['y_axis_left']['factor']);
782 //print "factor ".$this->calculated['x_axis']['factor']."<br />";
783 //expand_pre($this->calculated['x_plot']);
785 // calculate bar parameters if bars are to be drawn.
786 if ($this->calculated
['num_bars']) {
787 $xStep = $this->calculated
['x_axis']['step'];
788 $totalWidth = $this->calculated
['x_axis']['step'] - $this->parameter
['bar_spacing'];
789 $barWidth = $totalWidth / $this->calculated
['num_bars'];
791 $barX = ($barWidth - $totalWidth) / 2; // starting x offset
792 for ($i=0; $i < $this->calculated
['num_bars']; $i++
) {
793 $this->calculated
['bar_offset_x'][$i] = $barX;
794 $barX +
= $barWidth; // add width of bar to x offset.
796 $this->calculated
['bar_width'] = $barWidth;
802 function init_x_ticks() {
803 // get coords for x axis ticks and data plots
804 //$xGrid = $this->parameter['x_grid'];
805 $xStep = $this->calculated
['x_axis']['step'];
806 $ticksOffset = $this->parameter
['x_offset']; // where to start drawing ticks relative to y axis.
807 $gridLeft = $this->calculated
['boundary_box']['left'] +
($xStep * $ticksOffset); // grid x start
808 $tickX = $gridLeft; // tick x coord
810 foreach ($this->calculated
['x_axis']['text'] as $set => $value) {
811 //print "index: $set<br />";
813 $this->calculated
['x_axis']['tick_x'][$set] = $tickX;
814 // if num ticks is auto then x plot value is same as x tick
815 if ($this->parameter
['x_axis_gridlines'] == 'auto') $this->calculated
['x_plot'][$set] = round($tickX);
816 //print $this->calculated['x_plot'][$set].'<br />';
820 //print "xStep: $xStep <br />";
821 // if numeric x axis then calculate x coords for each data point. this is seperate from x ticks.
823 if (empty($this->calculated
['x_axis']['factor'])) {
824 $this->calculated
['x_axis']['factor'] = 0;
826 if (empty($this->calculated
['x_axis']['min'])) {
827 $this->calculated
['x_axis']['min'] = 0;
829 $factor = $this->calculated
['x_axis']['factor'];
830 $min = $this->calculated
['x_axis']['min'];
832 if ($this->parameter
['x_axis_gridlines'] != 'auto') {
833 foreach ($this->x_data
as $index => $x) {
834 //print "index: $index, x: $x<br />";
835 $offset = $x - $this->calculated
['x_axis']['min'];
837 //$gridX = ($offset * $this->calculated['x_axis']['factor']);
838 //print "offset: $offset <br />";
839 //$this->calculated['x_plot'][$set] = $gridLeft + ($offset * $this->calculated['x_axis']['factor']);
841 $this->calculated
['x_plot'][$index] = $gridLeft +
($x - $min) * $factor;
843 //print $this->calculated['x_plot'][$set].'<br />';
846 //expand_pre($this->calculated['boundary_box']);
847 //print "factor ".$this->calculated['x_axis']['factor']."<br />";
848 //expand_pre($this->calculated['x_plot']);
851 function init_y_ticks() {
852 // get coords for y axis ticks
854 $yStep = $this->calculated
['y_axis']['step'];
855 $gridBottom = $this->calculated
['boundary_box']['bottom'];
856 $tickY = $gridBottom; // tick y coord
858 for ($i = 0; $i < $this->parameter
['y_axis_gridlines']; $i++
) {
859 $this->calculated
['y_axis']['tick_y'][$i] = $tickY;
865 function init_labels() {
866 if ($this->parameter
['title']) {
867 $size = $this->get_boundaryBox(
868 array('points' => $this->parameter
['title_size'],
870 'font' => $this->parameter
['title_font'],
871 'text' => $this->parameter
['title']));
872 $this->calculated
['title']['boundary_box'] = $size;
873 $this->calculated
['title']['text'] = $this->parameter
['title'];
874 $this->calculated
['title']['font'] = $this->parameter
['title_font'];
875 $this->calculated
['title']['points'] = $this->parameter
['title_size'];
876 $this->calculated
['title']['colour'] = $this->parameter
['title_colour'];
877 $this->calculated
['title']['angle'] = 0;
879 $this->calculated
['boundary_box']['top'] +
= $size['height'] +
$this->parameter
['outer_padding'];
880 //$this->calculated['boundary_box']['top'] += $size['height'];
882 } else $this->calculated
['title']['boundary_box'] = $this->get_null_size();
884 if ($this->parameter
['y_label_left']) {
885 $this->calculated
['y_label_left']['text'] = $this->parameter
['y_label_left'];
886 $this->calculated
['y_label_left']['angle'] = $this->parameter
['y_label_angle'];
887 $this->calculated
['y_label_left']['font'] = $this->parameter
['label_font'];
888 $this->calculated
['y_label_left']['points'] = $this->parameter
['label_size'];
889 $this->calculated
['y_label_left']['colour'] = $this->parameter
['label_colour'];
891 $size = $this->get_boundaryBox($this->calculated
['y_label_left']);
892 $this->calculated
['y_label_left']['boundary_box'] = $size;
893 //$this->calculated['boundary_box']['left'] += $size['width'] + $this->parameter['inner_padding'];
894 $this->calculated
['boundary_box']['left'] +
= $size['width'];
896 } else $this->calculated
['y_label_left']['boundary_box'] = $this->get_null_size();
898 if ($this->parameter
['y_label_right']) {
899 $this->calculated
['y_label_right']['text'] = $this->parameter
['y_label_right'];
900 $this->calculated
['y_label_right']['angle'] = $this->parameter
['y_label_angle'];
901 $this->calculated
['y_label_right']['font'] = $this->parameter
['label_font'];
902 $this->calculated
['y_label_right']['points'] = $this->parameter
['label_size'];
903 $this->calculated
['y_label_right']['colour'] = $this->parameter
['label_colour'];
905 $size = $this->get_boundaryBox($this->calculated
['y_label_right']);
906 $this->calculated
['y_label_right']['boundary_box'] = $size;
907 //$this->calculated['boundary_box']['right'] -= $size['width'] + $this->parameter['inner_padding'];
908 $this->calculated
['boundary_box']['right'] -= $size['width'];
910 } else $this->calculated
['y_label_right']['boundary_box'] = $this->get_null_size();
912 if ($this->parameter
['x_label']) {
913 $this->calculated
['x_label']['text'] = $this->parameter
['x_label'];
914 $this->calculated
['x_label']['angle'] = $this->parameter
['x_label_angle'];
915 $this->calculated
['x_label']['font'] = $this->parameter
['label_font'];
916 $this->calculated
['x_label']['points'] = $this->parameter
['label_size'];
917 $this->calculated
['x_label']['colour'] = $this->parameter
['label_colour'];
919 $size = $this->get_boundaryBox($this->calculated
['x_label']);
920 $this->calculated
['x_label']['boundary_box'] = $size;
921 //$this->calculated['boundary_box']['bottom'] -= $size['height'] + $this->parameter['inner_padding'];
922 $this->calculated
['boundary_box']['bottom'] -= $size['height'];
924 } else $this->calculated
['x_label']['boundary_box'] = $this->get_null_size();
929 function init_legend() {
930 $this->calculated
['legend'] = array(); // array to hold calculated values for legend.
931 //$this->calculated['legend']['boundary_box_max'] = array('height' => 0, 'width' => 0);
932 $this->calculated
['legend']['boundary_box_max'] = $this->get_null_size();
933 if ($this->parameter
['legend'] == 'none') return;
935 $position = $this->parameter
['legend'];
936 $numSets = 0; // number of data sets with legends.
937 $sumTextHeight = 0; // total of height of all legend text items.
941 foreach ($this->y_order
as $set) {
942 $text = isset($this->y_format
[$set]['legend']) ?
$this->y_format
[$set]['legend'] : 'none';
943 $size = $this->get_boundaryBox(
944 array('points' => $this->parameter
['legend_size'],
946 'font' => $this->parameter
['legend_font'],
949 $this->calculated
['legend']['boundary_box'][$set] = $size;
950 $this->calculated
['legend']['text'][$set] = $text;
951 //$this->calculated['legend']['font'][$set] = $this->parameter['legend_font'];
952 //$this->calculated['legend']['points'][$set] = $this->parameter['legend_size'];
953 //$this->calculated['legend']['angle'][$set] = 0;
955 if ($text && $text!='none') {
957 $sumTextHeight +
= $size['height'];
960 if ($size['width'] > $this->calculated
['legend']['boundary_box_max']['width'])
961 $this->calculated
['legend']['boundary_box_max'] = $size;
964 $offset = $this->parameter
['legend_offset']; // offset in pixels of legend box from graph border.
965 $padding = $this->parameter
['legend_padding']; // padding in pixels around legend text.
966 $textWidth = $this->calculated
['legend']['boundary_box_max']['width']; // width of largest legend item.
967 $textHeight = $this->calculated
['legend']['boundary_box_max']['height']; // use height as size to use for colour square in legend.
968 $width = $padding * 2 +
$textWidth +
$textHeight * 2; // left and right padding + maximum text width + space for square
969 $height = ($padding +
$textHeight) * $numSets +
$padding; // top and bottom padding + padding between text + text.
971 $this->calculated
['legend']['boundary_box_all'] = array('width' => $width,
974 'reference' => $position);
976 switch ($position) { // move in right or bottom if legend is outside data plotting area.
978 $this->calculated
['boundary_box']['right'] -= $offset +
$width; // move in right hand side
981 case 'outside-bottom' :
982 $this->calculated
['boundary_box']['right'] -= $offset +
$width; // move in right hand side
985 case 'outside-left' :
986 $this->calculated
['boundary_box']['bottom'] -= $offset +
$height; // move in right hand side
989 case 'outside-right' :
990 $this->calculated
['boundary_box']['bottom'] -= $offset +
$height; // move in right hand side
995 function init_y_axis() {
996 $this->calculated
['y_axis_left'] = array(); // array to hold calculated values for y_axis on left.
997 $this->calculated
['y_axis_left']['boundary_box_max'] = $this->get_null_size();
998 $this->calculated
['y_axis_right'] = array(); // array to hold calculated values for y_axis on right.
999 $this->calculated
['y_axis_right']['boundary_box_max'] = $this->get_null_size();
1001 $axis_font = $this->parameter
['axis_font'];
1002 $axis_size = $this->parameter
['axis_size'];
1003 $axis_colour = $this->parameter
['axis_colour'];
1004 $axis_angle = $this->parameter
['y_axis_angle'];
1005 $y_tick_labels = $this->y_tick_labels
;
1007 $this->calculated
['y_axis_left']['has_data'] = FALSE;
1008 $this->calculated
['y_axis_right']['has_data'] = FALSE;
1010 // find min and max y values.
1011 $minLeft = $this->parameter
['y_min_left'];
1012 $maxLeft = $this->parameter
['y_max_left'];
1013 $minRight = $this->parameter
['y_min_right'];
1014 $maxRight = $this->parameter
['y_max_right'];
1015 $dataLeft = array();
1016 $dataRight = array();
1017 foreach ($this->y_order
as $order => $set) {
1018 if (isset($this->y_format
[$set]['y_axis']) && $this->y_format
[$set]['y_axis'] == 'right') {
1019 $this->calculated
['y_axis_right']['has_data'] = TRUE;
1020 $dataRight = array_merge($dataRight, $this->y_data
[$set]);
1022 $this->calculated
['y_axis_left']['has_data'] = TRUE;
1023 $dataLeft = array_merge($dataLeft, $this->y_data
[$set]);
1026 $dataLeftRange = $this->find_range($dataLeft, $minLeft, $maxLeft, $this->parameter
['y_resolution_left']);
1027 $dataRightRange = $this->find_range($dataRight, $minRight, $maxRight, $this->parameter
['y_resolution_right']);
1028 $minLeft = $dataLeftRange['min'];
1029 $maxLeft = $dataLeftRange['max'];
1030 $minRight = $dataRightRange['min'];
1031 $maxRight = $dataRightRange['max'];
1033 $this->calculated
['y_axis_left']['min'] = $minLeft;
1034 $this->calculated
['y_axis_left']['max'] = $maxLeft;
1035 $this->calculated
['y_axis_right']['min'] = $minRight;
1036 $this->calculated
['y_axis_right']['max'] = $maxRight;
1038 $stepLeft = ($maxLeft - $minLeft) / ($this->parameter
['y_axis_gridlines'] - 1);
1039 $startLeft = $minLeft;
1040 $step_right = ($maxRight - $minRight) / ($this->parameter
['y_axis_gridlines'] - 1);
1041 $start_right = $minRight;
1043 if ($this->parameter
['y_axis_text_left']) {
1044 for ($i = 0; $i < $this->parameter
['y_axis_gridlines']; $i++
) { // calculate y axis text sizes
1046 if ($y_tick_labels) {
1047 $value = $y_tick_labels[$i];
1049 $value = number_format($startLeft, $this->parameter
['y_decimal_left'], $this->parameter
['decimal_point'], $this->parameter
['thousand_sep']);
1051 $this->calculated
['y_axis_left']['data'][$i] = $startLeft;
1052 $this->calculated
['y_axis_left']['text'][$i] = $value; // text is formatted raw data
1054 $size = $this->get_boundaryBox(
1055 array('points' => $axis_size,
1056 'font' => $axis_font,
1057 'angle' => $axis_angle,
1058 'colour' => $axis_colour,
1060 $this->calculated
['y_axis_left']['boundary_box'][$i] = $size;
1062 if ($size['height'] > $this->calculated
['y_axis_left']['boundary_box_max']['height'])
1063 $this->calculated
['y_axis_left']['boundary_box_max']['height'] = $size['height'];
1064 if ($size['width'] > $this->calculated
['y_axis_left']['boundary_box_max']['width'])
1065 $this->calculated
['y_axis_left']['boundary_box_max']['width'] = $size['width'];
1067 $startLeft +
= $stepLeft;
1069 $this->calculated
['boundary_box']['left'] +
= $this->calculated
['y_axis_left']['boundary_box_max']['width']
1070 +
$this->parameter
['y_inner_padding'];
1073 if ($this->parameter
['y_axis_text_right']) {
1074 for ($i = 0; $i < $this->parameter
['y_axis_gridlines']; $i++
) { // calculate y axis text sizes
1076 $value = number_format($start_right, $this->parameter
['y_decimal_right'], $this->parameter
['decimal_point'], $this->parameter
['thousand_sep']);
1077 $this->calculated
['y_axis_right']['data'][$i] = $start_right;
1078 $this->calculated
['y_axis_right']['text'][$i] = $value; // text is formatted raw data
1079 $size = $this->get_boundaryBox(
1080 array('points' => $axis_size,
1081 'font' => $axis_font,
1082 'angle' => $axis_angle,
1083 'colour' => $axis_colour,
1085 $this->calculated
['y_axis_right']['boundary_box'][$i] = $size;
1087 if ($size['height'] > $this->calculated
['y_axis_right']['boundary_box_max']['height'])
1088 $this->calculated
['y_axis_right']['boundary_box_max'] = $size;
1089 if ($size['width'] > $this->calculated
['y_axis_right']['boundary_box_max']['width'])
1090 $this->calculated
['y_axis_right']['boundary_box_max']['width'] = $size['width'];
1092 $start_right +
= $step_right;
1094 $this->calculated
['boundary_box']['right'] -= $this->calculated
['y_axis_right']['boundary_box_max']['width']
1095 +
$this->parameter
['y_inner_padding'];
1099 function init_x_axis() {
1100 $this->calculated
['x_axis'] = array(); // array to hold calculated values for x_axis.
1101 $this->calculated
['x_axis']['boundary_box_max'] = array('height' => 0, 'width' => 0);
1103 $axis_font = $this->parameter
['axis_font'];
1104 $axis_size = $this->parameter
['axis_size'];
1105 $axis_colour = $this->parameter
['axis_colour'];
1106 $axis_angle = $this->parameter
['x_axis_angle'];
1108 // check whether to treat x axis as numeric
1109 if ($this->parameter
['x_axis_gridlines'] == 'auto') { // auto means text based x_axis, not numeric...
1110 $this->calculated
['x_axis']['num_ticks'] = sizeof($this->x_data
);
1111 $data = $this->x_data
;
1112 for ($i=0; $i < $this->calculated
['x_axis']['num_ticks']; $i++
) {
1113 $value = array_shift($data); // grab value from begin of array
1114 $this->calculated
['x_axis']['data'][$i] = $value;
1115 $this->calculated
['x_axis']['text'][$i] = $value; // raw data and text are both the same in this case
1116 $size = $this->get_boundaryBox(
1117 array('points' => $axis_size,
1118 'font' => $axis_font,
1119 'angle' => $axis_angle,
1120 'colour' => $axis_colour,
1122 $this->calculated
['x_axis']['boundary_box'][$i] = $size;
1123 if ($size['height'] > $this->calculated
['x_axis']['boundary_box_max']['height'])
1124 $this->calculated
['x_axis']['boundary_box_max'] = $size;
1127 } else { // x axis is numeric so find max min values...
1128 $this->calculated
['x_axis']['num_ticks'] = $this->parameter
['x_axis_gridlines'];
1130 $min = $this->parameter
['x_min'];
1131 $max = $this->parameter
['x_max'];
1133 $data = $this->find_range($this->x_data
, $min, $max, $this->parameter
['x_resolution']);
1134 $min = $data['min'];
1135 $max = $data['max'];
1136 $this->calculated
['x_axis']['min'] = $min;
1137 $this->calculated
['x_axis']['max'] = $max;
1139 $step = ($max - $min) / ($this->calculated
['x_axis']['num_ticks'] - 1);
1142 for ($i = 0; $i < $this->calculated
['x_axis']['num_ticks']; $i++
) { // calculate x axis text sizes
1143 $value = number_format($start, $this->parameter
['xDecimal'], $this->parameter
['decimal_point'], $this->parameter
['thousand_sep']);
1144 $this->calculated
['x_axis']['data'][$i] = $start;
1145 $this->calculated
['x_axis']['text'][$i] = $value; // text is formatted raw data
1147 $size = $this->get_boundaryBox(
1148 array('points' => $axis_size,
1149 'font' => $axis_font,
1150 'angle' => $axis_angle,
1151 'colour' => $axis_colour,
1153 $this->calculated
['x_axis']['boundary_box'][$i] = $size;
1155 if ($size['height'] > $this->calculated
['x_axis']['boundary_box_max']['height'])
1156 $this->calculated
['x_axis']['boundary_box_max'] = $size;
1161 if ($this->parameter
['x_axis_text'])
1162 $this->calculated
['boundary_box']['bottom'] -= $this->calculated
['x_axis']['boundary_box_max']['height']
1163 +
$this->parameter
['x_inner_padding'];
1166 // find max and min values for a data array given the resolution.
1167 function find_range($data, $min, $max, $resolution) {
1168 if (sizeof($data) == 0 ) return array('min' => 0, 'max' => 0);
1169 foreach ($data as $key => $value) {
1170 if ($value=='none') continue;
1171 if ($value > $max) $max = $value;
1172 if ($value < $min) $min = $value;
1178 if ($max < 0) $factor = - pow(10, (floor(log10(abs($max))) +
$resolution) );
1179 else $factor = pow(10, (floor(log10(abs($max))) - $resolution) );
1181 if ($factor > 0.1) { // To avoid some wierd rounding errors (Moodle)
1182 $factor = round($factor * 1000.0) / 1000.0; // To avoid some wierd rounding errors (Moodle)
1183 } // To avoid some wierd rounding errors (Moodle)
1185 $max = $factor * @ceil
($max / $factor);
1186 $min = $factor * @floor
($min / $factor);
1188 //print "max=$max, min=$min<br />";
1190 return array('min' => $min, 'max' => $max);
1193 public function __construct() {
1194 if (func_num_args() == 2) {
1195 $this->parameter
['width'] = func_get_arg(0);
1196 $this->parameter
['height'] = func_get_arg(1);
1198 //$this->boundaryBox = array(
1199 $this->calculated
['boundary_box'] = array(
1202 'right' => $this->parameter
['width'] - 1,
1203 'bottom' => $this->parameter
['height'] - 1);
1205 $this->init_colours();
1207 //ImageColorTransparent($this->image, $this->colour['white']); // colour for transparency
1211 * Old syntax of class constructor. Deprecated in PHP7.
1213 * @deprecated since Moodle 3.1
1215 public function graph() {
1216 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
1217 self
::__construct();
1221 * Prepare label's text for GD output.
1223 * @param string $label string to be prepared.
1224 * @return string Reversed input string, if we are in RTL mode and has no numbers.
1225 * Otherwise, returns the string as is.
1227 private function prepare_label_text($label) {
1228 if (right_to_left() and !preg_match('/[0-9]/i', $label)) {
1229 return core_text
::strrev($label);
1235 function print_TTF($message) {
1236 $points = $message['points'];
1237 $angle = $message['angle'];
1238 // We have to manually reverse the label, since php GD cannot handle RTL characters properly in UTF8 strings.
1239 $text = $this->prepare_label_text($message['text']);
1240 $colour = $this->colour
[$message['colour']];
1241 $font = $this->parameter
['path_to_fonts'].$message['font'];
1243 $x = $message['boundary_box']['x'];
1244 $y = $message['boundary_box']['y'];
1245 $offsetX = $message['boundary_box']['offsetX'];
1246 $offsetY = $message['boundary_box']['offsetY'];
1247 $height = $message['boundary_box']['height'];
1248 $width = $message['boundary_box']['width'];
1249 $reference = $message['boundary_box']['reference'];
1251 switch ($reference) {
1254 $y +
= $height - $offsetY;
1259 $y +
= ($height / 2) - $offsetY;
1267 $y +
= $height - $offsetY;
1268 $x -= ($width / 2) - $offsetX;
1272 $y +
= $height - $offsetY;
1273 $x -= $width - $offsetX;
1275 case 'right-center':
1276 $y +
= ($height / 2) - $offsetY;
1277 $x -= $width - $offsetX;
1279 case 'right-bottom':
1281 $x -= $width - $offsetX;
1283 case 'bottom-center':
1285 $x -= ($width / 2) - $offsetX;
1292 // start of Moodle addition
1293 $text = core_text
::utf8_to_entities($text, true, true); //does not work with hex entities!
1294 // end of Moodle addition
1295 [$x, $y] = [(int) round($x), (int) round($y)];
1296 ImageTTFText($this->image
, $points, $angle, $x, $y, $colour, $font, $text);
1299 // move boundaryBox to coordinates specified
1300 function update_boundaryBox(&$boundaryBox, $coords) {
1301 $width = $boundaryBox['width'];
1302 $height = $boundaryBox['height'];
1305 $reference = $coords['reference'];
1306 switch ($reference) {
1310 $bottom = $y +
$height;
1312 $right = $x +
$width;
1315 $top = $y - ($height / 2);
1316 $bottom = $y +
($height / 2);
1318 $right = $x +
$width;
1321 $top = $y - $height;
1324 $right = $x +
$width;
1328 $bottom = $y +
$height;
1329 $left = $x - ($width / 2);
1330 $right = $x +
($width / 2);
1335 $bottom = $y +
$height;
1336 $left = $x - $width;
1339 case 'right-center':
1340 $top = $y - ($height / 2);
1341 $bottom = $y +
($height / 2);
1342 $left = $x - $width;
1345 case 'bottom=right':
1346 case 'right-bottom':
1347 $top = $y - $height;
1349 $left = $x - $width;
1360 $boundaryBox = array_merge($boundaryBox, array('top' => $top,
1361 'bottom' => $bottom,
1366 'reference' => $reference));
1369 function get_null_size() {
1370 return array('width' => 0,
1378 function get_boundaryBox($message) {
1379 $points = $message['points'];
1380 $angle = $message['angle'];
1381 $font = $this->parameter
['path_to_fonts'].$message['font'];
1382 $text = $message['text'];
1384 //print ('get_boundaryBox');
1385 //expandPre($message);
1388 $bounds = ImageTTFBBox($points, $angle, $font, "W");
1390 $fontHeight = abs($bounds[7]-$bounds[1]);
1391 } else if ($angle > 0) {
1392 $fontHeight = abs($bounds[1]-$bounds[7]);
1394 $fontHeight = abs($bounds[7]-$bounds[1]);
1397 // get boundary box and offsets for printing at an angle
1398 // start of Moodle addition
1399 $text = core_text
::utf8_to_entities($text, true, true); //gd does not work with hex entities!
1400 // end of Moodle addition
1401 $bounds = ImageTTFBBox($points, $angle, $font, $text);
1404 $width = abs($bounds[4]-$bounds[0]);
1405 $height = abs($bounds[3]-$bounds[7]);
1406 $offsetY = abs($bounds[3]-$bounds[1]);
1409 } else if ($angle > 0) {
1410 $width = abs($bounds[2]-$bounds[6]);
1411 $height = abs($bounds[1]-$bounds[5]);
1413 $offsetX = abs($bounds[0]-$bounds[6]);
1416 $width = abs($bounds[4]-$bounds[6]);
1417 $height = abs($bounds[7]-$bounds[1]);
1418 $offsetY = $bounds[1];
1423 return array('width' => $width,
1424 'height' => $height,
1425 'offsetX' => $offsetX,
1426 'offsetY' => $offsetY,
1427 //'fontHeight' => $fontHeight
1431 function draw_rectangle($border, $colour, $type) {
1432 $colour = $this->colour
[$colour];
1434 case 'fill': // fill the rectangle
1435 ImageFilledRectangle($this->image
, $border['left'], $border['top'], $border['right'], $border['bottom'], $colour);
1437 case 'box': // all sides
1438 ImageRectangle($this->image
, $border['left'], $border['top'], $border['right'], $border['bottom'], $colour);
1440 case 'axis': // bottom x axis and left y axis
1441 ImageLine($this->image
, $border['left'], $border['top'], $border['left'], $border['bottom'], $colour);
1442 ImageLine($this->image
, $border['left'], $border['bottom'], $border['right'], $border['bottom'], $colour);
1444 case 'y': // left y axis only
1446 ImageLine($this->image
, $border['left'], $border['top'], $border['left'], $border['bottom'], $colour);
1448 case 'y-right': // right y axis only
1449 ImageLine($this->image
, $border['right'], $border['top'], $border['right'], $border['bottom'], $colour);
1451 case 'x': // bottom x axis only
1452 ImageLine($this->image
, $border['left'], $border['bottom'], $border['right'], $border['bottom'], $colour);
1454 case 'u': // u shaped. bottom x axis and both left and right y axis.
1455 ImageLine($this->image
, $border['left'], $border['top'], $border['left'], $border['bottom'], $colour);
1456 ImageLine($this->image
, $border['right'], $border['top'], $border['right'], $border['bottom'], $colour);
1457 ImageLine($this->image
, $border['left'], $border['bottom'], $border['right'], $border['bottom'], $colour);
1463 function init_colours() {
1464 $this->image
= ImageCreate($this->parameter
['width'], $this->parameter
['height']);
1466 $this->colour
['white'] = ImageColorAllocate ($this->image
, 0xFF, 0xFF, 0xFF); // first colour is background colour.
1467 $this->colour
['black'] = ImageColorAllocate ($this->image
, 0x00, 0x00, 0x00);
1468 $this->colour
['maroon'] = ImageColorAllocate ($this->image
, 0x80, 0x00, 0x00);
1469 $this->colour
['green'] = ImageColorAllocate ($this->image
, 0x00, 0x80, 0x00);
1470 $this->colour
['ltgreen'] = ImageColorAllocate ($this->image
, 0x52, 0xF1, 0x7F);
1471 $this->colour
['ltltgreen']= ImageColorAllocate ($this->image
, 0x99, 0xFF, 0x99);
1472 $this->colour
['olive'] = ImageColorAllocate ($this->image
, 0x80, 0x80, 0x00);
1473 $this->colour
['navy'] = ImageColorAllocate ($this->image
, 0x00, 0x00, 0x80);
1474 $this->colour
['purple'] = ImageColorAllocate ($this->image
, 0x80, 0x00, 0x80);
1475 $this->colour
['gray'] = ImageColorAllocate ($this->image
, 0x80, 0x80, 0x80);
1476 $this->colour
['red'] = ImageColorAllocate ($this->image
, 0xFF, 0x00, 0x00);
1477 $this->colour
['ltred'] = ImageColorAllocate ($this->image
, 0xFF, 0x99, 0x99);
1478 $this->colour
['ltltred'] = ImageColorAllocate ($this->image
, 0xFF, 0xCC, 0xCC);
1479 $this->colour
['orange'] = ImageColorAllocate ($this->image
, 0xFF, 0x66, 0x00);
1480 $this->colour
['ltorange'] = ImageColorAllocate ($this->image
, 0xFF, 0x99, 0x66);
1481 $this->colour
['ltltorange'] = ImageColorAllocate ($this->image
, 0xFF, 0xcc, 0x99);
1482 $this->colour
['lime'] = ImageColorAllocate ($this->image
, 0x00, 0xFF, 0x00);
1483 $this->colour
['yellow'] = ImageColorAllocate ($this->image
, 0xFF, 0xFF, 0x00);
1484 $this->colour
['blue'] = ImageColorAllocate ($this->image
, 0x00, 0x00, 0xFF);
1485 $this->colour
['ltblue'] = ImageColorAllocate ($this->image
, 0x00, 0xCC, 0xFF);
1486 $this->colour
['ltltblue'] = ImageColorAllocate ($this->image
, 0x99, 0xFF, 0xFF);
1487 $this->colour
['fuchsia'] = ImageColorAllocate ($this->image
, 0xFF, 0x00, 0xFF);
1488 $this->colour
['aqua'] = ImageColorAllocate ($this->image
, 0x00, 0xFF, 0xFF);
1489 //$this->colour['white'] = ImageColorAllocate ($this->image, 0xFF, 0xFF, 0xFF);
1491 $this->colour
['grayF0'] = ImageColorAllocate ($this->image
, 0xF0, 0xF0, 0xF0);
1492 $this->colour
['grayEE'] = ImageColorAllocate ($this->image
, 0xEE, 0xEE, 0xEE);
1493 $this->colour
['grayDD'] = ImageColorAllocate ($this->image
, 0xDD, 0xDD, 0xDD);
1494 $this->colour
['grayCC'] = ImageColorAllocate ($this->image
, 0xCC, 0xCC, 0xCC);
1495 $this->colour
['gray33'] = ImageColorAllocate ($this->image
, 0x33, 0x33, 0x33);
1496 $this->colour
['gray66'] = ImageColorAllocate ($this->image
, 0x66, 0x66, 0x66);
1497 $this->colour
['gray99'] = ImageColorAllocate ($this->image
, 0x99, 0x99, 0x99);
1499 $this->colour
['none'] = 'none';
1504 if ($this->debug
) { // for debugging purposes.
1505 //expandPre($this->graph);
1506 //expandPre($this->y_data);
1507 //expandPre($this->x_data);
1508 //expandPre($this->parameter);
1511 $expiresSeconds = $this->parameter
['seconds_to_live'];
1512 $expiresHours = $this->parameter
['hours_to_live'];
1514 if ($expiresHours ||
$expiresSeconds) {
1515 $now = mktime (date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));
1516 $expires = mktime (date("H")+
$expiresHours,date("i"),date("s")+
$expiresSeconds,date("m"),date("d"),date("Y"));
1517 $expiresGMT = gmdate('D, d M Y H:i:s', $expires).' GMT';
1518 $lastModifiedGMT = gmdate('D, d M Y H:i:s', $now).' GMT';
1520 Header('Last-modified: '.$lastModifiedGMT);
1521 Header('Expires: '.$expiresGMT);
1524 if ($this->parameter
['file_name'] == 'none') {
1525 switch ($this->parameter
['output_format']) {
1527 Header("Content-type: image/gif"); // GIF??. switch to PNG guys!!
1528 ImageGIF($this->image
);
1531 Header("Content-type: image/jpeg"); // JPEG for line art??. included for completeness.
1532 ImageJPEG($this->image
);
1535 Header("Content-type: image/png"); // preferred output format
1536 ImagePNG($this->image
);
1540 switch ($this->parameter
['output_format']) {
1542 ImageGIF($this->image
, $this->parameter
['file_name'].'.gif');
1545 ImageJPEG($this->image
, $this->parameter
['file_name'].'.jpg');
1548 ImagePNG($this->image
, $this->parameter
['file_name'].'.png');
1553 ImageDestroy($this->image
);
1555 } // function output
1557 function init_variable(&$variable, $value, $default) {
1558 if (!empty($value)) $variable = $value;
1559 else if (isset($default)) $variable = $default;
1560 else unset($variable);
1563 // plot a point. options include square, circle, diamond, triangle, and dot. offset is used for drawing shadows.
1564 // for diamonds and triangles the size should be an even number to get nice look. if odd the points are crooked.
1565 function plot($x, $y, $type, $size, $colour, $offset) {
1566 //print("drawing point of type: $type, at offset: $offset");
1568 $v = $this->calculated
['inner_border']['bottom'] - $y +
$offset;
1570 [$u, $v, $half] = [(int) round($u), (int) round($v), (int) round($half)];
1573 ImageFilledRectangle($this->image
, $u-$half, $v-$half, $u+
$half, $v+
$half, $this->colour
[$colour]);
1576 ImageRectangle($this->image
, $u-$half, $v-$half, $u+
$half, $v+
$half, $this->colour
[$colour]);
1579 ImageArc($this->image
, $u, $v, $size, $size, 0, 360, $this->colour
[$colour]);
1580 ImageFillToBorder($this->image
, $u, $v, $this->colour
[$colour], $this->colour
[$colour]);
1583 ImageArc($this->image
, $u, $v, $size, $size, 0, 360, $this->colour
[$colour]);
1586 if (version_compare(PHP_VERSION
, '8.0.0', '>=')) {
1587 ImageFilledPolygon($this->image
, array($u, $v - $half, $u +
$half, $v, $u, $v +
$half, $u - $half, $v), $this->colour
[$colour]);
1589 ImageFilledPolygon($this->image
, array($u, $v - $half, $u +
$half, $v, $u, $v +
$half, $u - $half, $v), 4, $this->colour
[$colour]);
1592 case 'diamond-open':
1593 if (version_compare(PHP_VERSION
, '8.0.0', '>=')) {
1594 ImagePolygon($this->image
, array($u, $v - $half, $u +
$half, $v, $u, $v +
$half, $u - $half, $v), $this->colour
[$colour]);
1596 ImagePolygon($this->image
, array($u, $v - $half, $u +
$half, $v, $u, $v +
$half, $u - $half, $v), 4, $this->colour
[$colour]);
1600 if (version_compare(PHP_VERSION
, '8.0.0', '>=')) {
1601 ImageFilledPolygon($this->image
, array($u, $v - $half, $u +
$half, $v +
$half, $u - $half, $v +
$half), $this->colour
[$colour]);
1603 ImageFilledPolygon($this->image
, array($u, $v - $half, $u +
$half, $v +
$half, $u - $half, $v +
$half), 3, $this->colour
[$colour]);
1606 case 'triangle-open':
1607 if (version_compare(PHP_VERSION
, '8.0.0', '>=')) {
1608 ImagePolygon($this->image
, array($u, $v - $half, $u +
$half, $v +
$half, $u - $half, $v +
$half), $this->colour
[$colour]);
1610 ImagePolygon($this->image
, array($u, $v - $half, $u +
$half, $v +
$half, $u - $half, $v +
$half), 3, $this->colour
[$colour]);
1614 ImageSetPixel($this->image
, $u, $v, $this->colour
[$colour]);
1619 function bar($x, $y, $type, $size, $colour, $offset, $index, $yoffset) {
1620 $index_offset = $this->calculated
['bar_offset_index'][$index];
1624 $bar_offsetx = $this->calculated
['bar_offset_x'][$index_offset];
1626 //$this->dbug("drawing bar at offset = $offset : index = $index: bar_offsetx = $bar_offsetx");
1628 $span = ($this->calculated
['bar_width'] * $size) / 2;
1629 $x_left = $x +
$bar_offsetx - $span;
1630 $x_right = $x +
$bar_offsetx +
$span;
1632 if ($this->parameter
['zero_axis'] != 'none') {
1633 $zero = $this->calculated
['zero_axis'];
1634 if ($this->parameter
['shadow_below_axis'] ) $zero +
= $offset;
1635 $u_left = (int) round($x_left +
$offset);
1636 $u_right = (int) round($x_right +
$offset - 1);
1637 $v = $this->calculated
['boundary_box']['bottom'] - $y +
$offset;
1644 $bottom = $zero - 1;
1647 [$top, $bottom] = [(int) round($top), (int) round($bottom)];
1652 ImageRectangle($this->image
, $u_left, $bottom, $u_right, $bottom, $this->colour
[$colour]);
1654 ImageRectangle($this->image
, $u_left, $top, $u_right, $top, $this->colour
[$colour]);
1655 ImageRectangle($this->image
, $u_left, $top, $u_left, $bottom, $this->colour
[$colour]);
1656 ImageRectangle($this->image
, $u_right, $top, $u_right, $bottom, $this->colour
[$colour]);
1659 ImageFilledRectangle($this->image
, $u_left, $top, $u_right, $bottom, $this->colour
[$colour]);
1665 $bottom = $this->calculated
['boundary_box']['bottom'];
1666 if ($this->parameter
['shadow_below_axis'] ) $bottom +
= $offset;
1667 if ($this->parameter
['inner_border'] != 'none') $bottom -= 1; // 1 pixel above bottom if border is to be drawn.
1668 $u_left = (int) round($x_left +
$offset);
1669 $u_right = (int) round($x_right +
$offset - 1);
1670 $v = $this->calculated
['boundary_box']['bottom'] - $y +
$offset;
1672 // Moodle addition, plus the function parameter yoffset
1673 if ($yoffset) { // Moodle
1674 $yoffset = $yoffset - round(($bottom - $v) / 2.0); // Moodle
1675 $bottom -= $yoffset; // Moodle
1676 $v -= $yoffset; // Moodle
1679 [$v, $bottom] = [(int) round($v), (int) round($bottom)];
1683 ImageRectangle($this->image
, $u_left, $v, $u_right, $bottom, $this->colour
[$colour]);
1686 ImageFilledRectangle($this->image
, $u_left, $v, $u_right, $bottom, $this->colour
[$colour]);
1692 function area($x_start, $y_start, $x_end, $y_end, $type, $colour, $offset) {
1693 //dbug("drawing area type: $type, at offset: $offset");
1694 if ($this->parameter
['zero_axis'] != 'none') {
1695 $bottom = $this->calculated
['boundary_box']['bottom'];
1696 $zero = $this->calculated
['zero_axis'];
1697 if ($this->parameter
['shadow_below_axis'] ) $zero +
= $offset;
1698 $u_start = $x_start +
$offset;
1699 $u_end = $x_end +
$offset;
1700 $v_start = $bottom - $y_start +
$offset;
1701 $v_end = $bottom - $y_end +
$offset;
1704 // draw it this way 'cos the FilledPolygon routine seems a bit buggy.
1705 if (version_compare(PHP_VERSION
, '8.0.0', '>=')) {
1706 ImageFilledPolygon($this->image
, array($u_start, $v_start, $u_end, $v_end, $u_end, $zero, $u_start, $zero), $this->colour
[$colour]);
1707 ImagePolygon($this->image
, array($u_start, $v_start, $u_end, $v_end, $u_end, $zero, $u_start, $zero), $this->colour
[$colour]);
1709 ImageFilledPolygon($this->image
, array($u_start, $v_start, $u_end, $v_end, $u_end, $zero, $u_start, $zero), 4, $this->colour
[$colour]);
1710 ImagePolygon($this->image
, array($u_start, $v_start, $u_end, $v_end, $u_end, $zero, $u_start, $zero), 4, $this->colour
[$colour]);
1714 ImageLine($this->image
, $u_start, $v_start, $u_end, $v_end, $this->colour
[$colour]);
1715 ImageLine($this->image
, $u_start, $v_start, $u_start, $zero, $this->colour
[$colour]);
1716 ImageLine($this->image
, $u_end, $v_end, $u_end, $zero, $this->colour
[$colour]);
1720 $bottom = $this->calculated
['boundary_box']['bottom'];
1721 $u_start = $x_start +
$offset;
1722 $u_end = $x_end +
$offset;
1723 $v_start = $bottom - $y_start +
$offset;
1724 $v_end = $bottom - $y_end +
$offset;
1726 if ($this->parameter
['shadow_below_axis'] ) $bottom +
= $offset;
1727 if ($this->parameter
['inner_border'] != 'none') $bottom -= 1; // 1 pixel above bottom if border is to be drawn.
1730 if (version_compare(PHP_VERSION
, '8.0.0', '>=')) {
1731 ImageFilledPolygon($this->image
, array($u_start, $v_start, $u_end, $v_end, $u_end, $bottom, $u_start, $bottom), $this->colour
[$colour]);
1733 ImageFilledPolygon($this->image
, array($u_start, $v_start, $u_end, $v_end, $u_end, $bottom, $u_start, $bottom), 4, $this->colour
[$colour]);
1737 if (version_compare(PHP_VERSION
, '8.0.0', '>=')) {
1738 ImagePolygon($this->image
, array($u_start, $v_start, $u_end, $v_end, $u_end, $bottom, $u_start, $bottom), $this->colour
[$colour]);
1740 ImagePolygon($this->image
, array($u_start, $v_start, $u_end, $v_end, $u_end, $bottom, $u_start, $bottom), 4, $this->colour
[$colour]);
1747 function line($x_start, $y_start, $x_end, $y_end, $type, $brush_type, $brush_size, $colour, $offset) {
1748 //dbug("drawing line of type: $type, at offset: $offset");
1749 $u_start = (int) round($x_start +
$offset);
1750 $v_start = (int) round($this->calculated
['boundary_box']['bottom'] - $y_start +
$offset);
1751 $u_end = (int) round($x_end +
$offset);
1752 $v_end = (int) round($this->calculated
['boundary_box']['bottom'] - $y_end +
$offset);
1756 $this->draw_brush_line($u_start, $v_start, $u_end, $v_end, $brush_size, $brush_type, $colour);
1759 ImageLine($this->image
, $u_start, $v_start, $u_end, $v_end, $this->colour
[$colour]);
1762 $this->image_dashed_line($this->image
, $u_start, $v_start, $u_end, $v_end, $this->colour
[$colour]); // Moodle
1767 // function to draw line. would prefer to use gdBrush but this is not supported yet.
1768 function draw_brush_line($x0, $y0, $x1, $y1, $size, $type, $colour) {
1769 //$this->dbug("line: $x0, $y0, $x1, $y1");
1773 $watchdog = 1024; // precaution to prevent infinite loops.
1775 $this->draw_brush($x0, $y0, $size, $type, $colour);
1776 if (abs($dx) > abs($dy)) { // slope < 1
1777 //$this->dbug("slope < 1");
1778 $m = $dy / $dx; // compute slope
1780 $dx = ($dx < 0) ?
-1 : 1;
1782 while (round($x0) != round($x1)) {
1783 if (!$watchdog--) break;
1784 $x0 +
= $dx; // step to next x value
1785 $t +
= $m; // add slope to y value
1787 //$this->dbug("x0=$x0, x1=$x1, y=$y watchdog=$watchdog");
1788 $this->draw_brush($x0, $y, $size, $type, $colour);
1791 } else { // slope >= 1
1792 //$this->dbug("slope >= 1");
1793 $m = $dx / $dy; // compute slope
1795 $dy = ($dy < 0) ?
-1 : 1;
1797 while (round($y0) != round($y1)) {
1798 if (!$watchdog--) break;
1799 $y0 +
= $dy; // step to next y value
1800 $t +
= $m; // add slope to x value
1802 //$this->dbug("x=$x, y0=$y0, y1=$y1 watchdog=$watchdog");
1803 $this->draw_brush($x, $y0, $size, $type, $colour);
1809 function draw_brush($x, $y, $size, $type, $colour) {
1812 $half = round($size / 2);
1815 ImageArc($this->image
, $x, $y, $size, $size, 0, 360, $this->colour
[$colour]);
1816 ImageFillToBorder($this->image
, $x, $y, $this->colour
[$colour], $this->colour
[$colour]);
1819 ImageFilledRectangle($this->image
, $x-$half, $y-$half, $x+
$half, $y+
$half, $this->colour
[$colour]);
1822 ImageFilledRectangle($this->image
, $x, $y-$half, $x+
1, $y+
$half, $this->colour
[$colour]);
1825 ImageFilledRectangle($this->image
, $x-$half, $y, $x+
$half, $y+
1, $this->colour
[$colour]);
1828 if (version_compare(PHP_VERSION
, '8.0.0', '>=')) {
1829 ImageFilledPolygon($this->image
, array(
1830 $x +
$half, $y - $half,
1831 $x +
$half +
1, $y - $half,
1832 $x - $half +
1, $y +
$half,
1833 $x - $half, $y +
$half
1834 ), $this->colour
[$colour]);
1836 ImageFilledPolygon($this->image
, array(
1837 $x +
$half, $y - $half,
1838 $x +
$half +
1, $y - $half,
1839 $x - $half +
1, $y +
$half,
1840 $x - $half, $y +
$half
1841 ), 4, $this->colour
[$colour]);
1845 if (version_compare(PHP_VERSION
, '8.0.0', '>=')) {
1846 ImageFilledPolygon($this->image
, array(
1847 $x - $half, $y - $half,
1848 $x - $half +
1, $y - $half,
1849 $x +
$half +
1, $y +
$half,
1850 $x +
$half, $y +
$half
1851 ), $this->colour
[$colour]);
1853 ImageFilledPolygon($this->image
, array(
1854 $x - $half, $y-$half,
1855 $x - $half +
1, $y - $half,
1856 $x +
$half +
1, $y +
$half,
1857 $x +
$half, $y +
$half
1858 ), 4, $this->colour
[$colour]);
1862 @eval
($type); // user can create own brush script.
1869 * A replacement for deprecated ImageDashedLine function.
1871 * @param resource|GdImage $image
1872 * @param int $x1 — x-coordinate for first point.
1873 * @param int $y1 — y-coordinate for first point.
1874 * @param int $x2 — x-coordinate for second point.
1875 * @param int $y2 — y-coordinate for second point.
1879 private function image_dashed_line($image, $x1, $y1, $x2, $y2, $colour): void
{
1880 // Create a dashed style.
1886 IMG_COLOR_TRANSPARENT
,
1887 IMG_COLOR_TRANSPARENT
,
1888 IMG_COLOR_TRANSPARENT
,
1889 IMG_COLOR_TRANSPARENT
1891 imagesetstyle($image, $style);
1893 // Apply the dashed style.
1894 imageline($image, $x1, $y1, $x2, $y2, IMG_COLOR_STYLED
);