QCustomPlot Discussion and Comments

how to clear graph with a simple push bouton ? Return to overview

Hello ,


I created a window with several buttons thanks to which I draw graphs from CSV file. it works fine except that the graph does not fade when another is plotted which is clearly problematic for me.
How to erase the graph via the clear button or directly for each new graph? thank you
{
ui->setupUi(this);

ui->courbefinale->setBackground(gui_colors[1]);
ui->courbefinale->setNotAntialiasedElements(QCP::aeAll);
QFont font;
font.setPointSize(11);
font.setStyleStrategy (QFont::NoAntialias);
font.setWeight( QFont::Black );


QCPCurve *courbe1 = new QCPCurve(ui->courbefinale->xAxis, ui->courbefinale-> yAxis);
ui->courbefinale->addGraph();
ui->courbefinale->graph()->setPen (line_colors[0]);
ui->courbefinale->graph()->setName ( QString("ADMISSION"));
ui->courbefinale->graph(0)->setLineStyle(QCPGraph::LineStyle::lsLine);
ui->courbefinale->graph(0)->setScatterStyle(QCPScatterStyle::ssNone);
ui->courbefinale->graph(0)->setData(x, y);
ui->courbefinale->xAxis->grid()->setSubGridVisible(true);
ui->courbefinale->xAxis->setLabel("ANGLE en DEGRES °");
ui->courbefinale->xAxis->setLabelFont(font);
ui->courbefinale->xAxis->setRange(0,0.0200);
ui->courbefinale->yAxis->grid()->setSubGridVisible(true);
ui->courbefinale->yAxis->setLabel("LEVEE DE CAME EN mm");
ui->courbefinale->yAxis->setLabelFont(font);
ui->courbefinale->yAxis->setLabelFont(font );
ui->courbefinale->yAxis->setRange(-3000,4000.000);


QCPCurve *courbe2 = new QCPCurve(ui->courbefinale->xAxis, ui->courbefinale-> yAxis);
ui->courbefinale->addGraph();
ui->courbefinale->graph()->setPen (line_colors[1]);
ui->courbefinale->graph()->setName ( QString("ECHAPPEMENT"));
ui->courbefinale->graph(1)->setLineStyle(QCPGraph::LineStyle::lsLine);
ui->courbefinale->graph(1)->setScatterStyle(QCPScatterStyle::ssNone);
ui->courbefinale->graph(0)->setData(x1, y1);
ui->courbefinale->xAxis->grid()->setSubGridVisible(true);
ui->courbefinale->xAxis->setLabel("ANGLE EN DEGRES °");
ui->courbefinale->xAxis->setLabelFont(font);
ui->courbefinale->xAxis->setRange(0,0.02000);
ui->courbefinale->yAxis->grid()->setSubGridVisible(true);
ui->courbefinale->yAxis->setLabel("LEVEE DE CAME EN mm");
ui->courbefinale->yAxis->setLabelFont(font);
ui->courbefinale->yAxis->setRange(-3000,4000.000);

QFont legendFont;
legendFont.setPointSize (11);
ui->courbefinale->legend->setVisible (true);
ui->courbefinale->legend->setFont (legendFont);
ui->courbefinale->legend->setBrush (gui_colors[2]);
ui->courbefinale->legend->setBorderPen (gui_colors[2]);

}

Courbes::~Courbes()
{
delete ui;
}

using Eigen::MatrixXd;
using Eigen::ArrayXd;
using Eigen::ArithmeticSequence;
using Eigen::all;
using Eigen::last;
using Eigen::VectorXf;
using namespace std;
using namespace Eigen;
using Eigen::MatrixFunctionReturnValue;
using Eigen::Array;

int csvRead(MatrixXd& outputMatrix, const string& fileName, const streamsize dPrec)
{
ifstream inputData;
inputData.open(fileName);
cout.precision(dPrec);
if (!inputData)
return -1;
string fileline, filecell;
unsigned int prevNoOfCols = 0, noOfRows = 0, noOfCols = 0;
while (getline(inputData, fileline)) {
noOfCols = 0;
stringstream linestream(fileline);
while (getline(linestream, filecell, ';')) {
try {
stod(filecell);
}
catch (...) {
return -1;
}
noOfCols++;
}
if (noOfRows++ == 0)
prevNoOfCols = noOfCols;
if (prevNoOfCols != noOfCols)
return -1;
}
inputData.close();
outputMatrix.resize(noOfRows, noOfCols);
inputData.open(fileName);
noOfRows = 0;
while (getline(inputData, fileline)) {
noOfCols = 0;
stringstream linestream(fileline);
while (getline(linestream, filecell, ';')) {
outputMatrix(noOfRows, noOfCols++) = stod(filecell);
}
noOfRows++;
}
return 0;

}


