记录使用echarts的graph类型绘制流程图全过程(一)-x,y位置的计算
先说下本次案例业务需求,输入2个节点,获取数据后绘制出2个节点间的路径,之前使用的是网状图,但是网状图的效果不佳,需要转换成流程图的模式:
那么如何在不修改数据的情况下,实现类似效果尼?
看了下echarts的graph类型,可以实现类似的,下面是官方的实例
从实例中可以看出,难点在于节点的显示位置x,y和曲线的设置。业务数据中:
1、节点的数量不定,关系的数量不定,
2、后台返回的数据只有单独的节点信息和关系信息
实现思路:
1、分析数据,获取前后节点关系,获得行数位置(节点的xIndex信息)
在节点数组中找到开始节点并设置xIndex 为1,然后从它开始找第二层的节点
// 获取节点的x轴顺序
setNodeOrder () {
this.entityList.forEach(item => {
if (item.id === this.startNode) {
item.xIndex = 1;
}
// 设置一个对象,记录节点信息,用于后面查询信息,比数组查询来的快
this.nodesObj[item.id] = item;
});
this.findNodeOrder(2, this.startNode);
}
// 广度遍历--按顺序找到第index层的数据,并设置对应的参数,再层层递进
findNodeOrder (xIndex, sId){
}
想一下,如果是第二层的节点,那么应该是关系中,源是startNode或者目的节点是startNode,如此类推,层层递进,
这里需要使用广度遍历而不是深度遍历,设定2个数组,currentQueue记录当前层的节点,afterQueue记录下一层的节点,当currentQueue层遍历完后,将afterQueue变成currentQueue,继续遍历,直至结束,核心代码如下:
......
let nextIds = [];
this.relationList.forEach(item => {
// 源
if (item.source === sId) {
if (!this.nodesObj[item.target].xIndex) {
this.nodesObj[item.target].xIndex = xIndex;
nextIds.push(item.target);
}
}
// 目的
if (item.target === sId) {
if (!this.nodesObj[item.source].xIndex) {
this.nodesObj[item.source].xIndex = xIndex;
nextIds.push(item.source);
}
}
});
let nextIdsLen = nextIds.length;
// 1、没有当前的队列,没有后续的队列,有nextIds
if (
!this.currentQueue.length &&
!this.afterQueue.length &&
nextIdsLen
) {
this.currentQueue = nextIds;
this.currentXindex = this.currentXindex + 1;
this.setNextOrder();
} else if (this.currentQueue.length && nextIdsLen) {
// 2、有当前的队列在遍历,排队
this.afterQueue = this.afterQueue.concat(nextIds);
} else if (!this.currentQueue.length && this.afterQueue.length) {
// 3、没有当前的队列了,有排队的队列,则排队的进去
if (nextIdsLen) {
this.afterQueue = this.afterQueue.concat(nextIds);
}
this.currentQueue = JSON.parse(JSON.stringify(this.afterQueue));
this.afterQueue = [];
this.currentXindex = this.currentXindex + 1;
}
setNextOrder函数:
setNextOrder () {
while (this.currentQueue.length) {
let id = this.currentQueue.shift();
this.findNodeOrder(this.currentXindex, id);
}
}
2、根据行数信息设置列数的位置(节点的yIndex层的信息)
先排序第一层和第二层的顺序,因为第一层就一个节点,第二层和第一层的关系都是一样的,所以顺序无所谓,可以直接遇到就叠加
// 排完了x,再排列y
// 先排第一和二行的元素的y,按顺序排
let maxXindex = 1;
let secondYIndexObj = {};
for (let i in this.nodesObj) {
let item = this.nodesObj[i];
if (!item.yIndex) {
maxXindex = item.xIndex > maxXindex ? item.xIndex : maxXindex;
if (item.xIndex === 1) {
item.yIndex = 1;
this.yIndexObj[item.id] = {
xIndex: 1,
yIndex: 1
};
}
if (item.xIndex === 2) {
if (!secondYIndexObj[item.xIndex]) {
item.yIndex = 1;
// 记录当前的y轴上的节点个数
secondYIndexObj[item.xIndex] = 1;
this.yIndexObj[item.id] = {
xIndex: 2,
yIndex: 1
};
} else {
item.yIndex = secondYIndexObj[item.xIndex] + 1;
secondYIndexObj[item.xIndex] = item.yIndex;
this.yIndexObj[item.id] = {
xIndex: 2,
yIndex: item.yIndex
};
}
}
}
}
但是从第三层开始就要注意了,不能再按照第二层的递增顺序的方法来控制。因为第二层与第三层的关系和数量都不一定,如果随机排列会导致线很乱,最好的方法是根据第二层的顺序,获取第三层的顺序。即将第二层第N个节点有关的的节点设置在第N层。这样如果第二层的数量与第三层的数量相等,那么就完全在一条直线上了。如果数量不等,也不至于那么混乱但是如果第三层有多个节点与第二层的同一个节点有关系,这个时候我选择的是随机选择层级了
// 从第三层开始
if (maxXindex > 2) {
// 后面列的排序根据前一列为准(尽量保证在一条直线上)
for (let j = 3; j <= maxXindex; j++) {
for (let i in this.nodesObj) {
let item = this.nodesObj[i];
while (item.xIndex === j && !item.yIndex) {
// 先看跟它一层的节点有多少个
if (!this.nodeOrders[j]) {
let totals = this.findYtotal(j);
this.nodeOrders[j] = [];
for (let i = 1; i <= totals; i++) {
this.nodeOrders[j].push(i);
}
}
// 找第二层中的对应的层级
let findX = j - 1;
// 找到所有的层级
let findYs = this.findLinkNode(item.id, findX);
// 找到还有的层级
let sameArr = findYs.filter(x =>
this.nodeOrders[j].includes(x)
);
let findY;
// 找不到按顺序抽取了
if (!sameArr.length) {
// 只能随机选择一个了
let ran = Math.floor(
Math.random() * this.nodeOrders[j].length
);
findY = this.nodeOrders[j][ran];
this.nodeOrders[j].splice(ran, 1);
} else {
findY = sameArr[0];
// 去除该顺序
let order;
this.nodeOrders[j].find((num, k) => {
if (num === findY) {
order = k;
return true;
}
});
this.nodeOrders[j].splice(order, 1);
}
this.yIndexObj[item.id] = {
xIndex: j,
yIndex: findY
};
item.yIndex = findY;
}
}
}
}
3、设置具体的位置信息
获取图表的中心位置centerY,将起点和终点的y位置放在中间,其他的根据上面获取的xIndex和yIndex的还有根据情况设置的间距(gapX,gapY)及节点的大小(size)计算出每个节点的x和y信息
// 设置节点的位置x,y
setNodesPositon () {
for (let i in this.nodesObj) {
let item = this.nodesObj[i];
if (item.id === this.startNode) {
item.y = this.centerY;
item.x = 1;
}
if (!item.x) {
item.x = this.gapX * (item.xIndex - 1) + this.size / 2;
}
if (!item.y && !item.total) {
item.total = this.findYtotal(item.xIndex);
if (item.total === 1) {
item.y = this.centerY;
} else {
let middleNum = item.total / 2;
let ceilNum = Math.ceil(middleNum);
let topGap;
let bottomGap;
let ty = (ceilNum - item.yIndex) * (this.gapY + this.size);
let by = (item.yIndex - ceilNum) * (this.gapY + this.size);
if (item.total % 2 === 1) {
topGap = this.centerY - ty;
bottomGap = this.centerY + by;
} else {
topGap = this.centerY - (ty + this.gapY + 0.5 * this.size);
bottomGap = this.centerY + (by - 0.5 * this.size);
}
if (item.yIndex <= middleNum) {
item.y = topGap;
} else {
item.y = bottomGap;
}
// 奇数
if (item.total % 2 === 1) {
if (item.yIndex === ceilNum) {
item.y = this.centerY;
}
}
}
}
}
}
记录使用echarts的graph类型绘制流程图全过程(一)-x,y位置的计算的更多相关文章
- 记录使用echarts的graph类型绘制流程图全过程(二)- 多层关系和圆形图片的设置
本文主要记录在使用echarts的graph类型绘制流程图时候遇到的2个问题:对于圆形图片的剪切和多层关系的设置 图片的设置 如果用echarts默认的symbol参数来显示图片,会显示图片的原始状态 ...
- Echarts中graph类型的运用求教
以下是百度Echarts官网上关系图的源码,但是这个关系图的node节点和edge都是静态文件里规定好的,我现在想动态实现,点击其中一个节点A然后新产生一个新节点B,并且有A和B之间的edge,就类似 ...
- 玩转控件:GDI+动态绘制流程图
前言 今天,要跟大家一起分享是"GDI+动态生成流程图"的功能.别看名字高大上(也就那样儿--!),其实就是动态生成控件,然后GDI+绘制直线连接控件罢了.实际项目效果图如下 ...
- 【Canvas】绘制几何级数Geometric series曲线 y=1+1/2+1/4+1/8+1/16+1/32+1/64+....
相关资料:https://baike.baidu.com/item/%E5%87%A0%E4%BD%95%E7%BA%A7%E6%95%B0/112584?fr=aladdin 图线: 代码: < ...
- graph easy绘制ascii简易流程图
graph-easy 日常我们经常需要画一些简易流程图,但是如果使用visio等工具来作图,一则略显大材小用,二则图片导出后再要粘贴.相比下,如果可以简单的用一些text的图来表达,则会简单的多.比如 ...
- Graphviz 绘制流程图
凝视说明非常具体.不再详述. digraph G{ //dot 是一种画图语言,它能够方便你採用图形的方式高速.直观地表达一些想法, //比方描写叙述某个问题的解决方式,构思一个程序的流程,澄清一堆貌 ...
- echarts拓扑图(graph,力导向布局图)
echarts连接:https://gallery.echartsjs.com/editor.html?c=xCLEj67T3H 讲解:https://www.cnblogs.com/koala201 ...
- 在echarts里在geojson绘制的地图上展示散点图(气泡)、线集。
先来要实现的效果图: 下方图1是官网的案例:http://www.echartsjs.com/gallery/editor.html?c=scatter-map 下图2是展示气泡类型为pin的效果: ...
- 使用SVG绘制流程图
本篇主要记录流程图的实现过程中的难点和核心技术点,先上效果图: 节点可以任意拖拽,曲线跟随变化 正在连接的线 1.节点实现 流程图是基于SVG绘制的,节点主要利用 g 和 foreignObject的 ...
随机推荐
- k8s集群部分常见问题处理
目录 部分常见问题处理 Coredns CrashLoopBackOff 导致无法成功添加工作节点的问题 添加工作节点时提示token过期 kubectl 执行命令报“The connection t ...
- HDU 5984 题解 数学推导 期望
Let’s talking about something of eating a pocky. Here is a Decorer Pocky, with colorful decorative s ...
- Educational Codeforces Round 43 E&976E. Well played! 贪心
传送门:http://codeforces.com/contest/976/problem/E 参考:https://www.cnblogs.com/void-f/p/8978658.html 题意: ...
- Investigating Div-Sum Property UVA - 11361
An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is d ...
- CF981B Businessmen Problems map 模拟 二十二
Businessmen Problems time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- AOE工程实践-NCNN组件
作者:杨科 NCNN是腾讯开源的一个为手机端极致优化的高性能神经网络前向计算框架.在AOE开源工程里,我们提供了NCNN组件,下面我们以SqueezeNet物体识别这个Sample为例,来讲一讲NCN ...
- 【DataBase】事务
一.事务概述 二.事务的四大特性(ACID) 三.事务的隔离性导致的问题 四.数据库的四个隔离级别 五.数据库中的锁机制: 六.更新丢失 七.并发事务所带来的的问题 一.事务概述 事务的概念:事务是指 ...
- 2019CSP初赛基础知识整理
一.硬件 计算机发展: 年代 元件 第一代 1946~1958 电子管 第二代 1959~1964 晶体管 第三代 1965~1970 集成电路 第四代 1971~? 大规模集成电路 世界上第一台 ...
- javascript 中 typeof 和 instanceof 的区别
在 javascript 中经常会用到 typeof 和 instanceof 来判断一个对象的类型,可能 typeof 用得多些,那来看看这两个之间的区别吧. typeof : typeof 是一个 ...
- Hbase 统计表行数的3种方式总结
有些时候需要我们去统计某一个Hbase表的行数,由于hbase本身不支持SQL语言,只能通过其他方式实现.可以通过一下几种方式实现hbase表的行数统计工作: 1.count命令 最直接的方式是在hb ...