c++qtqdatetimeqtcharts

Bad Values in QDateTimeAxis (QtCharts)


I am using QtCharts to display simulation data. The simulation starts at time zero, but my chart axis always seems to start at 19 hours. This confounds me. The set up of the chart is straight forward:

std::vector<SimData> data;

// ... Populate data

auto series = new QLineSeries();

for(auto i : data)
{
  // Append time in milliseconds and a value
  series->append(i.msTime, i.value);
}

this->legend()->hide();

this->addSeries(series);

this->axisX = new QDateTimeAxis;
this->axisX->setTickCount(10);
this->axisX->setFormat("HH:mm:ss");
this->axisX->setTitleText("Sim Time");
this->axisX->setMin(QDateTime());
this->addAxis(this->axisX, Qt::AlignBottom);
series->attachAxis(this->axisX);

this->axisY = new QValueAxis;
this->axisY->setLabelFormat("%i");
this->axisY->setTitleText(x->getID().c_str());
this->addAxis(this->axisY, Qt::AlignLeft);
series->attachAxis(this->axisY);

If I run with no data, but just display the chart, I get this:

QtChart with QDateTimeAxis If I add data, starting at time zero, the total amount of data is correct, but the time still starts at 19:00:00. Why does the time not start at 00:00:00?

QtChart with QDateTimeAxis


Solution

  • The problem was indeed confirmed to be UTC offset. SO had a good example of how to get the UTC Offset which I then used to offset the data going into the chart:

    Easy way to convert a struct tm (expressed in UTC) to time_t type

    I created a utility function from this to use with QDateTimeAxis series data.

    double GetUTCOffsetForQDateTimeAxis()
    {
      time_t zero = 24 * 60 * 60L;
      struct tm* timeptr;
      int gmtime_hours;
    
      // get the local time for Jan 2, 1900 00:00 UTC
      timeptr = localtime(&zero);
      gmtime_hours = timeptr->tm_hour;
    
      // if the local time is the "day before" the UTC, subtract 24 hours
      // from the hours to get the UTC offset
      if(timeptr->tm_mday < 2)
      {
        gmtime_hours -= 24;
      }
    
      return 24.0 + gmtime_hours;
    }
    

    Then the data conversion was simple.

    std::vector<SimData> data;
    
    // ... Populate data
    
    auto series = new QLineSeries();
    
    const auto utcOffset = sec2ms(hours2sec(GetUTCOffsetForQDateTimeAxis()));
    
    for(auto i : data)
    {
      // Append time in milliseconds and a value
      series->append(i.msTime - utcOffset, i.value);
    }
    
    // ...