Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect hover_cols for OHLC #1216

Merged
merged 2 commits into from Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions hvplot/converter.py
Expand Up @@ -2060,7 +2060,11 @@ def ohlc(self, x=None, y=None, data=None):
o, h, l, c = y
neg, pos = self.kwds.get('neg_color', 'red'), self.kwds.get('pos_color', 'green')
color_exp = (dim(o)>dim(c)).categorize({True: neg, False: pos})
ds = Dataset(data, [x], [o, h, l, c])
ohlc_cols = [o, h, l, c]
if x in self.hover_cols:
self.hover_cols.remove(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this problem if x is a or l and self.hover_cols is all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would x be equal to l?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I just took all the letters of all and did not see that it was already part of the ohlc columns.

vdims = list(dict.fromkeys(ohlc_cols + self.hover_cols))
ds = Dataset(data, [x], vdims)
if ds.data[x].dtype.kind in 'SUO':
rects = Rectangles(ds, [x, o, x, c])
else:
Expand All @@ -2078,9 +2082,9 @@ def ohlc(self, x=None, y=None, data=None):
tools = seg_cur_opts.pop('tools', [])
if 'hover' in tools:
tools[tools.index('hover')] = HoverTool(tooltips=[
(x, '@{%s}' % x), ('Open', '@{%s}' % o), ('High', '@{%s}' % h),
('Low', '@{%s}' % l), ('Close', '@{%s}' % c)
])
(x, f'@{x}'), ('Open', f'@{o}'), ('High', f'@{h}'),
('Low', f'@{l}'), ('Close', f'@{c}')
] + [(hc, f'@{hc}') for hc in vdims[4:]])
seg_cur_opts['tools'] = tools
seg_cur_opts ['color'] = self.kwds.get('line_color', 'black')
if 'xlabel' not in seg_cur_opts:
Expand Down
31 changes: 31 additions & 0 deletions hvplot/tests/plotting/testohlc.py
@@ -0,0 +1,31 @@
import pandas as pd
import hvplot.pandas # noqa


df = pd.DataFrame({
'Open': [100.00, 101.25, 102.75],
'High': [104.10, 105.50, 110.00],
'Low': [94.00, 97.10, 99.20],
'Close': [101.15, 99.70, 109.50],
'Volume': [10012, 5000, 18000],
}, index=[pd.Timestamp('2022-08-01'), pd.Timestamp('2022-08-03'), pd.Timestamp('2022-08-04')])

ohlc_cols = ['Open', 'High', 'Low', 'Close']


def test_ohlc_hover_cols():
plot = df.hvplot.ohlc(y=ohlc_cols, hover_cols=['Volume'])
segments = plot.Segments.I
assert 'Volume' in segments
tooltips = segments.opts.get('plot').kwargs['tools'][0].tooltips
assert len(tooltips) == len(df.columns) + 1
assert tooltips[-1] == ('Volume', '@Volume')


def test_ohlc_hover_cols_all():
plot = df.hvplot.ohlc(y=ohlc_cols, hover_cols='all')
segments = plot.Segments.I
assert 'Volume' in segments
tooltips = segments.opts.get('plot').kwargs['tools'][0].tooltips
assert len(tooltips) == len(df.columns) + 1
assert tooltips[-1] == ('Volume', '@Volume')