void MainWindow::setupDateTest(QCustomPlot *customPlot)
{
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
	customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
	QCPGraph *g = customPlot->addGraph();
	g->addData(QDateTime(QDate(350,5,21), QTime(0, 0)).toMSecsSinceEpoch()/1000.0, 1);
	g->addData(QDateTime(QDate(650,5,21), QTime(0, 0)).toMSecsSinceEpoch()/1000.0, 2);
	g->addData(QDateTime(QDate(740,5,21), QTime(0, 0)).toMSecsSinceEpoch()/1000.0, 4);
	g->addData(QDateTime(QDate(1000,5,21), QTime(0, 0)).toMSecsSinceEpoch()/1000.0, 8);
	g->rescaleAxes();
#endif
}
示例#2
0
void OdroidReader::updateCurve(int row, int col) {
	if (col != 0) return;

	bool enable = ui->sensors->item(row,col)->checkState() == Qt::Checked;
	if (enable && graphs.at(row) == nullptr) {
		if (origcols.count() == 0) {
			ui->sensors->item(row,col)->setCheckState(Qt::Unchecked);
			return;
		}
		QColor color = origcols.takeFirst();
		QCPGraph* graph = ui->globalPlot->addGraph();
		DataSeries* series =  data[rowMap[row]];
		graph->setName(series->descriptor->name());
		graph->setProperty("unit",series->descriptor->unit());
		graph->setPen(color);
		graph->setData(series->getTimestamps(), series->getValues());
		connect(series,&DataSeries::newValue,[graph,this](double time, double value) {
			graph->addData(time,value);
			ui->globalPlot->rescaleAxes();
			ui->globalPlot->yAxis->scaleRange(1.2,ui->globalPlot->yAxis->range().center());
			ui->globalPlot->replot();
		});
		graphs[row] = graph;
		ui->sensors->item(row,col)->setBackgroundColor(color);
	} else if (!enable && graphs.at(row) != nullptr){
		disconnect(data.at(rowMap[row]),SIGNAL(newValue(double,double)),0,0);
		origcols.push_front(graphs.at(row)->pen().color());
		ui->globalPlot->removeGraph(graphs.at(row));
		graphs[row] = nullptr;
		ui->sensors->item(row,col)->setBackgroundColor(Qt::white);
	}
示例#3
0
void MainWindow::on_listView_clicked(const QModelIndex &index)
{
    //QModelIndex a = ui->listView->selectedIndexes().back();
    QVariant selected = dataModel->compNameList->data(index,Qt::DisplayRole);
    dataModel->dataViewComp = selected.toString();
    QVector<double> dataViewPrice = dataModel->priceBuff.value(selected.toString());
    QVector<double> xval;
    QVector<double> baseVal;
    if(dataViewPrice.empty()) return;
    double min = dataViewPrice[0];
    for(auto &i : dataViewPrice) min = std::fmin(min,i);
    for(int i = 0; i<dataViewPrice.size(); i++){
        xval<<i;
        baseVal<<min;
    }
    QCustomPlot* p = ui->qcpDataView;;
    p->clearGraphs();

    QCPGraph* timeline = new QCPGraph(p->xAxis,p->yAxis);
    timeline->addData(xval,dataViewPrice);
    QCPGraph* base = new QCPGraph(p->xAxis,p->yAxis);
    base->setData(xval,baseVal);

    p->addPlottable(timeline);
    p->addPlottable(base);

    timeline->setChannelFillGraph(base);
    timeline->setPen(QPen(QColor(0,0,0,0)));
    timeline->setBrush(QColor(0,0,255,100));

    //QVector<double> ticks;
    QVector<QString> labels;

    int L = this->dataModel->dateBuff.values()[0].size()-1;
    int len = 6;
    float step = (float)L/(float)len;
    for(int i = 0; i<= len; i++){
        labels<<this->dataModel->dateBuff.values()[0][i*step].toString("MM/yy");
    }

    p->xAxis->setTickVectorLabels(labels);
    p->xAxis->setAutoTicks(true);
    p->xAxis->setAutoTickLabels(false);

    p->xAxis->setTickLabelRotation(-60);
    //p->xAxis->setSubTickCount(0);
    //p->xAxis->setTickLength(0, len-1);

    p->xAxis->grid()->setVisible(true);
    //p->xAxis->setRange(0, len-2);
    //p->xAxis->setTickVector(ticks);

    p->rescaleAxes();
    p->replot();
}
示例#4
0
void ScatterWidget::addPacket(const ScatterPacket &packet)
{
    QCPGraph *graph = NULL;
    int nG = ui->scatter->graphCount();
    for(int i=0; i<nG; i++)
    {
        if(ui->scatter->graph(i)->name() == QString::fromStdString(packet.legend))
        {
            graph = ui->scatter->graph(i);
            break;
        }
    }

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

    graph->addData(packet.point.x, packet.point.y);
    //if(packet.point.x > ui->scatter->xAxis->max)

    double max_x = qMax(packet.point.x, ui->scatter->xAxis->range().upper);
    double min_x = qMin(packet.point.x, ui->scatter->xAxis->range().lower);
    double max_y = qMax(packet.point.y, ui->scatter->yAxis->range().upper);
    double min_y = qMin(packet.point.y, ui->scatter->yAxis->range().lower);

    ui->scatter->xAxis->setRange(min_x, max_x);
    ui->scatter->yAxis->setRange(min_y, max_y);

    if(realTimePlot)
        ui->scatter->replot();
    else
        ui->scatter->replot(QCustomPlot::rpQueuedReplot);

    if(ui->recButton->isChecked())
    {
        vector<double> vec2_double(2);
        vec2_double[0] = packet.point.x;
        vec2_double[1] = packet.point.y;
        vector<string> vec2_string(2);
        vec2_string[0] = packet.legend + "_x";
        vec2_string[1] = packet.legend + "_y";
        logger.addLogCsv(graph->dataCount(), vec2_string, vec2_double);
    }
}
示例#5
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;
            }
        }
    }
}