260202_04_ENH: Total 하위 페이지들에 useMemo 적용 및 LifeStyle 샘플 데이터 전체 값 조정
This commit is contained in:
@@ -1,51 +1,64 @@
|
||||
'use client'
|
||||
"use client";
|
||||
|
||||
import {useEffect, useState} from "react";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import TopChartTable from "@/components/lge-dashboard/combo/TopChartTable";
|
||||
|
||||
import VerticalBar from "@/components/lge-dashboard/chart/VerticalBar";
|
||||
import {
|
||||
ChartData, GroupedStackBar, signStyleGapRow, TopChartTableRow, GroupedStackBarData
|
||||
type ChartData,
|
||||
GroupedStackBar,
|
||||
signStyleGapRow,
|
||||
type TopChartTableRow,
|
||||
type GroupedStackBarData,
|
||||
} from "@/components/lge-dashboard";
|
||||
import {useLayoutFilter} from "@/components/layout/LayoutFilterContext";
|
||||
import {apiBrandByYearCategories, apiTrendCategories} from "@/config/sample.data";
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {BRAND_LIST, PRODUCT_KEYS, PRODUCTS, YEARS, AXIS_HIDE_STYLE, GRID_X_NOT_PADDING} from "@/const/lgConst";
|
||||
import {formatRowLabel} from "@/lib/lg-dashboard";
|
||||
|
||||
import { useLayoutFilter } from "@/components/layout/LayoutFilterContext";
|
||||
import { apiBrandByYearCategories, apiTrendCategories } from "@/config/sample.data";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
BRAND_LIST,
|
||||
PRODUCT_KEYS,
|
||||
PRODUCTS,
|
||||
YEARS,
|
||||
AXIS_HIDE_STYLE,
|
||||
GRID_X_NOT_PADDING,
|
||||
} from "@/const/lgConst";
|
||||
import { formatRowLabel } from "@/lib/lg-dashboard";
|
||||
|
||||
const PenetrationPage = () => {
|
||||
const { isTotal, filterValue, registerExcelExporter } = useLayoutFilter();
|
||||
console.log(filterValue);
|
||||
|
||||
const [isBrandShare, setIsBrandShare] = useState<boolean>(false);
|
||||
const [isBrandShare, setIsBrandShare] = useState(false);
|
||||
|
||||
const productTableRows: TopChartTableRow[] = apiTrendCategories.map((row, i) => ({
|
||||
label: formatRowLabel(row.rowLabel),
|
||||
values: PRODUCT_KEYS.map((k) => (row as any)[k] ?? null),
|
||||
}));
|
||||
const productTableRows = useMemo<TopChartTableRow[]>(() => {
|
||||
return apiTrendCategories.map((row) => ({
|
||||
label: formatRowLabel(row.rowLabel),
|
||||
values: PRODUCT_KEYS.map((k) => (row as any)[k] ?? null),
|
||||
}));
|
||||
}, []);
|
||||
|
||||
const productChartData: ChartData = {
|
||||
categories: [...PRODUCTS], // 한글 라벨(세탁기/냉장고/...)
|
||||
series: apiTrendCategories
|
||||
.filter((r) => r.rowLabel.endsWith("년")) // 24년/25년/26년만 차트에 사용
|
||||
.map((r) => ({
|
||||
name: r.rowLabel, // legend: 24년/25년/26년
|
||||
data: PRODUCT_KEYS.map((k) => (r as any)[k] ?? null),
|
||||
})),
|
||||
}
|
||||
const productChartData = useMemo<ChartData>(() => {
|
||||
return {
|
||||
categories: [...PRODUCTS],
|
||||
series: apiTrendCategories
|
||||
.filter((r) => r.rowLabel.endsWith("년"))
|
||||
.map((r) => ({
|
||||
name: r.rowLabel,
|
||||
data: PRODUCT_KEYS.map((k) => (r as any)[k] ?? null),
|
||||
})),
|
||||
};
|
||||
}, []);
|
||||
|
||||
const rowMap = new Map<string, (typeof apiBrandByYearCategories)[number]>();
|
||||
for (const r of apiBrandByYearCategories) {
|
||||
rowMap.set(`${r.year}__${r.rowLabel}`, r);
|
||||
}
|
||||
const brandShareChartData = useMemo<GroupedStackBarData>(() => {
|
||||
const rowMap = new Map<string, (typeof apiBrandByYearCategories)[number]>();
|
||||
for (const r of apiBrandByYearCategories) {
|
||||
rowMap.set(`${r.year}__${r.rowLabel}`, r);
|
||||
}
|
||||
|
||||
const brandShareChartData: GroupedStackBarData = (() => {
|
||||
const values: number[][][] = YEARS.map(() =>
|
||||
BRAND_LIST.map(() => new Array(PRODUCTS.length).fill(0))
|
||||
);
|
||||
|
||||
YEARS.forEach((year, yi) =>{
|
||||
YEARS.forEach((year, yi) => {
|
||||
BRAND_LIST.forEach((brand, bi) => {
|
||||
PRODUCT_KEYS.forEach((k, ci) => {
|
||||
const row = rowMap.get(`${year}__${brand}`);
|
||||
@@ -53,19 +66,15 @@ const PenetrationPage = () => {
|
||||
values[yi][bi][ci] = typeof v === "number" ? v : 0;
|
||||
});
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
return { categories: PRODUCTS, years: YEARS, brands: BRAND_LIST, values };
|
||||
})();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
registerExcelExporter(async (payload) => {
|
||||
// payload.isTotal / payload.totalValue / payload.categoryValue 사용 가능
|
||||
// 여기서 데이터 수집 -> 엑셀 생성 -> 다운로드 수행
|
||||
// TODO: 실제 export 로직
|
||||
// await exportToExcel(payload);
|
||||
// TODO: export
|
||||
});
|
||||
|
||||
return () => registerExcelExporter(null);
|
||||
}, [registerExcelExporter]);
|
||||
|
||||
@@ -74,46 +83,47 @@ const PenetrationPage = () => {
|
||||
<div className="flex py-4">
|
||||
<Button
|
||||
className="h-8 px-3 text-sm font-semibold content-text-sm"
|
||||
variant={isBrandShare?"default":"secondary"}
|
||||
onClick={() => setIsBrandShare(!isBrandShare)}
|
||||
variant={isBrandShare ? "default" : "secondary"}
|
||||
onClick={() => setIsBrandShare((v) => !v)}
|
||||
>
|
||||
{"브랜드별 점유율"}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<TopChartTable
|
||||
title={isBrandShare?"브랜드별 점유율":"전체 보급률"}
|
||||
title={isBrandShare ? "브랜드별 점유율" : "전체 보급률"}
|
||||
categories={[...PRODUCTS]}
|
||||
rows={productTableRows}
|
||||
chart={
|
||||
isBrandShare?
|
||||
isBrandShare ? (
|
||||
<GroupedStackBar
|
||||
data={brandShareChartData}
|
||||
grid={{...GRID_X_NOT_PADDING, top:60}}
|
||||
grid={{ ...GRID_X_NOT_PADDING, top: 60 }}
|
||||
xAxis={{ type: "category", data: PRODUCTS, ...AXIS_HIDE_STYLE }}
|
||||
yAxis={{ type: "value", ...AXIS_HIDE_STYLE }}
|
||||
showFocusLabel={"none"}
|
||||
// 연도(24/25/26)별로 브랜드 3개 색상 지정 (브랜드 순서는 data.brands 순서)
|
||||
yearBrandPalette={{
|
||||
"24": ["--chart-cbrand-1", "--chart-samsung-1", "--chart-lg-1"],
|
||||
"25": ["--chart-cbrand-2", "--chart-samsung-2", "--chart-lg-2"],
|
||||
"26": ["--chart-cbrand-3", "--chart-samsung-3", "--chart-lg-3"],
|
||||
}}
|
||||
/>
|
||||
:
|
||||
<VerticalBar
|
||||
data={productChartData}
|
||||
paletteVarNames={["--chart-3", "--chart-2", "--chart-1"]}
|
||||
grid={GRID_X_NOT_PADDING}
|
||||
xAxis={{ type: "category", data: [...PRODUCTS], ...AXIS_HIDE_STYLE }}
|
||||
yAxis={{ type: "value", ...AXIS_HIDE_STYLE }}
|
||||
showFocusLabel={"last"}
|
||||
height={260}
|
||||
/>
|
||||
) : (
|
||||
<VerticalBar
|
||||
data={productChartData}
|
||||
paletteVarNames={["--chart-3", "--chart-2", "--chart-1"]}
|
||||
grid={GRID_X_NOT_PADDING}
|
||||
xAxis={{ type: "category", data: [...PRODUCTS], ...AXIS_HIDE_STYLE }}
|
||||
yAxis={{ type: "value", ...AXIS_HIDE_STYLE }}
|
||||
showFocusLabel={"last"}
|
||||
height={260}
|
||||
/>
|
||||
)
|
||||
}
|
||||
signStyle={signStyleGapRow}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default PenetrationPage;
|
||||
Reference in New Issue
Block a user