6

I am using D3 to draw a Directed Acyclic Graph and I would like to be able to highlight the path to a selected node by changing the color of the edges (and arrowheads) to that path. I was easily able to change the edge color, but I cannot figure out how to change the color of the corresponding arrowheads. The most applicable source I have found suggests that this was no possible, but it is also from about two years ago, so I am looking to see if things have changed. The code I am using to create the links, arrowheads, and update link color is below:

graph.append("svg:defs").selectAll("marker")
     .data(["end"])
   .enter().append("svg:marker")    
     .attr("id", String)
     .attr("viewBox", "0 -5 10 10")
     .attr("refX", 20)
     .attr("refY", 0)
     .attr("markerWidth", 6)
     .attr("markerHeight", 6)
     .attr("orient", "auto")
     .style("fill", "gray")
   .append("svg:path")
     .attr("d", "M0,-5L10,0L0,5");

. . .

var link = graph.append("svg:g").selectAll("line")
     .data(json.links)
   .enter().append("svg:line")
     .style("stroke", "gray")
     .attr("class", "link")
     .attr("marker-end", "url(#end)");

. . .

function highlightPath(node) {
  d3.selectAll("line")
    .style("stroke", function(d) { 
      if (d.target.name == node) {
        highlightPath(d.source.name);
        return "lightcoral";
      } else {
        return "gray";
      }
    });
}

Any advice would be great. Thank you.

Valentine
  • 103
  • 1
  • 4
  • possible duplicate of [Changing an SVG marker's color - CSS?](http://stackoverflow.com/questions/16664584/changing-an-svg-markers-color-css) – Josh Aug 02 '13 at 17:59
  • Thanks, that didn't come up in my searching so it's a good source. But I guess it is still not quite available for use at this time. – Valentine Aug 02 '13 at 19:08

2 Answers2

9

Create a function and give a return value to it and val should be dynamic:

function marker (color) {
    var val;
    graph.append("svg:defs").selectAll("marker")
         .data([val])
         .enter().append("svg:marker")    
         .attr("id", String)
         .attr("viewBox", "0 -5 10 10")
         .attr("refX", 20)
         .attr("refY", 0)
         .attr("markerWidth", 6)
         .attr("markerHeight", 6)
         .attr("orient", "auto")
         .style("fill", color)
         .append("svg:path")
         .attr("d", "M0,-5L10,0L0,5");
    return "url(#" +val+ ")";
}

var link = graph.append("svg:g").selectAll("line")
                .data(json.links)
                .enter().append("svg:line")
                .style("stroke", "gray")
                .attr("class", "link")
                .attr("marker-end", marker); //"url(#end)"
Brian S
  • 4,878
  • 4
  • 27
  • 46
swapnil
  • 99
  • 1
  • 2
2

Not the best solution, but you could draw arrowHeads like this

var arrowHeads = svg.selectAll("polygon.arrowHeads") //arrow heads are triangles
    .data(links)
  .enter().append("polygon")
    .attr("id", function(d, i) {return "arrowHead0" + i})
    .attr("points", function(d, i) {
        //function here that outputs the three points of a triangle
    })
;

an arrow and its head have the same index (because they have the same attached data).

So, you could use d3.select("#arrowHead0" + arrowIndex).attr("fill", "black");

justinjhendrick
  • 426
  • 3
  • 4