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: 1},
{id: 2},
{id: 3},
{id: 4},
{id: 5},
{id: 6}
],
links: [
{source: 1, target: 5},
{source: 4, target: 5},
{source: 4, target: 6},
{source: 3, target: 2},
{source: 5, target: 2},
{source: 1, target: 2},
{source: 3, target: 4}
]
};
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("circle")
.data(dataset.nodes)
.enter().append("circle")
.attr("r", 20)
.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)
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("cx", d => d.x)
.attr("cy", d => d.y);
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
}