@antv/x6在react中画人物关系图,用group画群组框

效果图:

在这里插入图片描述

首先 cnpm install @antv/x6 --save

复制代码:

import React, { Component } from 'react'
import { Graph, Node, Point } from '@antv/x6'
class antv extends Component {
    constructor(props){
      super(props);
    }
    componentDidMount() {
        Graph.registerNode(
          'org-node',
          {
            width: 180,
            height: 60,
            markup: [
              {
                tagName: 'rect',
                selector: 'body',
              },
              {
                tagName: 'image',
                selector: 'avatar',
              },
              {
                tagName: 'text',
                selector: 'rank',
              },
              {
                tagName: 'text',
                selector: 'name',
              },
            ],
            attrs: {
              body: {
                refWidth: '100%',
                refHeight: '100%',
                fill: '#FFFFFF',
                stroke: '#000000',
                strokeWidth: 2,
                rx: 10, // 圆角
                ry: 10, // 圆角
                pointerEvents: 'visiblePainted',
              },
              avatar: {
                width: 30,
                height: 30,
                refX: 18,
                refY: 18,
              },
              name: {
                refX: 140,
                refY: 20,
                fontFamily: 'Courier New',
                fontSize: 14,
                fontWeight: '800',
                textAnchor: 'end',
              },
            },
          },
          true,
        )
        
        Graph.registerEdge(
          'org-edge',
          {
            zIndex: -1,
            attrs: {
              line: {
                fill: 'none',
                strokeLinejoin: 'round',
                strokeWidth: '2',
                stroke: '#4b4a67',
                sourceMarker: null,
                targetMarker: null,
              },
            },
          },
          true,
        )
        
        const graph = new Graph({
          container: document.getElementById('container'),
          width: 800,
          height: 500,
          grid: true,
          connecting: {
            anchor: 'orth',
          },
          interacting: { // 禁止节点移动
            nodeMovable: false
          },
          translating: {
            restrict(view) {
              const cell = view.cell
              if (cell.isNode()) {
                const parent = cell.getParent()
                if (parent) {
                  return parent.getBBox()
                }
              }
        
              return null
            },
          },
        })
        graph.zoom(-0.5)
        const male = 'https://gw.alipayobjects.com/mdn/rms_43231b/afts/img/A*kUy8SrEDp6YAAAAAAAAAAAAAARQnAQ'
        const female = 'https://gw.alipayobjects.com/mdn/rms_43231b/afts/img/A*f6hhT75YjkIAAAAAAAAAAAAAARQnAQ'
        
        const bart = this.member(graph,300, 0,'王狗蛋1', male, 'red')
        const homer = this.member(graph,0,200,'李狗蛋1',male,'pink','#f1f1f1',
        )
        const marge = this.member(graph,300,200,'李狗蛋2',female,'pink','#f1f1f1',
        )
        const lisa = this.member(graph,600,200,'李狗蛋3',female,'pink','#f1f1f1',
        )
        const lenny = this.member(graph,110, 350,'张狗蛋1', male, 'green')
        const maggie = this.member(graph,410, 350,'张狗蛋2', female, 'green')
        const carl = this.member(graph,-110, 350,'刘狗蛋1', male, 'green')
        
        this.link(graph,bart, marge, [])
        this.link(graph,bart, homer, [
          { x: 390, y: 100 }, //拐点
          { x: 90, y: 100 },
        ])
        this.link(graph,bart, lisa, [
          { x: 390, y: 100 },
          { x: 690, y: 100 },
        ])
        this.link(graph,homer, lenny, [
          { x: 90, y: 380 },
        ])
        this.link(graph,marge, maggie, [{ x: 390, y: 380 }])
        this.link(graph,homer, carl, [{ x: 90, y: 380 }])
        this.group(graph,-20,180,820,100,[homer,marge,lisa])
    }
    // 添加节点
    member(graph,x,y,name,image,background,textColor,) {
      return graph.addNode({
        x,
        y,
        shape: 'org-node',
        attrs: {
          body: {
            fill: background,
            stroke: 'none',
          },
          avatar: {
            opacity: 0.7,
            'xlink:href': image,
          },
          name: {
            text: name,
            fill: textColor,
            fontSize: 24,
            fontFamily: 'Arial',
            // letterSpacing: 0,
          },
        },
      })
    }
    // 添加连线
    link(graph,source, target, vertices) {
      return graph.addEdge({
        vertices,
        source: { cell: source },
        target: { cell: target },
        shape: 'org-edge',
      })
    }
    // 添加组
    group(graph,x,y,width,height,array){
      const parent = graph.addNode({
        x,
        y,
        width,
        height,
        zIndex: 1,
        attrs: {
          body: {
            fill: 'rgba(255,255,255,0)',
            stroke: '#000000',
            opacity:0.1,
            rx: 10, // 圆角
            ry: 10, // 圆角
          },
        },
      })
      array.forEach(val => {
        parent.addChild(val)
      })
    }
    render() {
      return (
        <div>
          <div id="container"></div>
        </div>
      );
    }
  }
   
  export default antv;
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值