python flask pyecharts kline 01.py
1 #endcoing:utf-8 2 # 參考 https://blog.csdn.net/m0_59236127/article/details/137670671 3 4 from flask import Flask,render_template 5 6 from pyecharts.globals import CurrentConfig 7 CurrentConfig.ONLINE_HOST = "https://cdn.kesci.com/lib/pyecharts_assets/" 8 9 from pyecharts import options as opts 10 from pyecharts.globals import ThemeType 11 from pyecharts.charts import Bar,Kline , Line , Grid 12 13 #from jinja2 import Markup 14 15 from jinja2.utils import markupsafe 16 17 app = Flask(__name__) 18 19 def bar_base(): 20 bar = ( 21 Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)) 22 .add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]) 23 .add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) 24 .add_yaxis("商家B", [15, 25, 30, 18, 65, 70]) 25 .set_global_opts(title_opts=opts.TitleOpts(title="主標題", subtitle="副標題")) 26 ) 27 return bar 28 29 30 date_list = ["2020/4/{}".format(i + 1) for i in range(30)] 31 y_data = [ 32 [2320.26, 2320.26, 2287.3, 2362.94], 33 [2300, 2291.3, 2288.26, 2308.38], 34 [2295.35, 2346.5, 2295.35, 2345.92], 35 [2347.22, 2358.98, 2337.35, 2363.8], 36 [2360.75, 2382.48, 2347.89, 2383.76], 37 [2383.43, 2385.42, 2371.23, 2391.82], 38 [2377.41, 2419.02, 2369.57, 2421.15], 39 [2425.92, 2428.15, 2417.58, 2440.38], 40 [2411, 2433.13, 2403.3, 2437.42], 41 [2432.68, 2334.48, 2427.7, 2441.73], 42 [2430.69, 2418.53, 2394.22, 2433.89], 43 [2416.62, 2432.4, 2414.4, 2443.03], 44 [2441.91, 2421.56, 2418.43, 2444.8], 45 [2420.26, 2382.91, 2373.53, 2427.07], 46 [2383.49, 2397.18, 2370.61, 2397.94], 47 [2378.82, 2325.95, 2309.17, 2378.82], 48 [2322.94, 2314.16, 2308.76, 2330.88], 49 [2320.62, 2325.82, 2315.01, 2338.78], 50 [2313.74, 2293.34, 2289.89, 2340.71], 51 [2297.77, 2313.22, 2292.03, 2324.63], 52 [2322.32, 2365.59, 2308.92, 2366.16], 53 [2364.54, 2359.51, 2330.86, 2369.65], 54 [2332.08, 2273.4, 2259.25, 2333.54], 55 [2274.81, 2326.31, 2270.1, 2328.14], 56 [2333.61, 2347.18, 2321.6, 2351.44], 57 [2340.44, 2324.29, 2304.27, 2352.02], 58 [2326.42, 2318.61, 2314.59, 2333.67], 59 [2314.68, 2310.59, 2296.58, 2320.96], 60 [2309.16, 2286.6, 2264.83, 2333.29], 61 [2282.17, 2263.97, 2253.25, 2286.33], ] 62 63 columnsName=[ 'o' , 'c' , 'l' , 'h' ] 64 import pandas as pd 65 data=pd.DataFrame( y_data , columns= columnsName ) 66 67 data["ma5"]=data["c"].rolling(5).mean().dropna() 68 data["ma10"]=data["c"].rolling(10).mean().dropna() 69 data["ma20"]=data["c"].rolling(20).mean().dropna() 70 print( data["ma5"] ) 71 72 def kline_base( ) : 73 74 markMaxMinPointsData__=[ opts.MarkPointItem(type_='max', name='最高價'), 75 opts.MarkPointItem(type_='min', name='最低價')] 76 kline = (Kline() 77 .add_xaxis(date_list) 78 .add_yaxis('', y_data, 79 itemstyle_opts=opts.ItemStyleOpts(color="#ff0000", color0="#00ff00"), 80 markpoint_opts=opts.MarkPointOpts(data=markMaxMinPointsData__) 81 ) 82 .set_global_opts( 83 legend_opts=opts.LegendOpts(is_show=True, 84 pos_bottom=10, 85 pos_left="center"), 86 87 datazoom_opts=[ 88 opts.DataZoomOpts( 89 is_show=False, 90 type_="slider", 91 xaxis_index=[0], 92 range_start=50, 93 range_end=100, 94 ), 95 opts.DataZoomOpts( 96 is_show=True, 97 xaxis_index=[0], 98 type_="slider", 99 pos_top="85%", 100 range_start=100, 101 range_end=100, 102 ), 103 ], 104 yaxis_opts=opts.AxisOpts( 105 is_scale=True, 106 splitarea_opts=opts.SplitAreaOpts( 107 is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1) 108 ), 109 ), 110 111 ) 112 113 ) 114 115 116 #kline.render_notebook() 117 return kline 118 119 def line_base(): 120 line=(Line() ) 121 # line.add_xaxis(xaxis_data=list(data.index)) 122 line.add_xaxis( date_list ) 123 124 line.add_yaxis( 125 "ma5", 126 data["ma5"].tolist(), 127 color="#ff0000", 128 # xaxis_index=1, 129 # yaxis_index=1, 130 is_symbol_show=False , 131 label_opts=opts.LabelOpts(is_show=False), 132 ) 133 134 # line.add_yaxis( 135 # series_name="ma10", 136 # y_axis=data["ma10"].tolist(), 137 # xaxis_index=1, 138 # yaxis_index=1, 139 # label_opts=opts.LabelOpts(is_show=True), 140 # ) 141 142 # line.add_yaxis( 143 # series_name="ma20", 144 # y_axis=data["ma20"].tolist(), 145 # xaxis_index=1, 146 # yaxis_index=1, 147 # label_opts=opts.LabelOpts(is_show=True), 148 # ) 149 150 line.set_global_opts( legend_opts=opts.LegendOpts( pos_left='right') ) 151 return line 152 153 154 155 @app.route('/', methods=['GET', 'POST']) 156 def index(): 157 bar = bar_base() 158 # return markupsafe.Markup(bar.render_embed()) 159 klineObj=kline_base() 160 lineObj=line_base() 161 klineObj.overlap( lineObj ) 162 163 gridObj=Grid( init_opts= opts.InitOpts(width="640px" , height="320px") ) 164 gridObj.add( klineObj , grid_opts=opts.GridOpts(pos_left='10%', pos_right='10%',height='60%') ) 165 166 return markupsafe.Markup( gridObj.render_embed() ) 167 168 if __name__ == '__main__': 169 app.run(host="127.0.0.1", port=5000,debug=True)