MDL-55089 core: Charts are displayed with a data table by default
[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
601da0e6
FM
98 /**
99 * Create a new instance of a series from serialised data.
100 *
101 * @static
102 * @method create
103 * @param {Object} obj The data of the series.
104 * @return {module:core/chart_series}
105 */
357ec2d5
FM
106 Series.prototype.create = function(obj) {
107 var s = new Series(obj.label, obj.values);
357ec2d5 108 s.setType(obj.type);
ccaa2b34
FM
109
110 // Colors are exported as an array with 1, or n values.
111 if (obj.colors && obj.colors.length > 1) {
112 s.setColors(obj.colors);
113 } else {
114 s.setColor(obj.colors[0]);
115 }
116
357ec2d5
FM
117 return s;
118 };
119
601da0e6
FM
120 /**
121 * Get the color.
122 *
123 * @return {String}
124 */
357ec2d5 125 Series.prototype.getColor = function() {
ccaa2b34
FM
126 return this._colors[0] || null;
127 };
128
129 /**
130 * Get the colors for each value in the series.
131 *
132 * @return {String[]}
133 */
134 Series.prototype.getColors = function() {
135 return this._colors;
357ec2d5
FM
136 };
137
601da0e6
FM
138 /**
139 * Get the number of values in the series.
140 *
141 * @return {Number}
142 */
357ec2d5
FM
143 Series.prototype.getCount = function() {
144 return this._values.length;
145 };
146
601da0e6
FM
147 /**
148 * Get the series label.
149 *
150 * @return {String}
151 */
357ec2d5
FM
152 Series.prototype.getLabel = function() {
153 return this._label;
154 };
155
601da0e6
FM
156 /**
157 * Get the series type.
158 *
159 * @return {String}
160 */
357ec2d5
FM
161 Series.prototype.getType = function() {
162 return this._type;
163 };
164
601da0e6
FM
165 /**
166 * Get the series values.
167 *
168 * @return {Number[]}
169 */
357ec2d5
FM
170 Series.prototype.getValues = function() {
171 return this._values;
172 };
173
ccaa2b34
FM
174 /**
175 * Whether there is a color per value.
176 *
177 * @return {Bool}
178 */
179 Series.prototype.hasColoredValues = function() {
180 return this._colors.length == this.getCount();
181 };
182
601da0e6
FM
183 /**
184 * Set the series color.
185 *
186 * @param {String} color A CSS-compatible color.
187 */
357ec2d5 188 Series.prototype.setColor = function(color) {
ccaa2b34
FM
189 this._colors = [color];
190 };
191
192 /**
193 * Set a color for each value in the series.
194 *
195 * @param {String[]} colors CSS-compatible colors.
196 */
197 Series.prototype.setColors = function(colors) {
198 if (colors && colors.length != this.getCount()) {
199 throw new Error('When setting multiple colors there must be one per value.');
200 }
201 this._colors = colors || [];
357ec2d5
FM
202 };
203
601da0e6
FM
204 /**
205 * Set the type of the series.
206 *
207 * @param {String} type A type constant value.
208 */
357ec2d5
FM
209 Series.prototype.setType = function(type) {
210 if (type != this.TYPE_DEFAULT && type != this.TYPE_LINE) {
211 throw new Error('Invalid serie type.');
212 }
213 this._type = type || null;
214 };
215
216 return Series;
217
218});