260127_01_ENH: 커스텀 컴포넌트들 1차 완성 및 일부페이지에 SampleData 적용
This commit is contained in:
65
src/components/lge-dashboard/combo/CategoryByData.tsx
Normal file
65
src/components/lge-dashboard/combo/CategoryByData.tsx
Normal file
@@ -0,0 +1,65 @@
|
||||
import {bannerType, type ChartData, type DataColumn, DataTable, StackBar} from "@/components/lge-dashboard";
|
||||
import BannerButtons from "@/components/lge-dashboard/BannerButtons";
|
||||
import {totalFilters} from "@/config/filter.data";
|
||||
import {useEffect, useState} from "react";
|
||||
|
||||
type CategoryByDataProps<T extends Record<string, unknown>> = {
|
||||
chartData:ChartData;
|
||||
tableColumns: DataColumn<T>[];
|
||||
allTableData: T[];
|
||||
}
|
||||
|
||||
const banners: bannerType[] = totalFilters.map((filter) => {
|
||||
return {variable: filter.variable, variableName: filter.variableName}
|
||||
})
|
||||
|
||||
const CategoryByData = <T extends Record<string, unknown>>({
|
||||
chartData,
|
||||
tableColumns,
|
||||
allTableData,
|
||||
}: CategoryByDataProps<T>) => {
|
||||
const [selectedBanner, setSelectedBanner] = useState<bannerType>(banners[0]);
|
||||
const [tableData, setTableData] = useState<T[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const filteredTableData = allTableData.filter((row) => row[selectedBanner.variable] === selectedBanner.variableName);
|
||||
setTableData(filteredTableData);
|
||||
}, [selectedBanner, allTableData]);
|
||||
|
||||
return (
|
||||
<div className="flex gap-6 p-6">
|
||||
{/* 좌측: 차트 */}
|
||||
<div className="flex-shrink-0" style={{ width: "300px" }}>
|
||||
<StackBar
|
||||
data={chartData}
|
||||
height={300}
|
||||
paletteVarNames={[
|
||||
"--lg-red-300",
|
||||
"--lg-red-400",
|
||||
"--lg-red-500",
|
||||
]}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 중앙: 버튼 */}
|
||||
<div className="flex items-center">
|
||||
<BannerButtons
|
||||
banners={banners}
|
||||
selectedBanner={selectedBanner}
|
||||
onSelectBanner={setSelectedBanner}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 우측: 테이블 */}
|
||||
<div className="flex-1">
|
||||
<DataTable
|
||||
columns={tableColumns}
|
||||
rows={tableData}
|
||||
wrapperClassName="border rounded-lg"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default CategoryByData;
|
||||
266
src/components/lge-dashboard/combo/LeftChartTable.tsx
Normal file
266
src/components/lge-dashboard/combo/LeftChartTable.tsx
Normal file
@@ -0,0 +1,266 @@
|
||||
"use client";
|
||||
|
||||
import React, { useMemo } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { DataColumn, SignStyle } from "../types";
|
||||
|
||||
type MergedChartCell<T> = {
|
||||
/** 차트를 삽입할 컬럼 key(해당 컬럼 td는 startRow에서만 렌더되고 rowSpan 적용) */
|
||||
key: keyof T;
|
||||
/** rowSpan 시작 row index (기본 0) */
|
||||
startRowIndex?: number;
|
||||
/** rowSpan 수 (기본: rows.length - startRowIndex) */
|
||||
rowSpan?: number;
|
||||
/** chart td 클래스 (예: lg-table__sep-r) */
|
||||
className?: string;
|
||||
};
|
||||
|
||||
type Props<T extends Record<string, unknown>> = {
|
||||
title?: string;
|
||||
columns: DataColumn<T>[];
|
||||
rows: T[];
|
||||
/** 머지된 셀 안에 렌더할 차트 */
|
||||
chart: React.ReactNode;
|
||||
/** 어떤 컬럼을 머지-차트 셀로 교체할지 */
|
||||
chartCell: MergedChartCell<T>;
|
||||
|
||||
/** +/-/0 표기/색상 옵션(선택) */
|
||||
signStyle?: SignStyle<T>;
|
||||
|
||||
wrapperClassName?: string;
|
||||
tableClassName?: string;
|
||||
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;
|
||||
}
|
||||
|
||||
function renderSigned(
|
||||
n: number,
|
||||
opts: {
|
||||
display: "number" | "arrow";
|
||||
showPlus: boolean;
|
||||
arrowWithNumber: boolean;
|
||||
zeroText: string;
|
||||
}
|
||||
) {
|
||||
if (n === 0) return opts.zeroText;
|
||||
if (opts.display === "arrow") {
|
||||
const arrow = n > 0 ? "▲" : "▼";
|
||||
const num = Math.abs(n);
|
||||
return opts.arrowWithNumber ? `${arrow} ${num}` : arrow;
|
||||
}
|
||||
if (n > 0) return opts.showPlus ? `+${n}` : `${n}`;
|
||||
return `${n}`;
|
||||
}
|
||||
|
||||
export default function LeftChartTable<T extends Record<string, unknown>>({
|
||||
title,
|
||||
columns,
|
||||
rows,
|
||||
chart,
|
||||
chartCell,
|
||||
signStyle,
|
||||
wrapperClassName,
|
||||
tableClassName,
|
||||
minWidthPx = 1200,
|
||||
}: Props<T>) {
|
||||
const hasGroupHeader = useMemo(
|
||||
() => columns.some((c) => c.group) || columns.some((c) => c.headRowSpan === 2),
|
||||
[columns]
|
||||
);
|
||||
|
||||
const groupCells = useMemo(() => {
|
||||
if (!hasGroupHeader) return [] as Array<
|
||||
| { kind: "rowspan"; label: string; colSpan: 1; rowSpan: 2; className?: string }
|
||||
| { kind: "group"; label: string; colSpan: number; rowSpan: 1; className?: string }
|
||||
| { kind: "blank"; label: string; colSpan: 1; rowSpan: 1; className?: string }
|
||||
>;
|
||||
|
||||
type Cell =
|
||||
| { kind: "rowspan"; label: string; colSpan: 1; rowSpan: 2; className?: string }
|
||||
| { kind: "group"; label: string; colSpan: number; rowSpan: 1; className?: string }
|
||||
| { kind: "blank"; label: string; colSpan: 1; rowSpan: 1; className?: string };
|
||||
|
||||
const out: Cell[] = [];
|
||||
let i = 0;
|
||||
while (i < columns.length) {
|
||||
const c = columns[i];
|
||||
if (c.headRowSpan === 2) {
|
||||
out.push({
|
||||
kind: "rowspan",
|
||||
label: c.header,
|
||||
colSpan: 1,
|
||||
rowSpan: 2,
|
||||
className: c.headerClassName,
|
||||
});
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const g = c.group ?? "";
|
||||
if (!g) {
|
||||
out.push({ kind: "blank", label: "", colSpan: 1, rowSpan: 1, className: c.headerClassName });
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
let span = 1;
|
||||
while (
|
||||
i + span < columns.length &&
|
||||
(columns[i + span].headRowSpan ?? 1) !== 2 &&
|
||||
(columns[i + span].group ?? "") === g
|
||||
) {
|
||||
span += 1;
|
||||
}
|
||||
|
||||
out.push({ kind: "group", label: g, colSpan: span, rowSpan: 1, className: columns[i + span -1].headerClassName });
|
||||
i += span;
|
||||
}
|
||||
return out;
|
||||
}, [columns, hasGroupHeader]);
|
||||
|
||||
const display = signStyle?.display ?? "number";
|
||||
const arrowWithNumber = signStyle?.arrowWithNumber ?? true;
|
||||
const showPlus = signStyle?.showPlus ?? true;
|
||||
const zeroText = signStyle?.zeroText ?? "-";
|
||||
const posCls = signStyle?.positiveTextClassName ?? "lg-num-pos";
|
||||
const negCls = signStyle?.negativeTextClassName ?? "lg-num-neg";
|
||||
const zeroCls = signStyle?.zeroTextClassName ?? "";
|
||||
|
||||
const start = chartCell.startRowIndex ?? 0;
|
||||
const rowSpan = chartCell.rowSpan ?? Math.max(0, rows.length - start);
|
||||
|
||||
return (
|
||||
<div className={cn("w-full", wrapperClassName)}>
|
||||
{title && (
|
||||
<div className="mb-2 font-semibold flex items-center gap-2">
|
||||
<span className="w-3 h-3 bg-primary"></span>
|
||||
{title}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="overflow-x-auto">
|
||||
<table className={cn("lg-table", tableClassName)} style={{ minWidth: `${minWidthPx}px` }}>
|
||||
<thead>
|
||||
{hasGroupHeader ? (
|
||||
<tr className="lg-table__group">
|
||||
{groupCells.map((g, idx) => (
|
||||
<th
|
||||
key={`${g.kind}-${idx}`}
|
||||
className={cn(g.className)}
|
||||
colSpan={g.colSpan}
|
||||
rowSpan={g.rowSpan}
|
||||
>
|
||||
{g.label}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
) : null}
|
||||
|
||||
<tr className={hasGroupHeader ? "lg-table__subhead" : undefined}>
|
||||
{columns
|
||||
.filter((c) => (c.headRowSpan ?? 1) !== 2)
|
||||
.map((c) => (
|
||||
<th key={String(c.key)} className={cn(c.headerClassName)} style={c.width ? { width: c.width } : undefined}>
|
||||
{c.header}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
{rows.map((row, ri) => {
|
||||
const isSignRow = signStyle?.rowSelector ? signStyle.rowSelector(row, ri) : false;
|
||||
|
||||
return (
|
||||
<tr key={ri}>
|
||||
{columns.map((c) => {
|
||||
// 머지-차트 셀
|
||||
if (c.key === chartCell.key) {
|
||||
if (ri !== start) return null;
|
||||
return (
|
||||
<td
|
||||
key={String(c.key)}
|
||||
rowSpan={rowSpan}
|
||||
className={cn(c.cellClassName, chartCell.className)}
|
||||
style={{ padding: 0, verticalAlign: "middle" }}
|
||||
>
|
||||
{chart}
|
||||
</td>
|
||||
);
|
||||
}
|
||||
|
||||
// 일반 셀
|
||||
const raw = c.key in row ? row[c.key] : undefined;
|
||||
const content = c.render
|
||||
? c.render(row)
|
||||
: raw === null || raw === undefined
|
||||
? ""
|
||||
: typeof raw === "string" || typeof raw === "number"
|
||||
? raw
|
||||
: String(raw);
|
||||
|
||||
const n = isSignRow ? toNumber(raw) : null;
|
||||
const shouldSign = isSignRow && (signStyle?.keys ? signStyle.keys.includes(c.key) : !!c.numeric);
|
||||
|
||||
if (c.rowHead) {
|
||||
return (
|
||||
<th
|
||||
key={String(c.key)}
|
||||
scope="row"
|
||||
className={cn("lg-table__rowhead", c.cellClassName)}
|
||||
style={c.width ? { width: c.width } : undefined}
|
||||
>
|
||||
{content}
|
||||
</th>
|
||||
);
|
||||
}
|
||||
|
||||
if (!shouldSign) {
|
||||
return (
|
||||
<td
|
||||
key={String(c.key)}
|
||||
className={cn(c.cellClassName)}
|
||||
style={c.width ? { width: c.width } : undefined}
|
||||
>
|
||||
{content}
|
||||
</td>
|
||||
);
|
||||
}
|
||||
|
||||
// sign row: 값이 없으면 "-" 유지
|
||||
if (n === null) {
|
||||
return (
|
||||
<td key={String(c.key)} className={cn(c.cellClassName)}>
|
||||
-
|
||||
</td>
|
||||
);
|
||||
}
|
||||
|
||||
const cls = n > 0 ? posCls : n < 0 ? negCls : zeroCls;
|
||||
const txt = renderSigned(n, { display, showPlus, arrowWithNumber, zeroText });
|
||||
return (
|
||||
<td key={String(c.key)} className={cn(c.cellClassName, cls)}>
|
||||
{txt}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
219
src/components/lge-dashboard/combo/TopChartTable.tsx
Normal file
219
src/components/lge-dashboard/combo/TopChartTable.tsx
Normal file
@@ -0,0 +1,219 @@
|
||||
"use client";
|
||||
|
||||
import React, { useMemo } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { ChartData, SignStyle, TopChartTableRow } from "../types";
|
||||
|
||||
type BaseProps = {
|
||||
title?: string;
|
||||
/** 테이블 상단(헤더 1행) 콜스팬 셀 내부에 렌더될 차트 */
|
||||
chart?:
|
||||
| React.ReactNode
|
||||
| ((ctx: {
|
||||
/** 테이블 컬럼(=x축) 기준으로 구성된 ChartData */
|
||||
chartData: ChartData;
|
||||
tableCategories: string[];
|
||||
tableRows: TopChartTableRow[];
|
||||
}) => React.ReactNode);
|
||||
/** chart 셀 높이(px) */
|
||||
chartHeight?: number;
|
||||
/** 가로 스크롤 min-width */
|
||||
minWidthPx?: number;
|
||||
tableClassName?: string;
|
||||
wrapperClassName?: string;
|
||||
/** +/-/0 색상/표기 옵션(선택) */
|
||||
signStyle?: SignStyle<TopChartTableRow>;
|
||||
};
|
||||
|
||||
/**
|
||||
* 1) (신규) HTML 형태: columns=categories, rows=TopChartTableRow
|
||||
*/
|
||||
type HtmlModeProps = BaseProps & {
|
||||
categories: string[];
|
||||
rows: TopChartTableRow[];
|
||||
};
|
||||
|
||||
/**
|
||||
* 2) (호환) 기존 형태: ChartData 기반으로 rows/columns 자동 생성
|
||||
* - rows = data.categories
|
||||
* - cols = data.series.name
|
||||
*/
|
||||
type ChartDataModeProps = BaseProps & {
|
||||
data: ChartData;
|
||||
/** 좌측 rowhead 컬럼 헤더 */
|
||||
categoryHeader?: string;
|
||||
/** 값 표기 포맷터 */
|
||||
valueFormatter?: (v: number) => string;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function formatSignedNumber(n: number, zeroText: string, showPlus: boolean) {
|
||||
if (n === 0) return zeroText;
|
||||
if (n > 0) return showPlus ? `+${n}` : `${n}`;
|
||||
return `${n}`;
|
||||
}
|
||||
|
||||
type CellValue = TopChartTableRow["values"][number];
|
||||
function toCellValue(v: unknown): CellValue {
|
||||
if (v === null || v === undefined) return v;
|
||||
if (typeof v === "number" || typeof v === "string") return v;
|
||||
return String(v);
|
||||
}
|
||||
|
||||
export default function TopChartTable(props: TopChartTableProps) {
|
||||
const {
|
||||
title,
|
||||
chart,
|
||||
chartHeight = 260,
|
||||
minWidthPx = 1600,
|
||||
tableClassName,
|
||||
wrapperClassName,
|
||||
signStyle,
|
||||
} = props;
|
||||
|
||||
const display = signStyle?.display ?? "number";
|
||||
const arrowWithNumber = signStyle?.arrowWithNumber ?? true;
|
||||
const showPlus = signStyle?.showPlus ?? true;
|
||||
const posCls = signStyle?.positiveTextClassName ?? "lg-num-pos";
|
||||
const negCls = signStyle?.negativeTextClassName ?? "lg-num-neg";
|
||||
const zeroText = signStyle?.zeroText ?? "-";
|
||||
|
||||
const { categories, rows, rowHeadHeader, valueFormatter } = useMemo(() => {
|
||||
if ("data" in props) {
|
||||
const d = props.data;
|
||||
const tableRows: TopChartTableRow[] = d.categories.map((cat, i) => ({
|
||||
label: String(cat ?? ""),
|
||||
values: d.series.map((s) => toCellValue(s.data[i] ?? null)),
|
||||
}));
|
||||
|
||||
return {
|
||||
categories: d.series.map((s) => s.name),
|
||||
rows: tableRows,
|
||||
rowHeadHeader: props.categoryHeader ?? "",
|
||||
valueFormatter: props.valueFormatter,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
categories: props.categories,
|
||||
rows: props.rows,
|
||||
rowHeadHeader: "",
|
||||
valueFormatter: undefined,
|
||||
};
|
||||
}, [props]);
|
||||
|
||||
// 테이블 컬럼(=categories)을 x축으로 사용하는 chartData를 항상 제공
|
||||
const chartData: ChartData = useMemo(
|
||||
() => ({
|
||||
categories,
|
||||
series: rows.map((r) => ({
|
||||
name: r.label,
|
||||
data: r.values.map((v) => toNumber(v) ?? null),
|
||||
})),
|
||||
}),
|
||||
[categories, rows]
|
||||
);
|
||||
|
||||
const chartNode = useMemo(() => {
|
||||
if (!chart) return null;
|
||||
return typeof chart === "function" ? chart({ chartData, tableCategories: categories, tableRows: rows }) : chart;
|
||||
}, [chart, chartData, categories, rows]);
|
||||
|
||||
return (
|
||||
<div className={cn("w-full", wrapperClassName)}>
|
||||
{title && (
|
||||
<div className="mb-2 font-semibold flex items-center gap-2">
|
||||
<span className="w-3 h-3 bg-primary"></span>
|
||||
{title}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="overflow-x-auto">
|
||||
<table
|
||||
className={cn("lg-table", tableClassName)}
|
||||
style={{ minWidth: `${minWidthPx}px` }}
|
||||
>
|
||||
<thead>
|
||||
<tr>
|
||||
{/* 좌상단: rowhead 헤더(필요 시 표시). HTML과 동일하게 2행 rowspan */}
|
||||
<th rowSpan={2} className="lg-table__sep-r w-15" style={{ whiteSpace: "nowrap" }}>
|
||||
{rowHeadHeader}
|
||||
</th>
|
||||
<td colSpan={categories.length} style={{ padding: 0, height: chartNode ? chartHeight : 0 }}>
|
||||
{chartNode ? <div style={{ width: "100%", height: "100%" }}>{chartNode}</div> : null}
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr className="lg-table__subhead">
|
||||
{categories.map((c) => (
|
||||
<th key={c} className={"content-text-xs"}>{c}</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
{rows.map((r, ri) => {
|
||||
const isSignRow = !!(signStyle?.rowSelector && signStyle.rowSelector(r, ri));
|
||||
|
||||
return (
|
||||
<tr key={`${r.label}-${ri}`}>
|
||||
<th scope="row" className="lg-table__rowhead lg-table__sep-r content-text-sm">
|
||||
{r.label}
|
||||
</th>
|
||||
|
||||
{categories.map((_, ci) => {
|
||||
const raw = r.values?.[ci];
|
||||
const n = toNumber(raw);
|
||||
|
||||
// 일반 행
|
||||
if (!isSignRow) {
|
||||
if (n === null) return <td key={ci}>-</td>;
|
||||
const txt = valueFormatter ? valueFormatter(n) : n.toLocaleString();
|
||||
return <td key={ci}>{txt}</td>;
|
||||
}
|
||||
|
||||
// sign row
|
||||
if (n === null) return <td key={ci}>-</td>;
|
||||
if (n === 0) return <td key={ci}>{zeroText}</td>;
|
||||
|
||||
const cls = n > 0 ? posCls : negCls;
|
||||
|
||||
if (display === "arrow") {
|
||||
const arrow = n > 0 ? "▲" : "▼";
|
||||
const num = Math.abs(n);
|
||||
return (
|
||||
<td key={ci} className={cls}>
|
||||
{arrow}
|
||||
{arrowWithNumber ? ` ${num}` : ""}
|
||||
</td>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<td key={ci} className={cls}>
|
||||
{formatSignedNumber(n, zeroText, showPlus)}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user