From 5dd5a29a9c46c43ef5f15b0bd74a6a5bdf385e48 Mon Sep 17 00:00:00 2001 From: Giuliano Paschoalino Date: Wed, 25 Feb 2026 14:04:45 -0300 Subject: [PATCH] Refactor GrossAnualChart and Dashboard components: improve data handling and filtering logic for better accuracy in displayed values --- .../graph/grossAnualChart/GrossAnualChart.tsx | 31 ++++++++++++------- src/pages/dashboard/index.tsx | 4 +-- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/components/graph/grossAnualChart/GrossAnualChart.tsx b/src/components/graph/grossAnualChart/GrossAnualChart.tsx index 3df79f8..a5daa5f 100644 --- a/src/components/graph/grossAnualChart/GrossAnualChart.tsx +++ b/src/components/graph/grossAnualChart/GrossAnualChart.tsx @@ -85,10 +85,16 @@ export function GrossAnualChart({ title, subtitle, dataProps = [], label, datase color: '#255488', clip: false, formatter: (value, ctx) => { - const percentage = (dataProps[ctx.dataIndex]?.econ_percentual * 100).toFixed(0) + "%"; - const result = `${spacement(parseInt(value).toLocaleString('pt-br'))}${percentage}\n${parseInt(value).toLocaleString('pt-br')}${spacement(parseInt(value).toLocaleString('pt-br'))}` - - return value == null ? null : result + if (value == null) return null + const year = labels[ctx.dataIndex] + const estimatedEntry = dataProps.find((d) => String(d.ano) === String(year) && d.dad_estimado === true) + const consolidatedEntry = dataProps.find((d) => String(d.ano) === String(year) && d.dad_estimado === false) + const topEntry = estimatedEntry || consolidatedEntry + if (!topEntry) return null + const totalValue = parseFloat(topEntry.economia_acumulada) + const percentage = (parseFloat(topEntry.econ_percentual) * 100).toFixed(0) + "%" + const formatted = parseInt(String(totalValue)).toLocaleString('pt-br') + return `${spacement(formatted)}${percentage}\n${formatted}${spacement(formatted)}` }, anchor: 'end', align: 'end', @@ -115,13 +121,15 @@ export function GrossAnualChart({ title, subtitle, dataProps = [], label, datase // Build dataset arrays aligned to `labels` (years). This avoids index misalignment // when `dataProps` is not in the same order or has missing/extra items const consolidatedData = labels.map((lbl) => { + const hasEstimated = dataProps.some((d) => String(d.ano) === String(lbl) && d.dad_estimado === true) + if (hasEstimated) return null const match = dataProps.find((d) => String(d.ano) === String(lbl) && d.dad_estimado === false) return match ? parseFloat(match.economia_acumulada) : null }) const estimatedData = labels.map((lbl) => { - const match = dataProps.find((d) => String(d.ano) === String(lbl) && d.dad_estimado === true) - return match ? parseFloat(match.economia_acumulada) : null + const estimated = dataProps.find((d) => String(d.ano) === String(lbl) && d.dad_estimado === true) + return estimated ? parseFloat(estimated.economia_acumulada) : null }) const data: any = { @@ -133,10 +141,11 @@ export function GrossAnualChart({ title, subtitle, dataProps = [], label, datase // stacked: true, data: consolidatedData, datalabels: { - // backgroundColor: '#255488', - // borderRadius: 8, - // opacity: .8, - display: (ctx) => ctx.dataIndex === 0, // Exibe apenas o primeiro + display: (ctx) => { + const year = labels[ctx.dataIndex] + const hasEstimated = dataProps.some((d) => String(d.ano) === String(year) && d.dad_estimado === true) + return !hasEstimated + }, }, skipNull: true, borderRadius: 8, @@ -146,7 +155,7 @@ export function GrossAnualChart({ title, subtitle, dataProps = [], label, datase type: 'bar', label: 'Estimado', datalabels: { - // keep the previous behaviour of offsetting the second estimated bar if needed + display: true, }, data: estimatedData, skipNull: true, diff --git a/src/pages/dashboard/index.tsx b/src/pages/dashboard/index.tsx index 7470b09..fae9245 100644 --- a/src/pages/dashboard/index.tsx +++ b/src/pages/dashboard/index.tsx @@ -104,10 +104,10 @@ export default function Dashboard({ grossAnualGraph, grossAnualYears, grossMensa setLastConsolidatedYearAnual(lastYearAnual) if (lastYearAnual !== null) { - // Filter to show only last consolidated year and next 6 years (7 years total) + const minYearAnual = Math.min(...grossAnualGraph.map((item) => parseInt(item.ano))) const filteredAnualData = grossAnualGraph.filter((item) => { const year = parseInt(item.ano) - return year >= lastYearAnual && year <= lastYearAnual + 6 + return year >= minYearAnual && year <= minYearAnual + 6 }) setProcessedAnualData(filteredAnualData) setProcessedAnualYears(filteredAnualData.map((value) => value.ano))