Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * @copyright  2016 Frédéric Massart - FMCorz.net
20
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 * @module     core/chart_series
22
 */
23
define([], function() {
24
 
25
    /**
26
     * Chart data series.
27
     *
28
     * @class
29
     * @param {String} label The series label.
30
     * @param {Number[]} values The values.
31
     */
32
    function Series(label, values) {
33
        if (typeof label !== 'string') {
34
            throw new Error('Invalid label for series.');
35
 
36
        } else if (typeof values !== 'object') {
37
            throw new Error('Values for a series must be an array.');
38
 
39
        } else if (values.length < 1) {
40
            throw new Error('Invalid values received for series.');
41
        }
42
 
43
        this._colors = [];
44
        this._label = label;
45
        this._values = values;
46
    }
47
 
48
    /**
49
     * The default type of series.
50
     *
51
     * @type {Null}
52
     * @const
53
     */
54
    Series.prototype.TYPE_DEFAULT = null;
55
 
56
    /**
57
     * Type of series 'line'.
58
     *
59
     * @type {String}
60
     * @const
61
     */
62
    Series.prototype.TYPE_LINE = 'line';
63
 
64
    /**
65
     * The colors of the series.
66
     *
67
     * @type {String[]}
68
     * @protected
69
     */
70
    Series.prototype._colors = null;
71
 
72
    /**
73
     * The fill mode of the series.
74
     *
75
     * @type {Object}
76
     * @protected
77
     */
78
    Series.prototype._fill = false;
79
 
80
    /**
81
     * The label of the series.
82
     *
83
     * @type {String}
84
     * @protected
85
     */
86
    Series.prototype._label = null;
87
 
88
    /**
89
     * The labels for the values of the series.
90
     *
91
     * @type {String[]}
92
     * @protected
93
     */
94
     Series.prototype._labels = null;
95
 
96
    /**
97
     * Whether the line of the serie should be smooth or not.
98
     *
99
     * @type {Bool}
100
     * @protected
101
     */
102
    Series.prototype._smooth = false;
103
 
104
    /**
105
     * The type of the series.
106
     *
107
     * @type {String}
108
     * @protected
109
     */
110
    Series.prototype._type = Series.prototype.TYPE_DEFAULT;
111
 
112
    /**
113
     * The values in the series.
114
     *
115
     * @type {Number[]}
116
     * @protected
117
     */
118
    Series.prototype._values = null;
119
 
120
    /**
121
     * The index of the X axis.
122
     *
123
     * @type {Number[]}
124
     * @protected
125
     */
126
    Series.prototype._xaxis = null;
127
 
128
    /**
129
     * The index of the Y axis.
130
     *
131
     * @type {Number[]}
132
     * @protected
133
     */
134
    Series.prototype._yaxis = null;
135
 
136
    /**
137
     * Create a new instance of a series from serialised data.
138
     *
139
     * @static
140
     * @method create
141
     * @param {Object} obj The data of the series.
142
     * @return {module:core/chart_series}
143
     */
144
    Series.prototype.create = function(obj) {
145
        var s = new Series(obj.label, obj.values);
146
        s.setType(obj.type);
147
        s.setXAxis(obj.axes.x);
148
        s.setYAxis(obj.axes.y);
149
        s.setLabels(obj.labels);
150
 
151
        // Colors are exported as an array with 1, or n values.
152
        if (obj.colors && obj.colors.length > 1) {
153
            s.setColors(obj.colors);
154
        } else {
155
            s.setColor(obj.colors[0]);
156
        }
157
 
158
        s.setFill(obj.fill);
159
        s.setSmooth(obj.smooth);
160
        return s;
161
    };
162
 
163
    /**
164
     * Get the color.
165
     *
166
     * @return {String}
167
     */
168
    Series.prototype.getColor = function() {
169
        return this._colors[0] || null;
170
    };
171
 
172
    /**
173
     * Get the colors for each value in the series.
174
     *
175
     * @return {String[]}
176
     */
177
    Series.prototype.getColors = function() {
178
        return this._colors;
179
    };
180
 
181
    /**
182
     * Get the number of values in the series.
183
     *
184
     * @return {Number}
185
     */
186
    Series.prototype.getCount = function() {
187
        return this._values.length;
188
    };
189
 
190
    /**
191
     * Get the fill mode of the series.
192
     *
193
     * @return {Object}
194
     */
195
    Series.prototype.getFill = function() {
196
      return this._fill;
197
    };
198
 
199
    /**
200
     * Get the series label.
201
     *
202
     * @return {String}
203
     */
204
    Series.prototype.getLabel = function() {
205
        return this._label;
206
    };
207
 
208
    /**
209
     * Get labels for the values of the series.
210
     *
211
     * @return {String[]}
212
     */
213
    Series.prototype.getLabels = function() {
214
        return this._labels;
215
    };
216
 
217
    /**
218
     * Get whether the line of the serie should be smooth or not.
219
     *
220
     * @returns {Bool}
221
     */
222
    Series.prototype.getSmooth = function() {
223
        return this._smooth;
224
    };
225
 
226
    /**
227
     * Get the series type.
228
     *
229
     * @return {String}
230
     */
231
    Series.prototype.getType = function() {
232
        return this._type;
233
    };
234
 
235
    /**
236
     * Get the series values.
237
     *
238
     * @return {Number[]}
239
     */
240
    Series.prototype.getValues = function() {
241
        return this._values;
242
    };
243
 
244
    /**
245
     * Get the index of the X axis.
246
     *
247
     * @return {Number}
248
     */
249
    Series.prototype.getXAxis = function() {
250
        return this._xaxis;
251
    };
252
 
253
    /**
254
     * Get the index of the Y axis.
255
     *
256
     * @return {Number}
257
     */
258
    Series.prototype.getYAxis = function() {
259
        return this._yaxis;
260
    };
261
 
262
    /**
263
     * Whether there is a color per value.
264
     *
265
     * @return {Bool}
266
     */
267
    Series.prototype.hasColoredValues = function() {
268
        return this._colors.length == this.getCount();
269
    };
270
 
271
    /**
272
     * Set the series color.
273
     *
274
     * @param {String} color A CSS-compatible color.
275
     */
276
    Series.prototype.setColor = function(color) {
277
        this._colors = [color];
278
    };
279
 
280
    /**
281
     * Set a color for each value in the series.
282
     *
283
     * @param {String[]} colors CSS-compatible colors.
284
     */
285
    Series.prototype.setColors = function(colors) {
286
        if (colors && colors.length != this.getCount()) {
287
            throw new Error('When setting multiple colors there must be one per value.');
288
        }
289
        this._colors = colors || [];
290
    };
291
 
292
    /**
293
     * Set the fill mode for the series.
294
     *
295
     * @param {Object} fill
296
     */
297
    Series.prototype.setFill = function(fill) {
298
      this._fill = typeof fill === 'undefined' ? null : fill;
299
    };
300
 
301
    /**
302
     * Set the labels for the values of the series.
303
     *
304
     * @param {String[]} labels the labels of the series values.
305
     */
306
    Series.prototype.setLabels = function(labels) {
307
        this._validateLabels(labels);
308
        labels = typeof labels === 'undefined' ? null : labels;
309
        this._labels = labels;
310
    };
311
 
312
    /**
313
     * Set Whether the line of the serie should be smooth or not.
314
     *
315
     * Only applicable for line chart or a line series, if null it assumes the chart default (not smooth).
316
     *
317
     * @param {Bool} smooth True if the lines should be smooth, false for tensioned lines.
318
     */
319
    Series.prototype.setSmooth = function(smooth) {
320
        smooth = typeof smooth === 'undefined' ? null : smooth;
321
        this._smooth = smooth;
322
    };
323
 
324
    /**
325
     * Set the type of the series.
326
     *
327
     * @param {String} type A type constant value.
328
     */
329
    Series.prototype.setType = function(type) {
330
        if (type != this.TYPE_DEFAULT && type != this.TYPE_LINE) {
331
            throw new Error('Invalid serie type.');
332
        }
333
        this._type = type || null;
334
    };
335
 
336
    /**
337
     * Set the index of the X axis.
338
     *
339
     * @param {Number} index The index.
340
     */
341
    Series.prototype.setXAxis = function(index) {
342
        this._xaxis = index || null;
343
    };
344
 
345
 
346
    /**
347
     * Set the index of the Y axis.
348
     *
349
     * @param {Number} index The index.
350
     */
351
    Series.prototype.setYAxis = function(index) {
352
        this._yaxis = index || null;
353
    };
354
 
355
    /**
356
     * Validate series labels.
357
     *
358
     * @protected
359
     * @param {String[]} labels The labels of the serie.
360
     */
361
    Series.prototype._validateLabels = function(labels) {
362
        if (labels && labels.length > 0 && labels.length != this.getCount()) {
363
            throw new Error('Series labels must match series values.');
364
        }
365
    };
366
 
367
    return Series;
368
 
369
});