Skip to content

Commit 878924e

Browse files
authored
Merge pull request #160 from BuildCanada/data-from-api
Use labour productivity data from the API
2 parents 7c82ca2 + b688876 commit 878924e

File tree

5 files changed

+69
-3838
lines changed

5 files changed

+69
-3838
lines changed

.github/workflows/scrape-labour-productivity.yaml

Lines changed: 0 additions & 35 deletions
This file was deleted.

components/charts/GDPChart.tsx

Lines changed: 0 additions & 198 deletions
This file was deleted.

components/charts/LabourProductivityGrowthChart.tsx

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import {
1111
Tooltip,
1212
Legend,
1313
} from "chart.js/auto";
14-
import labourProductivityData from "@/metrics/statscan/labour-productivity.json";
1514
import {
1615
getPrimaryLineStyling,
1716
getTargetLineStyling,
1817
getTrendLineStyling,
1918
} from "@/components/charts/utils/styling";
2019
import { LineChartDataset } from "@/components/charts/types";
2120
import { calculateLinearTrend } from "./utils/trendCalculator";
21+
import useSWR from "swr";
2222

2323
ChartJS.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+
4547
export 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(

components/charts/ProductivityChart.tsx

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import {
1111
Tooltip,
1212
Legend,
1313
} from "chart.js/auto";
14-
import labourProductivityData from "@/metrics/statscan/labour-productivity.json";
1514
import {
1615
getPrimaryLineStyling,
1716
getTargetLineStyling,
1817
getTrendLineStyling,
1918
} from "@/components/charts/utils/styling";
2019
import { calculateLinearTrend } from "@/components/charts/utils/trendCalculator";
2120
import { LineChartDataset } from "@/components/charts/types";
21+
import useSWR from "swr";
2222

2323
ChartJS.register(
2424
CategoryScale,
@@ -42,27 +42,51 @@ interface ProductivityChartProps {
4242
showTrend?: boolean;
4343
}
4444

45+
const fetcher = (url: string) => fetch(url).then((res) => res.json());
46+
4547
export default function ProductivityChart({
4648
title = "Public Service Productivity",
4749
sector = "Non-business sector and others",
4850
startYear = 2015,
49-
endYear = 2024,
51+
endYear = 2025,
5052
quarterlyData = true,
5153
showTarget = true,
5254
targetValue = 120,
5355
showGrowthRate = false,
5456
showTrend = true,
5557
}: ProductivityChartProps) {
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 productivity data...</div>;
69+
}
70+
71+
if (error || !productivityData) {
72+
return <div>Error loading 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
// Format dates for display
6892
const labels = filteredData.map((dataPoint: [string, number]) => {

0 commit comments

Comments
 (0)