"use client"; import { useState, useEffect, useRef } from "react"; import { useApp } from "@/contexts/AppContext"; import * as echarts from "echarts"; export default function APYHistoryCard() { const { t } = useApp(); const [activeTab, setActiveTab] = useState<"apy" | "price">("apy"); const chartRef = useRef(null); const chartInstance = useRef(null); // 生成从#FBEADE到#C65122的9个渐变颜色 const getColorGradient = () => { const colors = [ "#FBEADE", "#F5D4BE", "#EFBF9E", "#E9AA7E", "#E3955E", "#DD804E", "#D76B3E", "#D1562E", "#C65122", ]; return colors; }; const chartData = [4.2, 5.1, 5.8, 6.3, 7.1, 8.2, 9.5, 10.8, 12.4]; 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(); }; }, [activeTab]); const updateChart = () => { if (!chartInstance.current) return; const colors = getColorGradient(); // 模拟价格数据 const priceData = [1.12, 1.15, 1.18, 1.14, 1.21, 1.19, 1.25, 1.28, 1.32]; 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]; const suffix = activeTab === "apy" ? "%" : " USDC"; return `
Day ${data.dataIndex + 1}
${data.value}${suffix}
`; }, }, xAxis: { type: "category", data: chartData.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: [ activeTab === "apy" ? { data: chartData.map((value, index) => ({ value, itemStyle: { color: colors[index], borderRadius: [2, 2, 0, 0], }, })), type: "bar", barWidth: "50%", } : { 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); }; return (
{/* Tabs */}
{/* Chart Area */}
{t("apy.lastDays")}
{/* ECharts Chart */}
{/* Stats */}
{t("apy.highest")} {activeTab === "apy" ? "24.8%" : "$1.32"}
{t("apy.lowest")} {activeTab === "apy" ? "18.2%" : "$1.12"}
); }