# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : radar_chart.py
# @Time       : 2020/12/1 下午4:50
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------
import os

import imgkit

import pyecharts.options as opts
from pyecharts.charts import Radar, Bar
import uuid
import cv2
from app.api.engine import work_dir


def gen_radar_chart(radar_chart_data):
    """
    Gallery 使用 pyecharts 1.1.0
    参考地址: https://echarts.apache.org/examples/editor.html?c=radar
    目前无法实现的功能:
    1、雷达图周围的图例的 textStyle 暂时无法设置背景颜色
    """
    # v1 = [[4300, 10000, 28000, 35000, 50000]]
    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(198, 167, 116, 0.5)',
                    'rgba(198, 167, 116, 0.4)',
                    'rgba(198, 167, 116, 0.3)',
                    'rgba(198, 167, 116, 0.2)',
                    'rgba(198, 167, 116, 0.1)',
                ])
            ),
            textstyle_opts=opts.TextStyleOpts(color="#000", font_size=16),
        ).add(
            series_name="预算分配（Allocated Budget）",
            data=v1,
            linestyle_opts=opts.LineStyleOpts(width=2, color="#C6A774"),
            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': '#222A77',
                                         'fontSize': 20,
                                         'lineHeight': 23,
                                         'fontWeight': 'bolder',
                                         }),
            legend_opts=opts.LegendOpts(selected_mode=False, is_show=False)
        )

    html_name = work_dir + '/app/html/' + str(uuid.uuid4()) + '.html'
    img_name = work_dir + '/app/html/img/radar_chart_' + str(uuid.uuid4()) + '.png'
    radar.render(html_name)
    imgkit.from_file(html_name, img_name)
    # 读取图片
    img = cv2.imread(img_name)
    # 获取宽度和高度
    height = len(img)
    width = len(img[0])

    img = img[0:height, 160:750]
    cv2.imwrite(img_name, img)
    os.popen('rm -f {}'.format(html_name))
    return img_name

if __name__ == '__main__':
    gen_radar_chart()