散景 TapTool,如何使用多个 TapTool

时间:2021-05-11 15:42:14

标签: python jupyter-notebook bokeh

我有两个数据源,想用 TapTool 分别选择它们。当我点击圆圈时,它会将我重定向到一个网页。这适用于一个情节,但我无法让它适用于两个不同的情节。 我试过 add_tool 函数,但它根本不起作用。

source1 = ColumnDataSource(data=dict(
    x=df1["xPos"].values,
    y=df1["yPos"].values,
    url=df1["Url"].values
source2 = ColumnDataSource(data=dict(
    x=df2["xPos"].values,
    y=df2["yPos"].values,
    url=df2["Url"].values

plot = figure(width= 500, height= 500, tools="pan,wheel_zoom,save,reset,tap", 
              x_range=Range1d(-10.1, 10.1), y_range=Range1d(-10.1, 10.1), title='TapToolTest')

plot1 = plot.circle('x', 'y', size=20, source=source1)
plot2 = plot.circle('x', 'y', size=10, source=source2)

url = "@url"
#taptool = plot.select_one(TapTool)
#taptool.renderers = [plot1]
#taptool.callback = OpenURL(url=url) #This methode works, but only for plot 1

plot.add_tools(TapTool(renderers=[plot1], callback = OpenURL(url=url)))
plot.add_tools(TapTool(renderers=[plot2], callback = OpenURL(url=url)))

show(plot)

1 个答案:

答案 0 :(得分:1)

在您的示例中,您有 1 个绘图和 2 个渲染器。下面的代码适用于所有圈子(从 Bokeh v2.1.1 开始):

from bokeh.models import ColumnDataSource, OpenURL, TapTool
from bokeh.plotting import show, figure

p = figure(plot_width=400, plot_height=400,
           tools="tap", title="Click the Dots")

source1 = ColumnDataSource(data=dict(
    x=[1, 2, 3, ],
    y=[2, 5, 8, ],
    color=["navy", "orange", "olive", ]
    ))
source2 = ColumnDataSource(data=dict(
    x=[4, 5, 6],
    y=[2, 7, 4],
    color=["firebrick", "gold", "skyblue"]
    ))

p.circle('x', 'y', color='color', size=20, source=source1)
p.circle('x', 'y', color='color', size=30, source=source2)

url = "http://www.colors.commutercreative.com/@color/"
taptool = p.select(type=TapTool)
taptool.callback = OpenURL(url=url)

show(p)
相关问题