Vue使用vis实现拓扑图

72 篇文章 4 订阅

visjs官方文档
visjs示例

1. 安装

npm install vis

2. 封装成组件 

<template>
  <div :id="id" style="width: 100%; height: 100%"></div>
</template>
<script>
import vis from "vis";
let options;
export default {
  data() {
    return {
      network: "",
    };
  },
  props: {
    id: {
      type: String,
      required: true,
    },
    edges: {
      type: Array,
      default: () => [
        { from: 1, to: 3 },
        { from: 1, to: 2 },
        { from: 2, to: 4 },
        { from: 2, to: 5 }
      ]
    },
    nodes: {
      type: Array,
      default: () => [
        { id: 1, label: "Node 1" },
        { id: 2, label: "Node 2" },
        { id: 3, label: "Node 3" },
        { id: 4, label: "Node 4" },
        { id: 5, label: "Node 5" }
      ]
    }
  },
  watch: {
    nodes() {
      this.updateVisTopology()
    }
  },
  mounted() {
    this.createVisTopology();
  },
  methods: {
    //  创建一个vis拓扑图
    createVisTopology() {
      // create an array with nodes
      let nodes = new vis.DataSet(this.nodes);

      // create an array with edges
      let edges = new vis.DataSet(this.edges);

      // provide the data in the vis format
      let data = {
        nodes: nodes,
        edges: edges,
      };
      options = {
        //节点样式
        nodes: {
          shape: "box", //设置节点node样式为矩形 可选值:ellipse | circle | database | box | text
          fixed: false, //节点node固定不可移动
          font: {
            color: "white", //字体的颜色
            size: 30, //显示字体大小
          },
          scaling: {
            min: 16,
            max: 32, //缩放效果比例
          },
        },
        //连接线的样式
        edges: {
          color: {
            color: "rgb(97, 168, 224)",
            highlight: "rgb(97, 168, 224)",
            hover: "green",
            inherit: "from",
            opacity: 1.0,
          },
          font: {
            align: "top", //连接线文字位置
          },
          smooth: true, //是否显示方向箭头
          arrows: { to: true }, //箭头指向from节点
        },
        layout: {
          //以分层方式定位节点
          hierarchical: {
            direction: "LR", //分层排序方向
            sortMethod: "directed", //分层排序方法
            levelSeparation: 400, //不同级别之间的距离
          },
        },
        interaction: {
          navigationButtons: true,
          hover: true, //鼠标移过后加粗该节点和连接线
          selectConnectedEdges: false, //选择节点后是否显示连接线
        },
      };

      // initialize your network!
      this.network = new vis.Network(document.getElementById(this.id),data,options);
    },
    updateVisTopology() {
      this.network.setData({
        nodes: this.nodes,
        edges: this.edges
      })
      // this.network.setOptions(options)
    }
  },
};
</script>

3. 组件使用 

<template>
  <div style="width: 100%;height: 100%;">
    <div class="vis-container">
      <VisTopology :id="'VisTopology'" />
    </div>
  </div>
</template>
<script>
import VisTopology from './vis.vue'
export default {
  components: { VisTopology }
}
</script>
<style scoped>
.vis-container {
  width: 500px;
  height: 500px;
  border: 1px solid red;
}
</style>

效果图 

 

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会说法语的猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值