MDL-55030 core: Call class setter when creating a new axis

Part of MDL-54987 epic.
This commit is contained in:
Frederic Massart 2016-06-28 12:21:37 +08:00 committed by Dan Poltawski
parent a6c6faefb7
commit 826ade18f1
3 changed files with 16 additions and 8 deletions

View File

@ -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});
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});

View File

@ -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;

View File

@ -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() {