ghhhhhh

from pyecharts import options as opts  # 全局、系列配置
from pyecharts.charts import Bar, Grid, Line  # 柱形图
from pyecharts.globals import ThemeType  # 主题风格
from pyecharts.charts import Kline, Line, Bar, Gauge, Pie, Scatter, TreeMap  # K线图、线形图、柱状图、仪表盘、饼图、散点图、矩形树图
from pyecharts.charts import Grid, Tab  # 组合组件、分页组件
from pyecharts.components import Table  # 表格组件
import time  # 时间模块
import pandas as pd  # Pandas模块
import numpy as np  # Numpy模块
from sqlalchemy import create_engine  # 数据引擎模块

conn = create_engine('mysql+pymysql://root:test@127.0.0.1:3306/quant_db?charset=utf8')  # 创建引擎


#################################################################################################################################
class Module:
    '''
    1、上证指数、涨跌幅
    2、深证指数、涨跌幅
    3、创业板指数、涨跌幅
    '''

    def __init__(self, sql):
        self.sql = sql
        self.data = pd.read_sql(self.sql, conn)

    def func_kline(self, x, y, series, title,title_link,pos_left,**kwargs):
        # 数据处理部分
        x = self.data['trade_date'].tolist()
        y = []

        for index, row in self.data.iterrows():
            list = [row['open'], row['close'], row['low'], row['high']]
            y.append(list)

        # K线图绘制
        kline = (
            Kline(init_opts=opts.InitOpts(theme=ThemeType.DARK))
                .add_xaxis(x)
                .add_yaxis(series, y)
                .set_global_opts(
                    xaxis_opts=opts.AxisOpts(is_scale=True),
                    yaxis_opts=opts.AxisOpts(
                        is_scale=True,
                        splitarea_opts=opts.SplitAreaOpts(
                            is_show=True,
                            areastyle_opts=opts.AreaStyleOpts(opacity=1)
                        ),
                    ),
                    # 标题组件配置项
                    title_opts=opts.TitleOpts(
                        title=title, # 主标题文本
                        title_link=title_link,   # 主标题跳转url链接,可以为None
                        pos_left='',  # 标题组件离容器左侧的距离,可以为None,具体值,百分比,‘left、right、center’
                        pos_right='',
                        pos_top = '',
                        pos_bottom=''
                    ),

                    # 图例组件配置项
                    legend_opts=opts.LegendOpts(
                        pos_left='',  # 图例组件离容器左侧的距离,可以为None,具体值,百分比,‘left、right、center’
                        pos_right='',
                        pos_top='',
                        pos_bottom='',
                        orient='',   #  图例列表的布局朝向。可选:'horizontal', 'vertical'
                        legend_icon='' #  图例项的 icon。标记类型包括 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none'
                    ),

                    # 工具箱组件配置项
                    toolbox_opts=opts.ToolBoxFeatureSaveAsImagesOpts(
                        type_='',     # 保存的图片格式。支持 'png' 和 'jpeg'。
                        is_show='', # 是否显示该工具。
                        title= "保存为图片", # 提示语
                    ),

                    # 系列提示框配置项
                    tooltip_opts=opts.TooltipOpts(
                        
                    )

        )
            )
        )

    def func_bar(self, x, y, series, title):
        # 柱形图绘制
        bar = (
            Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
                .add_xaxis(x)
                .add_yaxis(series, y)
                .set_global_opts(
                title_opts=opts.TitleOpts(title=title, pos_top="48%"),
                legend_opts=opts.LegendOpts(pos_top="48%")
            )
        )

        grid = (
            Grid()
                .add(kline, grid_opts=opts.GridOpts(pos_bottom="60%", pos_right='50%'))
                .add(bar, grid_opts=opts.GridOpts(pos_top="60%"))
            # .render("grid_vertical.html")
        )

        return grid

    def func02(self):
        # 1、近90日深证指数走势及涨跌幅

        data = pd.read_sql(sql, conn)

        x = data['trade_date'].tolist()
        y1 = []
        y2 = data['pct_chg'].tolist()

        for index, row in data.iterrows():
            list = [row['open'], row['close'], row['low'], row['high']]
            print(list)
            y1.append(list)

        kline = (
            Kline()
                .add_xaxis(x)
                .add_yaxis("深证指数", y1)
                .set_global_opts(
                xaxis_opts=opts.AxisOpts(is_scale=True),
                yaxis_opts=opts.AxisOpts(
                    is_scale=True,
                    splitarea_opts=opts.SplitAreaOpts(
                        is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
                    ),
                ),
                title_opts=opts.TitleOpts(title="近90日深证指数走势图"),
            )
        )

        bar = (
            Bar()
                .add_xaxis(x)
                .add_yaxis("深证涨跌幅", y2)
                .set_global_opts(
                title_opts=opts.TitleOpts(title="Bar-Brush示例", subtitle="我是副标题", pos_top="48%"),
                legend_opts=opts.LegendOpts(pos_top="48%")
            )
        )

        grid = (
            Grid()
                .add(kline, grid_opts=opts.GridOpts(pos_bottom="60%", pos_right='50%'))
                .add(bar, grid_opts=opts.GridOpts(pos_top="60%"))
            # .render("grid_vertical.html")
        )

        return grid

    def func03(self):
        # 1、近90日创业板指数走势及涨跌幅

        data = pd.read_sql(sql, conn)

        x = data['trade_date'].tolist()
        y1 = []
        y2 = data['pct_chg'].tolist()

        for index, row in data.iterrows():
            list = [row['open'], row['close'], row['low'], row['high']]
            print(list)
            y1.append(list)

        kline = (
            Kline()
                .add_xaxis(x)
                .add_yaxis("创业板指数", y1)
                .set_global_opts(
                xaxis_opts=opts.AxisOpts(is_scale=True),
                yaxis_opts=opts.AxisOpts(
                    is_scale=True,
                    splitarea_opts=opts.SplitAreaOpts(
                        is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
                    ),
                ),
                title_opts=opts.TitleOpts(title="近90日创业板指数走势图"),
            )
        )

        bar = (
            Bar()
                .add_xaxis(x)
                .add_yaxis("创业板涨跌幅", y2)
                .set_global_opts(
                title_opts=opts.TitleOpts(title="Bar-Brush示例", subtitle="我是副标题", pos_top="48%"),
                legend_opts=opts.LegendOpts(pos_top="48%")
            )
        )

    def func_grid(self):
        grid = (
            Grid()
                .add(kline, grid_opts=opts.GridOpts(pos_bottom="60%", pos_right='50%'))
                .add(bar, grid_opts=opts.GridOpts(pos_top="60%"))
            # .render("grid_vertical.html")
        )

        return grid


