如何用Mermaid图表绘制表示多服务实例的堆叠卡片?
用Mermaid实现重叠卡片展示多可用区服务实例
当然可以实现,通过Mermaid的节点样式自定义和位置偏移设置,就能还原你需要的重叠卡片效果。下面是两个实用的示例:
基础实现示例
flowchart TD subgraph CloudRegion[AWS us-east-1 区域] direction LR ServiceInstance1[用户认证服务\n版本: v2.3.1\nCPU: 2核\n内存: 4GB] ServiceInstance2[可用区 us-east-1a] ServiceInstance3[可用区 us-east-1b] ServiceInstance4[可用区 us-east-1c] %% 自定义节点样式与位置,实现重叠 style ServiceInstance1 fill:#4285F4,stroke:#333,stroke-width:2px,width:220px,height:120px style ServiceInstance2 fill:#34A853,stroke:#333,stroke-width:2px,width:220px,height:120px,x:-20px,y:20px style ServiceInstance3 fill:#FBBC05,stroke:#333,stroke-width:2px,width:220px,height:120px,x:-40px,y:40px style ServiceInstance4 fill:#EA4335,stroke:#333,stroke-width:2px,width:220px,height:120px,x:-60px,y:60px end
这个示例中:
- 顶部的
ServiceInstance1完全展示,标注了服务的完整属性; - 下方的可用区实例通过
x/y参数偏移位置,实现重叠效果,仅露出顶部的可用区名称; - 不同可用区用不同填充色区分,提升辨识度。
优化维护版(用类统一样式)
如果需要添加更多可用区实例,用classDef统一管理样式会更高效:
flowchart TD classDef mainInstance fill:#4285F4,stroke:#333,stroke-width:2px,width:220px,height:120px classDef azInstance stroke:#333,stroke-width:2px,width:220px,height:120px subgraph CloudRegion[阿里云华东1 区域] direction LR ServiceInstance1[订单服务\n版本: v1.8.0\n实例数: 1\n存储: 20GB]:::mainInstance ServiceInstance2[可用区 cn-hangzhou-a]:::azInstance ServiceInstance3[可用区 cn-hangzhou-b]:::azInstance ServiceInstance4[可用区 cn-hangzhou-c]:::azInstance style ServiceInstance2 fill:#34A853,x:-20px,y:20px style ServiceInstance3 fill:#FBBC05,x:-40px,y:40px style ServiceInstance4 fill:#EA4335,x:-60px,y:60px end
可调整的参数
- 修改
x/y的偏移值:控制卡片的重叠程度,数值越大,露出的部分越多; - 调整
fill颜色:自定义不同可用区的标识色; - 修改节点内的文本:替换成你实际的服务属性、可用区名称;
- 调整
width/height:适配卡片的大小需求。
内容的提问来源于stack exchange,提问作者mrwnt10
相关产品推荐
相关产品推荐

