MDL-55110 core: Add support for smooth lines
[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 81
f0f1e031
SL
82 /**
83 * Whether the line of the serie should be smooth or not.
84 *
85 * @type {Bool}
86 * @protected
87 */
88 Series.prototype._smooth = false;
89
601da0e6
FM
90 /**
91 * The type of the series.
92 *
93 * @type {String}
94 * @protected
95 */
357ec2d5 96 Series.prototype._type = Series.prototype.TYPE_DEFAULT;
601da0e6
FM
97
98 /**
99 * The values in the series.
100 *
101 * @type {Number[]}
102 * @protected
103 */
357ec2d5
FM
104 Series.prototype._values = null;
105
2b01f915
FM
106 /**
107 * The index of the X axis.
108 *
109 * @type {Number[]}
110 * @protected
111 */
112 Series.prototype._xaxis = null;
113
114 /**
115 * The index of the Y axis.
116 *
117 * @type {Number[]}
118 * @protected
119 */
120 Series.prototype._yaxis = null;
121
601da0e6
FM
122 /**
123 * Create a new instance of a series from serialised data.
124 *
125 * @static
126 * @method create
127 * @param {Object} obj The data of the series.
128 * @return {module:core/chart_series}
129 */
357ec2d5
FM
130 Series.prototype.create = function(obj) {
131 var s = new Series(obj.label, obj.values);
357ec2d5 132 s.setType(obj.type);
2b01f915
FM
133 s.setXAxis(obj.axes.x);
134 s.setYAxis(obj.axes.y);
ccaa2b34
FM
135
136 // Colors are exported as an array with 1, or n values.
137 if (obj.colors && obj.colors.length > 1) {
138 s.setColors(obj.colors);
139 } else {
140 s.setColor(obj.colors[0]);
141 }
142
f0f1e031 143 s.setSmooth(obj.smooth);
357ec2d5
FM
144 return s;
145 };
146
601da0e6
FM
147 /**
148 * Get the color.
149 *
150 * @return {String}
151 */
357ec2d5 152 Series.prototype.getColor = function() {
ccaa2b34
FM
153 return this._colors[0] || null;
154 };
155
156 /**
157 * Get the colors for each value in the series.
158 *
159 * @return {String[]}
160 */
161 Series.prototype.getColors = function() {
162 return this._colors;
357ec2d5
FM
163 };
164
601da0e6
FM
165 /**
166 * Get the number of values in the series.
167 *
168 * @return {Number}
169 */
357ec2d5
FM
170 Series.prototype.getCount = function() {
171 return this._values.length;
172 };
173
601da0e6
FM
174 /**
175 * Get the series label.
176 *
177 * @return {String}
178 */
357ec2d5
FM
179 Series.prototype.getLabel = function() {
180 return this._label;
181 };
182
f0f1e031
SL
183 /**
184 * Get whether the line of the serie should be smooth or not.
185 *
186 * @returns {Bool}
187 */
188 Series.prototype.getSmooth = function() {
189 return this._smooth;
190 };
191
601da0e6
FM
192 /**
193 * Get the series type.
194 *
195 * @return {String}
196 */
357ec2d5
FM
197 Series.prototype.getType = function() {
198 return this._type;
199 };
200
601da0e6
FM
201 /**
202 * Get the series values.
203 *
204 * @return {Number[]}
205 */
357ec2d5
FM
206 Series.prototype.getValues = function() {
207 return this._values;
208 };
209
2b01f915
FM
210 /**
211 * Get the index of the X axis.
212 *
213 * @return {Number}
214 */
215 Series.prototype.getXAxis = function() {
216 return this._xaxis;
217 };
218
219 /**
220 * Get the index of the Y axis.
221 *
222 * @return {Number}
223 */
224 Series.prototype.getYAxis = function() {
225 return this._yaxis;
226 };
227
ccaa2b34
FM
228 /**
229 * Whether there is a color per value.
230 *
231 * @return {Bool}
232 */
233 Series.prototype.hasColoredValues = function() {
234 return this._colors.length == this.getCount();
235 };
236
601da0e6
FM
237 /**
238 * Set the series color.
239 *
240 * @param {String} color A CSS-compatible color.
241 */
357ec2d5 242 Series.prototype.setColor = function(color) {
ccaa2b34
FM
243 this._colors = [color];
244 };
245
246 /**
247 * Set a color for each value in the series.
248 *
249 * @param {String[]} colors CSS-compatible colors.
250 */
251 Series.prototype.setColors = function(colors) {
252 if (colors && colors.length != this.getCount()) {
253 throw new Error('When setting multiple colors there must be one per value.');
254 }
255 this._colors = colors || [];
357ec2d5
FM
256 };
257
f0f1e031
SL
258 /**
259 * Set Whether the line of the serie should be smooth or not.
260 *
261 * Only applicable for line chart or a line series, if null it assumes the chart default (not smooth).
262 *
263 * @param smooth
264 */
265 Series.prototype.setSmooth = function(smooth) {
266 smooth = typeof smooth === 'undefined' ? null : smooth;
267 this._smooth = smooth;
268 };
269
601da0e6
FM
270 /**
271 * Set the type of the series.
272 *
273 * @param {String} type A type constant value.
274 */
357ec2d5
FM
275 Series.prototype.setType = function(type) {
276 if (type != this.TYPE_DEFAULT && type != this.TYPE_LINE) {
277 throw new Error('Invalid serie type.');
278 }
279 this._type = type || null;
280 };
281
2b01f915
FM
282 /**
283 * Set the index of the X axis.
284 *
285 * @param {Number} index The index.
286 */
287 Series.prototype.setXAxis = function(index) {
288 this._xaxis = index || null;
289 };
290
291
292 /**
293 * Set the index of the Y axis.
294 *
295 * @param {Number} index The index.
296 */
297 Series.prototype.setYAxis = function(index) {
298 this._yaxis = index || null;
299 };
300
357ec2d5
FM
301 return Series;
302
303});