Python 条件工具提示bokeh堆叠图表
我正在尝试根据堆叠图表上的数据创建自定义悬停。在下面的示例中,如果用户将鼠标悬停在“cat1”上,则应返回“cat1_text”“cat2”和“cat3”对应的“cat2”和“cat3”文本 对于工具提示,由于$name将返回'cat1'、'cat2'或'cat3',我认为通过添加'u text',将相应地调用该值(当然,这似乎不是python/bokeh的工作方式)。我也在考虑使用任何函数/索引调用,但不太确定如何这样做。好心的建议。非常感谢Python 条件工具提示bokeh堆叠图表,python,bar-chart,bokeh,stacked-chart,Python,Bar Chart,Bokeh,Stacked Chart,我正在尝试根据堆叠图表上的数据创建自定义悬停。在下面的示例中,如果用户将鼠标悬停在“cat1”上,则应返回“cat1_text”“cat2”和“cat3”对应的“cat2”和“cat3”文本 对于工具提示,由于$name将返回'cat1'、'cat2'或'cat3',我认为通过添加'u text',将相应地调用该值(当然,这似乎不是python/bokeh的工作方式)。我也在考虑使用任何函数/索引调用,但不太确定如何这样做。好心的建议。非常感谢 category = ['cat1', 'cat2
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])
$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
""")
$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
""")
这是使用
CustomJSHover
建议的实现(适用于Bokeh v1.3.0):
再努力一点,您就可以使用CustomJS回调(BokehV1.3.0)实现同样的效果:
这是使用
CustomJSHover
建议的实现(适用于Bokeh v1.3.0):
再努力一点,您就可以使用CustomJS回调(BokehV1.3.0)实现同样的效果:
谢谢!我使用了您获取索引的方法来获取包含上面tony的实现代码的列。下面是供任何人将来参考的解决方案:hover_code=“”(cb_data.index.index.length>0){idx=cb_data.index.index[0]row=cb_obj.name+“_text”tooltipVal=source.data[row][idx]cb u obj.tooltips[2]=['info',tooltipVal.toString()]”@bigreddot:在CustomJS回调中可以访问
special_vars
吗?不,special_vars
只在命中测试/悬停的环境中存在并有意义,但是CustomJS
可以在与此无关的所有情况下使用CustomJSHover
是专门创建的一个可以访问它们的地方(因为CustomJSHover
仅用于命中测试/悬停),谢谢!我使用了您获取索引的方法来获取包含上面tony的实现代码的列。下面是供任何人将来参考的解决方案:hover_code=“”(cb_data.index.index.length>0){idx=cb_data.index.index[0]row=cb_obj.name+“_text”tooltipVal=source.data[row][idx]cb u obj.tooltips[2]=['info',tooltipVal.toString()]”@bigreddot:在CustomJS回调中可以访问special_vars
吗?不,special_vars
只在命中测试/悬停的环境中存在并有意义,但是CustomJS
可以在与此无关的所有情况下使用CustomJSHover
是专门创建的一个可以访问它们的地方(因为CustomJSHover
仅用于命中测试/悬停)
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)