【AntV-G6】决策树简单实例

2023-12-14 13:59
文章标签 简单 实例 决策树 antv g6

本文主要是介绍【AntV-G6】决策树简单实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用步骤

  1. 复制treeMind.vue文件到前端页面目录,
  2. 在需要展示的页面引入treeMind
  3. npm install错误解决方法

树组件 treeMind.vue

<template><div id="container"></div>
</template><script>
import insertCss from "insert-css";
import G6 from "@antv/g6";export default {name: "treeMind",props: {// 数据data: {type: Object,default() {return {};}},//高height: {type: Number,default() {return 500;}}},data() {return {}},mounted() {},methods: {init(mockData) {insertCss(`.g6-component-tooltip {background-color: rgba(0,0,0, 0.65);padding: 10px;box-shadow: rgb(174, 174, 174) 0px 0px 10px;width: fit-content;color: #fff;border-radius = 4px;}`);const colors = {BLUE: '#5B8FF9',RED: '#F46649',YELLOW: '#EEBC20',GREEN: '#5BD8A6',DARKGREY: '#A7A7A7',BROWN: '#A98847',};//  组件propsconst props = {data: mockData ? mockData : this.data,config: {padding: [20, 50],defaultLevel: 3,defaultZoom: 0.8,modes: {default: ['zoom-canvas', 'drag-canvas']},},};const container = document.getElementById('container');const width = container.scrollWidth;const height = container.scrollHeight || 600;// 默认配置const defaultConfig = {width,height,modes: {default: ['zoom-canvas', 'drag-canvas'],},fitView: true,animate: true,defaultNode: {type: 'flow-rect',},defaultEdge: {type: 'cubic-horizontal',style: {stroke: '#CED4D9',},},layout: {type: 'indented',direction: 'LR',dropCap: false,indent: 300,getHeight: () => {return 60;},},};// 自定义节点、边const registerFn = () => {/*** 自定义节点*/G6.registerNode('flow-rect',{shapeType: 'flow-rect',draw(cfg, group) {const {name = '',qty,collapsed,unit,color,rate} = cfg;const grey = '#CED4D9';const rectConfig = {width: 140,height: 50,lineWidth: 1,fontSize: 12,fill: '#fff',radius: 4,stroke: grey,opacity: 1,};const nodeOrigin = {x: -rectConfig.width / 2,y: -rectConfig.height / 2,};const textConfig = {//center / end / left / right / starttextAlign: 'center',//top / middle / bottom / alphabetic / hangingtextBaseline: 'bottom',};const rect = group.addShape('rect', {attrs: {x: nodeOrigin.x,y: nodeOrigin.y,...rectConfig,},});const rectBBox = rect.getBBox();// namegroup.addShape('text', {attrs: {...textConfig,x: 0 ,y: 20 + nodeOrigin.y,text: name.length > 28 ? name.substr(0, 28) + '...' : name,fontSize: 12,opacity: 0.85,fill: '#000',cursor: 'pointer',},name: 'name-shape',});// qtyconst qtyLabel = group.addShape('text', {attrs: {...textConfig,//调整数量的宽x: -4,y: rectBBox.maxY - 12,text: qty,fontSize: 12,fill: '#000',opacity: 0.85,},});//  unitgroup.addShape('text', {attrs: {...textConfig,//调整单位的宽x: qtyLabel.getBBox().maxX +20,y: rectBBox.maxY - 12,text: unit,fontSize: 12,fill: '#000',opacity: 0.75,},});// bottom line backgroundconst bottomBackRect = group.addShape('rect', {attrs: {x: nodeOrigin.x,y: rectBBox.maxY - 4,width: rectConfig.width,height: 4,radius: [0, 0, rectConfig.radius, rectConfig.radius],fill: '#E0DFE3',},});// bottom percentconst bottomRect = group.addShape('rect', {attrs: {x: nodeOrigin.x,y: rectBBox.maxY - 4,width: rate * rectBBox.width,height: 4,radius: [0, 0, rectConfig.radius, rectConfig.radius],fill: colors[color],},});// collapse rectif (cfg.children && cfg.children.length) {group.addShape('rect', {attrs: {x: rectConfig.width / 2 - 8,y: -8,width: 16,height: 16,stroke: 'rgba(0, 0, 0, 0.25)',cursor: 'pointer',fill: '#fff',},name: 'collapse-back',modelId: cfg.id,});// collpase textgroup.addShape('text', {attrs: {x: rectConfig.width / 2,y: 0,textAlign: 'center',textBaseline: 'middle',text: collapsed ? '+' : '-',fontSize: 16,cursor: 'pointer',fill: 'rgba(0, 0, 0, 0.25)',},name: 'collapse-text',modelId: cfg.id,});}this.drawLinkPoints(cfg, group);return rect;},update(cfg, item) {const {level, color, name} = cfg;const group = item.getContainer();let mask = group.find(ele => ele.get('name') === 'mask-shape');let maskLabel = group.find(ele => ele.get('name') === 'mask-qty-shape');if (level === 0) {group.get('children').forEach(child => {if (child.get('name')?.includes('collapse')) return;child.hide();})if (!mask) {mask = group.addShape('rect', {attrs: {x: -101,y: -30,width: 202,height: 60,opacity: 0,fill: colors[color]},name: 'mask-shape',});maskLabel = group.addShape('text', {attrs: {fill: '#fff',fontSize: 20,x: 0,y: 10,text: name.length > 28 ? name.substr(0, 16) + '...' : name,textAlign: 'center',opacity: 0,},name: 'mask-qty-shape',});const collapseRect = group.find(ele => ele.get('name') === 'collapse-back');const collapseText = group.find(ele => ele.get('name') === 'collapse-text');collapseRect?.toFront();collapseText?.toFront();} else {mask.show();maskLabel.show();}mask.animate({opacity: 1}, 200);maskLabel.animate({opacity: 1}, 200);return mask;} else {group.get('children').forEach(child => {if (child.get('name')?.includes('collapse')) return;child.show();})mask?.animate({opacity: 0}, {duration: 200,callback: () => mask.hide()});maskLabel?.animate({opacity: 0}, {duration: 200,callback: () => maskLabel.hide()});}this.updateLinkPoints(cfg, group);},setState(name, value, item) {if (name === 'collapse') {const group = item.getContainer();const collapseText = group.find((e) => e.get('name') === 'collapse-text');if (collapseText) {if (!value) {collapseText.attr({text: '-',});} else {collapseText.attr({text: '+',});}}}},getAnchorPoints() {return [[0, 0.5],[1, 0.5],];},},'rect',);G6.registerEdge('flow-cubic',{getControlPoints(cfg) {let controlPoints = cfg.controlPoints; // 指定controlPointsif (!controlPoints || !controlPoints.length) {const {startPoint, endPoint, sourceNode, targetNode} = cfg;const {x: startX, y: startY, coefficientX, coefficientY} = sourceNode? sourceNode.getModel(): startPoint;const {x: endX, y: endY} = targetNode ? targetNode.getModel() : endPoint;let curveStart = (endX - startX) * coefficientX;let curveEnd = (endY - startY) * coefficientY;curveStart = curveStart > 40 ? 40 : curveStart;curveEnd = curveEnd < -30 ? curveEnd : -30;controlPoints = [{x: startPoint.x + curveStart, y: startPoint.y},{x: endPoint.x + curveEnd, y: endPoint.y},];}return controlPoints;},getPath(points) {const path = [];path.push(['M', points[0].x, points[0].y]);path.push(['C',points[1].x,points[1].y,points[2].x,points[2].y,points[3].x,points[3].y,]);return path;},},'single-line',);};registerFn();const {data} = props;let graph = null;const initGraph = (data) => {if (!data) {return;}const {onInit, config} = props;const tooltip = new G6.Tooltip({// offsetX and offsetY include the padding of the parent containeroffsetX: 20,offsetY: 30,// the types of items that allow the tooltip show up// 允许出现 tooltip 的 item 类型itemTypes: ['node'],// custom the tooltip's content// 自定义 tooltip 内容getContent: (e) => {const outDiv = document.createElement('div');//outDiv.style.padding = '0px 0px 20px 0px';const nodeName = e.item.getModel().name;let formatedNodeName = '';for (let i = 0; i < nodeName.length; i++) {formatedNodeName = `${formatedNodeName}${nodeName[i]}`;if (i !== 0 && i % 20 === 0) formatedNodeName = `${formatedNodeName}<br/>`;}outDiv.innerHTML = `${formatedNodeName}`;return outDiv;},shouldBegin: (e) => {if (e.target.get('name') === 'name-shape' || e.target.get('name') === 'mask-qty-shape') return true;return false;},});graph = new G6.TreeGraph({container: 'container',...defaultConfig,...config,plugins: [tooltip],});if (typeof onInit === 'function') {onInit(graph);}graph.data(data);graph.render();const handleCollapse = (e) => {const target = e.target;const id = target.get('modelId');const item = graph.findById(id);const nodeModel = item.getModel();nodeModel.collapsed = !nodeModel.collapsed;graph.layout();graph.setItemState(item, 'collapse', nodeModel.collapsed);};graph.on('collapse-text:click', (e) => {handleCollapse(e);});graph.on('collapse-back:click', (e) => {handleCollapse(e);});// 监听画布缩放,缩小到一定程度,节点显示缩略样式let currentLevel = 1;const briefZoomThreshold = Math.max(graph.getZoom(), 0.5);graph.on('viewportchange', e => {if (e.action !== 'zoom') return;const currentZoom = graph.getZoom();let toLevel = currentLevel;if (currentZoom < briefZoomThreshold) {toLevel = 0;} else {toLevel = 1;}if (toLevel !== currentLevel) {currentLevel = toLevel;graph.getNodes().forEach(node => {graph.updateItem(node, {level: toLevel})})}});};initGraph(data);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>
#container {width: 100%;height: 100%;
}
</style>

引入treeMind.vue (复制到页面)


<template><treeMind ref="treeMind"></treeMind>
</template><script>
export default {name: "tree",components: {TreeMind,},data() {return {//树的数据格式(可自定义字段,需更改treeMind.vue中的参数)treeData: {}};},methods: {// 获取后端传的树格式数据,如 treeDataqueryTreeData(data).then((res) => {if (res.status == 0) {// 初始化树组件,并传入树格式的数据res.data[0]this.$nextTick(() => {this.$refs.treeMind.init(res.data[0]);});} else {this.$message.error(res.msg);}});}
}
</script>

树的数据格式如下(可自定义字段)

treeData: {id: 'g1',name: 'Name1',qty: '538.90',unit: 'Yuan',rate: 1.0,color: 'BULE',children: [{id: 'g12',name: 'Deal with LONG qty',qty: '338.00',unit: 'Yuan',rate: 1,color: 'RED',children: [{id: 'g121',name: 'Name3',collapsed: true,qty: '138.00',rate: 1,color: 'BULE',unit: 'Yuan',children: [],},{id: 'g122',name: 'Name5',collapsed: true,qty: '100.00',rate: 1,color: 'GREEN',unit: 'Yuan',children: [],},],},{id: 'g13',name: 'Name9',qty: '100.90',rate: 1,color: 'BULE',unit: 'Yuan',children: [{id: 'g131',name: 'Name10',qty: '33.90',rate: 1,color: 'RED',unit: 'Yuan',children: [],},{id: 'g132',name: 'Name11',qty: '67.00',rate: 1,color: 'GREEN',unit: 'Yuan',children: [],},],},{id: 'g14',name: 'Name12',qty: '100.00',rate: 1,color: 'GREEN',unit: 'Yuan',children: [],},],}

npm install报错

在这里插入图片描述

typescript版本问题,图片上是3.9.10,在package.json文件里更改为"typescript": "^4.2.4",再重新npm install即可

这篇关于【AntV-G6】决策树简单实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/492796

相关文章

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知

Java Stream流以及常用方法操作实例

《JavaStream流以及常用方法操作实例》Stream是对Java中集合的一种增强方式,使用它可以将集合的处理过程变得更加简洁、高效和易读,:本文主要介绍JavaStream流以及常用方法... 目录一、Stream流是什么?二、stream的操作2.1、stream流创建2.2、stream的使用2.

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

Python跨文件实例化、跨文件调用及导入库示例代码

《Python跨文件实例化、跨文件调用及导入库示例代码》在Python开发过程中,经常会遇到需要在一个工程中调用另一个工程的Python文件的情况,:本文主要介绍Python跨文件实例化、跨文件调... 目录1. 核心对比表格(完整汇总)1.1 自定义模块跨文件调用汇总表1.2 第三方库使用汇总表1.3 导

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

MySQL多实例管理如何在一台主机上运行多个mysql

《MySQL多实例管理如何在一台主机上运行多个mysql》文章详解了在Linux主机上通过二进制方式安装MySQL多实例的步骤,涵盖端口配置、数据目录准备、初始化与启动流程,以及排错方法,适用于构建读... 目录一、什么是mysql多实例二、二进制方式安装MySQL1.获取二进制代码包2.安装基础依赖3.清

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束