散景悬停工具 - 在自定义中格式化日期变量 html

bokeh hover tool - format date variable in custom html

我有

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>&nbsp;
        <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 &#9; @open_interest{1,1}</span>
    </div>
    <div style="line-height: 1;">
        <span style="font-size: 10px; color: #666; white-space:pre;">Date &#9; &#9; &#9; @date</span>
    </div>
</span>
"""
oi_hover = HoverTool(tooltips=oi_tooltips)
oi.add_tools(oi_hover)

问题在于日期 (Date &#9; &#9; &#9; @date),它在悬停工具中显示为纪元时间。

我试过:

我想知道使用 html 中的悬停工具的日期类型的格式设置选项。 具体格式设置如下:


dt_source 正在使用从 Postgresql 中提取的数据,它正在调用的过程正在返回日期类型。 运行 result_detail["report_date_as_yyyy_mm_dd"] 给出:

0      2016-01-26
1      2016-01-19
2      2016-01-12
3      2016-01-05
       ...

自从最初发布此答案后,Bokeh 已投入新工作以简化操作。日期时间字段可以通过悬停工具直接格式化为日期时间,方法是指定格式化程序,例如:

HoverTool(tooltips=[('date', '@date_col{%F}')],
          formatters={'date_col': 'datetime'})

不再需要像下面那样预先格式化数据源中的日期字段。有关详细信息,请参阅 Formatting Tooltip Fields


旧答案:

截至 2016 年 2 月 4 日,没有快速的方法,并且 issue is currently open 请求功能在悬停中格式化 date/time 值工具.

目前可以使用的问题页面上也给出了解决方法:

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").