From: Simon Brunel Date: Fri, 20 May 2016 21:42:24 +0000 (+0200) Subject: Refactor addElements and addElementAndReset X-Git-Tag: v2.1.4~2^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=381aeaa19f1233865ffd4bb33fe8b98ae3a13cbc;p=thirdparty%2FChart.js.git Refactor addElements and addElementAndReset Data controllers should now rarely implement addElements and addElementAndReset but instead should define dataElementType (and optionally datasetElementType). Also remove some dead code (e.g. numBars, colorForNewElement, etc.). --- diff --git a/src/chart.js b/src/chart.js index 6c2d8c51b..58061c2ab 100644 --- a/src/chart.js +++ b/src/chart.js @@ -13,12 +13,10 @@ require('./core/core.scaleService')(Chart); require('./core/core.title')(Chart); require('./core/core.tooltip')(Chart); -require('./controllers/controller.bar')(Chart); -require('./controllers/controller.bubble')(Chart); -require('./controllers/controller.doughnut')(Chart); -require('./controllers/controller.line')(Chart); -require('./controllers/controller.polarArea')(Chart); -require('./controllers/controller.radar')(Chart); +require('./elements/element.arc')(Chart); +require('./elements/element.line')(Chart); +require('./elements/element.point')(Chart); +require('./elements/element.rectangle')(Chart); require('./scales/scale.category')(Chart); require('./scales/scale.linear')(Chart); @@ -26,10 +24,14 @@ require('./scales/scale.logarithmic')(Chart); require('./scales/scale.radialLinear')(Chart); require('./scales/scale.time')(Chart); -require('./elements/element.arc')(Chart); -require('./elements/element.line')(Chart); -require('./elements/element.point')(Chart); -require('./elements/element.rectangle')(Chart); +// Controllers must be loaded after elements +// See Chart.core.datasetController.dataElementType +require('./controllers/controller.bar')(Chart); +require('./controllers/controller.bubble')(Chart); +require('./controllers/controller.doughnut')(Chart); +require('./controllers/controller.line')(Chart); +require('./controllers/controller.polarArea')(Chart); +require('./controllers/controller.radar')(Chart); require('./charts/Chart.Bar')(Chart); require('./charts/Chart.Bubble')(Chart); diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index 7a6915cf6..11cb32447 100644 --- a/src/controllers/controller.bar.js +++ b/src/controllers/controller.bar.js @@ -29,12 +29,16 @@ module.exports = function(Chart) { }; Chart.controllers.bar = Chart.DatasetController.extend({ + + dataElementType: Chart.elements.Rectangle, + initialize: function(chart, datasetIndex) { Chart.DatasetController.prototype.initialize.call(this, chart, datasetIndex); // Use this to indicate that this is a bar dataset. this.getMeta().bar = true; }, + // Get the number of datasets that display bars. We use this to correctly calculate the bar width getBarCount: function getBarCount() { var barCount = 0; @@ -47,40 +51,13 @@ module.exports = function(Chart) { return barCount; }, - addElements: function() { - var meta = this.getMeta(); - helpers.each(this.getDataset().data, function(value, index) { - meta.data[index] = meta.data[index] || new Chart.elements.Rectangle({ - _chart: this.chart.chart, - _datasetIndex: this.index, - _index: index - }); - }, this); - }, - - addElementAndReset: function(index) { - var rectangle = new Chart.elements.Rectangle({ - _chart: this.chart.chart, - _datasetIndex: this.index, - _index: index - }); - - var numBars = this.getBarCount(); - - // Add to the points array and reset it - this.getMeta().data.splice(index, 0, rectangle); - this.updateElement(rectangle, index, true, numBars); - }, - update: function update(reset) { - var numBars = this.getBarCount(); - helpers.each(this.getMeta().data, function(rectangle, index) { - this.updateElement(rectangle, index, reset, numBars); + this.updateElement(rectangle, index, reset); }, this); }, - updateElement: function updateElement(rectangle, index, reset, numBars) { + updateElement: function updateElement(rectangle, index, reset) { var meta = this.getMeta(); var xScale = this.getScaleForId(meta.xAxisID); var yScale = this.getScaleForId(meta.yAxisID); @@ -91,13 +68,11 @@ module.exports = function(Chart) { helpers.extend(rectangle, { // Utility - _chart: this.chart.chart, _xScale: xScale, _yScale: yScale, _datasetIndex: this.index, _index: index, - // Desired view properties _model: { x: this.calculateBarX(index, this.index), @@ -366,7 +341,6 @@ module.exports = function(Chart) { helpers.extend(rectangle, { // Utility - _chart: this.chart.chart, _xScale: xScale, _yScale: yScale, _datasetIndex: this.index, diff --git a/src/controllers/controller.bubble.js b/src/controllers/controller.bubble.js index 1faa10f65..d0f949101 100644 --- a/src/controllers/controller.bubble.js +++ b/src/controllers/controller.bubble.js @@ -37,29 +37,9 @@ module.exports = function(Chart) { } }; - Chart.controllers.bubble = Chart.DatasetController.extend({ - addElements: function() { - var meta = this.getMeta(); - helpers.each(this.getDataset().data, function(value, index) { - meta.data[index] = meta.data[index] || new Chart.elements.Point({ - _chart: this.chart.chart, - _datasetIndex: this.index, - _index: index - }); - }, this); - }, - addElementAndReset: function(index) { - var point = new Chart.elements.Point({ - _chart: this.chart.chart, - _datasetIndex: this.index, - _index: index - }); - // Add to the points array and reset it - this.getMeta().data.splice(index, 0, point); - this.updateElement(point, index, true); - }, + dataElementType: Chart.elements.Point, update: function update(reset) { var meta = this.getMeta(); @@ -69,7 +49,6 @@ module.exports = function(Chart) { helpers.each(points, function(point, index) { this.updateElement(point, index, reset); }, this); - }, updateElement: function(point, index, reset) { @@ -84,7 +63,6 @@ module.exports = function(Chart) { helpers.extend(point, { // Utility - _chart: this.chart.chart, _xScale: xScale, _yScale: yScale, _datasetIndex: this.index, @@ -115,17 +93,6 @@ module.exports = function(Chart) { return value.r || this.chart.options.elements.point.radius; }, - draw: function(ease) { - var easingDecimal = ease || 1; - - // Transition and Draw the Points - helpers.each(this.getMeta().data, function(point, index) { - point.transition(easingDecimal); - point.draw(); - }); - - }, - setHoverStyle: function(point) { // Point var dataset = this.chart.data.datasets[point._datasetIndex]; diff --git a/src/controllers/controller.doughnut.js b/src/controllers/controller.doughnut.js index 00152fb00..9f7cd2f2c 100644 --- a/src/controllers/controller.doughnut.js +++ b/src/controllers/controller.doughnut.js @@ -113,39 +113,10 @@ module.exports = function(Chart) { Chart.controllers.doughnut = Chart.controllers.pie = Chart.DatasetController.extend({ - // no scales for doughnut - linkScales: helpers.noop, - addElements: function() { - var _this = this; - var meta = this.getMeta(), - data = meta.data; - helpers.each(_this.getDataset().data, function(value, index) { - data[index] = data[index] || new Chart.elements.Arc({ - _chart: _this.chart.chart, - _datasetIndex: _this.index, - _index: index - }); - }); - }, + dataElementType: Chart.elements.Arc, - addElementAndReset: function(index, colorForNewElement) { - var _this = this; - var arc = new Chart.elements.Arc({ - _chart: _this.chart.chart, - _datasetIndex: _this.index, - _index: index - }), - ds = _this.getDataset(); - - if (colorForNewElement && helpers.isArray(ds.backgroundColor)) { - ds.backgroundColor.splice(index, 0, colorForNewElement); - } - - // Add to the points array and reset it - _this.getMeta().data.splice(index, 0, arc); - _this.updateElement(arc, index, true); - }, + linkScales: helpers.noop, // Get index of the dataset in relation to the visible datasets. This allows determining the inner and outer radius correctly getRingIndex: function getRingIndex(datasetIndex) { @@ -232,7 +203,6 @@ module.exports = function(Chart) { helpers.extend(arc, { // Utility - _chart: chart.chart, _datasetIndex: _this.index, _index: index, diff --git a/src/controllers/controller.line.js b/src/controllers/controller.line.js index 39d0fca28..4c692d5ab 100644 --- a/src/controllers/controller.line.js +++ b/src/controllers/controller.line.js @@ -24,40 +24,16 @@ module.exports = function(Chart) { }; Chart.controllers.line = Chart.DatasetController.extend({ - addElements: function() { - var me = this; - var meta = me.getMeta(); - var data = me.getDataset().data || []; - var value, i, ilen; - - meta.dataset = meta.dataset || new Chart.elements.Line({ - _chart: me.chart.chart, - _datasetIndex: me.index, - _points: meta.data - }); - - for (i=0, ilen=data.length; i