
该项目是浙江大学地理空间数据库课程作业8:空间分析中,使用 flask + pyecharts 搭建的简单新冠肺炎疫情数据可视化交互分析平台的一部分,完整的实现包含疫情数据获取、态势感知、预测分析、舆情监测等任务;
包含完整代码、数据集和实现的github地址: https://github.com/yunwei37/COVID-19-NLP-vis
项目分析报告已部署到网页端,可点击http://flask.yunwei123.tech/进行查看,数据已更新到6.17
动态交互展示的世界地图:

除了世界地图还可以动态展示中国地图:(这里就暂时没有录屏啦qwq)

由于篇幅限制,这里讨论一下具体的函数实现方式,完整代码可在github中获取:
实现的基本原理是采用ajax方式,通过页面响应向后端flask发送请求,用pyecharts渲染新的地图,然后返回前端进行动态刷新:
先定义一个渲染当前国内确诊人数的函数,返回pyecharts图表:
import time, json
import pandas as pd
from pyecharts.charts import Map
import pyecharts.options as opts
def render_mapcountChina_rate(dateId):
data = pd.read_csv(n)
data = data[data['dateId'] == dateId]
#print(data['currentConfirmedCount'])
list_data = zip(list(data['provinceShortName']), list((data['deadCount']*1000 // data['confirmedCount'])/10))
# [('湖北', 48206), ('广东', 1241), ('河南', 1169), ('浙江', 1145), ..., ('澳门', 10), ('西藏', 1)]
c = (
Map()
.add('', list_data, 'china')
.set_global_opts(
title_opts=opts.TitleOpts(title='全国疫情分布图(死亡率)'+str(dateId)),
visualmap_opts=opts.VisualMapOpts(is_show=True,
split_number=6,
is_piecewise=True, # 是否为分段型
pos_top='center',
pieces=[
{'min': 40, 'color': '#7f1818'}, #不指定 max
{'min': 20, 'max': 40},
{'min': 10, 'max': 20},
{'min': 8, 'max': 10},
{'min': 4, 'max': 8},
{'min': 1, 'max': 4},
{'min': 0.6, 'max': 1},
{'min': 0.1, 'max': 0.5},
{'min': 0, 'max': 0}
]),
)
)
return c
def render_mapcountChina(dateId,type = 0):
if(type==0):
return render_mapcountChina_current(dateId)
elif(type==1):
return render_mapcountChina_death(dateId)
elif(type==2):
return render_mapcountChina_rate(dateId)在flask中调用该函数:
@app.route("/chinamap",methods=['POST', 'GET'])
def get_china_map():
if request.method == 'GET':
maptype = int(request.args.get('type', ''))
i = int(request.args.get('index', ''))
print(maptype)
print(i)
return render_mapcountChina(date_list[int(i)],maptype).dump_options_with_quotes()在html页面中添加:
<div id="current-maps" >
<div>
<label>请选择地图类型: </label>
<select name="select-map" id="mapselecter">
<option value="0" >现存确诊人数</option>
<option value="1" >累计死亡人数</option>
<option value="2" >死亡率(死亡/确诊)</option>
</select>
</div>
<div>
<label>拖动滑块即可切换日期:</label>
<input id='slider' style="width: 400px;vertical-align: middle;" type='range' min='0' max='121' step='1'/>
</div>
<div id="worldMap" class="maps" style="width:800px; height:600px;display: inline-block;"></div>
<div id="chinaMap" class="maps" style="width:800px; height:600px;display: inline-block;"></div>
</div>添加js代码:
document.getElementById('slider').onchange = function changeDate(){
fetchworldMapData(worldmap);
fetchchinaMapData(chinamap);
}
document.getElementById('mapselecter').onchange = function changeCountry(){
fetchworldMapData(worldmap);
fetchchinaMapData(chinamap);
}
function fetchworldMapData(chart) {
$.ajax({
type: "GET",
url: "/worldmap",
dataType: "json",
data: "type="+document.getElementById('mapselecter').value+'&'+"index="+document.getElementById('slider').value,
success: function (result) {
chart.setOption(result);
}
});
}
function fetchchinaMapData(chart) {
$.ajax({
type: "GET",
url: "/chinamap",
dataType: "json",
data: "type="+document.getElementById('mapselecter').value+'&'+"index="+document.getElementById('slider').value,
success: function (result) {
chart.setOption(result);
}
});
}通过交互分析可以发现:
总体来看: