配置参数
注释
nodeSep
相同rank的邻接节点的距离
edgeSep
相同rank的邻接边的距离
rankSep
不同 rank 元素之间的距离
rankDir
布局方向 ( "TB" (top-to-bottom) / "BT" (bottom-to-top) / "LR" (left-to-right) / "RL"(right-to-left))
marginX
number of pixels to use as a margin around the left and right of the graph.
marginY
number of pixels to use as a margin around the top and bottom of the graph.
ranker
排序算法。 Possible values: 'network-simplex' (default), 'tight-tree' or 'longest-path'.
resizeClusters
set to false if you don't want parent elements to stretch in order to fit all their embedded children. Default is true.
clusterPadding
A gap between the parent element and the boundary of its embedded children. It could be a number or an object e.g. { left: 10, right: 10, top: 30, bottom: 10 }. It defaults to 10.
setPosition(element, position)
a function that will be used to set the position of elements at the end of the layout. This is useful if you don't want to use the default element.set('position', position) but want to set the position in an animated fashion via .
setVertices(link, vertices)
If set to true the layout will adjust the links by setting their vertices. It defaults to false. If the option is defined as a function it will be used to set the vertices of links at the end of the layout. This is useful if you don't want to use the default link.set('vertices', vertices) but want to set the vertices in an animated fashion via .
setLabels(link, labelPosition, points)
If set to true the layout will adjust the labels by setting their position. It defaults to false. If the option is defined as a function it will be used to set the labels of links at the end of the layout. Note: Only the first label (link.label(0);) is positioned by the layout.
dagre
默认情况下,dagre 应该在全局命名空间当中,不过你也可以当作参数传进去
graphlib
默认情况下,graphlib 应该在全局命名空间当中,不过你也可以当作参数传进去
我们来试一下。NodeJS 后端
var express = require('express'); var joint = require('jointjs'); var dagre = require('dagre') var graphlib = require('graphlib'); var app = express(); function get_graph(){ var graph = new joint.dia.Graph(); var rect = new joint.shapes.standard.Rectangle(); rect.position(100, 30); rect.resize(100, 40); rect.attr({ body: { fill: 'blue' }, label: { text: 'Hello', fill: 'white' } }); rect.addTo(graph); var rect2 = rect.clone(); rect2.translate(300, 0); rect2.attr('label/text', 'World!'); rect2.addTo(graph); for(var i=0; i<10; i++){ var cir = new joint.shapes.standard.Circle(); cir.resize(100, 100); cir.position(10, 10); cir.attr('root/title', 'joint.shapes.standard.Circle'); cir.attr('label/text', 'Circle' + i); cir.attr('body/fill', 'lightblue'); cir.addTo(graph); var ln = new joint.shapes.standard.Link(); ln.source(cir); ln.target(rect2); ln.addTo(graph); } var link = new joint.shapes.standard.Link(); link.source(rect); link.target(rect2); link.addTo(graph); //auto layout joint.layout.DirectedGraph.layout(graph, { nodeSep: 50, edgeSep: 50, rankDir: "TB", dagre: dagre, graphlib: graphlib }); return graph.toJSON(); } app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); next(); }); app.get('/graph', function(req, res){ console.log('[+] send graph json to client') res.send(get_graph()); }); app.listen(8071);
HTML 前端