Example #1
0
void ScatterWidget::setData(const std::vector<PointD> &data, string legend)
{
    QCPGraph *graph = NULL;
    int nG = ui->scatter->graphCount();
    for(int i=0; i<nG; i++)
    {
        if(ui->scatter->graph(i)->name() == QString::fromStdString(legend))
        {
            graph = ui->scatter->graph(i);
            break;
        }
    }

    if(!graph)
    {
        graph = ui->scatter->addGraph();
        // ----------------------- Scatter Configuration ---------------------------
        graph->setName(QString::fromStdString(legend));
        QColor color_ = colorManager.getNewDifferentColor();
        graph->setPen(QPen(color_));
        graph->setLineStyle(QCPGraph::lsNone);
        graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssPlusCircle, 4));
        ui->scatter->legend->setVisible(true);
    }

    double min_x =  INFINITY, min_y =  INFINITY;
    double max_x = -INFINITY, max_y = -INFINITY;

    QVector<QCPGraphData> q_data(data.size());
    for(unsigned int i=0; i<data.size(); i++)
    {
        q_data[i] = QCPGraphData(data[i].x, data[i].y);

        if(ui->recButton->isChecked())
        {
            vector<double> vec2_double(2);
            vec2_double[0] = data[i].x;
            vec2_double[1] = data[i].y;
            vector<string> vec2_string(2);
            vec2_string[0] = legend + "_x";
            vec2_string[1] = legend + "_y";
            logger.addLogCsv(graph->dataCount(), vec2_string, vec2_double);
        }

        max_x = qMax(data[i].x, ui->scatter->xAxis->range().upper);
        min_x = qMin(data[i].x, ui->scatter->xAxis->range().lower);
        max_y = qMax(data[i].y, ui->scatter->yAxis->range().upper);
        min_y = qMin(data[i].y, ui->scatter->yAxis->range().lower);
    }

    graph->data()->set(q_data);
    ui->scatter->xAxis->setRange(min_x, max_x);
    ui->scatter->yAxis->setRange(min_y, max_y);
    ui->scatter->replot();
}
Example #2
0
void DataExplorer::pointInfo(QMouseEvent *event)
{
	QCPAbstractPlottable *plottable = ui->runPlot->plottableAt(event->localPos());
	if (!plottable) return;

	double x = ui->runPlot->xAxis->pixelToCoord(event->localPos().x());
	QToolTip::hideText();

	QCPGraph *graph = qobject_cast<QCPGraph*>(plottable);
	if (graph) {
		double key = 0;
		double value = 0;
		bool ok = false;
		double m = std::numeric_limits<double>::max();
		for (QCPData data : graph->data()->values()) {
			double d = qAbs(x - data.key);

			if(d < m) {
				key = data.key;
				value = data.value;

				ok = true;
				m = d;
			}
		}
		if (!ok) return;
		QToolTip::showText(event->globalPos(),
			tr("<center><b>%L1</b><br/>%L2 %L3@ %L4s</center>").
				arg(graph->name().isEmpty() ? "..." : graph->name()).
				arg(value).arg(graph->property("unit").toString()).
				arg(key),
			ui->runPlot, ui->runPlot->rect());
		return;
	}

	QCPStatisticalBox *graphBox = qobject_cast<QCPStatisticalBox*>(plottable);
	if (graphBox) {
		QToolTip::showText(event->globalPos(),
			tr("<center><b>%L1</b><br/></center>Max: %2<br/>Upper: %3<br/>Median: %4<br/>Lower: %5<br/>Min: %6<br/>StdDev: %7<br/>Avg: %8<br/>Avg Time: %9").
				arg(graphBox->name().isEmpty() ? "..." : graphBox->name()).
				arg(graphBox->maximum()).arg(graphBox->upperQuartile()).arg(graphBox->median()).
				arg(graphBox->lowerQuartile()).arg(graphBox->minimum()).
				arg(graphBox->property("StdDev").toDouble()).arg(graphBox->property("avg").toDouble()).
				arg(graphBox->property("avgTime").toDouble()),
			ui->runPlot, ui->runPlot->rect());
	}

}
void AP2DataPlot2D::plotMouseMove(QMouseEvent *evt)
{
    if (!ui.showValuesCheckBox->isChecked())
    {
        return;
    }
    QString newresult = "";
    for (int i=0;i<m_graphClassMap.keys().size();i++)
    {

        double key=0;
        double val=0;
        QCPGraph *graph = m_graphClassMap.value(m_graphClassMap.keys()[i]).graph;
        graph->pixelsToCoords(evt->x(),evt->y(),key,val);
        if (i == 0)
        {
            if (m_logLoaded)
            {
                newresult.append("Log Line: " + QString::number(key,'f',0) + "\n");
            }
            else
            {
                newresult.append("Time: " + QDateTime::fromMSecsSinceEpoch(key * 1000.0).toString("hh:mm:ss") + "\n");
            }
        }
        if (m_graphClassMap.keys()[i] == "MODE")
        {
            if (m_graphClassMap.value(m_graphClassMap.keys()[i]).modeMap.keys().size() > 1)
            {
                for (QMap<double,QString>::const_iterator modemapiterator = m_graphClassMap.value(m_graphClassMap.keys()[i]).modeMap.constBegin();modemapiterator!=m_graphClassMap.value(m_graphClassMap.keys()[i]).modeMap.constEnd();modemapiterator++)
                {
                    if (modemapiterator.key() < key)
                    {
                        if (modemapiterator==m_graphClassMap.value(m_graphClassMap.keys()[i]).modeMap.constEnd()-1)
                        {
                            //We're at the end, use the end
                            newresult.append(m_graphClassMap.keys()[i] + ": " + modemapiterator.value() + ((i == m_graphClassMap.keys().size()-1) ? "" : "\n"));
                        }
                        else if ((modemapiterator+1).key() > key)
                        {
                            //This only gets hit if we're not at the end, and we have the proper value
                            newresult.append(m_graphClassMap.keys()[i] + ": " + modemapiterator.value() + ((i == m_graphClassMap.keys().size()-1) ? "" : "\n"));
                            break;
                        }
                    }
                }
            }
            else if (m_graphClassMap.value(m_graphClassMap.keys()[i]).modeMap.keys().size() == 1)
            {
                newresult.append(m_graphClassMap.keys()[i] + ": " + m_graphClassMap.value(m_graphClassMap.keys()[i]).modeMap.begin().value() + ((i == m_graphClassMap.keys().size()-1) ? "" : "\n"));
            }
            else
            {
                newresult.append(m_graphClassMap.keys()[i] + ": " + "Unknown" + ((i == m_graphClassMap.keys().size()-1) ? "" : "\n"));
            }
        }
        else if (graph->data()->contains(key))
        {
            newresult.append(m_graphClassMap.keys()[i] + ": " + QString::number(graph->data()->value(key).value,'f',4) + ((i == m_graphClassMap.keys().size()-1) ? "" : "\n"));
        }
        else if (graph->data()->lowerBound(key) != graph->data()->constEnd())
        {
            newresult.append(m_graphClassMap.keys()[i] + ": " + QString::number((graph->data()->lowerBound(key).value().value),'f',4) + ((i == m_graphClassMap.keys().size()-1) ? "" : "\n"));
        }
        else
        {
            newresult.append(m_graphClassMap.keys()[i] + ": " + "ERR" + ((i == m_graphClassMap.keys().size()-1) ? "" : "\n"));
        }
    }
    QToolTip::showText(QPoint(evt->pos().x() + m_plot->x(),evt->pos().y()+m_plot->y()),newresult);
}
Example #4
0
void smart_plot::mousePress(QMouseEvent *event)
{
    static double prevKey, prevValue;

    if(event->button() == Qt::MiddleButton)
    {
        QCPAbstractPlottable *plottable = activePlot()->plottableAt(event->localPos());

        if(plottable)
        {
            QCPGraph *graph = qobject_cast<QCPGraph*>(plottable);
            plot_analytics analytics;
            plotStats stats;

            if(graph)
            {
                //Do diff by % range vs
                double mouseKey = graph->keyAxis()->pixelToCoord(event->localPos().x());
                double mouseValue = graph->valueAxis()->pixelToCoord(event->localPos().y());
                double keyRange = graph->keyAxis()->range().size();
                double keyDistance_no_abs = 0;
                double value = 0;
                double key = 0;

                bool ok = false;
                double m = std::numeric_limits<double>::max();
                //QCPGraphDataContainer
                analytics.plotAnalyze( graph, &stats, graph->keyAxis()->range());

                //Iterate through on screen data and see which point is closest
                QCPGraphDataContainer::const_iterator QCPGraphDataBegin = graph->data().data()->findBegin(graph->keyAxis()->range().lower,true);
                QCPGraphDataContainer::const_iterator QCPGraphDataEnd = graph->data().data()->findEnd(graph->keyAxis()->range().upper,true);
                for (QCPGraphDataContainer::const_iterator QCPGraphDataIt=QCPGraphDataBegin; QCPGraphDataIt!=QCPGraphDataEnd; ++QCPGraphDataIt)
                {
                    double valueRange = graph->valueAxis()->range().size();
                    double keyDistance = qAbs(mouseKey - QCPGraphDataIt->key)/keyRange;
                    double valueDistance = qAbs(mouseValue - QCPGraphDataIt->value)/valueRange;

                    if( (valueDistance + keyDistance) < m )
                    {
                        value = QCPGraphDataIt->value;
                        key = QCPGraphDataIt->key;
                        keyDistance_no_abs = mouseKey - QCPGraphDataIt->key;
                        ok = true;
                        m = (valueDistance + keyDistance);
                    }
                }
//                qDebug () << QDateTime::fromTime_t((int)mouseKey) << value;

                if(ok)
                {
                    QToolTip::hideText();

                    if(!qSharedPointerDynamicCast<QCPAxisTickerDateTime>(graph->keyAxis()->ticker()).isNull())
                    {
                        if(QApplication::keyboardModifiers().testFlag(Qt::ControlModifier))
                        {
                            quint32 timeDelta = qAbs(mouseKey-prevKey);

                            // add the bracket at the top:
                            QCPItemBracket *bracket = new QCPItemBracket(activePlot());
                            bracket->left->setAxes(graph->keyAxis(), graph->valueAxis());
                            bracket->left->setCoords(prevKey, value);
                            bracket->right->setAxes(graph->keyAxis(), graph->valueAxis());
                            bracket->right->setCoords(mouseKey, value);

                            // add the text label at the top:
                            QCPItemText *wavePacketText = new QCPItemText(activePlot());
                            wavePacketText->position->setParentAnchor(bracket->center);
                            wavePacketText->position->setCoords(0, -10); // move 10 pixels to the top from bracket center anchor
                            wavePacketText->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter);
                            wavePacketText->setText(
                                QString("%L1: ΔX->%L2 ΔY->%L3").
                                   arg(graph->name().isEmpty() ? "..." : graph->name()).
                                   arg(seconds_to_DHMS(timeDelta)).
                                   arg(value-prevValue));
                            wavePacketText->setFont(QFont(font().family(), 12));
                            activePlot()->replot();
                        }
                        else if(QApplication::keyboardModifiers().testFlag(Qt::AltModifier))
                        {
                            if(QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier))
                                graph->addData(graph->keyAxis()->pixelToCoord(event->localPos().x()),
                                               graph->valueAxis()->pixelToCoord(event->localPos().y()));
                            else if(keyDistance_no_abs < 0)
                                graph->addData(key - 1, std::numeric_limits<double>::quiet_NaN());
                            else
                                graph->addData(key + 1, std::numeric_limits<double>::quiet_NaN());

                            activePlot()->replot();
                        }
                        else if(QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier))
                        {
                            //Delete point
                            graph->data().data()->remove(key);
                            activePlot()->replot();
                        }
                        //Hold Alt to insert NAN(Break link?)
                        else
                        {
                            QDateTime dateTime;
                            dateTime.setTime_t(mouseKey);

                            //activePlot()->xAxis->tick
                            QToolTip::showText(event->globalPos(),
                            QString("<table>"
                                    "<tr>" "<th colspan=\"2\">%L1</th>"  "</tr>"
                                    "<tr>" "<td>X:</td>"   "<td>%L2</td>" "</tr>"
                                    "<tr>" "<td>Y:</td>"   "<td>%L3</td>" "</tr>"
                                    "<tr>" "<td>Min:</td>" "<td>%L4</td>" "</tr>"
                                    "<tr>" "<td>Avg:</td>" "<td>%L5</td>" "</tr>"
                                    "<tr>" "<td>Max:</td>" "<td>%L6</td>" "</tr>"
                                   "</table>").
                               arg(graph->name().isEmpty() ? "..." : graph->name()).
                               arg(dateTime.toString(qSharedPointerDynamicCast<QCPAxisTickerDateTime>(graph->keyAxis()->ticker())->dateTimeFormat())).
                               arg(value).
                               arg(stats.minValue).
                               arg(stats.avgValue).
                               arg(stats.maxValue),
                               activePlot(), activePlot()->rect());
                        }
                    }
                    else
                    {
                        QToolTip::showText(event->globalPos(),
                        QString("<table>"
                                "<tr>" "<th colspan=\"2\">%L1</th>"  "</tr>"
                                "<tr>" "<td>X:</td>"   "<td>%L2</td>" "</tr>"
                                "<tr>" "<td>Y:</td>"   "<td>%L3</td>" "</tr>"
                                "<tr>" "<td>Min:</td>" "<td>%L4</td>" "</tr>"
                                "<tr>" "<td>Avg:</td>" "<td>%L5</td>" "</tr>"
                                "<tr>" "<td>Max:</td>" "<td>%L6</td>" "</tr>"
                               "</table>").
                           arg(graph->name().isEmpty() ? "..." : graph->name()).
                           arg(mouseKey).
                           arg(value).
                           arg(stats.minValue).
                           arg(stats.avgValue).
                           arg(stats.maxValue),
                           activePlot(), activePlot()->rect());
                    }
                }

                prevKey = mouseKey;
                prevValue = value;
            }
        }
    }
}