From 0a80103cfc4cc7e0a01bac5b965079102afc0499 Mon Sep 17 00:00:00 2001 From: fahed Date: Tue, 7 Apr 2026 13:14:17 +0300 Subject: [PATCH] feat: add % toggle to channel performance, default events and channel to pie chart Co-Authored-By: Claude Sonnet 4.6 --- src/components/Dashboard.tsx | 70 +++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/src/components/Dashboard.tsx b/src/components/Dashboard.tsx index 55f7ec3..17079a9 100644 --- a/src/components/Dashboard.tsx +++ b/src/components/Dashboard.tsx @@ -80,8 +80,9 @@ function Dashboard({ data, seasons, userRole, showDataLabels, setShowDataLabels, const [trendGranularity, setTrendGranularity] = useState('week'); const [selectedSeason, setSelectedSeason] = useState(''); const [eventMetric, setEventMetric] = useState<'visitors' | 'revenue'>('revenue'); - const [eventChartType, setEventChartType] = useState<'bar' | 'pie'>('bar'); - const [channelChartType, setChannelChartType] = useState<'bar' | 'pie'>('bar'); + const [eventChartType, setEventChartType] = useState<'bar' | 'pie'>('pie'); + const [channelChartType, setChannelChartType] = useState<'bar' | 'pie'>('pie'); + const [channelDisplayMode, setChannelDisplayMode] = useState<'absolute' | 'percent'>('absolute'); const filteredData = useMemo(() => filterData(data, filters), [data, filters]); @@ -253,6 +254,16 @@ function Dashboard({ data, seasons, userRole, showDataLabels, setShowDataLabels, }; }, [seasonFilteredData, includeVAT]); + const channelChartData = useMemo(() => { + if (channelDisplayMode === 'absolute') return channelData; + const total = channelData.datasets[0].data.reduce((s: number, v: number) => s + v, 0); + if (total === 0) return channelData; + return { + ...channelData, + datasets: [{ ...channelData.datasets[0], data: channelData.datasets[0].data.map((v: number) => parseFloat(((v / total) * 100).toFixed(1))) }] + }; + }, [channelData, channelDisplayMode]); + // District data const districtData = useMemo(() => { const grouped = groupByDistrict(seasonFilteredData, includeVAT); @@ -650,15 +661,35 @@ function Dashboard({ data, seasons, userRole, showDataLabels, setShowDataLabels, title={t('dashboard.channelPerformance')} className="chart-container" controls={ -
- - +
+
+ + +
+
+ + +
} > {channelChartType === 'bar' - ? - : + ? channelDisplayMode === 'percent' ? v.toFixed(1) + '%' : baseOptions.plugins.datalabels.formatter(v, {} as any) } }, + scales: { ...baseOptions.scales, x: { ...baseOptions.scales.x, ticks: { ...baseOptions.scales.x.ticks, callback: (v: number | string) => channelDisplayMode === 'percent' ? v + '%' : v } } } + }} /> + : v > 3 ? v.toFixed(1) + '%' : '' } + : { display: false }, + tooltip: { ...pieOptions.plugins.tooltip, callbacks: { label: (ctx: any) => channelDisplayMode === 'percent' ? ` ${ctx.parsed.toFixed(1)}%` : ` ${formatCurrency(ctx.parsed)}` } } + } + }} /> }
@@ -767,14 +798,29 @@ function Dashboard({ data, seasons, userRole, showDataLabels, setShowDataLabels,

{t('dashboard.channelPerformance')}

-
- - +
+
+ + +
+
+ + +
{channelChartType === 'bar' - ? - : + ? + : v > 3 ? v.toFixed(1) + '%' : '' } + : { display: false }, + tooltip: { ...pieOptions.plugins.tooltip, callbacks: { label: (ctx: any) => channelDisplayMode === 'percent' ? ` ${ctx.parsed.toFixed(1)}%` : ` ${formatCurrency(ctx.parsed)}` } } + } + }} /> }