from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
from PyQt5.QtCore import Qt
from PyQt5.QtChart import *
import math
import numpy as np
import pandas as pd
import datetime
df = pd.read_csv('file.txt',
index_col='DATE',
parse_dates=True,
infer_datetime_format=True)
date = df.iloc[:, 0].index.date
o = df.iloc[:, 0].values
h = df.iloc[:, 1].values
l = df.iloc[:, 2].values
z = df.iloc[:, 3].values
x = len(z)
x_ = x - 1
qt = [None] * x
for i in range(0, x):
qt[i] = (date[i].strftime("%Y/%m/%d"))
print(type(date[0]))
def __init__(self, parent=None):
super().__init__(parent)
self.step = 30
self._chart_view = QtChart.QChartView()
QtCore.Qt.Horizontal,
sliderMoved=self.onAxisSliderMoved,
pageStep=self.step,
)
self.scrollbar.setRange(0, x_)
central_widget
= QtWidgets.
QWidget() self.setCentralWidget(central_widget)
for w in (self._chart_view, self.scrollbar):
lay.addWidget(w)
self._chart = QtChart.QChart()
self._candlestick_serie = QtChart.QCandlestickSeries()
tm = []
for i in range(0, len(z)):
o_ = o[i]
h_ = h[i]
l_ = l[i]
c_ = z[i]
#tm = qt[i]
tm.append(date[i])
self._candlestick_serie.append(QtChart.QCandlestickSet(o_, h_, l_, c_))
min_x, max_x = 0, x_
self._chart.addSeries(self._candlestick_serie)
self._chart.legend().hide()
self._chart.axisX(self._candlestick_serie).setCategories(tm)
axisX = QDateTimeAxis()
axisX.setTickCount(5)
axisX.setFormat("dd.MM.yyyy")
self._chart.addAxis(axisX, Qt.AlignBottom)
self._candlestick_serie.attachAxis(axisX)
axisY = QValueAxis()
axisY.setLabelFormat("%i")
self._chart.addAxis(axisY, Qt.AlignLeft)
self._candlestick_serie.attachAxis(axisY)
self._chart_view.setChart(self._chart)
self.lims = np.array([min_x, max_x])
self.onAxisSliderMoved(self.scrollbar.value())
self.adjust_axes(1, 31)
#self._chart.axisX(self._candlestick_serie).setTickCount(7)
def adjust_axes(self, value_min, value_max):
if value_min > 0 and value_max > 0 and value_max <= x_ and value_max > value_min:
self._chart.axisX(self._candlestick_serie).setRange(str(value_min), str(value_max))
@QtCore.pyqtSlot(int)
def onAxisSliderMoved(self, value):
value2 = value + self.step
value1 = value
if value2 >= x_:
value2 = x_
value1 = value2 - self.step
self.adjust_axes(math.floor(value1), math.ceil(value2))
if __name__ == "__main__":
import sys
w = MainWindow()
w.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
from PyQt5.QtCore import Qt
from PyQt5.QtChart import *
import math
import numpy as np
import pandas as pd
import datetime
df = pd.read_csv('file.txt',
index_col='DATE',
parse_dates=True,
infer_datetime_format=True)
date = df.iloc[:, 0].index.date
o = df.iloc[:, 0].values
h = df.iloc[:, 1].values
l = df.iloc[:, 2].values
z = df.iloc[:, 3].values
x = len(z)
x_ = x - 1
qt = [None] * x
for i in range(0, x):
qt[i] = (date[i].strftime("%Y/%m/%d"))
print(type(date[0]))
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.step = 30
self._chart_view = QtChart.QChartView()
self.scrollbar = QtWidgets.QScrollBar(
QtCore.Qt.Horizontal,
sliderMoved=self.onAxisSliderMoved,
pageStep=self.step,
)
self.scrollbar.setRange(0, x_)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
for w in (self._chart_view, self.scrollbar):
lay.addWidget(w)
self._chart = QtChart.QChart()
self._candlestick_serie = QtChart.QCandlestickSeries()
tm = []
for i in range(0, len(z)):
o_ = o[i]
h_ = h[i]
l_ = l[i]
c_ = z[i]
#tm = qt[i]
tm.append(date[i])
self._candlestick_serie.append(QtChart.QCandlestickSet(o_, h_, l_, c_))
min_x, max_x = 0, x_
self._chart.addSeries(self._candlestick_serie)
self._chart.legend().hide()
self._chart.axisX(self._candlestick_serie).setCategories(tm)
axisX = QDateTimeAxis()
axisX.setTickCount(5)
axisX.setFormat("dd.MM.yyyy")
self._chart.addAxis(axisX, Qt.AlignBottom)
self._candlestick_serie.attachAxis(axisX)
axisY = QValueAxis()
axisY.setLabelFormat("%i")
self._chart.addAxis(axisY, Qt.AlignLeft)
self._candlestick_serie.attachAxis(axisY)
self._chart_view.setChart(self._chart)
self.lims = np.array([min_x, max_x])
self.onAxisSliderMoved(self.scrollbar.value())
self.adjust_axes(1, 31)
#self._chart.axisX(self._candlestick_serie).setTickCount(7)
def adjust_axes(self, value_min, value_max):
if value_min > 0 and value_max > 0 and value_max <= x_ and value_max > value_min:
self._chart.axisX(self._candlestick_serie).setRange(str(value_min), str(value_max))
@QtCore.pyqtSlot(int)
def onAxisSliderMoved(self, value):
value2 = value + self.step
value1 = value
if value2 >= x_:
value2 = x_
value1 = value2 - self.step
self.adjust_axes(math.floor(value1), math.ceil(value2))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks