def gen_zwyx_type(zwlb): qs = ZpZwyxByTypeModel.objects if zwlb: qs = qs.filter(zwlb=zwlb) path = f'zwyx_type/{zwlb}.html' else: path = 'zwyx_type.html' # 当月职位月薪与公司性质 df = read_frame(qs.all()) if len(df) > 0: page = Page() Grid_chart1 = Timeline(width=1500, height=450, timeline_bottom=0) Grid_chart2 = Timeline(width=1500, height=450, timeline_bottom=0) df_group = df.groupby(['year', 'month']) for name, group in df_group: month = group['month'].tolist()[0] year = group['year'].tolist()[0] df_new = group.groupby('type').apply(get_echarts_all_by_zwyx_value, 'type') # 薪资 Overlap_chart = Overlap(width=800, height=450) bar_chart = Bar(f'{zwlb}职位月薪与公司性质') bar_chart.add('最低薪资', df_new['type'].tolist(), df_new['min_zwyx'].tolist(), is_label_show=True, is_more_utils=True) bar_chart.add('最高薪资', df_new['type'].tolist(), df_new['max_zwyx'].tolist(), is_label_show=True, is_more_utils=True) line_chart = Line() line_chart.add("平均薪资", df_new['type'].tolist(), [(a + b) / 2 for a, b in zip(df_new['min_zwyx'].tolist(), df_new['max_zwyx'].tolist())], is_label_show=True) Overlap_chart.add(bar_chart) Overlap_chart.add(line_chart) Grid_chart1.add(Overlap_chart, f'{year}年{month}月') # 职位量 chart3 = Pie(f'{zwlb}职位量及招聘人数', width=1500) chart3.add('职位量'.format(zwlb), df_new['type'].tolist(), df_new['count'].tolist(), is_label_show=True, is_stack=True, center=[25, 50]) chart3.add('招聘人数'.format(zwlb), df_new['type'].tolist(), df_new['zprs'].tolist(), is_label_show=True, is_stack=True, center=[75, 50]) Grid_chart2.add(chart3, f'{year}年{month}月') page.add(Grid_chart1) page.add(Grid_chart2) page.render(os.path.join(BASE_DIR, 'templates/{}'.format(path)))
def gen_zwyx_dd(zwlb): qs = ZpZwByAreaModel.objects if zwlb: qs = qs.filter(zwlb=zwlb) path = f'zwyx_dd/{zwlb}.html' else: path = 'zwyx_dd.html' page = Page() df = read_frame(qs.all()) if len(df) > 0: df_group = df.groupby(['year', 'month']) time_line_chart1 = Timeline(width=1500, height=450, is_auto_play=False, timeline_bottom=0) time_line_chart2 = Timeline(width=1500, height=450, is_auto_play=False, timeline_bottom=0) for name, group in df_group: # 地图 平均薪资 month = group['month'].tolist()[0] year = group['year'].tolist()[0] df_new = group.groupby('province').apply( get_echarts_all_by_zwyx_value, 'province') data = [ (a, (b + c) / 2) for a, b, c in zip(df_new['province'].tolist( ), df_new['max_zwyx'].tolist(), df_new['min_zwyx'].tolist()) ] chart = Map(f'{zwlb}平均职位月薪与地点', width=1500, height=450) attr, value = chart.cast(data) chart.add(f'平均薪资', attr, value, wmaptype='china', is_label_show=True, is_visualmap=True, visual_range=[int(min(value)), int(max(value))], visual_pos='right', visual_top='top') time_line_chart1.add(chart, f'{year}年{month}月') # 本月职位量Top20 chart3 = Pie(f'{zwlb}职位量及招聘人数', width=1500) chart3.add('职位量', df_new['province'].tolist(), df_new['count'].tolist(), center=[25, 50], is_label_show=True) chart3.add('招聘人数', df_new['province'].tolist(), df_new['zprs'].tolist(), center=[75, 50], is_label_show=True) time_line_chart2.add(chart3, f'{year}年{month}月') page.add(time_line_chart1) page.add(time_line_chart2) page.render(os.path.join(BASE_DIR, 'templates/{}'.format(path)))
def create_charts(): page = Page() style = Style(width=WIDTH, height=HEIGHT) df = pd.read_csv('./data_cleaned.csv') df['CREATE_TIME'] = pd.to_datetime(df['CREATE_TIME']) df['MONTH'] = 0 months = [] for i in df.CREATE_TIME: month = i.strftime("%Y-%m") months.append(month) df.MONTH = months chart = Timeline(is_auto_play=True, timeline_bottom=0, width=WIDTH, height=HEIGHT) for name, c in df.groupby('COMMUNITY_NAME'): month_count = defaultdict(int) for month, group in c.groupby('MONTH'): month_count[month] = len(group) m_s = sorted(list(month_count.keys())) m_l = [month_count[i] for i in m_s] chart_1 = Line("各月份社区事件数", **style.init_style) chart_1.add( "事件数", m_s, m_l, mark_point=["max", "min"], is_more_utils=True, mark_line=["average"], is_smooth=True, ) chart.add(chart_1, name) page.add(chart) chart = Timeline(is_auto_play=True, timeline_bottom=0, width=WIDTH, height=HEIGHT) for month, group in df.groupby('MONTH'): COMMUNITY_NAME = group.COMMUNITY_NAME.value_counts() chart_1 = Bar("各月份社区事件数", **style.init_style) chart_1.add("", COMMUNITY_NAME.index, COMMUNITY_NAME.values, mark_point=["max", "min"], mark_line=["average"], is_stack=True) chart.add(chart_1, month) page.add(chart) return page
def test_timeline_pie(): style = Style() pie_style = style.add(is_label_show=True, radius=[30, 55], rosetype="radius") pie_1 = Pie("2012 年销量比例", "数据纯属虚构") pie_1.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) pie_2 = Pie("2013 年销量比例", "数据纯属虚构") pie_2.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) pie_3 = Pie("2014 年销量比例", "数据纯属虚构") pie_3.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) pie_4 = Pie("2015 年销量比例", "数据纯属虚构") pie_4.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) pie_5 = Pie("2016 年销量比例", "数据纯属虚构") pie_5.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) timeline = Timeline(is_auto_play=True, timeline_bottom=0, width=1200, height=600) timeline.add(pie_1, "2012 年") timeline.add(pie_2, "2013 年") timeline.add(pie_3, "2014 年") timeline.add(pie_4, "2015 年") timeline.add(pie_5, "2016 年") assert len(timeline._option.get("baseOption").get("series")) == 0 timeline.render()
def test_timeline_pie(): style = Style() pie_style = style.add( is_label_show=True, radius=[30, 55], rosetype="radius" ) pie_1 = Pie("2012 年销量比例", "数据纯属虚构") pie_1.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) pie_2 = Pie("2013 年销量比例", "数据纯属虚构") pie_2.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) pie_3 = Pie("2014 年销量比例", "数据纯属虚构") pie_3.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) pie_4 = Pie("2015 年销量比例", "数据纯属虚构") pie_4.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) pie_5 = Pie("2016 年销量比例", "数据纯属虚构") pie_5.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)], **pie_style) timeline = Timeline(is_auto_play=True, timeline_bottom=0, width=1200, height=600) timeline.add(pie_1, '2012 年') timeline.add(pie_2, '2013 年') timeline.add(pie_3, '2014 年') timeline.add(pie_4, '2015 年') timeline.add(pie_5, '2016 年')
def ShowNumWithYear(df): # show bus num in different year at different cities years = list(set(df['year'])) years.sort() cities = [] values = [] total_num = 0 geos = [] # store the geo every year timeline = Timeline(width=1500,height=800,is_auto_play=True, timeline_bottom=-10,timeline_symbol_size=20,\ timeline_play_interval = 800,timeline_left=20,timeline_right=100 , is_timeline_show = False ) for index in range(len(years)): df_temp = df[df['year'] == years[index]] cities = cities + list(df_temp['city']) values = values + list(df_temp['num']) total_num = sum(values) geos.append(Geo( str(years[index]) + " , Fist level title" , title_top = "10%" , title_text_size=50 , subtitle = "second level title" , \ subtitle_text_size = 23 , subtitle_color="white", \ title_color="red", title_pos="center", width=1200, height=600, \ background_color='#404a59')) # type="effectScatter", is_random=True, effect_scale=5 使点具有发散性 geos[index].add("数量", cities, values, type="effectScatter", maptype='china' , is_random=True, effect_scale=3, is_selected = True,is_toolbox_show = True ,is_more_utils =True,\ visual_text_color="#fff", symbol_size=10, is_label_show = True , legend_orient = 'left' ,is_legend_show = False, legend_top = 'bottom' , label_formatter = '{b}' , \ is_visualmap=True, is_roam=True , label_text_color="#00FF00" , is_piecewise=True, label_text_size = 7,visual_range=[1, 300] , \ geo_cities_coords = {'柯桥': [120.443 , 30.0822] ,} , \ pieces=[ {"min":0.1, "max": 500 , "label": "0-500"}, {"min": 500, "max": 1000 , "label": "501-1000"}, {"min": 1001, "max": 2000 , "label": "1001-2000"}, {"min":2001, "max": 5000, "label": "2001-5000"}, {"min":5001, "max": 100000, "label": ">5000"}, ] ) geos[index].show_config() geos[index].render("数量.html") # 时间轴定义 timeline.add(geos[index], years[index]) timeline.render('redult.html')
def test_timeline_map(): timeline = Timeline(timeline_bottom=0) value = [155, 10, 66, 78, 33, 80, 190, 53, 49.6] attr = ["福建", "山东", "北京", "上海", "甘肃", "新疆", "河南", "广西", "西藏"] map = Map("Map 结合 VisualMap 示例", width=1200, height=600) map.add( "", attr, value, maptype="china", is_visualmap=True, visual_text_color="#000", visual_top="30%", ) timeline.add(map, "test1") value = [155, 10, 66, 78, 33] attr = ["福建", "山东", "北京", "上海", "甘肃"] map = Map("Map 结合 VisualMap 示例", width=1200, height=600) map.add( "", attr, value, maptype="china", is_visualmap=True, visual_text_color="#000", visual_top="30%", ) timeline.add(map, "test2") assert len(timeline._option.get("baseOption").get("series")) == 2 timeline.render()
def creatTimeLine(charts, timepoints, speed=500, showTimeLine=True): tt = Timeline(timeline_play_interval=speed, is_timeline_show=showTimeLine) cnt = 0 for chart in charts: tt.add(chart, time_point=timepoints[cnt]) cnt += 1 return tt
def get_pic(): df2 = shops(mob_site)[1] a = order(df2, order_countJA, order_countHA, '') sz = order(df2, order_countJ, order_countH, shop_order, 4)[0] gz = order(df2, order_countJ, order_countH, shop_order, 42)[0] hz = order(df2, order_countJ, order_countH, shop_order, 53)[0] hk = order(df2, order_countJ, order_countH, shop_order, 85)[0] sh = order(df2, order_countJ, order_countH, shop_order, 11)[0] cs = order(df2, order_countJ, order_countH, shop_order, 205)[0] fz = order(df2, order_countJ, order_countH, shop_order, 201)[0] bar_a = out_bar(a, "全国") bar_sz = out_bar(sz, "深圳") bar_gz = out_bar(gz, "广州") bar_hz = out_bar(hz, "杭州") bar_hk = out_bar(hk, "海口") bar_sh = out_bar(sh, "上海") bar_cs = out_bar(cs, "长沙") bar_fz = out_bar(fz, "福州") timeline = Timeline(is_auto_play=False, timeline_bottom=0, width=1100) timeline.add(bar_a, '全国') timeline.add(bar_sz, '深圳') timeline.add(bar_gz, '广州') timeline.add(bar_hz, '杭州') timeline.add(bar_hk, '海口') timeline.add(bar_sh, '上海') timeline.add(bar_cs, '长沙') timeline.add(bar_fz, '福州') return timeline
def spot_line(cnt,name): follow_spot=df[df['channel_id']==cnt] follow_spot=follow_spot.groupby(by=['spot_id','date'],as_index=False).sum()[['spot_id','date','cost_r']] spt=follow_spot[follow_spot['date'].apply(lambda x:str(x))==end_time.replace('-', '')].sort_values(by='cost_r').tail(7)['spot_id'] top_spt=follow_spot[follow_spot['spot_id'].isin(spt)] #TOP广告位 lists=[] for i,j in itertools.product(top_spt['spot_id'].drop_duplicates(),df['date'].drop_duplicates()): lists.append([i,j]) lists=pd.DataFrame(lists,columns=['spot_id','date']) top_spt=pd.merge(lists,top_spt,how='left',on=['spot_id','date']) top_spt=top_spt.fillna(0) top_spt=pd.merge(top_spt,meta_spot,how='left',left_on='spot_id',right_on='spotid') top_spt.loc[top_spt['name'].isnull(),'name']=top_spt[top_spt['name'].isnull()]['spot_id'] line = Line(name+'TOP广告位消耗') for i in top_spt['name'].drop_duplicates(): line.add(i,top_spt[top_spt['name']==i]['date'].apply(lambda x:str(x)),top_spt[top_spt['name']==i]['cost_r'].apply(lambda x:int(x)),legend_pos="84%",mark_point=['max']) # grid= Grid(width=1000,height=350) # grid.add(line,grid_right="25%") # page.add_chart(grid) ''' 涨跌幅TOP广告位 ''' lists=[] for i,j in itertools.product(follow_spot['spot_id'].drop_duplicates(),df['date'].drop_duplicates()): lists.append([i,j]) lists=pd.DataFrame(lists,columns=['spot_id','date']) follow_spt=pd.merge(lists,follow_spot,how='left',on=['spot_id','date'])#笛卡尔积 follow_spt=follow_spt.fillna(0) diff_spt=follow_spt.groupby(by='spot_id',as_index=False).diff()['cost_r'].rename('rise') diff_spt=pd.concat([follow_spt,diff_spt],axis=1) #涨幅TOP 7 spt=diff_spt[diff_spt['date'].apply(lambda x:str(x))==end_time.replace('-', '')].sort_values(by='rise').tail(7)['spot_id'] rise_spt=diff_spt[diff_spt['spot_id'].isin(spt)] #TOP广告位 rise_spt=pd.merge(rise_spt,meta_spot,how='left',left_on='spot_id',right_on='spotid') rise_spt.loc[rise_spt['name'].isnull(),'name']=rise_spt[rise_spt['name'].isnull()]['spot_id'] line1 = Line(name+'涨幅TOP广告位',width=1000,height=450) for i in rise_spt['name'].drop_duplicates(): line1.add(i,rise_spt[rise_spt['name']==i]['date'].apply(lambda x:str(x)),rise_spt[rise_spt['name']==i]['cost_r'].apply(lambda x:int(x)),legend_pos="84%") #跌幅TOP 7 spt=diff_spt[diff_spt['date'].apply(lambda x:str(x))==end_time.replace('-', '')].sort_values(by='rise').head(7)['spot_id'] fall_spt=diff_spt[diff_spt['spot_id'].isin(spt)] #TOP广告位 fall_spt=pd.merge(fall_spt,meta_spot,how='left',left_on='spot_id',right_on='spotid') fall_spt.loc[fall_spt['name'].isnull(),'name']=fall_spt[fall_spt['name'].isnull()]['spot_id'] line2 = Line(name+'跌幅TOP广告位',width=1000,height=450) for i in fall_spt['name'].drop_duplicates(): line2.add(i,fall_spt[fall_spt['name']==i]['date'].apply(lambda x:str(x)),fall_spt[fall_spt['name']==i]['cost_r'].apply(lambda x:int(x)),legend_pos="84%") #画图 grid= Grid(width=900,height=350) timeline = Timeline(timeline_bottom=0) timeline.add(line, name+'TOP消耗') timeline.add(line1, name+'涨幅TOP消耗') timeline.add(line2, name+'跌幅TOP消耗') grid.add(timeline) # grid= Grid(width=1200,height=350) # grid.add(line,grid_right="55%") # grid.add(line1,grid_left="55%") page.add_chart(grid)
def test_timeline_label_color(): attr = ["{}月".format(i) for i in range(1, 7)] bar = Bar("1 月份数据", "数据纯属虚构") bar.add( "bar", attr, [randint(10, 50) for _ in range(6)], label_color=["red", "#213", "black"], ) line = Line() line.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_0 = Overlap() overlap_0.add(bar) overlap_0.add(line) bar_1 = Bar("2 月份数据", "数据纯属虚构") bar_1.add("bar", attr, [randint(10, 50) for _ in range(6)]) line_1 = Line() line_1.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_1 = Overlap() overlap_1.add(bar_1) overlap_1.add(line_1) timeline = Timeline(timeline_bottom=0) timeline.add(overlap_0, "1 月") timeline.add(overlap_1, "2 月") content = timeline._repr_html_() assert '"color": [' in content assert "red" in content assert "#213" in content assert "black" in content
def gen_gwzz_word(zwlb): qs = ZpWordByZwlbModel.objects if zwlb: qs = qs.filter(zwlb=zwlb) path = f'zp_word/{zwlb}.html' else: path = 'zp_word.html' df = read_frame(qs.all()) if len(df) > 0: page = Page() Grid_chart1 = Timeline(width=1500, height=800, timeline_bottom=0) df_group = df.groupby(['year', 'month']) for name, group in df_group: month = group['month'].tolist()[0] year = group['year'].tolist()[0] df_new = group.groupby('word').apply(get_echarts_all_by_value, 'word') chart = WordCloud(f'{zwlb}岗位需求词云', width=1500) shape_list = [ None, 'circle', 'cardioid', 'diamond', 'triangle-forward', 'triangle', 'pentagon', 'star' ] chart.add("", df_new['word'].tolist(), df_new['count'].tolist(), word_size_range=[30, 100], rotate_step=66, shape=shape_list[random.randint(0, len(shape_list) - 1)]) Grid_chart1.add(chart, f'{year}年{month}月') page.add(Grid_chart1) page.render(os.path.join(BASE_DIR, 'templates/{}'.format(path)))
def _initial(self, speed): timeline = Timeline(width=self.width, height=self.height, timeline_play_interval=speed) index = 0 for bar in self.bars: timeline.add(bar, time_point=self.timePoints[index]) index += 1 return timeline
def create_charts(): page = Page() style = Style(width=WIDTH, height=HEIGHT) df = pd.read_csv('./data_cleaned.csv') STREET_NAME = df.STREET_NAME.value_counts() chart = Bar("街道种类", **style.init_style) chart.add( "", STREET_NAME.index, STREET_NAME.values, mark_point=["max", "min"], mark_line=["average"], is_stack=True, ) page.add(chart) chart = Timeline(is_auto_play=True, timeline_bottom=0, width=WIDTH, height=HEIGHT) for name, c in df.groupby('STREET_NAME'): EVENT_TYPE_NAME = c.EVENT_TYPE_NAME.value_counts() chart_1 = Bar("各社区事件类型", **style.init_style) chart_1.add("", EVENT_TYPE_NAME.index, EVENT_TYPE_NAME.values, mark_point=["max", "min"], mark_line=["average"], is_stack=True) chart.add(chart_1, name) page.add(chart) table = pd.pivot_table(df, index=['STREET_NAME'], columns=['EVENT_TYPE_NAME'], values=['SUB_TYPE_NAME'], aggfunc='count', fill_value=0, margins=1) table = table.ix[:-1, :-1] comm = list(table.index) name = [i[1] for i in table] value = [[i, j, float(table.values[i][j])] for i in range(len(comm)) for j in range(len(name))] chart = HeatMap("街道与事件热力图", width=WIDTH, height=HEIGHT) chart.add("", comm, name, value, is_visualmap=True, visual_text_color="#000", visual_orient='horizontal') page.add(chart) return page
def show_ruanke_best(begin_year,end_year): ''' @author: Hong wentao 用于展示每一个学科的学校的排名可视化,使用的是软科最好大学排名的数据 ''' path=data_path+"ruanke_best_subject/" ruanke_html_path=html_path+"ruanke/" isExists=os.path.exists(ruanke_html_path) if not isExists: os.makedirs(ruanke_html_path) first_names=os.listdir(path) for first_name in first_names: first_data_path=path+first_name+"/" first_html_path=ruanke_html_path+first_name+"/" isExists=os.path.exists(first_html_path) if not isExists: os.makedirs(first_html_path) second_names=os.listdir(first_data_path) for second_name in second_names: second_data_path=first_data_path+second_name+"/" second_html_path=first_html_path+second_name+"/" isExists=os.path.exists(second_html_path) if not isExists: os.makedirs(second_html_path) data=deal_ruanke.every_subject_ruanke(first_name,second_name) timeline = Timeline(is_auto_play=False, timeline_bottom=0,width=stand_width) for i in range(begin_year,end_year+1): bar = Bar(second_name+str(i)+"年大学排名数据") try: want_data=data[i] nan='空白区域说明该学校当年无该数据' final_data=[] have_name=[] before_median=[] for x in want_data: have_name.append(x[2]) for name in university_name: if name not in have_name: final_data.append("nan") else: for x in want_data: if x[2]==name: final_data.append(x[0]) before_median.append(x[0]) break; a=np.array(before_median).astype("int") median=np.median(a).astype("int") bar.add("排名 "+nan, university_name,final_data, is_label_show=True,is_stack=True,xaxis_interval=0,xaxis_name_size=15,xaxis_rotate=0,yaxis_name_rotate=0,xaxis_name_pos="end", xaxis_name="学校名",yaxis_name="排名",yaxis_name_gap=10,yaxis_name_pos="end",mark_point=["max","min"],mark_line_raw=[{"yAxis": median,"name":"中位数","lineStyle": { "color": 'black' }}]) timeline.add(bar, str(i)+'年') timeline.render(second_html_path+second_name+".html") except Exception: print(first_name+" "+second_name+str(i)+"没有") continue timeline.render(second_html_path+second_name+".html")
def test_null_in_timeline_options(): bar_1 = Bar("2012 年销量", "数据纯属虚构") bar_1.add("春季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_1._option["series"][0]["symbol"] = NULL timeline = Timeline(is_auto_play=True, timeline_bottom=0) timeline.add(bar_1, "2012 年") assert isinstance(timeline._option["options"][0]["series"][0]["symbol"], JsValue)
def gauge_timeline(data_array): chart1 = Timeline(**timeline_Style) array_gauge = [] for i in range(0, 13): array_gauge.append(data_array[i]['heat'].sum()) maxgauge = max(array_gauge) array_gauge = soomthByStage(array_gauge, 5) for i in range(0, 60): gauge = Gauge("观看人数趋势图 ", title_pos='center') gauge.add("", "占比", int(array_gauge[i] / maxgauge * 100)) chart1.add(gauge, String_filename_1[i]) return chart1
def show_map_school(begin_year, end_year): ''' 用地图展示各学校的排名,这样可以观察地域与学校排名的关系 ''' datas = deal_ruanke.ranking_ruanke(begin_year, end_year) university_name = (list)(datas.keys()) ranking_datas = (list)(datas.values()) timeline = Timeline(is_auto_play=False, timeline_bottom=0, width=stand_width, height=500) for i in range(begin_year, end_year + 1): data = [] dict = {} nan = '' for j in range(0, len(university_name)): line = [] line.append(university_name[j]) if (ranking_datas[j][i - begin_year] == "nan"): continue line.append(ranking_datas[j][i - begin_year]) line_tuple = (tuple)(line) data.append(line_tuple) dict[university_name[j]] = ranking_datas[j][i - begin_year] if (i == 2016): nan = "(云南大学,青海大学,西藏大学未纳入排名)" geo = Geo(str(i) + "年度全国大学排名" + nan, "全国大学排名", title_color="#000", title_pos="center", width=1600, height=400, background_color='#DCDCDC') attr, value = geo.cast(data) geo.add("", attr, value, visual_range=[0, 600], maptype='china', visual_text_color="#000", symbol_size=20, is_visualmap=True, is_roam=False, visual_top="center", geo_normal_color="#404a59", visual_range_text=["high", "low"], visual_range_color=['#50a3ba', '#faef61', '#d94e5d'], label_formatter="{c0}", tooltip_formatter="{b}: [经度(E),纬度(N),排名] {c}名") geo timeline.add(geo, str(i) + '年') return timeline
def time_line(): timeline = Timeline(page_title="Time Line") stack_bar, line = StackBar.stack_bar() tree_chart = TreeChart.tree_chart() stack_bar_chart = StackBarChart.stack_bar_chart2() stack_line = StackChart.starck_line_chart() timeline.add(stack_line, time_point=7) timeline.add(stack_bar_chart, time_point=8) timeline.add(tree_chart, time_point=6) timeline.add(stack_bar,time_point=2) timeline.add(line, time_point=3) timeline.render("timeline.html")
def geographical_location_GDP_ratio_distribution(): data = pd.read_excel("./data/car_sales_data.xlsx", sheet_name=u"人均GDP", encoding='utf-8') data.columns = [str(col).split('-')[0] for col in data.columns] float_cols = [str(year) for year in range(1998, 2018)] not_replace = [ u'池州市', u'宣城市', u'眉山市', u'达州市', u'庆阳市', u'广安市', u'贺州市', u'来宾市', u'崇左市', u'临沧市', u'固原市', u'中卫市', u'丽江市' ] data['city_name'] = data['city_name'].apply(lambda xx: xx.replace(u'市', '') if xx not in not_replace else xx) city_none = [u'黔西南布依族苗族自治州', u'乌兰察布', u'巴音郭楞蒙古自治州(库尔勒)'] data = data[~data['city_name'].isin(city_none)] data[float_cols] = data[float_cols].fillna(0).applymap(lambda xx: round(xx, 2) if xx else 0) cols_show = ['city_name'] + float_cols data = data[cols_show] data = calc_ratio_percent(data) data[range(1999, 2018)] = data[range(1999, 2018)].applymap(lambda xx: round(xx * 100, 2)) data.to_excel('./car_sales_visualization/GDP_ratio_distribution.xlsx') timeline = Timeline(is_auto_play=False, timeline_bottom=1250, width=2480, height=1330) for year in range(1999, 2018): dataset = [(city, sales) for city, sales in data[['city_name'] + [year]].values] geo = Geo( "%s - Car Sales Num Ratio Distribution" % year, "", title_pos="center", title_color="black", width=2480, height=1330, background_color='#ffffff' ) attr, value = geo.cast(dataset) geo.add( "", attr, value, type="effectScatter", is_visualmap=True, maptype='china', visual_range=[-100.0, 100.0], visual_text_color="black", effect_scale=5, symbol_size=5 ) timeline.add(geo, year) timeline.render('./car_sales_visualization/GDP_ratio_distribution.html')
def test_timeline_bar_line(): attr = ["{}月".format(i) for i in range(1, 7)] bar = Bar("1 月份数据", "数据纯属虚构") bar.add("bar", attr, [randint(10, 50) for _ in range(6)]) line = Line() line.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_0 = Overlap() overlap_0.add(bar) overlap_0.add(line) bar_1 = Bar("2 月份数据", "数据纯属虚构") bar_1.add("bar", attr, [randint(10, 50) for _ in range(6)]) line_1 = Line() line_1.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_1 = Overlap() overlap_1.add(bar_1) overlap_1.add(line_1) bar_2 = Bar("3 月份数据", "数据纯属虚构") bar_2.add("bar", attr, [randint(10, 50) for _ in range(6)]) line_2 = Line() line_2.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_2 = Overlap() overlap_2.add(bar_2) overlap_2.add(line_2) bar_3 = Bar("4 月份数据", "数据纯属虚构") bar_3.add("bar", attr, [randint(10, 50) for _ in range(6)]) line_3 = Line() line_3.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_3 = Overlap() overlap_3.add(bar_3) overlap_3.add(line_3) bar_4 = Bar("5 月份数据", "数据纯属虚构") bar_4.add("bar", attr, [randint(10, 50) for _ in range(6)]) line_4 = Line() line_4.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_4 = Overlap() overlap_4.add(bar_4) overlap_4.add(line_4) timeline = Timeline(timeline_bottom=0) timeline.add(overlap_0, '1 月') timeline.add(overlap_1, '2 月') timeline.add(overlap_2, '3 月') timeline.add(overlap_3, '4 月') timeline.add(overlap_4, '5 月') assert len(timeline._option.get("baseOption").get("series")) == 10 timeline.render()
def show_total_ruanke(begin_year, end_year): ''' 1.软科总体排名 @author: Hong wentao 最终的可视化结果呈现出的是2016年至今这14所学校在软科最好大学排名中的排名名次 ''' datas = deal_ruanke.ranking_ruanke(begin_year, end_year) university_name = (list)(datas.keys()) ranking_datas = (list)(datas.values()) timeline = Timeline(is_auto_play=False, timeline_bottom=0, width=stand_width) for i in range(0, end_year - begin_year + 1): bar = Bar("软科" + str(2016 + i) + "年大学排名数据") want_data = [] nan = '' before_median = [] for x in ranking_datas: want_data.append(x[i]) if x[i] == "nan": nan = "(如果某学校当年无数据,则该区域为为空白)" else: before_median.append(x[i]) a = np.array(before_median).astype("int") median = np.median(a).astype("int") bar.add("排名 " + nan, university_name, want_data, is_label_show=True, is_stack=True, xaxis_interval=0, xaxis_name_size=15, xaxis_rotate=0, yaxis_name_rotate=0, xaxis_name_pos="end", xaxis_name="学校名", yaxis_name="排名", yaxis_name_gap=10, yaxis_name_pos="end", mark_point=["max", "min"], mark_line_raw=[{ "yAxis": median, "name": "中位数", "lineStyle": { "color": 'black' } }]) timeline.add(bar, str(2016 + i) + ' 年') return timeline
def test_timeline_bar_line(): attr = ["{}月".format(i) for i in range(1, 7)] bar = Bar("1 月份数据", "数据纯属虚构") bar.add("bar", attr, [randint(10, 50) for _ in range(6)]) line = Line() line.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_0 = Overlap() overlap_0.add(bar) overlap_0.add(line) bar_1 = Bar("2 月份数据", "数据纯属虚构") bar_1.add("bar", attr, [randint(10, 50) for _ in range(6)]) line_1 = Line() line_1.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_1 = Overlap() overlap_1.add(bar_1) overlap_1.add(line_1) bar_2 = Bar("3 月份数据", "数据纯属虚构") bar_2.add("bar", attr, [randint(10, 50) for _ in range(6)]) line_2 = Line() line_2.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_2 = Overlap() overlap_2.add(bar_2) overlap_2.add(line_2) bar_3 = Bar("4 月份数据", "数据纯属虚构") bar_3.add("bar", attr, [randint(10, 50) for _ in range(6)]) line_3 = Line() line_3.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_3 = Overlap() overlap_3.add(bar_3) overlap_3.add(line_3) bar_4 = Bar("5 月份数据", "数据纯属虚构") bar_4.add("bar", attr, [randint(10, 50) for _ in range(6)]) line_4 = Line() line_4.add("line", attr, [randint(50, 80) for _ in range(6)]) overlap_4 = Overlap() overlap_4.add(bar_4) overlap_4.add(line_4) timeline = Timeline(timeline_bottom=0) timeline.add(overlap_0, "1 月") timeline.add(overlap_1, "2 月") timeline.add(overlap_2, "3 月") timeline.add(overlap_3, "4 月") timeline.add(overlap_4, "5 月") timeline.render()
def geographical_location_ratio_distribution(): data = pd.read_csv("./data/car_sales_volume_data.csv", encoding='utf-8') data = data[data['city'].str.len() > 1] data = calc_ratio_percent(data) data[range(1998, 2019)] = data[range(1998, 2019)].applymap(lambda xx: round(xx * 100, 4)) cols_show = ['city'] + range(1998, 2019) data = data[cols_show] city_none = [ u'三沙', u'中卫', u'临沧', u'丽江', u'克孜勒苏柯尔克孜自治州', u'其它', u'固原', u'新疆自治区直辖', u'普洱', u'河南省省直辖', u'海南省省直辖', u'湖北省省直辖' ] data = data[~data['city'].isin(city_none)] data.to_excel('./car_sales_visualization/car_sales_num_ratio.xlsx') timeline = Timeline(is_auto_play=False, timeline_bottom=1250, width=2480, height=1330) for year in range(1998, 2019): dataset = [(city, sales) for city, sales in data[['city'] + [year]].values] geo = Geo( "%s - Car Sales Num Ratio Distribution" % year, "", title_pos="center", title_color="black", width=2480, height=1330, background_color='#ffffff' ) attr, value = geo.cast(dataset) geo.add( "", attr, value, type="effectScatter", is_visualmap=True, maptype='china', visual_range=[-100, 100], visual_text_color="black", effect_scale=5, symbol_size=5 ) timeline.add(geo, year) timeline.render('./car_sales_visualization/car_sales_num_ratio.html')
def sale(): from pyecharts import Bar, Timeline from random import randint attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] bar_1 = Bar("2012 年销量", "数据纯属虚构") bar_1.add("春季", attr, [randint(10, 100) for _ in range(6)]) bar_1.add("夏季", attr, [randint(10, 100) for _ in range(6)]) bar_1.add("秋季", attr, [randint(10, 100) for _ in range(6)]) bar_1.add("冬季", attr, [randint(10, 100) for _ in range(6)]) bar_2 = Bar("2013 年销量", "数据纯属虚构") bar_2.add("春季", attr, [randint(10, 100) for _ in range(6)]) bar_2.add("夏季", attr, [randint(10, 100) for _ in range(6)]) bar_2.add("秋季", attr, [randint(10, 100) for _ in range(6)]) bar_2.add("冬季", attr, [randint(10, 100) for _ in range(6)]) bar_3 = Bar("2014 年销量", "数据纯属虚构") bar_3.add("春季", attr, [randint(10, 100) for _ in range(6)]) bar_3.add("夏季", attr, [randint(10, 100) for _ in range(6)]) bar_3.add("秋季", attr, [randint(10, 100) for _ in range(6)]) bar_3.add("冬季", attr, [randint(10, 100) for _ in range(6)]) bar_4 = Bar("2015 年销量", "数据纯属虚构") bar_4.add("春季", attr, [randint(10, 100) for _ in range(6)]) bar_4.add("夏季", attr, [randint(10, 100) for _ in range(6)]) bar_4.add("秋季", attr, [randint(10, 100) for _ in range(6)]) bar_4.add("冬季", attr, [randint(10, 100) for _ in range(6)]) bar_5 = Bar("2016 年销量", "数据纯属虚构") bar_5.add("春季", attr, [randint(10, 100) for _ in range(6)]) bar_5.add("夏季", attr, [randint(10, 100) for _ in range(6)]) bar_5.add("秋季", attr, [randint(10, 100) for _ in range(6)]) bar_5.add("冬季", attr, [randint(10, 100) for _ in range(6)], is_legend_show=True) timeline = Timeline(is_auto_play=True, timeline_bottom=0) timeline.add(bar_1, '2012 年') timeline.add(bar_2, '2013 年') timeline.add(bar_3, '2014 年') timeline.add(bar_4, '2015 年') timeline.add(bar_5, '2016 年') return template(timeline)
def get_html_project_report(project_name, type=0): mylogger = common.LogOutput.Log().get_logger() mylogger.info( "------------------------------------------------------------------") mylogger.info("当前运行文件:{}".format(__file__)) try: project_run_num = get_project_run_num(project_name, type) # num = [successNum, failedNum, noRunNum] num = [project_run_num[0], project_run_num[1], project_run_num[2]] attr = ["成功用例", "失败用例", "不执行用例"] bar = Bar(project_name, "用例执行结果") bar.add("", attr, num, is_more_utils=True) bar.show_config() pie_1 = Pie(project_name, "用例执行结果") pie_1.add(project_name, attr, num, is_label_show=True, radius=[30, 55], rosetype='radius') timeline = Timeline(is_auto_play=True, timeline_bottom=0) timeline.add(bar, '树形图') timeline.add(pie_1, '饼形图') PATH = lambda P: os.path.abspath( os.path.join(os.path.dirname(__file__), P)) html_dirname = PATH( "../report") + "\\" + project_name + "\\" + "htmlReport\\" # 判断是否存在case目录,没有,则创建 if not os.path.exists(html_dirname): os.mkdir(html_dirname) mylogger.info("生成用例目录路径:{}".format(html_dirname)) if type == 0: html_report_name = time.strftime('%Y-%m-%d %H-%M-%S') + '.html' elif type == 1: html_report_name = 'settingReport' + time.strftime( '%Y-%m-%d %H-%M-%S') + '.html' htmlReportPath = html_dirname + html_report_name timeline.render(htmlReportPath) return 'success' except TypeError as e: return 'caseEmpty', e
def flush_visual_data(): while True: timeline = Timeline(is_auto_play=True, timeline_bottom=0) cpudata = cpu(attr) memdata = mem(attr) diskdata = disk(attr) timeline.add(cpudata, 'CPU') timeline.add(memdata, '内存') timeline.add(diskdata, '磁盘') context = dict( visual_sysinfo=timeline.render_embed(), host=DEFAULT_HOST, script_list=timeline.get_js_dependencies(), ) context_json = json.dumps(context) with open('temp_json_info', 'wb') as temp_file: temp_file.write(context_json) time.sleep(600)
def geographical_location_amount_distribution(): data = pd.read_csv("./data/car_sales_volume_data.csv", encoding='utf-8') data = data[data['city'].str.len() > 1] city_none = [ u'三沙', u'中卫', u'临沧', u'丽江', u'克孜勒苏柯尔克孜自治州', u'其它', u'固原', u'新疆自治区直辖', u'普洱', u'河南省省直辖', u'海南省省直辖', u'湖北省省直辖' ] data = data[~data['city'].isin(city_none)] int_cols = [str(col) for col in range(1998, 2019)] data[int_cols] = data[int_cols].applymap(lambda xx: float(xx)) timeline = Timeline(is_auto_play=False, timeline_bottom=1250, width=2480, height=1330) for year in range(1998, 2019): dataset = [(city, sales) for city, sales in data[('city %s' % year).split()].values] print dataset geo = Geo( "%s - Car Sales Num Amount Distribution" % year, "", title_pos="center", title_color="black", width=2700, height=1340, background_color='#ffffff' ) attr, value = geo.cast(dataset) geo.add( "", attr, value, maptype='china', visual_range=[0, 300000], visual_text_color="black", type="heatmap", is_visualmap=True, effect_scale=5, symbol_size=5, ) timeline.add(geo, year) timeline.render('./car_sales_visualization/car_sales_num_amount.html')
def create_charts(): page = Page() style = Style(width=WIDTH, height=HEIGHT) df = pd.read_csv('./data_cleaned.csv') df['CREATE_TIME'] = pd.to_datetime(df['CREATE_TIME']) df['MONTH'] = 0 months = [] for i in df.CREATE_TIME: month = i.strftime("%Y-%m") months.append(month) df.MONTH = months df['DISPOSE_UNIT_NAME_DIST'] = 0 df['DISPOSE_UNIT_NAME_DIST'] = df['DISPOSE_UNIT_NAME'].apply(deletekh) DISPOSE_UNIT_NAME_DIST = df.DISPOSE_UNIT_NAME_DIST.value_counts() chart = Bar("处理部门", **style.init_style) chart.add("", DISPOSE_UNIT_NAME_DIST.index, DISPOSE_UNIT_NAME_DIST.values, mark_point=["max", "min"], is_datazoom_show=True, datazoom_range=[0, 30], mark_line=["average"], is_stack=True) page.add(chart) chart = Timeline(is_auto_play=True, timeline_bottom=0, width=WIDTH, height=HEIGHT) for month, group in df.groupby('MONTH'): DISPOSE_UNIT_NAME = group.DISPOSE_UNIT_NAME_DIST.value_counts() chart_1 = Bar("处理部门事件数", **style.init_style) chart_1.add("", DISPOSE_UNIT_NAME.index, DISPOSE_UNIT_NAME.values, mark_point=["max", "min"], mark_line=["average"], is_stack=True) chart.add(chart_1, month) page.add(chart) return page
def test_timeline_bar(): bar_1 = Bar("2012 年销量", "数据纯属虚构") bar_1.add("春季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_1.add("夏季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_1.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_1.add("冬季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_2 = Bar("2013 年销量", "数据纯属虚构") bar_2.add("春季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_2.add("夏季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_2.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_2.add("冬季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_3 = Bar("2014 年销量", "数据纯属虚构") bar_3.add("春季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_3.add("夏季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_3.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_3.add("冬季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_4 = Bar("2015 年销量", "数据纯属虚构") bar_4.add("春季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_4.add("夏季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_4.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_4.add("冬季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_5 = Bar("2016 年销量", "数据纯属虚构") bar_5.add("春季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_5.add("夏季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_5.add("秋季", CLOTHES, [randint(10, 100) for _ in range(6)]) bar_5.add( "冬季", CLOTHES, [randint(10, 100) for _ in range(6)], is_legend_show=True, ) timeline = Timeline(is_auto_play=True, timeline_bottom=0) timeline.add(bar_1, "2012 年") timeline.add(bar_2, "2013 年") timeline.add(bar_3, "2014 年") timeline.add(bar_4, "2015 年") timeline.add(bar_5, "2016 年") assert len(timeline._option.get("baseOption").get("series")) == 0 timeline.render()