Custom Router For Bottom Top Tree
Does not take `vertices` into account .
by Roman Bruckner
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.4.4/joint.css" />
</head>
<body>
<!-- content -->
<div id="paper"></div>
<!-- dependencies -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.4.0/backbone.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jointjs/3.4.4/joint.js"></script>
</body>
</html>
JavaScript
var graph = new joint.dia.Graph({}, {
cellNamespace: joint.shapes
});
const paper = new joint.dia.Paper({
el: document.getElementById('paper'),
width: 800,
height: 900,
model: graph,
defaultConnectionPoint: {
name: 'boundary'
},
cellViewNamespace: joint.shapes,
gridSize: 10,
async: true,
sorting: joint.dia.Paper.sorting.APPROX,
defaultRouter: function(_, opt, linkView) {
const s = linkView.sourceBBox.bottomMiddle();
const t = linkView.targetBBox.topMiddle();
const mid = (s.y + t.y) / 2;
return [{ x: s.x, y: mid }, { x: t.x, y: mid }];
}
});
// Example
var el1 = new joint.shapes.standard.Circle({
position: {
x: 10,
y: 170
},
size: {
width: 50,
height: 50
},
attrs: {
label: {
text: 'A'
}
}
});
var el2 = new joint.shapes.standard.Rectangle({
position: {
x: 100,
y: 400
},
size: {
width: 100,
height: 90
}
});
const el3 = el2.clone().position(300, 400)
const el4 = el3.clone().position(500, 400);
const l1 = new joint.shapes.standard.Link({
source: {
id: el1.id
},
target: {
id: el2.id
}
});
const l2 = new joint.shapes.standard.Link({
source: {
id: el1.id
},
target: {
id: el3.id
}
});
const l3 = new joint.shapes.standard.Link({
source: {
id: el1.id
},
target: {
id: el4.id
}
});
graph.resetCells([el1, el2, el3, el4, l1, l2, l3]);
paper.unfreeze();