chart_cluster_drag = {
const svg = d3.select(DOM.svg(width, height));
const labels = svg.selectAll('text')
.data(countries)
.enter()
.append('text')
.attr('x', d => xScale(d) + 10)
.attr('y', height/2 +40)
.attr('font-size', 14)
.text(d => d);
const nodes = svg
.selectAll('circle').data(data)
.enter()
.append('circle')
.attr('fill', d => color(d.launchCountry))
.attr('r', 5);
const ticked = () => {
nodes
.attr('cx', d => d.x)
.attr('cy', d => d.y)
}
const dragstarted = (d) => {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
const dragged = (d) => {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
const dragended = (d) => {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
nodes.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
const simulation = d3.forceSimulation(data)
.force("charge", d3.forceManyBody()
.strength(-5)
.distanceMax(100))
.force('collision', d3.forceCollide()
.radius(5))
.force("center", d3.forceCenter(width / 2, height / 2))
.force('x', d3.forceX().x(d=> xScale(d.launchCountry)))
.force('y', d3.forceY().y(0))
.on('tick', ticked)
return svg.node()
}