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

Set OHLC x-axis tooltip to datetime format #1493

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions hvplot/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2630,15 +2630,23 @@ def ohlc(self, x=None, y=None, data=None):
seg_cur_opts, seg_compat_opts = self._get_compat_opts('Segments')
tools = seg_cur_opts.pop('tools', [])
if 'hover' in tools:
x_data = data[x] if x in data.columns else data.index
if pd.api.types.is_datetime64_any_dtype(x_data):
x_tooltip = f'@{x}{{%F}}'
Copy link
Member

Choose a reason for hiding this comment

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

Two comments on %F:

  • I can never remember what these sort of format codes mean :) I looked up Bokeh's documentation and think I found the place where it's documented as part of the DateFormatter. %F is a strftime format code Equivalent to %Y-%m-%d (the ISO 8601 date format). Could you add a little comment like %F: strftime code for %Y-%m-%d?
  • Are OHLC plots limited to daily time series? Asked ChatGPT that says it's the main use case but hourly also happens ("mainly day traders and algorithmic traders focusing on short-term price movements"). What do you think about using instead %F %T, equivalent to %Y-%m-%d %H:%M:%S?

formatter = {f'@{x}': 'datetime'}
else:
x_tooltip = f'@{x}'
formatter = {}
tools[tools.index('hover')] = HoverTool(
formatters=formatter,
tooltips=[
(x, f'@{x}'),
(x, x_tooltip),
('Open', f'@{o}'),
('High', f'@{h}'),
('Low', f'@{l}'),
('Close', f'@{c}'),
]
+ [(hc, f'@{hc}') for hc in vdims[4:]]
+ [(hc, f'@{hc}') for hc in vdims[4:]],
)
seg_cur_opts['tools'] = tools
seg_cur_opts['color'] = self.kwds.get('line_color', 'black')
Expand Down
36 changes: 36 additions & 0 deletions hvplot/tests/plotting/testohlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,39 @@ def test_ohlc_hover_cols_all():
tooltips = segments.opts.get('plot').kwargs['tools'][0].tooltips
assert len(tooltips) == len(df.columns) + 1
assert tooltips[-1] == ('Volume', '@Volume')


def test_ohlc_date_tooltip_format():
Copy link
Member

Choose a reason for hiding this comment

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

Another test could be added with a DataFrame whose date column isn't the index.

plot = df.hvplot.ohlc(y=ohlc_cols)
segments = plot.Segments.I
hover_tool = segments.opts.get('plot').kwargs['tools'][0]
tooltips = hover_tool.tooltips
x_label, x_tooltip = tooltips[0]
assert '{%F}' in x_tooltip
formatter_key = '@' + x_label
formatter = hover_tool.formatters
assert formatter.get(formatter_key) == 'datetime'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert formatter.get(formatter_key) == 'datetime'
assert formatter[formatter_key] == 'datetime'



def test_ohlc_non_datetime_x_axis():
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=[1, 2, 3],
)

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

plot = df.hvplot.ohlc(y=ohlc_cols)
segments = plot.Segments.I
hover_tool = segments.opts.get('plot').kwargs['tools'][0]
tooltips = hover_tool.tooltips
x_label, x_tooltip = tooltips[0]
assert '{%F}' not in x_tooltip
formatter_key = '@' + x_label
assert formatter_key not in hover_tool.formatters
Loading