I'm testinf d3.js
and i'm trying to add links between a root node (the center one in the JsFiddle) and child nodes. How can i achieve that simply ?
Here is the code that i have so far: http://jsfiddle.net/fLqekg12/2/
var container = d3.select("svg#svg");
var data = [2, 1, 1, 1, 1, 1, 1];
var dataTree = {
id: "root",
size: 12,
children: data.map(function (d) {
return {
size: 10,
parent: "root"
};
})
};
var maxRadius = 50,
padding = 40;
var radiusScale = d3.scale.sqrt()
.domain([0, 50 /* d3.max(data) */ ])
.range([0, 50]); // maxRadius
var roughCircumference = d3.sum(data.map(radiusScale)) * 2 + padding * (data.length - 1),
radius = roughCircumference / (Math.PI * 2);
// make a radial tree layouts
var tree = d3.layout.tree()
.size([360, radius])
.separation(function (a, b) {
return radiusScale(a.size) + radiusScale(b.size);
});
// create a holder group for all the graph nodes
var svgGroup = container.append('g')
.attr('transform', 'translate(' + 80 + ',' + 90 + ')');
var nodes = tree.nodes(dataTree),
links = tree.links(nodes); // and then... ?
// declare the nodes (this creates placed groups)
var svgNodes = svgGroup.selectAll('.node')
.data(nodes) // cut out the root node, we don't need it : nodes.slice(1)
.enter().append('g')
.attr('class', 'node')
.attr('transform', function (d) {
return "rotate(" + (d.x - 90) + ") translate(" + d.y + ")";
});
// append a cirl to all nodes groups
svgNodes.append('circle').attr('r', function (d) {
return d.size;
});
EDIT
Progress was made with this code.
var diagonal = d3.svg.diagonal.radial()
.projection(function (d) {
return [d.y, d.x / 180 * Math.PI];
});
var svgLinks = svgGroup.selectAll('path')
.data(tree.links(nodes))
.enter().append('svg:path')
.attr('class', 'link')
.attr('d', diagonal)
.attr("fill", "none")
.attr("stroke", "gray");
Fiddle update: http://jsfiddle.net/fLqekg12/4/ The only thing i need now is straight lines instead of curved ones. Anyone ?