Skip to content

maximehamm/graphviz-java

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphviz-java

Build Status codecov License

Use graphviz with pure java.

Uses this javascript version of graphviz created using Emscripten.

The javascript code is executed either with J2V8 or, as a fallback with Java 8's Nashorn engine.

Usage

Logging

Graphviz-java uses the SLF4J facade to log. Users must therefore provide a logging implementation like LOGBack

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

or Log4j

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.8.2</version>
</dependency>

Simple example

The basic usage is as follows (assuming import static guru.nidi.graphviz.model.Factory.*):

Graph g = graph("example1").directed().with(node("a").link(node("b")));
Graphviz.fromGraph(g).width(200).render(Format.PNG).toFile(new File("example/ex1.png"));

Configuration

The size of the resulting image, the rendering engine and the output format can be configured:

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).rasterizer(Rasterizer.BATIK).render(Format.PNG).toFile(new File("example/ex5b.png"));
viz.width(200).rasterizer(Rasterizer.SALAMANDER).render(Format.PNG).toFile(new File("example/ex5s.png"));
String json = viz.engine(Engine.NEATO).render(Format.JSON).toString();
BufferedImage image = viz.render(Format.PNG).toImage();

Complex example

Node
        init = node("init"),
        execute = node("execute"),
        compare = node("compare").with(Shape.RECTANGLE, Style.FILLED, Color.hsv(.7, .3, 1.0)),
        mkString = node("mkString").with(Label.of("make a\nstring")),
        printf = node("printf");

Graph g = graph("example2").directed().with(
        node("main").with(Shape.RECTANGLE).link(
                to(node("parse").link(execute)).with("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"));

Example with records

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()
        .generalAttr().with(RankDir.LEFT_TO_RIGHT)
        .with(
                node0.link(
                        between(loc("f0"), node1.loc("v", SOUTH)),
                        between(loc("f1"), node2.loc(WEST)),
                        between(loc("f2"), node3.loc(WEST)),
                        between(loc("f3"), node4.loc(WEST)),
                        between(loc("f4"), node5.loc("v", NORTH))),
                node2.link(between(loc("p"), node6.loc(NORTH_WEST))),
                node4.link(between(loc("p"), node7.loc(SOUTH_WEST))));
Graphviz.fromGraph(g).width(900).render(Format.PNG).toFile(new File("example/ex3.png"));

Read and manipulate graphs

Dot files can be parsed and thus manipulated:

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
}

MutableGraph g = Parser.read(getClass().getResourceAsStream("/color.dot"));
Graphviz.fromGraph(g).width(700).render(Format.PNG).toFile(new File("example/ex4-1.png"));

g.generalAttrs()
        .add(Color.WHITE.gradient(Color.rgb("888888")).background().angle(90))
        .nodeAttrs().add(Color.WHITE.fill())
        .nodes().forEach(node ->
        node.add(
                Color.named(node.label().toString()),
                Style.lineWidth(4).and(Style.FILLED)));
Graphviz.fromGraph(g).width(700).render(Format.PNG).toFile(new File("example/ex4-2.png"));

About

Use graphviz with pure java

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%