我正在使用QtCharts显示模拟数据。模拟从时间为零开始,但我的图表轴似乎总是在19个小时开始。这让我很困惑。图表的设置是笔直的:
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);如果我在没有数据的情况下运行,但只显示图表,就会得到以下结果:

如果我添加数据,从时间0开始,总数据量是正确的,但时间仍然从19:00开始。为什么时间不从00:00开始?

发布于 2017-10-12 13:06:03
这个问题确实被证实是世界协调时的抵消。因此,有一个很好的例子说明了如何获得UTC偏移量,然后我用它来偏移图表中的数据:
我从此创建了一个实用程序函数,用于QDateTimeAxis系列数据。
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;
}然后,数据转换很简单。
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);
}
// ...发布于 2017-10-11 21:55:59
我相信这是因为你在东海岸( UTC-5 ),所以当0代表凌晨12点(2400)时,UTC-5 0ms将代表5小时(前一天的1900)。我也有同样的问题,把我的时区设置为UTC (在ubuntu下),而瞧轴从0小时开始,而不是19小时。
发布于 2018-08-20 13:36:22
对孤独的流浪者来说可能会在这里结束。
我的KISS解决方案实际上是设置时间,然后一些秒,最后添加一个新的数据点:
for(int i = 0; i <= points; i++) {
QDateTime timeStamp;
timeStamp.setDate(QDate(1980, 1, 1));
timeStamp.setTime(QTime(0, 0, 0));
timeStamp = timeStamp.addSecs(i);
data->append(timeStamp.toMSecsSinceEpoch(), /* your y here */);
}稍后,在绘制图表时,我使用了:
QSplineSeries *temps1 = /* wherever you get your series */;
QChart *chTemp = new QChart();
tempAxisX->setTickCount(5);
tempAxisX->setFormat(QString("hh:mm:ss"));
tempAxisX->setTitleText("Time");
chTemp->addAxis(tempAxisX, Qt::AlignBottom);
temps1->attachAxis(tempAxisX);希望这能帮助未来的访客一次(包括我自己)。
https://stackoverflow.com/questions/45326462
复制相似问题