int csvWrite(const MatrixXd& inputMatrix, const string& fileName, const streamsize dPrec) {
int i, j;
ofstream outputData;
outputData.open(fileName);
if (!outputData)
return -1;
outputData.precision(dPrec);
for (i = 0; i < inputMatrix.rows(); i++) {
for (j = 0; j < inputMatrix.cols(); j++) {
outputData << inputMatrix(i, j);
if (j < (inputMatrix.cols() - 1))
outputData << ";";
}
if (i < (inputMatrix.rows() - 1))
outputData << endl;
}
outputData.close();
if (!outputData)
return -1;
return 0;
}

void Courbes::on_LEVEESOUP_clicked()
{
QVector<double> x(511), y(511);
QVector<double> x1(511),y1(511);
int i=0;
QFile file("D:/Mes Documents/Arduino/PROJETS YANN/BANC AAC/LOGICIEL/arbre 200 cv/EXTRACTION.csv");

if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);

while (!in.atEnd())
{

QString line = in.readLine();
QStringList fields = line.split(';');
colonne5= fields[5].toDouble();
colonne6= fields[6].toDouble();
colonne7= fields[7].toDouble();
angle<<colonne5;
levee1<<colonne6;
levee2<<colonne7;

}
for (i=0;i<angle.size();i++)
{
x<<angle;
y<<levee1;
x1<<angle;
y1<<levee2;

}
qDebug()<<"valeur x="<<x;
qDebug()<<"valeur y="<<y;
ui->courbefinale->graph(0)->setData(x, y);
ui->courbefinale->graph(1)->setData(x1, y1);
ui->courbefinale->replot();

}

void Courbes::on_vitsoupape_clicked()
{
QVector<double> x(511), y(511);
QVector<double> x1(511),y1(511);
int i=0;
QFile file("D:/Mes Documents/Arduino/PROJETS YANN/BANC AAC/LOGICIEL/arbre 200 cv/GraphMot.csv");

if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);

while (!in.atEnd())
{

QString line = in.readLine();
QStringList fields = line.split(';');
colonne5= fields[3].toDouble();
colonne6= fields[4].toDouble();
colonne7= fields[5].toDouble();
angle<<colonne5;
levee1<<colonne6;
levee2<<colonne7;

}
for (i=0;i<angle.size();i++)
{
x<<angle;
y<<levee1;
x1<<angle;
y1<<levee2;

}
qDebug()<<"valeur x="<<x;
qDebug()<<"valeur y="<<y;
ui->courbefinale->graph(0)->setData(x, y);
ui->courbefinale->graph(1)->setData(x1, y1);
ui->courbefinale->replot();

}

void Courbes::on_ACCSOUP_clicked()
{
QVector<double> x(511), y(511);
QVector<double> x1(511),y1(511);
int i=0;
QFile file("D:/Mes Documents/Arduino/PROJETS YANN/BANC AAC/LOGICIEL/arbre 200 cv/GraphMot.csv");

if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);

while (!in.atEnd())
{

QString line = in.readLine();
QStringList fields = line.split(';');
colonne5= fields[3].toDouble();
colonne6= fields[6].toDouble();
colonne7= fields[7].toDouble();
angle<<colonne5;
levee1<<colonne6;
levee2<<colonne7;

}
for (i=0;i<angle.size();i++)
{
x<<angle;
y<<levee1;
x1<<angle;
y1<<levee2;

}
qDebug()<<"valeur x="<<x;
qDebug()<<"valeur y="<<y;
ui->courbefinale->graph(0)->setData(x, y);
ui->courbefinale->graph(1)->setData(x1, y1);
ui->courbefinale->replot();
}

void Courbes::on_JERK_clicked()
{
QVector<double> x(511), y(511);
QVector<double> x1(511),y1(511);
int i=0;
QFile file("D:/Mes Documents/Arduino/PROJETS YANN/BANC AAC/LOGICIEL/arbre 200 cv/GraphMot.csv");

if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);

while (!in.atEnd())
{

QString line = in.readLine();
QStringList fields = line.split(';');
colonne5= fields[3].toDouble();
colonne6= fields[8].toDouble();
colonne7= fields[9].toDouble();
angle<<colonne5;
levee1<<colonne6;
levee2<<colonne7;

}
for (i=0;i<angle.size();i++)
{
x<<angle;
y<<levee1;
x1<<angle;
y1<<levee2;

}
qDebug()<<"valeur x="<<x;
qDebug()<<"valeur y="<<y;
ui->courbefinale->graph(0)->setData(x, y);
ui->courbefinale->graph(1)->setData(x1, y1);
ui->courbefinale->replot();
}

void Courbes::clearData()
{
x.clear();
y.clear();
x1.clear();
y1.clear();
}

void Courbes::on_CLEARGRAPH_clicked()
{
clearData();
}

I'm not sure where is the problem...
Do you want to replace the previous graph with a new one ?
For that you can just replace the data with

graph->setData(data);

You can also use
graph->data()->clear;
if you just want to remove the data.
Or even
plot->removeGraph(graph);
if you want to delete the graph from the plot

And for the button, you just have to connect it to the slot that will clear/replace the data.


void Courbes::on_CLEARGRAPH_clicked()
{
ui->courbefinale->clearGraphs();
ui->courbefinale->clearItems();
ui->courbefinale->replot();
}