From 46de49dc9fc2d041048add6a8bea78053b9e0412 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Mon, 27 Jun 2016 18:50:41 +0800 Subject: [PATCH] MDL-55030 core: Support min and max in axes Part of MDL-54987 epic. --- lib/amd/build/chart_axis.min.js | 2 +- lib/amd/build/chart_output_chartjs.min.js | 2 +- lib/amd/src/chart_axis.js | 20 ++++++++++++++++++++ lib/amd/src/chart_output_chartjs.js | 10 ++++++++++ lib/classes/chart_axis.php | 20 ++++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/amd/build/chart_axis.min.js b/lib/amd/build/chart_axis.min.js index 6a8f59dbce2..9499e8deb6c 100644 --- a/lib/amd/build/chart_axis.min.js +++ b/lib/amd/build/chart_axis.min.js @@ -1 +1 @@ -define([],function(){function a(){}return a.prototype.POS_DEFAULT=null,a.prototype.POS_BOTTOM="bottom",a.prototype.POS_LEFT="left",a.prototype.POS_RIGHT="right",a.prototype.POS_TOP="top",a.prototype._label=null,a.prototype._position=null,a.prototype._stepSize=null,a.prototype.create=function(b){var c=new a;return c.setPosition(b.position),c.setLabel(b.label),c.setStepSize(b.stepSize),c},a.prototype.getLabel=function(){return this._label},a.prototype.getPosition=function(){return this._position},a.prototype.getStepSize=function(){return this._stepSize},a.prototype.setLabel=function(a){this._label=a||null},a.prototype.setPosition=function(a){if(a!=this.POS_DEFAULT&&a!=this.POS_BOTTOM&&a!=this.POS_LEFT&&a!=this.POS_RIGHT&&a!=this.POS_TOP)throw new Error("Invalid axis position.");this._position=a},a.prototype.setStepSize=function(a){if("undefined"==typeof a||null===a)a=null;else{if(isNaN(Number(a)))throw new Error("Value for stepSize is not a number.");a=Number(a)}this._stepSize=a},a}); \ No newline at end of file +define([],function(){function a(){}return a.prototype.POS_DEFAULT=null,a.prototype.POS_BOTTOM="bottom",a.prototype.POS_LEFT="left",a.prototype.POS_RIGHT="right",a.prototype.POS_TOP="top",a.prototype._label=null,a.prototype._max=null,a.prototype._min=null,a.prototype._position=null,a.prototype._stepSize=null,a.prototype.create=function(b){var c=new a;return c.setPosition(b.position),c.setLabel(b.label),c.setStepSize(b.stepSize),c.setMax(b.max),c.setMin(b.min),c},a.prototype.getLabel=function(){return this._label},a.prototype.getMax=function(){return this._max},a.prototype.getMin=function(){return this._min},a.prototype.getPosition=function(){return this._position},a.prototype.getStepSize=function(){return this._stepSize},a.prototype.setLabel=function(a){this._label=a||null},a.prototype.setMax=function(a){this._max="undefined"!=typeof a?a:null},a.prototype.setMin=function(a){this._min="undefined"!=typeof a?a:null},a.prototype.setPosition=function(a){if(a!=this.POS_DEFAULT&&a!=this.POS_BOTTOM&&a!=this.POS_LEFT&&a!=this.POS_RIGHT&&a!=this.POS_TOP)throw new Error("Invalid axis position.");this._position=a},a.prototype.setStepSize=function(a){if("undefined"==typeof a||null===a)a=null;else{if(isNaN(Number(a)))throw new Error("Value for stepSize is not a number.");a=Number(a)}this._stepSize=a},a}); \ No newline at end of file diff --git a/lib/amd/build/chart_output_chartjs.min.js b/lib/amd/build/chart_output_chartjs.min.js index 1bc028f7162..e9144ce899b 100644 --- a/lib/amd/build/chart_output_chartjs.min.js +++ b/lib/amd/build/chart_output_chartjs.min.js @@ -1 +1 @@ -define(["jquery","core/chartjs","core/chart_axis","core/chart_output_base"],function(a,b,c,d){function e(){d.prototype.constructor.apply(this,arguments),this._build()}return e.prototype=Object.create(d.prototype),e.prototype._config=null,e.prototype._chartjs=null,e.prototype.getDatasets=function(){var a=this._chart.getSeries().map(function(a){return{label:a.getLabel(),data:a.getValues(),type:a.getType(),fill:!1,borderColor:a.getColor(),backgroundColor:a.getColor()}});return a},e.prototype._build=function(){this._config=this._makeConfig(),this._chartjs=new b(this._node[0],this._config)},e.prototype._makeAxisConfig=function(a){var b={};return a.getPosition()!==c.prototype.POS_DEFAULT&&(b.position=a.getPosition()),null!==a.getLabel()&&(b.scaleLabel={display:!0,labelString:a.getLabel()}),null!==a.getStepSize()&&(b.ticks=b.ticks||{},b.ticks.stepSize=a.getStepSize()),b},e.prototype._makeConfig=function(){var a={type:this._chart.getType(),data:{labels:this._chart.getLabels(),datasets:this.getDatasets()},options:{title:{display:null!==this._chart.getTitle(),text:this._chart.getTitle()}}};return this._chart.getXAxes().forEach(function(b,c){a.options.scales=a.options.scales||{},a.options.scales.xAxes=a.options.scales.xAxes||[],a.options.scales.xAxes[c]=this._makeAxisConfig(b)}.bind(this)),this._chart.getYAxes().forEach(function(b,c){a.options.scales=a.options.scales||{},a.options.scales.yAxes=a.options.scales.yAxes||[],a.options.scales.yAxes[c]=this._makeAxisConfig(b)}.bind(this)),a},e.prototype.update=function(){a.extend(!0,this._config,this._makeConfig()),this._chartjs.update()},e}); \ No newline at end of file +define(["jquery","core/chartjs","core/chart_axis","core/chart_output_base"],function(a,b,c,d){function e(){d.prototype.constructor.apply(this,arguments),this._build()}return e.prototype=Object.create(d.prototype),e.prototype._config=null,e.prototype._chartjs=null,e.prototype.getDatasets=function(){var a=this._chart.getSeries().map(function(a){return{label:a.getLabel(),data:a.getValues(),type:a.getType(),fill:!1,borderColor:a.getColor(),backgroundColor:a.getColor()}});return a},e.prototype._build=function(){this._config=this._makeConfig(),this._chartjs=new b(this._node[0],this._config)},e.prototype._makeAxisConfig=function(a){var b={};return a.getPosition()!==c.prototype.POS_DEFAULT&&(b.position=a.getPosition()),null!==a.getLabel()&&(b.scaleLabel={display:!0,labelString:a.getLabel()}),null!==a.getStepSize()&&(b.ticks=b.ticks||{},b.ticks.stepSize=a.getStepSize()),null!==a.getMax()&&(b.ticks=b.ticks||{},b.ticks.max=a.getMax()),null!==a.getMin()&&(b.ticks=b.ticks||{},b.ticks.min=a.getMin()),b},e.prototype._makeConfig=function(){var a={type:this._chart.getType(),data:{labels:this._chart.getLabels(),datasets:this.getDatasets()},options:{title:{display:null!==this._chart.getTitle(),text:this._chart.getTitle()}}};return this._chart.getXAxes().forEach(function(b,c){a.options.scales=a.options.scales||{},a.options.scales.xAxes=a.options.scales.xAxes||[],a.options.scales.xAxes[c]=this._makeAxisConfig(b)}.bind(this)),this._chart.getYAxes().forEach(function(b,c){a.options.scales=a.options.scales||{},a.options.scales.yAxes=a.options.scales.yAxes||[],a.options.scales.yAxes[c]=this._makeAxisConfig(b)}.bind(this)),a},e.prototype.update=function(){a.extend(!0,this._config,this._makeConfig()),this._chartjs.update()},e}); \ No newline at end of file diff --git a/lib/amd/src/chart_axis.js b/lib/amd/src/chart_axis.js index ee5d4596fac..53a2b756cc5 100644 --- a/lib/amd/src/chart_axis.js +++ b/lib/amd/src/chart_axis.js @@ -36,6 +36,8 @@ define([], function() { Axis.prototype.POS_TOP = 'top'; Axis.prototype._label = null; + Axis.prototype._max = null; + Axis.prototype._min = null; Axis.prototype._position = null; Axis.prototype._stepSize = null; @@ -44,6 +46,8 @@ define([], function() { s.setPosition(obj.position); s.setLabel(obj.label); s.setStepSize(obj.stepSize); + s.setMax(obj.max); + s.setMin(obj.min); return s; }; @@ -51,6 +55,14 @@ define([], function() { return this._label; }; + Axis.prototype.getMax = function() { + return this._max; + }; + + Axis.prototype.getMin = function() { + return this._min; + }; + Axis.prototype.getPosition = function() { return this._position; }; @@ -63,6 +75,14 @@ define([], function() { this._label = label || null; }; + Axis.prototype.setMax = function(max) { + this._max = typeof max !== 'undefined' ? max : null; + }; + + Axis.prototype.setMin = function(min) { + this._min = typeof min !== 'undefined' ? min : null; + }; + Axis.prototype.setPosition = function(position) { if (position != this.POS_DEFAULT && position != this.POS_BOTTOM diff --git a/lib/amd/src/chart_output_chartjs.js b/lib/amd/src/chart_output_chartjs.js index 9b0e00bb4e2..f2a5c538825 100644 --- a/lib/amd/src/chart_output_chartjs.js +++ b/lib/amd/src/chart_output_chartjs.js @@ -78,6 +78,16 @@ define([ scaleData.ticks.stepSize = axis.getStepSize(); } + if (axis.getMax() !== null) { + scaleData.ticks = scaleData.ticks || {}; + scaleData.ticks.max = axis.getMax(); + } + + if (axis.getMin() !== null) { + scaleData.ticks = scaleData.ticks || {}; + scaleData.ticks.min = axis.getMin(); + } + return scaleData; }; diff --git a/lib/classes/chart_axis.php b/lib/classes/chart_axis.php index f151aeeace6..3ea13426aec 100644 --- a/lib/classes/chart_axis.php +++ b/lib/classes/chart_axis.php @@ -45,6 +45,8 @@ class chart_axis implements JsonSerializable { const POS_TOP = 'top'; protected $label = null; + protected $max = null; + protected $min = null; protected $position = self::POS_DEFAULT; protected $stepsize = null; @@ -55,6 +57,14 @@ class chart_axis implements JsonSerializable { return $this->label; } + public function get_max() { + return $this->max; + } + + public function get_min() { + return $this->min; + } + public function get_position() { return $this->position; } @@ -66,6 +76,8 @@ class chart_axis implements JsonSerializable { public function jsonSerialize() { return [ 'label' => $this->label, + 'max' => $this->max, + 'min' => $this->min, 'position' => $this->position, 'stepSize' => $this->stepsize, ]; @@ -75,6 +87,14 @@ class chart_axis implements JsonSerializable { return $this->label = $label; } + public function set_max($max) { + return $this->max = $max; + } + + public function set_min($min) { + return $this->min = $min; + } + public function set_position($position) { return $this->position = $position; }