Created
June 20, 2021 13:37
-
-
Save mr-yoo/005278b9b975cf5918172bbff3e90cab to your computer and use it in GitHub Desktop.
두 개의 축을 갖는 QtChart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtWidgets import * | |
from PyQt5 import uic | |
from PyQt5.QtChart import * | |
from PyQt5.QtGui import * | |
from PyQt5.QtCore import * | |
import pybithumb | |
import time | |
class ChartWorker(QThread): | |
# -------------------------------- | |
dataSent = pyqtSignal(dict) | |
def run(self): | |
ticker_list = ["BTC", "XRP"] | |
while True: | |
priceDict = { } | |
for ticker in ticker_list: | |
price = pybithumb.get_current_price(ticker) | |
priceDict[ticker] = price | |
self.dataSent.emit(priceDict) | |
time.sleep(1) | |
# -------------------------------- | |
class ChartWidget(QWidget): | |
def __init__(self): | |
super().__init__() | |
uic.loadUi("chart.ui", self) | |
# 1) 데이터 추가 | |
self.priceData = QLineSeries() | |
# 2) 도화지 연결 | |
self.priceChart = QChart() | |
self.priceChart.addSeries(self.priceData) | |
self.priceChart.legend().hide() | |
self.priceChart.layout().setContentsMargins(0, 0, 0, 0) | |
ax = QDateTimeAxis() | |
ax.setFormat("hh:mm:ss") | |
ax.setTickCount(4) | |
self.priceChart.addAxis(ax, Qt.AlignBottom) | |
ay = QValueAxis() | |
ay.setVisible(False) | |
self.priceChart.addAxis(ay, Qt.AlignLeft) | |
self.priceData.attachAxis(ax) | |
self.priceData.attachAxis(ay) | |
# -------------------------------- | |
# secondary axis | |
self.secData = QLineSeries() | |
secY = QValueAxis() | |
secY.setVisible(False) | |
self.priceChart.addSeries(self.secData) | |
self.priceChart.addAxis(secY, Qt.AlignRight) | |
self.secData.attachAxis(ax) | |
self.secData.attachAxis(secY) | |
# -------------------------------- | |
# 3) 위젯에 출력 | |
self.priceView.setChart(self.priceChart) | |
self.priceView.setRenderHints(QPainter.Antialiasing) | |
self.cw = ChartWorker() | |
self.cw.dataSent.connect(self.appendData) | |
self.cw.start() | |
self.viewLimit = 60 | |
def appendData(self, priceDict): | |
if len(self.priceData) == self.viewLimit: | |
self.priceData.remove(0) | |
# -------------------------------- | |
self.secData.remove(0) | |
# -------------------------------- | |
curr = QDateTime.currentDateTime() | |
self.priceData.append(curr.toMSecsSinceEpoch(), priceDict["BTC"]) | |
# -------------------------------- | |
self.secData.append(curr.toMSecsSinceEpoch(), priceDict["XRP"]) | |
# -------------------------------- | |
pvs = self.priceData.pointsVector() | |
x = pvs[0].x() | |
s = QDateTime.fromMSecsSinceEpoch( int(x) ) | |
e = s.addSecs(self.viewLimit) | |
ax = self.priceChart.axisX() | |
ax.setRange( s, e ) | |
ay = self.priceChart.axisY() | |
dataY = [item.y() for item in pvs] | |
minVal = min(dataY) | |
maxVal = max(dataY) | |
margin = (maxVal - minVal) * 0.2 | |
ay.setRange( minVal - margin, maxVal + margin ) | |
# -------------------------------- | |
pvs = self.secData.pointsVector() | |
dataY = [item.y() for item in pvs] | |
minVal = min(dataY) | |
maxVal = max(dataY) | |
margin = (maxVal - minVal) * 0.2 | |
ay = self.priceChart.axisY(self.secData) | |
ay.setRange( minVal - margin, maxVal + margin ) | |
# -------------------------------- | |
if __name__ == "__main__": | |
app = QApplication([]) | |
m = ChartWidget() | |
m.show() | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment