How to generate a legend with colors in PlantUML?
Asked Answered
W

5

50

I want to color classes in my class diagram, based on a certain criteria, and then display a legend, that would look roughly like:

Legend with colors

If I could add an HTML table within PlantUML's legend and endlegend, I could achieve this by changing the cell backgrounds. However, adding the HTML <table> doesn't work. I also tried using PlantUML's salt to insert a table, but I couldn't find any way to color the cells of the table.

Is there any way to achieve this?

Weston answered 23/6, 2015 at 9:43 Comment(0)
R
35

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

Image showing the legend.

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

Revisionist answered 20/10, 2015 at 11:11 Comment(0)
R
66

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

This is how it looks like:

Legend color table

Recuperate answered 27/5, 2017 at 14:18 Comment(2)
This worked much nicer for me than the accepted answer. Also, it's worth mentioning that you can use color names instead of the hex values (as shown in: github.com/qywx/PlantUML-colors)Donnydonnybrook
Nice, or even combine into single column of colored cells like this |Type| / |<#LightYellow>Unchanged| / |<#LightPink>Replaced| / endlegendCompact
R
35

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

Image showing the legend.

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

Revisionist answered 20/10, 2015 at 11:11 Comment(0)
P
9

Building on the other answers to get a legend styled to match the origianl request:

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

References used, in case someone needs to style further: 1, 2.

Persecute answered 7/2, 2023 at 14:46 Comment(0)
P
5

Build on the other answers:

enter image description here

@startuml

skinparam legend {
  backgroundColor #GhostWhite
  entrySeparator #GhostWhite
}

legend top
  <#GhostWhite,#GhostWhite>|        |= __Legend__ |
  |<#red>   | Type A Classes|
  |<#blue>  | Type B Classes|
  |<#green> | Type C Classes|
endlegend


@enduml
Paradrop answered 13/6, 2023 at 18:33 Comment(1)
Just what I needed.Apulia
W
2

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.

Legend Vertical Legend Horizontal

Weston answered 30/6, 2015 at 6:21 Comment(2)
You could ask for it in the Q&A part of the plantuml site.Contemporary
@Contemporary Yes, you are right. I figured that out later. I did, and got a better answer there. plantuml.sourceforge.net/qa/?qa=3596/… I have asked them to post their answer here too.Weston

© 2022 - 2024 — McMap. All rights reserved.