【From】 http://chwshuang.iteye.com/blog/1797740
布局變化,下方還有動畫效果選項:
<!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.setEnabled(false); // 更改點風格的樣式 var style = graph.getStylesheet().getDefaultVertexStyle(); style[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_ELLIPSE; style[mxConstants.STYLE_PERIMETER] = mxPerimeter.EllipsePerimeter; style[mxConstants.STYLE_GRADIENTCOLOR] = 'white'; style[mxConstants.STYLE_FONTSIZE] = '10'; // 設置容器隨內容自適應 //graph.setResizeContainer(true); // 設置圖大小 graph.gridSize = 40; // 創建默認窗體 var parent = graph.getDefaultParent(); // 創建新的布局算法 var layout = new mxFastOrganicLayout(graph); // 移動距離 layout.forceConstant = 80; // 動畫效果選項 var animate = document.getElementById('animate'); // 添加按鈕來更新布局 document.body.insertBefore(mxUtils.button('圓形布局Circle Layout', function(evt) { graph.getModel().beginUpdate(); try { // 創建圓形布局算法 var circleLayout = new mxCircleLayout(graph); circleLayout.execute(parent); } catch (e) { throw e; } finally { if (animate.checked) { var morph = new mxMorphing(graph); morph.addListener(mxEvent.DONE, function() { graph.getModel().endUpdate(); }); morph.startAnimation(); } else { graph.getModel().endUpdate(); } } } ), document.body.firstChild); // 添加按鈕來更新布局 document.body.insertBefore(mxUtils.button('隨機布局Organic Layout', function(evt) { graph.getModel().beginUpdate(); try { layout.execute(parent); } catch (e) { throw e; } finally { if (animate.checked) { //默認值是 6, 1.5, 20 var morph = new mxMorphing(graph, 10, 1.7, 20); morph.addListener(mxEvent.DONE, function() { graph.getModel().endUpdate(); }); morph.startAnimation(); } else { graph.getModel().endUpdate(); } } } ), document.body.firstChild); // 開啟更新事務 graph.getModel().beginUpdate(); var w = 30; var h = 30; try { var v1 = graph.insertVertex(parent, null, 'A', 0, 0, w, h); var v2 = graph.insertVertex(parent, null, 'B', 0, 0, w, h); var v3 = graph.insertVertex(parent, null, 'C', 0, 0, w, h); var v4 = graph.insertVertex(parent, null, 'D', 0, 0, w, h); var v5 = graph.insertVertex(parent, null, 'E', 0, 0, w, h); var v6 = graph.insertVertex(parent, null, 'F', 0, 0, w, h); var v7 = graph.insertVertex(parent, null, 'G', 0, 0, w, h); var v8 = graph.insertVertex(parent, null, 'H', 0, 0, w, h); var e1 = graph.insertEdge(parent, null, 'ab', v1, v2); var e2 = graph.insertEdge(parent, null, 'ac', v1, v3); var e3 = graph.insertEdge(parent, null, 'cd', v3, v4); var e4 = graph.insertEdge(parent, null, 'be', v2, v5); var e5 = graph.insertEdge(parent, null, 'cf', v3, v6); var e6 = graph.insertEdge(parent, null, 'ag', v1, v7); var e7 = graph.insertEdge(parent, null, 'gh', v7, v8); var e8 = graph.insertEdge(parent, null, 'gc', v7, v3); var e9 = graph.insertEdge(parent, null, 'gd', v7, v4); var e10 = graph.insertEdge(parent, null, 'eh', v5, v8); // 執行更改 layout.execute(parent); } finally { // 結束更新事務 graph.getModel().endUpdate(); } } }; </script> </head> <!-- 頁面載入時啟動程序 --> <body onload="main(document.getElementById('graphContainer'))"> <!-- 創建帶網格壁紙和曲線的一個容器,請一定要定義的position和overflow的屬性!根據在線API的54 頁內容增加的大小偵聽器 --> <div id="graphContainer" style="position:relative;overflow:visible;width:821px;height:641px;background:url('editors/images/grid.gif');"> </div> <br> <input type="checkbox" id="animate" checked="checked"/> Transitions </body> </html>