问题描述
我可以在Django上使用bokeh在Choropleth地图上显示GeoJson数据,但无法基于滑块输入来更新源数据框。使用CustomJS回调,似乎可以从JS内修改简单数据框,但是我们如何查询从Django模型获得的数据框?
下面是我的代码,任何帮助将不胜感激。
#this is the view
def dataviz(request):
#load the shapefile for world map
gdf = geopandas.read_file('/home/naveed/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp')[['ADMIN','ADM0_A3','geometry']]
#Rename columns.
gdf.columns = ['country','country_code','geometry']
#function to return geojson data based on date
def geojson_data(day):
if day == 10: day = datetime.date(2020,8,10)
df = pandas.DataFrame.from_records(OxCases.objects.filter(count_date=day).values_list())
df = df[[1,4]]
df.columns = ['confirmed_cases','country_code']
merged_df = gdf.merge(df,left_on='country_code',right_on='country_code')
geojson_data = json.dumps(json.loads(merged_df.to_json()))
return geojson_data
p = figure(title='My geo map',plot_width=1150,plot_height=500,tools="wheel_zoom,pan",background_fill_color='#FFFFFF')
p.toolbar.active_scroll = p.select_one(WheelZoomTool)
p.toolbar_location = None
p.xgrid.visible = False
p.ygrid.visible = False
hover = HoverTool(tooltips = [('','@country_code'),('','@confirmed_cases')],# callback = CustomJS(code = code_hover)
)
p.add_tools(hover)
palette = d3['Category20c'][12]
# palette = brewer['BuGn'][9]
#Reverse color order so that dark blue is highest obesity.
palette = palette[::-1]
#Instantiate LinearColorMapper that linearly maps numbers in a range,into a sequence of colors.
color_mapper = LinearColorMapper(palette = palette)
color_bar = ColorBar(color_mapper=color_mapper,label_standoff=10,width = 20,formatter=NumeralTickFormatter(format='0.0a'),border_line_color=None,location = (0,0),orientation = 'vertical')
day = date(2020,7,1)
source = GeoJSONDataSource(geojson = geojson_data(day))
p.patches('xs','ys',source = source,fill_color = {'field' :'confirmed_cases','transform' : color_mapper},line_color = '#585858',line_width = 0.25,fill_alpha = 0.8
)
p.add_layout(color_bar,'right')
#PROBLEM IS HERE
handler = CustomJS(args={'source':source},code="""
//need to change the 'day' value to query the dataframe in geojson_data function
source.geojson = geojson_data(10) //doesnt work obvIoUsly
//console.log('source data is ',source)
source.change.emit();
""")
slider = Slider(title = 'Date',start = 1,end = 30,step = 1,value = 5)
slider.js_on_change('value',handler)
layout = column(p,slider)
map_data = json.dumps(json_item(layout))
return render(request,'base.html',{'map_data': map_data})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)