-
Notifications
You must be signed in to change notification settings - Fork 0
/
+page.svelte
273 lines (256 loc) · 8.63 KB
/
+page.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<script lang="ts">
import * as Card from "$lib/components/ui/card";
import {scaleOrdinal, scaleTime } from 'd3-scale';
import { flatGroup } from 'd3-array';
import { stack } from 'd3-shape';
import { format as formatDate } from 'date-fns';
import {Separator } from '$lib/components/ui/separator'
import { PeriodType, format } from 'svelte-ux';
import { ToggleGroup as ToggleGroupUX, ToggleOption, TogglePanel } from 'svelte-ux';
import {Chart, Svg, Axis, Area, Tooltip, TooltipItem, Highlight, Point, Text,
LinearGradient, RectClipPath, AreaStack } from 'layerchart'
import { createDateSeries } from '../../../node_modules/layerchart/dist/utils/genData';
import { pivotLonger } from '../../../node_modules/layerchart/dist/utils/pivot';
import { flatten } from '../../../node_modules/svelte-ux/dist/utils/array';
import { appleStock } from './dateSeries';
const data = createDateSeries({ count: 30, min: 50, max: 100, value: 'integer' });
const keys = ['apples', 'bananas', 'oranges'];
const multiSeriesData = createDateSeries({
count: 30,
min: 10,
max: 100,
value: 'integer',
keys,
});
const multiSeriesFlatData = pivotLonger(multiSeriesData, keys, 'fruit', 'value');
const dataByFruit = flatGroup(multiSeriesFlatData, (d: { fruit: string }) => d.fruit);
const fruitColors = {
apples: 'var(--color-blue-500)',
bananas: 'var(--color-purple-500)',
oranges: 'var(--color-green-500)', };
// const fruitColors = {
// apples: 'gold',
// bananas: 'indigo',
// oranges: 'green',
// };
let selectedStr = 'a';
</script>
<!-- <h2 class="text-3xl font-bold tracking-tight ml-4 my-4">Svelte-UX - Toggle Group</h2>
<div class="inline-grid gap-2 p-2">
<ToggleGroupUX variant="fill" bind:value={selectedStr}>
<ToggleOption value="a">All</ToggleOption>
<ToggleOption value="b">Male</ToggleOption>
<ToggleOption value="c">Female</ToggleOption>
</ToggleGroupUX>
</div>
<div>
Toggle value: {selectedStr}
</div>
<Separator />
<h2 class="text-3xl font-bold tracking-tight ml-4 my-4">DaisyUI - Timeline</h2>
<Card.Content class="mt-2">
<ul class="timeline timeline-vertical [--timeline-col-start:2rem]">
<li>
<div class="timeline-start">1984</div>
<div class="timeline-end timeline-box">First Macintosh computer</div>
<div class="timeline-middle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z" clip-rule="evenodd" /></svg>
</div>
<hr/>
</li>
<li>
<hr/>
<div class="timeline-start">1998</div>
<div class="timeline-middle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z" clip-rule="evenodd" /></svg>
</div>
<div class="timeline-end timeline-box">iMac</div>
<hr/>
</li>
<li>
<hr/>
<div class="timeline-start">2001</div>
<div class="timeline-middle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z" clip-rule="evenodd" /></svg>
</div>
<div class="timeline-end timeline-box">iPod</div>
<hr/>
</li>
<li>
<hr/>
<div class="timeline-start">2007</div>
<div class="timeline-middle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z" clip-rule="evenodd" /></svg>
</div>
<div class="timeline-end timeline-box">iPhone</div>
<hr/>
</li>
<li>
<hr/>
<div class="timeline-start">2015</div>
<div class="timeline-middle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z" clip-rule="evenodd" /></svg>
</div>
<div class="timeline-end timeline-box">Apple Watch</div>
</li>
</ul>
</Card.Content> -->
<h1>Area - With Tooltip and Highlight</h1>
<div class="h-[300px] p-4 border rounded">
<Chart
{data}
x="date"
xScale={scaleTime()}
y="value"
yDomain={[0, null]}
yNice
padding={{ left: 16, bottom: 24 }}
tooltip
>
<Svg>
<Axis placement="left" grid rule />
<Axis
placement="bottom"
format={(d) => format(d, PeriodType.Day, "short")}
rule
/>
<Area
line={{ class: "stroke-2 stroke-chart" }}
class="fill-chart/30"
/>
<Highlight points lines />
</Svg>
<Tooltip header={(data) => formatDate(data.date, "eee, MMMM do")} let:data>
<TooltipItem label="value" value={data.value} />
</Tooltip>
</Chart>
</div>
<h2>Multiple series (highlight on hover)</h2>
<div class="h-[300px] p-4 border rounded">
<Chart
data={multiSeriesFlatData}
x="date"
xScale={scaleTime()}
y="value"
yDomain={[0, null]}
yNice
r="fruit"
rScale={scaleOrdinal()}
rDomain={Object.keys(fruitColors)}
rRange={Object.values(fruitColors)}
padding={{ left: 16, bottom: 24, right: 48 }}
tooltip={{ mode: "voronoi" }}
let:tooltip
let:rScale
>
<Svg>
<Axis placement="left" grid rule />
<Axis
placement="bottom"
format={(d) => format(d, PeriodType.Day, "short")}
rule
/>
{#each dataByFruit as [fruit, data]}
{@const color =
tooltip.data == null || tooltip.data.fruit === fruit
? rScale(fruit)
: "#ddd"}
<Area
{data}
fill={color}
fill-opacity={0.3}
line={{ class: "stroke-2", stroke: color }}
/>
<Point d={data[data.length - 1]} let:x let:y>
<circle cx={x} cy={y} r={4} fill={color} />
<Text
{x}
{y}
value={fruit}
verticalAnchor="middle"
dx={6}
dy={-2}
class="text-xs"
fill={color}
/>
</Point>
{/each}
<Highlight points lines />
</Svg>
<Tooltip header={(data) => formatDate(data.date, "eee, MMMM do")} let:data>
<TooltipItem label={data.fruit} value={data.value} />
</Tooltip>
</Chart>
</div>
<h1>Area - Clipped Area on Tooltip</h1>
<div class="h-[300px] border rounded">
<Chart
data={appleStock}
x="date"
xScale={scaleTime()}
y="value"
yDomain={[0, null]}
yNice
padding={{ top: 48, bottom: 24 }}
tooltip
let:width
let:height
let:padding
let:tooltip
>
<Svg>
<LinearGradient
class="from-chart/50 to-chart/0"
vertical
let:url
>
<Area
line={{ class: "stroke-2 stroke-chart opacity-20" }}
fill={url}
/>
<RectClipPath
x={0}
y={0}
width={tooltip.data ? tooltip.x : width}
{height}
spring
>
<Area line={{ class: "stroke-2 stroke-chart" }} fill={url} />
</RectClipPath>
</LinearGradient>
<Highlight
points
lines={{ class: "stroke-chart [stroke-dasharray:unset]" }}
/>
<Axis placement="bottom" />
</Svg>
<Tooltip
y={48}
xOffset={4}
variant="none"
class="text-sm font-semibold text-chart/100 leading-3"
let:data
>
{format(data.value, "currency")}
</Tooltip>
<Tooltip
x={4}
y={4}
variant="none"
class="text-sm font-semibold leading-3"
let:data
>
{format(data.date, PeriodType.Day)}
</Tooltip>
<Tooltip
x="data"
y={height + padding.top + 2}
anchor="top"
variant="none"
class="text-sm font-semibold bg-chart text-white leading-3 px-2 py-1 rounded whitespace-nowrap"
let:data
>
{format(data.date, PeriodType.Day)}
</Tooltip>
</Chart>
</div>