260331_02_ENH: 그룹 헤더 다단도 가능하게 수정

This commit is contained in:
2026-03-31 11:08:30 +09:00
parent e51c6ba810
commit d58c0b4dd4
13 changed files with 577 additions and 622 deletions

View File

@@ -1,22 +1,17 @@
import {ChartData, DataColumn, DataTable, HorizontalBar, LeftChartTable, SignStyle} from "@/components/lge-dashboard";
import {BannerKey, BannerRow} from "@/types/lg.types";
import {formatRowLabel, isGapRow, wrapSampleRow} from "@/lib/lg-dashboard";
import {useEffect, useMemo} from "react";
import {BANNER_GROUP} from "@/const/lg-banner.const";
import {BANNER_KEY_MAP} from "@/const/lg-schema.const";
import {SITE_TITLE} from "@/const/lg-common.const";
import { ChartData, DataColumn, DataTable, HorizontalBar, LeftChartTable, SignStyle } from "@/components/lge-dashboard";
import { BannerKey, BannerRow } from "@/types/lg.types";
import { buildBannerGroupColumns, formatRowLabel, isGapRow, wrapSampleRow } from "@/lib/lg-dashboard";
import { useEffect, useMemo } from "react";
import { BANNER_GROUP, flattenBannerGroupKeys } from "@/const/lg-banner.const";
import { BANNER_KEY_MAP } from "@/const/lg-schema.const";
import { SITE_TITLE } from "@/const/lg-common.const";
import { GRID_Y_NOT_PADDING } from "@/components/lge-dashboard/chart/chart.config";
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 "@/types/filter.types";
import {useDebouncedValue} from "@/hooks/useDebouncedValue";
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 "@/types/filter.types";
import { useDebouncedValue } from "@/hooks/useDebouncedValue";
type SectionSpec = {
title: string;
@@ -33,14 +28,13 @@ const SECTIONS: SectionSpec[] = [
const SEP_R = "lg-table__sep-r";
const getSectionBannerKeys = (spec: SectionSpec): BannerKey[] =>
spec.groupKeys.flatMap((gk) => BANNER_GROUP[gk].keys);
spec.groupKeys.flatMap((gk) => flattenBannerGroupKeys(BANNER_GROUP[gk].children));
const buildColumns = (spec: SectionSpec, selectedYear:string): DataColumn<BannerRow>[] => {
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,
@@ -49,7 +43,6 @@ const buildColumns = (spec: SectionSpec, selectedYear:string): DataColumn<Banner
key: "total",
header: "전체",
numeric: true,
headRowSpan: 2,
headerClassName: SEP_R,
cellClassName: SEP_R,
width: 80,
@@ -58,17 +51,20 @@ const buildColumns = (spec: SectionSpec, selectedYear:string): DataColumn<Banner
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,
columns.push({
id: `banner-group-${groupKey}`,
header: g.label,
children: buildBannerGroupColumns<BannerRow>(g, BANNER_KEY_MAP, {
numeric: true,
headerClassName: isLast ? SEP_R : undefined,
cellClassName: isLast ? SEP_R : undefined,
width: 90,
});
resolveLeafMeta: (_key, idx, total) => {
const isLast = idx === total - 1;
return {
headerClassName: isLast ? SEP_R : undefined,
cellClassName: isLast ? SEP_R : undefined,
};
},
}),
});
});
@@ -83,23 +79,23 @@ type BannerSummaryProps = {
};
const BannerSummary = ({ data, useChart, useYear, getCellClassName }: BannerSummaryProps) => {
const {filterValue, registerExcelExporter } = useLayoutFilter();
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();
.filter((r) => !r.rowLabel.includes("사례수"))
.map((r) => formatRowLabel(r.rowLabel))
.reverse();
const series = [
{
name:"전체",
name: "전체",
data: rows
.filter((r) => !r.rowLabel.includes("사례수"))
.map((r) => parseNumber(r.total) ?? null)
.reverse(),
}
.filter((r) => !r.rowLabel.includes("사례수"))
.map((r) => parseNumber(r.total) ?? null)
.reverse(),
},
];
return { categories, series };
@@ -113,21 +109,17 @@ const BannerSummary = ({ data, useChart, useYear, getCellClassName }: BannerSumm
zeroText: "-",
};
const sectionRows = useMemo(() => {
return SECTIONS.map((spec) => {
const specTitle = spec.title;
const bannerKeys = getSectionBannerKeys(spec);
const columns = buildColumns(spec, useYear? filterValue.year : "");
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 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 {
@@ -145,75 +137,71 @@ const BannerSummary = ({ data, useChart, useYear, getCellClassName }: BannerSumm
registerExcelExporter(async () => {
const product = debouncedFilterValue.product;
const type = debouncedFilterValue.type.label;
const target = debouncedFilterValue.target.value === CategoryTargetData.PROFILE? "Profile" : "보급률 변화"
const sheetName = `${product}_${type}_${target}`
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,
}))
;
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,
sheetName,
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}
getCellClassName={getCellClassName}
signStyle={signStyleGap}
chart={
<HorizontalBar
data={sectionRow.chartData}
height="100%"
showFocusLabel="all"
labelOptions={{ position: "insideRight", color: "#fff", distance: 4 }}
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 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}
getCellClassName={getCellClassName}
signStyle={signStyleGap}
chart={
<HorizontalBar
data={sectionRow.chartData}
height="100%"
showFocusLabel="all"
labelOptions={{ position: "insideRight", color: "#fff", distance: 4 }}
legend={{ show: false }}
grid={GRID_Y_NOT_PADDING}
xAxis={{ type: "value", show: false }}
yAxis={{ type: "category", data: sectionRow.chartData.categories, show: false }}
/>
}
</div>
);
})}
</div>
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;

