Example illustrating the use of both d3-force-attract
and d3-force-cluster
.
forked from ericsoco's block: d3-force-cluster + d3-force-attract I
forked from f94f's block: Segundo Circulos
forked from anonymous's block: Segundo Circulos
xxxxxxxxxx
<html>
<head>
<title>d3-force-cluster and d3-force-attract</title>
</head>
<style>
/*tooltip*/
.show {
display: block;
}
.hidden {
display: none;
}
.tooltip {
background-color: #333;
padding: 4px 8px;
position: absolute;
color: #fff;
opacity: 0.8;
z-index: 100;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/d3-force-attract@latest"></script>
<script src="https://unpkg.com/d3-force-cluster@latest"></script>
<script>
var datos = [
{text: "Emp", frequency: 31},
{text: "Pro", frequency: 20},
{text: "Can", frequency: 58},
{text: "No", frequency: 25},
{text: "Cli", frequency: 31},
{text: "Epa", frequency: 20},
{text: "Prd", frequency: 58},
{text: "Cie", frequency: 25},
{text: "Ent", frequency: 31},
{text: "Noh", frequency: 20},
{text: "Cen", frequency: 58},
{text: "Otros", frequency: 25}
];
var width = 960,
height = 500,
padding = 1.5, // separation between same-color nodes
clusterPadding = 2, // separation between different-color nodes
maxRadius = 50;
var n = datos.length, // total number of nodes
m = datos.length; // number of distinct clusters
//var color = d3.scaleSequential(d3.interpolateRainbow)
// .domain(d3.range(m));
var color = d3.scaleLinear().domain([1,length])
.interpolate(d3.interpolateHcl)
.range([d3.rgb("#EBEBFF"), d3.rgb('#0000FF')]);
// The largest node for each cluster.
var clusters = new Array(m);
var nodes = d3.range(n).map(function () {
var i = Math.floor(Math.random() * m),
r = Math.sqrt((i + 1) / m * -Math.log(Math.random())) * maxRadius,
r1 = datos["frequency"] * maxRadius,
d = {
cluster: i,
radius: r,
x: Math.cos(i / m * 2 * Math.PI) * 200 + width / 2 + Math.random(),
y: Math.sin(i / m * 2 * Math.PI) * 200 + height / 2 + Math.random()
};
if (!clusters[i] || (r > clusters[i].radius)) clusters[i] = d;
return d;
});
var simulation = d3.forceSimulation()
// keep entire simulation balanced around screen center
.force('center', d3.forceCenter(width/2, height/2))
// pull toward center
.force('attract', d3.forceAttract()
.target([width/2, height/2])
.strength(0.01))
// cluster by section
.force('cluster', d3.forceCluster()
.centers(function (d) { return clusters[d.cluster]; })
.strength(0.5)
.centerInertia(0.1))
// apply collision with padding
.force('collide', d3.forceCollide(function (d) { return d.radius + padding; })
.strength(0))
.on('tick', layoutTick)
.nodes(nodes);
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
var node = svg.selectAll('circle')
.data(nodes)
.enter().append('circle')
.style('fill', function (d) { return color(d.cluster/10); })
.call(d3.drag()
.on('start', dragstarted)
.on('drag', dragged)
.on('end', dragended)
);
function dragstarted (d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
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;
}
// ramp up collision strength to provide smooth transition
var transitionTime = 3000;
var t = d3.timer(function (elapsed) {
var dt = elapsed / transitionTime;
simulation.force('collide').strength(Math.pow(dt, 2) * 0.7);
if (dt >= 1.0) t.stop();
});
function layoutTick (e) {
node
.attr('cx', function (d) { return d.x; })
.attr('cy', function (d) { return d.y; })
.attr('r', function (d) { return d.radius; });
}
</script>
</body>
</html>
https://d3js.org/d3.v4.min.js
https://unpkg.com/d3-force-attract@latest
https://unpkg.com/d3-force-cluster@latest