gpt4 book ai didi

javascript - Python Bokeh CustomJS : Debugging a JavaScript callback for the Taping-Tool

转载 作者:行者123 更新时间:2023-11-30 14:09:19 33 4
gpt4 key购买 nike

我正在使用 Python 3.6.2Bokeh 1.0.4 在我的绘图中创建自定义 JavaScript 回调。

通过点击图中的一个点,我希望突出显示 id 列中共享相同属性的所有点。

使用 JavaScript 遍历所有数据点并在 ColumnDataSource 对象中操作相应的“选定”属性应该可以解决问题。不幸的是,我不知道如何更正此代码。

# Import packages
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CustomJS, HoverTool, TapTool


# Create the data for the points
x = [0, 1, 2, 3]
y = [0, 1, 0, 1]
ids = ['A','B','A','B']

data = {'x':x, 'y':y, 'id':ids}
source = ColumnDataSource(data)


# Add tools to the plot
tap = TapTool()
hover = HoverTool(tooltips=[("X", "@x"),
("Y", "@y"),
("ID", "@id")])


# Create a plotting figure
p = figure(plot_width=400, plot_height=400, tools=[tap,hover])


# Code for the callback
code = """
// Set column name to select similar glyphs
var column = 'id';

// Get data from ColumnDataSource
var data = source.data;

// Get indices array of all selected items
var selected = source.selected.indices;

// Array to store glyph-indices to highlight
var select_inds = [];


// Check if only a single glyph is selected
if(selected.length==1){

// Get the value of the column to find similar attributes/glyphs
attribute_value = data[column][selected[0]];

// Iterate over all entries in the ColumnDataSource
for (var i=0; i<data[column].length; ++i){

// Check if items have the same attribute
if(data[column][i]==attribute_value){

// Add index to selected list
select_inds.push(i);
}
}
}

// Set selected glyphs in ColumnDataSource
source.selected.indices = select_inds;

// Save changes to ColumnDataSource
source.change.emit();
"""


# Create a CustomJS callback with the code and the data
callback = CustomJS(args={'source':source}, code=code)

# Add the callback to the ColumnDataSource
source.callback=callback


# Plots circles
p.circle('x', 'y', source=source, size=25, color='blue', alpha=1, hover_color='black', hover_alpha=1)

# Show plot
show(p)

older version Bokeh 0.13.0 无法解决我的问题。

最佳答案

你快到了!必须将回调添加到 TapTool 而不是 ColumnDataSource

# Import packages
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, CustomJS, HoverTool, TapTool


# Create the data for the points
x = [0, 1, 2, 3]
y = [0, 1, 0, 1]
ids = ['A','B','A','B']

# Generate data source for the visualization
data = {'x':x, 'y':y, 'id':ids}
source = ColumnDataSource(data)


# Add tools to the plot
tap = TapTool()
hover = HoverTool(tooltips=[("X", "@x"),
("Y", "@y"),
("ID", "@id")])


# Create a plotting figure
p = figure(plot_width=400, plot_height=400, tools=[tap,hover])


# Code for the callback
code = """
// Set column name to select similar glyphs
var column = 'id';

// Get data from ColumnDataSource
var data = source.data;

// Get indices array of all selected items
var selected = source.selected.indices;

// Array to store glyph-indices to highlight
var select_inds = [];

// Check if only a single glyph is selected
if(selected.length==1){

// Get the value of the column to find similar attributes/glyphs
var attribute_value = data[column][selected[0]];

// Iterate over all entries in the ColumnDataSource
for (var i=0; i<data[column].length; ++i){

// Check if items have the same attribute
if(data[column][i]==attribute_value){

// Add index to selected list
select_inds.push(i);
}
}
}

// Set selected glyphs in ColumnDataSource
source.selected.indices = select_inds;

// Save changes to ColumnDataSource
source.change.emit();
"""


# Create a CustomJS callback with the code and the data
callback = CustomJS(args={'source':source}, code=code)

# Add the callback to the taptool
tap.callback=callback


# Plots circles
p.circle('x', 'y', source=source, size=25, color='blue', alpha=1, hover_color='black', hover_alpha=1)

# Show plot
show(p)

关于javascript - Python Bokeh CustomJS : Debugging a JavaScript callback for the Taping-Tool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54768576/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com