一文玩转【relation-graph 关系图谱】再也不怕遇到这样的需求

✨ 关于 relation-graph

relation-graph是支持Vue2、Vue3、React的关系数据展示组件,支持通过【插槽】让使用者使用"普通HTML元素、Vue组件、React组件"来完全自定义图形元素,并提供实用的API接口让使用者轻松构建可交互的图形应用。
官网文档:https://www.relation-graph.com/#/index
在这里插入图片描述

✨ 基本使用:(Vue3.0)

第一步:在项目中安装对应的插件

npm install --save relation-graph-vue3

第二步:创建对应SFC组件

<template>
  <div>
    <div style="border: #efefef solid 1px; height: calc(100vh - 100px);width: 100%;">
      <relation-graph ref="graphRef$" :options="options" >
      			<template #node="{node}">
				<div class="node-item" @click="showNodeTips(node, $event)">
					//自定义节点中显示内容
					<div class="node-item-info">
						<div class="node-item-info-item">
							<div class="item-label">所属部门:</div>
							<div class="item-value">部门编号+部门名称</div>
						</div>
						<div class="node-item-info-item">
							<div class="item-label">工作电话:</div>
							<div class="item-value">00-1234 1234</div>
						</div>
						<div class="node-item-info-item">
							<div class="item-label">工作邮箱:</div>
							<div class="item-value">Pall.Lin.123@manpowergrc.com</div>
						</div>
						<div class="node-item-info-item">
							<div class="item-label">下属人数:</div>
							<div class="item-value">198</div>
						</div>
					</div>
				</div>
			</template>
		</relation-graph>
    </div>
  </div>
</template>
<script setup name="RelationGraph">
import RelationGraph from 'relation-graph-vue3'
const options = {
  "backgroundImage": "https://ssl.relation-graph.com/images/relatioon-graph-canvas-bg.png",
  "backgroundImageNoRepeat": true,
  "disableDragNode": true,
  "defaultFocusRootNode": false,
  "disableNodeClickEffect": true,
  "disableLineClickEffect": true,
  "defaultExpandHolderPosition": "bottom",
  "defaultNodeBorderWidth": 1,
  "defaultNodeColor": "#ffffff",
  "defaultNodeBorderColor": "#D8D8D8",
  "defaultNodeFontColor": "#303133",
  "defaultLineColor": "#999",
  "checkedLineColor": null,
  "defaultLineShape": 4,
  "defaultNodeShape": 1,
  "defaultNodeWidth": 340,
  "defaultNodeHeight": 148,
  "defaultJunctionPoint": "tb",
  "layouts": [
    {
      "label": "中心",
      "layoutName": "tree",
      "centerOffset_x": 0,
      "centerOffset_y": 0,
      "distance_coefficient": 1,
      "layoutDirection": "v",
      "from": "top",
      "levelDistance": "",
      "min_per_width": "360",
      "max_per_width": 500,
      "min_per_height": "300",
      "max_per_height": 500
    }
  ]
}
const jsonData = {
    rootId: 'a',
    nodes: [
      { id: 'a', text: 'a', },
      { id: 'b', text: 'b', },
      { id: 'c', text: 'c', },
      { id: 'd', text: 'd', },
      { id: 'e', text: 'e', },
      { id: 'f', text: 'f', },
    ],
    lines: [
      { from: 'a', to: 'b', },
      { from: 'a', to: 'c', },
      { from: 'a', to: 'd', },
      { from: 'a', to: 'e', },
      { from: 'a', to: 'f', },
    ],
  }
//点击当前节点
const showNodeTips = (nodeObject, $event) => {
	isShowNodeTipsPanel.value = true
}
// 点击画布事件
const onCanvasClick = ($event) => {
	isShowNodeTipsPanel.value = false
}
onMounted(()=>{
//初始化画布
	const graphInstance = graphRef.value?.getInstance()
	if (graphInstance) {
			graphInstance.setOptions(options)
			graphInstance.setJsonData(jsonData)
			graphInstance.moveToCenter()
			graphInstance.zoomToFit()
		}
	})

第三步:自定义配置界面
在这里插入图片描述
基本配置json

