diff --git a/lib/amd/build/chart_base.min.js b/lib/amd/build/chart_base.min.js index f78a3a3c0bb..118114d0456 100644 --- a/lib/amd/build/chart_base.min.js +++ b/lib/amd/build/chart_base.min.js @@ -1 +1 @@ -define(["core/chart_series","core/chart_axis"],function(a,b){function c(){this._series=[],this._labels=[],this._xaxes=[],this._yaxes=[],this._setDefaults()}return c.prototype._series=null,c.prototype._labels=null,c.prototype._title=null,c.prototype._xaxes=null,c.prototype._yaxes=null,c.prototype.COLORSET=["#f3c300","#875692","#f38400","#a1caf1","#be0032","#c2b280","#7f180d","#008856","#e68fac","#0067a5"],c.prototype.TYPE=null,c.prototype.addSeries=function(a){this._validateSeries(a),this._series.push(a),null===a.getColor()&&a.setColor(c.prototype.COLORSET[this._series.length%c.prototype.COLORSET.length])},c.prototype.create=function(c,d){var e=new c;return e.setLabels(d.labels),e.setTitle(d.title),d.series.forEach(function(b){e.addSeries(a.prototype.create(b))}),d.axes.x.forEach(function(a,c){e.setXAxis(b.prototype.create(a),c)}),d.axes.y.forEach(function(a,c){e.setYAxis(b.prototype.create(a),c)}),e},c.prototype.__getAxis=function(a,c,d){var e,f="x"===a?this._xaxes:this._yaxes,g=("x"===a?this.setXAxis:this.setYAxis).bind(this);if(c="undefined"==typeof c?0:c,d="undefined"!=typeof d&&d,e=f[c],"undefined"==typeof e){if(!d)throw new Error("Unknown axis.");e=new b,g(e,c)}return e},c.prototype.getLabels=function(){return this._labels},c.prototype.getSeries=function(){return this._series},c.prototype.getTitle=function(){return this._title},c.prototype.getType=function(){if(!this.TYPE)throw new Error("The TYPE property has not been set.");return this.TYPE},c.prototype.getXAxes=function(){return this._xaxes},c.prototype.getXAxis=function(a,b){return this.__getAxis("x",a,b)},c.prototype.getYAxes=function(){return this._yaxes},c.prototype.getYAxis=function(a,b){return this.__getAxis("y",a,b)},c.prototype._setDefaults=function(){},c.prototype.setLabels=function(a){if(a.length&&this._series.length&&this._series[0].length!=a.length)throw new Error("Series must match label values.");this._labels=a},c.prototype.setTitle=function(a){this._title=a},c.prototype.setXAxis=function(a,b){b="undefined"==typeof b?0:b,this._validateAxis("x",a,b),this._xaxes[b]=a},c.prototype.setYAxis=function(a,b){b="undefined"==typeof b?0:b,this._validateAxis("y",a,b),this._yaxes[b]=a},c.prototype._validateAxis=function(a,b,c){if(c="undefined"==typeof c?0:c,c>0){var d="x"==a?this._xaxes:this._yaxes;if("undefined"==typeof d[c-1])throw new Error("Missing "+a+" axis at index lower than "+c)}},c.prototype._validateSeries=function(a){if(this._series.length&&this._series[0].getCount()!=a.getCount())throw new Error("Series do not have an equal number of values.");if(this._labels.length&&this._labels.length!=a.getCount())throw new Error("Series must match label values.")},c}); \ No newline at end of file +define(["core/chart_series","core/chart_axis"],function(a,b){function c(){this._series=[],this._labels=[],this._xaxes=[],this._yaxes=[],this._setDefaults()}return c.prototype._series=null,c.prototype._labels=null,c.prototype._title=null,c.prototype._xaxes=null,c.prototype._yaxes=null,c.prototype.COLORSET=["#f3c300","#875692","#f38400","#a1caf1","#be0032","#c2b280","#7f180d","#008856","#e68fac","#0067a5"],c.prototype._configColorSet=null,c.prototype.TYPE=null,c.prototype.addSeries=function(a){if(this._validateSeries(a),this._series.push(a),null===a.getColor()){var b=this.getConfigColorSet()||c.prototype.COLORSET;a.setColor(b[this._series.length%b.length])}},c.prototype.create=function(c,d){var e=new c;return e.setConfigColorSet(d.config_colorset),e.setLabels(d.labels),e.setTitle(d.title),d.series.forEach(function(b){e.addSeries(a.prototype.create(b))}),d.axes.x.forEach(function(a,c){e.setXAxis(b.prototype.create(a),c)}),d.axes.y.forEach(function(a,c){e.setYAxis(b.prototype.create(a),c)}),e},c.prototype.__getAxis=function(a,c,d){var e,f="x"===a?this._xaxes:this._yaxes,g=("x"===a?this.setXAxis:this.setYAxis).bind(this);if(c="undefined"==typeof c?0:c,d="undefined"!=typeof d&&d,e=f[c],"undefined"==typeof e){if(!d)throw new Error("Unknown axis.");e=new b,g(e,c)}return e},c.prototype.getConfigColorSet=function(){return this._configColorSet},c.prototype.getLabels=function(){return this._labels},c.prototype.getSeries=function(){return this._series},c.prototype.getTitle=function(){return this._title},c.prototype.getType=function(){if(!this.TYPE)throw new Error("The TYPE property has not been set.");return this.TYPE},c.prototype.getXAxes=function(){return this._xaxes},c.prototype.getXAxis=function(a,b){return this.__getAxis("x",a,b)},c.prototype.getYAxes=function(){return this._yaxes},c.prototype.getYAxis=function(a,b){return this.__getAxis("y",a,b)},c.prototype.setConfigColorSet=function(a){this._configColorSet=a},c.prototype._setDefaults=function(){},c.prototype.setLabels=function(a){if(a.length&&this._series.length&&this._series[0].length!=a.length)throw new Error("Series must match label values.");this._labels=a},c.prototype.setTitle=function(a){this._title=a},c.prototype.setXAxis=function(a,b){b="undefined"==typeof b?0:b,this._validateAxis("x",a,b),this._xaxes[b]=a},c.prototype.setYAxis=function(a,b){b="undefined"==typeof b?0:b,this._validateAxis("y",a,b),this._yaxes[b]=a},c.prototype._validateAxis=function(a,b,c){if(c="undefined"==typeof c?0:c,c>0){var d="x"==a?this._xaxes:this._yaxes;if("undefined"==typeof d[c-1])throw new Error("Missing "+a+" axis at index lower than "+c)}},c.prototype._validateSeries=function(a){if(this._series.length&&this._series[0].getCount()!=a.getCount())throw new Error("Series do not have an equal number of values.");if(this._labels.length&&this._labels.length!=a.getCount())throw new Error("Series must match label values.")},c}); \ No newline at end of file diff --git a/lib/amd/build/chart_pie.min.js b/lib/amd/build/chart_pie.min.js index 557d2b36bdc..0d568cd5f82 100644 --- a/lib/amd/build/chart_pie.min.js +++ b/lib/amd/build/chart_pie.min.js @@ -1 +1 @@ -define(["core/chart_base"],function(a){function b(){a.prototype.constructor.apply(this,arguments)}return b.prototype=Object.create(a.prototype),b.prototype.TYPE="pie",b.prototype._doughnut=null,b.prototype.create=function(b,c){var d=a.prototype.create.apply(this,arguments);return d.setDoughnut(c.doughnut),d},b.prototype.addSeries=function(b){if(null===b.getColor()){for(var c=[],d=0;d=1)throw new Error("Pie charts only support one serie.");return a.prototype._validateSeries.apply(this,arguments)},b}); \ No newline at end of file +define(["core/chart_base"],function(a){function b(){a.prototype.constructor.apply(this,arguments)}return b.prototype=Object.create(a.prototype),b.prototype.TYPE="pie",b.prototype._doughnut=null,b.prototype.create=function(b,c){var d=a.prototype.create.apply(this,arguments);return d.setDoughnut(c.doughnut),d},b.prototype.addSeries=function(b){if(null===b.getColor()){for(var c=[],d=this.getConfigColorSet()||a.prototype.COLORSET,e=0;e=1)throw new Error("Pie charts only support one serie.");return a.prototype._validateSeries.apply(this,arguments)},b}); \ No newline at end of file diff --git a/lib/amd/src/chart_base.js b/lib/amd/src/chart_base.js index 58bfe40b372..3c718e15633 100644 --- a/lib/amd/src/chart_base.js +++ b/lib/amd/src/chart_base.js @@ -91,6 +91,14 @@ define(['core/chart_series', 'core/chart_axis'], function(Series, Axis) { Base.prototype.COLORSET = ['#f3c300', '#875692', '#f38400', '#a1caf1', '#be0032', '#c2b280', '#7f180d', '#008856', '#e68fac', '#0067a5']; + /** + * Set of colours defined by setting $CFG->chart_colorset to be picked when automatically assigning them. + * + * @type {String[]} + * @protected + */ + Base.prototype._configColorSet = null; + /** * The type of chart. * @@ -113,7 +121,8 @@ define(['core/chart_series', 'core/chart_axis'], function(Series, Axis) { // Give a default color from the set. if (series.getColor() === null) { - series.setColor(Base.prototype.COLORSET[this._series.length % Base.prototype.COLORSET.length]); + var configColorSet = this.getConfigColorSet() || Base.prototype.COLORSET; + series.setColor(configColorSet[this._series.length % configColorSet.length]); } }; @@ -132,7 +141,7 @@ define(['core/chart_series', 'core/chart_axis'], function(Series, Axis) { // TODO Not convinced about the usage of Klass here but I can't figure out a way // to have a reference to the class in the sub classes, in PHP I'd do new self(). var Chart = new Klass(); - + Chart.setConfigColorSet(data.config_colorset); Chart.setLabels(data.labels); Chart.setTitle(data.title); data.series.forEach(function(seriesData) { @@ -176,6 +185,15 @@ define(['core/chart_series', 'core/chart_axis'], function(Series, Axis) { return axis; }; + /** + * Get colours defined by setting. + * + * @return {String[]} + */ + Base.prototype.getConfigColorSet = function() { + return this._configColorSet; + }; + /** * Get the labels of the X axis. * @@ -256,6 +274,16 @@ define(['core/chart_series', 'core/chart_axis'], function(Series, Axis) { return this.__getAxis('y', index, createIfNotExists); }; + /** + * Set colours defined by setting. + * + * @param {String[]} colorset An array of css colours. + * @protected + */ + Base.prototype.setConfigColorSet = function(colorset) { + this._configColorSet = colorset; + }; + /** * Set the defaults for this chart type. * diff --git a/lib/amd/src/chart_pie.js b/lib/amd/src/chart_pie.js index bce2ef203ed..ef559c33365 100644 --- a/lib/amd/src/chart_pie.js +++ b/lib/amd/src/chart_pie.js @@ -61,8 +61,9 @@ define(['core/chart_base'], function(Base) { Pie.prototype.addSeries = function(series) { if (series.getColor() === null) { var colors = []; + var configColorSet = this.getConfigColorSet() || Base.prototype.COLORSET; for (var i = 0; i < series.getCount(); i++) { - colors.push(this.COLORSET[i % Base.prototype.COLORSET.length]); + colors.push(configColorSet[i % configColorSet.length]); } series.setColors(colors); } diff --git a/lib/classes/chart_base.php b/lib/classes/chart_base.php index 74ff7b365da..1cb3160c2ab 100644 --- a/lib/classes/chart_base.php +++ b/lib/classes/chart_base.php @@ -76,6 +76,7 @@ class chart_base implements JsonSerializable, renderable { * @return array */ public function jsonSerialize() { // @codingStandardsIgnoreLine (CONTRIB-6469). + global $CFG; return [ 'type' => $this->get_type(), 'series' => $this->series, @@ -84,7 +85,8 @@ class chart_base implements JsonSerializable, renderable { 'axes' => [ 'x' => $this->xaxes, 'y' => $this->yaxes, - ] + ], + 'config_colorset' => !empty($CFG->chart_colorset) ? $CFG->chart_colorset : null ]; }