Skip to content

feat: allow disabling of axis labels using text={false} or text={null} #95

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

Merged
merged 3 commits into from
May 29, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib/marks/AxisX.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
tickClass?: ConstantAccessor<string>;
/** ticks is a shorthand for defining data, tickCount or interval */
ticks?: number | string | RawValue[];
/** set to false or null to disable tick labels */
text: boolean | null;
} & XOR<
{
/** approximate number of ticks to be generated */
Expand Down Expand Up @@ -79,6 +81,7 @@
class: className,
tickCount = typeof magicTicks === 'number' ? magicTicks : undefined,
tickSpacing,
text = true,
...options
}: AxisXMarkProps = $props();

Expand Down Expand Up @@ -220,6 +223,7 @@
{tickPadding}
{tickFontSize}
{tickClass}
{text}
{options}
title={useTitle}
{plot} />
Expand Down
4 changes: 4 additions & 0 deletions src/lib/marks/AxisY.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
tickClass?: ConstantAccessor<string>;
/** ticks is a shorthand for defining data, tickCount or interval */
ticks?: number | string | RawValue[];
/** set to false or null to disable tick labels */
text: boolean | null;
} & XOR<
{
/** approximate number of ticks to be generated */
Expand Down Expand Up @@ -78,6 +80,7 @@
tickClass,
tickCount = typeof magicTicks === 'number' ? magicTicks : undefined,
tickSpacing,
text = true,
...options
}: AxisYMarkProps = $props();

Expand Down Expand Up @@ -200,6 +203,7 @@
{tickFontSize}
{tickClass}
{options}
{text}
title={useTitle}
{plot} />
{/if}
Expand Down
112 changes: 59 additions & 53 deletions src/lib/marks/helpers/BaseAxisX.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
dy: ConstantAccessor<number>;
filter: ChannelAccessor;
};
text: boolean;
plot: PlotState;
};

Expand All @@ -50,7 +51,8 @@
height,
options,
plot,
title
title,
text = true
}: BaseAxisXProps = $props();

function splitTick(tick: string | string[]) {
Expand Down Expand Up @@ -86,22 +88,25 @@
})
);
const T = tickObjects.length;
for (let i = 0; i < T; i++) {
let j = i;
// find the preceding tick that was not hidden
do {
j--;
} while (j >= 0 && tickObjects[j].hidden);
if (j >= 0) {
const tickLabelSpace = Math.abs(tickObjects[i].x - tickObjects[j].x);
tickObjects[i].hidden = tickLabelSpace < 15;
if (text) {
for (let i = 0; i < T; i++) {
let j = i;
// find the preceding tick that was not hidden
do {
j--;
} while (j >= 0 && tickObjects[j].hidden);
if (j >= 0) {
const tickLabelSpace = Math.abs(tickObjects[i].x - tickObjects[j].x);
tickObjects[i].hidden = tickLabelSpace < 15;
}
}
}
return tickObjects;
});

