使用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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值