2026-09-01 13:37:19 +08:00
|
|
|
import { useEffect, useRef } from "react"
|
|
|
|
|
import { BarChart, CandlestickChart, LineChart } from "echarts/charts"
|
|
|
|
|
import {
|
|
|
|
|
AxisPointerComponent,
|
|
|
|
|
DataZoomComponent,
|
|
|
|
|
GridComponent,
|
|
|
|
|
LegendComponent,
|
|
|
|
|
TitleComponent,
|
|
|
|
|
TooltipComponent,
|
|
|
|
|
} from "echarts/components"
|
|
|
|
|
import * as echarts from "echarts/core"
|
|
|
|
|
import { CanvasRenderer } from "echarts/renderers"
|
|
|
|
|
|
2026-09-02 18:26:57 +08:00
|
|
|
import { cn } from "@/shared/ui/utils"
|
|
|
|
|
|
2026-09-01 13:37:19 +08:00
|
|
|
import type { SelectionChart as SelectionChartData } from "../api/selection.types"
|
|
|
|
|
import { buildSelectionChartOption } from "./selection-chart-option"
|
|
|
|
|
|
|
|
|
|
echarts.use([
|
|
|
|
|
AxisPointerComponent,
|
|
|
|
|
BarChart,
|
|
|
|
|
CanvasRenderer,
|
|
|
|
|
CandlestickChart,
|
|
|
|
|
DataZoomComponent,
|
|
|
|
|
GridComponent,
|
|
|
|
|
LegendComponent,
|
|
|
|
|
LineChart,
|
|
|
|
|
TitleComponent,
|
|
|
|
|
TooltipComponent,
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
interface SelectionChartProps {
|
|
|
|
|
chart: SelectionChartData
|
2026-09-02 18:26:57 +08:00
|
|
|
className?: string
|
2026-09-01 13:37:19 +08:00
|
|
|
}
|
|
|
|
|
|
2026-09-02 18:26:57 +08:00
|
|
|
export function SelectionChart({ chart, className }: SelectionChartProps) {
|
2026-09-01 13:37:19 +08:00
|
|
|
const containerRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
const instanceRef = useRef<echarts.EChartsType | null>(null)
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const container = containerRef.current
|
|
|
|
|
if (!container) return
|
|
|
|
|
|
|
|
|
|
const instance = echarts.init(container, undefined, {
|
|
|
|
|
renderer: "canvas",
|
|
|
|
|
})
|
|
|
|
|
instanceRef.current = instance
|
|
|
|
|
const observer = new ResizeObserver(() => instance.resize())
|
|
|
|
|
observer.observe(container)
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
observer.disconnect()
|
|
|
|
|
instance.dispose()
|
|
|
|
|
instanceRef.current = null
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
instanceRef.current?.setOption(buildSelectionChartOption(chart.points), {
|
|
|
|
|
notMerge: true,
|
|
|
|
|
})
|
|
|
|
|
}, [chart.points])
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2026-09-01 22:57:58 +08:00
|
|
|
aria-label={`${chart.name} 日线、知行黄白线、成交量和 KDJ 图表`}
|
2026-09-02 18:26:57 +08:00
|
|
|
className={cn("h-full min-h-70 w-full", className)}
|
2026-09-01 13:37:19 +08:00
|
|
|
ref={containerRef}
|
|
|
|
|
role="img"
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|