Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Qwt - Plotting data from a vector

Tags:

c++

qt

qwt

I'm attempting to plot a graph based on data that I have obtained and stored inside a vector, but, I cannot seem to find any tutorials or references out there and give me any indication to what I need to do. So here is my code:

class Plotter : public QwtPlot 
{


   public:

    Plotter() {

    }
};



int main( int argc, char **argv )
{

   QApplication app(argc, argv);

   //Plotter *d_plot = new Plotter();

    Plotter* d_plot = new Plotter();

   d_plot->setTitle("DEMO");
   d_plot->setCanvasBackground(Qt::white);
   d_plot->setAxisScale( QwtPlot::yLeft, 0.1, 50.0 );
   d_plot->setAxisScale(QwtPlot::yRight, 0.1, 50.00);

   // PLOT THE DATA
   std::vector<double> data;
   data.push_back(1.03);
   data.push_back(13.12);
   //....

   d_plot->resize( 600, 400 );
   d_plot->show();


   return app.exec();
}

Could anyone give me any ideas to what function I could use to allow me to plot the data?

Thanks

like image 765
Phorce Avatar asked Sep 05 '13 15:09

Phorce


2 Answers

Check the QwtPlot docs: normally you create a QwtPlotCurve, use QwtPlotCurve::setSamples to get the data in it then QwtPlotCurve::attach to get the data drawn.

Should be something like this:

std::vector<double> x; 
std::vector<double> y; 
//fill x and y vectors
//make sure they're the same size etc
QwtPlotCurve curve( "Foo" ); 
//or use &x[ 0 ] or &(*x.first()) pre-C++11
cure.setSamples( x.data(), y.data(), (int) x.size() );
curve.attach( &plot );

http://qwt.sourceforge.net/class_qwt_plot_curve.html

http://qwt.sourceforge.net/class_qwt_plot.html

like image 193
stijn Avatar answered Nov 05 '22 17:11

stijn


One way would be to attach a curve to your plot, i.e.:

QwtPlotCurve myCurve;
myCurve->attach(&d_plot);

You could then use (in a member function, or wherever you need) the function QwtPlotCurve::setRawSample which has the following pretty much explanatory signature:

void QwtPlotCurve::setRawSample(const double* xData, const double* yData, int size);

Set your data with it and then call replot() to refresh the plot. It means you must have also a vector for the x values.

The code would look like this:

int main( int argc, char **argv )
{
   //...
   Plotter* d_plot = new Plotter();

   //Plot config

   // PLOT THE DATA
   std::vector<double> data_y;
   data_y.push_back(1.03);
   data_y.push_back(13.12);
   std::vector<double> data_x;
   data_x.push_back(1.0);
   data_x.push_back(2.0);
   //....
   myCurve->setRawSample(data_x.data(),data_y.data(),data_y.size());
   d_plot->resize( 600, 400 );
   d_plot->replot();
   d_plot->show();
   //...
}

I'd suggest you study the Qwt doc about curve

like image 45
JBL Avatar answered Nov 05 '22 19:11

JBL