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

  • 抖音号可以改吗(抖音号可以改吗可以改几次)

    抖音号可以改吗(抖音号可以改吗可以改几次)

  • 苹果13promax怎么设置返回键(苹果13promax怎么显示电池电量百分比)

    苹果13promax怎么设置返回键(苹果13promax怎么显示电池电量百分比)

  • 移动二次认证怎么在微信打开(移动二次认证怎么弄)

    移动二次认证怎么在微信打开(移动二次认证怎么弄)

  • 拼多多违规关店保证金能退回来吗(拼多多违规关店里面的资金能提出来吗)

    拼多多违规关店保证金能退回来吗(拼多多违规关店里面的资金能提出来吗)

  • 华为充电头叫什么(华为充电头叫什么英文)

    华为充电头叫什么(华为充电头叫什么英文)

  • 小米10pro多少克(小米10Pro多少克)

    小米10pro多少克(小米10Pro多少克)

  • excel表格不对称怎么做(表格中不对称的表格怎么做)

    excel表格不对称怎么做(表格中不对称的表格怎么做)

  • 手机卡三个月不交费会自动注销吗(手机卡三个月不用就自动注销了吗)

    手机卡三个月不交费会自动注销吗(手机卡三个月不用就自动注销了吗)

  • 连接速度866mbps是什么意思(连接速度866mbps是千兆路由器吗)

    连接速度866mbps是什么意思(连接速度866mbps是千兆路由器吗)

  • 抖音最多能发多长的视频(抖音最多能发多少秒)

    抖音最多能发多长的视频(抖音最多能发多少秒)

  • 快手个性化设置没有音乐(快手个性化设置是什么意思)

    快手个性化设置没有音乐(快手个性化设置是什么意思)

  • 自动回复怎么设置微信(自动回复怎么设置QQ)

    自动回复怎么设置微信(自动回复怎么设置QQ)

  • 信号弱或没有信号是怎么回事(信号弱或没有信号是怎么回事三星电视)

    信号弱或没有信号是怎么回事(信号弱或没有信号是怎么回事三星电视)

  • 怎么登录快手(怎么登录快手极速版)

    怎么登录快手(怎么登录快手极速版)

  • 华为视频文件存在哪里(华为视频文件存放目录)

    华为视频文件存在哪里(华为视频文件存放目录)

  • word文档行距怎么调(word文档怎么设置)

    word文档行距怎么调(word文档怎么设置)

  • 小米9pro5 g能用4g网吗(小米9pro支持5g频段有哪些)

    小米9pro5 g能用4g网吗(小米9pro支持5g频段有哪些)

  • 手机qq音乐怎么改音效(手机qq音乐怎么控制电脑的qq音乐)

    手机qq音乐怎么改音效(手机qq音乐怎么控制电脑的qq音乐)

  • 抖音怎么评论发表情包(抖音怎么评论发图片不是表情包)

    抖音怎么评论发表情包(抖音怎么评论发图片不是表情包)

  • 苹果云空间怎么打开(苹果云空间怎么找回照片和视频)

    苹果云空间怎么打开(苹果云空间怎么找回照片和视频)

  • b612没保存怎么找回(b612没保存的视频还能找回吗)

    b612没保存怎么找回(b612没保存的视频还能找回吗)

  • 华为平板m6操作说明(华为平板m6使用教程)

    华为平板m6操作说明(华为平板m6使用教程)

  • uc浏览器私密空间在哪(uc浏览器私密空间被和谐怎么恢复)

    uc浏览器私密空间在哪(uc浏览器私密空间被和谐怎么恢复)

  • 苹果手机描述文件不可移除怎么办 (苹果手机描述文件删除了如何恢复)

    苹果手机描述文件不可移除怎么办 (苹果手机描述文件删除了如何恢复)

  • 如何配置openai的返回Stream数据并转发到h5页面按markdown格式流式输出(怎么配置opencv)

    如何配置openai的返回Stream数据并转发到h5页面按markdown格式流式输出(怎么配置opencv)

  • 什么时候要计提坏账准备
  • 买车交的保险
  • 个体户生产经营个人所得税
  • 购货方付款会计分录
  • 办公家具可以一次性税前扣除吗
  • 其他应收款要做账吗
  • 政府专项补助资金需要交所得税吗
  • 政府减免税款如何账务处理
  • 异地预缴企业所得税
  • 税务是如何处理违章的
  • 小规模纳税人超过500万可以不转一般纳税人吗
  • 保险费可以抵扣嘛
  • 销售给个人的货款要走公户吗
  • 普通发票年份代码有什么具体含义?
  • 资本化和费用化的条件
  • 工程项目产生的沙石怎么处理
  • 公司名下没车能报油费吗
  • 报销时可以一个人报销吗
  • 微信提现收取手续费多少钱
  • spss安装后无法启动许可证授权向导
  • 印花税本月计提本月缴纳
  • 如果在XP系统中QQ音乐听不了怎么办?
  • 如何在没有开瓶器的情况下开红酒
  • bios boot设置
  • bash是什么命令
  • 在企业所得税前扣除的有哪些
  • 销售货款未收回应该从工资中扣除吗
  • 冲销以前年度多计提的工资资产负债表怎么平
  • 关于出售使用过的车辆
  • 固定资产公允价值变动会计处理
  • 留底的进项税额记哪儿?
  • 建筑业主营业务收入二级科目有哪些
  • PHP:pcntl_wait()的用法_PCNTL函数
  • 雪山上的老鼠
  • vue项目如何配置启动的端口
  • 小规模纳税人报税期是哪几个月
  • 朝圣者将风马旗扔向甘登寺上方的空中为新年祈福,中国西藏 (© Ian Cumming/plainpicture)
  • 工程结算在资产负债表中哪个科目显示
  • 小程序和公众号可以同名吗
  • vue封装组建
  • 128种chatGPT可以为人类做的事情
  • Code For Better 谷歌开发者之声——使用谷歌浏览器 Chrome 更好地调试
  • Ant Design-vue 解决input前后空格问题(推荐)
  • 外币应收账款汇兑损失计入
  • 富文本功能
  • 食品财务走账
  • Python中__slots__限制属性
  • 织梦怎么用模板建站
  • 经审计的财务报表是否要会计师事务所盖章
  • 固定资产账务处理候文江视频
  • 以前年度损益科目编码
  • 会计差错更正的会计处理方法
  • 税总函2019是什么意思
  • 税控维护费是什么意思
  • 残保金是公司交还是员工交
  • 贷款买车成功后,给客户什么手续
  • sqlserver的游标
  • freebsd windows
  • atwakeup进程
  • xp注册表损坏怎么修复
  • win10预览版退回正式版
  • 服务器centos6.8安装教程
  • mac 应用
  • 进去界面黑屏
  • windows8快速启动设置
  • window10路由
  • windows10不能装windows7
  • Extjs TriggerField在弹出窗口显示不出问题的解决方法
  • bootstrap tooltip
  • jquery使用jsonp
  • 详解 linux mysqldump 导出数据库、数据、表结构
  • python解析数据
  • js获取tbody
  • shell操作
  • win安装nodejs
  • jquery调用json数据
  • 基于mvc的项目实例
  • 省委巡视组收到的案件线索怎么分配
  • 智能财税证书含金量如何
  • 企业税务人员岗位竞聘报告怎么写
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设