条件工具提示bokeh stacked ch

2024-04-18 01:42:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试根据堆叠图表上的数据创建自定义悬停。在下面的示例中,如果用户将鼠标悬停在“cat1”上,则应返回“cat1\u text”;对于“cat2”和“cat3”,应相应地返回“cat2\u text”和“cat3\u text”。你知道吗

对于工具提示,由于$name将返回'cat1'、'cat2'或'cat3',我认为通过添加'\u text',将相应地调用该值(当然,这似乎不是python/bokeh的工作方式)。我也在考虑使用任何函数/索引调用,但不太确定如何这样做。好心的建议。非常感谢!你知道吗

category = ['cat1', 'cat2', 'cat3']

data = {'timeVal' : [0,1,2,3,4,5],
        'cat1'   : [2, 1, 4, 3, 2, 4],
        'cat2'   : [5, 3, 4, 2, 4, 6],
        'cat3'   : [3, 2, 4, 4, 5, 3],
        'cat1_text'   : ['a','b','c','d','e','f'],
        'cat2_text'   : ['a1','b1','c1','d1','e1','f1'],
        'cat3_text'   : ['a2','b2','c2','d2','e2','f2'],
}

toolTipArr = [
    ("name", "$name"),
    ("count", "@$name"),
    ("info", '@'+'$name'+'_text}')
]

p = figure(x_range=(startTime,endTime), plot_height=250, plot_width=1000, title="project",
           toolbar_location="right", tools="hover,pan,wheel_zoom,box_zoom,reset", 
           tooltips=toolTipArr)

p.vbar_stack(category, x='timeVal', width=2, color=colors, source=data,
             legend=[value(x) for x in category])

Tags: 数据textname示例dataplot图表width
2条回答

这是bigreddot使用CustomJSHover(适用于bokehv1.3.0)建议的实现:

from bokeh.core.properties import value
from bokeh.models import ColumnDataSource, CustomJSHover
from bokeh.plotting import figure, show

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]

data = {'fruits' : fruits,
        '2015'   : [2, 1, 4, 3, 2, 4],
        '2016'   : [5, 3, 4, 2, 4, 6],
        '2017'   : [3, 2, 4, 4, 5, 3]}

source = ColumnDataSource(data=data)

tooltips= [("name", "$name"), ("count", "@$name")]

p = figure(x_range=fruits, plot_height=350, title="Fruit Counts by Year",
           toolbar_location=None, tooltips=tooltips)

renderers = p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=source,
                         legend=[value(x) for x in years], name=years)

p.hover[0].tooltips.append(('info', '$name{custom}'))
p.hover[0].formatters = {'$name' : CustomJSHover(code = "return special_vars.name + '_text'")}

show(p)

再努力一点,就可以使用CustomJS回调(bokehv1.3.0)实现同样的效果:

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

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
colors = ["#c9d9d3", "#718dbf", "#e84d60"]

data = {'fruits' : fruits,
        '2015'   : [2, 1, 4, 3, 2, 4],
        '2016'   : [5, 3, 4, 2, 4, 6],
        '2017'   : [3, 2, 4, 4, 5, 3]}

source = ColumnDataSource(data=data)

p = figure(x_range=fruits, plot_height=350, title="Fruit Counts by Year",
           toolbar_location=None, tools="")

renderers = p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=source,
                         legend=[value(x) for x in years], name=years)

hover_code = "if (cb_data.index.indices.length > 0) { cb_obj.tooltips[2] = ['info', cb_obj.name + '_text'] }"

for renderer in renderers:
    p.add_tools (HoverTool(tooltips=[("name", "$name"),
                                     ("count", "@$name"),
                                     ("info", "@info"), ],
                           renderers=[renderer],
                           name = renderer.name,
                           callback = CustomJS(code = hover_code)))
show(p)

enter image description here

$name@$name是在任何其他文本操作之前首先计算的,所以这就是为什么上面的方法不起作用的原因。您需要使用^{}来实现这样的功能

custom = CustomJSHover(args=dict(source=source), code="""
    // use special_vars.name and use special_vars.indices to index in 
    // to the right column of source.data
""")

相关问题 更多 >