]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Avoid meta data access in calculateCircumference
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Mon, 25 Apr 2016 17:49:49 +0000 (19:49 +0200)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Tue, 26 Apr 2016 10:46:30 +0000 (12:46 +0200)
Fix access of uninitialized meta data while calculating circumference in the polar area chart by caching the number of visible elements in the update() method. Also make the calculateTotal() of the doughnut chart tolerant of uninitialized meta data.

src/controllers/controller.doughnut.js
src/controllers/controller.polarArea.js

index 6044dc893c11f4b0eae12092ac92a9daaaa1bb6b..cb428548a1c0b5bd18373b92431af1ab73ad761c 100644 (file)
@@ -265,11 +265,14 @@ module.exports = function(Chart) {
                },
 
                calculateTotal: function() {
+                       var dataset = this.getDataset();
                        var meta = this.getMeta();
                        var total = 0;
+                       var value;
 
-                       this.getDataset().data.forEach(function(value, index) {
-                               if (!isNaN(value) && !meta.data[index].hidden) {
+                       helpers.each(meta.data, function(element, index) {
+                               value = dataset.data[index];
+                               if (!isNaN(value) && !element.hidden) {
                                        total += Math.abs(value);
                                }
                        });
index c7dd87cad29bbe513f3d86cb5ac20956b515fc28..1882ffca5c1fa004ea22569e55d7a4e94caf112c 100644 (file)
@@ -119,6 +119,7 @@ module.exports = function(Chart) {
                },
 
                update: function update(reset) {
+                       var meta = this.getMeta();
                        var minSize = Math.min(this.chart.chartArea.right - this.chart.chartArea.left, this.chart.chartArea.bottom - this.chart.chartArea.top);
                        this.chart.outerRadius = Math.max((minSize - this.chart.options.elements.arc.borderWidth / 2) / 2, 0);
                        this.chart.innerRadius = Math.max(this.chart.options.cutoutPercentage ? (this.chart.outerRadius / 100) * (this.chart.options.cutoutPercentage) : 1, 0);
@@ -127,7 +128,9 @@ module.exports = function(Chart) {
                        this.outerRadius = this.chart.outerRadius - (this.chart.radiusLength * this.index);
                        this.innerRadius = this.outerRadius - this.chart.radiusLength;
 
-                       helpers.each(this.getMeta().data, function(arc, index) {
+                       meta.count = this.countVisibleElements();
+
+                       helpers.each(meta.data, function(arc, index) {
                                this.updateElement(arc, index, reset);
                        }, this);
                },
@@ -218,23 +221,27 @@ module.exports = function(Chart) {
                        arc._model.borderWidth = arc.custom && arc.custom.borderWidth ? arc.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().borderWidth, index, this.chart.options.elements.arc.borderWidth);
                },
 
-               calculateCircumference: function(value) {
-                       if (isNaN(value)) {
-                               return 0;
-                       }
-
-                       // Count the number of "visible"" values
+               countVisibleElements: function() {
+                       var dataset = this.getDataset();
                        var meta = this.getMeta();
                        var count = 0;
 
-                       this.getDataset().data.forEach(function(value, index) {
-                               if (!isNaN(value) && !meta.data[index].hidden) {
+                       helpers.each(meta.data, function(element, index) {
+                               if (!isNaN(dataset.data[index]) && !element.hidden) {
                                        count++;
                                }
                        });
 
-                       return (2 * Math.PI) / count;
+                       return count;
+               },
+
+               calculateCircumference: function(value) {
+                       var count = this.getMeta().count;
+                       if (count > 0 && !isNaN(value)) {
+                               return (2 * Math.PI) / count;
+                       } else {
+                               return 0;
+                       }
                }
        });
-
 };