205 lines
6.9 KiB
TypeScript
205 lines
6.9 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
import TopChartTable from "@/components/lge-dashboard/combo/TopChartTable";
|
|
import VerticalBar from "@/components/lge-dashboard/chart/VerticalBar";
|
|
import {
|
|
type ChartData,
|
|
GroupedStackBar,
|
|
signStyleGapRow,
|
|
type TopChartTableRow,
|
|
type GroupedStackBarData, BottomDescription,
|
|
} from "@/components/lge-dashboard";
|
|
import { useLayoutFilter } from "@/components/layout/LayoutFilterContext";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
BRAND_LIST,
|
|
PRODUCT_KEYS,
|
|
PRODUCTS,
|
|
YEARS,
|
|
AXIS_HIDE_STYLE,
|
|
GRID_X_NOT_PADDING, PRODUCT_KEY_MAP, SITE_TITLE
|
|
} from "@/const/lgConst";
|
|
import {
|
|
calcCagrRow, calcGapProductRow,
|
|
formatRowLabel,
|
|
normalizeFilterCondition, withCagrRow, withGapRow
|
|
} from "@/lib/lg-dashboard";
|
|
import {buildHeader_ProductKeyMap, exportDashboardExcel, Section} from "@/lib/lg-export-excel";
|
|
import {StatisticsRequest} from "@/types/lg";
|
|
import {useProductStatisticsQuery} from "@/hooks/useLgStatisticsQuery";
|
|
import {useDebouncedValue} from "@/hooks/useDebouncedValue";
|
|
import {toNumOr0} from "@/lib/utils";
|
|
import {SelectedMap} from "@/types/common";
|
|
|
|
const PenetrationPage = () => {
|
|
const { filterValue, registerExcelExporter } = useLayoutFilter();
|
|
const debouncedFilterValue = useDebouncedValue<SelectedMap>(filterValue as SelectedMap, 500);
|
|
const [isBrandShare, setIsBrandShare] = useState(false);
|
|
|
|
const chartColorByYear = Object.fromEntries(
|
|
YEARS.map((year, i) => [
|
|
year,
|
|
[`--chart-cbrand-${i + 1}`,`--chart-samsung-${i + 1}`,`--chart-lg-${i + 1}`],
|
|
])
|
|
) as Record<(typeof YEARS)[number], string[]>;
|
|
|
|
const req = useMemo<StatisticsRequest>(() => {
|
|
return {
|
|
variableIdMap: { penetration: "KQA1", penetration_brand: "KQA3_3_NET" },
|
|
bannerIdList: ["product"],
|
|
filterCondition: normalizeFilterCondition(debouncedFilterValue),
|
|
option: { useCache: true, lastYear: false, decimalPoint: 0 },
|
|
};
|
|
}, [debouncedFilterValue]);
|
|
|
|
const { data, isError, error } = useProductStatisticsQuery(req);
|
|
|
|
if(isError){
|
|
console.error(error);
|
|
}
|
|
|
|
const penetrationRows =
|
|
data?.statisticsDataMap?.penetration
|
|
.filter((r) => r.rowLabel == `보유`)
|
|
.map((row) => {
|
|
return {
|
|
...row,
|
|
rowLabel: row.year,
|
|
}
|
|
}) ?? [];
|
|
|
|
const penetrationBrandRows =
|
|
data?.statisticsDataMap?.penetration_brand
|
|
.filter((row) => BRAND_LIST.includes(row.rowLabel)) ?? [];
|
|
|
|
const penetrationRowsWithGapCagr = useMemo(() => {
|
|
const withGap = withGapRow(penetrationRows, calcGapProductRow);
|
|
return withCagrRow(withGap, calcCagrRow);
|
|
}, [penetrationRows]);
|
|
|
|
const productTableRows = useMemo<TopChartTableRow[]>(() => {
|
|
return penetrationRowsWithGapCagr.map((row) => ({
|
|
label: formatRowLabel(row.rowLabel),
|
|
total: row.total,
|
|
values: PRODUCT_KEYS.map((k) => (row as any)[k] ?? null),
|
|
}))
|
|
}, [penetrationRowsWithGapCagr]);
|
|
|
|
const productChartData = useMemo<ChartData>(() => {
|
|
return {
|
|
categories: [...PRODUCTS],
|
|
series: penetrationRows
|
|
.map((row) => ({
|
|
name: row.year,
|
|
data: PRODUCT_KEYS.map((k) => toNumOr0((row as any)[k])),
|
|
})),
|
|
};
|
|
}, [penetrationRows]);
|
|
|
|
const brandShareChartData = useMemo<GroupedStackBarData>(() => {
|
|
|
|
const rowMap = new Map<string, (typeof penetrationBrandRows)[number]>();
|
|
for (const r of penetrationBrandRows) {
|
|
rowMap.set(`${r.year}__${r.rowLabel}`, r);
|
|
}
|
|
|
|
const values: number[][][] = YEARS.map(() =>
|
|
BRAND_LIST.map(() => new Array(PRODUCTS.length).fill(0))
|
|
);
|
|
|
|
YEARS.forEach((year, yi) => {
|
|
BRAND_LIST.forEach((brand, bi) => {
|
|
PRODUCT_KEYS.forEach((k, ci) => {
|
|
const row = rowMap.get(`${year}__${brand}`);
|
|
values[yi][bi][ci] = (row as any)?.[k];
|
|
});
|
|
});
|
|
});
|
|
|
|
return { categories: PRODUCTS, years: YEARS, brands: BRAND_LIST, values };
|
|
}, [penetrationBrandRows]);
|
|
|
|
const description = useMemo(() => {
|
|
if(isBrandShare){
|
|
return "Q. 보유하고 계신 (제품)의 브랜드는 어떻게 되십니까?";
|
|
}
|
|
|
|
return "Q. 다음 중 귀댁에서 보유하고 계신 가전/전자 제품은 무엇입니까? 모두 선택해주세요.<br/>" +
|
|
"* 구독/렌탈/선물/경품/아파트 내 빌트인 제품을 모두 포함하여 선택해주세요.";
|
|
}, [isBrandShare]);
|
|
|
|
useEffect(() => {
|
|
registerExcelExporter(async () => {
|
|
const excelHeader = buildHeader_ProductKeyMap(PRODUCT_KEY_MAP);
|
|
type AnyRow = Record<string, any>;
|
|
|
|
const sections: Section<AnyRow>[] = [
|
|
{ title: "전체 보급률", header: excelHeader, rows: penetrationRowsWithGapCagr, rowGap: 1 },
|
|
...YEARS.map((year) => (
|
|
{
|
|
title: `브랜드 별 점유율_${year}년`,
|
|
header: excelHeader,
|
|
rows: penetrationBrandRows.filter((row) => row.year === year),
|
|
rowGap: 1,
|
|
}
|
|
))
|
|
];
|
|
|
|
await exportDashboardExcel({
|
|
fileName: `${SITE_TITLE}(보급률).xlsx`,
|
|
sheetName: "전체 보급률",
|
|
sections: sections,
|
|
});
|
|
});
|
|
return () => registerExcelExporter(null);
|
|
}, [registerExcelExporter, penetrationRows, penetrationBrandRows]);
|
|
|
|
return (
|
|
<div>
|
|
<div className="flex py-4">
|
|
<Button
|
|
className="h-8 px-3 text-sm font-semibold content-text-sm"
|
|
variant={isBrandShare ? "default" : "secondary"}
|
|
onClick={() => setIsBrandShare((v) => !v)}
|
|
>
|
|
{"브랜드별 점유율"}
|
|
</Button>
|
|
</div>
|
|
|
|
<TopChartTable
|
|
title={isBrandShare ? "브랜드별 점유율" : "전체 보급률"}
|
|
categories={[...PRODUCTS]}
|
|
rows={productTableRows}
|
|
chart={
|
|
isBrandShare ? (
|
|
<GroupedStackBar
|
|
data={brandShareChartData}
|
|
grid={{ ...GRID_X_NOT_PADDING, top: 60 }}
|
|
xAxis={{ type: "category", data: PRODUCTS, ...AXIS_HIDE_STYLE }}
|
|
yAxis={{ type: "value", ...AXIS_HIDE_STYLE }}
|
|
showFocusLabel={"none"}
|
|
yearBrandPalette={chartColorByYear}
|
|
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}
|
|
fillCAGR={true}
|
|
/>
|
|
<BottomDescription description={description} />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default PenetrationPage; |