This library is a Kotlin DSL for the Graphviz Dot language. Its syntax is fairly similar to standard Dot, with some
tweaks to get it to work as a Kotlin DSL. The library can be useful to generate graphs.
digraph {
// Normal Kotlin statements
val p = (0..9).partition { it % 2 == 0 }
val evens = p.first
val odds = p.second
// Building a Dot graph
for (i in evens) {
+"$i" + { shape = DotNodeShape.SQUARE }
odds.filter { abs(it - i) == 1 }.forEach { "$it" - "$i" }
}
for (i in odds) {
+"$i" + { shape = DotNodeShape.CIRCLE }
evens.filter { abs(it - i) == 1 }.forEach { "$it" - "$i" }
}
}
Puzzle
graph {
"a" - "b"
"a" - "c"
"a" - "d"
"b" - "a"
"b" - "d"
"c" - "a"
"c" - "d"
}
This library has all attributes listed in the Graphviz docs with corresponding types. Some attributes allow for multiple
types such as Boolean and String so you will find attributes with the suffixes "B" and "S" for Boolean and String
respectively.
Add nodes in graphs using the + unary operator like the Kotlin HTML DSL.
graph {
// basic nodes
+"a"
+"b"
// add node attributes using + binary infix op
+"c" + {
color = "blue"
}
}
Edges
You can add edges between nodes using the binary infix -. You can add edges between nodes and subgraphs. The graph
type -- graph vs digraph -- determines the edge type. In Dotlin, you just use - not "--" and "->".
graph {
// Nodes with attributes
+"a" + { color = "blue" }
+"b" + { shape = DotNodeShape.TRAPEZIUM }
// Basic Edge
"a" - "b"
}
digraph {
// Edge with attributes
"a" - "z" + {
color = "green"
style = DotEdgeStyle.DASHED
}
}
Subgraphs
Add subgraphs with +subgraph { ... } and the code within the subgraph block is the same as for a graph/digraph block.
graph {
// Add subgraph to graph
+subgraph {
...
}
// Subgraph as variable
val sg = subgraph { ... }
// Add it to the graph
+sg
// Subgraph as edge target
"A" - subgraph { ... }
// Subgraph as edge source
subgraph { ... } - "B"
}
How Kotlin DSLs Work
TODO
About
This library is a Kotlin DSL for the Graphviz Dot language. Its syntax is fairly similar to standard Dot, with some tweaks to get it to work as a Kotlin DSL. The library can be useful to generate graphs.
Puzzle
Example
Dot
Dotlin
Usage
Basics
Attributes
This library has all attributes listed in the Graphviz docs with corresponding types. Some attributes allow for multiple types such as Boolean and String so you will find attributes with the suffixes "B" and "S" for Boolean and String respectively.
See https://graphviz.org/doc/info/attrs.html
Graphs
Nodes
Add nodes in graphs using the
+
unary operator like the Kotlin HTML DSL.Edges
You can add edges between nodes using the binary infix
-
. You can add edges between nodes and subgraphs. The graph type -- graph vs digraph -- determines the edge type. In Dotlin, you just use-
not "--" and "->".Subgraphs
Add subgraphs with
+subgraph { ... }
and the code within the subgraph block is the same as for a graph/digraph block.Reference
The library is based on the BNF given in the official docs. See https://graphviz.org/doc/info/lang.html