]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Remove unneeded functions from core controller. Updated dataset controllers to have...
authorEvert Timberg <evert.timberg@gmail.com>
Tue, 22 Sep 2015 00:59:53 +0000 (20:59 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Tue, 22 Sep 2015 00:59:53 +0000 (20:59 -0400)
src/controllers/controller.bar.js
src/controllers/controller.doughnut.js
src/controllers/controller.line.js
src/controllers/controller.polarArea.js
src/controllers/controller.radar.js
src/core/core.controller.js
test/controller.bar.tests.js
test/controller.doughnut.tests.js

index 6f9724e8b51361431f91f138bf3f98ba166687f6..6b39622810f3f698152fafd2cfa28a6abfba399a 100644 (file)
                        this.update(true);
                },
 
-               update: function(reset) {
-                       var numBars = this.getBarCount();
-
+               buildOrUpdateElements: function buildOrUpdateElements() {
                        var numData = this.getDataset().data.length;
                        var numRectangles = this.getDataset().metaData.length;
 
                                        this.addElementAndReset(index);
                                }
                        }
+               },
+
+               update: function update(reset) {
+                       var numBars = this.getBarCount();
 
                        helpers.each(this.getDataset().metaData, function(rectangle, index) {
                                this.updateElement(rectangle, index, reset, numBars);
index d4586b4fca6d102434ddd0355cb8b373dec32338..da3386e311a077843730d0e6737bf2a92b6593a3 100644 (file)
                        this.update(true);
                },
 
-               update: function(reset) {
-
-                       this.chart.outerRadius = Math.max((helpers.min([this.chart.chart.width, this.chart.chart.height]) / 2) - this.chart.options.elements.arc.borderWidth / 2, 0);
-                       this.chart.innerRadius = Math.max(this.chart.options.cutoutPercentage ? (this.chart.outerRadius / 100) * (this.chart.options.cutoutPercentage) : 1, 0);
-                       this.chart.radiusLength = (this.chart.outerRadius - this.chart.innerRadius) / this.chart.data.datasets.length;
-
-                       this.getDataset().total = 0;
-                       helpers.each(this.getDataset().data, function(value) {
-                               this.getDataset().total += Math.abs(value);
-                       }, this);
-
-                       this.outerRadius = this.chart.outerRadius - (this.chart.radiusLength * this.index);
-                       this.innerRadius = this.outerRadius - this.chart.radiusLength;
-
+               buildOrUpdateElements: function buildOrUpdateElements() {
                        // Make sure we have metaData for each data point
                        var numData = this.getDataset().data.length;
                        var numArcs = this.getDataset().metaData.length;
                                        this.addElementAndReset(index);
                                }
                        }
+               },
+
+               update: function update(reset) {
+
+                       this.chart.outerRadius = Math.max((helpers.min([this.chart.chart.width, this.chart.chart.height]) / 2) - this.chart.options.elements.arc.borderWidth / 2, 0);
+                       this.chart.innerRadius = Math.max(this.chart.options.cutoutPercentage ? (this.chart.outerRadius / 100) * (this.chart.options.cutoutPercentage) : 1, 0);
+                       this.chart.radiusLength = (this.chart.outerRadius - this.chart.innerRadius) / this.chart.data.datasets.length;
+
+                       this.getDataset().total = 0;
+                       helpers.each(this.getDataset().data, function(value) {
+                               this.getDataset().total += Math.abs(value);
+                       }, this);
+
+                       this.outerRadius = this.chart.outerRadius - (this.chart.radiusLength * this.index);
+                       this.innerRadius = this.outerRadius - this.chart.radiusLength;
 
                        helpers.each(this.getDataset().metaData, function(arc, index) {
                                this.updateElement(arc, index, reset);
index 608b5d704bf621aed33d0dd0c3f65ab31f301968..6014a930640d700a3a0bc33d79e3227c387d5398 100644 (file)
                        this.update(true);
                },
 
-               update: function(reset) {
-                       var line = this.getDataset().metaDataset;
-                       var points = this.getDataset().metaData;
-
-                       var yScale = this.getScaleForId(this.getDataset().yAxisID);
-                       var xScale = this.getScaleForId(this.getDataset().xAxisID);
-                       var scaleBase;
-
+               buildOrUpdateElements: function buildOrUpdateElements() {
                        // Handle the number of data points changing
                        var numData = this.getDataset().data.length;
                        var numPoints = this.getDataset().metaData.length;
                                        this.addElementAndReset(index);
                                }
                        }
+               },
+
+               update: function update(reset) {
+                       var line = this.getDataset().metaDataset;
+                       var points = this.getDataset().metaData;
+
+                       var yScale = this.getScaleForId(this.getDataset().yAxisID);
+                       var xScale = this.getScaleForId(this.getDataset().xAxisID);
+                       var scaleBase;
 
                        if (yScale.min < 0 && yScale.max < 0) {
                                scaleBase = yScale.getPixelForValue(yScale.max);
index d832f09d18e924b01e016a0dc367d36e3f9b4dd9..3776184ad535add9fad8f312b95d25990d36e390 100644 (file)
                        this.update(true);
                },
 
-               update: function(reset) {
+               buildOrUpdateElements: function buildOrUpdateElements() {
+                       // Handle the number of data points changing
+                       var numData = this.getDataset().data.length;
+                       var numPoints = this.getDataset().metaData.length;
+
+                       // Make sure that we handle number of datapoints changing
+                       if (numData < numPoints) {
+                               // Remove excess bars for data points that have been removed
+                               this.getDataset().metaData.splice(numData, numPoints - numData)
+                       } else if (numData > numPoints) {
+                               // Add new elements
+                               for (var index = numPoints; index < numData; ++index) {
+                                       this.addElementAndReset(index);
+                               }
+                       }
+               },
+
+               update: function update(reset) {
 
                        Chart.scaleService.fitScalesForChart(this, this.chart.width, this.chart.height);
                        //this.chart.scale.setScaleSize();
index ce597d720f9184603b3114a5a60e0856a456d02d..85cf5a459f9ff8072907d390a7530c57d93360d4 100644 (file)
                        this.update(true);
                },
 
-               update: function(reset) {
+               buildOrUpdateElements: function buildOrUpdateElements() {
+                       // Handle the number of data points changing
+                       var numData = this.getDataset().data.length;
+                       var numPoints = this.getDataset().metaData.length;
+
+                       // Make sure that we handle number of datapoints changing
+                       if (numData < numPoints) {
+                               // Remove excess bars for data points that have been removed
+                               this.getDataset().metaData.splice(numData, numPoints - numData)
+                       } else if (numData > numPoints) {
+                               // Add new elements
+                               for (var index = numPoints; index < numData; ++index) {
+                                       this.addElementAndReset(index);
+                               }
+                       }
+               },
+
+               update: function update(reset) {
 
                        var line = this.getDataset().metaDataset;
                        var points = this.getDataset().metaData;
index 614b0579ddb4a2b9e859465c1df85f4a471842c8..3cf57687dff9430ba5d43147892dbe4326c2f047 100644 (file)
                        return this;
                },
 
-               addDataset: function addDataset(dataset, index) {
-                       if (index !== undefined) {
-                               this.data.datasets.splice(index, 0, dataset);
-                       } else {
-                               this.data.datasets.push(dataset);
-                       }
-
-                       this.buildOrUpdateControllers();
-                       dataset.controller.reset(); // so that animation looks ok
-                       this.update();
-               },
-               removeDataset: function removeDataset(index) {
-                       this.data.datasets.splice(index, 1);
-                       this.buildOrUpdateControllers();
-                       this.update();
-               },
-
-               // Add data to the given dataset
-               // @param data: the data to add
-               // @param {Number} datasetIndex : the index of the dataset to add to
-               // @param {Number} index : the index of the data
-               addData: function addData(data, datasetIndex, index) {
-                       if (datasetIndex < this.data.datasets.length) {
-                               if (index === undefined) {
-                                       index = this.data.datasets[datasetIndex].data.length;
-                               }
-
-                               var addElementArgs = [index];
-                               for (var i = 3; i < arguments.length; ++i) {
-                                       addElementArgs.push(arguments[i]);
-                               }
-
-                               this.data.datasets[datasetIndex].data.splice(index, 0, data);
-                               this.data.datasets[datasetIndex].controller.addElementAndReset.apply(this.data.datasets[datasetIndex].controller, addElementArgs);
-                               this.update();
-                       }
-               },
-
-               removeData: function removeData(datasetIndex, index) {
-                       if (datasetIndex < this.data.datasets.length) {
-                               this.data.datasets[datasetIndex].data.splice(index, 1);
-                               this.data.datasets[datasetIndex].controller.removeElement(index);
-                               this.update();
-                       }
-               },
-
                resize: function resize(silent) {
                        this.stop();
                        var canvas = this.chart.canvas;
                        Chart.scaleService.fitScalesForChart(this, this.chart.width, this.chart.height);
                },
 
-               buildOrUpdateControllers: function() {
+               buildOrUpdateControllers: function buildOrUpdateControllers(resetNewControllers) {
                        helpers.each(this.data.datasets, function(dataset, datasetIndex) {
                                var type = dataset.type || this.config.type;
                                if (dataset.controller) {
                                        return;
                                }
                                dataset.controller = new Chart.controllers[type](this, datasetIndex);
+
+                               if (resetNewControllers) {
+                                       dataset.controller.reset();
+                               }
                        }, this);
                },
 
                        }, this);
                },
 
-
                update: function update(animationDuration, lazy) {
-                       // This will loop through any data and do the appropriate element update for the type
                        Chart.scaleService.fitScalesForChart(this, this.chart.width, this.chart.height);
+
+                       // Make sure dataset controllers are updated and new controllers are reset
+                       this.buildOrUpdateControllers(true);
+
+                       // Make sure all dataset controllers have correct meta data counts
+                       helpers.each(this.data.datasets, function(dataset, datasetIndex) {
+                               dataset.controller.buildOrUpdateElements();
+                       }, this);
+
+                       // This will loop through any data and do the appropriate element update for the type
                        helpers.each(this.data.datasets, function(dataset, datasetIndex) {
                                dataset.controller.update();
                        }, this);
                        this.tooltip.transition(easingDecimal).draw();
                },
 
-
-
-
-
                // Get the single element that was clicked on
                // @return : An object containing the dataset index and element index of the matching element. Also contains the rectangle that was draw
                getElementAtEvent: function(e) {
                        for (var datasetIndex = 0; datasetIndex < this.data.datasets.length; datasetIndex++) {
                                for (var elementIndex = 0; elementIndex < this.data.datasets[datasetIndex].metaData.length; elementIndex++) {
                                        if (this.data.datasets[datasetIndex].metaData[elementIndex].inLabelRange(eventPosition.x, eventPosition.y)) {
-                                                helpers.each(this.data.datasets[datasetIndex].metaData, function(element, index) {
-                                                        elementsArray.push(element);
-                                                }, this);
+                                               helpers.each(this.data.datasets[datasetIndex].metaData, function(element, index) {
+                                                       elementsArray.push(element);
+                                               }, this);
                                        }
                                }
                        }
index df27506a16aa320e1da769f344fa6186daf8efa1..c20dd7cf8eb7b488085b4e8a4161b17cb2d77cd3 100644 (file)
@@ -194,6 +194,7 @@ describe('Bar controller tests', function() {
                var controller = new Chart.controllers.bar(chart, 1);
 
                chart.data.datasets[1].data = [1, 2]; // remove 2 items
+               controller.buildOrUpdateElements();
                controller.update();
 
                expect(chart.data.datasets[1].metaData.length).toBe(2);
@@ -236,6 +237,7 @@ describe('Bar controller tests', function() {
                });
 
                chart.data.datasets[1].data = [1, 2, 3];
+               controller.buildOrUpdateElements();
                controller.update();
 
                expect(chart.data.datasets[1].metaData.length).toBe(3); // should add a new meta data item
index cef6062637fe79924ee270e2eff958bb44066ed5..d4ff2ff51dc1371d6f044417b23885feaa6f9db1 100644 (file)
@@ -205,6 +205,7 @@ describe('Doughnut controller tests', function() {
 
                // Change the amount of data and ensure that arcs are updated accordingly
                chart.data.datasets[0].data = [1, 2]; // remove 2 elements from dataset 0
+               controller.buildOrUpdateElements();
                controller.update();
 
                expect(chart.data.datasets[0].metaData.length).toBe(2);
@@ -213,6 +214,7 @@ describe('Doughnut controller tests', function() {
 
                // Add data
                chart.data.datasets[0].data = [1, 2, 3, 4];
+               controller.buildOrUpdateElements();
                controller.update();
 
                expect(chart.data.datasets[0].metaData.length).toBe(4);