Skip to content

feature: apply mark defaults #110

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 21 commits into from
Jun 5, 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: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
onlyBuiltDependencies:
- puppeteer
6 changes: 4 additions & 2 deletions screenshot-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ const takeScreenshot = async (page, urlPath, outputPath, isDarkMode = false) =>
const finalOutputPath = outputPath.replace('.png', `${themeSuffix}.png`);

// Wait for the Plot component to be rendered
await page.waitForSelector('.content > figure.svelteplot', { timeout: 10000 });
await page.waitForSelector('.content figure.svelteplot ', {
timeout: 10000
});

// Toggle dark mode if needed
if (isDarkMode) {
Expand All @@ -112,7 +114,7 @@ const takeScreenshot = async (page, urlPath, outputPath, isDarkMode = false) =>

// Get the Plot SVG element
const elementHandle = await page.evaluateHandle(() =>
document.querySelector('.content > figure.svelteplot > .plot-body > svg')
document.querySelector('.content .screenshot')
);

// Take a screenshot of the element
Expand Down
3 changes: 2 additions & 1 deletion src/lib/Plot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<script lang="ts">
import Plot from './core/Plot.svelte';

import type { PlotOptions } from './types.js';
import type { PlotDefaults, PlotOptions } from './types.js';

// implicit marks
import AxisX from './marks/AxisX.svelte';
Expand All @@ -28,6 +28,7 @@
import { autoScale, autoScaleColor } from './helpers/autoScales.js';
import { namedProjection } from './helpers/autoProjection.js';
import { isObject } from './helpers/index.js';
import { getContext } from 'svelte';

let {
header: userHeader,
Expand Down
53 changes: 35 additions & 18 deletions src/lib/core/Plot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,17 @@
const maxMarginBottom = $derived(Math.max(...$autoMarginBottom.values()));
const maxMarginTop = $derived(Math.max(...$autoMarginTop.values()));

const USER_DEFAULTS = getContext<Partial<PlotDefaults>>('svelteplot/defaults') || {};

// default settings in the plot and marks can be overwritten by
// defining the svelteplot/defaults context outside of Plot
const DEFAULTS: PlotDefaults = {
axisXAnchor: 'bottom',
axisYAnchor: 'left',
xTickSpacing: 80,
yTickSpacing: 50,
height: 350,
initialWidth: 500,
inset: 0,
colorScheme: 'turbo',
unknown: '#cccccc99',
dotRadius: 3,
frame: false,
axes: true,
grid: false,

categoricalColorScheme: 'observable10',
pointScaleHeight: 18,
bandScaleHeight: 30,
Expand All @@ -77,7 +72,29 @@
compactDisplay: 'short'
},
markerDotRadius: 3,
...getContext<Partial<PlotDefaults>>('svelteplot/defaults')
...USER_DEFAULTS,
axisX: {
anchor: 'bottom',
implicit: true,
...USER_DEFAULTS.axis,
...USER_DEFAULTS.axisX
},
axisY: {
anchor: 'left',
implicit: true,
...USER_DEFAULTS.axis,
...USER_DEFAULTS.axisY
},
gridX: {
implicit: false,
...USER_DEFAULTS.grid,
...USER_DEFAULTS.gridX
},
gridY: {
implicit: false,
...USER_DEFAULTS.grid,
...USER_DEFAULTS.gridY
}
};

let {
Expand Down Expand Up @@ -390,16 +407,16 @@
? margins
: Math.max(5, maxMarginBottom),
inset: isOneDimensional ? 10 : DEFAULTS.inset,
grid: DEFAULTS.grid,
axes: DEFAULTS.axes,
frame: DEFAULTS.frame,
grid: (DEFAULTS.gridX?.implicit ?? false) && (DEFAULTS.gridY?.implicit ?? false),
axes: (DEFAULTS.axisX?.implicit ?? false) && (DEFAULTS.axisY?.implicit ?? false),
frame: DEFAULTS.frame?.implicit ?? false,
projection: null,
aspectRatio: null,
facet: {},
padding: 0.1,
x: {
type: 'auto',
axis: autoXAxis ? DEFAULTS.axisXAnchor : false,
axis: DEFAULTS.axisX.implicit && autoXAxis ? DEFAULTS.axisX.anchor : false,
labelAnchor: 'auto',
reverse: false,
clamp: false,
Expand All @@ -408,13 +425,13 @@
round: false,
percent: false,
align: 0.5,
tickSpacing: DEFAULTS.xTickSpacing,
tickSpacing: DEFAULTS.axisX.tickSpacing ?? 80,
tickFormat: 'auto',
grid: false
grid: DEFAULTS.gridX.implicit ?? false
},
y: {
type: 'auto',
axis: autoYAxis ? DEFAULTS.axisYAnchor : false,
axis: DEFAULTS.axisY.implicit && autoYAxis ? DEFAULTS.axisY.anchor : false,
labelAnchor: 'auto',
reverse: false,
clamp: false,
Expand All @@ -423,9 +440,9 @@
round: false,
percent: false,
align: 0.5,
tickSpacing: DEFAULTS.yTickSpacing,
tickSpacing: DEFAULTS.axisY.tickSpacing ?? 50,
tickFormat: 'auto',
grid: false
grid: DEFAULTS.gridY.implicit ?? false
},
opacity: {
type: 'linear',
Expand Down
16 changes: 13 additions & 3 deletions src/lib/marks/Area.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,30 @@
ConstantAccessor,
ChannelAccessor,
ScaledDataRecord,
LinkableMarkProps
LinkableMarkProps,
PlotDefaults
} from '../types.js';
import type { RawValue } from '$lib/types.js';
import type { StackOptions } from '$lib/transforms/stack.js';

let {
let markProps: AreaMarkProps = $props();

const DEFAULTS = {
fill: 'currentColor',
curve: 'linear',
tension: 0,
...getContext<PlotDefaults>('svelteplot/_defaults').area
};

const {
data,
/** the curve */
curve = 'linear',
tension = 0,
class: className = '',
canvas = false,
...options
}: AreaMarkProps = $props();
}: AreaMarkProps = $derived({ ...DEFAULTS, ...markProps });

const { getPlotState } = getContext<PlotContext>('svelteplot');
const plot = $derived(getPlotState());
Expand Down
10 changes: 7 additions & 3 deletions src/lib/marks/AreaX.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
import { renameChannels } from '$lib/transforms/rename.js';
import { stackX } from '$lib/transforms/stack.js';
import { recordizeX } from '$lib/transforms/recordize.js';
import type { ChannelAccessor } from '../types.js';
import type { ChannelAccessor, PlotDefaults } from '../types.js';
import { getContext } from 'svelte';

type AreaXProps = Omit<AreaMarkProps, 'y1' | 'y2'> & {
x?: ChannelAccessor;
y?: ChannelAccessor;
};

// we're discarding y1 and y2 props since they are not
let { data, stack, ...options }: AreaXProps = $props();
let markProps: AreaMarkProps = $props();

const DEFAULTS = getContext<PlotDefaults>('svelteplot/_defaults').areaX;

const { data, stack, ...options }: AreaMarkProps = $derived({ ...DEFAULTS, ...markProps });

const args = $derived(
renameChannels<AreaXProps>(
Expand Down
9 changes: 7 additions & 2 deletions src/lib/marks/AreaY.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import { renameChannels } from '$lib/transforms/rename.js';
import { stackY } from '$lib/transforms/stack.js';
import { recordizeY } from '$lib/transforms/recordize.js';
import type { ChannelAccessor } from '../types.js';
import type { ChannelAccessor, PlotDefaults } from '../types.js';
import { getContext } from 'svelte';

/**
* AreaY mark foo
Expand All @@ -17,7 +18,11 @@
y?: ChannelAccessor;
};

let { data, stack, ...options }: AreaYProps = $props();
let markProps: AreaMarkProps = $props();

const DEFAULTS = getContext<PlotDefaults>('svelteplot/_defaults').areaY;

const { data, stack, ...options }: AreaMarkProps = $derived({ ...DEFAULTS, ...markProps });

const args = $derived(
renameChannels<AreaYProps>(
Expand Down
31 changes: 24 additions & 7 deletions src/lib/marks/Arrow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@
</script>

<script lang="ts">
import { getContext, type Snippet } from 'svelte';
import { getContext } from 'svelte';
import type {
PlotContext,
DataRecord,
BaseMarkProps,
ConstantAccessor,
ChannelAccessor,
RawValue
RawValue,
PlotDefaults
} from '../types.js';
import { resolveChannel, resolveProp, resolveStyles } from '../helpers/resolve.js';
import { coalesce, maybeData, maybeNumber } from '../helpers/index.js';
Expand All @@ -55,7 +56,23 @@
import { addEventHandlers } from './helpers/events.js';
import GroupMultiple from './helpers/GroupMultiple.svelte';

let { data = [{}], class: className = null, ...options }: ArrowMarkProps = $props();
let markProps: ArrowMarkProps = $props();

const DEFAULTS = {
headAngle: 60,
headLength: 8,
inset: 0,
...getContext<PlotDefaults>('svelteplot/_defaults').arrow
};

const {
data = [{}],
class: className = '',
...options
}: ArrowMarkProps = $derived({
...DEFAULTS,
...markProps
});

const { getPlotState } = getContext<PlotContext>('svelteplot');
const plot = $derived(getPlotState());
Expand All @@ -68,7 +85,7 @@
: maybeData(data)
);

const args: ArrowProps = $derived(
const args: ArrowMarkProps = $derived(
replaceChannels({ data: sorted, ...options }, { y: ['y1', 'y2'], x: ['x1', 'x2'] })
);
</script>
Expand All @@ -78,16 +95,16 @@
required={['x1', 'x2', 'y1', 'y2']}
channels={['x1', 'y1', 'x2', 'y2', 'opacity', 'stroke', 'strokeOpacity']}
{...args}>
{#snippet children({ mark, usedScales, scaledData })}
{#snippet children({ usedScales, scaledData })}
{@const sweep = maybeSweep(args.sweep)}
<GroupMultiple class="arrow" length={scaledData.length}>
{#each scaledData as d, i (i)}
{#if d.valid}
{@const inset = resolveProp(args.inset, d.datum, 0)}
{@const insetStart = resolveProp(args.insetStart, d.datum)}
{@const insetEnd = resolveProp(args.insetEnd, d.datum)}
{@const headAngle = resolveProp(args.headAngle, d.datum, 60)}
{@const headLength = resolveProp(args.headLength, d.datum, 8)}
{@const headAngle = resolveProp(args.headAngle, d.datum)}
{@const headLength = resolveProp(args.headLength, d.datum)}
{@const bend = resolveProp(args.bend, d.datum, 0)}
{@const strokeWidth = resolveProp(args.strokeWidth, d.datum, 1)}
{@const arrPath = arrowPath(
Expand Down
39 changes: 19 additions & 20 deletions src/lib/marks/AxisX.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,11 @@
ticks?: number | string | RawValue[];
/** set to false or null to disable tick labels */
text: boolean | null;
} & XOR<
{
/** approximate number of ticks to be generated */
tickCount?: number;
},
{
/** approximate number of pixels between generated ticks */
tickSpacing?: number;
}
>;
/** approximate number of ticks to be generated */
tickCount?: number;
/** approximate number of pixels between generated ticks */
tickSpacing?: number;
};
</script>

<script lang="ts">
Expand All @@ -49,32 +44,36 @@
RawValue,
ConstantAccessor,
FacetContext,
DefaultOptions
PlotDefaults,
ChannelName
} from '../types.js';
import autoTimeFormat from '$lib/helpers/autoTimeFormat.js';
import { derived } from 'svelte/store';
import { autoTicks } from '$lib/helpers/autoTicks.js';
import { resolveScaledStyles } from '$lib/helpers/resolve.js';

const DEFAULTS = {
let markProps: AxisXMarkProps = $props();

const DEFAULTS: Omit<AxisXMarkProps, 'data' | ChannelName> = {
tickSize: 6,
tickPadding: 3,
tickFontSize: 11,
axisXAnchor: 'bottom',
...getContext<Partial<DefaultOptions>>('svelteplot/_defaults')
anchor: 'bottom',
...getContext<PlotDefaults>('svelteplot/_defaults').axis,
...getContext<PlotDefaults>('svelteplot/_defaults').axisX
};

let {
const {
ticks: magicTicks,
data = Array.isArray(magicTicks) ? magicTicks : [],
automatic = false,
title,
anchor = DEFAULTS.axisXAnchor as 'top' | 'bottom',
anchor,
facetAnchor = 'auto',
interval = typeof magicTicks === 'string' ? magicTicks : undefined,
tickSize = DEFAULTS.tickSize,
tickFontSize = DEFAULTS.tickFontSize,
tickPadding = DEFAULTS.tickPadding,
tickSize,
tickFontSize,
tickPadding,
labelAnchor,
tickFormat,
tickClass,
Expand All @@ -83,7 +82,7 @@
tickSpacing,
text = true,
...options
}: AxisXMarkProps = $props();
}: AxisXMarkProps = $derived({ ...DEFAULTS, ...markProps });

const { getPlotState } = getContext<PlotContext>('svelteplot');
const plot = $derived(getPlotState());
Expand Down
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