yog/io/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/io/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
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))),
node_shape: String,
highlight_color: String,
)
}
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))), node_shape: String, highlight_color: String, )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
- node_shape
-
Node shape (e.g., “circle”, “box”, “ellipse”)
- highlight_color
-
Highlight color for nodes/edges
Values
pub fn default_dot_options() -> DotOptions
Creates default DOT options with simple labeling.
pub fn path_to_dot_options(
path: utils.Path(e),
base_options: DotOptions,
) -> DotOptions
Converts a shortest path result to highlighted DOT options.
Example
let path = pathfinding.shortest_path(
in: graph,
from: 1,
to: 5,
with_zero: "0",
with_add: string_add, // Assume these exist or map to int/float
with_compare: string_compare,
)
case path {
Some(p) -> {
let options = dot.path_to_dot_options(p, default_dot_options())
let diagram = dot.to_dot(graph, options)
io.println(diagram)
}
None -> io.println("No path found")
}
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"];
}