I am using QtCharts.
I need both axes to be rescaled after appending values. The values I appended are not between 0 and 1 and also not from the year 1970.
Constructor code of my dialog looks like this:
m_series = new QLineSeries;
m_series->setName(name);
auto chart = new QChart;
chart->legend()->setVisible(false);
chart->addSeries(m_series);
m_axisX = new QDateTimeAxis;
//m_axisX->setFormat("HH:mm:ss");
m_axisX->setTitleText(tr("Zeitpunkt"));
chart->addAxis(m_axisX, Qt::AlignBottom);
m_series->attachAxis(m_axisX);
auto axisY = new QValueAxis;
axisY->setTitleText(unit);
chart->addAxis(axisY, Qt::AlignLeft);
m_series->attachAxis(axisY);
auto chartView = new QChartView(chart, this);
chartView->setRenderHint(QPainter::Antialiasing);
My MainWindow emits signals containg new values. Multiple opened chart dialogs are connected to that signal.
void ChartDialog::liveUpdate(const RealTimeMeasureRegisters ®isters)
{
auto result = ((®isters)->*m_methodPtr)();
m_series->append(registers.timestamp(), result);
}
Is there some easy way to tell QDateTimeAxis (in my case m_axisX
) to automatically adjust to the new values?
QDateTimeAxis::setRange() does not look good, because I need to set a minimum and maximum.
I don't know about a way to adjust automatically but you could look at this example which add points to the chart dynamically : http://doc.qt.io/qt-5/qtcharts-dynamicspline-example.html
Although in this example the range has been limited when adding points so to go further it would require to re-adjust the range when the next point is out of it.
As you say the setRange requires a min and a max but you could update it in your liveUpdate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With