myChart={
const div = html`<div style='max-width: 900px; overflow-x: auto; padding: 0px; margin: 0px;'></div>`;
const svg = d3.select(div)
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const dataset = {
nodes: [
{id: "A"},
{id: "B"},
{id: "C"},
{id: "D"},
{id: "E"},
{id: "F"},
{id: "G"}
],
links: [
{source: "A", target: "B"},
{source: "B", target: "C"},
{source: "C", target: "D"},
{source: "D", target: "E"},
{source: "E", target: "A"},
{source: "A", target: "E"},
{source: "E", target: "D"},
{source: "D", target: "C"},
{source: "C", target: "B"},
{source: "B", target: "A"},
{source: "E", target: "F"}
]
};
console.log("dataset is ...",dataset);
const link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(dataset.links)
.enter().append("line");
const node = svg.append("g")
.attr("class", "nodes")
.selectAll("rect")
.data(dataset.nodes)
.enter().append("rect")
.attr("width", 60)
.attr("height", 60)
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
);
const text = svg.append("g")
.attr("class", "text")
.selectAll("text")
.data(dataset.nodes)
.enter().append("text")
.text(d => d.id)
.attr("fill", "red")
.attr("font-size", "1.9em")
simulation
.nodes(dataset.nodes)
.on("tick", ticked);
simulation.force("link")
.links(dataset.links);
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("x", d => d.x)
.attr("y", d => d.y)
.attr("fill", "blue");
text.attr("x", d => d.x - 5)
.attr("y", d => d.y + 5);
}
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fy = d.y;
d.fx = d.x;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
console.log("dataset after dragged is ...",dataset);
}
return div
}