canvas()->contentsRect(), plotLayout()->canvasMargin()
canvasMap().
canvas()->contentsRect(), plotLayout()->canvasMargin()
canvasMap().
To copy to clipboard, switch view to plain text mode
- see code below -
void Plot2D::setVerticalColorBand( double lowerValue, double upperValue
{
// check parameters
if ( lowerValue > upperValue )
qSwap( lowerValue, upperValue );
// get axis extend in pixels
updateAxes();
// just for test purpose
QRect rect
= this
->canvas
()->contentsRect
();
double marginLeft
= double( this
->plotLayout
()->canvasMargin
( QwtPlot::yLeft ));
double marginRight
= double( this
->plotLayout
()->canvasMargin
( QwtPlot::yRight ));
double xSpanPixels = rect.width() - marginLeft - marginRight;
qDebug() << "contentsRect.width() =" << rect.width() << " margin left ="
<< marginLeft << " margin right =" << marginRight << " xSpanPixels =" << xSpanPixels;
// end of test
/*double*/xSpanPixels = xMap.pDist();
qDebug() << "xMap.p1()=" << xMap.p1() << " xMap.p2()=" << xMap.p2() << " xMap.pDist()=" << xMap.pDist();
// get axis extend in physical coordinates
double xMin
= axisScaleDiv
( QwtPlot::xBottom )->lowerBound
();
double xMax
= axisScaleDiv
( QwtPlot::xBottom )->upperBound
();
double yMin
= axisScaleDiv
( QwtPlot::yLeft )->lowerBound
();
double yMax
= axisScaleDiv
( QwtPlot::yLeft )->upperBound
();
// get color band line attributes (abcissa, width)
double x, widthPixels;
double xLowerPixels = xSpanPixels * (log10(lowerValue) - log10(xMin)) / (log10(xMax) - log10(xMin));
double xUpperPixels = xSpanPixels * (log10(upperValue) - log10(xMin)) / (log10(xMax) - log10(xMin));
widthPixels = xUpperPixels - xLowerPixels;
double xPixels = (xUpperPixels + xLowerPixels) * 0.5;
x = pow( 10., (log10(xMax) - log10(xMin)) * xPixels / xSpanPixels + log10(xMin) );
qDebug() << "LOG: xMin =" << xMin << " xMax =" << xMax << " lowerValue =" << lowerValue << " upperValue =" << upperValue << " x =" << x << " widthPixels =" << widthPixels;
}
else { // lin axis, other
x = ( lowerValue + upperValue ) * 0.5;
widthPixels = xSpanPixels / ( xMax - xMin ) * ( upperValue - lowerValue );
qDebug() << "LIN: xMin =" << xMin << " xMax =" << xMax << " lowerValue =" << lowerValue << " upperValue =" << upperValue << " x =" << x << " widthPixels =" << widthPixels;
}
// draw color band
double xData[2] = { x, x };
double yData[2] = { yMin, yMax };
curve->setSamples( xData, yData, 2 ); //vertical line from (xMin,y) to (xMax,y)
curve->setTitle( bandLabel );
color.setAlphaF( transparency ); // we want to see the underlying grid
curvePen.setColor( color ); // must be located after setAlphaF()!
curvePen.setWidthF( widthPixels );
//curvePen.setCapStyle( Qt::FlatCap );
curve->setPen( curvePen );
if ( bandLabel.isEmpty() )
curve->attach( this );
}
void Plot2D::setVerticalColorBand( double lowerValue, double upperValue
, QColor color, QString bandLabel, double transparency )
{
// check parameters
if ( lowerValue > upperValue )
qSwap( lowerValue, upperValue );
// get axis extend in pixels
updateAxes();
// just for test purpose
QRect rect = this->canvas()->contentsRect();
double marginLeft = double( this->plotLayout()->canvasMargin( QwtPlot::yLeft ));
double marginRight = double( this->plotLayout()->canvasMargin( QwtPlot::yRight ));
double xSpanPixels = rect.width() - marginLeft - marginRight;
qDebug() << "contentsRect.width() =" << rect.width() << " margin left ="
<< marginLeft << " margin right =" << marginRight << " xSpanPixels =" << xSpanPixels;
// end of test
QwtScaleMap xMap = canvasMap( QwtPlot::xBottom );
/*double*/xSpanPixels = xMap.pDist();
qDebug() << "xMap.p1()=" << xMap.p1() << " xMap.p2()=" << xMap.p2() << " xMap.pDist()=" << xMap.pDist();
// get axis extend in physical coordinates
double xMin = axisScaleDiv( QwtPlot::xBottom )->lowerBound();
double xMax = axisScaleDiv( QwtPlot::xBottom )->upperBound();
double yMin = axisScaleDiv( QwtPlot::yLeft )->lowerBound();
double yMax = axisScaleDiv( QwtPlot::yLeft )->upperBound();
// get color band line attributes (abcissa, width)
double x, widthPixels;
if ( axisScaleEngine(xBottom)->transformation()->type() == QwtScaleTransformation::Log10 ) { // log axis
double xLowerPixels = xSpanPixels * (log10(lowerValue) - log10(xMin)) / (log10(xMax) - log10(xMin));
double xUpperPixels = xSpanPixels * (log10(upperValue) - log10(xMin)) / (log10(xMax) - log10(xMin));
widthPixels = xUpperPixels - xLowerPixels;
double xPixels = (xUpperPixels + xLowerPixels) * 0.5;
x = pow( 10., (log10(xMax) - log10(xMin)) * xPixels / xSpanPixels + log10(xMin) );
qDebug() << "LOG: xMin =" << xMin << " xMax =" << xMax << " lowerValue =" << lowerValue << " upperValue =" << upperValue << " x =" << x << " widthPixels =" << widthPixels;
}
else { // lin axis, other
x = ( lowerValue + upperValue ) * 0.5;
widthPixels = xSpanPixels / ( xMax - xMin ) * ( upperValue - lowerValue );
qDebug() << "LIN: xMin =" << xMin << " xMax =" << xMax << " lowerValue =" << lowerValue << " upperValue =" << upperValue << " x =" << x << " widthPixels =" << widthPixels;
}
// draw color band
double xData[2] = { x, x };
double yData[2] = { yMin, yMax };
QwtPlotCurve* curve = new QwtPlotCurve;
curve->setSamples( xData, yData, 2 ); //vertical line from (xMin,y) to (xMax,y)
curve->setTitle( bandLabel );
QPen curvePen;
color.setAlphaF( transparency ); // we want to see the underlying grid
curvePen.setColor( color ); // must be located after setAlphaF()!
curvePen.setWidthF( widthPixels );
//curvePen.setCapStyle( Qt::FlatCap );
curve->setPen( curvePen );
if ( bandLabel.isEmpty() )
curve->setItemAttribute( QwtPlotItem::Legend, false );
curve->attach( this );
}
To copy to clipboard, switch view to plain text mode
Bookmarks