Files
lg-cxdash-front/src/components/lge-dashboard/combo/BannerSummary.tsx

162 lines
4.7 KiB
TypeScript

import {ChartData, DataColumn, DataTable, HorizontalBar, LeftChartTable, SignStyle} from "@/components/lge-dashboard";
import {BannerKey, ServerBannerRow} from "@/types/lg";
import {formatRowLabel, isSampleRow} from "@/lib/lg-dashboard";
import {useMemo} from "react";
import {BANNER_GROUP, BANNER_KEY_MAP, GRID_Y_NOT_PADDING} from "@/const/lgConst";
import {formatNumber, parseNumber} from "@/lib/utils";
type SectionSpec = {
title: string;
groupKeys: Array<keyof typeof BANNER_GROUP>;
};
/** 3개 섹션(테이블)도 여기서만 선언 */
const SECTIONS: SectionSpec[] = [
{ title: "가구 형태", groupKeys: ["lifeStage", "household", "age"] },
{ title: "거주 형태", groupKeys: ["tenure", "housing", "pyeong"] },
{ 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): DataColumn<ServerBannerRow>[] => {
const columns: DataColumn<ServerBannerRow>[] = [
{
key: "rowLabel",
header: "",
headRowSpan: 2,
rowHead: true,
cellClassName: "lg-table__rowhead",
width: 120,
},
{
key: "total",
header: "전체",
numeric: true,
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: ServerBannerRow[];
useChart: boolean;
};
const BannerSummary = ({ data, useChart }: BannerSummaryProps) => {
const buildChartData = (rows: ServerBannerRow[], bannerKeys: BannerKey[]): ChartData => {
const categories = rows
.filter((r) => r.rowLabel !== "사례수")
.map((r) => formatRowLabel(r.rowLabel));
const series = [
{
name:"전체",
data: rows
.filter((r) => r.rowLabel !== "사례수")
.map((r) => parseNumber(r.total) ?? null),
}
];
return { categories, series };
};
const rows = useMemo<ServerBannerRow[]>(() => {
const allKeys = Array.from(
new Set(Object.values(BANNER_GROUP).flatMap((g) => g.keys))
) as string[];
return data.map((r) => {
const out: any = { ...r };
if (isSampleRow(r.rowLabel)) {
// total
out.total = `(${formatNumber(Number(r.total ?? 0))})`;
// banner values
allKeys.forEach((k) => {
const v = (r as any)[k];
if (typeof v === "number") out[k] = `(${formatNumber(v)})`;
});
}
return out as ServerBannerRow;
});
}, [data]);
const signStyleGap: SignStyle<ServerBannerRow> = {
rowSelector: (row) => row.rowLabel === "gap",
display: "arrow",
arrowWithNumber: true,
paint: "text",
zeroText: "-",
};
return (
<div className="flex flex-col gap-6">
{SECTIONS.map((spec) => {
const bannerKeys = getSectionBannerKeys(spec);
const columns = buildColumns(spec);
const chartData = buildChartData(rows, bannerKeys);
return (
useChart ?
<LeftChartTable<ServerBannerRow>
key={spec.title}
title={spec.title}
columns={columns}
rows={rows}
signStyle={signStyleGap}
chart={
<HorizontalBar
data={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 }}
/>
}
chartCell={{ key: "total", startRowIndex: 1 }}
minWidthPx={1200}
/>
:
<DataTable<ServerBannerRow>
key={spec.title}
title={spec.title}
columns={columns}
rows={rows}
signStyle={signStyleGap}
/>
);
})}
</div>
);
}
export default BannerSummary;