Skip to content

Commit 7cc05cb

Browse files
committed
Make traffic chart lines appear rounded
Rounds the bends with curves that pass through the samples without overshooting. Keeps whole-pixel scrolling.
1 parent 13220f1 commit 7cc05cb

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ Statistics menu for monitored interfaces.
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
4444
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
45+
device-pixel steps to avoid antialiasing shimmer. Bounded curves round the bends
46+
while passing through the samples without overshooting their values. This adds about one sampling interval of visual delay
4647
without changing sampled values or the immediate, unsmoothed tray text rates.
4748
Animation stops while the chart is hidden and resumes from the current history
4849
when shown again.

‎src/chart.cpp‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,52 @@
33
#include <QBrush>
44
#include <QPen>
55
#include <QPolygonF>
6+
#include <QPainterPath>
67
#include <QTimer>
78
#include <QShowEvent>
89
#include <QHideEvent>
910
#include <algorithm>
1011
#include <cmath>
1112

13+
namespace {
14+
QPainterPath roundedTrace(const QPolygonF &points) {
15+
QPainterPath path;
16+
if (points.isEmpty())
17+
return path;
18+
path.moveTo(points.first());
19+
if (points.size() < 2)
20+
return path;
21+
22+
QVector<double> slopes(points.size() - 1);
23+
for (int i = 0; i < slopes.size(); ++i) {
24+
const double width = points[i + 1].x() - points[i].x();
25+
slopes[i] = width > 0.0 ? (points[i + 1].y() - points[i].y()) / width : 0.0;
26+
}
27+
QVector<double> tangents(points.size(), 0.0);
28+
tangents.first() = slopes.first();
29+
tangents.last() = slopes.last();
30+
for (int i = 1; i < points.size() - 1; ++i) {
31+
const double before = slopes[i - 1], after = slopes[i];
32+
// Flatten peaks and troughs. Else limit the shared tangent to both
33+
// adjacent slopes, keeping each curve inside its measured endpoints.
34+
if ((before > 0.0 && after > 0.0) || (before < 0.0 && after < 0.0))
35+
tangents[i] = std::copysign(std::min(std::abs(before), std::abs(after)), before);
36+
}
37+
for (int i = 0; i < points.size() - 1; ++i) {
38+
const QPointF &start = points[i], &end = points[i + 1];
39+
const double third = (end.x() - start.x()) / 3.0;
40+
if (third <= 0.0) {
41+
// Very narrow charts can put adjacent samples on the same pixel.
42+
path.lineTo(end);
43+
continue;
44+
}
45+
path.cubicTo(QPointF(start.x() + third, start.y() + tangents[i] * third),
46+
QPointF(end.x() - third, end.y() - tangents[i + 1] * third), end);
47+
}
48+
return path;
49+
}
50+
}
51+
1252
Chart::Chart(const ViewOptions *interfaceOptions, const double *uploadBuffer, const double *downloadBuffer,
1353
const double *maxspeed, const int *ptr, int bufferSize)
1454
: QWidget(), mInterfaceOptions(interfaceOptions), mUplBuffer(uploadBuffer),
@@ -130,8 +170,8 @@ void Chart::paintEvent(QPaintEvent *event) {
130170
pen.setWidth(1);
131171
pen.setCosmetic(true);
132172
paint.setPen(pen);
133-
paint.drawPolyline(download);
173+
paint.drawPath(roundedTrace(download));
134174
pen.setColor(QColor(mInterfaceOptions->mChartUplColor));
135175
paint.setPen(pen);
136-
paint.drawPolyline(upload);
176+
paint.drawPath(roundedTrace(upload));
137177
}

0 commit comments

Comments
 (0)