Qt - Scrolling the visible area of the chart

Viewed 3635

I'm beginner at Qt, and currently learning from Mastering Qt5 book and I got stuck. There is a piece of code

// Global variables
const int CHART_X_RANGE_COUNT = 50;
const int CHART_X_RANGE_MAX = CHART_X_RANGE_COUNT - 1;

void MemoryWidget::updateSeries()
{
  double memoryUsed = SysInfo::instance().memoryUsed(); // retrieve the latest memory percentage used
  mSeries->append(mPointPositionX++, memoryUsed);
  if (mSeries->count() > CHART_X_RANGE_COUNT)
  {
    QChart *chart = chartView().chart();
    chart->scroll(chart->plotArea().width() / CHART_X_RANGE_MAX, 0);
    mSeries->remove(0);
  }
}

I only don't understand the following piece of code

chart->scroll(chart->plotArea().width() / CHART_X_RANGE_MAX, 0);

If I want to scroll to the latest point on the X axis and nothing on Y, then why i have to do this:

chart->plotArea().width() / CHART_X_RANGE_MAX

and can't do just this:

chart->scroll(1, 0);
1 Answers

According to docs scroll Scrolls the visible area of the chart by the distance specified by dx and dy.

and why chart->scroll(chart->plotArea().width() / CHART_X_RANGE_MAX, 0);

because mSeries->append(mPointPositionX++, memoryUsed); is called periodically and after a while a full window of xaxis will be filled and after each full window fills plot will scroll, if you use chart->scroll(1, 0); each time updateSeries() called you scroll window to next and always showing last point,

In general this plot wants to scroll per CHART_X_RANGE_MAX.

Related