画布 Graph antv-x6在vue中的使用,graph函数动态添加节点和边

参考antv-x6官网: 画布 Graph

参考官网: 图表案例

  1. 实现效果图
    在这里插入图片描述

  2. 请求后端返回的数据格式(json):

{
    "data": {
        "nodes": [
            {
                "id": "00001022",
                "name": "xxxxxxx公共服务"
            },
            {
                "id": "00002001",
                "name": "xxxx服务后台"
            },
            {
                "id": "00001047",
                "name": "xxxx支撑服务"
            }
        ],
        "lines": [
            {
                "source": "00002001",
                "target": "00001022",
                "type": "接口关系",
                "weight": 1
            },
            {
                "source": "00001047",
                "target": "00001022",
                "type": "接口关系",
                "weight": 1
            }
        ]
    },
    "code": 20000
}
  1. vue代码实现

在这里插入图片描述

methods: {
	  getRelation(appid,level) {
	    getappRelation({'app_id': appid,'level':level}).then(response => {
	      //console.log(response.data)
        this.initmap(response.data)		  
	    })      
	  },
    initposition(sum){

    },
    initmap(app_data){
      this.initposition(app_data.length)
      var graph = new Graph({
        container: document.getElementById('my_map'),
        grid: true,
        panning: {enabled: true}
      })
      this.addNode(graph,app_data.nodes[0],0,50,300)
      for (let i = 1; i< app_data.nodes.length; i++) {
        this.addNode(graph,app_data.nodes[i],1,this.getposx(app_data.nodes.length - 1,i - 1),this.getposy(app_data.nodes.length - 1,i - 1))
      }
      for (let i = 0; i< app_data.lines.length; i++) {
        this.addLine(graph,app_data.lines[i])
      }
    },
    getposx(sum,num){
      const lines = parseInt( num  / 8)
      return lines*250 + 400
    },
    getposy(sum,num){
      //console.log('sum:' + sum + ' num:' + num)
      const lines = parseInt( num / 8)
      const pos = num  % 8
      //console.log('lines:' + lines + ' pos:' + pos)
      if (lines == parseInt( sum /8)){
        const allpos = sum % 8
        //console.log('allpos:' + allpos + ' return:' + (pos+1) * 720 / allpos)
        return (pos+1) * 720 / (allpos + 1)
      } else {
        return (pos+1) *90
      }
    },
    addNode(graph,node,type,x,y){
      
      //console.log('add node' + node.name + ' x:' + x +  ' y:' + y)
      const wrap = document.createElement('div')
      wrap.style.width = '100%'
      wrap.style.height = '100%'
      wrap.style.display = 'flex'
      wrap.style.alignItems = 'center'
      wrap.style.justifyContent = 'center'
      wrap.style.textAlign = 'center'
      wrap.style.borderRadius = '4px'
      wrap.style.margin = '0 auto'

      wrap.innerText = node.name
      if (type == 1) {
          wrap.style.background = 'yellow'
        wrap.style.border = '2px solid #ffa940'

        }else{
        wrap.style.background = '#f64040'
        wrap.style.border = '2px solid #ffa940'

      }
      const target = graph.addNode({
        shape: 'html',
        x: x,
        y: y,
        width: 150,
        height: 60,
        html: wrap,
      })
      this.map_data[node.id] = target
      //console.log('node info ' + node.id + ' ' + this.map_data[node.id])
      return target
    },
    addLine(graph, line){
      //console.log('add line ' + line.source + ' ' + line.target)
      const fromnode = this.map_data[line.source]
      const tonode = this.map_data[line.target]
      
      let marker1 = 'classic'
      let marker2 = 'circle'
      if (line.type=='变更订阅'){
        marker1 = 'classic'
        marker2 = 'diamond'
      } else if (line.type=='消息队列订阅'){
        marker1 = 'classic'
        marker2 = 'circlePlus'
      }
      graph.addEdge(new Shape.Edge(
        {
          source:fromnode,
          target:tonode,
          router: 'manhattan',
          connector: {
            name: 'rounded',
            args: {
              radius: 20,
            },
          },  
          attrs: {
            line: {
              targetMarker: {
                size: 10,
                name: marker1,
              },
              sourceMarker: {
                size: 4,
                name: marker2,
              },
              stroke: '#46AD4d',
              strokeWidth: line.weight/3+1,
            },
          },
        }))
    }
  }
要在Vue项目添加AntV X6标尺,可以按照以下步骤操作: 1. 安装AntV X6库,可以使用npm或yarn进行安装: ```bash npm install @antv/x6 --save ``` 或者 ```bash yarn add @antv/x6 ``` 2. 在Vue组件引入AntV X6库: ```javascript import { Graph, Edge, Node, Point } from '@antv/x6' ``` 3. 在Vue组件的mounted或created函数,创建一个Graph实例: ```javascript mounted() { const container = document.getElementById('container') const graph = new Graph({ container, width: container.offsetWidth, height: container.offsetHeight, gridSize: 10, drawGrid: true, background: { color: '#f5f5f5', }, }) } ``` 4. 在Graph实例添加标尺: ```javascript mounted() { const container = document.getElementById('container') const graph = new Graph({ container, width: container.offsetWidth, height: container.offsetHeight, gridSize: 10, drawGrid: true, background: { color: '#f5f5f5', }, snapline: true, // 添加吸附线 scroller: { enabled: true, pageVisible: true, pageBreak: false, pannable: true, }, }) // 添加横向标尺 const rulerX = new Node({ shape: 'path', data: 'M 0 0 L 10000 0', attrs: { stroke: '#ccc', 'stroke-width': 1, }, }) const rulerXNode = graph.addNode({ x: 0, y: 0, width: 10000, height: 0, nodeType: 'rulerX', zIndex: 1, attrs: { body: { stroke: 'none', fill: 'none', }, }, }) rulerXNode.appendChild(rulerX) // 添加纵向标尺 const rulerY = new Node({ shape: 'path', data: 'M 0 0 L 0 10000', attrs: { stroke: '#ccc', 'stroke-width': 1, }, }) const rulerYNode = graph.addNode({ x: 0, y: 0, width: 0, height: 10000, nodeType: 'rulerY', zIndex: 1, attrs: { body: { stroke: 'none', fill: 'none', }, }, }) rulerYNode.appendChild(rulerY) } ``` 5. 在Vue组件的template添加容器: ```html <template> <div id="container"></div> </template> ``` 这样就可以在Vue项目添加AntV X6的标尺了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值