diff --git a/app/utils/draw.py b/app/utils/draw.py index 56b6040433a2c7ec29b465d5ccda788bb4302497..5659621a746eb63186b69e5517eb83f7a6b76ce6 100644 --- a/app/utils/draw.py +++ b/app/utils/draw.py @@ -6,6 +6,10 @@ # @Email : acepengxiong@163.com # @Software : PyCharm # ----------------------------------------------------------------------------- +import base64 +from urllib import parse + +from io import BytesIO import numpy as np import matplotlib.pyplot as plt @@ -14,11 +18,11 @@ from matplotlib.ticker import FuncFormatter from matplotlib.font_manager import FontProperties # 䏿–‡å—体åˆå§‹åŒ– -plt.rcParams['font.sans-serif']=['simsunb.ttf'] +plt.rcParams['font.sans-serif']=['Heiti TC'] def to_percent(temp, position): - return '%1.0f' % temp + '%' + return '%.2f' % temp + '%' def draw_month_return_chart(xlabels, product_list, cumulative): @@ -28,7 +32,7 @@ def draw_month_return_chart(xlabels, product_list, cumulative): # plt.ylabel('Scores') figsize = (24, 12) # æ ‡ç¾æ–‡å—å¤§å° - fontsize = 20 + fontsize = 15 # åˆå§‹åŒ– fig = plt.figure(figsize=figsize) ax1 = fig.add_subplot(111) @@ -41,16 +45,17 @@ def draw_month_return_chart(xlabels, product_list, cumulative): # åæ ‡è½´ ax1.tick_params(labelsize=fontsize) ax2.tick_params(labelsize=fontsize) + # åæ ‡è½´é¢œè‰² ax2.tick_params(axis='y', colors='#C6A774') ax1.set_xticks(loc) ax1.set_xticklabels(xlabels) # ax1.yaxis.set_major_formatter(FuncFormatter(to_percent)) ax2.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']))) + # 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_ylim(0, max_rate + 15) # 柱状图 @@ -82,8 +87,12 @@ def draw_month_return_chart(xlabels, product_list, cumulative): ax2.text(a, b + 0.1, '%.2f' % b + '%', ha='center', va='bottom', fontsize=fontsize) ax2.legend(loc='upper center', fontsize=fontsize) - plt.show() - plt.savefig("") + # plt.show() + imgdata = BytesIO() + fig.savefig(imgdata, format='png') + 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_contribution_chart(xlabels, product_list, cumulative):