散景-如果工具提示缺少值,则不显示 [英] Bokeh - Do not show tooltip if it has missing value

查看:85
本文介绍了散景-如果工具提示缺少值,则不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究显示集群活动的散景人物.当用户将鼠标悬停在特定处理器上时,我希望它显示有关该处理器的统计信息.这是代码:

I'm working on a bokeh figure that shows cluster activity. When a user hovers over a particular processor, I want it to show statistics about the processor. Heres the code:

TOOLTIPS = [
    ("Usage", "@{usage}%"),
    ("Name", "@name"),
    ("PID", "@pid"),
    ("Command", "@command"),
    ("User", "@user"),
]

p = figure(title="Cluster Activity",
           plot_width=1200,
           plot_height=700,
           x_range=nodes,
           y_range=list(reversed(cores)),
           tools='hover',
           toolbar_location=None,
           tooltips=TOOLTIPS
           )

这可行,但是我不想显示值为None的工具提示.例如,如果特定处理器的用户"值为无",则工具提示不应包含用户值,而应显示"User:???".

This works, but I don't want to show tooltips with a value of None. For example, if a particular processor, has a None value for User, the tooltip should not contain a user value, rather than showing "User : ???".

有没有办法做到这一点?我似乎在教程中找不到与此类似的东西.我想避免编写自定义JS.

Is there any way to do this? I can't seem to find anything similar to this in the tutorials. I'd like to avoid writing custom JS.

推荐答案

您还可以使用附加到HoverTool(Bokeh 1.1.0)的JS回调动态创建工具提示.

You can also create the tooltips dynamically using JS callback attached to the HoverTool (Bokeh 1.1.0)

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, HoverTool, CustomJS

pid = [1, 2, 3, 4, 5, 6]
user = ['user1', 'user2', 'user3', 'user4', None, 'user6']
name = ['name', 'name2', 'name3', 'name4', 'name5', 'name6']

source = ColumnDataSource(data = dict(pid = pid, user = user, name = name))

p = figure(x_range = FactorRange(*name), sizing_mode = 'stretch_both', title = "Test", toolbar_location = None, tools = "")
p.vbar(x = 'name', top = 'pid', width = 0.2, source = source)

code = '''  hover.tooltips = [["Name", "@name"], ["PID", "@pid"]];
            if (cb_data.index.indices.length > 0) { 
                index = cb_data.index.indices[0];
                counts = source.data.user[index]

                if (counts != null)
                    hover.tooltips = [["Name", "@name"], ["User", "@user"], ["PID", "@pid"]];                                       

            } '''
hover = HoverTool()
hover.callback = CustomJS(args = dict(source = source, hover = hover), code = code)
p.add_tools(hover)

show(p)

结果:

这篇关于散景-如果工具提示缺少值,则不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