void BandwidthGui::initPlugin(qt_gui_cpp::PluginContext& ctx) { m_plot = new QCustomPlot(); m_plot->legend->setVisible(true); m_plot->legend->setIconBorderPen(Qt::NoPen); m_plot->yAxis->setLabel("Kb/s"); QWidget* wrapper = new QWidget(); m_connectionBox = new QComboBox(wrapper); QPushButton* groupBtn = new QPushButton("Group Settings", wrapper); QGridLayout* gl = new QGridLayout(wrapper); gl->addWidget(m_plot, 0, 0, 1, 2); gl->addWidget(m_connectionBox, 1, 0); gl->addWidget(groupBtn, 1, 1); wrapper->setLayout(gl); ctx.addWidget(wrapper); connect( m_plot, SIGNAL(legendClick(QCPLegend *, QCPAbstractLegendItem *, QMouseEvent *)), this, SLOT(handleClickedLegend(QCPLegend*, QCPAbstractLegendItem*, QMouseEvent*)) ); connect( m_connectionBox, SIGNAL(currentIndexChanged(int)), this, SLOT(clearPlot()) ); connect( groupBtn, SIGNAL(pressed()), this, SLOT(updateGroupInfo()) ); qRegisterMetaType<nimbro_topic_transport::SenderStatsConstPtr>(); connect( this, SIGNAL(senderStatsReceived(nimbro_topic_transport::SenderStatsConstPtr)), this, SLOT(handleSenderStats(nimbro_topic_transport::SenderStatsConstPtr)), Qt::QueuedConnection ); m_sub_senderStats = getPrivateNodeHandle().subscribe( "/network/sender_stats", 1, &BandwidthGui::senderStatsReceived, this ); m_plot->xAxis->setTickLabelType(QCPAxis::ltDateTime); m_plot->xAxis->setDateTimeFormat("hh:mm:ss"); m_plot->xAxis->setAutoTickStep(true); m_plot->yAxis->setRangeLower(0); QCPLayoutGrid* subLayout = new QCPLayoutGrid(); QCPLayoutElement* dummy = new QCPLayoutElement(); m_plot->plotLayout()->addElement(0, 1, subLayout); subLayout->addElement(0, 0, m_plot->legend); subLayout->addElement(1, 0, dummy); subLayout->setRowStretchFactor(0, 0.01); m_plot->plotLayout()->setColumnStretchFactor(1, 0.01); connect( m_plot->xAxis, SIGNAL(rangeChanged(QCPRange)), m_plot->xAxis2, SLOT(setRange(QCPRange)) ); connect( m_plot->yAxis, SIGNAL(rangeChanged(QCPRange)), m_plot->yAxis2, SLOT(setRange(QCPRange)) ); connect( &m_plotTimer, SIGNAL(timeout()), this, SLOT(updatePlot()) ); m_plotTimer.start(0); }
int BarPlot::plotGrant() { string dataType = Node->getFirst(); // Get the types // Note the type is not always going to be the within the children of the node (depending on what node is passed) // It will be possible to pass in a different node (say a publication type node) when implemented vector<node*> types; vector<node*>* temptypes = Node->getChildren(); for (int i = 0; i < temptypes->size(); i++){ if (temptypes->at(i)->getSecond() > 0) types.push_back(temptypes->at(i)); } // Grab Data and prepare x axis with (professor Name) labels: QVector<QString> labels; //display the types only for (int i = 0; i < Node->getChildren()->size(); i++) labels.push_back(QString::fromStdString(Node->getChildren()->at(i)->getFirst())); // stacked bar chart can get cluttered, ensure no more than 30 different types // determine which types to push into an "Others" group vector<int> othersNdx; if (types.size() > COUNT_MAX){ vector<double> typeSumCounts; for (int i = 0; i < types.size(); i++){ typeSumCounts.push_back(types.at(i)->getFourth()); } while (types.size() - othersNdx.size() > COUNT_MAX){ othersNdx.push_back(min_element(typeSumCounts.begin(), typeSumCounts.end()) - typeSumCounts.begin()); typeSumCounts.at(min_element(typeSumCounts.begin(), typeSumCounts.end()) - typeSumCounts.begin()) = std::numeric_limits<double>::infinity(); } } QVector<double> ticks; for (int i = 1; i <= labels.size(); i++) ticks.push_back(i); QVector<double> count(labels.size()); double *data = count.data(); // create a new plottable area for each type, group everything within the "Others" group together for (int i = 0; i < types.size(); i++){ data[i] = types.at(i)->getFourth(); } QCPBars *bar = new QCPBars(this->xAxis, this->yAxis); bar->setName(QString::fromStdString(dataType)); bar->setData(ticks, count); this->addPlottable(bar); // set the colors QPen pen; pen.setWidthF(1.2); int C_HUE = 0; QColor color_brush, color_pen; color_brush.setHsv(C_HUE, BAR_SAT, BAR_VAL); color_brush.setAlpha(BAR_ALPHA); color_pen.setHsv(C_HUE, BAR_SAT + 30, BAR_VAL + 10); color_pen.setAlpha(255); pen.setColor(color_pen); bar->setPen(pen); bar->setBrush(color_brush); // prepare x axis: this->xAxis->setAutoTicks(false); this->xAxis->setAutoTickLabels(false); this->xAxis->setTickVector(ticks); this->xAxis->setTickVectorLabels(labels); this->xAxis->setTickLabelRotation(60); this->xAxis->setSubTickCount(0); this->xAxis->setTickLength(0, 3); this->xAxis->grid()->setVisible(true); // prepare y axis: this->yAxis->setTickStep(5); this->yAxis->setPadding(5); // a bit more space to the left border this->yAxis->setLabel("Amount ($)"); this->yAxis->grid()->setSubGridVisible(true); QPen gridPen; gridPen.setStyle(Qt::SolidLine); gridPen.setColor(QColor(0, 0, 0, 25)); this->yAxis->grid()->setPen(gridPen); gridPen.setStyle(Qt::DotLine); this->yAxis->grid()->setSubGridPen(gridPen); this->yAxis->scaleRange(1.3, this->yAxis->range().center()); this->rescaleAxes(true); this->xAxis->setRange(0.5, 10.5); // setup legend: QCPLayoutGrid *subLayout = new QCPLayoutGrid; QCPLayoutElement *dummyElement = new QCPLayoutElement; this->plotLayout()->addElement(0, 1, subLayout); // add sub-layout in the cell to the right of the main axis rect subLayout->addElement(0, 0, this->legend); // add legend subLayout->addElement(1, 0, dummyElement); // add dummy element below legend subLayout->setRowStretchFactor(0, 0.01); // make legend cell (in row 0) take up as little vertical space as possible this->plotLayout()->setColumnStretchFactor(1, 0.01); // make the legend cell and dummy element column as small as possible this->legend->setVisible(true); this->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight); this->legend->setBrush(QColor(255, 255, 255, 200)); QPen legendPen; legendPen.setColor(QColor(130, 130, 130, 200)); this->legend->setBorderPen(legendPen); QFont legendFont = font(); legendFont.setPointSize(8); this->legend->setFont(legendFont); this->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iSelectLegend); return 0; }
void XyzWidget::setupRealtimeData1(QCustomPlot *customPlot) { #if QT_VERSION < QT_VERSION_CHECK(4, 7, 0) QMessageBox::critical(this, "", "You're using Qt < 4.7, the realtime data demo needs functions that are available with Qt 4.7 to work properly"); #endif // include this section to fully disable antialiasing for higher performance: /* customPlot->setNotAntialiasedElements(QCP::aeAll); QFont font; font.setStyleStrategy(QFont::NoAntialias); customPlot->xAxis->setTickLabelFont(font); customPlot->yAxis->setTickLabelFont(font); customPlot->legend->setFont(font); */ customPlot->legend->setVisible(true); customPlot->addGraph(); // Xline customPlot->graph(0)->setPen(QPen(Qt::blue)); customPlot->graph(0)->setName(QString("X")); customPlot->addGraph(); // Y line customPlot->graph(1)->setPen(QPen(Qt::red)); customPlot->graph(1)->setName(QString("Y")); customPlot->addGraph(); // Z line customPlot->graph(2)->setPen(QPen(Qt::green)); customPlot->graph(2)->setName(QString("Z")); customPlot->addGraph(); // X dot customPlot->graph(3)->setPen(QPen(Qt::blue)); customPlot->graph(3)->setLineStyle(QCPGraph::lsNone); customPlot->graph(3)->setScatterStyle(QCPScatterStyle::ssDisc); customPlot->addGraph(); // Y dot customPlot->graph(4)->setPen(QPen(Qt::red)); customPlot->graph(4)->setLineStyle(QCPGraph::lsNone); customPlot->graph(4)->setScatterStyle(QCPScatterStyle::ssDisc); customPlot->addGraph(); // Z dot customPlot->graph(5)->setPen(QPen(Qt::green)); customPlot->graph(5)->setLineStyle(QCPGraph::lsNone); customPlot->graph(5)->setScatterStyle(QCPScatterStyle::ssDisc); customPlot->legend->removeItem(3);//remove it from the legend customPlot->legend->removeItem(3);//remove it from the legend customPlot->legend->removeItem(3);//remove it from the legend customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime); customPlot->xAxis->setDateTimeFormat("hh:mm:ss"); customPlot->xAxis->setAutoTickStep(false); customPlot->xAxis->setTickStep(2); customPlot->axisRect()->setupFullAxesBox(); //Setting Layout of the graph QCPLayoutGrid *subLayout = new QCPLayoutGrid; QCPLayoutElement *dummyElement = new QCPLayoutElement; customPlot->plotLayout()->addElement(0, 1, subLayout); // add sub-layout in the cell to the right of the main axis rect customPlot->plotLayout()->setColumnStretchFactor(1,0.01); subLayout->addElement(0, 0, customPlot->legend); // add legend subLayout->addElement(1, 0, dummyElement); // add dummy element below legend subLayout->setRowStretchFactor(0, 0.01); // make legend cell (in row 0) take up as little vertical space as possible // make left and bottom axes transfer their ranges to right and top axes: connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange))); connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange))); // setup a timer that repeatedly calls MainWindow::realtimeDataSlot: }
int BarPlot::plotOther() { string dataType = Node->getFirst(); // Get the types // Note the type is not always going to be the within the children of the node (depending on what node is passed) // It will be possible to pass in a different node (say a publication type node) when implemented vector<node*> types; if (Node->getParent() == NULL){ vector<node*>* temptypes = Node->getChildren(); for (int i = 0; i < temptypes->size(); i++){ if (temptypes->at(i)->getSecond() > 0) types.push_back(temptypes->at(i)); } } else{ types.push_back(Node);} // Grab Data and prepare x axis with (professor Name) labels: QVector<QString> labels; // Search for the prof names for (int i = 0; i < types.size(); i++){ for (int j = 0; j < types.at(i)->getChildren()->size(); j++){ QString name = QString::fromStdString(types.at(i)->getChildren()->at(j)->getFirst()); if (!(labels.contains(name))) labels.push_back(name); } } // stacked bar chart can get cluttered, ensure no more than 30 different types // determine which types to push into an "Others" group vector<int> othersNdx; if (types.size() > COUNT_MAX){ vector<double> typeSumCounts; for (int i = 0; i < types.size(); i++){ if (Node->getFourth() > 0.0) typeSumCounts.push_back(types.at(i)->getFourth()); else typeSumCounts.push_back(types.at(i)->getSecond()); } while (types.size() - othersNdx.size() > COUNT_MAX){ othersNdx.push_back(min_element(typeSumCounts.begin(), typeSumCounts.end()) - typeSumCounts.begin()); typeSumCounts.at(min_element(typeSumCounts.begin(), typeSumCounts.end()) - typeSumCounts.begin()) = std::numeric_limits<double>::infinity(); } } QVector<double> ticks; for (int i = 1; i <= labels.size(); i++) ticks.push_back(i); vector<QCPBars*> bars; QVector<double> othersCount(labels.size()); double *othersData = othersCount.data(); // create a new plottable area for each type, group everything within the "Others" group together for (int i = 0; i < types.size(); i++) { QVector<double> count(labels.size()); double *data = count.data(); // Note: not all types have same number of children (profs) // this would affect the labels (prof names) for (int j = 0; j < types.at(i)->getChildren()->size(); j++){ int pos = labels.indexOf(QString::fromStdString(types.at(i)->getChildren()->at(j)->getFirst())); if (Node->getFourth() > 0.0) data[pos] = types.at(i)->getChildren()->at(j)->getFourth(); else data[pos] = types.at(i)->getChildren()->at(j)->getSecond(); } QCPBars *temp = new QCPBars(this->xAxis, this->yAxis); if (std::find(othersNdx.begin(), othersNdx.end(), i) != othersNdx.end()){ for (int j = 0; j < labels.size(); j++) othersData[j] += count[j]; } else{ temp->setName(QString::fromStdString(types.at(i)->getFirst())); temp->setData(ticks, count); bars.push_back(temp); this->addPlottable(temp); } } // Graph "Others" only if there's something in it if (std::find(othersCount.begin(), othersCount.end(), (!0)) != othersCount.end()){ QCPBars *temp = new QCPBars(this->xAxis, this->yAxis); temp->setName("Others"); temp->setData(ticks, othersCount); bars.push_back(temp); this->addPlottable(temp); } // stack bars ontop of each other: // loop through each of the QCPBar objects in the list bars if (bars.size() > 1){ for (int i = 0; i < (bars.size() - 1); i++) bars[i + 1]->moveAbove(bars[i]); } // set the colors QPen pen; pen.setWidthF(1.2); int C_HUE = 0; for (int i = 0; i < bars.size(); i++) { QColor color_brush, color_pen; color_brush.setHsv(C_HUE, BAR_SAT, BAR_VAL); color_brush.setAlpha(BAR_ALPHA); color_pen.setHsv(C_HUE, BAR_SAT + 30, BAR_VAL + 10); color_pen.setAlpha(255); pen.setColor(color_pen); bars[i]->setPen(pen); bars[i]->setBrush(color_brush); C_HUE += HUE_MAX / bars.size(); } //this->plotLayout()->addElement(0, 0, new QCPPlotTitle(this, QString::fromStdString(dataType))); // prepare x axis: this->xAxis->setAutoTicks(false); this->xAxis->setAutoTickLabels(false); this->xAxis->setTickVector(ticks); this->xAxis->setTickVectorLabels(labels); this->xAxis->setTickLabelRotation(60); this->xAxis->setSubTickCount(0); this->xAxis->setTickLength(0, 3); this->xAxis->grid()->setVisible(true); // prepare y axis: this->yAxis->setTickStep(5); this->yAxis->setPadding(5); // a bit more space to the left border if (Node->getFourth() > 0.0) this->yAxis->setLabel("Hours"); else this->yAxis->setLabel("Count"); this->yAxis->grid()->setSubGridVisible(true); QPen gridPen; gridPen.setStyle(Qt::SolidLine); gridPen.setColor(QColor(0, 0, 0, 25)); this->yAxis->grid()->setPen(gridPen); gridPen.setStyle(Qt::DotLine); this->yAxis->grid()->setSubGridPen(gridPen); this->yAxis->scaleRange(1.3, this->yAxis->range().center()); this->rescaleAxes(true); this->xAxis->setRange(0.5, 10.5); // setup legend: QCPLayoutGrid *subLayout = new QCPLayoutGrid; QCPLayoutElement *dummyElement = new QCPLayoutElement; this->plotLayout()->addElement(0, 1, subLayout); // add sub-layout in the cell to the right of the main axis rect subLayout->addElement(0, 0, this->legend); // add legend subLayout->addElement(1, 0, dummyElement); // add dummy element below legend subLayout->setRowStretchFactor(0, 0.01); // make legend cell (in row 0) take up as little vertical space as possible this->plotLayout()->setColumnStretchFactor(1, 0.01); // make the legend cell and dummy element column as small as possible this->legend->setVisible(true); this->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight); this->legend->setBrush(QColor(255, 255, 255, 200)); QPen legendPen; legendPen.setColor(QColor(130, 130, 130, 200)); this->legend->setBorderPen(legendPen); QFont legendFont = font(); legendFont.setPointSize(8); this->legend->setFont(legendFont); this->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iSelectLegend); if (std::find(othersCount.begin(), othersCount.end(), (!0)) != othersCount.end()){ return 1; } else return 0; }