1.8 KiB
1.8 KiB
Walkthrough - Revenue Chart Fix
I have fixed the issue where the revenue chart was not displaying data on the business dashboard. The root cause was a mismatch between the PostgreSQL output format for month names and the expected format in the frontend JavaScript, combined with incorrect database grouping.
Changes Made
Business Module
DashboardController.php
I updated the revenue method to ensure correct data aggregation and formatting for PostgreSQL:
- FM (Fill Mode): Changed
TO_CHAR(created_at, 'Month')toTO_CHAR(created_at, 'FMMonth'). This removes trailing spaces from month names (e.g., "January " becomes "January"), allowing them to match the keys expected by the frontend. - Grouping and Sorting: Replaced
groupBy('created_at')andorderBy('created_at')with logic that correctly aggregates data by month usingDATE_TRUNC('month', created_at).
- ->selectRaw("TO_CHAR(created_at, 'Month') as month, SUM(ABS(lossProfit)) as total")
- ->orderBy('created_at')
- ->groupBy('created_at')
+ ->selectRaw("TO_CHAR(created_at, 'FMMonth') as month, SUM(ABS(lossProfit)) as total")
+ ->groupByRaw("TO_CHAR(created_at, 'FMMonth'), DATE_TRUNC('month', created_at)")
+ ->orderByRaw("DATE_TRUNC('month', created_at)")
Verification Results
Logic Verification
- The use of
FMMonthensures that PostgreSQL returns strings like "January", "February", which directly match thegetMonthNameFromIndexfunction inbusiness-dashboard.js. - The updated grouping ensures that all sales within a single month are summed together into one data point per month, as expected by the chart rendering logic.