新增模块
This commit is contained in:
@@ -0,0 +1,360 @@
|
||||
import { useMemo } from 'react';
|
||||
import { ArrowLeft, ArrowRight, X } from 'lucide-react';
|
||||
import { useTheme } from '../../context/ThemeContext';
|
||||
import { useText } from '../../i18n/useText';
|
||||
import type { DigitalStaff, MetricLink, MetricNode } from '../../data/sqlite';
|
||||
import { alertColor, descendantsOf, execColor, fmt, fmtSigned } from '../../lib/metric';
|
||||
import { achieveAt, gapAt, levelAt, momAt, periodLabel, seriesOf, valueAt } from '../../lib/time';
|
||||
import { DEEP } from './deepTheme';
|
||||
|
||||
/**
|
||||
* 因果看板:图谱选中实体的「右栏一览」。
|
||||
* 把散在浮层卡里的信息升级为纵向叙事——游标读数 → 12 期趋势 → 前因 → 后果 → 下级回应 → AI 研判 → 汇报口径,
|
||||
* 全部数值随底部时间滑块重算,拖动即可看同一指标在不同时点的因果表现。
|
||||
*/
|
||||
interface Props {
|
||||
code: string | null;
|
||||
byCode: Map<string, MetricNode>;
|
||||
flat: MetricNode[];
|
||||
links: MetricLink[];
|
||||
staff: DigitalStaff[];
|
||||
period: number;
|
||||
/** 舞台态跟随深空主题,面板态跟随用户主题 */
|
||||
deep: boolean;
|
||||
onSelect: (code: string) => void;
|
||||
onClose?: () => void;
|
||||
}
|
||||
|
||||
export default function CausalPanel({ code, byCode, flat, links, staff, period, deep, onSelect, onClose }: Props) {
|
||||
const { tokens } = useTheme();
|
||||
const { t } = useText();
|
||||
const tk = deep ? { ...tokens, ...DEEP } : tokens;
|
||||
|
||||
const node = code ? byCode.get(code) : undefined;
|
||||
|
||||
/* 子树集合:维度/总览看辖域对外语义关联;L0 无从切分 → 回退全图最强因果链 */
|
||||
const isRoot = node?.level === 0;
|
||||
const set = useMemo(() => {
|
||||
if (!node) return null;
|
||||
const s = new Set<string>([node.code]);
|
||||
if (node.level <= 1) descendantsOf(node.code, flat).forEach(d => s.add(d.code));
|
||||
return s;
|
||||
}, [node, flat]);
|
||||
|
||||
const { incoming, outgoing } = useMemo(() => {
|
||||
if (!set) return { incoming: [], outgoing: [] as MetricLink[] };
|
||||
if (isRoot) {
|
||||
return { incoming: [] as MetricLink[], outgoing: [...links].sort((a, b) => Math.abs(b.strength) - Math.abs(a.strength)).slice(0, 6) };
|
||||
}
|
||||
return {
|
||||
incoming: links.filter(l => set.has(l.to_code) && !set.has(l.from_code)),
|
||||
outgoing: links.filter(l => set.has(l.from_code) && !set.has(l.to_code)),
|
||||
};
|
||||
}, [set, links, isRoot]);
|
||||
|
||||
const kids = useMemo(
|
||||
() => (node ? flat.filter(n => n.parent_code === node.code) : []),
|
||||
[node, flat],
|
||||
);
|
||||
|
||||
/* 同层对标:末级没有“下级回应”可看,就拿同父兄弟作参照,看板不至于空一块 */
|
||||
const sibs = useMemo(
|
||||
() => (node && node.parent_code && kids.length === 0)
|
||||
? flat.filter(n => n.parent_code === node.parent_code && n.code !== node.code)
|
||||
: [],
|
||||
[node, flat, kids.length],
|
||||
);
|
||||
|
||||
if (!node) {
|
||||
return (
|
||||
<div style={{
|
||||
display: 'flex', alignItems: 'center', justifyContent: 'center', flex: '1 1 auto', minWidth: 0, height: '100%',
|
||||
padding: 18, textAlign: 'center', fontSize: 12.5, lineHeight: 1.8, color: tk.textMuted,
|
||||
background: tk.panelBgAlt, border: `1px solid ${tk.border}`, borderRadius: 12,
|
||||
}}>
|
||||
点击图谱中的任意节点,这里将展开它的前因、后果与下级回应。
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const name = node.name_zh;
|
||||
const lv = levelAt(node, period);
|
||||
const c = alertColor(lv, tk);
|
||||
const val = valueAt(node, period);
|
||||
const gap = gapAt(node, period);
|
||||
const ach = achieveAt(node, period);
|
||||
const mom = momAt(node, period);
|
||||
const gapU = node.unit === '%' ? 'pt' : (node.unit);
|
||||
const valU = node.unit;
|
||||
/** 子指标偏差单位:% 类走 pt,其余按语言取单位(不在中文句里漏出日文汉字) */
|
||||
const kidGapU = (n: MetricNode) => (n.unit === '%' ? 'pt' : (n.unit));
|
||||
const series = seriesOf(node);
|
||||
const maxStrength = links.reduce((m, l) => Math.max(m, Math.abs(l.strength)), 0.001);
|
||||
const nameOf = (cd: string) => {
|
||||
const n = byCode.get(cd);
|
||||
return !n ? cd : n.name_zh;
|
||||
};
|
||||
|
||||
/* ---- AI 研判:游标读数 + 近 4 期斜率 + 最弱子项 / 最强前因 ---- */
|
||||
const win = series.slice(Math.max(0, period - 3), period + 1);
|
||||
const slope = win.length >= 2 ? +((win[win.length - 1] - win[0]) / Math.max(1, win.length - 1)).toFixed(2) : 0;
|
||||
const improving = node.polarity === 'down' ? slope < 0 : slope > 0;
|
||||
const weakening = node.polarity === 'down' ? slope > 0 : slope < 0;
|
||||
const kidGaps = kids.map(k => ({ k, g: gapAt(k, period) })).sort((a, b) => a.g - b.g);
|
||||
const worstKid = kidGaps.find(x => x.g < 0)?.k;
|
||||
const topCause = incoming.length
|
||||
? [...incoming].sort((a, b) => Math.abs(b.strength) - Math.abs(a.strength))[0]
|
||||
: null;
|
||||
const verdicts: { text: string; tone: string }[] = [
|
||||
{
|
||||
text: `${periodLabel(period)} 当期:${name} ${fmt(val)}${valU},较目标 ${fmtSigned(gap)}${gapU}(达成 ${fmt(ach, 0)}%)。`,
|
||||
tone: lv === 'red' ? tk.danger : lv === 'yellow' ? tk.warning : tk.success,
|
||||
},
|
||||
{
|
||||
text: mom === null
|
||||
? ('首期无上期,暂无环比。')
|
||||
: `环比上期 ${mom > 0 ? '+' : ''}${fmt(mom)}${valU},近 4 期呈${improving ? '改善' : weakening ? '走弱' : '平稳'}走势。`,
|
||||
tone: improving ? tk.success : weakening ? tk.danger : tk.textSub,
|
||||
},
|
||||
worstKid
|
||||
? {
|
||||
text: `下级「${nameOf(worstKid.code)}」偏差 ${fmtSigned(gapAt(worstKid, period))}${kidGapU(worstKid)},是回升支点。`,
|
||||
tone: tk.warning,
|
||||
}
|
||||
: topCause
|
||||
? {
|
||||
text: `最大驱动源:${nameOf(topCause.from_code)}(强度 ${fmt(topCause.strength, 2)}),稳住传播路径即可。`,
|
||||
tone: tk.info,
|
||||
}
|
||||
: {
|
||||
text: '已是末级驱动因子,无更深层拆解,关注口径稳定性。',
|
||||
tone: tk.textSub,
|
||||
},
|
||||
];
|
||||
|
||||
const secTitle = (label: string, n?: number) => (
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 6, marginBottom: 6 }}>
|
||||
<span style={{ width: 3, height: 12, borderRadius: 2, background: tk.accent, flexShrink: 0 }} />
|
||||
<span style={{ fontSize: 11.5, fontWeight: 800, color: tk.text, letterSpacing: '0.03em', flexShrink: 0 }}>{label}</span>
|
||||
{n !== undefined && <span style={{ fontSize: 10.5, color: tk.textMuted, fontVariantNumeric: 'tabular-nums' }}>{n}</span>}
|
||||
<span style={{ flex: 1, height: 1, background: tk.border }} />
|
||||
</div>
|
||||
);
|
||||
|
||||
const relColor = (r: string) => (r === 'drive' ? tk.success : r === 'conflict' ? tk.danger : tk.info);
|
||||
|
||||
/* 因果行:方向 + 关系徽章 + 实体 + 游标值 + 强度条 */
|
||||
const linkRow = (l: MetricLink, out: boolean) => {
|
||||
const other = out ? l.to_code : l.from_code;
|
||||
const on = byCode.get(other);
|
||||
const rc = relColor(l.relation);
|
||||
const oc = on ? alertColor(levelAt(on, period), tk) : tk.textMuted;
|
||||
return (
|
||||
<button key={`${l.id}-${out}`} onClick={() => onSelect(other)} className="ghost-btn" style={{
|
||||
display: 'flex', alignItems: 'center', gap: 6, width: '100%', textAlign: 'left', cursor: 'pointer',
|
||||
padding: '5px 7px', borderRadius: 8, background: tk.cardBg, border: `1px solid ${tk.border}`,
|
||||
}}>
|
||||
{out ? <ArrowRight size={11} color={rc} style={{ flexShrink: 0 }} /> : <ArrowLeft size={11} color={rc} style={{ flexShrink: 0 }} />}
|
||||
<span style={{ fontSize: 9.5, color: rc, background: `${rc}1a`, border: `1px solid ${rc}33`, borderRadius: 5, padding: '0 4px', flexShrink: 0 }}>{t.net[l.relation]}</span>
|
||||
<span style={{ fontSize: 12, color: tk.text, minWidth: 0, flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{nameOf(other)}</span>
|
||||
{on && <span style={{ fontSize: 11, color: oc, fontVariantNumeric: 'tabular-nums', fontFamily: 'ui-monospace, monospace', flexShrink: 0 }}>{fmt(valueAt(on, period))}</span>}
|
||||
<span style={{ width: 30, height: 3.5, borderRadius: 99, background: tk.border, overflow: 'hidden', flexShrink: 0 }}>
|
||||
<span style={{ display: 'block', width: `${Math.min(100, (Math.abs(l.strength) / maxStrength) * 100)}%`, height: '100%', background: rc }} />
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
};
|
||||
|
||||
/* 下级回应条:子指标在该期的达成率,一眼看出谁在拖、谁在扛 */
|
||||
const kidRow = (k: MetricNode) => {
|
||||
const ka = achieveAt(k, period);
|
||||
const kc = alertColor(levelAt(k, period), tk);
|
||||
const kg = gapAt(k, period);
|
||||
return (
|
||||
<button key={k.code} onClick={() => onSelect(k.code)} className="ghost-btn" style={{
|
||||
display: 'block', width: '100%', textAlign: 'left', cursor: 'pointer', padding: '5px 7px',
|
||||
borderRadius: 8, background: tk.cardBg, border: `1px solid ${tk.border}`,
|
||||
}}>
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
|
||||
<span style={{ width: 6, height: 6, borderRadius: '50%', background: kc, boxShadow: `0 0 5px ${kc}`, flexShrink: 0 }} />
|
||||
<span style={{ fontSize: 12, color: tk.text, minWidth: 0, flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{k.name_zh}</span>
|
||||
<span style={{ fontSize: 11, fontWeight: 800, color: kc, fontVariantNumeric: 'tabular-nums', fontFamily: 'ui-monospace, monospace', flexShrink: 0 }}>{fmt(ka, 0)}%</span>
|
||||
</div>
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 6, marginTop: 4 }}>
|
||||
<span style={{ flex: 1, height: 3, borderRadius: 99, background: tk.border, overflow: 'hidden' }}>
|
||||
<span style={{ display: 'block', width: `${Math.min(100, ka)}%`, height: '100%', background: kc, transition: 'width 0.4s ease' }} />
|
||||
</span>
|
||||
<span style={{ fontSize: 10, color: kg >= 0 ? tk.success : tk.danger, fontVariantNumeric: 'tabular-nums', flexShrink: 0 }}>
|
||||
{fmtSigned(kg)}{kidGapU(k)}
|
||||
</span>
|
||||
</div>
|
||||
</button>
|
||||
);
|
||||
};
|
||||
|
||||
/* 12 期趋势柱:游标柱高亮发光,目标线横贯 */
|
||||
const trendBars = () => {
|
||||
const mn = Math.min(...series, node.target);
|
||||
const mx = Math.max(...series, node.target);
|
||||
const span = mx - mn || 1;
|
||||
const H = 56;
|
||||
const ty = H - 4 - ((node.target - mn) / span) * (H - 10);
|
||||
return (
|
||||
<div style={{ position: 'relative', height: H, display: 'flex', alignItems: 'flex-end', gap: 3 }}>
|
||||
{series.map((v, i) => {
|
||||
const h = 6 + ((v - mn) / span) * (H - 12);
|
||||
const on = i === period;
|
||||
const col = alertColor(levelAt(node, i), tk);
|
||||
return (
|
||||
<div key={i} style={{
|
||||
flex: 1, height: h, borderRadius: 2.5, background: col,
|
||||
opacity: on ? 1 : i < period ? 0.4 : 0.16,
|
||||
boxShadow: on ? `0 0 9px ${col}99` : 'none',
|
||||
transition: 'opacity 0.25s ease, box-shadow 0.25s ease',
|
||||
}} />
|
||||
);
|
||||
})}
|
||||
<div style={{
|
||||
position: 'absolute', left: 0, right: 0, top: ty, borderTop: `1px dashed ${tk.textMuted}`, opacity: 0.65,
|
||||
}} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const guards = staff.filter(s => s.bound_metric === node.code);
|
||||
|
||||
return (
|
||||
<div className="space-scroll" style={{
|
||||
display: 'flex', flexDirection: 'column', gap: 12, flex: '1 1 auto', minWidth: 0, height: '100%', overflowY: 'auto',
|
||||
padding: '12px 13px', borderRadius: 12, background: tk.panelBgAlt,
|
||||
border: `1px solid ${tk.borderAccent}`, backdropFilter: 'blur(9px) saturate(1.15)',
|
||||
boxShadow: deep ? '0 10px 30px rgba(0,0,0,0.35)' : 'none',
|
||||
}}>
|
||||
{/* 实体抬头 */}
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 7 }}>
|
||||
<span className={lv === 'red' ? 'net-breathe' : undefined} style={{
|
||||
display: 'block', width: 10, height: 10, borderRadius: '50%', flexShrink: 0, background: c,
|
||||
boxShadow: `0 0 9px ${c}`,
|
||||
}} />
|
||||
<span style={{ fontSize: 15, fontWeight: 800, color: tk.text, minWidth: 0, flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{name}</span>
|
||||
<span style={{ fontSize: 10, color: tk.textMuted, border: `1px solid ${tk.border}`, borderRadius: 5, padding: '0 4px', fontFamily: 'ui-monospace, monospace', flexShrink: 0 }}>{node.code}</span>
|
||||
{onClose && (
|
||||
<button onClick={onClose} className="ghost-btn" style={{
|
||||
display: 'grid', placeItems: 'center', width: 20, height: 20, borderRadius: 5, cursor: 'pointer', flexShrink: 0,
|
||||
color: tk.textMuted, border: `1px solid ${tk.border}`, background: 'transparent',
|
||||
}}><X size={12} /></button>
|
||||
)}
|
||||
</div>
|
||||
<div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', fontSize: 10.5, color: tk.textMuted, marginTop: -8 }}>
|
||||
<span>L{node.level}</span><span>·</span><span>{node.owner}</span><span>·</span>
|
||||
<span>{node.frequency}</span><span>·</span>
|
||||
<span>{node.parent_code && node.parent_code !== 'OV' ? `上级 ${nameOf(node.parent_code)}` : 'OV'}</span>
|
||||
</div>
|
||||
|
||||
{/* 看板现已贯通到底(整屏高),纵向叙事优先填满高度;只有超宽屏才自动分双列 */}
|
||||
<div style={{
|
||||
display: 'grid', gap: 12, alignItems: 'start',
|
||||
gridTemplateColumns: 'repeat(auto-fit, minmax(280px, 1fr))',
|
||||
}}>
|
||||
|
||||
{/* 游标读数 */}
|
||||
<div>
|
||||
{secTitle(`${t.net.causalCursor} · ${periodLabel(period)}`)}
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 6 }}>
|
||||
{([
|
||||
[t.detail.current, `${fmt(val)}${valU}`, tk.text],
|
||||
[t.detail.targetVal, `${fmt(node.target)}${valU}`, tk.textSub],
|
||||
[t.detail.gap, `${fmtSigned(gap)}${gapU}`, gap >= 0 ? tk.success : tk.danger],
|
||||
[t.detail.achieve, `${fmt(ach, 0)}%`, execColor(ach, tk)],
|
||||
] as const).map(([lab, v, col]) => (
|
||||
<div key={lab} style={{ background: tk.cardBg, border: `1px solid ${tk.border}`, borderRadius: 8, padding: '5px 8px' }}>
|
||||
<div style={{ fontSize: 10, color: tk.textMuted }}>{lab}</div>
|
||||
<div style={{ fontSize: 15.5, fontWeight: 800, color: col, fontVariantNumeric: 'tabular-nums', fontFamily: 'ui-monospace, monospace', lineHeight: 1.3 }}>{v}</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 趋势柱 */}
|
||||
<div>
|
||||
{secTitle(t.net.causalTrend)}
|
||||
{trendBars()}
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', fontSize: 9.5, color: tk.textMuted, marginTop: 3 }}>
|
||||
<span>2025-01</span><span>虚线=目标</span><span>2025-12</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 前因 */}
|
||||
<div>
|
||||
{secTitle(t.net.causalIn, incoming.length)}
|
||||
{incoming.length
|
||||
? <div style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>{incoming.map(l => linkRow(l, false))}</div>
|
||||
: <div style={{ fontSize: 11.5, color: tk.textMuted }}>{isRoot ? t.net.noConn : t.net.causalNoIn}</div>}
|
||||
</div>
|
||||
|
||||
{/* 后果 */}
|
||||
<div>
|
||||
{secTitle(isRoot ? t.net.topChains : t.net.causalOut, outgoing.length)}
|
||||
{outgoing.length
|
||||
? <div style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>{outgoing.map(l => linkRow(l, true))}</div>
|
||||
: <div style={{ fontSize: 11.5, color: tk.textMuted }}>{t.net.causalNoOut}</div>}
|
||||
</div>
|
||||
|
||||
{/* 下级回应 */}
|
||||
<div>
|
||||
{secTitle(t.net.causalKids, kids.length)}
|
||||
{kids.length
|
||||
? <div style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>{[...kidGaps].map(x => kidRow(x.k))}</div>
|
||||
: <div style={{ fontSize: 11.5, color: tk.textMuted }}>{t.net.causalNoKid}</div>}
|
||||
</div>
|
||||
|
||||
{/* 同层对标(仅末级:没有下级可拆时,用兄弟指标补齐这块高度) */}
|
||||
{!kids.length && sibs.length > 0 && (
|
||||
<div>
|
||||
{secTitle(t.net.causalSibs, sibs.length)}
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
|
||||
{[...sibs].sort((a, b) => gapAt(a, period) - gapAt(b, period)).slice(0, 6).map(k => kidRow(k))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* AI 研判:整行占位,长句不拆栏 */}
|
||||
<div style={{ gridColumn: '1 / -1' }}>
|
||||
{secTitle(t.net.causalAi)}
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 5 }}>
|
||||
{verdicts.map(v => (
|
||||
<div key={v.text} style={{
|
||||
display: 'flex', gap: 6, fontSize: 11.5, lineHeight: 1.65, color: tk.textSub,
|
||||
background: tk.cardBg, border: `1px solid ${tk.border}`, borderLeft: `2px solid ${v.tone}`,
|
||||
borderRadius: 8, padding: '5px 8px',
|
||||
}}>{v.text}</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 汇报口径 */}
|
||||
<div style={{ gridColumn: '1 / -1' }}>
|
||||
{secTitle(t.net.causalReport)}
|
||||
<div style={{ background: tk.cardBg, border: `1px solid ${tk.border}`, borderRadius: 8, padding: '6px 8px' }}>
|
||||
<div style={{ fontSize: 10, color: tk.textMuted }}>{t.common.formula}</div>
|
||||
<div style={{ fontSize: 11.5, color: tk.textSub, marginBottom: 5 }}>{node.formula}</div>
|
||||
<div style={{ fontSize: 10, color: tk.textMuted }}>{t.detail.desc}</div>
|
||||
<div style={{ fontSize: 11.5, color: tk.textSub, lineHeight: 1.6 }}>{(node.desc_zh) || t.detail.noDesc}</div>
|
||||
</div>
|
||||
<div style={{ marginTop: 5, display: 'flex', flexDirection: 'column', gap: 4 }}>
|
||||
{guards.length ? guards.map(s => (
|
||||
<div key={s.code} style={{
|
||||
display: 'flex', alignItems: 'center', gap: 6, fontSize: 11, padding: '4px 8px', borderRadius: 8,
|
||||
background: tk.cardBg, border: `1px solid ${tk.border}`, color: tk.textSub,
|
||||
}}>
|
||||
<span style={{ width: 5, height: 5, borderRadius: '50%', flexShrink: 0, background: s.status === 'alert' ? tk.danger : s.status === 'working' ? tk.success : tk.textMuted }} />
|
||||
<span style={{ color: tk.text, fontWeight: 700, flexShrink: 0 }}>{s.name_zh}</span>
|
||||
<span style={{ minWidth: 0, flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{s.cur_zh}</span>
|
||||
</div>
|
||||
)) : <div style={{ fontSize: 11.5, color: tk.textMuted }}>{t.net.causalNoStaff}</div>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import { useMemo } from 'react';
|
||||
import type { CSSProperties } from 'react';
|
||||
|
||||
/**
|
||||
* 知识图谱氛围层(M5 深空叙事):星云漂移 + 闪烁星点 + 雷达扫描 + 中心辉光 + 边缘暗角。
|
||||
* 全部用 CSS 动画驱动(无 JS 帧循环,不抢占主线程),纯装饰层 pointerEvents: none。
|
||||
* deep 模式(全景舞台,强制深空)拉满;面板模式收敛为极淡星云底。
|
||||
*/
|
||||
|
||||
interface Props {
|
||||
w: number;
|
||||
h: number;
|
||||
/** 深空模式:舞台态强制深色底 */
|
||||
deep: boolean;
|
||||
/** 雷达扫描(仅舞台态开) */
|
||||
radar?: boolean;
|
||||
}
|
||||
|
||||
/** 与数据无关的固定分布:每次挂载只算一次 */
|
||||
function mulberry(seed: number) {
|
||||
let t = seed >>> 0;
|
||||
return () => {
|
||||
t += 0x6d2b79f5;
|
||||
let r = Math.imul(t ^ (t >>> 15), 1 | t);
|
||||
r ^= r + Math.imul(r ^ (r >>> 7), 61 | r);
|
||||
return ((r ^ (r >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
|
||||
export default function DeepSpaceBackdrop({ w, h, deep, radar = false }: Props) {
|
||||
const stars = useMemo(() => {
|
||||
const rnd = mulberry(20250911);
|
||||
return Array.from({ length: 46 }, () => ({
|
||||
left: `${(rnd() * 100).toFixed(2)}%`,
|
||||
top: `${(rnd() * 100).toFixed(2)}%`,
|
||||
size: rnd() < 0.82 ? 1.2 : 2,
|
||||
o: 0.18 + rnd() * 0.5,
|
||||
d: `${(2.6 + rnd() * 4.2).toFixed(1)}s`,
|
||||
delay: `${(rnd() * 5).toFixed(1)}s`,
|
||||
}));
|
||||
}, []);
|
||||
|
||||
const layer = (style: CSSProperties): CSSProperties => ({
|
||||
position: 'absolute', inset: 0, pointerEvents: 'none', ...style,
|
||||
});
|
||||
|
||||
return (
|
||||
<div style={layer({ overflow: 'hidden' })}>
|
||||
{/* 星云光斑:双色呼吸漂移 */}
|
||||
<div
|
||||
className="net-nebula"
|
||||
style={layer({
|
||||
background: deep
|
||||
? 'radial-gradient(52% 60% at 26% 22%, rgba(99,102,241,0.17), transparent 68%), radial-gradient(46% 54% at 78% 76%, rgba(56,189,248,0.11), transparent 66%)'
|
||||
: 'radial-gradient(52% 60% at 26% 22%, rgba(99,102,241,0.05), transparent 68%)',
|
||||
['--nb-d' as string]: '52s',
|
||||
})}
|
||||
/>
|
||||
<div
|
||||
className="net-nebula"
|
||||
style={layer({
|
||||
background: deep
|
||||
? 'radial-gradient(38% 44% at 64% 30%, rgba(129,140,248,0.10), transparent 64%)'
|
||||
: 'none',
|
||||
['--nb-d' as string]: '38s',
|
||||
})}
|
||||
/>
|
||||
|
||||
{/* 中心恒星辉光 + 雷达扫描(两者只在深空模式出现) */}
|
||||
{deep && (
|
||||
<>
|
||||
<div style={layer({
|
||||
background: 'radial-gradient(circle at 50% 50%, rgba(99,102,241,0.16), rgba(56,189,248,0.05) 30%, transparent 46%)',
|
||||
})} />
|
||||
{radar && (
|
||||
<div style={layer({
|
||||
display: 'grid', placeItems: 'center',
|
||||
})}>
|
||||
<div style={{
|
||||
width: Math.max(w, h) * 1.15, height: Math.max(w, h) * 1.15,
|
||||
borderRadius: '50%', mixBlendMode: 'screen',
|
||||
background: 'conic-gradient(from 0deg, transparent 0deg 292deg, rgba(129,140,248,0.05) 322deg, rgba(129,140,248,0.20) 355deg, transparent 360deg)',
|
||||
animation: 'net-radar 56s linear infinite',
|
||||
opacity: 0.75,
|
||||
}} />
|
||||
</div>
|
||||
)}
|
||||
{/* 星点闪烁 */}
|
||||
{stars.map((s, i) => (
|
||||
<span
|
||||
key={i}
|
||||
className="net-twinkle"
|
||||
style={{
|
||||
position: 'absolute', left: s.left, top: s.top,
|
||||
width: s.size, height: s.size, borderRadius: '50%',
|
||||
background: i % 7 === 0 ? '#93c5fd' : '#dbeafe',
|
||||
['--tw-o' as string]: s.o,
|
||||
['--tw-d' as string]: s.d,
|
||||
['--tw-delay' as string]: s.delay,
|
||||
animationDelay: s.delay,
|
||||
opacity: s.o,
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 边缘暗角:收束视线,制造纵深 */}
|
||||
{deep && (
|
||||
<div style={layer({
|
||||
background: 'radial-gradient(ellipse 78% 78% at 50% 50%, transparent 52%, rgba(2,6,18,0.55) 100%)',
|
||||
})} />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { ChevronLeft, ChevronRight, Play, Radio, Square } from 'lucide-react';
|
||||
import type { ThemeTokens } from '../../context/ThemeContext';
|
||||
import type { I18n } from '../../i18n/locales';
|
||||
import type { MetricNode } from '../../data/sqlite';
|
||||
import { achieveAt, PERIOD_MONTHS, periodLabel, CURRENT_PERIOD, PERIOD_COUNT } from '../../lib/time';
|
||||
import { execColor } from '../../lib/metric';
|
||||
|
||||
/**
|
||||
* BI 式时间轴滑块:拖动 / 点击刻度 / 自动回放,全图按游标时点重取值与状态。
|
||||
* 柱体高度与颜色取自整体健康度在各期的达成率,时间进度感与数据同轴呈现。
|
||||
*/
|
||||
interface Props {
|
||||
period: number;
|
||||
onPeriod: (p: number) => void;
|
||||
/** 整体健康度节点:驱动 12 根背景柱 */
|
||||
ov?: MetricNode;
|
||||
tokens: ThemeTokens;
|
||||
t: I18n;
|
||||
}
|
||||
|
||||
export default function MetricTimeline({ period, onPeriod, ov, tokens, t }: Props) {
|
||||
const [playing, setPlaying] = useState(false);
|
||||
const trackRef = useRef<HTMLDivElement>(null);
|
||||
const draggingRef = useRef(false);
|
||||
const periodRef = useRef(period); periodRef.current = period;
|
||||
const onPeriodRef = useRef(onPeriod); onPeriodRef.current = onPeriod;
|
||||
|
||||
useEffect(() => {
|
||||
if (!playing) return;
|
||||
const id = window.setInterval(() => {
|
||||
onPeriodRef.current((periodRef.current + 1) % PERIOD_COUNT);
|
||||
}, 850);
|
||||
return () => window.clearInterval(id);
|
||||
}, [playing]);
|
||||
|
||||
const posToPeriod = (clientX: number) => {
|
||||
const el = trackRef.current;
|
||||
if (!el) return periodRef.current;
|
||||
const r = el.getBoundingClientRect();
|
||||
const f = (clientX - r.left) / Math.max(1, r.width);
|
||||
return Math.max(0, Math.min(PERIOD_COUNT - 1, Math.floor(f * PERIOD_COUNT)));
|
||||
};
|
||||
|
||||
const months = PERIOD_MONTHS.map(m => `${Number(m.slice(5, 7))}月`);
|
||||
const cur = periodLabel(period);
|
||||
|
||||
return (
|
||||
<div style={{
|
||||
flex: '0 0 auto', display: 'flex', alignItems: 'center', gap: 12,
|
||||
margin: '0 6px 6px', padding: '9px 14px', borderRadius: 12,
|
||||
background: tokens.panelBgAlt, border: `1px solid ${tokens.border}`,
|
||||
backdropFilter: 'blur(8px)', userSelect: 'none',
|
||||
}}>
|
||||
{/* 回放控制 */}
|
||||
<button
|
||||
onClick={() => setPlaying(p => !p)}
|
||||
className="ghost-btn"
|
||||
title={playing ? t.net.tlPause : t.net.tlPlay}
|
||||
style={{
|
||||
display: 'flex', alignItems: 'center', gap: 5, flexShrink: 0, cursor: 'pointer',
|
||||
padding: '4px 10px', borderRadius: 8, fontSize: 12, fontWeight: 700,
|
||||
color: playing ? tokens.onAccent : tokens.accent,
|
||||
background: playing ? tokens.accent : tokens.accentBg,
|
||||
border: `1px solid ${tokens.borderAccent}`,
|
||||
}}
|
||||
>
|
||||
{playing ? <Square size={11} /> : <Play size={11} />}
|
||||
{playing ? t.net.tlPause : t.net.tlPlay}
|
||||
</button>
|
||||
|
||||
{/* 轨道:12 根健康度柱 + 游标 */}
|
||||
<div
|
||||
ref={trackRef}
|
||||
style={{ position: 'relative', flex: 1, height: 42, cursor: 'ew-resize', touchAction: 'none', minWidth: 0 }}
|
||||
onPointerDown={e => {
|
||||
draggingRef.current = true;
|
||||
setPlaying(false);
|
||||
try { e.currentTarget.setPointerCapture(e.pointerId); } catch { /* 合成指针下降级为无捕获拖动 */ }
|
||||
onPeriod(posToPeriod(e.clientX));
|
||||
}}
|
||||
onPointerMove={e => { if (draggingRef.current) onPeriod(posToPeriod(e.clientX)); }}
|
||||
onPointerUp={() => { draggingRef.current = false; }}
|
||||
onPointerCancel={() => { draggingRef.current = false; }}
|
||||
>
|
||||
{/* 柱阵列 */}
|
||||
<div style={{ position: 'absolute', inset: '0 0 12px', display: 'flex', gap: 3 }}>
|
||||
{Array.from({ length: PERIOD_COUNT }, (_, i) => {
|
||||
const rate = ov ? achieveAt(ov, i) : 100;
|
||||
const passed = i <= period;
|
||||
const h = 12 + Math.min(1.4, Math.max(0.25, rate / 100)) * 18;
|
||||
const col = execColor(rate, tokens);
|
||||
return (
|
||||
<div key={i} style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'flex-end', alignItems: 'stretch' }}>
|
||||
<div style={{
|
||||
height: h, borderRadius: 3,
|
||||
background: col,
|
||||
opacity: i === period ? 0.95 : passed ? 0.42 : 0.16,
|
||||
boxShadow: i === period ? `0 0 10px ${col}88` : 'none',
|
||||
transition: 'opacity 0.25s ease, height 0.4s ease, box-shadow 0.25s ease',
|
||||
}} />
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
{/* 游标发光线 */}
|
||||
<div style={{
|
||||
position: 'absolute', top: 0, bottom: 10, width: 2, borderRadius: 2,
|
||||
left: `calc(${((period + 0.5) / PERIOD_COUNT) * 100}% - 1px)`,
|
||||
background: `linear-gradient(180deg, ${tokens.accent}, ${tokens.info})`,
|
||||
boxShadow: `0 0 10px ${tokens.accentGlow}`,
|
||||
transition: 'left 0.28s cubic-bezier(0.22,1,0.36,1)', pointerEvents: 'none',
|
||||
}}>
|
||||
<span style={{
|
||||
position: 'absolute', top: -3, left: '50%', transform: 'translateX(-50%)',
|
||||
width: 9, height: 9, borderRadius: '50%', background: tokens.accent,
|
||||
boxShadow: `0 0 8px ${tokens.accentGlow}`, border: `2px solid ${tokens.panelBgAlt}`,
|
||||
}} />
|
||||
</div>
|
||||
{/* 月刻度 */}
|
||||
<div style={{ position: 'absolute', left: 0, right: 0, bottom: -2, display: 'flex' }}>
|
||||
{months.map((m, i) => (
|
||||
<span key={`${m}-${i}`} title={PERIOD_MONTHS[i]} style={{
|
||||
flex: 1, textAlign: 'center', fontSize: 9.5,
|
||||
fontVariantNumeric: 'tabular-nums',
|
||||
color: i === period ? tokens.accent : tokens.textMuted,
|
||||
opacity: i === period ? 1 : 0.55, fontWeight: i === period ? 800 : 400,
|
||||
}}>{m}</span>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 当前期读数 */}
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 7, flexShrink: 0 }}>
|
||||
<button
|
||||
onClick={() => onPeriod(Math.max(0, period - 1))}
|
||||
className="ghost-btn"
|
||||
style={{ display: 'grid', placeItems: 'center', width: 22, height: 22, borderRadius: 6, cursor: 'pointer', color: tokens.textSub, border: `1px solid ${tokens.border}`, background: 'transparent' }}
|
||||
><ChevronLeft size={13} /></button>
|
||||
<span style={{
|
||||
display: 'flex', alignItems: 'center', gap: 5, fontSize: 13.5, fontWeight: 800,
|
||||
color: tokens.accent, fontVariantNumeric: 'tabular-nums', fontFamily: 'ui-monospace, monospace',
|
||||
minWidth: 86, justifyContent: 'center',
|
||||
}}>
|
||||
<Radio size={11} />{cur}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => onPeriod(Math.min(PERIOD_COUNT - 1, period + 1))}
|
||||
className="ghost-btn"
|
||||
style={{ display: 'grid', placeItems: 'center', width: 22, height: 22, borderRadius: 6, cursor: 'pointer', color: tokens.textSub, border: `1px solid ${tokens.border}`, background: 'transparent' }}
|
||||
><ChevronRight size={13} /></button>
|
||||
<button
|
||||
onClick={() => { setPlaying(false); onPeriod(CURRENT_PERIOD); }}
|
||||
className="ghost-btn"
|
||||
disabled={period === CURRENT_PERIOD}
|
||||
style={{
|
||||
padding: '3px 10px', borderRadius: 8, fontSize: 11.5, fontWeight: 700, cursor: period === CURRENT_PERIOD ? 'default' : 'pointer',
|
||||
color: period === CURRENT_PERIOD ? tokens.textMuted : tokens.accent,
|
||||
border: `1px solid ${period === CURRENT_PERIOD ? tokens.border : tokens.borderAccent}`,
|
||||
background: period === CURRENT_PERIOD ? 'transparent' : tokens.accentBg,
|
||||
whiteSpace: 'nowrap',
|
||||
}}
|
||||
>{t.net.tlBack}</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { ThemeTokens } from '../../context/ThemeContext';
|
||||
|
||||
/** 舞台态强制深空(Bloomberg 黑屏模式):图谱与看板共用同一套语义色,不随用户主题漂移 */
|
||||
export const DEEP: Partial<ThemeTokens> = {
|
||||
text: '#e8eefc', textSub: '#a9b8d0', textMuted: 'rgba(154,172,199,0.82)',
|
||||
accent: '#818cf8', accentGlow: 'rgba(129,140,248,0.75)', accentBg: 'rgba(129,140,248,0.10)',
|
||||
border: 'rgba(129,140,248,0.16)', borderSub: 'rgba(129,140,248,0.08)', borderAccent: 'rgba(129,140,248,0.34)',
|
||||
panelBg: 'rgba(8,13,28,0.92)', panelBgAlt: 'rgba(10,16,32,0.72)', cardBg: 'rgba(20,29,54,0.66)',
|
||||
success: '#34d399', warning: '#fbbf24', danger: '#f87171', info: '#38bdf8',
|
||||
};
|
||||
|
||||
export const DEEP_BG = 'radial-gradient(120% 95% at 50% 40%, #0a1022 0%, #060a16 52%, #03060e 100%)';
|
||||
|
||||
/** 达成率弧:从 12 点顺时针的 donut 弧(节点形态即数据:环弧长度=达成率) */
|
||||
export function arcPath(cx: number, cy: number, r: number, frac: number): string {
|
||||
const f = Math.min(0.9999, Math.max(0.0001, frac));
|
||||
const a0 = -Math.PI / 2, a1 = a0 + f * Math.PI * 2;
|
||||
const x0 = cx + r * Math.cos(a0), y0 = cy + r * Math.sin(a0);
|
||||
const x1 = cx + r * Math.cos(a1), y1 = cy + r * Math.sin(a1);
|
||||
return `M ${x0.toFixed(2)},${y0.toFixed(2)} A ${r},${r} 0 ${f > 0.5 ? 1 : 0} 1 ${x1.toFixed(2)},${y1.toFixed(2)}`;
|
||||
}
|
||||
Reference in New Issue
Block a user