feat: enhance charting capabilities in report generation
Deploy Development / deploy (push) Successful in 1m1s
Build Test / pytest-backend (push) Successful in 4s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 20s

- 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:
Lars committed 2026-04-29 22:22:56 +02:00
1 parent 387ee6840f
commit d232b11411
2 files changed
+226 -30

No files matched your search

+117 -7
View File
@@ -122,6 +122,15 @@ def _weight_series_payload(bundle_weight: dict[str, Any]) -> dict[str, Any] | No
"borderColor": "#378ADD",
}
)
if any(p.get("avg14") is not None for p in series):
datasets.append(
{
"label": "Ø 14T",
"data": [safe_float(p.get("avg14")) for p in series],
"borderColor": "#085041",
"borderDash": [6, 3],
}
)
return {"chart_type": "line", "data": {"labels": labels, "datasets": datasets}, "metadata": {"confidence": "high"}}
@@ -145,6 +154,97 @@ def _line_payload_from_points(
}
def _donut_avg_to_pie_payload(donut: list[dict[str, Any]] | None) -> dict[str, Any] | None:
if not donut:
return None
labels = [str(x.get("name") or "") for x in donut]
values = [safe_float(x.get("value")) for x in donut]
colors = [str(x.get("color") or "#666666") for x in donut]
if not labels or not values or len(labels) != len(values):
return None
return {
"chart_type": "pie",
"data": {
"labels": labels,
"datasets": [{"data": values, "backgroundColor": colors}],
},
"metadata": {"confidence": "high"},
}
def _daily_macros_stacked_bar_payload(daily_macros: list[dict[str, Any]]) -> dict[str, Any] | None:
if len(daily_macros) < 2:
return None
labels = [str(d.get("date") or "") for d in daily_macros]
return {
"chart_type": "bar",
"data": {
"labels": labels,
"datasets": [
{
"label": "Protein (g)",
"data": [safe_float(d.get("Protein")) for d in daily_macros],
"backgroundColor": "#4a8f72",
"stack": "daily",
},
{
"label": "Fett (g)",
"data": [safe_float(d.get("Fett")) for d in daily_macros],
"backgroundColor": "#6e8eb8",
"stack": "daily",
},
{
"label": "KH (g)",
"data": [safe_float(d.get("KH")) for d in daily_macros],
"backgroundColor": "#c17d45",
"stack": "daily",
},
],
},
"metadata": {"confidence": "high"},
}
def _kcal_vs_weight_dual_payload(kw: dict[str, Any]) -> dict[str, Any] | None:
pts = kw.get("points") or []
if len(pts) < 2:
return None
labels = [str(p.get("date") or "") for p in pts]
tdee = kw.get("tdee_reference_kcal")
datasets: list[dict[str, Any]] = [
{
"label": "Ø Kalorien (7T)",
"data": [safe_float(p.get("kcal_avg")) for p in pts],
"borderColor": "#EA580C",
"yAxisID": "kcal",
},
]
if tdee is not None and float(safe_float(tdee) or 0) > 0:
td = float(tdee)
datasets.append(
{
"label": "TDEE (geschätzt)",
"data": [td] * len(labels),
"borderColor": "#888888",
"yAxisID": "kcal",
"borderDash": [5, 5],
}
)
datasets.append(
{
"label": "Gewicht (kg)",
"data": [safe_float(p.get("weight")) for p in pts],
"borderColor": "#2563EB",
"yAxisID": "weight",
}
)
return {
"chart_type": "dual_axis_line",
"data": {"labels": labels, "datasets": datasets},
"metadata": {"confidence": "high"},
}
def _append_body_bundle(story: list, styles: dict, profile_id: str, cfg: dict[str, Any]) -> None:
days = int(cfg.get("chart_days") or 30)
bundle = get_body_history_viz_bundle(profile_id, days)
@@ -235,17 +335,27 @@ def _append_nutrition_bundle(story: list, styles: dict, profile_id: str, cfg: di
pl2 = charts.get("nutrition_adherence")
if pl2:
_add_chart_to_story(story, styles, pl2, "Ernährungs-Adherence")
if cfg.get("show_macro_distribution_pair", False) or cfg.get("show_macro_daily_bars", False):
if cfg.get("show_macro_daily_bars", False):
dm = bundle.get("daily_macros") or []
pl_dm = _daily_macros_stacked_bar_payload(dm)
if pl_dm:
_add_chart_to_story(story, styles, pl_dm, "Makroverteilung täglich (g) · Fokus Protein")
if cfg.get("show_macro_distribution_pair", False):
pie_pl = _donut_avg_to_pie_payload(bundle.get("donut_avg_pct"))
if pie_pl:
_add_chart_to_story(story, styles, pie_pl, "Ø Makro-Quote (% der Makro-kcal)")
wm = bundle.get("weekly_macro_chart")
if isinstance(wm, dict) and wm.get("chart_type"):
_add_chart_to_story(story, styles, wm, "Makros (wöchentlich)")
meta_wm = wm.get("metadata") or {}
if meta_wm.get("confidence") != "insufficient":
_add_chart_to_story(story, styles, wm, "Wöch. Makro-Verteilung (Anteil %)")
kw = bundle.get("kcal_vs_weight") or {}
if cfg.get("show_kcal_vs_weight", False) and kw.get("points"):
pts = kw["points"]
if pts:
pl = _line_payload_from_points(pts, "date", "kcal", "kcal")
if pl:
_add_chart_to_story(story, styles, pl, "Kalorien vs. Zeit")
pl_kw = _kcal_vs_weight_dual_payload(kw)
if pl_kw:
_add_chart_to_story(story, styles, pl_kw, "Kalorien (Ø 7 Tage) vs. Gewicht")
story.append(Spacer(1, 2 * mm))