Profil: lzejsze wykresy parametrow ciala - gradient pod linia, punkty pomiarowe, osie bez linii, rok na osi przy dluzszej historii
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useMemo, useRef, useState } from 'react';
|
||||
import { useMemo, useRef, useState, type ReactNode } from 'react';
|
||||
import { Plus, Ruler, Scale, Trash2, type LucideIcon } from 'lucide-react';
|
||||
import {
|
||||
addCircumference,
|
||||
@@ -8,6 +8,8 @@ import {
|
||||
useBodyParameters,
|
||||
} from '../../api/queries';
|
||||
import {
|
||||
Area,
|
||||
AreaChart,
|
||||
LineChart,
|
||||
Line,
|
||||
XAxis,
|
||||
@@ -51,6 +53,26 @@ const TOOLTIP_STYLE = {
|
||||
color: 'var(--text)',
|
||||
};
|
||||
|
||||
/* Wykresy: oś bez linii i kresek, drobny dimowany tick — tło siatki tylko poziome. */
|
||||
const AXIS_TICK = { fill: 'var(--text-dim)', fontSize: 12 };
|
||||
const CHART_MARGIN = { top: 8, right: 8, left: 0, bottom: 0 };
|
||||
|
||||
/* Kolor linii wg jednostki: waga = accent, obwody = info. */
|
||||
const unitStroke = (unit: PartGroup['unit']) => (unit === 'kg' ? 'var(--accent)' : 'var(--info)');
|
||||
/* Tooltip: wartość z jednostką, nazwa serii bez zmian (formatter zwraca parę [wartość, nazwa]). */
|
||||
const tooltipUnit =
|
||||
(unit: PartGroup['unit']) =>
|
||||
(value: ReactNode, name: ReactNode): [ReactNode, ReactNode] => [`${value} ${unit}`, name];
|
||||
|
||||
/* Punkty pomiarowe: pusty środek (karta pod spodem), ring w kolorze linii. */
|
||||
const lineDot = (stroke: string) => ({ r: 3, strokeWidth: 2, fill: 'var(--bg-card)', stroke });
|
||||
const lineActiveDot = (stroke: string) => ({
|
||||
r: 5,
|
||||
strokeWidth: 2,
|
||||
fill: 'var(--bg-card)',
|
||||
stroke,
|
||||
});
|
||||
|
||||
interface CircGroup {
|
||||
part: string;
|
||||
series: CircumferenceSeries[];
|
||||
@@ -130,11 +152,20 @@ function groupDelta(g: PartGroup): { text: string; className: string } | null {
|
||||
return { text: `${signed(d)} vs poprzedni`, className: deltaClass(d) };
|
||||
}
|
||||
|
||||
/** Etykieta osi X: `17.10`; gdy historia przekracza rok — `17.10.26`. */
|
||||
function axisDateLabel(iso: string, withYear: boolean): string {
|
||||
return new Date(iso).toLocaleDateString('pl-PL', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
...(withYear ? { year: '2-digit' } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Wspólna oś dat dla serii: `[{ date, lewy, prawy }]` (brak pomiaru = null).
|
||||
* Jednostronna grupa → kolumna wg bieżącej strony (osie nazywane po stronie).
|
||||
*/
|
||||
function sideSeriesData(points: GroupPoint[], sides: Side[]) {
|
||||
function sideSeriesData(points: GroupPoint[], sides: Side[], withYear: boolean) {
|
||||
const col = (side: Side) => (side === 'left' ? 'lewy' : 'prawy');
|
||||
const byDate = new Map<string, Record<string, number | null>>();
|
||||
for (const p of points) {
|
||||
@@ -147,7 +178,7 @@ function sideSeriesData(points: GroupPoint[], sides: Side[]) {
|
||||
return [...byDate.entries()]
|
||||
.sort((a, b) => a[0].localeCompare(b[0]))
|
||||
.map(([date, row]) => ({
|
||||
date: new Date(date).toLocaleDateString('pl-PL', { day: '2-digit', month: '2-digit' }),
|
||||
date: axisDateLabel(date, withYear),
|
||||
...row,
|
||||
}));
|
||||
}
|
||||
@@ -213,11 +244,16 @@ function MeasurementPanel({
|
||||
}) {
|
||||
const latest = group.points[group.points.length - 1];
|
||||
const sided = group.sided && group.sidesPresent.length > 0;
|
||||
// historia przekracza rok → oś X z rokiem (również przy przerwie >12 mies. w jednym roku)
|
||||
const withYear =
|
||||
new Date(group.points[group.points.length - 1].date).getTime() -
|
||||
new Date(group.points[0].date).getTime() >
|
||||
300 * 86_400_000;
|
||||
const singleData = group.points.map((p) => ({
|
||||
date: new Date(p.date).toLocaleDateString('pl-PL', { day: '2-digit', month: '2-digit' }),
|
||||
date: axisDateLabel(p.date, withYear),
|
||||
value: p.value,
|
||||
}));
|
||||
const sidedData = sided ? sideSeriesData(group.points, group.sidesPresent) : [];
|
||||
const sidedData = sided ? sideSeriesData(group.points, group.sidesPresent, withYear) : [];
|
||||
const chartReady = sided ? sidedData.length >= 2 : group.points.length >= 2;
|
||||
|
||||
return (
|
||||
@@ -245,11 +281,25 @@ function MeasurementPanel({
|
||||
{chartReady ? (
|
||||
<ResponsiveContainer width="100%" height={220}>
|
||||
{sided ? (
|
||||
<LineChart data={sidedData}>
|
||||
<CartesianGrid stroke="var(--border)" />
|
||||
<XAxis dataKey="date" stroke="var(--text-dim)" />
|
||||
<YAxis stroke="var(--text-dim)" domain={['auto', 'auto']} />
|
||||
<Tooltip contentStyle={TOOLTIP_STYLE} />
|
||||
<LineChart data={sidedData} margin={CHART_MARGIN}>
|
||||
<CartesianGrid stroke="var(--border)" vertical={false} />
|
||||
<XAxis
|
||||
dataKey="date"
|
||||
tickLine={false}
|
||||
axisLine={false}
|
||||
tickMargin={10}
|
||||
minTickGap={32}
|
||||
tick={AXIS_TICK}
|
||||
/>
|
||||
<YAxis
|
||||
width={44}
|
||||
tickLine={false}
|
||||
axisLine={false}
|
||||
tickMargin={8}
|
||||
domain={['auto', 'auto']}
|
||||
tick={AXIS_TICK}
|
||||
/>
|
||||
<Tooltip contentStyle={TOOLTIP_STYLE} formatter={tooltipUnit(group.unit)} />
|
||||
{group.sidesPresent.map((side) => (
|
||||
<Line
|
||||
key={side}
|
||||
@@ -258,25 +308,55 @@ function MeasurementPanel({
|
||||
name={SIDE_LABELS[side]}
|
||||
stroke={SIDE_STROKE[side]}
|
||||
strokeWidth={2}
|
||||
dot={false}
|
||||
dot={lineDot(SIDE_STROKE[side])}
|
||||
activeDot={lineActiveDot(SIDE_STROKE[side])}
|
||||
connectNulls
|
||||
/>
|
||||
))}
|
||||
</LineChart>
|
||||
) : (
|
||||
<LineChart data={singleData}>
|
||||
<CartesianGrid stroke="var(--border)" />
|
||||
<XAxis dataKey="date" stroke="var(--text-dim)" />
|
||||
<YAxis stroke="var(--text-dim)" domain={['auto', 'auto']} />
|
||||
<Tooltip contentStyle={TOOLTIP_STYLE} />
|
||||
<Line
|
||||
type="monotone"
|
||||
dataKey="value"
|
||||
stroke={group.unit === 'kg' ? 'var(--accent)' : 'var(--info)'}
|
||||
strokeWidth={2}
|
||||
dot={false}
|
||||
/>
|
||||
</LineChart>
|
||||
(() => {
|
||||
const stroke = unitStroke(group.unit);
|
||||
const areaId = `bp-area-${encodeURIComponent(group.part)}`;
|
||||
return (
|
||||
<AreaChart data={singleData} margin={CHART_MARGIN}>
|
||||
<defs>
|
||||
<linearGradient id={areaId} x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stopColor={stroke} stopOpacity={0.25} />
|
||||
<stop offset="100%" stopColor={stroke} stopOpacity={0} />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<CartesianGrid stroke="var(--border)" vertical={false} />
|
||||
<XAxis
|
||||
dataKey="date"
|
||||
tickLine={false}
|
||||
axisLine={false}
|
||||
tickMargin={10}
|
||||
minTickGap={32}
|
||||
tick={AXIS_TICK}
|
||||
/>
|
||||
<YAxis
|
||||
width={44}
|
||||
tickLine={false}
|
||||
axisLine={false}
|
||||
tickMargin={8}
|
||||
domain={['auto', 'auto']}
|
||||
tick={AXIS_TICK}
|
||||
/>
|
||||
<Tooltip contentStyle={TOOLTIP_STYLE} formatter={tooltipUnit(group.unit)} />
|
||||
<Area
|
||||
type="monotone"
|
||||
dataKey="value"
|
||||
name={group.part}
|
||||
stroke={stroke}
|
||||
strokeWidth={2}
|
||||
fill={`url(#${areaId})`}
|
||||
dot={lineDot(stroke)}
|
||||
activeDot={lineActiveDot(stroke)}
|
||||
/>
|
||||
</AreaChart>
|
||||
);
|
||||
})()
|
||||
)}
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
|
||||
Reference in New Issue
Block a user