| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- 构建创作思维路径可视化 V3
- 新版特性:
- - 按节点发现编号分层布局(同一编号在同一层)
- - 同一个发现编号的节点使用相同的填充颜色
- - Tab 切换展示目录下所有帖子
- 读取 creation_pattern 目录下的 JSON 数据,输出单文件 HTML
- """
- import json
- import sys
- from pathlib import Path
- # 项目路径
- project_root = Path(__file__).parent.parent.parent
- sys.path.insert(0, str(project_root))
- from script.data_processing.path_config import PathConfig
- HTML_TEMPLATE = '''<!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>创作思维路径可视化</title>
- <script src="https://d3js.org/d3.v7.min.js"></script>
- <style>
- * { margin: 0; padding: 0; box-sizing: border-box; }
- body {
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
- background: #1a1a2e;
- color: #eee;
- overflow: hidden;
- }
- .container { display: flex; flex-direction: column; height: 100vh; }
- /* Tab 样式 */
- .tab-bar {
- display: flex;
- background: #0f3460;
- border-bottom: 1px solid #16213e;
- overflow-x: auto;
- flex-shrink: 0;
- }
- .tab-bar::-webkit-scrollbar { height: 4px; }
- .tab-bar::-webkit-scrollbar-thumb { background: #e94560; border-radius: 2px; }
- .tab-item {
- padding: 10px 16px;
- font-size: 12px;
- color: #888;
- cursor: pointer;
- border-bottom: 2px solid transparent;
- white-space: nowrap;
- transition: all 0.2s;
- }
- .tab-item:hover { color: #ccc; background: rgba(233, 69, 96, 0.1); }
- .tab-item.active {
- color: #e94560;
- border-bottom-color: #e94560;
- background: rgba(233, 69, 96, 0.1);
- }
- header {
- padding: 10px 20px;
- background: #16213e;
- border-bottom: 1px solid #0f3460;
- display: flex;
- align-items: center;
- gap: 20px;
- }
- h1 { font-size: 16px; font-weight: 500; color: #e94560; }
- .controls { display: flex; align-items: center; gap: 12px; flex: 1; }
- .slider-container {
- display: flex;
- align-items: center;
- gap: 8px;
- background: #0f3460;
- padding: 6px 12px;
- border-radius: 6px;
- }
- .slider-container label { font-size: 13px; color: #aaa; }
- #stepSlider { width: 200px; cursor: pointer; }
- #stepDisplay { font-size: 14px; font-weight: 600; color: #e94560; min-width: 60px; }
- .play-btn {
- background: #e94560;
- border: none;
- color: white;
- padding: 6px 14px;
- border-radius: 4px;
- cursor: pointer;
- font-size: 13px;
- }
- .play-btn:hover { background: #ff6b6b; }
- .legend {
- display: flex;
- gap: 12px;
- font-size: 11px;
- color: #888;
- margin-left: auto;
- background: #0f3460;
- padding: 6px 12px;
- border-radius: 6px;
- }
- .legend-item { display: flex; align-items: center; gap: 4px; }
- .legend-dot { width: 10px; height: 10px; border-radius: 50%; }
- main { flex: 1; display: flex; overflow: hidden; }
- .graph-area { flex: 1; position: relative; }
- .graph-area svg { width: 100%; height: 100%; }
- .right-panel {
- width: 340px;
- background: #16213e;
- border-left: 1px solid #0f3460;
- display: flex;
- flex-direction: column;
- overflow: hidden;
- }
- .post-summary {
- padding: 16px;
- border-bottom: 1px solid #0f3460;
- max-height: 180px;
- overflow-y: auto;
- }
- .post-title {
- font-size: 14px;
- font-weight: 600;
- color: #e94560;
- margin-bottom: 8px;
- line-height: 1.4;
- }
- .post-body {
- font-size: 12px;
- color: #aaa;
- line-height: 1.6;
- max-height: 80px;
- overflow-y: auto;
- }
- .post-meta {
- margin-top: 8px;
- font-size: 11px;
- color: #666;
- display: flex;
- gap: 12px;
- }
- .timeline {
- flex: 1;
- overflow-y: auto;
- padding: 16px;
- }
- .timeline-title {
- font-size: 13px;
- font-weight: 600;
- color: #888;
- margin-bottom: 12px;
- }
- .timeline-item {
- position: relative;
- padding-left: 24px;
- padding-bottom: 16px;
- border-left: 2px solid #0f3460;
- margin-left: 8px;
- cursor: pointer;
- transition: all 0.2s;
- }
- .timeline-item:hover {
- background: rgba(233, 69, 96, 0.1);
- margin-left: 6px;
- padding-left: 26px;
- }
- .timeline-item:last-child { border-left-color: transparent; }
- .timeline-item.active { border-left-color: #e94560; }
- .timeline-item.future { opacity: 0.4; }
- .timeline-dot {
- position: absolute;
- left: -7px;
- top: 0;
- width: 12px;
- height: 12px;
- border-radius: 50%;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 8px;
- font-weight: bold;
- color: white;
- }
- .timeline-node-name {
- font-size: 13px;
- font-weight: 500;
- color: #eee;
- margin-bottom: 4px;
- }
- .timeline-node-dim {
- font-size: 11px;
- padding: 2px 6px;
- border-radius: 3px;
- display: inline-block;
- margin-bottom: 6px;
- }
- .timeline-node-dim.灵感点 { background: rgba(255,107,107,0.2); color: #ff6b6b; }
- .timeline-node-dim.目的点 { background: rgba(78,205,196,0.2); color: #4ecdc4; }
- .timeline-node-dim.关键点 { background: rgba(255,230,109,0.2); color: #ffe66d; }
- .timeline-reason {
- font-size: 11px;
- color: #888;
- line-height: 1.5;
- background: #0f3460;
- padding: 8px;
- border-radius: 4px;
- }
- .timeline-from {
- font-size: 11px;
- color: #666;
- margin-bottom: 4px;
- }
- .timeline-from span { color: #e94560; }
- .layer-label { font-size: 12px; fill: #555; font-weight: 600; }
- .layer-line { stroke: #2a2a4a; stroke-width: 1; stroke-dasharray: 4,4; }
- .node { cursor: pointer; }
- .node circle { stroke-width: 0; transition: all 0.3s; }
- .node.unknown circle { fill: #2a2a4a !important; opacity: 0.4; }
- .node.highlight circle { filter: drop-shadow(0 0 8px rgba(255,255,255,0.5)); }
- .node text { font-size: 11px; fill: #ccc; text-anchor: middle; pointer-events: none; }
- .node.unknown text { fill: #555; }
- .node .order-badge { font-size: 10px; font-weight: bold; fill: white; }
- .node .constant-badge { font-size: 12px; fill: #ffd700; }
- .node.constant circle:first-child { filter: drop-shadow(0 0 4px rgba(255, 215, 0, 0.6)); }
- .link { stroke-opacity: 0.5; transition: all 0.3s; cursor: pointer; }
- .link.hidden { stroke-opacity: 0 !important; pointer-events: none; }
- .link.highlight { stroke-opacity: 0.8; stroke-width: 2px; }
- .link:hover { stroke-opacity: 0.9; stroke-width: 2.5px; }
- .link-label {
- font-size: 10px;
- fill: #f39c12;
- font-weight: 600;
- text-anchor: middle;
- pointer-events: none;
- }
- .link-label.hidden { opacity: 0; }
- .link-label-group.hidden { opacity: 0; pointer-events: none; }
- .link-label-bg {
- fill: #16213e;
- opacity: 0.85;
- }
- .edge-detail {
- position: fixed;
- background: #16213e;
- border: 1px solid #0f3460;
- border-radius: 8px;
- padding: 12px 16px;
- max-width: 320px;
- box-shadow: 0 4px 20px rgba(0,0,0,0.4);
- z-index: 1000;
- display: none;
- }
- .edge-detail.show { display: block; }
- .edge-detail-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 10px;
- }
- .edge-detail-type {
- font-size: 12px;
- padding: 3px 8px;
- border-radius: 4px;
- font-weight: 500;
- }
- .edge-detail-type.AI推导 { background: rgba(233,69,96,0.2); color: #e94560; }
- .edge-detail-type.共现推导 { background: rgba(52,152,219,0.2); color: #3498db; }
- .edge-detail-close {
- background: none;
- border: none;
- color: #888;
- cursor: pointer;
- font-size: 16px;
- }
- .edge-detail-close:hover { color: #fff; }
- .edge-detail-nodes {
- font-size: 13px;
- color: #ccc;
- margin-bottom: 8px;
- }
- .edge-detail-nodes span { color: #e94560; font-weight: 500; }
- .edge-detail-arrow { color: #666; margin: 0 6px; }
- .edge-detail-score {
- font-size: 11px;
- color: #888;
- margin-bottom: 8px;
- }
- .edge-detail-reason {
- font-size: 12px;
- color: #aaa;
- line-height: 1.6;
- background: #0f3460;
- padding: 10px;
- border-radius: 4px;
- }
- .edge-detail-path {
- margin-top: 10px;
- font-size: 11px;
- color: #888;
- background: #0f3460;
- padding: 10px;
- border-radius: 4px;
- max-height: 200px;
- overflow-y: auto;
- }
- .edge-detail-path-title {
- font-weight: 500;
- color: #e94560;
- margin-bottom: 6px;
- }
- .edge-path-item {
- display: flex;
- align-items: center;
- gap: 4px;
- padding: 4px 0;
- border-bottom: 1px solid #1a1a2e;
- }
- .edge-path-item:last-child { border-bottom: none; }
- .path-node {
- display: inline-flex;
- align-items: center;
- gap: 4px;
- background: rgba(233, 69, 96, 0.15);
- padding: 2px 6px;
- border-radius: 3px;
- }
- .path-node-domain {
- color: #888;
- font-size: 10px;
- margin-right: 4px;
- }
- .path-node-shape {
- margin-right: 4px;
- font-size: 10px;
- }
- .path-node-shape.circle { color: #e94560; }
- .path-node-shape.square { color: #3498db; }
- .path-node-name { color: #ccc; font-weight: 500; }
- .path-edge {
- display: inline-flex;
- align-items: center;
- gap: 4px;
- color: #3498db;
- }
- .path-edge-type { font-size: 10px; }
- .path-edge-score { color: #f39c12; font-weight: 500; }
- </style>
- </head>
- <body>
- <div class="edge-detail" id="edgeDetail">
- <div class="edge-detail-header">
- <span class="edge-detail-type" id="edgeType"></span>
- <button class="edge-detail-close" id="edgeClose">×</button>
- </div>
- <div class="edge-detail-nodes" id="edgeNodes"></div>
- <div class="edge-detail-score" id="edgeScore"></div>
- <div class="edge-detail-reason" id="edgeReason"></div>
- <div class="edge-detail-path" id="edgePath" style="display: none;"></div>
- </div>
- <div class="container">
- <div class="tab-bar" id="tabBar"></div>
- <header>
- <h1>创作思维路径</h1>
- <div class="controls">
- <div class="slider-container">
- <label>步骤:</label>
- <input type="range" id="stepSlider" min="0" max="9" value="9">
- <span id="stepDisplay">9/9</span>
- </div>
- <button class="play-btn" id="playBtn">▶ 播放</button>
- </div>
- <div class="legend">
- <div class="legend-item"><span class="legend-dot" style="background: #e94560;"></span><span>AI推导</span></div>
- <div class="legend-item"><span class="legend-dot" style="background: #3498db;"></span><span>共现推导</span></div>
- <div class="legend-item"><span style="color: #ffd700; font-size: 14px;">★</span><span>人设常量</span></div>
- </div>
- </header>
- <main>
- <div class="graph-area">
- <svg id="graph"></svg>
- </div>
- <div class="right-panel">
- <div class="post-summary" id="postSummary"></div>
- <div class="timeline" id="timeline"></div>
- </div>
- </main>
- </div>
- <script>
- const ALL_DATA = __DATA_PLACEHOLDER__;
- const stepColors = [
- '#e94560', '#9b59b6', '#3498db', '#1abc9c', '#2ecc71',
- '#f39c12', '#e74c3c', '#8e44ad', '#2980b9', '#16a085',
- '#27ae60', '#d35400', '#c0392b', '#7b1fa2', '#1565c0',
- ];
- function getStepColor(order) {
- if (!order || order <= 0) return '#2a2a4a';
- return stepColors[(order - 1) % stepColors.length];
- }
- let currentPostIndex = 0;
- let postDetail, graphNodes, graphLinks, nodeById, edgeByTarget, maxOrder;
- let currentStep, playing = false, playInterval = null;
- let svg, g, zoom, link, node;
- const slider = document.getElementById('stepSlider');
- const stepDisplay = document.getElementById('stepDisplay');
- const playBtn = document.getElementById('playBtn');
- // 边类型颜色
- const edgeColors = {
- 'AI推导': '#e94560',
- '共现推导': '#3498db',
- '支撑': '#666',
- '关联': '#666'
- };
- function loadPostData(index) {
- const DATA = ALL_DATA[index];
- postDetail = DATA['帖子详情'];
- const steps = DATA['步骤列表'];
- const lastStep = steps[steps.length - 1];
- const nodes = lastStep['输出']['节点列表'];
- const edges = lastStep['输出']['边列表'] || [];
- maxOrder = Math.max(...nodes.map(n => n['发现编号'] || 0), 1);
- // 只保留 AI推导 和 共现推导 用于 timeline 显示
- edgeByTarget = {};
- edges.filter(e => e['关系类型'] === 'AI推导' || e['关系类型'] === '共现推导')
- .forEach(e => { edgeByTarget[e['目标']] = e; });
- nodeById = {};
- graphNodes = nodes.map(n => {
- const node = {
- id: n['节点ID'],
- name: n['节点名称'],
- dimension: n['节点维度'],
- order: n['发现编号'],
- isKnown: n['是否已知'],
- isConstant: n['是人设常量'] || false
- };
- nodeById[node.id] = node;
- return node;
- });
- // 只加载 AI推导 和 共现推导 边(支撑/关联不展示)
- graphLinks = edges
- .filter(e => e['关系类型'] === 'AI推导' || e['关系类型'] === '共现推导')
- .map(e => ({
- source: e['来源'],
- target: e['目标'],
- type: e['关系类型'],
- score: e['score'] || e['可能性分数'],
- reason: e['推理说明'],
- path: e['推导路径']
- }));
- }
- function renderTabs() {
- const tabBar = document.getElementById('tabBar');
- tabBar.innerHTML = ALL_DATA.map((data, i) => {
- const title = data['帖子详情']?.postTitle || `帖子${i + 1}`;
- const shortTitle = title.length > 20 ? title.slice(0, 20) + '...' : title;
- return `<div class="tab-item ${i === 0 ? 'active' : ''}" data-index="${i}">${shortTitle}</div>`;
- }).join('');
- tabBar.querySelectorAll('.tab-item').forEach(tab => {
- tab.addEventListener('click', () => {
- const index = parseInt(tab.dataset.index);
- if (index !== currentPostIndex) {
- if (playing) { clearInterval(playInterval); playing = false; playBtn.textContent = '▶ 播放'; }
- currentPostIndex = index;
- tabBar.querySelectorAll('.tab-item').forEach(t => t.classList.remove('active'));
- tab.classList.add('active');
- loadPostData(index);
- renderAll();
- }
- });
- });
- }
- function renderPostSummary() {
- const container = document.getElementById('postSummary');
- const bodyText = postDetail.body_text || '';
- const shortBody = bodyText.length > 150 ? bodyText.slice(0, 150) + '...' : bodyText;
- container.innerHTML = `
- <div class="post-title">${postDetail.postTitle || '无标题'}</div>
- <div class="post-body">${shortBody}</div>
- <div class="post-meta">
- <span>❤️ ${postDetail.like_count || 0}</span>
- <span>⭐ ${postDetail.collect_count || 0}</span>
- </div>
- `;
- }
- function renderTimeline() {
- const container = document.getElementById('timeline');
- const sortedNodes = [...graphNodes].filter(n => n.order).sort((a, b) => a.order - b.order);
- let html = '<div class="timeline-title">推导过程</div>';
- sortedNodes.forEach(n => {
- const edge = edgeByTarget[n.id];
- const fromNode = edge ? nodeById[edge.source] : null;
- const stepColor = getStepColor(n.order);
- html += `
- <div class="timeline-item" data-order="${n.order}" data-node-id="${n.id}">
- <div class="timeline-dot" style="background: ${stepColor};">${n.order}</div>
- <div class="timeline-node-name">${n.name}</div>
- <div class="timeline-node-dim ${n.dimension}">${n.dimension}</div>
- ${fromNode ? `<div class="timeline-from">← <span>${fromNode.name}</span></div>` : '<div class="timeline-from">起点</div>'}
- ${edge?.reason ? `<div class="timeline-reason">${edge.reason}</div>` : ''}
- </div>
- `;
- });
- container.innerHTML = html;
- container.querySelectorAll('.timeline-item').forEach(item => {
- item.addEventListener('click', () => {
- const order = parseInt(item.dataset.order);
- slider.value = order;
- updateDisplay(order);
- highlightNode(item.dataset.nodeId);
- });
- item.addEventListener('mouseenter', () => highlightNode(item.dataset.nodeId));
- item.addEventListener('mouseleave', clearHighlight);
- });
- }
- function renderGraph() {
- const graphArea = document.querySelector('.graph-area');
- const width = graphArea.clientWidth;
- const height = graphArea.clientHeight;
- // 清空并重建 SVG
- d3.select('#graph').selectAll('*').remove();
- svg = d3.select('#graph');
- g = svg.append('g');
- zoom = d3.zoom()
- .scaleExtent([0.2, 3])
- .on('zoom', (event) => g.attr('transform', event.transform));
- svg.call(zoom);
- // 定义不同颜色的箭头
- const defs = svg.append('defs');
- Object.entries(edgeColors).forEach(([type, color]) => {
- defs.append('marker')
- .attr('id', `arrow-${type}`)
- .attr('viewBox', '0 -5 10 10')
- .attr('refX', 8)
- .attr('refY', 0)
- .attr('markerWidth', 6)
- .attr('markerHeight', 6)
- .attr('orient', 'auto')
- .append('path')
- .attr('d', 'M0,-4L8,0L0,4')
- .attr('fill', color);
- });
- // 分层布局
- const layerHeight = 100;
- const nodeSpacing = 140;
- const startY = 60;
- const nodesByOrder = {};
- graphNodes.forEach(n => {
- const order = n.order || 0;
- if (!nodesByOrder[order]) nodesByOrder[order] = [];
- nodesByOrder[order].push(n);
- });
- const orders = Object.keys(nodesByOrder).map(Number).sort((a, b) => a - b);
- orders.forEach((order, layerIndex) => {
- const layerNodes = nodesByOrder[order];
- const layerWidth = (layerNodes.length - 1) * nodeSpacing;
- const layerStartX = 80 + (width - 400 - layerWidth) / 2;
- layerNodes.forEach((n, i) => {
- n.x = layerStartX + i * nodeSpacing;
- n.y = startY + layerIndex * layerHeight;
- });
- });
- // 层级标签
- const layerGroup = g.append('g');
- orders.filter(o => o > 0).forEach((order, layerIndex) => {
- const y = startY + layerIndex * layerHeight;
- const stepColor = getStepColor(order);
- layerGroup.append('line')
- .attr('class', 'layer-line')
- .attr('x1', 20).attr('y1', y)
- .attr('x2', width - 360).attr('y2', y)
- .attr('stroke', stepColor).attr('stroke-opacity', 0.2);
- layerGroup.append('text')
- .attr('class', 'layer-label')
- .attr('x', 25).attr('y', y + 4)
- .attr('fill', stepColor)
- .text(`${order}`);
- });
- // 边(按类型着色)
- link = g.append('g').selectAll('path').data(graphLinks).join('path')
- .attr('class', d => `link link-${d.type}`)
- .attr('stroke', d => edgeColors[d.type] || '#666')
- .attr('stroke-width', d => (d.type === 'AI推导' || d.type === '共现推导') ? 1.5 : 1)
- .attr('fill', 'none')
- .attr('marker-end', d => d.type === 'AI推导' ? `url(#arrow-${d.type})` : null)
- .attr('d', d => {
- const s = nodeById[d.source], t = nodeById[d.target];
- if (!s || !t) return '';
- const sy = s.y + 20, ty = t.y - 20;
- const offset = Math.min(Math.abs(ty - sy) * 0.4, 40);
- return `M ${s.x} ${sy} C ${s.x} ${sy + offset} ${t.x} ${ty - offset} ${t.x} ${ty}`;
- })
- .on('click', (event, d) => showEdgeDetail(event, d));
- // 边分数标签
- const linkLabels = g.append('g').selectAll('g')
- .data(graphLinks.filter(d => d.score > 0))
- .join('g')
- .attr('class', 'link-label-group')
- .attr('transform', d => {
- const s = nodeById[d.source], t = nodeById[d.target];
- if (!s || !t) return '';
- const mx = (s.x + t.x) / 2;
- const my = (s.y + 20 + t.y - 20) / 2;
- return `translate(${mx}, ${my})`;
- });
- linkLabels.append('rect')
- .attr('class', 'link-label-bg')
- .attr('x', -16).attr('y', -8)
- .attr('width', 32).attr('height', 14)
- .attr('rx', 3);
- linkLabels.append('text')
- .attr('class', 'link-label')
- .attr('dy', 3)
- .text(d => d.score.toFixed(2));
- // 节点
- node = g.append('g').selectAll('g').data(graphNodes).join('g')
- .attr('class', d => `node${d.isConstant ? ' constant' : ''}`)
- .attr('transform', d => `translate(${d.x},${d.y})`)
- .on('click', (e, d) => { slider.value = d.order || maxOrder; updateDisplay(d.order || maxOrder); })
- .on('mouseenter', (e, d) => highlightNode(d.id))
- .on('mouseleave', clearHighlight);
- // 常量节点外环
- node.filter(d => d.isConstant).append('circle')
- .attr('r', 24)
- .attr('fill', 'none')
- .attr('stroke', '#ffd700')
- .attr('stroke-width', 2)
- .attr('stroke-dasharray', '4,2');
- node.append('circle').attr('r', 18).attr('fill', d => getStepColor(d.order));
- node.append('text').attr('class', 'order-badge').attr('dy', 4).text(d => d.order || '');
- // 常量节点显示星号
- node.filter(d => d.isConstant).append('text')
- .attr('class', 'constant-badge')
- .attr('dy', -22)
- .attr('text-anchor', 'middle')
- .text('★');
- node.append('text').attr('dy', 35).text(d => d.name);
- // 初始视图
- setTimeout(() => {
- const bounds = g.node().getBBox();
- const scale = Math.min((width - 380) / bounds.width, (height - 20) / bounds.height, 1);
- const tx = (width - 340 - bounds.width * scale) / 2 - bounds.x * scale;
- const ty = (height - bounds.height * scale) / 2 - bounds.y * scale;
- svg.transition().duration(300).call(zoom.transform, d3.zoomIdentity.translate(tx, ty).scale(scale));
- }, 50);
- }
- function highlightNode(nodeId) {
- node.classed('highlight', d => d.id === nodeId);
- link.classed('highlight', d => d.target === nodeId);
- document.querySelectorAll('.timeline-item').forEach(item => {
- item.classList.toggle('active', item.dataset.nodeId === nodeId);
- });
- }
- function clearHighlight() {
- node.classed('highlight', false);
- link.classed('highlight', false);
- }
- // 边详情弹窗
- const edgeDetail = document.getElementById('edgeDetail');
- const edgeType = document.getElementById('edgeType');
- const edgeNodes = document.getElementById('edgeNodes');
- const edgeScore = document.getElementById('edgeScore');
- const edgeReason = document.getElementById('edgeReason');
- const edgePath = document.getElementById('edgePath');
- const edgeClose = document.getElementById('edgeClose');
- function renderPath(pathData) {
- if (!pathData || pathData.length === 0) return '';
- let html = '<div class="edge-detail-path-title">推导路径</div>';
- pathData.forEach((item, idx) => {
- if (item['类型'] === '节点') {
- const domain = item['节点域'] || '';
- const dimension = item['节点维度'] || '';
- const nodeType = item['节点类型'] || '';
- const domainDim = [domain, dimension].filter(Boolean).join('-');
- const domainTag = domainDim ? `<span class="path-node-domain">[${domainDim}]</span>` : '';
- const shapeIcon = nodeType === '分类' ? '<span class="path-node-shape square">■</span>' : '<span class="path-node-shape circle">●</span>';
- html += `<div class="edge-path-item"><span class="path-node">${domainTag}${shapeIcon}<span class="path-node-name">${item['节点名称'] || ''}</span></span></div>`;
- } else if (item['类型'] === '边') {
- const scoreText = item['分数'] != null ? `<span class="path-edge-score">${item['分数']}</span>` : '';
- html += `<div class="edge-path-item"><span class="path-edge">↓ <span class="path-edge-type">${item['边类型'] || ''}</span> ${scoreText}</span></div>`;
- }
- });
- return html;
- }
- function showEdgeDetail(event, d) {
- event.stopPropagation();
- const sourceNode = nodeById[d.source];
- const targetNode = nodeById[d.target];
- edgeType.textContent = d.type;
- edgeType.className = `edge-detail-type ${d.type}`;
- const arrow = d.type === 'AI推导' ? '→' : '—';
- edgeNodes.innerHTML = `<span>${sourceNode?.name || d.source}</span><span class="edge-detail-arrow">${arrow}</span><span>${targetNode?.name || d.target}</span>`;
- edgeScore.textContent = d.score ? `分数: ${d.score}` : '';
- edgeReason.textContent = d.reason || '无推理说明';
- // 显示路径
- if (d.path && d.path.length > 0) {
- edgePath.innerHTML = renderPath(d.path);
- edgePath.style.display = 'block';
- } else {
- edgePath.style.display = 'none';
- }
- // 定位弹窗
- const x = Math.min(event.clientX + 10, window.innerWidth - 340);
- const y = Math.min(event.clientY + 10, window.innerHeight - 200);
- edgeDetail.style.left = x + 'px';
- edgeDetail.style.top = y + 'px';
- edgeDetail.classList.add('show');
- }
- function hideEdgeDetail() {
- edgeDetail.classList.remove('show');
- }
- edgeClose.addEventListener('click', hideEdgeDetail);
- document.addEventListener('click', (e) => {
- if (!edgeDetail.contains(e.target)) hideEdgeDetail();
- });
- function updateDisplay(step) {
- currentStep = step;
- stepDisplay.textContent = `${step}/${maxOrder}`;
- node.classed('known', d => d.order && d.order <= step);
- node.classed('unknown', d => !d.order || d.order > step);
- link.classed('hidden', d => {
- const s = nodeById[d.source], t = nodeById[d.target];
- return !s || !t || s.order > step || t.order > step;
- });
- // 同步控制边标签的显示
- d3.selectAll('.link-label-group').classed('hidden', function(d) {
- const s = nodeById[d.source], t = nodeById[d.target];
- return !s || !t || s.order > step || t.order > step;
- });
- document.querySelectorAll('.timeline-item').forEach(item => {
- item.classList.toggle('future', parseInt(item.dataset.order) > step);
- });
- }
- function renderAll() {
- slider.max = maxOrder;
- slider.value = maxOrder;
- currentStep = maxOrder;
- renderPostSummary();
- renderTimeline();
- renderGraph();
- updateDisplay(maxOrder);
- }
- slider.addEventListener('input', e => updateDisplay(parseInt(e.target.value)));
- playBtn.addEventListener('click', () => {
- if (playing) {
- clearInterval(playInterval);
- playBtn.textContent = '▶ 播放';
- playing = false;
- } else {
- if (currentStep >= maxOrder) { currentStep = 0; slider.value = 0; updateDisplay(0); }
- playing = true;
- playBtn.textContent = '⏸ 暂停';
- playInterval = setInterval(() => {
- currentStep++;
- slider.value = currentStep;
- updateDisplay(currentStep);
- const item = document.querySelector(`.timeline-item[data-order="${currentStep}"]`);
- if (item) item.scrollIntoView({ behavior: 'smooth', block: 'center' });
- if (currentStep >= maxOrder) {
- clearInterval(playInterval);
- playBtn.textContent = '▶ 播放';
- playing = false;
- }
- }, 1200);
- }
- });
- // 初始化
- renderTabs();
- loadPostData(0);
- renderAll();
- </script>
- </body>
- </html>
- '''
- def main():
- import argparse
- parser = argparse.ArgumentParser(description='构建创作思维路径可视化 V3(分层布局 + Tab切换)')
- parser.add_argument('input_dir', nargs='?', help='输入目录路径(可选,默认使用 PathConfig)')
- parser.add_argument('--version', '-v', type=str, default='v5',
- help='版本号 (v2/v3/v4/v5),默认 v5')
- parser.add_argument('--type', '-t', type=str, default='point_order',
- help='目录类型前缀 (creation_pattern/point_order),默认 point_order')
- args = parser.parse_args()
- if args.input_dir:
- input_dir = Path(args.input_dir)
- print(f"输入目录: {input_dir}")
- else:
- config = PathConfig()
- input_dir = config.intermediate_dir / f"{args.type}_{args.version}"
- print(f"账号: {config.account_name}")
- print(f"输入目录: {input_dir}")
- if not input_dir.exists():
- print(f"错误: 目录不存在: {input_dir}")
- sys.exit(1)
- # 查找所有分析文件(支持多种命名)
- pattern_files = sorted(input_dir.glob("*_点顺序.json"))
- if not pattern_files:
- pattern_files = sorted(input_dir.glob("*_创作模式.json"))
- print(f"找到 {len(pattern_files)} 个分析文件")
- if not pattern_files:
- print("错误: 没有找到分析文件 (*_点顺序.json 或 *_创作模式.json)!")
- sys.exit(1)
- # 读取所有帖子数据
- all_data = []
- for pattern_file in pattern_files:
- post_id = pattern_file.stem.replace("_点顺序", "").replace("_创作模式", "")
- print(f" 读取: {post_id}")
- with open(pattern_file, "r", encoding="utf-8") as f:
- data = json.load(f)
- all_data.append(data)
- # 生成单个 HTML 文件
- data_json = json.dumps(all_data, ensure_ascii=False)
- html_content = HTML_TEMPLATE.replace("__DATA_PLACEHOLDER__", data_json)
- output_file = input_dir / "创作思维路径可视化.html"
- with open(output_file, "w", encoding="utf-8") as f:
- f.write(html_content)
- print(f"\n输出: {output_file}")
- print("完成!")
- if __name__ == "__main__":
- main()
|