chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
const simulation = forceSimulation(nodes, links).on("tick", ticked);
const width = 600
const svg = d3.select('#my-canvas')
.attr("viewBox", [-width / 2, -height / 2, width, height]);
svg.append("svg:defs").selectAll("marker")
.data(["end"])
.enter().append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 12)
.attr("markerHeight", 12)
.attr("stroke", "red")
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll(".link-group")
.data(links)
.enter().append("line")
.attr("class", "link-group")
.attr("stroke-width", 1)
.attr("marker-end", "url(#end)");
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
node.append("circle")
.attr("r", 10)
.attr("fill", color)
.call(drag(simulation));
node.append("text")
.text(d => d.__proto__.id)
.style('fill', '#0000')
function ticked() {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
node
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
return svg.node();
}