layerchart 0.54.0 → 0.54.1
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 +170 -144
- package/dist/components/Area.svelte +96 -67
- package/dist/components/Area.svelte.d.ts +1 -0
- package/dist/components/Axis.svelte +205 -155
- package/dist/components/Bar.svelte +72 -45
- package/dist/components/Bars.svelte +45 -34
- package/dist/components/Blur.svelte +5 -3
- package/dist/components/Bounds.svelte +37 -21
- package/dist/components/Brush.svelte +181 -110
- package/dist/components/Calendar.svelte +51 -38
- package/dist/components/Chart.svelte +295 -74
- package/dist/components/Chart.svelte.d.ts +17 -17
- package/dist/components/ChartClipPath.svelte +8 -5
- package/dist/components/ChartContext.svelte +243 -93
- package/dist/components/ChartContext.svelte.d.ts +15 -23
- package/dist/components/Circle.svelte +25 -16
- package/dist/components/CircleClipPath.svelte +16 -10
- package/dist/components/ClipPath.svelte +11 -7
- package/dist/components/ColorRamp.svelte +12 -10
- package/dist/components/ForceSimulation.svelte +185 -116
- package/dist/components/Frame.svelte +10 -6
- package/dist/components/GeoCircle.svelte +15 -9
- package/dist/components/GeoContext.svelte +109 -62
- package/dist/components/GeoEdgeFade.svelte +20 -14
- package/dist/components/GeoPath.svelte +107 -69
- package/dist/components/GeoPoint.svelte +32 -18
- package/dist/components/GeoSpline.svelte +30 -22
- package/dist/components/GeoTile.svelte +40 -30
- package/dist/components/GeoVisible.svelte +10 -7
- package/dist/components/Graticule.svelte +14 -8
- package/dist/components/Grid.svelte +75 -48
- package/dist/components/Group.svelte +43 -31
- package/dist/components/Highlight.svelte +284 -243
- package/dist/components/HitCanvas.svelte +75 -42
- package/dist/components/Hull.svelte +40 -20
- package/dist/components/Labels.svelte +81 -70
- package/dist/components/Legend.svelte +105 -74
- package/dist/components/Legend.svelte.d.ts +1 -1
- package/dist/components/Line.svelte +29 -19
- package/dist/components/LinearGradient.svelte +21 -15
- package/dist/components/Link.svelte +44 -22
- package/dist/components/MonthPath.svelte +23 -16
- package/dist/components/MotionPath.svelte +34 -25
- package/dist/components/Pack.svelte +21 -14
- package/dist/components/Partition.svelte +35 -20
- package/dist/components/Pattern.svelte +8 -6
- package/dist/components/Pie.svelte +76 -57
- package/dist/components/Point.svelte +11 -7
- package/dist/components/Points.svelte +178 -143
- package/dist/components/RadialGradient.svelte +25 -18
- package/dist/components/Rect.svelte +33 -19
- package/dist/components/RectClipPath.svelte +16 -11
- package/dist/components/Rule.svelte +50 -42
- package/dist/components/Sankey.svelte +55 -30
- package/dist/components/Spline.svelte +118 -96
- package/dist/components/Text.svelte +137 -104
- package/dist/components/Threshold.svelte +18 -7
- package/dist/components/TileImage.svelte +56 -50
- package/dist/components/TransformContext.svelte +235 -135
- package/dist/components/TransformControls.svelte +57 -29
- package/dist/components/TransformControls.svelte.d.ts +1 -1
- package/dist/components/Tree.svelte +33 -23
- package/dist/components/Treemap.svelte +69 -41
- package/dist/components/Voronoi.svelte +55 -28
- package/dist/components/charts/AreaChart.svelte +128 -77
- package/dist/components/charts/AreaChart.svelte.d.ts +1 -1
- package/dist/components/charts/BarChart.svelte +169 -104
- package/dist/components/charts/BarChart.svelte.d.ts +1 -1
- package/dist/components/charts/LineChart.svelte +87 -43
- package/dist/components/charts/LineChart.svelte.d.ts +1 -1
- package/dist/components/charts/PieChart.svelte +102 -52
- package/dist/components/charts/PieChart.svelte.d.ts +1 -1
- package/dist/components/charts/ScatterChart.svelte +73 -38
- package/dist/components/charts/ScatterChart.svelte.d.ts +1 -1
- package/dist/components/layout/Canvas.svelte +63 -43
- package/dist/components/layout/Html.svelte +28 -18
- package/dist/components/layout/Svg.svelte +47 -32
- package/dist/components/tooltip/Tooltip.svelte +137 -91
- package/dist/components/tooltip/Tooltip.svelte.d.ts +1 -1
- package/dist/components/tooltip/TooltipContext.svelte +315 -249
- package/dist/components/tooltip/TooltipHeader.svelte +9 -3
- package/dist/components/tooltip/TooltipItem.svelte +17 -9
- package/dist/components/tooltip/TooltipList.svelte +2 -1
- package/dist/components/tooltip/TooltipSeparator.svelte +3 -2
- package/dist/docs/Blockquote.svelte +4 -3
- package/dist/docs/Code.svelte +15 -8
- package/dist/docs/CurveMenuField.svelte +17 -12
- package/dist/docs/GeoDebug.svelte +13 -9
- package/dist/docs/Header1.svelte +2 -1
- package/dist/docs/Json.svelte +6 -4
- package/dist/docs/Layout.svelte +6 -6
- package/dist/docs/PathDataMenuField.svelte +52 -44
- package/dist/docs/Preview.svelte +39 -33
- package/dist/docs/TilesetField.svelte +80 -62
- package/dist/docs/TransformDebug.svelte +8 -5
- package/dist/docs/ViewSourceButton.svelte +13 -9
- package/dist/stores/motionStore.d.ts +1 -1
- package/dist/utils/scales.d.ts +3 -3
- package/package.json +29 -30
|
@@ -1,26 +1,42 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { scaleLinear } from 'd3-scale';
|
|
3
|
+
|
|
4
|
+
import { chartContext } from './ChartContext.svelte';
|
|
5
|
+
import { motionScale } from '../utils/scales.js';
|
|
6
|
+
|
|
7
|
+
const { width, height } = chartContext();
|
|
8
|
+
|
|
9
|
+
type Extents = Partial<{ x0: number; y0: number; x1: number; y1: number }>;
|
|
10
|
+
type ExtentsAcccessor = (dimensions: { width: number; height: number }) => Extents;
|
|
11
|
+
|
|
12
|
+
export let domain: Extents | ExtentsAcccessor | null | undefined = undefined;
|
|
13
|
+
export let range: Extents | ExtentsAcccessor | null | undefined = undefined;
|
|
14
|
+
export let spring: boolean | Parameters<typeof motionScale>[1]['spring'] = undefined;
|
|
15
|
+
export let tweened: boolean | Parameters<typeof motionScale>[1]['tweened'] = undefined;
|
|
16
|
+
|
|
17
|
+
function getExtents(
|
|
18
|
+
extents: Extents | ExtentsAcccessor | null | undefined,
|
|
19
|
+
axis: 'x' | 'y',
|
|
20
|
+
fallback: number
|
|
21
|
+
) {
|
|
22
|
+
const resolvedExtents =
|
|
23
|
+
typeof extents === 'function' ? extents({ width: $width, height: $height }) : extents;
|
|
24
|
+
|
|
11
25
|
return [
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
26
|
+
// @ts-expect-error
|
|
27
|
+
resolvedExtents?.[axis + '0'] ?? 0, // x0 or y0
|
|
28
|
+
// @ts-expect-error
|
|
29
|
+
resolvedExtents?.[axis + '1'] ?? fallback, // x1 or y1, fallback as $width or $height
|
|
16
30
|
];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
$: xScale.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const xScale = motionScale(scaleLinear as any, { spring, tweened });
|
|
34
|
+
$: xScale.domain(getExtents(domain, 'x', $width));
|
|
35
|
+
$: xScale.range(getExtents(range, 'x', $width));
|
|
36
|
+
|
|
37
|
+
const yScale = motionScale(scaleLinear as any, { spring, tweened });
|
|
38
|
+
$: yScale.domain(getExtents(domain, 'y', $height));
|
|
39
|
+
$: yScale.range(getExtents(range, 'y', $height));
|
|
24
40
|
</script>
|
|
25
41
|
|
|
26
42
|
<slot xScale={$xScale} yScale={$yScale} />
|
|
@@ -1,135 +1,206 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
export let
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
export let
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { createEventDispatcher } from 'svelte';
|
|
3
|
+
import type { SVGAttributes } from 'svelte/elements';
|
|
4
|
+
import { extent } from 'd3-array';
|
|
5
|
+
import { clamp } from '@layerstack/utils';
|
|
6
|
+
import { cls } from '@layerstack/tailwind';
|
|
7
|
+
|
|
8
|
+
import { chartContext } from './ChartContext.svelte';
|
|
9
|
+
import Frame from './Frame.svelte';
|
|
10
|
+
import { localPoint } from '../utils/event.js';
|
|
11
|
+
import Group from './Group.svelte';
|
|
12
|
+
|
|
13
|
+
const { xScale, yScale, width, height, padding } = chartContext();
|
|
14
|
+
|
|
15
|
+
const dispatch = createEventDispatcher<{
|
|
16
|
+
change: { xDomain?: [any, any]; yDomain?: [any, any] };
|
|
17
|
+
brushStart: { xDomain?: [any, any]; yDomain?: [any, any] };
|
|
18
|
+
brushEnd: { xDomain?: [any, any]; yDomain?: [any, any] };
|
|
19
|
+
}>();
|
|
20
|
+
|
|
21
|
+
/** Axis to apply brushing */
|
|
22
|
+
export let axis: 'x' | 'y' | 'both' = 'x';
|
|
23
|
+
|
|
24
|
+
/** Size of draggable handles (width/height) */
|
|
25
|
+
export let handleSize = 5;
|
|
26
|
+
|
|
27
|
+
/** Only show range while actively brushing. Useful with `brushEnd` event */
|
|
28
|
+
export let resetOnEnd = false;
|
|
29
|
+
|
|
30
|
+
export let xDomain: [number | Date | null, number | Date | null] = $xScale.domain() as [
|
|
31
|
+
number,
|
|
32
|
+
number,
|
|
33
|
+
];
|
|
34
|
+
export let yDomain: [number | Date | null, number | Date | null] = $yScale.domain() as [
|
|
35
|
+
number,
|
|
36
|
+
number,
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
// Capture original domains for reset()
|
|
40
|
+
const originalXDomain = $xScale.domain() as [number, number];
|
|
41
|
+
const originalYDomain = $yScale.domain() as [number, number];
|
|
42
|
+
|
|
43
|
+
$: [xDomainMin, xDomainMax] = extent<number>($xScale.domain()) as [number, number];
|
|
44
|
+
$: [yDomainMin, yDomainMax] = extent<number>($yScale.domain()) as [number, number];
|
|
45
|
+
|
|
46
|
+
/** Attributes passed to range <rect> element */
|
|
47
|
+
export let range: SVGAttributes<SVGRectElement> | undefined = undefined;
|
|
48
|
+
|
|
49
|
+
/** Attributes passed to handle <rect> elements */
|
|
50
|
+
export let handle: SVGAttributes<SVGRectElement> | undefined = undefined;
|
|
51
|
+
|
|
52
|
+
export let classes: {
|
|
53
|
+
root?: string;
|
|
54
|
+
frame?: string;
|
|
55
|
+
range?: string;
|
|
56
|
+
handle?: string;
|
|
57
|
+
} = {};
|
|
58
|
+
|
|
59
|
+
let frameEl: SVGRectElement;
|
|
60
|
+
|
|
61
|
+
function handler(
|
|
62
|
+
fn: (
|
|
63
|
+
start: {
|
|
64
|
+
xDomain: [number, number];
|
|
65
|
+
yDomain: [number, number];
|
|
66
|
+
value: { x: number; y: number };
|
|
67
|
+
},
|
|
68
|
+
value: { x: number; y: number }
|
|
69
|
+
) => void
|
|
70
|
+
) {
|
|
71
|
+
return (e: PointerEvent) => {
|
|
72
|
+
const start = {
|
|
73
|
+
xDomain: [xDomain[0] ?? xDomainMin, xDomain[1] ?? xDomainMax] as [number, number],
|
|
74
|
+
yDomain: [yDomain[0] ?? yDomainMin, yDomain[1] ?? yDomainMax] as [number, number],
|
|
75
|
+
value: {
|
|
76
|
+
x: $xScale.invert?.((localPoint(frameEl, e)?.x ?? 0) - $padding.left),
|
|
77
|
+
y: $yScale.invert?.((localPoint(frameEl, e)?.y ?? 0) - $padding.top),
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
dispatch('brushStart', { xDomain, yDomain });
|
|
82
|
+
|
|
83
|
+
const onPointerMove = (e: PointerEvent) => {
|
|
84
|
+
fn(start, {
|
|
85
|
+
x: $xScale.invert?.((localPoint(frameEl, e)?.x ?? 0) - $padding.left),
|
|
86
|
+
y: $yScale.invert?.((localPoint(frameEl, e)?.y ?? 0) - $padding.top),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// if (xDomain[0] === xDomain[1] || yDomain[0] === yDomain[1]) {
|
|
90
|
+
// // Ignore?
|
|
91
|
+
// // TODO: What about when using `x` or `y` axis?
|
|
92
|
+
// } else {
|
|
93
|
+
dispatch('change', { xDomain, yDomain });
|
|
94
|
+
// }
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const onPointerUp = (e: PointerEvent) => {
|
|
98
|
+
if (e.target === frameEl) {
|
|
99
|
+
reset();
|
|
100
|
+
dispatch('change', { xDomain, yDomain });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
dispatch('brushEnd', { xDomain, yDomain });
|
|
104
|
+
|
|
105
|
+
if (resetOnEnd) {
|
|
106
|
+
reset();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
window.removeEventListener('pointermove', onPointerMove);
|
|
110
|
+
window.removeEventListener('pointerup', onPointerUp);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
window.addEventListener('pointermove', onPointerMove);
|
|
114
|
+
window.addEventListener('pointerup', onPointerUp);
|
|
67
115
|
};
|
|
68
|
-
}
|
|
69
|
-
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const createRange = handler((start, value) => {
|
|
70
119
|
isActive = true;
|
|
120
|
+
|
|
71
121
|
xDomain = [
|
|
72
|
-
|
|
73
|
-
|
|
122
|
+
clamp(Math.min(start.value.x, value.x), xDomainMin, xDomainMax),
|
|
123
|
+
clamp(Math.max(start.value.x, value.x), xDomainMin, xDomainMax),
|
|
74
124
|
];
|
|
125
|
+
|
|
75
126
|
yDomain = [
|
|
76
|
-
|
|
77
|
-
|
|
127
|
+
clamp(Math.min(start.value.y, value.y), yDomainMin, yDomainMax),
|
|
128
|
+
clamp(Math.max(start.value.y, value.y), yDomainMin, yDomainMax),
|
|
78
129
|
];
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const adjustRange = handler((start, value) => {
|
|
133
|
+
const dx = clamp(
|
|
134
|
+
value.x - start.value.x,
|
|
135
|
+
xDomainMin - start.xDomain[0],
|
|
136
|
+
xDomainMax - start.xDomain[1]
|
|
137
|
+
);
|
|
82
138
|
xDomain = [Number(start.xDomain[0]) + dx, Number(start.xDomain[1]) + dx];
|
|
83
|
-
|
|
139
|
+
|
|
140
|
+
const dy = clamp(
|
|
141
|
+
value.y - start.value.y,
|
|
142
|
+
yDomainMin - start.yDomain[0],
|
|
143
|
+
yDomainMax - start.yDomain[1]
|
|
144
|
+
);
|
|
84
145
|
yDomain = [Number(start.yDomain[0]) + dy, Number(start.yDomain[1]) + dy];
|
|
85
|
-
});
|
|
86
|
-
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const adjustBottom = handler((start, value) => {
|
|
87
149
|
yDomain = [
|
|
88
|
-
|
|
89
|
-
|
|
150
|
+
clamp(value.y > start.yDomain[1] ? start.yDomain[1] : value.y, yDomainMin, yDomainMax),
|
|
151
|
+
clamp(value.y > start.yDomain[1] ? value.y : start.yDomain[1], yDomainMin, yDomainMax),
|
|
90
152
|
];
|
|
91
|
-
});
|
|
92
|
-
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const adjustTop = handler((start, value) => {
|
|
93
156
|
yDomain = [
|
|
94
|
-
|
|
95
|
-
|
|
157
|
+
clamp(value.y < start.yDomain[1] ? value.y : start.yDomain[0], yDomainMin, yDomainMax),
|
|
158
|
+
clamp(value.y < start.yDomain[1] ? start.yDomain[0] : value.y, yDomainMin, yDomainMax),
|
|
96
159
|
];
|
|
97
|
-
});
|
|
98
|
-
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const adjustLeft = handler((start, value) => {
|
|
99
163
|
xDomain = [
|
|
100
|
-
|
|
101
|
-
|
|
164
|
+
clamp(value.x > start.xDomain[1] ? start.xDomain[1] : value.x, xDomainMin, xDomainMax),
|
|
165
|
+
clamp(value.x > start.xDomain[1] ? value.x : start.xDomain[1], xDomainMin, xDomainMax),
|
|
102
166
|
];
|
|
103
|
-
});
|
|
104
|
-
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const adjustRight = handler((start, value) => {
|
|
105
170
|
xDomain = [
|
|
106
|
-
|
|
107
|
-
|
|
171
|
+
clamp(value.x < start.xDomain[0] ? value.x : start.xDomain[0], xDomainMin, xDomainMax),
|
|
172
|
+
clamp(value.x < start.xDomain[0] ? start.xDomain[0] : value.x, xDomainMin, xDomainMax),
|
|
108
173
|
];
|
|
109
|
-
});
|
|
110
|
-
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
function reset() {
|
|
111
177
|
isActive = false;
|
|
178
|
+
|
|
112
179
|
xDomain = originalXDomain;
|
|
113
180
|
yDomain = originalYDomain;
|
|
114
|
-
}
|
|
115
|
-
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function selectAll() {
|
|
116
184
|
xDomain = [xDomainMin, xDomainMax];
|
|
117
185
|
yDomain = [yDomainMin, yDomainMax];
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
$:
|
|
121
|
-
$:
|
|
122
|
-
$:
|
|
123
|
-
$:
|
|
124
|
-
|
|
125
|
-
$:
|
|
126
|
-
$:
|
|
127
|
-
|
|
128
|
-
$:
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
$: top = $yScale(yDomain[1]);
|
|
189
|
+
$: bottom = $yScale(yDomain[0]);
|
|
190
|
+
$: left = $xScale(xDomain[0]);
|
|
191
|
+
$: right = $xScale(xDomain[1]);
|
|
192
|
+
|
|
193
|
+
$: rangeTop = axis === 'both' || axis === 'y' ? top : 0;
|
|
194
|
+
$: rangeLeft = axis === 'both' || axis === 'x' ? left : 0;
|
|
195
|
+
$: rangeWidth = axis === 'both' || axis === 'x' ? right - left : $width;
|
|
196
|
+
$: rangeHeight = axis === 'both' || axis === 'y' ? bottom - top : $height;
|
|
197
|
+
|
|
198
|
+
// Set reactively to handle cases where xDomain/yDomain are set externally (ex. `bind:xDomain`)
|
|
199
|
+
$: isActive =
|
|
129
200
|
xDomain[0]?.valueOf() !== originalXDomain[0]?.valueOf() ||
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
201
|
+
xDomain[1]?.valueOf() !== originalXDomain[1]?.valueOf() ||
|
|
202
|
+
yDomain[0]?.valueOf() !== originalYDomain[0]?.valueOf() ||
|
|
203
|
+
yDomain[1]?.valueOf() !== originalYDomain[1]?.valueOf();
|
|
133
204
|
</script>
|
|
134
205
|
|
|
135
206
|
<g class={cls('Brush select-none', classes.root, $$props.class)}>
|
|
@@ -1,46 +1,59 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export let
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { type ComponentProps } from 'svelte';
|
|
3
|
+
import { timeDays, timeMonths, timeWeek, timeYear } from 'd3-time';
|
|
4
|
+
import { index } from 'd3-array';
|
|
5
|
+
import { format } from 'date-fns';
|
|
6
|
+
|
|
7
|
+
import { chartContext } from './ChartContext.svelte';
|
|
8
|
+
import Rect from './Rect.svelte';
|
|
9
|
+
import type { TooltipContextValue } from './tooltip/TooltipContext.svelte';
|
|
10
|
+
import MonthPath from './MonthPath.svelte';
|
|
11
|
+
import Text from './Text.svelte';
|
|
12
|
+
import { chartDataArray } from '../utils/common.js';
|
|
13
|
+
|
|
14
|
+
export let start: Date;
|
|
15
|
+
export let end: Date;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Size of cell. If `number`, sets width/height as same value (square). If array, sets as [width,height]. If undefined, is derived from Chart width/height
|
|
19
|
+
*/
|
|
20
|
+
export let cellSize: number | [number, number] | undefined = undefined;
|
|
21
|
+
|
|
22
|
+
/** Enable drawing path around each month. If object, pass as props to underlying <path> */
|
|
23
|
+
export let monthPath: boolean | Partial<ComponentProps<MonthPath>> = false;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Tooltip context to setup mouse events to show tooltip for related data
|
|
27
|
+
*/
|
|
28
|
+
export let tooltip: TooltipContextValue | undefined = undefined;
|
|
29
|
+
|
|
30
|
+
const { width, height, x, cGet, data, config } = chartContext();
|
|
31
|
+
|
|
32
|
+
$: yearDays = timeDays(start, end);
|
|
33
|
+
$: yearMonths = timeMonths(start, end);
|
|
34
|
+
$: yearWeeks = timeWeek.count(start, end);
|
|
35
|
+
|
|
36
|
+
$: chartCellWidth = $width / (yearWeeks + 1);
|
|
37
|
+
$: chartCellHeight = $height / 7;
|
|
38
|
+
$: chartCellSize = Math.min(chartCellWidth, chartCellHeight); // Use smallest to fit, and keep square aspect
|
|
39
|
+
|
|
40
|
+
$: [cellWidth, cellHeight] = Array.isArray(cellSize)
|
|
30
41
|
? cellSize
|
|
31
42
|
: typeof cellSize === 'number'
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
$:
|
|
43
|
+
? [cellSize, cellSize]
|
|
44
|
+
: [chartCellSize, chartCellSize];
|
|
45
|
+
|
|
46
|
+
$: dataByDate = data && $config.x ? index(chartDataArray($data), (d) => $x(d)) : new Map();
|
|
47
|
+
|
|
48
|
+
$: cells = yearDays.map((date) => {
|
|
36
49
|
const cellData = dataByDate.get(date) ?? { date };
|
|
37
50
|
return {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
51
|
+
x: timeWeek.count(timeYear(date), date) * cellWidth,
|
|
52
|
+
y: date.getDay() * cellHeight,
|
|
53
|
+
color: $config.c ? $cGet(cellData) : 'transparent',
|
|
54
|
+
data: cellData,
|
|
42
55
|
};
|
|
43
|
-
});
|
|
56
|
+
});
|
|
44
57
|
</script>
|
|
45
58
|
|
|
46
59
|
<slot {cells}>
|