MDL-55665 core: Add support for stacked bar charts

This commit is contained in:
Simey Lameze 2016-08-23 15:17:37 +08:00
parent 35d5053ba2
commit c574995dde
5 changed files with 75 additions and 4 deletions

View File

@ -1 +1 @@
define(["core/chart_base"],function(a){function b(){a.prototype.constructor.apply(this,arguments)}return b.prototype=Object.create(a.prototype),b.prototype._horizontal=!1,b.prototype.TYPE="bar",b.prototype.create=function(b,c){var d=a.prototype.create.apply(this,arguments);return d.setHorizontal(c.horizontal),d},b.prototype._setDefaults=function(){a.prototype._setDefaults.apply(this,arguments);var b=this.getYAxis(0,!0);b.setMin(0)},b.prototype.getHorizontal=function(){return this._horizontal},b.prototype.setHorizontal=function(a){var b=this.getXAxis(0,!0);null===b.getMin()&&b.setMin(0),this._horizontal=Boolean(a)},b});
define(["core/chart_base"],function(a){function b(){a.prototype.constructor.apply(this,arguments)}return b.prototype=Object.create(a.prototype),b.prototype._horizontal=!1,b.prototype._stacked=null,b.prototype.TYPE="bar",b.prototype.create=function(b,c){var d=a.prototype.create.apply(this,arguments);return d.setHorizontal(c.horizontal),d.setStacked(c.stacked),d},b.prototype._setDefaults=function(){a.prototype._setDefaults.apply(this,arguments);var b=this.getYAxis(0,!0);b.setMin(0)},b.prototype.getHorizontal=function(){return this._horizontal},b.prototype.getStacked=function(){return this._stacked},b.prototype.setHorizontal=function(a){var b=this.getXAxis(0,!0);null===b.getMin()&&b.setMin(0),this._horizontal=Boolean(a)},b.prototype.setStacked=function(a){this._stacked=Boolean(a)},b});

View File

