nidi3 / graphviz-java

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

Cannot create link between nodes in parent graph #136

Closed thombergs closed 4 years ago

thombergs commented 4 years ago

Hi,

I want to do something like this (click for preview):

digraph {
    edge ["dir"="forward"]
    graph ["rankdir"="LR"]

    subgraph "cluster_internal components" {
        edge ["dir"="none"]
        graph ["label"="my app"]
        "B" ["shape"="rectangle"]
    }

    subgraph "cluster_external input components" {
        edge ["dir"="none"]
        graph ["label"="input","style"="dashed"]
        "A" ["shape"="rectangle"]
        "B" ["shape"="rectangle"]
    }

    "A" -> "B"
}

But I can't figure out how to add the link (A->B) to the parent graph. When I call node.addLink() the link always appears within one of the subgraphs, resulting in this:

digraph {
    edge ["dir"="forward"]
    graph ["rankdir"="LR"]

    subgraph "cluster_internal components" {
        edge ["dir"="none"]
        graph ["label"="my app"]
        "B" ["shape"="rectangle"]
    }

    subgraph "cluster_external input components" {
        edge ["dir"="none"]
        graph ["label"="input","style"="dashed"]
        "A" ["shape"="rectangle","label"="A to B"]
        "B" ["shape"="rectangle"]
        "A" -> "B"
    }
}

The link is within the second subgraph, causing the arrow not to show the arrowhead.

I tried adding the link directly to the parent graph using graph.addLink(), but this will always assume that the graph itself is the source of the link, which is not what I want.

Is there a way to add a link to the main graph to get the result in the first code snippet above?

thombergs commented 4 years ago

I have found a solution to add a link to the main graph by doing this:

mainGraph.add(node("A")
  .link("B"))

However, this way, I cannot add attributes (like a label) to the link, because link() returns a Node and not a Link.

How can I add a link between two nodes to the main graph and manipulate the attributes of the link?

nidi3 commented 4 years ago

Hi, sorry for the late answer. You can do mainGraph.add(node("A").link(to(node("B")).with(Style.DOTTED)