Skip to content

Commit 6f741d3

Browse files
committed
Enable non-linear network traffic graph
1 parent 7f0f853 commit 6f741d3

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

src/qt/trafficgraphwidget.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,27 @@ int TrafficGraphWidget::getGraphRangeMins() const
5050
void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue<float> &samples)
5151
{
5252
int sampleCount = samples.size();
53-
if(sampleCount > 0) {
53+
if(sampleCount > 0 && fMax > 0) {
5454
int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
5555
int x = XMARGIN + w;
5656
path.moveTo(x, YMARGIN + h);
5757
for(int i = 0; i < sampleCount; ++i) {
5858
x = XMARGIN + w - w * i / DESIRED_SAMPLES;
59-
int y = YMARGIN + h - (int)(h * samples.at(i) / fMax);
59+
int y = YMARGIN + h - (int)(h * 1.0 * (fToggle ? (pow(samples.at(i), 0.3) / pow(fMax, 0.3)) : (samples.at(i) / fMax)));
6060
path.lineTo(x, y);
6161
}
6262
path.lineTo(x, YMARGIN + h);
6363
}
6464
}
6565

66+
void TrafficGraphWidget::mousePressEvent(QMouseEvent *event)
67+
{
68+
QWidget::mousePressEvent(event);
69+
fToggle = !fToggle;
70+
QPaintEvent* erm = nullptr;
71+
paintEvent(erm);
72+
}
73+
6674
void TrafficGraphWidget::paintEvent(QPaintEvent *)
6775
{
6876
QPainter painter(this);
@@ -82,28 +90,28 @@ void TrafficGraphWidget::paintEvent(QPaintEvent *)
8290
const QString units = tr("kB/s");
8391
const float yMarginText = 2.0;
8492

85-
// draw lines
86-
painter.setPen(axisCol);
87-
painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
88-
for(float y = val; y < fMax; y += val) {
89-
int yy = YMARGIN + h - h * y / fMax;
90-
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
91-
}
92-
// if we drew 3 or fewer lines, break them up at the next lower order of magnitude
93-
if(fMax / val <= 3.0f) {
94-
axisCol = axisCol.darker();
93+
// if we drew 10 or 3 fewer lines, break them up at the next lower order of magnitude
94+
if(fMax / val <= (fToggle ? 10.0f : 3.0f)) {
95+
float oldval = val;
9596
val = pow(10.0f, base - 1);
96-
painter.setPen(axisCol);
97-
painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
97+
painter.setPen(axisCol.darker());
98+
painter.drawText(XMARGIN, YMARGIN + h - (h * 1.0 * (fToggle ? (pow(val, 0.3) / pow(fMax, 0.3)) : (val / fMax)))-yMarginText, QString("%1 %2").arg(val).arg(units));
9899
int count = 1;
99-
for(float y = val; y < fMax; y += val, count++) {
100-
// don't overwrite lines drawn above
100+
for(float y = val; y < (fToggle ? oldval : fMax); y += val, count++) {
101101
if(count % 10 == 0)
102102
continue;
103-
int yy = YMARGIN + h - h * y / fMax;
103+
int yy = YMARGIN + h - (h * 1.0 * (fToggle ? (pow(y, 0.3) / pow(fMax, 0.3)) : (y / fMax)));
104104
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
105105
}
106+
val = oldval;
107+
}
108+
// draw lines
109+
painter.setPen(axisCol);
110+
for(float y = val; y < fMax; y += val) {
111+
int yy = YMARGIN + h - (h * 1.0 * (fToggle ? (pow(y, 0.3) / pow(fMax, 0.3)) : (y / fMax)));
112+
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
106113
}
114+
painter.drawText(XMARGIN, YMARGIN + h - (h * 1.0 * (fToggle ? (pow(val, 0.3) / pow(fMax, 0.3)) : (val / fMax)))-yMarginText, QString("%1 %2").arg(val).arg(units));
107115

108116
painter.setRenderHint(QPainter::Antialiasing);
109117
if(!vSamplesIn.empty()) {

src/qt/trafficgraphwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class TrafficGraphWidget : public QWidget
2626

2727
protected:
2828
void paintEvent(QPaintEvent *) override;
29+
void mousePressEvent(QMouseEvent *event) override;
30+
bool fToggle = true;
2931

3032
public Q_SLOTS:
3133
void updateRates();

0 commit comments

Comments
 (0)