How to automatically scale an axis in QtCharts?

I am using QtCharts .

I need both axes to be rescaled after adding the values. The values ​​I added are not between 0 and 1, and also not since 1970.

QtChart axis scaling incorrect

The constructor code for my dialog is as follows:

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 containing new values. Several open chart dialogs are connected to this signal.

 void ChartDialog::liveUpdate(const RealTimeMeasureRegisters &registers) { auto result = ((&registers)->*m_methodPtr)(); m_series->append(registers.timestamp(), result); } 

Is there any easy way to tell QDateTimeAxis (in my case m_axisX ) to automatically adapt to the new values?

QDateTimeAxis :: setRange () does not look very good, because I need to set the minimum and maximum.

+5
source share
1 answer

I don’t know about the auto-tuning method, but you can look at this example, which dynamically adds points to the diagram: http://doc.qt.io/qt-5/qtcharts-dynamicspline-example.html

Although in this example, the range was limited when adding points, so to go further, you will need to change the range setting when the next point is outside of it.

As you say, setRange requires min and max, but you can update it in your liveUpdate.

0
source

All Articles