ui-svelte 0.1.0

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.
Files changed (238) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +118 -0
  3. package/dist/charts/ArcChart.svelte +320 -0
  4. package/dist/charts/ArcChart.svelte.d.ts +26 -0
  5. package/dist/charts/AreaChart.svelte +495 -0
  6. package/dist/charts/AreaChart.svelte.d.ts +32 -0
  7. package/dist/charts/BarChart.svelte +504 -0
  8. package/dist/charts/BarChart.svelte.d.ts +38 -0
  9. package/dist/charts/Candlestick.svelte +527 -0
  10. package/dist/charts/Candlestick.svelte.d.ts +38 -0
  11. package/dist/charts/LineChart.svelte +365 -0
  12. package/dist/charts/LineChart.svelte.d.ts +36 -0
  13. package/dist/charts/PieChart.svelte +311 -0
  14. package/dist/charts/PieChart.svelte.d.ts +28 -0
  15. package/dist/charts/css/arc-chart.css +237 -0
  16. package/dist/charts/css/area-chart.css +289 -0
  17. package/dist/charts/css/bar-chart.css +167 -0
  18. package/dist/charts/css/candlestick.css +197 -0
  19. package/dist/charts/css/line-chart.css +202 -0
  20. package/dist/charts/css/pie-chart.css +199 -0
  21. package/dist/control/Audio.svelte +212 -0
  22. package/dist/control/Audio.svelte.d.ts +8 -0
  23. package/dist/control/Button.svelte +116 -0
  24. package/dist/control/Button.svelte.d.ts +22 -0
  25. package/dist/control/IconButton.svelte +104 -0
  26. package/dist/control/IconButton.svelte.d.ts +17 -0
  27. package/dist/control/Record.svelte +430 -0
  28. package/dist/control/Record.svelte.d.ts +11 -0
  29. package/dist/control/ToggleTheme.svelte +21 -0
  30. package/dist/control/ToggleTheme.svelte.d.ts +8 -0
  31. package/dist/control/Video.svelte +222 -0
  32. package/dist/control/Video.svelte.d.ts +10 -0
  33. package/dist/control/css/btn.css +206 -0
  34. package/dist/control/css/media.css +78 -0
  35. package/dist/control/css/video.css +58 -0
  36. package/dist/css/animations.css +27 -0
  37. package/dist/css/base.css +192 -0
  38. package/dist/css/utilities.css +136 -0
  39. package/dist/display/Accordion.svelte +98 -0
  40. package/dist/display/Accordion.svelte.d.ts +20 -0
  41. package/dist/display/Alert.svelte +65 -0
  42. package/dist/display/Alert.svelte.d.ts +15 -0
  43. package/dist/display/Avatar.svelte +80 -0
  44. package/dist/display/Avatar.svelte.d.ts +13 -0
  45. package/dist/display/Badge.svelte +46 -0
  46. package/dist/display/Badge.svelte.d.ts +11 -0
  47. package/dist/display/Card.svelte +94 -0
  48. package/dist/display/Card.svelte.d.ts +21 -0
  49. package/dist/display/Carousel.svelte +359 -0
  50. package/dist/display/Carousel.svelte.d.ts +25 -0
  51. package/dist/display/ChatBox.svelte +249 -0
  52. package/dist/display/ChatBox.svelte.d.ts +18 -0
  53. package/dist/display/Chip.svelte +67 -0
  54. package/dist/display/Chip.svelte.d.ts +17 -0
  55. package/dist/display/Code.svelte +56 -0
  56. package/dist/display/Code.svelte.d.ts +9 -0
  57. package/dist/display/Collapsible.svelte +71 -0
  58. package/dist/display/Collapsible.svelte.d.ts +15 -0
  59. package/dist/display/Divider.svelte +32 -0
  60. package/dist/display/Divider.svelte.d.ts +10 -0
  61. package/dist/display/Empty.svelte +462 -0
  62. package/dist/display/Empty.svelte.d.ts +11 -0
  63. package/dist/display/Icon.svelte +20 -0
  64. package/dist/display/Icon.svelte.d.ts +11 -0
  65. package/dist/display/Item.svelte +119 -0
  66. package/dist/display/Item.svelte.d.ts +24 -0
  67. package/dist/display/Loading.svelte +8 -0
  68. package/dist/display/Loading.svelte.d.ts +26 -0
  69. package/dist/display/Marquee.svelte +164 -0
  70. package/dist/display/Marquee.svelte.d.ts +21 -0
  71. package/dist/display/Section.svelte +63 -0
  72. package/dist/display/Section.svelte.d.ts +16 -0
  73. package/dist/display/Table.svelte +407 -0
  74. package/dist/display/Table.svelte.d.ts +32 -0
  75. package/dist/display/TypeWriter.svelte +23 -0
  76. package/dist/display/TypeWriter.svelte.d.ts +11 -0
  77. package/dist/display/User.svelte +0 -0
  78. package/dist/display/User.svelte.d.ts +26 -0
  79. package/dist/display/css/accordion.css +98 -0
  80. package/dist/display/css/alert.css +51 -0
  81. package/dist/display/css/avatar.css +158 -0
  82. package/dist/display/css/badge.css +47 -0
  83. package/dist/display/css/card.css +231 -0
  84. package/dist/display/css/carousel.css +156 -0
  85. package/dist/display/css/chat-box.css +188 -0
  86. package/dist/display/css/chip.css +91 -0
  87. package/dist/display/css/code.css +19 -0
  88. package/dist/display/css/collapsible.css +86 -0
  89. package/dist/display/css/divider.css +54 -0
  90. package/dist/display/css/empty.css +8 -0
  91. package/dist/display/css/item.css +149 -0
  92. package/dist/display/css/listbox.css +24 -0
  93. package/dist/display/css/marquee.css +138 -0
  94. package/dist/display/css/section.css +85 -0
  95. package/dist/display/css/table.css +361 -0
  96. package/dist/form/Checkbox.svelte +45 -0
  97. package/dist/form/Checkbox.svelte.d.ts +13 -0
  98. package/dist/form/ComboBox.svelte +448 -0
  99. package/dist/form/ComboBox.svelte.d.ts +29 -0
  100. package/dist/form/CsvField.svelte +389 -0
  101. package/dist/form/CsvField.svelte.d.ts +21 -0
  102. package/dist/form/DateField.svelte +292 -0
  103. package/dist/form/DateField.svelte.d.ts +18 -0
  104. package/dist/form/Dropzone.svelte +196 -0
  105. package/dist/form/Dropzone.svelte.d.ts +30 -0
  106. package/dist/form/ImageCropper.svelte +254 -0
  107. package/dist/form/ImageCropper.svelte.d.ts +14 -0
  108. package/dist/form/PasswordField.svelte +170 -0
  109. package/dist/form/PasswordField.svelte.d.ts +28 -0
  110. package/dist/form/PhoneField.svelte +485 -0
  111. package/dist/form/PhoneField.svelte.d.ts +25 -0
  112. package/dist/form/PinField.svelte +139 -0
  113. package/dist/form/PinField.svelte.d.ts +17 -0
  114. package/dist/form/RadioGroup.svelte +70 -0
  115. package/dist/form/RadioGroup.svelte.d.ts +19 -0
  116. package/dist/form/Select.svelte +350 -0
  117. package/dist/form/Select.svelte.d.ts +26 -0
  118. package/dist/form/Slider.svelte +60 -0
  119. package/dist/form/Slider.svelte.d.ts +15 -0
  120. package/dist/form/TextField.svelte +154 -0
  121. package/dist/form/TextField.svelte.d.ts +31 -0
  122. package/dist/form/Textarea.svelte +137 -0
  123. package/dist/form/Textarea.svelte.d.ts +27 -0
  124. package/dist/form/Toggle.svelte +45 -0
  125. package/dist/form/Toggle.svelte.d.ts +13 -0
  126. package/dist/form/css/checkbox.css +46 -0
  127. package/dist/form/css/combo-box.css +69 -0
  128. package/dist/form/css/control.css +177 -0
  129. package/dist/form/css/csv-field.css +0 -0
  130. package/dist/form/css/date.css +56 -0
  131. package/dist/form/css/dropzone.css +133 -0
  132. package/dist/form/css/field.css +17 -0
  133. package/dist/form/css/image-cropper.css +155 -0
  134. package/dist/form/css/password.css +35 -0
  135. package/dist/form/css/radio-group.css +57 -0
  136. package/dist/form/css/select.css +18 -0
  137. package/dist/form/css/slider.css +80 -0
  138. package/dist/form/css/textarea.css +130 -0
  139. package/dist/form/css/toggle.css +27 -0
  140. package/dist/form/js/countries.d.ts +13 -0
  141. package/dist/form/js/countries.js +307 -0
  142. package/dist/form/js/phone-examples.d.ts +248 -0
  143. package/dist/form/js/phone-examples.js +247 -0
  144. package/dist/hooks/use-auth.svelte.d.ts +11 -0
  145. package/dist/hooks/use-auth.svelte.js +59 -0
  146. package/dist/hooks/use-chat.svelte.d.ts +40 -0
  147. package/dist/hooks/use-chat.svelte.js +265 -0
  148. package/dist/hooks/use-clipboard.svelte.d.ts +9 -0
  149. package/dist/hooks/use-clipboard.svelte.js +52 -0
  150. package/dist/hooks/use-fetch.svelte.d.ts +11 -0
  151. package/dist/hooks/use-fetch.svelte.js +38 -0
  152. package/dist/hooks/use-form.svelte.d.ts +31 -0
  153. package/dist/hooks/use-form.svelte.js +110 -0
  154. package/dist/hooks/use-localstorage.svelte.d.ts +3 -0
  155. package/dist/hooks/use-localstorage.svelte.js +26 -0
  156. package/dist/hooks/use-scroll.svelte.d.ts +6 -0
  157. package/dist/hooks/use-scroll.svelte.js +34 -0
  158. package/dist/hooks/use-search.svelte.d.ts +49 -0
  159. package/dist/hooks/use-search.svelte.js +229 -0
  160. package/dist/hooks/use-table.svelte.d.ts +85 -0
  161. package/dist/hooks/use-table.svelte.js +362 -0
  162. package/dist/hooks/use-websocket.svelte.d.ts +18 -0
  163. package/dist/hooks/use-websocket.svelte.js +79 -0
  164. package/dist/icons/index.d.ts +132 -0
  165. package/dist/icons/index.js +132 -0
  166. package/dist/index.css +115 -0
  167. package/dist/index.d.ts +76 -0
  168. package/dist/index.js +76 -0
  169. package/dist/layout/AppBar.svelte +94 -0
  170. package/dist/layout/AppBar.svelte.d.ts +17 -0
  171. package/dist/layout/Footer.svelte +94 -0
  172. package/dist/layout/Footer.svelte.d.ts +17 -0
  173. package/dist/layout/FooterLinks.svelte +28 -0
  174. package/dist/layout/FooterLinks.svelte.d.ts +11 -0
  175. package/dist/layout/Provider.svelte +52 -0
  176. package/dist/layout/Provider.svelte.d.ts +10 -0
  177. package/dist/layout/Scaffold.svelte +46 -0
  178. package/dist/layout/Scaffold.svelte.d.ts +15 -0
  179. package/dist/layout/Sidebar.svelte +40 -0
  180. package/dist/layout/Sidebar.svelte.d.ts +13 -0
  181. package/dist/layout/css/app-bar.css +35 -0
  182. package/dist/layout/css/bottom-bar.css +12 -0
  183. package/dist/layout/css/footer-links.css +17 -0
  184. package/dist/layout/css/footer.css +35 -0
  185. package/dist/layout/css/scaffold.css +15 -0
  186. package/dist/layout/css/sidebar.css +17 -0
  187. package/dist/navigation/BottomNav.svelte +0 -0
  188. package/dist/navigation/BottomNav.svelte.d.ts +26 -0
  189. package/dist/navigation/NavMenu.svelte +254 -0
  190. package/dist/navigation/SideNav.svelte +249 -0
  191. package/dist/navigation/Tabs.svelte +79 -0
  192. package/dist/navigation/Tabs.svelte.d.ts +19 -0
  193. package/dist/navigation/css/bottom-nav.css +0 -0
  194. package/dist/navigation/css/nav-menu.css +168 -0
  195. package/dist/navigation/css/side-nav.css +244 -0
  196. package/dist/navigation/css/tabs.css +118 -0
  197. package/dist/overlay/AlertDialog.svelte +0 -0
  198. package/dist/overlay/AlertDialog.svelte.d.ts +26 -0
  199. package/dist/overlay/Command.svelte +0 -0
  200. package/dist/overlay/Command.svelte.d.ts +26 -0
  201. package/dist/overlay/Drawer.svelte +129 -0
  202. package/dist/overlay/Drawer.svelte.d.ts +20 -0
  203. package/dist/overlay/Dropdown.svelte +140 -0
  204. package/dist/overlay/Modal.svelte +102 -0
  205. package/dist/overlay/Modal.svelte.d.ts +19 -0
  206. package/dist/overlay/PopoverStack.svelte +0 -0
  207. package/dist/overlay/PopoverStack.svelte.d.ts +26 -0
  208. package/dist/overlay/Toast.svelte +83 -0
  209. package/dist/overlay/Toast.svelte.d.ts +9 -0
  210. package/dist/overlay/Tooltip.svelte +140 -0
  211. package/dist/overlay/Tooltip.svelte.d.ts +12 -0
  212. package/dist/overlay/css/drawer.css +75 -0
  213. package/dist/overlay/css/dropdown.css +24 -0
  214. package/dist/overlay/css/hovercard.css +11 -0
  215. package/dist/overlay/css/modal.css +51 -0
  216. package/dist/overlay/css/toast.css +80 -0
  217. package/dist/overlay/css/tooltip.css +89 -0
  218. package/dist/stores/i18n.svelte.d.ts +16 -0
  219. package/dist/stores/i18n.svelte.js +137 -0
  220. package/dist/stores/theme.svelte.d.ts +5 -0
  221. package/dist/stores/theme.svelte.js +55 -0
  222. package/dist/stores/toast.svelte.d.ts +19 -0
  223. package/dist/stores/toast.svelte.js +38 -0
  224. package/dist/types.d.ts +75 -0
  225. package/dist/types.js +1 -0
  226. package/dist/utils/charts.d.ts +27 -0
  227. package/dist/utils/charts.js +140 -0
  228. package/dist/utils/class-names.d.ts +1 -0
  229. package/dist/utils/class-names.js +3 -0
  230. package/dist/utils/click-outside.d.ts +3 -0
  231. package/dist/utils/click-outside.js +9 -0
  232. package/dist/utils/popover.d.ts +3 -0
  233. package/dist/utils/popover.js +17 -0
  234. package/dist/utils/ulid.d.ts +1 -0
  235. package/dist/utils/ulid.js +22 -0
  236. package/dist/utils/validate-schema.d.ts +2 -0
  237. package/dist/utils/validate-schema.js +97 -0
  238. package/package.json +69 -0
