Repository navigation
Draw each colormap legend into its own control - #247
Open
KhadeerBasha1232 wants to merge 1 commit into
Open
KhadeerBasha1232 wants to merge 1 commit into
KhadeerBasha1232 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.jscreates a Leaflet control for the legend, but then attaches the SVG with a page-wide selector:d3.selectreturns the first match in the document, so every colormap appends its SVG to the first legend on the page. This shows up when: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:
L.Control.getContainer()returns thedivthatonAddcreated for this legend. A single legend renders exactly as before.Tests
Figuregives each one ad3.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..pre-commit-config.yaml.Checked in a browser
I rendered folium maps and counted the SVGs in each legend box, before and after:
DualMap, one colormap per sideNo console errors.
Note
This PR and #246 (legend
position) both touchcolor_scale.jsand add tests at the end oftests/test_colormap.py. They change different lines of the template; I'll rebase whichever is merged second.