vue中图谱关系插件relation-graph

一、效果图

在这里插入图片描述

二、安装下载(vue2.0版本的)

npm install --save relation-graph
var foo = 'bar';

三、直接上代码

<template>
  <div class="graphClass" ref="myPage">
    <RelationGraph
      ref="seeksRelationGraph"
      :options="graphOptions"
      :on-node-click="onNodeClick"
      :on-line-click="onLineClick"
    >
      <div
        class="node"
        style="height: 100%"
        slot="node"
        slot-scope="{ node }"
        @mouseover="showNodeTips(node, $event)"
        @mouseout="hideNodeTips(node, $event)"
      >
        <p
          style="
            position: absolute;
            top: 8px;
            left: 38px;
            min-width: 350px;
            font-size: 10px;
            color: #8c9094;
            text-align: left;
          "
        >
          {{ node.text }}
        </p>
      </div>
    </RelationGraph>
    <!-- 点击提示 -->
    <div
      v-show="isShowNodeTipsPanel"
      :style="{
        left: nodeMenuPanelPosition.x + 'px',
        top: nodeMenuPanelPosition.y + 'px',
      }"
      style="
        position: absolute;
        padding: 5px 10px;
        width: 250px;
        background: rgba(230, 217, 202, 0.8);
        z-index: 999;
      "
    >
      <div style="line-height: 15px; color: #888888; font-size: 10px">
        {{ currentNode.text }};[{{ currentNode.id }}]
      </div>
    </div>
  </div>
</template>
<script>
import RelationGraph from 'relation-graph';
import { knowledgeGraphList } from '../../api';
export default {
  components: { RelationGraph },
  props: {
    id: {
      type: [Number, String],
      default: '',
    },
  },
  data() {
    return {
      activeKey: '',
      isShowNodeTipsPanel: false,
      nodeMenuPanelPosition: { x: 0, y: 0 },
      currentNode: {},
      graphOptions: {
        allowShowMiniToolBar: false, //是否显示工具栏
        allowSwitchLineShape: true,
        allowSwitchJunctionPoint: true,
        defaultNodeShape: 0, //默认的节点形状,0:圆形;1:矩形
        // defaultExpandHolderPosition: 'bottom', //节点展开关闭的按钮位置
        defaultLineShape: 1, //默认的线条样式(1:直线/2:样式2/3:样式3/4:折线/5:样式5/6:样式6)
        defaultJunctionPoint: 'border', //默认的连线与节点接触的方式(border:边缘/ltrb:上下左右/tb:上下/lr:左右)当布局为树状布局时应使用tb或者lr,这样才会好看
        //   defaultNodeBorderWidth: 0, //节点边框粗细
        defaultcolor: '#8c9094', //默认的线条颜色
        defaultNodeColor: '#FACD91', //默认的节点背景颜色
        defaultNodeWidth: '30', //节点宽度
        defaultNodeHeight: '30', //节点高度
        defaultFocusRootNode: true, //默认为根节点添加一个被选中的样式
        moveToCenterWhenResize: true, //当图谱的大小发生变化时,是否重新让图谱的内容看起来居中
        debug: true,
        layouts: [
          {
            label: '中心',
            layoutName: 'center', //布局方式(tree树状布局/center中心布局/force自动布局)
            //   layoutClassName: 'seeks-layout-center', //当使用这个布局时,会将此样式添加到图谱上
            defaultJunctionPoint: 'border', //默认的连线与节点接触的方式
            defaultNodeShape: 0, //默认的节点形状,0:圆形;1:矩形
            defaultLineShape: 1, //默认的线条样式(1:直线/2:样式2/3:样式3/4:折线/5:样式5/6:样式6)
          },
        ],
      },
    };
  },
  mounted() {
    this.showSeeksGraph();
  },
  methods: {
    showNodeTips(nodeObject, $event) {
      this.currentNode = nodeObject;
      const _base_position = this.$refs.myPage.getBoundingClientRect();
      this.isShowNodeTipsPanel = true;
      this.nodeMenuPanelPosition.x = $event.clientX - _base_position.x + 10;
      this.nodeMenuPanelPosition.y = $event.clientY - _base_position.y + 10;
    },
    hideNodeTips(nodeObject, $event) {
      this.isShowNodeTipsPanel = false;
    },
    callback(val) {
      this.activeKey = val;
      this.showSeeksGraph();
    },
    //渲染节点和连接线
    showSeeksGraph() {
      knowledgeGraphList({ id: this.id }).then(({ data }) => {
        // 线
        let lines = data.edges.map(item => ({
          from: item.from.toString(),
          to: item.to.toString(),
          text: item.label,
          color: item.label == '依据' ? '#FACD91' : item.label == '历史' ? '#67C23A' : '#82D2F8',
          styleClass: 'my-line-highlightxxxxxxxxxxxxxxx',

          lineShape: 6,
          fromJunctionPoint: 'border',
          toJunctionPoint: 'bottom',
        }));
        // 节点
        let nodes = [];
        data.nodes.forEach((item, index) => {
          let color =
            lines.filter(c => c.to == item.id).length > 0
              ? lines.filter(c => c.to == item.id)[0].color
              : '#FACD91';

          if (index == 0) {
            nodes.push({
              id: item.id.toString(),
              text: item.label,
              color: '#3e7afa',
            });
          } else {
            nodes.push({
              id: item.id.toString(),
              text: item.label,
              color: color,
            });
          }
        });
        var __graph_json_data = {
          rootId: 'a',
          nodes: nodes,
          lines: lines,
        };
        // 以上数据中的node和link可以参考"Node节点"和"Link关系"中的参数进行配置
        this.$refs.seeksRelationGraph.setJsonData(__graph_json_data, graphInstance => {
          setTimeout(() => {
            graphInstance.stopAutoLayout();
          }, 1000);
        });
      });
    },
    //点击节点触发的函数
    onNodeClick(nodeObject, $event) {
      const allLinks = this.$refs.seeksRelationGraph.getLinks();
      allLinks.forEach(link => {
        // 还原所有样式
        link.relations.forEach(line => {
          if (line.data.orignColor) {
            line.color = line.data.orignColor;
          }
          if (line.data.orignFontColor) {
            line.fontColor = line.data.orignColor;
          }
          if (line.data.orignLineWidth) {
            line.lineWidth = line.data.orignLineWidth;
          }
        });
      });
      // 让与{nodeObject}相关的所有连线高亮
      allLinks
        .filter(link => link.fromNode === nodeObject || link.toNode === nodeObject)
        .forEach(link => {
          link.relations.forEach(line => {
            line.data.orignColor = line.color;
            line.data.orignFontColor = line.fontColor || line.color;
            line.data.orignLineWidth = line.lineWidth || 1;
            line.color = '#3e7afa';
            line.fontColor = '#3e7afa';
            line.lineWidth = 1;
          });
        });
      // 有时候更改一些属性后,并不能马上同步到视图,这需要以下方法让视图强制根据数据同步到最新
      this.$refs.seeksRelationGraph.getInstance().dataUpdated();
    },
    //店家连接线触发的函数
    onLineClick(lineObject, $event) {
      console.log('onLineClick:', lineObject);
    },
  },
};
</script>
<style lang="scss">
.graphClass {
  height: 700px;
  position: relative;
  border: 1px solid #f2f3f3;
  .rel-map-canvas {
    margin-left: calc(50% - 10px) !important;
  }
}
</style>
<style lang="scss" scoped>
::v-deep .relation-graph {
  .my-line-highlightxxxxxxxxxxxxxxx {
    animation: my-line-easy-anm1 2s linear infinite;
  }
  .rg-line-anm-1 {
    animation: my-line-easy-anm1 2s linear infinite;
  }
  //取消点击线条后节点的闪烁效果
  rel-node-flashing {
    animation: none;
  }
}

