如何在PlantUML中使用颜色生成图例?

47

我想基于某个特定标准在类图中给类着色,并显示一个类似以下的图例:

Legend with colors

如果我能在PlantUML的legendendlegend标签内添加HTML表格,我可以通过更改单元格背景来实现这一点。但是,添加HTML <table>无法生效。我还尝试使用PlantUML的salt插入表格,但我找不到任何方法来对表格单元格进行着色。

有没有办法实现这个需求呢?

5个回答

57

我尝试了nfec的解决方案,但对我不起作用,但它启发了我找到了一个可行的解决方案。这是我的方法:

legend right
    |Color| Type |
    |<#FF0000>| Type A class|
    |<#00FF00>| Type B class|
    |<#0000FF>| Type C class|
endlegend

这是它的外观:

图例颜色表


6
这对我来说比被接受的答案更有效。另外值得一提的是,你可以使用颜色名称而不是十六进制值(如所示:https://github.com/qywx/PlantUML-colors)。 - Maor
很好,甚至可以像这样将它们合并成单列的彩色单元格:|类型| / |<#浅黄色>未更改| / |<#浅粉色>已替换| / endlegend - patricksurry

33

这并不完美,但你可以使用一张Creole表格。(参见http://plantuml.sourceforge.net/creole.html

@startuml class foo

 legend
 |= |= Type |
 |<back:#FF0000>   </back>| Type A class |
 |<back:#00FF00>   </back>| Type B class |
 |<back:blue>   </back>| Type C class |
 endlegend

@enduml

展示图例的图片。

虽然存在一些绘制瑕疵,但这是您期望的结果吗?

来自PlantUML论坛。他们允许在此处复制此答案。

是的,请将我们的回答复制/粘贴到StackOverflow:这确实会有所帮助。

http://plantuml.sourceforge.net/qa/?qa=3596/how-to-generate-a-legend-with-colors-in-plantuml


8

在其他答案的基础上构建一个图例,使其与原始请求相匹配:

The legend

' set legend to have a white background
skinparam legendBackgroundColor #FFFFFF
' remove box around legend
skinparam legendBorderColor #FFFFFF
' remove the lines between the legend items
skinparam legendEntrySeparator #FFFFFF


legend right
'   the <#FFFFFF,#FFFFFF> sets the background color of the legend to white
    <#FFFFFF,#FFFFFF>|<#red>| Type A Classes|
    ' the space between the | and <#blue> is important to make the color column wider
    |<#blue>     | Type B Classes|
    |<#green>| Type C Classes|
endlegend

参考文献,以防有人需要进一步样式:12


2

在PlantUML图表中,似乎没有直接包含彩色图例的方法,但我找到了一个足够接近的解决方案。

  • LEGEND包内声明类TypeATypeBTypeC
  • 隐藏每个类的圆圈、方法和成员。
  • 在类之间添加“隐藏”的连接器。


package LEGEND <<Rect>> { ' Draw the LEGEND "package" as a rectangular box.
class TypeA as "Type A Class" #LightRed
hide  TypeA circle
hide  TypeA methods
hide  TypeA members

class TypeB as "Type B Class" #LightBlue
hide  TypeB circle
hide  TypeB methods
hide  TypeB members

class TypeC as "Type C Class" #LightGreen
hide  TypeC circle
hide  TypeC methods
hide  TypeC members

' Workaround to prevent PlantUML from positioning the legend blocks randomly.
TypeA -[hidden]- TypeB
TypeB -[hidden]- TypeC
}

或者,可以用-r-替换“隐藏”的连接器,如下所示:
TypeA -r- TypeB
TypeB -r- TypeC

这些内容生成了以下图表。虽然不完美,但比在图像编辑器中单独绘制传说要好。:-) 我希望PlantUML在未来的版本中直接支持这个功能。

你可以在PlantUML网站的问答部分提出这个问题。 - Fuhrmanator
@Fuhrmanator 是的,你说得对。我后来想通了。我去那里问了一下,得到了更好的答案。http://plantuml.sourceforge.net/qa/?qa=3596/how-to-generate-a-legend-with-colors-in-plantuml 我已经要求他们也在这里发布他们的答案。 - Masked Man

0
在其他答案的基础上继续发展:

enter image description here

@startuml
skinparam legend { backgroundColor #GhostWhite entrySeparator #GhostWhite } legend top <#GhostWhite,#GhostWhite>| |= __图例__ | |<#red> | 类型 A 的类| |<#blue> | 类型 B 的类| |<#green> | 类型 C 的类| endlegend
@enduml

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接