Skip to content

Commit

Permalink
Refactor normalizeChartData
Browse files Browse the repository at this point in the history
  • Loading branch information
atrincas committed Dec 11, 2024
1 parent afb6bbf commit 883e01c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions client/src/lib/normalize-widget-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ function getResponseRate(data: WidgetData) {
if (!data.chart || data.chart.length === 0) return 0;

const total = data.chart[0].total;
const notAnsweredTotal =
data.chart.find((c) => c.label === "N/A")?.value || 0;
const respondedTotal = total - notAnsweredTotal;
const totalWithoutNA = calculateTotalWithoutNA(data.chart);

return Math.round((respondedTotal / total) * 100);
return Math.round((totalWithoutNA / total) * 100);
}

/**
Expand All @@ -62,14 +60,22 @@ function calculateMapTotal(mapData: WidgetMapData): number {
}

/**
* Calculates percentage values for chart data using the total value
* without N/A, plus filters out N/A from the array
* Calculates the total sum of values without N/A values
*/
function normalizeChartData(chartData: WidgetChartData): WidgetChartData {
const totalWithoutNA = chartData.reduce(
function calculateTotalWithoutNA(chartData: WidgetChartData): number {
return chartData.reduce(
(acc, curr) => (curr.label !== "N/A" ? acc + curr.value : acc),
0,
);
}

/**
* Calculates percentage values for chart data using the total value
* without N/A, plus filters out N/A from the array
*/
function normalizeChartData(chartData: WidgetChartData): WidgetChartData {
const totalWithoutNA = calculateTotalWithoutNA(chartData);

return chartData
.map((entry) => ({
...entry,
Expand Down

0 comments on commit 883e01c

Please sign in to comment.