PDA

View Full Version : Strange lines in beginner plot...



emigrand
6th December 2011, 23:21
Hi,

first post, so please don't be too strict with me.

I'm biologist, not programmer, and it took me a while to get to this point. Now I'm stuck with a problem which I think is easy for a qwt-advanced. One of my final goals is to visualize data from an ad-converter with qwt. For getting accustomed with matters I tried to produce a simple plot with code I found somewhere (in a header?), but when I compile it, I find strange lines besides the curves I would expect:



class vtPlot : public QwtPlot
{
Q_OBJECT
static int const NPOINTS = 150;
double x[NPOINTS], ysin[NPOINTS], ycos[NPOINTS];

public:
explicit vtPlot(QObject *parent = 0);
void *myData();
QwtPointArrayData *myArray[2];
};

vtPlot::vtPlot(QObject *parent)
{
}

void *vtPlot::myData()
{
for(int i = 0; i < NPOINTS; i++)
{
x[i] = 0.1*i;
ysin[i] = sin(x[i]);
ycos[i] = cos(x[i]);
}
myArray[0] = new QwtPointArrayData(x,ysin,(sizeof(x)+sizeof(ysin))) ; // actually not sure if the third argument is correct
myArray[1] = new QwtPointArrayData(x,ycos,(sizeof(x)+sizeof(ycos))) ;
}


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;

vtPlot *myPlot = new vtPlot(&w);
QwtPlotCurve *curve1 = new QwtPlotCurve("sinus");
QwtPlotCurve *curve2 = new QwtPlotCurve("cosinus");

myPlot->myData();

curve1->setData(myPlot->myArray[0]);
curve1->attach(myPlot);

curve2->setData(myPlot->myArray[1]);
curve2->attach(myPlot);

myPlot->setTitle("Voltage [mV] - Time [s]");
myPlot->setAxisTitle(myPlot->yLeft,"Voltage [mV]");
myPlot->setAxisTitle(myPlot->xBottom,"Time [s]");

w.setCentralWidget(myPlot);

w.show();

return a.exec();
}


Gives me this:
7153

Of course I ensure having searched the forum and the www for help, but I wasn't successful.
Any hint would be appreciated.

Thanks a lot,
Emi

Santosh Reddy
7th December 2011, 01:34
Here you go, you already knew the problem but were not able to fix it. Here the fully working code and sample output

#include <QtGui> // <<<<<<<<<<<<<<< edited
#include <qwt_plot.h> // <<<<<<<<<<<<<<< edited
#include <qwt_plot_curve.h> // <<<<<<<<<<<<<<< edited
#include <qwt_series_data.h> // <<<<<<<<<<<<<<< edited


class vtPlot : public QwtPlot
{
Q_OBJECT
static int const NPOINTS = 150;
double x[NPOINTS], ysin[NPOINTS], ycos[NPOINTS];


public:
explicit vtPlot(QObject *parent = 0);
void myData(); // <<<<<<<<<<<<<<< edited
QwtPointArrayData *myArray[2];
};


vtPlot::vtPlot(QObject *parent)
{
}


void vtPlot::myData() // <<<<<<<<<<<<<<< edited
{
for(int i = 0; i < NPOINTS; i++)
{
x[i] = 0.1*i;
ysin[i] = sin(x[i]);
ycos[i] = cos(x[i]);
}
myArray[0] = new QwtPointArrayData(x,ysin,NPOINTS); // <<<<<<<<<<<<<<< edited
myArray[1] = new QwtPointArrayData(x,ycos,NPOINTS); // <<<<<<<<<<<<<<< edited
}




int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w; // <<<<<<<<<<<<<<< edited


vtPlot *myPlot = new vtPlot(&w);
QwtPlotCurve *curve1 = new QwtPlotCurve("sinus");
QwtPlotCurve *curve2 = new QwtPlotCurve("cosinus");


myPlot->myData();


curve1->setData(myPlot->myArray[0]);
curve1->attach(myPlot);


curve2->setData(myPlot->myArray[1]);
curve2->attach(myPlot);


myPlot->setTitle("Voltage [mV] - Time [s]");
myPlot->setAxisTitle(myPlot->yLeft,"Voltage [mV]");
myPlot->setAxisTitle(myPlot->xBottom,"Time [s]");


w.setCentralWidget(myPlot);


w.show();


return a.exec();
}


#include "main.moc" // <<<<<<<<<<<<<<< edited

7154

emigrand
7th December 2011, 08:12
Thanks a lot, Santosh! Works perfectly. Now I am motivated again.
For the next problem I promise to post the complete code, including headers.

Have a nice day!
Emi

FelixB
7th December 2011, 09:01
Hi emi,

there is a section for qwt in this forum, you should post there next time... not all qwt-specialists read here.

Felix