# Get map tiles
tile_provider = get_provider(CARTODBPOSITRON)
# Create ColumnDataSource from coordinates, sizes and predicted means
source = ColumnDataSource(dict(x=x_filtered, y=y_filtered, sizes=sizes_filtered, means=mean_filtered,
vars=np.sqrt(vars_filtered), alphas=alphas, lat=lat_filtered, lon=lon_filtered))
# Create linear colour map for pollution data
mapper = linear_cmap(field_name='means', palette=YlOrBr[9][::-1] ,low=0 ,high=max(mean_filtered)[0])
# Tooltips
tooltips = [("Lat/long", "@lat, @lon"),
("Predicted PM2.5", "@means"),
("Predicted Variance", "@vars")]
# Plot figure and add tiles
scaled_map = figure(title=None, x_range=(min(x), max(x)), y_range=(min(y), max(y)),
x_axis_type="mercator", y_axis_type="mercator", tooltips=tooltips, tools = "pan,wheel_zoom,box_zoom,reset,hover")
scaled_map.add_tile(tile_provider)
# Create and plot glyphs for predictions
glyph = Circle(x="x", y="y", radius="sizes", line_color="white", fill_color=mapper, fill_alpha=0.8, line_width=1,
line_alpha=1)
scaled_map.add_glyph(source, glyph)
# Add colour bar
color_bar = ColorBar(color_mapper=mapper['transform'], width=8, location=(0,0))
scaled_map.add_layout(color_bar, 'right')
callback = CustomJS(args=dict(source=source, xr=scaled_map.x_range, yr=scaled_map.y_range), code="""
var xstart = xr.start;
var xend = xr.end;
var ystart = yr.start;
var yend = yr.end;
var gridsize = 20;
var url = "http://127.0.0.1:5000/?xstart="+xstart+"&xend="+xend+"&ystart="+ystart+"¥d="+yend+"&gridsize="+gridsize;
var data = source.data;
var x = data["x"];
var y = data["y"];
var means = data["means"];
$.getJSON(url, function(request){
x = request.xgrid;
y = request.ygrid;
means = request.means;});
source.change.emit();
""")
scaled_map.js_on_event(LODEnd, callback)
output_notebook()
show(scaled_map)
# Save the plot by passing the plot -object and output path
#save(obj=scaled_map, filename=outfp)
Hi, I’m trying to get a CustomJS script to update a grid from an API when I zoom in. I’m using getJSON to call the API endpoint using the current x and y ranges and returning an array. I think I have the callback set up correctly (but possibly not, this is my first time using javascript) and it’s pinging the server but I’m not getting new points. Can anyone help? I feel like I’ve missed something simple but struggling to spot it!
Many Thanks