Skip to content

Commit f6a19c0

Browse files
committed
[Spline] Fix usage with geo visualizations (no defined data / domains)
1 parent 50ddb10 commit f6a19c0

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

.changeset/lucky-stingrays-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"layerchart": patch
3+
---
4+
5+
[Spline] Fix usage with geo visualizations (no defined data / domains)

packages/layerchart/src/lib/components/Spline.svelte

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import { motionStore } from '$lib/stores/motionStore';
1515
import Group from './Group.svelte';
1616
17-
const { data: contextData, xScale, yScale, xGet, yGet } = getContext('LayerCake');
17+
const { data: contextData, xScale, yScale, x: contextX, y: contextY } = getContext('LayerCake');
1818
1919
/** Override data instead of using context */
2020
export let data: any = undefined;
@@ -47,17 +47,31 @@
4747
export let curve: CurveFactory | CurveFactoryLineOnly | undefined = undefined;
4848
export let defined: Parameters<Line<any>['defined']>[0] | undefined = undefined;
4949
50+
function getScaleValue(
51+
data: any,
52+
scale: typeof $xScale | typeof $yScale,
53+
accessor: typeof x | typeof y
54+
) {
55+
if (scale.domain().length) {
56+
// If scale is defined with domain, map value
57+
return scale(accessor(data));
58+
} else {
59+
// Use raw value
60+
return accessor(data);
61+
}
62+
}
63+
5064
let d: string | null = '';
5165
$: tweenedOptions = tweened ? { interpolate: interpolatePath, ...tweened } : false;
5266
$: tweened_d = motionStore('', { tweened: tweenedOptions });
5367
$: {
5468
const path = radial
5569
? lineRadial()
56-
.angle((d) => (x ? $xScale(x(d)) : $xGet(d)))
57-
.radius((d) => (y ? $yScale(y(d)) : $yGet(d)))
70+
.angle((d) => getScaleValue(d, $xScale, x ?? $contextX))
71+
.radius((d) => getScaleValue(d, $yScale, y ?? $contextY))
5872
: d3Line()
59-
.x((d) => (x ? $xScale(x(d)) : $xGet(d)))
60-
.y((d) => (y ? $yScale(y(d)) : $yGet(d)));
73+
.x((d) => getScaleValue(d, $xScale, x ?? $contextX))
74+
.y((d) => getScaleValue(d, $yScale, y ?? $contextY));
6175
if (curve) path.curve(curve);
6276
if (defined) path.defined(defined);
6377

0 commit comments

Comments
 (0)