I am trying to generate an interactive plot using Bokeh. I have the following incomplete code
# data (data that we are going to interact in the plot)
src = ColumnDataSource(data = cbg[['cbg_id', 'color', 'alpha', 'selected']] )
# view
view = CDSView(source = src, filters=[BooleanFilter(src.data['selected'])])
# callback
callback = CustomJS( args = dict(source = src, view = view), code =
"""
...
// trigger update of data source
source.change.emit();
""")
# figure
p = figure(plot_height=500,
plot_width=450,
y_range = Range1d(min_northing, max_northing, bounds='auto'),
x_range = Range1d(min_easting, max_easting, bounds='auto'),
x_axis_type = 'mercator',
y_axis_type = 'mercator',
tools = 'pan, reset, save',
match_aspect= True
)
# tools
hover = HoverTool(tooltips = [ ('cbg','@{cbg_id}')], mode='mouse')
box_zoom = BoxZoomTool(match_aspect= True)
wheel_zoom = WheelZoomTool(zoom_on_axis= False)
taptool = TapTool(callback = callback)
p.add_tools(hover, box_zoom, wheel_zoom, taptool)
# map
tile_provider = get_provider('OSM')
p.add_tile(tile_provider)
# polygons
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
cbgroups = p.patches('xs','ys', source = geo_src,
fill_color = 'color', # 'blue'
fill_alpha = 'alpha', # 0.2
line_color = 'black',
line_width = 0.25)
show(p)
cbg is a pandas dataframe where:
-
cbg_id
is a numeral -
color
is text e.g. gray -
alpha
is value 0-1. -
selected
is boolean
I am trying to write the appropriate javascript callback that will allow me to store in a variable the cbg_id
value of a patch(= polygon) after clicking on it with the mouse. Everything seems to work fine but I just stuck as to how to achieve this.