使用joint.js 繪製圖,joint.js繪製圖
試了幾種在js畫graph的庫,還是joint.js合適一些,對於簡單的圖顯示很適用。具體連結:
http://www.daviddurman.com/automatic-graph-layout-with-jointjs-and-dagre.html
demo:
1.script和css下載:
http://jointjs.com/download :
<link rel="stylesheet" href="joint.css" >
<script src="joint.js" ></script>
<script src="joint.layout.DirectedGraph.js"></script>
http://www.daviddurman.com/assets/autolayout.js:
<script src="autolayout.js" ></script>
2. 範例程式碼:
<link rel="stylesheet" href="joint.css" ><script src="joint.js" ></script><script src="joint.layout.DirectedGraph.js"></script><script src="autolayout.js" ></script><textarea id="adjacency-list" rows=40 cols=100>{ 'a': ['b'], 'a': ['c'], 'b': ['f'], 'c': ['e', 'd'], 'd': [], 'e': [], 'f': ['g'], 'g': []}</textarea><br><button id="btn-layout">Layout</button><div id="paper"></div><script>// Main.// -----var graph = new joint.dia.Graph;var paper = new joint.dia.Paper({ el: $('#paper'), width: 2000, height: 2000, gridSize: 1, model: graph});// Just give the viewport a little padding.V(paper.viewport).translate(20, 20);$('#btn-layout').on('click', layout);function layout() { try { var adjacencyList = eval('adjacencyList = ' + $('#adjacency-list').val()); } catch (e) { alert(e); } var cells = buildGraphFromAdjacencyList(adjacencyList); graph.resetCells(cells); joint.layout.DirectedGraph.layout(graph, { setLinkVertices: false });}layout();</script>