I am trying to generate a sine wave using QChart and display it on the graph. Actually everything works fine. However, when scrolling the data, sometimes it is very fast, sometimes very slow, even at the beginning of the program, it shows correctly and goes off the screen over time. I couldn't figure out the source of the problem any ideas and suggestions would help me a lot.
Having a timer in my algorithm. When the timer is full, the function depending on 1 more of the x value works and the new point is added to the series. After this process, I try to scroll to a certain extent.
My timer function:
void MyChartView::handleTimeout() {
m_x += 1;
m_y = (amplitude * sin(period * (m_x + horizontalShift)) + verticalShift);
m_series->append(m_x, m_y);
qDebug() << "New Data -> m_x: " << m_x << " m_y: " << m_y;
qDebug() << "Plot area width: " << chart->plotArea().width() << " tickCount: " << m_axisX->tickCount();
if (m_x > 10) {
chart->scroll(chart->plotArea().width() / m_x, 0);
}
this->update();
}
Screenshot:
What I want is to scroll continuously with some space at the end of the data.
Thank you.

Check out the Dynamic Spline example - https://doc.qt.io/qt-6/qtcharts-dynamicspline-example.html. It initializes the QChart like the following:
Then it scrolls based on change (using random number generator):
The
m_x()member initializer is what defined how much space to have as data is added. In this instance, there will be a gap of 5 units because the range is 10 units set atm_axisX->setRange(0, 10);. The scroll function essentially scrolls per tick based onplotArea().width() / m_axisX->tickCount();.So to add space to your chart, just initialize
m_xto less than them_axisXrange max.Also, you should be scrolling based on the ticks count not the
m_x.chart->scroll(chart->plotArea().width() / m_axisX->tickCount(), 0);