|
|
@@ -37,24 +37,18 @@
|
|
|
/**
|
|
|
* 通用关系图画布(基础渲染)
|
|
|
*
|
|
|
- * 与案件图谱的 GraphAnalysisViewer 完全独立:数据模型、G6 注册名(ai-relation-*)、
|
|
|
- * 样式都单独一套,但布局复用 graphLayout(对象关系分析页的布局实现),
|
|
|
- * 保证同类图在聊天里的节点分布一致。
|
|
|
+ * 数据模型/解析独立,但节点视觉复用案件图谱的 ai-graph-node(彩色图标方块 + 下方标签),
|
|
|
+ * 与项目中既有关系图组件保持一致;布局复用 graphLayout。
|
|
|
*
|
|
|
* 基础交互:缩放/拖拽/适配/全屏 + 节点类型图例;不做查找、过滤、条件重排、节点明细下钻。
|
|
|
*/
|
|
|
import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue';
|
|
|
- import { Graph, GraphEvent } from '@antv/g6';
|
|
|
+ import { CanvasEvent, EdgeEvent, Graph, GraphEvent, NodeEvent, type IElementEvent } from '@antv/g6';
|
|
|
import Icon from '@/core/components/Icon/src/Icon.vue';
|
|
|
import type { ParsedGraph } from '../graph/graphData';
|
|
|
+ import { getEntityMeta, registerAIGraphElements } from '../graph/graphElements';
|
|
|
import { layoutGraph } from '../graph/graphLayout';
|
|
|
- import {
|
|
|
- RELATION_TYPE_META_LIST,
|
|
|
- buildRelationG6Data,
|
|
|
- relationTypeMeta,
|
|
|
- type ParsedRelationGraph,
|
|
|
- } from './relationData';
|
|
|
- import { registerRelationElements } from './relationElements';
|
|
|
+ import { RELATION_TYPE_META_LIST, buildRelationG6Data, type ParsedRelationGraph } from './relationData';
|
|
|
|
|
|
const props = defineProps<{
|
|
|
model: ParsedRelationGraph;
|
|
|
@@ -108,7 +102,7 @@
|
|
|
/* ---------------- 生命周期 ---------------- */
|
|
|
|
|
|
onMounted(() => {
|
|
|
- registerRelationElements();
|
|
|
+ registerAIGraphElements();
|
|
|
void rebuild();
|
|
|
});
|
|
|
|
|
|
@@ -161,26 +155,52 @@
|
|
|
stageResizeObserver.observe(stageRef.value);
|
|
|
}
|
|
|
|
|
|
+ /** 关系图节点类型 → 项目既有实体类型(复用案件图谱的图标与配色) */
|
|
|
+ function toEntityKind(type: string): string {
|
|
|
+ switch (type) {
|
|
|
+ case 'person':
|
|
|
+ return 'person';
|
|
|
+ case 'phone':
|
|
|
+ return 'phone';
|
|
|
+ case 'card':
|
|
|
+ case 'account':
|
|
|
+ return 'bank';
|
|
|
+ case 'org':
|
|
|
+ return 'group';
|
|
|
+ case 'place':
|
|
|
+ return 'house';
|
|
|
+ case 'vehicle':
|
|
|
+ return 'car';
|
|
|
+ default:
|
|
|
+ return 'other';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
function createNodeOptions() {
|
|
|
return {
|
|
|
- type: 'ai-relation-node',
|
|
|
+ // 复用案件图谱的 ai-graph-node:彩色图标方块 + 下方主标签,两套关系图视觉一致
|
|
|
+ type: 'ai-graph-node',
|
|
|
style: (node: any) => {
|
|
|
- const meta = relationTypeMeta(String(node.data?.type || 'other'));
|
|
|
- const label = String(node.data?.label || node.id || '');
|
|
|
- const degree = Number(node.data?.degree || 0);
|
|
|
+ const meta = getEntityMeta(toEntityKind(String(node.data?.type || 'other')));
|
|
|
return {
|
|
|
- // 标签越长节点越宽,限制在 120~190,避免长名称溢出或节点过小
|
|
|
- boxWidth: Math.min(190, Math.max(120, 96 + label.length * 8)),
|
|
|
- boxHeight: 38,
|
|
|
+ size: 96,
|
|
|
+ iconBoxSize: 40,
|
|
|
+ iconBoxRadius: 11,
|
|
|
+ iconSize: 32,
|
|
|
fill: meta.color,
|
|
|
- fillOpacity: 0.96,
|
|
|
- stroke: degree >= 6 ? 'rgba(255,255,255,0.92)' : 'rgba(255,255,255,0.42)',
|
|
|
- lineWidth: degree >= 6 ? 2 : 1,
|
|
|
+ fillOpacity: 1,
|
|
|
+ stroke: 'rgba(255,255,255,0.22)',
|
|
|
+ strokeOpacity: 1,
|
|
|
+ lineWidth: 1,
|
|
|
cursor: 'pointer',
|
|
|
- labelText: label,
|
|
|
- labelFill: '#ffffff',
|
|
|
- labelFontSize: 12,
|
|
|
+ iconSrc: meta.icon,
|
|
|
+ iconOpacity: 1,
|
|
|
+ labelText: String(node.data?.label || node.id || ''),
|
|
|
+ labelFill: '#334155',
|
|
|
+ labelFontSize: 13,
|
|
|
labelFontWeight: 600,
|
|
|
+ labelOpacity: 1,
|
|
|
+ secondaryLabelText: '',
|
|
|
};
|
|
|
},
|
|
|
state: {
|
|
|
@@ -189,15 +209,34 @@
|
|
|
haloStroke: '#1677ff',
|
|
|
haloLineWidth: 2,
|
|
|
stroke: '#1677ff',
|
|
|
+ strokeOpacity: 1,
|
|
|
lineWidth: 3,
|
|
|
+ fillOpacity: 1,
|
|
|
+ },
|
|
|
+ highlight: {
|
|
|
+ halo: true,
|
|
|
+ haloStroke: '#60a5fa',
|
|
|
+ haloLineWidth: 2,
|
|
|
+ stroke: '#60a5fa',
|
|
|
+ strokeOpacity: 1,
|
|
|
+ lineWidth: 2,
|
|
|
+ fillOpacity: 1,
|
|
|
+ },
|
|
|
+ inactive: {
|
|
|
+ fillOpacity: 0.15,
|
|
|
+ iconOpacity: 0.15,
|
|
|
+ labelOpacity: 0.15,
|
|
|
+ secondaryLabelOpacity: 0.15,
|
|
|
+ strokeOpacity: 0.15,
|
|
|
},
|
|
|
},
|
|
|
};
|
|
|
}
|
|
|
|
|
|
function createEdgeOptions() {
|
|
|
- // 边太多时隐藏关系名,避免标签把画布糊住;节点本身的色块仍可辨识
|
|
|
- const showLabel = props.model.edges.length <= 80;
|
|
|
+ // 边标签始终显示:否则用户无法判断关系线代表什么。
|
|
|
+ // 边很多时缩小字号,并由底部关系名图例辅助识别。
|
|
|
+ const dense = props.model.edges.length > 120;
|
|
|
return {
|
|
|
type: (edge: any) => String(edge.type || 'line'),
|
|
|
style: (edge: any) => {
|
|
|
@@ -211,19 +250,177 @@
|
|
|
endArrow: Boolean(edge.data?.directed),
|
|
|
endArrowFill: '#94a3b8',
|
|
|
endArrowSize: 8,
|
|
|
- labelText: showLabel ? String(edge.data?.label || '') : '',
|
|
|
+ labelText: String(edge.data?.label || edge.data?.relation || '关联'),
|
|
|
labelPlacement: 'center',
|
|
|
labelFill: '#516076',
|
|
|
- labelFontSize: 10,
|
|
|
- labelOpacity: 0.9,
|
|
|
+ labelFontSize: dense ? 9 : 10,
|
|
|
+ labelOpacity: dense ? 0.85 : 0.95,
|
|
|
labelBackground: true,
|
|
|
labelBackgroundFill: 'rgba(255,255,255,0.92)',
|
|
|
labelBackgroundRadius: 4,
|
|
|
};
|
|
|
},
|
|
|
+ state: {
|
|
|
+ selected: {
|
|
|
+ stroke: '#2563eb',
|
|
|
+ lineWidth: 3,
|
|
|
+ endArrowFill: '#2563eb',
|
|
|
+ labelFill: '#1d4ed8',
|
|
|
+ labelFontSize: 11,
|
|
|
+ labelOpacity: 1,
|
|
|
+ },
|
|
|
+ highlight: {
|
|
|
+ stroke: '#3b82f6',
|
|
|
+ lineWidth: 2.5,
|
|
|
+ opacity: 1,
|
|
|
+ endArrowFill: '#3b82f6',
|
|
|
+ labelFill: '#1d4ed8',
|
|
|
+ labelFontSize: 11,
|
|
|
+ labelOpacity: 1,
|
|
|
+ },
|
|
|
+ inactive: {
|
|
|
+ opacity: 0.12,
|
|
|
+ labelOpacity: 0.08,
|
|
|
+ },
|
|
|
+ },
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ /* ---------------- 选中节点/关系线:高亮相关点线 ---------------- */
|
|
|
+
|
|
|
+ /** 当前高亮的中心节点;再次点击同一节点时取消 */
|
|
|
+ let activeNodeId = '';
|
|
|
+ /** 当前高亮的关系线;再次点击同一条线时取消 */
|
|
|
+ let activeEdgeId = '';
|
|
|
+
|
|
|
+ async function clearHighlight() {
|
|
|
+ const graph = graphRef.value;
|
|
|
+ if (!graph) return;
|
|
|
+ const tasks: Array<Promise<unknown>> = [];
|
|
|
+ try {
|
|
|
+ graph.getNodeData().forEach((node) => {
|
|
|
+ tasks.push(graph.setElementState(String(node.id), [], false));
|
|
|
+ });
|
|
|
+ graph.getEdgeData().forEach((edge) => {
|
|
|
+ tasks.push(graph.setElementState(String(edge.id), [], false));
|
|
|
+ });
|
|
|
+ await Promise.all(tasks);
|
|
|
+ } catch {
|
|
|
+ /* 忽略状态清理异常 */
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async function highlightNeighbors(id: string) {
|
|
|
+ const graph = graphRef.value;
|
|
|
+ if (!graph) return;
|
|
|
+
|
|
|
+ // 再点同一个节点 = 取消高亮
|
|
|
+ if (activeNodeId === id) {
|
|
|
+ activeNodeId = '';
|
|
|
+ activeEdgeId = '';
|
|
|
+ await clearHighlight();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ activeNodeId = id;
|
|
|
+ activeEdgeId = '';
|
|
|
+
|
|
|
+ const neighborNodes = new Set<string>();
|
|
|
+ const neighborEdges = new Set<string>();
|
|
|
+ props.model.edges.forEach((edge) => {
|
|
|
+ if (edge.source === id || edge.target === id) {
|
|
|
+ neighborEdges.add(edge.id);
|
|
|
+ neighborNodes.add(edge.source === id ? edge.target : edge.source);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ await clearHighlight();
|
|
|
+ const tasks: Array<Promise<unknown>> = [];
|
|
|
+ try {
|
|
|
+ tasks.push(graph.setElementState(id, ['selected'], false));
|
|
|
+ neighborNodes.forEach((nodeId) => {
|
|
|
+ tasks.push(graph.setElementState(nodeId, ['highlight'], false));
|
|
|
+ });
|
|
|
+ neighborEdges.forEach((edgeId) => {
|
|
|
+ tasks.push(graph.setElementState(edgeId, ['highlight'], false));
|
|
|
+ });
|
|
|
+ props.model.nodes.forEach((node) => {
|
|
|
+ if (node.id === id || neighborNodes.has(node.id)) return;
|
|
|
+ tasks.push(graph.setElementState(node.id, ['inactive'], false));
|
|
|
+ });
|
|
|
+ props.model.edges.forEach((edge) => {
|
|
|
+ if (neighborEdges.has(edge.id)) return;
|
|
|
+ tasks.push(graph.setElementState(edge.id, ['inactive'], false));
|
|
|
+ });
|
|
|
+ await Promise.all(tasks);
|
|
|
+ } catch {
|
|
|
+ /* 忽略状态高亮异常 */
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async function highlightEdge(edgeId: string) {
|
|
|
+ const graph = graphRef.value;
|
|
|
+ if (!graph) return;
|
|
|
+
|
|
|
+ // 再点同一条线 = 取消高亮
|
|
|
+ if (activeEdgeId === edgeId) {
|
|
|
+ activeEdgeId = '';
|
|
|
+ await clearHighlight();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const edge = props.model.edges.find((item) => item.id === edgeId);
|
|
|
+ if (!edge) return;
|
|
|
+
|
|
|
+ activeEdgeId = edgeId;
|
|
|
+ activeNodeId = '';
|
|
|
+
|
|
|
+ await clearHighlight();
|
|
|
+ const tasks: Array<Promise<unknown>> = [];
|
|
|
+ try {
|
|
|
+ tasks.push(graph.setElementState(edgeId, ['selected'], false));
|
|
|
+ tasks.push(graph.setElementState(edge.source, ['highlight'], false));
|
|
|
+ tasks.push(graph.setElementState(edge.target, ['highlight'], false));
|
|
|
+ props.model.nodes.forEach((node) => {
|
|
|
+ if (node.id === edge.source || node.id === edge.target) return;
|
|
|
+ tasks.push(graph.setElementState(node.id, ['inactive'], false));
|
|
|
+ });
|
|
|
+ props.model.edges.forEach((item) => {
|
|
|
+ if (item.id === edgeId) return;
|
|
|
+ tasks.push(graph.setElementState(item.id, ['inactive'], false));
|
|
|
+ });
|
|
|
+ await Promise.all(tasks);
|
|
|
+ } catch {
|
|
|
+ /* 忽略状态高亮异常 */
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let lastElementClickAt = 0;
|
|
|
+
|
|
|
+ function bindGraphEvents(graph: Graph) {
|
|
|
+ graph.on<IElementEvent>(NodeEvent.CLICK, (event: IElementEvent) => {
|
|
|
+ const id = String(event.target?.id || '');
|
|
|
+ if (!id) return;
|
|
|
+ lastElementClickAt = Date.now();
|
|
|
+ void highlightNeighbors(id);
|
|
|
+ });
|
|
|
+
|
|
|
+ graph.on<IElementEvent>(EdgeEvent.CLICK, (event: IElementEvent) => {
|
|
|
+ const id = String(event.target?.id || '');
|
|
|
+ if (!id) return;
|
|
|
+ lastElementClickAt = Date.now();
|
|
|
+ void highlightEdge(id);
|
|
|
+ });
|
|
|
+
|
|
|
+ graph.on(CanvasEvent.CLICK, () => {
|
|
|
+ window.setTimeout(() => {
|
|
|
+ // 部分 G6 版本里节点/边点击也会冒泡到 canvas;刚点过元素时不要立刻清掉高亮
|
|
|
+ if (Date.now() - lastElementClickAt < 120) return;
|
|
|
+ activeNodeId = '';
|
|
|
+ activeEdgeId = '';
|
|
|
+ void clearHighlight();
|
|
|
+ }, 0);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
async function rebuild() {
|
|
|
const element = stageRef.value;
|
|
|
if (!element || !props.model.nodes.length) {
|
|
|
@@ -237,6 +434,8 @@
|
|
|
return;
|
|
|
}
|
|
|
try {
|
|
|
+ activeNodeId = '';
|
|
|
+ activeEdgeId = '';
|
|
|
destroyGraph();
|
|
|
const size = getCanvasSize();
|
|
|
const data = buildRelationG6Data(props.model);
|
|
|
@@ -277,6 +476,7 @@
|
|
|
} as any);
|
|
|
|
|
|
graphRef.value = graph;
|
|
|
+ bindGraphEvents(graph);
|
|
|
graph.on(GraphEvent.AFTER_TRANSFORM, refreshZoom);
|
|
|
await graph.render();
|
|
|
observeStageSize();
|
|
|
@@ -326,11 +526,24 @@
|
|
|
|
|
|
async function resizeAndFit() {
|
|
|
await nextTick();
|
|
|
- // 等一帧让 autoResize 完成尺寸测量,否则全屏后会按旧尺寸 fit
|
|
|
+ // 等两帧让全屏浮层的 flex 尺寸稳定;autoResize 在浮层切换时可能拿不到最终高度,
|
|
|
+ // 这里再显式 resize 一次,避免画布只铺满上半截。
|
|
|
await new Promise<void>((resolve) => {
|
|
|
- if (typeof requestAnimationFrame === 'function') requestAnimationFrame(() => resolve());
|
|
|
- else window.setTimeout(resolve, 0);
|
|
|
+ if (typeof requestAnimationFrame === 'function') {
|
|
|
+ requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
|
|
|
+ } else {
|
|
|
+ window.setTimeout(resolve, 0);
|
|
|
+ }
|
|
|
});
|
|
|
+ const graph = graphRef.value;
|
|
|
+ const element = stageRef.value;
|
|
|
+ if (graph && element) {
|
|
|
+ try {
|
|
|
+ graph.resize(Math.max(1, element.clientWidth), Math.max(1, element.clientHeight));
|
|
|
+ } catch {
|
|
|
+ /* 忽略尺寸同步异常 */
|
|
|
+ }
|
|
|
+ }
|
|
|
await fitView();
|
|
|
}
|
|
|
|
|
|
@@ -340,15 +553,20 @@
|
|
|
<style lang="less" scoped>
|
|
|
.ai-relation-viewer {
|
|
|
position: relative;
|
|
|
+ display: flex;
|
|
|
+ flex: 1 1 auto;
|
|
|
+ flex-direction: column;
|
|
|
width: 100%;
|
|
|
height: 100%;
|
|
|
- min-height: 280px;
|
|
|
+ min-height: 0;
|
|
|
background: var(--ai-bg-subtle, #f7f8fa);
|
|
|
overflow: hidden;
|
|
|
|
|
|
&__stage {
|
|
|
- position: absolute;
|
|
|
- inset: 0;
|
|
|
+ position: relative;
|
|
|
+ flex: 1 1 auto;
|
|
|
+ width: 100%;
|
|
|
+ min-height: 0;
|
|
|
}
|
|
|
|
|
|
&__toolbar {
|
|
|
@@ -422,6 +640,8 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /* 关系名不放在底部图例:线上已有标签,避免重复堆数据 */
|
|
|
+
|
|
|
&__dot {
|
|
|
display: inline-block;
|
|
|
width: 8px;
|