jamisonjiang / graph-support

Java re-implementation of tiny graphviz
Apache License 2.0
24 stars 7 forks source link

How to create clusters? #3

Closed PavelTurk closed 6 months ago

PavelTurk commented 6 months ago

I need to create clusters. For example, something like this:

Screenshot from 2023-12-28 22-30-46

Can I do such a picture using graph-support and apache batik? If yes, could anyone give an example how to do it?

jamisonjiang commented 6 months ago

@PavelTurk First the latest version no need apache batik if output PNG/JPEG, then if you familiar with graphviz and use dot script and it the same logic for graph-support

digraph G {
    edge[dir=back]
    C->BB
    subgraph cluster_A {
        label="Cluster A"
        A->C
        B->C
    }
    subgraph cluster_B {
        label="Cluster B"
        AA->CC
        BB->CC
    }
}

and java code example

Node A = Node.builder().label("A").build();
Node B = Node.builder().label("B").build();
Node C = Node.builder().label("C").build();
Node AA = Node.builder().label("AA").build();
Node BB = Node.builder().label("BB").build();
Node CC = Node.builder().label("CC").build();

Graphviz graphviz = Graphviz.digraph()
    .tempLine(Line.tempLine().dir(Dir.BACK).build())
    .addLine(C, BB)
    .cluster(
        Cluster.builder()
            .label("Cluster A")
            .addLine(A, C)
            .addLine(B, C)
            .build()
    )
    .cluster(
        Cluster.builder()
            .label("Cluster B")
            .addLine(AA, CC)
            .addLine(BB, CC)
            .build()
    )
    .build();

// Save to file system
graphviz.toFile(FileType.PNG).save("./", "test");

// Or handle the file resource by yourself
try (GraphResource graphResource = graphviz.toFile(FileType.PNG)) {
  byte[] bytes = graphResource.bytes();
  InputStream is = graphResource.inputStream();
}
PavelTurk commented 6 months ago

@jamisonjiang Thank you very much. We plan to create a project with your library. I will provide feedback.