位置: IT常识 - 正文

G6绘制树形图(自定义节点、自定义边、自定义布局)(gh树形数据是什么意思)

编辑:rootadmin
G6绘制树形图(自定义节点、自定义边、自定义布局) 目录1 设计节点1.1 定义节点和文本1.2 增加节点1.3 自定义节点样式2 树图配置2.1 允许使用自定义dom节点2.2 内置行为自定义边layout布局demo1 设计节点

推荐整理分享G6绘制树形图(自定义节点、自定义边、自定义布局)(gh树形数据是什么意思),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:绘制树状图,绘制树状图,g6 画图,绘制树状图,gh树形数据,绘制csg树,如何绘制树形图,绘制树状图,内容如对您有帮助,希望把文章链接给更多的朋友!

在 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', });G6绘制树形图(自定义节点、自定义边、自定义布局)(gh树形数据是什么意思)

节点和文字生成后,再定义他们的相对位置 参考官网定义复杂图样式的方式: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, })

效果如下

1.2 增加节点

如果想为节点再增加一个小节点,并且位置随着大节点移动,如图 新增节点和文本 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>

本文链接地址:https://www.jiuchutong.com/zhishi/269500.html 转载请保留说明!

上一篇:鸿蒙系统识别文字功能在哪? 鸿蒙提取图片文字的技巧(鸿蒙系统获取电脑文件)

