利用pyecharts做数据大屏dashboard

2022-01-17 08:01:09 浏览数 (2)

Python数据可视化

利用pyecharts做数据大屏dashboard
首先做一个折线图
代码语言:javascript复制
import os

from pyecharts import options as opts
from pyecharts.charts import Line
from pyecharts.globals import ThemeType

from main import tbsi_total


def Line_todayfund():
    r = tbsi_total()  # 取出我的基金持仓并计算
    attr = [i[0] for i in r]
    v1 = [round(i[1], 2) for i in r]
    # 主题调成紫色 报表id设置是为了后面整体布局用
    line = (Line(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION, chart_id=1))
            # 添加横坐标
            .add_xaxis(attr)
            # 添加数据
            .add_yaxis("", v1, is_smooth=True, is_symbol_show=False,
                       markpoint_opts=opts.MarkPointOpts(
                           data=[
                               opts.MarkPointItem(type_="max", name="最大值"),
                               opts.MarkPointItem(type_="min", name="最小值"),
                           ]
                       ),
                       markline_opts=opts.MarkLineOpts(
                           data=[opts.MarkLineItem(type_="average", name="平均值")]
                       ),
                       )
            .set_global_opts(title_opts=opts.TitleOpts(title="实时收益", subtitle="基金收益"),
                             # 鼠标悬浮显示数值 纵坐标
                             axispointer_opts=opts.AxisPointerOpts(
                                 is_show=True, link=[{"xAxisIndex": "all"}]
                             ),
                             # 鼠标悬浮显示数值 横坐标
                             tooltip_opts=opts.TooltipOpts(trigger="axis"),
                             # 增加横坐标区域缩放
                             datazoom_opts=[
                                 opts.DataZoomOpts(
                                     is_show=True,
                                     is_realtime=True,
                                     # 起始范围
                                     range_start=0,
                                     # 结束范围
                                     range_end=100
                                 )
                             ],
                             ))

    return line


line = Line_todayfund()
line.render('line.html')
os.system('line.html')

获取数据部分不是重点我这边就不展示了

饼图
代码语言:javascript复制
import os

from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.globals import ThemeType

from main import fund_count


def Pip_fundcount():
    r = fund_count()  # 取出我的基金持仓并计算
    x_data = [i[0] for i in r]
    y_data = [abs(i[1]) for i in r]
    data_pair = [list(z) for z in zip(x_data, y_data)]
    # 为了好看先排序了一下
    data_pair.sort(key=lambda x: x[1])
    # 主题调成紫色 报表id设置是为了后面整体布局用
    pie = (Pie(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION, chart_id=2))
        .add(
        "",
        data_pair,
        # 是否玫瑰图 有两种 radius:扇区圆心角展现数据的百分比,半径展现数据的大小
        # area:所有扇区圆心角相同,仅通过半径展现数据大小
        rosetype="radius",
        # 图表圆形半径
        radius="55%",
        # 距离左右距离
        center=["50%", "50%"],
    )
        # 数据整体样式
        .set_global_opts(title_opts=opts.TitleOpts(
        title="持仓分布",
        pos_left="center",
        # 距离标题距离
        pos_top="25",
    ),
        # 最上方图例组件隐藏
        legend_opts=opts.LegendOpts(is_show=False),
    )
    )
    return pie

pie = Pip_fundcount()
pie.render('pie.html')
os.system('pie.html')
柱状图
代码语言:javascript复制
import os

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType

from main import fund_count


def Bar_fundactral():
    r = fund_count()  # 数据准备
    x_data = [i[0] for i in r]
    y_data = [abs(i[1]) for i in r]
    bar = (
        # 主题调成紫色 报表id设置是为了后面整体布局用
        Bar(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION, chart_id=3))
            # 添加横坐标
            .add_xaxis(x_data)
            # 纵坐标数据
            .add_yaxis("收益", y_data)
            # 旋转横纵坐标
            .reversal_axis()
            .set_global_opts(
            # 横坐标放不下数据 旋转字体
            # xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-40)),
            # 标题 副标题
            title_opts=opts.TitleOpts(title="持有收益", subtitle="昨日净值算"),
            # 纵坐标范围筛选
            datazoom_opts=opts.DataZoomOpts(orient="vertical",
                                            # 起始范围
                                            range_start=0,
                                            # 结束范围
                                            range_end=100
                                            ),
            # 最上方图例组件隐藏
            legend_opts=opts.LegendOpts(is_show=False),
        )
            .set_series_opts(
            label_opts=opts.LabelOpts(position="right"),
        )

    )
    return bar


bar = Bar_fundactral()
bar.render('bar.html')
os.system('bar.html')
水球图
代码语言:javascript复制
import os

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


def Liquid_limit():
    liquid = (
        Liquid(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION, chart_id=4))
            .add(
            "",
            [0.3254],
            label_opts=opts.LabelOpts(
                font_size=50,
                formatter=JsCode(
                    """function (param) {
                        return (Math.floor(param.value * 10000) / 100)   '%';
                    }"""
                ),
                position="inside",
            ),
        )
            .set_global_opts(title_opts=opts.TitleOpts(title="剩余额度"))
    )
    return liquid


liquid = Liquid_limit()
liquid.render('liquid.html')
os.system('liquid.html')
词云图
代码语言:javascript复制
import os

from pyecharts import options as opts
from pyecharts.charts import WordCloud
from pyecharts.globals import ThemeType

from api.bk_api import bk_word


def wordcloud_fund():
    data = bk_word() #数据获取
    word = (
        WordCloud(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION, chart_id=5))
            .add(series_name="热点分析", data_pair=data, word_size_range=[6, 66])
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title="热点分析", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
            ),
            tooltip_opts=opts.TooltipOpts(is_show=True),
        )
    )
    return word


word = wordcloud_fund()
word.render('word.html')
os.system('word.html')
结合在一起
代码语言:javascript复制
def Page_total():
    page = (
        Page(layout=Page.DraggablePageLayout)
            .add(
            Line_todayfund(),
            Pip_fundcount(),
            Bar_fundactral(),
            Liquid_limit(),
            wordcloud_fund(),
        )
    )
    page.render('page.html')
Page_total()
os.system('page.html')

把图表拖拉成自己想要的样式和位置 然后保存成json格式的文件

代码语言:javascript复制
def Page_total():
    page = (
        Page(layout=Page.DraggablePageLayout)
            .add(
            Line_todayfund(),
            Pip_fundcount(),
            Bar_fundactral(),
            Liquid_limit(),
            wordcloud_fund(),
        )
    )
    page.render('page.html')
    Page.save_resize_html('page.html',
                          cfg_file='chart_config.json',
                          dest='dashboard.html')
Page_total()
os.system('dashboard.html')

这就是运行好后的数据大屏 (数据来源:天天基金网)

样式和代码已上传

1.代码

2.样式

本站文章除注明转载/出处外,均为本站原创,转载前请务必署名,转载请标明出处 最后编辑时间为: 2021/11/25 15:50:29

0 人点赞