1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name : radar_chart.py
# @Time : 2020/12/1 下午4:50
# @Author : X. Peng
# @Email : acepengxiong@163.com
# @Software : PyCharm
# -----------------------------------------------------------------------------
import base64
import os
import time
from io import BytesIO
import imgkit
import pyecharts.options as opts
from pyecharts.charts import Radar
import cv2
import numpy as np
def gen_radar_chart(radar_chart_data):
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://echarts.apache.org/examples/editor.html?c=radar
目前无法实现的功能:
1、雷达图周围的图例的 textStyle 暂时无法设置背景颜色
"""
radar_chart = radar_chart_data['data'][0:5]
v1 = [[data['data'] for data in radar_chart]]
radar = Radar(
init_opts=opts.InitOpts(bg_color="#fff", animation_opts=opts.AnimationOpts(animation=False))).add_schema(
schema=[
opts.RadarIndicatorItem(name="绝对收益", max_=100),
opts.RadarIndicatorItem(name="基金经理", max_=100),
opts.RadarIndicatorItem(name="极端风险", max_=100),
opts.RadarIndicatorItem(name="风险调整后收益", max_=100),
opts.RadarIndicatorItem(name="业绩持续性", max_=100),
],
# schema=[
# opts.RadarIndicatorItem(name=data['name'], max_=float(data['data'])) for data in radar_chart
# ],
splitarea_opt=opts.SplitAreaOpts(
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1, color=[
'rgba(194, 194, 194, 0.5)',
'rgba(232, 232, 232, 1)',
'rgba(242, 242, 242, 1)',
'rgba(247, 247, 247, 0.92)',
'rgba(247, 247, 247, 0.5)',
])
),
textstyle_opts=opts.TextStyleOpts(color="#000", font_size=14),
).add(
series_name="预算分配(Allocated Budget)",
data=v1,
linestyle_opts=opts.LineStyleOpts(width=2, color="#999999"),
tooltip_opts=None
).set_series_opts(label_opts=opts.LabelOpts(is_show=False)).set_global_opts(
# title_opts=opts.TitleOpts(
# title="综合评分", subtitle=radar_chart_data['data'][5]['data'], pos_top="center", pos_left="46%",
# title_textstyle_opts={'color': '#333',
# 'fontSize': 18,
# 'lineHeight': 20,
# 'fontWeight': 'normal'
# },
# subtitle_textstyle_opts={'color': '#983612' if float(radar_chart_data['data'][5]['data']) > 60 else '#22999F',
# 'fontSize': 20,
# 'lineHeight': 23,
# 'fontWeight': 'bolder',
# }),
legend_opts=opts.LegendOpts(selected_mode=False, is_show=False)
)
# radar.render("basic_radar_chart.html")
# img_content = work_dir + "/app/html/{}.png".format(str(uuid.uuid4()))
# make_snapshot(snapshot, "basic_radar_chart.html", img_content, delay=2)
html = radar.render_embed()
start = time.time()
image_bin = imgkit.from_string(html, None)
print('propose_fund cost', time.time() - start)
# 读取图片
img1 = np.frombuffer(image_bin, np.uint8)
img = cv2.imdecode(img1, cv2.IMREAD_ANYCOLOR)
# 获取宽度和高度
height = len(img)
width = len(img[0])
img = img[0:height, 160:750]
img_bin = cv2.imencode(".png", img)[1].tobytes()
img_content = 'data:image/png;base64,' + base64.b64encode(img_bin).decode('utf-8')
return img_content
if __name__ == '__main__':
gen_radar_chart()