### Graphviz Java Example with Record Nodes
Source: https://github.com/nidi3/graphviz-java/blob/master/README.md
Shows how to create nodes with record-based labels, allowing for structured ports within nodes. It demonstrates linking between specific ports of these record nodes and setting graph attributes like rank direction.
```java
import static guru.nidi.graphviz.attribute.Records.*;
import static guru.nidi.graphviz.model.Compass.*;
Node
node0 = node("node0").with(Records.of(rec("f0", ""), rec("f1", ""), rec("f2", ""), rec("f3", ""), rec("f4", ""))),
node1 = node("node1").with(Records.of(turn(rec("n4"), rec("v", "719"), rec("")))),
node2 = node("node2").with(Records.of(turn(rec("a1"), rec("805"), rec("p", "")))),
node3 = node("node3").with(Records.of(turn(rec("i9"), rec("718"), rec("")))),
node4 = node("node4").with(Records.of(turn(rec("e5"), rec("989"), rec("p", "")))),
node5 = node("node5").with(Records.of(turn(rec("t2"), rec("v", "959"), rec("")))),
node6 = node("node6").with(Records.of(turn(rec("o1"), rec("794"), rec("")))),
node7 = node("node7").with(Records.of(turn(rec("s7"), rec("659"), rec(""))));
Graph g = graph("example3").directed()
.graphAttr().with(Rank.dir(LEFT_TO_RIGHT))
.with(
node0.link(
between(port("f0"), node1.port("v", SOUTH)),
between(port("f1"), node2.port(WEST)),
between(port("f2"), node3.port(WEST)),
between(port("f3"), node4.port(WEST)),
between(port("f4"), node5.port("v", NORTH))),
node2.link(between(port("p"), node6.port(NORTH_WEST))),
node4.link(between(port("p"), node7.port(SOUTH_WEST))));
Graphviz.fromGraph(g).width(900).render(Format.PNG).toFile(new File("example/ex3.png"));
```
--------------------------------
### Dot File Example for Graphviz
Source: https://github.com/nidi3/graphviz-java/blob/master/README.md
Defines a simple graph in Dot language with nodes and edges, demonstrating basic graph structure and node ranking for visual layout.
```dot
graph {
{ rank=same; white}
{ rank=same; cyan; yellow; pink}
{ rank=same; red; green; blue}
{ rank=same; black}
white -- cyan -- blue
white -- yellow -- green
white -- pink -- red
cyan -- green -- black
yellow -- red -- black
pink -- blue -- black
}
```
--------------------------------
### Configure Graphviz Output Size, Format, and Engine (Java)
Source: https://github.com/nidi3/graphviz-java/blob/master/README.md
This example illustrates how to configure various aspects of the Graphviz output, including the resulting image size, the rendering engine, and the output format (e.g., SVG, PNG, DOT, JSON). It also demonstrates using different rasterizers like BATIK and SALAMANDER.
```java
Graphviz.useEngine(new GraphvizCmdLineEngine()); // Rasterizer.builtIn() works only with CmdLineEngine
Graph g = graph("example5").directed().with(node("abc").link(node("xyz")));
Graphviz viz = Graphviz.fromGraph(g);
viz.width(200).render(Format.SVG).toFile(new File("example/ex5.svg"));
viz.width(200).rasterize(Rasterizer.BATIK).toFile(new File("example/ex5b.png"));
viz.width(200).rasterize(Rasterizer.SALAMANDER).toFile(new File("example/ex5s.png"));
viz.width(200).rasterize(Rasterizer.builtIn("pdf")).toFile(new File("example/ex5p"));
String dot = viz.render(Format.DOT).toString();
String json = viz.engine(Engine.NEATO).render(Format.JSON).toString();
BufferedImage image = viz.render(Format.PNG).toImage();
```
--------------------------------
### Using Imperative API for Graph Definition in Graphviz-Java
Source: https://github.com/nidi3/graphviz-java/blob/master/README.md
Shows the imperative API style, built on the mutable version, where nodes and links are automatically added within a lambda function. This approach is closer to the DOT file syntax. The example creates a directed graph with a styled node and a link, then renders it.
```Java
MutableGraph g = mutGraph("example1").setDirected(true).use((gr, ctx) -> {
mutNode("b");
nodeAttrs().add(Color.RED);
mutNode("a").addLink(mutNode("b"));
});
Graphviz.fromGraph(g).width(200).render(Format.PNG).toFile(new File("example/ex1i.png"));
```
```Dot
digraph example1 {
b
node[color=red]
a -> b
}
```
--------------------------------
### graphviz-java API Basic Usage Overview
Source: https://github.com/nidi3/graphviz-java/blob/master/README.md
This section introduces the fundamental structure of the `graphviz-java` API, noting its separation into mutable and immutable components, and highlights the common practice of statically importing `guru.nidi.graphviz.model.Factory` for convenience.
```APIDOC
The API is separated into a mutable and immutable part.
The basic usage is as follows (assuming `import static guru.nidi.graphviz.model.Factory.*`).
```
--------------------------------
### Defining Graphs with Graphviz-Kotlin DSL
Source: https://github.com/nidi3/graphviz-java/blob/master/README.md
Demonstrates the experimental Kotlin DSL for defining graphs. It showcases how to set global attributes, define nodes and edges using operators, and chain links, then renders the graph to a PNG file. Requires importing `guru.nidi.graphviz.*`.
```Kotlin
graph(directed = true) {
edge["color" eq "red", Arrow.TEE]
node[Color.GREEN]
graph[Rank.dir(LEFT_TO_RIGHT)]
"a" - "b" - "c"
("c"[Color.RED] - "d"[Color.BLUE])[Arrow.VEE]
"d" / NORTH - "e" / SOUTH
}.toGraphviz().render(PNG).toFile(File("example/ex1.png"))
```
--------------------------------
### Apply Roughifyer Processor to Graphviz Graph in Java
Source: https://github.com/nidi3/graphviz-java/blob/master/README.md
This Java code demonstrates how to construct a complex graph using the `graphviz-java` library and then apply the `Roughifyer` processor to achieve a sketchy visual style. It configures various `Roughifyer` properties such as bowing, curve step count, roughness, fill style, and font, before rendering the graph to a PNG file.
```java
final Graph g = graph("ex1").directed().with(
graph().cluster()
.nodeAttr().with(Style.FILLED, Color.WHITE)
.graphAttr().with(Style.FILLED, Color.LIGHTGREY, Label.of("process #1"))
.with(node("a0").link(node("a1").link(node("a2")))),
graph("x").cluster()
.nodeAttr().with(Style.FILLED)
.graphAttr().with(Color.BLUE, Label.of("process #2"))
.with(node("b0").link(node("b1").link(node("b2")))),
node("start").with(Shape.M_DIAMOND).link("a0", "b0"),
node("a0").with(Style.FILLED, Color.RED.gradient(Color.BLUE)).link("b1"),
node("b1").link("a2"),
node("a2").link("end"),
node("b2").link("end"),
node("end").with(Shape.M_SQUARE)
);
Graphviz.fromGraph(g)
.processor(new Roughifyer()
.bowing(2)
.curveStepCount(6)
.roughness(1)
.fillStyle(FillStyle.hachure().width(2).gap(5).angle(0))
.font("*serif", "Comic Sans MS"))
.render(Format.PNG)
.toFile(new File("example/ex1-rough.png"));
```
--------------------------------
### Add graphviz-java Maven Dependency
Source: https://github.com/nidi3/graphviz-java/blob/master/README.md
This XML snippet shows how to include the `graphviz-java` library as a dependency in a Maven `pom.xml` file. It automatically includes J2V8 for the current platform, but Gradle users must manually add J2V8.
```XML
start"), Color.rgb("1020d0").font()),
init = node(Label.markdown("**_init_**")),
execute = node("execute"),
compare = node("compare").with(Shape.RECTANGLE, Style.FILLED, Color.hsv(.7, .3, 1.0)),
mkString = node("mkString").with(Label.lines(LEFT, "make", "a", "multi-line")),
printf = node("printf");
Graph g = graph("example2").directed().with(
main.link(
to(node("parse").link(execute)).with(LinkAttr.weight(8)),
to(init).with(Style.DOTTED),
node("cleanup"),
to(printf).with(Style.BOLD, Label.of("100 times"), Color.RED)),
execute.link(
graph().with(mkString, printf),
to(compare).with(Color.RED)),
init.link(mkString));
Graphviz.fromGraph(g).width(900).render(Format.PNG).toFile(new File("example/ex2.png"));
```
--------------------------------
### Add SLF4J Logback Maven Dependency
Source: https://github.com/nidi3/graphviz-java/blob/master/README.md
This XML snippet shows how to include the Logback logging implementation for SLF4J in a Maven `pom.xml` file. `graphviz-java` uses the SLF4J facade for logging, requiring a concrete implementation.
```XML
* {@graphviz * graph test { a -- b } * } *
* So easy. */ public class GraphvizTaglet implements Taglet {} ``` -------------------------------- ### Configure Maven Javadoc Plugin for Graphviz Taglet (XML) Source: https://github.com/nidi3/graphviz-java/blob/master/README.md This Maven plugin configuration for pom.xml enables the GraphvizTaglet, allowing Graphviz diagrams to be embedded directly within Javadoc comments. For JDK 9+, use 'graphviz-taglet9' instead of 'graphviz-taglet'. ```xml![]() |