Results 1 to 10 of 10

Thread: How to create QWT plot curve with gaps?

  1. #1
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to create QWT plot curve with gaps?

    I need to draw a line curve, which has "gaps" (or "holes").
    Yes, I can create several plot curves, which won't be connected each other, but is there a more elegant solution?
    Have I to override QwtPlotCurve?

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: How to create QWT plot curve with gaps?

    I never used qwt, but i did this with opengl in the following way:

    - wrote a function to find the zeros of the function (note that the gaps of f(x) are the zeros of 1/f(x))
    - create a class with a array of x and y points to store the graph values, and a min and max value (the limits of the segment)
    -create a array of that class (each element of the array will be a segment)
    - put the array in a for and draw the graph

    So I guess there is no elegant way, just use the segments, and if you find another away let us know

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

    kiriM (16th June 2009)

  4. #3
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to create QWT plot curve with gaps?

    Quote Originally Posted by kiriM View Post
    Have I to override QwtPlotCurve?
    Yes, QwtPlotCurve::drawLines().

    If you don't need any optimizations its simply iterating over the points, translating ( QwtScaleMap ) them into pixel positions and painting them.

    Uwe

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

    kiriM (16th June 2009)

  6. #4
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create QWT plot curve with gaps?

    Thanks, guys!
    I'll override drawLines().
    But it's desirable to implement this feature in future versions of QwtPlot )
    I'm surprised that nobody in this forum didn't run into this problem. I think it looks ugly when plot in missed points has a dip to X axis (when missed Y value is zero, for example).

  7. #5
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Wink Re: How to create QWT plot curve with gaps?

    OK, I've created a QwtPlotCurve descendant, QwtPlotGappedCurve, with
    virtual void draw(QPainter *p, const QwtScaleMap &xMap,
    const QwtScaleMap &yMap, int from, int to) const
    overridden and additional data member, gapValue.

    Everybody interested in can see attachment.

    Uwe, maybe it will be useful to include this class into Qwt package?
    Attached Files Attached Files

  8. The following 2 users say thank you to kiriM for this useful post:

    Fractal (10th February 2012), jesse_mark (5th February 2013)

  9. #6
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create QWT plot curve with gaps?

    Thanks, KiriM.

    I had to implement some tweaks to get this to work in qwt-6.0.0 because the draw() function is different in 6.0.0.

    I overrode QwtPlotCurve::drawSeries(), included the QRectF of the canvas in it's constructor, and used the d_deries->samples(i).y() data-values in the comparison to "gapValue_"
    Qt Code:
    1. ////////////////////////////////////////////////////////////////////////////////
    2. void QwtPlotGappedCurve::drawSeries(QPainter *painter, const QwtScaleMap &xMap,
    3. const QwtScaleMap &yMap, const QRectF &canvRect, int from, int to) const
    4. {
    5. if ( !painter || dataSize() <= 0 )
    6. return;
    7.  
    8. if (to < 0)
    9. to = dataSize() - 1;
    10.  
    11. int i = from;
    12. while (i < to)
    13. {
    14. // If data begins with missed values,
    15. // we need to find first non-missed point.
    16. double y = d_series->sample(i).y();
    17. while ((i < to) && (y == gapValue_))
    18. {
    19. ++i;
    20. y = d_series->sample(i).y();
    21. }
    22. // First non-missed point will be the start of curve section.
    23. int start = i;
    24. y = d_series->sample(i).y();
    25. // Find the last non-missed point, it will be the end of curve section.
    26. while ((i < to) && (y != gapValue_))
    27. {
    28. ++i;
    29. y = d_series->sample(i).y();
    30. }
    31. // Correct the end of the section if it is at missed point
    32. int end = (y == gapValue_) ? i - 1 : i;
    33.  
    34. // Draw the curve section
    35. if (start <= end)
    36. QwtPlotCurve::drawSeries(painter, xMap, yMap, canvRect, start, end);
    37. }
    38. }
    39.  
    40. ////////////////////////////////////////////////////////////////////////////////
    To copy to clipboard, switch view to plain text mode 

  10. #7
    Join Date
    Dec 2008
    Location
    California, USA
    Posts
    18
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create QWT plot curve with gaps?

    Thank you Fractal and KiriM. This subclass works great with Qwt6.0.2.

  11. #8
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create QWT plot curve with gaps?

    Hello!

    I'm trying to make this subclass work in my Qt 5.1.0 + Qwt 6.1.0 project, but without success. I added the change suggested by Fractal above, but the compiler gives me the error that "d_series" is private.

    Any guess how to correct this?

    Thanks,

    Momergil

  12. #9
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create QWT plot curve with gaps?

    Hello!

    Since no one managed to help me, I created a new class able to do this in a different way:

    Qt Code:
    1. //.h
    2. #define QWTPLOTCURVESPECIAL_DEFAULT -1
    3.  
    4. class QwtPlotCurveSpecial : public QwtPlotCurve
    5. {
    6. public:
    7. QwtPlotCurveSpecial(const QString &title = QString::null );
    8.  
    9. void setPenList(QList<QPen>& pen);
    10. void setPosList(QList<char>& pos);
    11. virtual void drawLines(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const;
    12.  
    13. private:
    14. QList<QPen> v_pen;
    15. QList<char> v_pos;
    16. };
    17.  
    18. //cpp
    19. QwtPlotCurveSpecial::QwtPlotCurveSpecial(const QString& title)
    20. {
    21. setTitle(title);
    22. }
    23.  
    24. void QwtPlotCurveSpecial::setPenList(QList<QPen>& pen)
    25. {
    26. v_pen = pen;
    27. }
    28.  
    29. void QwtPlotCurveSpecial::setPosList(QList<char>& pos)
    30. {
    31. v_pos = pos;
    32. }
    33.  
    34. void QwtPlotCurveSpecial::drawLines(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const
    35. {
    36. for (int counter = 1; counter < v_pos.size(); counter++)
    37. {
    38. if (v_pos.at(counter) == -1)
    39. continue;
    40.  
    41. p->setPen(v_pen[v_pos[counter]]);
    42.  
    43. QwtPlotCurve::drawLines(p,xMap,yMap,canvasRect,counter-1,counter);
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, in order to use this class, one have to crate a QList<QPen> with all the pen he want to use in the draw (useful for another situation, which is when somebody wants to draw a line with multiple colors) and a QList<char> containing which of the QPen he want to be used for each point. If hits QList<char> contains a -1 value, this is interpreted as not wanting to draw that point, so nothing is draw in that point.

    Notice that its required that the QList<char> list has the same value as the samples set in setSamples(...), otherwise one would have a failure there.


    Momergil

  13. The following user says thank you to Momergil for this useful post:

    another_qt (26th July 2016)

  14. #10
    Join Date
    Nov 2008
    Posts
    21
    Thanked 1 Time in 1 Post

    Default Re: How to create QWT plot curve with gaps?

    Hi,

    I'm using QWT 6.1.x.

    just had the same problem and used KiriM's solution patched with Fractal's code and had the same errors as Momergil ("d_series" is private). I found out that by just replacing 'd_series->sample' with 'sample' it works.
    If anyone needs a working solution with QWT >= 6.1.x, please use the attached files for the QwtPlotGappedCurve.

    Cheers and thanks to kiriM and Fractal

    Edit:

    Since the value 0.0 is a valid value in my case I needed another value that marks a gap. The double value NAN is not possible because NAN != NAN. Since I'm not a fan of curious magic numbers I simply use 1.0/0.0 (inf) as the gap value
    Attached Files Attached Files

  15. The following user says thank you to osiris81 for this useful post:

    Momergil (14th May 2014)

Similar Threads

  1. Embedding Qwt plot High Resolution
    By giusepped in forum Qwt
    Replies: 7
    Last Post: 21st April 2009, 09:44
  2. legend inside qwt plot
    By kaustav98255 in forum Qwt
    Replies: 5
    Last Post: 11th February 2009, 20:39
  3. Detecting changes in the qwt plot axis
    By mrcolin in forum Qwt
    Replies: 2
    Last Post: 27th January 2009, 12:53
  4. Replies: 4
    Last Post: 9th January 2009, 11:57
  5. export and printing qwt plot
    By giusepped in forum Qwt
    Replies: 6
    Last Post: 17th December 2008, 07: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.