Mxgraph使用总结二
1 新建画板,画板相关操作
var container = document.getElementById("main");
//设置背景样式
container.style.background = 'url(editors/images/grid.gif)';
container.style.height = "300px";
container.style.padding = "20px";
//创建一个画板
var graph = new mxGraph(container);
//获取顶层,可以认为是父节点
var parent = graph.getDefaultParent();
createVertex = function(){
var container = document.getElementById("main");
var graph = new mxGraph(container);
var parent = graph.getDefaultParent(); // 开启拖拽选择
new mxRubberband(graph); v1 = graph.insertVertex(parent, null, "text1", , , , );
graph.insertVertex(parent, null, "text2", , , , );
graph.insertVertex(parent, null, "text3", , , , );return graph;
};
2 style的使用,插入背景图
// 声明一个object
var style = {};
// 克隆一个object
style = mxUtils.clone(style);
style[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_LABEL; // 不设置这个属性 背景图片不出来
// 边框颜色
style[mxConstants.STYLE_STROKECOLOR] = '#999999';
// 边框大小
style[mxConstants.STYLE_STROKEWIDTH] = ;
// 字体颜色
style[mxConstants.STYLE_FONTCOLOR] = '#FFFF00';
// 文字水平方式
style[mxConstants.STYLE_ALIGN] = mxConstants.ALIGN_CENTER;
// 文字垂直对齐
style[mxConstants.STYLE_VERTICAL_ALIGN] = mxConstants.ALIGN_BOTTOM;
// 字体大小
style[mxConstants.STYLE_FONTSIZE] = ;
// 底图水平对齐
style[mxConstants.STYLE_IMAGE_ALIGN] = mxConstants.ALIGN_CENTER;
// 底图垂直对齐
style[mxConstants.STYLE_IMAGE_VERTICAL_ALIGN] = mxConstants.ALIGN_CENTER;
// 图片路径
//style[mxConstants.STYLE_IMAGE] = 'images/icons48/gear.png';
style[mxConstants.STYLE_IMAGE] = 'http://imgstatic.baidu.com/img/image/shouye/qizhi0822.jpg';
// 背景图片宽
style[mxConstants.STYLE_IMAGE_WIDTH] = ;
// 背景图片高
style[mxConstants.STYLE_IMAGE_HEIGHT] = ;
// 上间距设置
// 即使下边定义了全局设置,但这里单独设置上边间距仍单独有效
style[mxConstants.STYLE_SPACING_TOP] = ;
// 四边间距设置
style[mxConstants.STYLE_SPACING] = ;
// 把定义好的样式object push到stylesheet
graph.getStylesheet().putCellStyle("style1", style);
//样式使用
var v1 = graph.insertVertex(parent, null, "text1", 50, 50, 200, 200, "style1");
3、一些常用的方法
3.1 insertVertex 绘制图形
//mxGraph.prototype.insertVertex = function(parent,id,value,x,y,width,height,style,relative)
//parent画板父层,value值,x,y为坐标起点,width宽,height高
//style样式 stylename;image=imageUrl
//relative相对位置
graph.insertVertex(parent, null, '第一个盒子', , , , ,"style1");
3.2 insertEdge 连线
//mxGraph.prototype.insertEdge = function(parent,id,value,source,target,style)
//parent画板父层,value连线值,source起点,target重点,style样式
graph.insertEdge(parent, null, 'box1 connect to box2', v1, v2 , "");
3.3 addCellOverlay 添加告警
// 开启提示
graph.setTooltips(true); // 移出报警
var delOverlay = function(id){
// 获取ID单元
var cell = graph.getModel().getCell(id);
// 修改有报警物体的样式
graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "#CCCCCC", [cell]);
graph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "#000000", [cell]);
// 移除告警
graph.removeCellOverlays(cell);
}; // 给物体添加报警
var addOverlay = function(id, state){
// 获取ID单元
var cell = graph.getModel().getCell(id);
// 修改有报警物体的样式
graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "#FF0000", [cell]);
graph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "#FFFFFF", [cell]);
// 添加告警
graph.addCellOverlay(cell, createOverlay(graph.warningImage, '状态: '+state));
}; // 创建告警信息
var createOverlay = function(image, tooltip){
//function mxCellOverlay(image,tooltip,align,verticalAlign,offset,cursor)
//image图片,tooltip提示,align位置,verticalAlign竖直位置
var overlay = new mxCellOverlay(image, tooltip);
overlay.addListener(mxEvent.CLICK, function(sender, evt){
mxUtils.alert(tooltip);
});
return overlay;
};
3.4 添加按钮
// 添加按钮
document.body.appendChild(mxUtils.button('修改背景颜色', function(evt){
// Alaer
mxUtils.alert("Oh! You will Click me!!");
// 获取单元
var cell = graph.getModel().getCell(v1.id);
// 修改样式
graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "#000000", [cell]);
graph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "#FFFFFF", [cell]);
})); // 添加按钮
document.body.appendChild(mxUtils.button('还原背景颜色', function(evt){
// 获取单元
var cell = graph.getModel().getCell(v1.id);
// 获取默认样式
var style = graph.getStylesheet().getDefaultVertexStyle();
// 还原默认样式
for(var i in mxConstants){
graph.setCellStyles(mxConstants[i], style[mxConstants[i]], [cell]);
}
}));
3.5 缩放操作
// 居中缩放
graph.centerZoom = true;
// 放大按钮
document.body.appendChild(mxUtils.button('放大 +', function(evt){
graph.zoomIn();
}));
// 缩小按钮
document.body.appendChild(mxUtils.button('缩小 -', function(evt){
graph.zoomOut();
}));
// 还原按钮
document.body.appendChild(mxUtils.button('还原 #', function(evt){
graph.zoomActual();
graph.zoomFactor = 1.2;
input.value = 1.2;
}));
var input = document.createElement("input");
input.type = "text";
input.value = graph.zoomFactor;
input.addEventListener("blur", function(){
graph.zoomFactor = parseFloat(this.value, );
});
document.body.appendChild(input);
3.6 拖拽连线操作
// 开启可以拖拽建立关系
graph.setConnectable(true);
// 开启方块上的文字编辑功能
graph.setCellsEditable(false);
// 启用对齐线帮助定位
mxGraphHandler.prototype.guidesEnabled = true;
// 选择基本元素开启
graph.setEnabled(true);
3.7 图形形状介绍
var container = document.getElementById("main");
container.style.background = 'url(editors/images/grid.gif)';
container.style.width = "100%";
container.style.height = (window.screen.availHeight - ) + "px";
container.style.overflow = "hidden";
var graph = new mxGraph(container);
var parent = graph.getDefaultParent();
// 画方块 默认情况下
graph.insertVertex(parent, null, '矩形', , , , );
// 画方块 圆角矩形
// shape=rounded 定义圆角 arcSize=10 定义圆角弧度
graph.insertVertex(parent, null, '圆角矩形', , , , , "rounded=true;perimeter=ellipsePerimeter;arcSize=20;");
// 画椭圆
// shape=elipse 定义椭圆 perimeter=ellipsePerimeter 让连线的箭头或起点触到边缘
graph.insertVertex(parent, null, '椭圆', , , , , "shape=ellipse;perimeter=ellipsePerimeter;");
// 画三角形
// shape=triangl 定义三角形 perimeter=ellipsePerimeter 让连线的箭头或起点触到边缘 direction=south 让三角形倒立
graph.insertVertex(parent, null, '三角形', , , , , "shape=triangle;perimeter=ellipsePerimeter;direction=south;");
// 画菱形
// shape=rhombus 定义菱形
graph.insertVertex(parent, null, '三角形', , , , , "shape=rhombus;perimeter=ellipsePerimeter;");
// 画柱形
// shape=cylinder 定义柱形
graph.insertVertex(parent, null, '柱形', , , , , "shape=cylinder;perimeter=ellipsePerimeter;");
// 画人
// shape=actor 定义演员
graph.insertVertex(parent, null, '演员', , , , , "shape=actor;perimeter=ellipsePerimeter;");
// 画云
graph.insertVertex(parent, null, '云', , , , , "shape=cloud;perimeter=ellipsePerimeter;");
//矩形默认情况下
graph.insertVertex(parent, null, '矩形', , , , , "shape=rectangle;perimeter=ellipsePerimeter;");
//泳道
graph.insertVertex(parent, null, '泳道', , , , , "shape=swimlane;perimeter=ellipsePerimeter;");
//双圆
graph.insertVertex(parent, null, '双圆', , , , , "shape=doubleEllipse;perimeter=ellipsePerimeter;");
//六边形
graph.insertVertex(parent, null, '六边形', , , , , "shape=hexagon;perimeter=ellipsePerimeter;");
3.8 查看图形的xml
document.body.appendChild(mxUtils.button('View XML', function()
{
var encoder = new mxCodec();
var node = encoder.encode(graph.getModel());
mxUtils.popup(mxUtils.getPrettyXml(node), true); //以窗口的方式展示处理
}));
3.9 工具栏常用操作
buttons = [
{
label : "选择所有",
fun : function(graph){
return function(evt){
graph.selectAll();
};
}
},
{
label : "选择一个",
fun : function(graph){
return function(evt){
graph.selectCell();
};
}
},
{
label : "取消选择",
fun : function(graph){
return function(evt){
var cells = graph.getSelectionCells();
graph.removeSelectionCells(cells);
};
}
},
{
label : "随机添加",
fun : function(graph){
return function(evt){
var randColor = function(){
return "rgb("+randint(,)+","+randint(,)+","+randint(,)+")";
}; var style = "fillColor=" + randColor() + "; fontColor=" + randColor();
var width = randint(, );
var height = randint(, );
var x = randint(, - width);
var y = randint(, - height); graph.insertVertex(graph.getDefaultParent(), null, "随机添加", x, y, width, height, style);
};
}
},
{
label : "分组所选",
fun : function(graph){
return function(evt){
var cells = graph.getSelectionCells();
graph.groupCells(null, , cells);
};
}
},
{
label : "取消分组",
fun : function(graph){
return function(evt){
var cells = graph.getSelectionCells();
graph.ungroupCells(cells);
};
}
},
{
label : "删除所选",
fun : function(graph){
return function(evt){
var cells = graph.getSelectionCells();
graph.removeCells(cells);
};
}
},
{
label : "缩小",
fun : function(graph){
return function(evt){
graph.zoomOut();
};
}
},
{
label : "放大",
fun : function(graph){
return function(evt){
graph.zoomIn();
};
}
},
{
label : "还原",
fun : function(graph){
return function(evt){
graph.zoomActual();
};
}
},
{
label : "随机所选元素的位置",
fun : function(graph){
return function(evt){
var cells = graph.getSelectionCells();
for(var i=; i<cells.length; i++){
var x = randint(, - cells[i].geometry.width);
var y = randint(, - cells[i].geometry.height);
}
graph.moveCells([cells[i]], x , y);
};
}
} ];
3.10 将图形的xml进行回显
var xml=
'<mxGraphModel> '+
' <root> '+
' <mxCell id="0"/> '+
' <mxCell id="1" parent="0"/> '+
' <app appId="" appName="" protocol="" ip="" port="" context="" heartBeatUrl="" id="2"> '+
' <mxCell style="verticalLabelPosition=top;verticalAlign=bottom;shadow=1;fillColor=#FFFFFF" vertex="1" connectable="0" parent="1" type="app"> '+
' <mxGeometry x="100" y="320" width="20" height="40" as="geometry"/> '+
' </mxCell> '+
' </app> '+
' </root> '+
'</mxGraphModel> ';
var doc = mxUtils.parseXml(xml);
var codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());
Mxgraph使用总结二的更多相关文章
- mxgraph进阶(二)mxgraph的初步介绍与开发入门
mxgraph的初步介绍与开发入门 前言 由于小论文实验需求,需要实现根据用户日志提取出行为序列,然后根据行为序列生成有向图的形式,并且连接相邻动作的弧上标有执行此次相邻动作的频次.为此,在大师兄徐凯 ...
- mxgraph进阶(三)Web绘图——mxGraph项目实战(精华篇)
Web绘图--mxGraph项目实战(精华篇) 声明 本文部分内容所属论文现已发表,请慎重对待. 需求 由于小论文实验需求,需要实现根据用户日志提取出行为序列,然后根据行为序列生成有向图的形式 ...
- mxGraph实现鱼骨图(因果图)(转自CSDN,链接附于文中)
鱼骨图由日本管理大师石川馨先生所发明,故又名石川图.鱼骨图是一种发现问题“根本原因”的方法,它也可以称之为“Ishikawa”或者“因果图”.其特点是简捷实用,深入直观.它看上去有些象鱼骨,问题或缺陷 ...
- Mxgraph使用总结一
一.Mxgraph介绍: mxGraph 是一个 JS 绘图组件适用于需要在网页中设计/编辑 Workflow/BPM流程图.图表.网络图和普通图形的 Web 应用程序.mxgraph 下载包中包括j ...
- mxgraph中mxStencil使用教程
目录 标签嵌套关系 Shapes shape connections background foreground 其他样式 图形内部颜色绘制 封闭线段绘制 设置一条线的颜色大小 样例 官方文档:htt ...
- 【小程序分享篇 二 】web在线踢人小程序,维持用户只能在一个台电脑持登录状态
最近离职了, 突然记起来还一个小功能没做, 想想也挺简单,留下代码和思路给同事做个参考. 换工作心里挺忐忑, 对未来也充满了憧憬与担忧.(虽然已是老人, 换了N次工作了,但每次心里都和忐忑). 写写代 ...
- 前端开发中SEO的十二条总结
一. 合理使用title, description, keywords二. 合理使用h1 - h6, h1标签的权重很高, 注意使用频率三. 列表代码使用ul, 重要文字使用strong标签四. 图片 ...
- 【疯狂造轮子-iOS】JSON转Model系列之二
[疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
随机推荐
- 【TECH】CAS php客户端配置
搞完java又搞php,我整个人都不好了=.= 跟大师在linux上折腾了一下午,没调出来,早上在windows上跑通了,中午终于在linux上搞定了,嘿嘿. server端配置参见这里 在windo ...
- 【收藏】SearchCrawler By James Holmes
转自Crawling the Web with Java By James Holmes 无需任何扩展包,可直接运行. import java.awt.*; import java.awt.event ...
- ucsc genome brower的用法和说明(一)
官网说明书:http://genome.ucsc.edu/goldenpath/help/hgTracksHelp.html 1.genome brower的作用 a,展示任何尺度的基因组片段.比如, ...
- sql 数据库中只靠一个数据,查询到所在表和列名
有时候我们想通过一个值知道这个值来自数据库的哪个表以及哪个字段,在网上搜了一下,找到一个比较好的方法,通过一个存储过程实现的.只需要传入一个想要查找的值,即可查询出这个值所在的表和字段名. 前提是要将 ...
- PAT1028. List Sorting (25)
id用int,避免了id的strcmp,不然用string就超时. #include <iostream> #include <vector> #include <alg ...
- Codeforces Round #373 (Div. 2) A , B , C
A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...
- JQuery小知识点
//get() : 就是把JQ转成原生JS,可以让通过jquery获得元素使用JS的innerHTML方法. $(function(){ //document.getElementById('div1 ...
- tomcat配置 启动
<Context docBase="E:\apache-tomcat-7.0.50\wtpwebapps\mycms" path="" reloadab ...
- swoole帮助文档
入门指引 [编辑本页] Swoole虽然是标准的PHP扩展,实际上与普通的扩展不同.普通的扩展只是提供一个库函数.而swoole扩展在运行后会接管PHP的控制权,进入事件循环.当IO事件发生后,swo ...
- 《Think in Java》(八)多态
"封装"通过合并特征和行为来创建新的数据类型: "实现隐藏"通过将细节"私有化"把接口和实现分离开来: "多态"消除类型 ...