quantumgui 1.0.0__py3-none-any.whl

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 (73) hide show
  1. quantumgui/__init__.py +9 -0
  2. quantumgui/__main__.py +6 -0
  3. quantumgui/cli.py +341 -0
  4. quantumgui/core/__init__.py +39 -0
  5. quantumgui/core/animation.py +221 -0
  6. quantumgui/core/audit.py +323 -0
  7. quantumgui/core/context.py +54 -0
  8. quantumgui/core/data_source.py +426 -0
  9. quantumgui/core/draw.py +428 -0
  10. quantumgui/core/enterprise.py +402 -0
  11. quantumgui/core/enterprise_app.py +419 -0
  12. quantumgui/core/events.py +34 -0
  13. quantumgui/core/geometry.py +108 -0
  14. quantumgui/core/native_dialogs.py +276 -0
  15. quantumgui/core/pdf_report.py +390 -0
  16. quantumgui/core/rules.py +284 -0
  17. quantumgui/core/state.py +42 -0
  18. quantumgui/core/task_runner.py +181 -0
  19. quantumgui/core/widget.py +498 -0
  20. quantumgui/core/window.py +920 -0
  21. quantumgui/core/xlsx_export.py +145 -0
  22. quantumgui/py.typed +0 -0
  23. quantumgui/theme/__init__.py +32 -0
  24. quantumgui/theme/hct.py +191 -0
  25. quantumgui/theme/theme.py +93 -0
  26. quantumgui/theme/tokens.py +423 -0
  27. quantumgui/ui.py +2142 -0
  28. quantumgui/widgets/__init__.py +168 -0
  29. quantumgui/widgets/advanced.py +499 -0
  30. quantumgui/widgets/button.py +224 -0
  31. quantumgui/widgets/charts.py +625 -0
  32. quantumgui/widgets/charts_bi.py +528 -0
  33. quantumgui/widgets/charts_extra.py +430 -0
  34. quantumgui/widgets/charts_pro.py +568 -0
  35. quantumgui/widgets/checkbox.py +199 -0
  36. quantumgui/widgets/chip.py +219 -0
  37. quantumgui/widgets/containers.py +321 -0
  38. quantumgui/widgets/data_controls.py +487 -0
  39. quantumgui/widgets/data_grid.py +956 -0
  40. quantumgui/widgets/data_table.py +551 -0
  41. quantumgui/widgets/date_picker.py +407 -0
  42. quantumgui/widgets/display_media.py +372 -0
  43. quantumgui/widgets/display_pro.py +407 -0
  44. quantumgui/widgets/drawer.py +252 -0
  45. quantumgui/widgets/enterprise_controls.py +934 -0
  46. quantumgui/widgets/fab.py +261 -0
  47. quantumgui/widgets/forms_pro.py +703 -0
  48. quantumgui/widgets/forms_pro_media.py +587 -0
  49. quantumgui/widgets/inputs.py +870 -0
  50. quantumgui/widgets/layout_pro.py +162 -0
  51. quantumgui/widgets/list_view.py +307 -0
  52. quantumgui/widgets/masked_input.py +131 -0
  53. quantumgui/widgets/master_detail.py +155 -0
  54. quantumgui/widgets/navigation.py +382 -0
  55. quantumgui/widgets/overlays.py +355 -0
  56. quantumgui/widgets/productivity.py +500 -0
  57. quantumgui/widgets/productivity_boards.py +544 -0
  58. quantumgui/widgets/radio.py +209 -0
  59. quantumgui/widgets/scroll_view.py +129 -0
  60. quantumgui/widgets/select.py +373 -0
  61. quantumgui/widgets/sheets.py +249 -0
  62. quantumgui/widgets/slider.py +209 -0
  63. quantumgui/widgets/switch.py +196 -0
  64. quantumgui/widgets/tabs.py +175 -0
  65. quantumgui/widgets/text.py +101 -0
  66. quantumgui/widgets/text_field.py +490 -0
  67. quantumgui/widgets/time_picker.py +499 -0
  68. quantumgui-1.0.0.dist-info/METADATA +137 -0
  69. quantumgui-1.0.0.dist-info/RECORD +73 -0
  70. quantumgui-1.0.0.dist-info/WHEEL +5 -0
  71. quantumgui-1.0.0.dist-info/entry_points.txt +2 -0
  72. quantumgui-1.0.0.dist-info/licenses/LICENSE +32 -0
  73. quantumgui-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,568 @@
