feat(chart): рабочий максимум «вес × повторы» с зелёным прогрессом и красным откатом

This commit is contained in:
Иван Овчинников
2026-11-20 07:35:11 +03:00
parent 35fabb1610
commit 4da4fd9365
5 changed files with 129 additions and 82 deletions
+52 -52
View File
@@ -2,6 +2,7 @@
import { state } from '../state.js';
import { formatDate } from '../utils.js';
import { getExerciseChart } from '../logic/exercise.js';
/**
* Рисует график прогресса упражнения: каждая точка — подход (вес по вертикали,
@@ -38,37 +39,60 @@ export function drawExerciseProgress(canvas, exerciseName) {
const y = v => padding.top + chartH - (v / maxWeight) * chartH;
const maxReps = Math.max(1, ...days.flatMap(d => d.sets.map(s => s.reps)));
// Соединительная линия максимальных весов (силовая прогрессия)
if (n > 1) {
ctx.strokeStyle = '#bb86fc';
ctx.lineWidth = 2;
// Прогресс вершины относительно прошлой тренировки: вес выше — или тот же
// вес и больше повторов => зелёный; меньше => красный; ровно => фиолетовый.
const COLORS = { up: '#4caf50', down: '#e57373', flat: '#bb86fc' };
const cls = i => {
if (i === 0) return 'flat';
const a = days[i - 1], b = days[i];
if (b.max > a.max) return 'up';
if (b.max < a.max) return 'down';
if ((b.topReps || 0) > (a.topReps || 0)) return 'up';
if ((b.topReps || 0) < (a.topReps || 0)) return 'down';
return 'flat';
};
const classes = days.map((_, i) => cls(i));
// Линия рабочего максимума: отрезки окрашены по прогрессу конца отрезка
for (let i = 1; i < n; i++) {
ctx.strokeStyle = COLORS[classes[i]];
ctx.lineWidth = 2.5;
ctx.beginPath();
days.forEach((d, i) => {
const px = x(i), py = y(d.max);
i === 0 ? ctx.moveTo(px, py) : ctx.lineTo(px, py);
});
ctx.moveTo(x(i - 1), y(days[i - 1].max));
ctx.lineTo(x(i), y(days[i].max));
ctx.stroke();
}
days.forEach((d, i) => {
const px = x(i);
// Подходы дня: точки по весу, разнесены вокруг колонки дня
const spread = d.sets.length > 1 ? Math.min(11, chartW / (3 * n)) : 0;
d.sets.forEach((s, k) => {
const dx = d.sets.length > 1 ? -spread + (2 * spread * k) / (d.sets.length - 1) : 0;
const r = 2.2 + 3.2 * Math.min(1, s.reps / maxReps);
// Подходы дня ниже максимума — тусклые фоновые точки
const below = d.sets.filter(s => s.weight !== d.max);
const spread = below.length > 1 ? Math.min(11, chartW / (3 * n)) : 0;
below.forEach((s, k) => {
const dx = below.length > 1 ? -spread + (2 * spread * k) / (below.length - 1) : 0;
const r = 1.8 + 2.4 * Math.min(1, s.reps / maxReps);
ctx.beginPath();
ctx.fillStyle = s.weight === d.max ? '#bb86fc' : 'rgba(187, 134, 252, 0.45)';
ctx.fillStyle = 'rgba(187, 134, 252, 0.35)';
ctx.arc(px + dx, y(s.weight), r, 0, Math.PI * 2);
ctx.fill();
if (s.weight === d.max) {
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 1.2;
ctx.stroke();
}
});
// Вершина: рабочий максимум «вес × повторы» с цветом прогресса
ctx.beginPath();
ctx.fillStyle = COLORS[classes[i]];
ctx.arc(px, y(d.max), 4.2, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 1.2;
ctx.stroke();
if (n <= 10 || i >= n - 4 || classes[i] !== 'flat') {
ctx.fillStyle = COLORS[classes[i]];
ctx.font = 'bold 10px sans-serif';
ctx.textAlign = 'center';
ctx.fillText(`${d.max}×${d.topReps || ''}`, px, y(d.max) - 7);
}
// Количество подходов (выносливость)
if (n <= 12 || i % 2 === 0) {
ctx.fillStyle = '#b0b0b0';
@@ -89,45 +113,21 @@ export function drawExerciseProgress(canvas, exerciseName) {
}
drawLegend(ctx, width, [
{ color: '#bb86fc', label: 'макс. вес за тренировку' },
{ color: 'rgba(187, 134, 252, 0.45)', label: 'точка = подход (размер = повторы)' },
{ color: '#4caf50', label: 'рабочий максимум: зелёный — прогресс, красный — откат' },
{ color: 'rgba(187, 134, 252, 0.45)', label: 'тусклая точка = подход (размер = повторы)' },
]);
}
/**
* Данные графика: по каждой завершённой тренировке — список подходов
* (дроп-сеты развёрнуты), максимальный вес и общий объём.
* @returns {{date: Date, sets: {weight:number, reps:number}[], max:number, totalVolume:number}[]}
* (дроп-сеты развёрнуты) и максимальный вес. Варианты упражнения из одной
* группы справочника объединяются в одну линию (общий источник —
* getExerciseChart из logic/exercise.js).
*/
export function getExerciseProgressData(exerciseName) {
const normalized = exerciseName.trim().toLowerCase();
const days = [];
state.workouts.forEach(w => {
if (!w.endTime) return; // активная тренировка — не результат
(w.exercises || []).forEach(ex => {
if (ex.name.trim().toLowerCase() !== normalized) return;
const sets = [];
(ex.sets || []).forEach(s => {
const parts = s.isDropset && s.subSets?.length ? s.subSets : [s];
parts.forEach(p => {
const weight = parseFloat(p.weight) || 0;
const reps = parseInt(p.reps) || 0;
if (weight > 0 || reps > 0) sets.push({ weight, reps });
});
});
if (sets.length === 0) return;
days.push({
date: new Date(w.startTime),
sets,
max: Math.max(...sets.map(s => s.weight)),
totalVolume: sets.reduce((a, s) => a + s.weight * s.reps, 0),
});
});
});
days.sort((a, b) => a.date - b.date);
return days.slice(-10);
export function getExerciseProgressData(exerciseName, clientId = null) {
return getExerciseChart(exerciseName, clientId)
.map(p => ({ date: new Date(p.date), sets: p.sets, max: p.maxWeight, topReps: p.topReps }))
.slice(-10);
}
/**