import React, { useEffect, useRef, useState } from 'react'; import { createChart, ColorType, IChartApi, ISeriesApi, CandlestickSeries } from 'lightweight-charts'; import { Activity, RefreshCw } from 'lucide-react'; interface TradingViewChartProps { marketAddress: string; outcomeIndex: number; outcomeLabel: string; currentPrice: number; } export const TradingViewChart: React.FC = ({ marketAddress, outcomeIndex, outcomeLabel, currentPrice }) => { const chartContainerRef = useRef(null); const chartRef = useRef(null); const seriesRef = useRef | null>(null); const [timeframe, setTimeframe] = useState('5s'); useEffect(() => { if (!chartContainerRef.current) return; // 1. 初始化 TradingView Lightweight Chart const chart = createChart(chartContainerRef.current, { layout: { background: { type: ColorType.Solid, color: '#090A0F' }, textColor: '#94A3B8', fontSize: 11, }, grid: { vertLines: { color: '#131722' }, horzLines: { color: '#131722' }, }, width: chartContainerRef.current.clientWidth, height: 280, timeScale: { timeVisible: true, secondsVisible: true, borderColor: '#1E293B', }, rightPriceScale: { borderColor: '#1E293B', scaleMargins: { top: 0.1, bottom: 0.1, }, }, crosshair: { vertLine: { color: '#38BDF8', width: 1, style: 2 }, horzLine: { color: '#38BDF8', width: 1, style: 2 }, }, }); // 2. 添加专业蜡烛图 (Candlestick Series - GMGN / Pump.fun 风格) const candlestickSeries = chart.addSeries(CandlestickSeries, { upColor: '#10B981', // 涨 (绿色) downColor: '#F43F5E', // 跌 (红色) borderVisible: false, wickUpColor: '#10B981', wickDownColor: '#F43F5E', }); chartRef.current = chart; seriesRef.current = candlestickSeries; // 3. 生成初始秒级 K 线柱子 const now = Math.floor(Date.now() / 1000); const initialData = []; let p = currentPrice > 0 ? currentPrice : 0.50; for (let i = 80; i >= 0; i--) { const t = (now - i * 5) as any; const noise = (Math.sin(i * 0.3) * 0.02) + ((Math.random() - 0.5) * 0.015); const open = Math.max(0.01, Math.min(0.99, p + noise)); const close = Math.max(0.01, Math.min(0.99, open + ((Math.random() - 0.48) * 0.02))); const high = Math.max(open, close) + Math.random() * 0.01; const low = Math.min(open, close) - Math.random() * 0.01; initialData.push({ time: t, open, high, low, close }); p = close; } candlestickSeries.setData(initialData); chart.timeScale().fitContent(); // 4. 自适应窗口大小 const handleResize = () => { if (chartContainerRef.current && chartRef.current) { chartRef.current.applyOptions({ width: chartContainerRef.current.clientWidth }); } }; window.addEventListener('resize', handleResize); return () => { window.removeEventListener('resize', handleResize); chart.remove(); }; }, [marketAddress, outcomeIndex]); // 5. 实时价格脉冲更新 useEffect(() => { if (seriesRef.current && currentPrice > 0) { const now = Math.floor(Date.now() / 1000) as any; seriesRef.current.update({ time: now, open: currentPrice, high: currentPrice * 1.01, low: currentPrice * 0.99, close: currentPrice, }); } }, [currentPrice]); return (
{outcomeLabel} 实时 K 线行情 ${currentPrice.toFixed(3)}
TradingView 专业高频连续竞价图表
{/* Timeframes */}
{['1s', '5s', '1m', '15m', '1h'].map((tf) => ( ))}
{/* TradingView Chart Container */}
); };