How to generate a legend with colors in PlantUML?

+1 vote
asked Jun 30, 2015 in Wanted features by Masked Man (120 points)

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

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?

Note: Question previously asked on, and copied verbatim from, StackOverflow (Link: http://stackoverflow.com/q/30999290/252576).

1 Answer

+3 votes
answered Jun 30, 2015 by plantuml (294,960 points)
selected Jul 1, 2015 by Masked Man
 
Best answer

Hello,

This is not perfect, but you can use some 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 artifact, but is it what you are expecting ?

commented Jul 1, 2015 by Masked Man (120 points)
Yes, this is good enough for my purpose. Thanks.
commented Jul 5, 2015 by Masked Man (120 points)
@plantuml Do you also want to post this as an answer to my question on StackOverflow, so it might be helpful to more people who might look for it?
commented Jul 6, 2015 by plantuml (294,960 points)
Yes, please copy/paste our answer to StackOverflow : it would indeed by helpful
commented Feb 24, 2022 by Lora
The colours in the legend are displayed in png file but are not displayed in svg file.
...