@calimero-network/mero-charts 0.0.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/README.md ADDED
@@ -0,0 +1,171 @@
1
+ # @calimero/charts
2
+
3
+ A React chart library for the Calimero design system, featuring three main chart types designed for monitoring and analytics dashboards.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @calimero/charts recharts
9
+ ```
10
+
11
+ ## Chart Types
12
+
13
+ ### 1. Gauge Chart
14
+
15
+ A circular gauge component perfect for displaying metrics like CPU usage, memory usage, or any percentage-based values.
16
+
17
+ ```tsx
18
+ import { Gauge, GaugeCard } from '@calimero/charts';
19
+
20
+ // Basic gauge
21
+ <Gauge value={75} label="CPU" />
22
+
23
+ // Gauge with custom thresholds
24
+ <Gauge
25
+ value={85}
26
+ min={0}
27
+ max={100}
28
+ thresholds={{ good: 60, warning: 80, max: 100 }}
29
+ decimals={1}
30
+ />
31
+
32
+ // Gauge in a card
33
+ <GaugeCard
34
+ title="CPU Usage"
35
+ value={73.4}
36
+ suffix="%"
37
+ />
38
+ ```
39
+
40
+ #### Props
41
+
42
+ - `value` (number): The current value to display
43
+ - `min` (number, optional): Minimum value (default: 0)
44
+ - `max` (number, optional): Maximum value (default: 100)
45
+ - `label` (string, optional): Label displayed below the value
46
+ - `decimals` (number, optional): Number of decimal places (default: 1)
47
+ - `thresholds` (object, optional): Color thresholds for good/warning/max states
48
+
49
+ ### 2. Time Series Chart
50
+
51
+ A line chart component for displaying time-series data using Recharts.
52
+
53
+ ```tsx
54
+ import { SeriesCard, Series } from '@calimero/charts';
55
+
56
+ const data: Series[] = [
57
+ {
58
+ name: "Requests per second",
59
+ data: [
60
+ { t: Date.now() - 60000, y: 12.5 },
61
+ { t: Date.now() - 30000, y: 15.2 },
62
+ { t: Date.now(), y: 13.8 }
63
+ ]
64
+ }
65
+ ];
66
+
67
+ <SeriesCard
68
+ title="API Requests"
69
+ series={data}
70
+ yLabel="req/s"
71
+ />
72
+ ```
73
+
74
+ #### Props
75
+
76
+ - `title` (string): Chart title
77
+ - `series` (Series[]): Array of data series
78
+ - `yLabel` (string, optional): Y-axis label
79
+
80
+ ### 3. Vector Table
81
+
82
+ A table component for displaying Prometheus-style instant vector data.
83
+
84
+ ```tsx
85
+ import { VectorTable, PromVectorResult } from '@calimero/charts';
86
+
87
+ const vector: PromVectorResult = {
88
+ status: "success",
89
+ data: {
90
+ resultType: "vector",
91
+ result: [
92
+ {
93
+ metric: {
94
+ instance: "10.0.99.165:8429",
95
+ container: "vmsingle",
96
+ reason: "unsupported",
97
+ path: "*"
98
+ },
99
+ value: [1756235265, "0"]
100
+ }
101
+ ]
102
+ }
103
+ };
104
+
105
+ <VectorTable
106
+ title="HTTP Request Errors"
107
+ vector={vector}
108
+ />
109
+ ```
110
+
111
+ #### Props
112
+
113
+ - `vector` (PromVectorResult): The vector data to display
114
+ - `title` (string, optional): Table title
115
+
116
+ ## Demo Component
117
+
118
+ Use the `ChartsDemo` component to see all chart types in action:
119
+
120
+ ```tsx
121
+ import { ChartsDemo } from '@calimero/charts';
122
+
123
+ <ChartsDemo />
124
+ ```
125
+
126
+ ## Card Primitives
127
+
128
+ The package also exports card primitives that can be used independently:
129
+
130
+ ```tsx
131
+ import { Card, CardHeader, CardTitle, CardContent } from '@calimero/charts';
132
+
133
+ <Card>
134
+ <CardHeader>
135
+ <CardTitle>Custom Content</CardTitle>
136
+ </CardHeader>
137
+ <CardContent>
138
+ Your content here
139
+ </CardContent>
140
+ </Card>
141
+ ```
142
+
143
+ ## Styling
144
+
145
+ The charts use Calimero design tokens for consistent theming:
146
+
147
+ ### Colors
148
+ - **Success**: `#16a34a` (green) - Good zone
149
+ - **Warning**: `#f59e0b` (orange) - Warning zone
150
+ - **Error**: `#ef4444` (red) - Critical zone
151
+ - **Info**: `#3b82f6` (blue) - Informational
152
+ - **Background**: `#1A1A1A` (dark)
153
+ - **Border**: `#404040` (medium gray)
154
+ - **Text**: `white` and `#8E8E8E` (light gray)
155
+
156
+ The components are built with Tailwind CSS classes and can be customized by passing additional className props.
157
+
158
+ ## Dependencies
159
+
160
+ - React 18+
161
+ - Recharts 2.12.0+
162
+
163
+ ## Development
164
+
165
+ ```bash
166
+ # Build the package
167
+ pnpm build
168
+
169
+ # Type checking
170
+ pnpm typecheck
171
+ ```
package/dist/index.js ADDED
@@ -0,0 +1,334 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+ var recharts = require('recharts');
6
+
7
+ // src/Gauge.tsx
8
+ var defaultThresholds = { warning: 60, danger: 80 };
9
+ function clamp(n, lo, hi) {
10
+ return Math.max(lo, Math.min(hi, n));
11
+ }
12
+ function polarToCartesian(cx, cy, r, angle) {
13
+ return { x: cx + r * Math.cos(angle), y: cy + r * Math.sin(angle) };
14
+ }
15
+ function arcPath(cx, cy, r, start, end) {
16
+ const s = polarToCartesian(cx, cy, r, start);
17
+ const e = polarToCartesian(cx, cy, r, end);
18
+ const largeArc = Math.abs(end - start) <= Math.PI ? 0 : 1;
19
+ return `M ${s.x} ${s.y} A ${r} ${r} 0 ${largeArc} 1 ${e.x} ${e.y}`;
20
+ }
21
+ function Gauge({
22
+ value,
23
+ min = 0,
24
+ max = 100,
25
+ title,
26
+ label,
27
+ decimals = 1,
28
+ thresholds = defaultThresholds,
29
+ size = "md",
30
+ className = ""
31
+ }) {
32
+ const pct = react.useMemo(() => {
33
+ const normalizedValue = (value - min) / Math.max(1e-9, max - min) * 100;
34
+ return clamp(normalizedValue, 0, 100);
35
+ }, [value, min, max]);
36
+ const cx = 120, cy = 140;
37
+ const inner = 70;
38
+ const startAngle = 0.95 * Math.PI;
39
+ const endAngle = 2.05 * Math.PI;
40
+ const arcRadius = 100;
41
+ const valueEndAngle = startAngle + pct / 100 * (endAngle - startAngle);
42
+ const valueText = value.toFixed(decimals);
43
+ const getValueColor = (val) => {
44
+ if (val <= thresholds.warning) return "#A5FF11";
45
+ if (val <= thresholds.danger) return "#f59e0b";
46
+ return "#ef4444";
47
+ };
48
+ const valueColor = getValueColor(pct);
49
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex flex-col items-center", children: /* @__PURE__ */ jsxRuntime.jsxs("svg", { viewBox: "0 0 240 160", className: "w-full", children: [
50
+ /* @__PURE__ */ jsxRuntime.jsx("path", { d: arcPath(cx, cy, arcRadius, startAngle, endAngle), stroke: "#6B7280", strokeOpacity: 0.3, strokeWidth: 18, fill: "none", strokeLinecap: "butt" }),
51
+ /* @__PURE__ */ jsxRuntime.jsx(
52
+ "path",
53
+ {
54
+ d: arcPath(cx, cy, arcRadius, startAngle, valueEndAngle),
55
+ stroke: valueColor,
56
+ strokeWidth: 18,
57
+ fill: "none",
58
+ strokeLinecap: "butt"
59
+ }
60
+ ),
61
+ /* @__PURE__ */ jsxRuntime.jsx(
62
+ "path",
63
+ {
64
+ d: arcPath(cx, cy, arcRadius + 15, startAngle, startAngle + thresholds.warning / 100 * (endAngle - startAngle)),
65
+ stroke: "#A5FF11",
66
+ strokeOpacity: 0.3,
67
+ strokeWidth: 4,
68
+ fill: "none",
69
+ strokeLinecap: "butt"
70
+ }
71
+ ),
72
+ /* @__PURE__ */ jsxRuntime.jsx(
73
+ "path",
74
+ {
75
+ d: arcPath(cx, cy, arcRadius + 15, startAngle + thresholds.warning / 100 * (endAngle - startAngle), startAngle + thresholds.danger / 100 * (endAngle - startAngle)),
76
+ stroke: "#f59e0b",
77
+ strokeOpacity: 0.3,
78
+ strokeWidth: 4,
79
+ fill: "none",
80
+ strokeLinecap: "butt"
81
+ }
82
+ ),
83
+ /* @__PURE__ */ jsxRuntime.jsx(
84
+ "path",
85
+ {
86
+ d: arcPath(cx, cy, arcRadius + 15, startAngle + thresholds.danger / 100 * (endAngle - startAngle), endAngle),
87
+ stroke: "#ef4444",
88
+ strokeOpacity: 0.3,
89
+ strokeWidth: 4,
90
+ fill: "none",
91
+ strokeLinecap: "butt"
92
+ }
93
+ ),
94
+ /* @__PURE__ */ jsxRuntime.jsx("circle", { cx, cy, r: inner, fill: "#1F2A15" }),
95
+ " ",
96
+ /* @__PURE__ */ jsxRuntime.jsx("text", { x: cx, y: cy - 8, textAnchor: "middle", dominantBaseline: "middle", style: { fontWeight: 600, fontSize: "28px", fill: valueColor }, children: valueText }),
97
+ label && /* @__PURE__ */ jsxRuntime.jsx("text", { x: cx, y: cy + 14, textAnchor: "middle", style: { fontSize: "12px", fill: valueColor }, children: label })
98
+ ] }) });
99
+ }
100
+ function calculateMean(series) {
101
+ if (!series.data.length) return 0;
102
+ const sum = series.data.reduce((acc, point) => acc + point.y, 0);
103
+ return sum / series.data.length;
104
+ }
105
+ function calculateLast(series) {
106
+ if (!series.data.length) return 0;
107
+ return series.data[series.data.length - 1].y;
108
+ }
109
+ function calculateMax(series) {
110
+ if (!series.data.length) return 0;
111
+ return Math.max(...series.data.map((point) => point.y));
112
+ }
113
+ function formatTime(ts) {
114
+ const d = typeof ts === "number" ? new Date(ts) : new Date(ts);
115
+ return d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
116
+ }
117
+ var generateColors = (count) => {
118
+ const baseColors = [
119
+ "#A5FF11",
120
+ // Calimero green
121
+ "#fbbf24",
122
+ // Amber
123
+ "#f59e0b",
124
+ // Orange
125
+ "#3b82f6",
126
+ // Blue
127
+ "#8b5cf6",
128
+ // Purple
129
+ "#ec4899",
130
+ // Pink
131
+ "#06b6d4",
132
+ // Cyan
133
+ "#10b981",
134
+ // Emerald
135
+ "#f97316",
136
+ // Orange
137
+ "#ef4444"
138
+ // Red
139
+ ];
140
+ return Array.from({ length: count }, (_, i) => baseColors[i % baseColors.length]);
141
+ };
142
+ function TimeSeries({
143
+ series,
144
+ yLabel,
145
+ colors,
146
+ showLegend = true
147
+ }) {
148
+ const flat = series.flatMap((s) => s.data);
149
+ const domainY = react.useMemo(() => {
150
+ if (!flat.length) return [0, 1];
151
+ const min = Math.min(...flat.map((p) => p.y));
152
+ const max = Math.max(...flat.map((p) => p.y));
153
+ const pad = (max - min) * 0.1 || 1;
154
+ return [Math.max(0, min - pad), max + pad];
155
+ }, [flat]);
156
+ const rows = react.useMemo(() => {
157
+ const length = Math.max(...series.map((s) => s.data.length));
158
+ const ts = series[0]?.data.map((p) => p.t) ?? [];
159
+ return Array.from({ length }, (_, i) => {
160
+ const row = { t: ts[i] ?? i };
161
+ for (const s of series) row[s.name] = s.data[i]?.y ?? null;
162
+ return row;
163
+ });
164
+ }, [series]);
165
+ const finalColors = colors || generateColors(series.length);
166
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsxRuntime.jsx(recharts.ResponsiveContainer, { width: "100%", height: "100%", children: /* @__PURE__ */ jsxRuntime.jsxs(recharts.ComposedChart, { data: rows, margin: { left: 12, right: 12, top: 5, bottom: 5 }, children: [
167
+ /* @__PURE__ */ jsxRuntime.jsx(recharts.XAxis, { dataKey: "t", tickFormatter: formatTime, minTickGap: 32 }),
168
+ /* @__PURE__ */ jsxRuntime.jsx(recharts.YAxis, { domain: domainY, tickFormatter: (v) => `${v}` }),
169
+ /* @__PURE__ */ jsxRuntime.jsx(recharts.Tooltip, { labelFormatter: (l) => `Time: ${formatTime(l)}`, formatter: (v) => [v, yLabel ?? "value"] }),
170
+ showLegend && /* @__PURE__ */ jsxRuntime.jsx(recharts.Legend, {}),
171
+ series.map((s, index) => {
172
+ const color = finalColors[index];
173
+ if (s.fill) {
174
+ return /* @__PURE__ */ jsxRuntime.jsx(
175
+ recharts.Area,
176
+ {
177
+ type: "monotone",
178
+ dataKey: s.name,
179
+ stroke: color,
180
+ fill: color,
181
+ fillOpacity: 0.1,
182
+ strokeWidth: 2
183
+ },
184
+ s.name
185
+ );
186
+ } else {
187
+ return /* @__PURE__ */ jsxRuntime.jsx(
188
+ recharts.Line,
189
+ {
190
+ type: "monotone",
191
+ dataKey: s.name,
192
+ dot: false,
193
+ strokeWidth: 2,
194
+ stroke: color,
195
+ fill: "none"
196
+ },
197
+ s.name
198
+ );
199
+ }
200
+ })
201
+ ] }) }) });
202
+ }
203
+ function VectorTableRaw({
204
+ data,
205
+ colors,
206
+ columns
207
+ }) {
208
+ const defaultColumns = [
209
+ { title: "Mean", function: () => 0 },
210
+ { title: "Last *", function: () => 0 },
211
+ { title: "Max", function: () => 0 }
212
+ ];
213
+ const finalColumns = columns || defaultColumns;
214
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { overflow: "auto" }, children: /* @__PURE__ */ jsxRuntime.jsxs("table", { style: { width: "100%", fontSize: "14px" }, children: [
215
+ /* @__PURE__ */ jsxRuntime.jsx("thead", { children: /* @__PURE__ */ jsxRuntime.jsxs("tr", { style: { textAlign: "left", color: "#94a3b8" }, children: [
216
+ /* @__PURE__ */ jsxRuntime.jsx("th", { style: { padding: "8px 12px 8px 0", fontWeight: "500" }, children: "Name" }),
217
+ finalColumns.map((column, index) => /* @__PURE__ */ jsxRuntime.jsx("th", { style: { padding: "8px 12px", fontWeight: "500", textAlign: "right" }, children: column.title }, index))
218
+ ] }) }),
219
+ /* @__PURE__ */ jsxRuntime.jsx("tbody", { children: data.map((row, index) => /* @__PURE__ */ jsxRuntime.jsxs("tr", { style: { borderTop: "1px solid #374151" }, children: [
220
+ /* @__PURE__ */ jsxRuntime.jsxs("td", { style: { padding: "8px 12px 8px 0", position: "relative" }, children: [
221
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
222
+ position: "absolute",
223
+ left: "0",
224
+ top: "50%",
225
+ transform: "translateY(-50%)",
226
+ width: "4px",
227
+ height: "12px",
228
+ backgroundColor: colors?.[index] || row.color || "#A5FF11",
229
+ borderRadius: "2px"
230
+ } }),
231
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: { marginLeft: "16px", color: "white" }, children: row.name })
232
+ ] }),
233
+ finalColumns.map((column, colIndex) => {
234
+ const key = column.title.toLowerCase().replace(/\s+/g, "_");
235
+ const value = row[key] || 0;
236
+ return /* @__PURE__ */ jsxRuntime.jsx("td", { style: { padding: "8px 12px", textAlign: "right", color: "white", fontVariantNumeric: "tabular-nums" }, children: typeof value === "number" ? value.toFixed(3) : value }, colIndex);
237
+ })
238
+ ] }, index)) })
239
+ ] }) });
240
+ }
241
+ var generateColors2 = (count) => {
242
+ const baseColors = [
243
+ "#A5FF11",
244
+ // Calimero green
245
+ "#fbbf24",
246
+ // Amber
247
+ "#f59e0b",
248
+ // Orange
249
+ "#3b82f6",
250
+ // Blue
251
+ "#8b5cf6",
252
+ // Purple
253
+ "#ec4899",
254
+ // Pink
255
+ "#06b6d4",
256
+ // Cyan
257
+ "#10b981",
258
+ // Emerald
259
+ "#f97316",
260
+ // Orange
261
+ "#ef4444"
262
+ // Red
263
+ ];
264
+ return Array.from({ length: count }, (_, i) => baseColors[i % baseColors.length]);
265
+ };
266
+ function TimeSeriesWithTable({
267
+ timeSeriesData,
268
+ vectorTableData,
269
+ colors,
270
+ timeSeriesYLabel,
271
+ layout = "side-by-side",
272
+ showTimeSeriesLegend = true,
273
+ columns = [
274
+ { title: "Mean", function: calculateMean },
275
+ { title: "Last *", function: calculateLast },
276
+ { title: "Max", function: calculateMax }
277
+ ]
278
+ }) {
279
+ const finalColors = react.useMemo(() => {
280
+ if (colors) {
281
+ return colors;
282
+ }
283
+ return generateColors2(timeSeriesData.length);
284
+ }, [colors, timeSeriesData.length]);
285
+ const finalTableData = react.useMemo(() => {
286
+ if (vectorTableData) {
287
+ return vectorTableData;
288
+ }
289
+ return timeSeriesData.map((series) => {
290
+ const row = { name: series.name };
291
+ columns.forEach((column) => {
292
+ row[column.title.toLowerCase().replace(/\s+/g, "_")] = column.function(series);
293
+ });
294
+ return row;
295
+ });
296
+ }, [vectorTableData, timeSeriesData, columns]);
297
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
298
+ display: "flex",
299
+ flexDirection: layout === "side-by-side" ? "row" : "column",
300
+ gap: "24px",
301
+ minHeight: "240px"
302
+ }, children: [
303
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
304
+ height: layout === "side-by-side" ? "240px" : "240px",
305
+ flex: layout === "side-by-side" ? "1" : "none"
306
+ }, children: /* @__PURE__ */ jsxRuntime.jsx(
307
+ TimeSeries,
308
+ {
309
+ series: timeSeriesData,
310
+ yLabel: timeSeriesYLabel,
311
+ colors: finalColors,
312
+ showLegend: showTimeSeriesLegend
313
+ }
314
+ ) }),
315
+ (layout === "table-below" || layout === "side-by-side") && /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
316
+ flex: layout === "side-by-side" ? "1" : "none"
317
+ }, children: /* @__PURE__ */ jsxRuntime.jsx(
318
+ VectorTableRaw,
319
+ {
320
+ data: finalTableData,
321
+ colors: finalColors,
322
+ columns
323
+ }
324
+ ) })
325
+ ] });
326
+ }
327
+
328
+ exports.Gauge = Gauge;
329
+ exports.TimeSeries = TimeSeries;
330
+ exports.TimeSeriesWithTable = TimeSeriesWithTable;
331
+ exports.VectorTableRaw = VectorTableRaw;
332
+ exports.calculateLast = calculateLast;
333
+ exports.calculateMax = calculateMax;
334
+ exports.calculateMean = calculateMean;
package/dist/index.mjs ADDED
@@ -0,0 +1,326 @@
1
+ import { useMemo } from 'react';
2
+ import { jsx, jsxs } from 'react/jsx-runtime';
3
+ import { ResponsiveContainer, ComposedChart, XAxis, YAxis, Tooltip, Legend, Area, Line } from 'recharts';
4
+
5
+ // src/Gauge.tsx
6
+ var defaultThresholds = { warning: 60, danger: 80 };
7
+ function clamp(n, lo, hi) {
8
+ return Math.max(lo, Math.min(hi, n));
9
+ }
10
+ function polarToCartesian(cx, cy, r, angle) {
11
+ return { x: cx + r * Math.cos(angle), y: cy + r * Math.sin(angle) };
12
+ }
13
+ function arcPath(cx, cy, r, start, end) {
14
+ const s = polarToCartesian(cx, cy, r, start);
15
+ const e = polarToCartesian(cx, cy, r, end);
16
+ const largeArc = Math.abs(end - start) <= Math.PI ? 0 : 1;
17
+ return `M ${s.x} ${s.y} A ${r} ${r} 0 ${largeArc} 1 ${e.x} ${e.y}`;
18
+ }
19
+ function Gauge({
20
+ value,
21
+ min = 0,
22
+ max = 100,
23
+ title,
24
+ label,
25
+ decimals = 1,
26
+ thresholds = defaultThresholds,
27
+ size = "md",
28
+ className = ""
29
+ }) {
30
+ const pct = useMemo(() => {
31
+ const normalizedValue = (value - min) / Math.max(1e-9, max - min) * 100;
32
+ return clamp(normalizedValue, 0, 100);
33
+ }, [value, min, max]);
34
+ const cx = 120, cy = 140;
35
+ const inner = 70;
36
+ const startAngle = 0.95 * Math.PI;
37
+ const endAngle = 2.05 * Math.PI;
38
+ const arcRadius = 100;
39
+ const valueEndAngle = startAngle + pct / 100 * (endAngle - startAngle);
40
+ const valueText = value.toFixed(decimals);
41
+ const getValueColor = (val) => {
42
+ if (val <= thresholds.warning) return "#A5FF11";
43
+ if (val <= thresholds.danger) return "#f59e0b";
44
+ return "#ef4444";
45
+ };
46
+ const valueColor = getValueColor(pct);
47
+ return /* @__PURE__ */ jsx("div", { className: "w-full flex flex-col items-center", children: /* @__PURE__ */ jsxs("svg", { viewBox: "0 0 240 160", className: "w-full", children: [
48
+ /* @__PURE__ */ jsx("path", { d: arcPath(cx, cy, arcRadius, startAngle, endAngle), stroke: "#6B7280", strokeOpacity: 0.3, strokeWidth: 18, fill: "none", strokeLinecap: "butt" }),
49
+ /* @__PURE__ */ jsx(
50
+ "path",
51
+ {
52
+ d: arcPath(cx, cy, arcRadius, startAngle, valueEndAngle),
53
+ stroke: valueColor,
54
+ strokeWidth: 18,
55
+ fill: "none",
56
+ strokeLinecap: "butt"
57
+ }
58
+ ),
59
+ /* @__PURE__ */ jsx(
60
+ "path",
61
+ {
62
+ d: arcPath(cx, cy, arcRadius + 15, startAngle, startAngle + thresholds.warning / 100 * (endAngle - startAngle)),
63
+ stroke: "#A5FF11",
64
+ strokeOpacity: 0.3,
65
+ strokeWidth: 4,
66
+ fill: "none",
67
+ strokeLinecap: "butt"
68
+ }
69
+ ),
70
+ /* @__PURE__ */ jsx(
71
+ "path",
72
+ {
73
+ d: arcPath(cx, cy, arcRadius + 15, startAngle + thresholds.warning / 100 * (endAngle - startAngle), startAngle + thresholds.danger / 100 * (endAngle - startAngle)),
74
+ stroke: "#f59e0b",
75
+ strokeOpacity: 0.3,
76
+ strokeWidth: 4,
77
+ fill: "none",
78
+ strokeLinecap: "butt"
79
+ }
80
+ ),
81
+ /* @__PURE__ */ jsx(
82
+ "path",
83
+ {
84
+ d: arcPath(cx, cy, arcRadius + 15, startAngle + thresholds.danger / 100 * (endAngle - startAngle), endAngle),
85
+ stroke: "#ef4444",
86
+ strokeOpacity: 0.3,
87
+ strokeWidth: 4,
88
+ fill: "none",
89
+ strokeLinecap: "butt"
90
+ }
91
+ ),
92
+ /* @__PURE__ */ jsx("circle", { cx, cy, r: inner, fill: "#1F2A15" }),
93
+ " ",
94
+ /* @__PURE__ */ jsx("text", { x: cx, y: cy - 8, textAnchor: "middle", dominantBaseline: "middle", style: { fontWeight: 600, fontSize: "28px", fill: valueColor }, children: valueText }),
95
+ label && /* @__PURE__ */ jsx("text", { x: cx, y: cy + 14, textAnchor: "middle", style: { fontSize: "12px", fill: valueColor }, children: label })
96
+ ] }) });
97
+ }
98
+ function calculateMean(series) {
99
+ if (!series.data.length) return 0;
100
+ const sum = series.data.reduce((acc, point) => acc + point.y, 0);
101
+ return sum / series.data.length;
102
+ }
103
+ function calculateLast(series) {
104
+ if (!series.data.length) return 0;
105
+ return series.data[series.data.length - 1].y;
106
+ }
107
+ function calculateMax(series) {
108
+ if (!series.data.length) return 0;
109
+ return Math.max(...series.data.map((point) => point.y));
110
+ }
111
+ function formatTime(ts) {
112
+ const d = typeof ts === "number" ? new Date(ts) : new Date(ts);
113
+ return d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
114
+ }
115
+ var generateColors = (count) => {
116
+ const baseColors = [
117
+ "#A5FF11",
118
+ // Calimero green
119
+ "#fbbf24",
120
+ // Amber
121
+ "#f59e0b",
122
+ // Orange
123
+ "#3b82f6",
124
+ // Blue
125
+ "#8b5cf6",
126
+ // Purple
127
+ "#ec4899",
128
+ // Pink
129
+ "#06b6d4",
130
+ // Cyan
131
+ "#10b981",
132
+ // Emerald
133
+ "#f97316",
134
+ // Orange
135
+ "#ef4444"
136
+ // Red
137
+ ];
138
+ return Array.from({ length: count }, (_, i) => baseColors[i % baseColors.length]);
139
+ };
140
+ function TimeSeries({
141
+ series,
142
+ yLabel,
143
+ colors,
144
+ showLegend = true
145
+ }) {
146
+ const flat = series.flatMap((s) => s.data);
147
+ const domainY = useMemo(() => {
148
+ if (!flat.length) return [0, 1];
149
+ const min = Math.min(...flat.map((p) => p.y));
150
+ const max = Math.max(...flat.map((p) => p.y));
151
+ const pad = (max - min) * 0.1 || 1;
152
+ return [Math.max(0, min - pad), max + pad];
153
+ }, [flat]);
154
+ const rows = useMemo(() => {
155
+ const length = Math.max(...series.map((s) => s.data.length));
156
+ const ts = series[0]?.data.map((p) => p.t) ?? [];
157
+ return Array.from({ length }, (_, i) => {
158
+ const row = { t: ts[i] ?? i };
159
+ for (const s of series) row[s.name] = s.data[i]?.y ?? null;
160
+ return row;
161
+ });
162
+ }, [series]);
163
+ const finalColors = colors || generateColors(series.length);
164
+ return /* @__PURE__ */ jsx("div", { style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsx(ResponsiveContainer, { width: "100%", height: "100%", children: /* @__PURE__ */ jsxs(ComposedChart, { data: rows, margin: { left: 12, right: 12, top: 5, bottom: 5 }, children: [
165
+ /* @__PURE__ */ jsx(XAxis, { dataKey: "t", tickFormatter: formatTime, minTickGap: 32 }),
166
+ /* @__PURE__ */ jsx(YAxis, { domain: domainY, tickFormatter: (v) => `${v}` }),
167
+ /* @__PURE__ */ jsx(Tooltip, { labelFormatter: (l) => `Time: ${formatTime(l)}`, formatter: (v) => [v, yLabel ?? "value"] }),
168
+ showLegend && /* @__PURE__ */ jsx(Legend, {}),
169
+ series.map((s, index) => {
170
+ const color = finalColors[index];
171
+ if (s.fill) {
172
+ return /* @__PURE__ */ jsx(
173
+ Area,
174
+ {
175
+ type: "monotone",
176
+ dataKey: s.name,
177
+ stroke: color,
178
+ fill: color,
179
+ fillOpacity: 0.1,
180
+ strokeWidth: 2
181
+ },
182
+ s.name
183
+ );
184
+ } else {
185
+ return /* @__PURE__ */ jsx(
186
+ Line,
187
+ {
188
+ type: "monotone",
189
+ dataKey: s.name,
190
+ dot: false,
191
+ strokeWidth: 2,
192
+ stroke: color,
193
+ fill: "none"
194
+ },
195
+ s.name
196
+ );
197
+ }
198
+ })
199
+ ] }) }) });
200
+ }
201
+ function VectorTableRaw({
202
+ data,
203
+ colors,
204
+ columns
205
+ }) {
206
+ const defaultColumns = [
207
+ { title: "Mean", function: () => 0 },
208
+ { title: "Last *", function: () => 0 },
209
+ { title: "Max", function: () => 0 }
210
+ ];
211
+ const finalColumns = columns || defaultColumns;
212
+ return /* @__PURE__ */ jsx("div", { style: { overflow: "auto" }, children: /* @__PURE__ */ jsxs("table", { style: { width: "100%", fontSize: "14px" }, children: [
213
+ /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { style: { textAlign: "left", color: "#94a3b8" }, children: [
214
+ /* @__PURE__ */ jsx("th", { style: { padding: "8px 12px 8px 0", fontWeight: "500" }, children: "Name" }),
215
+ finalColumns.map((column, index) => /* @__PURE__ */ jsx("th", { style: { padding: "8px 12px", fontWeight: "500", textAlign: "right" }, children: column.title }, index))
216
+ ] }) }),
217
+ /* @__PURE__ */ jsx("tbody", { children: data.map((row, index) => /* @__PURE__ */ jsxs("tr", { style: { borderTop: "1px solid #374151" }, children: [
218
+ /* @__PURE__ */ jsxs("td", { style: { padding: "8px 12px 8px 0", position: "relative" }, children: [
219
+ /* @__PURE__ */ jsx("div", { style: {
220
+ position: "absolute",
221
+ left: "0",
222
+ top: "50%",
223
+ transform: "translateY(-50%)",
224
+ width: "4px",
225
+ height: "12px",
226
+ backgroundColor: colors?.[index] || row.color || "#A5FF11",
227
+ borderRadius: "2px"
228
+ } }),
229
+ /* @__PURE__ */ jsx("span", { style: { marginLeft: "16px", color: "white" }, children: row.name })
230
+ ] }),
231
+ finalColumns.map((column, colIndex) => {
232
+ const key = column.title.toLowerCase().replace(/\s+/g, "_");
233
+ const value = row[key] || 0;
234
+ return /* @__PURE__ */ jsx("td", { style: { padding: "8px 12px", textAlign: "right", color: "white", fontVariantNumeric: "tabular-nums" }, children: typeof value === "number" ? value.toFixed(3) : value }, colIndex);
235
+ })
236
+ ] }, index)) })
237
+ ] }) });
238
+ }
239
+ var generateColors2 = (count) => {
240
+ const baseColors = [
241
+ "#A5FF11",
242
+ // Calimero green
243
+ "#fbbf24",
244
+ // Amber
245
+ "#f59e0b",
246
+ // Orange
247
+ "#3b82f6",
248
+ // Blue
249
+ "#8b5cf6",
250
+ // Purple
251
+ "#ec4899",
252
+ // Pink
253
+ "#06b6d4",
254
+ // Cyan
255
+ "#10b981",
256
+ // Emerald
257
+ "#f97316",
258
+ // Orange
259
+ "#ef4444"
260
+ // Red
261
+ ];
262
+ return Array.from({ length: count }, (_, i) => baseColors[i % baseColors.length]);
263
+ };
264
+ function TimeSeriesWithTable({
265
+ timeSeriesData,
266
+ vectorTableData,
267
+ colors,
268
+ timeSeriesYLabel,
269
+ layout = "side-by-side",
270
+ showTimeSeriesLegend = true,
271
+ columns = [
272
+ { title: "Mean", function: calculateMean },
273
+ { title: "Last *", function: calculateLast },
274
+ { title: "Max", function: calculateMax }
275
+ ]
276
+ }) {
277
+ const finalColors = useMemo(() => {
278
+ if (colors) {
279
+ return colors;
280
+ }
281
+ return generateColors2(timeSeriesData.length);
282
+ }, [colors, timeSeriesData.length]);
283
+ const finalTableData = useMemo(() => {
284
+ if (vectorTableData) {
285
+ return vectorTableData;
286
+ }
287
+ return timeSeriesData.map((series) => {
288
+ const row = { name: series.name };
289
+ columns.forEach((column) => {
290
+ row[column.title.toLowerCase().replace(/\s+/g, "_")] = column.function(series);
291
+ });
292
+ return row;
293
+ });
294
+ }, [vectorTableData, timeSeriesData, columns]);
295
+ return /* @__PURE__ */ jsxs("div", { style: {
296
+ display: "flex",
297
+ flexDirection: layout === "side-by-side" ? "row" : "column",
298
+ gap: "24px",
299
+ minHeight: "240px"
300
+ }, children: [
301
+ /* @__PURE__ */ jsx("div", { style: {
302
+ height: layout === "side-by-side" ? "240px" : "240px",
303
+ flex: layout === "side-by-side" ? "1" : "none"
304
+ }, children: /* @__PURE__ */ jsx(
305
+ TimeSeries,
306
+ {
307
+ series: timeSeriesData,
308
+ yLabel: timeSeriesYLabel,
309
+ colors: finalColors,
310
+ showLegend: showTimeSeriesLegend
311
+ }
312
+ ) }),
313
+ (layout === "table-below" || layout === "side-by-side") && /* @__PURE__ */ jsx("div", { style: {
314
+ flex: layout === "side-by-side" ? "1" : "none"
315
+ }, children: /* @__PURE__ */ jsx(
316
+ VectorTableRaw,
317
+ {
318
+ data: finalTableData,
319
+ colors: finalColors,
320
+ columns
321
+ }
322
+ ) })
323
+ ] });
324
+ }
325
+
326
+ export { Gauge, TimeSeries, TimeSeriesWithTable, VectorTableRaw, calculateLast, calculateMax, calculateMean };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@calimero-network/mero-charts",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "0.0.1",
7
+ "description": "Chart components for Calimero design system",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "dependencies": {
21
+ "react": "^19.0.0",
22
+ "recharts": "^2.8.0",
23
+ "@calimero-network/mero-ui": "0.0.1"
24
+ },
25
+ "devDependencies": {
26
+ "@types/react": "^19.0.0",
27
+ "tsup": "^8.0.0",
28
+ "typescript": "^5.0.0"
29
+ },
30
+ "peerDependencies": {
31
+ "react": "^19.0.0"
32
+ },
33
+ "scripts": {
34
+ "build": "tsup",
35
+ "dev": "tsup --watch",
36
+ "clean": "rm -rf dist"
37
+ }
38
+ }