import React, { useState, useMemo } from 'react'; import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Brush } from 'recharts'; interface StockChartProps { data: Array<{ date: string; value: number }>; color?: string; showBrush?: boolean; height?: string; showTimeRange?: boolean; // 是否显示时间范围选择 legend?: string; // 图例文本 } export const StockChart: React.FC = ({ data, color = '#882323', showBrush = true, height = '100%', showTimeRange = true, legend }) => { const [selectedRange, setSelectedRange] = useState('ALL'); // 根据选择的时间范围过滤数据 const filteredData = useMemo(() => { if (selectedRange === 'ALL') return data; const rangeMap: Record = { '1Y': 12, '3Y': 36, '5Y': 60 }; const months = rangeMap[selectedRange]; if (!months) return data; return data.slice(-months); }, [data, selectedRange]); return (
{/* 顶部栏:图例和时间范围选择 */}
{/* 图例 - 左上角 */} {legend && (
{legend}
)} {/* 时间范围选择按钮 - 右上角 */} {showTimeRange && (
{['1Y', '3Y', '5Y', 'ALL'].map(range => ( ))}
)}
{/* 图表容器 */}
{showBrush && ( )}
); };