void MainWindow::onPacketParsed(Packet packet)
{
    _packetsReceivedCount++;

    if(!packet.isCrcValid())
    {
        _crcErrorCount++;
    }

    QString sourceId = ByteArrayUtils::toString(packet.sourceId()).replace(" 0x", "").replace("0x", "");
    if(!_rssValues.contains(sourceId))
    {
        _rssValues.insert(sourceId, QVector<double>());
        _timestampValues.insert(sourceId, QVector<double>());

        QCPGraph* deviceGraph = ui->plotWidget->addGraph();
        deviceGraph->setScatterStyle(QCP::ssDisc);
        deviceGraph->setScatterSize(5);
        deviceGraph->setName(sourceId);
        ensureDistinctColors();
    }

    _rssValues[sourceId].append(packet.rss());
    _timestampValues[sourceId].append(packet.timestamp().toTime_t());
    updatePlot();

    updateStatus();
}
Example #2
0
void ScatterPlotWidget::updatePlot(){
    ui->customPlot->clearGraphs();//clearPlottables();

    if(selectedTrips == NULL)
        return;

    //
    ExtraField field;
    switch(attrib1){
    case(FARE_AMOUNT):
        ui->customPlot->xAxis->setLabel(QString::fromStdString("Fare Amount (US$)"));
        break;
    case(TIP_AMOUNT):
        ui->customPlot->xAxis->setLabel(QString::fromStdString("Tip Amount (US$)"));
        break;
    case(DURATION):
        ui->customPlot->xAxis->setLabel(QString::fromStdString("Duration (min)"));
        break;
    case(DISTANCE):
        ui->customPlot->xAxis->setLabel(QString::fromStdString("Distance (mi)"));
        break;
    case(TOLL_AMOUNT):
        ui->customPlot->xAxis->setLabel(QString::fromStdString("Toll Amount (US$)"));
        break;
    case(AVG_SPEED):
        ui->customPlot->xAxis->setLabel(QString::fromStdString("Avg Speed (mi/h)"));
        break;
    case(TIME_OF_DAY):
        ui->customPlot->xAxis->setLabel(QString::fromStdString("Hour of day"));
        break;
    case(FIELD1):
        field = Global::getInstance()->getExtraField(0);
        ui->customPlot->xAxis->setLabel(field.axisLabel);
        break;
    case(FIELD2):
        field = Global::getInstance()->getExtraField(1);
        ui->customPlot->xAxis->setLabel(field.axisLabel);
        break;
    case(FIELD3):
        field = Global::getInstance()->getExtraField(2);
        ui->customPlot->xAxis->setLabel(field.axisLabel);
        break;
    case(FIELD4):
        field = Global::getInstance()->getExtraField(3);
        ui->customPlot->xAxis->setLabel(field.axisLabel);
        break;
    default:
        assert(false);
        break;
    }

    //
    switch(attrib2){
    case(FARE_AMOUNT):
        ui->customPlot->yAxis->setLabel(QString::fromStdString("Fare Amount (US$)"));
        break;
    case(TIP_AMOUNT):
        ui->customPlot->yAxis->setLabel(QString::fromStdString("Tip Amount (US$)"));
        break;
    case(DURATION):
        ui->customPlot->yAxis->setLabel(QString::fromStdString("Duration (min)"));
        break;
    case(DISTANCE):
        ui->customPlot->yAxis->setLabel(QString::fromStdString("Distance (mi)"));
        break;
    case(TOLL_AMOUNT):
        ui->customPlot->yAxis->setLabel(QString::fromStdString("Toll Amount (US$)"));
        break;
    case(AVG_SPEED):
        ui->customPlot->yAxis->setLabel(QString::fromStdString("Avg Speed (mi/h)"));
        break;
    case(TIME_OF_DAY):
        ui->customPlot->yAxis->setLabel(QString::fromStdString("Hour of day"));
        break;
    case(FIELD1):
        field = Global::getInstance()->getExtraField(0);
        ui->customPlot->yAxis->setLabel(field.axisLabel);
        break;
    case(FIELD2):
        field = Global::getInstance()->getExtraField(1);
        ui->customPlot->yAxis->setLabel(field.axisLabel);
        break;
    case(FIELD3):
        field = Global::getInstance()->getExtraField(2);
        ui->customPlot->yAxis->setLabel(field.axisLabel);
        break;
    case(FIELD4):
        field = Global::getInstance()->getExtraField(3);
        ui->customPlot->yAxis->setLabel(field.axisLabel);
        break;
    default:
        assert(false);
        break;
    }

    //
    bool buildGlobalPlot = (selectionGraph->isEmpty());

    set<Group> groups;
    map<Group,vector<SelectionGraphNode*> > mapGroupToNodes;
    map<Group,vector<SelectionGraphEdge*> > mapGroupToEdges;
    set<Group>::iterator groupIterator;
    selectionGraph->groupNodesAndEdgeByColor(groups,mapGroupToNodes,mapGroupToEdges);
    map<Group,QCPGraph*> mapGroupGraph;

    //
    set<Group> notEmptyGroups;
    map<Group,vector<SelectionGraphNode*> > tempMapGroupToNodes;

    if(buildGlobalPlot){
        QPen pen;
        QCPGraph* graph = ui->customPlot->addGraph();
        QColor color(0,0,0);
        mapGroupGraph[Group(color)] = graph;
        color.setAlphaF(0.05);
        pen.setColor(color);
        graph->setPen(pen);
        graph->setLineStyle(QCPGraph::lsNone);
        graph->setScatterStyle(QCP::ssDisc);
        graph->setScatterSize(10);
    }
    else{
        //
        for(groupIterator = groups.begin() ; groupIterator != groups.end() ; ++groupIterator){
            vector<SelectionGraphNode*> &groupNodes = mapGroupToNodes[*groupIterator];
            vector<SelectionGraphNode*> validGroupNodes;
            int numGroupNodes = groupNodes.size();

            for(int i = 0 ; i < numGroupNodes ; ++i){
                SelectionGraphNode* node = groupNodes.at(i);
                if(node->inDegree() + node->outDegree() == 0)
                    validGroupNodes.push_back(node);
            }

            vector<SelectionGraphEdge*> &groupEdges = mapGroupToEdges[*groupIterator];
            if(groupEdges.size() + validGroupNodes.size() > 0){
                notEmptyGroups.insert(*groupIterator);
                tempMapGroupToNodes[*groupIterator] = validGroupNodes;
            }
        }

        groups.clear();
        groups = notEmptyGroups;
        mapGroupToNodes.clear();
        mapGroupToNodes = tempMapGroupToNodes;

        //
        for(groupIterator = groups.begin() ; groupIterator != groups.end() ; ++groupIterator){
            QPen pen;
            QCPGraph* graph = ui->customPlot->addGraph();
            graph->setLineStyle(QCPGraph::lsNone);
            graph->setScatterStyle(QCP::ssDisc);
            graph->setScatterSize(10);

            Group group = *groupIterator;

            QColor color = group.getColor();
            mapGroupGraph[group] = graph;
            color.setAlphaF(0.3);
            pen.setColor(color);
            graph->setPen(pen);
        }
    }

    //int numberOfTrip = selectedTrips->size();
    map<Group,pair<QVector<double>,QVector<double> > > mapGroupData;
    if(buildGlobalPlot){
        int numberOfTrips = selectedTrips->size();
        //QVector<double> x(numberOfTrips), y(numberOfTrips);
        QVector<double> x, y;
        mapGroupData[QColor(0,0,0)] = make_pair(x,y);
    }
    else{
        QVector<double> x, y;
        for(groupIterator = groups.begin() ; groupIterator != groups.end() ; ++groupIterator){
            Group group = *groupIterator;

            mapGroupData[group.getColor()] = make_pair(x,y);

        }
    }

    // add graphs with different scatter styles:
    KdTrip::TripSet::iterator it = selectedTrips->begin();
    for (; it != selectedTrips->end(); ++it) {
        const KdTrip::Trip * trip = *it;
        QPointF coords = getCoords(trip);

        if(buildGlobalPlot){
            pair<QVector<double>,QVector<double> > &data =
                    mapGroupData[QColor(0,0,0)];
            QVector<double> &x = data.first;
            QVector<double> &y = data.second;
            x << coords.x();
            y << coords.y();
        }
        else{
            for(groupIterator = groups.begin() ; groupIterator != groups.end() ; ++groupIterator){
                Group currentGroup = *groupIterator;

                assert(mapGroupToNodes.count(currentGroup) > 0 && mapGroupToEdges.count(currentGroup) > 0);

                if(tripSatisfiesConstraints(trip, mapGroupToNodes[currentGroup],mapGroupToEdges[currentGroup])){
                    pair<QVector<double>,QVector<double> > &data =
                            mapGroupData[currentGroup.getColor()];
                    QVector<double> &x = data.first;
                    QVector<double> &y = data.second;
                    x << coords.x();
                    y << coords.y();
                    continue; // make sure the point is only added once
                }
            }
        }
    }

    if(buildGlobalPlot){
        pair<QVector<double>,QVector<double> > &data =
                mapGroupData[QColor(0,0,0)];
        QVector<double> &x = data.first;
        QVector<double> &y = data.second;
        QCPGraph* graph = mapGroupGraph[QColor(0,0,0)];
        graph->setData(x, y);
        graph->rescaleAxes(true);
    }
    else{
        for(groupIterator = groups.begin() ; groupIterator != groups.end() ; ++groupIterator){
            Group currentGroup = *groupIterator;
            pair<QVector<double>,QVector<double> > &data =
                    mapGroupData[currentGroup.getColor()];
            QVector<double> &x = data.first;
            QVector<double> &y = data.second;
            QCPGraph* graph = mapGroupGraph[currentGroup.getColor()];
            graph->setData(x, y);
            graph->rescaleAxes(true);
        }
    }

    //
    int numGraphs = ui->customPlot->graphCount();
    for(int i = 0 ; i < numGraphs ; ++i){
        ui->customPlot->graph(i)->rescaleAxes(false,true);
    }
    //ui->customPlot->yAxis->scaleRange(1.1, ui->customPlot->yAxis->range().center());
    ui->customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical);
    ui->customPlot->setRangeZoom(Qt::Horizontal|Qt::Vertical);
    ui->customPlot->setInteractions(QCustomPlot::iRangeDrag | QCustomPlot::iRangeZoom | QCustomPlot::iSelectAxes |
                                    QCustomPlot::iSelectLegend | QCustomPlot::iSelectPlottables | QCustomPlot::iSelectTitle);

    //
    ui->customPlot->replot();
}