score:56
Accepted answer
i have made a small code snippet. hope this is helpful.
var data = {
nodes: [{
name: "a",
x: 200,
y: 150
}, {
name: "b",
x: 140,
y: 300
}, {
name: "c",
x: 300,
y: 300
}, {
name: "d",
x: 300,
y: 180
}],
links: [{
source: 0,
target: 1
}, {
source: 1,
target: 2
}, {
source: 2,
target: 3
}, ]
};
var c10 = d3.scale.category10();
var svg = d3.select("body")
.append("svg")
.attr("width", 1200)
.attr("height", 800);
var drag = d3.behavior.drag()
.on("drag", function(d, i) {
d.x += d3.event.dx
d.y += d3.event.dy
d3.select(this).attr("cx", d.x).attr("cy", d.y);
links.each(function(l, li) {
if (l.source == i) {
d3.select(this).attr("x1", d.x).attr("y1", d.y);
} else if (l.target == i) {
d3.select(this).attr("x2", d.x).attr("y2", d.y);
}
});
});
var links = svg.selectall("link")
.data(data.links)
.enter()
.append("line")
.attr("class", "link")
.attr("x1", function(l) {
var sourcenode = data.nodes.filter(function(d, i) {
return i == l.source
})[0];
d3.select(this).attr("y1", sourcenode.y);
return sourcenode.x
})
.attr("x2", function(l) {
var targetnode = data.nodes.filter(function(d, i) {
return i == l.target
})[0];
d3.select(this).attr("y2", targetnode.y);
return targetnode.x
})
.attr("fill", "none")
.attr("stroke", "white");
var nodes = svg.selectall("node")
.data(data.nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("cx", function(d) {
return d.x
})
.attr("cy", function(d) {
return d.y
})
.attr("r", 15)
.attr("fill", function(d, i) {
return c10(i);
})
.call(drag);
svg {
background-color: grey;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
score:5
gilsha has a great answer, but note that newer versions of d3 no longer use the behavior module.
instead of this:
var drag = d3.behavior.drag()
.on("drag", function(d, i) {
d.x += d3.event.dx
d.y += d3.event.dy
d3.select(this).attr("cx", d.x).attr("cy", d.y);
links.each(function(l, li) {
if (l.source == i) {
d3.select(this).attr("x1", d.x).attr("y1", d.y);
} else if (l.target == i) {
d3.select(this).attr("x2", d.x).attr("y2", d.y);
}
});
});
simply change d3.behavior.drag() to d3.drag()
var drag = d3.drag()
.on("drag", function(d, i) {
d.x += d3.event.dx
d.y += d3.event.dy
d3.select(this).attr("cx", d.x).attr("cy", d.y);
links.each(function(l, li) {
if (l.source == i) {
d3.select(this).attr("x1", d.x).attr("y1", d.y);
} else if (l.target == i) {
d3.select(this).attr("x2", d.x).attr("y2", d.y);
}
});
});
Source: stackoverflow.com
Related Query
- 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
- How can I show a graph using d3 force layout with initial positions set and no movement at all?
- How to check d3 js force graph for nodes with no links and remove them?
- D3 force graph rendering nodes and links
- Speed up d3 force layout with many nodes and links
- reading nodes and edges from two distinct csv files using Force Layout
- D3v4 force directed graph - localStorage disconnects links and nodes
- Updating nodes and links with labels in d3 force directed network graph is not removing the nodes properly
- how to highlight(change color) of all connected(neighbours) nodes and links in a d3 force directed graph
- D3 Force Graph With Arrows and Curved Edges - shorten links so arrow doesnt overlap nodes
- d3 force directed layout - drawing links without changing the location of the nodes
- D3.js network graph using force-directed layout and rectangles for nodes
- force directed graph filter nodes and links
- Dynamically change opacity of links and nodes in d3 force graph based on radio button filter
- Why do I have to re-assign the links and nodes when updating the d3.js force layout
- Create nodes based on content BBox and get width of source node when adding links in force layout
- D3 force layout shall add nodes and links when clicking a node
- D3js force layout update nodes and links
- how to implement zoom in d3 graph of force directed layout and position the nodes
- Nodes and links in Collapsible Force Layout in D3 Javascript
- Avoid collision between nodes and edges in D3 force layout
- Saving and reloading a force layout using d3.js
- Adding and Removing Nodes in D3js Force Graph
- D3 highlight selected node, and its links to parent and ancestors in a force directed graph
- D3 force layout: links and nodes --- z-index(?)
- Stop Force Layout on d3js and start free dragging nodes
- Place pie charts on nodes of force directed layout graph in D3
- D3 force layout graph with nodes positioned in a grid
- how to generate a network graph with edge weight using D3.js force layout
More Query from same tag
- Listen for hover in legend
- d3 connected scatterplot example, question on syntax
- How to control the number of tick values in a graph with d3js
- How to log rotate attribute in transform using d3.js?
- How to update d3 table?
- D3 custom attributes not being selected
- Interactive Legend onclick or mouseover - D3js
- d3.js tree layout is bunching everything up at the top
- Simultaneously extracting list of dimensions and creating scale for each in d3.js - Expand on process?
- HTML2Canvas not rendering D3 font and font size
- d3.js rewriting zoom example in version4
- Brush selection in Box plot(d3.js)
- Integrating button to change divergent bar chart in d3.js?
- New layout after opening a group not base on the last layout with cola.js
- How to reuse JSON data that may have changed?
- Propagating data with selectAll() of d3.js
- get color of element in D3
- How to show utf-8 string in D3.js?
- outputing array variables javascript
- Remove JS SCRIPT from MASTER page in child pages
- Can you set style in d3 via something like foo.style({color:blah, background:blah})?
- D3 : Re-entrant table function
- d3.js axis add tick value
- Brush and transition coexist
- Functions not recognised when using R2D3
- d3.js zoomable circle packing: change radius for specific circles
- Problems in displaying multi-line chart
- d3.js - Plotting points on zoomable, clickable map
- Flush a d3 v4 transition
- d3.js root node "name" tag onclick pop jquery dialog