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

相关文章

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景