基于mxgraph.js开发的流程图组件
1.fabric.js
在决定使用mxgraph.js开发流程图之前,尝试过用fabric.js来开发,结果发现并没有想象中的那么简单,而且用户体验非常差,下面是体验地址:
workFlow
直到遇到一个致命bug,当我准备加一个双击图形输入文字功能,花了
一点时间捣鼓了一下,通过group组合实现了此功能,但是,第一次双击输入正常,第二次再次双击就出现莫名其妙的情况,最后反馈给作者说下个版本
不准备解决,看了下issues,也发现了类似的问题,此方案就此放弃。
2.GoJs
第二次有人推荐我使用GoJs,看了下功能确实强大,也找到了符合我想要的流程图应用,但是。。。。尝试运行简单的例子同步到Vue,没出什么问题,当同步
一个简单的流程图示例,各种问题,捣鼓了很久还是没能运行成功,最重要的它的api写法简直太难受,此方案也就此放弃。官方示例:
GoJs
3.mxgraph.js
先上图来一波
体验地址:
mxFlow
说一下基础api的使用以及一些坑
1.定义全局Cell样式
this.style = new Object();
this.style[mxConstants.STYLE_DASHED] = 0;//0实线 1虚线
this.style[mxConstants.STYLE_STROKEWIDTH] = 1;//边框宽度
// this.style[mxConstants.STYLE_GRADIENTCOLOR] = 'red';//渐变色
this.style[mxConstants.STYLE_GRADIENT_DIRECTION] = mxConstants.DIRECTION_WEST;//渐变方向 east-东 west-西 north-北 south-南
this.style[mxConstants.STYLE_FONTFAMILY] = "consolas";//字体
this.style[mxConstants.STYLE_FONTSTYLE] = 0;//1-加粗 2-斜体 4-下划线
this.style[mxConstants.STYLE_HORIZONTAL] = 1;//1-水平 0-垂直
this.style[mxConstants.STYLE_LABEL_POSITION] = "center";//文字居中 结合下面属性控制
this.style[mxConstants.STYLE_VERTICAL_LABEL_POSITION] = "middle";//
this.graph.getStylesheet().putDefaultVertexStyle(this.style);//put到全局style中
2.定义全局Edge、label样式
this.style = this.graph.getStylesheet().getDefaultEdgeStyle();
this.style['fontSize'] = this.fontSize;
this.style['verticalAlign'] = 'bottom';
this.style[mxConstants.STYLE_ROUNDED] = true;
this.style[mxConstants.STYLE_EDGE] = mxEdgeStyle.ElbowConnector;
this.style[mxConstants.STYLE_LABEL_BACKGROUNDCOLOR] = "white";//transparent 连线文字背景颜色
this.style[mxConstants.STYLE_ORTHOGONAL_LOOP] = 1;
this.style[mxConstants.STYLE_JETTY_SIZE] = "auto";
3.局部样式
let style = new Object();
style[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_IMAGE;
style[mxConstants.STYLE_PERIMETER] = mxPerimeter.RectanglePerimeter;
style[mxConstants.STYLE_IMAGE] = '../../../static/img/star_200.png';
style[mxConstants.STYLE_FONTCOLOR] = 'black';
this.graph.getStylesheet().putCellStyle('image', style);//自定义背景style
this.graph.insertVertex(this.parent, null, 'star', x, y, 100, 100, 'image');//使用方式
4.修改全局Cell默认样式 Edge同理
let style = this.graph.getStylesheet().getDefaultVertexStyle();
style[mxConstants.STYLE_FONTCOLOR] = this.color;
5.设置单个或者多个Cell样式 Edge同理
this.graph.setCellStyles(mxConstants.STYLE_GRADIENT_DIRECTION, this.curDirGradient);//第一种方式
this.graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "none", [vertex]);//第二种方式
6.复制
let selectionCells = this.graph.getSelectionCells();
mxClipboard.copy(this.graph, selectionCells);
7.粘贴
mxClipboard.paste(this.graph);
8.剪切
let selectionCells = this.graph.getSelectionCells();
mxClipboard.cut(this.graph, selectionCells);
9.放大
this.graph.zoomIn();
10.缩小
this.graph.zoomOut();
11.撤销与还原
初始化:
this.undoMng = new mxUndoManager();
let listener = (sender, evt) => {
this.undoMng.undoableEditHappened(evt.getProperty('edit'));
};
this.graph.getModel().addListener(mxEvent.UNDO, listener);
his.graph.getView().addListener(mxEvent.UNDO, listener);
撤销:
this.undoMng.undo();
还原:
this.undoMng.redo();
12.组合
let vertex = new mxCell(null, new mxGeometry(0, 0));
vertex.setVertex(true);
this.graph.groupCells(vertex, 0, this.graph.getSelectionCells());
13.分解
this.graph.ungroupCells(this.graph.getSelectionCells());
14.加载xml自定义图形
try {
let req = mxUtils.load(this.staticPath+'xml/basic.xml');//此处路径写成项目资源文件目录
let root = req.getDocumentElement();
let shape = root.firstChild;
while (shape != null) {
if (shape.nodeType == mxConstants.NODETYPE_ELEMENT) {
let name = shape.getAttribute('name');
this.basicItems.push(name);//存储读取的shape名称数组
mxStencilRegistry.addStencil(name, new mxStencil(shape));
}
shape = shape.nextSibling;
}
} catch (e) {
mxUtils.alert('Cannot load' + e);
}
使用方式:
let basic = document.getElementById("basic");
this.basicItems.forEach((name, index) => {
basic.append(this.createItem("", 45, 45, "shape=" + name));
});
createItem方法:
createItem(title, width, height, style) {
let vertex = new mxCell(null, new mxGeometry(0, 0, width, height), style);
vertex.setVertex(true);
let elt = document.createElement('div');
elt.className = 'el-col el-col-8 basic';
// elt.style.overflow = 'hidden';
// Blocks default click action
// mxEvent.addListener(elt, 'click', function(evt)
// {
// mxEvent.consume(evt);
// });
// this.graph.view.scaleAndTranslate(1, 0, 0);
this.graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "none", [vertex]);
this.graph.setCellStyles(mxConstants.STYLE_STROKEWIDTH, 3, [vertex]);
this.graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, "#515151", [vertex]);
this.graph.setCellStyles(mxConstants.STYLE_SHADOW, false, [vertex]);
this.graph.setCellStyles(mxConstants.STYLE_ALIGN, mxConstants.ALIGN_CENTER, [vertex]);
this.graph.setCellStyles(mxConstants.STYLE_IMAGE_WIDTH, 48, [vertex]);
this.graph.setCellStyles(mxConstants.STYLE_IMAGE_HEIGHT, 48, [vertex]);
this.graph.addCell(vertex);
let node;
node = this.graph.view.getCanvas().ownerSVGElement.cloneNode(true);
this.graph.getModel().clear();
elt.appendChild(node);
return elt;
},//创建自定义html包裹的svg
15.一些坑
有些全局样式也可以直接修改,(不用像上面那样put)例如:
mxConstants.VERTEX_SELECTION_COLOR = "#29B6F2"; 鼠标框选 不显示背景颜色
new mxRubberband(this.graph);
解决办法添加以下css
div.mxRubberband {
position: absolute;
overflow: hidden;
border-style: solid;
border-width: 1px;
border-color: #0000FF;
background: #3481D7;
} 加载xml自定义图形上面14点已完整说明。
暂时就介绍那么多,还有很多细节设置就下次再说了。
欢迎进群交流(927465926)
基于mxgraph.js开发的流程图组件的更多相关文章
- KoaHub平台基于Node.js开发的Koa 连接支付宝插件代码信息详情
KoaHub平台基于Node.js开发的Koa 链接支付宝插件代码信息详情 easy-alipay alipay payment & notification APIs easy-alipay ...
- 基于原生js的返回顶部组件,兼容主流浏览器
基于原生js的返回顶部插件,兼容IE8及以上.FF.chrome等主流浏览器. js文件中封装了getScrollTop()和changeScrollTop()函数分别用于获取滚动条滚动的高度和修改滚 ...
- 基于面向对象js的弹窗的组件的开发案例
var aInput = document.getElementsByTagName("input"); 2 aInput[0].onclick = function() { 3 ...
- 基于Vue.js的表格分页组件
有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...
- KoaHub.JS基于Node.js开发的Koa 生成验证码插件代
ccap node.js generate captcha using c++ library CImg without install any other lib or software node- ...
- 基于AGS JS开发自定义贴图图层
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.前言 假设一个景区有多张图片需要在地图上展示,并且随着地图的缩放而缩 ...
- 基于 Vue.js 的移动端组件库mint-ui实现无限滚动加载更多
通过多次爬坑,发现了这些监听滚动来加载更多的组件的共同点, 因为这些加载更多的方法是绑定在需要加载更多的内容的元素上的, 所以是进入页面则直接触发一次,当监听到滚动事件之后,继续加载更多, 所以对于无 ...
- 记:使用IScroll.js 开发picker日历组件遇到的问题及经验总结
IScroll中文文档 第一个问题: 边界留白 就是这种,上边界(最小),下边界(最大)有两个列表的位置是不能选择的.解决的办法是: 在HTML中,添加空白节点就行了. 第二个问题:初始化之后的滚动停 ...
- KoaHub平台基于Node.js开发的Koa的连接MongoDB插件代码详情
koa-mongo MongoDB middleware for koa, support connection pool. koa-mongo koa-mongo is a mongodb midd ...
随机推荐
- 【算法进阶-康托展开】-C++
目录 引入 这位老爷子就是康托 基本概念 康托展开是一个全排列到一个自然数的双射,常用于构建hash表时的空间压缩.设有n个数(1,2,3,4,-,n),可以有组成不同(n!种)的排列组合,康托展开表 ...
- 004_linux驱动之_class_create创建一个设备类
(一)解析:class_create函数和class_destroy函数 创建一个类 和 删除一个类. (二)class_create函数原型 struc ...
- 给json数组添加新字段并赋值
zNodes.forEach(function(item) { item.newtitle ="点击加号设为主席会场"; });
- java实现大文件上传和下载
[文件上传和下载]是很多系统必备功能, 比如PM\OA\ERP等:系统中常见的开发模式有B/S和C/S,而前者主要是通过浏览器来访问web服务器,一般采用七层协议中的[应用层http]进行数据传输,后 ...
- 二进制学习——Blob,ArrayBuffer、File、FileReader和FormData的区别
前言: Blob.ArrayBuffer.File.fileReader.formData这些名词总是经常看到,知道一点又好像不知道,像是同一个东西好像又不是,总是模模糊糊,最近终于下决心要弄清楚. ...
- 学密码学一定得学程序(SDUT 2463)
Problem Description 曾经,ZYJ同学非常喜欢密码学.有一天,他发现了一个很长很长的字符串S1.他很好奇那代表着什么,于是神奇的WL给了他另一个字符串S2.但是很不幸的是,WL忘记跟 ...
- 树莓派打造mini广播(FM)系统
树莓派打造mini广播(FM)系统 注意相关法律限制功率大小和频段.,以下只能用于测试目的 github项目: https://github.com/miegl/PiFmAdv 安装: apt-get ...
- 【转】java 解析多层json
java分别解析下面两个json字符串 package jansonDemo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjso ...
- 六、grep与正则表达式 (文本过滤)
一.正则表达式 正则表达式:Regual Expression, REGEXP.由一类特殊字符及文本字符所编写的模式,其中有些字符不表示其字面意义,而是用于表示控制或通配的功能:基本正则表达式:BRE ...
- vue实战_从头开始搭建vue工程
写在前面:vue工程入口文件分析 /index.html,/src/main.js,/src/APP.vue /index.html文件示例: <!DOCTYPE html> <ht ...