js画图开发库--mxgraph--[merge-合并.html]
<!Doctype html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>合并</title>
<!-- 如果本文件的包与src不是在同一个目录,就要将basepath设置到src目录下 -->
<script type="text/javascript">
mxBasePath = '../src';
</script>
<!-- 引入支持库文件 -->
<script type="text/javascript" src="../src/js/mxClient.js"></script>
<!-- 示例代码 -->
<script type="text/javascript">
// 程序在此方法中启动
function main(container)
{
// 检查浏览器支持
if (!mxClient.isBrowserSupported())
{
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// 在容器中创建图形
var graph = new mxGraph(container);
// 设置元素大小是否可以调整
graph.setCellsResizable(false);
// 设置所有元都设置成圆形
var style = graph.stylesheet.getDefaultVertexStyle();
style[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_ELLIPSE;
style[mxConstants.STYLE_PERIMETER] = mxPerimeter.EllipsePerimeter;
style[mxConstants.STYLE_FONTCOLOR] = '#FFFFFF';
style[mxConstants.STYLE_FONTSTYLE] = mxConstants.FONT_BOLD;
// 设置所有元素标题为灰色,背景为白色
style = graph.stylesheet.getDefaultEdgeStyle();
style[mxConstants.STYLE_LABEL_BACKGROUNDCOLOR] = '#FFFFFF';
style[mxConstants.STYLE_FONTCOLOR] = 'gray';
// 使用浏览器默认的按键监听
new mxRubberband(graph);
// 创建默认窗体
var parent = graph.getDefaultParent();
// 开始更新事务
graph.getModel().beginUpdate();
try
{
var a = graph.insertVertex(parent, 'a', 'A', 20, 20, 20, 20, 'fillColor=blue');
var b = graph.insertVertex(parent, 'b', 'B', 20, 200, 20, 20, 'fillColor=blue');
var c = graph.insertVertex(parent, 'c', 'C', 200, 20, 20, 20, 'fillColor=red');
var d = graph.insertVertex(parent, 'd', 'D', 200, 200, 20, 20, 'fillColor=red');
var ac = graph.insertEdge(parent, 'ac', 'ac', a, c, 'strokeColor=blue');
var ad = graph.insertEdge(parent, 'ad', 'ad', a, d, 'strokeColor=blue');
var bd = graph.insertEdge(parent, 'bd', 'bd', b, d, 'strokeColor=blue');
}
finally
{
// 更新结束
graph.getModel().endUpdate();
}
// 创建第二个不包含容器的图形
var graph2 = new mxGraph();
// 创建第二个默认窗体
var parent2 = graph2.getDefaultParent();
// 开启更新事务
graph2.getModel().beginUpdate();
try
{
var c = graph2.insertVertex(parent2, 'c', 'C', 200, 20, 20, 20, 'fillColor=green');
var d = graph2.insertVertex(parent2, 'd', 'D', 200, 200, 20, 20, 'fillColor=green');
var e = graph2.insertVertex(parent2, 'e', 'E', 400, 20, 20, 20, 'fillColor=green');
var f = graph2.insertVertex(parent2, 'f', 'F', 400, 200, 20, 20, 'fillColor=green');
var ce = graph2.insertEdge(parent2, 'ce', 'ce', c, e, 'strokeColor=green');
var ed = graph2.insertEdge(parent2, 'ed', 'ed', e, d, 'strokeColor=green');
var fd = graph2.insertEdge(parent2, 'bd', 'fd', f, d, 'strokeColor=green');
}
finally
{
// 结束更新事务
graph2.getModel().endUpdate();
}
// Merges the model from the second graph into the model of the first graph.
// Note: If you add a false to the parameter list then _not_ all edges will be cloned,
// that is, the edges are assumed to have an identity, and hence the edge
// "bd" will be changed to point from f to d, as specified in the edge for the same id in the second graph.
//graph.getModel().mergeChildren(parent2, parent/*, false*/);
graph.getModel().mergeChildren(parent2, parent);
}
};
</script>
</head>
<!-- 页面载入时启动程序 -->
<body οnlοad="main(document.getElementById('graphContainer'))" style="overflow:hidden;">
<!-- 创建带网格壁纸和曲线的一个容器 -->
<div id="graphContainer"
style="position:absolute;overflow:hidden;width:100%;height:100%;">
</div>
</body>
</html>