456

parent 92f5f387
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -89,7 +89,7 @@ def draw_month_return_chart(xlabels, product_list, cumulative): ...@@ -89,7 +89,7 @@ def draw_month_return_chart(xlabels, product_list, cumulative):
# plt.show() # plt.show()
imgdata = BytesIO() imgdata = BytesIO()
fig.savefig(imgdata, format='png') fig.savefig(imgdata, format='png', bbox_inches='tight')
imgdata.seek(0) # rewind the data imgdata.seek(0) # rewind the data
month_return_img = 'data:image/png;base64,' + base64.b64encode(imgdata.getvalue()).decode('utf-8') month_return_img = 'data:image/png;base64,' + base64.b64encode(imgdata.getvalue()).decode('utf-8')
return month_return_img return month_return_img
...@@ -100,7 +100,7 @@ def draw_contribution_chart(xlabels, product_list, cumulative): ...@@ -100,7 +100,7 @@ def draw_contribution_chart(xlabels, product_list, cumulative):
# plt.title('Scores by group and gender') # plt.title('Scores by group and gender')
# plt.ylabel('Scores') # plt.ylabel('Scores')
figsize = (20, 12) figsize = (25, 12)
# 标签文字大小 # 标签文字大小
fontsize = 22 fontsize = 22
# 初始化 # 初始化
...@@ -117,12 +117,12 @@ def draw_contribution_chart(xlabels, product_list, cumulative): ...@@ -117,12 +117,12 @@ def draw_contribution_chart(xlabels, product_list, cumulative):
ax1.set_xticks(loc) ax1.set_xticks(loc)
ax1.set_xticklabels(xlabels) ax1.set_xticklabels(xlabels)
ax1.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax1.yaxis.set_major_formatter(FuncFormatter(to_percent))
temp_rate = np.zeros(max_x_count) # temp_rate = np.zeros(max_x_count)
for i in range(len(product_list)): # for i in range(len(product_list)):
temp_rate += product_list[i]['data'] # temp_rate += product_list[i]['data']
max_rate = np.max(np.hstack((temp_rate, cumulative['data']))) # max_rate = np.max(np.hstack((temp_rate, cumulative['data'])))
ax2.set_xticks([]) ax2.set_xticks([])
ax2.set_ylim(0, max_rate + 10) # ax2.set_ylim(0, max_rate + 10)
# 堆叠柱状图 # 堆叠柱状图
prod_legend = [] prod_legend = []
...@@ -139,18 +139,83 @@ def draw_contribution_chart(xlabels, product_list, cumulative): ...@@ -139,18 +139,83 @@ def draw_contribution_chart(xlabels, product_list, cumulative):
else: else:
ax = ax1.bar(loc, product_list[i]['data'], width, bottom=bottom, alpha=0.8) ax = ax1.bar(loc, product_list[i]['data'], width, bottom=bottom, alpha=0.8)
prod_legend.append(ax[0]) prod_legend.append(ax[0])
ax1.legend(prod_legend, [prod['name'] for prod in product_list], bbox_to_anchor=(0.8, -0.1), ncol=4, fontsize=fontsize) ax1.legend(prod_legend, [prod['name'] for prod in product_list], bbox_to_anchor=(0.9, -0.1), ncol=4, fontsize=fontsize)
# 画折线图 # 画折线图
ax2.plot(loc, cumulative['data'], color='#C6A774', marker='', linewidth=3, label=cumulative['name']) ax2.plot(loc, cumulative['data'], color='#C6A774', marker='', linewidth=3, label=cumulative['name'])
ax2.legend(loc='upper left', fontsize=fontsize) ax2.legend(loc='upper left', fontsize=fontsize)
plt.show() imgdata = BytesIO()
# imgdata = BytesIO() fig.savefig(imgdata, format='png', bbox_inches='tight')
# fig.savefig(imgdata, format='png') imgdata.seek(0) # rewind the data
# imgdata.seek(0) # rewind the data month_return_img = 'data:image/png;base64,' + base64.b64encode(imgdata.getvalue()).decode('utf-8')
# month_return_img = 'data:image/png;base64,' + base64.b64encode(imgdata.getvalue()).decode('utf-8') return month_return_img
# return month_return_img
# def draw_contribution_chart(xlabels, product_list, cumulative):
# """贡献分解图"""
#
# # plt.title('Scores by group and gender')
# # plt.ylabel('Scores')
# figsize = (25, 12)
# # 标签文字大小
# fontsize = 22
# # 初始化
# fig = plt.figure(figsize=figsize)
# ax1 = fig.add_subplot()
# ax2 = ax1.twiny()
# max_x_count = max([x['data'].size for x in product_list])
# loc = np.arange(max_x_count) # the x locations for the groups
# width = 0.35 # the width of the bars: can also be len(x) sequence
# color_list = ['#222A77', '#6C71AA', '#E1BC95', '#F9DBB8']
#
# # 坐标轴
# ax1.tick_params(labelsize=fontsize)
# ax1.set_xticks(loc)
# ax1.set_xticklabels(xlabels)
# ax1.yaxis.set_major_formatter(FuncFormatter(to_percent))
# # temp_rate = np.zeros(max_x_count)
# # for i in range(len(product_list)):
# # temp_rate += product_list[i]['data']
# # max_rate = np.max(np.hstack((temp_rate, cumulative['data'])))
# ax2.set_xticks([])
# # ax2.set_ylim(0, max_rate + 10)
#
# # 堆叠柱状图
# prod_legend = []
# for i in range(len(product_list)):
# ax = None
# for j in range(len(product_list[i]['data'])):
# product_list[i]['bottom'] = product_list[i].get('bottom', 0)
# product_list[i]['bottom_neg'] = product_list[i].get('bottom_neg', 0)
# if j > 0:
# product_list[i]['bottom'] += product_list[i].get('bottom', 0)
# product_list[i]['bottom_neg'] += product_list[i].get('bottom_neg', 0)
# if i < len(color_list):
# for x in loc:
# if product_list[i]['data'][x] >= 0:
# ax = ax1.bar(x, product_list[i]['data'][x], width, bottom=product_list[i]['bottom'], color=color_list[i], alpha=0.8)
# else:
# ax = ax1.bar(x, product_list[i]['data'][x], width, bottom=product_list[i]['bottom_neg'],
# color=color_list[i], alpha=0.8)
# else:
# for x in loc:
# if product_list[i]['data'][x] >= 0:
# ax = ax1.bar(x, product_list[i]['data'][x], width, bottom=product_list[i]['bottom'], alpha=0.8)
# else:
# ax = ax1.bar(x, product_list[i]['data'][x], width, bottom=product_list[i]['bottom_neg'], alpha=0.8)
# prod_legend.append(ax[0])
# # ax1.legend(prod_legend, [prod['name'] for prod in product_list], bbox_to_anchor=(0.9, -0.1), ncol=4, fontsize=fontsize)
#
# # 画折线图
# ax2.plot(loc, cumulative['data'], color='#C6A774', marker='', linewidth=3, label=cumulative['name'])
# ax2.legend(loc='upper left', fontsize=fontsize)
#
# plt.show()
# # imgdata = BytesIO()
# # fig.savefig(imgdata, format='png', bbox_inches='tight')
# # imgdata.seek(0) # rewind the data
# # month_return_img = 'data:image/png;base64,' + base64.b64encode(imgdata.getvalue()).decode('utf-8')
# # return month_return_img
def draw_comment_chart(xlabels, source_prod, target_prod): def draw_comment_chart(xlabels, source_prod, target_prod):
...@@ -240,26 +305,26 @@ if __name__ == '__main__': ...@@ -240,26 +305,26 @@ if __name__ == '__main__':
# contrast = {'name': '同比上涨', 'data': np.array([10, 50, 120, 100, 36, 0, 50, 120, 100, 36, 23, 98])} # contrast = {'name': '同比上涨', 'data': np.array([10, 50, 120, 100, 36, 0, 50, 120, 100, 36, 23, 98])}
# draw_month_return_chart(xlabels, [product], contrast) # draw_month_return_chart(xlabels, [product], contrast)
# xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12') xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12')
# product1 = {'name': '塞亚成长1号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} product1 = {'name': '塞亚成长1号', 'data': np.array([-10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
# product2 = {'name': '塞亚成长2号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} product2 = {'name': '塞亚成长2号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
# product3 = {'name': '塞亚成长3号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} product3 = {'name': '塞亚成长3号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
# product4 = {'name': '塞亚成长4号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} product4 = {'name': '塞亚成长4号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
# product5 = {'name': '塞亚成长5号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} product5 = {'name': '塞亚成长5号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
# product6 = {'name': '塞亚成长6号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} product6 = {'name': '塞亚成长6号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
# product7 = {'name': '塞亚成长7号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} product7 = {'name': '塞亚成长7号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
# product8 = {'name': '塞亚成长8号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} product8 = {'name': '塞亚成长8号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
# product_list = [product1, product2, product3, product4, product5, product6, product7, product8] product_list = [product1, product2, product3, product4, product5, product6, product7, product8]
# cumulative = {'name': '总收益', 'data': np.array([10, 50, 120, 100, 36, 0, 50, 120, 100, 36, 23, 98])} cumulative = {'name': '总收益', 'data': np.array([10, 50, 120, 100, 36, 0, 50, 120, 100, 36, 23, 98])}
# draw_contribution_chart(xlabels, product_list, cumulative) draw_contribution_chart(xlabels, product_list, cumulative)
# xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12') # xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12')
# source_prod = {'name': '远澜银杏1号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} # source_prod = {'name': '远澜银杏1号', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
# target_prod = {'name': '上证指数', 'data': np.array([-10, 10, 5, 55, 24, 10, 20, 8, 10, 31, 40, 32])} # target_prod = {'name': '上证指数', 'data': np.array([-10, 10, 5, 55, 24, 10, 20, 8, 10, 31, 40, 32])}
# draw_comment_chart(xlabels, source_prod, target_prod) # draw_comment_chart(xlabels, source_prod, target_prod)
xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12') # xlabels = ('2020-1', '2020-2', '2020-3', '2020-4', '2020-5', '2020-6', '2020-7', '2020-8', '2020-9', '2020-10', '2020-11', '2020-12')
new_combination = {'name': '新组合', 'data': np.array([20, 30, 40, 50, 60, 20, 30, 40, 50, 60, 50, 60])} # new_combination = {'name': '新组合', 'data': np.array([20, 30, 40, 50, 60, 20, 30, 40, 50, 60, 50, 60])}
origin_combination = {'name': '原组合', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])} # origin_combination = {'name': '原组合', 'data': np.array([10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 40, 50])}
index = {'name': '上证指数', 'data': np.array([-10, 10, 5, 55, 24, 10, 20, 8, 10, 31, 40, 32])} # index = {'name': '上证指数', 'data': np.array([-10, 10, 5, 55, 24, 10, 20, 8, 10, 31, 40, 32])}
draw_combination_chart(xlabels, new_combination, origin_combination, index) # draw_combination_chart(xlabels, new_combination, origin_combination, index)
...@@ -6,11 +6,14 @@ ...@@ -6,11 +6,14 @@
# @Email : acepengxiong@163.com # @Email : acepengxiong@163.com
# @Software : PyCharm # @Software : PyCharm
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
from io import BytesIO, StringIO
import imgkit
import pyecharts.options as opts import pyecharts.options as opts
from pyecharts.charts import Radar, Bar from pyecharts.charts import Radar, Bar
from snapshot_phantomjs import snapshot from snapshot_phantomjs import snapshot
from pyecharts.render import make_snapshot from pyecharts.render import make_snapshot
import cv2
def gen_radar_chart(): def gen_radar_chart():
...@@ -21,11 +24,9 @@ def gen_radar_chart(): ...@@ -21,11 +24,9 @@ def gen_radar_chart():
1、雷达图周围的图例的 textStyle 暂时无法设置背景颜色 1、雷达图周围的图例的 textStyle 暂时无法设置背景颜色
""" """
v1 = [[4300, 10000, 28000, 35000, 50000]] v1 = [[4300, 10000, 28000, 35000, 50000]]
htmldata = StringIO()
( radar = Radar(
Radar( init_opts=opts.InitOpts(bg_color="#fff", animation_opts=opts.AnimationOpts(animation=False))).add_schema(
init_opts=opts.InitOpts(bg_color="#fff", animation_opts=opts.AnimationOpts(animation=False)))
.add_schema(
schema=[ schema=[
opts.RadarIndicatorItem(name="绝对收益", max_=6500), opts.RadarIndicatorItem(name="绝对收益", max_=6500),
opts.RadarIndicatorItem(name="业绩持续性", max_=16000), opts.RadarIndicatorItem(name="业绩持续性", max_=16000),
...@@ -43,15 +44,12 @@ def gen_radar_chart(): ...@@ -43,15 +44,12 @@ def gen_radar_chart():
]) ])
), ),
textstyle_opts=opts.TextStyleOpts(color="#000", font_size=16), textstyle_opts=opts.TextStyleOpts(color="#000", font_size=16),
) ).add(
.add(
series_name="预算分配(Allocated Budget)", series_name="预算分配(Allocated Budget)",
data=v1, data=v1,
linestyle_opts=opts.LineStyleOpts(width=2, color="#C6A774"), linestyle_opts=opts.LineStyleOpts(width=2, color="#C6A774"),
tooltip_opts=None tooltip_opts=None
) ).set_series_opts(label_opts=opts.LabelOpts(is_show=False)).set_global_opts(
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts( title_opts=opts.TitleOpts(
title="综合评分", subtitle="66.32", pos_top="center", pos_left="46%", title="综合评分", subtitle="66.32", pos_top="center", pos_left="46%",
title_textstyle_opts={'color': '#333', title_textstyle_opts={'color': '#333',
...@@ -66,10 +64,21 @@ def gen_radar_chart(): ...@@ -66,10 +64,21 @@ def gen_radar_chart():
}), }),
legend_opts=opts.LegendOpts(selected_mode=False, is_show=False) legend_opts=opts.LegendOpts(selected_mode=False, is_show=False)
) )
.render("basic_radar_chart.html") radar.render()
) imgkit.from_file('render.html', "radar_chart.png")
make_snapshot(snapshot, "basic_radar_chart.html", "basic_radar_chart.png", delay=2) # 读取图片
img = cv2.imread('radar_chart.png')
# 获取宽度和高度
height = len(img)
width = len(img[0])
img = img[height:height, 200:800]
height = len(img)
width = len(img[0])
print('图片大小%dX%d' % (width, height))
cv2.imshow('radar_chart.png', img)
cv2.waitKey(0)
if __name__ == '__main__': if __name__ == '__main__':
gen_radar_chart() gen_radar_chart()
\ No newline at end of file
from jinja2 import PackageLoader, Environment from jinja2 import PackageLoader, Environment
from app.service.result_service import UserCustomerResultAdaptor from app.service.result_service import UserCustomerResultAdaptor
import numpy as np
# 准备数据 # 准备数据
from app.utils.draw import draw_month_return_chart, draw_contribution_chart from app.utils.draw import draw_month_return_chart, draw_contribution_chart
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment