JSFiddle - React, Tailwind, and code Playground
HTML
<div id="main">
<table>
<tr>
<td>
<div class="intgraph"/>
</td>
</tr>
</table>
</div>
CSS
circle.node {
cursor: pointer;
stroke: #000;
stroke-width: .5px;
}
line.link {
fill: none;
stroke: #9ecae1;
stroke-width: 1.5px;
}
JavaScript
var w = 800,
h = 800,
node,
link,
root;
var x = d3.scale.linear()
.domain([0, w])
.range([0, w]);
var y = d3.scale.linear()
.domain([0, h])
.range([h, 0]);
var force = d3.layout.force()
.on("tick", tick)
.charge(function(d) { return d._children ? -d.size / 100 : -50; })
.linkDistance(function(d) { return d.target._children ? 80 : 30; })
.size([w, h]);
function zoom() {
node.call(transform);
update();
}
function transform(d){
d.cx = - x(d.x);
d.cy = - y(d.y);
}
var vis = d3.select(".intgraph").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.call(d3.behavior.zoom().x(x).y(y).on("zoom", zoom));
// the area to zoom
var rect = vis.append("rect")
.attr("width", w)
.attr("height", h)
.style("fill", "none")
.style("pointer-events", "all");
function readfile(json) {
root = json;
root.fixed = true;
root.x = w / 2;
root.y = h / 2 - 80;
update();
};
function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
.start();
// Update the links…
link = vis.selectAll("line.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links.
link.enter().insert("svg:line", ".node")
.attr("class", "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; });
// Exit any old links.
link.exit().remove();
// Update the nodes
node = vis.selectAll("circle.node")
.data(nodes, function(d) { return d.id; })
.style("fill", color);
node.transition()
.attr("r", function(d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; });
// Enter any new nodes.
...