graph-support
is a lightweight Java re-implementation of Graphviz for parsing, layout, and rendering graphs.
- Dual Support for Java API and DOT Script: Use the Java API or DOT script, depending on which method is more convenient for your use case.
- Lightweight Design:
graph-support
is designed to be lightweight, ensuring stability and efficiency. Unlike other graphing tools,graph-support
does not require Graphviz binaries, JavaScript runtime engines, or external renderers, making it ideal for embedding into applications. - Multi-Platform Support: Compatible with Mac, Linux, Windows, and Android platforms, ensuring seamless integration across different environments.
- Release
- Documentation
- Quick build:
- Latest stable OpenJDK 8
- Latest stable Apache Maven
- Run
mvn clean install
-
Download the latest CLI JAR.
-
Render by file
-
Prepare a DOT file
-
Run
java -jar graph-support-cli.jar example.dot -o example -Tpng
-
-
Render by script string
java -jar graph-support-cli.jar -s "digraph {a->b->c->d}" -o test -Tpng
-
Help
java -jar graph-support-cli.jar -h
Shows usage and options.
If you do not need DOT parsing, only the Core is required:
<dependency>
<groupId>org.graphper</groupId>
<artifactId>graph-support-core</artifactId>
<version>1.5.1</version>
</dependency>
public class Example {
public static void main(String[] args) {
// Node definition
Node nd_1 = Node.builder().label("Node 1").build();
Node nd_2 = Node.builder().label("Node 2").build();
Node nd_3_a = Node.builder().label("Above Right Node 3").build();
Node nd_3_l = Node.builder().label("Left of Node 3").build();
Node nd_3 = Node.builder().label("Node 3").build();
Node nd_3_r = Node.builder().label("Right of Node 3").build();
Node nd_4 = Node.builder().label("Node 4").build();
Graphviz graphviz = Graphviz.digraph()
// Node attribute template
.tempNode(Node.builder().shape(NodeShapeEnum.RECT).build())
// Edges in root graph
.addLine(nd_3_a, nd_3_r)
.addLine(nd_1, nd_2, nd_3, nd_4)
// Use Cluster to wrap the corresponding nodes and edges
.cluster(
Cluster.builder()
// Edge attribute template
.tempLine(Line.tempLine().color(Color.GREY).arrowHead(ArrowShape.NONE).build())
// Use Subgraph to keep certain nodes on the same rank
.subgraph(
Subgraph.builder()
.rank(Rank.SAME)
.addNode(nd_3_l, nd_3, nd_3_r)
.build()
)
.addLine(nd_3_l, nd_3, nd_3_r)
.build()
)
.build();
// Save PNG
try {
graphviz.toFile(FileType.PNG).save("./", "example");
System.out.println("Saved 'example.png'");
} catch (Exception e) {
e.printStackTrace();
}
}
}
If you want to parse a .dot
string or file, import the DOT module:
<dependency>
<groupId>org.graphper</groupId>
<artifactId>graph-support-dot</artifactId>
<version>1.5.1</version>
</dependency>
Then:
import org.graphper.dot.DotParser;
public class DotExample {
public static void main(String[] args) {
String dot = "digraph { a -> b }";
Graphviz graphviz = DotParser.parse(dot);
try {
graphviz.toFile(FileType.PNG).save("./", "example");
System.out.println("Saved 'example.png'");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Some properties can be used to debug how edges are routed:
If you want only the layout engine to compute node coordinates or line segments (without generating images), you can do:
Node a = Node.builder().label("a").build();
Node b = Node.builder().label("b").build();
Graphviz graphviz = Graphviz.digraph()
.addLine(a, b)
.build();
// Use the layout engine directly; skip rendering
DrawGraph drawGraph = Layout.DOT.getLayoutEngine().layout(graphviz);
for (NodeDrawProp node : drawGraph.nodes()) {
double x = node.getX();
double y = node.getY();
// Integrate these coordinates in your custom GUI or app
}
for (LineDrawProp line : drawGraph.lines()) {
// Each line can have multiple segments or Bezier curves
if (line.isBesselCurve()) {
// interpret sets of 4 control points
} else {
// a simple polyline
}
}
We welcome developers to contribute new features, bug fixes, or improvements! Some areas we’d love help with:
- More node/arrow shapes
- Additional styling options
- New line-routing or layout algorithms
Simply fork the repo, create a feature branch, and open a Pull Request. Thank you for your interest in graph-support!