QDateTimeAxis starts in default date time (01-01-1970 01:00)
-
Hello, I'm trying to plot a QChart with date-time horizontal axis and value vertical axis. I'm having trouble with the horizontal axis as I can't make it start with the current time. Here is my code:
Chart::Chart(): QWidget(), axisX(new QDateTimeAxis), axisY(new QValueAxis), chart(new QChart), chartView(new QChartView) { chart->addSeries(lineSeries); chart->legend()->hide(); //format the time axis axisX->setFormat("dd.MM.yyyy HH:mm"); axisX->setTitleText("Time"); axisX->setLabelsAngle(-90); QDateTime timeValue(QDate::currentDate(), QTime::currentTime()); axisX->setRange(timeValue, timeValue); chart->addAxis(axisX, Qt::AlignBottom); lineSeries->attachAxis(axisX); qDebug() << timeValue; //format the temperature axis axisY->setLabelFormat("%i"); axisY->setTitleText("Temperature (°C)"); axisY->setTickInterval(5); axisY->setRange(0, 40); chart->addAxis(axisY, Qt::AlignLeft); lineSeries->attachAxis(axisY); //place the chart in the window QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(chartView, 0); resize(500, 500); }
This shows a chart where the range is the default date time 01-01-1970 01:00, even thought
qDebug()
tells me thattimeValue
is set to the current time. How can I solve this?One possible solution is in the function that adds a real value to the series (with
lineSeries.append(...)
) and updates the max value of the horizontal axis.void Chart::addValueToChart(qreal newTempValue) { QDateTime current(QDate::currentDate(), QTime::currentTime()); lineSeries->append(current.toMSecsSinceEpoch(), newTempValue); axisX->setMax(current); }
I could be able to update the min value of this axis in this function too, but I think that updating it every single time would be a waste of time. The ideal would be to set the min date-time value one time. Would that be possible?
-
Hi,
Just to check, what happens if you configure your axis after you set it on the chart ?