def index_bar_every(): df = pd.read_csv('./static/data/renjunGDP.csv') tl = Timeline() for i in range(2010, 2018): c = (Map().add( "世界地图", [(city[i], j) for i, j in zip(list(df['Country Name']), list(df['{}年'.format( i)])) if city.get(i, None)], "world").set_series_opts(label_opts=opts.LabelOpts( is_show=False)).set_global_opts( title_opts=opts.TitleOpts(title="{}各国人均GDP".format(i)), visualmap_opts=opts.VisualMapOpts(max_=80), )) tl.add(c, "{}年".format(i)) return render_template( 'index.html', myechart=tl.render_embed(), result=df.values.tolist(), a=1, data_x=df.columns.values.tolist()[1:], text1='''通过贫困率和贫困人口的两张地图对比可得,地图都有类似的点 两张地图颜色分布和变化趋势大致相同, 可以得出贫困率跟贫困人口相关。 两份数据图的中东地区数据是都偏大的随着年份的增加,变化都不是很大 我们还可以看出随着年份的增加,尽管全球总体贫困率有所下降,尤其是在中国和拉丁美洲的多数地区, 但非洲和部分亚洲地区的贫困率仍居高不下。 通过各国人均GDP和贫困率的两张地图对比可得到: 人均GDP和贫困率有一定的关系,欧洲有些高收入国家贫困率的数据也不低, 说明人均GDP与贫困率的关系不大,但是也有一定的关系。看人均GDP和贫困人口,不能全面衡量和比较国与国之间贫困率的差异贫富差距和社会保障,是决定国家贫困率的另外两个重要因素。''' )
def index_bar_every_1_tp(): df = pd.read_csv("./static/data/lvyou.csv") df2 = pd.read_csv("./static/data/gdp.csv") x = list(df.地区) tl = Timeline() for i in range(2013, 2018): bar = ( Bar() .add_xaxis(x) .add_yaxis("世界各地区旅游总收入", list(df["{}".format(i)])) .add_yaxis("世界各地区GDP总量", list(df2["{}".format(i)])) .reversal_axis() .set_global_opts(title_opts=opts.TitleOpts("{}年世界各地区旅游总收入与GDP总量对比".format(i)), datazoom_opts=opts.DataZoomOpts(orient="vertical")) .set_series_opts( label_opts=opts.LabelOpts(is_show=False), ) ) tl.add(bar, "{}年".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 总体上看世界各地区旅游总收入是和与GDP总量成正比的,从图表数据看两者都有些增长,但增长有些波动总体上还是趋向平稳的 ''')
def index_bar_every_4(): df = pd.read_csv("./static/data/wuhai.csv") tl = Timeline() for i in range(2009, 2018): map0 = (Map().add("垃圾无害化清理率(单位:百分比)", list(zip(list(df.地区), list(df["{}".format(i)]))), "china", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}各省垃圾无害化清理率".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts( min_=min(list(df["{}".format(i)])), max_=max(list(df["{}".format(i)]))), )) tl.add(map0, "{}".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 垃圾无害化处理的工艺主要有:卫生填埋、堆肥和焚烧三种。 由垃圾无害化处理率地图可见,一开始,各省的处理率都十分不平均,高的高达97,低的低达29,西藏在2016年之前甚至没有数据。直到2016年后,全国的垃圾无害化处理都基本达到了90%。 ''', text1='''我对影响垃圾无害化处理的因素的猜测如下: 1.高纬度的地方天气寒冷,卫生填埋在地下分解的时间很长,因此导致无害化处理的进度缓慢。 2.西藏等地区,并不进行农业,堆肥的无害化处理方式并不适合此些地区,也将导致其无法无害化处理垃圾 3.人口密度高的地区无法进行焚烧、填埋,因为人类生活用地面积就极大''')
def get_index(): timeline = Timeline( # init_opts=opts.InitOpts(width="1600px", height="900px", theme=ThemeType.DARK) init_opts=opts.InitOpts( width="100vw", height="100vh", theme=ThemeType.DARK)) for day in date_list: g = get_week_chart(date=day) timeline.add(g, time_point=day) timeline.add_schema( orient="vertical", is_auto_play=True, is_inverse=True, play_interval=2000, pos_left="null", pos_right="20", pos_top="10", pos_bottom="10", width="60", label_opts=opts.LabelOpts(is_show=False, color="#fff"), ) # timeline.render("china_gdp_from_1993_to_2018.html") return render_template( "demo.html", # passed_data=msg_data, myechart=timeline.render_embed(), )
def index_bar(): df = pd.read_csv("./static/data/country_females.csv") tl = Timeline() for i in range(2014, 2018): map0 = (Map().add("女性患病率", list(zip(list(df.Country), list(df["{}".format(i)]))), "world", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}年女性患病率".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=10, max_=20))) tl.add(map0, "{}年".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 女性心理健康患病率在国家和地区间的分布大致同男性的差不多。 经济较发达的欧洲、美洲及大洋洲的心理健康患病率的比例较高 且四年间的变化趋势也趋于稳定,这与经济和社会发展水平有着密切联系, 随着社会医疗保障体系的完善,心理健康患病率正趋于稳定和下降趋势。''')
def index_bar_every(): df = pd.read_csv("./static/data/renkou.csv") tl = Timeline() for i in range(2009, 2018): map0 = (Map().add("垃圾清运量(单位:万吨)", list(zip(list(df.地区), list(df["{}".format(i)]))), "china", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}各省垃圾清运量".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts( min_=min(list(df["{}".format(i)])), max_=max(list(df["{}".format(i)]))), )) tl.add(map0, "{}".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 由上可见,广东的垃圾清运量十分巨大,在2009-2018期间,广东的垃圾清运量始终是占比最高的。 除此之外,随着年份的增加,其他省份的垃圾清运量也在上升,如浙江、山东,已经从绿色变成了黄色 ''', text1='''我对影响垃圾无害化处理的因素的猜测如下: 1.高纬度的地方天气寒冷,卫生填埋在地下分解的时间很长,因此导致无害化处理的进度缓慢。 2.西藏等地区,并不进行农业,堆肥的无害化处理方式并不适合此些地区,也将导致其无法无害化处理垃圾 3.人口密度高的地区无法进行焚烧、填埋,因为人类生活用地面积就极大''')
def index_word(): df = pd.read_csv("./static/data/country_males.csv") tl = Timeline() for i in range(2014, 2018): map0 = (Map().add("男性患病率", list(zip(list(df.Country), list(df["{}".format(i)]))), "world", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}年女性患病率".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=10, max_=20))) tl.add(map0, "{}年".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 由地图可知2014年-2017年亚洲、俄罗斯及部门南美洲地区的男性心理健康患病率 比欧洲、美国、澳洲要低,由此可见发达国家和地区患心理健康疾病的比例也比欠 发达的国家和地区高。且四年间的变化率不大,相对于较稳定趋势。''')
def index_bar_every_1_tp(): df = pd.read_csv("./static/data/GDP.csv") tl = Timeline() for i in range(2013, 2019): map0 = ( Map() .add( "GDP生产值(单位:万亿)", list(zip(list(df.地区), list(df["{}年".format(i)]))), "china", is_map_symbol_show=False ) .set_global_opts( title_opts=opts.TitleOpts(title="{}各省份GDP总值".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts(color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=1000, max_=80000), ) ) tl.add(map0, "{}年".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 从上面的模型中,我们可以看出近五年来,东南沿海地区的GDP生产总值在国内是较高的 以2018年为例,广东,浙江,山东三地的数值都要超过了80000。而西北地区的数值是偏低的 以2018年为例,西藏,青海,宁夏三地的数值都要低于10000 ''')
def wordY(): df = pd.read_csv("./static/data/greens.csv") prevention = request.args.get("city1", "emissions") tl = Timeline() for i in range(2014, 2018): map0 = (Map().add("城市绿化覆盖率", list( zip(list(df.province), list(df["{}年".format(i)]))), "china", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}年城市绿化覆盖率".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=30, max_=50))) tl.add(map0, "{}年".format(i)) return render_template( 'index.html', myechart=tl.render_embed(), text=''' 总体上2014年到2017年各省份的城市绿化覆盖率在增加极少的经济欠发达省份如西藏的城市绿化覆盖率在这近四年减少''', text1= '''由各年份城市绿化覆盖率图可以知道,经济越发达的省份城市绿化程度更好,如青海、西藏、新疆等省份经济欠发达的省份城市绿化程度相对比较差 东部沿海省份城市的城市绿化覆盖率最高,例如山东、江苏、浙江、福建、广东等省份''')
def index_bar_every_1(): df = pd.read_csv('./static/data/renjunGDP.csv', index_col=0) df1 = pd.read_csv('./static/data/pinkunlv.csv', index_col=0) df2 = pd.read_csv('./static/data/pinkunrenkou.csv', index_col=0) tl = Timeline() for i in range(2010, 2018): map0 = (Map().add( "各国贫困率跟贫困人口的关系", [(city[i], j) for i, j in zip(list(df2.index), list(df1['{}年'.format(i)])) if city.get(i, None)], "world", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}各国贫困率跟贫困人口的关系".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=-1, max_=11.5), )) tl.add(map0, "{}年".format(i)) return render_template( 'index.html', myechart=tl.render_embed(), result=df.values.tolist(), a=1, data_x=df.columns.values.tolist()[1:], text1='''通过贫困率和贫困人口的两张地图对比可得,地图都有类似的点 两张地图颜色分布和变化趋势大致相同, 可以得出贫困率跟贫困人口相关。 两份数据图的中东地区数据是都偏大的随着年份的增加,变化都不是很大 我们还可以看出随着年份的增加,尽管全球总体贫困率有所下降,尤其是在中国和拉丁美洲的多数地区, 但非洲和部分亚洲地区的贫困率仍居高不下。 通过各国人均GDP和贫困率的两张地图对比可得到: 人均GDP和贫困率有一定的关系,欧洲有些高收入国家贫困率的数据也不低,说明人均GDP与贫困率的关系不大,但是也有一定的关系。看人均GDP和贫困人口,不能全面衡量和比较国与国之间贫困率的差异贫富差距和社会保障 ,是决定国家贫困率的另外两个重要因素。''')
def index_bar_every_4(): prevention = request.args.get("city") df5 = pd.read_csv(r'./static/data/lvyou.csv') tl = Timeline() for i in range(2013, 2018): map0 = ( Map() .add( "", list(zip(list(df5.地区.unique()), list(df5["{}".format(i)]))), "world", is_map_symbol_show=False ) .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts( title_opts=opts.TitleOpts(title="{}年世界各地区旅游总收入".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts(color="black", font_size=16, font_style="italic")), visualmap_opts=opts.VisualMapOpts(max_=70000000000), ) ) tl.add(map0, "{}年".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 从上面图的走势,我们可以看出,世界各地区旅游总收入较多的国家分别是United States、France、Germany、China等国家。 ''')
def index_bar_every(): df1 = pd.read_csv("./static/data/dependency_ratio.csv", index_col="地区") tl = Timeline() for i in range(2014, 2019): map0 = (Map().add("抚养比", list(zip(list(df1.index), list(df1["{}年".format(i)]))), "china", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}抚养比".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=23, max_=51), )) tl.add(map0, "{}年".format(i)) return render_template('index.html', myechart=tl.render_embed(), text1=''' 其实,从中国整体来看,抚养比基本都是在逐年上升的; 根据最新一年(2018年)的分省抚养比数据来看: 贵州、山东、及其附近的省份【集中在地图中心区域的省份】的抚养比也是相对较高的; 结合上一个图表,我们可以得出结论:中国分省离婚率的上涨和抚养比是有一定关系的【抚养比:劳动力的抚养负担】 ''')
def index_bar(): df = pd.read_csv('./static/data/average_salary.csv', index_col=0, error_bad_lines=False) tl = Timeline() for i in range(2013, 2019): map0 = (Map().add( "average_salary", list(zip(list(df.index), list(df["{}".format(i)]))), "china", is_map_symbol_show=False).set_series_opts( label_opts=opts.LabelOpts(is_show=False)).set_global_opts( title_opts=opts.TitleOpts( title="{}年平均工资".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=20000, max_=100000), )) tl.add(map0, "{}".format(i)) return render_template( 'index.html', myechart=tl.render_embed(), text=''' 分析2013年到2018年人们生活水平的变化''', text1='''首先从地图轮播的图表来看,北京和上海始终处于高收入的地区,而西藏从2015年开始有非常明显的提升。 到了2017年,我国各地的平均年收入基本达到了六万左右,并且逐年递增。''')
def timeline_map_a() -> Timeline: tl = Timeline() for i in range(2014, 2018): map0 = ( Map() .add( "历年分省教育经费", list(zip(list(a.地区), list(a["{}".format(i)]))), "china", is_map_symbol_show=False ) .set_global_opts( title_opts=opts.TitleOpts(title="{}年分省教育经费".format(i), subtitle=""), visualmap_opts=opts.VisualMapOpts(min_=1000000, max_=30000000), )) tl.add(map0, "{}年".format(i)) return tl.render_embed()
def timeline_map_d() -> Timeline: tl = Timeline() for i in range(2014, 2019): map1 = ( Map() .add( "历年分省每10万人高中在校生数(人)", list(zip(list(d.地区), list(d["{}".format(i)]))), "china", is_map_symbol_show=False ) .set_global_opts( title_opts=opts.TitleOpts(title="{}年分省每10万人高中在校生数(人)".format(i), subtitle=""), visualmap_opts=opts.VisualMapOpts(min_=1000, max_=4000), ) ) tl.add(map1, "{}年".format(i)) return tl.render_embed()
def timeline_map_c() -> Timeline: tl = Timeline() for i in range(2009,2020): map0 = ( Map() .add( "女性劳动力参与率", (list(zip(list(dfc1.country),list(dfc1["{}".format(i)])))), "world",is_map_symbol_show = False ) .set_global_opts( title_opts=opts.TitleOpts(title="".format(i),subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts(color="blue",font_size=16,font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=0, max_=100,series_index=0), ) ) tl.add(map0, "{}".format(i)) return tl.render_embed()
def expend_map(): data = pd.read_csv('./data/分省人均支出.csv', encoding='gbk') c = Timeline() for i in range(2013, 2019): map = ( Map() .add("人均消费支出", list(zip(list(data.地区), list(data["{}".format(i)]))), "china", is_map_symbol_show=False) .set_global_opts( title_opts=opts.TitleOpts(title="{}各省人均消费支出对比".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts(color="blue", font_size=18)), visualmap_opts=opts.VisualMapOpts(min_=6000, max_=43351, series_index=0) ) ) c.add(map, "{}".format(i)) return c.render_embed()
def timeline_map() -> Timeline: tl = Timeline() for i in range(2010, 2020): map0 = (Map().add( "世界女性失业率", list(zip(list(失业_国家), list(df["{}".format(i)]))), "world", is_map_symbol_show=False).set_series_opts( label_opts=opts.LabelOpts(is_show=False)).set_global_opts( title_opts=opts.TitleOpts( title="世界女性失业率".format(i), subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=10, font_style="italic")), visualmap_opts=opts.VisualMapOpts(series_index=0, max_=30), )) tl.add(map0, "{}".format(i)) return tl.render_embed()
def index_scatter(): df = pd.read_csv("./static/data/data_1.csv") columns = df.columns.values.tolist() tl = Timeline() for i in range(2007, 2017): bar = (Bar().add_xaxis(columns[1:]).add_yaxis( "男性", list(df.iloc[0])[1:]).add_yaxis( "女性", list(df.iloc[1])[1:]).set_global_opts( title_opts=opts.TitleOpts("中国男女性心理健康障碍患病率".format(i)))) tl.add(bar, "{}年".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 由我们国家2007年到2017年男女性心理健康患病率对比可知,女性虽还是比男性患病率 比率高一点点,但两者已经不断趋平,且总体上患病率也逐年下降。这说明, 我们国家的医疗普及水平,以及对心理健康的关怀程度不断提升,让国民心理健康得到更多保障。''')
def Birth_map() -> Timeline: a = Timeline() for i in range(2010, 2018): map0 = (Map().add( "世界青春期女性生育率", list(zip(list(青春生育率.CountryName), list(青春生育率["{}".format(i)]))), "world", is_map_symbol_show=False).set_series_opts( label_opts=opts.LabelOpts(is_show=False)).set_global_opts( title_opts=opts.TitleOpts( title="世界青春期女性生育率".format(i), subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=10, font_style="italic")), visualmap_opts=opts.VisualMapOpts(series_index=0, max_=187), )) a.add(map0, "{}".format(i)) return a.render_embed()
def index_bar(): df = pd.read_csv("./static/data/laji.csv") tl = Timeline() for i in range(2009, 2018): pie = (Pie().add( "数值", list(zip(list(df.地区), list(df["{}".format(i)]))), rosetype="radius", radius=["30%", "55%"], ).set_global_opts(title_opts=opts.TitleOpts())) tl.add(pie, "{}".format(i)) return render_template( 'index.html', myechart=tl.render_embed(), text=''' 由上可见,人口的确是垃圾产量的一大原因。人口越多的地方,垃圾清运量越高,如广州,而西藏等人少的地方垃圾清运量很低''', text1='''我对影响垃圾无害化处理的因素的猜测如下: 1.高纬度的地方天气寒冷,卫生填埋在地下分解的时间很长,因此导致无害化处理的进度缓慢。 2.西藏等地区,并不进行农业,堆肥的无害化处理方式并不适合此些地区,也将导致其无法无害化处理垃圾 3.人口密度高的地区无法进行焚烧、填埋,因为人类生活用地面积就极大''')
def plot_timeline(): """ Timeline可以将多个图表制作成动画。 :return: """ # 初始化多组数据 attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] year = 5 start_year = 2018 num = len(attr) # 初始化时间线图形 timeline = Timeline(init_opts=opts.InitOpts(width='120%', height='300%')) # 时间轴添加配置项 timeline.add_schema(is_auto_play=False, is_loop_play=True, is_timeline_show=True, control_position='right', itemstyle_opts=opts.ItemStyleOpts(color='blue', opacity=0.8), play_interval=1000) for i in range(5): year_sales1 = [randint(10, 100) for _ in range(num)] year_sales2 = [randint(200, 500) for _ in range(num)] # 初始化 bar_temp = Bar(init_opts=opts.InitOpts()) # 加载数据 bar_temp.add_xaxis(xaxis_data=attr) bar_temp.add_yaxis(series_name=f'{start_year+i}年净销售额', y_axis=year_sales1) bar_temp.add_yaxis(series_name=f'{start_year+i}年实际销售额', y_axis=year_sales2) # bar配置项 bar_temp.set_global_opts(title_opts=opts.TitleOpts( title=f'{start_year + i} 销售额情况')) # 时间轮播图添加图形 timeline.add(bar_temp, f'{start_year + i}年营业额') # 输出时间线轮播图 return timeline.render_embed()
def timeline_bar(): cursor = connection.cursor() cursor.execute("""SELECT DATE_FORMAT( rev.create_time, '%Y-%m-%d' ) create_time, revt.`name`, count( * ) FROM resource_event rev LEFT JOIN resource_eventtype revt ON rev.event_type_id = revt.id GROUP BY create_time, event_type_id""") rows = cursor.fetchall() name_list = [] event_type_list = [] for row in rows: name = row[0] if name not in name_list: name_list.append(name) event_type = row[1] if event_type not in event_type_list: event_type_list.append(event_type) tl = Timeline(init_opts=opts.InitOpts(height="350px")) for day in name_list: bar = Bar() bar.add_xaxis(['违章记录']) for i in range(len(event_type_list)): event_type_name = event_type_list[i] num = 0 for row in rows: if day == row[0] and event_type_name == row[1]: num = row[2] break bar.add_yaxis(event_type_name, [num]) bar.set_global_opts(title_opts=opts.TitleOpts("{}".format(day))) tl.add(bar, "{}".format(day)) return tl.render_embed()
def index_bar(): df = pd.read_csv("./static/data/GDP.csv") tl = Timeline() for i in range(2013, 2019): pie = ( Pie() .add( "数值", list(zip(list(df.地区), list(df["{}年".format(i)]))), rosetype="radius", radius=["30%", "55%"], ) .set_global_opts(title_opts=opts.TitleOpts()) ) tl.add(pie, "{}年".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 从上面的模型中,我们可以看出近五年来,东南沿海地区的GDP生产总值在国内是较高的 以2018年为例,广东,浙江,山东三地的数值都要超过了80000。而西北地区的数值是偏低的 以2018年为例,西藏,青海,宁夏三地的数值都要低于10000''')
def query1controller(request): cursor = connection.cursor() # 第一步得到时间跨度,即minYear和maxYear cursor.execute( 'SELECT MIN("Date") AS "Min", MAX("Date") AS "Max" ' 'FROM "Data"' ) fetchResult = cursor.fetchone(); minYear, maxYear = int(fetchResult[0][0:4]), int(fetchResult[1][0:4]) # 上一步得到的结果格式为'YYYYMMDD',因此需要将年份提取出来并转换为int # 新建一个Timeline格式的图表 timeLine = Timeline() # 接下来需要创建每一年的Map格式的图表,添加到Timeline中 for year in range(minYear, maxYear + 1): curYear = str(year) + '%' # 执行原生SQL查询,最终得到的结果是当前年份的每个省份的平均AQI指数 # 具体的查询逻辑:1)首先在Data表中筛选出当前年份的所有的AQI数据,然后按照监测站点进行分组,计算每个分组的平均AQI指数,也就是每个站点一年的平均值 2)结合Monitors表,根据每个站点所在的城市,就可以汇总计算出每个城市的年平均AQI指数 # 3)最后再结合省份信息,汇总计算出每个省的年平均AQI指数 cursor.execute( 'SELECT "Provinces"."name" AS "Province", AVG(tmpres1."Value") AS "Value" ' 'FROM "Cities", "Provinces", ' '(SELECT "Monitors"."City", AVG(tmpres0."Value") AS "Value" ' 'FROM "Monitors", ' '(SELECT "Monitor" AS "ID", AVG("Value") AS "Value" ' 'FROM "Data" ' 'WHERE "Date" LIKE %s AND "Type" = %s ' 'GROUP BY "Monitor") tmpres0 ' 'WHERE "Monitors"."ID" = tmpres0."ID" ' 'GROUP BY "Monitors"."City") tmpres1 ' 'WHERE "Cities"."province" = "Provinces"."province" AND "Cities"."name" = tmpres1."City" ' 'GROUP BY "Provinces"."name"', [curYear, 'AQI'] ) fetchResult = cursor.fetchall() # 利用上述查询的数据创建一个Map格式的图表,具体的参数参考pyecharts的官网,在上面给出了详细的参数介绍 map = (Map() .add('AQI', fetchResult, "china", is_roam=False, zoom=1) .set_global_opts(title_opts=opts.TitleOpts(title = "{}年全国空气质量指数".format(year)), visualmap_opts=opts.VisualMapOpts(max_= 120, min_= 30),) ) # 最后将每个Map加入到Timeline中,生成带有时间轴的地图 timeLine.add(map, "{}年".format(year)) return HttpResponse(timeLine.render_embed()) # 这一步没有返回具体的html网页,而是通过调用render_embed()方法,将生成的html格式的Timeline图表显示在当前的url下
def index_bar_every_1_tp(): df = pd.read_csv("./static/data/renkou.csv") x = list(df.地区) tl = Timeline() for i in range(2009, 2018): bar = (Bar().add_xaxis(x).add_yaxis("各省垃圾清运量", list( df["{}".format(i)])).add_yaxis("各省人口", list( df["{}".format(i)])).reversal_axis().set_global_opts( title_opts=opts.TitleOpts("{}年各省产生垃圾与人口对比".format(i)), datazoom_opts=opts.DataZoomOpts( orient="vertical")).set_series_opts( label_opts=opts.LabelOpts(is_show=False), )) tl.add(bar, "{}".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 由上可见,人口的确是垃圾产量的一大原因。人口越多的地方,垃圾清运量越高,如广州,而西藏等人少的地方垃圾清运量很低 ''', text1='''我对影响垃圾无害化处理的因素的猜测如下: 1.高纬度的地方天气寒冷,卫生填埋在地下分解的时间很长,因此导致无害化处理的进度缓慢。 2.西藏等地区,并不进行农业,堆肥的无害化处理方式并不适合此些地区,也将导致其无法无害化处理垃圾 3.人口密度高的地区无法进行焚烧、填埋,因为人类生活用地面积就极大''')
def generate_timeline_html(time_data): tl = Timeline() tl.add_schema(is_auto_play=False, play_interval=500, is_loop_play=False) for date, bonds in time_data.items(): _date = datetime.datetime.strftime(date, '%Y-%m-%d') x = [] y = [] bonds = sorted(bonds.items(), key=lambda i: i[1], reverse=False) for name, money, in bonds: x.append(name) y.append(money) bar = ( Bar(init_opts=opts.InitOpts( theme=ThemeType.ROMANTIC)).add_xaxis(x).add_yaxis("价格", y) # .add_yaxis("投入资金", y) .reversal_axis().set_series_opts(label_opts=opts.LabelOpts( is_show=True, position='right'), ).set_global_opts( legend_opts=opts.LegendOpts(is_show=True), title_opts=opts.TitleOpts("{}可转债组合".format(_date)), )) tl.add(bar, "{}".format(_date)) return "<center>" + tl.render_embed('template.html', env) + "</center>"
def index_bar(): df2 = pd.read_csv("./static/data/gdp.csv") tl = Timeline() for i in range(2013, 2018): map0 = ( Map() .add( "", list(zip(list(df2.地区.unique()), list(df2["{}".format(i)]))), "world", is_map_symbol_show=False ) .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts( title_opts=opts.TitleOpts(title="{}年世界各地区GDP总量".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts(color="black", font_size=16, font_style="italic")), visualmap_opts=opts.VisualMapOpts(max_=15000000000000), ) ) tl.add(map0, "{}年".format(i)) return render_template('index.html', myechart=tl.render_embed(), text=''' 从上面的模型中,我们可以看出近五年来,东南沿海地区的GDP生产总值在国内是较高的 以2018年为例,广东,浙江,山东三地的数值都要超过了80000。而西北地区的数值是偏低的 以2018年为例,西藏,青海,宁夏三地的数值都要低于10000''')
def index_bar_every(): df = pd.read_csv('./static/data/average_salary.csv') tl = Timeline() for i in range(2013, 2019): map0 = (Map().add( "average_salary", list(zip(list(df.area), list(df["{}".format(i)]))), "china", is_map_symbol_show=False).set_series_opts( label_opts=opts.LabelOpts(is_show=False)).set_global_opts( title_opts=opts.TitleOpts( title="{}年平均工资".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=20000, max_=100000), )) tl.add(map0, "{}".format(i)) return render_template('index.html', myechart=tl.render_embed(), result=df.values.tolist(), a=1, data_x=df.columns.values.tolist()[1:])
def run_select(): the_region = request.form["the_region_selected"] logger.debug(the_region) # 检查用户输入 if the_region == regions_available[0]: df = pd.read_csv('人均消费.csv', encoding='utf8', index_col=0) data_str = df.to_html() tl = Timeline() for i in range(2011, 2019): pie = ( Pie().add( "数值", list(zip(list(df.index), list(df["{}年".format(i)]))), rosetype="radius", radius=["30%", "55%"], ) # .set_global_opts(title_opts=opts.TitleOpts("人均消费".format(i))) ) tl.add(pie, "{}年".format(i)) return render_template('results2.html', the_plot_all=tl.render_embed(), the_res=data_str, the_select_region=regions_available) elif the_region == regions_available[1]: df = pd.read_csv('fuyang.csv', encoding='utf8', index_col=0) data_str = df.to_html() tl = Timeline() for i in range(2011, 2019): pie = (Pie().add( "数值", list(zip(list(df.index), list(df["{}年".format(i)]))), rosetype="radius", radius=["30%", "55%"], )) tl.add(pie, "{}年".format(i)) return render_template('results2.html', the_plot_all=tl.render_embed(), the_res=data_str, the_select_region=regions_available) elif the_region == regions_available[2]: df1 = pd.read_csv('growth.csv', encoding='utf8', index_col=0) data_str = df1.to_html() tl = Timeline() for i in range(2011, 2019): c = (Funnel().add( "人口自然增长率", list(zip(list(df1.index), list(df1["{}年".format(i)]))), label_opts=opts.LabelOpts(position='inside')).set_global_opts( title_opts=opts.TitleOpts( title="{}人口自然增长率".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=1, max_=15))) tl.add(c, "{}年".format(i)) return render_template('results2.html', the_plot_all=tl.render_embed(), the_res=data_str, the_select_region=regions_available) elif the_region == regions_available[3]: df = pd.read_csv('人均消费.csv', encoding='utf8', index_col=0) df1 = pd.read_csv('growth.csv', encoding='utf8', index_col=0) df2 = pd.read_csv('fuyang.csv', encoding='utf8', index_col=0) tl1 = Timeline() for i in range(2011, 2019): map0 = (Map().add( "人口自然增长率", list(zip(list(df1.index), list(df1["{}年".format(i)]))), "china", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}人口自然增长率".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=-1, max_=11.5), )) tl1.add(map0, "{}年".format(i)) tl2 = Timeline() for i in range(2011, 2019): map0 = (Map().add( "人均消费水平", list(zip(list(df.index), list(df["{}年".format(i)]))), "china", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}人均消费".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=5000, max_=50000), )) tl2.add(map0, "{}年".format(i)) tl3 = Timeline() for i in range(2011, 2019): map0 = (Map().add( "抚养比", list(zip(list(df2.index), list(df2["{}年".format(i)]))), "china", is_map_symbol_show=False).set_global_opts( title_opts=opts.TitleOpts( title="{}抚养比".format(i), subtitle="", subtitle_textstyle_opts=opts.TextStyleOpts( color="red", font_size=18, font_style="italic")), visualmap_opts=opts.VisualMapOpts(min_=21, max_=50), )) tl3.add(map0, "{}年".format(i)) return render_template('results3.html', the_plot1=tl1.render_embed(), the_plot2=tl2.render_embed(), the_plot3=tl3.render_embed(), the_select_region=regions_available)