@ -1 +1 @@
define(["jquery","core/chartjs","core/chart_axis","core/chart_bar","core/chart_output_base","core/chart_line","core/chart_pie","core/chart_series"],function(a,b,c,d,e,f,g,h){function i(){e.prototype.constructor.apply(this,arguments),this._canvas=this._node,"CANVAS"!=this._canvas.prop("tagName")&&(this._canvas=a("<canvas>"),this._node.append(this._canvas)),this._build()}var j=function(a,b){return"axis-"+a+"-"+b};return i.prototype=Object.create(e.prototype),i.prototype._config=null,i.prototype._chartjs=null,i.prototype._canvas=null,i.prototype._build=function(){this._config=this._makeConfig(),this._chartjs=new b(this._canvas[0],this._config)},i.prototype._getChartType=function(){var a=this._chart.getType();return this._chart.getType()===d.prototype.TYPE&&this._chart.getHorizontal()===!0&&(a="horizontalBar"),a},i.prototype._makeAxisConfig=function(a,b,d){var e={id:j(b,d)};return a.getPosition()!==c.prototype.POS_DEFAULT&&(e.position=a.getPosition()),null!==a.getLabel()&&(e.scaleLabel={display:!0,labelString:a.getLabel()}),null!==a.getStepSize()&&(e.ticks=e.ticks||{},e.ticks.stepSize=a.getStepSize()),null!==a.getMax()&&(e.ticks=e.ticks||{},e.ticks.max=a.getMax()),null!==a.getMin()&&(e.ticks=e.ticks||{},e.ticks.min=a.getMin()),e},i.prototype._makeConfig=function(){var a={type:this._getChartType(),data:{labels:this._chart.getLabels(),datasets:this._makeDatasetsConfig()},options:{title:{display:null!==this._chart.getTitle(),text:this._chart.getTitle()}}};return this._chart.getXAxes().forEach(function(b,c){var d=b.getLabels();a.options.scales=a.options.scales||{},a.options.scales.xAxes=a.options.scales.xAxes||[],a.options.scales.xAxes[c]=this._makeAxisConfig(b,"x",c),null!==d&&(a.options.scales.xAxes[c].ticks.callback=function(a,b){return d[b]||""})}.bind(this)),this._chart.getYAxes().forEach(function(b,c){var d=b.getLabels();a.options.scales=a.options.scales||{},a.options.scales.yAxes=a.options.scales.yAxes||[],a.options.scales.yAxes[c]=this._makeAxisConfig(b,"y",c),null!==d&&(a.options.scales.yAxes[c].ticks.callback=function(a){return d[parseInt(a,10)]||""})}.bind(this)),a.options.tooltips={callbacks:{label:this._makeTooltip.bind(this)}},a},i.prototype._makeDatasetsConfig=function(){var a=this._chart.getSeries().map(function(a){var b=a.hasColoredValues()?a.getColors():a.getColor(),c={label:a.getLabel(),data:a.getValues(),type:a.getType(),fill:!1,backgroundColor:b,borderColor:this._chart.getType()==g.prototype.TYPE?null:b,lineTension:this._isSmooth(a)?.3:0};return null!==a.getXAxis()&&(c.xAxisID=j("x",a.getXAxis())),null!==a.getYAxis()&&(c.yAxisID=j("y",a.getYAxis())),c}.bind(this));return a},i.prototype._makeTooltip=function(a,b){var c=this._chart.getSeries()[a.datasetIndex],d=c.getLabel(),e=c.getLabels(),f=b.datasets[a.datasetIndex].data,g=f[a.index],h=d+": "+g;return null!==e&&(h=e[a.index]),h},i.prototype._isSmooth=function(a){var b=!1;return this._chart.getType()===f.prototype.TYPE?(b=a.getSmooth(),null===b&&(b=this._chart.getSmooth())):a.getType()===h.prototype.TYPE_LINE&&(b=a.getSmooth()),b},i.prototype.update=function(){a.extend(!0,this._config,this._makeConfig()),this._chartjs.update()},i});
define(["jquery","core/chartjs","core/chart_axis","core/chart_bar","core/chart_output_base","core/chart_line","core/chart_pie","core/chart_series"],function(a,b,c,d,e,f,g,h){function i(){e.prototype.constructor.apply(this,arguments),this._canvas=this._node,"CANVAS"!=this._canvas.prop("tagName")&&(this._canvas=a("<canvas>"),this._node.append(this._canvas)),this._build()}var j=function(a,b){return"axis-"+a+"-"+b};return i.prototype=Object.create(e.prototype),i.prototype._config=null,i.prototype._chartjs=null,i.prototype._canvas=null,i.prototype._build=function(){this._config=this._makeConfig(),this._chartjs=new b(this._canvas[0],this._config)},i.prototype._getChartType=function(){var a=this._chart.getType();return this._chart.getType()===d.prototype.TYPE&&this._chart.getHorizontal()===!0&&(a="horizontalBar"),a},i.prototype._makeAxisConfig=function(a,b,d){var e={id:j(b,d)};return a.getPosition()!==c.prototype.POS_DEFAULT&&(e.position=a.getPosition()),null!==a.getLabel()&&(e.scaleLabel={display:!0,labelString:a.getLabel()}),null!==a.getStepSize()&&(e.ticks=e.ticks||{},e.ticks.stepSize=a.getStepSize()),null!==a.getMax()&&(e.ticks=e.ticks||{},e.ticks.max=a.getMax()),null!==a.getMin()&&(e.ticks=e.ticks||{},e.ticks.min=a.getMin()),e},i.prototype._makeConfig=function(){var a={type:this._getChartType(),data:{labels:this._chart.getLabels(),datasets:this._makeDatasetsConfig()},options:{title:{display:null!==this._chart.getTitle(),text:this._chart.getTitle()}}};return this._chart.getXAxes().forEach(function(b,c){var d=b.getLabels();a.options.scales=a.options.scales||{},a.options.scales.xAxes=a.options.scales.xAxes||[],a.options.scales.xAxes[c]=this._makeAxisConfig(b,"x",c),null!==d&&(a.options.scales.xAxes[c].ticks.callback=function(a,b){return d[b]||""}),a.options.scales.xAxes[c].stacked=this._isStacked("x")}.bind(this)),this._chart.getYAxes().forEach(function(b,c){var d=b.getLabels();a.options.scales=a.options.scales||{},a.options.scales.yAxes=a.options.scales.yAxes||[],a.options.scales.yAxes[c]=this._makeAxisConfig(b,"y",c),null!==d&&(a.options.scales.yAxes[c].ticks.callback=function(a){return d[parseInt(a,10)]||""}),a.options.scales.yAxes[c].stacked=this._isStacked("y")}.bind(this)),a.options.tooltips={callbacks:{label:this._makeTooltip.bind(this)}},a},i.prototype._makeDatasetsConfig=function(){var a=this._chart.getSeries().map(function(a){var b=a.hasColoredValues()?a.getColors():a.getColor(),c={label:a.getLabel(),data:a.getValues(),type:a.getType(),fill:!1,backgroundColor:b,borderColor:this._chart.getType()==g.prototype.TYPE?null:b,lineTension:this._isSmooth(a)?.3:0};return null!==a.getXAxis()&&(c.xAxisID=j("x",a.getXAxis())),null!==a.getYAxis()&&(c.yAxisID=j("y",a.getYAxis())),c}.bind(this));return a},i.prototype._makeTooltip=function(a,b){var c=this._chart.getSeries()[a.datasetIndex],d=c.getLabel(),e=c.getLabels(),f=b.datasets[a.datasetIndex].data,g=f[a.index],h=d+": "+g;return null!==e&&(h=e[a.index]),h},i.prototype._isSmooth=function(a){var b=!1;return this._chart.getType()===f.prototype.TYPE?(b=a.getSmooth(),null===b&&(b=this._chart.getSmooth())):a.getType()===h.prototype.TYPE_LINE&&(b=a.getSmooth()),b},i.prototype._isStacked=function(a){var b=!1,c=this._getChartType();return c===d.prototype.TYPE&&"x"==a?b=this._chart.getStacked():"horizontalBar"===c&&"y"==a&&(b=this._chart.getStacked()),b},i.prototype.update=function(){a.extend(!0,this._config,this._makeConfig()),this._chartjs.update()},i});

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

View 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());

View 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;
}
}