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,528 @@
|
|
|
1
|
+
"""Phase 11 Advanced BI Charts (100% Skia GPU native):
|
|
2
|
+
TreemapChart, SankeyChart, WaterfallChart, BoxPlotChart, and inline Sparkline.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import Sequence
|
|
8
|
+
|
|
9
|
+
import skia
|
|
10
|
+
|
|
11
|
+
from ..core.draw import (
|
|
12
|
+
draw_text_centered_y,
|
|
13
|
+
font_for_style,
|
|
14
|
+
make_rrect,
|
|
15
|
+
make_rrect_spec,
|
|
16
|
+
measure_text,
|
|
17
|
+
resolve_color,
|
|
18
|
+
)
|
|
19
|
+
from ..core.events import MouseEvent
|
|
20
|
+
from ..core.geometry import Constraints, Rect, Size
|
|
21
|
+
from ..core.widget import Widget
|
|
22
|
+
from ..theme import Color, Theme
|
|
23
|
+
from .charts import ChartColorRole, _resolve_accent
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _bi_palette(theme: Theme) -> list[Color]:
|
|
27
|
+
c = theme.colors
|
|
28
|
+
return [
|
|
29
|
+
c.primary,
|
|
30
|
+
c.tertiary,
|
|
31
|
+
c.secondary,
|
|
32
|
+
c.primary.blend(c.tertiary, 0.5),
|
|
33
|
+
c.secondary.blend(c.primary, 0.5),
|
|
34
|
+
c.error,
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# ---------------------------------------------------------------------------
|
|
39
|
+
# 1. TreemapChart
|
|
40
|
+
# ---------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class TreemapChart(Widget):
|
|
44
|
+
"""Proportional Treemap chart rendered in Skia GPU with hover highlight."""
|
|
45
|
+
|
|
46
|
+
interactive = True
|
|
47
|
+
manages_own_box = True
|
|
48
|
+
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
items: Sequence[tuple[str, float]],
|
|
52
|
+
*,
|
|
53
|
+
title: str | None = None,
|
|
54
|
+
chart_height: float = 230.0,
|
|
55
|
+
) -> None:
|
|
56
|
+
super().__init__()
|
|
57
|
+
self.items: list[tuple[str, float]] = [(str(k), max(0.0, float(v))) for k, v in items]
|
|
58
|
+
self.title = title
|
|
59
|
+
self.chart_height = float(chart_height)
|
|
60
|
+
self._rects: list[tuple[int, Rect]] = []
|
|
61
|
+
self._hover_idx: int = -1
|
|
62
|
+
self._fill_width = True
|
|
63
|
+
|
|
64
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
65
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 460.0
|
|
66
|
+
return Size(w, self.chart_height)
|
|
67
|
+
|
|
68
|
+
def on_mouse_move(self, event: MouseEvent) -> None:
|
|
69
|
+
hit = -1
|
|
70
|
+
for idx, rr in self._rects:
|
|
71
|
+
if rr.contains(event.x, event.y):
|
|
72
|
+
hit = idx
|
|
73
|
+
break
|
|
74
|
+
if hit != self._hover_idx:
|
|
75
|
+
self._hover_idx = hit
|
|
76
|
+
self.invalidate()
|
|
77
|
+
|
|
78
|
+
def on_mouse_leave(self) -> None:
|
|
79
|
+
super().on_mouse_leave()
|
|
80
|
+
self._hover_idx = -1
|
|
81
|
+
|
|
82
|
+
def _layout_slice(self, entries: list[tuple[int, float]], area: Rect, out: list[tuple[int, Rect]]) -> None:
|
|
83
|
+
if not entries or area.width <= 1.0 or area.height <= 1.0:
|
|
84
|
+
return
|
|
85
|
+
if len(entries) == 1:
|
|
86
|
+
out.append((entries[0][0], area))
|
|
87
|
+
return
|
|
88
|
+
total = sum(v for _, v in entries)
|
|
89
|
+
if total <= 0.0:
|
|
90
|
+
return
|
|
91
|
+
half = total * 0.5
|
|
92
|
+
acc = 0.0
|
|
93
|
+
split = 1
|
|
94
|
+
for i, (_, v) in enumerate(entries):
|
|
95
|
+
acc += v
|
|
96
|
+
if acc >= half:
|
|
97
|
+
split = max(1, min(len(entries) - 1, i + 1))
|
|
98
|
+
break
|
|
99
|
+
left_part = entries[:split]
|
|
100
|
+
right_part = entries[split:]
|
|
101
|
+
ratio = sum(v for _, v in left_part) / total
|
|
102
|
+
gap = 4.0
|
|
103
|
+
if area.width >= area.height:
|
|
104
|
+
lw = max(1.0, area.width * ratio - gap * 0.5)
|
|
105
|
+
r1 = Rect(area.x, area.y, lw, area.height)
|
|
106
|
+
r2 = Rect(area.x + lw + gap, area.y, max(1.0, area.width - lw - gap), area.height)
|
|
107
|
+
else:
|
|
108
|
+
lh = max(1.0, area.height * ratio - gap * 0.5)
|
|
109
|
+
r1 = Rect(area.x, area.y, area.width, lh)
|
|
110
|
+
r2 = Rect(area.x, area.y + lh + gap, area.width, max(1.0, area.height - lh - gap))
|
|
111
|
+
self._layout_slice(left_part, r1, out)
|
|
112
|
+
self._layout_slice(right_part, r2, out)
|
|
113
|
+
|
|
114
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
115
|
+
c = theme.colors
|
|
116
|
+
r = self.rect
|
|
117
|
+
rrect = make_rrect_spec(r, self._shape_override or "medium", theme)
|
|
118
|
+
bg = resolve_color(self._bg_override, theme, c.surface_container_low) or c.surface_container_low
|
|
119
|
+
canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
|
|
120
|
+
|
|
121
|
+
top = r.y + 12.0
|
|
122
|
+
if self.title:
|
|
123
|
+
tfont = font_for_style(theme.font_family, theme.typography.title_small)
|
|
124
|
+
draw_text_centered_y(canvas, self.title, r.x + 14.0, r.y + 20.0, tfont, c.on_surface)
|
|
125
|
+
top = r.y + 38.0
|
|
126
|
+
|
|
127
|
+
plot = Rect(r.x + 12.0, top, max(10.0, r.width - 24.0), max(10.0, r.bottom - top - 12.0))
|
|
128
|
+
ordered = sorted([(i, v) for i, (_, v) in enumerate(self.items) if v > 0], key=lambda x: x[1], reverse=True)
|
|
129
|
+
total = sum(v for _, v in ordered) or 1.0
|
|
130
|
+
self._rects = []
|
|
131
|
+
self._layout_slice(ordered, plot, self._rects)
|
|
132
|
+
|
|
133
|
+
pal = _bi_palette(theme)
|
|
134
|
+
lfont = font_for_style(theme.font_family, theme.typography.label_medium)
|
|
135
|
+
sfont = font_for_style(theme.font_family, theme.typography.label_small)
|
|
136
|
+
|
|
137
|
+
for idx, rr in self._rects:
|
|
138
|
+
label, val = self.items[idx]
|
|
139
|
+
col = pal[idx % len(pal)]
|
|
140
|
+
if idx == self._hover_idx:
|
|
141
|
+
col = col.blend(Color(255, 255, 255), 0.14)
|
|
142
|
+
canvas.drawRRect(make_rrect(rr, 8.0), skia.Paint(AntiAlias=True, Color=col.to_skia()))
|
|
143
|
+
if rr.width >= 48.0 and rr.height >= 28.0:
|
|
144
|
+
canvas.save()
|
|
145
|
+
canvas.clipRect(skia.Rect.MakeXYWH(rr.x + 4.0, rr.y + 4.0, rr.width - 8.0, rr.height - 8.0))
|
|
146
|
+
draw_text_centered_y(canvas, label, rr.x + 8.0, rr.y + 16.0, lfont, Color(255, 255, 255))
|
|
147
|
+
if rr.height >= 44.0:
|
|
148
|
+
pct = f"{(val / total) * 100.0:.0f}%"
|
|
149
|
+
draw_text_centered_y(canvas, pct, rr.x + 8.0, rr.y + 34.0, sfont, Color(255, 255, 255, 215))
|
|
150
|
+
canvas.restore()
|
|
151
|
+
self._paint_custom_border(canvas, theme, "medium")
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
# ---------------------------------------------------------------------------
|
|
155
|
+
# 2. SankeyChart
|
|
156
|
+
# ---------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class SankeyChart(Widget):
|
|
160
|
+
"""GPU Sankey Flow Diagram connecting source -> target nodes with cubic Bézier ribbons."""
|
|
161
|
+
|
|
162
|
+
manages_own_box = True
|
|
163
|
+
|
|
164
|
+
def __init__(
|
|
165
|
+
self,
|
|
166
|
+
flows: Sequence[tuple[str, str, float]],
|
|
167
|
+
*,
|
|
168
|
+
title: str | None = None,
|
|
169
|
+
chart_height: float = 230.0,
|
|
170
|
+
) -> None:
|
|
171
|
+
super().__init__()
|
|
172
|
+
self.flows: list[tuple[str, str, float]] = [(str(s), str(t), max(0.0, float(v))) for s, t, v in flows]
|
|
173
|
+
self.title = title
|
|
174
|
+
self.chart_height = float(chart_height)
|
|
175
|
+
self._fill_width = True
|
|
176
|
+
|
|
177
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
178
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 480.0
|
|
179
|
+
return Size(w, self.chart_height)
|
|
180
|
+
|
|
181
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
182
|
+
c = theme.colors
|
|
183
|
+
r = self.rect
|
|
184
|
+
rrect = make_rrect_spec(r, self._shape_override or "medium", theme)
|
|
185
|
+
bg = resolve_color(self._bg_override, theme, c.surface_container_low) or c.surface_container_low
|
|
186
|
+
canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
|
|
187
|
+
|
|
188
|
+
top = r.y + 14.0
|
|
189
|
+
if self.title:
|
|
190
|
+
tfont = font_for_style(theme.font_family, theme.typography.title_small)
|
|
191
|
+
draw_text_centered_y(canvas, self.title, r.x + 14.0, r.y + 20.0, tfont, c.on_surface)
|
|
192
|
+
top = r.y + 40.0
|
|
193
|
+
|
|
194
|
+
if not self.flows:
|
|
195
|
+
return
|
|
196
|
+
|
|
197
|
+
left_totals: dict[str, float] = {}
|
|
198
|
+
right_totals: dict[str, float] = {}
|
|
199
|
+
for s, t, v in self.flows:
|
|
200
|
+
left_totals[s] = left_totals.get(s, 0.0) + v
|
|
201
|
+
right_totals[t] = right_totals.get(t, 0.0) + v
|
|
202
|
+
|
|
203
|
+
plot_h = max(40.0, r.bottom - top - 16.0)
|
|
204
|
+
node_w = 14.0
|
|
205
|
+
left_x = r.x + 92.0
|
|
206
|
+
right_x = r.right - 92.0 - node_w
|
|
207
|
+
gap = 8.0
|
|
208
|
+
|
|
209
|
+
def layout_col(totals: dict[str, float], x: float) -> dict[str, Rect]:
|
|
210
|
+
total_v = sum(totals.values()) or 1.0
|
|
211
|
+
avail = max(20.0, plot_h - gap * max(0, len(totals) - 1))
|
|
212
|
+
y = top
|
|
213
|
+
res: dict[str, Rect] = {}
|
|
214
|
+
for name, val in totals.items():
|
|
215
|
+
nh = max(10.0, avail * (val / total_v))
|
|
216
|
+
res[name] = Rect(x, y, node_w, nh)
|
|
217
|
+
y += nh + gap
|
|
218
|
+
return res
|
|
219
|
+
|
|
220
|
+
l_rects = layout_col(left_totals, left_x)
|
|
221
|
+
r_rects = layout_col(right_totals, right_x)
|
|
222
|
+
l_off = {k: 0.0 for k in l_rects}
|
|
223
|
+
r_off = {k: 0.0 for k in r_rects}
|
|
224
|
+
|
|
225
|
+
pal = _bi_palette(theme)
|
|
226
|
+
s_idx = {name: i for i, name in enumerate(l_rects)}
|
|
227
|
+
lfont = font_for_style(theme.font_family, theme.typography.label_small)
|
|
228
|
+
|
|
229
|
+
# Draw ribbons first
|
|
230
|
+
for s, t, v in self.flows:
|
|
231
|
+
lr, rr = l_rects[s], r_rects[t]
|
|
232
|
+
lh = lr.height * (v / (left_totals[s] or 1.0))
|
|
233
|
+
rh = rr.height * (v / (right_totals[t] or 1.0))
|
|
234
|
+
y0_top = lr.y + l_off[s]
|
|
235
|
+
y0_bot = y0_top + lh
|
|
236
|
+
y1_top = rr.y + r_off[t]
|
|
237
|
+
y1_bot = y1_top + rh
|
|
238
|
+
l_off[s] += lh
|
|
239
|
+
r_off[t] += rh
|
|
240
|
+
|
|
241
|
+
x0 = lr.right
|
|
242
|
+
x1 = rr.x
|
|
243
|
+
mid = (x0 + x1) * 0.5
|
|
244
|
+
path = skia.Path()
|
|
245
|
+
path.moveTo(x0, y0_top)
|
|
246
|
+
path.cubicTo(mid, y0_top, mid, y1_top, x1, y1_top)
|
|
247
|
+
path.lineTo(x1, y1_bot)
|
|
248
|
+
path.cubicTo(mid, y1_bot, mid, y0_bot, x0, y0_bot)
|
|
249
|
+
path.close()
|
|
250
|
+
col = pal[s_idx[s] % len(pal)]
|
|
251
|
+
canvas.drawPath(path, skia.Paint(AntiAlias=True, Color=col.with_alpha(0.36).to_skia()))
|
|
252
|
+
|
|
253
|
+
# Draw node bars + labels
|
|
254
|
+
for i, (name, lr) in enumerate(l_rects.items()):
|
|
255
|
+
col = pal[i % len(pal)]
|
|
256
|
+
canvas.drawRRect(make_rrect(lr, 4.0), skia.Paint(AntiAlias=True, Color=col.to_skia()))
|
|
257
|
+
tw = measure_text(lfont, name)
|
|
258
|
+
draw_text_centered_y(canvas, name, lr.x - tw - 8.0, lr.center_y, lfont, c.on_surface)
|
|
259
|
+
|
|
260
|
+
for i, (name, rr) in enumerate(r_rects.items()):
|
|
261
|
+
col = pal[(i + 2) % len(pal)]
|
|
262
|
+
canvas.drawRRect(make_rrect(rr, 4.0), skia.Paint(AntiAlias=True, Color=col.to_skia()))
|
|
263
|
+
draw_text_centered_y(canvas, name, rr.right + 8.0, rr.center_y, lfont, c.on_surface)
|
|
264
|
+
self._paint_custom_border(canvas, theme, "medium")
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
# ---------------------------------------------------------------------------
|
|
268
|
+
# 3. WaterfallChart (Financial Bridge)
|
|
269
|
+
# ---------------------------------------------------------------------------
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
class WaterfallChart(Widget):
|
|
273
|
+
"""Financial Waterfall chart showing positive, negative, and cumulative total bars."""
|
|
274
|
+
|
|
275
|
+
manages_own_box = True
|
|
276
|
+
|
|
277
|
+
def __init__(
|
|
278
|
+
self,
|
|
279
|
+
steps: Sequence[tuple[str, float] | tuple[str, float, bool]],
|
|
280
|
+
*,
|
|
281
|
+
title: str | None = None,
|
|
282
|
+
chart_height: float = 230.0,
|
|
283
|
+
) -> None:
|
|
284
|
+
super().__init__()
|
|
285
|
+
self.steps: list[tuple[str, float, bool]] = []
|
|
286
|
+
for s in steps:
|
|
287
|
+
if len(s) == 3:
|
|
288
|
+
self.steps.append((str(s[0]), float(s[1]), bool(s[2])))
|
|
289
|
+
else:
|
|
290
|
+
self.steps.append((str(s[0]), float(s[1]), False))
|
|
291
|
+
self.title = title
|
|
292
|
+
self.chart_height = float(chart_height)
|
|
293
|
+
self._fill_width = True
|
|
294
|
+
|
|
295
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
296
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 480.0
|
|
297
|
+
return Size(w, self.chart_height)
|
|
298
|
+
|
|
299
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
300
|
+
c = theme.colors
|
|
301
|
+
r = self.rect
|
|
302
|
+
rrect = make_rrect_spec(r, self._shape_override or "medium", theme)
|
|
303
|
+
bg = resolve_color(self._bg_override, theme, c.surface_container_low) or c.surface_container_low
|
|
304
|
+
canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
|
|
305
|
+
|
|
306
|
+
top = r.y + 14.0
|
|
307
|
+
if self.title:
|
|
308
|
+
tfont = font_for_style(theme.font_family, theme.typography.title_small)
|
|
309
|
+
draw_text_centered_y(canvas, self.title, r.x + 14.0, r.y + 20.0, tfont, c.on_surface)
|
|
310
|
+
top = r.y + 42.0
|
|
311
|
+
|
|
312
|
+
if not self.steps:
|
|
313
|
+
return
|
|
314
|
+
|
|
315
|
+
# Compute running bars (start, end, is_total)
|
|
316
|
+
running = 0.0
|
|
317
|
+
bars: list[tuple[str, float, float, float, bool]] = []
|
|
318
|
+
for label, val, is_tot in self.steps:
|
|
319
|
+
if is_tot:
|
|
320
|
+
bars.append((label, 0.0, running, running, True))
|
|
321
|
+
else:
|
|
322
|
+
nxt = running + val
|
|
323
|
+
bars.append((label, running, nxt, val, False))
|
|
324
|
+
running = nxt
|
|
325
|
+
|
|
326
|
+
all_y = [0.0] + [b[1] for b in bars] + [b[2] for b in bars]
|
|
327
|
+
lo, hi = min(all_y), max(all_y)
|
|
328
|
+
span = (hi - lo) if hi > lo else 1.0
|
|
329
|
+
plot = Rect(r.x + 20.0, top, max(40.0, r.width - 40.0), max(40.0, r.bottom - top - 34.0))
|
|
330
|
+
|
|
331
|
+
def map_y(v: float) -> float:
|
|
332
|
+
return plot.bottom - ((v - lo) / span) * plot.height
|
|
333
|
+
|
|
334
|
+
# Baseline at 0
|
|
335
|
+
y0 = map_y(0.0)
|
|
336
|
+
canvas.drawLine(plot.x, y0, plot.right, y0, skia.Paint(Color=c.outline_variant.to_skia(), StrokeWidth=1.0))
|
|
337
|
+
|
|
338
|
+
n = len(bars)
|
|
339
|
+
slot_w = plot.width / n
|
|
340
|
+
bar_w = min(46.0, slot_w * 0.58)
|
|
341
|
+
pos_c = Color.from_hex("#16A34A")
|
|
342
|
+
neg_c = Color.from_hex("#DC2626")
|
|
343
|
+
sfont = font_for_style(theme.font_family, theme.typography.label_small)
|
|
344
|
+
|
|
345
|
+
prev_end_y: float | None = None
|
|
346
|
+
for i, (label, start_v, end_v, raw_v, is_tot) in enumerate(bars):
|
|
347
|
+
cx = plot.x + (i + 0.5) * slot_w
|
|
348
|
+
ys, ye = map_y(start_v), map_y(end_v)
|
|
349
|
+
bt = min(ys, ye)
|
|
350
|
+
bh = max(3.0, abs(ye - ys))
|
|
351
|
+
br = Rect(cx - bar_w * 0.5, bt, bar_w, bh)
|
|
352
|
+
|
|
353
|
+
if prev_end_y is not None:
|
|
354
|
+
prev_x = plot.x + (i - 0.5) * slot_w + bar_w * 0.5
|
|
355
|
+
canvas.drawLine(prev_x, prev_end_y, br.x, prev_end_y, skia.Paint(Color=c.outline_variant.to_skia(), StrokeWidth=1.0))
|
|
356
|
+
|
|
357
|
+
col = c.primary if is_tot else (pos_c if raw_v >= 0 else neg_c)
|
|
358
|
+
canvas.drawRRect(make_rrect(br, 5.0), skia.Paint(AntiAlias=True, Color=col.to_skia()))
|
|
359
|
+
|
|
360
|
+
val_txt = f"{raw_v:+,.0f}" if not is_tot else f"{end_v:,.0f}"
|
|
361
|
+
draw_text_centered_y(canvas, val_txt, cx - measure_text(sfont, val_txt) * 0.5, bt - 10.0, sfont, c.on_surface)
|
|
362
|
+
draw_text_centered_y(canvas, label, cx - measure_text(sfont, label) * 0.5, plot.bottom + 16.0, sfont, c.on_surface_variant)
|
|
363
|
+
prev_end_y = ye
|
|
364
|
+
self._paint_custom_border(canvas, theme, "medium")
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
# ---------------------------------------------------------------------------
|
|
368
|
+
# 4. BoxPlotChart (Statistical Distribution)
|
|
369
|
+
# ---------------------------------------------------------------------------
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
class BoxPlotChart(Widget):
|
|
373
|
+
"""Statistical BoxPlot chart displaying min, Q1, median, Q3, and max whiskers."""
|
|
374
|
+
|
|
375
|
+
manages_own_box = True
|
|
376
|
+
|
|
377
|
+
def __init__(
|
|
378
|
+
self,
|
|
379
|
+
series: Sequence[tuple[str, float, float, float, float, float]],
|
|
380
|
+
*,
|
|
381
|
+
title: str | None = None,
|
|
382
|
+
color: ChartColorRole | Color = "primary",
|
|
383
|
+
chart_height: float = 230.0,
|
|
384
|
+
) -> None:
|
|
385
|
+
super().__init__()
|
|
386
|
+
# Each tuple: (label, min, q1, median, q3, max)
|
|
387
|
+
self.series: list[tuple[str, float, float, float, float, float]] = [
|
|
388
|
+
(str(s[0]), float(s[1]), float(s[2]), float(s[3]), float(s[4]), float(s[5]))
|
|
389
|
+
for s in series
|
|
390
|
+
]
|
|
391
|
+
self.title = title
|
|
392
|
+
self.color = color
|
|
393
|
+
self.chart_height = float(chart_height)
|
|
394
|
+
self._fill_width = True
|
|
395
|
+
|
|
396
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
397
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 460.0
|
|
398
|
+
return Size(w, self.chart_height)
|
|
399
|
+
|
|
400
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
401
|
+
c = theme.colors
|
|
402
|
+
r = self.rect
|
|
403
|
+
rrect = make_rrect_spec(r, self._shape_override or "medium", theme)
|
|
404
|
+
bg = resolve_color(self._bg_override, theme, c.surface_container_low) or c.surface_container_low
|
|
405
|
+
canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
|
|
406
|
+
|
|
407
|
+
top = r.y + 14.0
|
|
408
|
+
if self.title:
|
|
409
|
+
tfont = font_for_style(theme.font_family, theme.typography.title_small)
|
|
410
|
+
draw_text_centered_y(canvas, self.title, r.x + 14.0, r.y + 20.0, tfont, c.on_surface)
|
|
411
|
+
top = r.y + 40.0
|
|
412
|
+
|
|
413
|
+
if not self.series:
|
|
414
|
+
return
|
|
415
|
+
|
|
416
|
+
lo = min(s[1] for s in self.series)
|
|
417
|
+
hi = max(s[5] for s in self.series)
|
|
418
|
+
span = (hi - lo) if hi > lo else 1.0
|
|
419
|
+
plot = Rect(r.x + 36.0, top + 6.0, max(40.0, r.width - 52.0), max(40.0, r.bottom - top - 38.0))
|
|
420
|
+
|
|
421
|
+
def map_y(v: float) -> float:
|
|
422
|
+
return plot.bottom - ((v - lo) / span) * plot.height
|
|
423
|
+
|
|
424
|
+
sfont = font_for_style(theme.font_family, theme.typography.label_small)
|
|
425
|
+
accent = _resolve_accent(theme, self.color)
|
|
426
|
+
for g in range(4):
|
|
427
|
+
gv = lo + (g / 3.0) * span
|
|
428
|
+
gy = map_y(gv)
|
|
429
|
+
canvas.drawLine(plot.x, gy, plot.right, gy, skia.Paint(Color=c.outline_variant.with_alpha(0.5).to_skia(), StrokeWidth=1.0))
|
|
430
|
+
draw_text_centered_y(canvas, f"{gv:.0f}", r.x + 8.0, gy, sfont, c.on_surface_variant)
|
|
431
|
+
|
|
432
|
+
n = len(self.series)
|
|
433
|
+
slot_w = plot.width / n
|
|
434
|
+
box_w = min(38.0, slot_w * 0.48)
|
|
435
|
+
whisker_p = skia.Paint(AntiAlias=True, Color=accent.to_skia(), StrokeWidth=1.8)
|
|
436
|
+
|
|
437
|
+
for i, (label, vmin, q1, med, q3, vmax) in enumerate(self.series):
|
|
438
|
+
cx = plot.x + (i + 0.5) * slot_w
|
|
439
|
+
y_min, y_q1, y_med, y_q3, y_max = map_y(vmin), map_y(q1), map_y(med), map_y(q3), map_y(vmax)
|
|
440
|
+
|
|
441
|
+
# Vertical whisker line + caps
|
|
442
|
+
canvas.drawLine(cx, y_max, cx, y_min, whisker_p)
|
|
443
|
+
canvas.drawLine(cx - box_w * 0.3, y_max, cx + box_w * 0.3, y_max, whisker_p)
|
|
444
|
+
canvas.drawLine(cx - box_w * 0.3, y_min, cx + box_w * 0.3, y_min, whisker_p)
|
|
445
|
+
|
|
446
|
+
# IQR box (Q1..Q3)
|
|
447
|
+
br = Rect(cx - box_w * 0.5, min(y_q3, y_q1), box_w, max(4.0, abs(y_q1 - y_q3)))
|
|
448
|
+
canvas.drawRRect(make_rrect(br, 5.0), skia.Paint(AntiAlias=True, Color=accent.with_alpha(0.28).to_skia()))
|
|
449
|
+
canvas.drawRRect(make_rrect(br, 5.0), skia.Paint(AntiAlias=True, Color=accent.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=1.8))
|
|
450
|
+
|
|
451
|
+
# Median bar
|
|
452
|
+
canvas.drawLine(br.x, y_med, br.right, y_med, skia.Paint(AntiAlias=True, Color=accent.to_skia(), StrokeWidth=2.6))
|
|
453
|
+
draw_text_centered_y(canvas, label, cx - measure_text(sfont, label) * 0.5, plot.bottom + 16.0, sfont, c.on_surface_variant)
|
|
454
|
+
self._paint_custom_border(canvas, theme, "medium")
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
# ---------------------------------------------------------------------------
|
|
458
|
+
# 5. Sparkline (Compact Inline Mini-Chart)
|
|
459
|
+
# ---------------------------------------------------------------------------
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
class Sparkline(Widget):
|
|
463
|
+
"""Compact inline GPU Sparkline for embedding inside cards, lists, or tables."""
|
|
464
|
+
|
|
465
|
+
manages_own_box = False
|
|
466
|
+
|
|
467
|
+
def __init__(
|
|
468
|
+
self,
|
|
469
|
+
values: Sequence[float],
|
|
470
|
+
*,
|
|
471
|
+
color: ChartColorRole | Color = "primary",
|
|
472
|
+
area: bool = True,
|
|
473
|
+
width: float = 140.0,
|
|
474
|
+
height: float = 36.0,
|
|
475
|
+
) -> None:
|
|
476
|
+
super().__init__()
|
|
477
|
+
self.values: list[float] = [float(v) for v in values]
|
|
478
|
+
self.color = color
|
|
479
|
+
self.area = area
|
|
480
|
+
self.spark_width = float(width)
|
|
481
|
+
self.spark_height = float(height)
|
|
482
|
+
|
|
483
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
484
|
+
return Size(self.spark_width, self.spark_height)
|
|
485
|
+
|
|
486
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
487
|
+
if len(self.values) < 2:
|
|
488
|
+
return
|
|
489
|
+
r = self.rect
|
|
490
|
+
accent = resolve_color(self._fg_override, theme) or _resolve_accent(theme, self.color)
|
|
491
|
+
lo, hi = min(self.values), max(self.values)
|
|
492
|
+
span = (hi - lo) if hi > lo else 1.0
|
|
493
|
+
pad = 4.0
|
|
494
|
+
w = max(4.0, r.width - pad * 2.0)
|
|
495
|
+
h = max(4.0, r.height - pad * 2.0)
|
|
496
|
+
|
|
497
|
+
pts = [
|
|
498
|
+
(r.x + pad + (i / (len(self.values) - 1)) * w, r.bottom - pad - ((v - lo) / span) * h)
|
|
499
|
+
for i, v in enumerate(self.values)
|
|
500
|
+
]
|
|
501
|
+
path = skia.Path()
|
|
502
|
+
path.moveTo(pts[0][0], pts[0][1])
|
|
503
|
+
for px, py in pts[1:]:
|
|
504
|
+
path.lineTo(px, py)
|
|
505
|
+
|
|
506
|
+
if self.area:
|
|
507
|
+
ap = skia.Path(path)
|
|
508
|
+
ap.lineTo(pts[-1][0], r.bottom - pad)
|
|
509
|
+
ap.lineTo(pts[0][0], r.bottom - pad)
|
|
510
|
+
ap.close()
|
|
511
|
+
grad = skia.GradientShader.MakeLinear(
|
|
512
|
+
[(0.0, r.y + pad), (0.0, r.bottom - pad)],
|
|
513
|
+
[accent.with_alpha(0.28).to_skia(), accent.with_alpha(0.0).to_skia()],
|
|
514
|
+
)
|
|
515
|
+
canvas.drawPath(ap, skia.Paint(AntiAlias=True, Shader=grad))
|
|
516
|
+
|
|
517
|
+
canvas.drawPath(
|
|
518
|
+
path,
|
|
519
|
+
skia.Paint(
|
|
520
|
+
AntiAlias=True,
|
|
521
|
+
Color=accent.to_skia(),
|
|
522
|
+
Style=skia.Paint.kStroke_Style,
|
|
523
|
+
StrokeWidth=2.0,
|
|
524
|
+
StrokeCap=skia.Paint.kRound_Cap,
|
|
525
|
+
StrokeJoin=skia.Paint.kRound_Join,
|
|
526
|
+
),
|
|
527
|
+
)
|
|
528
|
+
canvas.drawCircle(pts[-1][0], pts[-1][1], 3.2, skia.Paint(AntiAlias=True, Color=accent.to_skia()))
|