yog/render
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
Options for customizing Mermaid diagram rendering.
pub type MermaidOptions {
MermaidOptions(
node_label: fn(Int, String) -> String,
edge_label: fn(String) -> String,
highlighted_nodes: option.Option(List(Int)),
highlighted_edges: option.Option(List(#(Int, Int))),
)
}
Constructors
-
MermaidOptions( node_label: fn(Int, String) -> String, edge_label: fn(String) -> String, highlighted_nodes: option.Option(List(Int)), highlighted_edges: option.Option(List(#(Int, Int))), )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 (e.g., a path)
- highlighted_edges
-
Optional list of edges to highlight as (from, to) pairs
Values
pub fn default_dot_options() -> DotOptions
Creates default DOT options with simple labeling.
pub fn default_json_options() -> JsonOptions
Creates default JSON options.
Nodes are { "id": 1, "label": "Node A" }.
Edges are { "source": 1, "target": 2, "weight": "5" }.
pub fn default_options() -> MermaidOptions
Creates default Mermaid options with simple labeling.
Uses node ID as label and edge weight as-is.
pub fn path_to_dot_options(
path: pathfinding.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 = render.path_to_dot_options(p, default_dot_options())
let diagram = render.to_dot(graph, options)
io.println(diagram)
}
None -> io.println("No path found")
}
pub fn path_to_options(
path: pathfinding.Path(e),
base_options: MermaidOptions,
) -> MermaidOptions
Converts a shortest path result to highlighted Mermaid options.
Useful for visualizing pathfinding algorithm results.
Example
let path = pathfinding.shortest_path(
in: graph,
from: 1,
to: 5,
with_zero: "0",
with_add: string_add,
with_compare: string_compare,
)
case path {
Some(p) -> {
let options = render.path_to_options(p, default_options())
let diagram = render.to_mermaid(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 = render.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"];
}
Converts a graph to DOT (Graphviz) syntax.
pub fn to_json(
graph: model.Graph(String, String),
options: JsonOptions,
) -> String
Converts a graph to a JSON string compatible with many visualization libraries (e.g., D3.js).
The graph’s node data and edge data must be convertible to strings.
Time Complexity: O(V + E)
Example
import gleam/io
import gleam/json
import yog/model
pub fn main() {
let graph =
model.new(model.Directed)
|> model.add_node(1, "Alice")
|> model.add_node(2, "Bob")
|> model.add_edge(from: 1, to: 2, with: "follows")
let json_string = render.to_json(graph, render.default_json_options())
io.println(json_string)
}
This outputs:
{
"nodes": [
{"id": 1, "label": "Alice"},
{"id": 2, "label": "Bob"}
],
"edges": [
{"source": 1, "target": 2, "weight": "follows"}
]
}
pub fn to_mermaid(
graph: model.Graph(String, String),
options: MermaidOptions,
) -> String
Converts a graph to Mermaid diagram syntax.
The graph’s node data and edge data must be convertible to strings. Use the options to customize labels and highlight specific paths.
Time Complexity: O(V + E)
Example
let graph =
model.new(Directed)
|> model.add_node(1, "Start")
|> model.add_node(2, "Process")
|> model.add_node(3, "End")
|> model.add_edge(from: 1, to: 2, with: "5")
|> model.add_edge(from: 2, to: 3, with: "3")
// Basic rendering
let diagram = render.to_mermaid(graph, default_options())
// Highlight a path
let options = MermaidOptions(
..default_options(),
highlighted_nodes: Some([1, 2, 3]),
highlighted_edges: Some([#(1, 2), #(2, 3)]),
)
let highlighted = render.to_mermaid(graph, options)
The output can be embedded in markdown:
```mermaid
graph TD
1["Start"]
2["Process"]
3["End"]
1 -->|5| 2
2 -->|3| 3
```