MDL-55029 report_courseoverview: Convert report chart to the new library
[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
22 */
23define([], function() {
24
25 /**
26 * Chart data series.
27 *
28 * @param {String} label The series label.
29 * @param {Number[]} values The values.
30 */
31 function Series(label, values) {
32 if (typeof label !== 'string') {
33 throw new Error('Invalid label for series.');
34
35 } else if (typeof values !== 'object') {
36 throw new Error('Values for a series must be an array.');
37
38 } else if (values.length < 1) {
39 throw new Error('Invalid values received for series.');
40 }
41
42 this._label = label;
43 this._values = values;
44 }
45
46 Series.prototype.TYPE_DEFAULT = null;
47 Series.prototype.TYPE_LINE = 'line';
48
49 Series.prototype._color = null;
50 Series.prototype._label = null;
51 Series.prototype._type = Series.prototype.TYPE_DEFAULT;
52 Series.prototype._values = null;
53
54 Series.prototype.create = function(obj) {
55 var s = new Series(obj.label, obj.values);
56 s.setColor(obj.color);
57 s.setType(obj.type);
58 return s;
59 };
60
61 Series.prototype.getColor = function() {
62 return this._color;
63 };
64
65 Series.prototype.getCount = function() {
66 return this._values.length;
67 };
68
69 Series.prototype.getLabel = function() {
70 return this._label;
71 };
72
73 Series.prototype.getType = function() {
74 return this._type;
75 };
76
77 Series.prototype.getValues = function() {
78 return this._values;
79 };
80
81 Series.prototype.setColor = function(color) {
82 this._color = color || null;
83 };
84
85 Series.prototype.setType = function(type) {
86 if (type != this.TYPE_DEFAULT && type != this.TYPE_LINE) {
87 throw new Error('Invalid serie type.');
88 }
89 this._type = type || null;
90 };
91
92 return Series;
93
94});