def genCalendar(self,data = None, year='2017',title='Calendar示例',subtitle =''): ''' 日历表 param year 指定的年的日历 param data 日期和统计的数量 [('2020-01-01',300),('2020-01-02',200)] ''' if not data: import random begin = datetime.date(2017, 1, 1) end = datetime.date(2017, 12, 31) data = [ [str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)] for i in range((end - begin).days + 1) ] c = ( Calendar() .add("", data, calendar_opts=opts.CalendarOpts(range_=year)) .set_global_opts( title_opts=opts.TitleOpts(title=title,subtitle=subtitle), visualmap_opts=opts.VisualMapOpts( max_=10000, min_=0, orient="horizontal", is_piecewise=True, pos_top="230px", pos_left="100px", ), ) ) c.render() chart_name = f'static/chart{self.index}_calendar' make_snapshot(snapshot,c.render(f'{chart_name}.html'),f'{chart_name}.png',is_remove_html=True) self.index += 1
def genLine(self,x_list=None,y_orderdicts=None,title='线形示例',subtitle='副标题示例',is_smooth=True,average = None): ''' 折线图 平滑曲线 应用场景 趋势图 param x_list x轴的值 type list param y_dicts key是类别,value是y轴的值 type orderdict param title 图表的题目 type str param is_smooth 是否平滑 默认是 type bool param average 平均数的基准标记线 type number ''' x_list = x_list or Faker.choose() y_orderdicts = y_orderdicts or {i: Faker.values() for i in range(1, 3)} c = ( Line() .add_xaxis(x_list) .set_global_opts(title_opts=opts.TitleOpts(title=title,subtitle =subtitle)) ) for key, value in y_orderdicts.items(): if average: # c.add_yaxis(key, value,is_smooth=True,markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")])) c.add_yaxis(key, value,is_smooth=True,markline_opts=opts.MarkLineOpts(data=average)) else: c.add_yaxis(key, value,is_smooth=True) chart_name = f'static/chart{self.index}_line' make_snapshot(snapshot,c.render(f'{chart_name}.html'),f'{chart_name}.png',is_remove_html=True) self.index += 1
def genMultiPie(self, left_x_list=None,left_y_list=None, right_x_list=None, right_y_list=None, title='多玫瑰示例'): ''' 多个玫瑰图 param left_x_list 标注 type list param right_x_list 标注 type list datas ''' left_x_list = left_x_list or Faker.choose() left_y_list = left_y_list or Faker.values() right_x_list = right_x_list or Faker.choose() right_y_list = right_y_list or Faker.values() c = ( Pie() .add( "", [list(z) for z in zip(left_x_list, left_y_list)], radius=["30%", "75%"], center=["25%", "50%"], rosetype="radius", label_opts=opts.LabelOpts(is_show=False), ) .add( "", [list(z) for z in zip(right_x_list, right_y_list)], radius=["30%", "75%"], center=["75%", "50%"], rosetype="area", ) .set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图示例")) ) chart_name = f'static/chart{self.index}_multipie' make_snapshot(snapshot,c.render(f'{chart_name}.html'),f'{chart_name}.png',is_remove_html=True) self.index += 1
def genWordCloud(self,words=None,title='示例'): ''' 词云 应用场景 关键词显示 ''' words = words or [ ("Sam S Club", 10000), ("Macys", 6181), ("Amy Schumer", 4386), ("Jurassic World", 4055), ("Charter Communications", 2467), ("Chick Fil A", 2244), ("Planet Fitness", 1868), ("Pitch Perfect", 1484), ("Express", 1112), ("Home", 865), ("Johnny Depp", 847), ("Lena Dunham", 582), ("Lewis Hamilton", 555), ("KXAN", 550), ("Mary Ellen Mark", 462), ("Farrah Abraham", 366), ("Rita Ora", 360), ("Serena Williams", 282), ("NCAA baseball tournament", 273), ("Point Break", 265), ] c = ( WordCloud() .add("", words, word_size_range=[20, 100]) .set_global_opts(title_opts=opts.TitleOpts(title=title)) ) chart_name = f'static/chart{self.index}_wordcloud' make_snapshot(snapshot,c.render(f'{chart_name}.html'),f'{chart_name}.png',is_remove_html=True) self.index += 1
def draw_map_world(data, to_file, svg_name, label_name, number_max): """ 画地图 :param data: :param to_file: :param svg_name: :param label_name: 图例名称 :param number_max: 最大值 :return: """ geo = Map(init_opts=opts.InitOpts(width="800px", height="600px", bg_color='rgb(255, 255, 255)')) \ .add(label_name, data, maptype="world") \ .set_series_opts(label_opts=opts.LabelOpts(is_show=False), showLegendSymbol=False) \ .set_global_opts(legend_opts=opts.LegendOpts(item_width=50, item_height=30, textstyle_opts=opts.TextStyleOpts(font_size=30)), visualmap_opts=opts.VisualMapOpts(min_=0, max_=int(number_max), background_color='rgb(255, 255, 255)', is_piecewise=True, item_width=50, item_height=30, textstyle_opts=opts.TextStyleOpts(font_size=30)), toolbox_opts=opts.ToolboxOpts( feature=opts.ToolBoxFeatureOpts( data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False), # brush=opts.ToolBoxFeatureBrushOpts(is_show=False), ) ), ) # geo.render(to_file) make_snapshot(snapshot, geo.render(to_file), svg_name) # 生成svg图片
def geoHeatMap(self,area=None,data=None,maptype='china',title='热力图示例',subtitle='副标题'): ''' 热力图 param area: 省份的列表 type list 例如 ['吉林', '辽宁', '河北', '河南'] param data: 对应的值 type list 例如 [63,10,2,30] 应用场景 省份或者城市排行榜 # ['黑龙江', '吉林', '辽宁', '河北', '河南', '山东', '山西', '安徽', '江西', '江苏', '浙江', '福建', '台湾', '广东', # '湖南', '湖北', '海南', '云南', '贵州', '四川', '青海', '甘肃', '陕西', '内蒙古', '新疆', '广西', '宁夏', '西藏', # '北京','天津','上海','重庆','香港','澳门'] ''' area = area or Faker.provinces data = data or Faker.values() c = ( Geo() .add_schema(maptype=maptype) .add( "geo", [list(z) for z in zip(area, data)], type_=ChartType.HEATMAP, ) # .set_series_opts(label_opts=opts.LabelOpts(is_show=True)) .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts( visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title=title,subtitle=subtitle), ) ) chart_name = f'static/chart{self.index}_heatmap' make_snapshot(snapshot,c.render(f'{chart_name}.html'),f'{chart_name}.png',is_remove_html=True) self.index += 1
def drawPie(self, wordDic): """ 饼图可视化 :param wordDic: 词频统计字典 :return: """ key_list = wordDic.keys() #wordDic所有key组成list value_list = wordDic.values() #wordDic所有value组成list def pie_position() -> Pie: c = ( Pie().add( "", [list(z) for z in zip(key_list, value_list)], # dic -> list center=["35%", "50%"], ).set_global_opts( title_opts=opts.TitleOpts(title='饼图可视化'), # 设置标题 legend_opts=opts.LegendOpts(pos_left="15%"), ).set_series_opts(label_opts=opts.LabelOpts( formatter="{b}: {c}"))) return c # 保存结果 make_snapshot(snapshot, pie_position().render(), SAVAPATH + '饼图可视化.png')
def make_chart(self,filename):#pyechart #变量此时的数据类型:<class 'numpy.ndarray'>,需转化为列表 (time,confirm,heal,dead,add,suspect)=np.loadtxt(filename,\ skiprows=1,\ dtype='str',\ delimiter=',',\ usecols=(0,1,2,3,4,5),\ unpack=True) #新建图表 chart =Line()#init_opts=opts.InitOpts(theme=ThemeType.LIGHT) #设置图表标题 chart.set_global_opts(title_opts={"text": "全国NCP疫情趋势图", "subtext": self.timeNum})# #设置x,y轴 chart.add_xaxis([i[-4:] for i in time]) chart.add_yaxis("确诊", list(confirm),symbol=" ",is_symbol_show=True)#,is_connect_nones=False chart.add_yaxis("治愈", list(heal)) chart.add_yaxis("死亡", list(dead)) chart.add_yaxis("新增确诊", list(add)) chart.add_yaxis("疑似病例", list(suspect)) chart.width=100 chart.page_title="NCP疫情动态" chart.theme="black" #保存为本地文件 # render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件,bar.render("mycharts.html") make_snapshot(snapshot, chart.render("main.html"), "main.png") os.startfile(os.getcwd()+"\\main.html")#打开文件
def job4(): class Data: city = ["淮南市"] values = [[3], [2], [2], [5], [5], [5], [1], [3], [-1], [-4]] def geo_hometown(title, i) -> Geo: c = (Geo().add_schema(maptype="淮南").add( title, [list(z) for z in zip(Data.city, Data.values[i])], type_=ChartType.HEATMAP).set_global_opts( visualmap_opts=opts.VisualMapOpts(min_=-4, max_=5, is_piecewise=True), title_opts=opts.TitleOpts(title="淮南市近十天温度变化情况"), )) return c for i in range(10): str_date = "12月" + str(i + 5) + "日" geo_hometown(str_date, i).render(str_date + ".html") make_snapshot(snapshot, geo_hometown(str_date, i).render(), str(i + 1) + ".png", pixel_ratio=1) frames = [] for i in range(10): frames.append(imageio.imread(str(i + 1) + ".png")) imageio.mimsave("huainan.gif", frames, 'GIF', duration=0.35)
def xiaoqu_wordcloud(self, collections): save_dir = self.path + "\\xiaoqu_wordcloud" if not os.path.exists(save_dir): os.mkdir(save_dir) for city in collections: xiaoqu = {} wordcloud = [] result = list(self.db[city].aggregate([ { '$match': { 'xiaoqu_name': { '$ne': '' } } }, { '$project': { 'xiaoqu_name': 1, '_id': 0 } }, ])) for x in result: xiaoqu[x['xiaoqu_name']] = 0 for x in result: xiaoqu[x['xiaoqu_name']] += 1 for key, value in xiaoqu.items(): wordcloud.append((key, value)) xiaoqu_wordcloud = self.charts.wordcloud_diamond( wordcloud, title='{}市二手房热门小区'.format(city)) make_snapshot( snapshot, xiaoqu_wordcloud.render(), "{}\\{}.gif".format(save_dir, '{}市二手房热门小区'.format(city))) print("完成{}市热门小区词云".format(city))
def geo_getmap() -> Geo: c = ( Geo().add_schema( maptype="china", itemstyle_opts=opts.ItemStyleOpts(color="#323c48", border_color="#111"), ).add( "", allcases, type_=ChartType.EFFECT_SCATTER, # symbol_size="size", color="white", ).add( "", traces, type_=ChartType.LINES, # effect_opts=opts.EffectOpts( # symbol=SymbolType.ARROW, symbol_size=6, color="red" # ), linestyle_opts=opts.LineStyleOpts(curve=0.2), ).set_series_opts(label_opts=opts.LabelOpts( is_show=False)).set_global_opts(title_opts=opts.TitleOpts( title="2019-nCoV Trace Map(China)"))) make_snapshot(snapshot, c.render(), "tracemap.png")
def Conversion_rates(df): ''' 目标:订单每个环节的转化转化率 :param df: :return: ''' rates = pd.Series( { '创建': df['订单创建时间'].count(), '付款': df['订单付款时间'].count(), '实际成交': df[df['买家实际支付金额'] > 0].shape[0], '全额成交': df[df['买家实际支付金额'] == df['总金额']].shape[0], }, name='订单量').to_frame() # 绝对转化率=各环节订单数/订单创建数 rates['整体转化率'] = rates['订单量'].apply( lambda x: round(x * 100 / rates.iloc[0, 0], 3)) # 相对转化率=各项指标/上一个流程的指标 # rates['相对转化率']=(rates/rates.shift())['订单量'].fillna(1) print(f'\n{"-"*5}各环节绝对转化率(%){"-"*5}\n') print(rates) c = ( Funnel().add( '转化率', [list(z) for z in zip(rates.index, rates['整体转化率'])], # 设置标签位置及数据展现形式 label_opts=opts.LabelOpts( position='inside', formatter='{b}:{c}')).set_global_opts( title_opts=opts.TitleOpts(title='整体转化率(%)'))) # 转存 make_snapshot(snapshot, c.render(), '转化率1.png')
def html_echarts(self, html, output_name): # 选择要保存的图片的名字,需要添加后缀 # filename不允许添加路径,必须在当前目录吓的html文件 make_snapshot(snapshot, html, output_name)
def chart(self): # noinspection PyAttributeOutsideInit self.list_defect_cont = sorted(self.list_defect_cont, key=(lambda x: [x[1]]), reverse=True) x_axis = [] for i in range(0, 6): x_axis.append(self.list_defect_cont[i][0]) y_axis = [] for i in range(0, 6): y_axis.append(self.list_defect_cont[i][1]) bar = (Bar(init_opts=opts.InitOpts( bg_color='rgba(255,250,250,0.2)', width='1000px', height='450px', page_title='Defect Monitor')).add_xaxis(x_axis).add_yaxis( "Defect", y_axis, category_gap="20%", gap="0%").set_global_opts( title_opts=opts.TitleOpts( title="In the last 50 pcs, Defect Monitor"), xaxis_opts=opts.AxisOpts( name="Defect Code", axislabel_opts=opts.LabelOpts(rotate=-15)), yaxis_opts=opts.AxisOpts( name="单位:Pcs", max_=50)).set_series_opts( label_opts=opts.LabelOpts(is_show=True), markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(y=25, name="阈值=25")]))) make_snapshot(snapshot, bar.render('./source_file/defect_chart.html'), "./source_file/defect_chart.png")
def image_host_apps(doc_path, host_apps, host): if host_apps.get(host['id']) is None: return data_inner = [] data_outer = [] for i in host_apps.get(host['id']): number = 0 for x in i.get('apps'): number = number + x.get('number') data_outer.append([x.get('name'), x.get('number')]) data_inner.append(["★%s" % (i.get('user')), number]) options = opts.InitOpts(js_host=os.path.join(os.getcwd(), "scripts/javascript/"), animation_opts=opts.AnimationOpts(animation=False)) pipe = ( Pie(options) .add('', data_inner, radius=["0%", "30%"], label_opts=opts.LabelOpts(formatter='{b}')) .add('', data_outer, radius=['55%', '80%'], label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)', font_size=13)) .set_global_opts(legend_opts=opts.LegendOpts(orient='vertical', pos_left=0), title_opts=opts.TitleOpts(title="应用分布", pos_left='center')) ) make_snapshot(snapshot, pipe.render(os.path.join(doc_path, "render_%s.html" % (host['ip'].replace(".", "_")))), os.path.join(doc_path, "host_app_%s.png" % (host['ip'].replace(".", "_"))), pixel_ratio=1, delay=1, is_remove_html=True)
def create_grid_bar_and_bar(title, x_list, data_list, data_list_2, data_unit_list): # 1.先生成上方的柱状图 bar1 = Bar() bar1.add_xaxis(x_list) for key in data_list.keys(): bar1.add_yaxis(key, data_list[key]) bar1.set_global_opts(title_opts=opts.TitleOpts(title=title), legend_opts=opts.LegendOpts(pos_left="25%", pos_top="5%"), yaxis_opts=opts.AxisOpts(name="单位:" + data_unit_list[0] , is_scale=True)) # 2.下方柱状图 bar2 = Bar() bar2.add_xaxis(x_list) for key in data_list_2.keys(): bar2.add_yaxis(key, data_list_2[key]) bar2.set_global_opts(title_opts=opts.TitleOpts(title=title), legend_opts=opts.LegendOpts(pos_left="25%", pos_top="55%"), yaxis_opts=opts.AxisOpts(name="单位:" + data_unit_list[1], is_scale=True)) # 3.生成最终的组合图 c = ( Grid(init_opts=opts.InitOpts(theme=THEME_TYPE,height="700px")) .add(bar1, grid_opts=opts.GridOpts(pos_bottom="60%")) .add(bar2, grid_opts=opts.GridOpts(pos_top="60%")) ) src_path = "./指标-年度-图片生成/" html_file_name = src_path + title + ".html" img_file_name = src_path + title + ".png" make_snapshot(snapshot, c.render(html_file_name), img_file_name) print(img_file_name+"生成完毕...")
def create_mulline(): # 反序列化 data = request.get_data() data = json.loads(data) returnres = {"state": 200, "msg": "succsuful", "result": ''} print(data) try: schema = mullineSchema() data = schema.load(data) data = schema.dump(data) print(data) except ValidationError as err: error = err.messages print(error) returnres['result'] = error return jsonify(returnres) line = ( Line(init_opts=opts.InitOpts(theme=ThemeType.DARK)) .add_xaxis(data['xaxis']) .set_global_opts( title_opts=opts.TitleOpts(title=data['maintitle']), ) .set_colors() .set_series_opts() ) for i in range(len(data['typelist'])): line.add_yaxis(data['typelist'][i],data['yaxis'][i]) # 输出保存为图片 imagesname = str(uuid.uuid4()) make_snapshot(engine=snapshot, file_name=line.render(), output_name="../charts/image/%s.png" % imagesname,is_remove_html=True) image_data = open("../charts/image/%s.png" % imagesname, "rb").read() result = base64.b64encode(image_data) returnres = {"state": 200, "msg": "succsuful", "result": result} return jsonify(returnres)
def build_bar_to_pic(): bar = ( Bar() .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]) .add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) ) make_snapshot(snapshot, bar.render(), "pycharts_bar_to_pic.png")
def create_base_pie(title, data_list): """ :param title: 图表名称 :param dic: 字典{key1:[value1,value2...],key2:[value1,value2...]...},构成多条不同的折线 :return: 生成最新一期、各指标数据占比情况饼状图 """ # 1.数据格式转换 key_list = [] value_list = [] for key in data_list.keys(): key_list.append(key) value_list.append(data_list[key][len(data_list[key]) - 1]) data_pair = [list(z) for z in zip(key_list, value_list)] # 2.计算各项指标的占比 sum = 0 for x in data_pair: if x[1] is not None: sum += x[1] for x in data_pair: if x[1] is not None: # sum不为0,这在指标选取时规避 x[1] = round(x[1]/sum*100,2) # 3.开始生成饼状图 c = ( Pie(init_opts=opts.InitOpts(theme=THEME_TYPE,width="700px")) .add(series_name="占比情况", data_pair=data_pair,center=["40%", "50%"],radius=["0","60%"]) .set_global_opts(title_opts=opts.TitleOpts(title=title), legend_opts=opts.LegendOpts(pos_left="80%",orient="vertical")) .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}%" )) ) src_path = "./指标-年度-图片生成/" html_file_name = src_path + title + ".html" img_file_name = src_path + title + ".png" make_snapshot(snapshot, c.render(html_file_name), img_file_name) print(img_file_name+"生成完毕...")
def tag_wordcloud(self, collections): save_dir = self.path + sep + "tag_wordcloud" if not os.path.exists(save_dir): os.mkdir(save_dir) for city in collections: word = {} wordcloud = [] result = list(self.db[city].aggregate([{ '$match': { 'tag': { '$ne': [] } } }, { '$project': { 'tag': 1, '_id': 0 } }])) for tag in result: for i in tag['tag']: word[i] = 0 for tag in result: for i in tag['tag']: word[i] += 1 for key, value in word.items(): wordcloud.append((key, value)) tag = self.charts.wordcloud_diamond(wordcloud, title='{}市新房热门标签'.format(city)) make_snapshot(snapshot, tag.render(), img_name.format(save_dir, '{}市新房热门标签'.format(city))) print("完成{}市热门标签".format(city))
def plot_map(list_data, df_data, title, filepath_save): """ 绘制地图 @param list_data: @param df_data: @param title: @param filepath_save: @return: """ # 绘制地图 ncp_map = ( Map(init_opts=opts.InitOpts('1000px', '600px')).add( '', list_data, 'china').set_global_opts( title_opts=opts.TitleOpts(title=title, pos_left='center'), visualmap_opts=opts.VisualMapOpts( # 设置为分段形数据显示 is_piecewise=True, # 设置拖拽用的手柄 is_calculable=True, # 设置数据最大值 max_=df_data['sum_diagnose'].max(), # 自定义的每一段的范围,以及每一段的文字,以及每一段的特别的样式。 pieces=[{ 'min': 10001, 'label': '>10000', 'color': '#4F040A' }, { 'min': 1000, 'max': 10000, 'label': '1000 - 10000', 'color': '#811C24' }, { 'min': 500, 'max': 999, 'label': '500 - 999', 'color': '#CB2A2F' }, { 'min': 100, 'max': 499, 'label': '100 - 499', 'color': '#E55A4E' }, { 'min': 10, 'max': 99, 'label': '10 - 99', 'color': '#F59E83' }, { 'min': 1, 'max': 9, 'label': '1 - 9', 'color': '#FDEBCF' }, { 'min': 0, 'max': 0, 'label': '0', 'color': '#F7F7F7' }]), )) # 保存图片到本地 make_snapshot(snapshot, ncp_map.render(), filepath_save)
def huxing_count(self, collections): save_dir = self.path + sep + "huxing_count" if not os.path.exists(save_dir): os.mkdir(save_dir) for city in collections: huxing = list(self.db[city].aggregate([{ '$match': { 'huxing': { '$ne': '' } } }, { '$group': { '_id': '$huxing', 'huxing_count': { '$sum': 1 } } }])) huxing_key = [i['_id'] for i in huxing] huxing_value = [i['huxing_count'] for i in huxing] huxing_bar = self.charts.bar(huxing_key, huxing_value, "", huxing_count_template.format(city), "") make_snapshot( snapshot, huxing_bar.render(), img_name.format(save_dir, huxing_count_template.format(city))) print("finish {} huxing_count".format(city))
def square_meter_min_top5(self, collections): save_dir = self.path + sep + "square_meter_min_top5" if not os.path.exists(save_dir): os.mkdir(save_dir) for city in collections: min_top5 = list(self.db[city].aggregate([{ '$match': { 'main_price': { '$ne': -1 }, 'main_price_desc': '元/平(均价)' } }, { '$sort': { 'main_price': 1 } }, { '$limit': 5 }, { '$project': { 'main_price': 1, 'loupan': 1, '_id': 0 } }])) if min_top5: key = [i['loupan'] for i in min_top5] value = [i['main_price'] for i in min_top5] min_top5_scatter = self.charts.scatter_spliteline( key, value, city, square_price_min_top5.format(city)) make_snapshot( snapshot, min_top5_scatter.render(), img_name.format(save_dir, square_price_min_top5.format(city))) print('finish {} min_top5'.format(city))
def analysis_price_sales(): """ 商品价格与销量关系分析 :return: """ # 引入全局数据 global DF_STANDARD df = DF_STANDARD.copy() df['group'] = pd.qcut(df.price, 12) df.group.value_counts().reset_index() df_group_sales = df[['sales', 'group']].groupby('group').mean().reset_index() df_group_str = [str(i) for i in df_group_sales['group']] print(df_group_str) # 生成柱状图 bar = (Bar().add_xaxis(df_group_str).add_yaxis( "", list(df_group_sales['sales']), category_gap="50%").set_global_opts( title_opts=opts.TitleOpts( title="{}价格分区与平均销量柱状图".format(goodsSearchName)), yaxis_opts=opts.AxisOpts(name="价格区间"), xaxis_opts=opts.AxisOpts(name="平均销量", axislabel_opts={"rotate": 30}))) file_name = os.getcwd() + "/{}价格分区与平均销量柱状图.png".format(goodsSearchName) make_snapshot( snapshot, bar.render(), "{}价格分区与平均销量柱状图.png".format(goodsSearchName), ) shutil.move(file_name, topdeskPath)
def wuye_type_count(self, collections): save_dir = self.path + sep + "wuye_type_count" if not os.path.exists(save_dir): os.mkdir(save_dir) for city in collections: wuye_type = list(self.db[city].aggregate([{ '$match': { 'wuye_type': { '$ne': '' } } }, { '$group': { '_id': '$wuye_type', 'wuye_type_count': { '$sum': 1 } } }])) wuye_key = [i['_id'] for i in wuye_type] wuye_value = [i['wuye_type_count'] for i in wuye_type] wuye_bar = self.charts.bar(wuye_key, wuye_value, "", wuye_type_count_template.format(city), "") make_snapshot( snapshot, wuye_bar.render(), img_name.format(save_dir, wuye_type_count_template.format(city))) print("finish {} wuye_type_count".format(city))
def save_img(self, fp=None, page=None): if fp is None: fp = 'result.png' if page is None: fp = 'render.html' make_snapshot(snapshot, self.graph.render(page), fp)
def process_item(self, item, spider): cur = self.cursor select_sql = "select rate from test" cur.execute(select_sql) ret = cur.fetchall() #print(ret) xli = [] yli = [0] * 100 for i in arange(1, 101): xli.append(str(float(i) / 10)) for j in ret: yli[int(float(j[0]) * 10)] += 1 #print(xli) #print(yli) l = ( Line().add_xaxis(xli) #x轴坐标点必须是string类型 .add_yaxis("电视剧部数", yli, is_smooth=True).set_global_opts( title_opts=opts.TitleOpts(title="电视剧评分分布图"))) # 输出保存为图片 make_snapshot(snapshot, l.render("actor.html"), "D:\\Design\\Spider\\actor.png") # 保存路径可以自定义,输入图片文件的速度较慢 ,可以先输出网页,测试成功后,再转成图片 print("已生成图片") cur.close() # 关闭游标 self.conn.close() # 关闭连接
def position_wordcloud(self, collections): save_dir = self.path + sep + "position_wordcloud" if not os.path.exists(save_dir): os.mkdir(save_dir) for city in collections: position = {} wordcloud = [] result = list(self.db[city].aggregate([ { '$match': { 'position': { '$ne': '' } } }, { '$project': { 'position': 1, '_id': 0 } }, ])) for x in result: position[x['position']] = 0 for x in result: position[x['position']] += 1 for key, value in position.items(): wordcloud.append((key, value)) position_wordcloud = self.charts.wordcloud_diamond( wordcloud, title='{}市二手房热门地段'.format(city)) make_snapshot(snapshot, position_wordcloud.render(), img_name.format(save_dir, '{}市二手房热门地段'.format(city))) print("完成{}市二手房热门地段词云".format(city))
def draw_pie_picture(data, to_file, svg_name, colors=None): c = Pie(init_opts=opts.InitOpts(width="1600px", height="900px", bg_color='white')) \ .add("", data) \ .set_global_opts(legend_opts=opts.LegendOpts(is_show=False), toolbox_opts=opts.ToolboxOpts(is_show=True)) \ .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}", font_size=25)) \ .set_colors(colors) make_snapshot(snapshot, c.render(to_file), svg_name) # 生成svg图片
def display_recent_global_distribution(pic_file, maxCount=200, **kwargs): """ display the distribution of recent total numbers of confirmed patients. Parameters ---------- pic_file: str absolute path of the generated figure. maxCount: int maximumn count of colorbar. (default: 200) """ conn = db.connect(dbFile) cu = conn.cursor() OverallDf = pd.read_sql_query("""select * from Region_Data""", conn) OverallDf['updateTime'] = OverallDf['updateTime'].astype('int64') recentData = OverallDf.groupby('provinceShortName').apply( lambda t: t[t['updateTime'] == t['updateTime'].max()]) recentData = recentData.groupby('country').agg({ 'confirmedCount': 'sum', 'suspectedCount': 'sum', 'updateTime': 'mean' }) recentData['date'] = pd.to_datetime(recentData['updateTime'] / 1000, unit='s') time = recentData[recentData.index == '中国']['updateTime'] data = [[ searchCountryENName(recentData.index[i]), int(recentData['confirmedCount'][i]) ] for i in range(recentData.shape[0])] map_3 = Map() map_3.add("{0} worldwide COVID-19 patients distribution".format( recentData['date'][1].strftime('%Y-%m-%d')), data, maptype='world', is_map_symbol_show=False) map_3.set_series_opts( label_opts=opts.LabelOpts(is_show=False, font_size=100)) map_3.set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=maxCount), title_opts=opts.TitleOpts(title="")) if 'notebook' in kwargs.keys(): if kwargs['notebook']: map_3.render_notebook() else: html_file = '{0}.html'.format(os.path.splitext(pic_file)[0]) tmpHtmlFile = map_3.render() shutil.move(tmpHtmlFile, html_file) make_snapshot(snapshot, file_name=html_file, output_name=pic_file, is_remove_html=False, **kwargs)