{
const data = {
nodes: [
{id: "A", group: 1},
{id: "B", group: 1},
{id: "C", group: 2},
{id: "D", group: 2},
{id: "E", group: 3},
{id: "F", group: 3},
],
links: [
{source: "A", target: "B", value: 1},
{source: "A", target: "C", value: 2},
{source: "B", target: "D", value: 3},
{source: "C", target: "D", value: 4},
{source: "E", target: "F", value: 5},
]
};
const width = 600;
const height = 400;
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
const color = d3.scaleOrdinal().domain(nodes.map(d=>d.group).sort(d3.ascending)).range(d3.schemeCategory10);
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
let node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 5)
.attr("fill", color)
.call(drag(simulation));
}