跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客我的书AI学习GitHub 精选镜像AI工具UI配色美学关于
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
JavaScript大前端

前端关系图谱组件 relation-graph 实战指南

relation-graph 是一款支持 Vue2、Vue3 及 React 框架的开源关系图谱组件,具备开箱即用、配置灵活、文档完善等特点。本文详细阐述了三种主流框架下的安装步骤与基础用法,并通过实际项目案例演示了如何利用插槽自定义节点样式与布局。内容涵盖数据结构定义、事件处理及样式定制,适合需要快速实现关系网络可视化的前端开发者参考。

WenxuanMa发布于 2026/4/7更新于 2026/10/2291 浏览
前端关系图谱组件 relation-graph 实战指南

前端关系图谱组件 relation-graph 实战指南

relation-graph 是一款开源免费的关系图谱组件,支持 Vue2、Vue3 和 React 框架。它提供开箱即用的体验,配置简单,文档清晰,并支持通过插槽自定义节点样式。该组件具有高度可定制性,支持节点点击事件等交互功能,适用于构建各种关系图谱应用。

快速开始

1. Vue2 使用

安装
npm install --save relation-graph
代码示例

在 Vue 组件中引入并使用:

<template>
  <div>
    <RelationGraph 
      ref="graphRef" 
      :options="graphOptions" 
      :on-node-click="onNodeClick" 
      :on-line-click="onLineClick" 
    />
  </div>
</template>

<script>
import RelationGraph from 'relation-graph'

export default {
  name: 'Demo',
  components: { RelationGraph },
  data() {
    return {
      graphOptions: {
        defaultJunctionPoint: 'border'
      }
    }
  },
  mounted() {
    this.showGraph()
  },
  methods: {
    showGraph() {
      const jsonData = {
        rootId: 'a',
        nodes: [
          { id: 'a', text: 'A', borderColor: 'yellow' },
          { id: 'b', text: 'B', color: '#43a2f1', fontColor: 'yellow' },
          { id: 'c', text: 'C', nodeShape: 1, width: 80, height: 60 },
          { id: 'e', text: 'E', nodeShape: 0, width: 150, height: 150 }
        ],
        lines: [
          { from: 'a', to: 'b', text: '关系 1', color: '#43a2f1' },
          { from: 'a', to: 'c', text: '关系 2' },
          { from: 'a', to: 'e', text: '关系 3' },
          { from: 'b', to: 'e', color: '#67C23A' }
        ]
      }
      this.$refs.graphRef.setJsonData(jsonData)
    },
    onNodeClick(nodeObject, $event) {
      console.log('onNodeClick:', nodeObject)
    },
    onLineClick(lineObject, $event) {
      console.log('onLineClick:', lineObject)
    }
  }
}
</script>

2. Vue3 使用

安装
npm install --save relation-graph-vue3
代码示例

配合 <script setup> 使用:

<template>
  <div>
    <relation-graph ref="graphRef" :options="options" />
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import RelationGraph from 'relation-graph-vue3'

const graphRef = ref()
const options = {
  defaultExpandHolderPosition: 'right'
}

onMounted(() => {
  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)
})
</script>

3. React 使用

安装
npm install --save relation-graph-react
代码示例

React 组件中集成:

import React, { useEffect, useRef } from 'react';
import RelationGraph, { RelationGraphInstance } from 'relation-graph-react';

const staticJsonData = {
  rootId: '2',
  nodes: [
    { id: '1', text: '节点 -1' },
    { id: '2', text: '节点 -2', width: 100, height: 100 },
    { id: '3', text: '节点 -3' },
    // ... 更多节点数据
  ],
  lines: [
    { from: '7', to: '71', text: '投资' },
    { from: '8', to: '81', text: '投资' },
    // ... 更多连线数据
  ]
};

const NodeSlot = ({ node }) => {
  if (node.id === '2') {
    return (
      <div style={{ zIndex: 555, opacity: 0.5, lineHeight:'100px', width: '100px', height: '100px', borderRadius:'50%' }}>
        {node.text}
      </div>
    );
  }
  return <div style={{lineHeight: '80px', textAlign: 'center'}}>{node.text}</div>;
};

const SimpleGraph = () => {
  const graphRef = useRef();
  
  useEffect(() => {
    showGraph();
  }, []);

  const showGraph = async () => {
    await graphRef.current.setJsonData(staticJsonData);
  };

  const options = {
    debug: true,
    defaultLineShape: 1,
    layout: { layoutName: 'center', maxLayoutTimes: 3000 },
    defaultExpandHolderPosition: 'right'
  };

  const onNodeClick = (node) => {
    console.log('onNodeClick:', node.text);
    return true;
  };

  return (
    <div>
      <div style={{ height: 600, width: 900, border: '#efefef solid 1px' }}>
        <RelationGraph
          ref={graphRef}
          options={options}
          nodeSlot={NodeSlot}
          onNodeClick={onNodeClick}
        />
      </div>
    </div>
  );
};

export default SimpleGraph;

进阶用法:自定义节点样式

在实际项目中,我们往往需要更丰富的节点展示。以下是一个基于 Vue2 的自定义插槽示例,展示了如何结合 CSS 美化节点外观。

