|
14 | 14 | import { motionStore } from '$lib/stores/motionStore'; |
15 | 15 | import Group from './Group.svelte'; |
16 | 16 |
|
17 | | - const { data: contextData, xScale, yScale, xGet, yGet } = getContext('LayerCake'); |
| 17 | + const { data: contextData, xScale, yScale, x: contextX, y: contextY } = getContext('LayerCake'); |
18 | 18 |
|
19 | 19 | /** Override data instead of using context */ |
20 | 20 | export let data: any = undefined; |
|
47 | 47 | export let curve: CurveFactory | CurveFactoryLineOnly | undefined = undefined; |
48 | 48 | export let defined: Parameters<Line<any>['defined']>[0] | undefined = undefined; |
49 | 49 |
|
| 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 | +
|
50 | 64 | let d: string | null = ''; |
51 | 65 | $: tweenedOptions = tweened ? { interpolate: interpolatePath, ...tweened } : false; |
52 | 66 | $: tweened_d = motionStore('', { tweened: tweenedOptions }); |
53 | 67 | $: { |
54 | 68 | const path = radial |
55 | 69 | ? 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)) |
58 | 72 | : 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)); |
61 | 75 | if (curve) path.curve(curve); |
62 | 76 | if (defined) path.defined(defined); |
63 | 77 |
|
|
0 commit comments