MDL-57194 core_charts: Allow colors to be overridden by setting
authorSimey Lameze <simey@moodle.com>
Wed, 30 Nov 2016 05:47:47 +0000 (13:47 +0800)
committerSimey Lameze <simey@moodle.com>
Thu, 1 Dec 2016 06:44:00 +0000 (14:44 +0800)
lib/amd/build/chart_base.min.js
lib/amd/build/chart_pie.min.js
lib/amd/src/chart_base.js
lib/amd/src/chart_pie.js
lib/classes/chart_base.php

index f78a3a3..118114d 100644 (file)
Binary files a/lib/amd/build/chart_base.min.js and b/lib/amd/build/chart_base.min.js differ
index 557d2b3..0d568cd 100644 (file)
Binary files a/lib/amd/build/chart_pie.min.js and b/lib/amd/build/chart_pie.min.js differ
index 58bfe40..3c718e1 100644 (file)
@@ -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.
      *
index bce2ef2..ef559c3 100644 (file)
@@ -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);
         }
index 74ff7b3..1cb3160 100644 (file)
@@ -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
         ];
     }