mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-57194 core_charts: Allow colors to be overridden by setting
This commit is contained in:
parent
a0352aa95e
commit
7d17256a75
2
lib/amd/build/chart_base.min.js
vendored
2
lib/amd/build/chart_base.min.js
vendored
@ -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});
|
||||
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});
|
2
lib/amd/build/chart_pie.min.js
vendored
2
lib/amd/build/chart_pie.min.js
vendored
@ -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<b.getCount();d++)c.push(this.COLORSET[d%a.prototype.COLORSET.length]);b.setColors(c)}return a.prototype.addSeries.apply(this,arguments)},b.prototype.getDoughnut=function(){return this._doughnut},b.prototype.setDoughnut=function(a){this._doughnut=Boolean(a)},b.prototype._validateSeries=function(){if(this._series.length>=1)throw new Error("Pie charts only support one serie.");return a.prototype._validateSeries.apply(this,arguments)},b});
|
||||
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<b.getCount();e++)c.push(d[e%d.length]);b.setColors(c)}return a.prototype.addSeries.apply(this,arguments)},b.prototype.getDoughnut=function(){return this._doughnut},b.prototype.setDoughnut=function(a){this._doughnut=Boolean(a)},b.prototype._validateSeries=function(){if(this._series.length>=1)throw new Error("Pie charts only support one serie.");return a.prototype._validateSeries.apply(this,arguments)},b});
|
@ -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.
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user