Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. setRange(min, max) doesn't work for QDateTimeAxis. How to do it right?
Forum Updated to NodeBB v4.3 + New Features

setRange(min, max) doesn't work for QDateTimeAxis. How to do it right?

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 1 Posters 772 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    StarterKit
    wrote on last edited by StarterKit
    #1

    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 myQLineSeries not from what I put in axisX.setRange(). I tried to apply setRange() after addSeries() and it had no effect either.

    So, the question is - how I may change Range (or min/max values) for QDateTimeAxis?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      StarterKit
      wrote on last edited by StarterKit
      #2

      Ah, ok, I found my mistake!
      Declaration of setRange() method:
      void setRange(QDateTime min, QDateTime max)
      shows that it takes QDateTime and 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)))

      1 Reply Last reply
      4

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved