Skip to content

Commit 13220f1

Browse files
committed
Fix chart shimmering
1 parent 5056847 commit 13220f1

3 files changed

Lines changed: 34 additions & 16 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Statistics menu for monitored interfaces.
4141

4242
The chart scrolls continuously at approximately 30 frames per second, revealing
4343
new segments over one measurement interval. Its vertical scale and maximum-speed
44-
label transition together. This adds about one sampling interval of visual delay
44+
label transition together. Traces use crisp one-pixel strokes and move in whole
45+
device-pixel steps to avoid antialiasing shimmer. This adds about one sampling interval of visual delay
4546
without changing sampled values or the immediate, unsmoothed tray text rates.
4647
Animation stops while the chart is hidden and resumes from the current history
4748
when shown again.

‎src/chart.cpp‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "chart.h"
22
#include <QPainter>
33
#include <QBrush>
4+
#include <QPen>
5+
#include <QPolygonF>
46
#include <QTimer>
57
#include <QShowEvent>
68
#include <QHideEvent>
@@ -36,6 +38,7 @@ void Chart::snapshotHistory() {
3638
mFrameTimer->stop();
3739
mAnimationClock.invalidate();
3840
mScrollStart = 0.0;
41+
mNewestSample = mBufferSize > 0 ? quint64(mBufferSize - 1) : 0;
3942
mUpload.clear();
4043
mDownload.clear();
4144
// Copy the circular history in chronological order, oldest to newest.
@@ -63,6 +66,7 @@ void Chart::sampleUpdated(bool reset) {
6366
mScaleStart = displayedMaximumSpeed();
6467
mScaleTarget = *mMaxSpeed;
6568
mScrollStart = remaining + 1.0;
69+
++mNewestSample;
6670
mUpload.append(mUplBuffer[*mPtr]);
6771
mDownload.append(mDldBuffer[*mPtr]);
6872
const int retained = mBufferSize + int(std::ceil(mScrollStart));
@@ -96,26 +100,38 @@ void Chart::paintEvent(QPaintEvent *event) {
96100
QBrush brush(QColor(0x33, 0x33, 0x33), Qt::BrushStyle::CrossPattern);
97101
paint.fillRect(0, 0, width(), height(), brush);
98102

99-
const double maximum = displayedMaximumSpeed();
103+
const double progress = animationProgress();
104+
const double maximum = mScaleStart + (mScaleTarget - mScaleStart) * progress;
100105
if (mBufferSize < 2 || mUpload.size() < 2 || width() <= 1 || height() <= 1 || maximum <= 0.0)
101106
return;
102107

103108
const double right = width() - 1;
104109
const double chartHeight = height() - 1;
105110
const double spacing = right / (mBufferSize - 1);
106-
const double remaining = mScrollStart * (1.0 - animationProgress());
107-
paint.setClipRect(rect());
108-
paint.setRenderHint(QPainter::Antialiasing);
109-
for (int i = 1; i < mUpload.size(); ++i) {
110-
const double x = right + spacing * (remaining - (mUpload.size() - 1 - i));
111-
const double previousX = x - spacing;
112-
if (x < 0.0 || previousX > right)
113-
continue;
114-
paint.setPen(QColor(mInterfaceOptions->mChartDldColor));
115-
paint.drawLine(QPointF(previousX, chartHeight * (1.0 - mDownload[i - 1] / maximum)),
116-
QPointF(x, chartHeight * (1.0 - mDownload[i] / maximum)));
117-
paint.setPen(QColor(mInterfaceOptions->mChartUplColor));
118-
paint.drawLine(QPointF(previousX, chartHeight * (1.0 - mUpload[i - 1] / maximum)),
119-
QPointF(x, chartHeight * (1.0 - mUpload[i] / maximum)));
111+
const double remaining = mScrollStart * (1.0 - progress);
112+
const double ratio = devicePixelRatioF();
113+
// Give each sample a stable pixel position. Scroll the whole trace by an
114+
// integer number of device pixels, instead of changing every segment's
115+
// subpixel coverage on every frame. This also works with fractional DPI.
116+
const double origin = std::round((double(mNewestSample) - remaining) * spacing * ratio);
117+
QPolygonF upload, download;
118+
upload.reserve(mUpload.size());
119+
download.reserve(mDownload.size());
120+
for (int i = 0; i < mUpload.size(); ++i) {
121+
const double sample = double(mNewestSample) - (mUpload.size() - 1 - i);
122+
const double x = std::round(right * ratio) + std::round(sample * spacing * ratio) - origin;
123+
upload.append(QPointF(x, std::round(chartHeight * (1.0 - mUpload[i] / maximum) * ratio)));
124+
download.append(QPointF(x, std::round(chartHeight * (1.0 - mDownload[i] / maximum) * ratio)));
120125
}
126+
paint.setClipRect(rect());
127+
paint.setRenderHint(QPainter::Antialiasing, false);
128+
paint.scale(1.0 / ratio, 1.0 / ratio);
129+
QPen pen(QColor(mInterfaceOptions->mChartDldColor));
130+
pen.setWidth(1);
131+
pen.setCosmetic(true);
132+
paint.setPen(pen);
133+
paint.drawPolyline(download);
134+
pen.setColor(QColor(mInterfaceOptions->mChartUplColor));
135+
paint.setPen(pen);
136+
paint.drawPolyline(upload);
121137
}

‎src/chart.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Q_OBJECT
3434
const double *mMaxSpeed;
3535
const int *mPtr;
3636
int mBufferSize;
37+
quint64 mNewestSample = 0;
3738
QVector<double> mUpload, mDownload;
3839
QTimer *mFrameTimer;
3940
QElapsedTimer mAnimationClock;

0 commit comments

Comments
 (0)