Results 1 to 4 of 4

Thread: plot canvas coodinates

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

    Default plot canvas coodinates

    plot canvas coodinates

    First I want to state, that I am using qwt 6.0.1 and cannot upgrade to 6.1.2 at present time.

    To be sure we have the same understanding of the definition of the canvas, for me the canvas is the area where the grid and the data to plot are drawn, nothing more.

    Now to my problem.
    I want to draw a vertical (horizontal) color band to mark an area of interest into the plot - see picture attached -. To do that, I draw a line centered at the arithmetic middle of the band in case of a linear plot, centered at the geometric middle in case of a logarithmic plot. I also calculate the width of the pen to spread over the whole area of the band, then I draw the vertical (horizontal) line with the attribute described before.

    Since the width of the pen has to be given in pixels, I must convert the width of the band from physical coordinates into pixel (paint) coordinates.
    To do that I have tried to use both methods
    Qt Code:
    1. canvas()->contentsRect(), plotLayout()->canvasMargin()
    2. canvasMap().
    To copy to clipboard, switch view to plain text mode 
    - see code below -
    Both methods lead almost to the same result - there is a difference of one pixel between the two! - see debugger output below -, but neither of the 2 methods are giving the right width in pixels as can be seen in the 2 plots.
    It seems that the legend and also another offset is integrated in the result of the methods
    canvas()->contentsRect() and canvasMap().

    Any idea how to get around this issue?

    Thank you for your valuable time
    Alain

    debugger output:

    contentsRect.width() = 527 margin left = 4 margin right = 4 xSpanPixels = 519
    xMap.p1()= 6 xMap.p2()= 524 xMap.pDist()= 518
    LIN: xMin = 0 xMax = 10 lowerValue = 5 upperValue = 8 x = 6.5 widthPixels = 155.4
    contentsRect.width() = 527 margin left = 4 margin right = 4 xSpanPixels = 519
    xMap.p1()= 6 xMap.p2()= 524 xMap.pDist()= 518
    LIN: xMin = 0 xMax = 10 lowerValue = 5 upperValue = 8 x = 6.5 widthPixels = 155.4
    The program '[584] SigLibGraph.exe: Native' has exited with code 0 (0x0).

    source code:

    Qt Code:
    1. void Plot2D::setVerticalColorBand( double lowerValue, double upperValue
    2. , QColor color, QString bandLabel, double transparency )
    3. {
    4. // check parameters
    5. if ( lowerValue > upperValue )
    6. qSwap( lowerValue, upperValue );
    7.  
    8. // get axis extend in pixels
    9. updateAxes();
    10.  
    11. // just for test purpose
    12. QRect rect = this->canvas()->contentsRect();
    13. double marginLeft = double( this->plotLayout()->canvasMargin( QwtPlot::yLeft ));
    14. double marginRight = double( this->plotLayout()->canvasMargin( QwtPlot::yRight ));
    15. double xSpanPixels = rect.width() - marginLeft - marginRight;
    16. qDebug() << "contentsRect.width() =" << rect.width() << " margin left ="
    17. << marginLeft << " margin right =" << marginRight << " xSpanPixels =" << xSpanPixels;
    18. // end of test
    19.  
    20. QwtScaleMap xMap = canvasMap( QwtPlot::xBottom );
    21. /*double*/xSpanPixels = xMap.pDist();
    22. qDebug() << "xMap.p1()=" << xMap.p1() << " xMap.p2()=" << xMap.p2() << " xMap.pDist()=" << xMap.pDist();
    23.  
    24. // get axis extend in physical coordinates
    25. double xMin = axisScaleDiv( QwtPlot::xBottom )->lowerBound();
    26. double xMax = axisScaleDiv( QwtPlot::xBottom )->upperBound();
    27. double yMin = axisScaleDiv( QwtPlot::yLeft )->lowerBound();
    28. double yMax = axisScaleDiv( QwtPlot::yLeft )->upperBound();
    29.  
    30. // get color band line attributes (abcissa, width)
    31. double x, widthPixels;
    32. if ( axisScaleEngine(xBottom)->transformation()->type() == QwtScaleTransformation::Log10 ) { // log axis
    33. double xLowerPixels = xSpanPixels * (log10(lowerValue) - log10(xMin)) / (log10(xMax) - log10(xMin));
    34. double xUpperPixels = xSpanPixels * (log10(upperValue) - log10(xMin)) / (log10(xMax) - log10(xMin));
    35. widthPixels = xUpperPixels - xLowerPixels;
    36. double xPixels = (xUpperPixels + xLowerPixels) * 0.5;
    37. x = pow( 10., (log10(xMax) - log10(xMin)) * xPixels / xSpanPixels + log10(xMin) );
    38. qDebug() << "LOG: xMin =" << xMin << " xMax =" << xMax << " lowerValue =" << lowerValue << " upperValue =" << upperValue << " x =" << x << " widthPixels =" << widthPixels;
    39. }
    40. else { // lin axis, other
    41. x = ( lowerValue + upperValue ) * 0.5;
    42. widthPixels = xSpanPixels / ( xMax - xMin ) * ( upperValue - lowerValue );
    43. qDebug() << "LIN: xMin =" << xMin << " xMax =" << xMax << " lowerValue =" << lowerValue << " upperValue =" << upperValue << " x =" << x << " widthPixels =" << widthPixels;
    44. }
    45.  
    46. // draw color band
    47. double xData[2] = { x, x };
    48. double yData[2] = { yMin, yMax };
    49.  
    50. QwtPlotCurve* curve = new QwtPlotCurve;
    51. curve->setSamples( xData, yData, 2 ); //vertical line from (xMin,y) to (xMax,y)
    52. curve->setTitle( bandLabel );
    53.  
    54. QPen curvePen;
    55. color.setAlphaF( transparency ); // we want to see the underlying grid
    56. curvePen.setColor( color ); // must be located after setAlphaF()!
    57. curvePen.setWidthF( widthPixels );
    58. //curvePen.setCapStyle( Qt::FlatCap );
    59. curve->setPen( curvePen );
    60.  
    61. if ( bandLabel.isEmpty() )
    62. curve->setItemAttribute( QwtPlotItem::Legend, false );
    63.  
    64. curve->attach( this );
    65. }
    To copy to clipboard, switch view to plain text mode 

    setVerticalColor.jpg

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

    Default Re: plot canvas coodinates

    Take the code of QwtPlotZoneItem from Qwt 6.1 ( should be easy to be backported to Qwt 6.0 - if it doesn't work out of the box ) and use it as it is.

    Uwe

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

    Thumbs up Re: plot canvas coodinates

    thank you for the prompt reply, Uwe!

    I will give it a try, seems easy to implement.

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

    Default Re: plot canvas coodinates

    Done!

    Here the code, should somebody be interested in adding a zoneItem with qwt 6.0.X
    You can see the result in the attached picture.
    I have also attached the modified files qwt_plot_zoneitem_60.h and qwt_plot_zoneitem_60.cpp

    Qt Code:
    1. void Plot2D::setColorBand( Qt::Orientation orientation // Qt::Vertical ::Horizontal
    2. , double lowerValue, double upperValue
    3. , QColor color, QString bandLabel
    4. , double transparency ) // 0: fully transparent, 1: fully opaque
    5. {
    6. // use modified files 'qwt_plot_zoneitem.h' and
    7. // 'qwt_plot_zoneitem.cpp' from qwt 6.1.2 as follows:
    8. // 'qwt_plot_zoneitem.h':
    9. // # change
    10. // class QWT_EXPORT QwtPlotZoneItem:public QwtPlotItem to
    11. // class QwtPlotZoneItem:public QwtPlotItem
    12. // # comment out
    13. // virtual int rtti() const;
    14. // # add
    15. // void drawLegendIdentifier( QPainter *, const QRectF & ) const;
    16. // 'qwt_plot_zoneitem.cpp':
    17. // # comment out
    18. // int QwtPlotZoneItem::rtti() const { return QwtPlotItem::Rtti_PlotZone; }
    19. // # add function
    20. // void QwtPlotZoneItem::drawLegendIdentifier(
    21. // QPainter *painter, const QRectF &rect ) const
    22. // {
    23. // const double dim = qMin( rect.width(), rect.height() );
    24. // QRectF r( 0, 0, dim, dim );
    25. // r.moveCenter( rect.center() );
    26. // painter->fillRect( r, d_data->brush );
    27. // }
    28.  
    29. // check parameters
    30. if ( lowerValue > upperValue )
    31. qSwap( lowerValue, upperValue );
    32.  
    33. QwtPlotZoneItem* colorBand = new QwtPlotZoneItem;
    34.  
    35. colorBand->setTitle( bandLabel ); // we overwrite the default label 'zone'
    36. if ( !bandLabel.isEmpty() )
    37. colorBand->setItemAttribute( QwtPlotItem::Legend, true ); // default: false
    38.  
    39. colorBand->setOrientation( orientation );
    40.  
    41. colorBand->setInterval( lowerValue, upperValue );
    42.  
    43. color.setAlphaF( transparency );
    44. QBrush brush( color );
    45. colorBand->setBrush( brush );
    46.  
    47. colorBand->attach( this );
    48. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Attached Files Attached Files

Similar Threads

  1. How to plot scale on the canvas
    By baoxuefei771 in forum Qwt
    Replies: 2
    Last Post: 29th May 2013, 19:59
  2. Replies: 1
    Last Post: 17th May 2013, 07:10
  3. Replies: 1
    Last Post: 17th April 2012, 10:10
  4. How to set margin on one side of canvas plot (QwtPlot)
    By balvinder in forum Qt Programming
    Replies: 1
    Last Post: 13th February 2012, 16:15
  5. QwtPlotZoomer zooming off the plot canvas
    By BettaUseYoNikes in forum Qwt
    Replies: 1
    Last Post: 26th June 2011, 11: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.