话不多说,直接上代码
这里 也有更多pyecharts的代码~
示例
调用库
import pandas as pd import time from functools import partial from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui, QtWidgets from pyecharts import options as opts from pyecharts.charts import Kline, Line, Bar, Grid import webbrowser as wb
K线图、输出在默认浏览器显示
# 移动平均数计算 def moving_average(data, day_count): data = data.values[:, 0] result = [] for i in range(len(data)): start_day_index = i - day_count + 1 if start_day_index <= 0: start_day_index = 0 justified_day_count = i - start_day_index + 1 mean = data[start_day_index:i + 1].sum() / justified_day_count result.append(mean) return result # k线 --项目需求:已实现-- def show_kline(csv_name): # 读取.csv文件, stock_code = 'Brent_OIL' stock_data = pd.read_csv(csv_name, encoding='gb2312') # 将文件内容按照by=[‘date’]内容进行排序 stock_data = stock_data.sort_values(by=["date"], ascending=[True], inplace=False) stock_data_cleared = stock_data[stock_data['close'] > 0] stock_name = stock_data_cleared["position"][0] stock_data_extracted = stock_data_cleared[["open", "close", "low", "high", "volume", "date"]] kline = ( Kline() .add_xaxis(stock_data_extracted["date"].values.tolist()) .add_yaxis("K线图", stock_data_extracted.iloc[:, :4].values.tolist()) .set_global_opts( xaxis_opts=opts.AxisOpts(is_scale=True, is_show=False), # axis_opts=opts.AxisOpts(is_scale=True,min_=0), #y轴起始坐标可以设为0 yaxis_opts=opts.AxisOpts(is_scale=True), # y轴起始坐标可自动调整 #title_opts=opts.TitleOpts(title="价格", subtitle=stock_name + "\n" + stock_code, pos_top="20%"), axispointer_opts=opts.AxisPointerOpts( is_show=True, link=[{"xAxisIndex": "all"}], label=opts.LabelOpts(background_color="#777"), ), datazoom_opts=[ # 设置zoom参数后即可缩放 opts.DataZoomOpts( is_show=True, type_="inside", xaxis_index=[0, 1], # 设置第0轴和第1轴同时缩放 range_start=0, range_end=100, ), opts.DataZoomOpts( is_show=True, xaxis_index=[0, 1], type_="slider", pos_top="90%", range_start=0, range_end=100, ), ], ) ) # 移动平均线 line = ( Line() .add_xaxis(xaxis_data=stock_data_extracted["date"].values.tolist()) .add_yaxis( series_name="MA5", y_axis=moving_average(stock_data_extracted[["close"]], 5), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA10", y_axis=moving_average(stock_data_extracted[["close"]], 10), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA30", y_axis=moving_average(stock_data_extracted[["close"]], 30), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA60", y_axis=moving_average(stock_data_extracted[["close"]], 60), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA120", y_axis=moving_average(stock_data_extracted[["close"]], 120), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA240", y_axis=moving_average(stock_data_extracted[["close"]], 240), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA360", y_axis=moving_average(stock_data_extracted[["close"]], 360), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .set_global_opts(xaxis_opts=opts.AxisOpts(type_="category")) ) # 将K线图和移动平均线显示在一个图内 kline.overlap(line) # 成交量柱形图 x = stock_data_extracted[["date"]].values[:, 0].tolist() y = stock_data_extracted[["volume"]].values[:, 0].tolist() bar = ( Bar() .add_xaxis(x) .add_yaxis("成交量", y, label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts(color="#008080")) .set_global_opts(title_opts=opts.TitleOpts(title="成交量", pos_top="70%"), legend_opts=opts.LegendOpts(is_show=False), ) ) # 使用网格将多张图标组合到一起显示 grid_chart = Grid() grid_chart.add( kline, grid_opts=opts.GridOpts(pos_left="15%", pos_right="8%", height="55%"), ) grid_chart.add( bar, grid_opts=opts.GridOpts(pos_left="15%", pos_right="8%", pos_top="70%", height="20%"), ) htl = csv_name + ".html" grid_chart.render(htl) wb.open(htl)
主函数
def click_success(self): print("数据获取成功!") csv_name1 = 'outside_brent_oil.csv' outside_history_brent_oil_data().to_csv(csv_name1, index=False) show_kline(csv_name1) # html_success() def click_success_3(self): print("数据获取成功!") csv_name2 = 'outside_newyork_oil.csv' outside_history_newyork_oil_data().to_csv(csv_name2, index=False) show_kline(csv_name2) def click_success_4(self): print("数据获取成功!") csv_name3 = 'outside_newyork_gas.csv' outside_history_newyork_natural_gas_data().to_csv(csv_name3, index=False) show_kline(csv_name3) if __name__ == '__main__': app = QApplication(sys.argv) MainWindow = QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() # 外盘期货 ui.pushButton.clicked.connect(click_success) # 布伦特原油期货分析图 ui.pushButton_3.clicked.connect(click_success_3) # 纽约原油期货分析图 ui.pushButton_4.clicked.connect(click_success_4) # 纽约天然气期货分析图 sys.exit(app.exec_())
其中实现跳转代码为
htl = csv_name + ".html" grid_chart.render(htl) wb.open(htl)