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.
- quantumgui/__init__.py +9 -0
- quantumgui/__main__.py +6 -0
- quantumgui/cli.py +341 -0
- quantumgui/core/__init__.py +39 -0
- quantumgui/core/animation.py +221 -0
- quantumgui/core/audit.py +323 -0
- quantumgui/core/context.py +54 -0
- quantumgui/core/data_source.py +426 -0
- quantumgui/core/draw.py +428 -0
- quantumgui/core/enterprise.py +402 -0
- quantumgui/core/enterprise_app.py +419 -0
- quantumgui/core/events.py +34 -0
- quantumgui/core/geometry.py +108 -0
- quantumgui/core/native_dialogs.py +276 -0
- quantumgui/core/pdf_report.py +390 -0
- quantumgui/core/rules.py +284 -0
- quantumgui/core/state.py +42 -0
- quantumgui/core/task_runner.py +181 -0
- quantumgui/core/widget.py +498 -0
- quantumgui/core/window.py +920 -0
- quantumgui/core/xlsx_export.py +145 -0
- quantumgui/py.typed +0 -0
- quantumgui/theme/__init__.py +32 -0
- quantumgui/theme/hct.py +191 -0
- quantumgui/theme/theme.py +93 -0
- quantumgui/theme/tokens.py +423 -0
- quantumgui/ui.py +2142 -0
- quantumgui/widgets/__init__.py +168 -0
- quantumgui/widgets/advanced.py +499 -0
- quantumgui/widgets/button.py +224 -0
- quantumgui/widgets/charts.py +625 -0
- quantumgui/widgets/charts_bi.py +528 -0
- quantumgui/widgets/charts_extra.py +430 -0
- quantumgui/widgets/charts_pro.py +568 -0
- quantumgui/widgets/checkbox.py +199 -0
- quantumgui/widgets/chip.py +219 -0
- quantumgui/widgets/containers.py +321 -0
- quantumgui/widgets/data_controls.py +487 -0
- quantumgui/widgets/data_grid.py +956 -0
- quantumgui/widgets/data_table.py +551 -0
- quantumgui/widgets/date_picker.py +407 -0
- quantumgui/widgets/display_media.py +372 -0
- quantumgui/widgets/display_pro.py +407 -0
- quantumgui/widgets/drawer.py +252 -0
- quantumgui/widgets/enterprise_controls.py +934 -0
- quantumgui/widgets/fab.py +261 -0
- quantumgui/widgets/forms_pro.py +703 -0
- quantumgui/widgets/forms_pro_media.py +587 -0
- quantumgui/widgets/inputs.py +870 -0
- quantumgui/widgets/layout_pro.py +162 -0
- quantumgui/widgets/list_view.py +307 -0
- quantumgui/widgets/masked_input.py +131 -0
- quantumgui/widgets/master_detail.py +155 -0
- quantumgui/widgets/navigation.py +382 -0
- quantumgui/widgets/overlays.py +355 -0
- quantumgui/widgets/productivity.py +500 -0
- quantumgui/widgets/productivity_boards.py +544 -0
- quantumgui/widgets/radio.py +209 -0
- quantumgui/widgets/scroll_view.py +129 -0
- quantumgui/widgets/select.py +373 -0
- quantumgui/widgets/sheets.py +249 -0
- quantumgui/widgets/slider.py +209 -0
- quantumgui/widgets/switch.py +196 -0
- quantumgui/widgets/tabs.py +175 -0
- quantumgui/widgets/text.py +101 -0
- quantumgui/widgets/text_field.py +490 -0
- quantumgui/widgets/time_picker.py +499 -0
- quantumgui-1.0.0.dist-info/METADATA +137 -0
- quantumgui-1.0.0.dist-info/RECORD +73 -0
- quantumgui-1.0.0.dist-info/WHEEL +5 -0
- quantumgui-1.0.0.dist-info/entry_points.txt +2 -0
- quantumgui-1.0.0.dist-info/licenses/LICENSE +32 -0
- quantumgui-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
"""Material Design 3 GPU-accelerated Charts & KPI Cards (Skia native, zero matplotlib)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
from typing import Literal, Sequence
|
|
7
|
+
|
|
8
|
+
import skia
|
|
9
|
+
|
|
10
|
+
from ..core.draw import (
|
|
11
|
+
draw_elevation_shadow,
|
|
12
|
+
draw_icon,
|
|
13
|
+
draw_text_centered_y,
|
|
14
|
+
font_for_style,
|
|
15
|
+
make_rrect,
|
|
16
|
+
make_rrect_spec,
|
|
17
|
+
measure_text,
|
|
18
|
+
resolve_color,
|
|
19
|
+
)
|
|
20
|
+
from ..core.events import MouseEvent
|
|
21
|
+
from ..core.geometry import Constraints, Rect, Size
|
|
22
|
+
from ..core.widget import Widget
|
|
23
|
+
from ..theme import Color, Theme
|
|
24
|
+
|
|
25
|
+
ChartColorRole = Literal["primary", "secondary", "tertiary", "error"]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _resolve_accent(theme: Theme, role: ChartColorRole | Color) -> Color:
|
|
29
|
+
if isinstance(role, Color):
|
|
30
|
+
return role
|
|
31
|
+
return getattr(theme.colors, role)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _format_num(v: float) -> str:
|
|
35
|
+
if abs(v) >= 1_000_000:
|
|
36
|
+
return f"{v / 1_000_000:.1f}M"
|
|
37
|
+
if abs(v) >= 10_000:
|
|
38
|
+
return f"{v / 1_000:.1f}k"
|
|
39
|
+
if abs(v - round(v)) < 1e-4:
|
|
40
|
+
return f"{int(round(v)):,}"
|
|
41
|
+
return f"{v:,.1f}"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _draw_chart_tooltip(
|
|
45
|
+
canvas: skia.Canvas,
|
|
46
|
+
theme: Theme,
|
|
47
|
+
anchor_x: float,
|
|
48
|
+
anchor_y: float,
|
|
49
|
+
text: str,
|
|
50
|
+
bounds: Rect,
|
|
51
|
+
) -> None:
|
|
52
|
+
c = theme.colors
|
|
53
|
+
font = font_for_style(theme.font_family, theme.typography.label_medium)
|
|
54
|
+
tw = measure_text(font, text)
|
|
55
|
+
bw = tw + 18.0
|
|
56
|
+
bh = 28.0
|
|
57
|
+
bx = max(bounds.x + 4.0, min(bounds.right - bw - 4.0, anchor_x - bw * 0.5))
|
|
58
|
+
by = anchor_y - bh - 10.0
|
|
59
|
+
if by < bounds.y + 4.0:
|
|
60
|
+
by = anchor_y + 12.0
|
|
61
|
+
trect = Rect(bx, by, bw, bh)
|
|
62
|
+
draw_elevation_shadow(canvas, trect, 6.0, 2, c.shadow)
|
|
63
|
+
canvas.drawRRect(make_rrect(trect, 6.0), skia.Paint(AntiAlias=True, Color=c.inverse_surface.to_skia()))
|
|
64
|
+
draw_text_centered_y(canvas, text, trect.x + 9.0, trect.center_y, font, c.inverse_on_surface)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class KPICard(Widget):
|
|
68
|
+
"""Material Design 3 Metric / KPI Card with trend pill and optional sparkline."""
|
|
69
|
+
|
|
70
|
+
manages_own_box = True
|
|
71
|
+
|
|
72
|
+
def __init__(
|
|
73
|
+
self,
|
|
74
|
+
title: str,
|
|
75
|
+
value: str,
|
|
76
|
+
*,
|
|
77
|
+
delta: str | None = None,
|
|
78
|
+
positive: bool = True,
|
|
79
|
+
sparkline: Sequence[float] | None = None,
|
|
80
|
+
icon: str | None = None,
|
|
81
|
+
variant: Literal["elevated", "filled", "outlined"] = "elevated",
|
|
82
|
+
) -> None:
|
|
83
|
+
super().__init__()
|
|
84
|
+
self.title = title
|
|
85
|
+
self.value = value
|
|
86
|
+
self.delta = delta
|
|
87
|
+
self.positive = positive
|
|
88
|
+
self.sparkline: list[float] = [float(v) for v in sparkline] if sparkline else []
|
|
89
|
+
self.icon = icon
|
|
90
|
+
self.variant = variant
|
|
91
|
+
|
|
92
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
93
|
+
h = 136.0 if self.sparkline else 104.0
|
|
94
|
+
w = 220.0 if constraints.max_width == float("inf") else min(240.0, constraints.max_width)
|
|
95
|
+
return Size(w, h)
|
|
96
|
+
|
|
97
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
98
|
+
c = theme.colors
|
|
99
|
+
r = self.rect
|
|
100
|
+
rad_spec = self._shape_override if self._shape_override is not None else "large"
|
|
101
|
+
rad = rad_spec[0] if isinstance(rad_spec, tuple) else theme.shape.resolve(rad_spec)
|
|
102
|
+
rrect = make_rrect_spec(r, rad_spec, theme)
|
|
103
|
+
|
|
104
|
+
elev = self._elevation_override if self._elevation_override is not None else (
|
|
105
|
+
1 if self.variant == "elevated" else 0
|
|
106
|
+
)
|
|
107
|
+
if elev > 0:
|
|
108
|
+
sh = resolve_color(self._shadow_color_override, theme, c.shadow)
|
|
109
|
+
assert sh is not None
|
|
110
|
+
draw_elevation_shadow(canvas, r, rad, elev, sh)
|
|
111
|
+
|
|
112
|
+
if self._bg_override is not None:
|
|
113
|
+
bg = resolve_color(self._bg_override, theme, c.surface_container_low) or c.surface_container_low
|
|
114
|
+
elif self.variant == "elevated":
|
|
115
|
+
bg = c.surface_container_low
|
|
116
|
+
elif self.variant == "filled":
|
|
117
|
+
bg = c.surface_container_highest
|
|
118
|
+
else:
|
|
119
|
+
bg = c.surface
|
|
120
|
+
|
|
121
|
+
canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
|
|
122
|
+
bc = resolve_color(self._border_color_override, theme)
|
|
123
|
+
if bc is None and self.variant == "outlined":
|
|
124
|
+
bc = c.outline_variant
|
|
125
|
+
if bc is not None:
|
|
126
|
+
bw = self._border_width_override if self._border_width_override is not None else 1.0
|
|
127
|
+
canvas.drawRRect(
|
|
128
|
+
rrect,
|
|
129
|
+
skia.Paint(AntiAlias=True, Color=bc.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw),
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
# Top row: title + optional icon
|
|
133
|
+
tfont = font_for_style(theme.font_family, theme.typography.label_large)
|
|
134
|
+
draw_text_centered_y(canvas, self.title, r.x + 18.0, r.y + 24.0, tfont, c.on_surface_variant)
|
|
135
|
+
if self.icon:
|
|
136
|
+
canvas.drawCircle(r.right - 28.0, r.y + 26.0, 15.0, skia.Paint(AntiAlias=True, Color=c.primary_container.to_skia()))
|
|
137
|
+
draw_icon(canvas, self.icon, r.right - 28.0, r.y + 26.0, 16.0, c.on_primary_container)
|
|
138
|
+
|
|
139
|
+
# Main big metric
|
|
140
|
+
vfont = font_for_style(theme.font_family, theme.typography.headline_medium)
|
|
141
|
+
draw_text_centered_y(canvas, self.value, r.x + 18.0, r.y + 62.0, vfont, c.on_surface)
|
|
142
|
+
|
|
143
|
+
# Delta pill
|
|
144
|
+
if self.delta:
|
|
145
|
+
dfont = font_for_style(theme.font_family, theme.typography.label_small)
|
|
146
|
+
vw = measure_text(vfont, self.value)
|
|
147
|
+
dw = measure_text(dfont, self.delta)
|
|
148
|
+
pill_w = dw + 14.0
|
|
149
|
+
pill_h = 22.0
|
|
150
|
+
px = min(r.right - pill_w - 14.0, r.x + 18.0 + vw + 10.0)
|
|
151
|
+
py = r.y + 52.0
|
|
152
|
+
pbg = c.primary_container if self.positive else c.error_container
|
|
153
|
+
pfg = c.on_primary_container if self.positive else c.on_error_container
|
|
154
|
+
prect = Rect(px, py, pill_w, pill_h)
|
|
155
|
+
canvas.drawRRect(make_rrect(prect, 11.0), skia.Paint(AntiAlias=True, Color=pbg.to_skia()))
|
|
156
|
+
draw_text_centered_y(canvas, self.delta, prect.center_x - dw * 0.5, prect.center_y, dfont, pfg)
|
|
157
|
+
|
|
158
|
+
# Mini sparkline
|
|
159
|
+
if len(self.sparkline) >= 2:
|
|
160
|
+
sx = r.x + 18.0
|
|
161
|
+
sy = r.y + 88.0
|
|
162
|
+
sw = max(20.0, r.width - 36.0)
|
|
163
|
+
sh = max(16.0, r.bottom - sy - 14.0)
|
|
164
|
+
lo, hi = min(self.sparkline), max(self.sparkline)
|
|
165
|
+
span = (hi - lo) if hi > lo else 1.0
|
|
166
|
+
pts = [
|
|
167
|
+
(
|
|
168
|
+
sx + (i / (len(self.sparkline) - 1)) * sw,
|
|
169
|
+
sy + sh - ((v - lo) / span) * sh,
|
|
170
|
+
)
|
|
171
|
+
for i, v in enumerate(self.sparkline)
|
|
172
|
+
]
|
|
173
|
+
line_color = c.primary if self.positive else c.error
|
|
174
|
+
path = skia.Path()
|
|
175
|
+
path.moveTo(pts[0][0], pts[0][1])
|
|
176
|
+
for x, y in pts[1:]:
|
|
177
|
+
path.lineTo(x, y)
|
|
178
|
+
|
|
179
|
+
area = skia.Path(path)
|
|
180
|
+
area.lineTo(pts[-1][0], sy + sh)
|
|
181
|
+
area.lineTo(pts[0][0], sy + sh)
|
|
182
|
+
area.close()
|
|
183
|
+
grad = skia.GradientShader.MakeLinear(
|
|
184
|
+
[(0.0, sy), (0.0, sy + sh)],
|
|
185
|
+
[line_color.with_alpha(0.24).to_skia(), line_color.with_alpha(0.0).to_skia()],
|
|
186
|
+
)
|
|
187
|
+
canvas.drawPath(area, skia.Paint(AntiAlias=True, Shader=grad))
|
|
188
|
+
canvas.drawPath(
|
|
189
|
+
path,
|
|
190
|
+
skia.Paint(
|
|
191
|
+
AntiAlias=True,
|
|
192
|
+
Color=line_color.to_skia(),
|
|
193
|
+
Style=skia.Paint.kStroke_Style,
|
|
194
|
+
StrokeWidth=2.0,
|
|
195
|
+
StrokeCap=skia.Paint.kRound_Cap,
|
|
196
|
+
StrokeJoin=skia.Paint.kRound_Join,
|
|
197
|
+
),
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class LineChart(Widget):
|
|
202
|
+
"""Material Design 3 GPU LineChart with smooth curve, gradient fill, and live hover tooltip."""
|
|
203
|
+
|
|
204
|
+
interactive = True
|
|
205
|
+
cursor = "pointer"
|
|
206
|
+
|
|
207
|
+
def __init__(
|
|
208
|
+
self,
|
|
209
|
+
labels: Sequence[str],
|
|
210
|
+
values: Sequence[float],
|
|
211
|
+
*,
|
|
212
|
+
title: str | None = None,
|
|
213
|
+
color: ChartColorRole | Color = "primary",
|
|
214
|
+
smooth: bool = True,
|
|
215
|
+
area: bool = True,
|
|
216
|
+
show_points: bool = True,
|
|
217
|
+
chart_height: float = 230.0,
|
|
218
|
+
) -> None:
|
|
219
|
+
super().__init__()
|
|
220
|
+
self.labels = [str(x) for x in labels]
|
|
221
|
+
self.values = [float(v) for v in values]
|
|
222
|
+
self.title = title
|
|
223
|
+
self.color: ChartColorRole | Color = color
|
|
224
|
+
self.smooth = smooth
|
|
225
|
+
self.area = area
|
|
226
|
+
self.show_points = show_points
|
|
227
|
+
self.chart_height = float(chart_height)
|
|
228
|
+
self._hover_idx: int = -1
|
|
229
|
+
self._plot_rect = Rect(0.0, 0.0, 0.0, 0.0)
|
|
230
|
+
self._points: list[tuple[float, float]] = []
|
|
231
|
+
self._fill_width = True
|
|
232
|
+
|
|
233
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
234
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 420.0
|
|
235
|
+
return Size(w, self.chart_height)
|
|
236
|
+
|
|
237
|
+
def on_mouse_move(self, event: MouseEvent) -> None:
|
|
238
|
+
if not self._points:
|
|
239
|
+
return
|
|
240
|
+
best = -1
|
|
241
|
+
best_dx = float("inf")
|
|
242
|
+
for i, (px, _py) in enumerate(self._points):
|
|
243
|
+
dx = abs(event.x - px)
|
|
244
|
+
if dx < best_dx:
|
|
245
|
+
best_dx = dx
|
|
246
|
+
best = i
|
|
247
|
+
if best != self._hover_idx:
|
|
248
|
+
self._hover_idx = best
|
|
249
|
+
self.invalidate()
|
|
250
|
+
|
|
251
|
+
def on_mouse_leave(self) -> None:
|
|
252
|
+
super().on_mouse_leave()
|
|
253
|
+
self._hover_idx = -1
|
|
254
|
+
|
|
255
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
256
|
+
c = theme.colors
|
|
257
|
+
r = self.rect
|
|
258
|
+
accent = _resolve_accent(theme, self.color)
|
|
259
|
+
top_pad = 36.0 if self.title else 14.0
|
|
260
|
+
left_pad = 44.0
|
|
261
|
+
right_pad = 16.0
|
|
262
|
+
bottom_pad = 28.0
|
|
263
|
+
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))
|
|
264
|
+
self._plot_rect = plot
|
|
265
|
+
|
|
266
|
+
if self.title:
|
|
267
|
+
tfont = font_for_style(theme.font_family, theme.typography.title_medium)
|
|
268
|
+
draw_text_centered_y(canvas, self.title, r.x + 4.0, r.y + 14.0, tfont, c.on_surface)
|
|
269
|
+
|
|
270
|
+
if not self.values:
|
|
271
|
+
return
|
|
272
|
+
|
|
273
|
+
lo = min(0.0, min(self.values))
|
|
274
|
+
hi = max(self.values)
|
|
275
|
+
if hi <= lo:
|
|
276
|
+
hi = lo + 1.0
|
|
277
|
+
hi *= 1.12
|
|
278
|
+
|
|
279
|
+
# Horizontal gridlines + Y labels
|
|
280
|
+
lbl_font = font_for_style(theme.font_family, theme.typography.label_small)
|
|
281
|
+
grid_paint = skia.Paint(AntiAlias=True, Color=c.outline_variant.with_alpha(0.55).to_skia(), StrokeWidth=1.0)
|
|
282
|
+
for step in range(5):
|
|
283
|
+
frac = step / 4.0
|
|
284
|
+
gy = plot.bottom - frac * plot.height
|
|
285
|
+
canvas.drawLine(plot.x, gy, plot.right, gy, grid_paint)
|
|
286
|
+
val_txt = _format_num(lo + frac * (hi - lo))
|
|
287
|
+
tw = measure_text(lbl_font, val_txt)
|
|
288
|
+
draw_text_centered_y(canvas, val_txt, plot.x - tw - 8.0, gy, lbl_font, c.on_surface_variant)
|
|
289
|
+
|
|
290
|
+
n = len(self.values)
|
|
291
|
+
pts: list[tuple[float, float]] = []
|
|
292
|
+
for i, v in enumerate(self.values):
|
|
293
|
+
px = plot.center_x if n == 1 else plot.x + (i / (n - 1)) * plot.width
|
|
294
|
+
py = plot.bottom - ((v - lo) / (hi - lo)) * plot.height
|
|
295
|
+
pts.append((px, py))
|
|
296
|
+
self._points = pts
|
|
297
|
+
|
|
298
|
+
# X labels
|
|
299
|
+
for i, (px, _py) in enumerate(pts):
|
|
300
|
+
if i < len(self.labels):
|
|
301
|
+
lw = measure_text(lbl_font, self.labels[i])
|
|
302
|
+
draw_text_centered_y(canvas, self.labels[i], px - lw * 0.5, plot.bottom + 15.0, lbl_font, c.on_surface_variant)
|
|
303
|
+
|
|
304
|
+
# Build curve path
|
|
305
|
+
path = skia.Path()
|
|
306
|
+
path.moveTo(pts[0][0], pts[0][1])
|
|
307
|
+
if self.smooth and len(pts) >= 3:
|
|
308
|
+
for i in range(len(pts) - 1):
|
|
309
|
+
x0, y0 = pts[i]
|
|
310
|
+
x1, y1 = pts[i + 1]
|
|
311
|
+
cx = (x0 + x1) * 0.5
|
|
312
|
+
path.cubicTo(cx, y0, cx, y1, x1, y1)
|
|
313
|
+
else:
|
|
314
|
+
for px, py in pts[1:]:
|
|
315
|
+
path.lineTo(px, py)
|
|
316
|
+
|
|
317
|
+
if self.area and len(pts) >= 2:
|
|
318
|
+
area_path = skia.Path(path)
|
|
319
|
+
area_path.lineTo(pts[-1][0], plot.bottom)
|
|
320
|
+
area_path.lineTo(pts[0][0], plot.bottom)
|
|
321
|
+
area_path.close()
|
|
322
|
+
grad = skia.GradientShader.MakeLinear(
|
|
323
|
+
[(0.0, plot.y), (0.0, plot.bottom)],
|
|
324
|
+
[accent.with_alpha(0.26).to_skia(), accent.with_alpha(0.0).to_skia()],
|
|
325
|
+
)
|
|
326
|
+
canvas.drawPath(area_path, skia.Paint(AntiAlias=True, Shader=grad))
|
|
327
|
+
|
|
328
|
+
canvas.drawPath(
|
|
329
|
+
path,
|
|
330
|
+
skia.Paint(
|
|
331
|
+
AntiAlias=True,
|
|
332
|
+
Color=accent.to_skia(),
|
|
333
|
+
Style=skia.Paint.kStroke_Style,
|
|
334
|
+
StrokeWidth=2.6,
|
|
335
|
+
StrokeCap=skia.Paint.kRound_Cap,
|
|
336
|
+
StrokeJoin=skia.Paint.kRound_Join,
|
|
337
|
+
),
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
# Points
|
|
341
|
+
if self.show_points:
|
|
342
|
+
for i, (px, py) in enumerate(pts):
|
|
343
|
+
if i == self._hover_idx:
|
|
344
|
+
continue
|
|
345
|
+
canvas.drawCircle(px, py, 3.5, skia.Paint(AntiAlias=True, Color=accent.to_skia()))
|
|
346
|
+
|
|
347
|
+
# Interactive hover crosshair + ring + live tooltip
|
|
348
|
+
if 0 <= self._hover_idx < len(pts):
|
|
349
|
+
hx, hy = pts[self._hover_idx]
|
|
350
|
+
canvas.drawLine(
|
|
351
|
+
hx, plot.y, hx, plot.bottom,
|
|
352
|
+
skia.Paint(AntiAlias=True, Color=accent.with_alpha(0.35).to_skia(), StrokeWidth=1.2),
|
|
353
|
+
)
|
|
354
|
+
canvas.drawCircle(hx, hy, 8.0, skia.Paint(AntiAlias=True, Color=accent.with_alpha(0.22).to_skia()))
|
|
355
|
+
canvas.drawCircle(hx, hy, 4.5, skia.Paint(AntiAlias=True, Color=c.surface.to_skia()))
|
|
356
|
+
canvas.drawCircle(
|
|
357
|
+
hx, hy, 4.5,
|
|
358
|
+
skia.Paint(AntiAlias=True, Color=accent.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=2.5),
|
|
359
|
+
)
|
|
360
|
+
lbl = self.labels[self._hover_idx] if self._hover_idx < len(self.labels) else f"#{self._hover_idx + 1}"
|
|
361
|
+
_draw_chart_tooltip(canvas, theme, hx, hy, f"{lbl}: {_format_num(self.values[self._hover_idx])}", r)
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
class BarChart(Widget):
|
|
365
|
+
"""Material Design 3 GPU BarChart with rounded-top bars and live hover tooltip."""
|
|
366
|
+
|
|
367
|
+
interactive = True
|
|
368
|
+
cursor = "pointer"
|
|
369
|
+
|
|
370
|
+
def __init__(
|
|
371
|
+
self,
|
|
372
|
+
labels: Sequence[str],
|
|
373
|
+
values: Sequence[float],
|
|
374
|
+
*,
|
|
375
|
+
title: str | None = None,
|
|
376
|
+
color: ChartColorRole | Color = "primary",
|
|
377
|
+
chart_height: float = 230.0,
|
|
378
|
+
) -> None:
|
|
379
|
+
super().__init__()
|
|
380
|
+
self.labels = [str(x) for x in labels]
|
|
381
|
+
self.values = [float(v) for v in values]
|
|
382
|
+
self.title = title
|
|
383
|
+
self.color: ChartColorRole | Color = color
|
|
384
|
+
self.chart_height = float(chart_height)
|
|
385
|
+
self._hover_idx: int = -1
|
|
386
|
+
self._bar_rects: list[Rect] = []
|
|
387
|
+
self._fill_width = True
|
|
388
|
+
|
|
389
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
390
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 420.0
|
|
391
|
+
return Size(w, self.chart_height)
|
|
392
|
+
|
|
393
|
+
def on_mouse_move(self, event: MouseEvent) -> None:
|
|
394
|
+
found = -1
|
|
395
|
+
for i, br in enumerate(self._bar_rects):
|
|
396
|
+
if br.x - 6.0 <= event.x <= br.right + 6.0 and self.rect.y <= event.y <= self.rect.bottom:
|
|
397
|
+
found = i
|
|
398
|
+
break
|
|
399
|
+
if found != self._hover_idx:
|
|
400
|
+
self._hover_idx = found
|
|
401
|
+
self.invalidate()
|
|
402
|
+
|
|
403
|
+
def on_mouse_leave(self) -> None:
|
|
404
|
+
super().on_mouse_leave()
|
|
405
|
+
self._hover_idx = -1
|
|
406
|
+
|
|
407
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
408
|
+
c = theme.colors
|
|
409
|
+
r = self.rect
|
|
410
|
+
accent = _resolve_accent(theme, self.color)
|
|
411
|
+
top_pad = 36.0 if self.title else 14.0
|
|
412
|
+
left_pad = 44.0
|
|
413
|
+
right_pad = 16.0
|
|
414
|
+
bottom_pad = 28.0
|
|
415
|
+
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))
|
|
416
|
+
|
|
417
|
+
if self.title:
|
|
418
|
+
tfont = font_for_style(theme.font_family, theme.typography.title_medium)
|
|
419
|
+
draw_text_centered_y(canvas, self.title, r.x + 4.0, r.y + 14.0, tfont, c.on_surface)
|
|
420
|
+
|
|
421
|
+
if not self.values:
|
|
422
|
+
return
|
|
423
|
+
|
|
424
|
+
hi = max(1.0, max(self.values) * 1.15)
|
|
425
|
+
lbl_font = font_for_style(theme.font_family, theme.typography.label_small)
|
|
426
|
+
grid_paint = skia.Paint(AntiAlias=True, Color=c.outline_variant.with_alpha(0.55).to_skia(), StrokeWidth=1.0)
|
|
427
|
+
for step in range(5):
|
|
428
|
+
frac = step / 4.0
|
|
429
|
+
gy = plot.bottom - frac * plot.height
|
|
430
|
+
canvas.drawLine(plot.x, gy, plot.right, gy, grid_paint)
|
|
431
|
+
val_txt = _format_num(frac * hi)
|
|
432
|
+
tw = measure_text(lbl_font, val_txt)
|
|
433
|
+
draw_text_centered_y(canvas, val_txt, plot.x - tw - 8.0, gy, lbl_font, c.on_surface_variant)
|
|
434
|
+
|
|
435
|
+
n = len(self.values)
|
|
436
|
+
slot_w = plot.width / n
|
|
437
|
+
bar_w = max(10.0, min(42.0, slot_w * 0.56))
|
|
438
|
+
self._bar_rects = []
|
|
439
|
+
|
|
440
|
+
for i, v in enumerate(self.values):
|
|
441
|
+
bh = max(2.0, (max(0.0, v) / hi) * plot.height)
|
|
442
|
+
bx = plot.x + i * slot_w + (slot_w - bar_w) * 0.5
|
|
443
|
+
by = plot.bottom - bh
|
|
444
|
+
br = Rect(bx, by, bar_w, bh)
|
|
445
|
+
self._bar_rects.append(br)
|
|
446
|
+
|
|
447
|
+
is_hov = i == self._hover_idx
|
|
448
|
+
fill_c = accent if (self._hover_idx == -1 or is_hov) else accent.with_alpha(0.42)
|
|
449
|
+
rr = skia.RRect()
|
|
450
|
+
rad = min(8.0, bar_w * 0.5, bh)
|
|
451
|
+
rr.setRectRadii(
|
|
452
|
+
skia.Rect.MakeXYWH(br.x, br.y, br.width, br.height),
|
|
453
|
+
[(rad, rad), (rad, rad), (0.0, 0.0), (0.0, 0.0)],
|
|
454
|
+
)
|
|
455
|
+
canvas.drawRRect(rr, skia.Paint(AntiAlias=True, Color=fill_c.to_skia()))
|
|
456
|
+
|
|
457
|
+
if i < len(self.labels):
|
|
458
|
+
lw = measure_text(lbl_font, self.labels[i])
|
|
459
|
+
draw_text_centered_y(canvas, self.labels[i], br.center_x - lw * 0.5, plot.bottom + 15.0, lbl_font, c.on_surface_variant)
|
|
460
|
+
|
|
461
|
+
if 0 <= self._hover_idx < len(self._bar_rects):
|
|
462
|
+
br = self._bar_rects[self._hover_idx]
|
|
463
|
+
lbl = self.labels[self._hover_idx] if self._hover_idx < len(self.labels) else f"#{self._hover_idx + 1}"
|
|
464
|
+
_draw_chart_tooltip(canvas, theme, br.center_x, br.y, f"{lbl}: {_format_num(self.values[self._hover_idx])}", r)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
class DonutChart(Widget):
|
|
468
|
+
"""Material Design 3 GPU DonutChart with M3 tonal slices, legend, and live hover tooltip."""
|
|
469
|
+
|
|
470
|
+
interactive = True
|
|
471
|
+
cursor = "pointer"
|
|
472
|
+
|
|
473
|
+
def __init__(
|
|
474
|
+
self,
|
|
475
|
+
items: Sequence[tuple[str, float]],
|
|
476
|
+
*,
|
|
477
|
+
title: str | None = None,
|
|
478
|
+
center_label: str | None = None,
|
|
479
|
+
thickness: float = 26.0,
|
|
480
|
+
chart_height: float = 230.0,
|
|
481
|
+
) -> None:
|
|
482
|
+
super().__init__()
|
|
483
|
+
self.items: list[tuple[str, float]] = [(str(k), max(0.0, float(v))) for k, v in items]
|
|
484
|
+
self.title = title
|
|
485
|
+
self.center_label = center_label
|
|
486
|
+
self.thickness = float(thickness)
|
|
487
|
+
self.chart_height = float(chart_height)
|
|
488
|
+
self._hover_idx: int = -1
|
|
489
|
+
self._center: tuple[float, float] = (0.0, 0.0)
|
|
490
|
+
self._outer_r: float = 0.0
|
|
491
|
+
self._slices: list[tuple[float, float]] = [] # (start_deg, sweep_deg)
|
|
492
|
+
self._fill_width = True
|
|
493
|
+
|
|
494
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
495
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 360.0
|
|
496
|
+
return Size(w, self.chart_height)
|
|
497
|
+
|
|
498
|
+
def _slice_palette(self, theme: Theme) -> list[Color]:
|
|
499
|
+
c = theme.colors
|
|
500
|
+
return [
|
|
501
|
+
c.primary,
|
|
502
|
+
c.tertiary,
|
|
503
|
+
c.secondary,
|
|
504
|
+
c.primary_container,
|
|
505
|
+
c.tertiary_container,
|
|
506
|
+
c.secondary_container,
|
|
507
|
+
]
|
|
508
|
+
|
|
509
|
+
def on_mouse_move(self, event: MouseEvent) -> None:
|
|
510
|
+
cx, cy = self._center
|
|
511
|
+
dx, dy = event.x - cx, event.y - cy
|
|
512
|
+
dist = math.hypot(dx, dy)
|
|
513
|
+
found = -1
|
|
514
|
+
if (self._outer_r - self.thickness - 8.0) <= dist <= (self._outer_r + 8.0):
|
|
515
|
+
ang = math.degrees(math.atan2(dy, dx))
|
|
516
|
+
# Normalize relative to -90 deg (12 o'clock) in [0, 360)
|
|
517
|
+
rel = (ang + 90.0) % 360.0
|
|
518
|
+
for i, (start, sweep) in enumerate(self._slices):
|
|
519
|
+
s = (start + 90.0) % 360.0
|
|
520
|
+
if s <= rel <= s + sweep:
|
|
521
|
+
found = i
|
|
522
|
+
break
|
|
523
|
+
if found != self._hover_idx:
|
|
524
|
+
self._hover_idx = found
|
|
525
|
+
self.invalidate()
|
|
526
|
+
|
|
527
|
+
def on_mouse_leave(self) -> None:
|
|
528
|
+
super().on_mouse_leave()
|
|
529
|
+
self._hover_idx = -1
|
|
530
|
+
|
|
531
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
532
|
+
c = theme.colors
|
|
533
|
+
r = self.rect
|
|
534
|
+
top_pad = 36.0 if self.title else 10.0
|
|
535
|
+
|
|
536
|
+
if self.title:
|
|
537
|
+
tfont = font_for_style(theme.font_family, theme.typography.title_medium)
|
|
538
|
+
draw_text_centered_y(canvas, self.title, r.x + 4.0, r.y + 14.0, tfont, c.on_surface)
|
|
539
|
+
|
|
540
|
+
total = sum(v for _, v in self.items)
|
|
541
|
+
if total <= 0:
|
|
542
|
+
return
|
|
543
|
+
|
|
544
|
+
avail_h = max(80.0, r.height - top_pad - 12.0)
|
|
545
|
+
radius = min(avail_h * 0.44, r.width * 0.28)
|
|
546
|
+
cx = r.x + radius + 28.0
|
|
547
|
+
cy = r.y + top_pad + avail_h * 0.5
|
|
548
|
+
self._center = (cx, cy)
|
|
549
|
+
self._outer_r = radius + self.thickness * 0.5
|
|
550
|
+
|
|
551
|
+
palette = self._slice_palette(theme)
|
|
552
|
+
self._slices = []
|
|
553
|
+
start_deg = -90.0
|
|
554
|
+
for i, (label, val) in enumerate(self.items):
|
|
555
|
+
sweep = (val / total) * 360.0
|
|
556
|
+
self._slices.append((start_deg, sweep))
|
|
557
|
+
is_hov = i == self._hover_idx
|
|
558
|
+
col = palette[i % len(palette)]
|
|
559
|
+
if self._hover_idx != -1 and not is_hov:
|
|
560
|
+
col = col.with_alpha(0.45)
|
|
561
|
+
|
|
562
|
+
rad_i = radius + (4.0 if is_hov else 0.0)
|
|
563
|
+
oval = skia.Rect.MakeXYWH(cx - rad_i, cy - rad_i, rad_i * 2.0, rad_i * 2.0)
|
|
564
|
+
gap = 2.0 if len(self.items) > 1 and sweep > 4.0 else 0.0
|
|
565
|
+
canvas.drawArc(
|
|
566
|
+
oval,
|
|
567
|
+
start_deg + gap * 0.5,
|
|
568
|
+
max(0.5, sweep - gap),
|
|
569
|
+
False,
|
|
570
|
+
skia.Paint(
|
|
571
|
+
AntiAlias=True,
|
|
572
|
+
Color=col.to_skia(),
|
|
573
|
+
Style=skia.Paint.kStroke_Style,
|
|
574
|
+
StrokeWidth=self.thickness + (3.0 if is_hov else 0.0),
|
|
575
|
+
StrokeCap=skia.Paint.kButt_Cap,
|
|
576
|
+
),
|
|
577
|
+
)
|
|
578
|
+
start_deg += sweep
|
|
579
|
+
|
|
580
|
+
# Center readout (updates live on hover!)
|
|
581
|
+
val_font = font_for_style(theme.font_family, theme.typography.title_large)
|
|
582
|
+
sub_font = font_for_style(theme.font_family, theme.typography.label_small)
|
|
583
|
+
if 0 <= self._hover_idx < len(self.items):
|
|
584
|
+
hlbl, hval = self.items[self._hover_idx]
|
|
585
|
+
pct = f"{(hval / total) * 100.0:.1f}%"
|
|
586
|
+
pw = measure_text(val_font, pct)
|
|
587
|
+
sw = measure_text(sub_font, hlbl)
|
|
588
|
+
draw_text_centered_y(canvas, pct, cx - pw * 0.5, cy - 7.0, val_font, c.on_surface)
|
|
589
|
+
draw_text_centered_y(canvas, hlbl, cx - sw * 0.5, cy + 13.0, sub_font, c.on_surface_variant)
|
|
590
|
+
else:
|
|
591
|
+
tot_txt = _format_num(total)
|
|
592
|
+
tw = measure_text(val_font, tot_txt)
|
|
593
|
+
draw_text_centered_y(canvas, tot_txt, cx - tw * 0.5, cy - (7.0 if self.center_label else 0.0), val_font, c.on_surface)
|
|
594
|
+
if self.center_label:
|
|
595
|
+
sw = measure_text(sub_font, self.center_label)
|
|
596
|
+
draw_text_centered_y(canvas, self.center_label, cx - sw * 0.5, cy + 13.0, sub_font, c.on_surface_variant)
|
|
597
|
+
|
|
598
|
+
# Right-side M3 legend
|
|
599
|
+
leg_x = cx + radius + self.thickness * 0.5 + 28.0
|
|
600
|
+
leg_y = cy - (len(self.items) * 24.0) * 0.5 + 12.0
|
|
601
|
+
leg_font = font_for_style(theme.font_family, theme.typography.body_small)
|
|
602
|
+
if leg_x + 70.0 < r.right:
|
|
603
|
+
for i, (label, val) in enumerate(self.items):
|
|
604
|
+
ly = leg_y + i * 24.0
|
|
605
|
+
col = palette[i % len(palette)]
|
|
606
|
+
canvas.drawCircle(leg_x + 5.0, ly, 5.0, skia.Paint(AntiAlias=True, Color=col.to_skia()))
|
|
607
|
+
pct = (val / total) * 100.0
|
|
608
|
+
txt = f"{label} ({pct:.0f}%)"
|
|
609
|
+
draw_text_centered_y(
|
|
610
|
+
canvas,
|
|
611
|
+
txt,
|
|
612
|
+
leg_x + 16.0,
|
|
613
|
+
ly,
|
|
614
|
+
leg_font,
|
|
615
|
+
c.on_surface if i == self._hover_idx else c.on_surface_variant,
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
# Live tooltip near hovered slice midpoint
|
|
619
|
+
if 0 <= self._hover_idx < len(self._slices):
|
|
620
|
+
start, sweep = self._slices[self._hover_idx]
|
|
621
|
+
mid_rad = math.radians(start + sweep * 0.5)
|
|
622
|
+
tx = cx + math.cos(mid_rad) * (radius + self.thickness * 0.5)
|
|
623
|
+
ty = cy + math.sin(mid_rad) * (radius + self.thickness * 0.5)
|
|
624
|
+
hlbl, hval = self.items[self._hover_idx]
|
|
625
|
+
_draw_chart_tooltip(canvas, theme, tx, ty, f"{hlbl}: {_format_num(hval)} ({(hval / total) * 100.0:.1f}%)", r)
|