Created
January 27, 2018 15:46
-
-
Save TYsewyn/99f86b42ec4fbedf06db611a1a04bea4 to your computer and use it in GitHub Desktop.
Visualizing your Spring Integration Components & Flows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Visualizing your Spring Integration Components & Flows</title> | |
<script type="text/javascript" src="http://d3js.org/d3.v4.min.js"></script> | |
<script type="text/javascript" src="http://d3js.org/d3-selection-multi.v1.min.js"></script> | |
<style> | |
#graph { | |
display: inline-block; | |
position: relative; | |
width: 100%; | |
padding-bottom: 100%; | |
vertical-align: middle; | |
overflow: hidden; | |
} | |
.svg { | |
display: inline-block; | |
position: absolute; | |
top: 0; | |
left: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="graph"></div> | |
<script> | |
var graphEndpoint = "http://localhost:8080/integration"; | |
var width = window.innerWidth || document.body.clientWidth, height = window.innerHeight || document.body.clientHeight; | |
var colors = d3.scaleOrdinal(d3.schemeCategory10); | |
var svg = d3.select("#graph").append("svg") | |
.attr("class", "svg") | |
.attr("width", "100%").attr("height", "100%") | |
.attr("pointer-events", "all"); | |
svg.append('defs') | |
.append('marker') | |
.attrs({'id':'arrowhead', | |
'viewBox':'-0 -5 10 10', | |
'refX':13, | |
'refY':0, | |
'orient':'auto', | |
'markerWidth':13, | |
'markerHeight':13, | |
'xoverflow':'visible'}) | |
.append('svg:path') | |
.attr('d', 'M 0,-5 L 10 ,0 L 0,5') | |
.attr('fill', '#999') | |
.style('stroke','none'); | |
var simulation = d3.forceSimulation() | |
.force("link", d3.forceLink().id(function(d) { return d.nodeId; }).distance(100).strength(1)) | |
.force("charge", d3.forceManyBody().strength(-1000)) | |
.force("center", d3.forceCenter(width / 2, height / 2)); | |
d3.json(graphEndpoint, function(error, graph) { | |
if (error) throw error; | |
updateGraph(graph.links, graph.nodes); | |
}); | |
function updateGraph(links, nodes) { | |
links.forEach(function(d){ | |
d.source = d.from; | |
d.target = d.to; | |
}); | |
link = svg.selectAll(".link") | |
.data(links) | |
.enter() | |
.append("line") | |
.attr("class", "link") | |
.attr('marker-end','url(#arrowhead)'); | |
link.text(function (d) {return d.type;}); | |
edgepaths = svg.selectAll(".edgepath") | |
.data(links) | |
.enter() | |
.append('path') | |
.attrs({ | |
'class': 'edgepath', | |
'fill-opacity': 0, | |
'stroke-opacity': 0, | |
'id': function (d, i) {return 'edgepath' + i} | |
}) | |
.style("pointer-events", "none"); | |
edgelabels = svg.selectAll(".edgelabel") | |
.data(links) | |
.enter() | |
.append('text') | |
.style("pointer-events", "none") | |
.attrs({ | |
'class': 'edgelabel', | |
'id': function (d, i) {return 'edgelabel' + i}, | |
'font-size': 10, | |
'fill': '#aaa' | |
}); | |
edgelabels.append('textPath') | |
.attr('xlink:href', function (d, i) {return '#edgepath' + i}) | |
.style("text-anchor", "middle") | |
.style("pointer-events", "none") | |
.attr("startOffset", "50%") | |
.text(function (d) {return d.type}); | |
node = svg.selectAll(".node") | |
.data(nodes) | |
.enter() | |
.append("g") | |
.attr("class", "node") | |
.call(d3.drag() | |
.on("start", dragstarted) | |
.on("drag", dragged) | |
.on("end", dragended) | |
); | |
node.append("circle") | |
.attr("r", 5) | |
.style("fill", function (d, i) {return colors(i);}); | |
node.append("title") | |
.text(function (d) {return d.id;}); | |
node.append("text") | |
.attr("dy", -3) | |
.text(function (d) {return d.name+":"+d.componentType;}); | |
simulation | |
.nodes(nodes) | |
.on("tick", ticked); | |
simulation.force("link") | |
.links(links); | |
} | |
function ticked() { | |
link | |
.attr("x1", function (d) {return d.source.x;}) | |
.attr("y1", function (d) {return d.source.y;}) | |
.attr("x2", function (d) {return d.target.x;}) | |
.attr("y2", function (d) {return d.target.y;}); | |
node | |
.attr("transform", function (d) {return "translate(" + d.x + ", " + d.y + ")";}); | |
edgepaths.attr('d', function (d) { | |
return 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d.target.x + ' ' + d.target.y; | |
}); | |
edgelabels.attr('transform', function (d) { | |
if (d.target.x < d.source.x) { | |
var bbox = this.getBBox(); | |
rx = bbox.x + bbox.width / 2; | |
ry = bbox.y + bbox.height / 2; | |
return 'rotate(180 ' + rx + ' ' + ry + ')'; | |
} | |
else { | |
return 'rotate(0)'; | |
} | |
}); | |
} | |
function dragstarted(d) { | |
if (!d3.event.active) simulation.alphaTarget(0.3).restart() | |
d.fx = d.x; | |
d.fy = d.y; | |
} | |
function dragged(d) { | |
d.fx = d3.event.x; | |
d.fy = d3.event.y; | |
} | |
function dragended(d) { | |
if (!d3.event.active) simulation.alphaTarget(0); | |
d.fx = undefined; | |
d.fy = undefined; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OMG, you've saved man-months of sufferings for us probably! %)
Thank you, man!!!