位置: 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聚焦什么意思)

  • 微信隐藏聊天怎么显示出来(微信隐藏聊天怎么找回来)

    微信隐藏聊天怎么显示出来(微信隐藏聊天怎么找回来)

  • 苹果手机那个小圆圈怎么弄出来(苹果手机那个小圆点怎么设置)

    苹果手机那个小圆圈怎么弄出来(苹果手机那个小圆点怎么设置)

  • 华为nova5怎么设置永不休眠(华为nova5怎么设置锁屏密码)

    华为nova5怎么设置永不休眠(华为nova5怎么设置锁屏密码)

  • 抖音怎么快速取消点赞的作品(抖音怎么快速取消喜欢)

    抖音怎么快速取消点赞的作品(抖音怎么快速取消喜欢)

  • 华为手机怎样查看手机内存(华为手机怎样查找对方的手机位置)

    华为手机怎样查看手机内存(华为手机怎样查找对方的手机位置)

  • 苹果6sp耳机模式怎么取消(苹果6SP耳机模式在哪里关闭)

    苹果6sp耳机模式怎么取消(苹果6SP耳机模式在哪里关闭)

  • 奔图打印机状态黄灯亮(奔图打印机状态灯)

    奔图打印机状态黄灯亮(奔图打印机状态灯)

  • oppo账号如何禁止申诉(怎么禁止oppo账号登录游戏)

    oppo账号如何禁止申诉(怎么禁止oppo账号登录游戏)

  • 苹果手机连接apple id服务器出错是什么意思(苹果手机连接apple ID服务器时出错)

    苹果手机连接apple id服务器出错是什么意思(苹果手机连接apple ID服务器时出错)

  • 耳机40mm和50mm区别(耳机30mm和40mm)

    耳机40mm和50mm区别(耳机30mm和40mm)

  • 华为手电筒能调亮度么(华为手电筒能调节亮度吗怎么调)

    华为手电筒能调亮度么(华为手电筒能调节亮度吗怎么调)

  • 华为荣耀20怎么设置返回键(华为荣耀20怎么设置指纹解锁)

    华为荣耀20怎么设置返回键(华为荣耀20怎么设置指纹解锁)

  • 微信朋友圈秒赞是设置的吗(微信朋友圈秒赞网站)

    微信朋友圈秒赞是设置的吗(微信朋友圈秒赞网站)

  • 戴尔进bios快捷键是什么(戴尔bios快速启动设置)

    戴尔进bios快捷键是什么(戴尔bios快速启动设置)

  • 用户不存在是拉黑了吗(用户不存在是拉黑还是删除 用其他微信号可以搜得到)

    用户不存在是拉黑了吗(用户不存在是拉黑还是删除 用其他微信号可以搜得到)

  • 微信调成听筒模式怎么调回来

    微信调成听筒模式怎么调回来

  • oppo手表能连苹果手机吗(oppo手表能不能连苹果手机)

    oppo手表能连苹果手机吗(oppo手表能不能连苹果手机)

  • qq语音通话怎么开摄像头(qq语音通话怎么关闭对方声音)

    qq语音通话怎么开摄像头(qq语音通话怎么关闭对方声音)

  • iphone11promax能不能反向充电(苹果11pro max能支持无线充电吗)

    iphone11promax能不能反向充电(苹果11pro max能支持无线充电吗)

  • 隔空传送正在等待是什么意思(隔空传送显示正在转换是什么)

    隔空传送正在等待是什么意思(隔空传送显示正在转换是什么)

  • 隔空投送会被对方发现吗(隔空投送会被对方监控吗)

    隔空投送会被对方发现吗(隔空投送会被对方监控吗)

  • 卡点视频怎么做教程(照片卡点视频怎么做)

    卡点视频怎么做教程(照片卡点视频怎么做)

  • 小米8支持5g网络不(小米支持5g网络吗)

    小米8支持5g网络不(小米支持5g网络吗)

  • 抖音怎样取关所有人(取消抖音的关注怎么弄)

    抖音怎样取关所有人(取消抖音的关注怎么弄)

  • 三级网络技术知识点(三级网络技术题库有多少套?)

    三级网络技术知识点(三级网络技术题库有多少套?)

  • 怎么看电脑ip(win10怎么看电脑ip)

    怎么看电脑ip(win10怎么看电脑ip)

  • stk一al00是华为什么型号详情(stk al00华为)

    stk一al00是华为什么型号详情(stk al00华为)

  • 若依(RuoYi )权限管理设计(若依和pig)

    若依(RuoYi )权限管理设计(若依和pig)

  • 深度学习之bottleneck layer

    深度学习之bottleneck layer

  • 期间费用明细表怎么填
  • 公司购车需要公章吗
  • 经营活动现金流量公式
  • 支付的工会经费现金流量项目是什么?
  • 个体生产经营所得税税率表
  • 收到政府补助的固定资产的账务处理
  • 未收到增值税专用发票
  • 开票为单位 收款为个人
  • 公司开租房发票,税钱由公司承担
  • 停车费属于不动产租赁服务税率
  • 联营和合营的区别共同控制
  • 债转股的税收政策
  • 以前年度城建税减免可以计去营业外收入嘛
  • 收境外的钱
  • 专票密码区出来一点能报吗
  • 计提的跌价准备销售时怎么处理
  • 在win7系统中安装win10
  • 怎么获取免费的腾讯vip
  • 在mac上安装ios应用
  • PHP:__halt_compiler()的用法_misc函数
  • 股本减少的账务怎么处理
  • 机关事业单位体检费用标准规定
  • “Ninja is required to load C++ extensions”解决方案
  • php获取ftp文件目录
  • 【机器学习】9种回归算法及实例总结,建议学习收藏
  • show version命令详解
  • 10-Ajax&Vue
  • 通用机打发票可以查验吗
  • 公司主营业务和经营范围的区别
  • 企业所得税季度申报表怎么填
  • python如何获取列表元素
  • 收到汽车会计分录
  • mysql复制一条记录
  • 长期股权投资成本法
  • sqlserver 进程死锁
  • 详解中国女足出线形势
  • 在业和存续的区别
  • 房屋租赁房产税如何征收
  • 交易性金融资产是什么意思
  • 累计折旧计提会计分录
  • 收回发票会计分录
  • 用材料抵扣货款通知怎样写
  • 土地转移是什么意思
  • 中小微企业有哪些企业
  • 税收优惠退回的税金怎么入账
  • 制造费用可以计提吗
  • 以前年度损益科目类别怎么填
  • 总公司费用能分摊到分公司吗
  • 收到厂家返利账务处理
  • 小规模纳税人减按1%政策
  • 收到银行利息记什么会计科目
  • 出售未计提完折旧的固定资产需要折扣吗
  • 会计档案销毁方案怎么写
  • mysql 5.7.17 winx64安装配置教程
  • Win10系统如何取消密码
  • 如何使用mac book
  • 添加到右键快捷方式
  • Ubuntu10.10 Zend FrameWork配置方法及helloworld显示
  • mac安装软件提示无法检查更新
  • linux设置用户的密码
  • spools.exe - spools是什么进程 有何作用
  • android程序的基本结构
  • c#多线程应用
  • js的forin
  • jquery购物车商品价格计算
  • node.js gui
  • unity控制相机旋转
  • jquery中的动画方法有哪些
  • Python简单格式化时间的方法【strftime函数】
  • android studio项目无法运行
  • 个人出租住房增值税免税政策
  • 浦发银行企业银行电话
  • 厦门市地方税务局市稽查局关于规范稽查有关规定
  • 税源管理科是干什么的工作
  • 海口市地方税务局是什么级别
  • 发货确认收入还是以开票确认收入
  • 苏州税务局下班时间
  • 国网成都市局和绵阳市局的区别?
  • 餐饮票抵扣成本怎么算
  • 舆论与舆情之间的关系是怎样的?
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设