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

Axis ticks misaligned when using integersOnly flag #2311

Open
Tracked by #2320
nickofthyme opened this issue Jan 24, 2024 · 0 comments
Open
Tracked by #2320

Axis ticks misaligned when using integersOnly flag #2311

nickofthyme opened this issue Jan 24, 2024 · 0 comments
Assignees
Labels
:axis Axis related issue bug Something isn't working :xy Bar/Line/Area chart related

Comments

@nickofthyme
Copy link
Collaborator

Describe the issue

Using the integersOnly flag with a small domain (i.e. 0 to 0.7) the axis ticks become misaligned with the scaled data.

image

Notice the value of the left-most bar is 0.68574 but the top tick is displayed as 1 when the true value is 0.7.

Expected behaviour
Ticks are accurately displayed to align with the axis scale.

Additional context
This seems to be a bug in the visible_ticks.ts logic. When I disable the adaptiveTickCount it works as expected. Do you know what this adaptive logic is doing and why it's computed independently from the actual scale?

const fillLayer = (maxTickCountForLayer: number) => {
let fallbackAskedTickCount = 2;
let fallbackReceivedTickCount = Infinity;
if (adaptiveTickCount) {
let previousActualTickCount = NaN;
for (let triedTickCount = maxTickCountForLayer; triedTickCount >= 1; triedTickCount--) {
const scale = getScale(triedTickCount);
const actualTickCount = scale?.ticks().length ?? 0;
if (!scale || actualTickCount === previousActualTickCount || actualTickCount < 2) continue;
const raster = getMeasuredTicks(scale, scale.ticks(), undefined, 0, userProvidedLabelFormatter);
const nonZeroLengthTicks = raster.ticks.filter((tick) => tick.label.length > 0);
const uniqueLabels = new Set(raster.ticks.map((tick) => tick.label));
const areLabelsUnique = raster.ticks.length === uniqueLabels.size;
const areAdjacentTimeLabelsUnique =
scale.type === ScaleType.Time &&
!axisSpec.showDuplicatedTicks &&
(areLabelsUnique || raster.ticks.every((d, i, a) => i === 0 || d.label !== a[i - 1]?.label));
const atLeastTwoTicks = uniqueLabels.size >= 2;
const allTicksFit = !uniqueLabels.has('');
const compliant =
axisSpec &&
(scale.type === ScaleType.Time || atLeastTwoTicks) &&
(scale.type === ScaleType.Log || allTicksFit) &&
((scale.type === ScaleType.Time && (axisSpec.showDuplicatedTicks || areAdjacentTimeLabelsUnique)) ||
(scale.type === ScaleType.Log
? new Set(nonZeroLengthTicks.map((tick) => tick.label)).size === nonZeroLengthTicks.length
: areLabelsUnique));
previousActualTickCount = actualTickCount;
if (raster && compliant) {
return {
entry: {
...raster,
ticks: scale.type === ScaleType.Log ? raster.ticks : nonZeroLengthTicks,
},
fallbackAskedTickCount,
};
} else if (atLeastTwoTicks && uniqueLabels.size <= fallbackReceivedTickCount) {
// let's remember the smallest triedTickCount that yielded two distinct ticks
fallbackReceivedTickCount = uniqueLabels.size;
fallbackAskedTickCount = triedTickCount;
}
}
}
return { fallbackAskedTickCount };
};

So taking your example, like below, you can see the issue. But if I log the domain, range and ticks for all iterations of the y axis. You can see the first 3 are determined using the correct scale, the last 3 are from the code above, which appears to be walking the domain out from 0.7 to 0.8 to 1. This returns the ticks but these ticks are used with the scale computed originally with a different domain. 🤔 What the??

data: (5) [
  ['Sensor 1', 0.68574]
  ['Sensor 2', 0.5857399999999999]
  ['Sensor 3', 0.48573999999999995]
  ['Sensor 4', 0.38573999999999997]
  ['Sensor 5', 0.28574]
]

Tick iterations of y axis continuous scale
-------------------------------------------
 domain: (2) [0, 0.7]
 range: (2) [0, 1]
 ticks: (8) [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
-------------------------------------------
 domain: (2) [0, 0.7]
 range: (2) [307, 0]
 ticks: (8) [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
-------------------------------------------
 domain: (2) [0, 0.7]
 range: (2) [307, 0]
 ticks: (8) [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
-------------------------------------------
 domain: (2) [0, 0.8]
 range: (2) [307, 0]
 ticks: (5) [0, 0.2, 0.4, 0.6, 0.8]
-------------------------------------------
 domain: (2) [0, 0.8]
 range: (2) [307, 0]
 ticks: (5) [0, 0.2, 0.4, 0.6, 0.8]
-------------------------------------------
 domain: (2) [0, 1]
 range: (2) [307, 0]
 ticks: (3) [0, 0.5, 1]
@nickofthyme nickofthyme added bug Something isn't working :axis Axis related issue labels Jan 24, 2024
@markov00 markov00 self-assigned this Jan 26, 2024
@markov00 markov00 added the :xy Bar/Line/Area chart related label Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:axis Axis related issue bug Something isn't working :xy Bar/Line/Area chart related
Projects
None yet
Development

No branches or pull requests

2 participants