JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://d3js.org/d3.v3.min.js"></script>
CSS
.link {
fill: url(#gradient);
}
JavaScript
var graph = {
"nodes":[
{"name":"Node 1"},
{"name":"Node 2"}
],
"links":[
{"source":1,"target":0,"sValue":1, "tValue": 2}
]
};
var width = 600,
height = 400;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(130)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var gradient = d3.select("svg").append("defs")
.append("linearGradient")
.attr("id", "gradient")
.attr("spreadMethod", "pad");
//start color white
gradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", "red")
.attr("stop-opacity", 1);
//end color steel blue
gradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", "green")
.attr("stop-opacity", 1);
var link = svg.selectAll("path.link")
.data(graph.links)
.enter().append("svg:path")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); })
.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; });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 20)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
d3.selectAll(".link")
.attr("d", function (d) {
var radius = 10;
var linkVector = new Vector2(d.target.x-d.source.x,d.target.y-d.source.y).getUnitVector();
var perpVector =...