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 = [];
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();
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;
74 * Serialize the object.
78 public function jsonSerialize() { // @codingStandardsIgnoreLine (CONTRIB-6469).
80 'type' => $this->get_type(),
81 'series' => $this->series
,
82 'labels' => $this->labels
,
83 'title' => $this->title
,
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.
99 private function get_axis($type, $index, $createifnotexists) {
100 $isx = $type === 'x';
102 $axis = isset($this->xaxes
[$index]) ?
$this->xaxes
[$index] : null;
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();
114 $this->set_xaxis($axis, $index);
116 $this->set_yaxis($axis, $index);
124 * Get the labels of the X axis.
128 public function get_labels() {
129 return $this->labels
;
135 * @return chart_series[]
137 public function get_series() {
138 return $this->series
;
146 public function get_title() {
151 * Get the chart type.
155 public function get_type() {
156 $classname = get_class($this);
157 return substr($classname, strpos($classname, '_') +
1);
163 * @return chart_axis[]
165 public function get_xaxes() {
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.
176 public function get_xaxis($index = 0, $createifnotexists = false) {
177 return $this->get_axis('x', $index, $createifnotexists);
183 * @return chart_axis[]
185 public function get_yaxes() {
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.
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.
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;
225 * @param string $title The title.
227 public function set_title($title) {
228 $this->title
= $title;
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;
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;
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) {
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);