const fs = require("fs"); const path = require("path"); // Генерация SVG-графика (встроенная, без sharp/canvas) function drawBarChart(bars, monthName) { const W = 600, H = 400; const pad = { top: 50, right: 20, bottom: 70, left: 70 }; const cw = W - pad.left - pad.right; const ch = H - pad.top - pad.bottom; const maxY = Math.max(1, ...bars.map((b) => Math.max(b.income, b.expense))); const niceMax = Math.ceil(maxY / 10000) * 10000 || 10000; const n = bars.length; const groupW = cw / n; const barW = Math.min(28, groupW * 0.35); const out = []; out.push(``); out.push(``); out.push(`${monthName}`); for (let i = 0; i <= 4; i++) { const val = (niceMax / 4) * i; const y = pad.top + ch - (ch * val) / niceMax; out.push(``); out.push(`${Math.round(val / 1000)}k`); } bars.forEach((b, i) => { const cx = pad.left + groupW * i + groupW / 2; const hInc = (ch * b.income) / niceMax; const hExp = (ch * b.expense) / niceMax; out.push(``); out.push(``); out.push(`${b.date}`); }); out.push(``); out.push(`Доход`); out.push(``); out.push(`Расход`); out.push(``); return out.join("\n"); } function barsForMonth(userId, getPeriodSummary, now) { const from = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)); const to = new Date(now.getTime()); const s = getPeriodSummary(userId, from, to); const days = Object.keys(s.byDay || {}).sort(); return days.map((d) => { const v = s.byDay[d]; return { date: new Date(d).toLocaleDateString("ru-RU", { day: "2-digit", month: "2-digit" }), income: v.income, expense: v.expense }; }); } module.exports = { drawBarChart, barsForMonth };