MDL-56989 boost: don't double-escape page titles
[moodle.git] / lib / classes / chart_base.php
blob74ff7b365da1af88d958510f2a633cebd22182ff
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Chart base.
20 * @package core
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core;
26 defined('MOODLE_INTERNAL') || die();
28 use coding_exception;
29 use JsonSerializable;
30 use renderable;
32 /**
33 * Chart base class.
35 * @package core
36 * @copyright 2016 Frédéric Massart - FMCorz.net
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class chart_base implements JsonSerializable, renderable {
41 /** @var chart_series[] The series constituting this chart. */
42 protected $series = [];
43 /** @var string[] The labels for the X axis when categorised. */
44 protected $labels = [];
45 /** @var string The title of the chart. */
46 protected $title = null;
47 /** @var chart_axis[] The X axes. */
48 protected $xaxes = [];
49 /** @var chart_axis[] The Y axes. */
50 protected $yaxes = [];
52 /**
53 * Constructor.
55 * Must not take any argument.
57 * Most of the time you do not want to extend this, rather extend the
58 * method {@link self::set_defaults} to set the defaults on instantiation.
60 public function __construct() {
61 $this->set_defaults();
64 /**
65 * Add a series to the chart.
67 * @param chart_series $serie The serie.
69 public function add_series(chart_series $serie) {
70 $this->series[] = $serie;
73 /**
74 * Serialize the object.
76 * @return array
78 public function jsonSerialize() { // @codingStandardsIgnoreLine (CONTRIB-6469).
79 return [
80 'type' => $this->get_type(),
81 'series' => $this->series,
82 'labels' => $this->labels,
83 'title' => $this->title,
84 'axes' => [
85 'x' => $this->xaxes,
86 'y' => $this->yaxes,
91 /**
92 * Get an axis.
94 * @param string $type Accepts values 'x' or 'y'.
95 * @param int $index The index of this axis.
96 * @param bool $createifnotexists Whether to create the axis if not found.
97 * @return chart_axis
99 private function get_axis($type, $index, $createifnotexists) {
100 $isx = $type === 'x';
101 if ($isx) {
102 $axis = isset($this->xaxes[$index]) ? $this->xaxes[$index] : null;
103 } else {
104 $axis = isset($this->yaxes[$index]) ? $this->yaxes[$index] : null;
107 if ($axis === null) {
108 if (!$createifnotexists) {
109 throw new coding_exception('Unknown axis.');
112 $axis = new chart_axis();
113 if ($isx) {
114 $this->set_xaxis($axis, $index);
115 } else {
116 $this->set_yaxis($axis, $index);
120 return $axis;
124 * Get the labels of the X axis.
126 * @return string[]
128 public function get_labels() {
129 return $this->labels;
133 * Get the series.
135 * @return chart_series[]
137 public function get_series() {
138 return $this->series;
142 * Get the title.
144 * @return string
146 public function get_title() {
147 return $this->title;
151 * Get the chart type.
153 * @return string
155 public function get_type() {
156 $classname = get_class($this);
157 return substr($classname, strpos($classname, '_') + 1);
161 * Get the X axes.
163 * @return chart_axis[]
165 public function get_xaxes() {
166 return $this->xaxes;
170 * Get an X axis.
172 * @param int $index The index of the axis.
173 * @param bool $createifnotexists When true, create an instance of the axis if none exist at this index yet.
174 * @return chart_axis
176 public function get_xaxis($index = 0, $createifnotexists = false) {
177 return $this->get_axis('x', $index, $createifnotexists);
181 * Get the Y axes.
183 * @return chart_axis[]
185 public function get_yaxes() {
186 return $this->yaxes;
190 * Get a Y axis.
192 * @param int $index The index of the axis.
193 * @param bool $createifnotexists When true, create an instance of the axis if none exist at this index yet.
194 * @return chart_axis
196 public function get_yaxis($index = 0, $createifnotexists = false) {
197 return $this->get_axis('y', $index, $createifnotexists);
201 * Set the defaults for this chart type.
203 * Child classes can extend this to set default values on instantiation.
205 * In general the constructor could be used, but this method is here to
206 * emphasize and self-document the default values set by the chart type.
208 * @return void
210 protected function set_defaults() {
214 * Set the chart labels.
216 * @param string[] $labels The labels.
218 public function set_labels(array $labels) {
219 $this->labels = $labels;
223 * Set the title.
225 * @param string $title The title.
227 public function set_title($title) {
228 $this->title = $title;
232 * Set an X axis.
234 * Note that this will override any predefined axis without warning.
236 * @param chart_axis $axis The axis.
237 * @param int $index The index of the axis.
239 public function set_xaxis(chart_axis $axis, $index = 0) {
240 $this->validate_axis('x', $axis, $index);
241 return $this->xaxes[$index] = $axis;
245 * Set an Y axis.
247 * Note that this will override any predefined axis without warning.
249 * @param chart_axis $axis The axis.
250 * @param int $index The index of the axis.
252 public function set_yaxis(chart_axis $axis, $index = 0) {
253 $this->validate_axis('y', $axis, $index);
254 return $this->yaxes[$index] = $axis;
258 * Validate an axis.
260 * We validate this from PHP because not doing it here could result in errors being
261 * hard to trace down. For instance, if we were to add axis at keys without another
262 * axis preceding, we would effectively contain the axes in an associative array
263 * rather than a simple array, and that would have consequences on serialisation.
265 * @param string $xy Accepts x or y.
266 * @param chart_axis $axis The axis to validate.
267 * @param index $index The index of the axis.
269 protected function validate_axis($xy, chart_axis $axis, $index = 0) {
270 if ($index > 0) {
271 $axes = $xy == 'x' ? $this->xaxes : $this->yaxes;
272 if (!isset($axes[$index - 1])) {
273 throw new coding_exception('Missing ' . $xy . ' axis at index lower than ' . $index);