Yog.Multi.Mermaid (YogEx v0.98.0)

Copy Markdown View Source

Mermaid.js format export for visualizing multigraphs.

This module exports multigraphs (graphs with support for parallel edges) to Mermaid syntax. It mirrors the capabilities of Yog.Render.Mermaid but adapts the edge configurations to account for multiple relationships.

Per-Edge Customization

Because multiple edges can exist between the same nodes, callback functions pass the unique edge_id:

options = %{
  Yog.Multi.Mermaid.default_options() |
  edge_attributes: fn from, to, edge_id, weight ->
    if edge_id == 5 do
      [{:stroke, "#d32f2f"}, {:stroke_width, "3px"}]
    else
      []
    end
  end
}

Subgraphs

Group nodes visually using subgraphs:

options = %{
  Yog.Multi.Mermaid.default_options() |
  subgraphs: [
    %{
      name: "Group A",
      label: "Cluster A",
      node_ids: [1, 2, 3]
    }
  ]
}

Summary

Types

CSS length unit for styling.

Direction for graph layout

Node shape options for Mermaid diagrams.

Options for customizing multigraph Mermaid rendering

A subgraph for grouping nodes visually in the diagram.

Functions

Creates Mermaid options that color nodes by community assignment.

Creates Mermaid options that color the source and sink sides of a min-cut.

Creates default Mermaid options with multigraph labeling capabilities.

Creates default Mermaid options with custom label formatters for both nodes and edges.

Creates default Mermaid options with a custom edge formatter.

Creates default Mermaid options with all edge labels hidden.

Creates Mermaid options that highlight matched edges from a matching result.

Creates Mermaid options that highlight an MST result.

Converts a shortest path result to highlighted Mermaid options.

Returns a pre-configured theme as Mermaid options for multigraphs.

Converts a multigraph (Yog.Multi.Graph) to Mermaid syntax.

Types

css_length()

@type css_length() ::
  {:px, integer()}
  | {:em, float()}
  | {:rem, float()}
  | {:percent, float()}
  | {:custom, String.t()}

CSS length unit for styling.

direction()

@type direction() :: :td | :lr | :bt | :rl

Direction for graph layout

node_shape()

@type node_shape() ::
  :rounded_rect
  | :stadium
  | :subroutine
  | :cylinder
  | :circle
  | :asymmetric
  | :rhombus
  | :hexagon
  | :parallelogram
  | :parallelogram_alt
  | :trapezoid
  | :trapezoid_alt

Node shape options for Mermaid diagrams.

See Yog.Render.Mermaid for the full list of shapes.

options()

@type options() :: %{
  node_label: (Yog.Model.node_id(), any() -> String.t()),
  edge_label: (Yog.Multi.Graph.edge_id(), any() -> String.t()),
  highlighted_nodes: [Yog.Model.node_id()] | nil,
  highlighted_edges:
    [Yog.Multi.Graph.edge_id() | {Yog.Model.node_id(), Yog.Model.node_id()}]
    | nil,
  node_attributes: (Yog.Model.node_id(), any() -> [{atom(), String.t()}]),
  edge_attributes: (Yog.Model.node_id(),
                    Yog.Model.node_id(),
                    Yog.Multi.Graph.edge_id(),
                    any() ->
                      [{atom(), String.t()}]),
  subgraphs: [subgraph()] | nil,
  direction: direction(),
  node_shape: node_shape() | (Yog.Model.node_id(), any() -> node_shape()),
  default_fill: String.t() | nil,
  default_stroke: String.t() | nil,
  default_stroke_width: css_length() | nil,
  default_font_color: String.t() | nil,
  default_link_stroke: String.t() | nil,
  highlight_fill: String.t(),
  highlight_stroke: String.t(),
  highlight_stroke_width: css_length(),
  link_thickness: css_length(),
  highlight_link_stroke: String.t(),
  highlight_link_stroke_width: css_length()
}

Options for customizing multigraph Mermaid rendering

subgraph()

@type subgraph() :: %{
  name: String.t(),
  label: String.t() | nil,
  node_ids: [Yog.Model.node_id()] | nil
}

A subgraph for grouping nodes visually in the diagram.

Functions

community_to_options(map, base_options \\ default_options())

@spec community_to_options(Yog.Community.Result.t(), options()) :: options()

Creates Mermaid options that color nodes by community assignment.

Each community gets a distinct color from a generated palette.

cut_to_options(map, base_options \\ default_options())

@spec cut_to_options(Yog.Flow.MinCutResult.t(), options()) :: options()

Creates Mermaid options that color the source and sink sides of a min-cut.

default_options()

@spec default_options() :: options()

Creates default Mermaid options with multigraph labeling capabilities.

default_options_with(list)

@spec default_options_with(
  node_label: (Yog.Model.node_id(), any() -> String.t()),
  edge_label: (any() -> String.t())
) :: options()

Creates default Mermaid options with custom label formatters for both nodes and edges.

default_options_with_edge_formatter(edge_formatter)

@spec default_options_with_edge_formatter((any() -> String.t())) :: options()

Creates default Mermaid options with a custom edge formatter.

Use this when your multigraph has non-String edge data.

default_options_without_labels()

@spec default_options_without_labels() :: options()

Creates default Mermaid options with all edge labels hidden.

Use this when you want a clean diagram without edge annotations.

matching_to_options(matching, base_options \\ default_options())

@spec matching_to_options(
  %{required(Yog.Model.node_id()) => Yog.Model.node_id()},
  options()
) ::
  options()

Creates Mermaid options that highlight matched edges from a matching result.

mst_to_options(result, base_options \\ default_options())

@spec mst_to_options(Yog.MST.Result.t(), options()) :: options()

Creates Mermaid options that highlight an MST result.

path_to_options(path, base_options \\ default_options())

@spec path_to_options(map(), options()) :: options()

Converts a shortest path result to highlighted Mermaid options.

Creates a copy of the base options with the path's nodes and edges set to be highlighted.

theme(atom)

@spec theme(atom()) :: options()

Returns a pre-configured theme as Mermaid options for multigraphs.

Available themes:

  • :default — Yellow highlight, orange stroke (same as default_options/0)
  • :dark — Dark-friendly colors with neon accent colors
  • :minimal — Clean wireframe look with no fills and thin lines
  • :presentation — Large strokes and bold colors for slides and demos

to_mermaid(graph, options \\ default_options())

@spec to_mermaid(Yog.Multi.Graph.t(), options()) :: String.t()

Converts a multigraph (Yog.Multi.Graph) to Mermaid syntax.