score:33
Creating a "compound" element is as simple as appending one or more children to another element. In your example, you want to bind your data to a selection of <a>
elements, and give each <a>
a single <circle>
child.
First of all, you need to select "a.node"
instead of "circle.node"
. This is because your hyperlinks are going to be the parent elements. If there isn't an obvious parent element, and you just want to add multiple elements for each datum, use <g>
, SVG's group element.
Then, you want to append one <a>
element to each node in the entering selection. This creates your hyperlinks. After setting each hyperlink's attributes, you want to give it a <circle>
child. Simple: just call .append("circle")
.
var node = vis.selectAll("a.node")
.data(nodes);
// The entering selection: create the new <a> elements here.
// These elements are automatically part of the update selection in "node".
var nodeEnter = node.enter().append("a")
.attr("class", "node")
.attr("xlink:href", "http://whatever.com")
.call(force.drag);
// Appends a new <circle> element to each element in nodeEnter.
nodeEnter.append("circle")
.attr("r", 5)
.style("fill", function(d) { return fill(d.group); })
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
Remember that D3 primarily operates on selections of nodes. So calling .append()
on the entering selection means that each node in the selection gets a new child. Powerful stuff!
One more thing: SVG has its own <a>
element, which is what I was referring to above. This is different from the HTML one! Typically, you only use SVG elements with SVG, and HTML with HTML.
Thanks to @mbostock for suggesting that I clarify the variable naming.
score:11
Reply to Jason Davies (since stackoverflow limits the length of reply comments…): Excellent answer. Be careful with the method chaining, though; typically you want node
to refer to the outer anchor element rather than the inner circle element. So I'd recommend a small variation:
var node = vis.selectAll("a.node")
.data(nodes)
.enter().append("a")
.attr("class", "node")
.attr("xlink:href", "http://whatever.com")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.call(force.drag);
node.append("circle")
.attr("r", 5)
.style("fill", function(d) { return fill(d.group); });
I've also replaced the circle's cx and cy attributes with a transform on the containing anchor element; either one will work. You can treat svg:a elements as svg:g (both are containers), which is nice if you want to add labels later.
Source: stackoverflow.com
Related Query
- How to add compound node in a D3 force layout?
- How to add node labels to Bounded Force Layout (D3 v4)?
- Add text label to d3 node in Force layout
- How to display a text when mouseover a node in D3 force layout
- How to get the same node positions in d3's force layout graph
- D3 Force Layout : How to force a group of node to stay in a given area
- How to add text to Cluster Force Layout bubbles
- How to add a force drag event in D3 and make the node stay where i leave it?
- How to add text in the center of node in force directed graph?
- How to pan to a node using d3's force layout
- d3: Optionally add child elements based on node content in force layout
- How to organise node positions in D3 Force layout
- How to define the node entering position in D3 js force layout
- d3 force layout - how to create a more sensible node structure
- How to add hypertext link to Cluster Force Layout circle
- How to create satellite nodes to a main node in D3.js force layout graphs
- D3 force layout shall add nodes and links when clicking a node
- How to update a data of a node in d3 force layout using react
- How to use hover and add text over node in d3 force simulation
- how to add title to d3js bubble chart with force layout
- d3: Optionally add child elements based on node content in force layout - when filter is not the solution
- Fix Node Position in D3 Force Directed Layout
- How to make force layout graph in D3.js responsive to screen/browser size
- Add text label to d3 node in Force directed Graph and resize on hover
- How to update elements of D3 force layout when the underlying data changes
- d3.js: suggested node position in force layout
- D3 force layout fix root node at the center
- How to speed up the force layout animation in d3.js
- D3 Force Layout Graph - Self linking node
- how find out if force layout done placing the nodes?
More Query from same tag
- D3.js Line chart not updating properly when applying the general update pattern
- Javascript D3 graphs, sorting an array
- json values in variable for D3 chart throwing error.While the static json files works fine
- d3 line graph with smooth update animation
- Conditional fill on multiple svg based on data
- improve cartographic visualization
- D3 positioning a custom SVG on scattersplot
- D3.js Enter Animation
- D3 updating x-y axes
- SVG path not displaying
- (d3.js force directed graph) Cannot create property 'vx', but this happens in localhost only
- Why does external css doesnt work on svg elements in IE using D3
- Is there a way to make an SVG USE subsequently modifiable (or a different technique)?
- How to override inherited svg style
- No 'Access-Control-Allow-Origin' header is present on the requested resource Error when loading JSON file
- simple cubism.js with json datasource
- change the color of chart d3
- X axis values for histogram
- Les Miserables Co-occurrence
- Error: Invalid value for <text> attribute x="NaN" in angularJS nvd3-multi-bar-chart
- How to replace d3.js path element rather than draw another one?
- Add event to Series.Toggle in Rickshaw
- Progress bar: how to make color transition according to the progress?
- Programmatically zooming to prior zoom state [v7]
- Using retina map tiles in d3/js with Stamen or CartoDB
- How to add a line break using d3.js text
- adding onClick event to show a hidden div above each bar in D3.js (maybe I'm missing important css code also?)
- D3.js Stacked Bar OnClick Action Not Triggered
- Issue labeling d3 sunburst
- Need help lining up x axis ticks with bars in D3.js bar chart