简体   繁体   English

散景:无法更新悬停工具提示的格式

[英]Bokeh: Not able to update format of hover tooltip

I'm trying to update the format of an already defined hover tooltip, but I do not observe any change. 我正在尝试更新已经定义的悬停工具提示的格式,但是我没有观察到任何变化。 The change I do in the example below is changing the x-axis between number and time scale ('00:00:00'). 我在以下示例中所做的更改是在数字和时间刻度('00:00:00')之间更改x轴。 The x-axis is updated as expected. x轴已按预期更新。 Using Bokeh version 0.12.16, mac OS X, Safari browser. 使用Bokeh版本0.12.16,mac OS X,Safari浏览器。

Any hints with respect to what I'm doing wrong is appreciated. 关于我在做什么错的任何提示都值得赞赏。

from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import HoverTool, NumeralTickFormatter, AdaptiveTicker
from bokeh.models.widgets import RadioGroup
from bokeh.layouts import row, widgetbox
from bokeh.io import curdoc


def update_axis_format(new):
    if new == 0:
        format_num = '0'
        mantissas= [1,2,5]
    else:
        format_num = '00:00:00'
        mantissas=[3.6, 7.2, 18]

    p.xaxis[0].formatter = NumeralTickFormatter(format = format_num)
    p.xaxis.ticker = AdaptiveTicker(base = 10, mantissas = mantissas)
    p.xgrid.ticker = AdaptiveTicker(base = 10, mantissas = mantissas)
    p.tools[0].tooltips[2] = ("x", "@x{{{}}}".format(format_num))


source = ColumnDataSource(data=dict(
    x=[10, 2000, 10000, 40000, 50000],
    y=[2, 5, 8, 2, 7],
    desc=['A', 'b', 'C', 'd', 'E'],
))

hover = HoverTool(tooltips=[
    ("index", "$index"),
    ("desc", "@desc"),
    ("x", "@x")
])

p = figure(plot_width=400, plot_height=400, tools=[hover],
           title="Mouse over the dots")

p.circle('x', 'y', size=20, source=source)

xaxis_format = RadioGroup(
        labels=["x-axis as number", "x-axis as time"], active=0)

xaxis_format.on_click(update_axis_format)

widget = widgetbox(xaxis_format)

curdoc().add_root(row(widget,p))

The BokehJS code is not sensitive to "internal" (ie in place) changes to tooltips . BokehJS代码对“内部”(即就地)对tooltips更改不敏感。 You need to replace the tooltips value entirely. 您需要完全替换tooltips值。 Eg this simplified code works as expected: 例如,此简化的代码按预期工作:

def update_axis_format(new):
    if new == 0:
        format_num = '0'
        mantissas= [1,2,5]
    else:
        format_num = '00:00:00'
        mantissas=[3.6, 7.2, 18]

    p.xaxis[0].formatter = NumeralTickFormatter(format = format_num)
    p.xaxis.ticker = AdaptiveTicker(base = 10, mantissas = mantissas)
    p.xgrid.ticker = AdaptiveTicker(base = 10, mantissas = mantissas)

    # replace all of tooltips, not just part
    p.tools[0].tooltips = [("x", "@x{{{}}}".format(format_num))]

hover = HoverTool(tooltips=[("x", "@x")])

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM