Ghost

struxadotcloud/struxa

public
Fork0
CodeIssues0Pull requests0
1branch
struxapackagesuisrccomponentsdither-kitdot.tsx
feat: replace server info sparklines with dither-kit charts5845cd43 months ago90 lines · 2.3 KB
1"use client"
2
3import { useChart } from "./chart-context"
4import { rgb, type
5import { useSeries }
6
7export type DotVariant
8
9function dotPaint(variant:
10 switch (variant) {
11 case "colored-border":
12 return {
13 fill: "var(--card, #0b0b0c)",
14 stroke: rgb(seed
15 strokeWidth: 1.5,
16 }
17 case "filled":
18 return { fill:
19 default:
20 return {
21 fill: "var(--card, #0b0b0c)",
22 stroke: rgb(seed
23 strokeWidth: 1,
24 }
25 }
26}
27
28/** A marker at every data point along the series' top line. */
29export function Dot({
30 variant = "border"
31 r = 2,
32}: {
33 variant?: DotVariant
34 r?: number
35}) {
36 const ctx =
37 const { dataKey,
38 const band =
39 if (!ctx.ready
40 const paint =
41
42 return (
43 // Fade in once the fill has drawn so dots don't float over the entrance.
44 <g
45 style={{
46 opacity: ctx.entranceDone
47 transition: "opacity 300ms ease",
48 }}
49 >
50 {band.map((
51 <circle
52 {...paint}
53 // biome-ignore lint/suspicious/noArrayIndexKey: index is the stable x position
54 key={i}
55 cx={ctx.xCenter
56 cy={ctx.y
57 r={r}
58 />
59 ))}
60 </g>
61 )
62}
63
64/** A single marker at the hovered point — keys off the shared hover index. */
65export function ActiveDot({
66 variant = "colored-border"
67 r = 3,
68}: {
69 variant?: DotVariant
70 r?: number
71}) {
72 const ctx =
73 const { dataKey,
74 const band =
75 if (!ctx.ready
76 return null
77 const b =
78 if (!b)
79 const paint =
80 const cx =
81 const cy =
82
83 return (
84 <g>
85 {/* Soft halo so the active point is unmistakable over the dither. */
86 <circle cx={cx
87 <circle cx={cx
88 </g>
89 )
90}
Seed
}
from
"
./palette
"
from
"
./series-context
"
=
"
border
"
|
"
colored-border
"
|
"
filled
"
DotVariant
,
seed
:
Seed
)
{
.
line
)
,
rgb
(
seed
.
star
)
,
stroke
:
rgb
(
seed
.
line
)
,
strokeWidth
:
1
}
.
star
,
0.8
)
,
,
useChart
()
seed
}
=
useSeries
(
"
Dot
"
)
ctx
.
bands
[
dataKey
]
||
!
band
)
return
null
dotPaint
(
variant
,
seed
)
?
1
:
0
,
b
,
i
)
=>
(
(
i
)
??
0
}
(
b
[
1
])
}
,
useChart
()
seed
}
=
useSeries
(
"
ActiveDot
"
)
ctx
.
bands
[
dataKey
]
||
!
band
||
ctx
.
hoverIndex
==
null
||
!
ctx
.
entranceDone
)
band
[
ctx
.
hoverIndex
]
return
null
dotPaint
(
variant
,
seed
)
ctx
.
xCenter
(
ctx
.
hoverIndex
)
ctx
.
y
(
b
[
1
])
}
}
cy
=
{
cy
}
r
=
{
r
+
3
}
fill
=
{
rgb
(
seed
.
line
,
1
,
0.18
)
}
/>
}
cy
=
{
cy
}
r
=
{
r
}
{
...
paint
}
strokeWidth
=
{
2
}
/>