def main():
    # 上证指数
    sql1 = '''
                SELECT
                    trade_date,     -- 交易日期
                    `open`,         -- 开盘价
                    high,           -- 最高价
                    low,            -- 最低价
                    `close`,        -- 收盘价
                    pct_chg         -- 涨跌幅
                FROM
                    stock_daily 
                WHERE
                    ts_code = '000001.SH'
    '''

    # 深证指数
    sql2 = '''
                SELECT
                    trade_date,     -- 交易日期
                    `open`,         -- 开盘价
                    high,           -- 最高价
                    low,            -- 最低价
                    `close`,        -- 收盘价
                    pct_chg         -- 涨跌幅
                FROM
                    stock_daily 
                WHERE
                    ts_code = '399001.SH'
    '''

    # 创业板指数
    sql3 = '''
                SELECT
                    trade_date,     -- 交易日期
                    `open`,         -- 开盘价
                    high,           -- 最高价
                    low,            -- 最低价
                    `close`,        -- 收盘价
                    pct_chg         -- 涨跌幅
                FROM
                    stock_daily 
                WHERE
                    ts_code = '399006.SH'
    '''


if __name__ == '__main__':
    grid01 = func01()
    grid02
    tab = Tab()
    tab.add(grid01, "bar-example")
    # tab.add(line_markpoint(), "line-example")
    # tab.add(pie_rosetype(), "pie-example")
    # tab.add(grid_mutil_yaxis(), "grid-example")
    tab.render("tab_base.html")

    print("完成")

 

posted @ 2020-07-17 00:50  麦小秋  阅读(967)  评论(0编辑  收藏  举报