setRange(min, max) doesn't work for QDateTimeAxis. How to do it right?
-
Hi.
I try to draw a chart with a couple of time series and mostly I succeeded with it. But to make it nice I need to adjust min/max values of x/y-axis. And I found that I can't do it for X-axis represented by QDateTimeAxis.
Here is my code:axisX = QtCharts.QDateTimeAxis() axisX.setTickCount(12) axisX.setRange(QDateTime(QDate(2020, 3, 1)).toMSecsSinceEpoch(), QDateTime(QDate(2021, 9, 1)).toMSecsSinceEpoch()) axisX.setFormat("yyyy/MM/dd") axisX.setLabelsAngle(-90) axisY = QtCharts.QValueAxis() axisY.setTickCount(11) axisY.setRange(50.0, 100.0) chartView = QtCharts.QChartView() self.chartView.chart().addAxis(axisX, Qt.AlignBottom) self.chartView.chart().addAxis(axisY, Qt.AlignLeft)As result I get an empty chart (that is right because I didn't create series here) with correct Y-axis labeled from 50 to 100. But X-axis is empty.
Ok, it appears it depends on series data. So, I added it with following simple code:chartView.chart().addSeries(myQLineSeries) chartView.chart().setAxisX(axisX, myQLineSeries)Now X-axis is labeled and I see that ticks count (12), format ("yyyy/MM/dd") and angle (-90 degrees) are applied. But... min and max values are taken from
myQLineSeriesnot from what I put inaxisX.setRange(). I tried to applysetRange()afteraddSeries()and it had no effect either.So, the question is - how I may change Range (or min/max values) for
QDateTimeAxis? -
Ah, ok, I found my mistake!
Declaration ofsetRange()method:
void setRange(QDateTime min, QDateTime max)
shows that it takesQDateTimeand not unix timestamp.
I was confused because series take unix timestamps as date/time data and I expected here to be the same.
It was my mistake, now it works with:
axisX.setRange(QDateTime(QDate(2020, 3, 1)), QDateTime(QDate(2021, 9, 1)))