@uibot/blocks-studio 0.0.0-stage → 0.2.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.
@@ -0,0 +1,140 @@
1
+ import type { RendererComponent } from "@uibot/renderer-react";
2
+
3
+ function cn(...cls: (string | false | null | undefined)[]) {
4
+ return cls.filter(Boolean).join(" ");
5
+ }
6
+
7
+ interface Series {
8
+ name?: string;
9
+ points: number[];
10
+ area?: boolean;
11
+ }
12
+
13
+ function parseSeries(raw: unknown): Series[] {
14
+ if (!Array.isArray(raw)) return [];
15
+ return raw
16
+ .map((s) => {
17
+ if (!s || typeof s !== "object") return null;
18
+ const rec = s as Record<string, unknown>;
19
+ const points = Array.isArray(rec["points"])
20
+ ? rec["points"].map((p) => (typeof p === "number" ? p : Number(p) || 0))
21
+ : [];
22
+ return {
23
+ name: typeof rec["name"] === "string" ? rec["name"] : undefined,
24
+ points,
25
+ area: rec["area"] === true,
26
+ } as Series;
27
+ })
28
+ .filter((s): s is Series => s !== null && s.points.length > 0);
29
+ }
30
+
31
+ // Padding reserved around the plot so strokes are not clipped at the edges.
32
+ const PAD = 8;
33
+
34
+ // Map a series' points into an SVG polyline `points` string. min/max are taken
35
+ // across every series so all lines share one vertical scale.
36
+ function toPolyline(points: number[], w: number, h: number, min: number, max: number): string {
37
+ const range = max - min || 1;
38
+ const innerW = w - PAD * 2;
39
+ const innerH = h - PAD * 2;
40
+ const step = points.length > 1 ? innerW / (points.length - 1) : 0;
41
+ return points
42
+ .map((value, i) => {
43
+ const x = PAD + step * i;
44
+ const y = PAD + innerH - ((value - min) / range) * innerH;
45
+ return `${x.toFixed(1)},${y.toFixed(1)}`;
46
+ })
47
+ .join(" ");
48
+ }
49
+
50
+ // A small palette of theme-token colours, cycled per series.
51
+ const STROKE = ["text-primary", "text-sky-500", "text-emerald-500", "text-amber-500"];
52
+
53
+ /**
54
+ * Pure-SVG line / area chart. No third-party charting dependency (keeps the
55
+ * package peerDeps-only). Intended for small dogfood datasets (≤ ~200 points
56
+ * per series); larger inputs produce heavy DOM.
57
+ */
58
+ export const TrendChart: RendererComponent = (props) => {
59
+ const series = parseSeries(props["series"]);
60
+ const title = typeof props["title"] === "string" ? props["title"] : "";
61
+ const w = typeof props["width"] === "number" ? props["width"] : 480;
62
+ const h = typeof props["height"] === "number" ? props["height"] : 160;
63
+
64
+ const all = series.flatMap((s) => s.points);
65
+ const min = all.length ? Math.min(...all) : 0;
66
+ const max = all.length ? Math.max(...all) : 1;
67
+
68
+ return (
69
+ <div
70
+ className="uibot-trendchart bg-card text-card-foreground rounded-xl border p-4 shadow-sm"
71
+ data-uibot-type="TrendChart"
72
+ >
73
+ {title && <h3 className="mb-2 text-sm font-medium">{title}</h3>}
74
+ {series.length === 0 ? (
75
+ <p className="py-6 text-center text-sm text-muted-foreground">No data</p>
76
+ ) : (
77
+ <svg
78
+ viewBox={`0 0 ${w} ${h}`}
79
+ className="h-auto w-full"
80
+ role="img"
81
+ aria-label={title || "Trend chart"}
82
+ preserveAspectRatio="none"
83
+ >
84
+ {/* baseline grid lines */}
85
+ {[0, 0.25, 0.5, 0.75, 1].map((t) => {
86
+ const y = PAD + (h - PAD * 2) * t;
87
+ return (
88
+ <line
89
+ key={t}
90
+ x1={PAD}
91
+ x2={w - PAD}
92
+ y1={y}
93
+ y2={y}
94
+ className="text-border"
95
+ stroke="currentColor"
96
+ strokeWidth={0.5}
97
+ />
98
+ );
99
+ })}
100
+ {series.map((s, i) => {
101
+ const pts = toPolyline(s.points, w, h, min, max);
102
+ const colour = STROKE[i % STROKE.length];
103
+ return (
104
+ <g key={i} className={colour}>
105
+ {s.area && (
106
+ <polygon
107
+ fill="currentColor"
108
+ fillOpacity={0.12}
109
+ points={`${PAD},${h - PAD} ${pts} ${w - PAD},${h - PAD}`}
110
+ />
111
+ )}
112
+ <polyline
113
+ fill="none"
114
+ stroke="currentColor"
115
+ strokeWidth={2}
116
+ strokeLinecap="round"
117
+ strokeLinejoin="round"
118
+ points={pts}
119
+ />
120
+ </g>
121
+ );
122
+ })}
123
+ </svg>
124
+ )}
125
+ {series.some((s) => s.name) && (
126
+ <ul className="mt-3 flex flex-wrap gap-4">
127
+ {series.map((s, i) => (
128
+ <li key={i} className="flex items-center gap-1.5 text-xs text-muted-foreground">
129
+ <span className={cn("h-2 w-2 rounded-full bg-current", STROKE[i % STROKE.length])} />
130
+ {s.name ?? `Series ${i + 1}`}
131
+ </li>
132
+ ))}
133
+ </ul>
134
+ )}
135
+ </div>
136
+ );
137
+ };
138
+
139
+ /** Alias of {@link TrendChart} for specs that prefer the LineChart name. */
140
+ export const LineChart = TrendChart;