diff --git a/src/core/core.datasetController.js b/src/core/core.datasetController.js index 584aeda027e..63aea1f33ad 100644 --- a/src/core/core.datasetController.js +++ b/src/core/core.datasetController.js @@ -67,7 +67,7 @@ function getSortedDatasetIndices(chart, filterVisible) { return keys; } -function applyStack(stack, value, dsIndex, options) { +function applyStack(stack, value, dsIndex, options = {}) { const keys = stack.keys; const singleMode = options.mode === 'single'; let i, ilen, datasetIndex, otherValue; @@ -212,6 +212,8 @@ function clearStacks(meta, items) { const isDirectUpdateMode = (mode) => mode === 'reset' || mode === 'none'; const cloneIfNotShared = (cached, shared) => shared ? cached : Object.assign({}, cached); +const createStack = (canStack, meta, chart) => canStack && !meta.hidden && meta._stacked + && {keys: getSortedDatasetIndices(chart, true), values: null}; export default class DatasetController { @@ -567,11 +569,7 @@ export default class DatasetController { const values = stack && parsed._stacks[scale.axis]; if (stack && values) { stack.values = values; - // Need to consider individual stack values for data range, - // in addition to the stacked value - range.min = Math.min(range.min, value); - range.max = Math.max(range.max, value); - value = applyStack(stack, parsedValue, this._cachedMeta.index, {all: true}); + value = applyStack(stack, parsedValue, this._cachedMeta.index); } range.min = Math.min(range.min, value); range.max = Math.max(range.max, value); @@ -586,16 +584,15 @@ export default class DatasetController { const sorted = meta._sorted && scale === meta.iScale; const ilen = _parsed.length; const otherScale = this._getOtherScale(scale); - const stack = canStack && meta._stacked && {keys: getSortedDatasetIndices(this.chart, true), values: null}; + const stack = createStack(canStack, meta, this.chart); const range = {min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY}; const {min: otherMin, max: otherMax} = getUserBounds(otherScale); - let i, value, parsed, otherValue; + let i, parsed; function _skip() { parsed = _parsed[i]; - value = parsed[scale.axis]; - otherValue = parsed[otherScale.axis]; - return !isFinite(value) || otherMin > otherValue || otherMax < otherValue; + const otherValue = parsed[otherScale.axis]; + return !isFinite(parsed[scale.axis]) || otherMin > otherValue || otherMax < otherValue; } for (i = 0; i < ilen; ++i) { diff --git a/test/fixtures/controller.line/stacking/bounds-data.png b/test/fixtures/controller.line/stacking/bounds-data.png index 90ce6a30c28..71ea7e96392 100644 Binary files a/test/fixtures/controller.line/stacking/bounds-data.png and b/test/fixtures/controller.line/stacking/bounds-data.png differ diff --git a/test/specs/controller.line.tests.js b/test/specs/controller.line.tests.js index 7210367ca95..684b1919bb2 100644 --- a/test/specs/controller.line.tests.js +++ b/test/specs/controller.line.tests.js @@ -59,6 +59,32 @@ describe('Chart.controllers.line', function() { expect(createChart).not.toThrow(); }); + it('should find min and max for stacked chart', function() { + var chart = window.acquireChart({ + type: 'line', + data: { + datasets: [{ + data: [10, 11, 12, 13] + }, { + data: [1, 2, 3, 4] + }], + labels: ['a', 'b', 'c', 'd'] + }, + options: { + scales: { + y: { + stacked: true + } + } + } + }); + expect(chart.getDatasetMeta(0).controller.getMinMax(chart.scales.y, true)).toEqual({min: 10, max: 13}); + expect(chart.getDatasetMeta(1).controller.getMinMax(chart.scales.y, true)).toEqual({min: 11, max: 17}); + chart.hide(0); + expect(chart.getDatasetMeta(0).controller.getMinMax(chart.scales.y, true)).toEqual({min: 10, max: 13}); + expect(chart.getDatasetMeta(1).controller.getMinMax(chart.scales.y, true)).toEqual({min: 1, max: 4}); + }); + it('Should create line elements and point elements for each data item during initialization', function() { var chart = window.acquireChart({ type: 'line',