From ceb389cfb006276d0d8e50dc2f9e402c87b06c95 Mon Sep 17 00:00:00 2001 From: Christian Vidal Wolf Date: Mon, 2 Feb 2026 11:38:26 +0100 Subject: [PATCH] feat: Add YTD (Year-To-Date) cumulative lines to chart - Calculate accumulated Sell Out and Units per week for each year - Add toggle button to show/hide YTD lines (cyan color) - YTD lines use thicker stroke with lower opacity for distinction --- components/DataGrid.tsx | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/components/DataGrid.tsx b/components/DataGrid.tsx index d9a99d8..e32c264 100644 --- a/components/DataGrid.tsx +++ b/components/DataGrid.tsx @@ -249,6 +249,7 @@ const DataGrid: React.FC = ({ data, hasCustomerFilter, adsData, s const [visibleMetrics, setVisibleMetrics] = useState<('sellOut' | 'units')[]>(['sellOut', 'units']); const [showAdsMetrics, setShowAdsMetrics] = useState(true); const [showAttributedSales, setShowAttributedSales] = useState(false); + const [showYTD, setShowYTD] = useState(false); const [showOnlyTop50, setShowOnlyTop50] = useState(false); const [showDimensionMenu, setShowDimensionMenu] = useState(false); const dimensionRef = useRef(null); @@ -350,6 +351,27 @@ const DataGrid: React.FC = ({ data, hasCustomerFilter, adsData, s }); } + // Calculate YTD (Year-To-Date) cumulative values for each year + const ytdAccumulators: { [year: string]: { sellOut: number; units: number } } = {}; + yearsInView.forEach((year: string) => { + ytdAccumulators[year] = { sellOut: 0, units: 0 }; + }); + + salesChartData.forEach((point: any) => { + yearsInView.forEach((year: string) => { + const sellOutKey = isMultiYear ? `${year}_sellOut` : 'sellOut'; + const unitsKey = isMultiYear ? `${year}_units` : 'units'; + + // Accumulate values + ytdAccumulators[year].sellOut += (point[sellOutKey] as number) || 0; + ytdAccumulators[year].units += (point[unitsKey] as number) || 0; + + // Add YTD values directly to point + point[`${year}_ytdSellOut`] = ytdAccumulators[year].sellOut; + point[`${year}_ytdUnits`] = ytdAccumulators[year].units; + }); + }); + return { chartData: salesChartData, uniqueYears: yearsInView, @@ -700,6 +722,33 @@ const DataGrid: React.FC = ({ data, hasCustomerFilter, adsData, s strokeWidth={2} dot={false} /> + ), + // YTD Sell Out line (cumulative) + showYTD && visibleMetrics.includes('sellOut') && ( + + ), + // YTD Units line (cumulative) + showYTD && visibleMetrics.includes('units') && ( + ) ]) ) : ( @@ -856,6 +905,17 @@ const DataGrid: React.FC = ({ data, hasCustomerFilter, adsData, s {showAttributedSales ? 'Hide Attr. Sales' : 'Show Attr. Sales'} )} + + {/* YTD (Year-To-Date) Toggle */} +