js画图开发库--mxgraph--[orthogonal-正交.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() { // 检查浏览器支持 if (!mxClient.isBrowserSupported()) { mxUtils.error('Browser is not supported!', 200, false); } else { // 去锯齿效果 mxShape.prototype.crisp = true; // 启用导航线 mxGraphHandler.prototype.guidesEnabled = true; // Alt 键禁用导航线 mxGuide.prototype.isEnabledForEvent = function(evt) { return !mxEvent.isAltDown(evt); }; // 启用导航线自动连接目标元素 mxEdgeHandler.prototype.snapToTerminals = true; // 在IE中启动正交连接预览 mxConnectionHandler.prototype.movePreviewAway = false; // 在容器中创建图形 var graph = new mxGraph(container); graph.disconnectOnMove = false; graph.foldingEnabled = false; graph.cellsResizable = false; graph.extendParents = false; graph.setConnectable(true); // 实现连接点周边为固定点 graph.view.updateFixedTerminalPoint = function(edge, terminal, source, constraint) { mxGraphView.prototype.updateFixedTerminalPoint.apply(this, arguments); var pts = edge.absolutePoints; var pt = pts[(source) ? 0 : pts.length - 1]; if (terminal != null && pt == null && this.getPerimeterFunction(terminal) == null) { edge.setAbsoluteTerminalPoint(new mxPoint(this.getRoutingCenterX(terminal), this.getRoutingCenterY(terminal)), source) } }; //更新默认的连接线样式 graph.getStylesheet().getDefaultEdgeStyle()['edgeStyle'] = 'orthogonalEdgeStyle'; delete graph.getStylesheet().getDefaultEdgeStyle()['endArrow']; // 实现连接预览 graph.connectionHandler.createEdgeState = function(me) { var edge = graph.createEdge(null, null, null, null, null); return new mxCellState(this.graph.view, edge, this.graph.getCellStyle(edge)); }; // 设置容器自动改变 //graph.setResizeContainer(true); // 启用浏览器默认右键下拉菜单 new mxRubberband(graph); // 创建默认窗体 var parent = graph.getDefaultParent(); // 开始更新事务 graph.getModel().beginUpdate(); try { var v1 = graph.insertVertex(parent, null, '', 40, 40, 40, 30); v1.setConnectable(false); var v11 = graph.insertVertex(v1, null, '', 0.5, 0, 10, 40, 'portConstraint=northsouth;', true); v11.geometry.offset = new mxPoint(-5, -5); var v12 = graph.insertVertex(v1, null, '', 0, 0.5, 10, 10, 'portConstraint=west;shape=triangle;direction=west;perimeter=none;' + 'routingCenterX=-0.5;routingCenterY=0;', true); v12.geometry.offset = new mxPoint(-10, -5); var v13 = graph.insertVertex(v1, null, '', 1, 0.5, 10, 10, 'portConstraint=east;shape=triangle;direction=east;perimeter=none;' + 'routingCenterX=0.5;routingCenterY=0;', true); v13.geometry.offset = new mxPoint(0, -5); var v2 = graph.addCell(graph.getModel().cloneCell(v1)); v2.geometry.x = 200; v2.geometry.y = 60; var v3 = graph.addCell(graph.getModel().cloneCell(v1)); v3.geometry.x = 40; v3.geometry.y = 150; var v4 = graph.addCell(graph.getModel().cloneCell(v1)); v4.geometry.x = 200; v4.geometry.y = 170; graph.insertEdge(parent, null, '', v1.getChildAt(2), v2.getChildAt(1)); graph.insertEdge(parent, null, '', v2.getChildAt(2), v3.getChildAt(1)); graph.insertEdge(parent, null, '', v3.getChildAt(2), v4.getChildAt(1)); } finally { // 结束更新事务 graph.getModel().endUpdate(); } } }; </script> </head> <!-- 页面载入时启动程序 --> <body onload="main(document.getElementById('graphContainer'))"> <!-- 创建带网格壁纸和曲线的一个容器 --> <div id="graphContainer" style="overflow:hidden;position:relative;width:321px;height:241px;background:url('editors/images/grid.gif');cursor:default;"> </div> </body> </html>