yog/render/dot
DOT (Graphviz) format export for visualizing graphs.
This module exports graphs to the DOT language,
which is the native format for Graphviz - a powerful open-source
graph visualization tool. The exported files can be rendered to PNG, SVG, PDF, and other
formats using the dot, neato, circo, or other Graphviz layout engines.
Quick Start
import yog/render/dot
// Export with default styling
let dot_string = dot.to_string(my_graph)
// Write to file and render with Graphviz CLI
// $ dot -Tpng output.dot -o graph.png
Customization
Use DotOptions to customize:
- Node labels and shapes
- Edge labels and styles
- Highlight specific nodes or paths
- Graph direction (LR, TB, etc.)
Rendering Options
| Engine | Best For |
|---|---|
dot | Hierarchical layouts (DAGs, trees) |
neato | Spring-based layouts (undirected) |
circo | Circular layouts |
fdp | Force-directed layouts |
sfdp | Large graphs |
References
Types
Arrow head/tail style
pub type ArrowStyle {
Normal
ArrowDot
ArrowDiamond
ODiamond
ArrowBox
Crow
Vee
Inv
Tee
ArrowNone
CustomArrow(String)
}
Constructors
-
NormalStandard arrow
-
ArrowDotDot circle
-
ArrowDiamondFilled diamond
-
ODiamondEmpty diamond
-
ArrowBoxArrowBox
-
CrowCrow’s foot (database notation)
-
VeeV-shaped
-
InvInverted V
-
TeeTee (perpendicular line)
-
ArrowNoneNo arrow
-
CustomArrow(String)Custom arrow (e.g., “ediamond”, “odot”)
Options for customizing DOT (Graphviz) diagram rendering.
pub type DotOptions {
DotOptions(
node_label: fn(Int, String) -> String,
edge_label: fn(String) -> String,
highlighted_nodes: option.Option(List(Int)),
highlighted_edges: option.Option(List(#(Int, Int))),
graph_name: String,
layout: option.Option(Layout),
rankdir: option.Option(RankDir),
bgcolor: option.Option(String),
splines: option.Option(Splines),
overlap: option.Option(Overlap),
nodesep: option.Option(Float),
ranksep: option.Option(Float),
node_shape: NodeShape,
node_color: String,
node_style: Style,
node_fontname: String,
node_fontsize: Int,
node_fontcolor: String,
edge_color: String,
edge_style: Style,
edge_fontname: String,
edge_fontsize: Int,
edge_penwidth: Float,
arrowhead: option.Option(ArrowStyle),
arrowtail: option.Option(ArrowStyle),
highlight_color: String,
highlight_penwidth: Float,
)
}
Constructors
-
DotOptions( node_label: fn(Int, String) -> String, edge_label: fn(String) -> String, highlighted_nodes: option.Option(List(Int)), highlighted_edges: option.Option(List(#(Int, Int))), graph_name: String, layout: option.Option(Layout), rankdir: option.Option(RankDir), bgcolor: option.Option(String), splines: option.Option(Splines), overlap: option.Option(Overlap), nodesep: option.Option(Float), ranksep: option.Option(Float), node_shape: NodeShape, node_color: String, node_style: Style, node_fontname: String, node_fontsize: Int, node_fontcolor: String, edge_color: String, edge_style: Style, edge_fontname: String, edge_fontsize: Int, edge_penwidth: Float, arrowhead: option.Option(ArrowStyle), arrowtail: option.Option(ArrowStyle), highlight_color: String, highlight_penwidth: Float, )Arguments
- node_label
-
Function to convert node ID and data to a display label
- edge_label
-
Function to convert edge weight to a display label
- highlighted_nodes
-
Optional list of node IDs to highlight
- highlighted_edges
-
Optional list of edges to highlight as (from, to) pairs
- graph_name
-
Graph name (default: “G”)
- layout
-
Layout engine (default: None = auto-detect)
- rankdir
-
Graph direction (default: Some(TopToBottom))
- bgcolor
-
Background color (CSS color, default: None)
- splines
-
Edge routing (default: None = Graphviz default)
- overlap
-
Overlap handling (default: None)
- nodesep
-
Minimum space between nodes in inches (default: 0.25)
- ranksep
-
Minimum space between ranks in inches (default: 0.5)
- node_shape
-
Node shape (default: Ellipse)
- node_color
-
Default node fill color (CSS color)
- node_style
-
Node style (default: Filled)
- node_fontname
-
Node font name (default: “Helvetica”)
- node_fontsize
-
Node font size in points (default: 12)
- node_fontcolor
-
Node font color (CSS color, default: “black”)
- edge_color
-
Default edge color (CSS color, default: “black”)
- edge_style
-
Edge style (default: Solid)
- edge_fontname
-
Edge font name (default: “Helvetica”)
- edge_fontsize
-
Edge font size in points (default: 10)
- edge_penwidth
-
Edge line thickness (default: 1.0)
- arrowhead
-
Arrow head style (default: None = Graphviz default)
- arrowtail
-
Arrow tail style (default: None)
- highlight_color
-
Highlight color for nodes/edges (CSS color, default: “red”)
- highlight_penwidth
-
Highlight pen width (default: 2.0)
Graphviz layout engine
pub type Layout {
Dot
Neato
Circo
Fdp
Sfdp
Twopi
Osage
CustomLayout(String)
}
Constructors
-
DotHierarchical layouts (DAGs, trees) - default for most use cases
-
NeatoSpring-based layouts (undirected graphs)
-
CircoCircular layouts
-
FdpForce-directed placement
-
SfdpScalable force-directed (for large graphs)
-
TwopiRadial layouts
-
OsageClustered layouts (array-based)
-
CustomLayout(String)Custom layout engine
Node shapes
pub type NodeShape {
Box
Circle
Ellipse
Diamond
Hexagon
Pentagon
Octagon
Triangle
Rectangle
Square
Rect
InvTriangle
House
InvHouse
Parallelogram
Trapezoid
CustomShape(String)
}
Constructors
-
Box -
Circle -
Ellipse -
Diamond -
Hexagon -
Pentagon -
Octagon -
Triangle -
Rectangle -
Square -
RectRounded rectangle
-
InvTriangleInverted triangle
-
HouseHouse shape (pentagon with flat top)
-
InvHouseInverted house
-
ParallelogramParallelogram
-
TrapezoidTrapezoid
-
CustomShape(String)Custom shape (for advanced Graphviz shapes)
Overlap handling
pub type Overlap {
OverlapTrue
OverlapFalse
Scale
ScaleXY
Prism
CustomOverlap(String)
}
Constructors
-
OverlapTrueAllow overlaps (faster)
-
OverlapFalseRemove all overlaps (slower)
-
ScaleScale graph to remove overlaps
-
ScaleXYScale x and y independently
-
PrismPrism algorithm (Voronoi-based)
-
CustomOverlap(String)Custom overlap mode
Graph direction (rank direction)
pub type RankDir {
TopToBottom
LeftToRight
BottomToTop
RightToLeft
}
Constructors
-
TopToBottomTop to Bottom (vertical, downward)
-
LeftToRightLeft to Right (horizontal)
-
BottomToTopBottom to Top (vertical, upward)
-
RightToLeftRight to Left (horizontal, reversed)
Edge routing style
pub type Splines {
Line
Polyline
Curved
Ortho
Spline
SplinesNone
}
Constructors
-
LineStraight lines
-
PolylinePolyline (bent lines)
-
CurvedCurved edges
-
OrthoOrthogonal (right angles only)
-
SplineBezier splines (smooth curves) - Graphviz default
-
SplinesNoneNo edges
Values
pub fn default_dot_options() -> DotOptions
Creates default DOT options with simple labeling and sensible styling.
Default configuration:
- Layout: Auto-detected by Graphviz
- Direction: Top-to-bottom
- Node shape: Ellipse
- Colors: Light blue nodes, black edges
- Font: Helvetica 12pt
pub fn path_to_dot_options(
path: utils.Path(e),
base_options: DotOptions,
) -> DotOptions
Converts a shortest path result to highlighted DOT options.
pub fn to_dot(
graph: model.Graph(String, String),
options: DotOptions,
) -> String
Converts a graph to DOT (Graphviz) syntax.
The graph’s node data and edge data must be convertible to strings. Use the options to customize labels and highlighting.
Time Complexity: O(V + E)
Example
let graph =
model.new(Directed)
|> model.add_node(1, "Start")
|> model.add_node(2, "Process")
|> model.add_edge(from: 1, to: 2, with: "5")
let diagram = dot.to_dot(graph, default_dot_options())
// io.println(diagram)
This output can be processed by Graphviz tools (e.g., dot -Tpng -o graph.png):
digraph G {
node [shape=ellipse];
1 [label="Start"];
2 [label="Process"];
1 -> 2 [label="5"];
}