layerchart 2.0.0-next.3 → 2.0.0-next.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/AnnotationPoint.svelte +16 -9
- package/dist/components/AnnotationRange.svelte +3 -3
- package/dist/components/Arc.svelte +2 -2
- package/dist/components/Axis.svelte +63 -14
- package/dist/components/Axis.svelte.d.ts +12 -2
- package/dist/components/Blur.svelte +5 -3
- package/dist/components/Blur.svelte.d.ts +2 -5
- package/dist/components/BrushContext.svelte +1 -1
- package/dist/components/Calendar.svelte +10 -6
- package/dist/components/Calendar.svelte.d.ts +2 -1
- package/dist/components/Chart.svelte +2 -2
- package/dist/components/Connector.svelte +2 -2
- package/dist/components/Connector.svelte.d.ts +1 -1
- package/dist/components/Ellipse.svelte +187 -0
- package/dist/components/Ellipse.svelte.d.ts +64 -0
- package/dist/components/ForceSimulation.svelte +168 -50
- package/dist/components/ForceSimulation.svelte.d.ts +80 -21
- package/dist/components/GeoPath.svelte +12 -5
- package/dist/components/GeoPoint.svelte +1 -2
- package/dist/components/GeoSpline.svelte +4 -4
- package/dist/components/GeoSpline.svelte.d.ts +1 -1
- package/dist/components/Group.svelte +2 -2
- package/dist/components/Highlight.svelte +2 -2
- package/dist/components/Hull.svelte +1 -1
- package/dist/components/Labels.svelte +3 -2
- package/dist/components/Labels.svelte.d.ts +2 -2
- package/dist/components/Legend.svelte +19 -12
- package/dist/components/Legend.svelte.d.ts +5 -5
- package/dist/components/MonthPath.svelte +14 -11
- package/dist/components/MonthPath.svelte.d.ts +4 -3
- package/dist/components/Polygon.svelte +285 -0
- package/dist/components/Polygon.svelte.d.ts +115 -0
- package/dist/components/RadialGradient.svelte +1 -3
- package/dist/components/Spline.svelte +30 -18
- package/dist/components/Spline.svelte.d.ts +12 -4
- package/dist/components/Text.svelte +62 -60
- package/dist/components/Text.svelte.d.ts +6 -0
- package/dist/components/TransformControls.svelte +16 -20
- package/dist/components/Treemap.svelte +63 -26
- package/dist/components/Treemap.svelte.d.ts +11 -11
- package/dist/components/Voronoi.svelte +51 -33
- package/dist/components/Voronoi.svelte.d.ts +3 -1
- package/dist/components/charts/ArcChart.svelte +5 -3
- package/dist/components/charts/AreaChart.svelte +11 -11
- package/dist/components/charts/BarChart.svelte +64 -53
- package/dist/components/charts/DefaultTooltip.svelte +1 -1
- package/dist/components/charts/LineChart.svelte +10 -6
- package/dist/components/charts/PieChart.svelte +5 -3
- package/dist/components/charts/ScatterChart.svelte +2 -3
- package/dist/components/charts/utils.svelte.d.ts +2 -2
- package/dist/components/charts/utils.svelte.js +5 -1
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +5 -1
- package/dist/components/layout/Canvas.svelte +67 -49
- package/dist/components/layout/Canvas.svelte.d.ts +6 -0
- package/dist/components/layout/Layer.svelte +6 -4
- package/dist/components/layout/Layer.svelte.d.ts +6 -4
- package/dist/components/tooltip/Tooltip.svelte +14 -7
- package/dist/components/tooltip/TooltipContext.svelte +78 -34
- package/dist/components/tooltip/TooltipContext.svelte.d.ts +3 -3
- package/dist/components/tooltip/TooltipHeader.svelte +5 -4
- package/dist/components/tooltip/TooltipHeader.svelte.d.ts +3 -3
- package/dist/components/tooltip/TooltipItem.svelte +5 -4
- package/dist/components/tooltip/TooltipItem.svelte.d.ts +3 -3
- package/dist/components/tooltip/TooltipList.svelte +1 -1
- package/dist/components/tooltip/tooltipMetaContext.d.ts +2 -2
- package/dist/docs/Blockquote.svelte +6 -4
- package/dist/docs/Blockquote.svelte.d.ts +4 -19
- package/dist/docs/Code.svelte +20 -12
- package/dist/docs/Code.svelte.d.ts +9 -23
- package/dist/docs/Header1.svelte +4 -2
- package/dist/docs/Header1.svelte.d.ts +4 -28
- package/dist/docs/Json.svelte +11 -3
- package/dist/docs/Json.svelte.d.ts +9 -21
- package/dist/docs/Layout.svelte +10 -7
- package/dist/docs/Layout.svelte.d.ts +4 -19
- package/dist/docs/Link.svelte +7 -3
- package/dist/docs/Link.svelte.d.ts +4 -38
- package/dist/docs/Preview.svelte +6 -3
- package/dist/docs/TilesetField.svelte +20 -19
- package/dist/docs/TilesetField.svelte.d.ts +5 -22
- package/dist/docs/ViewSourceButton.svelte +9 -6
- package/dist/docs/ViewSourceButton.svelte.d.ts +7 -21
- package/dist/utils/arcText.svelte.js +4 -4
- package/dist/utils/array.d.ts +11 -0
- package/dist/utils/array.js +23 -0
- package/dist/utils/array.test.d.ts +1 -0
- package/dist/utils/array.test.js +200 -0
- package/dist/utils/canvas.d.ts +77 -0
- package/dist/utils/canvas.js +105 -41
- package/dist/utils/genData.d.ts +14 -0
- package/dist/utils/genData.js +24 -6
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/path.d.ts +10 -0
- package/dist/utils/path.js +30 -0
- package/dist/utils/scales.svelte.d.ts +3 -2
- package/dist/utils/scales.svelte.js +7 -3
- package/dist/utils/shape.d.ts +43 -0
- package/dist/utils/shape.js +59 -0
- package/dist/utils/string.d.ts +49 -0
- package/dist/utils/string.js +4 -2
- package/dist/utils/ticks.d.ts +15 -4
- package/dist/utils/ticks.js +140 -159
- package/dist/utils/ticks.test.js +6 -16
- package/dist/utils/treemap.d.ts +1 -1
- package/package.json +27 -25
- package/dist/utils/object.js +0 -2
|
@@ -175,7 +175,7 @@
|
|
|
175
175
|
y2: max(ctx.yRange) as unknown as number,
|
|
176
176
|
})),
|
|
177
177
|
];
|
|
178
|
-
} else if (xCoord) {
|
|
178
|
+
} else if (xCoord != null) {
|
|
179
179
|
tmpLines = [
|
|
180
180
|
...tmpLines,
|
|
181
181
|
{
|
|
@@ -201,7 +201,7 @@
|
|
|
201
201
|
y2: yItem + yOffset,
|
|
202
202
|
})),
|
|
203
203
|
];
|
|
204
|
-
} else if (yCoord) {
|
|
204
|
+
} else if (yCoord != null) {
|
|
205
205
|
tmpLines = [
|
|
206
206
|
...tmpLines,
|
|
207
207
|
{
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
<script lang="ts">
|
|
56
56
|
import { min } from 'd3-array';
|
|
57
57
|
import { Delaunay } from 'd3-delaunay';
|
|
58
|
-
// @ts-expect-error
|
|
58
|
+
// @ts-expect-error - no types available
|
|
59
59
|
import { geoVoronoi } from 'd3-geo-voronoi';
|
|
60
60
|
import { curveLinearClosed } from 'd3-shape';
|
|
61
61
|
import { cls } from '@layerstack/tailwind';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script lang="ts" module>
|
|
2
2
|
import Text, { type TextProps } from './Text.svelte';
|
|
3
3
|
import { type ComponentProps, type Snippet } from 'svelte';
|
|
4
|
-
import { format as formatValue, type FormatType } from '@layerstack/utils';
|
|
4
|
+
import { format as formatValue, type FormatType, type FormatConfig } from '@layerstack/utils';
|
|
5
5
|
import type { Without } from '../utils/types.js';
|
|
6
6
|
import Points, { type Point } from './Points.svelte';
|
|
7
7
|
import { accessor, type Accessor } from '../utils/common.js';
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
/**
|
|
52
52
|
* The format of the label
|
|
53
53
|
*/
|
|
54
|
-
format?: FormatType;
|
|
54
|
+
format?: FormatType | FormatConfig;
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
57
|
* Define unique value for {#each} `(key)` expressions to improve transitions.
|
|
@@ -109,6 +109,7 @@
|
|
|
109
109
|
|
|
110
110
|
const formattedValue = formatValue(
|
|
111
111
|
displayValue,
|
|
112
|
+
// @ts-expect-error - improve types
|
|
112
113
|
format ??
|
|
113
114
|
(value
|
|
114
115
|
? undefined
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Text, { type TextProps } from './Text.svelte';
|
|
2
2
|
import { type ComponentProps, type Snippet } from 'svelte';
|
|
3
|
-
import { type FormatType } from '@layerstack/utils';
|
|
3
|
+
import { type FormatType, type FormatConfig } from '@layerstack/utils';
|
|
4
4
|
import type { Without } from '../utils/types.js';
|
|
5
5
|
import { type Point } from './Points.svelte';
|
|
6
6
|
import { type Accessor } from '../utils/common.js';
|
|
@@ -42,7 +42,7 @@ export type LabelsPropsWithoutHTML<T = any> = {
|
|
|
42
42
|
/**
|
|
43
43
|
* The format of the label
|
|
44
44
|
*/
|
|
45
|
-
format?: FormatType;
|
|
45
|
+
format?: FormatType | FormatConfig;
|
|
46
46
|
/**
|
|
47
47
|
* Define unique value for {#each} `(key)` expressions to improve transitions.
|
|
48
48
|
* `index` position used by default
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script lang="ts" module>
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
3
|
import type { Placement } from './types.js';
|
|
4
|
-
import type
|
|
4
|
+
import { asAny, type Without } from '../utils/types.js';
|
|
5
5
|
|
|
6
6
|
export type LegendItem = {
|
|
7
7
|
value: any;
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
* @default width / 64
|
|
38
38
|
*/
|
|
39
39
|
ticks?: number;
|
|
40
|
-
tickFormat?: FormatType;
|
|
40
|
+
tickFormat?: FormatType | FormatConfig;
|
|
41
41
|
tickValues?: any[];
|
|
42
42
|
/**
|
|
43
43
|
* The font size of the tick labels.
|
|
@@ -86,9 +86,9 @@
|
|
|
86
86
|
title?: string;
|
|
87
87
|
label?: string;
|
|
88
88
|
tick?: string;
|
|
89
|
-
|
|
89
|
+
items?: string;
|
|
90
90
|
swatch?: string;
|
|
91
|
-
item?: (item: LegendItem) => string;
|
|
91
|
+
item?: string | ((item: LegendItem) => string);
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
/**
|
|
@@ -110,7 +110,7 @@
|
|
|
110
110
|
import { scaleBand, scaleLinear } from 'd3-scale';
|
|
111
111
|
import { quantize, interpolate, interpolateRound } from 'd3-interpolate';
|
|
112
112
|
import { quantile, range } from 'd3-array';
|
|
113
|
-
import { format, type FormatType } from '@layerstack/utils';
|
|
113
|
+
import { format, type FormatType, type FormatConfig } from '@layerstack/utils';
|
|
114
114
|
|
|
115
115
|
import ColorRamp from './ColorRamp.svelte';
|
|
116
116
|
import { cls } from '@layerstack/tailwind';
|
|
@@ -245,6 +245,7 @@
|
|
|
245
245
|
const tickValues = range(thresholds.length);
|
|
246
246
|
const tickFormat = (i: number) => {
|
|
247
247
|
const value = thresholds[i];
|
|
248
|
+
// @ts-expect-error - improve types
|
|
248
249
|
return tickFormatProp ? format(value, tickFormatProp) : value;
|
|
249
250
|
};
|
|
250
251
|
|
|
@@ -360,7 +361,8 @@
|
|
|
360
361
|
classes.label
|
|
361
362
|
)}
|
|
362
363
|
>
|
|
363
|
-
|
|
364
|
+
<!-- @ts-expect-error - improve types -->
|
|
365
|
+
{tickFormatProp ? format(tick, asAny(tickFormatProp)) : tick}
|
|
364
366
|
</text>
|
|
365
367
|
|
|
366
368
|
{#if scaleConfig.tickLine}
|
|
@@ -381,7 +383,7 @@
|
|
|
381
383
|
layerClass('legend-swatch-group'),
|
|
382
384
|
'flex gap-x-4 gap-y-1',
|
|
383
385
|
orientation === 'vertical' && 'flex-col',
|
|
384
|
-
classes.
|
|
386
|
+
classes.items
|
|
385
387
|
)}
|
|
386
388
|
>
|
|
387
389
|
{#each scaleConfig.tickValues ?? scaleConfig.xScale?.ticks?.(ticks) ?? [] as tick}
|
|
@@ -390,26 +392,31 @@
|
|
|
390
392
|
<button
|
|
391
393
|
class={cls(
|
|
392
394
|
layerClass('legend-swatch-button'),
|
|
393
|
-
'flex gap-1',
|
|
395
|
+
'flex items-center gap-1 truncate',
|
|
394
396
|
!onclick && 'cursor-auto',
|
|
395
|
-
classes.item
|
|
397
|
+
typeof classes.item === 'function' ? classes.item(item) : classes.item
|
|
396
398
|
)}
|
|
397
399
|
onclick={(e) => onclick?.(e, item)}
|
|
398
400
|
onpointerenter={(e) => onpointerenter?.(e, item)}
|
|
399
401
|
onpointerleave={(e) => onpointerleave?.(e, item)}
|
|
400
402
|
>
|
|
401
403
|
<div
|
|
402
|
-
class={cls(
|
|
404
|
+
class={cls(
|
|
405
|
+
layerClass('legend-swatch'),
|
|
406
|
+
'h-4 w-4 shrink-0 rounded-full',
|
|
407
|
+
classes.swatch
|
|
408
|
+
)}
|
|
403
409
|
style:background-color={color}
|
|
404
410
|
></div>
|
|
405
411
|
<div
|
|
406
412
|
class={cls(
|
|
407
413
|
layerClass('legend-swatch-label'),
|
|
408
|
-
'text-xs text-surface-content whitespace-nowrap',
|
|
414
|
+
'text-xs text-surface-content truncate whitespace-nowrap',
|
|
409
415
|
classes.label
|
|
410
416
|
)}
|
|
411
417
|
>
|
|
412
|
-
|
|
418
|
+
<!-- @ts-expect-error - improve types -->
|
|
419
|
+
{tickFormatProp ? format(tick, asAny(tickFormatProp)) : tick}
|
|
413
420
|
</div>
|
|
414
421
|
</button>
|
|
415
422
|
{/each}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
2
|
import type { Placement } from './types.js';
|
|
3
|
-
import type
|
|
3
|
+
import { type Without } from '../utils/types.js';
|
|
4
4
|
export type LegendItem = {
|
|
5
5
|
value: any;
|
|
6
6
|
color: string;
|
|
@@ -30,7 +30,7 @@ export type LegendPropsWithoutHTML = {
|
|
|
30
30
|
* @default width / 64
|
|
31
31
|
*/
|
|
32
32
|
ticks?: number;
|
|
33
|
-
tickFormat?: FormatType;
|
|
33
|
+
tickFormat?: FormatType | FormatConfig;
|
|
34
34
|
tickValues?: any[];
|
|
35
35
|
/**
|
|
36
36
|
* The font size of the tick labels.
|
|
@@ -73,9 +73,9 @@ export type LegendPropsWithoutHTML = {
|
|
|
73
73
|
title?: string;
|
|
74
74
|
label?: string;
|
|
75
75
|
tick?: string;
|
|
76
|
-
|
|
76
|
+
items?: string;
|
|
77
77
|
swatch?: string;
|
|
78
|
-
item?: (item: LegendItem) => string;
|
|
78
|
+
item?: string | ((item: LegendItem) => string);
|
|
79
79
|
};
|
|
80
80
|
/**
|
|
81
81
|
* A bindable reference to the wrapping `<div>` element.
|
|
@@ -90,7 +90,7 @@ export type LegendPropsWithoutHTML = {
|
|
|
90
90
|
};
|
|
91
91
|
export type LegendProps = LegendPropsWithoutHTML & Without<HTMLAttributes<HTMLElement>, LegendPropsWithoutHTML>;
|
|
92
92
|
import type { HTMLAttributes } from 'svelte/elements';
|
|
93
|
-
import { type FormatType } from '@layerstack/utils';
|
|
93
|
+
import { type FormatType, type FormatConfig } from '@layerstack/utils';
|
|
94
94
|
import type { AnyScale } from '../utils/scales.svelte.js';
|
|
95
95
|
declare const Legend: import("svelte").Component<LegendProps, {}, "ref">;
|
|
96
96
|
type Legend = ReturnType<typeof Legend>;
|
|
@@ -20,30 +20,33 @@
|
|
|
20
20
|
*
|
|
21
21
|
* @bindable
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
pathRef?: SVGPathElement;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
export type MonthPathProps = MonthPathPropsWithoutHTML &
|
|
27
|
-
|
|
27
|
+
// we omit the spline props to avoid conflicts with attribute names since we are
|
|
28
|
+
// passing them through to `<Spline />`
|
|
29
|
+
Without<SVGAttributes<SVGPathElement>, MonthPathPropsWithoutHTML & SplinePropsWithoutHTML>;
|
|
28
30
|
</script>
|
|
29
31
|
|
|
30
32
|
<script lang="ts">
|
|
31
|
-
import { timeWeek, timeYear } from 'd3-time';
|
|
32
|
-
import { endOfMonth } from 'date-fns';
|
|
33
|
+
import { timeWeek, timeMonth, timeYear } from 'd3-time';
|
|
33
34
|
import { cls } from '@layerstack/tailwind';
|
|
35
|
+
import { endOfInterval } from '@layerstack/utils';
|
|
34
36
|
import { layerClass } from '../utils/attributes.js';
|
|
37
|
+
import Spline, { type SplinePropsWithoutHTML } from './Spline.svelte';
|
|
35
38
|
|
|
36
39
|
let {
|
|
37
40
|
date,
|
|
38
41
|
cellSize: cellSizeProp,
|
|
39
|
-
|
|
42
|
+
pathRef: pathRefProp = $bindable(),
|
|
40
43
|
class: className,
|
|
41
44
|
...restProps
|
|
42
45
|
}: MonthPathProps = $props();
|
|
43
46
|
|
|
44
|
-
let
|
|
47
|
+
let pathRef = $state<SVGPathElement>();
|
|
45
48
|
$effect.pre(() => {
|
|
46
|
-
|
|
49
|
+
pathRefProp = pathRef;
|
|
47
50
|
});
|
|
48
51
|
|
|
49
52
|
const cellSize = $derived(
|
|
@@ -55,7 +58,7 @@
|
|
|
55
58
|
const startWeek = $derived(timeWeek.count(timeYear(date), date));
|
|
56
59
|
|
|
57
60
|
// end of month
|
|
58
|
-
const monthEnd = $derived(
|
|
61
|
+
const monthEnd = $derived(endOfInterval('month', date));
|
|
59
62
|
const endDayOfWeek = $derived(monthEnd.getDay());
|
|
60
63
|
const endWeek = $derived(timeWeek.count(timeYear(monthEnd), monthEnd));
|
|
61
64
|
|
|
@@ -68,9 +71,9 @@
|
|
|
68
71
|
`);
|
|
69
72
|
</script>
|
|
70
73
|
|
|
71
|
-
<
|
|
72
|
-
bind:
|
|
73
|
-
|
|
74
|
+
<Spline
|
|
75
|
+
bind:pathRef
|
|
76
|
+
{pathData}
|
|
74
77
|
fill="none"
|
|
75
78
|
class={cls(layerClass('month-path'), 'stroke-surface-content/20', className)}
|
|
76
79
|
{...restProps}
|
|
@@ -16,9 +16,10 @@ export type MonthPathPropsWithoutHTML = {
|
|
|
16
16
|
*
|
|
17
17
|
* @bindable
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
pathRef?: SVGPathElement;
|
|
20
20
|
};
|
|
21
|
-
export type MonthPathProps = MonthPathPropsWithoutHTML & Without<SVGAttributes<SVGPathElement>, MonthPathPropsWithoutHTML>;
|
|
22
|
-
|
|
21
|
+
export type MonthPathProps = MonthPathPropsWithoutHTML & Without<SVGAttributes<SVGPathElement>, MonthPathPropsWithoutHTML & SplinePropsWithoutHTML>;
|
|
22
|
+
import { type SplinePropsWithoutHTML } from './Spline.svelte';
|
|
23
|
+
declare const MonthPath: import("svelte").Component<MonthPathProps, {}, "pathRef">;
|
|
23
24
|
type MonthPath = ReturnType<typeof MonthPath>;
|
|
24
25
|
export default MonthPath;
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { CommonStyleProps, Without } from '../utils/types.js';
|
|
3
|
+
|
|
4
|
+
export type PolygonPropsWithoutHTML = {
|
|
5
|
+
/**
|
|
6
|
+
* The center x position of the polygon.
|
|
7
|
+
*
|
|
8
|
+
* @default 0
|
|
9
|
+
*/
|
|
10
|
+
cx?: number;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The initial center x position of the polygon.
|
|
14
|
+
*
|
|
15
|
+
* @default cx
|
|
16
|
+
*/
|
|
17
|
+
initialCx?: number;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The center y position of the polygon.
|
|
21
|
+
*
|
|
22
|
+
* @default 0
|
|
23
|
+
*/
|
|
24
|
+
cy?: number;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The initial center y position of the polygon.
|
|
28
|
+
*
|
|
29
|
+
* @default cy
|
|
30
|
+
*/
|
|
31
|
+
initialCy?: number;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The radius of the polygon.
|
|
35
|
+
*
|
|
36
|
+
* @default 1
|
|
37
|
+
*/
|
|
38
|
+
r?: number;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The initial radius of the polygon.
|
|
42
|
+
*
|
|
43
|
+
* @default r
|
|
44
|
+
*/
|
|
45
|
+
initialR?: number;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The number of points or explicit points to create the polygon.
|
|
49
|
+
*
|
|
50
|
+
* @default 4
|
|
51
|
+
*/
|
|
52
|
+
points?: number | { x: number; y: number }[];
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The radius of the curve for rounded corners.
|
|
56
|
+
*
|
|
57
|
+
* @default 0
|
|
58
|
+
*/
|
|
59
|
+
cornerRadius?: number;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The rotation of the polygon.
|
|
63
|
+
*
|
|
64
|
+
* @default 0
|
|
65
|
+
*/
|
|
66
|
+
rotate?: number;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The percent to inset the odd points of the star (<1 inset, >1 outset)
|
|
70
|
+
*
|
|
71
|
+
* @default 0
|
|
72
|
+
*/
|
|
73
|
+
inset?: number;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The horizontal stretch factor of the polygon.
|
|
77
|
+
*
|
|
78
|
+
* @default 1
|
|
79
|
+
*/
|
|
80
|
+
scaleX?: number;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The vertical stretch factor of the polygon.
|
|
84
|
+
*
|
|
85
|
+
* @default 1
|
|
86
|
+
*/
|
|
87
|
+
scaleY?: number;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The skew angle in degrees along the X axis.
|
|
91
|
+
*
|
|
92
|
+
* @default 0
|
|
93
|
+
*/
|
|
94
|
+
skewX?: number;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The skew angle in degrees along the Y axis.
|
|
98
|
+
*
|
|
99
|
+
* @default 0
|
|
100
|
+
*/
|
|
101
|
+
skewY?: number;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The tilt factor for x-coordinates.
|
|
105
|
+
*
|
|
106
|
+
* @default 1
|
|
107
|
+
*/
|
|
108
|
+
tiltX?: number;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The tilt factor for y-coordinates.
|
|
112
|
+
*
|
|
113
|
+
* @default 1
|
|
114
|
+
*/
|
|
115
|
+
tiltY?: number;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A bindable reference to the `<path>` element
|
|
119
|
+
*
|
|
120
|
+
* @bindable
|
|
121
|
+
*/
|
|
122
|
+
ref?: SVGPathElement;
|
|
123
|
+
|
|
124
|
+
motion?: MotionProp;
|
|
125
|
+
} & CommonStyleProps;
|
|
126
|
+
|
|
127
|
+
export type PolygonProps = PolygonPropsWithoutHTML &
|
|
128
|
+
Without<SVGAttributes<Element>, PolygonPropsWithoutHTML>;
|
|
129
|
+
</script>
|
|
130
|
+
|
|
131
|
+
<script lang="ts">
|
|
132
|
+
import type { SVGAttributes } from 'svelte/elements';
|
|
133
|
+
import { cls } from '@layerstack/tailwind';
|
|
134
|
+
import { merge } from 'lodash-es';
|
|
135
|
+
import { interpolatePath } from 'd3-interpolate-path';
|
|
136
|
+
|
|
137
|
+
import { getRenderContext } from './Chart.svelte';
|
|
138
|
+
import {
|
|
139
|
+
createMotion,
|
|
140
|
+
extractTweenConfig,
|
|
141
|
+
type MotionProp,
|
|
142
|
+
type ResolvedMotion,
|
|
143
|
+
} from '../utils/motion.svelte.js';
|
|
144
|
+
import { registerCanvasComponent } from './layout/Canvas.svelte';
|
|
145
|
+
import { renderPathData, type ComputedStylesOptions } from '../utils/canvas.js';
|
|
146
|
+
import { createKey } from '../utils/key.svelte.js';
|
|
147
|
+
import { layerClass } from '../utils/attributes.js';
|
|
148
|
+
import { polygon } from '../utils/shape.js';
|
|
149
|
+
import { roundedPolygonPath } from '../utils/path.js';
|
|
150
|
+
|
|
151
|
+
let {
|
|
152
|
+
cx = 0,
|
|
153
|
+
initialCx: initialCxProp,
|
|
154
|
+
cy = 0,
|
|
155
|
+
initialCy: initialCyProp,
|
|
156
|
+
r = 1,
|
|
157
|
+
initialR: initialRProp,
|
|
158
|
+
points = 4,
|
|
159
|
+
cornerRadius = 0,
|
|
160
|
+
rotate = 0,
|
|
161
|
+
inset = 0,
|
|
162
|
+
scaleX = 1,
|
|
163
|
+
scaleY = 1,
|
|
164
|
+
skewX = 0,
|
|
165
|
+
skewY = 0,
|
|
166
|
+
tiltX = 0,
|
|
167
|
+
tiltY = 0,
|
|
168
|
+
motion,
|
|
169
|
+
fill,
|
|
170
|
+
fillOpacity,
|
|
171
|
+
stroke,
|
|
172
|
+
strokeWidth,
|
|
173
|
+
opacity,
|
|
174
|
+
class: className,
|
|
175
|
+
ref: refProp = $bindable(),
|
|
176
|
+
...restProps
|
|
177
|
+
}: PolygonProps = $props();
|
|
178
|
+
|
|
179
|
+
let ref = $state<SVGPathElement>();
|
|
180
|
+
|
|
181
|
+
$effect.pre(() => {
|
|
182
|
+
refProp = ref;
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const initialCx = initialCxProp ?? cx;
|
|
186
|
+
const initialCy = initialCyProp ?? cy;
|
|
187
|
+
const initialR = initialRProp ?? r;
|
|
188
|
+
|
|
189
|
+
const motionCx = createMotion(initialCx, () => cx, motion);
|
|
190
|
+
const motionCy = createMotion(initialCy, () => cy, motion);
|
|
191
|
+
const motionR = createMotion(initialR, () => r, motion);
|
|
192
|
+
|
|
193
|
+
let polygonPoints = $derived(
|
|
194
|
+
typeof points === 'number'
|
|
195
|
+
? polygon({
|
|
196
|
+
cx: motionCx.current,
|
|
197
|
+
cy: motionCy.current,
|
|
198
|
+
count: points,
|
|
199
|
+
radius: motionR.current,
|
|
200
|
+
rotate,
|
|
201
|
+
inset,
|
|
202
|
+
scaleX,
|
|
203
|
+
scaleY,
|
|
204
|
+
skewX,
|
|
205
|
+
skewY,
|
|
206
|
+
tiltX,
|
|
207
|
+
tiltY,
|
|
208
|
+
})
|
|
209
|
+
: points
|
|
210
|
+
);
|
|
211
|
+
let d = $derived(roundedPolygonPath(polygonPoints, cornerRadius));
|
|
212
|
+
|
|
213
|
+
const extractedTween = extractTweenConfig(motion);
|
|
214
|
+
|
|
215
|
+
const tweenedOptions: ResolvedMotion | undefined = extractedTween
|
|
216
|
+
? {
|
|
217
|
+
type: extractedTween.type,
|
|
218
|
+
options: { interpolate: interpolatePath, ...extractedTween.options },
|
|
219
|
+
}
|
|
220
|
+
: undefined;
|
|
221
|
+
|
|
222
|
+
const tweenedState = createMotion(null, () => d, tweenedOptions);
|
|
223
|
+
|
|
224
|
+
const renderCtx = getRenderContext();
|
|
225
|
+
|
|
226
|
+
function render(
|
|
227
|
+
ctx: CanvasRenderingContext2D,
|
|
228
|
+
styleOverrides: ComputedStylesOptions | undefined
|
|
229
|
+
) {
|
|
230
|
+
renderPathData(
|
|
231
|
+
ctx,
|
|
232
|
+
tweenedState.current,
|
|
233
|
+
styleOverrides
|
|
234
|
+
? merge({ styles: { strokeWidth } }, styleOverrides)
|
|
235
|
+
: {
|
|
236
|
+
styles: { fill, fillOpacity, stroke, strokeWidth, opacity },
|
|
237
|
+
classes: cls(layerClass('polygon'), fill == null && 'fill-surface-content', className),
|
|
238
|
+
}
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// TODO: Use objectId to work around Svelte 4 reactivity issue (even when memoizing gradients)
|
|
243
|
+
const fillKey = createKey(() => fill);
|
|
244
|
+
const strokeKey = createKey(() => stroke);
|
|
245
|
+
|
|
246
|
+
if (renderCtx === 'canvas') {
|
|
247
|
+
registerCanvasComponent({
|
|
248
|
+
name: 'Polygon',
|
|
249
|
+
render,
|
|
250
|
+
events: {
|
|
251
|
+
click: restProps.onclick,
|
|
252
|
+
pointerenter: restProps.onpointerenter,
|
|
253
|
+
pointermove: restProps.onpointermove,
|
|
254
|
+
pointerleave: restProps.onpointerleave,
|
|
255
|
+
pointerdown: restProps.onpointerdown,
|
|
256
|
+
pointerover: restProps.onpointerover,
|
|
257
|
+
pointerout: restProps.onpointerout,
|
|
258
|
+
touchmove: restProps.ontouchmove,
|
|
259
|
+
},
|
|
260
|
+
deps: () => [
|
|
261
|
+
fillKey.current,
|
|
262
|
+
fillOpacity,
|
|
263
|
+
strokeKey.current,
|
|
264
|
+
strokeWidth,
|
|
265
|
+
opacity,
|
|
266
|
+
className,
|
|
267
|
+
tweenedState.current,
|
|
268
|
+
],
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
</script>
|
|
272
|
+
|
|
273
|
+
{#if renderCtx === 'svg'}
|
|
274
|
+
<path
|
|
275
|
+
d={tweenedState.current}
|
|
276
|
+
{fill}
|
|
277
|
+
fill-opacity={fillOpacity}
|
|
278
|
+
{stroke}
|
|
279
|
+
stroke-width={strokeWidth}
|
|
280
|
+
{opacity}
|
|
281
|
+
class={cls(layerClass('polygon'), fill == null && 'fill-surface-content', className)}
|
|
282
|
+
{...restProps}
|
|
283
|
+
bind:this={ref}
|
|
284
|
+
/>
|
|
285
|
+
{/if}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { CommonStyleProps, Without } from '../utils/types.js';
|
|
2
|
+
export type PolygonPropsWithoutHTML = {
|
|
3
|
+
/**
|
|
4
|
+
* The center x position of the polygon.
|
|
5
|
+
*
|
|
6
|
+
* @default 0
|
|
7
|
+
*/
|
|
8
|
+
cx?: number;
|
|
9
|
+
/**
|
|
10
|
+
* The initial center x position of the polygon.
|
|
11
|
+
*
|
|
12
|
+
* @default cx
|
|
13
|
+
*/
|
|
14
|
+
initialCx?: number;
|
|
15
|
+
/**
|
|
16
|
+
* The center y position of the polygon.
|
|
17
|
+
*
|
|
18
|
+
* @default 0
|
|
19
|
+
*/
|
|
20
|
+
cy?: number;
|
|
21
|
+
/**
|
|
22
|
+
* The initial center y position of the polygon.
|
|
23
|
+
*
|
|
24
|
+
* @default cy
|
|
25
|
+
*/
|
|
26
|
+
initialCy?: number;
|
|
27
|
+
/**
|
|
28
|
+
* The radius of the polygon.
|
|
29
|
+
*
|
|
30
|
+
* @default 1
|
|
31
|
+
*/
|
|
32
|
+
r?: number;
|
|
33
|
+
/**
|
|
34
|
+
* The initial radius of the polygon.
|
|
35
|
+
*
|
|
36
|
+
* @default r
|
|
37
|
+
*/
|
|
38
|
+
initialR?: number;
|
|
39
|
+
/**
|
|
40
|
+
* The number of points or explicit points to create the polygon.
|
|
41
|
+
*
|
|
42
|
+
* @default 4
|
|
43
|
+
*/
|
|
44
|
+
points?: number | {
|
|
45
|
+
x: number;
|
|
46
|
+
y: number;
|
|
47
|
+
}[];
|
|
48
|
+
/**
|
|
49
|
+
* The radius of the curve for rounded corners.
|
|
50
|
+
*
|
|
51
|
+
* @default 0
|
|
52
|
+
*/
|
|
53
|
+
cornerRadius?: number;
|
|
54
|
+
/**
|
|
55
|
+
* The rotation of the polygon.
|
|
56
|
+
*
|
|
57
|
+
* @default 0
|
|
58
|
+
*/
|
|
59
|
+
rotate?: number;
|
|
60
|
+
/**
|
|
61
|
+
* The percent to inset the odd points of the star (<1 inset, >1 outset)
|
|
62
|
+
*
|
|
63
|
+
* @default 0
|
|
64
|
+
*/
|
|
65
|
+
inset?: number;
|
|
66
|
+
/**
|
|
67
|
+
* The horizontal stretch factor of the polygon.
|
|
68
|
+
*
|
|
69
|
+
* @default 1
|
|
70
|
+
*/
|
|
71
|
+
scaleX?: number;
|
|
72
|
+
/**
|
|
73
|
+
* The vertical stretch factor of the polygon.
|
|
74
|
+
*
|
|
75
|
+
* @default 1
|
|
76
|
+
*/
|
|
77
|
+
scaleY?: number;
|
|
78
|
+
/**
|
|
79
|
+
* The skew angle in degrees along the X axis.
|
|
80
|
+
*
|
|
81
|
+
* @default 0
|
|
82
|
+
*/
|
|
83
|
+
skewX?: number;
|
|
84
|
+
/**
|
|
85
|
+
* The skew angle in degrees along the Y axis.
|
|
86
|
+
*
|
|
87
|
+
* @default 0
|
|
88
|
+
*/
|
|
89
|
+
skewY?: number;
|
|
90
|
+
/**
|
|
91
|
+
* The tilt factor for x-coordinates.
|
|
92
|
+
*
|
|
93
|
+
* @default 1
|
|
94
|
+
*/
|
|
95
|
+
tiltX?: number;
|
|
96
|
+
/**
|
|
97
|
+
* The tilt factor for y-coordinates.
|
|
98
|
+
*
|
|
99
|
+
* @default 1
|
|
100
|
+
*/
|
|
101
|
+
tiltY?: number;
|
|
102
|
+
/**
|
|
103
|
+
* A bindable reference to the `<path>` element
|
|
104
|
+
*
|
|
105
|
+
* @bindable
|
|
106
|
+
*/
|
|
107
|
+
ref?: SVGPathElement;
|
|
108
|
+
motion?: MotionProp;
|
|
109
|
+
} & CommonStyleProps;
|
|
110
|
+
export type PolygonProps = PolygonPropsWithoutHTML & Without<SVGAttributes<Element>, PolygonPropsWithoutHTML>;
|
|
111
|
+
import type { SVGAttributes } from 'svelte/elements';
|
|
112
|
+
import { type MotionProp } from '../utils/motion.svelte.js';
|
|
113
|
+
declare const Polygon: import("svelte").Component<PolygonProps, {}, "ref">;
|
|
114
|
+
type Polygon = ReturnType<typeof Polygon>;
|
|
115
|
+
export default Polygon;
|