score:2
Accepted answer
simple styling missing from your chart.
adding the following to your chart:
path.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
here's a snippet with the above changes:
var treedata = [
{
"name": "top level",
"children": [
{
"name": "level 2: a",
"children": [
{
"name": "son of a",
},
{
"name": "daughter of a",
}
]
},
{
"name": "level 2: b",
}
]
}
];
// ************** generate the tree diagram *****************
var margin = { top: 20, right: 120, bottom: 20, left: 120 },
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0,
duration = 750,
root;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function (d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treedata[0];
root.x0 = height / 2;
root.y0 = 0;
update(root);
d3.select(self.frameelement).style("height", "500px");
function update(source) {
// compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// normalize for fixed-depth.
nodes.foreach(function (d) { d.y = d.depth * 180; });
// update the nodes…
var node = svg.selectall("g.node")
.data(nodes, function (d) { return d.id || (d.id = ++i); });
// enter any new nodes at the parent's previous position.
var nodeenter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function (d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("click", click);
nodeenter.append("circle")
.attr("r", 1e-6)
.style("fill", function (d) { return d._children ? "#f1a33a" : "#f95c33"; });
nodeenter.append("text")
.attr("x", function (d) { return d.children || d._children ? -13 : 13; })
.attr("dy", ".35em")
.attr("text-anchor", function (d) { return d.children || d._children ? "end" : "start"; })
.text(function (d) { return d.name; })
.style("fill-opacity", 1e-6);
// transition nodes to their new position.
var nodeupdate = node.transition()
.duration(duration)
.attr("transform", function (d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeupdate.select("circle")
.attr("r", 10)
.style("fill", function (d) { return d._children ? "#f1a33a" : "#f95c33"; });
nodeupdate.select("text")
.style("fill-opacity", 1);
// transition exiting nodes to the parent's new position.
var nodeexit = node.exit().transition()
.duration(duration)
.attr("transform", function (d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeexit.select("circle")
.attr("r", 1e-6);
nodeexit.select("text")
.style("fill-opacity", 1e-6);
// update the links…
var link = svg.selectall("path.link")
.data(links, function (d) { return d.target.id; });
// enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function (d) {
var o = { x: source.x0, y: source.y0 };
return diagonal({ source: o, target: o });
});
// transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal)
.attr("d", function (d) {
/* calculating the top shift */
var source = { x: d.source.x, y: d.source.y };
var target = { x: d.target.x, y: d.target.y };
return diagonal({ source: source, target: target });
});
// transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function (d) {
var o = { x: source.x, y: source.y };
return diagonal({ source: o, target: o });
})
.remove();
// stash the old positions for transition.
nodes.foreach(function (d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
path.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
score:0
you have to set the fill attribute of the link.path to none and stroke width
link.path{
fill: none;
stroke: #000000;
stroke-width: 1;
}
the easier way is to use a library, here is an example with balkangraph
it is exactly what you want
score:2
var treedata = [
{
"name": "top level",
"children": [
{
"name": "level 2: a",
"children": [
{
"name": "son of a",
},
{
"name": "daughter of a",
}
]
},
{
"name": "level 2: b",
}
]
}
];
// ************** generate the tree diagram *****************
var margin = { top: 20, right: 120, bottom: 20, left: 120 },
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0,
duration = 750,
root;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function (d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treedata[0];
root.x0 = height / 2;
root.y0 = 0;
update(root);
d3.select(self.frameelement).style("height", "500px");
function update(source) {
// compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// normalize for fixed-depth.
nodes.foreach(function (d) { d.y = d.depth * 180; });
// update the nodes…
var node = svg.selectall("g.node")
.data(nodes, function (d) { return d.id || (d.id = ++i); });
// enter any new nodes at the parent's previous position.
var nodeenter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function (d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("click", click);
nodeenter.append("circle")
.attr("r", 1e-6)
.style("fill", function (d) { return d._children ? "#f1a33a" : "#f95c33"; });
nodeenter.append("text")
.attr("x", function (d) { return d.children || d._children ? -13 : 13; })
.attr("dy", ".35em")
.attr("text-anchor", function (d) { return d.children || d._children ? "end" : "start"; })
.text(function (d) { return d.name; })
.style("fill-opacity", 1e-6);
// transition nodes to their new position.
var nodeupdate = node.transition()
.duration(duration)
.attr("transform", function (d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeupdate.select("circle")
.attr("r", 10)
.style("fill", function (d) { return d._children ? "#f1a33a" : "#f95c33"; });
nodeupdate.select("text")
.style("fill-opacity", 1);
// transition exiting nodes to the parent's new position.
var nodeexit = node.exit().transition()
.duration(duration)
.attr("transform", function (d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeexit.select("circle")
.attr("r", 1e-6);
nodeexit.select("text")
.style("fill-opacity", 1e-6);
// update the links…
var link = svg.selectall("path.link")
.data(links, function (d) { return d.target.id; });
// enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function (d) {
var o = { x: source.x0, y: source.y0 };
return diagonal({ source: o, target: o });
});
// transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal)
.attr("d", function (d) {
/* calculating the top shift */
var source = { x: d.source.x, y: d.source.y };
var target = { x: d.target.x, y: d.target.y };
return diagonal({ source: source, target: target });
});
// transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function (d) {
var o = { x: source.x, y: source.y };
return diagonal({ source: o, target: o });
})
.remove();
// stash the old positions for transition.
nodes.foreach(function (d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
path.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
Source: stackoverflow.com
Related Query
- D3 Collapsible tree not working properly in angular
- d3.js - mouseover event not working properly on svg group
- Exit not working properly
- Webpack 2 Not Tree Shaking D3.js Properly
- Angular 4 + D3v4 : dragging circle not working correctly in conjunction with zooming
- Angular ng-click's inside a dynamically created d3 chart are not working
- LinkText is not working in tree layout d3js
- Slice and bounce effect not working properly in pie chart
- d3 - mouseover and mouseout event not working properly
- D3's scale not working properly
- d3.js Bar Chart transitions not working properly
- d3 js exit animation not working properly
- D3 bar chart not working properly with all negative and positive values
- viewbox with pixel value to make d3 chart resposive is not working properly
- d3.js circle plotting does not working properly while trying to load after first time
- nvd3 Interactive Guideline hover not working properly
- tree.nodeSize() not working in D3 tree graph to inc. the space between nodes
- D3 bisector function not working properly
- Mouse up event not working properly in SVG
- Pie chart interpolate not working properly
- D3 forceCollide not working properly after updating the underlying data
- d3.mouse not working properly on transformed SVG
- useInteractiveGuideline not working correctly in Angular nvd3
- Importing d3.js in angular 2 project not working
- Angular nvd3 multibarchart stacked not showing properly
- Internet Explorer Scaling not working properly
- d3.js hiding popups with opacity not working properly with pointer events
- Collapsible Radial Tidy Tree found in ObservableHQ is not collapsing after mouse click if I use d3 v6 instead of d3 v5
- Angular 2 D3 tree is not rendering correctly
- Angular NVD3 not working
More Query from same tag
- Plot density function with 2 or 3 colored areas?
- How to convert/transform in the DOM following dl dt structure to table on the fly using jQuery or d3js in a document
- d3 trying to use callbacks "i" as index
- D3 Angular confusion: fill color doesn't change
- Can I call indexOf within a D3 selection?
- Making a Sankey diagram in R for printing
- D3js: two .append methods on the same selection can't be in the same line
- in browser screenshots of nvd3 (or d3 with css)
- how to prevent scaling of text and border while scaling path
- Have gaps in a SVG path when there is no data
- D3 tooltip - keep mouse on tooltip with html elements
- dimple.js filter data by legend gives error
- Using WPF WebBrowser control to render web app built with D3.js framework
- D3 - Binding each data category to g
- d3 javascript change range of the y axis
- Horizontal Panning D3.js
- D3 Hexbin Mesh Alignment
- D3.js change time format and export data into csv
- How can I embed a graph from d3js with CSS
- hide X and Y axes on charts with n3-line-chart
- D3 reading promise data into a constructor
- d3 v4: Data join for bar chart
- d3 label are overlaping
- d3.js : Want to understand the concept of applying individual properties to elements via a single command
- Filter data with variable at time of parsing
- How to merge the best of mpld3 and square/crossfilter as a custom plugin?
- Add consistent margin to paths aligned to circles in D3.js
- D3 - tree nodes not built from external JSON file
- d3 js how to select from selection
- Getting .attr("display":none) to work on mouseout (D3.js)