]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Refactor addElements and addElementAndReset
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 20 May 2016 21:42:24 +0000 (23:42 +0200)
committerEvert Timberg <evert.timberg+github@gmail.com>
Wed, 25 May 2016 22:39:42 +0000 (18:39 -0400)
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.).

src/chart.js
src/controllers/controller.bar.js
src/controllers/controller.bubble.js
src/controllers/controller.doughnut.js
src/controllers/controller.line.js
src/controllers/controller.polarArea.js
src/controllers/controller.radar.js
src/core/core.datasetController.js

index 6c2d8c51bb87d7b4f397071626f2d6d5e23c4041..58061c2ab6c2f6eb4669f74d8837fef239d79c61 100644 (file)
@@ -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);
index 7a6915cf6e8d1237ad6262b7b72fcaf36ce32eef..11cb32447e3ac8bc4e96ff4bee7ca213ff91534e 100644 (file)
@@ -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,
index 1faa10f65c4397822d2640fc95c8100c08c2d5ad..d0f9491018fcad25531321aad545af00c3b9e7ce 100644 (file)
@@ -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];
index 00152fb00e141730a7af5ed4b141b6de9f38f724..9f7cd2f2c8d1babde5f74f0fb68836c1f557c74a 100644 (file)
@@ -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,
 
