diff --git a/doc/user_guide/Customization.ipynb b/doc/user_guide/Customization.ipynb index 454be91b8..0f7185acb 100644 --- a/doc/user_guide/Customization.ipynb +++ b/doc/user_guide/Customization.ipynb @@ -60,22 +60,20 @@ " Whether to flip the axis left to right or up and down respectively\n", " grid (default=False): boolean\n", " Whether to show a grid\n", - " hover : boolean\n", + " hover: boolean or Literal[\"vline\", \"hline\"]\n", " Whether to show hover tooltips, default is True unless datashade is\n", - " True in which case hover is False by default\n", + " True in which case hover is False by default. Can also set\n", + " `vline` for hitbox across a vertical line or `hline` for hitbox across a horizontal line.\n", " hover_cols (default=[]): list or str\n", " Additional columns to add to the hover tool or 'all' which will\n", " includes all columns (including indexes if use_index is True).\n", + " hover_formatters:\n", + " A dict of formatting options for the hover tooltip.\n", " hover_tooltips list[str] or list[tuple]:\n", " A list of dimensions to be displayed in the hover tooltip. See\n", " [HoloViews docs](https://holoviews.org/user_guide/Plotting_with_Bokeh.html#hover-tools)\n", " or [Bokeh docs](https://docs.bokeh.org/en/latest/docs/user_guide/interaction/tools.html#hovertool)\n", " for more info on structuring.\n", - " hover_formatters:\n", - " A dict of formatting options for the hover tooltip.\n", - " hover_mode (default='mouse'):\n", - " The hover mode determines how the hover tool is activated;\n", - " select from 'mouse', 'vline', or 'hline'.\n", " invert (default=False): boolean\n", " Swaps x- and y-axis\n", " frame_width/frame_height: int\n", diff --git a/hvplot/converter.py b/hvplot/converter.py index 57baa2f04..7f9a01f57 100644 --- a/hvplot/converter.py +++ b/hvplot/converter.py @@ -148,12 +148,10 @@ class HoloViewsConverter: hover_cols (default=[]): list or str Additional columns to add to the hover tool or 'all' which will includes all columns (including indexes if use_index is True). - hover_tooltips list[str] or list[tuple]: - A list of dimensions to be displayed in the hover tooltip. hover_formatters: A dict of formatting options for the hover tooltip. - hover_mode (default='mouse'): - The hover mode determines how the hover tool is activated. + hover_tooltips list[str] or list[tuple]: + A list of dimensions to be displayed in the hover tooltip. invert (default=False): boolean Swaps x- and y-axis frame_width/frame_height: int @@ -562,9 +560,9 @@ def __init__( logy=None, loglog=None, hover=None, - hover_tooltips=None, + hover_cols=[], hover_formatters=None, - hover_mode=None, + hover_tooltips=None, subplots=False, label=None, invert=False, @@ -587,7 +585,6 @@ def __init__( flip_xaxis=None, flip_yaxis=None, dynspread=False, - hover_cols=[], x_sampling=None, y_sampling=None, project=False, @@ -837,17 +834,15 @@ def __init__( if hover and not any( t for t in tools if isinstance(t, HoverTool) or t in ['hover', 'vline', 'hline'] ): - if hover in ['vline', 'hline']: - tools.append(hover) - else: - tools.append('hover') + if hover in {'vline', 'hline'}: + plot_opts['hover_mode'] = hover + tools.append('hover') + if 'hover' in tools: + if hover_tooltips: + plot_opts['hover_tooltips'] = hover_tooltips + if hover_formatters: + plot_opts['hover_formatters'] = hover_formatters plot_opts['tools'] = tools - if hover_tooltips: - plot_opts['hover_tooltips'] = hover_tooltips - if hover_formatters: - plot_opts['hover_formatters'] = hover_formatters - if hover_mode: - plot_opts['hover_mode'] = hover_mode if self.crs and global_extent: plot_opts['global_extent'] = global_extent diff --git a/hvplot/tests/testcharts.py b/hvplot/tests/testcharts.py index 37006dfc4..0067d6444 100644 --- a/hvplot/tests/testcharts.py +++ b/hvplot/tests/testcharts.py @@ -469,6 +469,31 @@ def test_errorbars_no_hover(self): bkplot = Store.renderers['bokeh'].get_plot(plot) assert not bkplot.tools + @parameterized.expand(['vline', 'hline']) + def test_hover_line(self, hover_mode): + plot = self.df.hvplot('x', 'y', hover=hover_mode) + opts = Store.lookup_options('bokeh', plot, 'plot') + self.assertEqual(opts.kwargs['hover_mode'], hover_mode) + + def test_hover_tooltips(self): + plot = self.df.hvplot('x', 'y', hover_tooltips=['x']) + opts = Store.lookup_options('bokeh', plot, 'plot') + self.assertEqual(opts.kwargs['hover_tooltips'], ['x']) + + def test_hover_formatter(self): + plot = self.df.hvplot('x', 'y', hover_formatters={'x': 'datetime'}) + opts = Store.lookup_options('bokeh', plot, 'plot') + self.assertEqual(opts.kwargs['hover_formatters'], {'x': 'datetime'}) + + def test_hover_disabled(self): + plot = self.df.hvplot( + 'x', 'y', hover_tooltips=['x'], hover_formatters={'x': 'datetime'}, hover=False + ) + opts = Store.lookup_options('bokeh', plot, 'plot') + self.assertEqual(opts.kwargs['tools'], []) + assert 'hover_formatters' not in opts.kwargs + assert 'hover_tooltips' not in opts.kwargs + def test_labels_format(self): plot = self.df.hvplot('x', 'y', text='({x}, {y})', kind='labels') assert list(plot.dimensions()) == [Dimension('x'), Dimension('y'), Dimension('label')]