Results 1 to 14 of 14

Thread: Plot the graph for real time data

  1. #1
    Join Date
    Jul 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Plot the graph for real time data

    Hai,

    I am newbie to QT as well as Qwt.

    I am having QT 5.0.2, QT Creator 2.7.0 and Qwt 6.1.0.

    My requirement is Plotting the graph for the data is continuous data in real time, this data is reding from background process.


    According to my application, I am started the background process using QProcess that process is .exe file for C code. In this C code ther is a while(1) loop for reading Infinite data. In GUI I am able to read the data also.

    Now I want to plot the graph for those values to find the deviation.

    I have simple qwt code as below

    Qt Code:
    1. float y[20], x[20];
    2. int i;
    3. for(i=0;i<20;i++)
    4. {
    5. x[i]=i;
    6. y[i]=2*sin((x[i]));
    7.  
    8. }
    9. QwtPlot *plot=new QwtPlot();
    10. plot->setTitle( "Plot Demo" );
    11. plot->setCanvasBackground( Qt::white );
    12. //plot->setAxisScale( QwtPlot::yLeft, 0.0, 20.0);
    13. // plot->setAxisScale(QwtPlot::xBottom, 0.0, 20.0);
    14. plot->insertLegend( new QwtLegend() );
    15. plot->setAxisTitle(QwtPlot::xBottom, "X Axis");
    16. plot->setAxisTitle(QwtPlot::yLeft, "Y Axis");
    17. QwtPlotGrid *grid = new QwtPlotGrid();
    18. grid->attach( plot );
    19. grid->setPen( Qt::gray, 0.0, Qt::DotLine );
    20.  
    21. QwtPlotCurve *curve = new QwtPlotCurve();
    22. curve->setTitle( "Pixel Count" );
    23. curve->setPen(QPen(Qt::blue, 3,Qt::SolidLine) ),
    24. curve->setRenderHint( QwtPlotItem::RenderAntialiased, true);
    25.  
    26. QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,QBrush(Qt::yellow),QPen(Qt::red,2),QSize(8,8));
    27. curve->setSymbol(symbol );
    28.  
    29. QPolygonF points;
    30. for(i=0;i<20;i++)
    31. points<<QPointF(x[i],y[i]);//<<QPointF(x2,y2)<<QPointF(x3,y3)<<QPointF(x4,y4)<< QPointF(x5,y5);
    32. curve->setSamples( points );
    33. curve->attach(plot );
    34. plot->replot();
    35. plot->resize( 600, 400 );
    36.  
    37. plot->show();
    To copy to clipboard, switch view to plain text mode 

    and code for reading the background data

    Qt Code:
    1. QList<QByteArray> read_data;
    2.  
    3. b1= process->readAllStandardOutput();
    4.  
    5. read_data=b1.split('\n');
    6. int i=0,n=read_data.count();
    7. float y[100000];
    8. for(i=0;i<n;i++)
    9. {
    10. QString result=read_data.at(i);
    11. result.remove(0,10);
    12. qDebug("%s",result.toLatin1().data());
    13. y[i]=result.toLatin1().toFloat();
    14. }
    To copy to clipboard, switch view to plain text mode 

    how can I send the data in y[i] to "QPolygonF points"

    tried to send to QPolygonF but am not getting what I want.

    any one can give suggetions to meet my requirment

    Thanks in advance

  2. #2
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Plot the graph for real time data

    you have the solution in your code already:

    Qt Code:
    1. QwtPlot *plot=new QwtPlot();
    2. plot->setTitle( "Plot Demo" );
    3. plot->setCanvasBackground( Qt::white );
    4. //plot->setAxisScale( QwtPlot::yLeft, 0.0, 20.0);
    5. // plot->setAxisScale(QwtPlot::xBottom, 0.0, 20.0);
    6. plot->insertLegend( new QwtLegend() );
    7. plot->setAxisTitle(QwtPlot::xBottom, "X Axis");
    8. plot->setAxisTitle(QwtPlot::yLeft, "Y Axis");
    9. QwtPlotGrid *grid = new QwtPlotGrid();
    10. grid->attach( plot );
    11. grid->setPen( Qt::gray, 0.0, Qt::DotLine );
    12.  
    13. QwtPlotCurve *curve = new QwtPlotCurve();
    14. curve->setTitle( "Pixel Count" );
    15. curve->setPen(QPen(Qt::blue, 3,Qt::SolidLine) );
    16. curve->setRenderHint( QwtPlotItem::RenderAntialiased, true);
    17. QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,QBrush(Qt::yellow),QPen(Qt::red,2),QSize(8,8));
    18. curve->setSymbol(symbol );
    19.  
    20. QPolygonF points;
    21. curve->setSamples( points );
    22. curve->attach( plot );
    23.  
    24. QList<QByteArray> read_data;
    25. b1= process->readAllStandardOutput();
    26. read_data=b1.split('\n');
    27. int i=0, n=read_data.count();
    28. float y[100000];
    29.  
    30. for( i=0; i<n; i++ )
    31. {
    32. QString result=read_data.at(i);
    33. result.remove(0,10);
    34. qDebug("%s",result.toLatin1().data());
    35. y[i]=result.toLatin1().toFloat();
    36. points << QPointF( i, y[i] );
    37. // check wether curve is updated, if not do it here
    38. plot->replot;
    39. plot->show;
    40. }
    To copy to clipboard, switch view to plain text mode 

    Qwt is not installed on the computer I am writing from, so I cannot check my code, but the solution might not be far from here.

    Just a remark. Converting a byte array to a string and then to a float is heavy, there should be a better way to do that. How fast is your data input rate?

  3. #3
    Join Date
    Jul 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Plot the graph for real time data

    Thank you for your reply,


    Data input rate -the data is reading from c.exe file with in fraction of seconds I am able to read 40000 values.

    I have doubt for your solution,

    you place this code in loop

    plot->replot;
    plot->show;
    so for every "i" value these to lines will be called so there is a possible to "i" no of windows are opened.

    and my requirement is plotting the graph continuously.

    for example if we take a sin wave form, it will gives the continuos graph. Like that I want continuous graph with those values

  4. #4
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Plot the graph for real time data

    sorry, but I do not undestand what you are meaning.
    In my example, the plot is actualized in real time, and plotted continuously. The question is just wether your hardware is fast enough to do that.
    You might define what you mean with real time.
    The strong interpretation is that you are able to display the data one by one as they are available, continuously without any data loss.
    A weaker interpretation of real time ist that you are displaying the new data in blocks of several data, but still without data loss.

  5. #5
    Join Date
    Jul 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Plot the graph for real time data

    sorry for my explanation. for the question
    The question is just wether your hardware is fast enough to do that.
    It is able to do as fast as much.

    for( i=0; i<n; i++ )
    {
    QString result=read_data.at(i);
    result.remove(0,10);
    qDebug("%s",result.toLatin1().data());
    y[i]=result.toLatin1().toFloat();
    points << QPointF( i, y[i] );
    // check wether curve is updated, if not do it here
    plot->replot;
    plot->show;
    }
    It is the part of your code. In this you write the code

    plot->replot;
    plot->show;

    in for loop.
    My question is, loop will execute for every i value till it reaches the count.

    the code in the loop will excute for every "i" value so plot->show is also excute for every value.

    means it will open the plot window for every i value.

    I have tested your code, it will displaying so many no of windows

    how to modify that code to plot the graph continuously.

  6. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,277
    Thanked 871 Times in 819 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Plot the graph for real time data

    2 considerations, when implementing a realtime plot - especially when you expect to have many, many samples:


    • always decouple sample and refresh rate
      decoupling the rates means, that you only add new samples to a buffer without repainting. The curve update depends on a timer that runs periodically triggering a replot only when something has changed in the last cycle.
    • consider to use incremental painting
      incremental painting means, that you design your plot in a way, that it doesn't change for new samples - beside of drawing a couple of new lines ( the oscilloscope example is designed this way ). But this is only possible when you don't have to change the scales.


    Nore that the oscilloscope example runs with heavy data and high refresh rates with low CPU usage - even on a device like the PI !

    Uwe

  7. The following user says thank you to Uwe for this useful post:

    Momergil (27th June 2014)

  8. #7
    Join Date
    Jul 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Plot the graph for real time data

    consider to use incremental painting
    incremental painting means, that you design your plot in a way, that it doesn't change for new samples - beside of drawing a couple of new lines ( the oscilloscope example is designed this way ). But this is only possible when you don't have to change the scales.
    Sorry I am a bit confused,

    UWE said that
    "incremental painting means, that you design your plot in a way, that it doesn't change for new samples"
    that it doesn't change for new samples means every time It shows only previous samples only?
    can you explain me

  9. #8
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,277
    Thanked 871 Times in 819 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Plot the graph for real time data

    Incremental in this context means that only the new parts of a curve are painted on top of the existing plot - without repainting from scratch.

    Uwe

  10. #9
    Join Date
    Jul 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Plot the graph for real time data

    thank you for reply,

    in that Oscilloscope example there are so many .pri files were added. these files are needed or not???\

  11. #10
    Join Date
    Jul 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Plot the graph for real time data

    UWE said that

    the new parts of a curve are painted on top of the existing plot,,

    means only top values are plotted?

  12. #11
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,277
    Thanked 871 Times in 819 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Plot the graph for real time data

    With QwtPlotDirectPainter you can draw any part of a curve on top of an existing plot - without repainting the plot. It is up to the application code which part you want to draw - but usually these are points, that are not painted already ( = new ).

    Uwe

  13. #12
    Join Date
    Jul 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Plot the graph for real time data

    I want each and every value from starting to ending. how it is possible? can you explain me

  14. #13
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,277
    Thanked 871 Times in 819 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Plot the graph for real time data

    QwtPlot::replot().

    Uwe

  15. #14
    Join Date
    Jul 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Plot the graph for real time data

    I want to clarify one doubt,

    Can I use oscilloscope example as reference to my requirement?

Similar Threads

  1. Replies: 3
    Last Post: 12th April 2013, 06:18
  2. Replies: 2
    Last Post: 5th April 2013, 04:58
  3. How to plot real time x/y graph in Qtopia
    By desperado_43 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 18th May 2012, 08:00
  4. Replies: 2
    Last Post: 24th June 2011, 05:31
  5. real time graph plot
    By robotics in forum Qt Programming
    Replies: 5
    Last Post: 24th May 2011, 05:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.