260202_01_ENH: route 구조 초창기 기획으로 다시 변경 및 Category 별 하위 페이지에 샘플 데이터 모두 적용
This commit is contained in:
@@ -5,6 +5,16 @@ import type { EChartsOption } from "echarts";
|
||||
import BaseEChart from "../BaseEChart";
|
||||
import type { ChartData, ValueFormatter } from "../types";
|
||||
import { buildLgPalette } from "../color";
|
||||
import {
|
||||
createDefaultTooltip,
|
||||
createDefaultLegend,
|
||||
createDefaultGrid,
|
||||
createCategoryAxis,
|
||||
createValueAxis,
|
||||
createBarSeries,
|
||||
createReverseTooltip,
|
||||
createReverseLegend, reverseSeries,
|
||||
} from "@/lib/chart-options";
|
||||
|
||||
type Props = {
|
||||
data: ChartData;
|
||||
@@ -13,7 +23,6 @@ type Props = {
|
||||
paletteVarNames?: string[];
|
||||
valueFormatter?: ValueFormatter;
|
||||
orientation?: "vertical" | "horizontal";
|
||||
/** series.stack이 없을 때 기본 stack key */
|
||||
defaultStackKey?: string;
|
||||
tooltip?: EChartsOption["tooltip"];
|
||||
legend?: EChartsOption["legend"];
|
||||
@@ -21,58 +30,76 @@ type Props = {
|
||||
xAxis?: EChartsOption["xAxis"];
|
||||
yAxis?: EChartsOption["yAxis"];
|
||||
barMaxWidth?: number;
|
||||
isReverse?: boolean;
|
||||
};
|
||||
|
||||
const StackBar = ({
|
||||
data,
|
||||
height,
|
||||
className,
|
||||
paletteVarNames,
|
||||
valueFormatter,
|
||||
orientation = "vertical",
|
||||
defaultStackKey = "total",
|
||||
tooltip,
|
||||
legend,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
barMaxWidth,
|
||||
}: Props) => {
|
||||
const palette = useMemo(() => buildLgPalette(paletteVarNames), [paletteVarNames]);
|
||||
data,
|
||||
height,
|
||||
className,
|
||||
paletteVarNames,
|
||||
valueFormatter,
|
||||
orientation = "vertical",
|
||||
defaultStackKey = "total",
|
||||
tooltip,
|
||||
legend,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
barMaxWidth,
|
||||
isReverse = false,
|
||||
}: Props) => {
|
||||
|
||||
// ✅ 렌더링용 데이터만 뒤집기
|
||||
const renderData = useMemo(
|
||||
() => (isReverse ? reverseSeries(data) : data),
|
||||
[data, isReverse]
|
||||
);
|
||||
|
||||
// ✅ 팔레트도 뒤집어줘야 "시리즈 이름별" 색이 유지됨
|
||||
const palette = useMemo(() => {
|
||||
const temp = buildLgPalette(paletteVarNames);
|
||||
const n = data.series.length; // 원본 기준
|
||||
const base = temp.slice(0, n);
|
||||
return isReverse ? [...base].reverse() : base;
|
||||
}, [paletteVarNames, data.series.length, isReverse]);
|
||||
|
||||
const option: EChartsOption = useMemo(() => {
|
||||
const fmt = valueFormatter ?? ((v: number) => String(v));
|
||||
|
||||
const isV = orientation === "vertical";
|
||||
|
||||
const defaultTooltip: EChartsOption["tooltip"] = {
|
||||
trigger: "axis",
|
||||
axisPointer: { type: "shadow" },
|
||||
valueFormatter: (v: unknown) => (typeof v === "number" ? fmt(v) : String(v)),
|
||||
};
|
||||
const defaultLegend: EChartsOption["legend"] = { top: 0 };
|
||||
const defaultGrid: EChartsOption["grid"] = { left: 16, right: 16, top: 36, bottom: 12, containLabel: true };
|
||||
const defaultXAxis: EChartsOption["xAxis"] = isV ? { type: "category", data: data.categories } : { type: "value" };
|
||||
const defaultYAxis: EChartsOption["yAxis"] = isV ? { type: "value" } : { type: "category", data: data.categories };
|
||||
return {
|
||||
color: palette,
|
||||
tooltip: tooltip ?? defaultTooltip,
|
||||
legend: legend ?? defaultLegend,
|
||||
grid: grid ?? defaultGrid,
|
||||
xAxis: xAxis ?? defaultXAxis,
|
||||
yAxis: yAxis ?? defaultYAxis,
|
||||
series: data.series.map((s) => ({
|
||||
name: s.name,
|
||||
type: "bar",
|
||||
stack: s.stack ?? defaultStackKey,
|
||||
data: s.data,
|
||||
emphasis: { disabled: true },
|
||||
barMaxWidth: barMaxWidth ?? 42,
|
||||
})),
|
||||
tooltip: tooltip ?? (isReverse ? createReverseTooltip(data, fmt) : createDefaultTooltip(fmt)),
|
||||
legend: legend ?? (isReverse ? createReverseLegend(data) : createDefaultLegend()),
|
||||
grid: grid ?? createDefaultGrid(),
|
||||
xAxis: xAxis ?? (isV ? createCategoryAxis(renderData.categories) : createValueAxis()),
|
||||
yAxis: yAxis ?? (isV ? createValueAxis() : createCategoryAxis(renderData.categories)),
|
||||
|
||||
series: renderData.series.map((s) =>
|
||||
createBarSeries(s.name, s.data, {
|
||||
stack: s.stack ?? defaultStackKey,
|
||||
barMaxWidth,
|
||||
})
|
||||
),
|
||||
};
|
||||
}, [data, palette, valueFormatter, orientation, defaultStackKey, tooltip, legend, grid, xAxis, yAxis, barMaxWidth]);
|
||||
}, [
|
||||
data,
|
||||
renderData,
|
||||
palette,
|
||||
valueFormatter,
|
||||
orientation,
|
||||
defaultStackKey,
|
||||
tooltip,
|
||||
legend,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
barMaxWidth,
|
||||
isReverse,
|
||||
]);
|
||||
|
||||
return <BaseEChart option={option} height={height} className={className} />;
|
||||
}
|
||||
};
|
||||
|
||||
export default StackBar;
|
||||
Reference in New Issue
Block a user