问题
I have
dt_source = ColumnDataSource(
{
"date": result_detail['report_date_as_yyyy_mm_dd']
,"contract": result_detail['contract_name']
,"open_interest": result_detail['open_interest']
}
)
tools="wheel_zoom,reset,save,crosshair,pan,box_zoom"
oi = figure(plot_width=800, plot_height=200, x_axis_type="datetime", tools=tools, title="Open Interest")
oi.line(x="date", y="open_interest", source=dt_source)
oi.title_text_font_size = value("12pt")
oi.ygrid.grid_line_color = None
oi.yaxis.minor_tick_line_color = None
oi.xgrid.grid_line_dash = "dashed"
#adjust what information you get when you hover over it
oi_tooltips = """
<span face="font-family: Arial, Helvetica, sans-serif">
<div>
<span style="font-size: 15px;">@contract</span>
<span style="font-size: 10px; color: #666;">@signal</span>
</div>
<div style="line-height: 1;">
<span style="font-size: 10px; color: #666; white-space:pre;">Open Interest 	 @open_interest{1,1}</span>
</div>
<div style="line-height: 1;">
<span style="font-size: 10px; color: #666; white-space:pre;">Date 	 	 	 @date</span>
</div>
</span>
"""
oi_hover = HoverTool(tooltips=oi_tooltips)
oi.add_tools(oi_hover)
The issue is with the date (Date 	 	 	 @date
), it is being presented in the hover tool as epoch time.
I have tried:
@date
@date{date}
@date{datetime}
@date{dd/mm/yyyy}
@date{"dd/mm/yyyy"}
I would like to know the formatting options for date types using the hover tool in the html. Specifically formatting for either of the following:
yyyy-mm-dd
dd/mm/yyyy
dt_source
is using data pulled from Postgresql, the procedure it is calling is returning a date type. Running result_detail["report_date_as_yyyy_mm_dd"]
gives:
0 2016-01-26
1 2016-01-19
2 2016-01-12
3 2016-01-05
...
回答1:
Since this answer was originally posted, new work has gone into Bokeh to make things simpler. A datetime field can be formatted as a datetime directly by the hover tool, by specifying a formatter, e.g.:
HoverTool(tooltips=[('date', '@date_col{%F}')],
formatters={'date_col': 'datetime'})
It is no longer necessary to pre-format date fields in the data source as below. For more information see Formatting Tooltip Fields
OLD ANSWER:
As at 4th Feb, 2016, there is no quick way possible, and an issue is currently open requesting the feature to format date/time values in the hover tool.
A workaround is also given on the issue page that can currently be used:
To get around this, I need to add a ColumnDataSource with a single field, like so:
source = ColumnDataSource(data=dict( time=df.map(lambda x: x.strftime('%d-%m-%Y')) ))
and then I tell my tooltip to display
("time", "@time")
.
来源:https://stackoverflow.com/questions/35128934/bokeh-hover-tool-format-date-variable-in-custom-html