From 826ade18f1c4d2cb4ae958c18dbd014de7b90d71 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Tue, 28 Jun 2016 12:21:37 +0800 Subject: [PATCH] MDL-55030 core: Call class setter when creating a new axis Part of MDL-54987 epic. --- lib/amd/build/chart_base.min.js | 2 +- lib/amd/src/chart_base.js | 3 ++- lib/classes/chart_base.php | 19 +++++++++++++------ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/amd/build/chart_base.min.js b/lib/amd/build/chart_base.min.js index 375f189bbc6..f99005ff1f6 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=["red","green","blue","yellow","pink","orange"],c.prototype.TYPE=null,c.prototype.addSeries=function(a){this._validateSerie(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;if(c="undefined"==typeof c?0:c,d="undefined"==typeof d?!1:d,e=f[c],"undefined"==typeof e){if(!d)throw new Error("Unknown axis.");e=new b,f[c]=e}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._xaxes[b]=a},c.prototype.setYAxis=function(a,b){b="undefined"==typeof b?0:b,this._yaxes[b]=a},c.prototype._validateSerie=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=["red","green","blue","yellow","pink","orange"],c.prototype.TYPE=null,c.prototype.addSeries=function(a){this._validateSerie(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?!1: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._xaxes[b]=a},c.prototype.setYAxis=function(a,b){b="undefined"==typeof b?0:b,this._yaxes[b]=a},c.prototype._validateSerie=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/src/chart_base.js b/lib/amd/src/chart_base.js index 74da8635c85..1c9fc026d52 100644 --- a/lib/amd/src/chart_base.js +++ b/lib/amd/src/chart_base.js @@ -73,6 +73,7 @@ define(['core/chart_series', 'core/chart_axis'], function(Series, Axis) { Base.prototype.__getAxis = function(xy, index, createIfNotExists) { var axes = xy === 'x' ? this._xaxes : this._yaxes, + setAxis = (xy === 'x' ? this.setXAxis : this.setYAxis).bind(this), axis; index = typeof index === 'undefined' ? 0 : index; @@ -84,7 +85,7 @@ define(['core/chart_series', 'core/chart_axis'], function(Series, Axis) { throw new Error('Unknown axis.'); } axis = new Axis(); - axes[index] = axis; + setAxis(axis, index); } return axis; diff --git a/lib/classes/chart_base.php b/lib/classes/chart_base.php index 6abbcf592b0..ce0967fa1bd 100644 --- a/lib/classes/chart_base.php +++ b/lib/classes/chart_base.php @@ -66,20 +66,27 @@ class chart_base implements JsonSerializable, renderable { } private function get_axis($type, $index, $createifnotexists) { - if ($type === 'x') { - $axes = &$this->xaxes; + $isx = $type === 'x'; + if ($isx) { + $axis = isset($this->xaxes[$index]) ? $this->xaxes[$index] : null; } else { - $axes = &$this->yaxes; + $axis = isset($this->yaxes[$index]) ? $this->yaxes[$index] : null; } - if (!isset($axes[$index])) { + if ($axis === null) { if (!$createifnotexists) { throw new coding_exception('Unknown axis.'); } - $axes[$index] = new chart_axis(); + + $axis = new chart_axis(); + if ($isx) { + $this->set_xaxis($axis, $index); + } else { + $this->set_yaxis($axis, $index); + } } - return $axes[$index]; + return $axis; } public function get_labels() {