Skip to content

Commit 6eb0762

Browse files
committed
added pie vizualization as an option to BW graph stats
1 parent 918cdfb commit 6eb0762

8 files changed

Lines changed: 176 additions & 64 deletions

File tree

retroshare-gui/src/gui/common/RSGraphWidget.cpp

Lines changed: 93 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
#include <QWheelEvent>
3333
#include <QTimer>
3434

35+
#include <QChartView>
36+
#include <QChart>
37+
#include <QPieSeries>
38+
#include <QPieSlice>
39+
#include <QValueAxis>
40+
3541
#include <retroshare-gui/RsAutoUpdatePage.h>
3642
#include "rshare.h"
3743
#include "RSGraphWidget.h"
@@ -259,32 +265,38 @@ void RSGraphWidget::setTimeScale(float pixels_per_second)
259265
_time_scale =pixels_per_second ;
260266
}
261267

268+
void RSGraphWidget::setViewMode(ViewMode m)
269+
{
270+
_viewMode = m;
271+
}
262272
/** Default contructor */
263273
RSGraphWidget::RSGraphWidget(QWidget *parent)
264274
: QFrame(parent)
265275
{
276+
_viewMode = ViewMode::History;
277+
_mousePressed = false;
266278
_source =NULL;
267-
_painter = new QPainter();
279+
_painter = new QPainter();
268280

269-
/* Initialize graph values */
270-
_maxPoints = getNumPoints();
271-
_maxValue = MINUSER_SCALE;
281+
/* Initialize graph values */
282+
_maxPoints = getNumPoints();
283+
_maxValue = MINUSER_SCALE;
272284

273-
_linewidthscale = 1.0f;
274-
_opacity = 0.6 ;
275-
_flags = 0;
276-
_time_scale = 5.0f ; // in pixels per second.
277-
_time_filter = 1.0f ;
278-
_timer = new QTimer ;
279-
QObject::connect(_timer,SIGNAL(timeout()),this,SLOT(updateIfPossible())) ;
285+
_linewidthscale = 1.0f;
286+
_opacity = 0.6 ;
287+
_flags = 0;
288+
_time_scale = 5.0f ; // in pixels per second.
289+
_time_filter = 1.0f ;
290+
_timer = new QTimer ;
291+
QObject::connect(_timer,SIGNAL(timeout()),this,SLOT(updateIfPossible())) ;
280292

281293

282-
_y_scale = 1.0f ;
283-
_timer->start(1000);
294+
_y_scale = 1.0f ;
295+
_timer->start(1000);
284296

285297
float FS = QFontMetricsF(font()).height();
286-
setMinimumHeight(12*FS);
287-
_graph_base = FS*GRAPH_BASE;
298+
setMinimumHeight(12*FS);
299+
_graph_base = FS*GRAPH_BASE;
288300
}
289301

290302
void RSGraphWidget::updateIfPossible()
@@ -353,14 +365,16 @@ void RSGraphWidget::paintEvent(QPaintEvent *)
353365
}
354366
_painter->drawRect(_rec);
355367

356-
/* Paint the scale */
357-
paintScale1();
358-
359-
/* Plot the data */
360-
paintData();
368+
if(_viewMode == ViewMode::History)
369+
{
370+
/* Paint the scale */
371+
paintScale1();
361372

362-
/* Paint the totals */
363-
paintTotals();
373+
/* Plot the data */
374+
paintData();
375+
}
376+
else if(_viewMode == ViewMode::Slice)
377+
paintTotals();
364378

