Creating a complex and advanced graph in D2 (Directed Drawing) requires leveraging all the styling options available, including:
- Shapes
- Colors
- Styling (bold, italic, underlined)
- Icons
- Edges with different styles (dashed, dotted, etc.)
- Edge labels
- Direction control
- Customization of layout (orthogonal, polyline, etc.)
- Clusters/namespaces
- Tooltips
- Opacity
- Border styles
Below is an example of a highly styled, complex D2 diagram that demonstrates most of these features.
🌐 Advanced Graph Example in D2 Lang
# Define custom shapes and icons
user: {
shape: person
icon: https://icons.terrastruct.com/identity%20and%20access/user.svg
style.fill: #FFDDC1
tooltip: "End User"
}
auth_service: {
shape: service
icon: https://icons.terrastruct.com/security/shield-check.svg
style.fill: #C1E1FF
style.bold: true
}
db: {
shape: sql_table
icon: https://icons.terrastruct.com/data/database.svg
style.fill: #D4EDD4
style.italic: true
}
cache: {
shape: cylinder
icon: https://icons.terrastruct.com/storage/cache.svg
style.fill: #FFF3CD
style.underline: true
}
gateway: {
shape: hexagon
style.stroke: #551A8B
style.stroke_width: 3
style.fill: #FFE4E1
}
rate_limiter: {
shape: rectangle
style.fill: #FEE291
style.opacity: 0.7
}
analytics: {
shape: report
style.fill: #E6E6FA
}
monitoring: {
shape: dashboard
style.fill: #E0FFFF
}
# Cluster for Authentication Flow
auth_flow: {
direction: right
label: "Authentication Flow"
user -> auth_service: "Authenticates via\nJWT Token"
auth_service -> db: "Validates credentials"
auth_service.tooltip: "Performs token validation"
}
# Cluster for API Gateway Layer
api_layer: {
label: "API Gateway Layer"
gateway -> rate_limiter
rate_limiter -> auth_service
gateway.style.stroke-dasharray: 5
}
# Data flow from Gateway to Backend
gateway -> analytics: "Sends metadata"
analytics.style.stroke: green
# Caching layer interaction
auth_service -> cache: "Caches session"
cache -> auth_service: "Retrieves session"
# Monitoring system integration
monitoring -> auth_service: "Health check"
monitoring -> db: "Database metrics"
monitoring.shape: ellipse
monitoring.style.stroke: red
monitoring.style.stroke_width: 2
# Edge styling variations
analytics -> monitoring: "Pushes data" {
style.stroke-dasharray: 4
style.stroke: orange
label_position: center
}
# Complex edge path
user -> gateway: "Initial request" {
style.stroke: blue
style.stroke-linecap: round
arrowhead: crow
}
# Custom layout and directions
layout: dagre
direction: LR
🧩 Features Used in This Diagram
| Feature | Description |
|---|
shape | Various shapes like person, sql_table, cylinder, dashboard, ellipse, etc. |
icon | Custom icons added using URLs |
style.* | Fill color, bold, italic, underline, opacity, stroke width and color |
tooltip | Adds hover tooltips on nodes |
cluster / namespace | Groups nodes into logical sections (auth_flow, api_layer) |
edge styling | Dashed edges, colored arrows, custom line caps |
arrowhead | Different arrow types like crow, vee, etc. |
label_position | Control where edge labels appear |
layout | Uses dagre layout engine |
direction | Layout direction: LR (left to right) |
📈 How to View This Diagram
- Go to https://play.d2lang.com
- Paste the code above
- Click "Render"
You’ll see a fully rendered, highly stylized architecture diagram that showcases D2's full visual capabilities.
Would you like me to generate a version tailored for a specific domain like microservices, network architecture, or CI/CD pipelines?