Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_plot_abstract_barchart.h"
00011 #include "qwt_scale_map.h"
00012
00013 static inline double qwtTransformWidth(
00014 const QwtScaleMap &map, double value, double width )
00015 {
00016 const double w2 = 0.5 * width;
00017
00018 const double v1 = map.transform( value - w2 );
00019 const double v2 = map.transform( value + w2 );
00020
00021 return qAbs( v2 - v1 );
00022 }
00023
00024 class QwtPlotAbstractBarChart::PrivateData
00025 {
00026 public:
00027 PrivateData():
00028 layoutPolicy( QwtPlotAbstractBarChart::AutoAdjustSamples ),
00029 layoutHint( 0.5 ),
00030 spacing( 10 ),
00031 margin( 5 ),
00032 baseline( 0.0 )
00033 {
00034 }
00035
00036 QwtPlotAbstractBarChart::LayoutPolicy layoutPolicy;
00037 double layoutHint;
00038 int spacing;
00039 int margin;
00040 double baseline;
00041 };
00042
00047 QwtPlotAbstractBarChart::QwtPlotAbstractBarChart( const QwtText &title ):
00048 QwtPlotSeriesItem( title )
00049 {
00050 d_data = new PrivateData;
00051
00052 setItemAttribute( QwtPlotItem::Legend, true );
00053 setItemAttribute( QwtPlotItem::AutoScale, true );
00054 setItemAttribute( QwtPlotItem::Margins, true );
00055 setZ( 19.0 );
00056 }
00057
00059 QwtPlotAbstractBarChart::~QwtPlotAbstractBarChart()
00060 {
00061 delete d_data;
00062 }
00063
00072 void QwtPlotAbstractBarChart::setLayoutPolicy( LayoutPolicy policy )
00073 {
00074 if ( policy != d_data->layoutPolicy )
00075 {
00076 d_data->layoutPolicy = policy;
00077 itemChanged();
00078 }
00079 }
00080
00088 QwtPlotAbstractBarChart::LayoutPolicy QwtPlotAbstractBarChart::layoutPolicy() const
00089 {
00090 return d_data->layoutPolicy;
00091 }
00092
00101 void QwtPlotAbstractBarChart::setLayoutHint( double hint )
00102 {
00103 hint = qMax( 0.0, hint );
00104 if ( hint != d_data->layoutHint )
00105 {
00106 d_data->layoutHint = hint;
00107 itemChanged();
00108 }
00109 }
00110
00118 double QwtPlotAbstractBarChart::layoutHint() const
00119 {
00120 return d_data->layoutHint;
00121 }
00122
00131 void QwtPlotAbstractBarChart::setSpacing( int spacing )
00132 {
00133 spacing = qMax( spacing, 0 );
00134 if ( spacing != d_data->spacing )
00135 {
00136 d_data->spacing = spacing;
00137 itemChanged();
00138 }
00139 }
00140
00145 int QwtPlotAbstractBarChart::spacing() const
00146 {
00147 return d_data->spacing;
00148 }
00159 void QwtPlotAbstractBarChart::setMargin( int margin )
00160 {
00161 margin = qMax( margin, 0 );
00162 if ( margin != d_data->margin )
00163 {
00164 d_data->margin = margin;
00165 itemChanged();
00166 }
00167 }
00168
00175 int QwtPlotAbstractBarChart::margin() const
00176 {
00177 return d_data->margin;
00178 }
00179
00194 void QwtPlotAbstractBarChart::setBaseline( double value )
00195 {
00196 if ( value != d_data->baseline )
00197 {
00198 d_data->baseline = value;
00199 itemChanged();
00200 }
00201 }
00202
00207 double QwtPlotAbstractBarChart::baseline() const
00208 {
00209 return d_data->baseline;
00210 }
00211
00224 double QwtPlotAbstractBarChart::sampleWidth( const QwtScaleMap &map,
00225 double canvasSize, double boundingSize, double value ) const
00226 {
00227 double width;
00228
00229 switch( d_data->layoutPolicy )
00230 {
00231 case ScaleSamplesToAxes:
00232 {
00233 width = qwtTransformWidth( map, value, d_data->layoutHint );
00234 break;
00235 }
00236 case ScaleSampleToCanvas:
00237 {
00238 width = canvasSize * d_data->layoutHint;
00239 break;
00240 }
00241 case FixedSampleSize:
00242 {
00243 width = d_data->layoutHint;
00244 break;
00245 }
00246 case AutoAdjustSamples:
00247 default:
00248 {
00249 const size_t numSamples = dataSize();
00250
00251 double w = 1.0;
00252 if ( numSamples > 1 )
00253 {
00254 w = qAbs( boundingSize / ( numSamples - 1 ) );
00255 }
00256
00257 width = qwtTransformWidth( map, value, w );
00258 width -= d_data->spacing;
00259 width = qMax( width, d_data->layoutHint );
00260 }
00261 }
00262
00263 return width;
00264 }
00265
00288 void QwtPlotAbstractBarChart::getCanvasMarginHint( const QwtScaleMap &xMap,
00289 const QwtScaleMap &yMap, const QRectF &canvasRect,
00290 double &left, double &top, double &right, double &bottom ) const
00291 {
00292 double hint = -1.0;
00293
00294 switch( layoutPolicy() )
00295 {
00296 case ScaleSampleToCanvas:
00297 {
00298 if ( orientation() == Qt::Vertical )
00299 hint = 0.5 * canvasRect.width() * d_data->layoutHint;
00300 else
00301 hint = 0.5 * canvasRect.height() * d_data->layoutHint;
00302
00303 break;
00304 }
00305 case FixedSampleSize:
00306 {
00307 hint = 0.5 * d_data->layoutHint;
00308 break;
00309 }
00310 case AutoAdjustSamples:
00311 case ScaleSamplesToAxes:
00312 default:
00313 {
00314 const size_t numSamples = dataSize();
00315 if ( numSamples <= 0 )
00316 break;
00317
00318
00319
00320 const QRectF br = dataRect();
00321 double spacing = 0.0;
00322 double sampleWidthS = 1.0;
00323
00324 if ( layoutPolicy() == ScaleSamplesToAxes )
00325 {
00326 sampleWidthS = qMax( d_data->layoutHint, 0.0 );
00327 }
00328 else
00329 {
00330 spacing = d_data->spacing;
00331
00332 if ( numSamples > 1 )
00333 {
00334 sampleWidthS = qAbs( br.width() / ( numSamples - 1 ) );
00335 }
00336 }
00337
00338 double ds, w;
00339 if ( orientation() == Qt::Vertical )
00340 {
00341 ds = qAbs( xMap.sDist() );
00342 w = canvasRect.width();
00343 }
00344 else
00345 {
00346 ds = qAbs( yMap.sDist() );
00347 w = canvasRect.height();
00348 }
00349
00350 const double sampleWidthP = ( w - spacing * ( numSamples - 1 ) )
00351 * sampleWidthS / ( ds + sampleWidthS );
00352
00353 hint = 0.5 * sampleWidthP;
00354 hint += qMax( d_data->margin, 0 );
00355 }
00356 }
00357
00358 if ( orientation() == Qt::Vertical )
00359 {
00360 left = right = hint;
00361 top = bottom = -1.0;
00362 }
00363 else
00364 {
00365 left = right = -1.0;
00366 top = bottom = hint;
00367 }
00368 }