Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically rescale axis in QtCharts?

Tags:

c++

qt

qtcharts

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.

QtChart axis wrong scaled

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 &registers)
{
    auto result = ((&registers)->*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.

like image 420
feedc0de Avatar asked Sep 09 '16 10:09

feedc0de


1 Answers

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.

like image 195
Dinendal Avatar answered Nov 10 '22 03:11

Dinendal