From 696fdb77c36d3a1db3b567f6347a04d002b8f930 Mon Sep 17 00:00:00 2001 From: KhadeerBasha1232 Date: Mon, 16 Nov 2026 18:40:09 +0530 Subject: [PATCH] Draw each colormap legend into its own control 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 --- branca/templates/color_scale.js | 2 +- tests/test_colormap.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/branca/templates/color_scale.js b/branca/templates/color_scale.js index c49fd24..8cecefd 100644 --- a/branca/templates/color_scale.js +++ b/branca/templates/color_scale.js @@ -25,7 +25,7 @@ .tickSize(1) .tickValues({{ this.tick_labels }}); - {{this.get_name()}}.svg = d3.select(".legend.leaflet-control").append("svg") + {{this.get_name()}}.svg = d3.select({{this.get_name()}}.legend.getContainer()).append("svg") .attr("id", 'legend') .attr("width", {{ this.width }}) .attr("height", {{ this.height }}); diff --git a/tests/test_colormap.py b/tests/test_colormap.py index cd30405..ba2d723 100644 --- a/tests/test_colormap.py +++ b/tests/test_colormap.py @@ -6,6 +6,7 @@ import pytest import branca.colormap as cm +from branca.element import Figure def test_simple_step(): @@ -151,3 +152,15 @@ def test_max_labels_step(max_labels, expected): except AssertionError: # rendering outside parent Figure raises error pass assert colorbar.tick_labels == expected + + +def test_each_legend_draws_into_its_own_control(): + figure = Figure() + first = cm.LinearColormap(["red", "blue"]).add_to(figure) + second = cm.StepColormap(["red", "blue"]).add_to(figure) + out = figure.render() + # A global selector would put every legend into the first one on the page. + assert 'd3.select(".legend.leaflet-control")' not in out + for colormap in (first, second): + name = colormap.get_name() + assert f"d3.select({name}.legend.getContainer())" in out