From: Simon Brunel Date: Mon, 25 Apr 2016 17:49:49 +0000 (+0200) Subject: Avoid meta data access in calculateCircumference X-Git-Tag: 2.1.0~13^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=aa0933e040b456e23557a999408f6047578e631c;p=thirdparty%2FChart.js.git Avoid meta data access in calculateCircumference 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. --- diff --git a/src/controllers/controller.doughnut.js b/src/controllers/controller.doughnut.js index 6044dc893..cb428548a 100644 --- a/src/controllers/controller.doughnut.js +++ b/src/controllers/controller.doughnut.js @@ -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); } }); diff --git a/src/controllers/controller.polarArea.js b/src/controllers/controller.polarArea.js index c7dd87cad..1882ffca5 100644 --- a/src/controllers/controller.polarArea.js +++ b/src/controllers/controller.polarArea.js @@ -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; + } } }); - };