You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bokeh交互式地图实现整国选区同步选中的高效方案问询

解决Bokeh Server中点击多Patch国家时选中状态延迟的问题

你的实现思路没问题,但延迟主要来自两个核心点:一是每次点击都要临时通过DataFrame查询同国家的索引,二是selected事件触发时,TapTool已经先选中了单个Patch,你的回调再去更新选中状态,导致视觉上出现明显的切换延迟。下面给你更高效的优化方案:

优化核心思路

  • 预存国家-索引映射:提前把每个国家对应的所有Patch索引存入字典,避免每次点击都去查询DataFrame,彻底省去重复计算的开销。
  • 简化回调逻辑:直接调用预存的映射快速获取目标索引,同时优化选中状态的更新逻辑,减少不必要的判断步骤。

优化后的代码

from bokeh.models import ColumnDataSource, Patches, Selection
from bokeh.plotting import figure
from bokeh.layouts import row
from bokeh.io import curdoc
import pandas as pd

# 模拟数据
x = [[5,2,4], [3,5,6], [6,9,7], [8,7,6]]
y = [[5,3,2], [6,5,8], [3,1,6], [1,2,1]]
country = ['A', 'A', 'B', 'B']
id = [0,1,2,3]

df = pd.DataFrame(data=dict(x=x, y=y, country=country, id=id))
source = ColumnDataSource(df)

# 预构建国家到索引列表的映射,初始化时一次性计算完成
country_to_indices = df.groupby('country')['id'].apply(list).to_dict()

p = figure(tools="tap")
renderer = p.patches('x', 'y', source=source)

def my_tap_handler(attr, old, new):
    # 仅处理有选中项的情况
    if new.indices:
        # 获取点击的Patch对应的国家
        clicked_country = source.data['country'][new.indices[0]]
        # 直接从预存映射中取出同国家的所有索引
        target_indices = country_to_indices[clicked_country]
        # 直接更新选中状态,跳过中间过渡
        source.selected = Selection(indices=target_indices)

# 绑定回调事件
renderer.data_source.on_change("selected", my_tap_handler)

curdoc().add_root(row(p, width=800))

额外进阶优化(纯前端无延迟)

如果你的交互不需要依赖Python后端的其他逻辑,推荐使用CustomJS回调,让逻辑在浏览器端执行,几乎可以消除延迟:

from bokeh.models import CustomJS

# 把预存的映射转成JS可识别的格式
country_indices_js = {k: v for k, v in country_to_indices.items()}

# 替换Python回调为前端JS回调
renderer.data_source.selected.js_on_change('indices', CustomJS(
    args=dict(source=source, country_map=country_indices_js),
    code="""
        const new_indices = cb_obj.indices;
        if (new_indices.length > 0) {
            const clicked_country = source.data['country'][new_indices[0]];
            const target_indices = country_map[clicked_country];
            source.selected.indices = target_indices;
        }
    """
))

这个版本完全在前端处理选中逻辑,没有后端通信的开销,延迟会极低,适合纯交互类的地图场景。

内容的提问来源于stack exchange,提问作者otwtm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:58:04