Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QChart not displaying any series data when using QDateTimeAxis
Forum Updated to NodeBB v4.3 + New Features

QChart not displaying any series data when using QDateTimeAxis

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.2k 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.
  • I Offline
    I Offline
    InverseTransform
    wrote on 22 Oct 2018, 08:54 last edited by InverseTransform
    #1

    Hi,

    I want to display a real-time plot using QChart and QLineSeries where X values are timestamps. Everything works as expected when I use QValueAxis for X axis, but switching to QDateTimeAxis results in no data being plotted - just an empty chart.

    Sample code that demonstrates the problem:

    QLineSeries *series = new QLineSeries();
    series->setUseOpenGL(true);
    
    QChart *chart = new QChart();
    chart->addSeries(series);
    
    QValueAxis *axisY = new QValueAxis();
    axisY->setTickCount(5);
    axisY->setMinorTickCount(1);
    axisY->setLabelFormat("%.2f");
    
    QDateTimeAxis *axisX = new QDateTimeAxis(); // Using QValueAxis here instead makes the problem disappear.
    axisX->setTitleText("Timestamp");
    axisX->setTickCount(5);
    
    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);
    series->attachAxis(axisX);
    series->attachAxis(axisY);  
    
    QChartView *chartView = new QChartView(chart);  
    chartView->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
    
    // Add some sample points.
    QDateTime xval;
    xval.setDate(QDate(2018, 1, 5));
    series->append(xval.toMSecsSinceEpoch(), 3);
    xval.setDate(QDate(2018, 1, 6));
    series->append(xval.toMSecsSinceEpoch(), 6);
    xval.setDate(QDate(2018, 1, 7));
    series->append(xval.toMSecsSinceEpoch(), 4);
    
    // Set ranges to display.
    chart->axisX()->setRange(series->at(0).x(), series->at(series->count()-1).x()); 
    chart->axisY()->setRange(0.0, 10.0);
    

    This results in an empty chart with default X axis values ('01-01-1970 1:00' for all ticks), yet the same code renders the series correctly if QValueAxis is used instead of QDateTimeAxis.

    What I tried:

    • chartView->repaint() - has no effect;

    • chart->removeSeries()/addSeries() to re-add the series after appending the data; this causes the series to be displayed, but X axis tick values are wrong: they all show default '01-01-1970 ...' labels, not the ones corresponding to the data. Even if this were a working solution, it shouldn't be necessary to remove and re-add the series.

    I'm using Qt 5.9.2 on Windows 10.

    Why does QDateTimeAxis behave differently? Is there a way to make this work consistently regardless of axis type?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      PentaPanda
      wrote on 22 Oct 2018, 09:18 last edited by PentaPanda
      #2

      Maybe it's a Bug!
      See DateTimeAxis and useOpenGL does not draw correctly
      You can try

      series->setUseOpenGL(false);
      

      See What will happen

      1 Reply Last reply
      0
      • I Offline
        I Offline
        InverseTransform
        wrote on 22 Oct 2018, 09:26 last edited by InverseTransform
        #3

        Disabling OpenGL has no effect - no series is displayed. Moreover, I absolutely need OpenGL enabled, so even if that worked, it wouldn't be an acceptable solution in my situation.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MrShawn
          wrote on 22 Oct 2018, 20:59 last edited by
          #4

          A lot of times "Not displaying" or "Not updating" that I have seen come from axis issues.

          I am guessing that it is having a hard time converting QReal to QDateTime perhaps? Also worth printing out the min and max for your x axis range to see if it is changing. Also try putting in hard date times and see if that works. I noticed that QReals do NOT work with setRange on QDateTime Axis, so maybe because the chart pointer returns an abstract axis type and is probably failing to put the correct values into your range for your axis. Either cast the axis pointer to DateTimeAxis, or use the poiinter you already created.

          try:

          axisX->setRange(QDateTime::fromMSecsSinceEpoch(series->at(0).x()), 
                    QDateTime::fromMSecsSinceEpoch(series->at(series->count()-1).x())); 
          
          
          1 Reply Last reply
          1
          • I Offline
            I Offline
            InverseTransform
            wrote on 23 Oct 2018, 10:23 last edited by InverseTransform
            #5

            @MrShawn said in QChart not displaying any series data when using QDateTimeAxis:

            axisX->setRange(QDateTime::fromMSecsSinceEpoch(series->at(0).x()),
            QDateTime::fromMSecsSinceEpoch(series->at(series->count()-1).x()));

            Thanks for the tip, this solved it. So fromMSecsSinceEpoch() must be called to obtain the right range for values added with toMSecsSinceEpoch(). Ironically, I was not using the right inverse transform ...

            1 Reply Last reply
            0

            5/5

            23 Oct 2018, 10:23

            • Login

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