QDateTimeAxis not shown in QChartView
Unsolved
Language Bindings
-
I want to plot QDate timestamps using QDateTimeAxis. The floats are also generated correctly by QDateTime.toMSecsSinceEpoch() methods for QLineSeries. But there is no QDateTimeAxis shown in QChartView and no time stamps are plotted
Here is userchartwidget.pyfrom PyQt5.QtChart import QChartView from PyQt5.QtWidgets import QWidget, QHBoxLayout, QApplication, QMainWindow from PyQt5.QtCore import Qt, QEvent, pyqtSlot, QDate from charts import UserLogChart from utils import Geometry from models import ModelUserChart class UserChartWidget(QWidget): def __init__(self, parent, width=1400, height=700): QWidget.__init__(self, parent) self.setMaximumSize(width, height) self.setMinimumSize(width, height) self.layout = QHBoxLayout() self.setLayout(self.layout) self.layout.setContentsMargins(0,0,0,0) self.layout.setSpacing(5) # Create chart (View) self.chart = UserLogChart() def setModel(self, model): self.chart.setModel(model) def create(self): # Create the View chartView = QChartView(self.chart) chartView.setRubberBand(QChartView.HorizontalRubberBand) chartView.setRubberBand(QChartView.VerticalRubberBand) self.chart.update() self.chart.createDefaultAxes() self.layout.addWidget(chartView) self.grabGesture(Qt.PinchGesture) self.grabGesture(Qt.SwipeGesture) def event(self, evt): if evt.type() == QEvent.Gesture: if evt.gesture(Qt.PinchGesture): print("Pinch detected") #self.chart.zoomIn() elif evt.gesture(Qt.SwipeGesture): print("Swipe detected") else: print("Gesture detected") evt.accept() print("gesture detected") return True ''' Tester ''' if __name__ == "__main__": app = QApplication([]) screen_resolution = app.desktop().screenGeometry() width, height = screen_resolution.width(), screen_resolution.height() geo = Geometry() geo.appDims = (width, height) dims = geo.appDims window = QMainWindow() # create Model for chart widget model = ModelUserChart() model.intervalList = [(QDate(2019,3,13),15, 20),(QDate(2019,3,13),25,30), (QDate(2019,3,14), 5, 10)] userChartWidget = UserChartWidget(window) userChartWidget.setModel(model) userChartWidget.create() window.setCentralWidget(userChartWidget) window.show() app.exec_()
and here are is the model for the chart:
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal class ModelUserChart(QObject): signal = pyqtSignal() def __init__(self): QObject.__init__(self) self._intervalList = [] @pyqtProperty(list) def intervalList(self, notify=signal): return self._intervalList @intervalList.setter def intervalList(self, intervals): self._intervalList = intervals self.signal.emit()
and here is the class extending the QChart:
from PyQt5.QtChart import QChart, QLineSeries, QValueAxis, QDateTimeAxis from PyQt5.QtCore import QPoint, Qt, pyqtSlot, QDateTime, QDate from PyQt5.QtGui import QPen, QColor class UserLogChart(QChart): def __init__(self): super(UserLogChart, self).__init__() self.legend() self.setTheme(self.ChartThemeBlueIcy) self.setAnimationOptions(self.SeriesAnimations) self.setAnimationDuration(70) self.colorNamesList = QColor().colorNames() self.model = None self.allSeries = [] self.pen = QPen() color=QColor("orange") self.pen.setWidth(30) self.pen.setColor(color) # Create SINGLE axes self.xAxis = QDateTimeAxis() self.xAxis.setTickCount(10) self.xAxis.setFormat("MM yyyy") self.xAxis.setTitleText("Datum") self.yAxis = QValueAxis() # Set axes ranges #self.xAxis.setRange(0.0, 30.0) self.yAxis.setRange(0.0, 50) self.yAxis.setTitleText("Zeit") self.addAxis(self.xAxis, Qt.AlignBottom) self.addAxis(self.yAxis, Qt.AlignLeft) #self.legend().hide() def _addInterval(self, interval): self.lineSeries = QLineSeries() self.allSeries.append(self.lineSeries) assert isinstance(interval[0], QDate) print(interval[0]) momentInTime = QDateTime() momentInTime.setDate(interval[0]) print(momentInTime.toMSecsSinceEpoch()) self.allSeries[-1].append(momentInTime.toMSecsSinceEpoch(), interval[1]) self.allSeries[-1].append(momentInTime.toMSecsSinceEpoch(), interval[2]) self.allSeries[-1].setPen(self.pen) self.allSeries[-1].setPointsVisible(True) self.addSeries(self.allSeries[-1]) self.allSeries[-1].attachAxis(self.yAxis) self.allSeries[-1].attachAxis(self.xAxis) def setModel(self, model): self.removeAllSeries() self.model = model self.allSeries = [] intervalList = self.model.intervalList for interval in intervalList: self._addInterval(interval) self.model.signal.connect(self.updateIntervals) self.update() for curv in self.series(): print("self.allCruvs[] : ", len(curv), "is visible : ", curv.isVisible()) @pyqtSlot() def updateIntervals(self): print("View called") self.removeAllSeries() intervalList = self.model.intervalList for interval in intervalList: self._addInterval(interval)