MDL-55030 core: Support min and max in axes
[moodle.git] / lib / amd / src / chart_output_chartjs.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 output for chart.js.
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 */
f5474e65
FM
23define([
24 'jquery',
25 'core/chartjs',
26 'core/chart_axis',
27 'core/chart_output_base',
28], function($, Chartjs, Axis, Base) {
357ec2d5
FM
29
30 /**
31 * Chart output for Chart.js.
32 */
33 function Output() {
34 Base.prototype.constructor.apply(this, arguments);
35 this._build();
36 }
37
38 Output.prototype = Object.create(Base.prototype);
39
40 Output.prototype._config = null;
41 Output.prototype._chartjs = null;
42
43 Output.prototype.getDatasets = function() {
44 var sets = this._chart.getSeries().map(function(series) {
45 return {
46 label: series.getLabel(),
47 data: series.getValues(),
48 type: series.getType(),
49 fill: false,
50 borderColor: series.getColor(),
51 backgroundColor: series.getColor()
52 };
53 });
54 return sets;
55 };
56
57 Output.prototype._build = function() {
58 this._config = this._makeConfig();
59 this._chartjs = new Chartjs(this._node[0], this._config);
60 };
61
f5474e65
FM
62 Output.prototype._makeAxisConfig = function(axis) {
63 var scaleData = {};
64
65 if (axis.getPosition() !== Axis.prototype.POS_DEFAULT) {
66 scaleData.position = axis.getPosition();
67 }
68
69 if (axis.getLabel() !== null) {
70 scaleData.scaleLabel = {
71 display: true,
72 labelString: axis.getLabel()
73 };
74 }
75
909c5cf2
FM
76 if (axis.getStepSize() !== null) {
77 scaleData.ticks = scaleData.ticks || {};
78 scaleData.ticks.stepSize = axis.getStepSize();
79 }
80
46de49dc
FM
81 if (axis.getMax() !== null) {
82 scaleData.ticks = scaleData.ticks || {};
83 scaleData.ticks.max = axis.getMax();
84 }
85
86 if (axis.getMin() !== null) {
87 scaleData.ticks = scaleData.ticks || {};
88 scaleData.ticks.min = axis.getMin();
89 }
90
f5474e65
FM
91 return scaleData;
92 };
93
357ec2d5
FM
94 Output.prototype._makeConfig = function() {
95 var config = {
96 type: this._chart.getType(),
97 data: {
98 labels: this._chart.getLabels(),
99 datasets: this.getDatasets()
100 },
101 options: {
858cbfdf
SL
102 title: {
103 display: this._chart.getTitle() !== null,
104 text: this._chart.getTitle()
105 }
357ec2d5
FM
106 }
107 };
f5474e65
FM
108
109 this._chart.getXAxes().forEach(function(axis, i) {
110 config.options.scales = config.options.scales || {};
111 config.options.scales.xAxes = config.options.scales.xAxes || [];
112 config.options.scales.xAxes[i] = this._makeAxisConfig(axis);
113 }.bind(this));
114
115 this._chart.getYAxes().forEach(function(axis, i) {
116 config.options.scales = config.options.scales || {};
117 config.options.scales.yAxes = config.options.scales.yAxes || [];
118 config.options.scales.yAxes[i] = this._makeAxisConfig(axis);
119 }.bind(this));
120
357ec2d5
FM
121 return config;
122 };
123
124 Output.prototype.update = function() {
125 $.extend(true, this._config, this._makeConfig());
126 this._chartjs.update();
127 };
128
129 return Output;
130
131});