score:1
Here's some code that might achieve the effect you're looking for. It's largely attributable to Mike Bostock's Bubble Chart and Collision Detection examples.
This uses D3's pack layout to initially position the nodes. Then a force layout is used to "auto adjust" the other circles when dragging a node.
Working example at bl.ocks.org
var width = 900;
var height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var nodes = d3.range(128).map(function () { return {radius: Math.random() * 16 + 8}; });
var nodesCopy = $.extend(true, [], nodes);
function dblclick(d) {
d3.select(this).classed("fixed", d.fixed = false);
}
function dragstart(d) {
d3.select(this).classed("fixed", d.fixed = true);
}
function collide(node) {
var r = node.radius + 16;
var nx1 = node.x - r;
var nx2 = node.x + r;
var ny1 = node.y - r;
var ny2 = node.y + r;
return function (quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== node)) {
var x = node.x - quad.point.x;
var y = node.y - quad.point.y;
var l = Math.sqrt(x * x + y * y);
var npr = node.radius + quad.point.radius;
if (l < npr) {
l = (l - npr) / l * 0.5;
x *= l;
node.x -= x;
y *= l;
node.y -= y;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
};
}
function packup() {
var pack = d3.layout.pack()
.sort(null)
.size([width, height])
.padding(0)
.value(function (d) { return d.radius; });
svg.selectAll(".node")
.data(pack.nodes({"children": nodes})
.filter(function (d) { return !d.children; }))
.enter().append("circle")
.attr("r", function (d) { return d.radius; })
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; });
}
function forceup() {
var force = d3.layout.force()
.nodes(nodes)
.gravity(0.05)
.charge(0)
.size([width, height])
.start();
var drag = force.drag().on("dragstart", dragstart);
force.on("tick", function () {
var q = d3.geom.quadtree(nodes);
var i = 0;
var n = nodes.length;
while (++i < n) {
q.visit(collide(nodes[i]));
}
svg.selectAll("circle")
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; });
});
d3.selectAll("circle")
.on("dblclick", dblclick)
.call(drag);
}
function reset() {
svg.selectAll("*").remove();
nodes = $.extend(true, [], nodesCopy);
packup();
forceup();
}
packup();
forceup();
Source: stackoverflow.com
Related Query
- d3js dragging circle should rearrange other circles in main boundary circle
- I am using d3 to visualize some nodes and links. I want to be able to fadeout other circles (nodes) when I hover over one circle
- D3 drawing circles on the circumference of other circle
- Tooltips for nested circles in D3 circle pack layout
- Stop Force Layout on d3js and start free dragging nodes
- Angular 4 + D3v4 : dragging circle not working correctly in conjunction with zooming
- d3js Making last circle a hyperlink
- How to draw circles around circular path with D3js
- Text along circles in a D3 circle pack layout
- How to change the alignment of circles in packed circle d3
- d3js force layout dynamically adding image or circle for each node
- Text not displaying in the middle of circles in d3js
- Controlling order of circles in D3 circle pack layout algorithm
- d3js : how to put circles at the end of an arc
- Dragging on force layout prevents other mouseup listeners
- All circle elements stuck behind each other
- add circle in a spiral chart with d3js with line connecting to center
- d3js - SVG circle in smooth perpetual motion
- Stop moving other nodes while dragging one node in D3 forceSimulation
- Circle Not Generating For All Value In csv File In ScatterPlot d3js
- d3js Box and Whisker plot: add a single circle to plot given a point in the distribution
- D3js responsive stacked bar chart - other topics solutions not working
- d3js won't draw circles using data array
- Text blocking circle click method d3js
- how to adjust parent circle radius using d3js pack layout
- D3 getting the id of polygon underneath while dragging a circle
- d3 - draw smooth line if two circles or points are close to each other
- D3jS show info on hover over circle
- How should i use nested data in d3js
- Move d3 circles away from center circle - force layout
More Query from same tag
- d3.js - evenly spaced bars on a time scale
- Can't delete circle in D3 on update
- Every imported function comes up as "undefined", though the import is clearly being made
- Non-overlapping nodes in Forced-Directional Graph
- Turn string to array name in D3 Javascript
- Trying to link object and text when dragging in v4
- Applying stroke-dasharray style to only part of a path
- D3 Layout : Tree-like Structure, but link length varies
- Performing operations on substrings of text objects generated by D3
- d3 grouped bar chart creation issue
- d3.js Linechart in angular2 taking data from url(server) not Local data
- How to access the DOM element that correlates to a D3 SVG object?
- What does the row function do in this D3 multi-series line graph?
- How do I add labels to d3.js force bubble chart
- d3 Responsive bar chart
- D3 js bar chart pan
- Tooltip not working as expected
- How to read JSON data from same json for tree layout insteadof reading from other .JSON file?
- Passing Object function into data() in d3
- While creating a bar graph using d3 how to associate the bar height with the numbers on the scale?
- Is there a way to get a subset/section of a path in d3.js?
- d3 limit zoom level for time axis
- How do I output subtasks to the diagram d3.js?
- Changing rectangle dimensions in d3 zoom
- Plotting 2 graphs together in Rickshaw
- Zooming and panning 2 corresponding images in d3.js by same amount?
- how to remove gridlines for subticks for log scale
- Cleaning lower levels of D3 nested data
- How do I print out an array to screen ? SVG/D3/JS
- Understanding d3 visualization quadtree, continued (2)