score:25
First off, the <g> element doesn't care about x
and y
attributes (as in: they're just ignored). You can use transform="translate(x,y)"
instead.
Second, you will need to check that the element you get in the dragmove handler actually is the <g> element, and not a child element of it. This is because <g> elements have no actual hit area themselves. Their children do though, and the mouse events first go to the children then bubble up to the parent. You can inspect evt.target
and evt.currentTarget
to see this in action. target
is the element that was hit originally, currentTarget
is the event target that is currently handling the event (e.g the <g> element if the event bubbled up).
score:4
For d3 v4:
var drag_this = d3.drag().subject(this)
.on('start',function (d) {
if (d.x1){
d.x1 = d3.event.x - d.xt;
d.y1 = d3.event.y - d.yt;
}else{
d.x1 = d3.event.x;
d.y1 = d3.event.y;
}
})
.on('drag',function(d){
d3.select(this)
.attr("transform", "translate(" + (d3.event.x - d.x1) + "," + (d3.event.y - d.y1) + ")");
d.xt = d3.event.x - d.x1;
d.yt = d3.event.y - d.y1;
});
my_group.call(drag_this);
This assumes that you have data bound to the group.
Source: stackoverflow.com
Related Query
- How to drag an svg group using d3.js drag behavior?
- How to drag and drop a group of rectangles in svg in d3.js?
- Using d3.behavior.drag, How to keep a drag image visible outside of the svg element
- How to drag drop elements into svg group
- How can I make 2 different elements in a SVG group have different drag behaviours?
- How to add an image to an svg container using D3.js
- How do I get the width of an svg element using d3js?
- How to set the Origin (drag.origin) for drag behavior in d3 JavaScript library
- How to show a tooltip with value when mouseover a svg line graph using d3.js?
- How to get reference to SVG (child) elements nested in another SVG element (parent) using D3?
- How to add filled sections to SVG circles using d3.js
- How can I drag group (g) element in D3.js?
- How to output SVG in a Jupyter notebook using jsdom, D3 and IJavascript
- How to group by multiple keys at the same time using D3?
- How to move elements along with svg group
- How to render end ticks always using d3 svg axis
- How do I create a minimap of an SVG using D3 v5 and show the current viewport dimensions?
- How do I save an svg node to a file using nodejs?
- How to create a flow layout in SVG using D3.js?
- How to drag and rotate orthographic map (globe) using d3.js
- How to get SVG circle to drag along SVG path?
- How to select and deselect a svg line and change its color on selection and deselection using d3.js and javascript , jquery?
- How can I make a SVG group follow a gradient in a circle
- How can I add an external group to an existing svg with d3.js?
- How to append an svg to an prexisting svg using d3.js?
- How to create responsive svg using d3.js
- How to color SVG image based on value from color range using d3?
- Using D3 to generate SVG Pie Chart how to vertically align text
- How to unregister zoom behavior of SVG element in d3.js
- How so create complex SVG shapes using D3JS?
More Query from same tag
- D3 color change on mouseover using classed("active",true)
- How to set up custom color scale with multiple sub-parts in D3?
- Is there a way to create scales with domains that have a sub-domain?
- D3 V4 - Trying to Chart a Simple line Graph
- Bring a SVG line to front
- Check in Jest unit tests that returned brushX or brushY
- SVG expand line verticaly
- Updating graph in d3
- D3 with Backbone / D3 with Angular / D3 with Ember?
- On button click open a new window and draw multiple large circles with D3
- d3 donut chart transform: translateY
- Leaflet Choropleth Map and D3 Line Graph Interference
- D3: Absolute Positioned Tooltip
- Grouped multiple stacked bar chart
- How can I continuously update the range of the x axis in d3 graph?
- Bar Chart outside bounds and mouseover event issue
- CSV File opened using JS not showing the updated version of the file when replaced by another one with the same name. d3.csv
- SVG GaussianBlur quality
- D3.js setting multiple styles dynamically
- How do I get my topojson file to work with datamaps?
- Export SVG with external CSS to server
- C3.js, y-axis time
- Let's make a TopoJSON map and view it with D3.js
- Using Googlemaps HeatmapLayer with in D3
- Generating tool-tip for dynamic data in D3
- d3.js and dimple, reading data in from a file rather than hard coded
- in d3.js how do you add and remove multiple lines on different click events?
- Remove rect(s) of specific colour from svg - javascript
- d3: search input to return nested data value
- Error encountered when building composite chart dc.js