Changing the link color based on the attribute from the node

Hi

We have the attribute in the Node as “isNew”. I would like to change the color of the link based on both the connecting nodes. Eg: if the fromNode or the toNode has the data as “isNew” the color should be green if not it should be black. I have added the binding of Stroke to the toNode. How i can change the color for the drawing link based on the attribute in the Node.

diagram.linkTemplate =
(go.Link... (go.Shape, // the link shape
{ strokeWidth: 1.5 },
new go.Binding(“stroke”, “toNode”, function(n) {

          return n.data.linkColor; 
        }).ofObject() 
   ),
  
   $(go.Shape,  // the arrowhead
    {  toArrow: "standard", stroke: null },
         new go.Binding("fill", "toNode", function(n) { return n.data.linkColor; })
         .ofObject(),    
    )

);

Bindings aren’t supposed to help with dependencies that extend beyond the Part itself. In this case that means a data binding on a connected Node rather than on the Link data itself.

Does this do what you want?

function updateLinkColor(link) {
  var color = "black";
  if (link.fromNode && link.fromNode.data.isNew) color = "green";
  if (color === "black" && link.toNode && link.toNode.data.isNew) color = "green";
  link.path.stroke = color;
}
. . .
$(go.Link,
  {
    fromPortChanged: updateLinkColor,
    toPortChanged: updateLinkColor
  },
  . . .)

Hi Walter,

Thanks that worked. But the arrow head is not inheriting the link color. Can you please help me to change the arrow head as well.

https://gojs.net/latest/learn/graphObject.html