Skip to content

Commit b62aabe

Browse files
authored
Assign unique scale IDs (chartjs#6291)
1 parent 067a413 commit b62aabe

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/core/core.controller.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ function updateConfig(chart) {
137137
chart.tooltip.initialize();
138138
}
139139

140+
function nextAvailableScaleId(axesOpts, prefix, index) {
141+
var id;
142+
var hasId = function(obj) {
143+
return obj.id === id;
144+
};
145+
146+
do {
147+
id = prefix + index++;
148+
} while (helpers.findIndex(axesOpts, hasId) >= 0);
149+
150+
return id;
151+
}
152+
140153
function positionIsHorizontal(position) {
141154
return position === 'top' || position === 'bottom';
142155
}
@@ -295,11 +308,15 @@ helpers.extend(Chart.prototype, /** @lends Chart */ {
295308
var scaleOptions = options.scale;
296309

297310
helpers.each(scalesOptions.xAxes, function(xAxisOptions, index) {
298-
xAxisOptions.id = xAxisOptions.id || ('x-axis-' + index);
311+
if (!xAxisOptions.id) {
312+
xAxisOptions.id = nextAvailableScaleId(scalesOptions.xAxes, 'x-axis-', index);
313+
}
299314
});
300315

301316
helpers.each(scalesOptions.yAxes, function(yAxisOptions, index) {
302-
yAxisOptions.id = yAxisOptions.id || ('y-axis-' + index);
317+
if (!yAxisOptions.id) {
318+
yAxisOptions.id = nextAvailableScaleId(scalesOptions.yAxes, 'y-axis-', index);
319+
}
303320
});
304321

305322
if (scaleOptions) {

test/specs/core.controller.tests.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,23 @@ describe('Chart', function() {
10591059
expect(yScale.options.ticks.max).toBe(10);
10601060
});
10611061

1062+
it ('should assign unique scale IDs', function() {
1063+
var chart = acquireChart({
1064+
type: 'line',
1065+
options: {
1066+
scales: {
1067+
xAxes: [{id: 'x-axis-0'}, {}, {}],
1068+
yAxes: [{id: 'y-axis-1'}, {}, {}]
1069+
}
1070+
}
1071+
});
1072+
1073+
expect(Object.keys(chart.scales).sort()).toEqual([
1074+
'x-axis-0', 'x-axis-1', 'x-axis-2',
1075+
'y-axis-1', 'y-axis-2', 'y-axis-3'
1076+
]);
1077+
});
1078+
10621079
it ('should remove discarded scale', function() {
10631080
var chart = acquireChart({
10641081
type: 'line',

0 commit comments

Comments
 (0)