File size: 15,900 Bytes
c8a3dc1 291850b bd69e5a 291850b bd69e5a 36dd155 291850b c8a3dc1 291850b bd69e5a 291850b bd69e5a 36dd155 bd69e5a 7fcea49 36dd155 291850b 36dd155 bd69e5a 36dd155 bd69e5a 36dd155 291850b 36dd155 bd69e5a 36dd155 bd69e5a 291850b 36dd155 bd69e5a 291850b 36dd155 c8a3dc1 36dd155 291850b bd69e5a 291850b c8a3dc1 291850b 36dd155 c8a3dc1 291850b c8a3dc1 291850b c8a3dc1 291850b c8a3dc1 291850b c8a3dc1 36dd155 c8a3dc1 291850b c8a3dc1 36dd155 bd69e5a 291850b 36dd155 291850b c8a3dc1 36dd155 7fcea49 36dd155 bd69e5a 291850b 339fff1 291850b bd69e5a 291850b 36dd155 291850b 36dd155 291850b 36dd155 291850b bd69e5a 291850b 36dd155 291850b bd69e5a 291850b 36dd155 291850b 36dd155 291850b 36dd155 c8a3dc1 291850b c8a3dc1 291850b bd69e5a 291850b bd69e5a c8a3dc1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
import React, { useMemo, useState } from 'react';
import {
ComposedChart,
Bar,
Line,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
ResponsiveContainer,
ReferenceLine,
ReferenceArea,
Label,
LabelList,
Brush
} from 'recharts';
import { KLinePoint } from '../types';
interface LifeKLineChartProps {
data: KLinePoint[];
currentAge?: number;
onYearClick?: (year: number) => void;
}
// Moving Average calculation utility
function calculateMA(data: KLinePoint[], period: number): (number | null)[] {
return data.map((_, index) => {
if (index < period - 1) return null;
const slice = data.slice(index - period + 1, index + 1);
const sum = slice.reduce((acc, d) => acc + d.score, 0);
return Math.round((sum / period) * 10) / 10;
});
}
// Da Yun zone interface
interface DaYunZone {
daYun: string | undefined;
startAge: number;
endAge: number;
index: number;
}
const CustomTooltip = ({ active, payload }: any) => {
if (active && payload && payload.length) {
const data = payload[0].payload as KLinePoint;
const isUp = data.close >= data.open;
return (
<div className="bg-white/95 backdrop-blur-sm p-5 rounded-xl shadow-2xl border border-gray-200 z-50 w-[90vw] max-w-[320px] md:max-w-[400px]">
{/* Header */}
<div className="flex justify-between items-start mb-3 border-b border-gray-100 pb-2">
<div>
<p className="text-xl font-bold text-gray-800 font-serif-sc">
{data.year} {data.ganZhi}年 <span className="text-base text-gray-500 font-sans">({data.age}岁)</span>
</p>
<p className="text-sm text-indigo-600 font-medium mt-1">
大运:{data.daYun || '未知'}
</p>
</div>
<div className={`text-base font-bold px-2 py-1 rounded ${isUp ? 'bg-emerald-100 text-emerald-700' : 'bg-rose-100 text-rose-700'}`}>
{isUp ? '吉 ▲' : '凶 ▼'}
</div>
</div>
{/* Data Grid */}
<div className="grid grid-cols-4 gap-2 text-xs text-gray-500 mb-4 bg-gray-50 p-2 rounded">
<div className="text-center">
<span className="block scale-90">开盘</span>
<span className="font-mono text-gray-700 font-bold">{data.open}</span>
</div>
<div className="text-center">
<span className="block scale-90">收盘</span>
<span className="font-mono text-gray-700 font-bold">{data.close}</span>
</div>
<div className="text-center">
<span className="block scale-90">最高</span>
<span className="font-mono text-gray-700 font-bold">{data.high}</span>
</div>
<div className="text-center">
<span className="block scale-90">最低</span>
<span className="font-mono text-gray-700 font-bold">{data.low}</span>
</div>
</div>
{/* MA Info */}
<div className="flex gap-4 text-xs mb-3">
{(data as any).ma5 && (
<span className="text-indigo-500">MA5: {(data as any).ma5}</span>
)}
{(data as any).ma10 && (
<span className="text-amber-500">MA10: {(data as any).ma10}</span>
)}
</div>
{/* Detailed Reason */}
<div className="text-sm text-gray-700 leading-relaxed text-justify max-h-[200px] overflow-y-auto custom-scrollbar">
{data.reason}
</div>
</div>
);
}
return null;
};
// Gradient CandleShape with glow effects for extreme scores
const GradientCandleShape = (props: any) => {
const { x, y, width, height, payload, yAxis } = props;
const isUp = payload.close >= payload.open;
const isExtreme = payload.score > 90 || payload.score < 10;
// Use gradient fill
const fillId = isUp ? 'url(#gradientUp)' : 'url(#gradientDown)';
const strokeColor = isUp ? '#047857' : '#BE123C'; // emerald-700 / rose-700
let highY = y;
let lowY = y + height;
if (yAxis && typeof yAxis.scale === 'function') {
try {
highY = yAxis.scale(payload.high);
lowY = yAxis.scale(payload.low);
} catch (e) {
highY = y;
lowY = y + height;
}
}
const center = x + width / 2;
// Enforce minimum body height so flat doji candles are visible
const renderHeight = height < 2 ? 2 : height;
// Glow effect for extreme scores
const glowFilter = isExtreme ? 'url(#glowFilter)' : 'none';
return (
<g filter={glowFilter}>
{/* Wick - made slightly thicker for visibility */}
<line
x1={center}
y1={highY}
x2={center}
y2={lowY}
stroke={strokeColor}
strokeWidth={2}
/>
{/* Body with gradient */}
<rect
x={x}
y={y}
width={width}
height={renderHeight}
fill={fillId}
stroke={strokeColor}
strokeWidth={1}
rx={2}
ry={2}
/>
</g>
);
};
// Custom Label Component for the Peak Star
const PeakLabel = (props: any) => {
const { x, y, width, value, maxHigh } = props;
// Only render if this value equals the global max high
if (value !== maxHigh) return null;
return (
<g>
{/* Golden Star Icon */}
<path
d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"
transform={`translate(${x + width / 2 - 6}, ${y - 24}) scale(0.5)`}
fill="#F59E0B" // amber-500
stroke="#D97706" // amber-600
strokeWidth="1"
/>
{/* Score Text */}
<text
x={x + width / 2}
y={y - 28}
fill="#D97706"
fontSize={10}
fontWeight="bold"
textAnchor="middle"
>
{value}
</text>
</g>
);
};
// Trough Label Component
const TroughLabel = (props: any) => {
const { x, y, width, height, value, minLow } = props;
if (value !== minLow) return null;
return (
<g>
{/* Down Arrow */}
<path
d="M12 22l-6-6h4V8h4v8h4z"
transform={`translate(${x + width / 2 - 6}, ${y + height + 4}) scale(0.5)`}
fill="#6B7280" // gray-500
stroke="#4B5563" // gray-600
strokeWidth="1"
/>
<text
x={x + width / 2}
y={y + height + 20}
fill="#4B5563"
fontSize={10}
fontWeight="bold"
textAnchor="middle"
>
{value}
</text>
</g>
);
};
// Crosshair Cursor Component
const CrosshairCursor = (props: any) => {
const { points, width, height, top, left } = props;
if (!points || !points[0]) return null;
const { x, y } = points[0];
return (
<g>
{/* Vertical line */}
<line
x1={x}
y1={top || 0}
x2={x}
y2={(top || 0) + (height || 400)}
stroke="#9CA3AF"
strokeWidth={1}
strokeDasharray="4 4"
/>
{/* Horizontal line */}
<line
x1={left || 0}
y1={y}
x2={(left || 0) + (width || 800)}
y2={y}
stroke="#9CA3AF"
strokeWidth={1}
strokeDasharray="4 4"
/>
</g>
);
};
const LifeKLineChart: React.FC<LifeKLineChartProps> = ({ data, currentAge, onYearClick }) => {
const [brushDomain, setBrushDomain] = useState<[number, number] | null>(null);
// Calculate Da Yun background zones
const daYunZones = useMemo<DaYunZone[]>(() => {
if (!data || data.length === 0) return [];
const zones: DaYunZone[] = [];
let currentDaYun = data[0]?.daYun;
let startAge = data[0]?.age;
for (let i = 1; i <= data.length; i++) {
if (i === data.length || data[i]?.daYun !== currentDaYun) {
zones.push({
daYun: currentDaYun,
startAge,
endAge: data[i-1]?.age,
index: zones.length
});
if (i < data.length) {
currentDaYun = data[i]?.daYun;
startAge = data[i]?.age;
}
}
}
return zones;
}, [data]);
// Calculate MAs and transform data
const transformedData = useMemo(() => {
const ma5 = calculateMA(data, 5);
const ma10 = calculateMA(data, 10);
return data.map((d, i) => ({
...d,
bodyRange: [Math.min(d.open, d.close), Math.max(d.open, d.close)],
labelPoint: d.high,
ma5: ma5[i],
ma10: ma10[i],
}));
}, [data]);
// Identify Da Yun change points for reference lines
const daYunChanges = useMemo(() => {
return data.filter((d, i) => {
if (i === 0) return true;
return d.daYun !== data[i-1].daYun;
});
}, [data]);
// Calculate Global Max High and Min Low
const maxHigh = useMemo(() =>
data.length > 0 ? Math.max(...data.map(d => d.high)) : 100,
[data]
);
const minLow = useMemo(() =>
data.length > 0 ? Math.min(...data.map(d => d.low)) : 0,
[data]
);
// Calculate default brush range based on current age
const defaultBrushIndex = useMemo(() => {
if (!currentAge || !data.length) return { start: 0, end: Math.min(30, data.length - 1) };
const currentIndex = data.findIndex(d => d.age === currentAge);
if (currentIndex === -1) return { start: 0, end: Math.min(30, data.length - 1) };
const start = Math.max(0, currentIndex - 15);
const end = Math.min(data.length - 1, currentIndex + 15);
return { start, end };
}, [currentAge, data]);
if (!data || data.length === 0) {
return <div className="h-[350px] sm:h-[450px] lg:h-[500px] flex items-center justify-center text-gray-400">无数据</div>;
}
return (
<div className="w-full h-[400px] sm:h-[500px] lg:h-[650px] bg-white p-2 md:p-6 rounded-xl border border-gray-200 shadow-sm relative">
{/* Header with Legend */}
<div className="mb-4 flex flex-wrap justify-between items-center px-2 gap-2">
<h3 className="text-xl font-bold text-dark-slate font-serif-sc">流年大运轨迹图</h3>
<div className="flex flex-wrap gap-2 md:gap-4 text-xs font-medium">
<span className="flex items-center text-emerald-700 bg-emerald-50 px-2 py-1 rounded">
<div className="w-2 h-2 bg-emerald-500 mr-2 rounded-full"></div> 吉运 (涨)
</span>
<span className="flex items-center text-rose-700 bg-rose-50 px-2 py-1 rounded">
<div className="w-2 h-2 bg-rose-500 mr-2 rounded-full"></div> 凶运 (跌)
</span>
<span className="flex items-center text-indigo-600 bg-indigo-50 px-2 py-1 rounded">
<div className="w-3 h-0.5 bg-indigo-400 mr-2"></div> MA5
</span>
<span className="flex items-center text-amber-600 bg-amber-50 px-2 py-1 rounded">
<div className="w-3 h-0.5 bg-amber-400 mr-2"></div> MA10
</span>
</div>
</div>
<ResponsiveContainer width="100%" height="85%">
<ComposedChart
data={transformedData}
margin={{ top: 30, right: 10, left: 0, bottom: 50 }}
onClick={(e) => {
if (e?.activePayload?.[0]?.payload && onYearClick) {
onYearClick(e.activePayload[0].payload.year);
}
}}
>
{/* SVG Definitions for Gradients and Filters */}
<defs>
{/* Gradient for bullish (up) candles - emerald */}
<linearGradient id="gradientUp" x1="0" y1="1" x2="0" y2="0">
<stop offset="0%" stopColor="#047857" stopOpacity={1} />
<stop offset="100%" stopColor="#10B981" stopOpacity={1} />
</linearGradient>
{/* Gradient for bearish (down) candles - rose */}
<linearGradient id="gradientDown" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="#BE123C" stopOpacity={1} />
<stop offset="100%" stopColor="#F43F5E" stopOpacity={1} />
</linearGradient>
{/* Glow filter for extreme scores */}
<filter id="glowFilter" x="-50%" y="-50%" width="200%" height="200%">
<feGaussianBlur stdDeviation="3" result="coloredBlur"/>
<feMerge>
<feMergeNode in="coloredBlur"/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
</defs>
{/* Da Yun Background Zones - render first so they're behind everything */}
{daYunZones.map((zone, idx) => (
<ReferenceArea
key={`zone-${idx}`}
x1={zone.startAge}
x2={zone.endAge}
fill={idx % 2 === 0 ? '#f0f9ff' : '#faf5ff'}
fillOpacity={0.6}
stroke="none"
/>
))}
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#f3f4f6" />
<XAxis
dataKey="age"
tick={{fontSize: 10, fill: '#6b7280'}}
interval={9}
axisLine={{ stroke: '#e5e7eb' }}
tickLine={false}
label={{ value: '年龄', position: 'insideBottomRight', offset: -5, fontSize: 10, fill: '#9ca3af' }}
/>
<YAxis
domain={[0, 'auto']}
tick={{fontSize: 10, fill: '#6b7280'}}
axisLine={false}
tickLine={false}
label={{ value: '运势分', angle: -90, position: 'insideLeft', fontSize: 10, fill: '#9ca3af' }}
/>
<Tooltip
content={<CustomTooltip />}
cursor={<CrosshairCursor />}
/>
{/* Da Yun Reference Lines with Labels */}
{daYunChanges.map((point, index) => (
<ReferenceLine
key={`dayun-${index}`}
x={point.age}
stroke="#cbd5e1"
strokeDasharray="3 3"
strokeWidth={1}
>
<Label
value={point.daYun}
position="top"
fill="#6366f1"
fontSize={10}
fontWeight="bold"
/>
</ReferenceLine>
))}
{/* Current Age Marker */}
{currentAge && (
<ReferenceLine
x={currentAge}
stroke="#F59E0B"
strokeWidth={2}
strokeDasharray="none"
>
<Label
value="今"
position="top"
fill="#F59E0B"
fontSize={12}
fontWeight="bold"
/>
</ReferenceLine>
)}
{/* MA10 Line (behind MA5) */}
<Line
type="monotone"
dataKey="ma10"
stroke="#FBBF24"
dot={false}
strokeWidth={1.5}
name="MA10"
connectNulls
/>
{/* MA5 Line */}
<Line
type="monotone"
dataKey="ma5"
stroke="#818CF8"
dot={false}
strokeWidth={2}
name="MA5"
connectNulls
/>
{/* K-Line Candles with Gradient */}
<Bar
dataKey="bodyRange"
shape={<GradientCandleShape />}
isAnimationActive={true}
animationDuration={1500}
>
{/* Peak Label */}
<LabelList
dataKey="high"
position="top"
content={<PeakLabel maxHigh={maxHigh} />}
/>
</Bar>
{/* Brush for zoom/pan */}
<Brush
dataKey="age"
height={30}
stroke="#6366f1"
fill="#f5f3ff"
startIndex={defaultBrushIndex.start}
endIndex={defaultBrushIndex.end}
travellerWidth={10}
/>
</ComposedChart>
</ResponsiveContainer>
{/* Instruction hint */}
<p className="text-center text-xs text-gray-400 mt-1">
拖动底部滑块可缩放查看 • 点击K线可查看月度详情
</p>
</div>
);
};
export default LifeKLineChart;
|