AntV 是蚂蚁金服全新一代数据可视化解决方案,主要包含数据驱动的高交互可视化图形语法G2,专注解决流程与关系分析的图表库 G6、适于对性能、体积、扩展性要求严苛的场景。

demo使用数字模拟真实的节点及分组数据。combo内的nodes亦是使用随机数生成,节点之前的连线edges由节点从小到大依次连接 ,大家在用的时候,可以注意一下连线对象的 sourcetarget 两个属性即可

安装模块依赖 :npm install @antv/g6

main.js 中引入,并绑定Vue原型方法

import G6 from '@antv/g6';
import Vue from 'vue'; Vue.prototype.G6 = G6;

创建Graph.vue

<template>
<div>
<div id="drawGraph"></div>
</div>
</template> <script>
let graphG = null
export default {
mounted() {
this.initData();
},
methods: {
initData() {
let combos = [
{ id: '100-600', label: '100-600', type: 'root' },
{ id: '100-200', label: '100-200' },
{ id: '200-300', label: '200-300' },
{ id: '300-400', label: '300-400' },
{ id: '400-500', label: '400-500' },
{ id: '500-600', label: '500-600' },
]
let edges = [
{ source: '100-200', target: '100-600' },
{ source: '200-300', target: '100-600' },
{ source: '300-400', target: '100-600' },
{ source: '400-500', target: '100-600' },
{ source: '500-600', target: '100-600' },
]
let data = { combos, edges }
this.makeRelationData(data);
},
// 分组 点 连线处理
makeRelationData(data) {
if (graphG) {
graphG.destroy();
}
let drawGraph = document.getElementById("drawGraph");
this.graphWidth = drawGraph.scrollWidth;
this.graphHeight = drawGraph.scrollHeight || 1200;
let origin = [this.graphWidth / 2, 100];
let row = 150, clo = 180;
let combos = data.combos
let row_clo = Math.floor(Math.sqrt(combos.length));
for (let i = 0; i < combos.length; i++) {
let rowindex = Math.floor(i / row_clo) + 1;
let cloindex = (i % row_clo) + 1;
// 分组默认样式设置
if (combos[i].type === 'root') {
combos[i].x = this.graphWidth / 3
combos[i].y = this.graphHeight / 3
combos[i].style = {
fill: "#B19693",
opacity: 0.4,
cursor: "pointer",
};
} else {
// 分组定位
combos[i].x = origin[0] + clo * cloindex;
combos[i].y = origin[1] + row * rowindex;
if (i % 2 === 1) {
combos[i].y += 40;
}
combos[i].style = {
fill: "#FAD069",
opacity: 0.5,
cursor: "pointer",
}
}
}
this.drawQfast(data)
},
drawQfast(data) {
graphG = new this.G6.Graph({
container: "drawGraph",
width: this.graphWidth,
height: this.graphHeight,
groupByTypes: false,
modes: {
default: [
{ type: "zoom-canvas", enableOptimize: true, optimizeZoom: 0.2 },
{ type: "drag-canvas", enableOptimize: true },
{ type: "drag-node", enableOptimize: true, onlyChangeComboSize: true },
{ type: "drag-combo", enableOptimize: true, onlyChangeComboSize: true },
{ type: "brush-select", enableOptimize: true },
],
},
defaultEdge: {
type: 'cubic-horizontal',
lineWidth: 1,
style: {
endArrow: true,
stroke: "#FAD069",
},
},
edgeStateStyles: {
hover: {
lineWidth: 2,
},
},
defaultNode: {
type: "circle",
size: 15,
labelCfg: {
position: "bottom",
style: {
fontSize: 15,
},
},
},
defaultCombo: {
type: "circle",
opacity: 0,
lineWidth: 1,
collapsed: true,
labelCfg: {
position: "top",
refY: 5,
style: {
fontSize: 16,
},
},
},
});
graphG.data(data);
graphG.render(); // 渲染图
graphG.zoom(0.8); // 如果觉得节点大,可以缩放整个图
graphG.on("edge:mouseenter", (e) => {
graphG.setItemState(e.item, "hover", true);
}); graphG.on("edge:mouseleave", (e) => {
graphG.setItemState(e.item, "hover", false);
}); graphG.on("combo:dblclick", (e) => {
e.item._cfg.model.type = e.item._cfg.model.type === "rect" ? "circle" : "rect"; // 分组形状,方圆切换
e.item._cfg.model.labelCfg.refY = e.item._cfg.model.type === "rect" ? -20 : 5; // 切换形状,改变label定位 const comboId = e.item._cfg.model.id
graphG.collapseExpandCombo(comboId); // 分组收缩时,删除分组内的连线和节点
if (e.item._cfg.model.collapsed) { // 收缩
let newedges = e.item.getEdges();
let newNodes = e.item.getNodes();
for (let j = 0; j < newedges.length; j++) {
graphG.removeItem(newedges[j]);
}
for (let i = 0; i < newNodes.length; i++) {
graphG.removeItem(newNodes[i]);
}
data.edges.forEach(edge => {
graphG.addItem("edge", edge);
});
} else { // 展开
// 分组展开时, 添加节点和连线,并给分组内的节点 添加位置信息
let origin = [e.item._cfg.model.x, e.item._cfg.model.y]; // 获取当前分组combs的坐标
let row = 110, clo = 150;
// 生成(10-20)随机数个 随机数 模拟展开分组内的节点
let randomCount = Math.floor(Math.random() * 10) + 10;
let row_clo = Math.floor(Math.sqrt(randomCount));
let nodes = []
for (let i = 0; i < randomCount; i++) {
let min = comboId.split('-')[0] - 0
let max = comboId.split('-')[1] - 0
let randomNum = Math.floor(Math.random() * (max - min)) + min;
if (nodes.indexOf(randomNum) > -1) {
i--
continue;
}
nodes.push(randomNum)
let rowindex = Math.floor(i / row_clo);
let cloindex = i % row_clo;
let y = origin[1] + row * rowindex
let node = {
label: randomNum,
id: randomNum.toString(),
comboId: comboId,
style: {
fillOpacity: 0.5,
cursor: "pointer",
fill: randomNum % 5 == 0 ? "#81C7D4" : "#986DB2"
},
x: origin[0] + clo * cloindex,
y: i % 2 == 0 ? y + 40 : y
}
graphG.addItem("node", node); // 将节点添加至分组
}
nodes.sort((a, b) => a - b) // 将分组内的数字排序,从小到大依次连接,模拟真实数据
for (let i = 0; i < nodes.length - 1; i++) {
let edge = {
source: nodes[i].toString(),
target: nodes[i + 1].toString(),
lineWidth: 1,
style: {
lineDash: [2, 2],
lineWidth: 0.5,
stroke: "#00AA90"
}
}
graphG.addItem("edge", edge); // 添加连线 将分组内的数字排序,从小到大依次连接
}
}
});
},
}
};
</script>

