92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
"use client";
|
|
|
|
import { useMemo } from "react";
|
|
import type { EChartsOption } from "echarts";
|
|
import BaseEChart from "../BaseEChart";
|
|
import {ChartData, ShowLabel, ValueFormatter} from "../types";
|
|
import { buildLgPalette } from "../color";
|
|
import {getIsShowLabel} from "@/components/lge-dashboard/chart/utill";
|
|
import {
|
|
createDefaultTooltip,
|
|
createDefaultLegend,
|
|
createDefaultGrid,
|
|
createCategoryAxis,
|
|
createValueAxis,
|
|
} from "@/lib/chart-options";
|
|
import { BAR_WIDTHS } from "@/components/lge-dashboard/chart/chart.config";
|
|
|
|
type Props = {
|
|
data: ChartData;
|
|
height?: number | string;
|
|
className?: string;
|
|
paletteVarNames?: string[];
|
|
valueFormatter?: ValueFormatter;
|
|
tooltip?: EChartsOption["tooltip"];
|
|
legend?: EChartsOption["legend"];
|
|
grid?: EChartsOption["grid"];
|
|
xAxis?: EChartsOption["xAxis"];
|
|
yAxis?: EChartsOption["yAxis"];
|
|
barMaxWidth?: number;
|
|
showFocusLabel?:ShowLabel;
|
|
labelOptions?: {
|
|
position?: "right" | "insideRight";
|
|
color?: string;
|
|
distance?: number;
|
|
};
|
|
};
|
|
|
|
const HorizontalBar = ({
|
|
data,
|
|
height,
|
|
className,
|
|
paletteVarNames,
|
|
valueFormatter,
|
|
tooltip,
|
|
legend,
|
|
grid,
|
|
xAxis,
|
|
yAxis,
|
|
barMaxWidth,
|
|
showFocusLabel = "all",
|
|
labelOptions,
|
|
}: Props) => {
|
|
const palette = useMemo(() => buildLgPalette(paletteVarNames), [paletteVarNames]);
|
|
|
|
const option: EChartsOption = useMemo(() => {
|
|
const fmt = valueFormatter ?? ((v: number) => String(v));
|
|
|
|
return {
|
|
color: palette,
|
|
tooltip: tooltip ?? createDefaultTooltip(fmt),
|
|
legend: legend ?? createDefaultLegend(),
|
|
grid: grid ?? createDefaultGrid(),
|
|
xAxis: xAxis ?? createValueAxis(),
|
|
yAxis: yAxis ?? createCategoryAxis(data.categories),
|
|
series: data.series.map((s, index) => {
|
|
const showLabel = getIsShowLabel(showFocusLabel, index, data.series.length);
|
|
|
|
return {
|
|
name: s.name,
|
|
type: "bar",
|
|
data: s.data,
|
|
emphasis: { disabled: true },
|
|
barMaxWidth: barMaxWidth ?? BAR_WIDTHS.DEFAULT,
|
|
label: showLabel
|
|
? {
|
|
show: true,
|
|
position: labelOptions?.position ?? "right",
|
|
color: labelOptions?.color,
|
|
distance: labelOptions?.distance,
|
|
formatter: (p: any) => (p?.value ?? "") as any,
|
|
}
|
|
: undefined,
|
|
};
|
|
}),
|
|
};
|
|
}, [data, palette, valueFormatter, tooltip, legend, grid, xAxis, yAxis, barMaxWidth, showFocusLabel, labelOptions]);
|
|
|
|
return <BaseEChart option={option} height={height} className={className} />;
|
|
}
|
|
|
|
export default HorizontalBar;
|