$name
和@$name
在进行任何其他文本操作之前先被评估,因此这就是上述方法行不通的原因。您将需要使用CustomJSHover
来完成类似的工作
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
""")
,
这是bigreddot使用CustomJSHover
建议的实现(适用于Bokeh v1.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,6],'2017' : [3,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回调(Bokeh v1.3.0)实现相同的目标:
from bokeh.core.properties import value
from bokeh.models import ColumnDataSource,HoverTool,CustomJS
from bokeh.plotting import figure,3]}
source = ColumnDataSource(data=data)
p = figure(x_range=fruits,tools="")
renderers = p.vbar_stack(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"),("info","@info"),],renderers=[renderer],name = renderer.name,callback = CustomJS(code = hover_code)))
show(p)
本文链接:https://www.f2er.com/3157493.html