@@ -0,0 +1,365 @@
1
+ <script lang="ts">
2
+ import { cn } from '../utils/class-names.js';
3
+ import { onMount } from 'svelte';
4
+
5
+ type TooltipData = {
6
+ x: number;
7
+ y: number;
8
+ seriesName: string;
9
+ color: Color;
10
+ };
11
+
12
+ type LinearScale = {
13
+ (value: number): number;
14
+ invert: (pixel: number) => number;
15
+ domain: [number, number];
16
+ range: [number, number];
17
+ };
18
+
19
+ type Color = 'primary' | 'secondary' | 'success' | 'info' | 'warning' | 'danger' | 'muted';
20
+
21
+ type DataPoint = {
22
+ x: number;
23
+ y: number;
24
+ };
25
+
26
+ type Series = {
27
+ name: string;
28
+ data: DataPoint[];
29
+ color?: Color;
30
+ };
31
+
32
+ type Margin = {
33
+ top: number;
34
+ right: number;
35
+ bottom: number;
36
+ left: number;
37
+ };
38
+
39
+ type Props = {
40
+ data?: DataPoint[];
41
+ series?: Series[];
42
+ margin?: Margin;
43
+ color?: Color;
44
+ colors?: Color[];
45
+ showPoints?: boolean;
46
+ showGrid?: boolean;
47
+ showLegend?: boolean;
48
+ curve?: 'linear' | 'smooth';
49
+ strokeWidth?: number;
50
+ pointRadius?: number;
51
+ loading?: boolean;
52
+ empty?: boolean;
53
+ emptyText?: string;
54
+ class?: string;
55
+ };
56
+
57
+ let {
58
+ data = [],
59
+ series = [],
60
+ margin = { top: 20, right: 20, bottom: 40, left: 50 },
61
+ color = 'primary' as Color,
62
+ colors = ['primary', 'secondary', 'success', 'info', 'warning', 'danger'] as Color[],
63
+ showPoints = true,
64
+ showGrid = true,
65
+ showLegend = false,
66
+ curve = 'linear' as 'linear' | 'smooth',
67
+ strokeWidth = 2,
68
+ pointRadius = 3,
69
+ loading = false,
70
+ empty = false,
71
+ emptyText = 'No data available',
72
+ class: className
73
+ }: Props = $props();
74
+
75
+ let containerEl: HTMLDivElement | undefined = $state();
76
+ let containerSize = $state({ width: 0, height: 0 });
77
+
78
+ let width = $derived(containerSize.width || 600);
79
+ let height = $derived(containerSize.height || 400);
80
+
81
+ function createLinearScale(domain: [number, number], range: [number, number]): LinearScale {
82
+ const [d0, d1] = domain;
83
+ const [r0, r1] = range;
84
+
85
+ const scale = (value: number): number => {
86
+ return r0 + ((value - d0) / (d1 - d0)) * (r1 - r0);
87
+ };
88
+
89
+ scale.invert = (pixel: number): number => {
90
+ return d0 + ((pixel - r0) / (r1 - r0)) * (d1 - d0);
91
+ };
92
+
93
+ scale.domain = domain;
94
+ scale.range = range;
95
+
96
+ return scale;
97
+ }
98
+
99
+ function formatNumber(value: number): string {
100
+ if (value === 0) return '0';
101
+ const abs = Math.abs(value);
102
+ if (abs >= 1e9) return `${(value / 1e9).toFixed(1)}B`;
103
+ if (abs >= 1e6) return `${(value / 1e6).toFixed(1)}M`;
104
+ if (abs >= 1e3) return `${(value / 1e3).toFixed(1)}K`;
105
+ if (abs < 1) return value.toFixed(2);
106
+ return value.toFixed(0);
107
+ }
108
+
109
+ let innerWidth = $derived(width - margin.left - margin.right);
110
+ let innerHeight = $derived(height - margin.top - margin.bottom);
111
+
112
+ let normalizedSeries = $derived.by((): Series[] => {
113
+ if (series.length > 0) {
114
+ return series.map((s, i) => ({
115
+ name: s.name,
116
+ data: s.data,
117
+ color: s.color || colors[i % colors.length]
118
+ }));
119
+ }
120
+
121
+ if (data.length > 0) {
122
+ return [
123
+ {
124
+ name: 'Data',
125
+ data: data,
126
+ color: color
127
+ }
128
+ ];
129
+ }
130
+
131
+ return [];
132
+ });
133
+
134
+ let xDomain = $derived.by((): [number, number] => {
135
+ if (normalizedSeries.length === 0) return [0, 1];
136
+ const allValues = normalizedSeries.flatMap((s) => s.data);
137
+ return [Math.min(...allValues.map((d) => d.x)), Math.max(...allValues.map((d) => d.x))];
138
+ });
139
+
140
+ let yDomain = $derived.by((): [number, number] => {
141
+ if (normalizedSeries.length === 0) return [0, 1];
142
+ const allValues = normalizedSeries.flatMap((s) => s.data);
143
+ const values = allValues.map((d) => d.y);
144
+ const min = Math.min(...values);
145
+ const max = Math.max(...values);
146
+ const padding = (max - min) * 0.1;
147
+ return [min - padding, max + padding];
148
+ });
149
+
150
+ function createPath(points: DataPoint[], smooth: boolean = false): string {
151
+ if (points.length === 0) return '';
152
+
153
+ const xScale = createLinearScale(xDomain, [0, innerWidth]);
154
+ const yScale = createLinearScale(yDomain, [innerHeight, 0]);
155
+
156
+ let path = `M ${xScale(points[0].x)} ${yScale(points[0].y)}`;
157
+
158
+ if (smooth && points.length > 1) {
159
+ for (let i = 1; i < points.length; i++) {
160
+ const x0 = xScale(points[i - 1].x);
161
+ const y0 = yScale(points[i - 1].y);
162
+ const x1 = xScale(points[i].x);
163
+ const y1 = yScale(points[i].y);
164
+ const cpx = (x0 + x1) / 2;
165
+ path += ` C ${cpx} ${y0}, ${cpx} ${y1}, ${x1} ${y1}`;
166
+ }
167
+ } else {
168
+ for (let i = 1; i < points.length; i++) {
169
+ path += ` L ${xScale(points[i].x)} ${yScale(points[i].y)}`;
170
+ }
171
+ }
172
+
173
+ return path;
174
+ }
175
+
176
+ function createGridLines(): Array<{ x: number; y: number; value: number }> {
177
+ const yScale = createLinearScale(yDomain, [innerHeight, 0]);
178
+ const yTicks = 5;
179
+
180
+ return Array.from({ length: yTicks + 1 }, (_, i) => {
181
+ const value = yDomain[0] + (yDomain[1] - yDomain[0]) * (i / yTicks);
182
+ return { x: innerWidth, y: yScale(value), value };
183
+ });
184
+ }
185
+
186
+ let tooltipData = $state<TooltipData | null>(null);
187
+ let tooltipPosition = $state<{ x: number; y: number }>({ x: 0, y: 0 });
188
+ let isTooltipActive = $state(false);
189
+
190
+ function handlePointHover(
191
+ point: DataPoint,
192
+ seriesName: string,
193
+ event: MouseEvent,
194
+ pointColor: Color
195
+ ): void {
196
+ const target = event.target as SVGCircleElement;
197
+ const rect = target.getBoundingClientRect();
198
+
199
+ tooltipData = { x: point.x, y: point.y, seriesName, color: pointColor };
200
+ tooltipPosition = {
201
+ x: rect.right + 8,
202
+ y: rect.top + rect.height / 2
203
+ };
204
+ isTooltipActive = true;
205
+ }
206
+
207
+ function handlePointLeave(): void {
208
+ isTooltipActive = false;
209
+ setTimeout(() => {
210
+ if (!isTooltipActive) {
211
+ tooltipData = null;
212
+ }
213
+ }, 100);
214
+ }
215
+
216
+ onMount(() => {
217
+ const updateSize = () => {
218
+ if (containerEl) {
219
+ const rect = containerEl.getBoundingClientRect();
220
+ containerSize = { width: rect.width, height: rect.height };
221
+ }
222
+ };
223
+
224
+ updateSize();
225
+ const resizeObserver = new ResizeObserver(updateSize);
226
+ if (containerEl) {
227
+ resizeObserver.observe(containerEl);
228
+ }
229
+
230
+ return () => {
231
+ resizeObserver.disconnect();
232
+ };
233
+ });
234
+ </script>
235
+
236
+ <div bind:this={containerEl} class={cn('line-chart-container', className)}>
237
+ {#if loading}
238
+ <div class="line-chart-loading">
239
+ <svg class="line-chart-loading-spinner" viewBox="0 0 24 24">
240
+ <circle
241
+ cx="12"
242
+ cy="12"
243
+ r="10"
244
+ stroke="currentColor"
245
+ stroke-width="4"
246
+ fill="none"
247
+ opacity="0.25"
248
+ />
249
+ <path
250
+ fill="currentColor"
251
+ d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
252
+ />
253
+ </svg>
254
+ </div>
255
+ {:else if empty || (data.length === 0 && series.length === 0)}
256
+ <div class="line-chart-empty">
257
+ <svg class="line-chart-empty-icon" fill="none" viewBox="0 0 24 24" stroke="currentColor">
258
+ <path
259
+ stroke-linecap="round"
260
+ stroke-linejoin="round"
261
+ stroke-width="2"
262
+ d="M7 12l3-3 3 3 4-4M8 21l4-4 4 4M3 4h18M4 4h16v12a1 1 0 01-1 1H5a1 1 0 01-1-1V4z"
263
+ />
264
+ </svg>
265
+ <span>{emptyText}</span>
266
+ </div>
267
+ {:else}
268
+ <div class="line-chart">
269
+ <svg class="line-chart-svg" {width} {height}>
270
+ <g transform="translate({margin.left}, {margin.top})">
271
+ {#if normalizedSeries.length > 0}
272
+ {@const grid = createGridLines()}
273
+ {@const xScale = createLinearScale(xDomain, [0, innerWidth])}
274
+ {@const yScale = createLinearScale(yDomain, [innerHeight, 0])}
275
+
276
+ {#if showGrid}
277
+ <g class="line-chart-grid">
278
+ {#each grid as line}
279
+ <line x1={0} y1={line.y} x2={line.x} y2={line.y} class="line-chart-grid-line" />
280
+ {/each}
281
+ </g>
282
+ {/if}
283
+
284
+ <g class="line-chart-axis">
285
+ <line
286
+ x1={0}
287
+ y1={innerHeight}
288
+ x2={innerWidth}
289
+ y2={innerHeight}
290
+ class="line-chart-axis-line"
291
+ />
292
+ <line x1={0} y1={0} x2={0} y2={innerHeight} class="line-chart-axis-line" />
293
+
294
+ {#each grid as line}
295
+ <text
296
+ x={-10}
297
+ y={line.y}
298
+ class="line-chart-axis-label"
299
+ text-anchor="end"
300
+ dominant-baseline="middle"
301
+ >
302
+ {formatNumber(line.value)}
303
+ </text>
304
+ {/each}
305
+ </g>
306
+
307
+ {#each normalizedSeries as s}
308
+ <path
309
+ d={createPath(s.data, curve === 'smooth')}
310
+ class="line-chart-line is-{s.color}"
311
+ style="stroke-width: {strokeWidth};"
312
+ />
313
+
314
+ {#if showPoints}
315
+ {#each s.data as point}
316
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
317
+ <circle
318
+ cx={xScale(point.x)}
319
+ cy={yScale(point.y)}
320
+ r={pointRadius}
321
+ class="line-chart-point is-{s.color}"
322
+ onmouseenter={(e) => handlePointHover(point, s.name, e, s.color!)}
323
+ onmouseleave={handlePointLeave}
324
+ />
325
+ {/each}
326
+ {/if}
327
+ {/each}
328
+ {/if}
329
+ </g>
330
+ </svg>
331
+ </div>
332
+
333
+ {#if tooltipData && isTooltipActive}
334
+ <div
335
+ class="line-chart-tooltip"
336
+ style="top: {tooltipPosition.y}px; left: {tooltipPosition.x}px;"
337
+ >
338
+ <div class="line-chart-tooltip-content">
339
+ {#if tooltipData.seriesName !== 'Data'}
340
+ <div class="line-chart-tooltip-title">{tooltipData.seriesName}</div>
341
+ {/if}
342
+ <div class="line-chart-tooltip-row">
343
+ <span class="line-chart-tooltip-label">x:</span>
344
+ <span class="line-chart-tooltip-value">{tooltipData.x}</span>
345
+ </div>
346
+ <div class="line-chart-tooltip-row">
347
+ <span class="line-chart-tooltip-label">y:</span>
348
+ <span class="line-chart-tooltip-value">{tooltipData.y}</span>
349
+ </div>
350
+ </div>
351
+ </div>
352
+ {/if}
353
+
354
+ {#if showLegend && normalizedSeries.length > 1}
355
+ <div class="line-chart-legend">
356
+ {#each normalizedSeries as s}
357
+ <div class="line-chart-legend-item">
358
+ <div class="line-chart-legend-color is-{s.color}"></div>
359
+ <span>{s.name}</span>
360
+ </div>
361
+ {/each}
362
+ </div>
363
+ {/if}
364
+ {/if}
365
+ </div>
@@ -0,0 +1,36 @@
1
+ type Color = 'primary' | 'secondary' | 'success' | 'info' | 'warning' | 'danger' | 'muted';
2
+ type DataPoint = {
3
+ x: number;
4
+ y: number;
5
+ };
6
+ type Series = {
7
+ name: string;
8
+ data: DataPoint[];
9
+ color?: Color;
10
+ };
11
+ type Margin = {
12
+ top: number;
13
+ right: number;
14
+ bottom: number;
15
+ left: number;
16
+ };
17
+ type Props = {
18
+ data?: DataPoint[];
19
+ series?: Series[];
20
+ margin?: Margin;
21
+ color?: Color;
22
+ colors?: Color[];
23
+ showPoints?: boolean;
24
+ showGrid?: boolean;
25
+ showLegend?: boolean;
26
+ curve?: 'linear' | 'smooth';
27
+ strokeWidth?: number;
28
+ pointRadius?: number;
29
+ loading?: boolean;
30
+ empty?: boolean;
31
+ emptyText?: string;
32
+ class?: string;
33
+ };
34
+ declare const LineChart: import("svelte").Component<Props, {}, "">;
35
+ type LineChart = ReturnType<typeof LineChart>;
36
+ export default LineChart;
@@ -0,0 +1,311 @@
1
+ <script lang="ts">
2
+ import { cn } from '../utils/class-names.js';
3
+ import { onMount } from 'svelte';
4
+
5
+ type Slice = DataPoint & {
6
+ startAngle: number;
7
+ endAngle: number;
8
+ midAngle: number;
9
+ percentage: number;
10
+ color: Color;
11
+ };
12
+
13
+ type Color = 'primary' | 'secondary' | 'success' | 'info' | 'warning' | 'danger' | 'muted';
14
+
15
+ type DataPoint = {
16
+ label: string;
17
+ value: number;
18
+ color?: Color;
19
+ };
20
+
21
+ type Props = {
22
+ data?: DataPoint[];
23
+ colors?: Color[];
24
+ donut?: boolean;
25
+ donutWidth?: number;
26
+ showLabels?: boolean;
27
+ showValues?: boolean;
28
+ showLegend?: boolean;
29
+ showPercentages?: boolean;
30
+ centerLabel?: string;
31
+ centerValue?: string | number;
32
+ startAngle?: number;
33
+ padAngle?: number;
34
+ loading?: boolean;
35
+ empty?: boolean;
36
+ emptyText?: string;
37
+ class?: string;
38
+ chartClass?: string;
39
+ };
40
+
41
+ let {
42
+ data = [],
43
+ colors = ['primary', 'secondary', 'success', 'info', 'warning', 'danger', 'muted'] as Color[],
44
+ donut = false,
45
+ donutWidth = 60,
46
+ showLabels = true,
47
+ showValues = true,
48
+ showLegend = true,
49
+ showPercentages = true,
50
+ centerLabel = 'Total',
51
+ centerValue = undefined,
52
+ startAngle = -90,
53
+ padAngle = 0,
54
+ loading = false,
55
+ empty = false,
56
+ emptyText = 'No data available',
57
+ class: className,
58
+ chartClass
59
+ }: Props = $props();
60
+
61
+ const colorClass = {
62
+ primary: 'is-primary',
63
+ secondary: 'is-secondary',
64
+ success: 'is-success',
65
+ info: 'is-info',
66
+ warning: 'is-warning',
67
+ danger: 'is-danger',
68
+ muted: 'is-muted'
69
+ };
70
+
71
+ let containerEl: HTMLDivElement | undefined = $state();
72
+ let containerSize = $state({ width: 0, height: 0 });
73
+
74
+ let width = $derived(containerSize.width);
75
+ let height = $derived(containerSize.height);
76
+
77
+ let total = $derived(data.reduce((sum, d) => sum + d.value, 0));
78
+
79
+ let slices = $derived.by((): Slice[] => {
80
+ const startRad = (startAngle * Math.PI) / 180;
81
+ let currentAngle = startRad;
82
+ const padRad = (padAngle * Math.PI) / 180;
83
+
84
+ return data.map((d, i) => {
85
+ const percentage = (d.value / total) * 100;
86
+ const angle = (d.value / total) * 2 * Math.PI - padRad;
87
+ const startAngle = currentAngle;
88
+ const endAngle = currentAngle + angle;
89
+ const midAngle = startAngle + angle / 2;
90
+
91
+ currentAngle = endAngle + padRad;
92
+
93
+ return {
94
+ ...d,
95
+ startAngle,
96
+ endAngle,
97
+ midAngle,
98
+ percentage,
99
+ color: d.color || colors[i % colors.length]
100
+ };
101
+ });
102
+ });
103
+
104
+ function createArc(
105
+ startAngle: number,
106
+ endAngle: number,
107
+ innerRadius: number,
108
+ outerRadius: number
109
+ ): string {
110
+ const x1 = Math.cos(startAngle) * outerRadius;
111
+ const y1 = Math.sin(startAngle) * outerRadius;
112
+ const x2 = Math.cos(endAngle) * outerRadius;
113
+ const y2 = Math.sin(endAngle) * outerRadius;
114
+
115
+ const largeArc = endAngle - startAngle > Math.PI ? 1 : 0;
116
+
117
+ let path = `M ${x1} ${y1} A ${outerRadius} ${outerRadius} 0 ${largeArc} 1 ${x2} ${y2}`;
118
+
119
+ if (innerRadius > 0) {
120
+ const x3 = Math.cos(endAngle) * innerRadius;
121
+ const y3 = Math.sin(endAngle) * innerRadius;
122
+ const x4 = Math.cos(startAngle) * innerRadius;
123
+ const y4 = Math.sin(startAngle) * innerRadius;
124
+
125
+ path += ` L ${x3} ${y3} A ${innerRadius} ${innerRadius} 0 ${largeArc} 0 ${x4} ${y4} Z`;
126
+ } else {
127
+ path += ' L 0 0 Z';
128
+ }
129
+
130
+ return path;
131
+ }
132
+
133
+ let tooltipData = $state<Slice | null>(null);
134
+ let tooltipPosition = $state<{ x: number; y: number }>({ x: 0, y: 0 });
135
+ let isTooltipActive = $state(false);
136
+
137
+ function handleSliceHover(slice: Slice, event: MouseEvent): void {
138
+ const target = event.target as SVGPathElement;
139
+ const rect = target.getBoundingClientRect();
140
+
141
+ tooltipData = slice;
142
+ tooltipPosition = {
143
+ x: rect.right + 8,
144
+ y: rect.top + rect.height / 2
145
+ };
146
+ isTooltipActive = true;
147
+ }
148
+
149
+ function handleSliceLeave(): void {
150
+ isTooltipActive = false;
151
+ setTimeout(() => {
152
+ if (!isTooltipActive) {
153
+ tooltipData = null;
154
+ }
155
+ }, 100);
156
+ }
157
+
158
+ onMount(() => {
159
+ const updateSize = () => {
160
+ if (containerEl) {
161
+ const rect = containerEl.getBoundingClientRect();
162
+ containerSize = { width: rect.width, height: rect.height };
163
+ }
164
+ };
165
+
166
+ updateSize();
167
+ const resizeObserver = new ResizeObserver(updateSize);
168
+ if (containerEl) {
169
+ resizeObserver.observe(containerEl);
170
+ }
171
+
172
+ return () => {
173
+ resizeObserver.disconnect();
174
+ };
175
+ });
176
+ </script>
177
+
178
+ <div class={cn('pie-chart-container', className)}>
179
+ {#if loading}
180
+ <div class="pie-chart-loading">
181
+ <svg class="pie-chart-loading-spinner" viewBox="0 0 24 24">
182
+ <circle
183
+ cx="12"
184
+ cy="12"
185
+ r="10"
186
+ stroke="currentColor"
187
+ stroke-width="4"
188
+ fill="none"
189
+ opacity="0.25"
190
+ />
191
+ <path
192
+ fill="currentColor"
193
+ d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
194
+ />
195
+ </svg>
196
+ </div>
197
+ {:else if empty || data.length === 0}
198
+ <div class="pie-chart-empty">
199
+ <svg class="pie-chart-empty-icon" fill="none" viewBox="0 0 24 24" stroke="currentColor">
200
+ <path
201
+ stroke-linecap="round"
202
+ stroke-linejoin="round"
203
+ stroke-width="2"
204
+ d="M11 3.055A9.001 9.001 0 1020.945 13H11V3.055z"
205
+ />
206
+ <path
207
+ stroke-linecap="round"
208
+ stroke-linejoin="round"
209
+ stroke-width="2"
210
+ d="M20.488 9H15V3.512A9.025 9.025 0 0120.488 9z"
211
+ />
212
+ </svg>
213
+ <span>{emptyText}</span>
214
+ </div>
215
+ {:else}
216
+ <div bind:this={containerEl} class={cn('pie-chart', chartClass)}>
217
+ <svg class="pie-chart-svg">
218
+ {#if slices.length > 0}
219
+ {@const centerX = width / 2}
220
+ {@const centerY = height / 2}
221
+ {@const radius = Math.min(width, height) / 2}
222
+ {@const innerRadius = donut ? radius - donutWidth : 0}
223
+
224
+ <g transform="translate({centerX}, {centerY})">
225
+ {#each slices as slice}
226
+ {@const labelRadius = donut ? radius - donutWidth / 2 : radius * 0.7}
227
+ {@const labelX = Math.cos(slice.midAngle) * labelRadius}
228
+ {@const labelY = Math.sin(slice.midAngle) * labelRadius}
229
+
230
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
231
+ <path
232
+ d={createArc(slice.startAngle, slice.endAngle, innerRadius, radius)}
233
+ class="pie-chart-slice is-{slice.color}"
234
+ onmouseenter={(e) => handleSliceHover(slice, e)}
235
+ onmouseleave={handleSliceLeave}
236
+ />
237
+
238
+ {#if showLabels && slice.percentage > 5}
239
+ <text
240
+ x={labelX}
241
+ y={labelY}
242
+ class="pie-chart-slice-label"
243
+ text-anchor="middle"
244
+ dominant-baseline="middle"
245
+ >
246
+ {#if showPercentages}
247
+ {slice.percentage.toFixed(1)}%
248
+ {:else if showValues}
249
+ {slice.value}
250
+ {/if}
251
+ </text>
252
+ {/if}
253
+ {/each}
254
+
255
+ {#if donut}
256
+ <text
257
+ x="0"
258
+ y="-5"
259
+ class="pie-chart-center-value"
260
+ text-anchor="middle"
261
+ dominant-baseline="middle"
262
+ >
263
+ {centerValue ?? total}
264
+ </text>
265
+ <text
266
+ x="0"
267
+ y="15"
268
+ class="pie-chart-center-label"
269
+ text-anchor="middle"
270
+ dominant-baseline="middle"
271
+ >
272
+ {centerLabel}
273
+ </text>
274
+ {/if}
275
+ </g>
276
+ {/if}
277
+ </svg>
278
+ </div>
279
+
280
+ {#if tooltipData && isTooltipActive}
281
+ <div
282
+ class="pie-chart-tooltip"
283
+ style="top: {tooltipPosition.y}px; left: {tooltipPosition.x}px;"
284
+ >
285
+ <div class="pie-chart-tooltip-content">
286
+ <div class="pie-chart-tooltip-title">{tooltipData.label}</div>
287
+ <div class="pie-chart-tooltip-row">
288
+ <div class="pie-chart-tooltip-color is-{tooltipData.color}"></div>
289
+ <span class="pie-chart-tooltip-value">
290
+ {tooltipData.value} ({tooltipData.percentage.toFixed(1)}%)
291
+ </span>
292
+ </div>
293
+ </div>
294
+ </div>
295
+ {/if}
296
+
297
+ {#if showLegend}
298
+ <div class="pie-chart-legend">
299
+ {#each slices as slice}
300
+ <div class="pie-chart-legend-item">
301
+ <div class={cn('pie-chart-legend-color', colorClass[slice.color])}></div>
302
+ <span>{slice.label}</span>
303
+ {#if showValues}
304
+ <span class="pie-chart-legend-value">({slice.value})</span>
305
+ {/if}
306
+ </div>
307
+ {/each}
308
+ </div>
309
+ {/if}
310
+ {/if}
311
+ </div>