Cluster shape and fill color in dot

Learn cluster shape and fill color in dot with practical examples, diagrams, and best practices. Covers graphviz development techniques with visual explanations.

Mastering Cluster Styling in Graphviz Dot: Shape and Fill Color

Hero image for Cluster shape and fill color in dot

Learn how to effectively use cluster subgraphs in Graphviz Dot to group related nodes and apply distinct shapes and fill colors for enhanced visual organization and clarity.

Graphviz Dot is a powerful graph description language for visualizing structured data. When dealing with complex graphs, it often becomes necessary to group related nodes to improve readability and convey hierarchical or conceptual relationships. This is where cluster subgraphs come into play. Beyond just grouping, Dot allows you to style these clusters with various shapes and fill colors, making your diagrams more intuitive and aesthetically pleasing. This article will guide you through the process of defining and styling clusters, including practical examples and a visual demonstration using Mermaid.

Understanding Cluster Subgraphs in Dot

A cluster in Graphviz is a special type of subgraph that is treated as a single entity for layout purposes and can have its own border, background, and label. The key to defining a cluster is to prefix the subgraph name with cluster_. If you omit this prefix, Graphviz will treat it as a regular subgraph, which groups nodes but does not draw a distinct boundary around them. Clusters are particularly useful for representing modules, departments, or logical groupings within a larger system.

digraph G {
    subgraph cluster_0 {
        label = "Cluster A";
        node [style=filled,color=white];
        a1 -> a2;
        a2 -> a3;
    }

    subgraph cluster_1 {
        label = "Cluster B";
        node [style=filled,color=lightgrey];
        b1 -> b2;
        b2 -> b3;
    }

    a3 -> b1; 
}

Basic Graphviz Dot code demonstrating two simple clusters.

Applying Shapes and Fill Colors to Clusters

While nodes within a cluster can have their own shapes and colors, the cluster itself can also be styled. The shape attribute, however, applies to individual nodes, not directly to the cluster boundary. For clusters, you primarily control the appearance of their bounding box. The style attribute is crucial here, allowing you to set properties like filled for background color, rounded for rounded corners, or dashed for a dashed border. The fillcolor attribute (or color when style=filled) then defines the background color of the cluster. You can also set the peripheries attribute to draw multiple concentric boundaries.

digraph G {
    rankdir=LR;

    subgraph cluster_Process {
        label = "Data Processing";
        style = "filled,rounded";
        fillcolor = "#e0f7fa"; // Light Cyan
        color = "#00796b"; // Dark Teal border
        fontcolor = "#004d40";

        P1 [label="Input Data"];
        P2 [label="Transform"];
        P3 [label="Validate"];

        P1 -> P2 -> P3;
    }

    subgraph cluster_Output {
        label = "Output & Storage";
        style = "filled,dashed";
        fillcolor = "#fff3e0"; // Light Orange
        color = "#e65100"; // Dark Orange border
        fontcolor = "#bf360c";

        O1 [label="Store Result"];
        O2 [label="Generate Report"];

        O1 -> O2;
    }

    P3 -> O1 [label="Processed Data"];
}

Advanced cluster styling with fill colors, rounded corners, and dashed borders.

graph TD
    subgraph "Data Processing"
        direction LR
        style Data_Processing fill:#e0f7fa,stroke:#00796b,stroke-width:2px,stroke-dasharray: 0
        P1["Input Data"] --> P2["Transform"]
        P2 --> P3["Validate"]
    end

    subgraph "Output & Storage"
        direction LR
        style Output_Storage fill:#fff3e0,stroke:#e65100,stroke-width:2px,stroke-dasharray: 5 5
        O1["Store Result"] --> O2["Generate Report"]
    end

    P3 -- "Processed Data" --> O1

    linkStyle 0 stroke:#333,stroke-width:1px,fill:none;
    linkStyle 1 stroke:#333,stroke-width:1px,fill:none;
    linkStyle 2 stroke:#333,stroke-width:1px,fill:none;
    linkStyle 3 stroke:#333,stroke-width:1px,fill:none;
    linkStyle 4 stroke:#333,stroke-width:1px,fill:none;

Mermaid diagram illustrating styled clusters, mirroring the Dot example.

Best Practices for Cluster Styling

When styling clusters, consider the following best practices to ensure your diagrams remain clear and effective:

  1. Consistency: Use a consistent color palette and styling for similar types of clusters across your diagrams.
  2. Contrast: Ensure sufficient contrast between the cluster's fill color, its border color, and the text label within it.
  3. Meaningful Colors: Assign colors that have a logical meaning (e.g., green for 'active', red for 'critical', blue for 'data').
  4. Avoid Over-Styling: Too many different styles can make the diagram cluttered and harder to read. Use styling judiciously to highlight important distinctions.
  5. Labels: Always provide clear and concise labels for your clusters to explain their purpose.