Skip to content

Fix: derive band/point scale domain from interval #68

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 27, 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
150 changes: 81 additions & 69 deletions src/lib/Plot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -91,89 +91,101 @@
<!-- There's a bug triggering RangeError: Maximum call stack size exceeded
when using SveltePlot in ssr, so for now, we're disabling it -->

<Plot
{overlay}
{underlay}
{...restOptions}
header={userHeader ||
restOptions.title ||
restOptions.subtitle ||
restOptions.color?.legend ||
restOptions.symbol?.legend
? header
: null}
footer={userFooter || restOptions?.caption ? footer : null}
projection={projectionOpts}
implicitScales
{...scales}>
{#snippet children({
hasProjection,
hasExplicitAxisX,
hasExplicitAxisY,
hasExplicitGridX,
hasExplicitGridY,
options,
scales,
...restProps
})}
<svelte:boundary onerror={(err) => console.warn(err)}>
<!-- implicit axes -->
{#if !hasProjection && !hasExplicitAxisX}
{#if options.axes && (options.x.axis === 'top' || options.x.axis === 'both')}
<AxisX anchor="top" automatic />
<svelte:boundary>
<Plot
{overlay}
{underlay}
{...restOptions}
header={userHeader ||
restOptions.title ||
restOptions.subtitle ||
restOptions.color?.legend ||
restOptions.symbol?.legend
? header
: null}
footer={userFooter || restOptions?.caption ? footer : null}
projection={projectionOpts}
implicitScales
{...scales}>
{#snippet children({
hasProjection,
hasExplicitAxisX,
hasExplicitAxisY,
hasExplicitGridX,
hasExplicitGridY,
options,
scales,
...restProps
})}
<svelte:boundary onerror={(err) => console.warn(err)}>
<!-- implicit axes -->
{#if !hasProjection && !hasExplicitAxisX}
{#if options.axes && (options.x.axis === 'top' || options.x.axis === 'both')}
<AxisX anchor="top" automatic />
{/if}
{#if options.axes && (options.x.axis === 'bottom' || options.x.axis === 'both')}
<AxisX anchor="bottom" automatic />
{/if}
{/if}
{#if options.axes && (options.x.axis === 'bottom' || options.x.axis === 'both')}
<AxisX anchor="bottom" automatic />
{#if !hasProjection && !hasExplicitAxisY}
{#if options.axes && (options.y.axis === 'left' || options.y.axis === 'both')}
<AxisY anchor="left" automatic />
{/if}
{#if options.axes && (options.y.axis === 'right' || options.y.axis === 'both')}
<AxisY anchor="right" automatic />
{/if}
{/if}
{/if}
{#if !hasProjection && !hasExplicitAxisY}
{#if options.axes && (options.y.axis === 'left' || options.y.axis === 'both')}
<AxisY anchor="left" automatic />
<!-- implicit grids -->
{#if !hasExplicitGridX && (options.grid || options.x.grid)}
<GridX automatic />
{/if}
{#if options.axes && (options.y.axis === 'right' || options.y.axis === 'both')}
<AxisY anchor="right" automatic />
{#if !hasExplicitGridY && (options.grid || options.y.grid)}
<GridY automatic />
{/if}
{/if}
<!-- implicit grids -->
{#if !hasExplicitGridX && (options.grid || options.x.grid)}
<GridX automatic />
{/if}
{#if !hasExplicitGridY && (options.grid || options.y.grid)}
<GridY automatic />
{/if}
<!-- implicit frame -->
{#if options.frame}
<Frame automatic />
{/if}
{@render parentChildren?.({
options,
scales,
...restProps
})}
{#snippet failed(error, reset)}
<text class="error" transform="translate(10,10)">
{#each error.message.split('\n') as line, i (i)}
<tspan x="0" dy={i ? 14 : 0}>{line}</tspan>
{/each}
</text>{/snippet}
</svelte:boundary>
{/snippet}
{#snippet facetAxes()}
<FacetAxes />
<!-- implicit frame -->
{#if options.frame}
<Frame automatic />
{/if}
{@render parentChildren?.({
options,
scales,
...restProps
})}
{#snippet failed(error, reset)}
<text class="error" transform="translate(10,10)">
{#each error.message.split('\n') as line, i (i)}
<tspan x="0" dy={i ? 14 : 0}>{line}</tspan>
{/each}
</text>{/snippet}
</svelte:boundary>
{/snippet}
{#snippet facetAxes()}
<FacetAxes />
{/snippet}
</Plot>
{#snippet failed(error)}
<div class="error">Error: {error.message}</div>
{/snippet}
</Plot>
</svelte:boundary>

<style>
:root {
--plot-bg: white;
--plot-fg: currentColor;
}
text.error {
stroke: var(--plot-bg);
fill: crimson;
.error {
font-size: 11px;
stroke-width: 3px;
font-weight: bold;
}
text.error {
stroke: var(--plot-bg);
fill: crimson;
paint-order: stroke fill;
}
div.error {
color: crimson;
white-space: pre-wrap;
line-height: 1.1;
}
</style>
16 changes: 8 additions & 8 deletions src/lib/helpers/autoTicks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RawValue, ScaleType } from '$lib/types.js';
import { maybeTimeInterval } from './time.js';
import { range as rangei } from 'd3-array';
import { extent, range as rangei } from 'd3-array';

export function maybeInterval(interval: null | number | string | (<T>(d: T) => T)) {
if (interval == null) return;
Expand Down Expand Up @@ -38,11 +38,11 @@ export function autoTicks(
scaleFn,
count: number
) {
return ticks
? ticks
: interval
? maybeInterval(interval, type).range(domain[0], domain[1])
: typeof scaleFn.ticks === 'function'
? scaleFn.ticks(count)
: [];
if (ticks) return ticks;
if (interval) {
const [lo, hi] = extent(domain);
const I = maybeInterval(interval, type);
return I.range(lo, I.offset(hi));
}
return typeof scaleFn.ticks === 'function' ? scaleFn.ticks(count) : [];
}
22 changes: 21 additions & 1 deletion src/lib/helpers/scales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
import isDataRecord from './isDataRecord.js';

import { createProjection } from './projection.js';
import { maybeInterval } from './autoTicks.js';

/**
* compute the plot scales
Expand Down Expand Up @@ -302,7 +303,7 @@ export function createScale<T extends ScaleOptions>(
const valueArray =
type === 'quantile' || type === 'quantile-cont' ? allDataValues.toSorted() : valueArr;

const domain = scaleOptions.domain
let domain = scaleOptions.domain
? isOrdinal
? scaleOptions.domain
: extent(scaleOptions.zero ? [0, ...scaleOptions.domain] : scaleOptions.domain)
Expand All @@ -317,6 +318,18 @@ export function createScale<T extends ScaleOptions>(
: valueArray
: extent(scaleOptions.zero ? [0, ...valueArray] : valueArray);

if (scaleOptions.interval) {
if (isOrdinal) {
domain = domainFromInterval(domain, scaleOptions.interval, name);
} else {
if (markTypes.size > 0) {
console.warn(
'Setting interval via axis options is only supported for ordinal scales'
);
}
}
}

if (!scaleOptions.scale) {
throw new Error(`No scale function defined for ${name}`);
}
Expand Down Expand Up @@ -350,6 +363,13 @@ export function createScale<T extends ScaleOptions>(
};
}

function domainFromInterval(domain: RawValue[], interval: string | number, name: ScaleName) {
const interval_ = maybeInterval(interval);
const [lo, hi] = extent(domain);
const out = interval_.range(lo, interval_.offset(hi));
return name === 'y' ? out.toReversed() : out;
}

/**
* Infer a scale type based on the scale name, the data values mapped to it and
* the mark types that are bound to the scale
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