收缩图:

展开图:

可视化—AntV G6实现节点连线及展开收缩分组的更多相关文章

  1. 可视化—AntV G6 紧凑树实现节点与边动态样式、超过X条展示更多等实用小功能

    通过一段时间的使用和学习,对G6有了更一步的经验,这篇博文主要从以下几个小功能着手介绍,文章最后会给出完整的demo代码. 目录 1. 树图的基本布局和使用 2. 根据返回数据的属性不同,定制不一样的 ...

  2. 可视化—AntV G6 高亮相邻节点的两种方式

    目录 内置的高亮节点 自定义高亮 自定义高亮时保持原始颜色 总结 案例完整代码 通过官方文档,可知高亮相邻节点分为两种方法,文档描述并不是很清楚,对刚接触这个库的小白并不是很友好,慢慢总结慢慢来吧 内 ...

  3. React使用AntV G6实现流程图

    安装 npm install @antv/g6 --save 引用 import G6 from '@antv/g6' 自定义节点 /** * 方式一 */ G6.registerNode('rect ...

  4. antV G6流程图在Vue中的使用

    最近我司项目中需要加入流程图制作功能,于是乎百度各种找可视化绘制拓扑图的轮子,大部分都是国外的,看文档太吃力,不过好在最终让我发现了AntV G6流程图图表库,最新版为2.0,不过编辑器在2.0版本还 ...

  5. vue结合antV/g6 实现网络拓扑图

    最近很多业务场景都会需要用到拓扑图,翻找了很多资料,最后选择了antV/g6,主要原因有以下几点: 1.阿里出品,所以框架的成熟性有保障 2.业务场景契合(1.规则拓扑图:2.动画流向:每个节点会有流 ...

  6. React + umi +antd+antv/g6 实现力图

    官方示例效果:http://antv.alipay.com/zh-cn/g6/2.x/demo/net/2017-link-data.html 改编效果: 实现步骤: 环境:nodejs.yarn/n ...

  7. dhtmlxtree 节点 展开收缩:新增了直接点 文本内容 也 实现了 展开收缩 功能(并记住了展开、收缩状态)

    dhtmlxtree 节点 展开收缩通常情况我们按 +- 就实现了 展开收缩 功能,为了方便我们新增了直接点 文本内容 也 实现了 展开收缩 功能(并记住了展开.收缩状态) tree = new dh ...

  8. extjs4.0 treepanel节点的选中、展开! 数据的重新加载

    1.extjs4.0API较3.0有非常大变化2.多级子父节点的选中和展开.3.数据的重新加载.tree.getStore().load4.节点的移除,从树中根据ID获取节点 tree.getStor ...

  9. AntV G6绘制流程图学习例子

    下面代码可以直接贴到html文件中运行看效果. 代码说明 js中data是一个json变量,里面有两个关键对象"nodes.edges",分别来描述节点.节点间箭线. 更多&quo ...

  10. activiti 配置节点 连线信息获取

    1.1.1. 前言 当使用eclipse插件进行流程设计的时候,部署流程之后,我们如何获取我们定义的所有的节点.连线.关口等配置信息呢?有的人看到这个需求,不免窃喜,这不很简单嘛,重新打来bmpn中定 ...

随机推荐

  1. pageOffice插件 springboot实现服务器上Word文档在线打开编辑保存

    需求: 在oa系统上,想实现在线,服务器上doc,docx文档,在web打开,编辑.编辑后,可以再同步保存到服务器端. 开发环境: java springboot,thymeleaf 服务器环境: 无 ...

  2. WPF自定义FixedColumnGrid布局控件

    按照上一节所讲,我已经对布局系统又所了解.接下来我就实现一个布局控件FixedColumnGrid. 1.基础版 布局控件机制如下,FixedColumnGrid将子控件按照水平排列,每行满两列后换行 ...

  3. 【C# wpf】个人网盘练习项目总结

    采用 .net frameowrok 4.5.2 未写持久层代码,不可保存运行时的数据状态.分服务端,客户端,采用tcp通讯,使用了supersocket组件.服务端用winform ,客户端用wpf ...

  4. WPF实现轮播图

    1.效果图 2.前端代码 <Window x:Class="LiveChartDemo.View.CarouselView" xmlns="http://schem ...

  5. 视觉族: 基于Stable Diffusion的免费AI绘画图片生成器工具

    视觉族是一款基于Stable Diffusion文生图模型的免费在线AI绘画图片生成器工具,可以使用提示关键词快速生成精美的艺术图片,支持中文提示.无论你是想要创作自己的原创作品,还是想要为你的文字增 ...

  6. 如何保留 Excel 表头和第一行数据并追加 CSV 数据

    准备工作 在开始之前,确保你的 Python 环境中已经安装了 openpyxl 和 pandas 库.可以使用以下命令进行安装: pip install openpyxl pandas 第一步:编写 ...

  7. (C#) IIS 响应标头过滤敏感信息(如:Server/X-Powered-By等) 运维知识

    背景: 再一次净网行动中,客户要求安全改造发现了接口请求的header标头中出现如图中的敏感信息. 说明: 其意义在于告知浏网站是用什么语言或者框架编写的.解决办法就是修改该响应头为一个错误的值,将攻 ...

  8. ETL工具-nifi干货系列 第十五讲 nifi处理器ConsumeKafka实战教程

    1.上一节课我们学习了处理器PushKafka,通过该处理器往kafka中间件写数据,今天我们一起学习处理器ConsumeKafka,此处理器从kafka读取数据进行后续处理,如下图所示: 本次示例比 ...

  9. ES 关于 remote_cluster 的一记小坑

    最近有小伙伴找到我们说 Kibana 上添加不了 Remote Cluster,填完信息点 Save 直接跳回原界面了.具体页面,就和没添加前一样. 我们和小伙伴虽然隔着网线但还是进行了深入.详细的交 ...

  10. Vue TypeScript 实战:掌握静态类型编程

    title: Vue TypeScript 实战:掌握静态类型编程 date: 2024/6/10 updated: 2024/6/10 excerpt: 这篇文章介绍了如何在TypeScript环境 ...