This document describes the subgraph (cluster) support implemented in Task 18 for Ferrite's Mermaid flowchart rendering.
Subgraphs allow grouping related nodes together in a flowchart with a visual container and optional title. This implementation supports:
- Basic subgraphs:
subgraph titlesyntax - Named subgraphs:
subgraph id [title]syntax - Nested subgraphs: Subgraphs within subgraphs
- Direction overrides:
direction TB/LR/etcwithin subgraphs (parsed, not yet applied)
flowchart TD
subgraph MyGroup
A[Node A]
B[Node B]
end
flowchart TD
subgraph sg1 [Service Layer]
API[API Gateway]
Auth[Auth Service]
end
flowchart TD
subgraph Outer
subgraph Inner
A[Node A]
end
B[Node B]
end
/// A subgraph (cluster) in a flowchart.
pub struct FlowSubgraph {
/// Unique identifier for the subgraph
pub id: String,
/// Display title (may differ from id)
pub title: Option<String>,
/// IDs of nodes directly contained in this subgraph
pub node_ids: Vec<String>,
/// IDs of nested subgraphs
pub child_subgraph_ids: Vec<String>,
/// Optional direction override for this subgraph
pub direction: Option<FlowDirection>,
}The parser handles subgraph blocks using a stack-based approach:
- When
subgraphkeyword is encountered, push a newSubgraphBuilderonto the stack - Associate nodes/edges with the current (top of stack) subgraph
- When
endkeyword is encountered, pop the builder and create the subgraph - Register nested subgraphs as children of their parent
Subgraph bounding boxes are computed after node positions are determined:
- For each subgraph (processing children before parents):
- Calculate min/max bounds of all member nodes
- Include bounds of nested subgraphs
- Add padding around content
- Add space for title at top
pub struct SubgraphLayout {
/// Bounding box position (top-left corner)
pub pos: Pos2,
/// Bounding box size
pub size: Vec2,
/// Title to display (if any)
pub title: Option<String>,
}Subgraphs are rendered as the first layer (behind edges and nodes):
- Draw semi-transparent rounded rectangle as background
- Draw title text in top-left corner if present
- Parent subgraphs drawn before children to layer correctly
Layout configuration for subgraphs:
| Parameter | Default | Description |
|---|---|---|
subgraph_padding |
15.0 | Padding around subgraph content |
subgraph_title_height |
24.0 | Height reserved for title |
Theme colors for subgraphs:
| Color | Dark Theme | Light Theme |
|---|---|---|
subgraph_fill |
rgba(60, 70, 90, 40) | rgba(200, 210, 230, 60) |
subgraph_stroke |
rgb(80, 100, 130) | rgb(150, 170, 200) |
subgraph_title |
rgb(160, 175, 195) | rgb(80, 95, 120) |
- Direction overrides: Parsed but not yet applied to layout
- Edge to subgraph: Edges connecting directly to subgraphs (not individual nodes) are not yet supported
- Subgraph styling: Custom per-subgraph styling is not supported
- Apply direction overrides within subgraphs
- Support edges to/from subgraph ids
- Custom styling per subgraph
- Collapse/expand subgraphs interactively
- Implementation:
src/markdown/mermaid.rs - Key types:
FlowSubgraph,SubgraphLayout,SubgraphBuilder - Key functions:
parse_subgraph_header(),compute_subgraph_layouts(),draw_subgraph()