python - Bokeh TapTool 运行自定义 JS 和工具提示
问题描述
我在 Bokeh 中有一个应用程序,显示一张带有不同圆圈集的地图。我希望能够使用 TapTool 在点击一个圆圈时显示一个小显示,类似于使用 HoverTool 显示的显示,运行 JS 代码并使用自定义 HTML 模板。在录制散景图的元素时,我在 Fixed HoverTool TOOLTIPS的答案中找到了一个解决方案,其输出如下
然而,它的行为并不像预期的那样。信息不会像 HoverTool 那样使用 TapTool 显示在选定的圆圈旁边,而是显示在图的右侧,如此处所示 。
我知道对此有一个很好的解释,比如正在使用的 Bokeh 版本(我尝试了 1.0.4、1.4.0 和 2.0.0,具有相同的输出)或其他一些配置问题,但我找不到它。我也试过不同的浏览器,以防万一,但输出是一样的。
解决方案
问题是它Div
最终被包裹在另一个div
向右移动的地方,因为它Row
与主情节属于同一情节。
以下是适用于 Bokeh 2.0.0 的更新代码段:
div = Div(text='<div id="tooltip"></div>')
code = ''' if (cb_data.source.selected.indices.length > 0){
const selected_index = cb_data.source.selected.indices[0];
const tooltip = document.getElementById("tooltip");
const tooltip_wrapper = tooltip.parentElement.parentElement;
if (tooltip_wrapper.className !== 'bk')
throw new Error('Unable to find the correct tooltip wrapper element');
tooltip_wrapper.style.left = Number(cb_data.geometries.sx) + Number(20) + 'px';
tooltip_wrapper.style.top = Number(cb_data.geometries.sy) - Number(20) + 'px';
tooltip.style.display = 'block';
tp = tp.replace('@imgs', cb_data.source.data.imgs[selected_index]);
tp = tp.replace('@desc', cb_data.source.data.desc[selected_index]);
tp = tp.replace('@fonts{safe}', cb_data.source.data.fonts[selected_index]);
tp = tp.replace('$index', selected_index);
tp = tp.replace('$x', Math.round(cb_data.geometries.x));
tp = tp.replace('$y', Math.round(cb_data.geometries.y));
tooltip.innerHTML = tp;
} '''
p.select(TapTool).callback = CustomJS(args={'circles': circles, 'tp': TOOLTIPS}, code=code)
推荐阅读
- jquery - jQuery click() 函数在我的 Linux 机器上的 FireFox 中不起作用,但在 Windows 中起作用
- c# - 如何显示用户请求的位数?
- javascript - 从多个位置复制或从文件附加到文件
- asp.net-mvc - 优化完成对本地托管的外部服务 (Solr) 的请求的时间
- javascript - 打开新的浏览器窗口
- php - 在 laravel Horizon 中动态创建队列并在该队列上调度作业
- python - 为具有相同 django 项目的不同应用程序问题导入模型
- excel - 创建循环搜索
- java - 在 Eclipse 中导出新项目使用以前项目/导出中的值
- react-native - 如何设置 react-native-navigation 抽屉的主父宽度