2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
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 = [];
51 /** @var array Options for the chart legend. */
52 protected $legendoptions = [];
57 * Must not take any argument.
59 * Most of the time you do not want to extend this, rather extend the
60 * method {@link self::set_defaults} to set the defaults on instantiation.
62 public function __construct() {
63 $this->set_defaults();
67 * Add a series to the chart.
69 * @param chart_series $serie The serie.
71 public function add_series(chart_series
$serie) {
72 $this->series
[] = $serie;
76 * Serialize the object.
80 public function jsonSerialize() { // @codingStandardsIgnoreLine (CONTRIB-6469).
83 'type' => $this->get_type(),
84 'series' => $this->series
,
85 'labels' => $this->labels
,
86 'title' => $this->title
,
91 'legend_options' => !empty($this->legendoptions
) ?
$this->legendoptions
: null,
92 'config_colorset' => !empty($CFG->chart_colorset
) ?
$CFG->chart_colorset
: null
99 * @param string $type Accepts values 'x' or 'y'.
100 * @param int $index The index of this axis.
101 * @param bool $createifnotexists Whether to create the axis if not found.
104 private function get_axis($type, $index, $createifnotexists) {
105 $isx = $type === 'x';
107 $axis = isset($this->xaxes
[$index]) ?
$this->xaxes
[$index] : null;
109 $axis = isset($this->yaxes
[$index]) ?
$this->yaxes
[$index] : null;
112 if ($axis === null) {
113 if (!$createifnotexists) {
114 throw new coding_exception('Unknown axis.');
117 $axis = new chart_axis();
119 $this->set_xaxis($axis, $index);
121 $this->set_yaxis($axis, $index);
129 * Get the labels of the X axis.
133 public function get_labels() {
134 return $this->labels
;
138 * Get an array of options for the chart legend.
142 public function get_legend_options() {
143 return $this->legendoptions
;
149 * @return chart_series[]
151 public function get_series() {
152 return $this->series
;
160 public function get_title() {
165 * Get the chart type.
169 public function get_type() {
170 $classname = get_class($this);
171 return substr($classname, strpos($classname, '_') +
1);
177 * @return chart_axis[]
179 public function get_xaxes() {
186 * @param int $index The index of the axis.
187 * @param bool $createifnotexists When true, create an instance of the axis if none exist at this index yet.
190 public function get_xaxis($index = 0, $createifnotexists = false) {
191 return $this->get_axis('x', $index, $createifnotexists);
197 * @return chart_axis[]
199 public function get_yaxes() {
206 * @param int $index The index of the axis.
207 * @param bool $createifnotexists When true, create an instance of the axis if none exist at this index yet.
210 public function get_yaxis($index = 0, $createifnotexists = false) {
211 return $this->get_axis('y', $index, $createifnotexists);
215 * Set the defaults for this chart type.
217 * Child classes can extend this to set default values on instantiation.
219 * In general the constructor could be used, but this method is here to
220 * emphasize and self-document the default values set by the chart type.
224 protected function set_defaults() {
228 * Set the chart labels.
230 * @param string[] $labels The labels.
232 public function set_labels(array $labels) {
233 $this->labels
= $labels;
237 * Set options for the chart legend.
238 * See https://www.chartjs.org/docs/2.7.0/configuration/legend.html for options.
240 * Note: Setting onClick and onHover events is not directly supported through
241 * this method. These config options must be set directly within Javascript
244 * @param array $legendoptions Whether or not to display the chart's legend.
246 public function set_legend_options(array $legendoptions) {
247 $this->legendoptions
= $legendoptions;
253 * @param string $title The title.
255 public function set_title($title) {
256 $this->title
= $title;
262 * Note that this will override any predefined axis without warning.
264 * @param chart_axis $axis The axis.
265 * @param int $index The index of the axis.
267 public function set_xaxis(chart_axis
$axis, $index = 0) {
268 $this->validate_axis('x', $axis, $index);
269 return $this->xaxes
[$index] = $axis;
275 * Note that this will override any predefined axis without warning.
277 * @param chart_axis $axis The axis.
278 * @param int $index The index of the axis.
280 public function set_yaxis(chart_axis
$axis, $index = 0) {
281 $this->validate_axis('y', $axis, $index);
282 return $this->yaxes
[$index] = $axis;
288 * We validate this from PHP because not doing it here could result in errors being
289 * hard to trace down. For instance, if we were to add axis at keys without another
290 * axis preceding, we would effectively contain the axes in an associative array
291 * rather than a simple array, and that would have consequences on serialisation.
293 * @param string $xy Accepts x or y.
294 * @param chart_axis $axis The axis to validate.
295 * @param index $index The index of the axis.
297 protected function validate_axis($xy, chart_axis
$axis, $index = 0) {
299 $axes = $xy == 'x' ?
$this->xaxes
: $this->yaxes
;
300 if (!isset($axes[$index - 1])) {
301 throw new coding_exception('Missing ' . $xy . ' axis at index lower than ' . $index);