Results 1 to 3 of 3

Thread: How to plot discontinuous curves in Qwt ?

  1. #1
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Question How to plot discontinuous curves in Qwt ?

    Hi everyone,

    I just have to plot a curve which can be discontinuous over some intervals, but I can't find the way to do this.

    My samples are set with the setRawSamples(const double *xData, const double *yData, int size) function of QwtPlotCurve.

    To let a gap in Qwt curves I thought about 3 solutions:

    1) Paint the empty intervals with segments colored in white/transparent (declare another data array specifying the color of each sample - but I believe multiple colors in one curve are not implemented in Qwt yet)

    2) As all my curves are positive, I can set for example a negative value to yData[i] to tell the draw function that the sample should'nt be drawed and a gap has to be left (so I probably have to overload a Qwt function, and recompile and reinstall all the framework...)

    3) Set a value that is far out of the Y axis range, so I get a gap, but the invisible samples are joined to the visible ones and I get two vertical segments which are undesirable...


    Which solution is best to explore ? Or is there an easier solution to achieve this ?

    Thanks a lot
    Romain

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to plot discontinuous curves in Qwt ?

    You could reimplement draw() method in curve to draw a bunch of poly lines instead of one.
    You can create polylines from the curve data after setting samples on the curve.
    As long as the samples don't change, you don't have to re-create polylines.

    something like that:
    Qt Code:
    1. // this code is just to ilustrate the idea, it isn't correct nor compilable
    2. curve->setSamples( data_x, data_y );
    3. curve->prepare();
    4.  
    5. void MyCurve::prepare()
    6. {
    7. this->lines; // <QList< QVector< QPointF> >
    8.  
    9. QVector< QPointF > l;
    10. double some_threshold = 10.0; // how far the points have to be from each other to be considered non-continous
    11. for( int i = 0; i < this->dataSize(); ++i )
    12. {
    13. int prev = i -1;
    14. if( prev >= 0 && this->x( i ) - this->x( prev ) > some_treshold )
    15. {
    16. this->lines.append( l );
    17. l.clear();
    18. }
    19. l << QPointF( this->x( i ), this->y( i ) );
    20. }
    21. }
    22.  
    23. QVecror< QPointF > MyCurve::worldToScreen( const QVector< double >& points, const QwtScaleMap &xMap, const QwtScaleMap &yMap )
    24. {
    25. QVector< QPointF > screen;
    26. for( int i = 0; i < points.size(); ++i )
    27. {
    28. screen << QPointF( xMap.transform( line.x1() ), yMap.transform( line.y1() ) );
    29. }
    30. return screen;
    31. }
    32.  
    33. void MyCurve::draw( QPainter* p, const QwtScaleMap& xMap, const QwtScaleMap& yMap, const QRect& r ) const
    34. {
    35. p->save();
    36. p->setPen( this->pen() );
    37. p->setClipRect( r );
    38.  
    39. for( int i = 0; i < this->lines.size(); ++i )
    40. {
    41. p->drawPolyLine( this->worldToScreen( this->lines.at( i ), xMap, yMap ), this->lines.at( i ).size() );
    42. }
    43.  
    44. p->drawLines( v );
    45. p->restore();
    46.  
    47. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Spitfire for this useful post:

    motla (12th June 2012)

  4. #3
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Smile Re: How to plot discontinuous curves in Qwt ?

    Ok, so assuming you advised me to reimplement a function, I worked on my solution 2) to get it work, with some inspiration of your code.
    For my use I couldn't use the threshold method, so I preferred the "negative values = gap" method.
    Anyway it's finally easy to reimplement the drawCurve function of the QwtPlotCurve class, so I made this code and it works great:

    Qt Code:
    1. class MyQwtPlotCurve : public QwtPlotCurve
    2. {
    3. protected:
    4. virtual void drawCurve( QPainter *p, int style, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to ) const;
    5. };
    6.  
    7.  
    8.  
    9. curve = new MyQwtPlotCurve();
    10. curve->attach(this); // "this" is the QwtPlot
    11.  
    12.  
    13.  
    14. void MyQwtPlotCurve::drawCurve( QPainter *painter, int style, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to ) const{
    15.  
    16. int preceding_from = from;
    17. bool is_gap = true;
    18.  
    19. // Scan all data to identify gaps
    20. for (int i = from; i <= to; i++){
    21. const QPointF sample = d_series->sample(i);
    22.  
    23. // In a gap
    24. if(sample.y() >= 0 && is_gap){ // wait for the curve to be positive again
    25. preceding_from = i;
    26. is_gap = false;
    27. }
    28.  
    29. // At the beginning of a gap (or the end of the serie) : draw the preceding interval
    30. if((sample.y() < 0 && !is_gap) || (i == to && sample.y() >= 0)){
    31. drawSteps(painter, xMap, yMap, canvasRect, preceding_from, i>from ? i-1 : i); // or drawLines, drawSticks, drawDots
    32. is_gap = true;
    33. }
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 
    Thanks a lot for your help !

    Romain

Similar Threads

  1. Replies: 2
    Last Post: 29th February 2012, 10:39
  2. qwt several curves at a plot
    By freude3 in forum Qwt
    Replies: 4
    Last Post: 4th January 2011, 07:45
  3. Validating multiple discontinuous ranges
    By Nivek in forum Newbie
    Replies: 4
    Last Post: 19th August 2010, 20:26
  4. Replies: 1
    Last Post: 7th December 2009, 14:59
  5. discontinuous QTimeLine problem
    By yagabey in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2008, 21:20

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.