{
const svg = d3.create('svg')
.attr('width', anim2.width)
.attr('height', anim2.height)
const onMouseEnter = () => {
const ev = d3.event;
console.log('ticked');
datanodes.forEach((d, i) => {
datanodes[i].x = Math.random() * width / 2
datanodes[i].y = Math.random() * height / 2
})
sim.force('x', d3.forceX().x(d => d.index * 20))
.force('y', d3.forceY().y(d => height / 2))
.alpha(1)
.restart();
}
const onMouseLeave = () => {
const ev = d3.event
sim.force('x', d3.forceX().x(() => Math.random() * width / 2))
.force('y', d3.forceY().y(() => Math.random() * height / 2))
.alpha(1)
.restart();
}
svg.on('mouseenter', onMouseEnter);
svg.on('mouseleave', onMouseLeave);
const ticked = () => {
texts.attr('transform', d => `translate(${d.x},${d.y})`)
}
const sim = d3.forceSimulation(datanodes)
.force('x', d3.forceX().x(d => d.index * 20))
.force('y', d3.forceY().y(d => height / 2))
.force('charge', d3.forceManyBody().strength(2))
.force('center', d3.forceCenter(width / 2, height / 2))
.force('collision', d3.forceCollide().radius(5))
.on('tick', ticked);
const gEnter = enter => enter.append('text')
.classed('mytext', true)
.text(d => d.text)
const gUpdate = null;
const gExit = null;
const texts = svg.selectAll('.mytext')
.data(datanodes)
.join(gEnter, gUpdate, gExit);
return svg.node();
}