View File

@@ -1,23 +1,12 @@
import {
bannerType,
type DataColumn,
DataTable,
HorizontalBar, SectionTitle, SignStyle,
StackBar
} from "@/components/lge-dashboard";
import { bannerType, type DataColumn, DataTable, HorizontalBar, SectionTitle, SignStyle, StackBar } from "@/components/lge-dashboard";
import BannerButtons from "@/components/lge-dashboard/BannerButtons";
import {categoryByDataBanner, totalFilters} from "@/const/filter.const";
import React, {useMemo, useState} from "react";
import {BannerGroupKey, CategoryChartMode, BannerRow, LegendOption} from "@/types/lg.types";
import {
buildRowLabelSeriesChartData,
buildYearSeriesChartData, formatRowLabel,
isBannerGroupKey, isGapRow,
wrapSampleRow
} from "@/lib/lg-dashboard";
import {BANNER_GROUP} from "@/const/lg-banner.const";
import {BANNER_KEY_MAP} from "@/const/lg-schema.const";
import {createDefaultGrid, reverseCategories} from "@/lib/chart-options";
import { categoryByDataBanner } from "@/const/filter.const";
import React, { useMemo, useState } from "react";
import { BannerGroupKey, CategoryChartMode, BannerRow, LegendOption } from "@/types/lg.types";
import { buildBannerGroupColumns, buildRowLabelSeriesChartData, buildYearSeriesChartData, formatRowLabel, isBannerGroupKey, isGapRow, wrapSampleRow } from "@/lib/lg-dashboard";
import { BANNER_GROUP } from "@/const/lg-banner.const";
import { BANNER_KEY_MAP } from "@/const/lg-schema.const";
import { createDefaultGrid, reverseCategories } from "@/lib/chart-options";
type CategoryByDataProps = {
selectedYear: string;
@@ -30,11 +19,11 @@ type CategoryByDataProps = {
fillGreaterThan5?: boolean;
chartHeight?: number | string;
chartWidth?: number;
}
};
const banners: bannerType[] = categoryByDataBanner.map((filter) => {
return {variable: filter.variable, variableName: filter.variableName}
})
return { variable: filter.variable, variableName: filter.variableName };
});
const signStyleGap: SignStyle<BannerRow> = {
rowSelector: (row) => isGapRow(row.rowLabel),
@@ -45,62 +34,56 @@ const signStyleGap: SignStyle<BannerRow> = {
};
const CategoryByData = ({
selectedYear,
title,
surveyTitle,
chartMode,
chartLegend,
chartTopMargin,
allTableData,
fillGreaterThan5,
chartHeight,
chartWidth,
}: CategoryByDataProps) => {
selectedYear,
title,
surveyTitle,
chartMode,
chartLegend,
chartTopMargin,
allTableData,
fillGreaterThan5,
chartHeight,
chartWidth,
}: CategoryByDataProps) => {
const [selectedBanner, setSelectedBanner] = useState<bannerType>(banners[0]);
const buildColumns = (groupKey: BannerGroupKey, selectedYear:string): DataColumn<BannerRow>[] => {
const columns: DataColumn<BannerRow>[] = [
const buildColumns = (groupKey: BannerGroupKey, year: string): DataColumn<BannerRow>[] => {
const g = BANNER_GROUP[groupKey];
return [
{
key: "rowLabel",
header: `${selectedYear}년`,
headRowSpan: 2,
header: `${year}년`,
rowHead: true,
cellClassName: "lg-table__rowhead",
width: 120,
},
{
key: "total",
header: `전체`,
headRowSpan: 2,
header: "전체",
numeric: true,
cellClassName: "lg-table__rowhead",
width: 120,
},
{
id: `category-banner-${groupKey}`,
header: g.label,
children: buildBannerGroupColumns<BannerRow>(g, BANNER_KEY_MAP, {
numeric: true,
width: 90,
}),
},
];
const g = BANNER_GROUP[groupKey];
g.keys.forEach((k) => {
columns.push({
key: k as any,
header: BANNER_KEY_MAP[k] ?? String(k),
group: g.label,
numeric: true,
width: 90,
});
});
return columns;
};
const chartData = useMemo(() => {
const excludeRowLabels =["사례수", "gap"];
const excludeRowLabels = ["사례수", "gap"];
const chartTargetData = allTableData.filter((row) => row.year === selectedYear || Number(row.year) === Number(selectedYear) - 1);
if(chartMode == "horizontal"){
return reverseCategories(buildYearSeriesChartData(chartTargetData, {excludeRowLabels}));
if (chartMode === "horizontal") {
return reverseCategories(buildYearSeriesChartData(chartTargetData, { excludeRowLabels }));
}
return buildRowLabelSeriesChartData(chartTargetData, {excludeRowLabels});
return buildRowLabelSeriesChartData(chartTargetData, { excludeRowLabels });
}, [allTableData, selectedYear, chartMode]);
const tableColumns = useMemo(() => {
@@ -108,75 +91,70 @@ const CategoryByData = ({
return isBannerGroupKey(v) ? buildColumns(v, selectedYear) : [];
}, [selectedBanner.variable, selectedYear]);
const tableData = useMemo(() =>
const tableData = useMemo(
() =>
wrapSampleRow(allTableData)
.filter((row) => row.year === selectedYear)
.map((row) => {
row.rowLabel = formatRowLabel(row.rowLabel);
return row
})
, [allTableData, selectedYear]);
.filter((row) => row.year === selectedYear)
.map((row) => {
row.rowLabel = formatRowLabel(row.rowLabel);
return row;
}),
[allTableData, selectedYear]
);
return (
<div>
<div className="mb-2 flex items-center pr-6">
{title ? <SectionTitle title={title} /> : <div />}
</div>
<div className="flex gap-6 p-3">
<div className="flex-shrink-0" style={{ width: `${chartWidth ?? 400}px`}}>
{(() => {
switch (chartMode) {
case "horizontal":
return (
<HorizontalBar
data={chartData}
legend={chartLegend}
grid={chartTopMargin?createDefaultGrid("default", { top: chartTopMargin }):undefined}
height={chartHeight}
showFocusLabel="none"
paletteVarNames={
chartData.series.length === 1 ? ["--chart-1"] : ["--chart-2", "--chart-1"]
}
/>
);
case "stack":
case "reverseStack":
return (
<StackBar
data={chartData}
legend={chartLegend}
height={chartHeight}
grid={createDefaultGrid("default", { top: chartTopMargin?chartTopMargin:52 })}
isReverse={chartMode === "reverseStack"}
/>
);
default:
return null;
}
})()}
</div>
<div className="flex items-center">
<BannerButtons
banners={banners}
selectedBanner={selectedBanner}
onSelectBanner={setSelectedBanner}
/>
</div>
<div className="flex-1">
<DataTable
columns={tableColumns}
rows={tableData}
signStyle={signStyleGap}
fillGreaterThan5={fillGreaterThan5}
starUnder30Sample={true}
/>
</div>
</div>
<div className="flex content-text-sm ml-5 mb-5 -mt-3">{surveyTitle}</div>
<div>
<div className="mb-2 flex items-center pr-6">
{title ? <SectionTitle title={title} /> : <div />}
</div>
<div className="flex gap-6 p-3">
<div className="flex-shrink-0" style={{ width: `${chartWidth ?? 400}px` }}>
{(() => {
switch (chartMode) {
case "horizontal":
return (
<HorizontalBar
data={chartData}
legend={chartLegend}
grid={chartTopMargin ? createDefaultGrid("default", { top: chartTopMargin }) : undefined}
height={chartHeight}
showFocusLabel="none"
paletteVarNames={chartData.series.length === 1 ? ["--chart-1"] : ["--chart-2", "--chart-1"]}
/>
);
case "stack":
case "reverseStack":
return (
<StackBar
data={chartData}
legend={chartLegend}
height={chartHeight}
grid={createDefaultGrid("default", { top: chartTopMargin ? chartTopMargin : 52 })}
isReverse={chartMode === "reverseStack"}
/>
);
default:
return null;
}
})()}
</div>
<div className="flex items-center">
<BannerButtons banners={banners} selectedBanner={selectedBanner} onSelectBanner={setSelectedBanner} />
</div>
<div className="flex-1">
<DataTable
columns={tableColumns}
rows={tableData}
signStyle={signStyleGap}
fillGreaterThan5={fillGreaterThan5}
starUnder30Sample={true}
/>
</div>
</div>
<div className="ml-5 mb-5 -mt-3 flex content-text-sm">{surveyTitle}</div>
</div>
);
};