"use client"; import { useState, useEffect, useRef } from "react"; import { useApp } from "@/contexts/AppContext"; import * as echarts from "echarts"; export default function PriceHistoryCard() { const { t } = useApp(); const chartRef = useRef(null); const chartInstance = useRef(null); // 模拟价格数据 const priceData = [1.01, 1.02, 1.03, 1.02, 1.04, 1.03, 1.05, 1.04, 1.03]; useEffect(() => { if (chartRef.current) { chartInstance.current = echarts.init(chartRef.current); updateChart(); } const handleResize = () => { chartInstance.current?.resize(); }; window.addEventListener("resize", handleResize); return () => { window.removeEventListener("resize", handleResize); chartInstance.current?.dispose(); }; }, []); const updateChart = () => { if (!chartInstance.current) return; const option: echarts.EChartsOption = { grid: { left: 0, right: 0, top: 10, bottom: 0, }, tooltip: { trigger: "axis", show: true, confine: true, backgroundColor: "rgba(17, 24, 39, 0.9)", borderColor: "#374151", textStyle: { color: "#f9fafb", fontSize: 12, fontWeight: 500, }, formatter: function(params: any) { const data = params[0]; return `
Day ${data.dataIndex + 1}
${data.value} USDC
`; }, }, xAxis: { type: "category", data: priceData.map((_, i) => i + 1), axisLine: { show: false, }, axisTick: { show: false, }, axisLabel: { show: false, }, }, yAxis: { type: "value", axisLine: { show: false, }, axisTick: { show: false, }, axisLabel: { show: false, }, splitLine: { show: false, }, }, series: [ { data: priceData, type: "line", smooth: true, symbol: "circle", symbolSize: 6, lineStyle: { color: "#10b981", width: 2, }, itemStyle: { color: "#10b981", }, areaStyle: { color: { type: "linear", x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: "rgba(16, 185, 129, 0.3)" }, { offset: 1, color: "rgba(16, 185, 129, 0)" }, ], }, }, }, ], }; chartInstance.current.setOption(option); }; // 计算统计数据 const highest = Math.max(...priceData).toFixed(2); const lowest = Math.min(...priceData).toFixed(2); const current = priceData[priceData.length - 1].toFixed(2); const avg = (priceData.reduce((a, b) => a + b, 0) / priceData.length).toFixed(2); return (
{/* Header */}
{t("alp.priceHistory")}
{/* Content */}
{/* Info Row */}
{t("alp.lastDays")}
{t("alp.avg")}: {avg} USDC
{/* ECharts Chart */}
{/* Stats - Vertical Layout */}
{t("alp.highest")}
{highest} USDC
{t("alp.lowest")}
{lowest} USDC
{t("alp.current")}
{current} USDC
); }