mapbox / mapboxgl-jupyter

Use Mapbox GL JS to visualize data in a Python Jupyter notebook
MIT License
661 stars 136 forks source link

How to hide the legends for GraduatedCircleViz? #181

Open xchen0326 opened 3 years ago

xchen0326 commented 3 years ago

I tried legend=False but it does not work. I wanted to assign the radius and the color of the circles based on two properties, but it leads to a problem that there creates two legends which either overlap each other or overlap the map. How can I fix that?

akacarlyann commented 3 years ago

Hi @xchen0326 sorry for the delay. Hope you found a workaround in the meantime! This bug is due to an oversight where some default values are not being set in the main.html template when legend is set to False. The easiest way around this would be to update the mapboxgl.viz.MapViz.create_html method so that all of the legend-based settings are included in the template either way. This could be as simple as changing lines 303-322 https://github.com/mapbox/mapboxgl-jupyter/blob/master/mapboxgl/viz.py#L303-L322 to the following:

        if self.legend:

            if all([self.legend, self.legend_gradient, self.legend_function == 'radius']):
                raise LegendError(' '.join(['Gradient legend format not compatible with a variable radius legend.',
                                            'Please either change `legend_gradient` to False or `legend_function` to "color".']))

        options.update(
            showLegend=self.legend,
            legendLayout=self.legend_layout,
            legendFunction=self.legend_function,
            legendStyle=self.legend_style, # reserve for custom CSS
            legendGradient=json.dumps(self.legend_gradient),
            legendFill=self.legend_fill,
            legendHeaderFill=self.legend_header_fill,
            legendTextColor=self.legend_text_color,
            legendNumericPrecision=json.dumps(self.legend_text_numeric_precision),
            legendTitleHaloColor=self.legend_title_halo_color,
            legendKeyShape=self.legend_key_shape,
            legendKeyBordersOn=json.dumps(self.legend_key_borders_on)
        )

(basically move the options.update({...}) outside of the if self.legend if block. Does that make sense? I think the CSS needs an overhaul so I'll think if there's a good way to address this there.