antv G6关系树,拓扑图 不同层级不同颜色

前端菜鸡… 近期遇到一个需求,要求关系图每个层级不同颜色展示,位置还得随机,目前echart实现后都不太满意…em 于是G6 …

废话不多说 看代码(效果)//

1,引入G6 相关 (基础数据是官方的)

import G6 from '@antv/g6'


 fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/algorithm-category.json')
                .then(res => res.json())
                .then(data => {
                  console.log(data)
                    const width = document.getElementById('map').scrollWidth;
                    const height = document.getElementById('map').scrollHeight || 500;
                     const graph= new G6.TreeGraph({
                        container: 'map',
                        width,
                        height,
                        pixelRatio: 2,
                        modes: {
                            default: [{
                                type: 'collapse-expand',
                                onChange: function onChange(item, collapsed) {
                                  console.log(item)
                                    const data = item.get('model').data;
                                    data.collapsed = collapsed;
                                    return true;
                                }
                            }, 'drag-canvas', 'zoom-canvas']
                        },
                        defaultNode: {
                            size: [200, 50],
                           
                            shape: 'rect',
                          
                            style: {
                                fill: '#C6E5FF',
                                stroke: '#5B8FF9'
                            }
                        },
                        defaultEdge: {
                            shape: 'cubic-horizontal',
                            style: {
                                stroke: '#A3B1BF'
                            }
                        },
                        layout: {
                            type: 'mindmap',
                            direction: 'H',
                            getHeight: () => {
                                return 40;
                            },
                            getWidth: () => {
                                return 160;
                            },
                            getVGap: () => {
                                return 10;
                            },
                            getHGap: () => {
                                return 100;
                            }
                        }
                    });
                    
                    let centerX = 0;
                    // 以下重点
                    graph.node(function (node) {
                        // depth 类似节点标识
                        if(node.depth == 0){
                            console.log(node)
                            return {
                                size:[100,60],
                                style:{
                                    fill:'red',
                                    // stroke:''
                                },
                                label:node.id
                            }
                        }
              
                        if(node.depth == 1){
                            console.log(node)
                            return {
                                size:[100,60],
                                style:{
                                    fill:'blue',
                             
                                },
                                label:node.id
                            }
                        }
                        return {
                            label: node.id, // 设置显示名称
                            labelCfg: {
                                // position: node.children && node.children.length > 0 ? 'left' : node.x > centerX ? 'right' : 'left', // 设置显示左右
                                offset: 5
                            }
                        };
                    });
                    graph.data(data);
                    graph.render();
                    graph.fitView();
                });

这样效果就出来了. 不同级不同颜色 看图在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值