score:1
With .append("g")
you insert a SVG Group Element.
The problem is, that you try to apply attributes that are for circles, like the radius with .attr("r",15)
, to the group element.
You have to use circles if you want to draw a circle. Group elements do not have any shape. They are used to group elements like circles.
A solution would be to append the g element and transform it to the location of the node. I updated your code in the following snippet. I used the group elements and added the circle, image and text inside the group elements.
Moreover I removed the backslashes before each angular bracket and set the title to the field name
instead of label
.
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var graph = getData();
var nodeMap = {};
graph.nodes.forEach(function(d) { nodeMap[d.name] = d; });
graph.links.forEach(function(l) {
l.source = nodeMap[l.source];
l.target = nodeMap[l.target];
})
force.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter()
.append("line")
.attr("class", "link")
.style("stroke", function(d) {
return d.line_color;
})
.style("stroke-width", function(d) {
return Math.sqrt(d.value)+1;
});
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter()
.append("g")
.attr("transform", function(d){return "translate("+d.x+","+d.y+")"})
.call(force.drag);
node.append("circle")
.attr("class", "node")
.attr("r", 15)
.style("fill", function(d) { return d.fill_color; })
.on("click", function(d){
alert("You clicked on node " + d.name);
});
node.append("title")
.text(function(d) { return d.name; });
node.append("image")
.attr("xlink:href", function(d) { return d.image_url })
.attr("x", -8)
.attr("y", -8)
.attr("width", 26)
.attr("height", 26);
node.append("text")
.attr("dx", function(d) {
if (d.image_url == "/profile.png"){
return 100;
}
else{
return 16;
}
})
.attr("dy", function(d) {
if (d.image_url == "/profile.png"){
return 100;
}
else{
return ".35em";
}
})
// .attr("dy", ".35em")
.text(function(d) { return d.name });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d){return "translate("+d.x+","+d.y+")"});
});
function getData() {
return {
"nodes":[
{"name":"user1","image_url":"http://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/103px-Wikipedia-logo-v2.svg.png","fill_color":"blue","text_color":"black"},
{"name":"user2","image_url":"http://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/103px-Wikipedia-logo-v2.svg.png","fill_color":"blue","text_color":"black"},
{"name":"user3","image_url":"http://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/103px-Wikipedia-logo-v2.svg.png","fill_color":"blue","text_color":"black"},
{"name":"tag1","image_url":"","fill_color":"blue","text_color":"black"},
{"name":"tag2","image_url":"","fill_color":"blue","text_color":"black"},
{"name":"tag3","image_url":"","fill_color":"blue","text_color":"black"}
],
"links":[
{"source":"tag1","target":"user1","value":1,"line_color":"green"},
{"source":"tag2","target":"user1","value":1,"line_color":"green"},
{"source":"tag3","target":"user1","value":1,"line_color":"green"},
{"source":"tag1","target":"user2","value":1,"line_color":"green"},
{"source":"tag2","target":"user2","value":1,"line_color":"green"}
]
};
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- Labels and images for D3 nodes incorrectly show at top left of screen
- D3.js v7 - How to make Y axis labels always show on screen for a scrollable chart
- d3.js: "Cannot read property 'weight' of undefined" when manually defining both nodes and links for force layout
- 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?
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- How to show and hides nodes when you move the mouse over a node in D3 Javascript
- How to check d3 js force graph for nodes with no links and remove them?
- How to make labels and nodes in D3 forced layout clickable to navigate to a URL?
- Setting different images for D3 force-directed layout nodes
- D3: Tree diagram with images and circles as nodes
- d3 show labels only for ticks with data in a bar chart
- JS- how to remove duplicate JSON nodes and add one link for all nodes that get merged
- Changing nodes for images in d3.js force layout graph
- How can I add images in the nodes of a D3 Sankey diagram, using the rcv and networkD3 packages in R?
- Updating nodes and links with labels in d3 force directed network graph is not removing the nodes properly
- Show yearly X labels for June instead of January on d3.js chart
- How to reset svg scaling and fit to screen for random but large maps/datasets with different orientations
- D3 JS chart is cut off after adding labels for X and Y axis
- Interactive labels on nodes using python and networkx
- D3, SVG, and Javascript : Need to assign unique images to dynamically created nodes
- D3 Tree - How to expand tree to show and highlight all nodes of same name?
- How to convert table data to nodes and links for d3js Sankey
- Graph nodes pushed to top left corner of svg
- D3 show text for nodes when zoomed
- d3.js v4 - nodes are stuck in the top left corner
- How to specify the colors and toggle labels for each category in R sunburst?
- NetworkD3 Sankey Plot in R: How to switch text labels from the right to the left of the inner nodes only
- Zoom left and right axis for dual scaled chart in d3
- Colored text and line-breaks for D3 node labels
- Is it possible for a ElasticY left axis and a defined rightYaxis domain?
More Query from same tag
- Displaying place (city) data with D3 and TopoJson
- Iteratively appending links to a D3 force directed network visualization
- D3.JS chart not showing
- D3.js with update button adds new data to old instead of replacing old data
- d3.js - Circle added to the mouse move is not visible
- d3 how to insert new SVG element before existing other element?
- bootstrap switch d3 on change event
- What is the proper way to use D3's projection.stream()?
- typescript import function from 3rd party declare module
- Cannot push values into an array
- Gauge chart with steps of colors
- JavaScript Parsing error
- d3 giveing error of unexpect token on import
- Trouble using DC.js (crossfilter and d3 convenience library) - bar chart not showing values
- d3.js ticks function giving more elements than needed
- getComputedTextLength throws an error in IE10
- Difference between dynamic rectangles and circles in d3?
- How to use the accessor function(d) to access the parent's value?
- D3.js: text attributes not being appended correctly?
- How do you create a treemap in D3 with custome colors
- D3 update data on the wrong bars
- D3: dataset update does not remove elements from DOM
- In D3 zoomable sunburst, how do I make the labeling dependent upon the zoom level?
- OpenSeaDragon with D3 svg-overlay draggable elements
- D3js line chart error "attribute d: Expected number, "M49,NaNZ"."
- d3 chart show multiple label in one slice on mouse hover
- dc.js BarChart yAxis range plus padding
- Angular-nvD3 Pie Chart Not Displaying
- 10 points within unit circle - finding optimal distribution with D3 force layout?
- Adding FontAwesome icons to a D3 graph