From c80ffaa69a050c26f8d8defb5994be1d249afc49 Mon Sep 17 00:00:00 2001 From: Josh Pigford Date: Tue, 20 May 2025 13:25:54 -0500 Subject: [PATCH] Refactor Sankey chart controller to use Number.parseFloat for value formatting and improve code readability by restructuring conditional logic for node shapes. --- .../controllers/sankey_chart_controller.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/javascript/controllers/sankey_chart_controller.js b/app/javascript/controllers/sankey_chart_controller.js index 4536b6d9..9601b088 100644 --- a/app/javascript/controllers/sankey_chart_controller.js +++ b/app/javascript/controllers/sankey_chart_controller.js @@ -109,7 +109,7 @@ export default class extends Controller { .attr("stroke", (d, i) => `url(#link-gradient-${d.source.index}-${d.target.index}-${i})`) .attr("stroke-width", (d) => Math.max(1, d.width)) .append("title") - .text((d) => `${nodes[d.source.index].name} → ${nodes[d.target.index].name}: ${this.currencySymbolValue}${parseFloat(d.value).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })} (${d.percentage}%)`); + .text((d) => `${nodes[d.source.index].name} → ${nodes[d.target.index].name}: ${this.currencySymbolValue}${Number.parseFloat(d.value).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })} (${d.percentage}%)`); // Draw nodes const node = svg @@ -146,7 +146,9 @@ export default class extends Controller { Q ${x0},${y1} ${x0},${y1 - effectiveCornerRadius} L ${x0},${y0 + effectiveCornerRadius} Q ${x0},${y0} ${x0 + effectiveCornerRadius},${y0} Z`; - } else if (isTargetNode) { // Flat left corners, round right for Categories/Surplus + } + + if (isTargetNode) { // Flat left corners, round right for Categories/Surplus if (h < effectiveCornerRadius * 2) { return `M ${x0},${y0} L ${x1},${y0} L ${x1},${y1} L ${x0},${y1} Z`; } @@ -156,9 +158,10 @@ export default class extends Controller { L ${x1},${y1 - effectiveCornerRadius} Q ${x1},${y1} ${x1 - effectiveCornerRadius},${y1} L ${x0},${y1} Z`; - } else { // Fallback for intermediate nodes (e.g., "Cash Flow") - draw as a simple sharp-cornered rectangle - return `M ${x0},${y0} L ${x1},${y0} L ${x1},${y1} L ${x0},${y1} Z`; } + + // Fallback for intermediate nodes (e.g., "Cash Flow") - draw as a simple sharp-cornered rectangle + return `M ${x0},${y0} L ${x1},${y0} L ${x1},${y1} L ${x0},${y1} Z`; }) .attr("fill", (d) => d.color || "var(--color-gray-400)") .attr("stroke", (d) => { @@ -195,7 +198,7 @@ export default class extends Controller { .style("font-size", "0.65rem"); // Explicitly set smaller font size financialDetailsTspan.append("tspan") - .text(stimulusControllerInstance.currencySymbolValue + parseFloat(d.value).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })); + .text(stimulusControllerInstance.currencySymbolValue + Number.parseFloat(d.value).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })); }); } } \ No newline at end of file