File size: 6,245 Bytes
e1cc3bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | import { useRef, useEffect } from "react";
import { Chart, registerables } from "chart.js";
import { useTheme } from "../hooks/useTheme.ts";
import type { MonthlyProjection } from "../types.ts";
Chart.register(...registerables);
interface ProjectionChartProps {
userProjections: MonthlyProjection[];
templateProjections: MonthlyProjection[] | null;
templateName?: string;
}
export function ProjectionChart({
userProjections,
templateProjections,
templateName,
}: ProjectionChartProps) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const chartRef = useRef<Chart | null>(null);
const theme = useTheme();
// Create chart on mount, rebuild on theme change
useEffect(() => {
if (!canvasRef.current) return;
const textColor = theme === "dark" ? "#9ca3af" : "#6b7280";
const gridColor = theme === "dark" ? "#374151" : "#e5e7eb";
chartRef.current = new Chart(canvasRef.current, {
type: "line",
data: {
labels: Array.from({ length: 12 }, (_, i) => `M${i + 1}`),
datasets: [
// User scenario (solid lines)
{
label: "MRR",
borderColor: "#3b82f6",
backgroundColor: "rgba(59, 130, 246, 0.1)",
data: [],
fill: false,
tension: 0.3,
pointRadius: 0,
borderWidth: 2,
},
{
label: "Gross Profit",
borderColor: "#10b981",
backgroundColor: "rgba(16, 185, 129, 0.1)",
data: [],
fill: false,
tension: 0.3,
pointRadius: 0,
borderWidth: 2,
},
{
label: "Net Profit",
borderColor: "#f59e0b",
backgroundColor: "rgba(245, 158, 11, 0.1)",
data: [],
fill: false,
tension: 0.3,
pointRadius: 0,
borderWidth: 2,
},
// Template comparison (dashed lines)
{
label: "Template MRR",
borderColor: "#3b82f6",
borderDash: [5, 5],
data: [],
fill: false,
tension: 0.3,
pointRadius: 0,
borderWidth: 1.5,
hidden: true,
},
{
label: "Template Gross",
borderColor: "#10b981",
borderDash: [5, 5],
data: [],
fill: false,
tension: 0.3,
pointRadius: 0,
borderWidth: 1.5,
hidden: true,
},
{
label: "Template Net",
borderColor: "#f59e0b",
borderDash: [5, 5],
data: [],
fill: false,
tension: 0.3,
pointRadius: 0,
borderWidth: 1.5,
hidden: true,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
interaction: {
intersect: false,
mode: "index",
},
plugins: {
legend: { display: false },
tooltip: {
backgroundColor: theme === "dark" ? "#1f2937" : "#ffffff",
titleColor: theme === "dark" ? "#f9fafb" : "#111827",
bodyColor: theme === "dark" ? "#9ca3af" : "#6b7280",
borderColor: gridColor,
borderWidth: 1,
callbacks: {
label: (context) => {
const value = context.parsed.y;
if (value === null) return "";
const formatted =
Math.abs(value) >= 1000
? `$${(value / 1000).toFixed(1)}K`
: `$${value.toFixed(0)}`;
return `${context.dataset.label}: ${formatted}`;
},
},
},
},
scales: {
y: {
grid: { color: gridColor },
ticks: {
color: textColor,
callback: (value) => {
const num = Number(value);
if (Math.abs(num) >= 1000) {
return `$${(num / 1000).toFixed(0)}K`;
}
return `$${num}`;
},
},
},
x: {
grid: { display: false },
ticks: { color: textColor },
},
},
},
});
return () => chartRef.current?.destroy();
}, [theme]);
// Update data when projections change
useEffect(() => {
if (!chartRef.current) return;
const chart = chartRef.current;
chart.data.datasets[0].data = userProjections.map((p) => p.mrr);
chart.data.datasets[1].data = userProjections.map((p) => p.grossProfit);
chart.data.datasets[2].data = userProjections.map((p) => p.netProfit);
if (templateProjections) {
chart.data.datasets[3].data = templateProjections.map((p) => p.mrr);
chart.data.datasets[4].data = templateProjections.map(
(p) => p.grossProfit,
);
chart.data.datasets[5].data = templateProjections.map((p) => p.netProfit);
chart.data.datasets[3].hidden = false;
chart.data.datasets[4].hidden = false;
chart.data.datasets[5].hidden = false;
} else {
chart.data.datasets[3].hidden = true;
chart.data.datasets[4].hidden = true;
chart.data.datasets[5].hidden = true;
}
chart.update("none"); // Skip animation for smoother slider updates
}, [userProjections, templateProjections]);
return (
<section className="chart-section">
<h2 className="section-title">12-Month Projection</h2>
<div className="chart-container">
<canvas ref={canvasRef} />
</div>
<div className="chart-legend">
<span className="legend-item">
<span style={{ color: "#3b82f6" }}>--</span> MRR
</span>
<span className="legend-item">
<span style={{ color: "#10b981" }}>--</span> Gross Profit
</span>
<span className="legend-item">
<span style={{ color: "#f59e0b" }}>--</span> Net Profit
</span>
{templateProjections && (
<span className="legend-item legend-template">
(dashed = {templateName})
</span>
)}
</div>
</section>
);
}
|