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.options.get("baseOption").get("series")) == 0 timeline.render()
def test_pie_type_radius(): pie = Pie("饼图示例", title_pos='center', width=1000, height=600) pie.add("", ['A', 'B', 'C', 'D', 'E', 'F'], [335, 321, 234, 135, 251, 148], radius=[40, 55], is_label_show=True) pie.add("", ['H', 'I', 'J'], [335, 679, 204], radius=[0, 30], legend_orient='vertical', legend_pos='left') pie.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 年') timeline.render()
def test_grid_line_pie(): line = Line("折线图示例", width=1200) line.add( "最高气温", WEEK, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"], ) line.add( "最低气温", WEEK, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_pos="20%", ) v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图示例", title_pos="55%") pie.add( "", CLOTHES, v1, radius=[45, 65], center=[65, 50], legend_pos="80%", legend_orient="vertical", ) grid = Grid() grid.add(line, grid_right="55%") grid.add(pie, grid_left="60%") grid.render()
def test_pie_type_rose(): v1 = [11, 12, 13, 10, 10, 10] v2 = [19, 21, 32, 20, 20, 33] pie = Pie("饼图-玫瑰图示例", title_pos="center", width=900) pie.add( "商品A", CLOTHES, v1, center=[25, 50], is_random=True, radius=[30, 75], rosetype="radius", ) pie.add( "商品B", CLOTHES, v2, center=[75, 50], is_random=True, radius=[30, 75], rosetype="area", is_legend_show=False, is_label_show=True, ) pie.render()
def get_pie(item_name,item_name_list,item_num_list): totle = item_num_list[0]+item_num_list[1]+item_num_list[2] subtitle = "共有:%d个好友"%totle pie = Pie(item_name,page_title = item_name,title_text_size=30,title_pos='center',\ subtitle = subtitle,subtitle_text_size = 25,width=800,height= 800) pie.add("", item_name_list, item_num_list,is_label_show=True,center=[50, 45],radius=[0,50],\ legend_pos ='left',legend_orient='vertical',label_text_size=20) out_file_name = './analyse/'+item_name+'.html' #print(out_file_name) pie.render(out_file_name)
def test_pie_legend(): v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图-圆环图示例", title_pos="center") pie.add( "", CLOTHES, v1, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient="vertical", legend_pos="left", ) pie.render()
def test_page(): page = Page() line = Line("折线图示例") line.chart_id = "id_my_cell_line" line.add( "最高气温", WEEK, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"], ) line.add( "最低气温", WEEK, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], ) # pie v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图-圆环图示例", title_pos="center", width="600px") pie.add( "", CLOTHES, v1, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient="vertical", legend_pos="left", ) page.add([line, pie, create_a_bar(TITLE)]) # Start render and test html = page._repr_html_() # Test base html structure assert html.count("<script>") == html.count("</script>") == 2 assert html.count("<div") == html.count("</div>") == 3 assert html.count("require.config") == html.count("function(echarts)") == 1 # Test some chart attributes json_encoded_title = json.dumps(TITLE) assert json_encoded_title in html assert "nbextensions/echarts" in html # default jshost assert html.count("height:400px") == 3 assert html.count("width:600px") == 1 assert html.count("width:800px") == 2 assert html.count("id_my_cell_line") == 6
def draw_citys_pic(csv_file): page = Page(csv_file+":评论城市分析") info = count_city(csv_file) geo = Geo("","Ctipsy原创",title_pos="center", width=1200,height=600, background_color='#404a59', title_color="#fff") while True: # 二次筛选,和pyecharts支持的城市库进行匹配,如果报错则删除该城市对应的统计 try: attr, val = geo.cast(info) geo.add("", attr, val, visual_range=[0, 300], visual_text_color="#fff", is_geo_effect_show=False, is_piecewise=True, visual_split_number=6, symbol_size=15, is_visualmap=True) except ValueError as e: e = str(e) e = e.split("No coordinate is specified for ")[1] # 获取不支持的城市名称 info.pop(e) else: break info = sorted(info.items(), key=lambda x: x[1], reverse=False) # list排序 # print(info) info = dict(info) #list转dict # print(info) attr, val = [], [] for key in info: attr.append(key) val.append(info[key]) geo1 = Geo("", "评论城市分布", title_pos="center", width=1200, height=600, background_color='#404a59', title_color="#fff") geo1.add("", attr, val, visual_range=[0, 300], visual_text_color="#fff", is_geo_effect_show=False, is_piecewise=True, visual_split_number=10, symbol_size=15, is_visualmap=True, is_more_utils=True) #geo1.render(csv_file + "_城市dotmap.html") page.add_chart(geo1) geo2 = Geo("", "评论来源热力图",title_pos="center", width=1200,height=600, background_color='#404a59', title_color="#fff",) geo2.add("", attr, val, type="heatmap", is_visualmap=True, visual_range=[0, 50],visual_text_color='#fff', is_more_utils=True) #geo2.render(csv_file+"_城市heatmap.html") # 取CSV文件名的前8位数 page.add_chart(geo2) bar = Bar("", "评论来源排行", title_pos="center", width=1200, height=600 ) bar.add("", attr, val, is_visualmap=True, visual_range=[0, 100], visual_text_color='#fff',mark_point=["average"],mark_line=["average"], is_more_utils=True, is_label_show=True, is_datazoom_show=True, xaxis_rotate=45) #bar.render(csv_file+"_城市评论bar.html") # 取CSV文件名的前8位数 page.add_chart(bar) pie = Pie("", "评论来源饼图", title_pos="right", width=1200, height=600) pie.add("", attr, val, radius=[20, 50], label_text_color=None, is_label_show=True, legend_orient='vertical', is_more_utils=True, legend_pos='left') #pie.render(csv_file + "_城市评论Pie.html") # 取CSV文件名的前8位数 page.add_chart(pie) page.render(csv_file + "_城市评论分析汇总.html")
def test_pie_type_radius(): pie = Pie("饼图示例", title_pos="center", width=1000, height=600) pie.add( "", ["A", "B", "C", "D", "E", "F"], [335, 321, 234, 135, 251, 148], radius=[40, 55], is_label_show=True, ) pie.add( "", ["H", "I", "J"], [335, 679, 204], radius=[0, 30], legend_orient="vertical", legend_pos="left", ) pie.render()
def test_pie_multiple(): import random attr = ['A', 'B', 'C', 'D', 'E', 'F'] pie = Pie("饼图示例", width=1000, height=600) pie.add("", attr, [random.randint(0, 100) for _ in range(6)], radius=[50, 55], center=[25, 50], is_random=True) pie.add("", attr, [random.randint(20, 100) for _ in range(6)], radius=[0, 45], center=[25, 50], rosetype='area') pie.add("", attr, [random.randint(0, 100) for _ in range(6)], radius=[50, 55], center=[65, 50], is_random=True) pie.add("", attr, [random.randint(20, 100) for _ in range(6)], radius=[0, 45], center=[65, 50], rosetype='radius') pie.render()
def get_pie(item_name, item_name_list, item_num_list): totle = item_num_list[0] + item_num_list[1] + item_num_list[2] subtitle = '一共有:%d个好友' % totle pie = Pie(item_name, page_title=item_name, title_text_size=30, title_pos='center', subtitle=subtitle, subtitle_text_size=25, width=1000, height=1000) pie.add('', item_name_list, item_num_list, is_label_show=True, center=[50, 45], radius=[0, 50], legend_pos='left', legend_orient='vertical', label_text_size=20) out_file_name = './analyse/' + item_name + '.html' pie.render(out_file_name)
def get(self, request, *args, **kwargs): connect = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_SPACE, password=settings.REDIS_PASSWD) SYSTEMTYPE_STATUS = str(connect.get('SYSTEMTYPE_STATUS'), encoding='utf-8').replace('\'', '"') try: SYSTEMTYPE_STATUS = json.loads(SYSTEMTYPE_STATUS) except TypeError as e: SYSTEMTYPE_STATUS = {"name": [], "value": []} from pyecharts import Pie from pyecharts.base import TRANSLATOR p = Pie('操作系统类型', title_pos='center') p.add( "操作系统", SYSTEMTYPE_STATUS.get('name'), SYSTEMTYPE_STATUS.get('value'), radius=[20, 65], legend_orient='vertical', ) #is_legend_show=False,) snippet = TRANSLATOR.translate(p.options) return Response(json.loads((snippet.as_snippet())), status.HTTP_200_OK)
def Rose(): def cut(value): values = value.split(',') return values[0] datas = file['types'].apply(cut) attr = datas.value_counts().index.tolist()[0:9] valu = datas.value_counts().tolist()[0:9] pie = Pie("", title_pos='center') pie.add( "", attr, valu, radius=[30, 75], label_text_color='', is_label_show=True, legend_orient="", #图例的方向 vertical 垂直 legend_pos="", #图例的位置 rosetype='area', ) pie.render('./Echarts/rose.html')
def statistics_friends(): # 初始化 unknown, known_male, known_female, known_other = 0, 0, 0, 0 # 遍历 for user in friends: # 备注不为空 if((user.remark_name).strip()): if(user.sex == 1): known_male += 1 elif(user.sex == 2): known_female += 1 else: known_other += 1 else: unknown += 1 name_list = ['未设置备注的好友', '设置备注的男性好友', '设置备注的女性好友', '设置备注的其他好友'] num_list = [unknown, known_male, known_female, known_other] pie = Pie("你认识的好友比例", title_pos='center') pie.add("", name_list, num_list, is_label_show=True, legend_orient="vertical", legend_pos="left") pie.render('data/你认识的好友比例.html')
def unicorn_world2(data): data = data.groupby(by='Country').sum() data = data.sort_values(by='Valuation', ascending=False) data2 = data[:5]['Valuation'] # 前5国家 data3 = data[5:]['Valuation'].sum() # 后5国家全部划分为其他 data2.loc['Else'] = data3 attr = list(data2.index) value = list(data2.values) chart = Pie( '各国独角兽公司估值对比', '中美占据 80%', title_pos='center', ) chart.add('', attr, value, is_label_show=True, is_legend_show=False, ) chart.render(path='各国独角兽公司市值对比.png')
def s_compyr(): try: usertype = session['type'] if usertype == "staff": cursor = conn.cursor() query = 'SELECT sum(price) FROM purchases, ticket, flight WHERE purchases.ticket_id = ticket.ticket_id AND ticket.airline_name = flight.airline_name AND ticket.flight_num = flight.flight_num AND booking_agent_id is null AND (purchase_date BETWEEN DATE_SUB(CURRENT_DATE(),INTERVAL 1 YEAR) AND CURRENT_DATE())' cursor.execute(query) direct = cursor.fetchone() cursor.close() cursor = conn.cursor() query = 'SELECT sum(price) FROM purchases, ticket, flight WHERE purchases.ticket_id = ticket.ticket_id AND ticket.airline_name = flight.airline_name AND ticket.flight_num = flight.flight_num AND booking_agent_id is not null AND (purchase_date BETWEEN DATE_SUB(CURRENT_DATE(),INTERVAL 1 YEAR) AND CURRENT_DATE())' cursor.execute(query) indirect = cursor.fetchone() cursor.close() xpie = ['direct to customer', '3rd party'] ypie = [] print(direct, indirect) for key in direct: if direct[key] == None: ypie.append(0) else: ypie.append(int(direct[key])) for key in indirect: if indirect[key] == None: ypie.append(0) else: ypie.append(int(indirect[key])) pie = Pie('Revenue in last year') pie.add('', xpie, ypie, is_label_show=True) return render_template('s_compyr.html', myechart=pie.render_embed(), host=REMOTE_HOST, script_list=pie.get_js_dependencies()) else: return render_template('error.html') except KeyError: return render_template('error.html')
def level_score_visualization(userLevel, score): from pyecharts import Pie userLevel_list = list(set(userLevel)) userLevel_num = [] for i in range(len(userLevel_list)): userLevel_num.append(userLevel.count(userLevel_list[i])) score_list = list(set(score)) score_num = [] for i in range(len(score_list)): score_num.append(score.count(score_list[i])) pie01 = Pie("等级环状饼图", title_pos='center', width=900) pie01.add( "等级", userLevel_list, userLevel_num, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient="vertical", legend_pos="left", ) pie01.render("H:\PyCoding\spider_maoyan\picture\level_pie.html") pie02 = Pie("评分玫瑰饼图", title_pos='center', width=900) pie02.add( "评分", score_list, score_num, center=[50, 50], is_random=True, radius=[30, 75], rosetype="area", is_legend_show=False, is_label_show=True, ) pie02.render("H:\PyCoding\spider_maoyan\picture\score_pie.html")
def draw_charts(self): data = xlrd.open_workbook('%s%s表格.xls' % (self.city, self.search)) table = data.sheets()[0] ops = ['district', 'education', 'salary', 'companyshortname'] for op in ops: INDEX = table.row_values(0).index(op) statistics = {} for dis in table.col_values(INDEX)[1:]: if dis not in statistics: statistics[dis] = 1 else: statistics[dis] += 1 c = Pie(self.city + "地区%s %s 图表" % (self.search, op), title_pos='center') keys = list(statistics.keys()) values = list(statistics.values()) top10_values = [] top10_keys = [] if op == 'salary' or op == 'companyshortname': for _ in range(10): index = values.index(max(values)) top10_values.append(values[index]) top10_keys.append(keys[index]) values.pop(index) keys.pop(index) keys = top10_keys values = top10_values c.add( "", keys, values, is_label_show=True, legend_orient="vertical", legend_pos="left", ) c.render(self.city + "%s %s charts.html" % (self.search, op))
def movietypeem(movietype='传记'): type_movie_critic_all = all_movie_type() tmtresult = type_movie_critic_all[0] mtcrresult = type_movie_critic_all[1] typemovie = re.compile(movietype) page = Page() moviename = [] mvgr = [] mvmr = [] mvbr = [] for key, values in tmtresult.items(): type = re.match(typemovie, key) if type: for value in values[0]: moviename.append(value[0]) mvgr.append(value[1]) mvmr.append(value[2]) mvbr.append(value[3]) bar = Bar(movietype) bar.add("好评率", moviename, mvgr, mark_point=["average"]) bar.add("中评率", moviename, mvmr) bar.add("差评率", moviename, mvbr) page.add(bar) pie = Pie("电影类型评论对比图") for tkey, tvalue in mtcrresult.items(): type = re.match(typemovie, tkey) if type: pie.add( "", [tkey, "好评", "中评", "差评"], [tvalue[3], tvalue[0], tvalue[1], tvalue[2]], is_label_show=True, ) #legend_top="bottom") page.add(pie) return page
def chart_view03(): # 从mongo中获取数据 (x, y) = BidNotice.get_records_group_by_notice_type() # 设置bar图形的基本属性 pie = Pie("公告类型分析图", "From: cmccb2b") pie.use_theme('light') pie.add("公告类型", x, y, is_stack=False) # 从Mongo读取的数据存放在这里 javascript_snippet = TRANSLATOR.translate(pie.options) # 将bar的属性设置翻译为js脚本 # pie.print_echarts_options() # 该行只为了打印配置项,方便调试时使用 return render_template( "pyecharts.html", # 自定义,位于templates/的模版文件 chart_id=pie.chart_id, # 默认设置, host=REMOTE_HOST, # 常量定义,存放js文件的url地址 renderer=pie.renderer, # 默认设置, my_width="100%", # 默认设置,定义图表的宽度 my_height=600, # 默认设置,定义图表的高度 custom_function=javascript_snippet. function_snippet, # 默认设置,保存图片的方法,似乎基于node.js options=javascript_snippet.option_snippet, # 默认设置, script_list=pie.get_js_dependencies(), # 默认设置,需要动态加载的js文件 )
def StarMap(self): ''' Get a pie map :return: ''' score = dict( Counter( map(self.GetStars, self.GetOneCol(name="comment_score", method="average")))) attr, value = Geo.cast(score) pie = Pie(self.title, "数据来源:豆瓣电影", title_pos="center", width=900) pie.add("", attr, value, center=[50, 50], is_random=True, radius=[30, 75], rosetype="area", is_legend_show=False, is_label_show=True) if self.saved_file_type is None: pie.render(os.path.join(self.path, "StarMap.png")) elif self.saved_file_type == "html": pie.render(os.path.join(self.path, "StarMap.html"))
def pie(title, attr, values): """ 输出Pie图 :param title: 标题 :param attr: 属性 :param values: 数据 :return: 图形 """ pie = Pie(title, title_pos='center', width=300) for _value in values: pie.add(title, attr, _value, center=[50, 50], is_random=False, radius=[20, 45], rosetype='rose', is_legend_show=False, is_label_show=True, background_color='#b0bab9', label_text_size=12) pie.options['toolbox']['show'] = False return pie.render_embed()
def educaAnalysis(self, items): educa_data = items['education'] educalist_data = set(educa_data) print(educalist_data) edupie_list = [] edupie_data = [] for item in educalist_data: edupie_list.append(item) edupie_data.append(educa_data.count(item)) print(edupie_list) print(edupie_data) pie = Pie("重庆医疗健康招聘学历要求", title_pos='center') pie.add( "学历", edupie_list, edupie_data, center=[50, 50], is_random=True, radius=[30, 75], rosetype="area", is_legend_show=False, is_label_show=True, ) pie.render()
def get_pie(item_name, item_name_list, item_num_list): totle = item_num_list[0] + item_num_list[1] + item_num_list[2] subtitle = "共有:%d个好友" % totle pie = Pie(item_name,page_title = item_name,title_text_size=30,title_pos='center',\ subtitle = subtitle,subtitle_text_size = 25,width=800,height= 800) # pie =Pie(item_name) # pie=Pie() # .add("", [list(z) for z in zip(Faker.choose(), Faker.values())]) # .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例")) # .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) # pie = Pie(item_name) # print(pie) pie.add("", item_name_list, item_num_list,is_label_show=True,center=[50, 45],radius=[0,50],\ legend_pos ='left',legend_orient='vertical',label_text_size=20) out_file_name = './analyse/' + item_name + '.html' #print(out_file_name) pie.render(out_file_name)
def drawOnePie(self, fid, attrExprList, divideExprList, diseaseNameList, dbName, year, isPercent, complete): try: dataList = [] dataNow = self.dbInf.getDiffDistrictData(attrExprList, divideExprList, diseaseNameList, dbName, "all", year, isPercent, complete) dataNow = [x[0] for x in dataNow] data = [x[1] for x in dataNow] dataList.append(data) large = 1.3 size = 0.8 pie = Pie("", title_pos='center', width=800 * size, height=480 * size) pie.add("", diseaseNameList, dataList[0], radius=[0 * large, 45 * large], legend_orient='vertical', is_label_show=True, legend_pos='left', label_formatter='{d}%', label_pos='left') pie.render() make_a_snapshot('render.html', fid + '.png') print("图片生成完成:" + fid) except Exception as e: print(traceback.print_exc())
def renovation_pie(self): data = pd.read_csv(self.all_house_path, encoding='utf-8') mpl.rcParams['font.sans-serif'] = ['FangSong' ] # 指定默认字体 # 调节matplotlib支持中文字体 mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示不了的问题 count = data['renovation'].value_counts() # attr = count.index attr = list(count.index) values = list(count.values) # print(attr) # print(values) pie = Pie('广州租房装修比重', title_pos='65%') pie.add("", attr, values, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient="vertical", legend_pos="left", rosetype='radius') pie.render(path='pie_decorated.html')
def small_class_avg_salary(bigclass): print("正在计算...") data = sel_small_class_avg_salary() attr = [] avg_salary_list = [] small_class_count = [] for i in data: if i[0] == "%s" % bigclass: attr.append(i[1]) avg_salary_list.append(i[2]) small_class_count.append(i[3]) print(attr, avg_salary_list, small_class_count) pie = Pie( "%s岗位各类别薪资(左)与职位数量(右)统计" % bigclass, title_pos='top', width=1200, background_color='#F4F3EF', ) pie.add("平均薪资统计", attr, avg_salary_list, center=[25, 50], is_random=True, radius=[30, 75], rosetype='radius', is_label_show=True) pie.add("职位数量统计", attr, small_class_count, center=[75, 50], is_random=True, radius=[30, 75], rosetype='radius', is_label_show=True, legend_top='bottom') return pie
def generate_pie_charts(self): """ 生成饼图 """ # 生成各种评价占比的饼图 a = ["默认好评", "好评", "中评", "差评"] v = [self.product.default_good_count, self.product.good_count - self.product.default_good_count, self.product.general_count, self.product.poor_count] pie = Pie("好评,中评,差评") pie.add("", a, v, is_label_show=True) self.page.add(pie) comments = self.product.get_all_comments() # 匿名评论情况饼图 v = [0, 0] for c in comments: if c.isAnonymous: v[0] += 1 else: v[1] += 1 chart = Pie('匿名评论情况') chart.add('', ['匿名', '非匿名'], v, xaxis_interval=0) self.page.add(chart) # 生成不同配置购买量、不同颜色购买量、用户等级和用户客户端的饼图 for attr in self.attr_title_dict.keys(): d = {} for c in comments: temp = getattr(c, attr) if d.get(temp): d[temp] += 1 else: d[temp] = 1 a = [] v = [] for k in d.keys(): if k == '': a.append('网页') else: a.append(k) v.append(d.get(k)) if attr == 'level': # 解决图例过长遮挡标题的bug chart = Pie('') else: chart = Pie(self.attr_title_dict.get(attr)) chart.add("", a, v, is_label_show=True) self.page.add(chart)
def pie_charts(): vote_result = pd.read_csv( '/home/caisi/PycharmProjects/DataVisualization/data/vote_result.csv') data_x = vote_result['Areas_of_interest'] data_y = vote_result['Votes'] pie = Pie( title="Pie object for user interest", title_pos="left", subtitle= "The following is vote result.\nreaders are most interested in Finaene and Health care and marketing." ) return pie.add("First", data_x, data_y, center=[60, 60], legend_orient="vertical", legend_pos="right", is_toolbox_show=False, is_label_show=True)
def score_distribution(self, levels, scores): pie_l = Pie('评论者等级饼图') attr_l = ['一级', '二级', '三级', '四级', '五级'] v_l = [] v_l.append(levels.count('1')) v_l.append(levels.count('2')) v_l.append(levels.count('3')) v_l.append(levels.count('4')) v_l.append(levels.count('5')) pie_l.add("", attr_l, v_l, radius=[60, 75], is_random=True, is_label_show=True) pie_l.add( "", attr_l, v_l, radius=[0, 55], rosetype="area", ) # pie_l.render(self.path +'/level_pie.html') scores_coun = Counter(scores) scores_list = sorted(scores_coun.items(), key=lambda x: x[0], reverse=False) # print(scores_list) attr_s = [] v_s = [] for score in scores_list: attr_s.append(score[0] + '分') v_s.append(score[1]) pie_s = Pie('评论者评分饼图') pie_s.add("", attr_s, v_s, is_random=True, radius=[30, 78], rosetype="area", is_label_show=True, legend_orient='vertical', legend_pos='left', legend_top='center') page = Page() page.add(pie_l) page.add(pie_s) page.render(self.path + '/levelscore_pie.html')
def test_pie_multiple(): import random attr = ['A', 'B', 'C', 'D', 'E', 'F'] pie = Pie("饼图示例", width=1000, height=600) pie.add( "", attr, [random.randint(0, 100) for _ in range(6)], radius=[50, 55], center=[25, 50], is_random=True, ) pie.add( "", attr, [random.randint(20, 100) for _ in range(6)], radius=[0, 45], center=[25, 50], rosetype='area', ) pie.add( "", attr, [random.randint(0, 100) for _ in range(6)], radius=[50, 55], center=[65, 50], is_random=True, ) pie.add( "", attr, [random.randint(20, 100) for _ in range(6)], radius=[0, 45], center=[65, 50], rosetype='radius', ) pie.render()
def test_pie_multiple(): import random attr = ["A", "B", "C", "D", "E", "F"] pie = Pie("饼图示例", width=1000, height=600) pie.add( "", attr, [random.randint(0, 100) for _ in range(6)], radius=[50, 55], center=[25, 50], is_random=True, ) pie.add( "", attr, [random.randint(20, 100) for _ in range(6)], radius=[0, 45], center=[25, 50], rosetype="area", ) pie.add( "", attr, [random.randint(0, 100) for _ in range(6)], radius=[50, 55], center=[65, 50], is_random=True, ) pie.add( "", attr, [random.randint(20, 100) for _ in range(6)], radius=[0, 45], center=[65, 50], rosetype="radius", ) pie.render()
def cx_to_pie(complaints): data = {} attr = [] value = [] for i in complaints: cx = i.cx if cx in data.keys(): data[cx] += 1 else: data[cx] = 1 for k in sorted(data,key=data.__getitem__,reverse=True): attr.append(k) value.append(data[k]) pie = Pie('投诉车型分布图', width=1300) if len(attr) > 15: pie.add('所有车型', attr, value, center=[30, 50],is_legend_show=False) pie.add('前十车型', attr[:10], value[:10], center=[70, 50],is_legend_show=False,is_label_show=True) else: pie.add('所有车型', attr, value, is_label_show=True) return pie
city_errands_time[city] = city_errands_time[city] + int(errands[0]) errands_time = errands_time + int(errands[0]) #print(city_errands_count) #print(city_errands_time) page = Page() pie_errands_count = Pie('2017研发出差次数统计(单位:次) 累计:%d次' % errands_count, width=1280, height=720, title_top='bootom') pie_errands_count.add('', city_errands_count.keys(), city_errands_count.values(), is_label_show=True, label_text_color='#F00', legend_top='bottom') pie_errands_time = Pie('2017研发出差时间统计(单位:工作小时/每个工作日7.5小时) 累计: %d 小时' % errands_time, width=1280, height=720, title_top='bootom') pie_errands_time.add('', city_errands_time.keys(), city_errands_time.values(), is_label_show=True, label_text_color='#F00', legend_top='bottom')
def plotClassarea(): list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("上海")] [list_address.append(i[1]) for i in foodClassarea("上海")] print(list_address) print(list_sum) page = Page() pie1 = Pie("上海菜品分类", title_pos='center') pie1.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar1 = Bar("上海菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar1.add("上海", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("北京")] [list_address.append(i[1]) for i in foodClassarea("北京")] print(list_address) print(list_sum) pie2 = Pie("北京菜品分类", title_pos='center') pie2.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar2 = Bar("北京菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar2.add("北京", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("广州")] [list_address.append(i[1]) for i in foodClassarea("广州")] print(list_address) print(list_sum) pie3 = Pie("广州菜品分类", title_pos='center') pie3.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar3 = Bar("广州菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar3.add("广州", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("深圳")] [list_address.append(i[1]) for i in foodClassarea("深圳")] print(list_address) print(list_sum) pie4 = Pie("深圳菜品分类", title_pos='center') pie4.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar4 = Bar("深圳菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar4.add("深圳", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("天津")] [list_address.append(i[1]) for i in foodClassarea("天津")] print(list_address) print(list_sum) pie5 = Pie("天津菜品分类", title_pos='center') pie5.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar5 = Bar("天津菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar5.add("天津", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("杭州")] [list_address.append(i[1]) for i in foodClassarea("杭州")] print(list_address) print(list_sum) pie6 = Pie("杭州菜品分类", title_pos='center') pie6.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar6 = Bar("杭州菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar6.add("杭州", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("南京")] [list_address.append(i[1]) for i in foodClassarea("南京")] print(list_address) print(list_sum) pie7 = Pie("南京菜品分类", title_pos='center') pie7.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar7 = Bar("南京菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar7.add("南京", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("苏州")] [list_address.append(i[1]) for i in foodClassarea("苏州")] print(list_address) print(list_sum) pie8 = Pie("苏州菜品分类", title_pos='center') pie8.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar8 = Bar("苏州菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar8.add("苏州", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("成都")] [list_address.append(i[1]) for i in foodClassarea("成都")] print(list_address) print(list_sum) pie9 = Pie("成都菜品分类", title_pos='center') pie9.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar9 = Bar("成都菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar9.add("成都", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("武汉")] [list_address.append(i[1]) for i in foodClassarea("武汉")] print(list_address) print(list_sum) pie10 = Pie("武汉菜品分类", title_pos='center') pie10.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar10 = Bar("武汉菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar10.add("武汉", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("重庆")] [list_address.append(i[1]) for i in foodClassarea("重庆")] print(list_address) print(list_sum) pie11 = Pie("重庆菜品分类", title_pos='center') pie11.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar11 = Bar("重庆菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar11.add("重庆", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") list_address = [] list_sum = [] [list_sum.append(i[0]) for i in foodClassarea("西安")] [list_address.append(i[1]) for i in foodClassarea("西安")] print(list_address) print(list_sum) pie12 = Pie("西安菜品分类", title_pos='center') pie12.add("", list_address, list_sum, radius=[30, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='auto', is_legend_show=False) bar12 = Bar("西安菜品分类", "数据来源于大众点评TOP100", title_pos='center') bar12.add("西安", list_address, list_sum, mark_point=["max", "min"], legend_pos="right") page.add(pie1) page.add(bar1) page.add(pie2) page.add(bar2) page.add(pie3) page.add(bar3) page.add(pie4) page.add(bar4) page.add(pie5) page.add(bar5) page.add(pie6) page.add(bar6) page.add(pie7) page.add(bar7) page.add(pie8) page.add(bar8) page.add(pie9) page.add(bar9) page.add(pie10) page.add(bar10) page.add(pie11) page.add(bar11) page.add(pie12) page.add(bar12) page.render("各城市菜品分类.html")
from pyecharts import Pie '''圆环图''' # 输入标签 attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "鞋子"] # 输入各标签数据 v1 = [11, 12, 13, 10, 10, 10] # 设置属性 pie = Pie("各院优秀学生人数", title_pos="center") pie.add("", attr, v1, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient="vertical", legend_pos="left") # 生成html,打开即可 pie.render()
is_more_utils=True) bar.show_config() bar.render(path='bar.html') from pyecharts import EffectScatter v1 = [10, 20, 30, 40, 50, 60] v2 = [25, 20, 15, 10, 60, 33] es = EffectScatter("带有涟漪特效动画的动态散点图示例") es.add("effectScatter", v1, v2) es.render("effect_scatter.html") from pyecharts import Pie attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图示例") pie.add("", attr, v1, is_label_show=True) pie.render("pie.html") from pyecharts import Gauge gauge = Gauge("仪表盘示例") gauge.add("业务指标", "完成率", 66.66) gauge.show_config() gauge.render("gauge.html") from pyecharts import Line, Overlap, Bar attr = ['A', 'B', 'C', 'D', 'E', 'F'] v1 = [10, 20, 30, 40, 50, 60] v2 = [38, 28, 58, 48, 78, 68]
def timeline_charts(): page = Page() 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 年销量", "数据纯属虚构", width=WIDTH, height=HEIGHT) 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) chart = Timeline(is_auto_play=True, timeline_bottom=0) chart.add(bar_1, '2012 年') chart.add(bar_2, '2013 年') chart.add(bar_3, '2014 年') chart.add(bar_4, '2015 年') chart.add(bar_5, '2016 年') page.add(chart) attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] pie_style = { "is_label_show": True, "radius": [30, 55], "rosetype":'radius' } pie_1 = Pie("2012 年销量比例", "数据纯属虚构") pie_1.add("秋季", attr, [randint(10, 100) for _ in range(6)], **pie_style) pie_2 = Pie("2013 年销量比例", "数据纯属虚构") pie_2.add("秋季", attr, [randint(10, 100) for _ in range(6)], **pie_style) pie_3 = Pie("2014 年销量比例", "数据纯属虚构") pie_3.add("秋季", attr, [randint(10, 100) for _ in range(6)], **pie_style) pie_4 = Pie("2015 年销量比例", "数据纯属虚构") pie_4.add("秋季", attr, [randint(10, 100) for _ in range(6)], **pie_style) pie_5 = Pie("2016 年销量比例", "数据纯属虚构", width=WIDTH, height=HEIGHT) pie_5.add("秋季", attr, [randint(10, 100) for _ in range(6)], **pie_style) chart = Timeline(is_auto_play=True, timeline_bottom=0) chart.add(pie_1, '2012 年') chart.add(pie_2, '2013 年') chart.add(pie_3, '2014 年') chart.add(pie_4, '2015 年') chart.add(pie_5, '2016 年') page.add(chart) 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 月份数据", "数据纯属虚构", width=WIDTH, height=HEIGHT) 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) chart = Timeline(timeline_bottom=0) chart.add(overlap_0.chart, '1 月') chart.add(overlap_1.chart, '2 月') chart.add(overlap_2.chart, '3 月') chart.add(overlap_3.chart, '4 月') chart.add(overlap_4.chart, '5 月') page.add(chart) return page
def graphpage(items,startdate,enddate,option,width1, height1): #labels:复权ork线or分笔 option:hfq, qfq or 15, 30, D, etc page = Page() for i in items:#generate numbers of graphs according to numbers of queries in treewidget j = re.split("-",i) if len(j)==3: a = generateline(j[1],j[2],startdate,enddate,option)#stock number, Type, startdate, enddate, 30 or 15 or days if a is None: continue time = [d[0] for d in a]#get time from returned dictionary if j[2]!="Kline": if len(a[0])==4 and a[0][2]=="bar": #for 分笔data overlap = Overlap() form = [e[1] for e in a] bar = Bar(j[0] + "-" + j[2], width=width1 * 10 / 11, height=(height1 * 10 / 11) / len(items)) bar.add(j[0] + "-" + j[2], time, form, yaxis_min = "dataMin",yaxis_max = "dataMax",is_datazoom_show = True, datazoom_type = "slider") overlap.add(bar) line = Line(j[0] + "price", width=width1 * 10 / 11, height=(height1 * 10 / 11) / len(items)) price = [e[3] for e in a] line.add(j[0] + "price", time, price, yaxis_min = "dataMin",yaxis_max = "dataMax", is_datazoom_show = True, datazoom_type = "slider", yaxis_type="value") overlap.add(line,yaxis_index=1, is_add_yaxis=True) page.add(overlap) if len(a[0])==5 and a[0][3]=="pie": overlap = Overlap() timeline = Timeline(is_auto_play=False, timeline_bottom=0) #zip(namearray,valuearray,quarter,flag,num) namearray = [c[0] for c in a] valuearray = [d[1] for d in a] quarter = [e[2] for e in a] num = a[0][4] for x in range(0, num / 10): list1 = valuearray[x] names = namearray[x] quarters = quarter[x][0] for idx, val in enumerate(list1): list1[idx] = float(val) pie = Pie(j[0]+"-"+"前十股东".decode("utf-8"),width=width1 * 10 / 11, height=(height1 * 10 / 11)) pie.add(j[0]+"-"+"前十股东".decode("utf-8"), names, list1, radius=[30, 55], is_legend_show=False, is_label_show=True, label_formatter = "{b}: {c}\n{d}%") # print list # print names # print quarterarray timeline.add(pie, quarters) # namearray = [y for y in namearray[x]] timeline.render() return #need more statement else: form = [e[1] for e in a]#for not分笔 data line = Line(j[0] + "-" + j[2], width=width1*10/11, height=(height1*10/11)/len(items)) line.add(j[0] + "-" + j[2], time, form, is_datazoom_show=True, datazoom_type="slider",yaxis_min="dataMin",yaxis_max="dataMax") page.add(line) else: overlap = Overlap()#for k线 close = zip(*a)[2] candle = [[x[1], x[2], x[3], x[4]] for x in a] candlestick = Kline(j[0] + "-" + j[2], width=width1*10/11, height = (height1*10/11) / len(items)) candlestick.add(j[0], time, candle, is_datazoom_show=True, datazoom_type="slider",yaxis_interval = 1) overlap.add(candlestick) if len(close)>10: ma10 = calculateMa(close, 10) line1 = Line(title_color="#C0C0C0") line1.add(j[0] + "-" + "MA10", time, ma10) overlap.add(line1) if len(close)>20: ma20 = calculateMa(close, 20) line2 = Line(title_color="#C0C0C0") line2.add(j[0] + "-" + "MA20", time, ma20) overlap.add(line2) if len(close)>30: ma30 = calculateMa(close, 30) line3 = Line(title_color="#C0C0C0") line3.add(j[0] + "-" + "MA30", time, ma30) overlap.add(line3) page.add(overlap) else: for k in range(1, len(j)/3):#if graphs are combined j[3*k-1] = re.sub("\n&","",j[3*k-1]) sizearray=[] #if j[1] != "Candlestick" layout = Overlap() for i in xrange(0, len(j),3): array = j[i:i +3] b = generateline(array[1],array[2],startdate,enddate,option) if b is None: continue btime = [d[0] for d in b] if array[2] != "Kline": if len(b[0])==4 and b[0][2]=="bar": form = [e[1] for e in b] bar = Bar(array[0] + "-" + array[2], width=width1 * 10 / 11, height=(height1 * 10 / 11) / len(items)) bar.add(array[0] + "-" + array[2], btime, form, is_datazoom_show=True, datazoom_type="slider", yaxis_min="dataMin", yaxis_max="dataMax") layout.add(bar) line = Line(array[0] + "price", width=width1 * 10 / 11, height=(height1 * 10 / 11) / len(items)) price = [e[3] for e in b] line.add(array[0] + "price", btime, price, is_datazoom_show=True, datazoom_type="slider", yaxis_min="dataMin", yaxis_type="value") layout.add(line, yaxis_index=1, is_add_yaxis=True) else: line = Line(array[0] + "-" + array[2],width=width1*10/11, height=(height1*10/11) / len(items)) line.add(array[0]+"-"+array[2], btime, b, is_datazoom_show=True, yaxis_max = "dataMax", yaxis_min = "dataMin",datazoom_type="slider") layout.add(line) else: candle = [[x[1], x[2], x[3], x[4]] for x in b] candlestick = Kline(array[0] + "-" + array[1], width=width1*10/11, height=(height1*10/11) / len(items)) candlestick.add(array[0], btime, candle, is_datazoom_show=True, datazoom_type=["slider"]) #if i == 0: close = zip(*b)[2] if len(close)>10: ma10 = calculateMa(close, 10) line4 = Line(title_color="#C0C0C0") line4.add(array[0] + "-" + "MA10", btime, ma10) layout.add(line4) if len(close)>20: ma20 = calculateMa(close, 20) line5 = Line(title_color="#C0C0C0") line5.add(array[0] + "-" + "MA20", btime, ma20) layout.add(line5) if len(close)>30: ma30 = calculateMa(close, 30) line6 = Line(title_color="#C0C0C0") line6.add(array[0] + "-" + "MA30", btime, ma30) layout.add(line6) layout.add(candlestick) page.add(layout) page.render()
def test_pie_multiple_movie(): pie = Pie('各类电影中"好片"所占的比例', "数据来着豆瓣", title_pos="center") style = Style() pie_style = style.add( label_pos="center", is_label_show=True, label_text_color=None ) pie.add( "", ["剧情", ""], [25, 75], center=[10, 30], radius=[18, 24], **pie_style ) pie.add( "", ["奇幻", ""], [24, 76], center=[30, 30], radius=[18, 24], **pie_style ) pie.add( "", ["爱情", ""], [14, 86], center=[50, 30], radius=[18, 24], **pie_style ) pie.add( "", ["惊悚", ""], [11, 89], center=[70, 30], radius=[18, 24], **pie_style ) pie.add( "", ["冒险", ""], [27, 73], center=[90, 30], radius=[18, 24], **pie_style ) pie.add( "", ["动作", ""], [15, 85], center=[10, 70], radius=[18, 24], **pie_style ) pie.add( "", ["喜剧", ""], [54, 46], center=[30, 70], radius=[18, 24], **pie_style ) pie.add( "", ["科幻", ""], [26, 74], center=[50, 70], radius=[18, 24], **pie_style ) pie.add( "", ["悬疑", ""], [25, 75], center=[70, 70], radius=[18, 24], **pie_style ) pie.add( "", ["犯罪", ""], [28, 72], center=[90, 70], radius=[18, 24], legend_top="center", **pie_style ) pie.render()
def test_grid(): # grid_0 attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图示例", height=720, is_grid=True) bar.add("商家A", attr, v1, is_stack=True, grid_bottom="60%") bar.add("商家B", attr, v2, is_stack=True, grid_bottom="60%") line = Line("折线图示例", title_top="50%") attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"]) line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_top="50%") bar.grid(line.get_series(), grid_top="60%") bar.show_config() bar.render() # grid_1 v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] scatter = Scatter(width=1200, is_grid=True) scatter.add("散点图示例", v1, v2, grid_left="60%", legend_pos="70%") es = EffectScatter() es.add("动态散点图示例", [11, 11, 15, 13, 12, 13, 10], [1, -2, 2, 5, 3, 2, 0], effect_scale=6, legend_pos="20%") scatter.grid(es.get_series(), grid_right="60%") scatter.show_config() scatter.render() # grid_2 attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图示例", height=720, width=1200, title_pos="65%", is_grid=True) bar.add("商家A", attr, v1, is_stack=True, grid_bottom="60%", grid_left="60%") bar.add("商家B", attr, v2, is_stack=True, grid_bottom="60%", grid_left="60%", legend_pos="80%") line = Line("折线图示例") attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"]) line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_pos="20%") v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] scatter = Scatter("散点图示例", title_top="50%", title_pos="65%") scatter.add("scatter", v1, v2, legend_top="50%", legend_pos="80%") es = EffectScatter("动态散点图示例", title_top="50%") es.add("es", [11, 11, 15, 13, 12, 13, 10], [1, -2, 2, 5, 3, 2, 0], effect_scale=6, legend_top="50%", legend_pos="20%") bar.grid(line.get_series(), grid_bottom="60%", grid_right="60%") bar.grid(scatter.get_series(), grid_top="60%", grid_left="60%") bar.grid(es.get_series(), grid_top="60%", grid_right="60%") bar.show_config() bar.render() # grid_3 line = Line("折线图示例", width=1200, is_grid=True) attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"], grid_right="65%") line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_pos="20%") attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图示例", title_pos="45%") pie.add("", attr, v1, radius=[30, 55], legend_pos="65%", legend_orient='vertical') line.grid(pie.get_series(), grid_left="60%") line.show_config() line.render() # grid_4 line = Line("折线图示例", width=1200, is_grid=True) attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"], grid_right="60%") line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_pos="20%", grid_right="60%") attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] value = [20, 40, 60, 80, 100, 120] v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("K 线图示例", title_pos="60%") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1, legend_pos="80%") line.grid(kline.get_series(), grid_left="55%") line.show_config() line.render() # grid_5 import random x_axis = ["12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a", "12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"] y_aixs = ["Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday", "Sunday"] data = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)] heatmap = HeatMap("热力图示例", height=700, is_grid=True) heatmap.add("热力图直角坐标系", x_axis, y_aixs, data, is_visualmap=True, visual_top="45%", visual_text_color="#000", visual_orient='horizontal', grid_bottom="60%") attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图示例", title_top="52%") bar.add("商家A", attr, v1, is_stack=True) bar.add("商家B", attr, v2, is_stack=True, legend_top="50%") heatmap.grid(bar.get_series(), grid_top="60%") heatmap.show_config() heatmap.render()
def test_more(): page = Page() # line attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line = Line("折线图示例") line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"]) line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"]) page.add(line) # pie attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图-圆环图示例", title_pos='center') pie.add("", attr, v1, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='left') page.add(pie) # kline v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("K 线图示例") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1) page.add(kline) # radar schema = [("销售", 6500), ("管理", 16000), ("信息技术", 30000), ("客服", 38000), ("研发", 52000), ("市场", 25000)] v1 = [[4300, 10000, 28000, 35000, 50000, 19000]] v2 = [[5000, 14000, 28000, 31000, 42000, 21000]] radar = Radar("雷达图示例") radar.config(schema) radar.add("预算分配", v1, is_splitline=True, is_axisline_show=True) radar.add("实际开销", v2, label_color=["#4e79a7"], is_area_show=False, legend_selectedmode='single') page.add(radar) # scatter3d import random data = [[ random.randint(0, 100), random.randint(0, 100), random.randint(0, 100) ] for _ in range(80)] range_color = [ '#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf', '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026' ] scatter3D = Scatter3D("3D 散点图示例", width=1200, height=600) scatter3D.add("", data, is_visualmap=True, visual_range_color=range_color) page.add(scatter3D) # wordcloud name = [ 'Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World', 'Charter Communications', 'Chick Fil A', 'Planet Fitness', 'Pitch Perfect', 'Express', 'Home', 'Johnny Depp', 'Lena Dunham', 'Lewis Hamilton', 'KXAN', 'Mary Ellen Mark', 'Farrah Abraham', 'Rita Ora', 'Serena Williams', 'NCAA baseball tournament', 'Point Break' ] value = [ 10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112, 965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265 ] wordcloud = WordCloud(width=1300, height=620) wordcloud.add("", name, value, word_size_range=[30, 100], rotate_step=66) page.add(wordcloud) # liquid liquid = Liquid("水球图示例") liquid.add("Liquid", [0.6]) page.add(liquid) page.render()
def test_more(): page = Page() # line line = Line("折线图示例") line.add( "最高气温", WEEK, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"], ) line.add( "最低气温", WEEK, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], ) # pie v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图-圆环图示例", title_pos="center") pie.add( "", CLOTHES, v1, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient="vertical", legend_pos="left", ) page.add([line, pie]) # kline v1 = [ [2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22], ] kline = Kline("K 线图示例") kline.add( "日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1, is_datazoom_show=True, ) page.add(kline) # radar schema = [ ("销售", 6500), ("管理", 16000), ("信息技术", 30000), ("客服", 38000), ("研发", 52000), ("市场", 25000), ] v1 = [[4300, 10000, 28000, 35000, 50000, 19000]] v2 = [[5000, 14000, 28000, 31000, 42000, 21000]] radar = Radar("雷达图示例") radar.config(schema) radar.add("预算分配", v1, is_splitline=True, is_axisline_show=True) radar.add( "实际开销", v2, label_color=["#4e79a7"], is_area_show=False, legend_selectedmode="single", ) page.add(radar) # scatter3d import random data = [ [ random.randint(0, 100), random.randint(0, 100), random.randint(0, 100), ] for _ in range(80) ] scatter3D = Scatter3D("3D 散点图示例", width=1200, height=600) scatter3D.add("", data, is_visualmap=True, visual_range_color=RANGE_COLOR) page.add(scatter3D) # wordcloud name = [ "Sam S Club", "Macys", "Amy Schumer", "Jurassic World", "Charter Communications", "Chick Fil A", "Planet Fitness", "Pitch Perfect", "Express", "Home", "Johnny Depp", "Lena Dunham", "Lewis Hamilton", "KXAN", "Mary Ellen Mark", "Farrah Abraham", "Rita Ora", "Serena Williams", "NCAA baseball tournament", "Point Break", ] value = [ 10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112, 965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265, ] wordcloud = WordCloud(width=1300, height=620) wordcloud.add("", name, value, word_size_range=[30, 100], rotate_step=66) page.add(wordcloud) # liquid liquid = Liquid("水球图示例") liquid.add("Liquid", [0.6]) page.add(liquid) assert len(page) == 7 assert isinstance(page[0], Line) assert ( ("echarts" in page.js_dependencies) or ("echarts.min" in page.js_dependencies) ) page.render()
def draw_height_pie(data): pie = Pie("妹子身高分布饼图-圆环图", title_pos='center') pie.add("", height_interval, data, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', is_random=True, legend_pos='left') return pie
def test_pie(): # pie_0 attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图示例") pie.add("", attr, v1, is_label_show=True) pie.show_config() pie.render() # pie_1 attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图-圆环图示例", title_pos='center') pie.add("", attr, v1, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient='vertical', legend_pos='left') pie.show_config() pie.render() # pie_2 attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [11, 12, 13, 10, 10, 10] v2 = [19, 21, 32, 20, 20, 33] pie = Pie("饼图-玫瑰图示例", title_pos='center', width=900) pie.add("商品A", attr, v1, center=[25, 50], is_random=True, radius=[30, 75], rosetype='radius') pie.add("商品B", attr, v2, center=[75, 50], is_random=True, radius=[30, 75], rosetype='area', is_legend_show=False, is_label_show=True) pie.show_config() pie.render() # pie_3 pie = Pie("饼图示例", title_pos='center', width=1000, height=600) pie.add("", ['A', 'B', 'C', 'D', 'E', 'F'], [335, 321, 234, 135, 251, 148], radius=[40, 55], is_label_show=True) pie.add("", ['H', 'I', 'J'], [335, 679, 204], radius=[0, 30], legend_orient='vertical', legend_pos='left') pie.show_config() pie.render() # pie_4 import random attr = ['A', 'B', 'C', 'D', 'E', 'F'] pie = Pie("饼图示例", width=1000, height=600) pie.add("", attr, [random.randint(0, 100) for _ in range(6)], radius=[50, 55], center=[25, 50], is_random=True) pie.add("", attr, [random.randint(20, 100) for _ in range(6)], radius=[0, 45], center=[25, 50], rosetype='area') pie.add("", attr, [random.randint(0, 100) for _ in range(6)], radius=[50, 55], center=[65, 50], is_random=True) pie.add("", attr, [random.randint(20, 100) for _ in range(6)], radius=[0, 45], center=[65, 50], rosetype='radius') pie.show_config() pie.render() # Pie_5 pie = Pie('各类电影中"好片"所占的比例', "数据来着豆瓣", title_pos='center') pie.add("", ["剧情", ""], [25, 75], center=[10, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, ) pie.add("", ["奇幻", ""], [24, 76], center=[30, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, legend_pos='left') pie.add("", ["爱情", ""], [14, 86], center=[50, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None) pie.add("", ["惊悚", ""], [11, 89], center=[70, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None) pie.add("", ["冒险", ""], [27, 73], center=[90, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None) pie.add("", ["动作", ""], [15, 85], center=[10, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None) pie.add("", ["喜剧", ""], [54, 46], center=[30, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None) pie.add("", ["科幻", ""], [26, 74], center=[50, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None) pie.add("", ["悬疑", ""], [25, 75], center=[70, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None) pie.add("", ["犯罪", ""], [28, 72], center=[90, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, is_legend_show=True, legend_top="center") pie.show_config() pie.render()
def graphpage(labels,mode_combo,startdate,enddate,optInterval,width1, height1): #optInterval='D/W/M' labels startdate = startdate.replace("/","-")#convert to tushare readable date enddate = enddate.replace("/","-") page = Page() for label in labels: # generate numbers of graphs according to numbers of queries in treewidget label1 = re.split("-", label) overlap = Overlap() if label1[2]!="分笔" and label1[2]!="季度饼图": #"K线" "复权" "历史分钟" if mode_combo == "复权": #if optInterval == "qfq" or optInterval == "hfq":#复权 array = ts.get_h_data(label1[1], start=startdate, end=enddate, autype="qfq") array = array.sort_index() time = array.index.format() # 日期正是index elif mode_combo == "K线": #elif optInterval.isalnum() :#历史K线 array = ts.get_k_data(label1[1], start=startdate, end=enddate, ktype=optInterval) time = array['date'].tolist() # array.date elif mode_combo == "历史分钟": array_bfr = ts.get_tick_data(label1[1], date=startdate) array_bfr.sort_values("time") a = startdate + " " + array_bfr["time"] array_bfr["time"] = a array_bfr["time"] = pd.to_datetime(a) array_bfr = array_bfr.set_index("time") if label1[2] != "Volume" and label1[2] != "Amount": price_df = array_bfr["price"].resample(optInterval).ohlc() # resample重新取样,选出ohlc=open/high/low,提取除价格之外另外4列.close,tushare是ohcl price_df = price_df.dropna() array = price_df time = price_df.index.format() elif label1[2] == "Volume": vols = array_bfr["volume"].resample(optInterval).sum() #resample重新取样,累加间隔内数值 relevant data processing algorithm taken from Jimmy, Creator of Tushare vols = vols.dropna() vol_df = pd.DataFrame(vols, columns=["volume"]) array = vol_df time = vol_df.index.format() else: amounts = array_bfr["amount"].resample(optInterval).sum() amounts = amounts.dropna() amount_df = pd.DataFrame(amounts, columns=["amount"]) array = amount_df time = amount_df.index.format() #绘图方法 if label1[2] == 'Kline': re_array = array[['open', 'close', 'high', 'low']] data_li = list(row.tolist() for index, row in re_array.iterrows()) # data_list = list(re_array.as_matrix()) close = array['close'].tolist() kline = Kline(label1[0] + "-" + optInterval, width=width1*10/11, height = (height1*10/11) / len(labels)) kline.add(label1[0], time, data_li, is_datazoom_show=True, datazoom_type="slider", yaxis_interval=1) overlap.add(kline) if len(close) > 10: ma10 = calculateMa(close, 10) line1 = Line(title_color="#C0C0C0") line1.add(label1[0] + "-" + "MA10", time, ma10) overlap.add(line1) if len(close) > 20: ma20 = calculateMa(close, 20) line2 = Line(title_color="#C0C0C0") line2.add(label1[0] + "-" + "MA20", time, ma20) overlap.add(line2) if len(close) > 30: ma30 = calculateMa(close, 30) line3 = Line(title_color="#C0C0C0") line3.add(label1[0] + "-" + "MA30", time, ma30) overlap.add(line3) page.add(overlap) else:#When label1[2]==open/close/volume if label1[2] == 'Open': list_aft = array['open'].tolist() elif label1[2] == 'Close': list_aft = close elif label1[2] == 'High': list_aft = array['high'].tolist() elif label1[2] == 'Low': list_aft = array['low'].tolist() elif label1[2] == 'Volume':#volume list_aft = array['volume'].tolist() else:#amount list_aft = array['amount'].tolist() line = Line(label1[0] + "-" + label1[2], width=width1 * 10 / 11, height=(height1 * 10 / 11) / len(labels)) line.add(label1[0] + "-" + label1[2], time, list_aft, is_datazoom_show=True, yaxis_max="dataMax", yaxis_min="dataMin", datazoom_type="slider") overlap.add(line) page.add(overlap) elif label1[2]=="分笔": array = ts.get_tick_data(label1[1], date=startdate) array = array.sort_values("time") date = array["time"].tolist() amount = array["amount"].tolist() atype = array["type"].tolist() price = array["price"].tolist() flag = ["bar" for i in date] for idx, val in enumerate(atype): if val == "卖盘": amount[idx] = -amount[idx] if val == "中性盘": amount[idx] = 0 returnarray = list(zip(date, amount, flag, price)) form = [e[1] for e in returnarray] time = [d[0] for d in returnarray] bar = Bar(label1[0] + "-" + label1[2], width=width1 * 10 / 11, height=(height1 * 10 / 11) / len(labels)) bar.add(label1[0] + "-" + label1[2], time, form, is_datazoom_show=True, datazoom_type="slider", yaxis_min="dataMin", yaxis_max="dataMax") overlap.add(bar) line = Line(label1[0] + "price", width=width1 * 10 / 11, height=(height1 * 10 / 11) / len(labels)) price = [e[3] for e in returnarray] line.add(label1[0] + "price", time, price, yaxis_min="dataMin", yaxis_max="dataMax", is_datazoom_show=True, datazoom_type="slider", yaxis_type="value") overlap.add(line, yaxis_index=1, is_add_yaxis=True) page.add(overlap) elif label1[2]=="季度饼图": datestr = startdate.split("-") thisyear = datestr[0] df2 = ts.top10_holders(code=label1[1], gdtype="1") test = df2[1]["quarter"].tolist() df_ready = df2[1] idxlist = [] for idx, val in enumerate(test): a = val.split("-") if a[0] == thisyear: # print a[0],idx idxlist.append(idx) thing = df_ready.loc[idxlist] thing = thing.sort_values(["quarter", "name"]) # print a[0],id name = thing["name"].tolist() value = thing["hold"].tolist() quarter = thing["quarter"].tolist() namearray = [name[i:i + 10] for i in range(0, len(name), 10)] valuearray = [value[j:j + 10] for j in range(0, len(value), 10)] quarterarray = [quarter[k:k + 10] for k in range(0, len(quarter), 10)] flag = ["pie" for i in namearray] num = [len(value) for k in namearray] returnarray = list(zip(namearray, valuearray, quarterarray, flag, num)) timeline = Timeline(is_auto_play=False, timeline_bottom=0) # zip(namearray,valuearray,quarter,flag,num) namearray = [c[0] for c in returnarray] valuearray = [d[1] for d in returnarray] quarter = [e[2] for e in returnarray] num = returnarray[0][4] for x in range(0, int(num / 10)): list1 = valuearray[x] names = namearray[x] quarters = quarter[x][0] for idx, val in enumerate(list1): list1[idx] = float(val) pie = Pie(label1[0] + "-" + "前十股东", width=width1 * 10 / 11, height=(height1 * 10 / 11)) pie.add(label1[0] + "-" + "前十股东", names, list1, radius=[30, 55], is_legend_show=False, is_label_show=True, label_formatter="{b}: {c}\n{d}%") timeline.add(pie, quarters) # namearray = [y for y in namearray[x]] timeline.render() page.render()
def test_grid(): # grid_0 attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图示例", height=720, is_grid=True) bar.add("商家A", attr, v1, is_stack=True, grid_bottom="60%") bar.add("商家B", attr, v2, is_stack=True, grid_bottom="60%") line = Line("折线图示例", title_top="50%") attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"]) line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_top="50%") bar.grid(line.get_series(), grid_top="60%") bar.show_config() bar.render() # grid_1 v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] scatter = Scatter(width=1200, is_grid=True) scatter.add("散点图示例", v1, v2, grid_left="60%", legend_pos="70%") es = EffectScatter() es.add("动态散点图示例", [11, 11, 15, 13, 12, 13, 10], [1, -2, 2, 5, 3, 2, 0], effect_scale=6, legend_pos="20%") scatter.grid(es.get_series(), grid_right="60%") scatter.show_config() scatter.render() # grid_2 attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图示例", height=720, width=1200, title_pos="65%", is_grid=True) bar.add("商家A", attr, v1, is_stack=True, grid_bottom="60%", grid_left="60%") bar.add("商家B", attr, v2, is_stack=True, grid_bottom="60%", grid_left="60%", legend_pos="80%") line = Line("折线图示例") attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"]) line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_pos="20%") v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] scatter = Scatter("散点图示例", title_top="50%", title_pos="65%") scatter.add("scatter", v1, v2, legend_top="50%", legend_pos="80%") es = EffectScatter("动态散点图示例", title_top="50%") es.add("es", [11, 11, 15, 13, 12, 13, 10], [1, -2, 2, 5, 3, 2, 0], effect_scale=6, legend_top="50%", legend_pos="20%") bar.grid(line.get_series(), grid_bottom="60%", grid_right="60%") bar.grid(scatter.get_series(), grid_top="60%", grid_left="60%") bar.grid(es.get_series(), grid_top="60%", grid_right="60%") bar.show_config() bar.render() # grid_3 line = Line("折线图示例", width=1200, is_grid=True) attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"], grid_right="65%") line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_pos="20%") attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图示例", title_pos="45%") pie.add("", attr, v1, radius=[30, 55], legend_pos="65%", legend_orient='vertical') line.grid(pie.get_series(), grid_left="60%") line.show_config() line.render() # grid_4 line = Line("折线图示例", width=1200, is_grid=True) attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"], grid_right="60%") line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_pos="20%", grid_right="60%") attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] value = [20, 40, 60, 80, 100, 120] v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("K 线图示例", title_pos="60%") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1, legend_pos="80%") line.grid(kline.get_series(), grid_left="55%") line.show_config() line.render() # grid_5 import random x_axis = [ "12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a", "12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p" ] y_aixs = [ "Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday", "Sunday" ] data = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)] heatmap = HeatMap("热力图示例", height=700, is_grid=True) heatmap.add("热力图直角坐标系", x_axis, y_aixs, data, is_visualmap=True, visual_top="45%", visual_text_color="#000", visual_orient='horizontal', grid_bottom="60%") attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图示例", title_top="52%") bar.add("商家A", attr, v1, is_stack=True) bar.add("商家B", attr, v2, is_stack=True, legend_top="50%") heatmap.grid(bar.get_series(), grid_top="60%") heatmap.show_config() heatmap.render()
def test_pie_default(): v1 = [11, 12, 13, 10, 10, 10] pie = Pie("饼图示例") pie.add("", CLOTHES, v1, is_label_show=True) pie.render()