G6绘制树形图(自定义节点、自定义边、自定义布局)

这篇具有很好参考价值的文章主要介绍了G6绘制树形图(自定义节点、自定义边、自定义布局)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1 设计节点

在 registerNode 中定义所有的节点

G6.registerNode('tree-node', {
	drawShape: function drawShape(cfg, group) {
	定义图中需要的节点
	}
}, 'single-node',);

为了使用内置的布局方式,选择参数为 ‘tree-node’ 树节点类型,数据格式可以存在children子节点,效果自动生成子树
cfg 可以拿到数据,如cfg.id、cfg.name

1.1 定义节点和文本

使用 group.addShape(‘rect’, {}) 定义节点 rect
配置参数:https://antv-g6.gitee.io/zh/docs/api/shapeProperties/#fill

        // 定义节点 rect 
        const rect = group.addShape('rect', {  // 'rect'表示矩形图形
          attrs: {
            // 节点定义参数:颜色、阴影...
          },
          name: 'rect-shape',   // 为这个节点起名字 不过没有使用过这个名字
        });

使用 group.addShape(‘text’, {}) 定义文本 text

		// 定义文本text
        const text = group.addShape('text', {  // 'text'表示文本
          attrs: {
            // 参数:颜色、文字...
          },
          name: 'text-shape',
        });

节点和文字生成后,再定义他们的相对位置
参考官网定义复杂图样式的方式:https://antv-g6.gitee.io/zh/examples/tree/customItemTree#customTree
使用 .getBBox() 获得该文本的盒子bbox,使用文本盒子的相对位置后面的位置坐标

        const bbox = text.getBBox();   // 获得文本的盒子
        // 设置rect 节点的位置
        rect.attr({
          x: -bbox.width / 2 - 5,   // x坐标
          y: -bbox.height,			// y坐标
          width: bbox.width +  12 ,	 // 宽
          height: bbox.height + 8,   // 高
        });
        // 设置text文本的位置
        text.attr({
          x: -bbox.width / 2,
          y: -bbox.height / 2 + 3,
        })

效果如下
G6绘制树形图(自定义节点、自定义边、自定义布局)

1.2 增加节点

如果想为节点再增加一个小节点,并且位置随着大节点移动,如图
G6绘制树形图(自定义节点、自定义边、自定义布局)
新增节点和文本 rect2 text2

rect2 = group.addShape('rect', {
        attrs: {
                // 参数
        },
        name: 'rect-shape2',
});
const text2 = group.addShape('text', {
        attrs: {
                // 参数
        },
        name: 'text-shape2',
});

为rect2 text2设置坐标,以bbox作为参考位置

              // 设置坐标轴和宽高
              rect2.attr({
                  x: -bbox.width / 2 - 24,
                  y: -bbox.height / 2 - 1,
                  width: 14,
                  height: 10,
              });
              text2.attr({
                  x: -bbox.width / 2 - 23,
                  y: -bbox.height / 2 + 4,
              })

1.3 自定义节点样式

			roup.addShape('dom', {
                attrs: {
                  x: -bbox.width / 2 - 24 + 14,   // 即:rect的坐标 + rect的宽 
                  y: -bbox.height / 2 - 1,
                  width: 10,
                  height: 10,
                  html: `
                  <div style="border: 5px solid red;">
                  	自定义dom
                  </div>
                    `,
                },
                draggable: true,
              });

使用自定义dom,在 new G6.TreeGraph中 需要设置

renderer : 'svg',   // 奇怪的是设置之后原来节点的布局有些影响

2 树图配置

2.1 允许使用自定义dom节点

renderer : 'svg', 

2.2 内置行为

https://antv-g6.gitee.io/zh/docs/manual/middle/states/defaultBehavior#%E5%86%85%E7%BD%AE-behavior

modes: {
          default: [
            {
              type: 'collapse-expand',
              onChange: function onChange(item, collapsed) {
                const data = item.get('model');
                graph.updateItem(item, {
                  collapsed,
                });
                data.collapsed = collapsed;
                return true;
              },
            },
            'drag-canvas',    // 允许拖动
            'zoom-canvas',	 // ....
          ],
        },

自定义边

defaultEdge: {
          type: 'cubic-horizontal',
          style: {
            stroke: 'red'   //红色
          },
        },

layout布局

https://antv-g6.gitee.io/zh/docs/manual/middle/layout/tree-graph-layout

