Skip to content

Commit 82aca78

Browse files
authored
Use custom scale defaults and dataset axis ID options to determine the axis (#11134)
Use custom scale defaults to determine the axis
1 parent 1324672 commit 82aca78

File tree

3 files changed

+118
-11
lines changed

3 files changed

+118
-11
lines changed

.size-limit.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = [
1313
},
1414
{
1515
path: 'dist/chart.js',
16-
limit: '34 KB',
16+
limit: '34.5 KB',
1717
import: '{ Chart }',
1818
running: false,
1919
modifyWebpackConfig

src/core/core.config.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ function getDefaultScaleIDFromAxis(axis, indexAxis) {
2222
return axis === indexAxis ? '_index_' : '_value_';
2323
}
2424

25+
function idMatchesAxis(id) {
26+
if (id === 'x' || id === 'y' || id === 'r') {
27+
return id;
28+
}
29+
}
30+
2531
function axisFromPosition(position) {
2632
if (position === 'top' || position === 'bottom') {
2733
return 'x';
@@ -31,20 +37,35 @@ function axisFromPosition(position) {
3137
}
3238
}
3339

34-
export function determineAxis(id, scaleOptions) {
35-
if (id === 'x' || id === 'y' || id === 'r') {
40+
export function determineAxis(id, ...scaleOptions) {
41+
if (idMatchesAxis(id)) {
3642
return id;
3743
}
44+
for (const opts of scaleOptions) {
45+
const axis = opts.axis
46+
|| axisFromPosition(opts.position)
47+
|| id.length > 1 && idMatchesAxis(id[0].toLowerCase());
48+
if (axis) {
49+
return axis;
50+
}
51+
}
52+
throw new Error(`Cannot determine type of '${id}' axis. Please provide 'axis' or 'position' option.`);
53+
}
3854

39-
id = scaleOptions.axis
40-
|| axisFromPosition(scaleOptions.position)
41-
|| id.length > 1 && determineAxis(id[0].toLowerCase(), scaleOptions);
42-
43-
if (id) {
44-
return id;
55+
function getAxisFromDataset(id, axis, dataset) {
56+
if (dataset[axis + 'AxisID'] === id) {
57+
return {axis};
4558
}
59+
}
4660

47-
throw new Error(`Cannot determine type of '${name}' axis. Please provide 'axis' or 'position' option.`);
61+
function retrieveAxisFromDatasets(id, config) {
62+
if (config.data && config.data.datasets) {
63+
const boundDs = config.data.datasets.filter((d) => d.xAxisID === id || d.yAxisID === id);
64+
if (boundDs.length) {
65+
return getAxisFromDataset(id, 'x', boundDs[0]) || getAxisFromDataset(id, 'y', boundDs[0]);
66+
}
67+
}
68+
return {};
4869
}
4970

5071
function mergeScaleConfig(config, options) {
@@ -62,7 +83,7 @@ function mergeScaleConfig(config, options) {
6283
if (scaleConf._proxy) {
6384
return console.warn(`Ignoring resolver passed as options for scale: ${id}`);
6485
}
65-
const axis = determineAxis(id, scaleConf);
86+
const axis = determineAxis(id, scaleConf, retrieveAxisFromDatasets(id, config), defaults.scales[scaleConf.type]);
6687
const defaultId = getDefaultScaleIDFromAxis(axis, chartIndexAxis);
6788
const defaultScaleOptions = chartDefaults.scales || {};
6889
scales[id] = mergeIf(Object.create(null), [{axis}, scaleConf, defaultScaleOptions[axis], defaultScaleOptions[defaultId]]);

test/specs/core.scale.tests.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,35 @@ describe('Core.scale', function() {
481481
expect(scale._layers().length).toEqual(3);
482482
});
483483

484+
it('should create the chart with custom scale ids without axis or position options', function() {
485+
function createChart() {
486+
return window.acquireChart({
487+
type: 'scatter',
488+
data: {
489+
datasets: [{
490+
data: [{x: 0, y: 0}, {x: 1, y: 1}, {x: 2, y: 2}],
491+
xAxisID: 'customIDx',
492+
yAxisID: 'customIDy'
493+
}]
494+
},
495+
options: {
496+
scales: {
497+
customIDx: {
498+
type: 'linear',
499+
display: false
500+
},
501+
customIDy: {
502+
type: 'linear',
503+
display: false
504+
}
505+
}
506+
}
507+
});
508+
}
509+
510+
expect(createChart).not.toThrow();
511+
});
512+
484513
it('should default to one layer for custom scales', function() {
485514
class CustomScale extends Chart.Scale {
486515
draw() {}
@@ -514,6 +543,63 @@ describe('Core.scale', function() {
514543
expect(scale._layers()[0].z).toEqual(20);
515544
});
516545

546+
it('should default to one layer for custom scales for axis', function() {
547+
class CustomScale1 extends Chart.Scale {
548+
draw() {}
549+
convertTicksToLabels() {
550+
return ['tick'];
551+
}
552+
}
553+
CustomScale1.id = 'customScale1';
554+
CustomScale1.defaults = {axis: 'x'};
555+
Chart.register(CustomScale1);
556+
557+
var chart = window.acquireChart({
558+
type: 'line',
559+
options: {
560+
scales: {
561+
my: {
562+
type: 'customScale1',
563+
grid: {
564+
z: 10
565+
},
566+
ticks: {
567+
z: 20
568+
}
569+
}
570+
}
571+
}
572+
});
573+
574+
var scale = chart.scales.my;
575+
expect(scale._layers().length).toEqual(1);
576+
expect(scale._layers()[0].z).toEqual(20);
577+
});
578+
579+
it('should fail for custom scales without any axis or position', function() {
580+
class CustomScale2 extends Chart.Scale {
581+
draw() {}
582+
}
583+
CustomScale2.id = 'customScale2';
584+
CustomScale2.defaults = {};
585+
Chart.register(CustomScale2);
586+
587+
function createChart() {
588+
return window.acquireChart({
589+
type: 'line',
590+
options: {
591+
scales: {
592+
my: {
593+
type: 'customScale2'
594+
}
595+
}
596+
}
597+
});
598+
}
599+
600+
expect(createChart).toThrow(new Error('Cannot determine type of \'my\' axis. Please provide \'axis\' or \'position\' option.'));
601+
});
602+
517603
it('should return 3 layers when z is not equal between ticks and grid', function() {
518604
var chart = window.acquireChart({
519605
type: 'line',

0 commit comments

Comments
 (0)