260202_01_ENH: route 구조 초창기 기획으로 다시 변경 및 Category 별 하위 페이지에 샘플 데이터 모두 적용
This commit is contained in:
162
src/components/lge-dashboard/combo/BannerSummary.tsx
Normal file
162
src/components/lge-dashboard/combo/BannerSummary.tsx
Normal file
@@ -0,0 +1,162 @@
|
||||
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;
|
||||
134
src/components/lge-dashboard/combo/CategoryByData.tsx
Normal file
134
src/components/lge-dashboard/combo/CategoryByData.tsx
Normal file
@@ -0,0 +1,134 @@
|
||||
import {
|
||||
bannerType,
|
||||
type DataColumn,
|
||||
DataTable,
|
||||
HorizontalBar, SectionTitle, SignStyle,
|
||||
StackBar
|
||||
} from "@/components/lge-dashboard";
|
||||
import BannerButtons from "@/components/lge-dashboard/BannerButtons";
|
||||
import {totalFilters} from "@/config/filter.data";
|
||||
import {useMemo, useState} from "react";
|
||||
import {BannerGroupKey, CategoryChartMode, ServerBannerRow} from "@/types/lg";
|
||||
import {buildRowLabelSeriesChartData, buildYearSeriesChartData, isBannerGroupKey} from "@/lib/lg-dashboard";
|
||||
import {BANNER_GROUP, BANNER_KEY_MAP, LAST_YEAR} from "@/const/lgConst";
|
||||
import {createDefaultGrid, createDefaultLegend, reverseCategories} from "@/lib/chart-options";
|
||||
|
||||
type CategoryByDataProps = {
|
||||
title?: string;
|
||||
chartMode: CategoryChartMode;
|
||||
allTableData: ServerBannerRow[];
|
||||
fillGreaterThan5?: boolean;
|
||||
chartHeight?: number;
|
||||
chartWidth?: number;
|
||||
}
|
||||
|
||||
const banners: bannerType[] = totalFilters.map((filter) => {
|
||||
return {variable: filter.variable, variableName: filter.variableName}
|
||||
})
|
||||
|
||||
const signStyleGap: SignStyle<ServerBannerRow> = {
|
||||
rowSelector: (row) => row.rowLabel === "gap",
|
||||
display: "arrow",
|
||||
arrowWithNumber: true,
|
||||
paint: "text",
|
||||
zeroText: "-",
|
||||
};
|
||||
|
||||
const CategoryByData = ({
|
||||
title,
|
||||
chartMode,
|
||||
allTableData,
|
||||
fillGreaterThan5,
|
||||
chartHeight,
|
||||
chartWidth,
|
||||
}: CategoryByDataProps) => {
|
||||
const [selectedBanner, setSelectedBanner] = useState<bannerType>(banners[0]);
|
||||
|
||||
const buildColumns = (groupKey: BannerGroupKey): DataColumn<ServerBannerRow>[] => {
|
||||
const columns: DataColumn<ServerBannerRow>[] = [
|
||||
{
|
||||
key: "rowLabel",
|
||||
header: "",
|
||||
headRowSpan: 2,
|
||||
rowHead: true,
|
||||
cellClassName: "lg-table__rowhead",
|
||||
width: 120,
|
||||
},
|
||||
];
|
||||
|
||||
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"];
|
||||
if(chartMode == "horizontal"){
|
||||
return reverseCategories(buildYearSeriesChartData(allTableData, {excludeRowLabels}));
|
||||
}
|
||||
|
||||
return buildRowLabelSeriesChartData(allTableData, {excludeRowLabels});
|
||||
|
||||
}, [allTableData, chartMode]);
|
||||
|
||||
const tableColumns = useMemo(() => {
|
||||
const v = selectedBanner.variable;
|
||||
return isBannerGroupKey(v) ? buildColumns(v) : [];
|
||||
}, [selectedBanner.variable]);
|
||||
const tableData = useMemo(() => allTableData.filter((row) => row.year === LAST_YEAR), [allTableData, LAST_YEAR])
|
||||
|
||||
return (
|
||||
<div>
|
||||
{title && <SectionTitle title={title} />}
|
||||
<div className="flex gap-6 p-3">
|
||||
<div className="flex-shrink-0" style={{ width: `${chartWidth ?? 400}px`}}>
|
||||
{
|
||||
chartMode === "stack" || chartMode === "reverseStack" ?
|
||||
<StackBar
|
||||
data={chartData}
|
||||
height={`${chartHeight ?? 340}px`}
|
||||
legend={createDefaultLegend({ top: 0 })}
|
||||
grid={createDefaultGrid("default", { top: 52 })}
|
||||
isReverse={chartMode === "reverseStack"}
|
||||
/>
|
||||
:
|
||||
<HorizontalBar
|
||||
data={chartData}
|
||||
height={`${chartHeight ?? 340}px`}
|
||||
showFocusLabel="none"
|
||||
/>
|
||||
}
|
||||
</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}
|
||||
wrapperClassName="border rounded-lg"
|
||||
fillGreaterThan5={fillGreaterThan5}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default CategoryByData;
|
||||
@@ -1,9 +1,10 @@
|
||||
"use client";
|
||||
|
||||
import React, { useMemo } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn, parseNumber } from "@/lib/utils";
|
||||
import type { DataColumn, SignStyle } from "../types";
|
||||
import SectionTitle from "../SectionTitle";
|
||||
import { TABLE_DIMENSIONS } from "@/const/chartConst";
|
||||
|
||||
type MergedChartCell<T> = {
|
||||
/** 차트를 삽입할 컬럼 key(해당 컬럼 td는 startRow에서만 렌더되고 rowSpan 적용) */
|
||||
@@ -33,17 +34,7 @@ type Props<T extends Record<string, unknown>> = {
|
||||
minWidthPx?: number;
|
||||
};
|
||||
|
||||
function toNumber(v: unknown): number | null {
|
||||
if (v === null || v === undefined) return null;
|
||||
if (typeof v === "number") return Number.isFinite(v) ? v : null;
|
||||
if (typeof v === "string") {
|
||||
const s = v.trim();
|
||||
if (s === "" || s === "-") return null;
|
||||
const n = Number(s.replaceAll(",", ""));
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// toNumber는 utils.ts의 parseNumber로 통합됨
|
||||
|
||||
function renderSigned(
|
||||
n: number,
|
||||
@@ -73,7 +64,7 @@ export default function LeftChartTable<T extends Record<string, unknown>>({
|
||||
signStyle,
|
||||
wrapperClassName,
|
||||
tableClassName,
|
||||
minWidthPx = 1200,
|
||||
minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.COMPACT,
|
||||
}: Props<T>) {
|
||||
const hasGroupHeader = useMemo(
|
||||
() => columns.some((c) => c.group) || columns.some((c) => c.headRowSpan === 2),
|
||||
@@ -184,8 +175,8 @@ export default function LeftChartTable<T extends Record<string, unknown>>({
|
||||
<tr key={ri}>
|
||||
{columns.map((c) => {
|
||||
// 머지-차트 셀
|
||||
if (c.key === chartCell.key) {
|
||||
if (ri !== start) return null;
|
||||
if (c.key === chartCell.key && ri >= start) {
|
||||
if (ri > start) return null;
|
||||
return (
|
||||
<td
|
||||
key={String(c.key)}
|
||||
@@ -208,7 +199,7 @@ export default function LeftChartTable<T extends Record<string, unknown>>({
|
||||
? raw
|
||||
: String(raw);
|
||||
|
||||
const n = isSignRow ? toNumber(raw) : null;
|
||||
const n = isSignRow ? parseNumber(raw) : null;
|
||||
const shouldSign = isSignRow && (signStyle?.keys ? signStyle.keys.includes(c.key) : !!c.numeric);
|
||||
|
||||
if (c.rowHead) {
|
||||
|
||||
83
src/components/lge-dashboard/combo/LifeStageDiv.tsx
Normal file
83
src/components/lge-dashboard/combo/LifeStageDiv.tsx
Normal file
@@ -0,0 +1,83 @@
|
||||
import GaugeCardTable from "./GaugeCardTable";
|
||||
import {GaugeColumn} from "@/components/lge-dashboard";
|
||||
import {useMemo} from "react";
|
||||
import {apiLifeStageByProduct} from "@/config/sample.data";
|
||||
import {LifeStageRow} from "@/types/lg";
|
||||
import {asNum, norm} from "@/lib/utils";
|
||||
|
||||
const LifeStageDiv = () => {
|
||||
const LIFE_STAGE_COLUMNS: GaugeColumn<LifeStageRow>[] = [
|
||||
{ key: "item", header: "", rowHead: true, renderLabel: (r) => r.item },
|
||||
|
||||
{ key: "total", header: "전체", max: 100, align: "left", cellType:"number"},
|
||||
{ key: "single", header: "미혼", max: 100 , align: "left"},
|
||||
|
||||
{ key: "noKids", header: "무자녀", group: "기혼", max: 100 , align: "left"},
|
||||
{ key: "preschoolKids", header: "미취학", group: "기혼", max: 100 , align: "left"},
|
||||
{ key: "schoolKids", header: "취학자녀", group: "기혼", max: 100 , align: "left"},
|
||||
{ key: "adultKids", header: "성인자녀", group: "기혼", max: 100 , align: "left"},
|
||||
|
||||
{ key: "independentSenior", header: "독립\n시니어", max: 100 , align: "left"},
|
||||
];
|
||||
|
||||
const LIFE_STAGE_GROUPS = [
|
||||
{ title: "대형가전 군", items: ["냉장고", "세탁기", "건조기", "에어컨", "TV", "김치 냉장고", "식기세척기", "의류 관리기"] },
|
||||
{ title: "소형가전 군", items: ["청소기", "공기 청정기", "정수기"] },
|
||||
{ title: "신가전 군", items: ["로봇 청소기", "음식물처리기"] },
|
||||
] as const;
|
||||
|
||||
const lifeStageRowsByGroup = useMemo(() => {
|
||||
const pick = (label: string) => apiLifeStageByProduct.find((r) => norm(r.rowLabel) === norm(label));
|
||||
|
||||
return LIFE_STAGE_GROUPS.map((g) => {
|
||||
const rows: LifeStageRow[] = g.items.map((label) => {
|
||||
const r = pick(label);
|
||||
|
||||
return {
|
||||
item: label,
|
||||
total: asNum((r as any)?.total), // 서버에 all이 없으면 null로 남음
|
||||
single: asNum((r as any)?.single),
|
||||
noKids: asNum((r as any)?.noKids),
|
||||
preschoolKids: asNum((r as any)?.preschoolKids),
|
||||
schoolKids: asNum((r as any)?.schoolKids),
|
||||
adultKids: asNum((r as any)?.adultKids),
|
||||
independentSenior: asNum((r as any)?.independentSenior),
|
||||
};
|
||||
});
|
||||
|
||||
return { title: g.title, rows };
|
||||
});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="mt-6">
|
||||
<div className="flex justify-end gap-6 text-xs text-muted-foreground mb-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="inline-block h-4 w-8 border bg-gray-200 border-gray-300"/>
|
||||
<span>제품 보유율</span>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<span
|
||||
className="inline-block h-4 w-8 border border-gray-300"
|
||||
style={{ backgroundColor: "hsl(var(--lg-pink-300) / 0.4)" }}
|
||||
/>
|
||||
<span>시장 기회공간</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
{lifeStageRowsByGroup.map((g) => (
|
||||
<GaugeCardTable
|
||||
key={g.title}
|
||||
title={g.title}
|
||||
columns={LIFE_STAGE_COLUMNS}
|
||||
rows={g.rows}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default LifeStageDiv;
|
||||
@@ -1,9 +1,10 @@
|
||||
"use client";
|
||||
|
||||
import React, { useMemo } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn, parseNumber } from "@/lib/utils";
|
||||
import type { ChartData, SignStyle, TopChartTableRow } from "../types";
|
||||
import SectionTitle from "../SectionTitle";
|
||||
import { CHART_HEIGHTS, TABLE_DIMENSIONS } from "@/const/chartConst";
|
||||
|
||||
type BaseProps = {
|
||||
title?: string;
|
||||
@@ -49,17 +50,7 @@ type ChartDataModeProps = BaseProps & {
|
||||
|
||||
export type TopChartTableProps = HtmlModeProps | ChartDataModeProps;
|
||||
|
||||
function toNumber(v: unknown): number | null {
|
||||
if (v === null || v === undefined) return null;
|
||||
if (typeof v === "number") return Number.isFinite(v) ? v : null;
|
||||
if (typeof v === "string") {
|
||||
const s = v.trim();
|
||||
if (s === "" || s === "-") return null;
|
||||
const n = Number(s.replaceAll(",", ""));
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// toNumber는 utils.ts의 parseNumber로 통합됨
|
||||
|
||||
function formatSignedNumber(n: number, zeroText: string, showPlus: boolean) {
|
||||
if (n === 0) return zeroText;
|
||||
@@ -78,8 +69,8 @@ export default function TopChartTable(props: TopChartTableProps) {
|
||||
const {
|
||||
title,
|
||||
chart,
|
||||
chartHeight = 260,
|
||||
minWidthPx = 1600,
|
||||
chartHeight = CHART_HEIGHTS.DEFAULT,
|
||||
minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.STANDARD,
|
||||
tableClassName,
|
||||
wrapperClassName,
|
||||
signStyle,
|
||||
@@ -122,7 +113,7 @@ export default function TopChartTable(props: TopChartTableProps) {
|
||||
categories,
|
||||
series: rows.map((r) => ({
|
||||
name: r.label,
|
||||
data: r.values.map((v) => toNumber(v) ?? null),
|
||||
data: r.values.map((v) => parseNumber(v) ?? null),
|
||||
})),
|
||||
}),
|
||||
[categories, rows]
|
||||
@@ -174,7 +165,7 @@ export default function TopChartTable(props: TopChartTableProps) {
|
||||
|
||||
{categories.map((_, ci) => {
|
||||
const raw = r.values?.[ci];
|
||||
const n = toNumber(raw);
|
||||
const n = parseNumber(raw);
|
||||
|
||||
// 일반 행
|
||||
if (!isSignRow) {
|
||||
|
||||
Reference in New Issue
Block a user