diff --git a/lib/graphlib.php b/lib/graphlib.php index c06db479b79fe..0eebb05728a42 100644 --- a/lib/graphlib.php +++ b/lib/graphlib.php @@ -537,7 +537,10 @@ function draw_zero_axis_left() { $colour = $this->parameter['zero_axis']; if ($colour == 'none') return; // draw zero axis on left hand side - $this->calculated['zero_axis'] = round($this->calculated['boundary_box']['top'] + ($this->calculated['y_axis_left']['max'] * $this->calculated['y_axis_left']['factor'])); + $this->calculated['zero_axis'] = (int) round( + $this->calculated['boundary_box']['top'] + + ($this->calculated['y_axis_left']['max'] * $this->calculated['y_axis_left']['factor']) + ); ImageLine($this->image, $this->calculated['boundary_box']['left'], $this->calculated['zero_axis'], $this->calculated['boundary_box']['right'], $this->calculated['zero_axis'], $this->colour[$colour]); } @@ -545,7 +548,10 @@ function draw_zero_axis_right() { $colour = $this->parameter['zero_axis']; if ($colour == 'none') return; // draw zero axis on right hand side - $this->calculated['zero_axis'] = round($this->calculated['boundary_box']['top'] + ($this->calculated['y_axis_right']['max'] * $this->calculated['y_axis_right']['factor'])); + $this->calculated['zero_axis'] = (int) round( + $this->calculated['boundary_box']['top'] + + ($this->calculated['y_axis_right']['max'] * $this->calculated['y_axis_right']['factor']) + ); ImageLine($this->image, $this->calculated['boundary_box']['left'], $this->calculated['zero_axis'], $this->calculated['boundary_box']['right'], $this->calculated['zero_axis'], $this->colour[$colour]); } @@ -691,10 +697,10 @@ function draw_y_axis() { if (!$this->calculated['y_axis_left']['has_data'] && $yGrid != 'none') { // draw grid if not drawn already (above) switch ($yGrid) { case 'line': - ImageLine($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour); + ImageLine($this->image, (int) round($gridLeft), (int) round($tickY), (int) round($gridRight), (int) round($tickY), $gridColour); break; case 'dash': - $this->image_dashed_line($this->image, round($gridLeft), round($tickY), round($gridRight), round($tickY), $gridColour); // Moodle + $this->image_dashed_line($this->image, (int) round($gridLeft), (int) round($tickY), (int) round($gridRight), (int) round($tickY), $gridColour); // Moodle break; } } @@ -702,7 +708,7 @@ function draw_y_axis() { if ($this->parameter['y_axis_text_right'] && !($set % $this->parameter['y_axis_text_right'])) { // test if tick should be displayed // draw tick if ($tickColour != 'none') - ImageLine($this->image, round($tickLeft), round($tickY), round($tickRight), round($tickY), $tickColour); + ImageLine($this->image, (int) round($tickLeft), (int) round($tickY), (int) round($tickRight), (int) round($tickY), $tickColour); // draw axis text... $coords = array('x' => $textLeft, 'y' => $tickY, 'reference' => $reference); @@ -767,12 +773,12 @@ function init_data() { if (isset($this->y_format[$set]['y_axis']) && $this->y_format[$set]['y_axis'] == 'right') { $this->calculated['y_plot'][$set][$index] = - round(($this->y_data[$set][$index] - $this->calculated['y_axis_right']['min']) + (int) round(($this->y_data[$set][$index] - $this->calculated['y_axis_right']['min']) * $this->calculated['y_axis_right']['factor']); } else { //print "$set $index
"; $this->calculated['y_plot'][$set][$index] = - round(($this->y_data[$set][$index] - $this->calculated['y_axis_left']['min']) + (int) round(($this->y_data[$set][$index] - $this->calculated['y_axis_left']['min']) * $this->calculated['y_axis_left']['factor']); } @@ -812,7 +818,7 @@ function init_x_ticks() { // x tick value $this->calculated['x_axis']['tick_x'][$set] = $tickX; // if num ticks is auto then x plot value is same as x tick - if ($this->parameter['x_axis_gridlines'] == 'auto') $this->calculated['x_plot'][$set] = round($tickX); + if ($this->parameter['x_axis_gridlines'] == 'auto') $this->calculated['x_plot'][$set] = (int) round($tickX); //print $this->calculated['x_plot'][$set].'
'; $tickX += $xStep; } @@ -1179,7 +1185,7 @@ function find_range($data, $min, $max, $resolution) { else $factor = pow(10, (floor(log10(abs($max))) - $resolution) ); } if ($factor > 0.1) { // To avoid some wierd rounding errors (Moodle) - $factor = round($factor * 1000.0) / 1000.0; // To avoid some wierd rounding errors (Moodle) + $factor = (int) round($factor * 1000.0) / 1000.0; // To avoid some wierd rounding errors (Moodle) } // To avoid some wierd rounding errors (Moodle) $max = $factor * @ceil($max / $factor); @@ -1671,7 +1677,7 @@ function bar($x, $y, $type, $size, $colour, $offset, $index, $yoffset) { // Moodle addition, plus the function parameter yoffset if ($yoffset) { // Moodle - $yoffset = $yoffset - round(($bottom - $v) / 2.0); // Moodle + $yoffset = $yoffset - (int) round(($bottom - $v) / 2.0); // Moodle $bottom -= $yoffset; // Moodle $v -= $yoffset; // Moodle } // Moodle @@ -1779,11 +1785,11 @@ function draw_brush_line($x0, $y0, $x1, $y1, $size, $type, $colour) { $t += $y0; $dx = ($dx < 0) ? -1 : 1; $m *= $dx; - while (round($x0) != round($x1)) { + while ((int) round($x0) != (int) round($x1)) { if (!$watchdog--) break; $x0 += $dx; // step to next x value $t += $m; // add slope to y value - $y = round($t); + $y = (int) round($t); //$this->dbug("x0=$x0, x1=$x1, y=$y watchdog=$watchdog"); $this->draw_brush($x0, $y, $size, $type, $colour); @@ -1794,11 +1800,11 @@ function draw_brush_line($x0, $y0, $x1, $y1, $size, $type, $colour) { $t += $x0; $dy = ($dy < 0) ? -1 : 1; $m *= $dy; - while (round($y0) != round($y1)) { + while ((int) round($y0) != (int) round($y1)) { if (!$watchdog--) break; $y0 += $dy; // step to next y value $t += $m; // add slope to x value - $x = round($t); + $x = (int) round($t); //$this->dbug("x=$x, y0=$y0, y1=$y1 watchdog=$watchdog"); $this->draw_brush($x, $y0, $size, $type, $colour); @@ -1807,9 +1813,9 @@ function draw_brush_line($x0, $y0, $x1, $y1, $size, $type, $colour) { } function draw_brush($x, $y, $size, $type, $colour) { - $x = round($x); - $y = round($y); - $half = round($size / 2); + $x = (int) round($x); + $y = (int) round($y); + $half = (int) round($size / 2); switch ($type) { case 'circle': ImageArc($this->image, $x, $y, $size, $size, 0, 360, $this->colour[$colour]); diff --git a/lib/tests/graphlib_test.php b/lib/tests/graphlib_test.php index a8838cebc64d1..f2afe31acb7ea 100644 --- a/lib/tests/graphlib_test.php +++ b/lib/tests/graphlib_test.php @@ -53,17 +53,35 @@ public function create_data(): array { -1, -1, -1, -1, -1, -1 ], 'strpreferred' => 'Preferred', + 'strimagine' => 'Imagine', + 'buckets3' => [ + 1, + 2.75, + 0.5, + 3.5, + 1.25, + 3, + ], 'stdev1' => [ 0.82915619758885, 1.1180339887499, 1.1180339887499, 1.1180339887499, 1.1180339887499, 1.1180339887499 ], 'stdev2' => [ 0, 0, 0, 0, 0, 0 ], + 'stdev3' => [ + 0.92915619758885, + 2.1180339887499, + 2.1180339887499, + 2.1180339887499, + 2.1180339887499, + 2.1180339887499, + ], 'options' => [ 'Almost never', 'Seldom', 'Sometimes', 'Often', 'Almost always' ], 'maxbuckets1' => 2.5, - 'maxbuckets2' => -1 + 'maxbuckets2' => -1, + 'maxbuckets3' => 3.5, ] ] ]; @@ -81,6 +99,7 @@ public function test_graphlib($mock) { $graph = new \graph(300, 200); ob_start(); $graph->parameter['title'] = strip_tags(format_string($mock['survey_name'], true)); + $graph->parameter['zero_axis'] = 'black'; $graph->x_data = $mock['names']; $graph->y_data['answers1'] = $mock['buckets1']; $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square', @@ -88,24 +107,53 @@ public function test_graphlib($mock) { $graph->y_data['answers2'] = $mock['buckets2']; $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square', 'shadow_offset' => 4, 'legend' => $mock['strpreferred']); + $graph->y_data['answers3'] = $mock['buckets3']; + $graph->y_format['answers3'] = [ + 'colour' => 'ltred', + 'line' => 'brush', + 'point' => 'square', + 'shadow_offset' => 4, + 'legend' => $mock['stractual'], + ]; $graph->y_data['stdev1'] = $mock['stdev1']; $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill', 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3); $graph->y_data['stdev2'] = $mock['stdev2']; $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill', 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2); + $graph->y_data['stdev3'] = $mock['stdev3']; + $graph->y_format['stdev3'] = [ + 'colour' => 'ltred', + 'bar' => 'fill', + 'shadow_offset' => '4', + 'legend' => 'none', + 'bar_size' => 0.2, + ]; $graph->offset_relation['stdev1'] = 'answers1'; $graph->offset_relation['stdev2'] = 'answers2'; + $graph->offset_relation['stdev3'] = 'answers3'; $graph->parameter['legend'] = 'outside-top'; $graph->parameter['legend_border'] = 'black'; $graph->parameter['legend_offset'] = 4; $graph->y_tick_labels = $mock['options']; - if (($mock['maxbuckets1'] > 0.0) && ($mock['maxbuckets2'] > 0.0)) { - $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2'); + if (($mock['maxbuckets1'] > 0.0) && ($mock['maxbuckets2'] > 0.0) && ($mock['maxbuckets3'] > 0.0)) { + $graph->y_order = [ + 'stdev1', + 'answers1', + 'stdev2', + 'answers2', + 'stdev3', + 'answers3', + ]; } else if ($mock['maxbuckets1'] > 0.0) { $graph->y_order = array('stdev1', 'answers1'); - } else { + } else if ($mock['maxbuckets2'] > 0.0) { $graph->y_order = array('stdev2', 'answers2'); + } else { + $graph->y_order = [ + 'stdev3', + 'answers3', + ]; } $graph->parameter['y_max_left'] = 4; $graph->parameter['y_axis_gridlines'] = 5;