MDL-55016 core: Support stepSize in chart axes
[moodle.git] / lib / amd / src / chart_axis.js
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/>.
16 /**
17  * Chart axis.
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  */
23 define([], function() {
25     /**
26      * Chart axis.
27      */
28     function Axis() {
29         // Please eslint no-empty-function.
30     }
32     Axis.prototype.POS_DEFAULT = null;
33     Axis.prototype.POS_BOTTOM = 'bottom';
34     Axis.prototype.POS_LEFT = 'left';
35     Axis.prototype.POS_RIGHT = 'right';
36     Axis.prototype.POS_TOP = 'top';
38     Axis.prototype._label = null;
39     Axis.prototype._position = null;
40     Axis.prototype._stepSize = null;
42     Axis.prototype.create = function(obj) {
43         var s = new Axis();
44         s.setPosition(obj.position);
45         s.setLabel(obj.label);
46         s.setStepSize(obj.stepSize);
47         return s;
48     };
50     Axis.prototype.getLabel = function() {
51         return this._label;
52     };
54     Axis.prototype.getPosition = function() {
55         return this._position;
56     };
58     Axis.prototype.getStepSize = function() {
59         return this._stepSize;
60     };
62     Axis.prototype.setLabel = function(label) {
63         this._label = label || null;
64     };
66     Axis.prototype.setPosition = function(position) {
67         if (position != this.POS_DEFAULT
68                 && position != this.POS_BOTTOM
69                 && position != this.POS_LEFT
70                 && position != this.POS_RIGHT
71                 && position != this.POS_TOP) {
72             throw new Error('Invalid axis position.');
73         }
74         this._position = position;
75     };
77     Axis.prototype.setStepSize = function(stepSize) {
78         if (typeof stepSize === 'undefined' || stepSize === null) {
79             stepSize = null;
80         } else if (isNaN(Number(stepSize))) {
81             throw new Error('Value for stepSize is not a number.');
82         } else {
83             stepSize = Number(stepSize);
84         }
86         this._stepSize = stepSize;
87     };
89     return Axis;
91 });