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 from pyecharts.commons.utils import JsCode import time # 时间模块 #import pandas as pd # Pandas模块 ########################################################### Pandas数据读写操作 ########################################################### class Pands_Handler: # 初始化 def __init__(self): self.start = start self.end = end # pandas查询数据 def data_query(self, table): sql = '''select * from {table}'''.format(table=table) ########################################################### 宏观资金流动性 ########################################################### class DataCollection: # 正回购、逆回购 def get_data_gksccz(self): data = Pands_Handler().data_query('a_china_znhg') data = data.replace({'正回购': -1, '逆回购': 1}) data['amount'] = data['deal_amount'] * data['trading_method'] data = data[['operation_from_date', 'amount']] data.columns = ['日期', '交易金额'] return data # 货币供应量 def get_data_hbgyl(self): data = Pands_Handler().data_query('a_china_money_supply') data.columns = ['日期', 'M2', 'M1', 'M0'] return data # 上证、深证、创业板指数 def get_data_index(self): data = Pands_Handler().data_query('a_index_daily') data1 = data[data['scode'] == '000001.SH'] # 上证指数 data2 = data[data['scode'] == '399001.SZ'] # 深证指数 data3 = data[data['scode'] == '399006.SZ'] # 创业板指数 data1 = data1[['date', 'oprice', 'hprice', 'lprice', 'cprice', 'pct_chg', 'amount']] data2 = data2[['date', 'oprice', 'hprice', 'lprice', 'cprice', 'pct_chg', 'amount']] data3 = data3[['date', 'oprice', 'hprice', 'lprice', 'cprice', 'pct_chg', 'amount']] data1.columns = ['日期', '开盘价', '最高价', '最低价', '收盘价', '涨跌幅', '成交额'] data2.columns = ['日期', '开盘价', '最高价', '最低价', '收盘价', '涨跌幅', '成交额'] data3.columns = ['日期', '开盘价', '最高价', '最低价', '收盘价', '涨跌幅', '成交额'] return data1, data2, data3 # 沪深股通资金 def get_data_hsgt(self): data = Pands_Handler().data_query('a_hsgt_moneyflow') data.columns = ['日期', '沪股通', '深股通', '北向资金'] return data # Shibor利率 def get_data_shibor(self): data = Pands_Handler().data_query('a_shibor') data = data[['date', 'pre_night_shibor']] data.columns = ['日期', '隔夜shibor'] return data # 沪深两市成交额 def get_data_hsls(self): data = Pands_Handler().data_query('a_stock_daily') data = data[['date','amount']] data = data.groupby('date').sum().reset_index() data.columns = ['日期','成交额'] return data # 沪深竞价成交额 def get_data_call(self): data = Pands_Handler().data_query('a_stock_call_auction') data = data[['date','amount']] data = data.groupby('date').sum().reset_index() data.columns = ['日期','成交额'] return data # 龙虎榜席位成交 def get_data_lhb_classify(self): data = Pands_Handler().data_query('a_stock_lhb_list') # youziicon:一线游资(1)、顶级游资(2)、知名游资(3)、机构(4)、沪股通(5)、深股通(6)、其他(0) data['amount'] = data['buy'] + data['sell'] data = data[['date', 'direction', 'amount', 'youziicon']] data = pd.pivot_table(data, values='amount', columns='direction', index=['date', 'youziicon'], aggfunc='sum', margins=True) data = data.reset_index() data = data[data['date'] != 'All'] data = data.replace({0: '其他', 1: '一线游资', 2: '顶级游资', 3: '知名游资', 4: '机构', 5: '沪股通', 6: '深股通'}) data.columns = ['日期', '资金类型', '买入额', '卖出额', '净买入额'] return data # 大资金流向 def get_data_dzjlx(self): data = Pands_Handler().data_query('a_stock_big_money') data['md_net_amount'] = data['bmd_amount'] - data['smd_amount'] data['lg_net_amount'] = data['blg_amount'] - data['slg_amount'] data['elg_net_amount'] = data['belg_amount'] - data['selg_amount'] data = data[['date','bmd_amount','smd_amount','md_net_amount','blg_amount','slg_amount','lg_net_amount','belg_amount','selg_amount','elg_net_amount','net_amount']] data = data.groupby('date').sum().reset_index() data.columns = ['日期','中单买入','中单卖出','中单净流入','大单买入','大单卖出','大单净流入','超大单买入','超大单卖出','超大单净流入','净流入'] print(data) # 市场情绪 def get_data_scqx(self): data1 = Pands_Handler().data_query('a_market_limit_up_count') data2 = Pands_Handler().data_query('a_stock_scqx_zdtjs') data3 = Pands_Handler().data_query('a_stock_pjgnum') data4 = Pands_Handler().data_query('a_stock_xgdnum') data5 = Pands_Handler().data_query('a_stock_limit_list') class Market: # 特殊柱状图 def sbar(self, x_data, y_data, series_names, title, subtitle): c = ( Bar() .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name=series_names, y_axis=y_data, is_large=True, large_threshold=400, label_opts=opts.LabelOpts( is_show=False, ), ) .set_global_opts( title_opts=opts.TitleOpts( title=title, subtitle=subtitle, item_gap=10, title_textstyle_opts=opts.TextStyleOpts( font_family='Microsoft YaHei', font_weight='bolder', font_size=14, color='', ), subtitle_textstyle_opts=opts.TextStyleOpts( font_family=None, font_size=10, color='', ), ), legend_opts=opts.LegendOpts( is_show=True, pos_right=5, orient='vertical', legend_icon='rect', ), tooltip_opts=opts.TooltipOpts( is_show=True, trigger='axis', trigger_on='click', formatter="{a}: {c}", textstyle_opts=opts.TextStyleOpts( font_family='Arial', font_size=14, ) ), visualmap_opts=opts.VisualMapOpts( is_piecewise=True, pieces=[ {"max": 0, "color": 'green'}, {"min": 0, "color": 'red'} ] ) ) .render('shangzheng.html') ) def cbar(self,x_data,series_names,y_data): bar_1 = ( Bar() .add_xaxis(xaxis_data=data["times"]) .add_yaxis( series_name="Volumn", yaxis_data=data["vols"], xaxis_index=1, yaxis_index=1, label_opts=opts.LabelOpts(is_show=False), # 改进后在 grid 中 add_js_funcs 后变成如下 itemstyle_opts=opts.ItemStyleOpts( color=JsCode( """ function(params) { var colorList; if (barData[params.dataIndex][1] > barData[params.dataIndex][0]) { colorList = '#ef232a'; } else { colorList = '#14b143'; } return colorList; } """ ) ), ) .set_global_opts( xaxis_opts=opts.AxisOpts( type_="category", grid_index=1, axislabel_opts=opts.LabelOpts(is_show=False), ), legend_opts=opts.LegendOpts(is_show=False), ) ) # 常规折线图 def cline(self,x_data): c = ( Line() .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name=series_names, y_axis=y_data, is_smooth=True, linestyle_opts=opts.LineStyleOpts( width=3, opacity=0.5, curve=1, type_='solid', color='' ) ) .set_global_opts(title_opts=opts.TitleOpts(title="Line-smooth")) .render("line_smooth.html") ) def bar1(self): bar = ( Bar() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values(), label_opts=opts.LabelOpts(position="right")) .add_yaxis("商家B", Faker.values(), label_opts=opts.LabelOpts(position="right")) .reversal_axis() .set_global_opts( title_opts=opts.TitleOpts("Timeline-Bar-Reversal (时间: {} 年)".format(i)) ) ) # 特殊折线图 def kline(self): kline = ( Kline() .add_xaxis(xaxis_data=data["times"]) .add_yaxis( series_name="", y_axis=data["datas"], itemstyle_opts=opts.ItemStyleOpts( color="#ef232a", color0="#14b143", border_color="#ef232a", border_color0="#14b143", ), markpoint_opts=opts.MarkPointOpts( data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ] ), markline_opts=opts.MarkLineOpts( label_opts=opts.LabelOpts( position="middle", color="blue", font_size=15 ), data=split_data_part(), symbol=["circle", "none"], ), ) .set_series_opts( markarea_opts=opts.MarkAreaOpts(is_silent=True, data=split_data_part()) ) .set_global_opts( title_opts=opts.TitleOpts(title="K线周期图表", pos_left="0"), xaxis_opts=opts.AxisOpts( type_="category", is_scale=True, boundary_gap=False, axisline_opts=opts.AxisLineOpts(is_on_zero=False), splitline_opts=opts.SplitLineOpts(is_show=False), split_number=20, min_="dataMin", max_="dataMax", ), yaxis_opts=opts.AxisOpts( is_scale=True, splitline_opts=opts.SplitLineOpts(is_show=True) ), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="line"), datazoom_opts=[ opts.DataZoomOpts( is_show=False, type_="inside", xaxis_index=[0, 0], range_end=100 ), opts.DataZoomOpts( is_show=True, xaxis_index=[0, 1], pos_top="97%", range_end=100 ), opts.DataZoomOpts(is_show=False, xaxis_index=[0, 2], range_end=100), ], # 三个图的 axis 连在一块 # axispointer_opts=opts.AxisPointerOpts( # is_show=True, # link=[{"xAxisIndex": "all"}], # label=opts.LabelOpts(background_color="#777"), # ), ) ) def timeline(self): attr = Faker.choose() tl = Timeline() for i in range(2015, 2020): pie = ( Pie() .add( "商家A", [list(z) for z in zip(attr, Faker.values())], rosetype="radius", radius=["30%", "55%"], ) .set_global_opts(title_opts=opts.TitleOpts("某商店{}年营业额".format(i))) ) tl.add(pie, "{}年".format(i)) tl.render("timeline_pie.html") if __name__=='__main__': start = '' end = '' #data = pd.DataFrame() x_data = [1,2,3,4] y_data = [-100,200,30,-50] series_names = '' title = '涨跌幅' subtitle='单位' Market().sbar(x_data,y_data,series_names,title,subtitle) Market().cbar( x_data, series_names, y_data)