diff --git a/holoviews/plotting/bokeh/element.py b/holoviews/plotting/bokeh/element.py index 6cd4fb8538..3d18b7a2df 100644 --- a/holoviews/plotting/bokeh/element.py +++ b/holoviews/plotting/bokeh/element.py @@ -1,6 +1,8 @@ +import base64 import warnings from collections import defaultdict from itertools import chain +from textwrap import dedent from types import FunctionType import bokeh @@ -13,6 +15,7 @@ BinnedTicker, ColorBar, ColorMapper, + CustomAction, CustomJS, EqHistColorMapper, GlyphRenderer, @@ -2360,6 +2363,38 @@ def _draw_scalebar(self, plot): self.handles['scalebar'] = scale_bar plot.add_layout(scale_bar) + if plot.toolbar: + if "scalebar_tool" in self.handles: + self.handles["scalebar_tool"].callback.args["scale_bars"] = plot.select(ScaleBar) + else: + ruler_icon = """\ + + + + + + + + + """ + encoded_icon = base64.b64encode(dedent(ruler_icon).encode()).decode('ascii') + scalebar_tool = CustomAction( + icon=f"data:image/svg+xml;base64,{encoded_icon}", + description="Toggle ScaleBar", + callback=CustomJS( + args=dict(scale_bars=plot.select(ScaleBar)), + code=""" + export default ({scale_bars}) => { + for (let i = 0; i < scale_bars.length; i++) { + scale_bars[i].visible = !scale_bars[i].visible + } + }""", + ) + ) + plot.toolbar.tools.append(scalebar_tool) + self.handles["scalebar_tool"] = scalebar_tool + + class CompositeElementPlot(ElementPlot): """ A CompositeElementPlot is an Element plot type that coordinates