nidi3 / graphviz-java

Use graphviz with pure java
Apache License 2.0
937 stars 107 forks source link

Adding labels to edges using Mutable API #141

Closed lucaspcamargo closed 4 years ago

lucaspcamargo commented 4 years ago

Hello. I am trying to get my head around the API but can't figure out how to add an edge between two nodes, with a label for the edge. I would appreciate a nudge in the right direction. I must be missing something.

Edit: what I am doing currently (in Scala):

      val node = guru.nidi.graphviz.model.Factory.mutNode(src.toString)
      val dstNode = node.addLink(dst.toString)
      val link = node.linkTo(dstNode)
      mg.add( node )
nidi3 commented 4 years ago

You mean graph().with(node("A").link(to(node("B")).with(Label.of("Edge")))) ?

lucaspcamargo commented 4 years ago

That would be for Immutable graphs, no?

I tried to add a Label attribute to the Link returned by node.linkTo(dstnode), to no effect.

nidi3 commented 4 years ago

The problem with your code is you're creating a link with val link = node.linkTo(dstNode) but it's not added to the node. Adding node.addLink(link) should help. But mutGraph().add(mutNode("A").addLink(to(mutNode("B")).with(Label.of("Edge")))); is still nicer in my opinion.

lucaspcamargo commented 4 years ago

Oh, I see. The factory APIs makes sense to me now. And your solution is much more elegant. Thank you so much!