I'm just starting with d3 myself, but I think you need to use link.style("stroke",#xxxxxx).
How do I change the color of path in D3.js?
-
21-07-2023 - |
سؤال
I am creating some paths ina tree-graph in D3 and I'd like to change the color of the lines (paths), but I can't make my code work:
// Enter any new links at the parent's previous position.
link.enter().insert("svg:path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
})
.transition()
.duration(duration)
.attr("d", diagonal);
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// 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();
I tried using the following to change and I've look-up up a few other solution, but I can't really understand what I need to select in my update selection to change it.
link.selectAll("path")
.attr("stroke", "#000000");
Thanks for the help in advance.
المحلول
نصائح أخرى
An svg can change the color by using fill attribute (same is complex for path attribute even though its children of svg)
svg
.data([newValue])
.transition()
.duration(1500)
.attr('fill', function(d) { return color(d); })
Working code for your reference:
https://jsfiddle.net/pjrc0yy3/1/
لا تنتمي إلى StackOverflow