I would like to plot a time series (x-axis: absolute time, y-axis: data) and provide in the hover tooltip the absolute and relative time. The data is provided as a ColumnDataSource to the plot. I do not want to add an additional series for the relative time to the ColumnDataSource since this represents an unnecessary data duplication. I’m looking for a way to add a “derived” series with
relative_time = absolute_time - offset
to the hover tooltip. CustomJSHover
provides a way to add a derived hover tooltip. However, so far I could not manage to add a tooltip for the same series from the ColumnDataSource but with 2 different formatters.
Minimal example:
from bokeh.plotting import figure, output_file, show
from bokeh.models.glyphs import Line
from bokeh.models import ColumnDataSource, HoverTool, CustomJSHover
x = [10, 10, 13, 14, 15]
y = [6, 7, 2, 4, 5]
source = ColumnDataSource(dict(x=x, y=y))
output_file("lines.html")
p = figure(x_axis_label='x', y_axis_label='y')
l = Line(x='x', y='y')
p.add_glyph(source, l)
offset = 10
x_custom = CustomJSHover(
args=dict(offsetSource=ColumnDataSource(dict(offset=[offset]))),
code="""
return "" + (special_vars.data_x - offsetSource.data.offset[0]);
""")
p.add_tools(HoverTool(
tooltips=[
( 'absolute_time','@x{0.000}' ),
( 'relative_time','@x{custom}' ),
],
formatters={"@x": x_custom}
))
show(p)
The result is a hover tooltip like this:
absolute_time: 4
relative_time: 4
I would like to get something like this:
absolute_time: 14.000
relative_time: 4.000
Is there a way to achieve this with CustomJSHover or is this use case currently not supported?