达大厦
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 # 表格组件 from pyecharts.options import ComponentTitleOpts 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): # K线图绘制 kline = ( Kline(init_opts=opts.InitOpts(theme=ThemeType.DARK)) .add_xaxis(x) .add_yaxis(series, y) .set_global_opts( # 坐标轴配置项 axis_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='', title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16), ), # 图例组件配置项 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( is_show='', # 是否显示提示框组件,包括提示框浮层和 axisPointer。 trigger_on='',# 提示框触发的条件,可选'mousemove': 鼠标移动时触发。'click': 鼠标点击时触发。'mousemove|click': 同时鼠标移动和点击时触发。'none': 不在 'mousemove' 或 'click' 时触发, ), # 区域缩放配置项 datazoom_opts=opts.ToolBoxFeatureDataZoomOpts( is_show='',# 是否显示该工具。 zoom_title= "区域缩放", # 提示语 back_title= "区域缩放还原", # 提示语 ), # 视觉映射配置项 visualmap_opts=opts.VisualMapOpts( is_show='', # 否显示视觉映射配置 type_= "color",# 映射过渡类型,可选,"color", "size" ) ) ) 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( # 坐标轴配置项 axis_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( is_show='', # 是否显示提示框组件,包括提示框浮层和 axisPointer。 trigger_on='',# 提示框触发的条件,可选'mousemove': 鼠标移动时触发。'click': 鼠标点击时触发。'mousemove|click': 同时鼠标移动和点击时触发。'none': 不在 'mousemove' 或 'click' 时触发, ), # 区域缩放配置项 datazoom_opts=opts.ToolBoxFeatureDataZoomOpts( is_show='',# 是否显示该工具。 zoom_title= "区域缩放", # 提示语 back_title= "区域缩放还原", # 提示语 ), # 视觉映射配置项 visualmap_opts=opts.VisualMapOpts( is_show='', # 否显示视觉映射配置 type_= "color",# 映射过渡类型,可选,"color", "size" ) ) ) def func_table(self): table = ( Table() .add( headers='', rows='' ), ) def func_pie(self): from pyecharts import options as opts from pyecharts.charts import Pie from pyecharts.faker import Faker pie = ( Pie() .add( "", [list(z) for z in zip(Faker.choose(), Faker.values())], radius=["40%", "75%"], ) .set_global_opts( title_opts=opts.TitleOpts(title="Pie-Radius"), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"), ) .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) .render("pie_radius.html") ) table = Table() headers = ["City name", "Area", "Population", "Annual Rainfall"] rows = [ ["Brisbane", 5905, 1857594, 1146.4], ["Adelaide", 1295, 1158259, 600.5], ["Darwin", 112, 120900, 1714.7], ["Hobart", 1357, 205556, 619.5], ["Sydney", 2058, 4336374, 1214.8], ["Melbourne", 1566, 3806092, 646.9], ["Perth", 5386, 1554769, 869.4], ] table.add(headers, rows) table.set_global_opts( title_opts=ComponentTitleOpts(title="Table-基本示例", subtitle="我是副标题支持换行哦") ) table.render("table_base.html") 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("完成")