You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
991 lines
30 KiB
991 lines
30 KiB
<template>
|
|
<div class="knowledge-graph-container">
|
|
<Menu
|
|
:initial-active="0"
|
|
/>
|
|
<!-- 控制面板 -->
|
|
<!-- <div class="control-panel">-->
|
|
<!-- <!– 节点控制 –>-->
|
|
<!-- <div class="section">-->
|
|
<!-- <h5>节点控制</h5>-->
|
|
<!-- <label style="text-align: left;margin: 10px 0;display: flex;align-items: center;font-size: 12px;">-->
|
|
<!-- <input v-model="nodeShowLabel" type="checkbox" />-->
|
|
<!-- 显示节点标签-->
|
|
<!-- </label>-->
|
|
<!-- <div class="form-group">-->
|
|
<!-- <label>字体名称:</label>-->
|
|
<!-- <select v-model="nodeFontFamily">-->
|
|
<!-- <!– 中文常用字体 –>-->
|
|
<!-- <option value="Microsoft YaHei, sans-serif">微软雅黑</option>-->
|
|
<!-- <option value="SimSun, serif">宋体(SimSun)</option>-->
|
|
<!-- <option value="SimHei, sans-serif">黑体(SimHei)</option>-->
|
|
<!-- <option value="KaiTi, serif">楷体(KaiTi)</option>-->
|
|
<!-- <option value="FangSong, serif">仿宋(FangSong)</option>-->
|
|
<!-- <option value="PingFang SC, Helvetica Neue, sans-serif">苹方(PingFang SC)</option>-->
|
|
|
|
<!-- <!– 英文/通用字体 –>-->
|
|
<!-- <option value="Arial, sans-serif">Arial</option>-->
|
|
<!-- <option value="Helvetica, sans-serif">Helvetica</option>-->
|
|
<!-- <option value="Times New Roman, serif">Times New Roman</option>-->
|
|
<!-- <option value="Georgia, serif">Georgia</option>-->
|
|
<!-- <option value="Courier New, monospace">Courier New(等宽)</option>-->
|
|
<!-- <option value="Verdana, sans-serif">Verdana</option>-->
|
|
<!-- <option value="Tahoma, sans-serif">Tahoma</option>-->
|
|
<!-- <option value="Impact, sans-serif">Impact</option>-->
|
|
<!-- </select>-->
|
|
<!-- </div>-->
|
|
<!-- <div class="slider-group">-->
|
|
<!-- <span>字体大小: {{ nodeFontSize }}px</span>-->
|
|
<!-- <input-->
|
|
<!-- v-model.number="nodeFontSize"-->
|
|
<!-- type="range"-->
|
|
<!-- min="10"-->
|
|
<!-- max="24"-->
|
|
<!-- step="1"-->
|
|
<!-- />-->
|
|
<!-- </div>-->
|
|
|
|
<!-- <div class="color-picker">-->
|
|
<!-- <label>字体颜色:</label>-->
|
|
<!-- <input v-model="nodeFontColor" type="color" />-->
|
|
<!-- </div>-->
|
|
<!-- <div class="form-group">-->
|
|
<!-- <label>图形:</label>-->
|
|
<!-- <select v-model="nodeShape">-->
|
|
<!-- <option value="circle">圆形</option>-->
|
|
<!-- <option value="diamond">菱形</option>-->
|
|
<!-- <option value="triangle">三角形</option>-->
|
|
<!-- <option value="rect">矩形</option>-->
|
|
<!-- <option value="star">星形</option>-->
|
|
<!-- <option value="hexagon">六边形</option>-->
|
|
<!-- </select>-->
|
|
<!-- </div>-->
|
|
<!-- <div class="form-group">-->
|
|
<!-- <label>尺寸:</label>-->
|
|
<!-- <input v-model.number="nodeSize" type="number" min="30" max="100" />-->
|
|
<!-- </div>-->
|
|
<!-- <div class="color-picker">-->
|
|
<!-- <label>填充颜色:</label>-->
|
|
<!-- <input v-model="nodeFill" type="color" />-->
|
|
<!-- </div>-->
|
|
<!-- <div class="color-picker">-->
|
|
<!-- <label>边框颜色:</label>-->
|
|
<!-- <input v-model="nodeStroke" type="color" />-->
|
|
<!-- </div>-->
|
|
<!-- <div class="form-group">-->
|
|
<!-- <label>边框尺寸:</label>-->
|
|
<!-- <input v-model.number="nodeLineWidth" type="number" min="1" max="5" />-->
|
|
<!-- </div>-->
|
|
<!-- </div>-->
|
|
|
|
<!-- <!– 连边控制 –>-->
|
|
<!-- <div class="section">-->
|
|
<!-- <h5>连边控制</h5>-->
|
|
<!-- <label style="text-align: left;margin: 10px 0;display: flex;align-items: center;font-size: 12px;">-->
|
|
<!-- <input v-model="edgeShowLabel" type="checkbox" />-->
|
|
<!-- 显示连边标签-->
|
|
<!-- <input v-model="edgeEndArrow" type="checkbox" style="margin-left: 10px"/>-->
|
|
<!-- 显示端点箭头-->
|
|
<!-- </label>-->
|
|
<!-- <div class="form-group">-->
|
|
<!-- <label>字体名称:</label>-->
|
|
<!-- <select v-model="edgeFontFamily">-->
|
|
<!-- <option value="Microsoft YaHei, sans-serif">微软雅黑</option>-->
|
|
<!-- <option value="SimSun, serif">宋体</option>-->
|
|
<!-- <option value="SimHei, sans-serif">黑体</option>-->
|
|
<!-- <option value="Arial, sans-serif">Arial</option>-->
|
|
<!-- <option value="sans-serif">无衬线(默认)</option>-->
|
|
<!-- </select>-->
|
|
<!-- </div>-->
|
|
<!-- <div class="slider-group">-->
|
|
<!-- <span>字体大小: {{ edgeFontSize }}px</span>-->
|
|
<!-- <input-->
|
|
<!-- v-model.number="edgeFontSize"-->
|
|
<!-- type="range"-->
|
|
<!-- min="8"-->
|
|
<!-- max="16"-->
|
|
<!-- step="1"-->
|
|
<!-- />-->
|
|
<!-- </div>-->
|
|
<!-- <div class="color-picker">-->
|
|
<!-- <label>字体颜色:</label>-->
|
|
<!-- <input v-model="edgeFontColor" type="color" />-->
|
|
<!-- </div>-->
|
|
<!-- <div class="form-group">-->
|
|
<!-- <label>连边类型:</label>-->
|
|
<!-- <select v-model="edgeType">-->
|
|
<!-- <option value="line">直线</option>-->
|
|
<!-- <option value="polyline">折线边</option>-->
|
|
<!-- <option value="cubic">三次贝塞尔曲线边</option>-->
|
|
<!-- <option value="cubic-horizontal">水平三次贝塞尔曲线边</option>-->
|
|
<!-- <option value="cubic-vertical">垂直三次贝塞尔曲线边</option>-->
|
|
<!-- <option value="quadratic">二次贝塞尔曲线边</option>-->
|
|
|
|
<!-- </select>-->
|
|
<!-- </div>-->
|
|
<!-- <div class="form-group">-->
|
|
<!-- <label>线粗细:</label>-->
|
|
<!-- <input v-model.number="edgeLineWidth" type="number" min="1" max="5" />-->
|
|
<!-- </div>-->
|
|
<!-- <div class="color-picker">-->
|
|
<!-- <label>线条颜色:</label>-->
|
|
<!-- <input v-model="edgeStroke" type="color" />-->
|
|
<!-- </div>-->
|
|
<!--<!– <label>–>-->
|
|
<!--<!– <input v-model="edgeEndArrow" type="checkbox" />–>-->
|
|
<!--<!– 显示端点箭头–>-->
|
|
<!--<!– </label>–>-->
|
|
<!-- </div>-->
|
|
<!-- <button @click="resetView">重置视图</button>-->
|
|
<!-- <button @click="resetStyle">重置样式</button>-->
|
|
<!-- <!– 操作按钮 –>-->
|
|
|
|
<!-- </div>-->
|
|
|
|
|
|
|
|
<div class="icd10-tree-container" style="overflow: scroll;width: 25%">
|
|
<div>
|
|
<el-radio-group v-model="typeRadio" @change="changeTree">
|
|
<el-radio value="Disease">疾病</el-radio>
|
|
<el-radio value="Drug">药品</el-radio>
|
|
<el-radio value="Check">检查</el-radio>
|
|
</el-radio-group>
|
|
</div>
|
|
<div>
|
|
<el-tree
|
|
:data="treeData"
|
|
:props="treeProps"
|
|
:expand-on-click-node="false"
|
|
@node-click="handleNodeClick"
|
|
>
|
|
<template #default="{ node, data }">
|
|
<span class="custom-tree-node">
|
|
<!-- <el-tag size="small" :type="getTagType(data.level)" class="level-tag">-->
|
|
<!-- -->
|
|
<!-- </el-tag>-->
|
|
<!-- {{ getLevelLabel(data.level) }}-->
|
|
<span class="code">{{ data.code }}</span>
|
|
<span class="label">{{ data.label }}</span>
|
|
</span>
|
|
</template>
|
|
</el-tree>
|
|
</div>
|
|
</div>
|
|
<div style="width: 75%">
|
|
<div style="display: flex; gap: 10px; margin-bottom: 10px;">
|
|
<!-- 图例项 -->
|
|
<div v-for="(item, index) in legendItems" :key="index" class="legend-item">
|
|
<div
|
|
:style="{
|
|
backgroundColor: item.color,
|
|
opacity: visibleCategories.has(item.key) ? 1 : 0.3 // ✅ 关键:根据状态调整透明度
|
|
}"
|
|
class="color-block"
|
|
@click="toggleCategory(item.key)"
|
|
></div>
|
|
<span>{{ item.label }}</span>
|
|
</div>
|
|
</div>
|
|
<div ref="graphContainer" class="graph-container" id="container"></div>
|
|
</div>
|
|
<!-- 图谱容器 -->
|
|
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import {getCheckTree, getDrugTree, getGraph, getTestGraphData} from "@/api/graph"
|
|
import { Graph } from '@antv/g6';
|
|
import Menu from "@/components/Menu.vue";
|
|
import {a} from "vue-router/dist/devtools-EWN81iOl.mjs";
|
|
|
|
export default {
|
|
name: 'GraphDemo',
|
|
components: {Menu},
|
|
data() {
|
|
return {
|
|
G6: null, // 添加这个
|
|
// 节点样式
|
|
nodeShowLabel: true,
|
|
nodeFontSize: 12,
|
|
nodeFontColor: '#fff',
|
|
nodeShape: 'circle',
|
|
nodeSize: 50,
|
|
nodeFill: '#9FD5FF',
|
|
nodeStroke: '#5B8FF9',
|
|
nodeLineWidth: 2,
|
|
nodeFontFamily: 'Microsoft YaHei, sans-serif',
|
|
|
|
// 边样式
|
|
edgeShowLabel: true,
|
|
edgeFontSize: 10,
|
|
edgeFontColor: '#666666',
|
|
edgeType: 'quadratic',
|
|
edgeLineWidth: 2,
|
|
edgeStroke: '#b6b2b2',
|
|
edgeEndArrow: true,
|
|
edgeFontFamily: 'Microsoft YaHei, sans-serif',
|
|
|
|
defaultData: {
|
|
nodes: [
|
|
{id: 'A', label: '人工智能'},
|
|
{id: 'B', label: '机器学习'},
|
|
{id: 'C', label: '深度学习'}
|
|
],
|
|
edges: [
|
|
{source: 'A', target: 'B', label: '包含'},
|
|
{source: 'B', target: 'C', label: '子领域'}
|
|
]
|
|
},
|
|
treeData: [],
|
|
treeProps: {
|
|
children: 'children',
|
|
label: 'title' // 虽然用插槽,但 el-tree 内部仍会读取,可留空或任意
|
|
},
|
|
typeRadio:"Disease",
|
|
drugTree:[],
|
|
diseaseTree:[],
|
|
checkTree:[],
|
|
legendItems: [
|
|
{ key: 'Drug', label: '药品', color: '#91cc75' },
|
|
{ key: 'Symptom', label: '症状', color: '#fac858' },
|
|
{ key: 'Disease', label: '疾病', color: '#EF4444' },
|
|
{ key: 'Check', label: '检查', color: '#59d1d4' },
|
|
{ key: 'Other', label: '其他', color: '#336eee' }
|
|
],
|
|
visibleCategories: new Set(), // 先空着
|
|
|
|
}
|
|
},
|
|
|
|
async mounted() {
|
|
this.visibleCategories = new Set(this.legendItems.map(i => i.key));
|
|
await this.loadDiseaseTreeData()
|
|
this.treeData=this.diseaseTree
|
|
await this.$nextTick();
|
|
try {
|
|
const response = await getTestGraphData(); // 等待 Promise 解析
|
|
const updatedNodes = response.nodes.map(node => ({
|
|
...node,
|
|
type: this.nodeShape,
|
|
style:{
|
|
size: this.nodeSize,
|
|
fill: this.nodeFill,
|
|
stroke: this.nodeStroke,
|
|
lineWidth: this.nodeLineWidth,
|
|
label: this.nodeShowLabel,
|
|
labelFontSize: this.nodeFontSize,
|
|
labelFontFamily: this.nodeFontFamily,
|
|
labelFill: this.nodeFontColor,
|
|
}
|
|
}))
|
|
const updatedEdges = response.edges.map(edge => ({
|
|
...edge,
|
|
id: edge.data.relationship.id,
|
|
type: this.edgeType,
|
|
style: {
|
|
endArrow: this.edgeEndArrow,
|
|
stroke: this.edgeStroke,
|
|
lineWidth: this.edgeLineWidth,
|
|
label: this.edgeShowLabel,
|
|
labelFontSize: this.edgeFontSize,
|
|
labelFontFamily: this.edgeFontFamily,
|
|
labelFill: this.edgeFontColor,
|
|
},
|
|
|
|
}))
|
|
const updatedData = {
|
|
nodes: updatedNodes,
|
|
edges: updatedEdges
|
|
}
|
|
this.defaultData = updatedData
|
|
setTimeout(() => {
|
|
this.initGraph();
|
|
this.loadDrugTreeData()
|
|
this.loadCheckTreeData()
|
|
this.buildCategoryIndex();
|
|
window.addEventListener('resize', this.handleResize);
|
|
}, 1000);
|
|
} catch (error) {
|
|
console.error('加载图谱数据失败:', error);
|
|
}
|
|
|
|
},
|
|
beforeUnmount() {
|
|
this._graph.destroy()
|
|
this._graph = null;
|
|
window.removeEventListener('resize', this.handleResize);
|
|
},
|
|
watch: {
|
|
// 节点相关
|
|
nodeShowLabel: 'updateAllNodes',
|
|
nodeFontSize: 'updateAllNodes',
|
|
nodeFontColor: 'updateAllNodes',
|
|
nodeShape: 'updateAllNodes',
|
|
nodeSize: 'updateAllNodes',
|
|
nodeFill: 'updateAllNodes',
|
|
nodeStroke: 'updateAllNodes',
|
|
nodeLineWidth: 'updateAllNodes',
|
|
nodeFontFamily: 'updateAllNodes',
|
|
|
|
// 边相关
|
|
edgeShowLabel: 'updateAllEdges',
|
|
edgeFontSize: 'updateAllEdges',
|
|
edgeFontColor: 'updateAllEdges',
|
|
edgeType: 'updateAllEdges',
|
|
edgeLineWidth: 'updateAllEdges',
|
|
edgeStroke: 'updateAllEdges',
|
|
edgeEndArrow: 'updateAllEdges',
|
|
edgeFontFamily: 'updateAllEdges',
|
|
},
|
|
methods: {
|
|
buildCategoryIndex() {
|
|
const index = {};
|
|
const nodes = this._graph.getNodeData() // 获取所有节点
|
|
nodes.forEach(node => {
|
|
console.log(node.data.label)
|
|
const category = node.data.label; // 假设 label 是类别
|
|
if(category=='Drug'||category=='Symptom'||
|
|
category=='Disease'||category=='Check'){
|
|
if (!index[category]) index[category] = [];
|
|
index[category].push(node.id);
|
|
}else{
|
|
if (!index["Other"]) index["Other"] = [];
|
|
index["Other"].push(node.id);
|
|
}
|
|
});
|
|
this.categoryToNodeIds = index;
|
|
},
|
|
// 切换某个类别的显示状态
|
|
toggleCategory (key){
|
|
if (this.visibleCategories.has(key)) {
|
|
this.visibleCategories.delete(key)
|
|
} else {
|
|
this.visibleCategories.add(key)
|
|
}
|
|
this.updateGraphFilter()
|
|
},
|
|
|
|
// 更新 G6 图的过滤规则
|
|
updateGraphFilter() {
|
|
console.log('visibleCategories:', this.visibleCategories);
|
|
|
|
// Step 1: 收集所有当前应该显示的节点 ID(扁平化)
|
|
const visibleNodeIds = new Set();
|
|
for (const [category, nodeIds] of Object.entries(this.categoryToNodeIds)) {
|
|
if (this.visibleCategories.has(category)) {
|
|
nodeIds.forEach(id => visibleNodeIds.add(id));
|
|
}
|
|
}
|
|
|
|
// Step 2: 更新节点的显示状态
|
|
for (const [category, nodeIds] of Object.entries(this.categoryToNodeIds)) {
|
|
if (this.visibleCategories.has(category)) {
|
|
this._graph.showElement(nodeIds, true);
|
|
} else {
|
|
this._graph.hideElement(nodeIds, true);
|
|
}
|
|
}
|
|
|
|
// Step 3: 处理边的显示/隐藏
|
|
const edges = this._graph.getEdgeData(); // 获取所有边数据
|
|
const edgesToShow = [];
|
|
const edgesToHide = [];
|
|
|
|
edges.forEach(edge => {
|
|
const sourceVisible = visibleNodeIds.has(edge.source);
|
|
const targetVisible = visibleNodeIds.has(edge.target);
|
|
|
|
// 只有当 source 和 target 都可见时,才显示这条边
|
|
if (sourceVisible && targetVisible) {
|
|
edgesToShow.push(edge.id);
|
|
} else {
|
|
edgesToHide.push(edge.id);
|
|
}
|
|
});
|
|
|
|
if (edgesToShow.length > 0) {
|
|
this._graph.showElement(edgesToShow, true);
|
|
}
|
|
if (edgesToHide.length > 0) {
|
|
this._graph.hideElement(edgesToHide, true);
|
|
}
|
|
},
|
|
|
|
changeTree(){
|
|
if(this.typeRadio=="Disease"){
|
|
this.treeData=this.diseaseTree
|
|
}
|
|
if(this.typeRadio=="Drug") {
|
|
this.treeData=this.drugTree
|
|
}
|
|
if(this.typeRadio=="Check") {
|
|
this.treeData=this.checkTree
|
|
}
|
|
},
|
|
async loadDiseaseTreeData() {
|
|
try {
|
|
const res = await fetch('/icd10.json')
|
|
if (!res.ok) throw new Error('Failed to load JSON')
|
|
this.diseaseTree = await res.json()
|
|
console.log(this.treeData)
|
|
} catch (error) {
|
|
console.error('加载 ICD-10 数据失败:', error)
|
|
this.$message.error('加载编码数据失败,请检查文件路径')
|
|
}
|
|
},
|
|
async loadDrugTreeData() {
|
|
try {
|
|
const res = await getDrugTree()
|
|
this.drugTree = res
|
|
console.log(this.drugTree)
|
|
} catch (error) {
|
|
}
|
|
},
|
|
async loadCheckTreeData() {
|
|
try {
|
|
const res = await getCheckTree()
|
|
this.checkTree = res
|
|
console.log(this.checkTree)
|
|
} catch (error) {
|
|
}
|
|
},
|
|
async handleNodeClick(data) {
|
|
console.log('点击节点:', data)
|
|
// 可用于显示详情、复制 code 等
|
|
if(data.type=="Drug"||data.type=="Check"){
|
|
const response = await getGraph(data); // 等待 Promise 解析
|
|
this.formatData(response)
|
|
}
|
|
if(data.level=="category"||
|
|
data.level=="subcategory"||
|
|
data.level=="diagnosis"){
|
|
data.type="Disease"
|
|
const response = await getGraph(data); // 等待 Promise 解析
|
|
this.formatData(response)
|
|
}
|
|
},
|
|
buildNodeLabelMap(nodes) {
|
|
this._nodeLabelMap = new Map();
|
|
nodes.forEach(node => {
|
|
this._nodeLabelMap.set(node.id, node.data?.label || 'default');
|
|
});
|
|
},
|
|
clearGraphState() {
|
|
if (!this._graph) return;
|
|
|
|
// 1. 清除所有节点和边的状态
|
|
this._graph.getNodeData().forEach(node => {
|
|
this._graph.setElementState(node.id,[]);
|
|
|
|
});
|
|
this._graph.getEdgeData().forEach(edge => {
|
|
this._graph.setElementState(edge.id,[]);
|
|
});
|
|
|
|
// 2. (可选)取消所有 pending 的交互
|
|
// 比如如果你有高亮定时器,这里 clearTimeout
|
|
},
|
|
formatData(data){
|
|
this._graph.stopLayout();
|
|
this.clearGraphState();
|
|
// data.nodes = data.nodes.slice(0, 1000);
|
|
// data.edges = data.edges.slice(0, 0);
|
|
const updatedEdges = data.edges.map(edge => ({
|
|
...edge,
|
|
type: this.edgeType,
|
|
style: {
|
|
endArrow: this.edgeEndArrow,
|
|
stroke: this.edgeStroke,
|
|
lineWidth: this.edgeLineWidth,
|
|
label: this.edgeShowLabel,
|
|
labelFontSize: this.edgeFontSize,
|
|
labelFontFamily: this.edgeFontFamily,
|
|
labelFill: this.edgeFontColor,
|
|
|
|
},
|
|
}))
|
|
const updatedNodes = data.nodes.map(node => ({
|
|
...node,
|
|
type: this.nodeShape,
|
|
style:{
|
|
size: this.nodeSize,
|
|
lineWidth: this.nodeLineWidth,
|
|
label: this.nodeShowLabel,
|
|
labelFontSize: this.nodeFontSize,
|
|
labelFontFamily: this.nodeFontFamily,
|
|
labelFill: this.nodeFontColor,
|
|
opacity: 1,
|
|
}
|
|
}))
|
|
const updatedData = {
|
|
nodes: updatedNodes,
|
|
edges: updatedEdges
|
|
}
|
|
this.buildNodeLabelMap(updatedNodes);
|
|
this.updateGraph(updatedData)
|
|
},
|
|
getLevelLabel(level) {
|
|
const map = {
|
|
chapter: '章',
|
|
section: '节',
|
|
category: '类目',
|
|
subcategory: '亚目',
|
|
diagnosis: '条目'
|
|
}
|
|
return map[level] || level
|
|
},
|
|
|
|
getTagType(level) {
|
|
const map = {
|
|
chapter: 'primary',
|
|
section: 'success',
|
|
category: 'warning',
|
|
subcategory: 'info',
|
|
diagnosis: 'info'
|
|
}
|
|
return map[level] || ''
|
|
},
|
|
initGraph() {
|
|
|
|
if (this._graph!=null){
|
|
this._graph.destroy()
|
|
this._graph = null;
|
|
}
|
|
if (!this._nodeLabelMap) {
|
|
this.buildNodeLabelMap(this.defaultData.nodes);
|
|
}
|
|
console.log(this.defaultData)
|
|
console.log(this._nodeLabelMap)
|
|
const container = this.$refs.graphContainer;
|
|
const width = container.clientWidth || 800;
|
|
const height = container.clientHeight || 600;
|
|
console.log(width)
|
|
console.log(height)
|
|
const graph = new Graph({
|
|
container,
|
|
width,
|
|
height,
|
|
layout: {
|
|
type: 'force', // 力导向布局
|
|
gravity: 0.3, // 重力系数,控制节点聚集程度
|
|
repulsion: 500, // 排斥力
|
|
attraction: 20, // 吸引力
|
|
preventOverlap: true // 防止节点重叠
|
|
// type: 'radial',
|
|
// preventOverlap: true,
|
|
// unitRadius: 200,
|
|
// maxPreventOverlapIteration:100
|
|
// type: 'force-atlas2',
|
|
// preventOverlap: true,
|
|
// kr: 1000,
|
|
// center: [250, 250],
|
|
// barnesHut:true,
|
|
|
|
},
|
|
behaviors: [ 'zoom-canvas', 'drag-element',
|
|
'click-select','focus-element', {
|
|
type: 'hover-activate',
|
|
degree: 1,
|
|
},
|
|
{
|
|
type: 'drag-canvas',
|
|
enable: (event) => event.shiftKey === false,
|
|
},
|
|
{
|
|
type: 'brush-select',
|
|
},
|
|
],
|
|
|
|
node: {
|
|
style: {
|
|
fill: (d) => {
|
|
const label = d.data?.label;
|
|
if (label === 'Disease') return '#EF4444'; // 红
|
|
if (label === 'Drug') return '#91cc75'; // 绿
|
|
if (label === 'Symptom') return '#fac858'; // 橙
|
|
if (label === 'Check') return '#59d1d4'; // 橙
|
|
return '#336eee'; // 默认灰蓝
|
|
},
|
|
stroke: (d) => {
|
|
const label = d.data?.label;
|
|
if (label === 'Disease') return '#B91C1C';
|
|
if (label === 'Drug') return '#047857';
|
|
if (label === 'Check') return '#40999b'; // 橙
|
|
if (label === 'Symptom') return '#B45309';
|
|
return '#1D4ED8';
|
|
},
|
|
labelText: (d) => d.data.name,
|
|
labelPlacement: 'center',
|
|
labelWordWrap: true,
|
|
labelMaxWidth: '150%',
|
|
labelMaxLines: 3,
|
|
labelTextOverflow: 'ellipsis',
|
|
labelTextAlign: 'center',
|
|
opacity: 1
|
|
},
|
|
state: {
|
|
active: {
|
|
lineWidth: 2,
|
|
shadowColor: '#ffffff',
|
|
shadowBlur: 10,
|
|
opacity: 1
|
|
},
|
|
inactive: {
|
|
opacity: 0.3
|
|
},
|
|
normal:{
|
|
opacity: 1
|
|
}
|
|
|
|
|
|
},
|
|
},
|
|
edge: {
|
|
style: {
|
|
labelText: (d) => d.data.relationship.properties.label,
|
|
stroke: (d) => {
|
|
// 获取 target 节点的 label
|
|
const targetLabel = this._nodeLabelMap.get(d.target); // d.target 是目标节点 ID
|
|
// 根据 target 节点类型返回对应浅色
|
|
if (targetLabel === 'Disease') return 'rgba(239,68,68,0.5)';
|
|
if (targetLabel === 'Drug') return 'rgba(145,204,117,0.5)';
|
|
if (targetLabel === 'Symptom') return 'rgba(250,200,88,0.5)';
|
|
if (targetLabel === 'Check') return 'rgba(89,209,212,0.5)'; // 橙
|
|
return 'rgba(51,110,238,0.5)'; // default
|
|
},
|
|
// labelFill: (d) => {
|
|
// // 获取 target 节点的 label
|
|
// const targetLabel = this._nodeLabelMap.get(d.target); // d.target 是目标节点 ID
|
|
// // 根据 target 节点类型返回对应浅色
|
|
//
|
|
// if (targetLabel === 'Disease') return '#ff4444';
|
|
// if (targetLabel === 'Drug') return '#2f9b70';
|
|
// if (targetLabel === 'Symptom') return '#f89775';
|
|
// return '#6b91ff'; // default
|
|
// }
|
|
|
|
},
|
|
state: {
|
|
selected: {
|
|
stroke: '#1890FF',
|
|
lineWidth: 2,
|
|
},
|
|
highlight: {
|
|
halo: true,
|
|
haloStroke: '#1890FF',
|
|
haloLineWidth: 6,
|
|
haloStrokeOpacity: 0.3,
|
|
lineWidth: 3,
|
|
opacity: 1
|
|
},
|
|
inactive: {
|
|
opacity: 0.3
|
|
},
|
|
normal:{
|
|
opacity: 1
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
data:this.defaultData,
|
|
|
|
|
|
});
|
|
|
|
graph.render();
|
|
// graph.on('node:pointerover', (evt) => {
|
|
// const nodeItem = evt.target; // 获取当前鼠标进入的节点元素
|
|
// const relatedEdges = graph.getRelatedEdgesData(nodeItem.id);
|
|
// const relatedEdgeIds = relatedEdges.map(edge => edge.id);
|
|
//
|
|
// // 3. 高亮这些边(比如用 'highlight' 状态)
|
|
// relatedEdgeIds.forEach(edgeId => {
|
|
// graph.setElementState(edgeId, 'highlight', true);
|
|
// });
|
|
// graph.setElementState(nodeItem.id, 'active',true);
|
|
//
|
|
// // 2. 获取邻居节点 ID(去重)
|
|
// const neighborNodeIds = new Set();
|
|
// relatedEdges.forEach(edge => {
|
|
// if (edge.source !== nodeItem.id) neighborNodeIds.add(edge.source);
|
|
// if (edge.target !== nodeItem.id) neighborNodeIds.add(edge.target);
|
|
// });
|
|
//
|
|
// neighborNodeIds.forEach(id => {
|
|
// graph.setElementState(id, 'active', true);
|
|
// });
|
|
//
|
|
// graph.getEdgeData().forEach(edge => {
|
|
// if (!relatedEdgeIds.includes(edge.id)) {
|
|
// graph.setElementState(edge.id, 'inactive', true);
|
|
// }
|
|
// });
|
|
// graph.getNodeData().forEach(node => {
|
|
// if (node.id !== nodeItem.id && !neighborNodeIds.has(node.id)) {
|
|
// graph.setElementState(node.id, 'inactive',true);
|
|
// }
|
|
// });
|
|
// });
|
|
// graph.on('node:pointerleave', (evt) => {
|
|
// graph.getEdgeData().forEach(edge => {
|
|
// graph.setElementState(edge.id, 'highlight', false);
|
|
// graph.setElementState(edge.id, 'inactive', false);
|
|
// graph.setElementState(edge.id, 'normal', true);
|
|
// });
|
|
// graph.getNodeData().forEach(node => {
|
|
// graph.setElementState(node.id, 'active', false);
|
|
// graph.setElementState(node.id, 'inactive', false);
|
|
// graph.setElementState(node.id, 'normal', true);
|
|
// });
|
|
// });
|
|
graph.on('node:click', (evt) => {
|
|
const nodeItem = evt.target.id; // 获取当前鼠标进入的节点元素
|
|
let node=graph.getNodeData(nodeItem).data
|
|
let data={
|
|
label:node.name,
|
|
type:node.label
|
|
}
|
|
getGraph(data).then(response=>{
|
|
console.log(response)
|
|
this.formatData(response)
|
|
}); // 等待 Promise 解析
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this._graph = graph
|
|
this._graph?.fitView()
|
|
|
|
},
|
|
|
|
updateGraph(data) {
|
|
if (!this._graph) return
|
|
|
|
|
|
this._graph.setData(data)
|
|
this._graph.render()
|
|
},
|
|
|
|
updateAllNodes() {
|
|
if (!this._graph) return
|
|
const updatedNodes = this.defaultData.nodes.map(node => ({
|
|
...node,
|
|
type: this.nodeShape,
|
|
style:{
|
|
size: this.nodeSize,
|
|
fill: this.nodeFill,
|
|
stroke: this.nodeStroke,
|
|
lineWidth: this.nodeLineWidth,
|
|
label: this.nodeShowLabel,
|
|
labelFontSize: this.nodeFontSize,
|
|
labelFontFamily: this.nodeFontFamily,
|
|
labelFontColor: this.nodeFontColor,
|
|
}
|
|
}))
|
|
const updatedData = {
|
|
nodes: updatedNodes,
|
|
edges: this.defaultData.edges
|
|
}
|
|
this.defaultData = updatedData
|
|
this.updateGraph(updatedData)
|
|
},
|
|
|
|
updateAllEdges() {
|
|
if (!this._graph) return
|
|
const updatedEdges = this.defaultData.edges.map(edge => ({
|
|
...edge,
|
|
type:this.edgeType,
|
|
style: {
|
|
endArrow: this.edgeEndArrow,
|
|
stroke: this.edgeStroke,
|
|
lineWidth: this.edgeLineWidth,
|
|
label: this.edgeShowLabel,
|
|
labelFontSize: this.edgeFontSize,
|
|
labelFontFamily: this.edgeFontFamily,
|
|
labelFill: this.edgeFontColor,
|
|
|
|
},
|
|
|
|
}))
|
|
const updatedData = {
|
|
nodes: this.defaultData.nodes,
|
|
edges: updatedEdges
|
|
}
|
|
this.defaultData = updatedData
|
|
this.updateGraph(updatedData)
|
|
},
|
|
|
|
clearAllStates() {
|
|
if (!this._graph) return
|
|
|
|
// 清除所有节点的状态
|
|
this._graph.getNodes().forEach(node => {
|
|
this._graph.clearItemStates(node)
|
|
})
|
|
|
|
// 清除所有边的状态
|
|
this._graph.getEdges().forEach(edge => {
|
|
this._graph.clearItemStates(edge)
|
|
})
|
|
},
|
|
|
|
handleResize() {
|
|
const container = this.$refs.graphContainer
|
|
if (this._graph && container) {
|
|
this._graph.resize(container.offsetWidth, container.offsetHeight)
|
|
}
|
|
},
|
|
|
|
resetView() {
|
|
this._graph?.fitView()
|
|
},
|
|
|
|
resetStyle() {
|
|
Object.assign(this, {
|
|
nodeShowLabel: true,
|
|
nodeFontSize: 12,
|
|
nodeFontColor: '#000000',
|
|
nodeShape: 'circle',
|
|
nodeSize: 60,
|
|
nodeFill: '#9FD5FF',
|
|
nodeStroke: '#5B8FF9',
|
|
nodeLineWidth: 2,
|
|
nodeFontFamily: 'Microsoft YaHei, sans-serif',
|
|
|
|
edgeShowLabel: true,
|
|
edgeFontSize: 10,
|
|
edgeFontColor: '#666666',
|
|
edgeType: 'line',
|
|
edgeLineWidth: 2,
|
|
edgeStroke: '#F00',
|
|
edgeEndArrow: true,
|
|
edgeFontFamily: 'Microsoft YaHei, sans-serif'
|
|
})
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style scoped>
|
|
.knowledge-graph-container {
|
|
display: flex;
|
|
height: 100vh;
|
|
}
|
|
|
|
.control-panel {
|
|
width: 280px;
|
|
background: #f5f7fa;
|
|
padding: 16px;
|
|
border-right: 1px solid #ddd;
|
|
box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
|
|
font-family: 'Microsoft YaHei';
|
|
}
|
|
|
|
.section {
|
|
margin-bottom: 10px;
|
|
padding: 10px;
|
|
background: white;
|
|
border-radius: 8px;
|
|
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
|
|
}
|
|
|
|
.section h5 {
|
|
margin-top: 0;
|
|
color: #333;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.slider-group {
|
|
margin: 10px 0;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.slider-group span {
|
|
margin-right: 10px;
|
|
font-size: 12px;
|
|
}
|
|
|
|
.form-group {
|
|
margin: 10px 0;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.form-group label {
|
|
margin-right: 8px;
|
|
font-size: 12px;
|
|
}
|
|
|
|
.form-group select,
|
|
.form-group input[type="number"] {
|
|
padding: 4px;
|
|
border: 1px solid #ccc;
|
|
border-radius: 4px;
|
|
font-size: 12px;
|
|
}
|
|
|
|
.color-picker {
|
|
margin: 10px 0;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.color-picker label {
|
|
margin-right: 8px;
|
|
font-size: 12px;
|
|
}
|
|
|
|
.color-picker input[type="color"] {
|
|
width: 40px;
|
|
height: 20px;
|
|
border: none;
|
|
cursor: pointer;
|
|
}
|
|
|
|
button {
|
|
width: 45%;
|
|
padding: 10px;
|
|
background: #007bff;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
cursor: pointer;
|
|
margin-right: 2%;
|
|
}
|
|
|
|
button:hover {
|
|
background: #0056b3;
|
|
}
|
|
|
|
.graph-container {
|
|
flex: 1;
|
|
border: 1px solid #eee;
|
|
background: #fff;
|
|
}
|
|
|
|
.legend-item {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 5px;
|
|
cursor: pointer;
|
|
user-select: none;
|
|
}
|
|
|
|
.color-block {
|
|
width: 16px;
|
|
height: 16px;
|
|
border-radius: 4px;
|
|
transition: all 0.2s ease;
|
|
}
|
|
|
|
.legend-item:hover {
|
|
opacity: 0.8;
|
|
}
|
|
</style>
|