feat: implement energy availability warning and enhance nutrition visualization
- Added `get_energy_availability_warning_payload` function to assess energy availability and provide contextual warnings based on multiple health indicators. - Integrated energy availability KPI tile into the nutrition history visualization, enhancing user insights on energy balance. - Updated frontend components to conditionally display the energy availability warning, improving user experience and data interpretation. - Refactored existing logic in `charts.py` to utilize the new energy availability functionality, streamlining data handling.
This commit is contained in:
1 parent
fc816da335
commit
d7304c1a44
8 files changed
+182
-102
No files matched your search
@@ -350,6 +350,11 @@ body { font-family: var(--font); background: var(--bg); color: var(--text1); -we
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
/* KPI: Kurz-Hinweis max. 2 Zeilen — Details weiter per ℹ */
|
||||
.kpi-tiles-card__hint {
|
||||
max-height: 2.8em;
|
||||
}
|
||||
|
||||
/* Verlauf Ernährung: Donut (Ø-Quote) + wöchentliche Makro-Verteilung (E3) */
|
||||
.nutrition-macro-pair {
|
||||
display: grid;
|
||||
|
||||
@@ -125,14 +125,17 @@ export default function KpiTilesOverview({
|
||||
<div
|
||||
className="kpi-tiles-card__hint"
|
||||
style={{
|
||||
marginTop: 10,
|
||||
padding: '8px 10px',
|
||||
borderRadius: 8,
|
||||
fontSize: 10,
|
||||
lineHeight: 1.45,
|
||||
marginTop: 6,
|
||||
paddingLeft: 8,
|
||||
borderLeft: `2px solid ${accent}`,
|
||||
fontSize: 9,
|
||||
lineHeight: 1.35,
|
||||
color: 'var(--text2)',
|
||||
background: 'var(--surface2)',
|
||||
borderLeft: `3px solid ${accent}`,
|
||||
display: '-webkit-box',
|
||||
WebkitLineClamp: 2,
|
||||
WebkitBoxOrient: 'vertical',
|
||||
overflow: 'hidden',
|
||||
wordBreak: 'break-word',
|
||||
}}
|
||||
>
|
||||
{cardHint}
|
||||
|
||||
@@ -206,7 +206,12 @@ export function WeeklyMacroDistributionPanel({ macroWeeklyData, loading, error }
|
||||
/**
|
||||
* Nutrition Charts (E1–E5). Verlauf: `showWeeklyMacroDistribution={false}` wenn E3 separat (z. B. neben Donut) gerendert wird.
|
||||
*/
|
||||
export default function NutritionCharts({ days = 28, showWeeklyMacroDistribution = true }) {
|
||||
export default function NutritionCharts({
|
||||
days = 28,
|
||||
showWeeklyMacroDistribution = true,
|
||||
/** Verlauf: E5-Kachel liegt in nutrition-history-viz KPIs — doppelte Karte ausblenden */
|
||||
hideEnergyAvailabilityCard = false,
|
||||
}) {
|
||||
const [energyData, setEnergyData] = useState(null)
|
||||
const [proteinData, setProteinData] = useState(null)
|
||||
const [macroWeeklyData, setMacroWeeklyData] = useState(null)
|
||||
@@ -221,15 +226,17 @@ export default function NutritionCharts({ days = 28, showWeeklyMacroDistribution
|
||||
|
||||
useEffect(() => {
|
||||
loadCharts()
|
||||
}, [days, showWeeklyMacroDistribution])
|
||||
}, [days, showWeeklyMacroDistribution, hideEnergyAvailabilityCard])
|
||||
|
||||
const loadCharts = async () => {
|
||||
const tasks = [
|
||||
loadEnergyBalance(),
|
||||
loadProteinAdequacy(),
|
||||
loadAdherence(),
|
||||
loadWarning(),
|
||||
]
|
||||
if (!hideEnergyAvailabilityCard) {
|
||||
tasks.push(loadWarning())
|
||||
}
|
||||
if (showWeeklyMacroDistribution) {
|
||||
tasks.splice(2, 0, loadMacroWeekly())
|
||||
}
|
||||
@@ -474,7 +481,7 @@ export default function NutritionCharts({ days = 28, showWeeklyMacroDistribution
|
||||
)}
|
||||
|
||||
{!loading.adherence && !errors.adherence && renderAdherence()}
|
||||
{!loading.warning && !errors.warning && renderWarning()}
|
||||
{!hideEnergyAvailabilityCard && !loading.warning && !errors.warning && renderWarning()}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -700,6 +700,26 @@ function BodySection({ profile, insights, onRequest, loadingSlug, filterActiveSl
|
||||
)
|
||||
}
|
||||
|
||||
/** TDEE-Linie muss in der kcal-Y-Domain liegen (sonst unsichtbar trotz Legende). */
|
||||
function kcalVsWeightKcalDomain(points, tdeeRef) {
|
||||
const vals = (points || [])
|
||||
.map(d => Number(d.kcal_avg))
|
||||
.filter(v => !Number.isNaN(v))
|
||||
if (!vals.length) return ['auto', 'auto']
|
||||
let lo = Math.min(...vals)
|
||||
let hi = Math.max(...vals)
|
||||
const t = tdeeRef != null ? Number(tdeeRef) : NaN
|
||||
if (!Number.isNaN(t)) {
|
||||
lo = Math.min(lo, t)
|
||||
hi = Math.max(hi, t)
|
||||
}
|
||||
const span = hi - lo || 400
|
||||
const pad = Math.max(100, span * 0.1)
|
||||
return [Math.max(0, Math.floor(lo - pad)), Math.ceil(hi + pad)]
|
||||
}
|
||||
|
||||
const TDEE_REF_LINE_COLOR = '#475569'
|
||||
|
||||
/** Legende unter dem Chart: Linien + ggf. TDEE-Referenz (gestrichelt). */
|
||||
function KcalVsWeightLegend({ showTdee }) {
|
||||
const line = (color) => ({
|
||||
@@ -753,7 +773,7 @@ function KcalVsWeightLegend({ showTdee }) {
|
||||
height: 0,
|
||||
verticalAlign: 'middle',
|
||||
marginRight: 6,
|
||||
borderTop: '2px dashed #EA580C',
|
||||
borderTop: `2px dashed ${TDEE_REF_LINE_COLOR}`,
|
||||
opacity: 0.95,
|
||||
}}
|
||||
/>
|
||||
@@ -774,6 +794,7 @@ function KcalVsWeightChart({ vizKcalWeight, corrData: corrRows, profile, cutoffD
|
||||
}))
|
||||
const n = vizKcalWeight.common_days_count ?? kcalVsW.length
|
||||
const tdeeLabel = tdee != null && tdee > 0 ? Math.round(tdee) : null
|
||||
const kcalDomain = kcalVsWeightKcalDomain(kcalVsW, tdeeLabel)
|
||||
return (
|
||||
<div className="card" style={{ marginBottom: 12 }}>
|
||||
<div style={{ fontSize: 12, fontWeight: 600, color: 'var(--text3)', marginBottom: 6 }}>
|
||||
@@ -786,14 +807,21 @@ function KcalVsWeightChart({ vizKcalWeight, corrData: corrRows, profile, cutoffD
|
||||
<LineChart data={kcalVsW} margin={{ top: 4, right: 8, bottom: 0, left: -16 }}>
|
||||
<CartesianGrid stroke="var(--border)" strokeDasharray="3 3" />
|
||||
<XAxis dataKey="date" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} interval={Math.max(0, Math.floor(kcalVsW.length / 6) - 1)} />
|
||||
<YAxis yAxisId="kcal" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} domain={['auto', 'auto']} />
|
||||
<YAxis yAxisId="kcal" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} domain={kcalDomain} />
|
||||
<YAxis yAxisId="weight" orientation="right" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} domain={['auto', 'auto']} />
|
||||
<Tooltip
|
||||
contentStyle={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, fontSize: 11 }}
|
||||
formatter={(v, n) => [`${Math.round(v)} ${n === 'weight' ? 'kg' : 'kcal'}`, n === 'kcal_avg' ? 'Ø Kalorien' : 'Gewicht']}
|
||||
/>
|
||||
{tdeeLabel != null && (
|
||||
<ReferenceLine yAxisId="kcal" y={tdeeLabel} stroke="#EA580C" strokeDasharray="6 4" strokeWidth={1.2} />
|
||||
<ReferenceLine
|
||||
yAxisId="kcal"
|
||||
y={tdeeLabel}
|
||||
stroke={TDEE_REF_LINE_COLOR}
|
||||
strokeDasharray="6 5"
|
||||
strokeWidth={2}
|
||||
isFront
|
||||
/>
|
||||
)}
|
||||
<Line yAxisId="kcal" type="monotone" dataKey="kcal_avg" stroke="#EA580C" strokeWidth={2.5} dot={false} name="kcal_avg" />
|
||||
<Line yAxisId="weight" type="monotone" dataKey="weight" stroke="#2563EB" strokeWidth={2.5} dot={{ r: 2, fill: '#2563EB' }} name="weight" />
|
||||
@@ -823,6 +851,7 @@ function KcalVsWeightChart({ vizKcalWeight, corrData: corrRows, profile, cutoffD
|
||||
const bmr = sex === 'm' ? 10 * latestW + 6.25 * height - 5 * age + 5 : 10 * latestW + 6.25 * height - 5 * age - 161
|
||||
const tdee = Math.round(bmr * 1.4)
|
||||
const kcalVsW = rollingAvg(raw.map(d => ({ ...d, date: fmtDate(d.date) })), 'kcal')
|
||||
const kcalDomainFb = kcalVsWeightKcalDomain(kcalVsW, tdee)
|
||||
|
||||
return (
|
||||
<div className="card" style={{ marginBottom: 12 }}>
|
||||
@@ -836,13 +865,20 @@ function KcalVsWeightChart({ vizKcalWeight, corrData: corrRows, profile, cutoffD
|
||||
<LineChart data={kcalVsW} margin={{ top: 4, right: 8, bottom: 0, left: -16 }}>
|
||||
<CartesianGrid stroke="var(--border)" strokeDasharray="3 3" />
|
||||
<XAxis dataKey="date" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} interval={Math.max(0, Math.floor(kcalVsW.length / 6) - 1)} />
|
||||
<YAxis yAxisId="kcal" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} domain={['auto', 'auto']} />
|
||||
<YAxis yAxisId="kcal" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} domain={kcalDomainFb} />
|
||||
<YAxis yAxisId="weight" orientation="right" tick={{ fontSize: 9, fill: 'var(--text3)' }} tickLine={false} domain={['auto', 'auto']} />
|
||||
<Tooltip
|
||||
contentStyle={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, fontSize: 11 }}
|
||||
formatter={(v, n) => [`${Math.round(v)} ${n === 'weight' ? 'kg' : 'kcal'}`, n === 'kcal_avg' ? 'Ø Kalorien' : 'Gewicht']}
|
||||
/>
|
||||
<ReferenceLine yAxisId="kcal" y={tdee} stroke="#EA580C" strokeDasharray="6 4" strokeWidth={1.2} />
|
||||
<ReferenceLine
|
||||
yAxisId="kcal"
|
||||
y={tdee}
|
||||
stroke={TDEE_REF_LINE_COLOR}
|
||||
strokeDasharray="6 5"
|
||||
strokeWidth={2}
|
||||
isFront
|
||||
/>
|
||||
<Line yAxisId="kcal" type="monotone" dataKey="kcal_avg" stroke="#EA580C" strokeWidth={2.5} dot={false} name="kcal_avg" />
|
||||
<Line yAxisId="weight" type="monotone" dataKey="weight" stroke="#2563EB" strokeWidth={2.5} dot={{ r: 2, fill: '#2563EB' }} name="weight" />
|
||||
</LineChart>
|
||||
@@ -1054,7 +1090,7 @@ function NutritionSection({ profile, insights, onRequest, loadingSlug, filterAct
|
||||
<div style={{ fontSize: 12, fontWeight: 600, color: 'var(--text3)', marginBottom: 8, marginTop: 4 }}>
|
||||
Zeitverläufe (Energie & Protein)
|
||||
</div>
|
||||
<NutritionCharts days={chartDays} showWeeklyMacroDistribution={false} />
|
||||
<NutritionCharts days={chartDays} showWeeklyMacroDistribution={false} hideEnergyAvailabilityCard />
|
||||
|
||||
<InsightBox insights={insights} slugs={filterActiveSlugs(['ernaehrung'])} onRequest={onRequest} loading={loadingSlug}/>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user