From 8864a1f790e6c3ba0abe28933ad2cb99ede5a423 Mon Sep 17 00:00:00 2001 From: R E Broadley Date: Thu, 2 Dec 2021 13:27:34 +0100 Subject: [PATCH 1/2] Network Graph - Create y_value function --- src/qt/trafficgraphwidget.cpp | 16 +++++++++++----- src/qt/trafficgraphwidget.h | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/qt/trafficgraphwidget.cpp b/src/qt/trafficgraphwidget.cpp index 7e12410c801..a5624c0b093 100644 --- a/src/qt/trafficgraphwidget.cpp +++ b/src/qt/trafficgraphwidget.cpp @@ -47,16 +47,22 @@ int TrafficGraphWidget::getGraphRangeMins() const return nMins; } +int TrafficGraphWidget::y_value(float value) +{ + int h = height() - YMARGIN * 2; + return YMARGIN + h - (h * 1.0 * value / fMax); +} + void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue &samples) { int sampleCount = samples.size(); - if(sampleCount > 0) { + if(sampleCount > 0 && fMax > 0) { int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2; int x = XMARGIN + w; path.moveTo(x, YMARGIN + h); for(int i = 0; i < sampleCount; ++i) { x = XMARGIN + w - w * i / DESIRED_SAMPLES; - int y = YMARGIN + h - (int)(h * samples.at(i) / fMax); + int y = y_value(samples.at(i)); path.lineTo(x, y); } path.lineTo(x, YMARGIN + h); @@ -84,7 +90,7 @@ void TrafficGraphWidget::paintEvent(QPaintEvent *) // draw lines painter.setPen(axisCol); - painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units)); + painter.drawText(XMARGIN, y_value(val)-yMarginText, QString("%1 %2").arg(val).arg(units)); for(float y = val; y < fMax; y += val) { int yy = YMARGIN + h - h * y / fMax; painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); @@ -94,13 +100,13 @@ void TrafficGraphWidget::paintEvent(QPaintEvent *) axisCol = axisCol.darker(); val = pow(10.0f, base - 1); painter.setPen(axisCol); - painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units)); + painter.drawText(XMARGIN, y_value(val)-yMarginText, QString("%1 %2").arg(val).arg(units)); int count = 1; for(float y = val; y < fMax; y += val, count++) { // don't overwrite lines drawn above if(count % 10 == 0) continue; - int yy = YMARGIN + h - h * y / fMax; + int yy = y_value(y); painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); } } diff --git a/src/qt/trafficgraphwidget.h b/src/qt/trafficgraphwidget.h index 2d8c825815a..fddebbc90a1 100644 --- a/src/qt/trafficgraphwidget.h +++ b/src/qt/trafficgraphwidget.h @@ -26,6 +26,7 @@ class TrafficGraphWidget : public QWidget protected: void paintEvent(QPaintEvent *) override; + int y_value(float value); public Q_SLOTS: void updateRates(); From ad431ff5d18d1f76b43aef98d9e67019bb9824f2 Mon Sep 17 00:00:00 2001 From: R E Broadley Date: Mon, 15 Nov 2021 15:22:05 +0100 Subject: [PATCH 2/2] Enable non-linear network traffic graph --- src/qt/trafficgraphwidget.cpp | 40 +++++++++++++++++++++++------------ src/qt/trafficgraphwidget.h | 2 ++ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/qt/trafficgraphwidget.cpp b/src/qt/trafficgraphwidget.cpp index a5624c0b093..acad6142e71 100644 --- a/src/qt/trafficgraphwidget.cpp +++ b/src/qt/trafficgraphwidget.cpp @@ -50,7 +50,7 @@ int TrafficGraphWidget::getGraphRangeMins() const int TrafficGraphWidget::y_value(float value) { int h = height() - YMARGIN * 2; - return YMARGIN + h - (h * 1.0 * value / fMax); + return YMARGIN + h - (h * 1.0 * (fToggle ? (pow(value, 0.30102) / pow(fMax, 0.30102)) : (value / fMax))); } void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue &samples) @@ -69,6 +69,13 @@ void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue &samples) } } +void TrafficGraphWidget::mousePressEvent(QMouseEvent *event) +{ + QWidget::mousePressEvent(event); + fToggle = !fToggle; + update(); +} + void TrafficGraphWidget::paintEvent(QPaintEvent *) { QPainter painter(this); @@ -88,28 +95,33 @@ void TrafficGraphWidget::paintEvent(QPaintEvent *) const QString units = tr("kB/s"); const float yMarginText = 2.0; - // draw lines - painter.setPen(axisCol); - painter.drawText(XMARGIN, y_value(val)-yMarginText, QString("%1 %2").arg(val).arg(units)); - for(float y = val; y < fMax; y += val) { - int yy = YMARGIN + h - h * y / fMax; - painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); - } - // if we drew 3 or fewer lines, break them up at the next lower order of magnitude - if(fMax / val <= 3.0f) { - axisCol = axisCol.darker(); + // if we drew 10 or 3 fewer lines, break them up at the next lower order of magnitude + if(fMax / val <= (fToggle ? 10.0f : 3.0f)) { + float oldval = val; val = pow(10.0f, base - 1); - painter.setPen(axisCol); + painter.setPen(axisCol.darker()); painter.drawText(XMARGIN, y_value(val)-yMarginText, QString("%1 %2").arg(val).arg(units)); + if (fToggle) { + int yy = y_value(val*0.1); + painter.drawText(XMARGIN, yy-yMarginText, QString("%1 %2").arg(val*0.1).arg(units)); + painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); + } int count = 1; - for(float y = val; y < fMax; y += val, count++) { - // don't overwrite lines drawn above + for(float y = val; y < (!fToggle || fMax / val < 20 ? fMax : oldval); y += val, count++) { if(count % 10 == 0) continue; int yy = y_value(y); painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); } + val = oldval; } + // draw lines + painter.setPen(axisCol); + for(float y = val; y < fMax; y += val) { + int yy = y_value(y); + painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); + } + painter.drawText(XMARGIN, y_value(val)-yMarginText, QString("%1 %2").arg(val).arg(units)); painter.setRenderHint(QPainter::Antialiasing); if(!vSamplesIn.empty()) { diff --git a/src/qt/trafficgraphwidget.h b/src/qt/trafficgraphwidget.h index fddebbc90a1..5fda4be73c1 100644 --- a/src/qt/trafficgraphwidget.h +++ b/src/qt/trafficgraphwidget.h @@ -27,6 +27,8 @@ class TrafficGraphWidget : public QWidget protected: void paintEvent(QPaintEvent *) override; int y_value(float value); + void mousePressEvent(QMouseEvent *event) override; + bool fToggle = true; public Q_SLOTS: void updateRates();