score:4
Accepted answer
Okay, seems like writing all that actually helped me think and I managed to solve this problem by myself. Posting the answer here just in case somebody happens to have the same problem.
The very simple solution has to do with the object on which the .call(d3.drag())
is executed. I cut this piece of code from the node variable (the circle
SVG elements) and pasted it at the end of the variable that defines the g
SVG elements:
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
So the g
elements declaration ends up like this:
var gnodes = svg.selectAll("g.gnode")
.data(graph.nodes)
.enter()
.append("g")
.classed("gnode", true)
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
And it works correctly from then on. You can even drag just the label, and the node will follow it around the canvas. Great!
Source: stackoverflow.com
Related Query
- Dragging nodes with labels in d3 v4 force layout glitches
- Dragging force layout nodes by dragging their labels
- d3js create a force layout with fixed nodes
- Stop Force Layout on d3js and start free dragging nodes
- D3 force layout graph with nodes positioned in a grid
- d3js (v4) canvas force layout with text on nodes
- Speed up d3 force layout with many nodes and links
- D3js force layout - line with gradient between nodes
- d3 force collapsible layout - start page with all nodes collapsed
- How to use D3 force layout with existing SVG elements as nodes
- Add labels to nodes in Hobbelt's "Group/Bundle Nodes" D3 force layout example?
- Updating nodes and links with labels in d3 force directed network graph is not removing the nodes properly
- Remove all .fixed classes from force layout nodes with jQuery
- d3js force layout with hide/unhide on node click misplaces nodes after expanding
- force layout set fixed distance each nodes even after dragging
- d3.js force layout with some fixed nodes (in a grid)
- Force layout collision detection with group nodes
- Force layout nodes filled with images
- d3.js collapsible force layout with all the nodes collapsed
- D3 force layout with some nodes collapsed
- D3 force layout - Links exceed the boundaries of the container, while dragging the nodes
- D3.js force layout with drag/zoom/pan and join nodes capabilities
- d3.js collapsible force layout with images won't start with nodes collapsed
- D3 force directed layout with bounding box
- Simple graph of nodes and links without using force layout
- d3.js: "Cannot read property 'weight' of undefined" when manually defining both nodes and links for force layout
- D3 force layout - linking nodes by name instead of index
- Avoid collision between nodes and edges in D3 force layout
- Labels / text on the nodes of a D3 force directed graph
- Update existing nodes in a d3 Force Layout
More Query from same tag
- updating d3 chart with input from form
- adding labels to node in d3.js
- getting json data from localhost to use in D3
- specify color scheme for n number of nodes in networkD3 in R
- Why doesn't click event always fire?
- d3.js - How to stylize a pie chart
- Barchart X-Axis is getting hidden when bringing to bottom in D3.js
- set style conditionally with if value of variable matches specific string in array
- Cannot run python SImpleHTTPServer on windows 10 for d3.js testing (issue changing environment variable)
- SVG / HTML 5 canvas - circles pattern displacement, stripe.com approach
- Polygons in d3.js not filling/building correctly
- What programming paradigm/metamodel characterizes D3.js?
- D3 area chart filling above the line
- dc.js - dynamically change valueAccessor of a stacked layer in a lineChart and redraw it
- d3 tree <g> tag padding/border overall styling
- How can I attach dynamic objects to an HTML element?
- Getting Cannot read property 'type' of null topojson
- Loading D3 from a local variable
- How to smooth in zoom in and zoom out in D3.js with 50,000 nodes?
- Retrieve stroke-width attribute of d3 axis path
- hovering (migration of .ease(d3.easeBounceOut) to .ease("easeBounceOut"))
- Offset x-axis ticks with scaleTime
- Wrong result in dimplejs scatterplot
- Animate line graph line
- nvd3 pie outerRadius
- Adding a Background to certain part of a graphic with nvd3
- D3.js event listeners cannot access "this" when ES6 arrow functions are used
- d3.js: Update multiple pie charts
- D3.js Trendline x-axis date
- Link two D3 plots