<template>
  <div class="dev-linkage-diagram">
    <h3>设备联动图</h3>
    <RelationGraph 
      ref="graphRef" 
      :options="graphOptions" 
      :on-node-click="onNodeClick" 
      :on-line-click="onLineClick"
    >
      <template #node="{ node }">
        <div class="nodeBox">
          <div class="top">
            <div class="left">
              <span></span>
              <strong>{{ node.text }}</strong>
              <div class="state">离线</div>
            </div>
            <div class="num">4</div>
          </div>
          <div class="bottom">
            办公室 1
          </div>
        </div>
      </template>
    </RelationGraph>
  </div>
</template>

<script>
import RelationGraph from 'relation-graph'

export default {
  name: "DevLinkageDiagram",
  components: { RelationGraph },
  data() {
    return {
      graphOptions: {
        defaultJunctionPoint: 'border',
        allowShowDownloadButton: false,
        defaultFocusRootNode: false,
        defaultNodeWidth: 1,
        defaultNodeHeight: 1,
        defaultShowLineLabel: false,
        allowShowRefreshButton: true
      }
    }
  },
  mounted() {
    this.showGraph()
  },
  methods: {
    showGraph() {
      const jsonData = {
        rootId: 'a',
        nodes: [
          { id: 'a', text: 'A' },
          { id: 'b', text: 'B' },
          { id: 'c', text: 'C' },
          { id: 'e', text: 'E' }
        ],
        lines: [
          { from: 'a', to: 'b' },
          { from: 'a', to: 'c' },
          { from: 'a', to: 'e' },
        ]
      }
      this.$refs.graphRef.setJsonData(jsonData)
    },
    onNodeClick(nodeObject, $event) {},
    onLineClick(lineObject, $event) {}
  }
}
</script>

<style scoped lang="less">
.dev-linkage-diagram {
  padding: 15px;
  background-color: #ffffff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
  }
  .nodeBox {
    width: 200px;
    border-radius: 20px;
    overflow: hidden;
    > div {
      padding: 0 16px;
    }
    .top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      height: 40px;
      background-color: #deb922;
      .left {
        display: flex;
        align-items: center;
        color: #fff;
        .state {
          padding: 1px 4px;
          margin-left: 4px;
          background-color: rgba(245, 245, 245, 0.3);
          border-radius: 4px;
        }
      }
      .num {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 24px;
        height: 24px;
        background-color: rgba(226, 226, 226, 0.5);
        border: 1px solid #fff;
        border-radius: 50%;
        color: #fff;
        font-weight: 700;
        font-size: 14px;
      }
    }
    .bottom {
      display: flex;
      align-items: center;
      width: 100%;
      height: 40px;
      background-color: #72716d;
      font-size: 14px;
    }
  }
}
</style>

效果展示

运行上述代码后,即可得到可视化的关系图谱。自定义插槽允许我们完全控制节点的渲染逻辑,满足复杂的业务展示需求。

运行成果图

整体来看,relation-graph 在易用性和扩展性之间取得了很好的平衡,非常适合用于构建各类数据可视化场景。

目录

  1. 前端关系图谱组件 relation-graph 实战指南
  2. 快速开始
  3. 1. Vue2 使用
  4. 安装
  5. 代码示例
  6. 2. Vue3 使用
  7. 安装
  8. 代码示例
  9. 3. React 使用
  10. 安装
  11. 代码示例
  12. 进阶用法:自定义节点样式
  13. 效果展示

更多推荐文章

查看全部
  • Web 自动化测试入门指南:从概念到 Selenium 实战
  • C++ 算法:DFS 与 BFS 详解及经典例题
  • Node.js 16.x 至 20.x 版本升级指南:兼容性与性能优化
  • Python 开发工具 uv 安装、配置与最佳实践
  • 详解 Python 多线程与多进程
  • Linux 进程信号(上):信号产生方式和闹钟
  • DataRoom 开源大屏设计器:基于 SpringBoot 快速构建数据可视化平台
  • AI 入门:常见术语解释与误区澄清
  • OpenClaw 集成飞书命令行配置指南
  • Python 开源 AI 模型引入与测试全流程实战
  • 马年新春 Python+Stable Diffusion 生成企业营销素材
  • 深入理解并查集:原理、优化技巧与 LeetCode 实战
  • VR + 具身智能 + 人形机器人:通往现实世界的智能接口
  • Java 核心面试知识点汇总:集合、多线程、JVM 及锁机制
  • Stable Diffusion WebUI 云服务器部署实战指南
  • SpringAI 通过 Ollama 本地部署 Deepseek 模型实现对话机器人
  • AWVS Web 安全漏洞扫描工具安装与使用指南
  • Visual C++ 运行库整合安装方案:VisualCppRedist AIO
  • 算法优选:位运算技巧实战解析
  • 运维排查问题常用工具与方法论

相关免费在线工具

  • Keycode 信息

    查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online

  • Escape 与 Native 编解码

    JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online

  • JavaScript / HTML 格式化

    使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online

  • JavaScript 压缩与混淆

    Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online