MDL-55030 core: Support setting defaults in charts
[moodle.git] / lib / classes / chart_base.php
CommitLineData
357ec2d5
FM
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.
13//
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/>.
16
17/**
18 * Chart base.
19 *
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
23 */
24
25namespace core;
26defined('MOODLE_INTERNAL') || die();
27
28use coding_exception;
29use JsonSerializable;
30use renderable;
31
32/**
33 * Chart base class.
34 *
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
38 */
39class chart_base implements JsonSerializable, renderable {
40
41 protected $series = [];
42 protected $labels = [];
858cbfdf 43 protected $title = null;
f5474e65
FM
44 protected $xaxes = [];
45 protected $yaxes = [];
357ec2d5
FM
46
47 public function __construct() {
a6c6faef 48 $this->set_defaults();
357ec2d5
FM
49 }
50
51 public function add_series(chart_series $serie) {
52 $this->series[] = $serie;
53 }
54
55 public function jsonSerialize() {
56 return [
57 'type' => $this->get_type(),
58 'series' => $this->series,
858cbfdf 59 'labels' => $this->labels,
f5474e65
FM
60 'title' => $this->title,
61 'axes' => [
62 'x' => $this->xaxes,
63 'y' => $this->yaxes,
64 ]
357ec2d5
FM
65 ];
66 }
67
f5474e65
FM
68 private function get_axis($type, $index, $createifnotexists) {
69 if ($type === 'x') {
70 $axes = &$this->xaxes;
71 } else {
72 $axes = &$this->yaxes;
73 }
74
75 if (!isset($axes[$index])) {
76 if (!$createifnotexists) {
77 throw new coding_exception('Unknown axis.');
78 }
79 $axes[$index] = new chart_axis();
80 }
81
82 return $axes[$index];
83 }
84
357ec2d5
FM
85 public function get_labels() {
86 return $this->labels;
87 }
88
89 public function get_series() {
90 return $this->series;
91 }
92
858cbfdf
SL
93 public function get_title() {
94 return $this->title;
95 }
96
357ec2d5
FM
97 public function get_type() {
98 $classname = get_class($this);
99 return substr($classname, strpos($classname, '_') + 1);
100 }
101
f5474e65
FM
102 public function get_xaxes() {
103 return $this->xaxes;
104 }
105
106 public function get_xaxis($index = 0, $createifnotexists = false) {
107 return $this->get_axis('x', $index, $createifnotexists);
108 }
109
110 public function get_yaxes() {
111 return $this->yaxes;
112 }
113
114 public function get_yaxis($index = 0, $createifnotexists = false) {
115 return $this->get_axis('y', $index, $createifnotexists);
116 }
117
a6c6faef
FM
118 protected function set_defaults() {
119 // For the child classes to extend.
120 }
121
357ec2d5
FM
122 public function set_labels(array $labels) {
123 $this->labels = $labels;
124 }
125
858cbfdf
SL
126 public function set_title($title) {
127 $this->title = $title;
128 }
f5474e65
FM
129
130 public function set_xaxis(chart_axis $axis, $index = 0) {
131 return $this->xaxes[$index] = $axis;
132 }
133
134 public function set_yaxis(chart_axis $axis, $index = 0) {
135 return $this->yaxes[$index] = $axis;
136 }
137
357ec2d5 138}