Skip to content

feature: axis tick count #78

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 4 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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"svelte-highlight": "^7.8.3",
"svg-path-parser": "^1.1.0",
"topojson-client": "^3.1.0",
"ts-essentials": "^10.0.4",
"tslib": "^2.8.1",
"typedoc": "^0.28.5",
"typedoc-plugin-markdown": "^4.6.3",
Expand Down
77 changes: 26 additions & 51 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions src/lib/marks/AxisX.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Renders a horizontal axis with labels and tick marks
-->
<script module lang="ts">
import type { XOR } from 'ts-essentials';
export type AxisXMarkProps = Omit<
BaseMarkProps,
'fill' | 'fillOpacity' | 'paintOrder' | 'title' | 'href' | 'target'
Expand All @@ -22,7 +23,16 @@
| Intl.NumberFormatOptions
| ((d: RawValue) => string);
tickClass?: ConstantAccessor<string>;
};
} & XOR<
{
/** approximate number of ticks to be generated */
tickCount?: number;
},
{
/** approximate number of pixels between generated ticks */
tickSpacing?: number;
}
>;
</script>

<script lang="ts">
Expand Down Expand Up @@ -64,14 +74,20 @@
tickFormat,
tickClass,
class: className,
tickCount,
tickSpacing,
...options
}: AxisXMarkProps = $props();

const { getPlotState } = getContext<PlotContext>('svelteplot');
const plot = $derived(getPlotState());

const autoTickCount = $derived(
Math.max(3, Math.round(plot.facetWidth / plot.options.x.tickSpacing))
tickCount != null
? tickCount
: tickSpacing != null
? Math.max(3, Math.round(plot.facetWidth / tickSpacing))
: Math.max(3, Math.round(plot.facetWidth / plot.options.x.tickSpacing))
);

const ticks: RawValue[] = $derived(
Expand Down
23 changes: 20 additions & 3 deletions src/lib/marks/AxisY.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Renders a vertical axis with labels and tick marks
-->
<script module lang="ts">
import type { XOR } from 'ts-essentials';
export type AxisYMarkProps = Omit<
BaseMarkProps,
'fill' | 'fillOpacity' | 'paintOrder' | 'title' | 'href' | 'target'
Expand All @@ -23,7 +24,16 @@
| Intl.NumberFormatOptions
| ((d: RawValue) => string);
tickClass?: ConstantAccessor<string>;
};
} & XOR<
{
/** approximate number of ticks to be generated */
tickCount?: number;
},
{
/** approximate number of pixels between generated ticks */
tickSpacing?: number;
}
>;
</script>

<script lang="ts">
Expand Down Expand Up @@ -56,20 +66,27 @@
title,
anchor = DEFAULTS.axisYAnchor as 'left' | 'right',
facetAnchor = 'auto',
interval,
lineAnchor = 'center',
tickSize = DEFAULTS.tickSize,
tickFontSize = DEFAULTS.tickFontSize,
tickPadding = DEFAULTS.tickPadding,
tickFormat,
tickClass,
tickCount,
tickSpacing,
...options
}: AxisYMarkProps = $props();

const { getPlotState } = getContext<PlotContext>('svelteplot');
const plot = $derived(getPlotState());

const autoTickCount = $derived(
Math.max(2, Math.round(plot.facetHeight / plot.options.y.tickSpacing))
tickCount != null
? tickCount
: tickSpacing != null
? Math.max(3, Math.round(plot.facetHeight / tickSpacing))
: Math.max(2, Math.round(plot.facetHeight / plot.options.y.tickSpacing))
);

const ticks: RawValue[] = $derived(
Expand All @@ -80,7 +97,7 @@
autoTicks(
plot.scales.y.type,
plot.options.y.ticks,
plot.options.y.interval,
interval || plot.options.y.interval,
plot.scales.y.domain,
plot.scales.y.fn,
autoTickCount
Expand Down
15 changes: 15 additions & 0 deletions src/routes/examples/axis/tick-count.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script module>
export const title = 'Tick count';
</script>

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

<Plot marginTop={30}>
<AxisY tickCount={20} />
<AxisX tickCount={2} />
<Line data={aapl} x="Date" y="Close" />
</Plot>
15 changes: 15 additions & 0 deletions src/routes/examples/axis/tick-interval.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script module>
export const title = 'Tick interval';
</script>

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

<Plot>
<AxisX interval="6 months" />
<AxisY interval={25} />
<Line data={aapl} x="Date" y="Close" />
</Plot>
Loading
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