feat: enhance charting capabilities in report generation
- Added support for dual-axis line charts and stacked bar charts in the report visualization. - Introduced new helper functions for safely converting raw data to float series and for handling color conversions. - Updated existing chart rendering logic to accommodate new chart types and improve data handling. - Enhanced payload generation for daily macros and calorie vs. weight visualizations, ensuring accurate representation in reports. - Improved error handling and data validation for chart datasets, enhancing overall robustness.
This commit is contained in:
1 parent
387ee6840f
commit
d232b11411
2 files changed
+226
-30
No files matched your search
@@ -10,8 +10,24 @@ matplotlib.use("Agg")
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def _safe_float_series(raw: list, n: int) -> list[float]:
|
||||
out: list[float] = []
|
||||
for i in range(n):
|
||||
if i >= len(raw):
|
||||
out.append(0.0)
|
||||
continue
|
||||
v = raw[i]
|
||||
try:
|
||||
out.append(float(v) if v is not None else 0.0)
|
||||
except (TypeError, ValueError):
|
||||
out.append(0.0)
|
||||
return out
|
||||
|
||||
|
||||
def _color_to_rgb(hex_or_rgba: str) -> tuple[float, float, float]:
|
||||
s = (hex_or_rgba or "#333333").strip()
|
||||
if s.startswith("#") and len(s) >= 9:
|
||||
s = s[:7]
|
||||
if s.startswith("#") and len(s) >= 7:
|
||||
try:
|
||||
r = int(s[1:3], 16) / 255.0
|
||||
@@ -43,7 +59,87 @@ def chart_payload_to_png(payload: dict[str, Any], fig_width_in: float = 6.2, fig
|
||||
else:
|
||||
ax.text(0.5, 0.5, "Keine Daten", ha="center", va="center", transform=ax.transAxes)
|
||||
|
||||
elif chart_type in ("line", "bar", "scatter") and datasets:
|
||||
elif chart_type == "dual_axis_line" and datasets and labels:
|
||||
ax2 = ax.twinx()
|
||||
x = list(range(len(labels)))
|
||||
step = max(1, len(labels) // 8)
|
||||
h1, l1 = [], []
|
||||
h2, l2 = [], []
|
||||
for i, ds in enumerate(datasets):
|
||||
y_raw = ds.get("data") or []
|
||||
if not y_raw:
|
||||
continue
|
||||
y = _safe_float_series(y_raw, len(labels))
|
||||
lab = ds.get("label") or f"Serie {i + 1}"
|
||||
col = _color_to_rgb(str(ds.get("borderColor") or ds.get("backgroundColor") or "#333333"))
|
||||
y_axis = str(ds.get("yAxisID") or "").lower()
|
||||
use_right = y_axis in ("right", "weight", "y1")
|
||||
use_left = not use_right
|
||||
target = ax if use_left else ax2
|
||||
linestyle = "--" if ds.get("borderDash") else "-"
|
||||
(ln,) = target.plot(
|
||||
x[: len(y)],
|
||||
y,
|
||||
label=lab,
|
||||
color=col,
|
||||
linewidth=1.8,
|
||||
linestyle=linestyle,
|
||||
marker="o",
|
||||
markersize=2 if linestyle == "-" else 0,
|
||||
)
|
||||
if use_left:
|
||||
h1.append(ln)
|
||||
l1.append(lab)
|
||||
else:
|
||||
h2.append(ln)
|
||||
l2.append(lab)
|
||||
ax.set_xticks(x[::step])
|
||||
ax.set_xticklabels([labels[j] for j in range(0, len(labels), step)], rotation=25, fontsize=7)
|
||||
ax.grid(True, alpha=0.25)
|
||||
ax.set_xmargin(0.02)
|
||||
leg = h1 + h2
|
||||
leg_l = l1 + l2
|
||||
if leg:
|
||||
ax.legend(leg, leg_l, loc="upper right", fontsize=7)
|
||||
|
||||
elif chart_type == "bar" and datasets and labels:
|
||||
n = len(labels)
|
||||
x = list(range(n))
|
||||
stack_keys_nonnull = [ds.get("stack") for ds in datasets if (ds.get("data") or []) != []]
|
||||
use_stacked = (
|
||||
len(datasets) >= 2
|
||||
and len(stack_keys_nonnull) == len(datasets)
|
||||
and all(sk for sk in stack_keys_nonnull)
|
||||
and len(set(stack_keys_nonnull)) == 1
|
||||
)
|
||||
if use_stacked:
|
||||
bottom = [0.0] * n
|
||||
for ds in datasets:
|
||||
y_raw = ds.get("data") or []
|
||||
yv = _safe_float_series(y_raw, n)
|
||||
lab = ds.get("label") or "Serie"
|
||||
col = _color_to_rgb(str(ds.get("backgroundColor") or ds.get("borderColor") or "#1D9E75"))
|
||||
ax.bar(x, yv, bottom=bottom, label=lab, color=col, alpha=0.9, width=0.72)
|
||||
bottom = [bottom[i] + yv[i] for i in range(n)]
|
||||
ax.set_xticks(x)
|
||||
ax.set_xticklabels(labels, rotation=30, fontsize=7)
|
||||
else:
|
||||
width = 0.8 / max(len(datasets), 1)
|
||||
for i, ds in enumerate(datasets):
|
||||
y_raw = ds.get("data") or []
|
||||
yv = _safe_float_series(y_raw, n)
|
||||
offset = (i - len(datasets) / 2 + 0.5) * width
|
||||
xpos = [xj + offset for xj in x]
|
||||
lab = ds.get("label") or f"Serie {i + 1}"
|
||||
col = _color_to_rgb(str(ds.get("backgroundColor") or ds.get("borderColor") or "#1D9E75"))
|
||||
ax.bar(xpos, yv, width=width * 0.9, label=lab, color=col, alpha=0.88)
|
||||
ax.set_xticks(x)
|
||||
ax.set_xticklabels(labels, rotation=30, fontsize=7)
|
||||
ax.legend(loc="upper right", fontsize=7)
|
||||
ax.grid(True, axis="y", alpha=0.25)
|
||||
ax.set_xmargin(0.02)
|
||||
|
||||
elif chart_type in ("line", "scatter") and datasets:
|
||||
x = range(len(labels)) if labels else []
|
||||
for i, ds in enumerate(datasets):
|
||||
y = ds.get("data") or []
|
||||
@@ -51,31 +147,21 @@ def chart_payload_to_png(payload: dict[str, Any], fig_width_in: float = 6.2, fig
|
||||
continue
|
||||
lab = ds.get("label") or f"Serie {i + 1}"
|
||||
col = _color_to_rgb(str(ds.get("borderColor") or ds.get("backgroundColor") or "#1D9E75"))
|
||||
if chart_type == "bar":
|
||||
yv = y[: len(labels)] if labels else y
|
||||
bg = ds.get("backgroundColor")
|
||||
if isinstance(bg, list):
|
||||
cols = [_color_to_rgb(str(c)) for c in bg[: len(yv)]]
|
||||
else:
|
||||
cols = [_color_to_rgb(str(bg or "#1D9E75"))] * len(yv)
|
||||
ax.bar(list(range(len(yv))), yv, label=lab, color=cols[: len(yv)], alpha=0.88)
|
||||
else:
|
||||
ax.plot(
|
||||
list(x)[: len(y)],
|
||||
y,
|
||||
label=lab,
|
||||
color=col,
|
||||
linewidth=1.6,
|
||||
marker="o",
|
||||
markersize=2,
|
||||
)
|
||||
if labels and chart_type != "bar":
|
||||
ls = "--" if ds.get("borderDash") else "-"
|
||||
ax.plot(
|
||||
list(x)[: len(y)],
|
||||
y,
|
||||
label=lab,
|
||||
color=col,
|
||||
linewidth=1.6,
|
||||
linestyle=ls,
|
||||
marker="o",
|
||||
markersize=2,
|
||||
)
|
||||
if labels:
|
||||
step = max(1, len(labels) // 8)
|
||||
ax.set_xticks(list(x)[::step])
|
||||
ax.set_xticklabels([labels[j] for j in range(0, len(labels), step)], rotation=25, fontsize=7)
|
||||
elif labels and chart_type == "bar":
|
||||
ax.set_xticks(list(x))
|
||||
ax.set_xticklabels(labels, rotation=30, fontsize=7)
|
||||
ax.legend(loc="upper right", fontsize=7)
|
||||
ax.grid(True, alpha=0.25)
|
||||
ax.set_xmargin(0.02)
|
||||
|
||||
Reference in new issue
Block a user