import React, { useState, useMemo } from 'react'; import { ChevronDown, ChevronUp, Download, FileSpreadsheet, TrendingUp, TrendingDown } from 'lucide-react'; import { KLinePoint } from '../types'; interface KLineTextTableProps { data: KLinePoint[]; birthYear?: number; className?: string; } const KLineTextTable: React.FC = ({ data, birthYear, className = '', }) => { const [expanded, setExpanded] = useState(false); const currentYear = new Date().getFullYear(); // Calculate display range - default show 10 years around current age const displayData = useMemo(() => { if (!data || data.length === 0) return []; if (expanded) { return data; } // Find current year index const currentYearIndex = data.findIndex(d => d.year === currentYear); if (currentYearIndex === -1) { // If current year not found, show first 10 items return data.slice(0, 10); } // Show 5 years before and 5 years after current year const startIndex = Math.max(0, currentYearIndex - 5); const endIndex = Math.min(data.length, currentYearIndex + 5); return data.slice(startIndex, endIndex); }, [data, expanded, currentYear]); // Export to Excel (CSV format) const handleExportExcel = () => { if (!data || data.length === 0) return; // Create CSV content with BOM for Excel compatibility const BOM = '\uFEFF'; const headers = ['年龄', '年份', '干支', '大运', '评分', '趋势', '运势分析']; const rows = data.map(item => { const trend = item.close >= item.open ? '上涨' : '下跌'; // Escape quotes and handle line breaks in reason const reason = (item.reason || '').replace(/"/g, '""').replace(/\n/g, ' '); return [ `${item.age}岁`, item.year, item.ganZhi || '', item.daYun || '', item.score, trend, `"${reason}"`, ].join(','); }); const csvContent = BOM + headers.join(',') + '\n' + rows.join('\n'); const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = `比迹_流年详批_${new Date().getTime()}.csv`; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); }; if (!data || data.length === 0) { return null; } return (
{/* Header */}

流年运势详批

({expanded ? `全部 ${data.length} 年` : `近10年`})
{/* Table */}
{displayData.map((item, index) => { const isCurrentYear = item.year === currentYear; const isUpTrend = item.close >= item.open; const scoreColor = isUpTrend ? 'text-green-600' : 'text-red-600'; const scoreBg = isUpTrend ? 'bg-green-50' : 'bg-red-50'; return ( {/* Age */} {/* Year + GanZhi */} {/* DaYun */} {/* Score */} {/* Reason */} ); })}
年龄 年份/干支 大运 评分 运势分析
{item.age}
{item.year} {item.ganZhi || '-'}
{item.daYun || '-'}
{item.score} {isUpTrend ? ( ) : ( )}

{item.reason || '暂无详细分析'}

{/* Expand/Collapse Button */}
); }; export default KLineTextTable;