How to generate a legend with colors in PlantUML?

故事扮演 提交于 2019-12-05 01:41:45

This is not perfect, but you can use a creole table. (see 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

There are some drawing artifacts, but is it what you are expecting ?

From the plantuml forum. Where they allowed to copy this answer here.

Yes, please copy/paste our answer to StackOverflow : it would indeed by helpful

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

I've tried nfec's solution and it was not working for me, but it started me off on a solution that did work. Here is what I got.

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

There doesn't appear to be a direct way of including a color-coded legend in a PlantUML diagram, but I figured out a workaround which is close enough.

  • Declare classes TypeA, TypeB, TypeC inside a package LEGEND.
  • Hide the circle, methods and members for each class.
  • Attach "hidden" connectors between the classes.


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
}

Alternatively, the "hidden" connectors could be replaced with -r-, like so:

TypeA -r- TypeB
TypeB -r- TypeC

These produces the following diagrams. Neither one is perfect, but better than drawing the legend separately in image editor. :-) I do hope PlantUML offers direct support for this in a future release.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!