分享

python数据可视化:基于pyecharts的BI可视化报表(I)

 北方的白桦林 2020-11-11



1

大家好!

现在很多公司慢慢都会有自己的BI大屏可视化数据系统,要么自己开发、要么外包,要有多炫就有多炫。

于是乎部门的老板们对于报表的眼光也越来越高,要求也越来越过分,当然费用是没有的、技术支持也是没有的、加班工资也是没有的o(╥﹏╥)o。

还好我知道一句话:“人生苦短我用python”!

今天来分享一下如何用Python基于pyecharts库制作一张BI可视化报表(I),仅仅抛砖引玉,效果还很差,大家多见谅,如下图所示。





2

案例中数据表格结构如下:



3

代码及演示:



from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType

fn = '''
    function(params) {
        if(params.name == '缺口')
            return '\\n\\n\\n';
        return params.name + ' : ' + params.value + '% ';
    }
    '''



def new_label_opts():
    return opts.LabelOpts(formatter=JsCode(fn), position='center')


pie1 = (
    Pie(init_opts=opts.InitOpts(width='250px', height='140px',theme = ThemeType.CHALK))
    .add(
        '',
        [['月达成',70],['缺口',30]],
        center=['25%''50%'],
        radius=[4255],
        label_opts=new_label_opts(),
    )
    .add(
        '',
        [['年达成',68],['缺口',32]],
        center=['75%''50%'],
        radius=[4255],
        label_opts=new_label_opts(),
    )

    .set_global_opts(
        title_opts=opts.TitleOpts(title='SX',pos_left = '43%'),
        legend_opts=opts.LegendOpts(
            is_show = 0,type_='scroll', pos_top='20%', pos_left='80%', orient='vertical'
        ),
    )
    #.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:'))
    #.render('mutiple_pie.html')
)

pie1.render_notebook()



import numpy as np
import pandas as pd
from pyecharts.globals import ThemeType
from pyecharts import options as opts
from pyecharts.charts import Map

df_map = pd.read_excel('D:\\01\\会Excel的隔壁老王\\Excel报表\\20200529-Python个人中心可视化报表\\20200529-Python个人中心可视化报表.xlsx',sheet_name = '全国二级机构年累计计划达成')

map1 = (
    Map(init_opts=opts.InitOpts(width='750px', height='400px',theme = ThemeType.CHALK))
    .add('全国二级机构年累计计划达成', df_map[['二级机构','年累计计划达成率']].values.tolist(), 'china')
    .set_global_opts(
        title_opts=opts.TitleOpts(title='全国二级机构年累计计划达成(微信公众号:会Excel的隔壁老王)',pos_bottom = '88%', pos_right = '14%'),
        visualmap_opts=opts.VisualMapOpts(type_= 'color',max_=1.65, min_ = 1.0,  range_text = ['计划达成率区间:'''],is_piecewise=1,pos_top= 'middle',pos_left='right',orient='vertical',split_number=5, textstyle_opts = opts.TextStyleOpts(color = '#ffffff')),
        #tooltip_opts = opts.TooltipOpts(formatter='{c} %')
    )
    #.render('map_visualmap_piecewise.html')
)

map1.render_notebook()



4

完整python代码参考:


from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
import numpy as np
import pandas as pd
from pyecharts.globals import ThemeType
from pyecharts.charts import *
from bs4 import BeautifulSoup


fn = '''
    function(params) {
        if(params.name == '缺口')
            return '\\n\\n\\n';
        return params.name + ' : ' + params.value + '% ';
    }
    '''



def new_label_opts():
    return opts.LabelOpts(formatter=JsCode(fn), position='center')


pie1 = (
    Pie(init_opts=opts.InitOpts(width='250px', height='140px',theme = ThemeType.CHALK))
    .add(
        '',
        [['月达成',70],['缺口',30]],
        center=['25%''50%'],
        radius=[4255],
        label_opts=new_label_opts(),
    )
    .add(
        '',
        [['年达成',68],['缺口',32]],
        center=['75%''50%'],
        radius=[4255],
        label_opts=new_label_opts(),
    )

    .set_global_opts(
        title_opts=opts.TitleOpts(title='SX',pos_left = '43%'),
        legend_opts=opts.LegendOpts(
            is_show = 0,type_='scroll', pos_top='20%', pos_left='80%', orient='vertical'
        ),
    )
    #.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:'))
    #.render('mutiple_pie.html')
)

#1-------------------------------------------------------------------------------------------------------------

