import React, { useEffect, useRef, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { Card, Row, Col, Button, Select, Tag, Statistic, Alert, Spin } from 'antd'; import { ArrowUpOutlined, ArrowDownOutlined, LineChartOutlined, BarChartOutlined, AlertOutlined, CalculatorOutlined } from '@ant-design/icons'; import { fetchFutureDetail } from '../../store/futuresSlice'; import { useLocation, useNavigate } from 'react-router-dom'; import { generateKlineData } from '../../utils/mockData'; import './Detail.css'; // 导入TradingView Lightweight Charts import { createChart } from 'lightweight-charts'; const { Option } = Select; const Detail = () => { const dispatch = useDispatch(); const navigate = useNavigate(); const location = useLocation(); const chartRef = useRef(null); const chartInstance = useRef(null); const { selectedFuture, loading } = useSelector(state => state.futures); const [timeframe, setTimeframe] = useState('1D'); // 解析URL参数获取品种信息 const getQueryParams = () => { const params = new URLSearchParams(location.search); return { code: params.get('code') || 'MA', name: params.get('name') || '甲醇' }; }; const { code, name } = getQueryParams(); useEffect(() => { // 获取品种详情数据 dispatch(fetchFutureDetail({ code, name })); }, [dispatch, code, name]); useEffect(() => { // 初始化K线图表 if (chartRef.current && selectedFuture) { if (chartInstance.current) { chartInstance.current.destroy(); } const chart = createChart(chartRef.current, { width: chartRef.current.clientWidth, height: 400, layout: { backgroundColor: '#fff', textColor: '#262626' }, grid: { vertLines: { color: '#f0f0f0' }, horzLines: { color: '#f0f0f0' } }, priceScale: { borderColor: '#f0f0f0' }, timeScale: { borderColor: '#f0f0f0', timeVisible: true, secondsVisible: false } }); // 添加K线系列 const candlestickSeries = chart.addCandlestickSeries({ upColor: '#52c41a', downColor: '#ff4d4f', borderUpColor: '#52c41a', borderDownColor: '#ff4d4f', wickUpColor: '#52c41a', wickDownColor: '#ff4d4f' }); // 生成K线数据 const klineData = generateKlineData(30); candlestickSeries.setData(klineData); // 添加成交量系列 const volumeSeries = chart.addHistogramSeries({ color: '#82ca9d', lineWidth: 1, priceScaleId: '', scaleMargins: { top: 0.8, bottom: 0 } }); const volumeData = klineData.map(item => ({ time: item.time, value: item.volume, color: item.close >= item.open ? '#52c41a' : '#ff4d4f' })); volumeSeries.setData(volumeData); // 缩放到合适的范围 chart.timeScale().fitContent(); chartInstance.current = chart; // 响应窗口大小变化 const handleResize = () => { if (chartInstance.current) { chartInstance.current.resize(chartRef.current.clientWidth, 400); } }; window.addEventListener('resize', handleResize); return () => { window.removeEventListener('resize', handleResize); if (chartInstance.current) { chartInstance.current.destroy(); } }; } }, [selectedFuture]); const handleBack = () => { navigate('/'); }; const getChangeColor = (changePercent) => { return changePercent >= 0 ? '#52c41a' : '#ff4d4f'; }; const getChangeIcon = (changePercent) => { return changePercent >= 0 ? : ; }; const getTrendColor = (direction) => { if (direction === '看多') return '#52c41a'; if (direction === '看空') return '#ff4d4f'; return '#faad14'; }; if (loading) { return (
); } if (!selectedFuture) { return (
); } return (
{/* 页面头部 */}

{selectedFuture.fullName}

{/* 基本信息 */} 60 ? '#52c41a' : selectedFuture.winRate > 40 ? '#faad14' : '#ff4d4f' }} /> {/* K线图表 */} K线图表
} className="detail-card" style={{ marginBottom: 24 }} >
{/* 多周期趋势分析 */} 多周期趋势分析} className="detail-card" style={{ marginBottom: 24 }} > {Object.entries(selectedFuture.trends).map(([period, trend]) => (

{period}

{trend.direction}
{trend.status}
RSI: {trend.rsi}
))}
{/* 技术指标 */}
MACD
{selectedFuture.indicators.macd}
RSI
{selectedFuture.indicators.rsi}
布林带
{selectedFuture.indicators.bollinger}
KDJ
{selectedFuture.indicators.kdj}
{/* 交易建议 */} 交易建议} className="detail-card" style={{ marginBottom: 24 }} > {/* 风险评估 */} 风险评估} className="detail-card" >
风险等级
{selectedFuture.riskLevel}
波动率
{selectedFuture.volatility}
); }; export default Detail;