@keyframes my-line-easy-anm1 {
  0% {
    stroke-dashoffset: 100px;
    stroke-dasharray: 5, 5, 5;
  }
  100% {
    stroke-dasharray: 5, 5, 5;
    stroke-dashoffset: 3px;
  }
}
</style>


链接: https://relation-graph.com/#/docs/start
链接: https://cloud.tencent.com/developer/article/2325304

### 如何在 relation-graph v2.2.11 中禁用元素拖动 在 `relation-graph` 组件中,可以通过配置选项来控制某些行为,比如禁用元素的拖动功能。以下是具体实现方式: #### 方法一:通过 options 配置项设置 `relation-graph` 提供了一个名为 `draggable` 的布尔型属性,用于启用或禁用整个图表中的节点拖拽功能。如果希望全局禁用拖动,则可以直接将其设为 `false`。 ```javascript const graphOptions = { draggable: false, // 禁用所有节点的拖动功能[^1] }; ``` 此配置会作用于所有的节点和边,使得它们无法被用户拖动。 --- #### 方法二:针对特定节点禁用拖动 如果仅需对部分节点禁用拖动,而其他节点仍然保持可拖动状态,则可通过节点的数据字段 `data.draggable` 来单独控制每个节点的行为。 ```javascript const nodes = [ { id: 'node1', text: 'Node 1', data: { draggable: false } }, // 节点不可拖动 { id: 'node2', text: 'Node 2', data: { draggable: true } }, // 节点可拖动 ]; ``` 在此情况下,`node1` 将不会响应用户的拖动操作,而 `node2` 则正常工作。 --- #### 方法三:处理移动端特殊场景下的拖动禁用 对于移动端设备上的拖动问题,可能还需要额外注意事件绑定的方式。由于默认的拖动逻辑依赖于鼠标的 `mousedown` 和 `mousemove` 事件,而在触控屏幕上这些事件会被替换为 `touchstart` 和 `touchmove`。因此,为了确保禁用效果生效,建议结合以下两种措施: 1. **调整组件内部事件监听器** 如果已经获取到组件源码,可以手动修改其事件绑定逻辑,将不必要的触摸事件移除或者阻止传播。 2. **外部拦截触摸事件** 使用 CSS 属性或 JavaScript 显式阻止触摸事件冒泡至目标节点。 ```css /* 添加样式防止触发默认手势 */ .relation-graph .rg-node { touch-action: none; /* 禁用手势识别[^3] */ } ``` --- #### 完整示例代码 下面是一个完整的例子,展示如何在初始化时禁用所有节点的拖动功能,并且兼容桌面端与移动端环境。 ```vue <template> <RelationGraph ref="graphRef" :options="graphOptions"> <!-- 自定义节点 --> <template #node="{ node }"> <div class="custom-node">{{ node.text }}</div> </template> </RelationGraph> </template> <script> export default { data() { return { graphOptions: { draggable: false, // 全局关闭拖动功能[^1] }, }; }, }; </script> <style scoped> .custom-node { touch-action: none; /* 移动端禁用手势[^3] */ } </style> ``` --- ### 注意事项 - 当前版本号为 `v2.2.11`,请确认所使用的文档与此版本一致,以免因 API 变更导致错误。 - 若仍遇到异常情况,推荐查看官方更新日志或联系技术支持团队以获得进一步帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值