mcpField
D
DrawIO Diagram Creation

DrawIO Diagram Creation

github.com/thomasrohde文档处理250 次安装26 周前更新
代码仓库

关于 DrawIO Diagram Creation

用于生成结构化 XML 格式 `.drawio` 图表的 Skill,适合制作流程图、架构图和 UML 图。内容涵盖单元格结构、布局、连接线和样式,并提供多种图表示例。

主要功能

  1. 01`.drawio` XML 结构指南
  2. 02形状与连接线样式
  3. 03单元格 ID 与布局指南
  4. 04常见图表类型示例
  5. 05XML 验证实践

适用场景

  1. 01创建流程图
  2. 02记录系统架构
  3. 03绘制 UML 和数据图表

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/thomasrohde/marketplace --skill drawio-diagram-creation

安装后在 Claude Code 中输入/drawio-diagram-creation确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

examples/
sequence-diagram.drawio
archimate.drawio
org-chart.drawio
c4-diagram.drawio
architecture.drawio
flowchart.drawio
network-diagram.drawio
class-diagram.drawio
er-diagram.drawio
references/
shape-library.md
styling-guide.md
xml-structure.md

详细介绍

DrawIO 图表创建

创建结构、样式和布局规范、可用于生产环境的 .drawio 文件。DrawIO(diagrams.net)使用基于 mxGraph 库构建的 XML 格式。

核心概念

文件格式

DrawIO 文件是扩展名为 .drawio 的 XML 文档(也可以是 .drawio.xml 或 .drawio.svg)。该格式使用 mxGraph 的 XML 序列化,内容可以压缩,也可以不压缩。

基本结构

每个 DrawIO 文件都遵循以下结构:

<mxfile host="app.diagrams.net" modified="2024-01-01T00:00:00.000Z" agent="Claude" version="21.0.0" type="device">
  <diagram id="diagram-id" name="Page-1">
    <mxGraphModel dx="1000" dy="600" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

关键元素:

  • mxfile - 根容器,可以包含多个图表(页面)
  • diagram - 单个页面,具有唯一的 id 和名称
  • mxGraphModel - 画布设置(大小、网格、参考线)
  • root - 包含所有单元格(形状和边)
  • mxCell id="0" - 必需的根单元格
  • mxCell id="1" - 所有形状必需的默认父单元格
单元格类型

顶点(形状):

<mxCell id="2" value="Label" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
  <mxGeometry x="100" y="100" width="120" height="60" as="geometry" />
</mxCell>

边(连接器):

<mxCell id="3" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;" edge="1" parent="1" source="2" target="4">
  <mxGeometry relative="1" as="geometry" />
</mxCell>

创建图表

ID 管理

为每个单元格分配唯一 ID。使用从 "2" 开始的简单递增模式(0 和 1 为保留值):

<mxCell id="2" ... />  
<mxCell id="3" ... />  
<mxCell id="4" ... />  
定位

使用相对于画布原点(左上角)的 x/y 坐标。常用间距:

  • 形状之间的水平间距:150-200 像素
  • 形状之间的垂直间距:80-100 像素
  • 形状宽度:100-160 像素
  • 形状高度:40-80 像素
常用样式

矩形(默认):

rounded=0;whiteSpace=wrap;html=1;

圆角矩形:

rounded=1;whiteSpace=wrap;html=1;

椭圆/圆形:

ellipse;whiteSpace=wrap;html=1;

菱形(决策):

rhombus;whiteSpace=wrap;html=1;

圆柱体(数据库):

shape=cylinder3;whiteSpace=wrap;html=1;boundedLbl=1;backgroundOutline=1;size=15;

文档:

shape=document;whiteSpace=wrap;html=1;boundedLbl=1;

流程(带侧边线的矩形):

shape=process;whiteSpace=wrap;html=1;backgroundOutline=1;
连接器样式

正交(直角):

edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;

曲线:

edgeStyle=orthogonalEdgeStyle;curved=1;orthogonalLoop=1;jettySize=auto;html=1;

直线:

edgeStyle=none;orthogonalLoop=1;jettySize=auto;html=1;

带箭头:

endArrow=classic;html=1;

虚线:

dashed=1;
颜色和样式设置

添加到样式字符串中:

  • 填充颜色:fillColor=#dae8fc;
  • 描边颜色:strokeColor=#6c8ebf;
  • 字体颜色:fontColor=#333333;
  • 字体大小:fontSize=12;
  • 粗体:fontStyle=1;
  • 描边宽度:strokeWidth=2;

常用配色方案:

  • 蓝色:fillColor=#dae8fc;strokeColor=#6c8ebf;
  • 绿色:fillColor=#d5e8d4;strokeColor=#82b366;
  • 橙色:fillColor=#ffe6cc;strokeColor=#d79b00;
  • 红色:fillColor=#f8cecc;strokeColor=#b85450;
  • 紫色:fillColor=#e1d5e7;strokeColor=#9673a6;
  • 灰色:fillColor=#f5f5f5;strokeColor=#666666;

图表类型

流程图

使用矩形表示流程,使用菱形表示决策,使用椭圆表示开始/结束:


