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

  • javascript function(javascript function 对象)

    javascript function(javascript function 对象)

  • iqooz5电池容量(iqooz5电池容量怎么查)

    iqooz5电池容量(iqooz5电池容量怎么查)

  • 抖音电脑直播需要什么(抖音电脑直播需要手机吗)

    抖音电脑直播需要什么(抖音电脑直播需要手机吗)

  • 电脑的序列号从哪看(电脑 序列号)

    电脑的序列号从哪看(电脑 序列号)

  • WPS分页符怎么设置(wps里的分页符怎么用)

    WPS分页符怎么设置(wps里的分页符怎么用)

  • 微信发送图片限制多少M(微信发送图片不能超过多大)

    微信发送图片限制多少M(微信发送图片不能超过多大)

  • 快手注销账号要多久,可以重新注册(快手注销账号要多久才搜索不到)

    快手注销账号要多久,可以重新注册(快手注销账号要多久才搜索不到)

  • 手机接收验证码总是延迟(手机接收验证码三门子)

    手机接收验证码总是延迟(手机接收验证码三门子)

  • 苹果腾讯视频付费系统繁忙(苹果腾讯视频付费方式)

    苹果腾讯视频付费系统繁忙(苹果腾讯视频付费方式)

  • 苹果耳机突然有滋啦声(苹果耳机突然有电流声)

    苹果耳机突然有滋啦声(苹果耳机突然有电流声)

  • 手机OTG从哪里找

    手机OTG从哪里找

  • 物联网终端主要分为哪几种类型(物联网终端设备有哪些)

    物联网终端主要分为哪几种类型(物联网终端设备有哪些)

  • 苹果7p怎么双击截屏(苹果7p怎么双击截屏手机屏幕)

    苹果7p怎么双击截屏(苹果7p怎么双击截屏手机屏幕)

  • 华为nova5支持什么快充(华为nova5支持什么卡)

    华为nova5支持什么快充(华为nova5支持什么卡)

  • iphone8plus屏幕刷新率(苹果8p屏幕失灵刷机可以解决吗)

    iphone8plus屏幕刷新率(苹果8p屏幕失灵刷机可以解决吗)

  • 华为手机是不是有两个系统(华为手机是不是有两个空间)

    华为手机是不是有两个系统(华为手机是不是有两个空间)

  • 华为10plus充电要多久(华为p10充电多长时间)

    华为10plus充电要多久(华为p10充电多长时间)

  • 华为mate30是单卡还是双卡(华为mate30 单卡)

    华为mate30是单卡还是双卡(华为mate30 单卡)

  • 标书目录算页码吗(标书目录页码错了会废标吗)

    标书目录算页码吗(标书目录页码错了会废标吗)

  • 淘宝店铺授权怎样操作(淘宝店铺授权怎么弄?具体流程是什么)

    淘宝店铺授权怎样操作(淘宝店铺授权怎么弄?具体流程是什么)

  • DES是什么类型的密码(des属于( ))

    DES是什么类型的密码(des属于( ))

  • 微博怎么设置虚拟位置(微博怎么设置虚拟抽奖)

    微博怎么设置虚拟位置(微博怎么设置虚拟抽奖)

  • sql优化的几种方法(sql优化的几种方法有哪些)

    sql优化的几种方法(sql优化的几种方法有哪些)

  • 抖音的水印在哪设置(抖音视频水印在哪)

    抖音的水印在哪设置(抖音视频水印在哪)

  • 小米语言设置在哪里(小米语言设置在哪里打开)

    小米语言设置在哪里(小米语言设置在哪里打开)

  • 手机怎么把图片反色(手机怎么把图片转换成pdf)

    手机怎么把图片反色(手机怎么把图片转换成pdf)

  • qt5core.dll丢失解决方法

    qt5core.dll丢失解决方法

  • dluca.exe是不是病毒程序 dluca进程有什么危害(daio.dll 病毒)

    dluca.exe是不是病毒程序 dluca进程有什么危害(daio.dll 病毒)

  • 二维码基本原理(二维码的实现原理和实现过程)

    二维码基本原理(二维码的实现原理和实现过程)

  • 一般纳税人税费计算明细表
  • 货物已到发票未开具
  • 暂估收入的必要条件
  • 利润表的上期金额是指全年吗
  • 核定征收财务报表
  • 多付银行承兑退回的会计分录怎么写?
  • 偿还不起债务大约能判多少年
  • 盘亏存货需要进项税额转出吗
  • 外资企业银行贷款限制
  • 补税款的分录应该怎么写
  • 公司法人信息变更是先去税务局还是先去银行
  • 单位房子没有房产证能不能买
  • 政府机关开票是普票还是专票
  • 增值税开票资料没有电话可以吗
  • 小型微利企业所得税优惠政策
  • 腾讯计算机系统扣费15元怎么查
  • 改扩建要计提折旧吗
  • 财务报表提供的信息仅对外部的投资者和债权人有用
  • 上个月没有报个税这个月一起报
  • 不抵扣勾选怎么挽回
  • 会务费税目
  • 外地餐费计入什么账户
  • 个体定税标准
  • 技术发明案例
  • 营改增劳务费增值税率
  • win11和win10哪个玩游戏好
  • 个税退手续费要多久到账
  • 应收账款的账面余额公式
  • 鸿蒙系统获取电脑文件
  • 材料暂估的会计分录怎么做
  • 加拿大沿海城市有哪些
  • 专项用途财政性资金纳税调整明细表
  • 明细分类帐户
  • 会计科目明细分类科目表
  • php \t
  • 购买生产用品计入什么科目
  • 单位未足额缴纳社保可以补交几年
  • 企业借款会计处理
  • php语言采用什么方式执行
  • sortable js
  • vue怎么用bootstrap
  • dhclient卡住
  • ps笔刷在哪里调
  • 酒店押金的账务处理
  • 资产负债表上应付账款是负数
  • 物权变动的原因是什么
  • 一般人转小规模政策文号
  • 政府专项补贴税收
  • 企业金融资产包括银行存款吗
  • 非营利企业的劳动力需求有哪些特点
  • 残疾人就业保障金怎么计算
  • 购入农产品的增值税税率是多少
  • 扫描仪有危害吗
  • 抵账的房子怎么办理房产证
  • 总资产法怎么计算公式
  • 服装厂布料都是在哪里进的
  • 财务的材质怎么写
  • 货款扣除质量赔款
  • 任何单位和个人都应当()为报警
  • 收入的利息如何计算
  • 办土地证费用会退吗
  • 工业企业中制造费用包括哪些内容
  • mysql5.7压缩包
  • centos7怎么配置yum源
  • win8怎么一开机就进入桌面
  • Tech Ed 2008:HPC Server 2008讲解
  • freebsd ifconfig
  • win8开始界面如何设置成win7
  • linux历史操作
  • linux文件一般放哪里
  • js中iframe
  • excel的窗口包含什么
  • jQuery Ajax请求后台数据并在前台接收
  • 解决的英文
  • 梦见擦窗户框
  • javascript模拟器
  • js常用继承
  • 安卓通知栏管理工具
  • 百望税控盘电子发票怎么打
  • 股权收购的好处
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设