]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Change updateElement to updateElements (#6722)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Tue, 19 Nov 2019 12:12:38 +0000 (04:12 -0800)
committerEvert Timberg <evert.timberg+github@gmail.com>
Tue, 19 Nov 2019 12:12:38 +0000 (07:12 -0500)
docs/getting-started/v3-migration.md
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 92b473ddda70f31f6f5c60f59c5b4586b514007c..0d1cecc60567889dfab01fa0fd62a2677410fd16 100644 (file)
@@ -101,6 +101,7 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released
 * `Chart.Animation.animationObject` was renamed to `Chart.Animation`
 * `Chart.Animation.chartInstance` was renamed to `Chart.Animation.chart`
 * `DatasetController.createMetaData` and `DatasetController.createMetaDataset` were replaced with `DatasetController.createElement`
+* `DatasetController.updateElement` was renamed to `DatasetController.updateElements`
 * `TimeScale.getLabelCapacity` was renamed to `TimeScale._getLabelCapacity`
 * `TimeScale.tickFormatFunction` was renamed to `TimeScale._tickFormatFunction`
 * `TimeScale.getPixelForOffset` was renamed to `TimeScale._getPixelForOffset`
index 804746f52432af8afe4bdf8f23ba79d89f323363..b4b2582b6e85d6e3c38584cbe5e525b779e1a869 100644 (file)
@@ -271,58 +271,49 @@ module.exports = DatasetController.extend({
        },
 
        update: function(reset) {
-               var me = this;
-               var rects = me._cachedMeta.data;
-               var i, ilen;
-
-               me._ruler = me.getRuler();
+               const me = this;
+               const rects = me._cachedMeta.data;
 
-               for (i = 0, ilen = rects.length; i < ilen; ++i) {
-                       me.updateElement(rects[i], i, reset);
-               }
+               me.updateElements(rects, 0, rects.length, reset);
        },
 
-       updateElement: function(rectangle, index, reset) {
-               var me = this;
-               var options = me._resolveDataElementOptions(index);
+       updateElements: function(rectangles, start, count, reset) {
+               const me = this;
+               const vscale = me._cachedMeta.vScale;
+               const base = vscale.getBasePixel();
+               const horizontal = vscale.isHorizontal();
+               const ruler = me.getRuler();
+               let i;
+
+               for (i = 0; i < start + count; i++) {
+                       const rectangle = rectangles[i];
+                       const options = me._resolveDataElementOptions(i);
+                       const vpixels = me.calculateBarValuePixels(i, options);
+                       const ipixels = me.calculateBarIndexPixels(i, ruler, options);
+
+                       rectangle._model = {
+                               backgroundColor: options.backgroundColor,
+                               borderColor: options.borderColor,
+                               borderSkipped: options.borderSkipped,
+                               borderWidth: options.borderWidth
+                       };
+
+                       const model = rectangle._model;
+
+                       // all borders are drawn for floating bar
+                       if (me._getParsed(i)._custom) {
+                               model.borderSkipped = null;
+                       }
 
-               rectangle._model = {
-                       backgroundColor: options.backgroundColor,
-                       borderColor: options.borderColor,
-                       borderSkipped: options.borderSkipped,
-                       borderWidth: options.borderWidth
-               };
+                       model.horizontal = horizontal;
+                       model.base = reset ? base : vpixels.base;
+                       model.x = horizontal ? reset ? base : vpixels.head : ipixels.center;
+                       model.y = horizontal ? ipixels.center : reset ? base : vpixels.head;
+                       model.height = horizontal ? ipixels.size : undefined;
+                       model.width = horizontal ? undefined : ipixels.size;
 
-               // all borders are drawn for floating bar
-               if (me._getParsed(index)._custom) {
-                       rectangle._model.borderSkipped = null;
+                       rectangle.pivot(me.chart._animationsDisabled);
                }
-
-               me._updateElementGeometry(rectangle, index, reset, options);
-
-               rectangle.pivot(me.chart._animationsDisabled);
-       },
-
-       /**
-        * @private
-        */
-       _updateElementGeometry: function(rectangle, index, reset, options) {
-               const me = this;
-               const meta = me._cachedMeta;
-               const model = rectangle._model;
-               const vScale = meta.vScale;
-               const base = vScale.getBasePixel();
-               const horizontal = vScale.isHorizontal();
-               const ruler = me._ruler || me.getRuler();
-               const vpixels = me.calculateBarValuePixels(index, options);
-               const ipixels = me.calculateBarIndexPixels(index, ruler, options);
-
-               model.horizontal = horizontal;
-               model.base = reset ? base : vpixels.base;
-               model.x = horizontal ? reset ? base : vpixels.head : ipixels.center;
-               model.y = horizontal ? ipixels.center : reset ? base : vpixels.head;
-               model.height = horizontal ? ipixels.size : undefined;
-               model.width = horizontal ? undefined : ipixels.size;
        },
 
        /**
index c1b58f8dd7309adeb1f5b64092653dd1890cb266..2809e84b482bd847911f7c84f296aeec1805297e 100644 (file)
@@ -116,39 +116,42 @@ module.exports = DatasetController.extend({
                const points = me._cachedMeta.data;
 
                // Update Points
-               helpers.each(points, function(point, index) {
-                       me.updateElement(point, index, reset);
-               });
+               me.updateElements(points, 0, points.length, reset);
        },
 
        /**
         * @protected
         */
-       updateElement: function(point, index, reset) {
+       updateElements: function(points, start, count, reset) {
                const me = this;
                const meta = me._cachedMeta;
                const xScale = meta.xScale;
                const yScale = meta.yScale;
-               const options = me._resolveDataElementOptions(index);
-               const parsed = !reset && me._getParsed(index);
-               const x = reset ? xScale.getPixelForDecimal(0.5) : xScale.getPixelForValue(parsed[xScale.id]);
-               const y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(parsed[yScale.id]);
-
-               point._options = options;
-               point._model = {
-                       backgroundColor: options.backgroundColor,
-                       borderColor: options.borderColor,
-                       borderWidth: options.borderWidth,
-                       hitRadius: options.hitRadius,
-                       pointStyle: options.pointStyle,
-                       rotation: options.rotation,
-                       radius: reset ? 0 : options.radius,
-                       skip: isNaN(x) || isNaN(y),
-                       x: x,
-                       y: y,
-               };
-
-               point.pivot(me.chart._animationsDisabled);
+               let i;
+
+               for (i = start; i < start + count; i++) {
+                       const point = points[i];
+                       const options = me._resolveDataElementOptions(i);
+                       const parsed = !reset && me._getParsed(i);
+                       const x = reset ? xScale.getPixelForDecimal(0.5) : xScale.getPixelForValue(parsed[xScale.id]);
+                       const y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(parsed[yScale.id]);
+
+                       point._options = options;
+                       point._model = {
+                               backgroundColor: options.backgroundColor,
+                               borderColor: options.borderColor,
+                               borderWidth: options.borderWidth,
+                               hitRadius: options.hitRadius,
+                               pointStyle: options.pointStyle,
+                               rotation: options.rotation,
+                               radius: reset ? 0 : options.radius,
+                               skip: isNaN(x) || isNaN(y),
+                               x: x,
+                               y: y,
+                       };
+
+                       point.pivot(me.chart._animationsDisabled);
+               }
        },
 
        /**
index b98331b76adf487c64161460446732448fae5e0c..2fcb7d272f94cf1889e2af1701419cd6d2a85c7b 100644 (file)
@@ -217,29 +217,30 @@ module.exports = DatasetController.extend({
                me.outerRadius = chart.outerRadius - chart.radiusLength * me._getRingWeightOffset(me.index);
                me.innerRadius = Math.max(me.outerRadius - chart.radiusLength * chartWeight, 0);
 
-               for (i = 0, ilen = arcs.length; i < ilen; ++i) {
-                       me.updateElement(arcs[i], i, reset);
-               }
+               me.updateElements(arcs, 0, arcs.length, reset);
        },
 
-       updateElement: function(arc, index, reset) {
-               var me = this;
-               var chart = me.chart;
-               var chartArea = chart.chartArea;
-               var opts = chart.options;
-               var animationOpts = opts.animation;
-               var centerX = (chartArea.left + chartArea.right) / 2;
-               var centerY = (chartArea.top + chartArea.bottom) / 2;
-               var startAngle = opts.rotation; // non reset case handled later
-               var endAngle = opts.rotation; // non reset case handled later
-               var circumference = reset && animationOpts.animateRotate ? 0 : arc.hidden ? 0 : me.calculateCircumference(arc._parsed * opts.circumference / DOUBLE_PI);
-               var innerRadius = reset && animationOpts.animateScale ? 0 : me.innerRadius;
-               var outerRadius = reset && animationOpts.animateScale ? 0 : me.outerRadius;
-               var options = arc._options || {};
-
-               helpers.extend(arc, {
-                       // Desired view properties
-                       _model: {
+
+       updateElements: function(arcs, start, count, reset) {
+               const me = this;
+               const chart = me.chart;
+               const chartArea = chart.chartArea;
+               const opts = chart.options;
+               const animationOpts = opts.animation;
+               const centerX = (chartArea.left + chartArea.right) / 2;
+               const centerY = (chartArea.top + chartArea.bottom) / 2;
+               const startAngle = opts.rotation; // non reset case handled later
+               const endAngle = opts.rotation; // non reset case handled later
+               const innerRadius = reset && animationOpts.animateScale ? 0 : me.innerRadius;
+               const outerRadius = reset && animationOpts.animateScale ? 0 : me.outerRadius;
+               var i;
+
+               for (i = 0; i < start + count; ++i) {
+                       const arc = arcs[i];
+                       const circumference = reset && animationOpts.animateRotate ? 0 : arc.hidden ? 0 : me.calculateCircumference(arc._parsed * opts.circumference / DOUBLE_PI);
+                       const options = arc._options || {};
+                       const model = {
+                               // Desired view properties
                                backgroundColor: options.backgroundColor,
                                borderColor: options.borderColor,
                                borderWidth: options.borderWidth,
@@ -251,23 +252,23 @@ module.exports = DatasetController.extend({
                                circumference: circumference,
                                outerRadius: outerRadius,
                                innerRadius: innerRadius
-                       }
-               });
+                       };
 
-               var model = arc._model;
+                       arc._model = model;
 
-               // Set correct angles if not resetting
-               if (!reset || !animationOpts.animateRotate) {
-                       if (index === 0) {
-                               model.startAngle = opts.rotation;
-                       } else {
-                               model.startAngle = me._cachedMeta.data[index - 1]._model.endAngle;
+                       // Set correct angles if not resetting
+                       if (!reset || !animationOpts.animateRotate) {
+                               if (i === 0) {
+                                       model.startAngle = opts.rotation;
+                               } else {
+                                       model.startAngle = me._cachedMeta.data[i - 1]._model.endAngle;
+                               }
+
+                               model.endAngle = model.startAngle + model.circumference;
                        }
 
-                       model.endAngle = model.startAngle + model.circumference;
+                       arc.pivot(chart._animationsDisabled);
                }
-
-               arc.pivot(chart._animationsDisabled);
        },
 
        calculateTotal: function() {
index 0d46f369669ccc04472d22649187c884986bbcba..a8acfd11c29de86af95622297a9c6fd323ad0eef 100644 (file)
@@ -68,14 +68,14 @@ module.exports = DatasetController.extend({
        },
 
        update: function(reset) {
-               var me = this;
-               var meta = me._cachedMeta;
-               var line = meta.dataset;
-               var points = meta.data || [];
-               var options = me.chart.options;
-               var config = me._config;
-               var showLine = me._showLine = valueOrDefault(config.showLine, options.showLines);
-               var i, ilen;
+               const me = this;
+               const meta = me._cachedMeta;
+               const line = meta.dataset;
+               const points = meta.data || [];
+               const options = me.chart.options;
+               const config = me._config;
+               const showLine = me._showLine = valueOrDefault(config.showLine, options.showLines);
+               let i, ilen;
 
                // Update Line
                if (showLine) {
@@ -88,9 +88,7 @@ module.exports = DatasetController.extend({
                }
 
                // Update Points
-               for (i = 0, ilen = points.length; i < ilen; ++i) {
-                       me.updateElement(points[i], i, reset);
-               }
+               me.updateElements(points, 0, points.length, reset);
 
                if (showLine && line._model.tension !== 0) {
                        me.updateBezierControlPoints();
@@ -102,35 +100,40 @@ module.exports = DatasetController.extend({
                }
        },
 
-       updateElement: function(point, index, reset) {
+       updateElements: function(points, start, count, reset) {
                const me = this;
                const meta = me._cachedMeta;
                const xScale = meta.xScale;
                const yScale = meta.yScale;
                const stacked = meta._stacked;
-               const parsed = me._getParsed(index);
-               const options = me._resolveDataElementOptions(index);
-               const x = xScale.getPixelForValue(parsed[xScale.id]);
-               const y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(stacked ? me._applyStack(yScale, parsed) : parsed[yScale.id]);
-
-               // Utility
-               point._options = options;
-
-               // Desired view properties
-               point._model = {
-                       x: x,
-                       y: y,
-                       skip: isNaN(x) || isNaN(y),
-                       // Appearance
-                       radius: options.radius,
-                       pointStyle: options.pointStyle,
-                       rotation: options.rotation,
-                       backgroundColor: options.backgroundColor,
-                       borderColor: options.borderColor,
-                       borderWidth: options.borderWidth,
-                       // Tooltip
-                       hitRadius: options.hitRadius
-               };
+               var i;
+
+               for (i = start; i < start + count; ++i) {
+                       const point = points[i];
+                       const parsed = me._getParsed(i);
+                       const options = me._resolveDataElementOptions(i);
+                       const x = xScale.getPixelForValue(parsed[xScale.id]);
+                       const y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(stacked ? me._applyStack(yScale, parsed) : parsed[yScale.id]);
+
+                       // Utility
+                       point._options = options;
+
+                       // Desired view properties
+                       point._model = {
+                               x: x,
+                               y: y,
+                               skip: isNaN(x) || isNaN(y),
+                               // Appearance
+                               radius: options.radius,
+                               pointStyle: options.pointStyle,
+                               rotation: options.rotation,
+                               backgroundColor: options.backgroundColor,
+                               borderColor: options.borderColor,
+                               borderWidth: options.borderWidth,
+                               // Tooltip
+                               hitRadius: options.hitRadius
+                       };
+               }
        },
 
        /**
@@ -234,7 +237,6 @@ module.exports = DatasetController.extend({
                var ilen = points.length;
 
                if (me._showLine) {
-
                        meta.dataset.draw();
                }
 
index a2fd5a1a43b29e79896be006ddbdb810f0e1a52a..d22893d5b188b2ff026818c470c9fbb6ff1720c1 100644 (file)
@@ -154,10 +154,7 @@ module.exports = DatasetController.extend({
                        start += angle;
                }
 
-               for (i = 0, ilen = arcs.length; i < ilen; ++i) {
-                       arcs[i]._options = me._resolveDataElementOptions(i);
-                       me.updateElement(arcs[i], i, reset);
-               }
+               me.updateElements(arcs, 0, arcs.length, reset);
        },
 
        /**
@@ -178,29 +175,29 @@ module.exports = DatasetController.extend({
                me.innerRadius = me.outerRadius - chart.radiusLength;
        },
 
-       updateElement: function(arc, index, reset) {
-               var me = this;
-               var chart = me.chart;
-               var dataset = me.getDataset();
-               var opts = chart.options;
-               var animationOpts = opts.animation;
-               var scale = chart.scale;
-
-               var centerX = scale.xCenter;
-               var centerY = scale.yCenter;
-
-               // var negHalfPI = -0.5 * Math.PI;
-               var datasetStartAngle = opts.startAngle;
-               var distance = arc.hidden ? 0 : scale.getDistanceFromCenterForValue(dataset.data[index]);
-               var startAngle = me._starts[index];
-               var endAngle = startAngle + (arc.hidden ? 0 : me._angles[index]);
-
-               var resetRadius = animationOpts.animateScale ? 0 : scale.getDistanceFromCenterForValue(dataset.data[index]);
-               var options = arc._options || {};
-
-               helpers.extend(arc, {
-                       // Desired view properties
-                       _model: {
+       updateElements: function(arcs, start, count, reset) {
+               const me = this;
+               const chart = me.chart;
+               const dataset = me.getDataset();
+               const opts = chart.options;
+               const animationOpts = opts.animation;
+               const scale = chart.scale;
+               const centerX = scale.xCenter;
+               const centerY = scale.yCenter;
+               var i;
+
+               for (i = 0; i < start + count; i++) {
+                       const arc = arcs[i];
+                       // var negHalfPI = -0.5 * Math.PI;
+                       const datasetStartAngle = opts.startAngle;
+                       const distance = arc.hidden ? 0 : scale.getDistanceFromCenterForValue(dataset.data[i]);
+                       const startAngle = me._starts[i];
+                       const endAngle = startAngle + (arc.hidden ? 0 : me._angles[i]);
+
+                       const resetRadius = animationOpts.animateScale ? 0 : scale.getDistanceFromCenterForValue(dataset.data[i]);
+                       const options = arc._options = me._resolveDataElementOptions(i);
+
+                       arc._model = {
                                backgroundColor: options.backgroundColor,
                                borderColor: options.borderColor,
                                borderWidth: options.borderWidth,
@@ -211,10 +208,10 @@ module.exports = DatasetController.extend({
                                outerRadius: reset ? resetRadius : distance,
                                startAngle: reset && animationOpts.animateRotate ? datasetStartAngle : startAngle,
                                endAngle: reset && animationOpts.animateRotate ? datasetStartAngle : endAngle
-                       }
-               });
+                       };
 
-               arc.pivot(chart._animationsDisabled);
+                       arc.pivot(chart._animationsDisabled);
+               }
        },
 
        countVisibleElements: function() {
index 6b9d24468b709ee985326c2d8c61340eebf68738..6ae47923deba76a883b1329a348d5cbba800c892 100644 (file)
@@ -108,9 +108,7 @@ module.exports = DatasetController.extend({
                line.pivot(animationsDisabled);
 
                // Update Points
-               for (i = 0, ilen = points.length; i < ilen; ++i) {
-                       me.updateElement(points[i], i, reset);
-               }
+               me.updateElements(points, 0, points.length, reset);
 
                // Update bezier control points
                me.updateBezierControlPoints();
@@ -121,34 +119,39 @@ module.exports = DatasetController.extend({
                }
        },
 
-       updateElement: function(point, index, reset) {
-               var me = this;
-               var dataset = me.getDataset();
-               var scale = me.chart.scale;
-               var pointPosition = scale.getPointPositionForValue(index, dataset.data[index]);
-               var options = me._resolveDataElementOptions(index);
-               var x = reset ? scale.xCenter : pointPosition.x;
-               var y = reset ? scale.yCenter : pointPosition.y;
-
-               // Utility
-               point._options = options;
-
-               // Desired view properties
-               point._model = {
-                       x: x, // value not used in dataset scale, but we want a consistent API between scales
-                       y: y,
-                       skip: isNaN(x) || isNaN(y),
-                       // Appearance
-                       radius: options.radius,
-                       pointStyle: options.pointStyle,
-                       rotation: options.rotation,
-                       backgroundColor: options.backgroundColor,
-                       borderColor: options.borderColor,
-                       borderWidth: options.borderWidth,
-
-                       // Tooltip
-                       hitRadius: options.hitRadius
-               };
+       updateElements: function(points, start, count, reset) {
+               const me = this;
+               const dataset = me.getDataset();
+               const scale = me.chart.scale;
+               var i;
+
+               for (i = start; i < start + count; i++) {
+                       const point = points[i];
+                       const pointPosition = scale.getPointPositionForValue(i, dataset.data[i]);
+                       const options = me._resolveDataElementOptions(i);
+                       const x = reset ? scale.xCenter : pointPosition.x;
+                       const y = reset ? scale.yCenter : pointPosition.y;
+
+                       // Utility
+                       point._options = options;
+
+                       // Desired view properties
+                       point._model = {
+                               x: x, // value not used in dataset scale, but we want a consistent API between scales
+                               y: y,
+                               skip: isNaN(x) || isNaN(y),
+                               // Appearance
+                               radius: options.radius,
+                               pointStyle: options.pointStyle,
+                               rotation: options.rotation,
+                               backgroundColor: options.backgroundColor,
+                               borderColor: options.borderColor,
+                               borderWidth: options.borderWidth,
+
+                               // Tooltip
+                               hitRadius: options.hitRadius
+                       };
+               }
        },
 
        /**
index 79139bfd355e8840166b73fe324bc3f966bccbbc..a389a5930be76059757b8a1c74a54594303d6d8a 100644 (file)
@@ -48,7 +48,6 @@ function listenArrayEvents(array, listener) {
        });
 }
 
-
 function scaleClip(scale, allowedOverflow) {
        var opts = scale && scale.options || {};
        var reverse = opts.reverse;
@@ -972,15 +971,15 @@ helpers.extend(DatasetController.prototype, {
        insertElements: function(start, count) {
                const me = this;
                const elements = [];
+               const data = me._cachedMeta.data;
                let i;
+
                for (i = start; i < start + count; ++i) {
                        elements.push(me.createElement(me.dataElementType));
                }
-               me._cachedMeta.data.splice(start, 0, ...elements);
+               data.splice(start, 0, ...elements);
                me._parse(start, count);
-               for (i = 0; i < count; ++i) {
-                       me.updateElement(elements[i], start + i, true);
-               }
+               me.updateElements(data, start, count);
        },
 
        /**