Skip to content

Commit 17e310e

Browse files
committed
fix(Axis): Default tickSpacing to null for categorical band scales, showing all ticks by default instead of reducing them. Use tickSpacing={80} to opt-in to tick reducing on categorical band scale axes.
1 parent f00228c commit 17e310e

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

.changeset/band-tick-spacing-default.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'layerchart': minor
33
---
44

5-
fix(Axis): Default `tickSpacing` to `null` for band scales, showing all ticks by default instead of thinning them. Use `tickSpacing={80}` to opt-in to tick thinning on band scale axes.
5+
fix(Axis): Default `tickSpacing` to `null` for categorical band scales, showing all ticks by default instead of reducing them. Use `tickSpacing={80}` to opt-in to tick reducing on categorical band scale axes.

packages/layerchart/src/lib/components/Axis.svelte

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,25 @@
182182
scaleProp ?? (['horizontal', 'angle'].includes(orientation) ? ctx.xScale : ctx.yScale)
183183
);
184184
185+
const interval = $derived(
186+
['horizontal', 'angle'].includes(orientation) ? ctx.xInterval : ctx.yInterval
187+
);
188+
189+
const defaultTickSpacing = $derived(
190+
['top', 'bottom', 'angle'].includes(placement)
191+
? 80
192+
: ['left', 'right', 'radius'].includes(placement)
193+
? 50
194+
: undefined
195+
);
196+
197+
// Disable tick thinning for categorical band scales (no interval), but keep spacing for date-based band scales
185198
const tickSpacing = $derived(
186199
tickSpacingProp !== undefined
187200
? tickSpacingProp
188-
: isScaleBand(scale)
201+
: isScaleBand(scale) && interval == null
189202
? null
190-
: ['top', 'bottom', 'angle'].includes(placement)
191-
? 80
192-
: ['left', 'right', 'radius'].includes(placement)
193-
? 50
194-
: undefined
195-
);
196-
const interval = $derived(
197-
['horizontal', 'angle'].includes(orientation) ? ctx.xInterval : ctx.yInterval
203+
: defaultTickSpacing
198204
);
199205
200206
// Default format to 'percentRound' for stackExpand layout considering axis direction
@@ -239,13 +245,23 @@
239245
return ctxSize;
240246
});
241247
248+
// Count used for tick thinning (null tickSpacing disables thinning)
242249
const tickCount = $derived(
243250
typeof ticks === 'number'
244251
? ticks
245252
: tickSpacing && effectiveSize
246253
? Math.round(effectiveSize / tickSpacing)
247254
: undefined
248255
);
256+
257+
// Count used for formatting (always based on default spacing so time formatting works)
258+
const formatCount = $derived(
259+
typeof ticks === 'number'
260+
? ticks
261+
: defaultTickSpacing && effectiveSize
262+
? Math.round(effectiveSize / defaultTickSpacing)
263+
: undefined
264+
);
249265
const tickVals = $derived.by(() => {
250266
let tickVals = autoTickVals(scale, ticks, tickCount);
251267
@@ -288,7 +304,7 @@
288304
autoTickFormat({
289305
scale,
290306
ticks,
291-
count: tickCount,
307+
count: formatCount,
292308
formatType: resolvedFormat,
293309
multiline: tickMultiline,
294310
placement,

0 commit comments

Comments
 (0)