Come libreria per plottare i dati ho usato QTCustomPlot per la semplicita' di zoom/pan mentre per la lettura di CSV e' stata impiegata QtCSV
Il codice e' disponibile su GitHub. E' possibile zoom/pan ed avere le coordinate del puntatore
Per impostare QTCustomPlot su QtCreator si devono imporate i file qtcustomplot.cpp/ qtcustomplot.h, si aggiunge al file .pro QT += printsupport, si trascina sul form un QWidget, tasto destro Promote To e si fa il promote a QtCustomPlot (il metodo e' cambiato rispetto al precedente post https://debiaonoldcomputers.blogspot.com/2013/11/software-per-progetto-force-gauge.html)
======================================================
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include "variantdata.h"
#include "reader.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString filePath = "/home/luca/ss/7.csv";
QList<QStringList> readData = QtCSV::Reader::readToList(filePath,";");
QVector<double> x(readData.size()), y(readData.size());
//l.resize(readData.size());
for ( int i = 0; i < readData.size(); ++i )
{
x[i] = readData.at(i).value(1).toDouble()-1587149254484;
y[i] = readData.at(i).value(2).toDouble();
}
//Abilita Drag e Zoom
ui->customPlot->setInteraction(QCP::iRangeDrag,true);
ui->customPlot->setInteraction(QCP::iRangeZoom,true);
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setData(x,y);
ui->customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20)));
ui->customPlot->graph(0)->setPen(QPen(Qt::blue));
connect(ui->customPlot,SIGNAL(mousePress(QMouseEvent *)),SLOT (clickedGraph(QMouseEvent*)));
connect(ui->customPlot,SIGNAL(mouseMove(QMouseEvent *)),SLOT (MouseGraph(QMouseEvent*)));
ui->customPlot->addGraph();
ui->customPlot->xAxis->setLabel("Time");
ui->customPlot->yAxis->setLabel("Acc");
//double x_min = *std::min_element(x.constBegin(), x.constEnd());
//double x_max = *std::max_element(x.constBegin(), x.constEnd());
//double y_min = *std::min_element(y.constBegin(), y.constEnd());
//double y_max = *std::max_element(y.constBegin(), y.constEnd());
//ui->customPlot->xAxis->setRange(x_min, x_max);
//ui->customPlot->yAxis->setRange(y_min, y_max);
ui->customPlot->rescaleAxes();
ui->customPlot->replot();
//qDebug() << ui->customPlot->xAxis->pixelToCoord(mouseEvent->pos().x()) << ui->customPlot->yAxis->pixelToCoord(mouseEvent->pos().y());
}
void MainWindow::clickedGraph(QMouseEvent *event)
{
QPoint point = event->pos();
qDebug() << ui->customPlot->xAxis->pixelToCoord(point.x());
qDebug() << ui->customPlot->yAxis->pixelToCoord(point.y());
}
void MainWindow::MouseGraph(QMouseEvent *event)
{
QPoint point = event->pos();
qDebug() << ui->customPlot->xAxis->pixelToCoord(point.x());
qDebug() << ui->customPlot->yAxis->pixelToCoord(point.y());
QVariant xi = ui->customPlot->xAxis->pixelToCoord(point.x());
QVariant yi = ui->customPlot->yAxis->pixelToCoord(point.y());
ui->xmouse->setText(xi.toString());
ui->ymouse->setText(yi.toString());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
}
Nessun commento:
Posta un commento