pie2 = (
    Pie(init_opts=opts.InitOpts(width='250px', height='140px',theme = ThemeType.CHALK))
    .add(
        '',
        [['月达成',30],['缺口',70]],
        center=['25%''50%'],
        radius=[4255],
        label_opts=new_label_opts(),
    )
    .add(
        '',
        [['年达成',68],['缺口',32]],
        center=['75%''50%'],
        radius=[4255],
        label_opts=new_label_opts(),
    )

    .set_global_opts(
        title_opts=opts.TitleOpts(title='SSY',pos_left = '40%'),
        legend_opts=opts.LegendOpts(
            is_show = 0,type_='scroll', pos_top='20%', pos_left='80%', orient='vertical'
        ),
    )
    #.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:'))
    #.render('mutiple_pie.html')
)


#2-------------------------------------------------------------------------------------------------------------

pie3 = (
    Pie(init_opts=opts.InitOpts(width='250px', height='140px',theme = ThemeType.CHALK))
    .add(
        '',
        [['月达成',50],['缺口',50]],
        center=['25%''50%'],
        radius=[4255],
        label_opts=new_label_opts(),
    )
    .add(
        '',
        [['年达成',68],['缺口',32]],
        center=['75%''50%'],
        radius=[4255],
        label_opts=new_label_opts(),
    )

    .set_global_opts(
        title_opts=opts.TitleOpts(title='SXDX',pos_left = '40%'),
        legend_opts=opts.LegendOpts(
            is_show = 0,type_='scroll', pos_top='20%', pos_left='80%', orient='vertical'
        ),
    )
    #.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:'))
    #.render('mutiple_pie.html')
)


#3-------------------------------------------------------------------------------------------------------------

df_map = pd.read_excel('D:\\01\\会Excel的隔壁老王\\Excel报表\\20200529-Python个人中心可视化报表\\20200529-Python个人中心可视化报表.xlsx',sheet_name = '全国二级机构年累计计划达成')

map1 = (
    Map(init_opts=opts.InitOpts(width='750px', height='400px',theme = ThemeType.CHALK))
    .add('全国二级机构年累计计划达成', df_map[['二级机构','年累计计划达成率']].values.tolist(), 'china')
    .set_global_opts(
        title_opts=opts.TitleOpts(title='全国二级机构年累计计划达成(微信公众号:会Excel的隔壁老王)',pos_bottom = '88%', pos_right = '14%'),
        visualmap_opts=opts.VisualMapOpts(type_= 'color',max_=1.65, min_ = 1.0,  range_text = ['计划达成率区间:'''],is_piecewise=1,pos_top= 'middle',pos_left='right',orient='vertical',split_number=5, textstyle_opts = opts.TextStyleOpts(color = '#ffffff')),
        #tooltip_opts = opts.TooltipOpts(formatter='{c} %')
    )
    #.render('map_visualmap_piecewise.html')
)


#4-------------------------------------------------------------------------------------------------------------

big_title = (
    Pie()
        .set_global_opts(
            title_opts=opts.TitleOpts(title='可视化报表',title_textstyle_opts=opts.TextStyleOpts(font_size=40, color='#FFFFFF',border_radius=True, border_color='white'),pos_top=0)
        )
)


page = (Page(page_title='可视化报表')
        #.add(big_title)
        .add(pie1)
        .add(pie2)
        .add(pie3)
        .add(map1)
        ).render('可视化报表.html')

with open('可视化报表.html''r+', encoding='utf-8'as html:
    html_bf = BeautifulSoup(html, 'lxml')
    divs = html_bf.select('.chart-container')
    divs[0]['style'] = 'width:250px;height:140px;position:absolute;top:5px;left:0px;border-style:solid;border-color:#FFFFFF;border-width:0px;'
    divs[1]['style'] = 'width:250px;height:140px;position:absolute;top:5px;left:250px;border-style:solid;border-color:#444444;border-width:0px;'
    divs[2]['style'] = 'width:250px;height:140px;position:absolute;top:5px;left:500px;border-style:solid;border-color:#444444;border-width:0px;'
    divs[3]['style'] = 'width:750px;height:540px;position:absolute;top:145px;left:0px;border-style:solid;border-color:#444444;border-width:0px;'

    body = html_bf.find('body')
    body['style'] = 'background-color:#ffffff;'
    html_new = str(html_bf)
    html.seek(00)
    html.truncate()
    html.write(html_new)
    html.close()

print('-------------------')




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多