const options = {
  "backgroundImage": "https://ssl.relation-graph.com/images/relatioon-graph-canvas-bg.png",
  "backgroundImageNoRepeat": true,
  "disableDragNode": true,
  "defaultFocusRootNode": false,
  "disableNodeClickEffect": true,
  "disableLineClickEffect": true,
  "defaultExpandHolderPosition": "bottom",
  "defaultNodeBorderWidth": 1,
  "defaultNodeColor": "#ffffff",
  "defaultNodeBorderColor": "#D8D8D8",
  "defaultNodeFontColor": "#303133",
  "defaultLineColor": "#999",
  "checkedLineColor": null,
  "defaultLineShape": 4,
  "defaultNodeShape": 1,
  "defaultNodeWidth": 340,
  "defaultNodeHeight": 148,
  "defaultJunctionPoint": "tb",
  "layouts": [
    {
      "label": "中心",
      "layoutName": "tree",
      "centerOffset_x": 0,
      "centerOffset_y": 0,
      "distance_coefficient": 1,
      "layoutDirection": "v",
      "from": "top",
      "levelDistance": "",
      "min_per_width": "360",
      "max_per_width": 500,
      "min_per_height": "300",
      "max_per_height": 500
    }
  ]
}

数据格式:

 const jsonData = {
    rootId: 'a',
    nodes: [
      { id: 'a', text: 'a', },
      { id: 'b', text: 'b', },
      { id: 'c', text: 'c', },
      { id: 'd', text: 'd', },
      { id: 'e', text: 'e', },
      { id: 'f', text: 'f', },
    ],
    lines: [
      { from: 'a', to: 'b', },
      { from: 'a', to: 'c', },
      { from: 'a', to: 'd', },
      { from: 'a', to: 'e', },
      { from: 'a', to: 'f', },
    ],
  }

数据赋值:

graphRef$.value.setJsonData(jsonData)

完成这些之后我们就可以得到一个基本的关系图谱
在这里插入图片描述
在这里插入图片描述

✨ 如何自定义图谱结构以及样式

1.配置界面进行可视化配置
在这里插入图片描述

2.完成之后点击此处,copy 出我们的json对象,替换到我们自定义组件中即可
在这里插入图片描述
3.具有丰富的事件与交互供大家使用
在这里插入图片描述
常用的事件:

  1. node-click 节点点击事件
  2. line-click 节点连线点击事件
  3. canvas-click 画布点击事件

4.自定义图表动画效果

在这里插入图片描述

在 Vue 3 中,如果你想要使用 `vue-relation-graph` 库来实现思维导图,并自定义连线(边)的样式,你需要创建一个自定义的 MyLine 组件,并覆盖或扩展默认的样式属性。这个组件通常会处理连接两个节点之间的路径绘制以及相关的样式配置。 以下是一个简单的示例,假设你已经安装了 `vue-relation-graph` 和 `vue-svg-loader`: ```html <!-- 引入MyLine组件 --> <template> <v-relation-graph :nodes="nodes" :links="links"> <!-- 使用组件作为边 --> <my-line v-for="(link, index) in links" :key="index" :source="link.source" :target="link.target" /> </v-relation-graph> </template> <script> import { Component, Prop } from 'vue'; import { Graph, Node, Link } from 'vue-relation-graph'; // 自定义 MyLine 组件 export default class MyLine extends Component { @Prop({ type: [Node, Node[]], required: true }) source!: Node | Node[]; @Prop({ type: [Node, Node[]], required: true }) target!: Node | Node[]; // 可能需要一个 SVG 元素来绘制线条,这里仅作示例,实际应用需引入svg相关库 private svgLine = document.createElementNS('http://www.w3.org/2000/svg', 'path'); mounted() { this.drawLine(); } drawLine() { // 根据需求设置路径数据、颜色、宽度等样式 const pathData = getCustomPath(this.source, this.target); // 获取自定义路径数据 this.svgLine.setAttribute('d', pathData); this.svgLine.style.stroke = 'red'; // 设置线的颜色 this.svgLine.style.strokeWidth = '2px'; // 设置线的粗细 // 将SVG元素添加到渲染区域 // 这里只是一个基本例子,实际应用可能需要将它附加到graph节点上,或者根据需要选择其他位置 this.$el.appendChild(this.svgLine); } } </script> ``` 请注意,这个例子是一个简化的版本,你可能需要根据实际情况调整代码,例如从服务器获取路径数据、使用更复杂的路径生成算法等。同时,`getCustomPath`函数是自定义的一部分,你可以根据需要编写计算路径数据的逻辑。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值