MDL-55026 quiz_statistics: Convert charts 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
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
45 this._label = label;
46 this._values = values;
47 }
48
601da0e6
FM
49 /**
50 * The default type of series.
51 *
52 * @type {Null}
53 * @const
54 */
357ec2d5 55 Series.prototype.TYPE_DEFAULT = null;
601da0e6
FM
56
57 /**
58 * Type of series 'line'.
59 *
60 * @type {String}
61 * @const
62 */
357ec2d5
FM
63 Series.prototype.TYPE_LINE = 'line';
64
601da0e6
FM
65 /**
66 * The color of the series.
67 *
68 * @type {String}
69 * @protected
70 */
357ec2d5 71 Series.prototype._color = null;
601da0e6
FM
72
73 /**
74 * The label of the series.
75 *
76 * @type {String}
77 * @protected
78 */
357ec2d5 79 Series.prototype._label = null;
601da0e6
FM
80
81 /**
82 * The type of the series.
83 *
84 * @type {String}
85 * @protected
86 */
357ec2d5 87 Series.prototype._type = Series.prototype.TYPE_DEFAULT;
601da0e6
FM
88
89 /**
90 * The values in the series.
91 *
92 * @type {Number[]}
93 * @protected
94 */
357ec2d5
FM
95 Series.prototype._values = null;
96
601da0e6
FM
97 /**
98 * Create a new instance of a series from serialised data.
99 *
100 * @static
101 * @method create
102 * @param {Object} obj The data of the series.
103 * @return {module:core/chart_series}
104 */
357ec2d5
FM
105 Series.prototype.create = function(obj) {
106 var s = new Series(obj.label, obj.values);
107 s.setColor(obj.color);
108 s.setType(obj.type);
109 return s;
110 };
111
601da0e6
FM
112 /**
113 * Get the color.
114 *
115 * @return {String}
116 */
357ec2d5
FM
117 Series.prototype.getColor = function() {
118 return this._color;
119 };
120
601da0e6
FM
121 /**
122 * Get the number of values in the series.
123 *
124 * @return {Number}
125 */
357ec2d5
FM
126 Series.prototype.getCount = function() {
127 return this._values.length;
128 };
129
601da0e6
FM
130 /**
131 * Get the series label.
132 *
133 * @return {String}
134 */
357ec2d5
FM
135 Series.prototype.getLabel = function() {
136 return this._label;
137 };
138
601da0e6
FM
139 /**
140 * Get the series type.
141 *
142 * @return {String}
143 */
357ec2d5
FM
144 Series.prototype.getType = function() {
145 return this._type;
146 };
147
601da0e6
FM
148 /**
149 * Get the series values.
150 *
151 * @return {Number[]}
152 */
357ec2d5
FM
153 Series.prototype.getValues = function() {
154 return this._values;
155 };
156
601da0e6
FM
157 /**
158 * Set the series color.
159 *
160 * @param {String} color A CSS-compatible color.
161 */
357ec2d5
FM
162 Series.prototype.setColor = function(color) {
163 this._color = color || null;
164 };
165
601da0e6
FM
166 /**
167 * Set the type of the series.
168 *
169 * @param {String} type A type constant value.
170 */
357ec2d5
FM
171 Series.prototype.setType = function(type) {
172 if (type != this.TYPE_DEFAULT && type != this.TYPE_LINE) {
173 throw new Error('Invalid serie type.');
174 }
175 this._type = type || null;
176 };
177
178 return Series;
179
180});