### 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 guru.nidi graphviz-java 0.18.1 ``` -------------------------------- ### Maven Dependency for Graphviz-Kotlin DSL Source: https://github.com/nidi3/graphviz-java/blob/master/README.md Provides the Maven dependency snippet required to include the experimental Graphviz-Kotlin DSL in a project. This allows using the Kotlin-specific syntax for graph definition. ```XML guru.nidi graphviz-kotlin 0.18.1 ``` -------------------------------- ### Create Complex Graphviz Graphs in Java Source: https://github.com/nidi3/graphviz-java/blob/master/README.md Illustrates the programmatic creation of a directed graph with multiple nodes, custom labels (HTML, Markdown, multi-line), shapes, styles, and complex link structures including nested graphs and custom link attributes like weight, style, and color. ```java Node main = node("main").with(Label.html("main
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 ch.qos.logback logback-classic 1.2.3 ``` -------------------------------- ### Customize Graphviz Output with Pre and Post Processors (Java) Source: https://github.com/nidi3/graphviz-java/blob/master/README.md This snippet demonstrates how to register pre-processors and post-processors to customize the Graphviz workflow. Pre-processors modify the DOT file before it's fed into the engine, while post-processors alter the engine's output (e.g., image or SVG). ```java Graph graph = graph().with(node("bad word").link("good word")); Graphviz g = Graphviz.fromGraph(graph) .preProcessor((source, options, processOptions) -> source.replace("bad word", "unicorn")) .postProcessor((result, options, processOptions) -> result.mapString(svg -> SvgElementFinder.use(svg, finder -> { finder.findNode("unicorn").setAttribute("class", "pink"); }))); g.render(Format.PNG).toFile(new File("example/ex9.png")); ``` -------------------------------- ### Creating Mutable Graphs with Graphviz-Java Source: https://github.com/nidi3/graphviz-java/blob/master/README.md Illustrates the mutable API for graph creation, which allows direct modification of graph elements. It shows how to create a directed graph, add a styled node, and link it to another node, then render it to a PNG file. This API uses setters and 'add' methods for modifications. ```Java MutableGraph g = mutGraph("example1").setDirected(true).add( mutNode("a").add(Color.RED).addLink(mutNode("b"))); Graphviz.fromGraph(g).width(200).render(Format.PNG).toFile(new File("example/ex1m.png")); ``` -------------------------------- ### Creating Immutable Graphs with Graphviz-Java Source: https://github.com/nidi3/graphviz-java/blob/master/README.md Demonstrates how to construct a directed graph using the immutable API, setting global attributes, node styles, and linking nodes with custom edge attributes. The graph is then rendered to a PNG file. Note that 'mutating' methods return new objects, leaving the original object unchanged. ```Java Graph g = graph("example1").directed() .graphAttr().with(Rank.dir(LEFT_TO_RIGHT)) .nodeAttr().with(Font.name("arial")) .linkAttr().with("class", "link-class") .with( node("a").with(Color.RED).link(node("b")), node("b").link( to(node("c")).with(attr("weight", 5), Style.DASHED) ) ); Graphviz.fromGraph(g).height(100).render(Format.PNG).toFile(new File("example/ex1.png")); ``` -------------------------------- ### Parse and Manipulate Dot Files with Graphviz Java Source: https://github.com/nidi3/graphviz-java/blob/master/README.md Demonstrates how to parse an existing Dot file into a MutableGraph object, render it to a PNG image, and then programmatically modify graph attributes, node attributes, and node styles before re-rendering the updated graph. ```java try (InputStream dot = getClass().getResourceAsStream("/color.dot")) { MutableGraph g = new Parser().read(dot); Graphviz.fromGraph(g).width(700).render(Format.PNG).toFile(new File("example/ex4-1.png")); g.graphAttrs() .add(Color.WHITE.gradient(Color.rgb("888888")).background().angle(90)) .nodeAttrs().add(Color.WHITE.fill()) .nodes().forEach(node -> node.add( Color.named(node.name().toString()), Style.lineWidth(4), Style.FILLED)); Graphviz.fromGraph(g).width(700).render(Format.PNG).toFile(new File("example/ex4-2.png")); } ``` -------------------------------- ### Add GraalVM JavaScript Engine Maven Dependency Source: https://github.com/nidi3/graphviz-java/blob/master/README.md This XML snippet shows how to include the GraalVM JavaScript engine as an additional dependency in a Maven `pom.xml` file. This is needed if Nashorn is too slow or on JDK version 15 or newer where Nashorn has been removed. ```XML org.graalvm.js js 20.0.0 ``` -------------------------------- ### Add Batik Rasterizer Dependency to Maven (XML) Source: https://github.com/nidi3/graphviz-java/blob/master/README.md To enable rasterization with Batik, this Maven dependency must be added to your project's pom.xml. This provides the necessary library for the Batik rasterizer. ```xml org.apache.xmlgraphics batik-rasterizer 1.13 ``` -------------------------------- ### Add Maven Dependency for Graphviz Rough Module Source: https://github.com/nidi3/graphviz-java/blob/master/README.md This XML snippet shows how to include the `graphviz-rough` module as a dependency in a Maven project. This module is required to use the `Roughifyer` processor for sketchy graph rendering. ```xml guru.nidi graphviz-rough 0.18.1 ``` -------------------------------- ### Add SLF4J Log4j2 Maven Dependencies Source: https://github.com/nidi3/graphviz-java/blob/master/README.md This XML snippet shows how to include the Log4j2 logging implementation and its SLF4J binding in a Maven `pom.xml` file. `graphviz-java` uses the SLF4J facade for logging, requiring a concrete implementation. ```XML org.apache.logging.log4j log4j-core 2.13.0 org.apache.logging.log4j log4j-slf4j-impl 2.13.0 ``` -------------------------------- ### Use Graphviz Taglet in Javadoc Comments (Java) Source: https://github.com/nidi3/graphviz-java/blob/master/README.md This Java snippet demonstrates the syntax for embedding Graphviz DOT language directly into Javadoc comments using the configured GraphvizTaglet. The diagram will be rendered as part of the generated Javadoc. ```java /** * Support graphviz inside javadoc. *

* {@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 maven-javadoc-plugin 3.1.0 guru.nidi.graphviz.taglet.GraphvizTaglet guru.nidi graphviz-taglet 0.18.1 ``` -------------------------------- ### Include Images in Graphviz using HTML Labels (Java) Source: https://github.com/nidi3/graphviz-java/blob/master/README.md This snippet demonstrates how to embed images within Graphviz graphs by using the tag inside an HTML label. Note that this method only works when using the command-line engine due to limitations in viz.js. ```java Graphviz.useEngine(new GraphvizCmdLineEngine()); Graphviz g = Graphviz.fromGraph(graph() .with(node(Label.html("
")))); g.basedir(new File("example")).render(Format.PNG).toFile(new File("example/ex7.png")); ``` -------------------------------- ### Include Images in Graphviz using Node Image Attribute (Java) Source: https://github.com/nidi3/graphviz-java/blob/master/README.md This snippet shows an alternative method for including images in Graphviz graphs by setting the 'image' attribute of a node. This approach is more versatile as it works with all available Graphviz engines. ```java Graphviz g = Graphviz.fromGraph(graph() .with(node(" ").with(Size.std().margin(.8, .7), Image.of("graphviz.png")))); g.basedir(new File("example")).render(Format.PNG).toFile(new File("example/ex8.png")); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.