How can I add icons to Builder legends?
CARTO Builder gives you the ability to symbolize point features with images. As a “best practice” to ensure legibility, you should make your Legend symbols match your point marker icons. This can be done in Builder with custom HTML. You can also represent line features with a Legend line, and polygon features with a Legend square.
In this map we are using circle markers to represent cities, except for a star icon that represents the state capital. We are also using polygon geometry to represent census tracts, and line geometry to represent roads and ferry routes.
Add an icon to your Legend
You can use the same image for your point marker style and your Legend.
-
After you finish styling your point markers with images, make sure your
STYLE
panel toggle is set toCARTOCSS
. Copy the url you find inside quotes in themarker-file
definition.For example, our star marker is in this CartoCSS style rule:
marker-file: ramp([name], (url("https://s3.amazonaws.com/com.cartodb.users-assets.production/production/documentation/assets/20190513213949star.svg")), ("Juneau"), "=");
From there we copied this link to the star image:
https://s3.amazonaws.com/com.cartodb.users-assets.production/production/documentation/assets/20190513213949star.svg
-
Click on the layer’s
LEGEND
panel. Select theCUSTOM
thumbnail in theSelect Style
section of theCOLOR
panel.Notice that the Legend items auto-populate based on your current styles. If you are using custom CartoCSS and the Legend items don’t auto-populate, you can click
ADD ITEM
and select the appropriate colors.For each item, a point icon will appear in the Legend by default.
-
Set the
LEGEND
panel toggle toHTML
. Find the<p>
element that contains the title of the item that you are symbolizing with an image.Above that you should see a
<span>
element similar to this:<span class="Legend-categoryCircle" style="opacity:1; background: #7F3C8D;"></span>
-
Replace that
<span>
element with the line below.<span style="opacity:1; height:15px; width:15px; background-image: url('https://s3.amazonaws.com/com.cartodb.users-assets.production/production/documentation/assets/20190513213949star.svg') no-repeat center; background-size: 15px 15px;"></span>
Replace the
src
url with the one you copied from yourmarker-file
style and clickAPPLY
.You can also change the pixel sizes as needed to accommodate your icon’s dimensions.
Add a square to your Legend
Take the steps below to create a square icon in your Legend, like we did for CENSUS TRACTS
in the map above.
- Open your map in your CARTO dashboard and click on your layer’s card to open it.
- Click on your map layer’s
LEGEND
tab and select theCUSTOM
thumbnail in theSelect Style
section of theCOLOR
panel. Notice that a point icon appears in the Legend by default. -
Enter a label in the field next to the color bar in the
Creating your legend
section. We entered census tracts in the example map above.Notice that you can enter field text with lower or upper case characters, but it always appears in upper case in the Legend.
- Set the
COLOR
panel’s toggle toHTML
. You will see aspan
element that looks similar to this:<span class="Legend-categoryCircle" style="opacity:1; background: #826DBA;"></span>
-
Replace that span element with the one below. It adds an 8 pixel by 8 pixel square to the legend and gives it a white outline to match the Legend’s white background.
Change the
background
color to the same one you’re using for your map feature. You can copy the feature’s hex color code from the color pickerHEX
value, which is visible when you click on the layerSTYLE
panel’sPOLYGON COLOR
color bar.<span style="opacity:1; border:1px solid #ffffff; background: #f1ccd0; height:8px; width:8px; display:inline-block"></span>
- After changing the
background
color clickAPPLY
.
Add a line to your Legend
Take the steps below to create line icons in your Legend, like we did for ROAD
and FERRY
in the map above.
- Open your map in your CARTO dashboard and click on your layer’s card to open it.
-
Click on your map layer’s LEGEND tab and select the CUSTOM thumbnail in the Select Style section of the COLOR panel.
In the
LEGEND
panel’sCreating your legend
section you should make sure an item has been added for each of the different symbols used in your layer. Click on the color bar for each item and set it’s color to the same one used in your STYLE panel. - Enter a label in the field next to the color bar in the
Creating your legend
section. We used road for the first label and ferry for the second. -
Set the
COLOR
panel’s toggle toHTML
. You will see aspan
element for each of your items.Comment them out by enclosing each span element in a
<!--
and-->
.<!-- <span class="Legend-categoryCircle" style="opacity:1; background: #4cc8a3;"></span> -->
-
Beneath the commented-out span element for road paste the code below.
This creates a 25px wide rectangle with a bottom border that is 3px tall. This represents our line icon.
<span style="opacity:1; border-bottom:3px solid #4cc8a3; background:#FFF; height:1px; width:25px; display:inline-block"></span>
-
The color of our line icon is controlled by the
border-bottom
color property. The commented-out span elements already include thebackground
colors needed for your lines.Copy the
background
hex color from the span element commented out for road. In the span element you just added beneath it, paste the color code into theborder-bottom
hex # value. -
For ferry routes, we used
line-dasharray
styling to create a dashed line. We can create a dashed line icon in our Legend also.Beneath the commented-out span element for ferry paste the code below and click
APPLY
.<span style="opacity:1; border-bottom:3px dashed #1d6996; background:#FFF; height:1px; width:25px; display:inline-block"></span>
To match the map feature’s line dash pattern exactly and consistently across browsers you can use a dashed image in the Legend. It would be implemented the same way we did above for Juneau’s Legend symbol.