score:2
Accepted answer
You aren't giving the node
any attributes. I take it you want a circle and not a rect :
var node = svgcontainer.selectAll('.node')
.data(nodes)
.enter().append('circle')
.attr('class', 'node')
.attr('x', function(d){ console.log(d); return d.x})
.attr('y', function(d){ return d.y})
.attr('r', 10)
.attr('transform', function(d){
return 'translate(' + d.x + ', ' + d.y + ')'
})
Notice the translate at the bottom. If you are using the force layout in D3, the tick function should take care of this, but it looks like you don't have one, so you have to insert it after you create the nodes.
If you didn't want circles and wanted rectangles then this should do :
var nodeRect = svgcontainer.selectAll('.nodeRect')
.data(nodes)
.enter().append('rect')
.attr('class', 'nodeRect')
.attr('x', function(d){ console.log(d); return d.x})
.attr('y', function(d){ return d.y})
.attr('width', 100)
.attr('height', 50)
.attr('transform', function(d){
return 'translate(' + d.x + ', ' + d.y + ')'
})
Working fiddle with both : https://jsfiddle.net/reko91/n13kqvw9/
var width = 500,
height = 500;
var nodes = [
{ x: width / 3, y: height / 2 }
//{ x: 2 * width / 3, y: height / 1 },
//{ x: 3 * width / 3, y: height / 2 },
//{ x: 4 * width / 3, y: height / 2 }
];
var force = d3.layout.force()
.size([width, height])
.nodes(nodes)
var svgcontainer = d3.select("body")
.append("svg")
.attr("width", 1000)
.attr("height", 900);
var rectdata = [{ "x": 50, "y": 70, "width": 600, "height": 150,"rx":80,"ry":80,"fill":"skyblue"},
{ "x": 50, "y": 260, "width": 200, "height": 400, "rx": 80, "ry": 90, "fill": "palegreen" },
{ "x": 440, "y": 260, "width": 200, "height": 400, "rx": 80, "ry": 90, "fill": "orange" },
{ "x": 50, "y": 700, "width": 600, "height": 150, "rx": 80, "ry": 80, "fill": "brown" }];
var svgrect = svgcontainer.selectAll("rect").data(rectdata).enter().append("rect");
var node = svgcontainer.selectAll('.node')
.data(nodes)
.enter().append('circle')
.attr('class', 'node')
.attr('x', function(d){ console.log(d); return d.x})
.attr('y', function(d){ return d.y})
.attr('r', 10)
.attr('transform', function(d){
return 'translate(' + d.x + ', ' + d.y + ')'
})
var nodeRect = svgcontainer.selectAll('.nodeRect')
.data(nodes)
.enter().append('rect')
.attr('class', 'nodeRect')
.attr('x', function(d){ console.log(d); return d.x})
.attr('y', function(d){ return d.y})
.attr('width', 100)
.attr('height', 50)
.attr('transform', function(d){
return 'translate(' + d.x + ', ' + d.y + ')'
})
force.on('end', function () {
svgrect.attr("x", function (d, i) { return d.x; })
.attr("y", function (d, i) { return d.y; })
.attr("rx", function (d, i) { return d.rx; })
.attr("ry", function (d, i) { return d.ry; })
.attr("width", function (d, i) { return d.width; })
.attr("height", function (d, i) { return d.height; })
.attr("fill", function (d, i) { return d.fill; });
});
force.start();
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- Adding nodes inside svg rect using d3 force layout
- Simple graph of nodes and links without using force layout
- Adding nodes dynamically to D3 Force Layout in version 4
- reading nodes and edges from two distinct csv files using Force Layout
- Force layout inside force layout: How to drag inner nodes
- How to use D3 force layout with existing SVG elements as nodes
- Adding new nodes to a clustered force layout
- SVG nodes in D3 force layout moves on node scale
- Adding arrows and text to nodes in d3js Force Layout
- D3 force layout in Canvas: adding & updating nodes (& links)
- D3 Collapsible Force Layout adding nodes to the layout dynamically
- Create nodes based on content BBox and get width of source node when adding links in force layout
- add text inside a rect in force layout graph d3.js
- Can a Child node have multiple Parent nodes in Collapsible Force Layout using D3
- add text inside a rect in force layout graph d3.js REACTJS
- adding nodes to tree force layout - almost working
- Adding new nodes to Force-directed layout
- D3 Tree Layout Separation Between Nodes using NodeSize
- d3.js: "Cannot read property 'weight' of undefined" when manually defining both nodes and links for force layout
- D3 force layout - linking nodes by name instead of index
- D3 force layout visualization dead slow when using a large dataset?
- D3: Using force layout for word clouds
- 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.js adding click action to a force layout circle?
- Update existing nodes in a d3 Force Layout
- Space out nodes evenly around root node in D3 force layout
- How can I show a graph using d3 force layout with initial positions set and no movement at all?
- d3js create a force layout with fixed nodes
More Query from same tag
- SVG Text disappears in IE until I click on it
- Rendering MathML on svg using d3.js
- How to make curved lines to straight lines for Hierarchy Chart using d3.js
- Ace editor in SVG Foreign Object
- D3 Linear Scaling - Color code mismatch
- Venue/Indoor Map using D3.js and Geojson
- Plotting on D3 projection
- Colorbrewer scale only returning lightest values on D3 map
- Map many possible input values to discrete color domains
- How to modify axis labels in d3 for a stacked bar chart when the axis labels are mapped as part of the scale's domain
- IO Error in d3py example code -- no such file or directory: 'static/d3.js'
- Importing multiple CSV files in Javascript
- How to control d3.format with SI-prefix?
- using d3.js as an external in webpack
- Placing labels at the center of nodes in d3.js
- D3.js implements first stage of transition but not 2nd
- Insert element as sibling using D3js
- Angular: async pipe and drawing svg
- d3.js - how to delete a group element
- Remove hour ticks
- d3, html in svg, animating opacity takes div out of position, why?
- HTM/CSS interfering with D3.js
- Creating a zoom function in D3
- D3 adding donut chart within a tooltip
- Object is of type 'unknown' error while setting data in react d3
- Data structure for a family tree with multiple partners and siblings?
- Problems displaying table inside a shape in d3.js
- D3 creating a table
- D3.js V4 : How to create X axis with time and %H:%M:%S format
- how to compute the size of polygon in svg by d3.js