Skip to content

Draw each colormap legend into its own control - #247

Open
KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:colormap-legend-own-control
Open

KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:colormap-legend-own-control

Conversation

@KhadeerBasha1232

Copy link
Copy Markdown

Draw each colormap legend into its own control

Summary

When a page has more than one colormap legend, all of them are drawn into the first legend box and the other boxes stay empty. This fixes that by drawing each legend into its own Leaflet control.

The bug

color_scale.js creates a Leaflet control for the legend, but then attaches the SVG with a page-wide selector:

d3.select(".legend.leaflet-control").append("svg")

d3.select returns the first match in the document, so every colormap appends its SVG to the first legend on the page. This shows up when:

  • Two colormaps are added to one map: both legends stack in one box, and the second box is empty.
  • Using folium's DualMap: the right-hand map's legend is drawn on the left-hand map. This is the bug reported in Legend location in a DualMap folium#1167.

The fix

Select the container of the legend's own control instead:

d3.select({{this.get_name()}}.legend.getContainer()).append("svg")

L.Control.getContainer() returns the div that onAdd created for this legend. A single legend renders exactly as before.

Tests

  • New test: rendering two colormaps in one Figure gives each one a d3.select(<its name>.legend.getContainer()) call, and the page-wide selector is gone. The test fails on the current template and passes with the fix.
  • tests/test_colormap.py: 16 passed.
  • flake8, isort, black, codespell, pyupgrade and add-trailing-comma pass on the changed files, using the versions pinned in .pre-commit-config.yaml.

Checked in a browser

I rendered folium maps and counted the SVGs in each legend box, before and after:

Case Before After
Two colormaps on one map box 1: both legends · box 2: empty each box: its own legend
DualMap, one colormap per side left map: both legends · right map: empty each map: its own legend

No console errors.

Note

This PR and #246 (legend position) both touch color_scale.js and add tests at the end of tests/test_colormap.py. They change different lines of the template; I'll rebase whichever is merged second.

The legend SVG was attached with d3.select(".legend.leaflet-control"),
which matches the first legend on the whole page. With more than one
colormap (two legends on one map, or one per map in folium's DualMap),
every legend was drawn into the first legend box and the others stayed
empty. Select the legend's own control container instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant