初探G6, 切换数据时保证图位置不变

 const data = {
  nodes: [{
    id: '收集日志',
    label: 'aaa',
  }, {
    id: '入 es 集群',
    label: 'bbb',
  }, {
    id: '入 hdfs',
    label: 'ccc',
  }, {
    id: 'hive 计算',
    label: 'ddd',
  }, {
    id: 'report',
    label: 'eee',
  }],
  edges: [{
    source: '收集日志',
    target: '入 es 集群'
  }, {
    source: '收集日志',
    target: '入 hdfs'
  }, {
    source: '入 hdfs',
    target: 'hive 计算'
  }, {
    source: '入 es 集群',
    target: 'hive 计算'
  }, {
    source: 'hive 计算',
    target: 'report'
  }]
};
       const datas = {
  nodes: [{
    id: '收集日志',
    label: 111,
  }, {
    id: '入 es 集群',
    label: 222,
  }, {
    id: '入 hdfs',
    label: 333,
  }, {
    id: 'hive 计算',
    label: 444,
  }, {
    id: 'report',
    label: 555,
  }],
  edges: [{
    source: '收集日志',
    target: '入 es 集群'
  }, {
    source: '收集日志',
    target: '入 hdfs'
  }, {
    source: '入 hdfs',
    target: 'hive 计算'
  }, {
    source: '入 es 集群',
    target: 'hive 计算'
  }, {
    source: 'hive 计算',
    target: 'report'
  }]
};
class IntroCard extends React.Component {
  constructor(props) {
        super(props);
        this.state = {
            showImage: true,
        };
        this.graph = null;
    }
  componentDidMount() {
      this.rendeG6(data);
    }
    rendeG6 = (data) => {
  this.graph= new G6.Graph({
  container: 'mountNode',
  // fitView: 'cc',
  height: window.innerHeight, // 画布高
  plugins: [new G6.Plugins['layout.dagre']()],
  defaultIntersectBox: 'rect' // 使用矩形包围盒
});

this.graph.node({
  shape: 'rect',
  label: function label(model) {
    return model.label;
  },

  style: {
    stroke: '#00C0A5',
    fill: '#92949F',
    fillOpacity: 0.45,
    lineWidth: 2
  }
});
this.graph.edge({
  style: {
    endArrow: true
  }
});
      let lastPoint = 0;
        this.graph.on('drag', (ev) => {
          this.graph.css({
            cursor: 'pointer',
          });
          if (lastPoint) {
            this.graph.translate(ev.domX - lastPoint.x, ev.domY - lastPoint.y);
          }
          lastPoint = {
            x: ev.domX,
            y: ev.domY,
          };
        });
        this.graph.on('dragend', () => {
          this.graph.css({
            cursor: 'default',
          });
          lastPoint = undefined;
        });
this.graph.read(data);
    }
    onload = () => {
      this.graph.read(datas);
    }
    onback = () => {
       this.graph.read(data);
    }
  render() {
    return (
      <div>
        <button onClick={this.onload}>切换数据</button>
        <button onClick={this.onback}>元数据</button>
        <div id="mountNode"></div>
       </div>
    )
  }

};
复制代码

codepen地址

转载于:https://juejin.im/post/5c1a0f3ce51d45255514a24d

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值