MDL-55146 core: Support custom labels on X axis
[moodle.git] / lib / amd / src / chart_series.js
CommitLineData
357ec2d5
FM
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15
16/**
17 * Chart series.
18 *
19 * @package core
20 * @copyright 2016 Frédéric Massart - FMCorz.net
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
601da0e6 22 * @module core/chart_series
357ec2d5
FM
23 */
24define([], function() {
25
26 /**
27 * Chart data series.
28 *
601da0e6
FM
29 * @class
30 * @alias module:core/chart_series
357ec2d5
FM
31 * @param {String} label The series label.
32 * @param {Number[]} values The values.
33 */
34 function Series(label, values) {
35 if (typeof label !== 'string') {
36 throw new Error('Invalid label for series.');
37
38 } else if (typeof values !== 'object') {
39 throw new Error('Values for a series must be an array.');
40
41 } else if (values.length < 1) {
42 throw new Error('Invalid values received for series.');
43 }
44
ccaa2b34 45 this._colors = [];
357ec2d5
FM
46 this._label = label;
47 this._values = values;
48 }
49
601da0e6
FM
50 /**
51 * The default type of series.
52 *
53 * @type {Null}
54 * @const
55 */
357ec2d5 56 Series.prototype.TYPE_DEFAULT = null;
601da0e6
FM
57
58 /**
59 * Type of series 'line'.
60 *
61 * @type {String}
62 * @const
63 */
357ec2d5
FM
64 Series.prototype.TYPE_LINE = 'line';
65
601da0e6 66 /**
ccaa2b34 67 * The colors of the series.
601da0e6 68 *
ccaa2b34 69 * @type {String[]}
601da0e6
FM
70 * @protected
71 */
ccaa2b34 72 Series.prototype._colors = null;
601da0e6
FM
73
74 /**
75 * The label of the series.
76 *
77 * @type {String}
78 * @protected
79 */
357ec2d5 80 Series.prototype._label = null;
601da0e6
FM
81
82 /**
83 * The type of the series.
84 *
85 * @type {String}
86 * @protected
87 */
357ec2d5 88 Series.prototype._type = Series.prototype.TYPE_DEFAULT;
601da0e6
FM
89
90 /**
91 * The values in the series.
92 *
93 * @type {Number[]}
94 * @protected
95 */
357ec2d5
FM
96 Series.prototype._values = null;
97
2b01f915
FM
98 /**
99 * The index of the X axis.
100 *
101 * @type {Number[]}
102 * @protected
103 */
104 Series.prototype._xaxis = null;
105
106 /**
107 * The index of the Y axis.
108 *
109 * @type {Number[]}
110 * @protected
111 */
112 Series.prototype._yaxis = null;
113
601da0e6
FM
114 /**
115 * Create a new instance of a series from serialised data.
116 *
117 * @static
118 * @method create
119 * @param {Object} obj The data of the series.
120 * @return {module:core/chart_series}
121 */
357ec2d5
FM
122 Series.prototype.create = function(obj) {
123 var s = new Series(obj.label, obj.values);
357ec2d5 124 s.setType(obj.type);
2b01f915
FM
125 s.setXAxis(obj.axes.x);
126 s.setYAxis(obj.axes.y);
ccaa2b34
FM
127
128 // Colors are exported as an array with 1, or n values.
129 if (obj.colors && obj.colors.length > 1) {
130 s.setColors(obj.colors);
131 } else {
132 s.setColor(obj.colors[0]);
133 }
134
357ec2d5
FM
135 return s;
136 };
137
601da0e6
FM
138 /**
139 * Get the color.
140 *
141 * @return {String}
142 */
357ec2d5 143 Series.prototype.getColor = function() {
ccaa2b34
FM
144 return this._colors[0] || null;
145 };
146
147 /**
148 * Get the colors for each value in the series.
149 *
150 * @return {String[]}
151 */
152 Series.prototype.getColors = function() {
153 return this._colors;
357ec2d5
FM
154 };
155
601da0e6
FM
156 /**
157 * Get the number of values in the series.
158 *
159 * @return {Number}
160 */
357ec2d5
FM
161 Series.prototype.getCount = function() {
162 return this._values.length;
163 };
164
601da0e6
FM
165 /**
166 * Get the series label.
167 *
168 * @return {String}
169 */
357ec2d5
FM
170 Series.prototype.getLabel = function() {
171 return this._label;
172 };
173
601da0e6
FM
174 /**
175 * Get the series type.
176 *
177 * @return {String}
178 */
357ec2d5
FM
179 Series.prototype.getType = function() {
180 return this._type;
181 };
182
601da0e6
FM
183 /**
184 * Get the series values.
185 *
186 * @return {Number[]}
187 */
357ec2d5
FM
188 Series.prototype.getValues = function() {
189 return this._values;
190 };
191
2b01f915
FM
192 /**
193 * Get the index of the X axis.
194 *
195 * @return {Number}
196 */
197 Series.prototype.getXAxis = function() {
198 return this._xaxis;
199 };
200
201 /**
202 * Get the index of the Y axis.
203 *
204 * @return {Number}
205 */
206 Series.prototype.getYAxis = function() {
207 return this._yaxis;
208 };
209
ccaa2b34
FM
210 /**
211 * Whether there is a color per value.
212 *
213 * @return {Bool}
214 */
215 Series.prototype.hasColoredValues = function() {
216 return this._colors.length == this.getCount();
217 };
218
601da0e6
FM
219 /**
220 * Set the series color.
221 *
222 * @param {String} color A CSS-compatible color.
223 */
357ec2d5 224 Series.prototype.setColor = function(color) {
ccaa2b34
FM
225 this._colors = [color];
226 };
227
228 /**
229 * Set a color for each value in the series.
230 *
231 * @param {String[]} colors CSS-compatible colors.
232 */
233 Series.prototype.setColors = function(colors) {
234 if (colors && colors.length != this.getCount()) {
235 throw new Error('When setting multiple colors there must be one per value.');
236 }
237 this._colors = colors || [];
357ec2d5
FM
238 };
239
601da0e6
FM
240 /**
241 * Set the type of the series.
242 *
243 * @param {String} type A type constant value.
244 */
357ec2d5
FM
245 Series.prototype.setType = function(type) {
246 if (type != this.TYPE_DEFAULT && type != this.TYPE_LINE) {
247 throw new Error('Invalid serie type.');
248 }
249 this._type = type || null;
250 };
251
2b01f915
FM
252 /**
253 * Set the index of the X axis.
254 *
255 * @param {Number} index The index.
256 */
257 Series.prototype.setXAxis = function(index) {
258 this._xaxis = index || null;
259 };
260
261
262 /**
263 * Set the index of the Y axis.
264 *
265 * @param {Number} index The index.
266 */
267 Series.prototype.setYAxis = function(index) {
268 this._yaxis = index || null;
269 };
270
357ec2d5
FM
271 return Series;
272
273});