365379
// part of the scale that needs to write over the data curves.
366380
paintScale2();
@@ -592,16 +606,54 @@ void RSGraphWidget::paintDots(const QVector<QPointF>& points, QColor color)
592606
/** Paints selected total indicators on the graph. */
593607
void RSGraphWidget::paintTotals()
594608
{
609+
#ifdef UNUSED
595610
float FS = QFontMetricsF(font()).height();
596611
//float fact = FS/14.0 ;
597-
598612
//int x = SCALE_WIDTH*fact + FS, y = 0;
599613
int rowHeight = FS;
600614

601615
#if !defined(Q_OS_MAC)
602616
/* On Mac, we don't need vertical spacing between the text rows. */
603617
rowHeight += 5;
604618
#endif
619+
#endif
620+
621+
auto c = new QtCharts::QChart();
622+
c->legend()->setVisible(true);
623+
c->legend()->setAlignment(Qt::AlignRight);
624+
625+
auto axisY = new QtCharts::QValueAxis();
626+
axisY->setTitleText("MB");
627+
axisY->setLabelFormat("%.1f");
628+
c->addAxis(axisY, Qt::AlignLeft);
629+
630+
std::vector<float> vals,tmp_vals;
631+
_source->getCumulatedValues(tmp_vals);
632+
633+
for(int i=0;i<_source->n_values();++i)
634+
if( _masked_entries.find(_source->displayName(i).toStdString()) == _masked_entries.end() )
635+
vals.push_back(tmp_vals[i]);
636+
637+
float total = 0.0;
638+
for(auto v:vals) total += v;
639+
640+
if(total != 0.0)
641+
for(auto& v:vals) v/=total;
642+
643+
auto pieSeries = new QtCharts::QPieSeries();
644+
for(uint i=0;i<vals.size();++i)
645+
{
646+
QtCharts::QPieSlice *slice = pieSeries->append(_source->legend(i,vals[i],false),vals[i]);
647+
slice->setLabelVisible(true);
648+
}
649+
650+
c->resize(_rec.width(),_rec.height());
651+
c->addSeries(pieSeries);
652+
653+
QGraphicsScene scene;
654+
scene.addItem(c);
655+
scene.setSceneRect(0, 0, _rec.width(), _rec.height());
656+
scene.render(_painter, _rec, _rec);
605657
}
606658

607659
/** Returns a formatted string with the correct size suffix. */
@@ -773,3 +825,21 @@ void RSGraphWidget::paintLegend()
773825
}
774826
}
775827

828+
// These functions capture the cursor
829+
void RSGraphWidget::mousePressEvent(QMouseEvent *e)
830+
{
831+
_mousePressed = true;
832+
QFrame::mousePressEvent(e);
833+
}
834+
void RSGraphWidget::mouseMoveEvent(QMouseEvent *e)
835+
{
836+
if(_mousePressed)
837+
std::cerr << "e->x() = " << e->x() << std::endl;
838+
QFrame::mouseMoveEvent(e);
839+
}
840+
void RSGraphWidget::mouseReleaseEvent(QMouseEvent *e)
841+
{
842+
_mousePressed = false;
843+
QFrame::mouseReleaseEvent(e);
844+
}
845+

retroshare-gui/src/gui/common/RSGraphWidget.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class RSGraphSource: public QObject
7373

7474
virtual QString unitName() const { return "" ; }// overload to give your own unit name (KB/s, Users, etc)
7575

76+
// Number of curves available
7677
int n_values() const ;
7778

7879
// Might be overloaded in order to show a fancy digit number with adaptive units.
@@ -155,6 +156,11 @@ class RSGraphWidget: public QFrame
155156
SolidLine = 0, /**< Plot bandwidth as solid lines. */
156157
AreaGraph = 1 /**< Plot bandwidth as alpha blended area graphs. */
157158
};
159+
enum ViewMode
160+
{
161+
History = 0x00, /**< Plots the full curves with time . */
162+
Slice = 0x01, /**< Plots a slice of the curves at some point in time. */
163+
};
158164

159165
/** Default Constructor */
160166
RSGraphWidget(QWidget *parent = 0);
@@ -166,6 +172,8 @@ class RSGraphWidget: public QFrame
166172
void setTimerPeriod(int miliseconds) ;
167173
void setSource(RSGraphSource *gs) ;
168174
void setTimeScale(float pixels_per_second) ;
175+
void setViewMode(ViewMode m) ;
176+
void setSliceProportion(float f) ; // float between 0 and 1. 0 means newest 1 means oldest.
169177

170178
/** Add data points. */
171179
//void addPoints(qreal rsDHT, qreal allDHT);
@@ -184,15 +192,21 @@ class RSGraphWidget: public QFrame
184192
void resetFlags(uint32_t flag) { _flags &= ~flag ; }
185193
protected:
186194
/** Overloaded QWidget::paintEvent() */
187-
void paintEvent(QPaintEvent *event);
195+
void paintEvent(QPaintEvent *event) override;
188196

189197
//QSize QFrame::sizeHint() const;
190198
// virtual QSizeF sizeHint( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
191199

