From b62aabe4f726f74e76fe484bfd1b82d48d39a091 Mon Sep 17 00:00:00 2001 From: Akihiko Kusanagi Date: Sun, 9 Jun 2019 15:45:12 +0800 Subject: [PATCH] Assign unique scale IDs (#6291) --- src/core/core.controller.js | 21 +++++++++++++++++++-- test/specs/core.controller.tests.js | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 168d40b4940..5349fb5f72d 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -137,6 +137,19 @@ function updateConfig(chart) { chart.tooltip.initialize(); } +function nextAvailableScaleId(axesOpts, prefix, index) { + var id; + var hasId = function(obj) { + return obj.id === id; + }; + + do { + id = prefix + index++; + } while (helpers.findIndex(axesOpts, hasId) >= 0); + + return id; +} + function positionIsHorizontal(position) { return position === 'top' || position === 'bottom'; } @@ -295,11 +308,15 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { var scaleOptions = options.scale; helpers.each(scalesOptions.xAxes, function(xAxisOptions, index) { - xAxisOptions.id = xAxisOptions.id || ('x-axis-' + index); + if (!xAxisOptions.id) { + xAxisOptions.id = nextAvailableScaleId(scalesOptions.xAxes, 'x-axis-', index); + } }); helpers.each(scalesOptions.yAxes, function(yAxisOptions, index) { - yAxisOptions.id = yAxisOptions.id || ('y-axis-' + index); + if (!yAxisOptions.id) { + yAxisOptions.id = nextAvailableScaleId(scalesOptions.yAxes, 'y-axis-', index); + } }); if (scaleOptions) { diff --git a/test/specs/core.controller.tests.js b/test/specs/core.controller.tests.js index 2fb982276ee..010235f4774 100644 --- a/test/specs/core.controller.tests.js +++ b/test/specs/core.controller.tests.js @@ -1059,6 +1059,23 @@ describe('Chart', function() { expect(yScale.options.ticks.max).toBe(10); }); + it ('should assign unique scale IDs', function() { + var chart = acquireChart({ + type: 'line', + options: { + scales: { + xAxes: [{id: 'x-axis-0'}, {}, {}], + yAxes: [{id: 'y-axis-1'}, {}, {}] + } + } + }); + + expect(Object.keys(chart.scales).sort()).toEqual([ + 'x-axis-0', 'x-axis-1', 'x-axis-2', + 'y-axis-1', 'y-axis-2', 'y-axis-3' + ]); + }); + it ('should remove discarded scale', function() { var chart = acquireChart({ type: 'line',