📅  最后修改于: 2022-03-11 15:03:56.398000             🧑  作者: Mango
var width = 600,
height = 400;
var colorScale = ['orange', 'lightblue', '#B19CD9'];
var xCenter = [100, 300, 500]
var numNodes = 100;
var nodes = d3.range(numNodes).map(function(d, i) {
return {
radius: Math.random() * 25,
category: i % 3
}
});
var u = d3.select('svg g')
.selectAll('circle')
.data(nodes);
var enter = u.enter()
.append('circle')
.attr('r', function(d) {
return d.radius;
})
.style('fill', function(d) {
return colorScale[d.category];
});
u = enter.merge(u);
u.exit().remove();
var simulation = d3.forceSimulation(nodes)
.force('charge', d3.forceManyBody().strength(5))
.force('x', d3.forceX().x(function(d) {
return xCenter[d.category];
}))
.force('collision', d3.forceCollide().radius(function(d) {
return d.radius;
}))
.on('tick', ticked);
function ticked() {
u.attr('cx', function(d) {
return d.x;
})
.attr('cy', function(d) {
return d.y;
})
}