1
+ """Material Design 3 Advanced GPU Charts: GaugeChart, MultiLineChart, Heatmap, RadarChart, ScatterChart."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import math
6
+ from typing import Mapping, Sequence
7
+
8
+ import skia
9
+
10
+ from ..core.draw import draw_text_centered_y, font_for_style, make_rrect, measure_text
11
+ from ..core.events import MouseEvent
12
+ from ..core.geometry import Constraints, Rect, Size
13
+ from ..core.widget import Widget
14
+ from ..theme import Color, Theme
15
+ from .charts import ChartColorRole, _draw_chart_tooltip, _format_num, _resolve_accent
16
+
17
+
18
+ def _series_colors(theme: Theme) -> list[Color]:
19
+ c = theme.colors
20
+ return [c.primary, c.tertiary, c.secondary, c.error, c.on_primary_container]
21
+
22
+
23
+ class GaugeChart(Widget):
24
+ """Material Design 3 semicircular GPU GaugeChart (220° arc)."""
25
+
26
+ def __init__(
27
+ self,
28
+ value: float,
29
+ *,
30
+ min: float = 0.0,
31
+ max: float = 100.0,
32
+ title: str | None = None,
33
+ unit: str = "%",
34
+ subtitle: str | None = None,
35
+ color: ChartColorRole | Color = "primary",
36
+ thickness: float = 18.0,
37
+ chart_height: float = 210.0,
38
+ ) -> None:
39
+ super().__init__()
40
+ self.min_val = float(min)
41
+ self.max_val = float(max) if max > min else float(min) + 1.0
42
+ self._value = float(value)
43
+ self.title = title
44
+ self.unit = unit
45
+ self.subtitle = subtitle
46
+ self.color: ChartColorRole | Color = color
47
+ self.thickness = float(thickness)
48
+ self.chart_height = float(chart_height)
49
+ self._fill_width = True
50
+
51
+ @property
52
+ def value(self) -> float:
53
+ return self._value
54
+
55
+ @value.setter
56
+ def value(self, val: float) -> None:
57
+ self._value = float(val)
58
+ self.invalidate()
59
+
60
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
61
+ w = constraints.max_width if constraints.max_width != float("inf") else 280.0
62
+ return Size(w, self.chart_height)
63
+
64
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
65
+ c = theme.colors
66
+ r = self.rect
67
+ accent = _resolve_accent(theme, self.color)
68
+ top_pad = 32.0 if self.title else 10.0
69
+
70
+ if self.title:
71
+ tf = font_for_style(theme.font_family, theme.typography.title_medium)
72
+ draw_text_centered_y(canvas, self.title, r.x + 4.0, r.y + 14.0, tf, c.on_surface)
73
+
74
+ avail_h = max(80.0, r.height - top_pad - 12.0)
75
+ radius = min(avail_h * 0.56, r.width * 0.38)
76
+ cx = r.center_x
77
+ cy = r.y + top_pad + avail_h * 0.62
78
+ oval = skia.Rect.MakeXYWH(cx - radius, cy - radius, radius * 2.0, radius * 2.0)
79
+
80
+ start_deg = 160.0
81
+ total_sweep = 220.0
82
+ ratio = max(0.0, min(1.0, (self._value - self.min_val) / (self.max_val - self.min_val)))
83
+
84
+ canvas.drawArc(
85
+ oval,
86
+ start_deg,
87
+ total_sweep,
88
+ False,
89
+ skia.Paint(
90
+ AntiAlias=True,
91
+ Color=c.secondary_container.to_skia(),
92
+ Style=skia.Paint.kStroke_Style,
93
+ StrokeWidth=self.thickness,
94
+ StrokeCap=skia.Paint.kRound_Cap,
95
+ ),
96
+ )
97
+ if ratio > 0.0:
98
+ canvas.drawArc(
99
+ oval,
100
+ start_deg,
101
+ total_sweep * ratio,
102
+ False,
103
+ skia.Paint(
104
+ AntiAlias=True,
105
+ Color=accent.to_skia(),
106
+ Style=skia.Paint.kStroke_Style,
107
+ StrokeWidth=self.thickness,
108
+ StrokeCap=skia.Paint.kRound_Cap,
109
+ ),
110
+ )
111
+
112
+ vf = font_for_style(theme.font_family, theme.typography.headline_medium)
113
+ val_txt = f"{_format_num(self._value)}{self.unit}"
114
+ vw = measure_text(vf, val_txt)
115
+ draw_text_centered_y(canvas, val_txt, cx - vw * 0.5, cy - 6.0, vf, c.on_surface)
116
+
117
+ if self.subtitle:
118
+ sf = font_for_style(theme.font_family, theme.typography.label_medium)
119
+ sw = measure_text(sf, self.subtitle)
120
+ draw_text_centered_y(canvas, self.subtitle, cx - sw * 0.5, cy + 20.0, sf, c.on_surface_variant)
121
+
122
+ # Min / Max labels at arc ends
123
+ lf = font_for_style(theme.font_family, theme.typography.label_small)
124
+ min_s = _format_num(self.min_val)
125
+ max_s = _format_num(self.max_val)
126
+ draw_text_centered_y(canvas, min_s, cx - radius - 4.0, cy + radius * 0.52, lf, c.on_surface_variant)
127
+ draw_text_centered_y(canvas, max_s, cx + radius - measure_text(lf, max_s) + 4.0, cy + radius * 0.52, lf, c.on_surface_variant)
128
+
129
+
130
+ class MultiLineChart(Widget):
131
+ """Material Design 3 GPU MultiLineChart comparing multiple series with combined hover tooltip."""
132
+
133
+ interactive = True
134
+ cursor = "pointer"
135
+
136
+ def __init__(
137
+ self,
138
+ labels: Sequence[str],
139
+ series: Mapping[str, Sequence[float]],
140
+ *,
141
+ title: str | None = None,
142
+ smooth: bool = True,
143
+ chart_height: float = 240.0,
144
+ ) -> None:
145
+ super().__init__()
146
+ self.labels = [str(x) for x in labels]
147
+ self.series: dict[str, list[float]] = {str(k): [float(v) for v in vals] for k, vals in series.items()}
148
+ self.title = title
149
+ self.smooth = smooth
150
+ self.chart_height = float(chart_height)
151
+ self._hover_idx: int = -1
152
+ self._xs: list[float] = []
153
+ self._fill_width = True
154
+
155
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
156
+ w = constraints.max_width if constraints.max_width != float("inf") else 460.0
157
+ return Size(w, self.chart_height)
158
+
159
+ def on_mouse_move(self, event: MouseEvent) -> None:
160
+ if not self._xs:
161
+ return
162
+ best = min(range(len(self._xs)), key=lambda i: abs(event.x - self._xs[i]))
163
+ if best != self._hover_idx:
164
+ self._hover_idx = best
165
+ self.invalidate()
166
+
167
+ def on_mouse_leave(self) -> None:
168
+ super().on_mouse_leave()
169
+ self._hover_idx = -1
170
+
171
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
172
+ c = theme.colors
173
+ r = self.rect
174
+ colors = _series_colors(theme)
175
+ top_pad = 40.0 if self.title else 24.0
176
+ left_pad = 44.0
177
+ right_pad = 16.0
178
+ bottom_pad = 28.0
179
+ plot = Rect(r.x + left_pad, r.y + top_pad, max(20.0, r.width - left_pad - right_pad), max(20.0, r.height - top_pad - bottom_pad))
180
+
181
+ if self.title:
182
+ tf = font_for_style(theme.font_family, theme.typography.title_medium)
183
+ draw_text_centered_y(canvas, self.title, r.x + 4.0, r.y + 14.0, tf, c.on_surface)
184
+
185
+ # Inline top-right legend
186
+ lf = font_for_style(theme.font_family, theme.typography.label_small)
187
+ lx = r.right - 12.0
188
+ for idx, name in reversed(list(enumerate(self.series.keys()))):
189
+ nw = measure_text(lf, name)
190
+ lx -= nw
191
+ draw_text_centered_y(canvas, name, lx, r.y + 14.0, lf, c.on_surface_variant)
192
+ lx -= 12.0
193
+ canvas.drawCircle(lx, r.y + 14.0, 4.5, skia.Paint(AntiAlias=True, Color=colors[idx % len(colors)].to_skia()))
194
+ lx -= 16.0
195
+
196
+ all_vals = [v for vals in self.series.values() for v in vals]
197
+ if not all_vals:
198
+ return
199
+ lo = min(0.0, min(all_vals))
200
+ hi = max(all_vals) * 1.12
201
+ if hi <= lo:
202
+ hi = lo + 1.0
203
+
204
+ grid_p = skia.Paint(AntiAlias=True, Color=c.outline_variant.with_alpha(0.55).to_skia(), StrokeWidth=1.0)
205
+ for step in range(5):
206
+ frac = step / 4.0
207
+ gy = plot.bottom - frac * plot.height
208
+ canvas.drawLine(plot.x, gy, plot.right, gy, grid_p)
209
+ vt = _format_num(lo + frac * (hi - lo))
210
+ draw_text_centered_y(canvas, vt, plot.x - measure_text(lf, vt) - 8.0, gy, lf, c.on_surface_variant)
211
+
212
+ n = max((len(v) for v in self.series.values()), default=0)
213
+ if n == 0:
214
+ return
215
+ self._xs = [plot.center_x if n == 1 else plot.x + (i / (n - 1)) * plot.width for i in range(n)]
216
+ for i, px in enumerate(self._xs):
217
+ if i < len(self.labels):
218
+ draw_text_centered_y(canvas, self.labels[i], px - measure_text(lf, self.labels[i]) * 0.5, plot.bottom + 15.0, lf, c.on_surface_variant)
219
+
220
+ hover_parts: list[str] = []
221
+ hover_y = plot.center_y
222
+ for s_idx, (name, vals) in enumerate(self.series.items()):
223
+ col = colors[s_idx % len(colors)]
224
+ pts = [
225
+ (self._xs[i], plot.bottom - ((v - lo) / (hi - lo)) * plot.height)
226
+ for i, v in enumerate(vals)
227
+ ]
228
+ if not pts:
229
+ continue
230
+ path = skia.Path()
231
+ path.moveTo(pts[0][0], pts[0][1])
232
+ if self.smooth and len(pts) >= 3:
233
+ for i in range(len(pts) - 1):
234
+ x0, y0 = pts[i]
235
+ x1, y1 = pts[i + 1]
236
+ cx = (x0 + x1) * 0.5
237
+ path.cubicTo(cx, y0, cx, y1, x1, y1)
238
+ else:
239
+ for px, py in pts[1:]:
240
+ path.lineTo(px, py)
241
+ canvas.drawPath(
242
+ path,
243
+ skia.Paint(AntiAlias=True, Color=col.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=2.4, StrokeCap=skia.Paint.kRound_Cap),
244
+ )
245
+ if 0 <= self._hover_idx < len(pts):
246
+ hx, hy = pts[self._hover_idx]
247
+ hover_y = min(hover_y, hy)
248
+ canvas.drawCircle(hx, hy, 5.0, skia.Paint(AntiAlias=True, Color=col.to_skia()))
249
+ hover_parts.append(f"{name}: {_format_num(vals[self._hover_idx])}")
250
+
251
+ if 0 <= self._hover_idx < len(self._xs) and hover_parts:
252
+ hx = self._xs[self._hover_idx]
253
+ canvas.drawLine(hx, plot.y, hx, plot.bottom, skia.Paint(AntiAlias=True, Color=c.outline.with_alpha(0.45).to_skia(), StrokeWidth=1.0))
254
+ lbl = self.labels[self._hover_idx] if self._hover_idx < len(self.labels) else f"#{self._hover_idx + 1}"
255
+ _draw_chart_tooltip(canvas, theme, hx, hover_y, f"{lbl} · " + " | ".join(hover_parts), r)
256
+
257
+
258
+ class Heatmap(Widget):
259
+ """Material Design 3 GPU Heatmap matrix with rounded cells and live hover tooltip."""
260
+
261
+ interactive = True
262
+ cursor = "pointer"
263
+
264
+ def __init__(
265
+ self,
266
+ matrix: Sequence[Sequence[float]],
267
+ *,
268
+ x_labels: Sequence[str] | None = None,
269
+ y_labels: Sequence[str] | None = None,
270
+ title: str | None = None,
271
+ color: ChartColorRole | Color = "primary",
272
+ chart_height: float = 220.0,
273
+ ) -> None:
274
+ super().__init__()
275
+ self.matrix: list[list[float]] = [[float(v) for v in row] for row in matrix]
276
+ self.x_labels = [str(x) for x in x_labels] if x_labels else []
277
+ self.y_labels = [str(y) for y in y_labels] if y_labels else []
278
+ self.title = title
279
+ self.color: ChartColorRole | Color = color
280
+ self.chart_height = float(chart_height)
281
+ self._hover_cell: tuple[int, int] | None = None
282
+ self._cell_rects: list[tuple[int, int, Rect]] = []
283
+ self._fill_width = True
284
+
285
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
286
+ w = constraints.max_width if constraints.max_width != float("inf") else 400.0
287
+ return Size(w, self.chart_height)
288
+
289
+ def on_mouse_move(self, event: MouseEvent) -> None:
290
+ found = None
291
+ for ri, ci, cr in self._cell_rects:
292
+ if cr.contains(event.x, event.y):
293
+ found = (ri, ci)
294
+ break
295
+ if found != self._hover_cell:
296
+ self._hover_cell = found
297
+ self.invalidate()
298
+
299
+ def on_mouse_leave(self) -> None:
300
+ super().on_mouse_leave()
301
+ self._hover_cell = None
302
+
303
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
304
+ c = theme.colors
305
+ r = self.rect
306
+ accent = _resolve_accent(theme, self.color)
307
+ top_pad = 36.0 if self.title else 12.0
308
+ left_pad = 42.0 if self.y_labels else 10.0
309
+ bottom_pad = 24.0 if self.x_labels else 10.0
310
+ plot = Rect(r.x + left_pad, r.y + top_pad, max(20.0, r.width - left_pad - 12.0), max(20.0, r.height - top_pad - bottom_pad))
311
+
312
+ if self.title:
313
+ tf = font_for_style(theme.font_family, theme.typography.title_medium)
314
+ draw_text_centered_y(canvas, self.title, r.x + 4.0, r.y + 14.0, tf, c.on_surface)
315
+
316
+ if not self.matrix or not self.matrix[0]:
317
+ return
318
+
319
+ rows = len(self.matrix)
320
+ cols = max(len(row) for row in self.matrix)
321
+ flat = [v for row in self.matrix for v in row]
322
+ lo, hi = min(flat), max(flat)
323
+ span = (hi - lo) if hi > lo else 1.0
324
+
325
+ gap = 4.0
326
+ cw = max(6.0, (plot.width - gap * (cols - 1)) / cols)
327
+ ch = max(6.0, (plot.height - gap * (rows - 1)) / rows)
328
+ lf = font_for_style(theme.font_family, theme.typography.label_small)
329
+ self._cell_rects = []
330
+
331
+ for ri, row in enumerate(self.matrix):
332
+ cy = plot.y + ri * (ch + gap)
333
+ if ri < len(self.y_labels):
334
+ yl = self.y_labels[ri]
335
+ draw_text_centered_y(canvas, yl, plot.x - measure_text(lf, yl) - 8.0, cy + ch * 0.5, lf, c.on_surface_variant)
336
+ for ci, val in enumerate(row):
337
+ cx = plot.x + ci * (cw + gap)
338
+ cr = Rect(cx, cy, cw, ch)
339
+ self._cell_rects.append((ri, ci, cr))
340
+ t = (val - lo) / span
341
+ cell_c = c.surface_container_highest.blend(accent, 0.12 + t * 0.88)
342
+ canvas.drawRRect(make_rrect(cr, 5.0), skia.Paint(AntiAlias=True, Color=cell_c.to_skia()))
343
+ if self._hover_cell == (ri, ci):
344
+ canvas.drawRRect(
345
+ make_rrect(cr, 5.0),
346
+ skia.Paint(AntiAlias=True, Color=c.on_surface.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=2.0),
347
+ )
348
+
349
+ for ci, xl in enumerate(self.x_labels[:cols]):
350
+ cx = plot.x + ci * (cw + gap) + cw * 0.5
351
+ draw_text_centered_y(canvas, xl, cx - measure_text(lf, xl) * 0.5, plot.bottom + 14.0, lf, c.on_surface_variant)
352
+
353
+ if self._hover_cell is not None:
354
+ ri, ci = self._hover_cell
355
+ val = self.matrix[ri][ci]
356
+ for r_i, c_i, cr in self._cell_rects:
357
+ if (r_i, c_i) == (ri, ci):
358
+ yl = self.y_labels[ri] if ri < len(self.y_labels) else f"F{ri + 1}"
359
+ xl = self.x_labels[ci] if ci < len(self.x_labels) else f"C{ci + 1}"
360
+ _draw_chart_tooltip(canvas, theme, cr.center_x, cr.y, f"{yl} · {xl}: {_format_num(val)}", r)
361
+ break
362
+
363
+
364
+ class RadarChart(Widget):
365
+ """Material Design 3 GPU Radar / Spider Chart for multi-axis comparisons."""
366
+
367
+ interactive = True
368
+ cursor = "pointer"
369
+
370
+ def __init__(
371
+ self,
372
+ axes: Sequence[str],
373
+ series: Mapping[str, Sequence[float]],
374
+ *,
375
+ max_value: float = 100.0,
376
+ title: str | None = None,
377
+ chart_height: float = 240.0,
378
+ ) -> None:
379
+ super().__init__()
380
+ self.axes = [str(a) for a in axes]
381
+ self.series: dict[str, list[float]] = {str(k): [float(v) for v in vals] for k, vals in series.items()}
382
+ self.max_value = max(1e-3, float(max_value))
383
+ self.title = title
384
+ self.chart_height = float(chart_height)
385
+ self._hover_axis: int = -1
386
+ self._center: tuple[float, float] = (0.0, 0.0)
387
+ self._radius: float = 0.0
388
+ self._fill_width = True
389
+
390
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
391
+ w = constraints.max_width if constraints.max_width != float("inf") else 360.0
392
+ return Size(w, self.chart_height)
393
+
394
+ def on_mouse_move(self, event: MouseEvent) -> None:
395
+ if not self.axes or self._radius <= 0:
396
+ return
397
+ cx, cy = self._center
398
+ dx, dy = event.x - cx, event.y - cy
399
+ if math.hypot(dx, dy) > self._radius + 24.0:
400
+ found = -1
401
+ else:
402
+ ang = (math.atan2(dx, -dy)) % (2.0 * math.pi)
403
+ found = int(round(ang / (2.0 * math.pi / len(self.axes)))) % len(self.axes)
404
+ if found != self._hover_axis:
405
+ self._hover_axis = found
406
+ self.invalidate()
407
+
408
+ def on_mouse_leave(self) -> None:
409
+ super().on_mouse_leave()
410
+ self._hover_axis = -1
411
+
412
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
413
+ c = theme.colors
414
+ r = self.rect
415
+ colors = _series_colors(theme)
416
+ top_pad = 34.0 if self.title else 12.0
417
+
418
+ if self.title:
419
+ tf = font_for_style(theme.font_family, theme.typography.title_medium)
420
+ draw_text_centered_y(canvas, self.title, r.x + 4.0, r.y + 14.0, tf, c.on_surface)
421
+
422
+ n = len(self.axes)
423
+ if n < 3:
424
+ return
425
+
426
+ avail_h = max(80.0, r.height - top_pad - 24.0)
427
+ rad = min(avail_h * 0.42, r.width * 0.34)
428
+ cx, cy = r.center_x, r.y + top_pad + avail_h * 0.52
429
+ self._center = (cx, cy)
430
+ self._radius = rad
431
+
432
+ grid_p = skia.Paint(AntiAlias=True, Color=c.outline_variant.with_alpha(0.6).to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=1.0)
433
+ for ring in range(1, 5):
434
+ rr = rad * (ring / 4.0)
435
+ p = skia.Path()
436
+ for i in range(n):
437
+ a = i * (2.0 * math.pi / n)
438
+ px, py = cx + math.sin(a) * rr, cy - math.cos(a) * rr
439
+ p.moveTo(px, py) if i == 0 else p.lineTo(px, py)
440
+ p.close()
441
+ canvas.drawPath(p, grid_p)
442
+
443
+ lf = font_for_style(theme.font_family, theme.typography.label_small)
444
+ for i, axis_lbl in enumerate(self.axes):
445
+ a = i * (2.0 * math.pi / n)
446
+ ex, ey = cx + math.sin(a) * rad, cy - math.cos(a) * rad
447
+ canvas.drawLine(cx, cy, ex, ey, grid_p)
448
+ lx = cx + math.sin(a) * (rad + 16.0)
449
+ ly = cy - math.cos(a) * (rad + 14.0)
450
+ lw = measure_text(lf, axis_lbl)
451
+ draw_text_centered_y(canvas, axis_lbl, lx - lw * 0.5, ly, lf, c.primary if i == self._hover_axis else c.on_surface_variant)
452
+
453
+ for s_idx, (name, vals) in enumerate(self.series.items()):
454
+ col = colors[s_idx % len(colors)]
455
+ p = skia.Path()
456
+ for i in range(n):
457
+ v = max(0.0, min(self.max_value, vals[i] if i < len(vals) else 0.0))
458
+ rr = rad * (v / self.max_value)
459
+ a = i * (2.0 * math.pi / n)
460
+ px, py = cx + math.sin(a) * rr, cy - math.cos(a) * rr
461
+ p.moveTo(px, py) if i == 0 else p.lineTo(px, py)
462
+ p.close()
463
+ canvas.drawPath(p, skia.Paint(AntiAlias=True, Color=col.with_alpha(0.22).to_skia()))
464
+ canvas.drawPath(p, skia.Paint(AntiAlias=True, Color=col.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=2.2))
465
+
466
+ if 0 <= self._hover_axis < n:
467
+ a = self._hover_axis * (2.0 * math.pi / n)
468
+ tx, ty = cx + math.sin(a) * rad * 0.75, cy - math.cos(a) * rad * 0.75
469
+ parts = [f"{k}: {_format_num(v[self._hover_axis])}" for k, v in self.series.items() if self._hover_axis < len(v)]
470
+ _draw_chart_tooltip(canvas, theme, tx, ty, f"{self.axes[self._hover_axis]} · " + " | ".join(parts), r)
471
+
472
+
473
+ class ScatterChart(Widget):
474
+ """Material Design 3 GPU Scatter / Bubble Chart (`(x, y)`, `(x, y, size)`, or `(x, y, size, label)`)."""
475
+
476
+ interactive = True
477
+ cursor = "pointer"
478
+
479
+ def __init__(
480
+ self,
481
+ points: Sequence[tuple],
482
+ *,
483
+ title: str | None = None,
484
+ color: ChartColorRole | Color = "primary",
485
+ chart_height: float = 230.0,
486
+ ) -> None:
487
+ super().__init__()
488
+ self.points: list[tuple[float, float, float, str]] = []
489
+ for idx, pt in enumerate(points):
490
+ x = float(pt[0])
491
+ y = float(pt[1])
492
+ sz = float(pt[2]) if len(pt) >= 3 else 7.0
493
+ lbl = str(pt[3]) if len(pt) >= 4 else f"P{idx + 1}"
494
+ self.points.append((x, y, sz, lbl))
495
+ self.title = title
496
+ self.color: ChartColorRole | Color = color
497
+ self.chart_height = float(chart_height)
498
+ self._hover_idx: int = -1
499
+ self._screen_pts: list[tuple[float, float, float]] = []
500
+ self._fill_width = True
501
+
502
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
503
+ w = constraints.max_width if constraints.max_width != float("inf") else 400.0
504
+ return Size(w, self.chart_height)
505
+
506
+ def on_mouse_move(self, event: MouseEvent) -> None:
507
+ found = -1
508
+ best = 24.0
509
+ for i, (px, py, rad) in enumerate(self._screen_pts):
510
+ d = math.hypot(event.x - px, event.y - py)
511
+ if d <= max(rad + 6.0, 14.0) and d < best:
512
+ best = d
513
+ found = i
514
+ if found != self._hover_idx:
515
+ self._hover_idx = found
516
+ self.invalidate()
517
+
518
+ def on_mouse_leave(self) -> None:
519
+ super().on_mouse_leave()
520
+ self._hover_idx = -1
521
+
522
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
523
+ c = theme.colors
524
+ r = self.rect
525
+ accent = _resolve_accent(theme, self.color)
526
+ top_pad = 36.0 if self.title else 14.0
527
+ plot = Rect(r.x + 44.0, r.y + top_pad, max(20.0, r.width - 60.0), max(20.0, r.height - top_pad - 28.0))
528
+
529
+ if self.title:
530
+ tf = font_for_style(theme.font_family, theme.typography.title_medium)
531
+ draw_text_centered_y(canvas, self.title, r.x + 4.0, r.y + 14.0, tf, c.on_surface)
532
+
533
+ if not self.points:
534
+ return
535
+
536
+ xs = [p[0] for p in self.points]
537
+ ys = [p[1] for p in self.points]
538
+ x_lo, x_hi = min(xs), max(xs)
539
+ y_lo, y_hi = min(ys), max(ys)
540
+ x_span = (x_hi - x_lo) if x_hi > x_lo else 1.0
541
+ y_span = (y_hi - y_lo) if y_hi > y_lo else 1.0
542
+
543
+ lf = font_for_style(theme.font_family, theme.typography.label_small)
544
+ grid_p = skia.Paint(AntiAlias=True, Color=c.outline_variant.with_alpha(0.55).to_skia(), StrokeWidth=1.0)
545
+ for step in range(5):
546
+ frac = step / 4.0
547
+ gy = plot.bottom - frac * plot.height
548
+ canvas.drawLine(plot.x, gy, plot.right, gy, grid_p)
549
+ vt = _format_num(y_lo + frac * y_span)
550
+ draw_text_centered_y(canvas, vt, plot.x - measure_text(lf, vt) - 8.0, gy, lf, c.on_surface_variant)
551
+
552
+ self._screen_pts = []
553
+ for i, (x, y, sz, _lbl) in enumerate(self.points):
554
+ px = plot.x + ((x - x_lo) / x_span) * plot.width
555
+ py = plot.bottom - ((y - y_lo) / y_span) * plot.height
556
+ rad = max(5.0, min(18.0, sz))
557
+ self._screen_pts.append((px, py, rad))
558
+ is_hov = i == self._hover_idx
559
+ canvas.drawCircle(px, py, rad + (2.0 if is_hov else 0.0), skia.Paint(AntiAlias=True, Color=accent.with_alpha(0.32 if not is_hov else 0.55).to_skia()))
560
+ canvas.drawCircle(
561
+ px, py, rad + (2.0 if is_hov else 0.0),
562
+ skia.Paint(AntiAlias=True, Color=accent.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=2.0),
563
+ )
564
+
565
+ if 0 <= self._hover_idx < len(self.points):
566
+ x, y, _sz, lbl = self.points[self._hover_idx]
567
+ px, py, _r = self._screen_pts[self._hover_idx]
568
+ _draw_chart_tooltip(canvas, theme, px, py, f"{lbl}: ({_format_num(x)}, {_format_num(y)})", r)