feat(mobile): Gewichtsverlauf als Diagramm
found.md: Gewichtsdaten wurden nur als Liste angezeigt. Neues WeightChart (react-native-svg, Port des Web-Diagramms) ab 2 Eintraegen ueber der Liste: x-Achse zeitproportional (unregelmaessige Wiegetermine), runde y-Ticks (1/2/5 x 10^n), Datumslabels nach Pixelabstand, Antippen waehlt die naechste Messung (Wert + Datum im Kopf), Veraenderung seit erster Messung. Linienfarbe als neues Token chartLine (#ea580c), mit dem Dataviz-Validator gegen helle und dunkle Flaechen geprueft. Geometrie rein in src/lib/weight-chart.ts mit Tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
81e617ece5
commit
e60abbabc6
7 files changed
+507
-13
No files matched your search
@@ -0,0 +1,202 @@
|
||||
import { useMemo, useState } from "react";
|
||||
import { View, Text, Pressable, StyleSheet, type LayoutChangeEvent } from "react-native";
|
||||
import Svg, { Path, Line, Circle, Text as SvgText, Defs, LinearGradient, Stop } from "react-native-svg";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useTheme } from "../../context/ThemeContext";
|
||||
import { Card } from "../Card";
|
||||
import { getTypography, fontFamily, type ColorTokens, type Typography } from "../../theme/tokens";
|
||||
import { buildWeightChart, nearestPointIndex, type WeightEntry } from "../../lib/weight-chart";
|
||||
|
||||
const CHART_HEIGHT = 170;
|
||||
const BOX = { left: 40, right: 12, top: 12, bottom: 26 };
|
||||
const AXIS_FONT_SIZE = 10;
|
||||
const MAX_LABELED_Y_TICKS = 5;
|
||||
const MARKER_RADIUS = 4;
|
||||
const SELECTED_RADIUS = 6;
|
||||
const LONG_SPAN_MS = 300 * 24 * 60 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* Weight history (found.md: Gewichtsdaten als Diagramm) — port of web's
|
||||
* src/components/health/WeightChart.tsx. Tap anywhere on the plot to select
|
||||
* the nearest weigh-in; its value and date show in the header (no floating
|
||||
* tooltip to collide with the edges on a phone). The newest entry is
|
||||
* selected by default. The entry list below the chart is the table view.
|
||||
*/
|
||||
export function WeightChart({ entries }: { entries: WeightEntry[] }) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const { colors } = useTheme();
|
||||
const styles = useMemo(() => makeStyles(colors), [colors]);
|
||||
const [width, setWidth] = useState(0);
|
||||
const [selectedIndex, setSelectedIndex] = useState<number | null>(null);
|
||||
|
||||
const chart = useMemo(
|
||||
() => (width > 0 ? buildWeightChart(entries, { width, height: CHART_HEIGHT, ...BOX }) : null),
|
||||
[entries, width]
|
||||
);
|
||||
|
||||
const formatKg = (kg: number, maximumFractionDigits = 2) =>
|
||||
`${kg.toLocaleString(i18n.language, { maximumFractionDigits })} kg`;
|
||||
const formatDate = (time: number, options: Intl.DateTimeFormatOptions) =>
|
||||
new Date(time).toLocaleDateString(i18n.language, { timeZone: "UTC", ...options });
|
||||
|
||||
const onLayout = (e: LayoutChangeEvent) => setWidth(Math.round(e.nativeEvent.layout.width));
|
||||
|
||||
if (entries.length < 2) return null;
|
||||
|
||||
const points = chart?.points ?? [];
|
||||
const activeIndex = selectedIndex !== null && selectedIndex < points.length ? selectedIndex : points.length - 1;
|
||||
const active = points[activeIndex];
|
||||
const delta = chart?.deltaKg ?? 0;
|
||||
const deltaLabel = `${delta > 0 ? "+" : delta < 0 ? "−" : "±"}${formatKg(Math.abs(delta))}`;
|
||||
const spansLong = points.length > 1 && points[points.length - 1].time - points[0].time > LONG_SPAN_MS;
|
||||
const axisDateOptions: Intl.DateTimeFormatOptions = spansLong
|
||||
? { month: "short", year: "2-digit" }
|
||||
: { day: "2-digit", month: "2-digit" };
|
||||
|
||||
const summary =
|
||||
points.length > 1
|
||||
? t("health.weightChartA11y", {
|
||||
from: formatKg(points[0].kg),
|
||||
fromDate: formatDate(points[0].time, { dateStyle: "medium" }),
|
||||
to: formatKg(points[points.length - 1].kg),
|
||||
toDate: formatDate(points[points.length - 1].time, { dateStyle: "medium" }),
|
||||
})
|
||||
: "";
|
||||
|
||||
return (
|
||||
<Card style={styles.card}>
|
||||
<View style={styles.header}>
|
||||
<View>
|
||||
<Text style={styles.eyebrow}>{t("health.weightChartTitle")}</Text>
|
||||
{active && (
|
||||
<Text style={styles.readout}>
|
||||
{formatKg(active.kg)}
|
||||
<Text style={styles.readoutDate}>{" "}{formatDate(active.time, { dateStyle: "medium" })}</Text>
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
<View style={styles.deltaChip}>
|
||||
<Text style={styles.deltaText}>{deltaLabel}</Text>
|
||||
<Text style={styles.deltaCaption}>{t("health.weightChartSinceFirst")}</Text>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
<Pressable
|
||||
onLayout={onLayout}
|
||||
onPress={(e) => chart && setSelectedIndex(nearestPointIndex(chart.points, e.nativeEvent.locationX))}
|
||||
accessibilityRole="image"
|
||||
accessibilityLabel={summary}
|
||||
style={styles.plot}
|
||||
>
|
||||
{chart && (
|
||||
<Svg width={width} height={CHART_HEIGHT}>
|
||||
<Defs>
|
||||
<LinearGradient id="weightArea" x1="0" y1="0" x2="0" y2="1">
|
||||
<Stop offset="0" stopColor={colors.chartLine} stopOpacity={0.2} />
|
||||
<Stop offset="1" stopColor={colors.chartLine} stopOpacity={0} />
|
||||
</LinearGradient>
|
||||
</Defs>
|
||||
|
||||
{chart.yTicks.map((tick, i) => (
|
||||
<Line
|
||||
key={`grid-${i}`}
|
||||
x1={BOX.left}
|
||||
x2={width - BOX.right}
|
||||
y1={tick.y}
|
||||
y2={tick.y}
|
||||
stroke={colors.hairlineGray}
|
||||
strokeWidth={1}
|
||||
/>
|
||||
))}
|
||||
{chart.yTicks.map((tick, i) =>
|
||||
chart.yTicks.length <= MAX_LABELED_Y_TICKS || i % 2 === 0 ? (
|
||||
<SvgText
|
||||
key={`ylabel-${i}`}
|
||||
x={BOX.left - 6}
|
||||
y={tick.y + AXIS_FONT_SIZE / 3}
|
||||
fontSize={AXIS_FONT_SIZE}
|
||||
fontFamily={fontFamily.regular}
|
||||
fill={colors.quietGray}
|
||||
textAnchor="end"
|
||||
>
|
||||
{tick.kg.toLocaleString(i18n.language, {
|
||||
minimumFractionDigits: chart.yTickDecimals,
|
||||
maximumFractionDigits: chart.yTickDecimals,
|
||||
})}
|
||||
</SvgText>
|
||||
) : null
|
||||
)}
|
||||
|
||||
<Path d={chart.areaPath} fill="url(#weightArea)" />
|
||||
<Path
|
||||
d={chart.linePath}
|
||||
fill="none"
|
||||
stroke={colors.chartLine}
|
||||
strokeWidth={2}
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
|
||||
{active && (
|
||||
<Line
|
||||
x1={active.x}
|
||||
x2={active.x}
|
||||
y1={BOX.top}
|
||||
y2={chart.baselineY}
|
||||
stroke={colors.quietGray}
|
||||
strokeWidth={1}
|
||||
strokeDasharray="3 3"
|
||||
/>
|
||||
)}
|
||||
|
||||
{chart.points.map((p, i) => (
|
||||
<Circle
|
||||
key={`pt-${i}`}
|
||||
cx={p.x}
|
||||
cy={p.y}
|
||||
r={i === activeIndex ? SELECTED_RADIUS : MARKER_RADIUS}
|
||||
fill={i === activeIndex ? colors.chartLine : colors.pureWhite}
|
||||
stroke={i === activeIndex ? colors.pureWhite : colors.chartLine}
|
||||
strokeWidth={2}
|
||||
/>
|
||||
))}
|
||||
|
||||
{chart.xLabelIndices.map((i) => {
|
||||
const p = chart.points[i];
|
||||
const isFirst = i === chart.xLabelIndices[0] && p.x - BOX.left < 20;
|
||||
const isLast = i === chart.points.length - 1;
|
||||
return (
|
||||
<SvgText
|
||||
key={`xlabel-${i}`}
|
||||
x={p.x}
|
||||
y={CHART_HEIGHT - 6}
|
||||
fontSize={AXIS_FONT_SIZE}
|
||||
fontFamily={fontFamily.regular}
|
||||
fill={colors.quietGray}
|
||||
textAnchor={isLast ? "end" : isFirst ? "start" : "middle"}
|
||||
>
|
||||
{formatDate(p.time, axisDateOptions)}
|
||||
</SvgText>
|
||||
);
|
||||
})}
|
||||
</Svg>
|
||||
)}
|
||||
</Pressable>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
function makeStyles(colors: ColorTokens) {
|
||||
const typography: Typography = getTypography(colors);
|
||||
return StyleSheet.create({
|
||||
card: { gap: 8 },
|
||||
header: { flexDirection: "row", justifyContent: "space-between", alignItems: "flex-start", gap: 12 },
|
||||
eyebrow: { ...typography.label },
|
||||
readout: { ...typography.title, fontSize: 20, marginTop: 2 },
|
||||
readoutDate: { ...typography.label, textTransform: "none", letterSpacing: 0, fontSize: 12 },
|
||||
deltaChip: { alignItems: "flex-end" },
|
||||
deltaText: { ...typography.body, fontFamily: typography.title.fontFamily },
|
||||
deltaCaption: { ...typography.label, textTransform: "none", letterSpacing: 0, fontSize: 11 },
|
||||
plot: { height: CHART_HEIGHT },
|
||||
});
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { TextField } from "../TextField";
|
||||
import { DateField } from "../DateField";
|
||||
import { Button } from "../Button";
|
||||
import { Card } from "../Card";
|
||||
import { WeightChart } from "./WeightChart";
|
||||
import { getTypography, type ColorTokens, type Typography } from "../../theme/tokens";
|
||||
import { displayDateToIso, todayAsDisplayDate } from "../../lib/date-format";
|
||||
|
||||
@@ -82,20 +83,23 @@ export function WeightLogSection({ petId }: { petId: string }) {
|
||||
) : logs.length === 0 ? (
|
||||
<Text style={styles.empty}>{t("health.weightEmpty")}</Text>
|
||||
) : (
|
||||
logs.map((log) => (
|
||||
<View key={log.id} style={styles.row}>
|
||||
<View style={styles.rowText}>
|
||||
<Text style={styles.rowPrimary}>{(log.weightGrams / 1000).toLocaleString(i18n.language)} kg</Text>
|
||||
<Text style={styles.rowSecondary}>
|
||||
{new Date(log.date).toLocaleDateString(i18n.language)}
|
||||
{log.notes ? ` · ${log.notes}` : ""}
|
||||
</Text>
|
||||
<>
|
||||
<WeightChart entries={logs} />
|
||||
{logs.map((log) => (
|
||||
<View key={log.id} style={styles.row}>
|
||||
<View style={styles.rowText}>
|
||||
<Text style={styles.rowPrimary}>{(log.weightGrams / 1000).toLocaleString(i18n.language)} kg</Text>
|
||||
<Text style={styles.rowSecondary}>
|
||||
{new Date(log.date).toLocaleDateString(i18n.language)}
|
||||
{log.notes ? ` · ${log.notes}` : ""}
|
||||
</Text>
|
||||
</View>
|
||||
<Pressable onPress={() => deleteMutation.mutate({ id: log.id, petId })} hitSlop={8}>
|
||||
<Trash2 color={colors.quietGray} size={16} />
|
||||
</Pressable>
|
||||
</View>
|
||||
<Pressable onPress={() => deleteMutation.mutate({ id: log.id, petId })} hitSlop={8}>
|
||||
<Trash2 color={colors.quietGray} size={16} />
|
||||
</Pressable>
|
||||
</View>
|
||||
))
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user