@@ -11,14 +11,14 @@ import {
1111 Tooltip ,
1212 Legend ,
1313} from "chart.js/auto" ;
14- import labourProductivityData from "@/metrics/statscan/labour-productivity.json" ;
1514import {
1615 getPrimaryLineStyling ,
1716 getTargetLineStyling ,
1817 getTrendLineStyling ,
1918} from "@/components/charts/utils/styling" ;
2019import { LineChartDataset } from "@/components/charts/types" ;
2120import { calculateLinearTrend } from "./utils/trendCalculator" ;
21+ import useSWR from "swr" ;
2222
2323ChartJS . register (
2424 CategoryScale ,
@@ -42,6 +42,8 @@ interface LabourProductivityGrowthChartProps {
4242 showTrend ?: boolean ;
4343}
4444
45+ const fetcher = ( url : string ) => fetch ( url ) . then ( ( res ) => res . json ( ) ) ;
46+
4547export default function LabourProductivityGrowthChart ( {
4648 title = "Labour Productivity Growth" ,
4749 sector = "Total economy" ,
@@ -53,16 +55,38 @@ export default function LabourProductivityGrowthChart({
5355 showProductivityIndex = false ,
5456 showTrend = true ,
5557} : LabourProductivityGrowthChartProps ) {
56- // Get data for selected sector
57- const productivityDataObj = labourProductivityData as any ;
58- const sectorData = productivityDataObj . data [ sector ] || [ ] ;
58+ const {
59+ data : productivityData ,
60+ error,
61+ isLoading,
62+ } = useSWR (
63+ "/tracker/api/v1/statcan_datasets/labour-productivity-quarterly" ,
64+ fetcher ,
65+ ) ;
5966
60- // Filter data by year range
61- const filteredData = sectorData . filter ( ( dataPoint : [ string , number ] ) => {
62- const dateStr = dataPoint [ 0 ] ;
63- const year = parseInt ( dateStr . split ( "-" ) [ 0 ] ) ;
64- return year >= startYear && year <= endYear ;
65- } ) ;
67+ if ( isLoading ) {
68+ return < div > Loading labour productivity data...</ div > ;
69+ }
70+
71+ if ( error || ! productivityData ) {
72+ return < div > Error loading labour productivity data</ div > ;
73+ }
74+
75+ // Transform the new API data format
76+ const apiData = productivityData . current_data || [ ] ;
77+
78+ // Filter data by sector and year range
79+ const filteredData = apiData
80+ . filter ( ( item : any ) => {
81+ const year = parseInt ( item . REF_DATE . split ( "-" ) [ 0 ] ) ;
82+ return (
83+ year >= startYear &&
84+ year <= endYear &&
85+ item [ "North American Industry Classification System (NAICS)" ] === sector
86+ ) ;
87+ } )
88+ . map ( ( item : any ) => [ item . REF_DATE , item . VALUE ] )
89+ . sort ( ( a : any , b : any ) => a [ 0 ] . localeCompare ( b [ 0 ] ) ) ;
6690
6791 // Calculate year-over-year growth rates
6892 const growthRates : ( number | null ) [ ] = filteredData . map (
0 commit comments