layerchart 0.60.3 → 0.70.0
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/Arc.svelte +83 -27
- package/dist/components/Area.svelte +51 -12
- package/dist/components/Bar.svelte +5 -1
- package/dist/components/Circle.svelte +54 -12
- package/dist/components/ComputedStyles.svelte +16 -0
- package/dist/components/ComputedStyles.svelte.d.ts +20 -0
- package/dist/components/GeoPath.svelte +36 -44
- package/dist/components/GeoPath.svelte.d.ts +2 -2
- package/dist/components/GeoPoint.svelte +27 -13
- package/dist/components/GeoTile.svelte +22 -7
- package/dist/components/Graticule.svelte.d.ts +4 -4
- package/dist/components/Group.svelte +46 -18
- package/dist/components/HitCanvas.svelte +7 -21
- package/dist/components/Legend.svelte.d.ts +1 -1
- package/dist/components/Line.svelte +70 -31
- package/dist/components/LinearGradient.svelte +89 -26
- package/dist/components/LinearGradient.svelte.d.ts +2 -2
- package/dist/components/Points.svelte +23 -41
- package/dist/components/Points.svelte.d.ts +2 -1
- package/dist/components/RadialGradient.svelte +86 -28
- package/dist/components/RadialGradient.svelte.d.ts +2 -2
- package/dist/components/Rect.svelte +62 -18
- package/dist/components/Rule.svelte +1 -1
- package/dist/components/Spline.svelte +95 -57
- package/dist/components/Text.svelte +80 -22
- package/dist/components/TransformControls.svelte.d.ts +1 -1
- package/dist/components/charts/AreaChart.svelte +14 -10
- package/dist/components/charts/BarChart.svelte +13 -9
- package/dist/components/charts/LineChart.svelte +13 -9
- package/dist/components/charts/PieChart.svelte +6 -2
- package/dist/components/charts/PieChart.svelte.d.ts +2 -1
- package/dist/components/charts/ScatterChart.svelte +14 -10
- package/dist/components/layout/Canvas.svelte +93 -11
- package/dist/components/layout/Canvas.svelte.d.ts +13 -0
- package/dist/components/layout/Svg.svelte +1 -1
- package/dist/components/tooltip/Tooltip.svelte.d.ts +1 -1
- package/dist/utils/canvas.d.ts +46 -0
- package/dist/utils/canvas.js +146 -0
- package/dist/utils/common.d.ts +10 -1
- package/dist/utils/common.js +13 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/math.d.ts +2 -0
- package/dist/utils/math.js +10 -0
- package/dist/utils/path.d.ts +7 -0
- package/dist/utils/path.js +17 -3
- package/package.json +2 -2
|
@@ -6,11 +6,11 @@ declare const __propDef: {
|
|
|
6
6
|
[x: string]: any;
|
|
7
7
|
geojson?: import("d3-geo").GeoPermissibleObjects | null | undefined;
|
|
8
8
|
render?: ((ctx: CanvasRenderingContext2D, options: {
|
|
9
|
-
|
|
9
|
+
newGeoPath: () => ReturnType<typeof import("..").geoCurvePath>;
|
|
10
10
|
}) => any) | undefined | undefined;
|
|
11
11
|
fill?: string | undefined | undefined;
|
|
12
12
|
stroke?: string | undefined | undefined;
|
|
13
|
-
strokeWidth?: number |
|
|
13
|
+
strokeWidth?: number | undefined | undefined;
|
|
14
14
|
tooltip?: import("./tooltip/TooltipContext.svelte").TooltipContextValue | undefined;
|
|
15
15
|
curve?: (import("d3-shape").CurveFactory | import("d3-shape").CurveFactoryLineOnly) | undefined;
|
|
16
16
|
class?: string | undefined | undefined;
|
|
@@ -20,11 +20,11 @@ declare const __propDef: {
|
|
|
20
20
|
[x: string]: any;
|
|
21
21
|
geojson?: import("d3-geo").GeoPermissibleObjects | null | undefined;
|
|
22
22
|
render?: ((ctx: CanvasRenderingContext2D, options: {
|
|
23
|
-
|
|
23
|
+
newGeoPath: () => ReturnType<typeof import("..").geoCurvePath>;
|
|
24
24
|
}) => any) | undefined | undefined;
|
|
25
25
|
fill?: string | undefined | undefined;
|
|
26
26
|
stroke?: string | undefined | undefined;
|
|
27
|
-
strokeWidth?: number |
|
|
27
|
+
strokeWidth?: number | undefined | undefined;
|
|
28
28
|
tooltip?: import("./tooltip/TooltipContext.svelte").TooltipContextValue | undefined;
|
|
29
29
|
curve?: (import("d3-shape").CurveFactory | import("d3-shape").CurveFactoryLineOnly) | undefined;
|
|
30
30
|
class?: string | undefined | undefined;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { tick } from 'svelte';
|
|
2
|
+
import { onDestroy, tick } from 'svelte';
|
|
3
3
|
import type { spring as springStore, tweened as tweenedStore } from 'svelte/motion';
|
|
4
4
|
|
|
5
5
|
import { chartContext } from './ChartContext.svelte';
|
|
6
6
|
import { motionStore } from '../stores/motionStore.js';
|
|
7
|
+
import { getCanvasContext } from './layout/Canvas.svelte';
|
|
7
8
|
|
|
8
9
|
const { width, height } = chartContext();
|
|
9
10
|
|
|
@@ -44,23 +45,50 @@
|
|
|
44
45
|
$: if (center || x != null || y != null) {
|
|
45
46
|
transform = `translate(${$tweened_x ?? 0}, ${$tweened_y ?? 0})`;
|
|
46
47
|
}
|
|
47
|
-
</script>
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
{
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
const canvasContext = getCanvasContext();
|
|
50
|
+
const renderContext = canvasContext ? 'canvas' : 'svg';
|
|
51
|
+
|
|
52
|
+
function render(ctx: CanvasRenderingContext2D) {
|
|
53
|
+
ctx.translate($tweened_x ?? 0, $tweened_y ?? 0);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let canvasUnregister: ReturnType<typeof canvasContext.register>;
|
|
57
|
+
$: if (renderContext === 'canvas') {
|
|
58
|
+
canvasUnregister = canvasContext.register({ name: 'Group', render, retainState: true });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
$: if (renderContext === 'canvas') {
|
|
62
|
+
$tweened_x && $tweened_y;
|
|
63
|
+
canvasContext.invalidate();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
onDestroy(() => {
|
|
67
|
+
if (renderContext === 'canvas') {
|
|
68
|
+
canvasUnregister();
|
|
62
69
|
}
|
|
63
|
-
}
|
|
64
|
-
>
|
|
70
|
+
});
|
|
71
|
+
</script>
|
|
72
|
+
|
|
73
|
+
{#if renderContext === 'canvas'}
|
|
65
74
|
<slot />
|
|
66
|
-
|
|
75
|
+
{:else if renderContext === 'svg'}
|
|
76
|
+
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
|
77
|
+
<g
|
|
78
|
+
{transform}
|
|
79
|
+
{...$$restProps}
|
|
80
|
+
on:click
|
|
81
|
+
on:pointerdown
|
|
82
|
+
on:pointerenter
|
|
83
|
+
on:pointermove
|
|
84
|
+
on:pointerleave
|
|
85
|
+
on:touchmove={(e) => {
|
|
86
|
+
if (preventTouchMove) {
|
|
87
|
+
// Prevent touch to not interfer with pointer
|
|
88
|
+
e.preventDefault();
|
|
89
|
+
}
|
|
90
|
+
}}
|
|
91
|
+
>
|
|
92
|
+
<slot />
|
|
93
|
+
</g>
|
|
94
|
+
{/if}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { createEventDispatcher
|
|
3
|
-
import { writable } from 'svelte/store';
|
|
4
|
-
import { scaleCanvas } from 'layercake';
|
|
2
|
+
import { createEventDispatcher } from 'svelte';
|
|
5
3
|
import { cls } from '@layerstack/tailwind';
|
|
6
4
|
|
|
7
5
|
import { chartContext } from './ChartContext.svelte';
|
|
@@ -27,18 +25,6 @@
|
|
|
27
25
|
};
|
|
28
26
|
}>();
|
|
29
27
|
|
|
30
|
-
const cntxt = {
|
|
31
|
-
ctx: writable({}),
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
onMount(() => {
|
|
35
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext#willreadfrequently
|
|
36
|
-
scaleCanvas(context, $width, $height);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
$: cntxt.ctx.set(context);
|
|
40
|
-
setContext('canvas', cntxt);
|
|
41
|
-
|
|
42
28
|
function* rgbColorGenerator(step = 500) {
|
|
43
29
|
let nextColor = 1;
|
|
44
30
|
|
|
@@ -124,9 +110,9 @@
|
|
|
124
110
|
dispatch('click', { event: e, data });
|
|
125
111
|
}
|
|
126
112
|
}}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
{
|
|
131
|
-
|
|
132
|
-
|
|
113
|
+
>
|
|
114
|
+
<!-- Do not render while dragging to improve interaction performance -->
|
|
115
|
+
{#if !$dragging}
|
|
116
|
+
<slot nextColor={() => colorGenerator.next().value} {setColorData}></slot>
|
|
117
|
+
{/if}
|
|
118
|
+
</Canvas>
|
|
@@ -12,7 +12,7 @@ declare const __propDef: {
|
|
|
12
12
|
tickValues?: any[] | undefined | undefined;
|
|
13
13
|
tickFontSize?: number | undefined;
|
|
14
14
|
tickLength?: number | undefined;
|
|
15
|
-
placement?: ("center" | "
|
|
15
|
+
placement?: ("center" | "bottom" | "left" | "right" | "top" | "top-left" | "top-right" | "bottom-left" | "bottom-right") | undefined;
|
|
16
16
|
orientation?: "horizontal" | "vertical" | undefined;
|
|
17
17
|
onClick?: ((tick: any) => any) | undefined | undefined;
|
|
18
18
|
variant?: "ramp" | "swatches" | undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { tick, type ComponentProps } from 'svelte';
|
|
2
|
+
import { onDestroy, tick, type ComponentProps } from 'svelte';
|
|
3
3
|
import type { spring as springStore, tweened as tweenedStore } from 'svelte/motion';
|
|
4
4
|
import { cls } from '@layerstack/tailwind';
|
|
5
5
|
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
import { uniqueId } from '@layerstack/utils';
|
|
8
8
|
|
|
9
9
|
import Marker from './Marker.svelte';
|
|
10
|
+
import { renderPathData } from '../utils/canvas.js';
|
|
11
|
+
import { getCanvasContext } from './layout/Canvas.svelte';
|
|
10
12
|
|
|
11
13
|
export let x1: number;
|
|
12
14
|
export let initialX1 = x1;
|
|
@@ -20,6 +22,10 @@
|
|
|
20
22
|
export let y2: number;
|
|
21
23
|
export let initialY2 = y2;
|
|
22
24
|
|
|
25
|
+
export let fill: string | undefined = undefined;
|
|
26
|
+
export let stroke: string | undefined = undefined;
|
|
27
|
+
export let strokeWidth: number | undefined = undefined;
|
|
28
|
+
|
|
23
29
|
/** Marker to attach to start and end points of path */
|
|
24
30
|
export let marker: ComponentProps<Marker>['type'] | ComponentProps<Marker> | undefined =
|
|
25
31
|
undefined;
|
|
@@ -47,37 +53,70 @@
|
|
|
47
53
|
tweened_x2.set(x2);
|
|
48
54
|
tweened_y2.set(y2);
|
|
49
55
|
});
|
|
56
|
+
|
|
57
|
+
const canvasContext = getCanvasContext();
|
|
58
|
+
const renderContext = canvasContext ? 'canvas' : 'svg';
|
|
59
|
+
|
|
60
|
+
function render(ctx: CanvasRenderingContext2D) {
|
|
61
|
+
const pathData = `M ${$tweened_x1},${$tweened_y1} L ${$tweened_x2},${$tweened_y2}`;
|
|
62
|
+
renderPathData(ctx, pathData, {
|
|
63
|
+
styles: { fill, stroke, strokeWidth },
|
|
64
|
+
classes: $$props.class,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let canvasUnregister: ReturnType<typeof canvasContext.register>;
|
|
69
|
+
$: if (renderContext === 'canvas') {
|
|
70
|
+
canvasUnregister = canvasContext.register({ name: 'Line', render });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
$: if (renderContext === 'canvas') {
|
|
74
|
+
// Redraw when props changes (TODO: styles, class, etc)
|
|
75
|
+
$tweened_x1 && $tweened_y1 && $tweened_x2 && $tweened_y2;
|
|
76
|
+
canvasContext.invalidate();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
onDestroy(() => {
|
|
80
|
+
if (renderContext === 'canvas') {
|
|
81
|
+
canvasUnregister();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
50
84
|
</script>
|
|
51
85
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
86
|
+
{#if renderContext === 'svg'}
|
|
87
|
+
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
|
88
|
+
<line
|
|
89
|
+
x1={$tweened_x1}
|
|
90
|
+
y1={$tweened_y1}
|
|
91
|
+
x2={$tweened_x2}
|
|
92
|
+
y2={$tweened_y2}
|
|
93
|
+
{fill}
|
|
94
|
+
{stroke}
|
|
95
|
+
stroke-width={strokeWidth}
|
|
96
|
+
marker-start={markerStartId ? `url(#${markerStartId})` : undefined}
|
|
97
|
+
marker-end={markerEndId ? `url(#${markerEndId})` : undefined}
|
|
98
|
+
class={cls($$props.stroke === undefined && 'stroke-surface-content')}
|
|
99
|
+
{...$$restProps}
|
|
100
|
+
on:click
|
|
101
|
+
on:pointermove
|
|
102
|
+
on:pointerleave
|
|
103
|
+
/>
|
|
104
|
+
|
|
105
|
+
<slot name="markerStart" id={markerStartId}>
|
|
106
|
+
{#if markerStart}
|
|
107
|
+
<Marker
|
|
108
|
+
id={markerStartId}
|
|
109
|
+
type={typeof markerStart === 'string' ? markerStart : undefined}
|
|
110
|
+
{...typeof markerStart === 'object' ? markerStart : null}
|
|
111
|
+
/>
|
|
112
|
+
{/if}
|
|
113
|
+
</slot>
|
|
114
|
+
|
|
115
|
+
<slot name="markerEnd" id={markerEndId}>
|
|
69
116
|
<Marker
|
|
70
|
-
id={
|
|
71
|
-
type={typeof
|
|
72
|
-
{...typeof
|
|
117
|
+
id={markerEndId}
|
|
118
|
+
type={typeof markerEnd === 'string' ? markerEnd : undefined}
|
|
119
|
+
{...typeof markerEnd === 'object' ? markerEnd : null}
|
|
73
120
|
/>
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
<slot name="markerEnd" id={markerEndId}>
|
|
78
|
-
<Marker
|
|
79
|
-
id={markerEndId}
|
|
80
|
-
type={typeof markerEnd === 'string' ? markerEnd : undefined}
|
|
81
|
-
{...typeof markerEnd === 'object' ? markerEnd : null}
|
|
82
|
-
/>
|
|
83
|
-
</slot>
|
|
121
|
+
</slot>
|
|
122
|
+
{/if}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
+
import { onDestroy } from 'svelte';
|
|
2
3
|
import { uniqueId } from '@layerstack/utils';
|
|
3
4
|
|
|
5
|
+
import { chartContext } from './ChartContext.svelte';
|
|
6
|
+
import { getCanvasContext } from './layout/Canvas.svelte';
|
|
7
|
+
import { getComputedStyles } from '../utils/canvas.js';
|
|
8
|
+
import { parsePercent } from '../utils/math.js';
|
|
9
|
+
|
|
4
10
|
/** Unique id for linearGradient */
|
|
5
11
|
export let id: string = uniqueId('linearGradient-');
|
|
6
12
|
|
|
@@ -21,31 +27,88 @@
|
|
|
21
27
|
|
|
22
28
|
/** Define the coordinate system for attributes (i.e. gradientUnits) */
|
|
23
29
|
export let units: 'objectBoundingBox' | 'userSpaceOnUse' = 'objectBoundingBox';
|
|
30
|
+
|
|
31
|
+
const { width, height, padding } = chartContext();
|
|
32
|
+
|
|
33
|
+
const canvasContext = getCanvasContext();
|
|
34
|
+
const renderContext = canvasContext ? 'canvas' : 'svg';
|
|
35
|
+
|
|
36
|
+
let canvasGradient: CanvasGradient;
|
|
37
|
+
|
|
38
|
+
function render(ctx: CanvasRenderingContext2D) {
|
|
39
|
+
// TODO: Use x1/y1/x2/y2 values (convert from pecentage strings)
|
|
40
|
+
const gradient = ctx.createLinearGradient(
|
|
41
|
+
$padding.left,
|
|
42
|
+
$padding.top,
|
|
43
|
+
vertical ? $padding.left : $width - $padding.right,
|
|
44
|
+
vertical ? $height + $padding.bottom : $padding.top
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// Use `getComputedStyles()` to convert each stop (if using CSS variables and/or classes) to color values
|
|
48
|
+
stops.forEach((stop, i) => {
|
|
49
|
+
if (Array.isArray(stop)) {
|
|
50
|
+
const { fill } = getComputedStyles(ctx.canvas, {
|
|
51
|
+
styles: { fill: stop[1] },
|
|
52
|
+
classes: $$props.class,
|
|
53
|
+
});
|
|
54
|
+
gradient.addColorStop(parsePercent(stop[0]), fill);
|
|
55
|
+
} else {
|
|
56
|
+
const { fill } = getComputedStyles(ctx.canvas, {
|
|
57
|
+
styles: { fill: stop },
|
|
58
|
+
classes: $$props.class,
|
|
59
|
+
});
|
|
60
|
+
gradient.addColorStop(i / (stops.length - 1), fill);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
canvasGradient = gradient;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let canvasUnregister: ReturnType<typeof canvasContext.register>;
|
|
68
|
+
$: if (renderContext === 'canvas') {
|
|
69
|
+
canvasUnregister = canvasContext.register({ name: 'Gradient', render });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
$: if (renderContext === 'canvas') {
|
|
73
|
+
// Redraw when props changes (TODO: styles, class, etc)
|
|
74
|
+
stops && x1 && y1 && x2 && y2 && $width && $height;
|
|
75
|
+
canvasContext.invalidate();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
onDestroy(() => {
|
|
79
|
+
if (renderContext === 'canvas') {
|
|
80
|
+
canvasUnregister();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
24
83
|
</script>
|
|
25
84
|
|
|
26
|
-
|
|
27
|
-
<
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
{
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
{
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
85
|
+
{#if renderContext === 'canvas'}
|
|
86
|
+
<slot {id} gradient={canvasGradient} />
|
|
87
|
+
{:else if renderContext === 'svg'}
|
|
88
|
+
<defs>
|
|
89
|
+
<linearGradient
|
|
90
|
+
{id}
|
|
91
|
+
{x1}
|
|
92
|
+
{y1}
|
|
93
|
+
{x2}
|
|
94
|
+
{y2}
|
|
95
|
+
gradientTransform={rotate ? `rotate(${rotate})` : ''}
|
|
96
|
+
gradientUnits={units}
|
|
97
|
+
{...$$restProps}
|
|
98
|
+
>
|
|
99
|
+
<slot name="stops">
|
|
100
|
+
{#if stops}
|
|
101
|
+
{#each stops as stop, i}
|
|
102
|
+
{#if Array.isArray(stop)}
|
|
103
|
+
<stop offset={stop[0]} stop-color={stop[1]} />
|
|
104
|
+
{:else}
|
|
105
|
+
<stop offset="{i * (100 / (stops.length - 1))}%" stop-color={stop} />
|
|
106
|
+
{/if}
|
|
107
|
+
{/each}
|
|
108
|
+
{/if}
|
|
109
|
+
</slot>
|
|
110
|
+
</linearGradient>
|
|
111
|
+
</defs>
|
|
112
|
+
|
|
113
|
+
<slot {id} gradient="url(#{id})" />
|
|
114
|
+
{/if}
|
|
@@ -16,11 +16,11 @@ declare const __propDef: {
|
|
|
16
16
|
[evt: string]: CustomEvent<any>;
|
|
17
17
|
};
|
|
18
18
|
slots: {
|
|
19
|
-
stops: {};
|
|
20
19
|
default: {
|
|
21
20
|
id: string;
|
|
22
|
-
|
|
21
|
+
gradient: string;
|
|
23
22
|
};
|
|
23
|
+
stops: {};
|
|
24
24
|
};
|
|
25
25
|
};
|
|
26
26
|
export type LinearGradientProps = typeof __propDef.props;
|
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
</script>
|
|
4
4
|
|
|
5
5
|
<script lang="ts">
|
|
6
|
-
import {
|
|
7
|
-
import type { Readable } from 'svelte/store';
|
|
6
|
+
import { onDestroy, type ComponentProps } from 'svelte';
|
|
8
7
|
import { extent } from 'd3-array';
|
|
9
8
|
import { pointRadial } from 'd3-shape';
|
|
10
9
|
import { notNull } from '@layerstack/utils';
|
|
@@ -13,6 +12,7 @@
|
|
|
13
12
|
import Circle from './Circle.svelte';
|
|
14
13
|
import Link from './Link.svelte';
|
|
15
14
|
import { isScaleBand, type AnyScale } from '../utils/scales.js';
|
|
15
|
+
import { getCanvasContext } from './layout/Canvas.svelte';
|
|
16
16
|
|
|
17
17
|
const context = chartContext() as any;
|
|
18
18
|
const {
|
|
@@ -25,9 +25,6 @@
|
|
|
25
25
|
yGet,
|
|
26
26
|
cGet,
|
|
27
27
|
rGet,
|
|
28
|
-
padding,
|
|
29
|
-
containerWidth,
|
|
30
|
-
containerHeight,
|
|
31
28
|
config,
|
|
32
29
|
radial,
|
|
33
30
|
} = context;
|
|
@@ -45,8 +42,9 @@
|
|
|
45
42
|
export let links: boolean | Partial<ComponentProps<Link>> = false;
|
|
46
43
|
|
|
47
44
|
export let fill: string | undefined = undefined;
|
|
45
|
+
export let fillOpacity: number | undefined = undefined;
|
|
48
46
|
export let stroke: string | undefined = undefined;
|
|
49
|
-
export let strokeWidth: number |
|
|
47
|
+
export let strokeWidth: number | undefined = undefined;
|
|
50
48
|
|
|
51
49
|
/** Render to canvas */
|
|
52
50
|
export let render: ((ctx: CanvasRenderingContext2D, points: Point[]) => any) | undefined =
|
|
@@ -55,9 +53,6 @@
|
|
|
55
53
|
let className: string | undefined = undefined;
|
|
56
54
|
export { className as class };
|
|
57
55
|
|
|
58
|
-
const canvas = getContext<{ ctx: Readable<CanvasRenderingContext2D> }>('canvas');
|
|
59
|
-
const DEFAULT_FILL = 'rgb(0, 0, 0)';
|
|
60
|
-
|
|
61
56
|
function getOffset(value: any, offset: Offset, scale: AnyScale) {
|
|
62
57
|
if (typeof offset === 'function') {
|
|
63
58
|
return offset(value, context);
|
|
@@ -165,46 +160,31 @@
|
|
|
165
160
|
}
|
|
166
161
|
});
|
|
167
162
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
$: if (renderContext === 'canvas' && $ctx) {
|
|
171
|
-
let computedStyles: Partial<CSSStyleDeclaration> = {};
|
|
172
|
-
|
|
173
|
-
// Transfer classes defined on <GeoPath> to <canvas> to enable window.getComputedStyle() retrieval (Tailwind classes, etc)
|
|
174
|
-
if (className) {
|
|
175
|
-
$ctx.canvas.classList.add(...className.split(' '));
|
|
176
|
-
computedStyles = window.getComputedStyle($ctx.canvas);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// Clear with negative offset due to Canvas `context.translate(...)`
|
|
180
|
-
$ctx.clearRect(-$padding.left, -$padding.top, $containerWidth, $containerHeight);
|
|
163
|
+
const canvasContext = getCanvasContext();
|
|
164
|
+
const renderContext = canvasContext ? 'canvas' : 'svg';
|
|
181
165
|
|
|
166
|
+
function _render(ctx: CanvasRenderingContext2D) {
|
|
182
167
|
if (render) {
|
|
183
|
-
render(
|
|
168
|
+
render(ctx, points);
|
|
184
169
|
} else {
|
|
185
|
-
|
|
186
|
-
$ctx.beginPath();
|
|
187
|
-
$ctx.arc(point.x, point.y, point.r, 0, 2 * Math.PI, false);
|
|
188
|
-
|
|
189
|
-
$ctx.lineWidth = Number(strokeWidth ?? 0);
|
|
190
|
-
$ctx.strokeStyle =
|
|
191
|
-
(stroke ?? computedStyles.stroke === 'none')
|
|
192
|
-
? 'transparent'
|
|
193
|
-
: (computedStyles.stroke ?? '');
|
|
194
|
-
$ctx.stroke();
|
|
195
|
-
|
|
196
|
-
$ctx.fillStyle =
|
|
197
|
-
fill ??
|
|
198
|
-
(computedStyles.fill !== DEFAULT_FILL ? computedStyles.fill : undefined) ??
|
|
199
|
-
'transparent';
|
|
200
|
-
$ctx.fill();
|
|
201
|
-
});
|
|
170
|
+
// Rendered below
|
|
202
171
|
}
|
|
203
172
|
}
|
|
173
|
+
|
|
174
|
+
let canvasUnregister: ReturnType<typeof canvasContext.register>;
|
|
175
|
+
$: if (renderContext === 'canvas') {
|
|
176
|
+
canvasUnregister = canvasContext.register({ name: 'Points', render: _render });
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
onDestroy(() => {
|
|
180
|
+
if (renderContext === 'canvas') {
|
|
181
|
+
canvasUnregister();
|
|
182
|
+
}
|
|
183
|
+
});
|
|
204
184
|
</script>
|
|
205
185
|
|
|
206
186
|
<slot {points}>
|
|
207
|
-
{#if renderContext === 'svg'}
|
|
187
|
+
{#if renderContext === 'svg' || (renderContext === 'canvas' && !render)}
|
|
208
188
|
{#if links}
|
|
209
189
|
<g class="link-group">
|
|
210
190
|
{#each _links as link}
|
|
@@ -225,7 +205,9 @@
|
|
|
225
205
|
cy={$radial ? radialPoint[1] : point.y}
|
|
226
206
|
r={point.r}
|
|
227
207
|
fill={fill ?? ($config.c ? $cGet(point.data) : null)}
|
|
208
|
+
{fillOpacity}
|
|
228
209
|
{stroke}
|
|
210
|
+
{strokeWidth}
|
|
229
211
|
class={className}
|
|
230
212
|
{...$$restProps}
|
|
231
213
|
/>
|
|
@@ -18,8 +18,9 @@ declare const __propDef: {
|
|
|
18
18
|
offsetY?: number | ((value: number, context: any) => number) | undefined;
|
|
19
19
|
links?: (boolean | Partial<ComponentProps<Link>>) | undefined;
|
|
20
20
|
fill?: string | undefined | undefined;
|
|
21
|
+
fillOpacity?: number | undefined | undefined;
|
|
21
22
|
stroke?: string | undefined | undefined;
|
|
22
|
-
strokeWidth?: number |
|
|
23
|
+
strokeWidth?: number | undefined | undefined;
|
|
23
24
|
render?: ((ctx: CanvasRenderingContext2D, points: Point[]) => any) | undefined | undefined;
|
|
24
25
|
class?: string | undefined | undefined;
|
|
25
26
|
};
|