Results 1 to 9 of 9

Thread: use factor for displaying y values

  1. #1
    Join Date
    Jun 2012
    Posts
    63
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default use factor for displaying y values

    Is it possible to use or define a factor that would be used to determine sample array points y-coordinate ?

    For example I am now using this code to modify each points Y value accordint to a factor.

    Qt Code:
    1. void CustomQwtCurve::setYFactor(const qreal &value)
    2. {
    3. if (value==0) return;
    4. qDebug() << " current factor: " << y_factor;
    5. qreal newfactor=value/y_factor;
    6. y_factor=value;
    7. qDebug() << " factor to multiply current points: " << newfactor;
    8. qDebug() << " new y_factor: " << y_factor;
    9.  
    10. for (int i=0; i<points_x.count(); i++) {
    11. qreal px=points_x.at(i);
    12. qreal py=points_y.at(i);
    13. py=py * newfactor;
    14. points_y[i]=py;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Problem is this is very slow approach as I might have 100 000 or even more samples in an array.

    Would it be possible to define a factor (where ?) that would be used only when drawing points so that I would not have to modify the array every time ?


    For drawing I use:

    QwtPlotDirectPainter->drawSeries( curve, curve->getLastX()-2, curve->pointsCount() - 1 );

    'curve' is own class derived from QwtPlotCurve

  2. #2
    Join Date
    Jun 2012
    Posts
    63
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: use factor for displaying y values

    Also I can't override drawSeries in my own curve class

    Qt Code:
    1. void QwtPlotCurve::drawSeries( QPainter *painter,
    2. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    3. const QRectF &canvasRect, int from, int to ) const
    To copy to clipboard, switch view to plain text mode 

    because that function accesses private data variable: d_data


    Added after 20 minutes:


    Ok, I can override QwtPlotCurve::drawCurve function.

    I can see that it uses QwtScaleMap. I would guess by using this class I can set my own factor for sample point's y-coordinate value. But the problem is I don't know how to use it.

    There is QwtScaleMap::updateFactor which calculates conversionfactor: d_cnv.

    Help is appreciated.
    Last edited by phenoboy; 11th November 2014 at 07:19.

  3. #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: use factor for displaying y values

    Curve and points are connected by a class derived from QwtSeriesData<QPointF>.
    You can simply overload the sample() ( and depending on your code the boundingRect() ) method including the factor there.

    By the way: I'm working on an aggressive point weeding filter for curves lines ( FilterPointsAggressive ), that significantly improves the performance in situations, where you throw trillions of points to a curve. Unfortunately there is a nasty bug in the raster paint engine of Qt in all versions since Qt 4.8, that becomes very dominant in this mode. Hope I have a workaround soon.

    Uwe

  4. #4
    Join Date
    Jun 2012
    Posts
    63
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: use factor for displaying y values

    Thanks!

    It seems 'sample' does not get called with the following code.

    I have now this:

    .h
    Qt Code:
    1. class CustomQwtCurve : public QwtPlotCurve
    2. {
    3.  
    4. public:
    5.  
    6. CustomQwtCurve();
    7. void appendPoint(qreal x, qreal y);
    8. virtual QPointF sample(int) const;
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 

    .cpp:

    Qt Code:
    1. QPointF CustomQwtCurve::sample(int index) const
    2. {
    3. QPointF p=QwtSeriesStore<QPointF>::sample(index);
    4. qDebug() << "sample: x:" << p.x() << ",y:" << p.y();
    5. p.setY(factorYj*p.y());
    6. return p;
    7. }
    To copy to clipboard, switch view to plain text mode 

    I need to take a look why.. does the code above look ok ?


    Added after 1 12 minutes:


    Ah, it seems QwtSeriesStore<QPointF>::sample is called when drawing and not QwtPlotCurve->sample. Looks like I need to provide derived class from QwtSeriesStore<QPointF> and take that into use in QwtPlotCurve. Need to figure out how to do that because QwtSeriesStore is inherited in QwtPlotCurve class.
    Last edited by phenoboy; 11th November 2014 at 12:42.

  5. #5
    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: use factor for displaying y values

    The only way to connect data with a curve is:

    Qt Code:
    1. QwtPlotCurve::setData( QwtSeriesData<QPointF> *series );
    To copy to clipboard, switch view to plain text mode 
    All other APIs are convenience only and sooner or later end in calling setData().

    So all you need to do is to derive from QwtSeriesData<QPointF> ( or one of its descendents ).

    Uwe

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

    phenoboy (11th November 2014)

  7. #6
    Join Date
    Jun 2012
    Posts
    63
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: use factor for displaying y values

    Perfect! Thanks for great support!

  8. #7
    Join Date
    Jun 2012
    Posts
    63
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: use factor for displaying y values

    Here's the code if someone else is interested also. If there's better/more efficient way, let me know !!

    header:

    Qt Code:
    1. #include <qwt_plot_curve.h>
    2.  
    3. class CustomQwtCurveData: public QwtSeriesData<QPointF>
    4. {
    5. public:
    6. CustomQwtCurveData();
    7. QPointF realSample( size_t index ) const;
    8. virtual QPointF sample( size_t index ) const;
    9. virtual size_t size() const;
    10. virtual QRectF boundingRect() const;
    11.  
    12. void appendPoint(qreal x, qreal y);
    13. quint64 pointsCount();
    14.  
    15. quint64 getLastX() const;
    16. void setLastX(const quint64 &value);
    17.  
    18. qreal getFactorY() const;
    19. void setFactorY(const qreal &value);
    20.  
    21. qreal getMaxYValue() const;
    22. void setMaxYValue(const qreal &value);
    23.  
    24. qreal getMinYValue() const;
    25. void setMinYValue(const qreal &value);
    26.  
    27. void clear();
    28.  
    29. QVector<double>& pointsX();
    30. QVector<double>& pointsY();
    31. QRectF _boundingRect;
    32.  
    33.  
    34.  
    35. protected:
    36. QVector<double> points_x;
    37. QVector<double> points_y;
    38. qreal factorY;
    39. qreal maxYValue;
    40. qreal minYValue;
    41. quint64 lastX;
    42. };
    To copy to clipboard, switch view to plain text mode 

    cpp:

    Qt Code:
    1. #include "customqwtcurve.h"
    2. #include <qwt_scale_map.h>
    3. #include <qwt_point_data.h>
    4.  
    5.  
    6. qreal CustomQwtCurveData::getMaxYValue() const
    7. {
    8. return maxYValue;
    9. }
    10.  
    11. void CustomQwtCurveData::setMaxYValue(const qreal &value)
    12. {
    13. maxYValue = value;
    14. }
    15.  
    16. QPointF CustomQwtCurveData::realSample(size_t index) const
    17. {
    18. QPointF p(points_x.value(index), points_y.value(index));
    19. return p;
    20. }
    21.  
    22. QPointF CustomQwtCurveData::sample(size_t index) const
    23. {
    24. QPointF p(points_x.value(index), points_y.value(index));
    25. p.setY(factorY*p.y());
    26. return p;
    27. }
    28.  
    29. size_t CustomQwtCurveData::size() const
    30. {
    31. return points_x.size();
    32. }
    33.  
    34. QRectF CustomQwtCurveData::boundingRect() const
    35. {
    36. // adjust the bounding rectangle
    37.  
    38. return _boundingRect;
    39. }
    40.  
    41. qreal CustomQwtCurveData::getFactorY() const
    42. {
    43. return factorY;
    44. }
    45.  
    46. void CustomQwtCurveData::setFactorY(const qreal &value)
    47. {
    48. factorY = value;
    49. }
    50.  
    51.  
    52. void CustomQwtCurveData::appendPoint(qreal x, qreal y)
    53. {
    54. if (y>maxYValue) {
    55. maxYValue=y;
    56. }
    57.  
    58. if (y<minYValue) {
    59. minYValue=y;
    60. }
    61.  
    62. points_x.append(x);
    63. points_y.append(y);
    64.  
    65. QPointF sample=QPointF(x,y);
    66.  
    67. if ( _boundingRect.width() < 0 || _boundingRect.height() < 0 )
    68. {
    69. _boundingRect.setRect( sample.x(), sample.y(), 0.0, 0.0 );
    70. }
    71. else
    72. {
    73. _boundingRect.setRight( sample.x() );
    74.  
    75. if ( sample.y() > _boundingRect.bottom() )
    76. _boundingRect.setBottom( sample.y() );
    77.  
    78. qreal top=_boundingRect.top();
    79. qreal bottom=_boundingRect.bottom();
    80. if ( sample.y() < _boundingRect.top() )
    81. _boundingRect.setTop( sample.y() );
    82. }
    83. }
    84.  
    85.  
    86. quint64 CustomQwtCurveData::pointsCount() {
    87. return points_x.size();
    88. }
    89.  
    90. void CustomQwtCurveData::clear() {
    91. points_x.clear();
    92. points_y.clear();
    93. }
    94.  
    95. QVector<double> &CustomQwtCurveData::pointsX()
    96. {
    97. return points_x;
    98. }
    99.  
    100. QVector<double> &CustomQwtCurveData::pointsY()
    101. {
    102. return points_y;
    103. }
    104. qreal CustomQwtCurveData::getMinYValue() const
    105. {
    106. return minYValue;
    107. }
    108.  
    109. void CustomQwtCurveData::setMinYValue(const qreal &value)
    110. {
    111. minYValue = value;
    112. }
    113.  
    114. CustomQwtCurveData::CustomQwtCurveData()
    115. {
    116. factorY=1;
    117. maxYValue=0;
    118. minYValue=0;
    119. points_x.reserve(5000);
    120. points_y.reserve(5000);
    121. }
    122.  
    123. quint64 CustomQwtCurveData::getLastX() const
    124. {
    125. return lastX;
    126. }
    127.  
    128. void CustomQwtCurveData::setLastX(const quint64 &value)
    129. {
    130. lastX = value;
    131. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: use factor for displaying y values

    Hello phenoboy,

    many thanks for your mail and support. With that i was able to create a subclass that
    is very similar to your CustomQwtCurveData.

    How did you use it?
    I guess you create an object in header file from your CustomQwtCurveData and use
    CustomQwtCurveData::appendPoint(qreal x, qreal y) to add a single value (x,y) and
    QwtPlotCurve::setSamples(CustomQwtCurveData *). Is that right?

    Thx
    Stefan

  10. #9
    Join Date
    Jun 2012
    Posts
    63
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: use factor for displaying y values

    Quote Originally Posted by HappyCoder View Post
    Hello phenoboy,

    many thanks for your mail and support. With that i was able to create a subclass that
    is very similar to your CustomQwtCurveData.

    How did you use it?
    I guess you create an object in header file from your CustomQwtCurveData and use
    CustomQwtCurveData::appendPoint(qreal x, qreal y) to add a single value (x,y) and
    QwtPlotCurve::setSamples(CustomQwtCurveData *). Is that right?

    Thx
    Stefan
    Sorry for late answer. Yes, you're correct. That is how I use it.

Similar Threads

  1. Qt Designer Layout and stretch factor
    By folibis in forum Qt Tools
    Replies: 2
    Last Post: 26th April 2012, 22:53
  2. Displaying integer values into textbox
    By deck99 in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2011, 13:07
  3. Item delegate displaying enumerated values
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2010, 16:31
  4. Replies: 3
    Last Post: 2nd February 2009, 23:40
  5. Replies: 1
    Last Post: 25th July 2008, 08:38

Tags for this Thread

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.