antv/G6使用记录,实现简单vue组件的demo

2023-12-12 21:20

本文主要是介绍antv/G6使用记录,实现简单vue组件的demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 antv/G6是蚂蚁金服数据可视化团队出品的一个功能完备的图可视化引擎。

 介于业务需要,最近在学习G6,特此记录下~~

简单展示:
简单的demo

功能点:

  • 连接关系图表
    • 鼠标控制缩放
    • 节点拖动事件
    • 画布拖动事件
    • 边的状态切换
    • 边动画

特性:

状态:G6中提供了状态管理,指的是节点或边的状态,包括交互状态和业务状态。
动画:动画是可视化中非常重要的内容,本例中涉及到边的动画效果
字体图标:本例中使用iconfont,具体实现参照G6官方文档,比较的详细
布局:G6提供了一些布局的方法,只需要在实例化G6时进行配置就好,但是本例没有使用布局,而是指定了节点额度坐标

 话不多说,上代码,使用vue写的组件,代码写的比较low~~~
完整代码

<template><div class="main-content-box"><div id="container"></div></div>
</template><script>
import G6 from '@antv/g6';
export default {name: 'g6demo',data () {return {data: {randomColor: ''}}},mounted () {setTimeout(() => {this.getInit()}, 100)},methods: {getInit () {const nodes = [];const edges = [];// 中心节点const centerNode = {id: 'center',x: 500,y: 300,// type: 'center-node',size: 86,label: '网络运营商',text: '\ue827',  //对应iconfont.css 里面的content,注意加ustyle: {fill: 'red'},labelCfg: {style: {fill: "blue"}},backgroundConfig: null  // 自定义项,用于判读是否需要圆背景};nodes.push(centerNode);// 左侧一级节点const leftMoudelNode = {id: 'node1',x: 350,y: 300,label: '光纤',text: '\ue78a',style: {fill: 'Coral'},backgroundConfig: null,size: 45,}nodes.push(leftMoudelNode);edges.push({ source: 'node1', target: 'center', type: 'can-running' });let leftGzzNode = {}// 左侧添加 4 个节点for (let i = 0; i < 4; i++) {const id = 'left' + i;nodes.push({id,x: 150,y: (i + 1) * 100 + 50,type: 'leaf-node',text: '\ue60e',label: (i + 1) + '号楼',style: {fill: '#5B8FF9'},backgroundConfig: null,size: 45,});edges.push({ source: id, target: 'node1', type: 'can-running' });}for (let i = 0; i < 4; i++) {const id = 'leftNodeGzz' + i;nodes.push({id,x: 50,y: (i + 1) * 100 + 50,type: 'leaf-node',text: '\ue627',label: '黑网吧',style: {fill: '#c6E5F5'},backgroundConfig: null,size: 45,});edges.push({ source: id, target: 'left' + i, type: 'can-running' });}// 右侧添加 5 个节点for (let i = 0; i < 5; i++) {const id = 'right' + i;const edgesId = 'edgeRight' + inodes.push({id,x: 750,y: i * 100 + 50,type: 'leaf-node',text: '\ue60e',label: (i + 5) + '号楼',relation: '以太网',style: {fill: '#5B8FF9'},backgroundConfig: null,size: 45,linebackgroundConfig: {fill: 'Coral',},});edges.push({id: edgesId,source: 'center',target: id,type: 'can-running',label: id === 'right0' ? '高速宽带' : '以太网',style: {stroke: 'red'},labelCfg: {style: {fill: id === 'right0' ? 'Coral' : 'cyan'}}});}for (let i = 0; i < 5; i++) {const id = 'rightNodeGzz' + i;nodes.push({id,x: 950,y: (i + 1) * 100,type: 'leaf-node',text: '\ue64c',label: '家庭用户',style: {fill: '#c6E5F5'},backgroundConfig: null,size: 45,});edges.push({ source: 'right' + i, target: id, type: 'can-running' });}//G6.registerNode('leaf-node',{draw (cfg, group) {const { backgroundConfig: backgroundStyle, style, labelCfg: labelStyle } = cfg;if (backgroundStyle) {group.addShape('circle', {attrs: {x: 0,y: 0,r: cfg.size,...backgroundStyle,},name: 'circle-shape',});}group.addShape('circle', {attrs: {x: -120,y: 0,r: 30,...backgroundStyle,},name: 'circle-shape',});const keyShape = group.addShape('text', {attrs: {x: 0,y: 0,fontFamily: 'iconfont', // 对应css里面的font-family: "iconfont";textAlign: 'center',textBaseline: 'middle',text: cfg.text,fontSize: cfg.size,...style,},name: 'text-shape1',});const labelY = backgroundStyle ? cfg.size * 2 : cfg.size;group.addShape('text', {attrs: {x: 0,y: labelY,textAlign: 'center',text: cfg.label,...labelStyle.style,},// must be assigned in G6 3.3 and later versions. it can be any value you wantname: 'text-shape1',});return keyShape;},getAnchorPoints () {return [[0, 0.5],[1, 0.5],];},},'circle',);//使用iconfontG6.registerNode('iconfont', {draw (cfg, group) {const { backgroundConfig: backgroundStyle, style, labelCfg: labelStyle } = cfg;if (backgroundStyle) {group.addShape('circle', {attrs: {x: 0,y: 0,r: cfg.size,...backgroundStyle,},// must be assigned in G6 3.3 and later versions. it can be any value you wantname: 'circle-shape',});}group.addShape('circle', {attrs: {x: -12,y: 0,r: 30,...backgroundStyle,},name: 'circle-shape',});const keyShape = group.addShape('text', {attrs: {x: 0,y: 0,fontFamily: 'iconfont', // 对应css里面的font-family: "iconfont";textAlign: 'center',textBaseline: 'middle',text: cfg.text,fontSize: cfg.size,...style,},// must be assigned in G6 3.3 and later versions. it can be any value you wantname: 'text-shape1',});const labelY = backgroundStyle ? cfg.size * 2 : cfg.size;group.addShape('text', {attrs: {x: 0,y: labelY,textAlign: 'center',text: cfg.label,...labelStyle.style,},// must be assigned in G6 3.3 and later versions. it can be any value you wantname: 'text-shape1',});return keyShape;},});// lineDash 的差值,可以在后面提供 util 方法自动计算const dashArray = [[0, 1],[0, 2],[1, 2],[0, 1, 1, 2],[0, 2, 1, 2],[1, 2, 1, 2],[2, 2, 1, 2],[3, 2, 1, 2],[4, 2, 1, 2],];const lineDash = [4, 2, 1, 2];const interval = 9;G6.registerEdge('can-running',{setState (name, value, item) {const shape = item.get('keyShape');if (name === 'running') {if (value) {const length = shape.getTotalLength(); // 后续 G 增加 totalLength 的接口let totalArray = [];for (let i = 0; i < length; i += interval) {totalArray = totalArray.concat(lineDash);}let index = 0;shape.animate(() => {const cfg = {lineDash: dashArray[index].concat(totalArray),};index = (index + 1) % interval;return cfg;},{repeat: true,duration: 3000,},);} else {shape.stopAnimate();shape.attr('lineDash', null);}}},},'cubic-horizontal',);let COLOR = '#40a9ff'const width = document.getElementById('container').scrollWidth * 0.7;const height = document.getElementById('container').scrollHeight || 600;const graph = new G6.Graph({container: 'container',width,height,renderer: 'svg',modes: {default: ['drag-canvas','drag-node','zoom-canvas']},defaultNode: {backgroundConfig: {backgroundType: 'circle',fill: COLOR,stroke: 'LightSkyBlue',},type: 'iconfont',size: 12,style: {fill: '#DEE9FF',stroke: '#5B8FF9',},labelCfg: {style: {fill: COLOR,fontSize: 12,},},},defaultEdge: {style: {stroke: '#b5b5b5',},},});graph.data({ nodes, edges });graph.render();graph.fitView();// 响应 hover 状态graph.on('node:mouseenter', ev => {const node = ev.item;const edges = node.getEdges();edges.forEach(edge => graph.setItemState(edge, 'running', true));});graph.on('node:mouseleave', ev => {const node = ev.item;const edges = node.getEdges();edges.forEach(edge => graph.setItemState(edge, 'running', false));});let model = {type: 'can-running',text: '\ue60e',style: {stroke: 'yellow'},labelCfg: {style: {fill: 'cyan'}}};//增加edge的标识id字段后const item = graph.findById('edgeRight0');const item2 = graph.findById('edgeRight1');const item3 = graph.findById('edgeRight2');const item4 = graph.findById('edgeRight3');const item5 = graph.findById('edgeRight4');setInterval(() => {model.labelCfg.style.fill = this.getRandomColor()model.style.stroke = this.getRandomColor()graph.updateItem(item, model);graph.updateItem(item2, model);graph.updateItem(item3, model);graph.updateItem(item4, model);graph.updateItem(item5, model);}, 1000);},getRandomColor () {var rand = Math.floor(Math.random() * 0xFFFFFF).toString(16);if (rand.length == 6) {return '#' + rand;} else {return this.getRandomColor();}}}
}</script><style scoped>
</style>

  以上就是示例的完整代码,G6的版本为3.5.0,话说G6的团队真的是很勤奋,版本更新的也蛮快的,官方文档也是越来越完善。希望越做越好吧。

Tips:
   如果本文章对您有帮助,欢迎评论告知!

这篇关于antv/G6使用记录,实现简单vue组件的demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring @RequestMapping 注解及使用技巧详解

《Spring@RequestMapping注解及使用技巧详解》@RequestMapping是SpringMVC中定义请求映射规则的核心注解,用于将HTTP请求映射到Controller处理方法... 目录一、核心作用二、关键参数说明三、快捷组合注解四、动态路径参数(@PathVariable)五、匹配请

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

Java中的record使用详解

《Java中的record使用详解》record是Java14引入的一种新语法(在Java16中成为正式功能),用于定义不可变的数据类,这篇文章给大家介绍Java中的record相关知识,感兴趣的朋友... 目录1. 什么是 record?2. 基本语法3. record 的核心特性4. 使用场景5. 自定

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

VSCode设置python SDK路径的实现步骤

《VSCode设置pythonSDK路径的实现步骤》本文主要介绍了VSCode设置pythonSDK路径的实现步骤,包括命令面板切换、settings.json配置、环境变量及虚拟环境处理,具有一定... 目录一、通过命令面板快速切换(推荐方法)二、通过 settings.json 配置(项目级/全局)三、

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=