Skip to content

Fix/axis types #121

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 17 commits into from
Jun 18, 2025
Prev Previous commit
Next Next commit
fix axis styles
  • Loading branch information
gka committed Jun 18, 2025
commit c5a00a40dc90a366e945f851978815c70a0a0740
2 changes: 2 additions & 0 deletions src/lib/core/FacetAxes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
scaleType="band"
ticks={fxValues}
tickFormat={(d) => d}
tickFontSize={11}
Copy link
Preview

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The hardcoded font size (11) is a magic number; consider making this configurable via a prop or theme token to improve flexibility and maintainability.

Suggested change
tickFontSize={11}
tickFontSize={tickFontSize}

Copilot uses AI. Check for mistakes.

tickSize={0}
tickPadding={5}
anchor={plot.options.fx.axis}
Expand All @@ -53,6 +54,7 @@
scaleType="band"
ticks={fyValues}
tickFormat={(d) => d}
tickFontSize={11}
tickSize={0}
tickPadding={5}
anchor={plot.options.fy.axis}
Expand Down
33 changes: 18 additions & 15 deletions src/lib/marks/AxisX.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
> {
data?: Datum[];
automatic?: boolean;
title?: string;
title?: string | false | null;
anchor?: 'top' | 'bottom';
interval?: string | number;
facetAnchor?: 'auto' | 'top-empty' | 'bottom-empty' | 'top' | 'bottom';
labelAnchor?: 'auto' | 'left' | 'center' | 'right';
tickSize?: number;
tickFontSize?: ConstantAccessor<number, Datum>;
titleFontSize?: number;
tickPadding?: number;
tickFormat?:
| 'auto'
Expand All @@ -56,6 +57,7 @@
tickSize: 6,
tickPadding: 3,
tickFontSize: 11,
titleFontSize: 11,
anchor: 'bottom',
...getContext<PlotDefaults>('svelteplot/_defaults').axis,
...getContext<PlotDefaults>('svelteplot/_defaults').axisX
Expand Down Expand Up @@ -140,18 +142,19 @@
const isQuantitative = $derived(scaleType !== 'point' && scaleType !== 'band');

const useTitle = $derived(
title ||
(optionsLabel === null
? null
: optionsLabel !== undefined
? optionsLabel
: plot.scales.x.autoTitle
? isQuantitative
? plot.options.x?.reverse
? `← ${plot.scales.x.autoTitle}${plot.options.x.percent ? ' (%)' : ''}`
: `${plot.scales.x.autoTitle}${plot.options.x.percent ? ' (%)' : ''} →`
: plot.scales.x.autoTitle
: '')
title !== undefined
? title || ''
: optionsLabel === null
? null
: optionsLabel !== undefined
? optionsLabel
: plot.scales.x.autoTitle
? isQuantitative
? plot.options.x?.reverse
? `← ${plot.scales.x.autoTitle}${plot.options.x.percent ? ' (%)' : ''}`
: `${plot.scales.x.autoTitle}${plot.options.x.percent ? ' (%)' : ''} →`
: plot.scales.x.autoTitle
: ''
);

const useLabelAnchor = $derived(labelAnchor || plot.options?.x?.labelAnchor || 'auto');
Expand Down Expand Up @@ -188,8 +191,10 @@
style={resolveScaledStyles(
null,
{
opacity: 0.8,
...options,
stroke: null,
fontSize: options.titleFontSize || 11,
textAnchor:
titleAlign === 'right'
? 'end'
Expand Down Expand Up @@ -231,8 +236,6 @@

<style>
text {
font-size: 11px;
opacity: 0.8;
fill: currentColor;
}
</style>
48 changes: 33 additions & 15 deletions src/lib/marks/AxisY.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
> {
data?: Datum[];
automatic?: boolean;
title?: string;
title?: string | false | null;
anchor?: 'left' | 'right';
facetAnchor?: 'auto' | 'left' | 'right' | 'left-empty' | 'right-empty';
lineAnchor?: 'top' | 'center' | 'bottom';
interval?: string | number;
labelAnchor?: 'auto' | 'left' | 'center' | 'right';
textAnchor?: 'auto' | 'start' | 'middle' | 'end';
tickSize?: number;
tickFontSize?: ConstantAccessor<number, Datum>;
titleFontSize?: number;
tickPadding?: number;
tickFormat?:
| 'auto'
Expand All @@ -43,7 +45,7 @@
/** ticks is a shorthand for defining data, tickCount or interval */
ticks?: number | string | Datum[];
/** set to false or null to disable tick labels */
text: boolean | null;
text?: boolean | null;
/** approximate number of ticks to be generated */
tickCount?: number;
/** approximate number of pixels between generated ticks */
Expand All @@ -57,6 +59,7 @@
tickPadding: 3,
tickFontSize: 11,
anchor: 'left',
textAnchor: 'auto',
...getContext<PlotDefaults>('svelteplot/_defaults').axis,
...getContext<PlotDefaults>('svelteplot/_defaults').axisY
};
Expand All @@ -71,6 +74,7 @@
facetAnchor = 'auto',
interval = typeof magicTicks === 'string' ? magicTicks : undefined,
lineAnchor = 'center',
textAnchor,
tickSize,
tickFontSize,
tickPadding,
Expand Down Expand Up @@ -138,14 +142,15 @@
const optionsLabel = $derived(plot.options.y.label);

const useTitle = $derived(
title ||
(optionsLabel === null
? null
: optionsLabel !== undefined
? optionsLabel
: plot.scales.y.autoTitle
? `↑ ${plot.scales.y.autoTitle}${plot.options.y.percent ? ' (%)' : ''}`
: '')
title !== undefined
? title || ''
: optionsLabel === null
? null
: optionsLabel !== undefined
? optionsLabel
: plot.scales.y.autoTitle
? `↑ ${plot.scales.y.autoTitle}${plot.options.y.percent ? ' (%)' : ''}`
: ''
);

const { getFacetState } = getContext<FacetContext>('svelteplot/facet');
Expand Down Expand Up @@ -176,22 +181,37 @@
<text
style={resolveScaledStyles(
null,
{ ...options, stroke: null, textAnchor: anchor === 'left' ? 'start' : 'end' },
{
opacity: 0.8,
...options,
fontSize: options.titleFontSize ?? 11,
fill: 'currentColor',
stroke: null,
textAnchor: anchor === 'left' ? 'start' : 'end'
},
{},
plot,
'fill'
)}
x={anchor === 'left' ? 0 : plot.width}
y={5}
class="axis-x-title"
dominant-baseline="hanging">{useTitle}</text>
dominant-baseline="hanging">{options.opacity}-{useTitle}</text>
{/if}
{#if showAxis}
<BaseAxisY
{anchor}
{className}
{lineAnchor}
{options}
options={{
...options,
textAnchor:
textAnchor == null || textAnchor === 'auto'
? anchor === 'left'
? 'end'
: 'start'
: textAnchor
}}
{plot}
{text}
{tickClass}
Expand All @@ -210,8 +230,6 @@

<style>
text {
font-size: 11px;
opacity: 0.8;
fill: currentColor;
}
</style>
6 changes: 4 additions & 2 deletions src/lib/marks/helpers/BaseAxisX.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@
tick,
options,
'stroke',
{ x: true }
{ x: true },
true
)}
<line
style={tickLineStyle}
Expand All @@ -185,7 +186,8 @@
stroke: null
},
'fill',
{ x: true }
{ x: true },
true
)}
<text
bind:this={tickTextElements[t]}
Expand Down
14 changes: 8 additions & 6 deletions src/lib/marks/helpers/BaseAxisY.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
tickSize: number;
tickPadding: number;
tickFontSize: ConstantAccessor<number>;
tickClass: ConstantAccessor<number>;
marginLeft: number;
width: number;
title: string | null;
options: {
dx: ConstantAccessor<number>;
dy: ConstantAccessor<number>;
textAnchor: 'start' | 'middle' | 'end';
};
plot: PlotState;
text: boolean | null;
Expand Down Expand Up @@ -93,6 +95,8 @@
// generate id used for registering margins
const id = randomId();

$inspect({ options });

const { autoMarginLeft, autoMarginRight, autoMarginTop } =
getContext<AutoMarginStores>('svelteplot/autoMargins');

Expand Down Expand Up @@ -160,7 +164,8 @@
stroke: null
},
'fill',
{ y: true }
{ y: true },
true
)}
<g
class="tick {tickClass_ || ''}"
Expand All @@ -173,7 +178,8 @@
tick,
options,
'stroke',
{ y: true }
{ y: true },
true
)}
<line
style={tickLineStyle}
Expand All @@ -199,10 +205,6 @@
stroke: currentColor;
}
text {
opacity: 0.8;
fill: currentColor;
}
text.is-left {
text-anchor: end;
}
</style>
2 changes: 1 addition & 1 deletion src/routes/examples/axis/ticks-inside.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<script>
import { Plot, AxisY, Line } from 'svelteplot';
import { page } from '$app/state';
import { setContext } from 'svelte';

let { aapl } = $derived(page.data.data);
</script>
Expand All @@ -17,7 +18,6 @@
tickSize={0}
tickPadding={0}
dy={-5}
title={false}
lineAnchor="bottom"
textAnchor="start" />
<Line data={aapl} x="Date" y="Close" />
Expand Down
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