layout: {
            type: 'indented',
            direction: 'LR',   // 节点从左向右分布
            dropCap: false,
            indent: 190,
            getHeight: () => {
              return 13;
            },
            getVGap: function getVGap () {
              return 10;
            },
        },

demo

<template>
    <div class="main-content-box">
      <div id="container"></div>
    </div>
</template>
  
  <script>
  import G6 from '@antv/g6';

  export default {
    name: 'multTagsSec',
    data () {
      return {
        gDatas:{
                "id": "1",
                "name": "storehouse A",
                "children": [
                  {
                    "id": "2",
                    "name": "B",
                    "percentage": "60%",
                    "children": [
                      {
                        "id": "3",
                        "name": "storehouse C",
                        "percentage": "80%",
                        "children": [
                          {
                            "name": "storehouse C",
                            "percentage": "80%",
                            "children": [
                              {
                                "name": "D",
                                "percentage": "20%"
                              },
                              {
                                "name": "storehouselllllll C",
                                "percentage": "20%"
                              }
                            ]
                          },
                          {
                            "name": "storehouse D",
                            "percentage": "20%"
                          }
                        ]
                      },
                      {
                        "name": "storehouse D",
                        "percentage": "20%"
                      }
                    ]
                  },
                  {
                    "name": "storehouse C",
                    "percentage": "100%"
                  },
                  {
                    "name": "storehouse B",
                    "percentage": "20%"
                  },
                  {
                    "name": "storehouse C",
                    "percentage": "20%"
                  },
                  {
                    "name": "storehouse C",
                    "percentage": "20%",
                    "children": [
                              {
                                "name": "D",
                                "percentage": "20%"
                              },
                              {
                                "name": "storehouse A",
                                "percentage": "20%"
                                
                              }
                            ]
                  }
                ]
              }
  
      }
    },
    mounted() {
      this.getInit();
    },
    methods: {
      getInit () {
        // var mycfg = null;
        G6.registerNode('tree-node', {
          drawShape: function drawShape(cfg, group) {
            // console.log(cfg)
            
            // --------------------标签内容节点----------------------
            var hasChildren = cfg.children && cfg.children.length > 0;   // 是否有孩子节点
            var strokeColor =  hasChildren == true ? 'red' : null     // 有孩子 为红色
            // 节点设置 
            const rect = group.addShape('rect', {
              attrs: {
                fill: '#fff',
                stroke: strokeColor,  // 边框颜色
                lineWidth: 1,       // 边框粗细
                radius: 2,
                shadowBlur: 15,
                shadowColor: '#666',
                // shadowOffsetX: 2,
                // shadowOffsetY: 2
              },
              name: 'rect-shape',
            });
            // 文本设置
            const text = group.addShape('text', {
              attrs: {
                text: cfg.name,       // 赋值name属性
                fontFamily: 'normal',
                fontSize: 11,
                fontWeight: 800,
                x: 0,
                y: 0,
                textAlign: 'left',
                textBaseline: 'middle',
                fill: '#666'
              },
              name: 'text-shape',
            });
            
            const bbox = text.getBBox();   // 获得文本的盒子 之后的两个节点的xy轴坐标参考bbox
            //const minbbox = rect.getBBox();
            // 设置 rect方框和text文本 的 x y坐标轴
            rect.attr({
              x: -bbox.width / 2 - 5,
              y: -bbox.height,
              // width: bbox.width + (hasChildren ? 20 : 12),
              width: bbox.width +  12 ,
              height: bbox.height + 8,
            });
            text.attr({
              x: -bbox.width / 2,
              y: -bbox.height / 2 + 3,
            })
            
            // -----------百分比节点----------
            var hasPercentage = cfg.percentage;
            var rect2 = 0;
            if(hasPercentage){
                // 节点设置 2
                rect2 = group.addShape('rect', {
                attrs: {
                    fill: '#4682B4',
                    stroke: '',  // 边框颜色
                    lineWidth: 0,       // 边框粗细
                    shadowBlur: 0,
                    shadowColor: '',
                },
                name: 'rect-shape2',
              });
              // 文本设置 2
              const text2 = group.addShape('text', {
              attrs: {
                  text: cfg.percentage,       // 赋值name属性
                  fontFamily: 'normal',
                  fontSize: 5,
                  fontWeight: 500,
                  textAlign: 'left',
                  textBaseline: 'middle',
                  fill: 'white'
                },
                name: 'text-shape2',
              });
              // 设置坐标轴和宽高
              rect2.attr({
                  x: -bbox.width / 2 - 24,
                  y: -bbox.height / 2 - 1,
                  width: 14,
                  height: 10,
              });
              text2.attr({
                  x: -bbox.width / 2 - 23,
                  y: -bbox.height / 2 + 4,
              })
              // -------连接两个节点的小节点----------
              // const rect3 = group.addShape('rect', {
              //     attrs: {
              //         fill: '#00BFFF',
              //         stroke: '',  // 边框颜色
              //         lineWidth: 0,       // 边框粗细
              //         shadowBlur: 0,
              //         shadowColor: '',
              //     },
              //     name: 'rect-shape3',
              // });
              // rect3.attr({
              //     x: -bbox.width / 2 - 24 + 14,   // 即:rect的坐标 + rect的宽 
              //     y: -bbox.height / 4 + 1,
              //     width: 4,
              //     height: 4
              // });
              // -------连接两个节点的小节点 三角形----------
              // 需要设置svg才能使用
              group.addShape('dom', {
                attrs: {
                  x: -bbox.width / 2 - 24 + 14,   // 即:rect的坐标 + rect的宽 
                  y: -bbox.height / 2 - 1,
                  width: 10,
                  height: 10,
                  html: `
                  <div style="border-left: 5px solid red; 
                              border-right: 5px solid transparent;
                              border-top: 5px solid transparent;
                              border-bottom: 5px solid transparent;">
                  </div>
                    `,
                },
                draggable: true,
              });
            }
            

            // 小圆圈
            if (hasChildren) {
              const redcircle = group.addShape('marker', {
                  attrs: {
                  symbol: cfg.collapsed ? G6.Marker.expand : G6.Marker.collapse,
                  // symbol: cfg.collapsed ? COLLAPSE_ICON : EXPAND_ICON,
                  stroke: 'red',
                  fill: 'red',
                  lineWidth: 1.8,
                },
                name: 'collapse-icon',
              });
              redcircle.attr({
                  x: bbox.width / 2 + 7,
                  y: -3 ,
                  r: 4,

              })
            }

          

            return rect;
          },
          update: (cfg, item) => {
            const group = item.getContainer();
            const icon = group.find((e) => e.get('name') === 'collapse-icon');
            icon.attr('symbol', cfg.collapsed ? G6.Marker.expand : G6.Marker.collapse);
          },
        },
        'single-node',
      );
  
      const container = document.getElementById('container');
      const width = container.scrollWidth;
      const height = container.scrollHeight || 500;
  
      const graph = new G6.TreeGraph({
        renderer : 'svg',     // 创建自定义DMO时定义 会报一个错 但好像不影响 
        container: 'container',
        width,
        height,
        modes: {
          default: [
            {
              type: 'collapse-expand',
              onChange: function onChange(item, collapsed) {
                const data = item.get('model');
                graph.updateItem(item, {
                  collapsed,
                });
                data.collapsed = collapsed;
                return true;
              },
            },
            // 'drag-canvas',    // 不可拖动
            'zoom-canvas',
          ],
        },
        defaultNode: {
          type: 'tree-node',
          anchorPoints: [
            [0, 0.5],
            [1, 0.5],
          ],
        },
        // 设置边的参数
        defaultEdge: {
          type: 'cubic-horizontal',
          style: {
            stroke: 'red'
          },
        },
        layout: {
            type: 'indented',
            direction: 'LR',
            dropCap: false,
            indent: 190,
            getHeight: () => {
              return 13;
            },
            getVGap: function getVGap () {
              return 10;
            },
        },
      });
  
  
      
  
      graph.data(this.gDatas);
      graph.render();
      graph.fitView();
  
      if (typeof window !== 'undefined')
        window.onresize = () => {
          if (!graph || graph.get('destroyed')) return;
          if (!container || !container.scrollWidth || !container.scrollHeight) return;
          graph.changeSize(container.scrollWidth, container.scrollHeight);
        };
      },
  
  
  
  
    }
  }
  
  </script>
  
  <style scoped>
  </style>

