adding nodes with data and spacing

master
Matt Huntington 8 years ago
parent 970b221593
commit b415ac03b0

@ -0,0 +1,4 @@
circle {
fill:red;
r: 5;
}

@ -4,3 +4,27 @@ var HEIGHT = 200;
d3.select("svg") d3.select("svg")
.attr("width", WIDTH) .attr("width", WIDTH)
.attr("height", HEIGHT); .attr("height", HEIGHT);
var nodesData = [
{"name": "Travis", "sex": "M"},
{"name": "Rake", "sex": "M"},
{"name": "Diana", "sex": "F"},
{"name": "Rachel", "sex": "F"},
{"name": "Shawn", "sex": "M"},
{"name": "Emerald", "sex": "F"}
];
var nodes = d3.select("g")
.selectAll("circle")
.data(nodesData)
.enter()
.append("circle");
d3.forceSimulation()
.nodes(nodesData)
.force("charge_force", d3.forceManyBody())
.force("center_force", d3.forceCenter(WIDTH / 2, HEIGHT / 2)) //position centering force at center x,y coords
.on("tick", function(){
nodes.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});

@ -3,10 +3,13 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title></title> <title></title>
<link rel="stylesheet" href="app.css">
<script src="https://d3js.org/d3.v4.min.js"></script> <script src="https://d3js.org/d3.v4.min.js"></script>
</head> </head>
<body> <body>
<svg></svg> <svg>
<g></g>
</svg>
<script src="app.js" charset="utf-8"></script> <script src="app.js" charset="utf-8"></script>
</body> </body>
</html> </html>

Loading…
Cancel
Save