218 lines
6.8 KiB
TypeScript
218 lines
6.8 KiB
TypeScript
import {ChartData, DataColumn, DataTable, HorizontalBar, LeftChartTable, SignStyle} from "@/components/lge-dashboard";
|
|
import {BannerKey, BannerRow} from "@/types/lg";
|
|
import {formatRowLabel, isGapRow, wrapSampleRow} from "@/lib/lg-dashboard";
|
|
import {useEffect, useMemo} from "react";
|
|
import {
|
|
BANNER_GROUP,
|
|
BANNER_KEY_MAP,
|
|
GRID_Y_NOT_PADDING,
|
|
SITE_TITLE,
|
|
} from "@/const/lgConst";
|
|
import {parseNumber} from "@/lib/utils";
|
|
import {
|
|
buildHeader_DataColumn,
|
|
exportDashboardExcel,
|
|
Section
|
|
} from "@/lib/lg-export-excel";
|
|
import {useLayoutFilter} from "@/components/layout/LayoutFilterContext";
|
|
import {CategoryFilterValue} from "@/components/layout/CategoryFilter";
|
|
import {CategoryTargetData} from "@/config/filter.types";
|
|
import {useDebouncedValue} from "@/hooks/useDebouncedValue";
|
|
|
|
|
|
type SectionSpec = {
|
|
title: string;
|
|
groupKeys: Array<keyof typeof BANNER_GROUP>;
|
|
};
|
|
|
|
/** 3개 섹션(테이블)도 여기서만 선언 */
|
|
const SECTIONS: SectionSpec[] = [
|
|
{ title: "가구 형태", groupKeys: ["lifeStage", "household", "age"] },
|
|
{ title: "거주 형태", groupKeys: ["occupancy", "housing", "houseSize"] },
|
|
{ title: "거주 지역 및 소득 수준", groupKeys: ["region", "income"] },
|
|
];
|
|
|
|
const SEP_R = "lg-table__sep-r";
|
|
|
|
const getSectionBannerKeys = (spec: SectionSpec): BannerKey[] =>
|
|
spec.groupKeys.flatMap((gk) => BANNER_GROUP[gk].keys);
|
|
|
|
const buildColumns = (spec: SectionSpec, selectedYear:string): DataColumn<BannerRow>[] => {
|
|
const columns: DataColumn<BannerRow>[] = [
|
|
{
|
|
key: "rowLabel",
|
|
header: `${selectedYear}년`,
|
|
headRowSpan: 2,
|
|
rowHead: true,
|
|
cellClassName: "lg-table__rowhead",
|
|
width: 120,
|
|
},
|
|
{
|
|
key: "total",
|
|
header: "전체",
|
|
numeric: true,
|
|
headRowSpan: 2,
|
|
headerClassName: SEP_R,
|
|
cellClassName: SEP_R,
|
|
width: 80,
|
|
},
|
|
];
|
|
|
|
spec.groupKeys.forEach((groupKey) => {
|
|
const g = BANNER_GROUP[groupKey];
|
|
g.keys.forEach((k, idx) => {
|
|
const isLast = idx === g.keys.length - 1;
|
|
columns.push({
|
|
key: k as any,
|
|
header: BANNER_KEY_MAP[k] ?? String(k),
|
|
group: g.label,
|
|
numeric: true,
|
|
headerClassName: isLast ? SEP_R : undefined,
|
|
cellClassName: isLast ? SEP_R : undefined,
|
|
width: 90,
|
|
});
|
|
});
|
|
});
|
|
|
|
return columns;
|
|
};
|
|
|
|
type BannerSummaryProps = {
|
|
data: BannerRow[];
|
|
useChart: boolean;
|
|
useYear: boolean;
|
|
};
|
|
|
|
const BannerSummary = ({ data, useChart, useYear }: BannerSummaryProps) => {
|
|
const {filterValue, registerExcelExporter } = useLayoutFilter();
|
|
const debouncedFilterValue = useDebouncedValue<CategoryFilterValue>(filterValue as CategoryFilterValue, 500);
|
|
|
|
const buildChartData = (rows: BannerRow[]): ChartData => {
|
|
const categories = rows
|
|
.filter((r) => !r.rowLabel.includes("사례수"))
|
|
.map((r) => formatRowLabel(r.rowLabel))
|
|
.reverse();
|
|
|
|
const series = [
|
|
{
|
|
name:"전체",
|
|
data: rows
|
|
.filter((r) => !r.rowLabel.includes("사례수"))
|
|
.map((r) => parseNumber(r.total) ?? null)
|
|
.reverse(),
|
|
}
|
|
];
|
|
|
|
return { categories, series };
|
|
};
|
|
|
|
const signStyleGap: SignStyle<BannerRow> = {
|
|
rowSelector: (row) => isGapRow(row.rowLabel),
|
|
display: "arrow",
|
|
arrowWithNumber: true,
|
|
paint: "text",
|
|
zeroText: "-",
|
|
};
|
|
|
|
|
|
const sectionRows = useMemo(() => {
|
|
return SECTIONS.map((spec) => {
|
|
const specTitle = spec.title;
|
|
|
|
const bannerKeys = getSectionBannerKeys(spec);
|
|
const columns = buildColumns(spec, useYear? filterValue.year : "");
|
|
|
|
// rows가 spec마다 다르면 여기서 가져오도록 구성
|
|
const rows =
|
|
wrapSampleRow(useYear? data.filter((d) => d.year === filterValue.year) : data)
|
|
.map((row) => {
|
|
row.rowLabel = formatRowLabel(row.rowLabel);
|
|
return row
|
|
})
|
|
const chartData = buildChartData(rows);
|
|
|
|
return {
|
|
spec,
|
|
specTitle,
|
|
bannerKeys,
|
|
columns,
|
|
rows,
|
|
chartData,
|
|
};
|
|
});
|
|
}, [data, useYear, filterValue.year]);
|
|
|
|
useEffect(() => {
|
|
registerExcelExporter(async () => {
|
|
const product = debouncedFilterValue.product;
|
|
const type = debouncedFilterValue.type.label;
|
|
const target = debouncedFilterValue.target.value === CategoryTargetData.PROFILE? "Profile" : "보급률 변화"
|
|
|
|
const sheetName = `${product}_${type}_${target}`
|
|
|
|
type AnyRow = Record<string, any>;
|
|
const sections: Section<AnyRow>[] =
|
|
sectionRows.map((sectionRow) => ({
|
|
title: sectionRow.specTitle,
|
|
header: buildHeader_DataColumn(sectionRow.columns),
|
|
rows: sectionRow.rows as AnyRow[],
|
|
rowGap: 1,
|
|
}))
|
|
;
|
|
|
|
await exportDashboardExcel({
|
|
fileName: `${SITE_TITLE}(${sheetName}).xlsx`,
|
|
sheetName: sheetName,
|
|
sections: sections,
|
|
});
|
|
});
|
|
return () => registerExcelExporter(null);
|
|
}, [registerExcelExporter, debouncedFilterValue, sectionRows]);
|
|
|
|
return (
|
|
<div className="flex flex-col gap-6">
|
|
{sectionRows.map((sectionRow) => {
|
|
return (
|
|
<div key={`div_${sectionRow.specTitle}`}>
|
|
{
|
|
useChart ?
|
|
<LeftChartTable<BannerRow>
|
|
key={sectionRow.specTitle}
|
|
title={sectionRow.specTitle}
|
|
columns={sectionRow.columns}
|
|
rows={sectionRow.rows}
|
|
signStyle={signStyleGap}
|
|
chart={
|
|
<HorizontalBar
|
|
data={sectionRow.chartData}
|
|
height="100%"
|
|
showFocusLabel="none"
|
|
legend={{ show: false }}
|
|
grid={GRID_Y_NOT_PADDING}
|
|
xAxis={{ type: "value", show: false }}
|
|
yAxis={{ type: "category", data: sectionRow.chartData.categories, show: false }}
|
|
/>
|
|
}
|
|
chartCell={{ key: "total", startRowIndex: 1 }}
|
|
minWidthPx={1200}
|
|
starUnder30Sample={true}
|
|
/>
|
|
:
|
|
<DataTable<BannerRow>
|
|
key={sectionRow.specTitle}
|
|
title={sectionRow.specTitle}
|
|
columns={sectionRow.columns}
|
|
rows={sectionRow.rows}
|
|
signStyle={signStyleGap}
|
|
unitText={""}
|
|
starUnder30Sample={true}
|
|
/>
|
|
}
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default BannerSummary; |