MDL-55665 core: Add support for stacked bar charts
authorSimey Lameze <simey@moodle.com>
Tue, 23 Aug 2016 07:17:37 +0000 (15:17 +0800)
committerSimey Lameze <simey@moodle.com>
Thu, 25 Aug 2016 02:37:20 +0000 (10:37 +0800)
lib/amd/build/chart_bar.min.js
lib/amd/build/chart_output_chartjs.min.js
lib/amd/src/chart_bar.js
lib/amd/src/chart_output_chartjs.js
lib/classes/chart_bar.php

index 05364fd..b98a825 100644 (file)
Binary files a/lib/amd/build/chart_bar.min.js and b/lib/amd/build/chart_bar.min.js differ
index c035888..62ae60a 100644 (file)
Binary files a/lib/amd/build/chart_output_chartjs.min.js and b/lib/amd/build/chart_output_chartjs.min.js differ
index 13c5596..f4eb2b6 100644 (file)
@@ -43,6 +43,14 @@ define(['core/chart_base'], function(Base) {
      */
     Bar.prototype._horizontal = false;
 
+    /**
+     * Whether the bars should be stacked or not.
+     *
+     * @type {Bool}
+     * @protected
+     */
+    Bar.prototype._stacked = null;
+
     /** @override */
     Bar.prototype.TYPE = 'bar';
 
@@ -50,6 +58,7 @@ define(['core/chart_base'], function(Base) {
     Bar.prototype.create = function(Klass, data) {
         var chart = Base.prototype.create.apply(this, arguments);
         chart.setHorizontal(data.horizontal);
+        chart.setStacked(data.stacked);
         return chart;
     };
 
@@ -69,6 +78,15 @@ define(['core/chart_base'], function(Base) {
         return this._horizontal;
     };
 
+    /**
+     * Get whether the bars should be stacked or not.
+     *
+     * @returns {Bool}
+     */
+    Bar.prototype.getStacked = function() {
+        return this._stacked;
+    };
+
     /**
      * Set whether the bars should be displayed horizontally or not.
      *
@@ -84,6 +102,16 @@ define(['core/chart_base'], function(Base) {
         this._horizontal = Boolean(horizontal);
     };
 
+    /**
+     * Set whether the bars should be stacked or not.
+     *
+     * @method setStacked
+     * @param {Bool} stacked True if the chart should be stacked or false otherwise.
+     */
+    Bar.prototype.setStacked = function(stacked) {
+        this._stacked = Boolean(stacked);
+    };
+
     return Bar;
 
 });
index d738479..dee3915 100644 (file)
@@ -195,6 +195,7 @@ define([
                     return axisLabels[index] || '';
                 };
             }
+            config.options.scales.xAxes[i].stacked = this._isStacked('x');
         }.bind(this));
 
         this._chart.getYAxes().forEach(function(axis, i) {
@@ -209,6 +210,7 @@ define([
                     return axisLabels[parseInt(value, 10)] || '';
                 };
             }
+            config.options.scales.yAxes[i].stacked = this._isStacked('y');
         }.bind(this));
 
         config.options.tooltips = {
@@ -301,6 +303,27 @@ define([
         return smooth;
     };
 
+    /**
+     * Verify if the bar chart is stacked or not.
+     *
+     * @protected
+     * @param {String} xy The axis of the serie.
+     * @returns {Bool}
+     */
+    Output.prototype._isStacked = function(xy) {
+        var stacked = false;
+        var chartType = this._getChartType();
+
+        // Check if the axis matches the chart type to avoid set stacked on a unused axis.
+        if (chartType === Bar.prototype.TYPE && xy == 'x') {
+            stacked = this._chart.getStacked();
+        } else if (chartType === 'horizontalBar' && xy == 'y') {
+            stacked = this._chart.getStacked();
+        }
+
+        return stacked;
+    };
+
     /** @override */
     Output.prototype.update = function() {
         $.extend(true, this._config, this._makeConfig());
index ace3a81..034d274 100644 (file)
@@ -36,7 +36,8 @@ class chart_bar extends chart_base {
 
     /** @var bool Whether the bars should be displayed horizontally or not. */
     protected $horizontal = false;
-
+    /** @var bool Whether the chart should be stacked or not. */
+    protected $stacked = null;
     /**
      * Add the horizontal to the parent and return the serialized data.
      *
@@ -45,6 +46,7 @@ class chart_bar extends chart_base {
     public function jsonSerialize() { // @codingStandardsIgnoreLine (CONTRIB-6469).
         $data = parent::jsonSerialize();
         $data['horizontal'] = $this->get_horizontal();
+        $data['stacked'] = $this->get_stacked();
         return $data;
     }
 
@@ -67,11 +69,29 @@ class chart_bar extends chart_base {
     }
 
     /**
-     * Get Set whether the bars should be displayed horizontally or not.
+     * Get whether the bars should be stacked or not.
+     *
+     * @return bool
+     */
+    public function get_stacked() {
+        return $this->stacked;
+    }
+
+    /**
+     * Set whether the bars should be displayed horizontally or not.
      *
      * @param bool $horizontal True if the bars should be displayed horizontally, false otherwise.
      */
     public function set_horizontal($horizontal) {
         $this->horizontal = $horizontal;
     }
+
+    /**
+     * Set whether the bars should be stacked or not.
+     *
+     * @param bool $stacked True if the chart should be stacked or false otherwise.
+     */
+    public function set_stacked($stacked) {
+        $this->stacked = $stacked;
+    }
 }