]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Stacks readability improvements (#6753)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Sat, 16 Nov 2019 15:52:41 +0000 (07:52 -0800)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sat, 16 Nov 2019 15:52:41 +0000 (10:52 -0500)
* Stacks readability improvements
* Variable renames

src/core/core.datasetController.js

index 541da77752380d279338070398508c46ce38ab0a..a5ea94035e77d506b534e1e1cb9824307d3279b8 100644 (file)
@@ -172,8 +172,8 @@ function isStacked(scale, meta) {
        return stacked || (stacked === undefined && meta.stack !== undefined);
 }
 
-function getStackKey(xScale, yScale, meta) {
-       return isStacked(yScale, meta) && xScale.id + '.' + yScale.id + '.' + meta.stack + '.' + meta.type;
+function getStackKey(indexScale, valueScale, meta) {
+       return indexScale.id + '.' + valueScale.id + '.' + meta.stack + '.' + meta.type;
 }
 
 function getFirstScaleId(chart, axis) {
@@ -460,18 +460,20 @@ helpers.extend(DatasetController.prototype, {
         * @private
         */
        _parse: function(start, count) {
-               var me = this;
-               var chart = me.chart;
-               var meta = me._cachedMeta;
-               var data = me._data;
-               var crossRef = chart._xref || (chart._xref = {});
-               var xScale = me._getIndexScale();
-               var yScale = me._getValueScale();
-               var xId = xScale.id;
-               var yId = yScale.id;
-               var xKey = getStackKey(xScale, yScale, meta);
-               var yKey = getStackKey(yScale, xScale, meta);
-               var stacks = xKey || yKey;
+               const me = this;
+               const chart = me.chart;
+               const meta = me._cachedMeta;
+               const data = me._data;
+               const stacks = chart._stacks || (chart._stacks = {}); // map structure is {stackKey: {datasetIndex: value}}
+               const xScale = me._getIndexScale();
+               const yScale = me._getValueScale();
+               const xId = xScale.id;
+               const yId = yScale.id;
+               const xStacked = isStacked(xScale, meta);
+               const yStacked = isStacked(yScale, meta);
+               const xKey = yStacked && getStackKey(xScale, yScale, meta);
+               const yKey = xStacked && getStackKey(yScale, xScale, meta);
+               const stacked = xStacked || yStacked;
                var i, ilen, parsed, stack, item, x, y;
 
                if (helpers.isArray(data[start])) {
@@ -483,25 +485,27 @@ helpers.extend(DatasetController.prototype, {
                }
 
                function storeStack(stackKey, indexValue, scaleId, value) {
-                       if (stackKey) {
-                               stackKey += '.' + indexValue;
-                               item._stackKeys[scaleId] = stackKey;
-                               stack = crossRef[stackKey] || (crossRef[stackKey] = {});
-                               stack[meta.index] = value;
-                       }
+                       stackKey += '.' + indexValue;
+                       item._stackKeys[scaleId] = stackKey;
+                       stack = stacks[stackKey] || (stacks[stackKey] = {});
+                       stack[meta.index] = value;
                }
 
                for (i = 0, ilen = parsed.length; i < ilen; ++i) {
                        item = parsed[i];
                        meta.data[start + i]._parsed = item;
 
-                       if (stacks) {
+                       if (stacked) {
                                item._stackKeys = {};
                                x = item[xId];
                                y = item[yId];
 
-                               storeStack(xKey, x, yId, y);
-                               storeStack(yKey, y, xId, x);
+                               if (yStacked) {
+                                       storeStack(xKey, x, yId, y);
+                               }
+                               if (xStacked) {
+                                       storeStack(yKey, y, xId, x);
+                               }
                        }
                }
 
@@ -611,7 +615,7 @@ helpers.extend(DatasetController.prototype, {
                var value = parsed[scale.id];
                var stack = {
                        keys: getSortedDatasetIndices(chart, true),
-                       values: chart._xref[parsed._stackKeys[scale.id]]
+                       values: chart._stacks[parsed._stackKeys[scale.id]]
                };
                return applyStack(stack, value, meta.index);
        },
@@ -624,7 +628,7 @@ helpers.extend(DatasetController.prototype, {
                var meta = this._cachedMeta;
                var metaData = meta.data;
                var ilen = metaData.length;
-               var crossRef = chart._xref || (chart._xref = {});
+               var stacks = chart._stacks || (chart._stacks = {});
                var max = Number.NEGATIVE_INFINITY;
                var stacked = canStack && meta._stacked;
                var indices = getSortedDatasetIndices(chart, true);
@@ -646,7 +650,7 @@ helpers.extend(DatasetController.prototype, {
                        if (stacked) {
                                stack = {
                                        keys: indices,
-                                       values: crossRef[parsed._stackKeys[scale.id]]
+                                       values: stacks[parsed._stackKeys[scale.id]]
                                };
                                value = applyStack(stack, value, meta.index, true);
                        }