260205_01_ENH: 페이지 별 엑셀 다운로드 적용
This commit is contained in:
@@ -1,9 +1,22 @@
|
||||
import {ChartData, DataColumn, DataTable, HorizontalBar, LeftChartTable, SignStyle} from "@/components/lge-dashboard";
|
||||
import {BannerKey, ServerBannerRow} from "@/types/lg";
|
||||
import {formatRowLabel, wrapSampleRow} from "@/lib/lg-dashboard";
|
||||
import {useMemo} from "react";
|
||||
import {BANNER_GROUP, BANNER_KEY_MAP, GRID_Y_NOT_PADDING} from "@/const/lgConst";
|
||||
import {useEffect, useMemo} from "react";
|
||||
import {
|
||||
BANNER_GROUP,
|
||||
BANNER_KEY_MAP,
|
||||
GRID_Y_NOT_PADDING,
|
||||
LAST_YEAR,
|
||||
} 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 {CategoryTypeValue} from "@/components/layout/CategoryFilter";
|
||||
import {CategoryTargetData} from "@/config/filter.types";
|
||||
|
||||
|
||||
type SectionSpec = {
|
||||
@@ -37,6 +50,7 @@ const buildColumns = (spec: SectionSpec): DataColumn<ServerBannerRow>[] => {
|
||||
key: "total",
|
||||
header: "전체",
|
||||
numeric: true,
|
||||
headRowSpan: 2,
|
||||
headerClassName: SEP_R,
|
||||
cellClassName: SEP_R,
|
||||
width: 80,
|
||||
@@ -68,25 +82,27 @@ type BannerSummaryProps = {
|
||||
};
|
||||
|
||||
const BannerSummary = ({ data, useChart }: BannerSummaryProps) => {
|
||||
const {filterValue, registerExcelExporter } = useLayoutFilter();
|
||||
|
||||
const buildChartData = (rows: ServerBannerRow[], bannerKeys: BannerKey[]): ChartData => {
|
||||
const categories = rows
|
||||
.filter((r) => !r.rowLabel.includes("사례수"))
|
||||
.map((r) => formatRowLabel(r.rowLabel));
|
||||
.map((r) => formatRowLabel(r.rowLabel))
|
||||
.reverse();
|
||||
|
||||
const series = [
|
||||
{
|
||||
name:"전체",
|
||||
data: rows
|
||||
.filter((r) => !r.rowLabel.includes("사례수"))
|
||||
.map((r) => parseNumber(r.total) ?? null),
|
||||
.map((r) => parseNumber(r.total) ?? null)
|
||||
.reverse(),
|
||||
}
|
||||
];
|
||||
|
||||
return { categories, series };
|
||||
};
|
||||
|
||||
const rows = useMemo<ServerBannerRow[]>(() => wrapSampleRow(data), [data]);
|
||||
|
||||
const signStyleGap: SignStyle<ServerBannerRow> = {
|
||||
rowSelector: (row) => row.rowLabel === "gap",
|
||||
display: "arrow",
|
||||
@@ -95,31 +111,73 @@ const BannerSummary = ({ data, useChart }: BannerSummaryProps) => {
|
||||
zeroText: "-",
|
||||
};
|
||||
|
||||
|
||||
const sectionRows = useMemo(() => {
|
||||
return SECTIONS.map((spec) => {
|
||||
const specTitle = spec.title;
|
||||
|
||||
const bannerKeys = getSectionBannerKeys(spec);
|
||||
const columns = buildColumns(spec);
|
||||
|
||||
// rows가 spec마다 다르면 여기서 가져오도록 구성
|
||||
const rows = wrapSampleRow(data);
|
||||
const chartData = buildChartData(rows, bannerKeys);
|
||||
|
||||
return {
|
||||
spec,
|
||||
specTitle,
|
||||
bannerKeys,
|
||||
columns,
|
||||
rows,
|
||||
chartData,
|
||||
};
|
||||
});
|
||||
}, [data]);
|
||||
|
||||
useEffect(() => {
|
||||
registerExcelExporter(async (payload) => {
|
||||
const targetString =filterValue.target === CategoryTargetData.PROFILE? "Profile" : "보급률 변화"
|
||||
const sheetName = `${filterValue.product}_${(filterValue.type as CategoryTypeValue).label}_${targetString}`
|
||||
|
||||
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: `20${LAST_YEAR}년도 가전 보급률 Dashboard(${sheetName}).xlsx`,
|
||||
sheetName: sheetName,
|
||||
sections: sections,
|
||||
});
|
||||
});
|
||||
return () => registerExcelExporter(null);
|
||||
}, [registerExcelExporter]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-6">
|
||||
{SECTIONS.map((spec) => {
|
||||
const bannerKeys = getSectionBannerKeys(spec);
|
||||
|
||||
const columns = buildColumns(spec);
|
||||
const chartData = buildChartData(rows, bannerKeys);
|
||||
|
||||
{sectionRows.map((sectionRow) => {
|
||||
return (
|
||||
useChart ?
|
||||
<LeftChartTable<ServerBannerRow>
|
||||
key={spec.title}
|
||||
title={spec.title}
|
||||
columns={columns}
|
||||
rows={rows}
|
||||
key={sectionRow.specTitle}
|
||||
title={sectionRow.specTitle}
|
||||
columns={sectionRow.columns}
|
||||
rows={sectionRow.rows}
|
||||
signStyle={signStyleGap}
|
||||
chart={
|
||||
<HorizontalBar
|
||||
data={chartData}
|
||||
data={sectionRow.chartData}
|
||||
height="100%"
|
||||
showFocusLabel="none"
|
||||
legend={{ show: false }}
|
||||
grid={GRID_Y_NOT_PADDING}
|
||||
xAxis={{ type: "value", show: false }}
|
||||
yAxis={{ type: "category", data: chartData.categories, show: false }}
|
||||
yAxis={{ type: "category", data: sectionRow.chartData.categories, show: false }}
|
||||
/>
|
||||
}
|
||||
chartCell={{ key: "total", startRowIndex: 1 }}
|
||||
@@ -127,10 +185,10 @@ const BannerSummary = ({ data, useChart }: BannerSummaryProps) => {
|
||||
/>
|
||||
:
|
||||
<DataTable<ServerBannerRow>
|
||||
key={spec.title}
|
||||
title={spec.title}
|
||||
columns={columns}
|
||||
rows={rows}
|
||||
key={sectionRow.specTitle}
|
||||
title={sectionRow.specTitle}
|
||||
columns={sectionRow.columns}
|
||||
rows={sectionRow.rows}
|
||||
signStyle={signStyleGap}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user