如何使用 js 回调返回散点的索引?

问题描述 投票:0回答:0

我正在尝试创建一个 JS 回调以返回我创建的散点图的索引。但似乎输出总是“未定义”。谁能帮我看看我的代码有什么问题?

这是一个简短的例子:

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


# create some data
source = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5],
                                    y=[6, 7, 8, 9, 10],color=["navy", "orange", "olive", "firebrick", "gold"]))

# create a plot with circles
plot = figure()
plot.circle('x', 'y', size=10, source=source, color = 'color')

# create the JavaScript callback
callback = CustomJS(args=dict(source=source), code="""  
    
    // get the index of the selected point
    var index = source.selected.indices[0];
    
    // alert the index
    alert(index);
""")

# add the callback to the plot
plot.js_on_event('tap', callback)

# show the plot
show(plot)
bokeh bokehjs
© www.soinside.com 2019 - 2024. All rights reserved.