Skip to content

Commit 6135445

Browse files
committed
ts: improve arrow prop type
1 parent 5668cbc commit 6135445

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

src/lib/marks/Arrow.svelte

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
<!-- @component
22
Creates arrows with customizable heads, angles, and bending
33
-->
4-
<script module lang="ts">
5-
export type ArrowMarkProps = Omit<BaseMarkProps, 'fill' | 'fillOpacity'> & {
6-
data: DataRecord[];
4+
<script lang="ts" generics="Datum extends DataRecord">
5+
interface ArrowMarkProps extends Omit<BaseMarkProps<Datum>, 'fill' | 'fillOpacity'> {
6+
data: Datum[];
77
sort?: ConstantAccessor<RawValue> | { channel: 'stroke' | 'fill' };
8-
x1: ChannelAccessor;
9-
y1: ChannelAccessor;
10-
x2: ChannelAccessor;
11-
y2: ChannelAccessor;
8+
x1: ChannelAccessor<Datum>;
9+
y1: ChannelAccessor<Datum>;
10+
x2: ChannelAccessor<Datum>;
11+
y2: ChannelAccessor<Datum>;
1212
/**
1313
* the bend angle, in degrees; defaults to 0°; true for 22.5°
1414
*/
15-
bend?: ConstantAccessor<number> | true;
15+
bend?: ConstantAccessor<number, Datum> | true;
1616
/**
1717
* the arrowhead angle, in degrees; defaults to 60°
1818
*/
19-
headAngle?: ConstantAccessor<number>;
19+
headAngle?: ConstantAccessor<number, Datum>;
2020
/**
2121
* the arrowhead scale; defaults to 8
2222
*/
23-
headLength?: ConstantAccessor<number>;
23+
headLength?: ConstantAccessor<number, Datum>;
2424
/**
2525
* inset at the end of the arrow (useful if the arrow points to a dot)
2626
*/
27-
insetEnd?: ConstantAccessor<number>;
27+
insetEnd?: ConstantAccessor<number, Datum>;
2828
/**
2929
* inset at the start of the arrow
3030
*/
31-
insetStart?: ConstantAccessor<number>;
31+
insetStart?: ConstantAccessor<number, Datum>;
3232
/**
3333
* shorthand for the two insets
3434
*/
35-
inset?: ConstantAccessor<number>;
35+
inset?: ConstantAccessor<number, Datum>;
3636
sweep?: SweepOption;
37-
};
38-
</script>
39-
40-
<script lang="ts">
37+
}
4138
import { getContext } from 'svelte';
4239
import type {
4340
PlotContext,
@@ -66,7 +63,7 @@
6663
};
6764
6865
const {
69-
data = [{}],
66+
data = [{} as Datum],
7067
class: className = '',
7168
...options
7269
}: ArrowMarkProps = $derived({

src/lib/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ export type ColorScaleOptions = ScaleOptions & {
229229
*/
230230
n: number;
231231
interpolate: (d: any) => typeof d;
232+
/**
233+
* The tick format for the color scale legend.
234+
*/
235+
tickFormat: false | Intl.NumberFormatOptions | ((d: RawValue) => string);
232236
};
233237

234238
export type AxisXAnchor = 'bottom' | 'top' | 'both';
@@ -254,7 +258,7 @@ export type XScaleOptions = ScaleOptions & {
254258

255259
labelAnchor: 'auto' | 'left' | 'center' | 'right';
256260

257-
tickFormat: 'auto' | string | ((d: RawValue) => string);
261+
tickFormat: false | Intl.NumberFormatOptions | ((d: RawValue) => string);
258262
};
259263

260264
export type YScaleOptions = ScaleOptions & {
@@ -270,7 +274,7 @@ export type YScaleOptions = ScaleOptions & {
270274
* add an explicit AxisY mark to your plot instead of using the implicit axes.
271275
*/
272276
axis: AxisYAnchor | false;
273-
tickFormat: string | ((d: RawValue) => string);
277+
tickFormat: false | Intl.NumberFormatOptions | ((d: RawValue) => string);
274278
/**
275279
* rotate the axis ticks
276280
*/

src/routes/examples/arrow/metro.svelte

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import { page } from '$app/state';
88
import { setContext } from 'svelte';
99
import { type PlotDefaults } from 'svelteplot/types';
10-
let { metros } = $derived(page.data.data);
10+
import type { ExamplesData } from '../types';
11+
let { metros } = $derived(
12+
page.data.data
13+
) as ExamplesData;
1114
1215
setContext<Partial<PlotDefaults>>(
1316
'svelteplot/defaults',
@@ -18,7 +21,7 @@
1821
}
1922
);
2023
21-
let hl = $state(false);
24+
let hl: false | (typeof metros)[0] = $state(false);
2225
</script>
2326

2427
<Plot
@@ -31,7 +34,10 @@
3134
color={{
3235
label: 'Change in inequality from 1980 to 2015',
3336
legend: true,
34-
tickFormat: '+f'
37+
tickFormat: {
38+
minimumFractionDigits: 0,
39+
maximumFractionDigits: 1
40+
}
3541
}}>
3642
<Arrow
3743
data={metros}

src/routes/examples/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ type RiaaRow = {
8181
revenue: number;
8282
};
8383

84+
type MetrosRow = {
85+
Metro: string;
86+
POP_1980: number;
87+
LPOP_1980: number;
88+
R90_10_1980: number;
89+
POP_2015: number;
90+
LPOP_2015: number;
91+
R90_10_2015: number;
92+
nyt_display: string;
93+
state_display: string;
94+
highlight: number;
95+
};
96+
8497
export type ExamplesData = {
8598
aapl: AaplRow[];
8699
simpsons: SimpsonsRow[];
@@ -93,4 +106,5 @@ export type ExamplesData = {
93106
world: any;
94107
beagle: BeagleRow[];
95108
riaa: RiaaRow[];
109+
metros: MetrosRow[];
96110
};

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