feat(chart): кнопка графика прогрессии упражнения
SVG-график максимального веса по завершённым тренировкам с объединением вариантов одной группы, подсказками на точках и списком последних результатов.
This commit is contained in:
@@ -21,6 +21,7 @@ import './modals/client-select-modal.js';
|
||||
import './modals/edit-exercise-modal.js';
|
||||
import './modals/edit-workout-date-modal.js';
|
||||
import './modals/group-modal.js';
|
||||
import './modals/chart-modal.js';
|
||||
|
||||
function renderHeader() {
|
||||
const modeLabel = state.mode === 'trainer' ? '👨🏫 Тренер' : '👤 Личный';
|
||||
|
||||
@@ -151,6 +151,41 @@ export function getExerciseHistory(exerciseName, clientId = null, excludeWorkout
|
||||
return history.slice(0, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Полная история для графика (без среза в 5 записей): варианты из одной
|
||||
* группы складывают в одну прогрессию.
|
||||
*/
|
||||
export function getExerciseChart(exerciseName, clientId = null) {
|
||||
const variantNames = refVariantNames(exerciseName);
|
||||
const targetClient = normalizeClientId(clientId);
|
||||
const points = [];
|
||||
|
||||
for (const w of state.workouts) {
|
||||
if (!isSameClient(w.client_id, targetClient)) continue;
|
||||
if (!w.endTime) continue; // активная тренировка ещё не результат
|
||||
|
||||
for (const ex of w.exercises || []) {
|
||||
if (!variantNames.has(normalizeName(ex.name))) continue;
|
||||
let maxWeight = 0, totalReps = 0;
|
||||
for (const s of ex.sets || []) {
|
||||
const wgt = s.isDropset && s.subSets?.length
|
||||
? Math.max(...s.subSets.map(x => parseFloat(x.weight) || 0))
|
||||
: (parseFloat(s.weight) || 0);
|
||||
if (wgt > maxWeight) maxWeight = wgt;
|
||||
totalReps += s.isDropset && s.subSets?.length
|
||||
? s.subSets.reduce((a, x) => a + (parseInt(x.reps) || 0), 0)
|
||||
: (parseInt(s.reps) || 0);
|
||||
}
|
||||
if (maxWeight > 0 || totalReps > 0) {
|
||||
points.push({ date: w.startTime, name: ex.name, maxWeight, totalReps });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
points.sort((a, b) => new Date(a.date) - new Date(b.date));
|
||||
return points;
|
||||
}
|
||||
|
||||
window.EL = {
|
||||
addExercise,
|
||||
toggleExercise,
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import { state } from '../state.js';
|
||||
import { escapeHtml } from '../utils.js';
|
||||
import { showModal, closeModal } from './modal-base.js';
|
||||
import { getExerciseChart } from '../logic/exercise.js';
|
||||
|
||||
/**
|
||||
* График прогрессии упражнения: максимальный вес (или повторения, если вес
|
||||
* не используется) по завершённым тренировкам. Варианты из одной группы
|
||||
* справочника складываются в одну линию.
|
||||
*/
|
||||
export function showChartDialog(exerciseName, clientId = null) {
|
||||
const points = getExerciseChart(exerciseName, clientId);
|
||||
|
||||
if (points.length === 0) {
|
||||
showModal(`
|
||||
<div class="modal-header"><span class="modal-title">${escapeHtml(exerciseName)}</span><button class="btn-icon" onclick="closeModal()">✕</button></div>
|
||||
<div class="history-hint" style="margin:8px 0">Нет завершённых тренировок с этим упражнением — график появится после первых результатов.</div>
|
||||
`);
|
||||
return;
|
||||
}
|
||||
|
||||
const byWeight = points.some(p => p.maxWeight > 0);
|
||||
const values = points.map(p => byWeight ? p.maxWeight : p.totalReps);
|
||||
const label = byWeight ? 'вес, кг' : 'повторы';
|
||||
|
||||
const W = 320, H = 170, padL = 30, padR = 12, padT = 14, padB = 24;
|
||||
const t0 = new Date(points[0].date).getTime();
|
||||
const t1 = new Date(points[points.length - 1].date).getTime();
|
||||
const span = Math.max(1, t1 - t0);
|
||||
const vMax = Math.max(...values), vMin = Math.min(...values);
|
||||
const vSpan = Math.max(1e-6, vMax - vMin);
|
||||
const x = t => padL + (W - padL - padR) * (new Date(t).getTime() - t0) / span;
|
||||
const y = v => padT + (H - padT - padB) * (1 - (v - vMin) / vSpan);
|
||||
|
||||
const pts = points.map(p => `${x(p.date).toFixed(1)},${y(byWeight ? p.maxWeight : p.totalReps).toFixed(1)}`);
|
||||
const circles = points.map((p, i) =>
|
||||
`<circle cx="${x(p.date).toFixed(1)}" cy="${y(values[i]).toFixed(1)}" r="3.5"><title>${new Date(p.date).toLocaleDateString('ru-RU')} — ${escapeHtml(p.name)}: ${values[i]} ${label === 'вес, кг' ? 'кг' : 'повт.'}</title></circle>`
|
||||
).join('');
|
||||
|
||||
const fmt = d => new Date(d).toLocaleDateString('ru-RU', { day: '2-digit', month: '2-digit' });
|
||||
const maxIdx = values.indexOf(vMax);
|
||||
|
||||
showModal(`
|
||||
<div class="modal-header"><span class="modal-title">📈 ${escapeHtml(exerciseName)}</span><button class="btn-icon" onclick="closeModal()">✕</button></div>
|
||||
<div class="chart-sub">${points.length} трен. · ${label} · варианты одной группы сложены вместе</div>
|
||||
<svg class="chart-svg" viewBox="0 0 ${W} ${H}">
|
||||
<line x1="${padL}" y1="${y(vMin).toFixed(1)}" x2="${W - padR}" y2="${y(vMin).toFixed(1)}" class="chart-axis"/>
|
||||
<line x1="${padL}" y1="${y(vMax).toFixed(1)}" x2="${W - padR}" y2="${y(vMax).toFixed(1)}" class="chart-axis"/>
|
||||
<text x="4" y="${(y(vMax) + 4).toFixed(1)}" class="chart-label">${vMax}</text>
|
||||
<text x="4" y="${(y(vMin) + 4).toFixed(1)}" class="chart-label">${vMin}</text>
|
||||
${points.length > 1 ? `<polyline points="${pts.join(' ')}" class="chart-line"/>` : ''}
|
||||
${circles}
|
||||
<text x="${padL}" y="${H - 8}" class="chart-label">${fmt(points[0].date)}</text>
|
||||
${points.length > 1 && new Date(points[points.length - 1].date) - new Date(points[0].date) > 864e5
|
||||
? `<text x="${W - padR}" y="${H - 8}" text-anchor="end" class="chart-label">${fmt(points[points.length - 1].date)}</text>` : ''}
|
||||
${points.length > 0 ? `<text x="${x(points[maxIdx].date).toFixed(1)}" y="${(y(values[maxIdx]) - 8).toFixed(1)}" text-anchor="middle" class="chart-label chart-max">макс ${values[maxIdx]}</text>` : ''}
|
||||
</svg>
|
||||
<div class="chart-list">${points.slice(-5).reverse().map(p =>
|
||||
`<div class="chart-row"><span class="chart-date">${new Date(p.date).toLocaleDateString('ru-RU')}</span><span class="chart-name">${escapeHtml(p.name)}</span><span class="chart-val">${byWeight ? p.maxWeight + ' кг' : p.totalReps + ' повт.'}</span></div>`
|
||||
).join('')}</div>
|
||||
`);
|
||||
}
|
||||
|
||||
// Клик с кнопки в карточке: имя берём из data-атрибута (без экранирования в JS-строке)
|
||||
export function showChartFromBtn(btn) {
|
||||
const name = btn.dataset.ex;
|
||||
const w = state.workouts.find(w => (w.exercises || []).some(e => e.name === name));
|
||||
showChartDialog(name, w ? w.client_id : null);
|
||||
}
|
||||
|
||||
window.CM = { showChartDialog, showChartFromBtn };
|
||||
@@ -1,4 +1,5 @@
|
||||
import { state } from '../state.js';
|
||||
import { escapeHtml } from '../utils.js';
|
||||
import { renderSetRow } from './set-row.js';
|
||||
|
||||
export function renderExercise(w, ex, isReadOnly = false) {
|
||||
@@ -15,6 +16,8 @@ export function renderExercise(w, ex, isReadOnly = false) {
|
||||
<span class="exercise-name">${ex.name}</span>
|
||||
<span class="exercise-summary">${summary}</span>
|
||||
</div>
|
||||
<button class="btn-icon chart-btn" data-ex="${escapeHtml(ex.name)}" title="График прогрессии"
|
||||
onclick="event.stopPropagation(); CM.showChartFromBtn(this)">📈</button>
|
||||
<span class="chevron ${isExpanded ? 'open' : ''}">›</span>
|
||||
</div>
|
||||
`;
|
||||
|
||||
@@ -130,3 +130,18 @@ textarea { resize: vertical; min-height: 60px; }
|
||||
border-color: var(--text);
|
||||
box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--text);
|
||||
}
|
||||
|
||||
/* === Progress chart === */
|
||||
.chart-btn { flex: 0 0 auto; font-size: 0.95rem; opacity: 0.75; }
|
||||
.chart-svg { width: 100%; height: auto; display: block; margin: 8px 0 4px; }
|
||||
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }
|
||||
.chart-svg circle { fill: var(--accent); stroke: var(--card-bg, #fff); stroke-width: 1.5; }
|
||||
.chart-axis { stroke: var(--border); stroke-width: 1; }
|
||||
.chart-label { font-size: 9px; fill: var(--text-secondary, #888); }
|
||||
.chart-max { fill: var(--accent); font-weight: 700; }
|
||||
.chart-sub { font-size: 0.72rem; color: var(--text-secondary, #888); }
|
||||
.chart-list { display: flex; flex-direction: column; gap: 2px; margin-top: 8px; }
|
||||
.chart-row { display: flex; justify-content: space-between; gap: 8px; font-size: 0.78rem; }
|
||||
.chart-date { color: var(--text-secondary, #888); flex: 0 0 auto; }
|
||||
.chart-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.chart-val { font-weight: 600; flex: 0 0 auto; }
|
||||
|
||||
Reference in New Issue
Block a user