$effect(() => {
untrack(() => [$autoMarginTop, $autoMarginBottom]);
if (!text) return;
const outsideTextAnchor = anchor === 'top' ? 'end' : 'start';
// measure tick label heights
const maxLabelHeight =
Expand Down Expand Up @@ -144,26 +149,7 @@
<g class="axis-x">
{#each positionedTicks as tick, t (t)}
{#if testFilter(tick.value, options) && !tick.hidden}
{@const textLines = tick.text}
{@const prevTextLines = t && positionedTicks[t - 1].text}
{@const fontSize = resolveProp(tickFontSize, tick)}
{@const tickClass_ = resolveProp(tickClass, tick.value)}
{@const estLabelWidth = max(textLines.map((t) => t.length)) * fontSize * 0.2}
{@const moveDown =
(tickSize + tickPadding + (tickRotate !== 0 ? tickFontSize * 0.35 : 0)) *
(anchor === 'bottom' ? 1 : -1)}
{@const [textStyle, textClass] = resolveStyles(
plot,
tick,
{
fontVariant: isQuantitative ? 'tabular-nums' : 'normal',
...options,
fontSize: tickFontSize,
stroke: null
},
'fill',
{ x: true }
)}
<g
class="tick {tickClass_ || ''}"
transform="translate({tick.x + tick.dx}, {tickY + tick.dy})"
Expand All @@ -182,31 +168,51 @@
y2={anchor === 'bottom' ? tickSize : -tickSize} />
{/if}

<text
bind:this={tickTextElements[t]}
transform="translate(0, {moveDown}) rotate({tickRotate})"
style={textStyle}
class={textClass}
x={0}
y={0}
dominant-baseline={tickRotate !== 0
? 'central'
: anchor === 'bottom'
? 'hanging'
: 'auto'}>
{#if ticks.length > 0 || t === 0 || t === ticks.length - 1}
{#if textLines.length === 1}
{textLines[0]}
{:else}
{#each textLines as line, i (i)}
<tspan x="0" dy={i ? 12 : 0}
>{!prevTextLines || prevTextLines[i] !== line
? line
: ''}</tspan>
{/each}
{#if text}
{@const textLines = tick.text}
{@const prevTextLines = t && positionedTicks[t - 1].text}

{@const moveDown =
(tickSize + tickPadding + (tickRotate !== 0 ? tickFontSize * 0.35 : 0)) *
(anchor === 'bottom' ? 1 : -1)}
{@const [textStyle, textClass] = resolveStyles(
plot,
tick,
{
fontVariant: isQuantitative ? 'tabular-nums' : 'normal',
...options,
fontSize: tickFontSize,
stroke: null
},
'fill',
{ x: true }
)}
<text
bind:this={tickTextElements[t]}
transform="translate(0, {moveDown}) rotate({tickRotate})"
style={textStyle}
class={textClass}
x={0}
y={0}
dominant-baseline={tickRotate !== 0
? 'central'
: anchor === 'bottom'
? 'hanging'
: 'auto'}>
{#if ticks.length > 0 || t === 0 || t === ticks.length - 1}
{#if textLines.length === 1}
{textLines[0]}
{:else}
{#each textLines as line, i (i)}
<tspan x="0" dy={i ? 12 : 0}
>{!prevTextLines || prevTextLines[i] !== line
? line
: ''}</tspan>
{/each}
{/if}
{/if}
{/if}
</text>
</text>
{/if}
</g>
{/if}
{/each}
Expand Down
42 changes: 24 additions & 18 deletions src/lib/marks/helpers/BaseAxisY.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
dy: ConstantAccessor<number>;
};
plot: PlotState;
text: boolean | null;
};

let {
Expand All @@ -46,7 +47,8 @@
width,
title,
plot,
options
options,
text = true
}: BaseAxisYProps = $props();

const LINE_ANCHOR = {
Expand All @@ -67,16 +69,18 @@
element: null as SVGTextElement | null
};
});
const T = tickObjects.length;
for (let i = 0; i < T; i++) {
let j = i;
// find the preceding tick that was not hidden
do {
j--;
} while (j >= 0 && tickObjects[j].hidden);
if (j >= 0) {
const tickLabelSpace = Math.abs(tickObjects[i].y - tickObjects[j].y);
tickObjects[i].hidden = tickLabelSpace < 15;
if (text) {
const T = tickObjects.length;
for (let i = 0; i < T; i++) {
let j = i;
// find the preceding tick that was not hidden
do {
j--;
} while (j >= 0 && tickObjects[j].hidden);
if (j >= 0) {
const tickLabelSpace = Math.abs(tickObjects[i].y - tickObjects[j].y);
tickObjects[i].hidden = tickLabelSpace < 15;
}
}
}
return tickObjects;
Expand Down Expand Up @@ -176,13 +180,15 @@
class={tickLineClass}
x2={anchor === 'left' ? -tickSize : tickSize} />
{/if}
<text
bind:this={tickTexts[t]}
class={[textClass, { 'is-left': anchor === 'left' }]}
style={textStyle}
x={(tickSize + tickPadding) * (anchor === 'left' ? -1 : 1)}
dominant-baseline={LINE_ANCHOR[lineAnchor]}
>{Array.isArray(tick.text) ? tick.text.join(' ') : tick.text}</text>
{#if text}
<text
bind:this={tickTexts[t]}
class={[textClass, { 'is-left': anchor === 'left' }]}
style={textStyle}
x={(tickSize + tickPadding) * (anchor === 'left' ? -1 : 1)}
dominant-baseline={LINE_ANCHOR[lineAnchor]}
>{Array.isArray(tick.text) ? tick.text.join(' ') : tick.text}</text>
{/if}
</g>
{/if}
{/each}
Expand Down
19 changes: 19 additions & 0 deletions src/routes/examples/axis/major-minor.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script module>
export const title = 'Major and minor ticks';
export const description =
'You can add minor ticks lines by adding a second axis and disabling the text labels.';
</script>

<script>
import { Plot, AxisX, AxisY, Line } from 'svelteplot';
import { page } from '$app/state';
let { aapl } = $derived(page.data.data);
</script>

<Plot grid inset={10}>
<AxisX />
<AxisX interval="1 month" text={false} tickSize={3} />
<AxisY />
<AxisY interval={5} text={false} tickSize={3} />
<Line data={aapl} x="Date" y="Close" />
</Plot>
23 changes: 23 additions & 0 deletions src/tests/axisX.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,27 @@ describe('AxisX mark', () => {
expect(ticks.length).toBe(5);
expect(tickValues).toStrictEqual(['Jan2000', 'Jul', 'Jan2001', 'Jul', 'Jan2002']);
});

it('disable tick texts', () => {
const { container } = render(AxisXTest, {
props: {
plotArgs: {
width: 400,
x: { domain: [new Date(2000, 0, 1), new Date(2002, 0, 1)] }
},
axisArgs: { ticks: '1 month', text: null }
}
});

const ticks = container.querySelectorAll('g.axis-x > g.tick') as NodeListOf<SVGGElement>;
const tickLines = container.querySelectorAll(
'g.axis-x > g.tick line'
) as NodeListOf<SVGLineElement>;
const tickLabels = container.querySelectorAll(
'g.axis-x > g.tick text'
) as NodeListOf<SVGTextElement>;
expect(ticks.length).toBe(25);
expect(tickLines.length).toBe(25);
expect(tickLabels.length).toBe(0);
});
});
Binary file added static/examples/axis/major-minor.dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/examples/axis/major-minor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified static/examples/axis/tick-spacing.dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified static/examples/axis/tick-spacing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/examples/dot/2-symbol-channel.dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/examples/dot/2-symbol-channel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy