JSFiddle - React, Tailwind, and code Playground
HTML
<style>
.node {
fill: #ccc;
stroke: #fff;
stroke-width: 2px;
}
.link {
stroke: #777;
stroke-width: 2px;
}
.node text {
font: 10px sans-serif;
pointer-events: none;
}
text {
font: 10px sans-serif;
pointer-events: none;
}
</style>
JavaScript
// Define the dimensions of the visualization. We're using
// a size that's convenient for displaying the graphic on
// http://jsDataV.is
var width = window.innerWidth - 20,
height = innerHeight - 20;
var color = d3.scale.category10();
var force = d3.layout.force()
.charge(-300)
.linkDistance(function (l) { return l.value; })
.gravity(0.03)
.friction(0.9)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var graph = getData();
var nodeMap = {};
graph.nodes.forEach(function(d) {
nodeMap[d.name] = d;
//centering first element
if(d.main == true)
{
graph.nodes[0].x = width / 2;
graph.nodes[0].y = height / 2;
}
});
graph.links.forEach(function(l) {
l.source = nodeMap[l.source];
l.target = nodeMap[l.target];
l.distance = l.value;
l.size = l.value;
})
force.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) {
return 1 / (d.value / 1000);
});
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g");
var circle = node.append("circle")
.attr("class", "node")
.attr("id", function (d) { return d.name; })
.attr("r", 30)
.style("fill", function (d) {
return color(d.group);
})
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.call(force.drag);
var label = node.append("svg:text")
.text(function(d) { return d.name; });
force.on("tick", function() {
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) {
console.log(d);
return...