Skip to content

Commit 1978053

Browse files
committed
fix: determine point/band scale domain from interval
1 parent 91cd1ce commit 1978053

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/lib/helpers/autoTicks.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { RawValue, ScaleType } from '$lib/types.js';
22
import { maybeTimeInterval } from './time.js';
3-
import { range as rangei } from 'd3-array';
3+
import { extent, range as rangei } from 'd3-array';
44

55
export function maybeInterval(interval: null | number | string | (<T>(d: T) => T)) {
66
if (interval == null) return;
@@ -38,11 +38,11 @@ export function autoTicks(
3838
scaleFn,
3939
count: number
4040
) {
41-
return ticks
42-
? ticks
43-
: interval
44-
? maybeInterval(interval, type).range(domain[0], domain[1])
45-
: typeof scaleFn.ticks === 'function'
46-
? scaleFn.ticks(count)
47-
: [];
41+
if (ticks) return ticks;
42+
if (interval) {
43+
const [lo, hi] = extent(domain);
44+
const I = maybeInterval(interval, type);
45+
return I.range(lo, I.offset(hi));
46+
}
47+
return typeof scaleFn.ticks === 'function' ? scaleFn.ticks(count) : [];
4848
}

src/lib/helpers/scales.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type {
2929
import isDataRecord from './isDataRecord.js';
3030

3131
import { createProjection } from './projection.js';
32+
import { maybeInterval } from './autoTicks.js';
3233

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

305-
const domain = scaleOptions.domain
306+
let domain = scaleOptions.domain
306307
? isOrdinal
307308
? scaleOptions.domain
308309
: extent(scaleOptions.zero ? [0, ...scaleOptions.domain] : scaleOptions.domain)
@@ -317,6 +318,16 @@ export function createScale<T extends ScaleOptions>(
317318
: valueArray
318319
: extent(scaleOptions.zero ? [0, ...valueArray] : valueArray);
319320

321+
if (scaleOptions.interval) {
322+
if (isOrdinal) {
323+
domain = domainFromInterval(domain, scaleOptions.interval);
324+
} else {
325+
throw new Error(
326+
'Setting interval via axis options is only supported for ordinal scales'
327+
);
328+
}
329+
}
330+
320331
if (!scaleOptions.scale) {
321332
throw new Error(`No scale function defined for ${name}`);
322333
}
@@ -350,6 +361,12 @@ export function createScale<T extends ScaleOptions>(
350361
};
351362
}
352363

364+
function domainFromInterval(domain: RawValue[], interval: string | number) {
365+
const interval_ = maybeInterval(interval);
366+
const [lo, hi] = extent(domain);
367+
return interval_.range(lo, interval_.offset(hi));
368+
}
369+
353370
/**
354371
* Infer a scale type based on the scale name, the data values mapped to it and
355372
* the mark types that are bound to the scale

0 commit comments

Comments
 (0)
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