score:21
Accepted answer
You simply need to set the origin()
function accordingly. In the second case this is straightforward, in the first case somewhat more difficult because you are using coordinates as well as a transform. The basic pattern (for the second case) looks like this:
.origin(function() {
var t = d3.select(this);
return {x: t.attr("x"), y: t.attr("y")};
})
Updated jsfiddle here.
score:0
group element has no x/y attributes - no problem! create ones:
var node = svg.append("g")
.attr('x',x_1)
.attr('y',y_1)
.attr("class","node")
.attr("name","Node_A")
.attr("transform","translate(" + x_1 + ", " + y_1 +")")
.call(drag);
when, the .origin function from Lars will work
and dont forget to update x,y attributes, while dragging:
function dragged() {
d3.select(this).attr("transform","translate(" + d3.event.x + ", " + d3.event.y +")");
d3.select(this).attr("x",d3.event.x);
d3.select(this).attr("y",d3.event.y);
}
Source: stackoverflow.com
Related Query
- How to set the Origin (drag.origin) for drag behavior in d3 JavaScript library
- How to set the origin while drag in d3.js in v4
- How to set the label for each vertical axis in a parallel coordinates visualization?
- How do you change the interpolation for a zoom behavior in d3.js
- How do I graph an individual line for each the groups within a Javascript map object using D3.js
- How do I use two versions of the same javascript library without bleeding functions in the same page?
- d3 library - how to set the legend values
- How do I set the X values for a singled stacked horizontal bar chart using d3.js?
- D3.js version 4: How to properly set the x-axis-intervals for a histogram
- How to properly set the type for property `d` in `path` element in a d3 react typescript application
- How can I set the transform origin to the middle of this rectangle in D3?
- need to set value for r(radius) in the <circle> tag using javascript
- How to limit the d3.js drag behavior on a mercator map
- How to set pattern origin at the top left point of each bar of a barchart in d3.js?
- How to set a scale for the time series, current x scale gives me NaN?
- how do I set up a grouping query on the server side. I want set up the query so I can use it for a pie chart in front end, using d3js
- How can I add an image as the fill for a circle dynamically in javascript using d3
- How to make into d3.js v3.4 the same behavior drag as into v3.1?
- D3.js: How to get the computed width and height for an arbitrary element?
- How to set text color for my d3 chart title?
- How do I display dates on the x-axis for nvd3 / d3.js?
- How to set the background-color of a D3.js svg?
- D3 Differentiate between click and drag for an element which has a drag behavior
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- how to set the domain and scale on an axis on a nvd3.js multiBarChart
- D3 force layout: How do I set the size of every node?
- D3js: How do I clear the zoom scale set by the d3.zoom event?
- How to use a JavaScript chart library like D3.js or Raphaƫl in server-side Java
- Putting an arrow (marker) at specific point on a path when using the d3 javascript library
More Query from same tag
- How to apply box-shadow effect on svg circle by changing svg filter element in D3 chart
- adding objects into an existing object
- d3.js: Cannot read property 'call' of undefined
- Assigning two class names from functions in D3
- event.pageX is working in Chrome but not working in Firefox
- Maximum Capacity of Input Data (CSV or JSON format) in drawing D3 visualization?
- Defining Regions in a US Map Using D3.js
- zooming on svg with d3 in angular
- Parsing error for nested object?
- D3.js <div> not working
- how can i group JSON data into the weeks according to calender?
- D3 Bar Chart, bars not showing, axis on top
- How to import d3 library properly in node.js?
- D3 mouseover event is not triggered on the bottom elements
- D3.js: Context + Focus zoom on multiple graphs
- PieChart in DC.js with Queue.js not working
- d3js (v4) canvas force layout with text on nodes
- Connect childs of a D3.js tree diagram if they have the same value
- d3.js Maximum call stack size exceeded error
- How to get Component reference(this) in D3 Context Menu OnOpen and OnClose
- How does D3JS send data into events
- kafka Rest calls + D3 animated chart
- Can d3 build shapes within a shape?
- d3 stacked area tooltip
- How to convert a jQuery object into a d3 object?
- Having trouble scaling my d3 map
- How to add live data to stacked bar chart
- Merging arrays that hold CSV data into one object array
- Using data from two separate geoJSON files in D3.js map visualization
- embed json file within d3.js (d3.layout.partition)