192200
protected slots:
193201
void updateIfPossible() ;
194202

195-
virtual void wheelEvent(QWheelEvent *e);
203+
virtual void wheelEvent(QWheelEvent *e) override;
204+
205+
protected:
206+
virtual void mousePressEvent(QMouseEvent *e) override;
207+
virtual void mouseMoveEvent(QMouseEvent *e) override;
208+
virtual void mouseReleaseEvent(QMouseEvent *e) override;
209+
196210
private:
197211
/** Gets the width of the desktop, the max # of points. */
198212
int getNumPoints();
@@ -254,5 +268,8 @@ protected slots:
254268
QTimer *_timer ;
255269

256270
RSGraphSource *_source ;
271+
272+
bool _mousePressed;
273+
ViewMode _viewMode;
257274
};
258275

retroshare-gui/src/gui/statistics/BWGraph.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -564,17 +564,17 @@ QString BWGraphSource::niceNumber(float v) const
564564
return QString::number(v/(1024*1024.0*1024),'f',2) + " GB";
565565
}
566566

567-
void BWGraphSource::setSelector(int selector_type,int graph_type,const std::string& selector_client_string)
567+
void BWGraphSource::setSelector(int selector_type,int selector_value,const std::string& selector_client_string)
568568
{
569569
#ifdef BWGRAPH_DEBUG
570-
std::cerr << "Setting Graph Source selector to " << selector_type << " - " << graph_type << " - " << selector_client_string << std::endl;
570+
std::cerr << "Setting Graph Source selector to " << selector_type << " - " << selector_value << " - " << selector_client_string << std::endl;
571571
#endif
572572

573573
bool changed = false ;
574574

575-
if(selector_type == SELECTOR_TYPE_FRIEND && (_friend_graph_type != graph_type || (graph_type == GRAPH_TYPE_SINGLE && selector_client_string != _current_selected_friend.toStdString())))
575+
if(selector_type == SELECTOR_TYPE_FRIEND && (_friend_graph_type != selector_value || (selector_value == GRAPH_TYPE_SINGLE && selector_client_string != _current_selected_friend.toStdString())))
576576
{
577-
if(graph_type == GRAPH_TYPE_SINGLE)
577+
if(selector_value == GRAPH_TYPE_SINGLE)
578578
{
579579

580580
RsPeerId ns(selector_client_string) ;
@@ -583,7 +583,7 @@ void BWGraphSource::setSelector(int selector_type,int graph_type,const std::stri
583583
{
584584
_current_selected_friend = ns ;
585585
changed = true ;
586-
_friend_graph_type = graph_type ;
586+
_friend_graph_type = selector_value ;
587587
}
588588

589589
else
@@ -592,13 +592,13 @@ void BWGraphSource::setSelector(int selector_type,int graph_type,const std::stri
592592
else
593593
{
594594
changed = true ;
595-
_friend_graph_type = graph_type ;
595+
_friend_graph_type = selector_value ;
596596
}
597597
}
598598
else if(selector_type == SELECTOR_TYPE_SERVICE
599-
&& (_service_graph_type != graph_type || (graph_type == GRAPH_TYPE_SINGLE && selector_client_string != QString::number(_current_selected_service,16).toStdString())))
599+
&& (_service_graph_type != selector_value || (selector_value == GRAPH_TYPE_SINGLE && selector_client_string != QString::number(_current_selected_service,16).toStdString())))
600600
{
601-
if(graph_type == GRAPH_TYPE_SINGLE)
601+
if(selector_value == GRAPH_TYPE_SINGLE)
602602
{
603603
//bool ok = false ;
604604
int tmp = QString::fromStdString(selector_client_string).toInt() ;
@@ -608,7 +608,7 @@ void BWGraphSource::setSelector(int selector_type,int graph_type,const std::stri
608608
_current_selected_service = tmp ;
609609

610610
changed = true ;
611-
_service_graph_type = graph_type ;
611+
_service_graph_type = selector_value ;
612612
}
613613
else
614614
std::cerr << "(EE) Cannot set current service to " << selector_client_string << ": unrecognized service string." << std::endl;
@@ -617,7 +617,7 @@ void BWGraphSource::setSelector(int selector_type,int graph_type,const std::stri
617617
else
618618
{
619619
changed = true ;
620-
_service_graph_type = graph_type ;
620+
_service_graph_type = selector_value ;
621621
}
622622
}
623623

retroshare-gui/src/gui/statistics/BWGraph.h

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ class BWGraphSource: public RSGraphSource
6767
BWGraphSource() ;
6868
virtual ~BWGraphSource() {}
6969

70-
enum { SELECTOR_TYPE_FRIEND=0x00, SELECTOR_TYPE_SERVICE=0x01 };
71-
enum { GRAPH_TYPE_SINGLE=0x00, GRAPH_TYPE_ALL=0x01, GRAPH_TYPE_SUM=0x02 };
72-
enum { UNIT_KILOBYTES=0x00, UNIT_COUNT=0x01 };
73-
enum { DIRECTION_DOWN=0x01,DIRECTION_UP=0x02 }; // can be combined using binary ops
74-
enum { TIMING_INSTANT = 0x01,TIMING_CUMULATED=0x02 };
70+
enum { GRAPH_TYPE_SINGLE =0x00 ,GRAPH_TYPE_ALL =0x01 ,GRAPH_TYPE_SUM =0x02 };
71+
enum { SELECTOR_TYPE_FRIEND =0x00 ,SELECTOR_TYPE_SERVICE=0x01 };
72+
enum { UNIT_KILOBYTES =0x00 ,UNIT_COUNT =0x01 };
73+
enum { DIRECTION_DOWN =0x01 ,DIRECTION_UP =0x02 }; // can be combined using binary ops
74+
enum { TIMING_INSTANT =0x00 ,TIMING_CUMULATED =0x01 };
7575

7676
// re-derived from RSGraphSource
7777

78-
virtual void getCumulatedValues(std::vector<float>& vals) const;
79-
virtual void getValues(std::map<std::string,float>& values) const;
80-
virtual QString displayValue(float v) const;
81-
virtual QString legend(int i,float v,bool show_value=true) const;
82-
virtual void update();
83-
QString unitName() const ;
78+
virtual void getCumulatedValues(std::vector<float>& vals) const override;
79+
virtual void getValues(std::map<std::string,float>& values) const override;
80+
virtual QString displayValue(float v) const override;
81+
virtual QString legend(int i,float v,bool show_value=true) const override;
82+
virtual void update() override;
83+
QString unitName() const override;
8484

8585
// own methdods to control what's used to create displayed info
8686

@@ -89,9 +89,9 @@ class BWGraphSource: public RSGraphSource
8989
void setUnit(int unit) ;
9090
void setTiming(int t) ;
9191

92-
int direction() const { return _current_direction ;}
93-
int unit() const { return _current_unit ;}
94-
int friendGraphType() const { return _friend_graph_type ;}
92+
int direction() const { return _current_direction ;}
93+
int unit() const { return _current_unit ;}
94+
int friendGraphType() const { return _friend_graph_type ;}
9595
int serviceGraphType() const { return _service_graph_type ;}
9696

9797
const std::map<RsPeerId,std::string>& visibleFriends() const { return mVisibleFriends; }
@@ -166,10 +166,12 @@ class BWGraph: public RSGraphWidget
166166
BWGraph(QWidget *parent);
167167
~BWGraph();
168168

169-
void setSelector(int selector_type, int graph_type, const std::string& selector_client_string = std::string()) { _local_source->setSelector(selector_type,graph_type,selector_client_string) ; }
169+
void setSelector(int selector_type, int selector_value, const std::string& selector_client_string = std::string()) { _local_source->setSelector(selector_type,selector_value,selector_client_string) ; }
170170
void setDirection(int dir) { _local_source->setDirection(dir); }
171171
void setTiming(int t) { _local_source->setTiming(t); }
172172
void setUnit(int unit) { _local_source->setUnit(unit) ;}
173+
void setDataSliceDelay(int d) { _data_slice_delay = d; }
174+
173175
void clear() { _local_source->clear() ; }
174176

175177
int direction() const { return _local_source->direction(); }
@@ -178,4 +180,7 @@ class BWGraph: public RSGraphWidget
178180
const std::set<uint16_t>& visibleServices() const { return _local_source->visibleServices(); }
179181
protected:
180182
BWGraphSource *_local_source ;
183+
int _data_slice_delay;
184+
185+
bool _mouse_pressed;
181186
};

0 commit comments

Comments
 (0)