160 lines
4.7 KiB
TypeScript
160 lines
4.7 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
GroupedOverlayBar,
|
|
type GroupedOverlayBarData,
|
|
signStyleGapRow,
|
|
TopChartTable,
|
|
type TopChartTableRow,
|
|
} from "@/components/lge-dashboard";
|
|
import {
|
|
LAST_YEAR,
|
|
LIFE_STAGE_COLUMNS,
|
|
LIFE_STAGE_GROUPS,
|
|
PRODUCT_KEY_MAP,
|
|
PRODUCT_KEYS,
|
|
PRODUCTS, SITE_TITLE,
|
|
YEARS
|
|
} from "@/const/lgConst";
|
|
import {useEffect, useMemo} from "react";
|
|
import {apiLifeStageByProduct, apiOwnShareOfTotalPenetration, apiTrendCategories} from "@/config/sample.data";
|
|
import LifeStageDiv from "@/components/lge-dashboard/combo/LifeStageDiv";
|
|
import { useLayoutFilter } from "@/components/layout/LayoutFilterContext";
|
|
import { formatRowLabel } from "@/lib/lg-dashboard";
|
|
import {
|
|
buildHeader_LifeStageColumns,
|
|
buildHeader_ProductKeyMap,
|
|
exportDashboardExcel,
|
|
Section
|
|
} from "@/lib/lg-export-excel";
|
|
import {norm} from "@/lib/utils";
|
|
import {LifeStageGroup, LifeStageRow} from "@/types/lg";
|
|
|
|
const LGPenetrationPage = () => {
|
|
const { filterValue, registerExcelExporter } = useLayoutFilter();
|
|
console.log(filterValue);
|
|
|
|
//TODO: LG가 0인 제품은 제외시키기
|
|
|
|
const productTableRows = useMemo<TopChartTableRow[]>(() => {
|
|
return apiOwnShareOfTotalPenetration.map((row) => ({
|
|
label: formatRowLabel(row.rowLabel),
|
|
values: PRODUCT_KEYS.map((k) => (row as any)[k] ?? null),
|
|
}));
|
|
}, []);
|
|
|
|
const productChartData = useMemo<GroupedOverlayBarData>(() => {
|
|
const total: Array<Array<number>> = YEARS.map(() => Array(PRODUCT_KEYS.length).fill(0));
|
|
const focus: Array<Array<number>> = YEARS.map(() => Array(PRODUCT_KEYS.length).fill(0));
|
|
|
|
const yearIndexMap = new Map<string, number>(YEARS.map((y, i) => [y, i]));
|
|
|
|
const totalRows = apiTrendCategories.filter(
|
|
(r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년")
|
|
);
|
|
const lgRows = apiOwnShareOfTotalPenetration.filter(
|
|
(r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년")
|
|
);
|
|
|
|
lgRows.forEach((r, ri) => {
|
|
const yi = yearIndexMap.get(r.year);
|
|
if (yi == null) return;
|
|
|
|
for (let ci = 0; ci < PRODUCT_KEYS.length; ci++) {
|
|
const k = PRODUCT_KEYS[ci];
|
|
const t = (totalRows[ri] as any)[k];
|
|
const v = (r as any)[k];
|
|
|
|
total[yi][ci] = Number(t);
|
|
focus[yi][ci] = Number(v);
|
|
}
|
|
});
|
|
|
|
return {
|
|
categories: PRODUCTS,
|
|
years: YEARS,
|
|
totalName: "전체",
|
|
focusName: "LG",
|
|
total,
|
|
focus,
|
|
};
|
|
}, []);
|
|
|
|
const lifeStageRowsByGroup:LifeStageGroup[] = useMemo(() => {
|
|
const pick = (label: string) => apiLifeStageByProduct.find((r) => norm(r.rowLabel) === norm(label));
|
|
|
|
return LIFE_STAGE_GROUPS.map((g) => {
|
|
const rows: LifeStageRow[] = g.items.map((label) => {
|
|
const r = pick(label);
|
|
|
|
return {
|
|
item: label,
|
|
total: r?.total ?? "",
|
|
single: r?.single ?? "",
|
|
noKids: r?.noKids ?? "",
|
|
preschoolKids: r?.preschoolKids ?? "",
|
|
schoolKids: r?.schoolKids ?? "",
|
|
adultKids: r?.adultKids ?? "",
|
|
independentSenior: r?.independentSenior ?? "",
|
|
};
|
|
});
|
|
|
|
return { title: g.title, rows };
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
registerExcelExporter(async () => {
|
|
const excelHeader = buildHeader_ProductKeyMap(PRODUCT_KEY_MAP);
|
|
const lifeStageHeader = buildHeader_LifeStageColumns(LIFE_STAGE_COLUMNS);
|
|
|
|
type AnyRow = Record<string, any>;
|
|
|
|
const sections: Section<AnyRow>[] = [
|
|
{
|
|
title: "LG제품 보급률",
|
|
header: excelHeader,
|
|
rows: apiOwnShareOfTotalPenetration as AnyRow[],
|
|
rowGap: 1,
|
|
},
|
|
|
|
...lifeStageRowsByGroup.map((lifeStageRow) => ({
|
|
title: lifeStageRow.title,
|
|
header: lifeStageHeader,
|
|
rows: lifeStageRow.rows as AnyRow[],
|
|
rowGap: 1,
|
|
})),
|
|
];
|
|
|
|
await exportDashboardExcel({
|
|
fileName: `${SITE_TITLE}(LG제품 보급률).xlsx`,
|
|
sheetName: "LG제품 보급률",
|
|
sections: sections,
|
|
});
|
|
});
|
|
return () => registerExcelExporter(null);
|
|
}, [registerExcelExporter]);
|
|
|
|
return (
|
|
<div>
|
|
<TopChartTable
|
|
title={"LG제품 보급률"}
|
|
categories={[...PRODUCTS]}
|
|
rows={productTableRows}
|
|
chart={
|
|
<GroupedOverlayBar
|
|
data={productChartData}
|
|
yearTotalColor={{ "24": "--chart-3-t", "25": "--chart-2-t", "26": "--chart-1-t" }}
|
|
yearFocusColor={{ "24": "--chart-3", "25": "--chart-2", "26": "--chart-1" }}
|
|
showFocusLabel={"last"}
|
|
/>
|
|
}
|
|
signStyle={signStyleGapRow}
|
|
/>
|
|
|
|
<LifeStageDiv lifeStageRowsByGroup={lifeStageRowsByGroup} />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default LGPenetrationPage; |