下一篇:win10锁屏聚焦功能不更新教程(win10锁屏界面windows聚焦什么意思)

  • 抖音粉丝等级在哪看(抖音怎么涨到1000粉丝)

    抖音粉丝等级在哪看(抖音怎么涨到1000粉丝)

  • 华为平板如何连接投影仪(华为平板如何连接热点)

    华为平板如何连接投影仪(华为平板如何连接热点)

  • win7截图快捷键ctrl加什么(win7截图快捷键命令)

    win7截图快捷键ctrl加什么(win7截图快捷键命令)

  • 苹果13锁屏怎么打开手电筒(苹果13锁屏怎么显示农历日期)

    苹果13锁屏怎么打开手电筒(苹果13锁屏怎么显示农历日期)

  • 小米指纹锁指纹无反应(小米指纹锁指纹失灵)

    小米指纹锁指纹无反应(小米指纹锁指纹失灵)

  • 钉钉里听不到老师声音(钉钉听不到老师讲课声音)

    钉钉里听不到老师声音(钉钉听不到老师讲课声音)

  • 苹果11数据怎么开(苹果11数据怎么切换不了)

    苹果11数据怎么开(苹果11数据怎么切换不了)

  • 外播已转接来电是关机了吗(外拨已转接来电什么意思啊)

    外播已转接来电是关机了吗(外拨已转接来电什么意思啊)

  • oppo相机防抖怎么设置(oppo手机相机防抖功能在哪里找?)

    oppo相机防抖怎么设置(oppo手机相机防抖功能在哪里找?)

  • 华为p7忘记开机密码(华为p7忘记锁屏密码)

    华为p7忘记开机密码(华为p7忘记锁屏密码)

  • 小米10pro几倍光学变焦(小米10pro是什么调光)

    小米10pro几倍光学变焦(小米10pro是什么调光)

  • 电脑自动打字怎么回事(电脑自动打字怎么关)

    电脑自动打字怎么回事(电脑自动打字怎么关)

  • 华为sa/nsa双模什么意思(华为sa+nsa模式是什么意思)

    华为sa/nsa双模什么意思(华为sa+nsa模式是什么意思)

  • 魅族17pro支持多少倍变焦(魅族17pro支持多少w无线充电)

    魅族17pro支持多少倍变焦(魅族17pro支持多少w无线充电)

  • 电脑按任何键都没反应(电脑按任何键都会开机)

    电脑按任何键都没反应(电脑按任何键都会开机)

  • 戴尔笔记本死机怎么强行重启(戴尔笔记本怎么开机)

    戴尔笔记本死机怎么强行重启(戴尔笔记本怎么开机)

  • word字体改完又还原(word字体)

    word字体改完又还原(word字体)

  • 主板红灯一直亮(主板红灯一直亮代表什么)

    主板红灯一直亮(主板红灯一直亮代表什么)

  • avi文件用啥打开(avi的文件)

    avi文件用啥打开(avi的文件)

  • 笔记本为什么很少用雾面屏(笔记本为什么很少用AMD显卡)

    笔记本为什么很少用雾面屏(笔记本为什么很少用AMD显卡)

  • qq有没有定时发送消息的功能(qq有没有定时发送)

    qq有没有定时发送消息的功能(qq有没有定时发送)

  • 计算机主板基本组成部分(计算机主板基本结构图)

    计算机主板基本组成部分(计算机主板基本结构图)

  • 美版蜂窝国内能用吗(apple 美版蜂窝)

    美版蜂窝国内能用吗(apple 美版蜂窝)

  • 苹果11原相机怎么设置正方形 (苹果11原相机怎么调才好看)

    苹果11原相机怎么设置正方形 (苹果11原相机怎么调才好看)

  • 手机照片怎么缩小尺寸(手机照片怎么缩放比例)

    手机照片怎么缩小尺寸(手机照片怎么缩放比例)

  • 天猫小黑盒怎么进入(天猫小黑盒怎么领取红包)

    天猫小黑盒怎么进入(天猫小黑盒怎么领取红包)

  • nova3怎么调闪充(nova3 快充)

    nova3怎么调闪充(nova3 快充)

  • 美团怎么看消费总额(美团怎么看消费总共花了多少钱)

    美团怎么看消费总额(美团怎么看消费总共花了多少钱)

  • 敏感资源无法加速怎么办

    敏感资源无法加速怎么办

  • 使用ffmpeg把mp4与m3u8相互转换的操作(ffmpeg png to mp4)

    使用ffmpeg把mp4与m3u8相互转换的操作(ffmpeg png to mp4)

  • 分期收款企业所得税调整
  • 属于印花税征税对象的是
  • 企业所得税固定资产
  • 个人所得税分摊方式月扣除金额修改
  • 借条时间到了怎么续
  • 个税手续费返还政策文件
  • 开了票印花税必须报吗
  • 准予结转以后年度怎么算
  • 固定资产台账登记表明细科目写什么
  • 工会经费计入应付职工薪酬
  • 办公室租赁合同需要交那些税?
  • 增值税普通发票怎么开
  • 金税盘使用说明
  • 可抵扣进项税怎么抵扣
  • 普票红冲后原件没有了怎么办
  • 设定提存计划怎么填
  • 高速公路通行费抵扣最新规定
  • 可转换债券赎回和回售如何理解
  • 手动添加mac
  • saproxy.exe - saproxy是什么进程 有什么用
  • 公司估值一般不超过市值多少
  • 企业对外捐赠的税法处理
  • 增值税纳税人申报表怎么填写
  • 电脑用久了会出现什么问题
  • php array_replace
  • php可变参数
  • PHPfor循环语句10的阶乘
  • 税务登记后每个月交什么钱
  • php数组函数输出《咏雪》里有多少"片"字
  • 企业预付工程款的会计分录怎么做
  • 出口退税如何办退税手续
  • phpfread
  • 北坡镇人民政府
  • vscode怎么运行前端
  • 好用的5款国产手机推荐
  • 增值税抄税报税流程
  • WordPress 浏览量修改
  • 暂估入库后发票来不了
  • 农民专业合作社名词解释
  • 项目建设期算不算折旧
  • mysql常用命令汇总
  • mysql语句like用法
  • python uppercase函数
  • 收到现金投资计入什么科目
  • 电子税务局发票作废流程
  • 账龄划分中有借有贷怎么分析
  • mysql分页优化原理
  • 个人社保应不应该缴纳
  • 一般纳税人第一次逾期申报处罚吗
  • 股东往来款算投资款吗
  • 印花税计入相关资产成本吗
  • 产品销售的账务处理办法
  • 公司把钱打到银行了,银行未打到我工资卡
  • 申报财产租赁合同怎么写
  • 融资租赁与经营租赁的区别主要是
  • 公司往来借款怎么做账
  • 固定资产处置办法
  • 三证合一之前
  • 企业债与公司债的还款有区别
  • sql多表连接查询(详细实例)
  • mac如何通过终端启动
  • linux使用场合
  • linux的grep命令详解
  • win7定时关机没反应
  • 怎么给文件夹设置密码保护
  • 苹果mac查看
  • 为避免10月20号后盗版系统出现黑屏的bat文件
  • linux中tr命令
  • win10系统需不需要装杀毒软件
  • [置顶] 关于UNITY5.0和高通AR4.2.3在手机上白屏的问题
  • opengl 输入框
  • bootstrap弹出表单
  • 模块化开发app
  • 置顶怎么折叠起来
  • jQuery插件库
  • python的异常处理语句
  • jQuery获取checkbox选中的值
  • python中对象的概念
  • Python使用dis模块把Python反编译为字节码的用法详解
  • 国家电子税务局江苏省电子税务局
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设