<mxCell id="2" value="Start" style="ellipse;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
  <mxGeometry x="200" y="40" width="80" height="40" as="geometry" />
</mxCell>

<mxCell id="3" value="Process Step" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
  <mxGeometry x="170" y="120" width="140" height="60" as="geometry" />
</mxCell>

<mxCell id="4" value="Condition?" style="rhombus;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
  <mxGeometry x="180" y="220" width="120" height="80" as="geometry" />
</mxCell>
架构图

使用分组容器、服务形状和带标签的连接器:


<mxCell id="2" value="Backend Services" style="swimlane;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;" vertex="1" parent="1">
  <mxGeometry x="40" y="40" width="300" height="200" as="geometry" />
</mxCell>

<mxCell id="3" value="API Gateway" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="2">
  <mxGeometry x="20" y="40" width="120" height="60" as="geometry" />
</mxCell>
序列图

使用垂直生命线和水平消息箭头:


<mxCell id="2" value="Client" style="shape=umlLifeline;perimeter=lifelinePerimeter;whiteSpace=wrap;html=1;container=1;collapsible=0;recursiveResize=0;outlineConnect=0;portConstraint=eastwest;newEdgeStyle={&quot;edgeStyle&quot;:&quot;elbowEdgeStyle&quot;,&quot;elbow&quot;:&quot;vertical&quot;,&quot;curved&quot;:0,&quot;rounded&quot;:0};" vertex="1" parent="1">
  <mxGeometry x="100" y="40" width="100" height="300" as="geometry" />
</mxCell>

<mxCell id="4" value="request()" style="html=1;verticalAlign=bottom;endArrow=block;edgeStyle=elbowEdgeStyle;elbow=vertical;curved=0;rounded=0;" edge="1" parent="1">
  <mxGeometry relative="1" as="geometry">
    <mxPoint x="150" y="100" as="sourcePoint" />
    <mxPoint x="350" y="100" as="targetPoint" />
  </mxGeometry>
</mxCell>
ER 图

使用带列的表格形状:

<mxCell id="2" value="users" style="swimlane;fontStyle=1;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
  <mxGeometry x="100" y="100" width="160" height="130" as="geometry" />
</mxCell>
<mxCell id="3" value="id: INT (PK)" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;whiteSpace=wrap;html=1;fontStyle=1;" vertex="1" parent="2">
  <mxGeometry y="26" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="4" value="email: VARCHAR(255)" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;whiteSpace=wrap;html=1;" vertex="1" parent="2">
  <mxGeometry y="52" width="160" height="26" as="geometry" />
</mxCell>
类图

使用 UML 类形状:

<mxCell id="2" value="&lt;b&gt;ClassName&lt;/b&gt;" style="swimlane;fontStyle=0;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=0;marginBottom=0;html=1;" vertex="1" parent="1">
  <mxGeometry x="100" y="100" width="160" height="130" as="geometry" />
</mxCell>
<mxCell id="3" value="- privateField: Type" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="2">
  <mxGeometry y="26" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="4" value="" style="line;strokeWidth=1;fillColor=none;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;strokeColor=inherit;" vertex="1" parent="2">
  <mxGeometry y="52" width="160" height="8" as="geometry" />
</mxCell>
<mxCell id="5" value="+ publicMethod(): void" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="2">
  <mxGeometry y="60" width="160" height="26" as="geometry" />
</mxCell>

其他资源

参考文件

如需了解详细信息,请参阅:

  • references/xml-structure.md - 完整的 XML 结构和属性
  • references/shape-library.md - 全面的形状样式和图标
  • references/styling-guide.md - 高级样式、主题和格式设置
示例文件

examples/ 中的可用示例:

  • flowchart.drawio - 带决策逻辑的基本流程图
  • architecture.drawio - 微服务架构图
  • sequence-diagram.drawio - UML 序列图
  • er-diagram.drawio - 实体关系数据库图
  • class-diagram.drawio - UML 类图
  • network-diagram.drawio - 网络拓扑图
  • org-chart.drawio - 组织层级图
  • archimate.drawio - ArchiMate 企业架构(业务/应用/技术层)
  • c4-diagram.drawio - 包含上下文图和容器图的 C4 模型

最佳实践

  1. 唯一 ID:始终确保每个 mxCell 都有唯一的 id
  2. 间距一致:使用网格对齐(以 10 px 为增量)
  3. 颜色编码:为相似元素使用一致的颜色
  4. 标签清晰:保持文字简洁,长文本使用换行
  5. 逻辑流程:将形状按从左到右或从上到下的顺序排列
  6. 分组:使用容器/泳道组织相关元素
  7. 边线路由:使用正交边线绘制清晰的图表
  8. 测试输出:确认文件能在 draw.io 中正确打开

快速入门模板

最简可用的 .drawio 文件:

<mxfile host="app.diagrams.net" modified="2024-01-01T00:00:00.000Z" agent="Claude" version="21.0.0" type="device">
  <diagram id="main" name="Diagram">
    <mxGraphModel dx="1000" dy="600" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

评论

支持 Markdown