Search code examples
c++qtqwt

C++ Qwt - Plotting data from a vector


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


Solution

  • 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