Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign unique scale IDs #6291

Merged
merged 1 commit into from Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/core/core.controller.js
Expand Up @@ -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';
}
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions test/specs/core.controller.tests.js
Expand Up @@ -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',
Expand Down