260127_01_ENH: 커스텀 컴포넌트들 1차 완성 및 일부페이지에 SampleData 적용
This commit is contained in:
78
src/components/lge-dashboard/chart/StackBar.tsx
Normal file
78
src/components/lge-dashboard/chart/StackBar.tsx
Normal file
@@ -0,0 +1,78 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo } from "react";
|
||||
import type { EChartsOption } from "echarts";
|
||||
import BaseEChart from "../BaseEChart";
|
||||
import type { ChartData, ValueFormatter } from "../types";
|
||||
import { buildLgPalette } from "../color";
|
||||
|
||||
type Props = {
|
||||
data: ChartData;
|
||||
height?: number | string;
|
||||
className?: string;
|
||||
paletteVarNames?: string[];
|
||||
valueFormatter?: ValueFormatter;
|
||||
orientation?: "vertical" | "horizontal";
|
||||
/** series.stack이 없을 때 기본 stack key */
|
||||
defaultStackKey?: string;
|
||||
tooltip?: EChartsOption["tooltip"];
|
||||
legend?: EChartsOption["legend"];
|
||||
grid?: EChartsOption["grid"];
|
||||
xAxis?: EChartsOption["xAxis"];
|
||||
yAxis?: EChartsOption["yAxis"];
|
||||
barMaxWidth?: number;
|
||||
};
|
||||
|
||||
const StackBar = ({
|
||||
data,
|
||||
height,
|
||||
className,
|
||||
paletteVarNames,
|
||||
valueFormatter,
|
||||
orientation = "vertical",
|
||||
defaultStackKey = "total",
|
||||
tooltip,
|
||||
legend,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
barMaxWidth,
|
||||
}: Props) => {
|
||||
const palette = useMemo(() => buildLgPalette(paletteVarNames), [paletteVarNames]);
|
||||
|
||||
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,
|
||||
})),
|
||||
};
|
||||
}, [data, palette, valueFormatter, orientation, defaultStackKey, tooltip, legend, grid, xAxis, yAxis, barMaxWidth]);
|
||||
|
||||
return <BaseEChart option={option} height={height} className={className} />;
|
||||
}
|
||||
|
||||
export default StackBar;
|
||||
Reference in New Issue
Block a user