G6绘制树形图(自定义节点、自定义边、自定义布局)文章来源地址https://www.toymoban.com/news/detail-454079.html

到了这里,关于G6绘制树形图(自定义节点、自定义边、自定义布局)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • antv/g6绘制数据流向图

    在业务开发中需要绘制数据流向图,由于echarts关系图的限制以及需求的特殊要求,转而使用antv/g6实现,本文以代码的方式实现数据流向需求以及节点分组,版本\\\"@antv/g6\\\": “^4.8.24”, 本文主要列出关键性的代码,并非全部代码

    2024年01月25日
    浏览(29)
  • 如何基于G6进行双树流转绘制?

    业务背景:CRM系统随着各业务条线对线索精细化分配的诉求逐渐增加,各个条线的流向规则会越来越复杂,各个条线甚至整个CRM的线索流转规则急需一种树形的可视化的图来表达。 技术背景:在开发之前考虑了三种方案,原生canvas、fabric以及G6,三种方案各有优劣势 原生ca

    2024年02月07日
    浏览(23)
  • Pyecharts绘制图表大全——柱形图

    说明:本文代码资料等来源于Pyecharts官网,进行了一些重要节点的备注说明梳理,便于学习。 今日学习柱形图! 目录 百分比柱形图  x轴标签旋转  堆叠数据  动态宏观经济指标图  通过 dict 进行配置柱形图  区域选择组件配置项  区域缩放配置项  好全的工具箱!  类似于

    2024年02月05日
    浏览(45)
  • python绘制柱形图系列

    Python版本为:3.7.1;图表绘制包matplotlib、Seaborn、plotnine的版本分别为:2.2.3、0.9.0、0.5.1;数据处理包NumPy和Pandas的版本分别为:1.15.4和0.23.4。

    2024年02月08日
    浏览(27)
  • 【Python】如何使用matlibplot绘制3D柱形图

    (1)构造需要显示的数据 如下图所示,X坐标取值为[0,1,2,3,4],Y坐标取值为[0,1,2,3,4,5,6,7,8],每一个(X,Y)组合的值Z=X+Y,所需要绘制的图就是在X,Y所对应的坐标位置上面根据Z的值来绘制柱形图。 (2)坐标设置 将坐标网格化, X=[0,1,2,3,4],Y=[0,1,2,3,4,5,6,7,8]网格化的结果,如下图

    2024年02月16日
    浏览(25)
  • Matlab绘制中国区域DEM地形图

    要绘制中国区域的DEM地形图,需要经过以下详细步骤。在本例中,将使用MATLAB的Mapping Toolbox来处理DEM数据和绘制地形图,并选择一种美观的配色方案。 步骤1:准备DEM数据 首先,您需要准备一个包含中国区域DEM数据的文件。通常,DEM数据以高程值的矩阵形式存储。确保数据的

    2024年02月12日
    浏览(40)
  • 【Python】Python中使用Matplotlib绘制折线图、散点图、饼形图、柱形图和箱线图

    python数据可视化课程,实验二 Matplotlib 中文API:API 概览 | Matplotlib 一、实验任务的数据背景 提供的源数据(数据文件employee.csv)共拥有4个特征,分别为就业人员、第一产业就业人员、第二产业就业人员、第三产业就业人员。根据3个产业就业人员的数量绘制散点图和折线图。

    2023年04月15日
    浏览(71)
  • 可视化—AntV G6实现节点连线及展开收缩分组

    AntV 是蚂蚁金服全新一代数据可视化解决方案,主要包含数据驱动的高交互可视化图形语法G2,专注解决流程与关系分析的图表库 G6、适于对性能、体积、扩展性要求严苛的场景。 demo使用 数字 模拟真实的节点及分组数据。 combo 内的 nodes 亦是使用 随机数 生成,节点之前的

    2024年02月16日
    浏览(36)
  • Matplotlib可视化数据分析图表下(常用图表的绘制、折线图、柱形图、直方图、饼形图、散点图、面积图、热力图、箱形图、3D图表、绘制多个图表、双y轴可视化图表、颜色渐变图)

    本文来自《Python数据分析从入门到精通》_明日科技编著 本节介绍常用图表的绘制,主要包括绘制折线图、绘制柱形图、绘制直方图、绘制饼形图、绘制散点图、绘制面积图、绘制热力图、绘制箱型图、绘制3D图表、绘制多个子图表以及图表的保存。对于常用的图表类型以绘制

    2023年04月23日
    浏览(37)
  • 第五章. 可视化数据分析图表—常用图表的绘制4—箱形图,3D图表

    第五章. 可视化数据分析图 本节主要介绍常用图表的绘制,主要包括箱形图,3D柱形图,3D曲面图。 ·箱形图又称箱线图、盒须图或盒式图 ·用于显示一组数据分散情况的统计图 ·优点:不受异常值的影响,可以以一种相对稳定的方式描述数据的离散分布情况,也常用于异常值

    2024年02月03日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包