This example takes bjtucker's block: Bounded Force Layout - force rank by group and swaps out the dataset, showing energy flows from source to use.
This example also adds a Simple D3 tooltip. Hat tip to biovisualize for that bl.ock.
See another view of this energy flow data at the bl.ock Sankey Particles - Transparent Links
README.md from the original bl.ock
This D3 example shows how to constrain the position of nodes within the rectangular bounds of the containing SVG element. As a side-effect of updating the node's cx
and cy
attributes, we update the node positions to be within the range [radius, width - radius] for x, [radius, height - radius] for y. If you prefer, you could use the each
operator to do this as a separate step, rather than as a side-effect of setting attributes.
Wrote the each
operator step described above, but used tick to settle groups into separate ranks. -bjtucker
forked from micahstubbs's block: Bounded Force Layout - Columns by Depth
xxxxxxxxxx
<meta charset="utf-8">
<style>
circle {
stroke-width: 1.5px;
}
line {
stroke: #999;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500,
radius = 6;
var fill = d3.scale.category20();
var force = d3.layout.force()
.gravity(0.05)
.charge(-50) // -50
.linkDistance(50)
.size([width, height])
.linkStrength(0.005)
.friction(0.9)
.theta(0.8)
.alpha(0.1)
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append('rect')
.attr("width", width)
.attr("height", height)
.style({
fill: 'none',
stroke: 'none',
'stroke-width': 1
});
d3.json("graph.json", function(error, graph) {
if (error) throw error;
var tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden");
var link = svg.selectAll("line")
.data(graph.links)
.enter().append("line");
var node = svg.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
.attr("r", radius - .75)
.style("fill","blue" )/*function(d) { return fill(d.year); }*/
/* .style("stroke", function(d) { return d3.rgb(fill(d.cluster)).darker(); })*/
.call(force.drag);
force
.nodes(graph.nodes)
.links(graph.links)
.on("tick", tick)
.start();
function tick() {
node.each(function(d) { w = 105 * (d.cluster); d.y -= (0.2 * (d.y - w)) })
node.attr("cx", function(d) {return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
.attr("cy", function(d) {return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
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
.on("mouseover", function(d){
return tooltip
.text("test")
.style("visibility", "visible");
})
.on("mousemove", function(){
return tooltip
.style("top", (event.pageY - 10) + "px")
.style("left",(event.pageX + 10) + "px");
})
.on("mouseout", function(){
return tooltip
.style("visibility", "hidden")
});
});
</script>
https://d3js.org/d3.v3.min.js