fix(analysis): mark sub-inflation growth orange and show 12 inflation months
Reserve red for negative variations across all currency columns and the total row. Show the last 12 months with inflation data plus the trailing months without it, and compute the accumulated inflation and total variation over the same 12-month window. Extend the monthly-last backend window to 15 months.
This commit is contained in:
@@ -35,9 +35,10 @@ const MONTH_NAMES = [
|
||||
"Diciembre",
|
||||
];
|
||||
|
||||
// La ventana trae 12 meses a mostrar + 1 mes "base" al inicio, que se usa para
|
||||
// Comparamos 12 meses con dato de inflación. El backend además trae hasta 2
|
||||
// meses sin dato (el actual y el anterior) y 1 mes "base" al inicio para
|
||||
// calcular la variación del mes más antiguo mostrado.
|
||||
const DISPLAY_MONTHS = 12;
|
||||
const INFLATION_MONTHS = 12;
|
||||
|
||||
function formatMonth(monthKey: string): string {
|
||||
const [y, m] = monthKey.split("-");
|
||||
@@ -58,6 +59,44 @@ function formatPrice(value: number): string {
|
||||
return `$${priceFormatter.format(value)}`;
|
||||
}
|
||||
|
||||
type VariationTone = "positive" | "orange" | "negative";
|
||||
|
||||
// El rojo queda reservado a las caídas: crecer por debajo de la inflación es
|
||||
// naranja, empatarla o superarla es verde.
|
||||
function getVariationTone(
|
||||
variation: number,
|
||||
inflation: number | null,
|
||||
): VariationTone {
|
||||
if (variation < 0) return "negative";
|
||||
if (inflation !== null && variation < inflation) return "orange";
|
||||
return "positive";
|
||||
}
|
||||
|
||||
const PLAIN_TONE_CLASSES: Record<VariationTone, string> = {
|
||||
positive: "text-emerald-500",
|
||||
orange: "text-orange-500",
|
||||
negative: "text-red-500",
|
||||
};
|
||||
|
||||
const COMPARED_TONE_CLASSES: Record<VariationTone, string> = {
|
||||
positive:
|
||||
"bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400 px-1.5 py-0.5 rounded",
|
||||
orange:
|
||||
"bg-orange-100 text-orange-700 dark:bg-orange-900/30 dark:text-orange-400 px-1.5 py-0.5 rounded",
|
||||
negative:
|
||||
"bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400 px-1.5 py-0.5 rounded",
|
||||
};
|
||||
|
||||
function getVariationClassName(
|
||||
variation: number,
|
||||
inflation: number | null,
|
||||
): string {
|
||||
const tone = getVariationTone(variation, inflation);
|
||||
const toneClasses =
|
||||
inflation !== null ? COMPARED_TONE_CLASSES[tone] : PLAIN_TONE_CLASSES[tone];
|
||||
return `text-xs font-medium inline-flex items-center gap-1 tabular-nums ${toneClasses}`;
|
||||
}
|
||||
|
||||
function TotalVariation({
|
||||
variation,
|
||||
inflation,
|
||||
@@ -74,17 +113,7 @@ function TotalVariation({
|
||||
}
|
||||
|
||||
return (
|
||||
<span
|
||||
className={`text-xs font-medium inline-flex items-center gap-1 tabular-nums ${
|
||||
inflation !== null
|
||||
? variation > inflation
|
||||
? "bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400 px-1.5 py-0.5 rounded"
|
||||
: "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400 px-1.5 py-0.5 rounded"
|
||||
: variation >= 0
|
||||
? "text-emerald-500"
|
||||
: "text-red-500"
|
||||
}`}
|
||||
>
|
||||
<span className={getVariationClassName(variation, inflation)}>
|
||||
{variation >= 0 ? (
|
||||
<ArrowUp className="size-3" />
|
||||
) : (
|
||||
@@ -121,17 +150,7 @@ function VariationIndicator({
|
||||
}
|
||||
|
||||
const indicator = (
|
||||
<span
|
||||
className={`text-xs font-medium inline-flex items-center gap-1 tabular-nums ${
|
||||
inflation !== null
|
||||
? variation > inflation
|
||||
? "bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400 px-1.5 py-0.5 rounded"
|
||||
: "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400 px-1.5 py-0.5 rounded"
|
||||
: variation >= 0
|
||||
? "text-emerald-500"
|
||||
: "text-red-500"
|
||||
}`}
|
||||
>
|
||||
<span className={getVariationClassName(variation, inflation)}>
|
||||
{variation >= 0 ? (
|
||||
<ArrowUp className="size-3" />
|
||||
) : (
|
||||
@@ -219,8 +238,7 @@ export function MonthlyVariationTable() {
|
||||
);
|
||||
const bnaVar = calcVariation(current?.BNA ?? null, prevData?.BNA ?? null);
|
||||
|
||||
let inflation: number | null = null;
|
||||
inflation = inflationMap.get(month) ?? null;
|
||||
const inflation: number | null = inflationMap.get(month) ?? null;
|
||||
|
||||
return {
|
||||
month,
|
||||
@@ -245,16 +263,27 @@ export function MonthlyVariationTable() {
|
||||
};
|
||||
});
|
||||
|
||||
// Descartamos el mes base (el más antiguo) del render: ya cumplió su
|
||||
// función como mes previo para calcular la variación del primero mostrado.
|
||||
return allRows.slice(-DISPLAY_MONTHS).reverse();
|
||||
// Mostramos los últimos 12 meses con dato de inflación y, desde el más
|
||||
// antiguo de ellos hacia adelante, también los meses aún sin dato (el
|
||||
// actual y a veces el anterior). El mes inmediatamente anterior al primero
|
||||
// visible se conserva fuera del render solo como base de su variación.
|
||||
const monthsWithInflation = allRows.filter((row) => row.inflation !== null);
|
||||
const firstVisibleIndex = Math.max(
|
||||
0,
|
||||
monthsWithInflation.length - INFLATION_MONTHS,
|
||||
);
|
||||
const firstVisibleMonth = monthsWithInflation[firstVisibleIndex]?.month;
|
||||
if (!firstVisibleMonth) return [];
|
||||
|
||||
return allRows.filter((row) => row.month >= firstVisibleMonth).reverse();
|
||||
}, [monthlyData, inflationData]);
|
||||
|
||||
const summary = useMemo(() => {
|
||||
if (rows.length < 2) return null;
|
||||
const inflationRows = rows.filter((row) => row.inflation !== null);
|
||||
if (inflationRows.length < 2) return null;
|
||||
|
||||
const oldest = rows[rows.length - 1];
|
||||
const newest = rows[0];
|
||||
const oldest = inflationRows[inflationRows.length - 1];
|
||||
const newest = inflationRows[0];
|
||||
|
||||
const totalVariation = (
|
||||
first: number | null,
|
||||
@@ -264,18 +293,15 @@ export function MonthlyVariationTable() {
|
||||
return ((last - first) / first) * 100;
|
||||
};
|
||||
|
||||
const accumulatedInflation = rows.reduce<number | null>((acc, row) => {
|
||||
if (row.inflation === null) return acc;
|
||||
const factor = 1 + row.inflation / 100;
|
||||
return (acc ?? 1) * factor;
|
||||
}, null);
|
||||
const accumulatedInflation = inflationRows.reduce<number>((acc, row) => {
|
||||
return acc * (1 + (row.inflation ?? 0) / 100);
|
||||
}, 1);
|
||||
|
||||
return {
|
||||
BELO: totalVariation(oldest.BELO.current, newest.BELO.current),
|
||||
BLUE: totalVariation(oldest.BLUE.current, newest.BLUE.current),
|
||||
BNA: totalVariation(oldest.BNA.current, newest.BNA.current),
|
||||
inflation:
|
||||
accumulatedInflation !== null ? (accumulatedInflation - 1) * 100 : null,
|
||||
inflation: (accumulatedInflation - 1) * 100,
|
||||
period: `${oldest.label} – ${newest.label}`,
|
||||
};
|
||||
}, [rows]);
|
||||
|
||||
Reference in New Issue
Block a user