index 39d0fca28d30bcf8c35911ec5d3c363b7b545e36..4c692d5aba1ead96c33b16d0518840484e694db8 100644 (file)
@@ -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<ilen; ++i) {
-                               value = data[i];
-                               meta.data[i] = meta.data[i] || new Chart.elements.Point({
-                                       _chart: me.chart.chart,
-                                       _datasetIndex: me.index,
-                                       _index: i
-                               });
-                       }
-               },
+
+               datasetElementType: Chart.elements.Line,
+
+               dataElementType: Chart.elements.Point,
 
                addElementAndReset: function(index) {
                        var me = this;
                        var options = me.chart.options;
-                       var point = new Chart.elements.Point({
-                               _chart: me.chart.chart,
-                               _datasetIndex: me.index,
-                               _index: index
-                       });
 
-                       // Add to the points array and reset it
-                       me.getMeta().data.splice(index, 0, point);
-                       me.updateElement(point, index, true);
+                       Chart.DatasetController.prototype.addElementAndReset.call(me, index);
 
                        // Make sure bezier control points are updated
                        if (options.showLines && options.elements.line.tension !== 0) {
@@ -193,7 +169,6 @@ module.exports = function(Chart) {
                        y = reset ? yScale.getBasePixel() : me.calculatePointY(value, index, datasetIndex, me.chart.isCombo);
 
                        // Utility
-                       point._chart = me.chart.chart;
                        point._xScale = xScale;
                        point._yScale = yScale;
                        point._datasetIndex = datasetIndex;
index 1523cfb1d7389fe8ba019930ccd8f8a9be09a4ac..372ede3f3a0beb131ea15eba96fc97e0930dc2ea 100644 (file)
@@ -100,33 +100,10 @@ module.exports = function(Chart) {
        };
 
        Chart.controllers.polarArea = Chart.DatasetController.extend({
-               linkScales: helpers.noop,
 
-               addElements: function() {
-                       var _this = this;
-                       var meta = this.getMeta();
-                       var 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) {
-                       var _this = this;
-                       var arc = new Chart.elements.Arc({
-                               _chart: _this.chart.chart,
-                               _datasetIndex: _this.index,
-                               _index: index
-                       });
-
-                       // Add to the points array and reset it
-                       _this.getMeta().data.splice(index, 0, arc);
-                       _this.updateElement(arc, index, true);
-               },
+               linkScales: helpers.noop,
 
                update: function update(reset) {
                        var _this = this;
@@ -198,7 +175,6 @@ module.exports = function(Chart) {
 
                        helpers.extend(arc, {
                                // Utility
-                               _chart: chart.chart,
                                _datasetIndex: _this.index,
                                _index: index,
                                _scale: scale,
index 4e1a5c48db11f80c6f03c77cfff137fc4157957f..6dcd976ce7af801394402be77141767a090db6db 100644 (file)
@@ -4,7 +4,6 @@ module.exports = function(Chart) {
 
        var helpers = Chart.helpers;
 
-
        Chart.defaults.radar = {
                scale: {
                        type: "radialLinear"
@@ -17,42 +16,15 @@ module.exports = function(Chart) {
        };
 
        Chart.controllers.radar = Chart.DatasetController.extend({
-               linkScales: function() {
-                       // No need. Single scale only
-               },
 
-               addElements: function() {
-                       var meta = this.getMeta();
+               datasetElementType: Chart.elements.Line,
 
-                       meta.dataset = meta.dataset || new Chart.elements.Line({
-                               _chart: this.chart.chart,
-                               _datasetIndex: this.index,
-                               _points: meta.data,
-                               _loop: true
-                       });
+               dataElementType: Chart.elements.Point,
 
-                       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,
-                                       _model: {
-                                               x: 0, //xScale.getPixelForValue(null, index, true),
-                                               y: 0 //this.chartArea.bottom,
-                                       }
-                               });
-                       }, this);
-               },
-               addElementAndReset: function(index) {
-                       var point = new Chart.elements.Point({
-                               _chart: this.chart.chart,
-                               _datasetIndex: this.index,
-                               _index: index
-                       });
+               linkScales: helpers.noop,
 
-                       // Add to the points array and reset it
-                       this.getMeta().data.splice(index, 0, point);
-                       this.updateElement(point, index, true);
+               addElementAndReset: function(index) {
+                       Chart.DatasetController.prototype.addElementAndReset.call(this, index);
 
                        // Make sure bezier control points are updated
                        this.updateBezierControlPoints();
@@ -77,6 +49,7 @@ module.exports = function(Chart) {
                                _datasetIndex: this.index,
                                // Data
                                _children: points,
+                               _loop: true,
                                // Model
                                _model: {
                                        // Appearance
index 835d0858fa9a0ea19265e195ae7f309c44659fb8..f4d213eca533002aee91acfff5f1b2760b7c6348 100644 (file)
@@ -11,12 +11,26 @@ module.exports = function(Chart) {
        };
 
        helpers.extend(Chart.DatasetController.prototype, {
+
+               /**
+                * Element type used to generate a meta dataset (e.g. Chart.element.Line).
+                * @type {Chart.core.element}
+                */
+               datasetElementType: null,
+
+               /**
+                * Element type used to generate a meta data (e.g. Chart.element.Point).
+                * @type {Chart.core.element}
+                */
+               dataElementType: null,
+
                initialize: function(chart, datasetIndex) {
                        this.chart = chart;
                        this.index = datasetIndex;
                        this.linkScales();
                        this.addElements();
                },
+
                updateIndex: function(datasetIndex) {
                        this.index = datasetIndex;
                },
@@ -49,6 +63,46 @@ module.exports = function(Chart) {
                        this.update(true);
                },
 
+               createMetaDataset: function() {
+                       var me = this;
+                       var type = me.datasetElementType;
+                       return type && new type({
+                               _chart: me.chart.chart,
+                               _datasetIndex: me.index
+                       });
+               },
+
+               createMetaData: function(index) {
+                       var me = this;
+                       var type = me.dataElementType;
+                       return type && new type({
+                               _chart: me.chart.chart,
+                               _datasetIndex: me.index,
+                               _index: index
+                       });
+               },
+
+               addElements: function() {
+                       var me = this;
+                       var meta = me.getMeta();
+                       var data = me.getDataset().data || [];
+                       var metaData = meta.data;
+                       var i, ilen;
+
+                       for (i=0, ilen=data.length; i<ilen; ++i) {
+                               metaData[i] = metaData[i] || me.createMetaData(meta, i);
+                       }
+
+                       meta.dataset = meta.dataset || me.createMetaDataset();
+               },
+
+               addElementAndReset: function(index) {
+                       var me = this;
+                       var element = me.createMetaData(index);
+                       me.getMeta().data.splice(index, 0, element);
+                       me.updateElement(element, index, true);
+               },
+
                buildOrUpdateElements: function buildOrUpdateElements() {
                        // Handle the number of data points changing
                        var meta = this.getMeta(),
@@ -68,15 +122,15 @@ module.exports = function(Chart) {
                        }
                },
 
-               // Controllers should implement the following
-               addElements: noop,
-               addElementAndReset: noop,
+               update: noop,
+
                draw: function(ease) {
                        var easingDecimal = ease || 1;
                        helpers.each(this.getMeta().data, function(element, index) {
                                element.transition(easingDecimal).draw();
                        });
                },
+
                removeHoverStyle: function(element, elementOpts) {
                        var dataset = this.chart.data.datasets[element._datasetIndex],
                                index = element._index,
@@ -89,6 +143,7 @@ module.exports = function(Chart) {
                        model.borderColor = custom.borderColor ? custom.borderColor : valueOrDefault(dataset.borderColor, index, elementOpts.borderColor);
                        model.borderWidth = custom.borderWidth ? custom.borderWidth : valueOrDefault(dataset.borderWidth, index, elementOpts.borderWidth);
                },
+
                setHoverStyle: function(element) {
                        var dataset = this.chart.data.datasets[element._datasetIndex],
                                index = element._index,
@@ -101,8 +156,7 @@ module.exports = function(Chart) {
                        model.backgroundColor = custom.hoverBackgroundColor ? custom.hoverBackgroundColor : valueOrDefault(dataset.hoverBackgroundColor, index, getHoverColor(model.backgroundColor));
                        model.borderColor = custom.hoverBorderColor ? custom.hoverBorderColor : valueOrDefault(dataset.hoverBorderColor, index, getHoverColor(model.borderColor));
                        model.borderWidth = custom.hoverBorderWidth ? custom.hoverBorderWidth : valueOrDefault(dataset.hoverBorderWidth, index, model.borderWidth);
-               },
-               update: noop
+               }
        });
 
        Chart.DatasetController.extend = helpers.inherits;