Qwt User's Guide 6.2.0

qwt_plot_abstract_barchart.cpp
1 /******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10 #include "qwt_plot_abstract_barchart.h"
11 #include "qwt_scale_map.h"
12 #include "qwt_math.h"
13
14 static inline double qwtTransformWidth(
15 const QwtScaleMap& map, double value, double width )
16 {
17 const double w2 = 0.5 * width;
18
19 const double v1 = map.transform( value - w2 );
20 const double v2 = map.transform( value + w2 );
21
22 return qAbs( v2 - v1 );
23 }
24
25 class QwtPlotAbstractBarChart::PrivateData
26 {
27 public:
28 PrivateData()
29 : layoutPolicy( QwtPlotAbstractBarChart::AutoAdjustSamples )
30 , layoutHint( 0.5 )
31 , spacing( 10 )
32 , margin( 5 )
33 , baseline( 0.0 )
34 {
35 }
36
37 QwtPlotAbstractBarChart::LayoutPolicy layoutPolicy;
38 double layoutHint;
39 int spacing;
40 int margin;
41 double baseline;
42 };
43
48 QwtPlotAbstractBarChart::QwtPlotAbstractBarChart( const QwtText& title )
49 : QwtPlotSeriesItem( title )
50 {
51 m_data = new PrivateData;
52
53 setItemAttribute( QwtPlotItem::Legend, true );
54 setItemAttribute( QwtPlotItem::AutoScale, true );
55 setItemAttribute( QwtPlotItem::Margins, true );
56 setZ( 19.0 );
57 }
58
60 QwtPlotAbstractBarChart::~QwtPlotAbstractBarChart()
61 {
62 delete m_data;
63 }
64
73 void QwtPlotAbstractBarChart::setLayoutPolicy( LayoutPolicy policy )
74 {
75 if ( policy != m_data->layoutPolicy )
76 {
77 m_data->layoutPolicy = policy;
78 itemChanged();
79 }
80 }
81
89 QwtPlotAbstractBarChart::LayoutPolicy QwtPlotAbstractBarChart::layoutPolicy() const
90 {
91 return m_data->layoutPolicy;
92 }
93
102 void QwtPlotAbstractBarChart::setLayoutHint( double hint )
103 {
104 hint = qwtMaxF( 0.0, hint );
105 if ( hint != m_data->layoutHint )
106 {
107 m_data->layoutHint = hint;
108 itemChanged();
109 }
110 }
111
119 double QwtPlotAbstractBarChart::layoutHint() const
120 {
121 return m_data->layoutHint;
122 }
123
132 void QwtPlotAbstractBarChart::setSpacing( int spacing )
133 {
134 spacing = qMax( spacing, 0 );
135 if ( spacing != m_data->spacing )
136 {
137 m_data->spacing = spacing;
138 itemChanged();
139 }
140 }
141
146 int QwtPlotAbstractBarChart::spacing() const
147 {
148 return m_data->spacing;
149 }
160 void QwtPlotAbstractBarChart::setMargin( int margin )
161 {
162 margin = qMax( margin, 0 );
163 if ( margin != m_data->margin )
164 {
165 m_data->margin = margin;
166 itemChanged();
167 }
168 }
169
176 int QwtPlotAbstractBarChart::margin() const
177 {
178 return m_data->margin;
179 }
180
195 void QwtPlotAbstractBarChart::setBaseline( double value )
196 {
197 if ( value != m_data->baseline )
198 {
199 m_data->baseline = value;
200 itemChanged();
201 }
202 }
203
208 double QwtPlotAbstractBarChart::baseline() const
209 {
210 return m_data->baseline;
211 }
212
225 double QwtPlotAbstractBarChart::sampleWidth( const QwtScaleMap& map,
226 double canvasSize, double boundingSize, double value ) const
227 {
228 double width;
229
230 switch( m_data->layoutPolicy )
231 {
232 case ScaleSamplesToAxes:
233 {
234 width = qwtTransformWidth( map, value, m_data->layoutHint );
235 break;
236 }
237 case ScaleSampleToCanvas:
238 {
239 width = canvasSize * m_data->layoutHint;
240 break;
241 }
242 case FixedSampleSize:
243 {
244 width = m_data->layoutHint;
245 break;
246 }
247 case AutoAdjustSamples:
248 default:
249 {
250 const size_t numSamples = dataSize();
251
252 double w = 1.0;
253 if ( numSamples > 1 )
254 {
255 w = qAbs( boundingSize / ( numSamples - 1 ) );
256 }
257
258 width = qwtTransformWidth( map, value, w );
259 width -= m_data->spacing;
260 width = qwtMaxF( width, m_data->layoutHint );
261 }
262 }
263
264 return width;
265 }
266
289 void QwtPlotAbstractBarChart::getCanvasMarginHint( const QwtScaleMap& xMap,
290 const QwtScaleMap& yMap, const QRectF& canvasRect,
291 double& left, double& top, double& right, double& bottom ) const
292 {
293 double hint = -1.0;
294
295 switch( layoutPolicy() )
296 {
297 case ScaleSampleToCanvas:
298 {
299 if ( orientation() == Qt::Vertical )
300 hint = 0.5 * canvasRect.width() * m_data->layoutHint;
301 else
302 hint = 0.5 * canvasRect.height() * m_data->layoutHint;
303
304 break;
305 }
306 case FixedSampleSize:
307 {
308 hint = 0.5 * m_data->layoutHint;
309 break;
310 }
311 case AutoAdjustSamples:
312 case ScaleSamplesToAxes:
313 default:
314 {
315 const size_t numSamples = dataSize();
316 if ( numSamples <= 0 )
317 break;
318
319 // doesn't work for nonlinear scales
320
321 const QRectF br = dataRect();
322 double spacing = 0.0;
323 double sampleWidthS = 1.0;
324
325 if ( layoutPolicy() == ScaleSamplesToAxes )
326 {
327 sampleWidthS = qwtMaxF( m_data->layoutHint, 0.0 );
328 }
329 else
330 {
331 spacing = m_data->spacing;
332
333 if ( numSamples > 1 )
334 {
335 sampleWidthS = qAbs( br.width() / ( numSamples - 1 ) );
336 }
337 }
338
339 double ds, w;
340 if ( orientation() == Qt::Vertical )
341 {
342 ds = qAbs( xMap.sDist() );
343 w = canvasRect.width();
344 }
345 else
346 {
347 ds = qAbs( yMap.sDist() );
348 w = canvasRect.height();
349 }
350
351 const double sampleWidthP = ( w - spacing * ( numSamples - 1 ) )
352 * sampleWidthS / ( ds + sampleWidthS );
353
354 hint = 0.5 * sampleWidthP;
355 hint += qMax( m_data->margin, 0 );
356 }
357 }
358
359 if ( orientation() == Qt::Vertical )
360 {
361 left = right = hint;
362 top = bottom = -1.0; // no hint
363 }
364 else
365 {
366 left = right = -1.0; // no hint
367 top = bottom = hint;
368 }
369 }
QwtAbstractSeriesStore::dataRect
virtual QRectF dataRect() const =0
QwtAbstractSeriesStore::dataSize
virtual size_t dataSize() const =0
QwtPlotAbstractBarChart
Abstract base class for bar chart items.
Definition: qwt_plot_abstract_barchart.h:25
QwtPlotAbstractBarChart::setSpacing
void setSpacing(int)
Set the spacing.
Definition: qwt_plot_abstract_barchart.cpp:132
QwtPlotAbstractBarChart::layoutHint
double layoutHint() const
Definition: qwt_plot_abstract_barchart.cpp:119
QwtPlotAbstractBarChart::QwtPlotAbstractBarChart
QwtPlotAbstractBarChart(const QwtText &title)
Definition: qwt_plot_abstract_barchart.cpp:48
QwtPlotAbstractBarChart::sampleWidth
double sampleWidth(const QwtScaleMap &map, double canvasSize, double boundingSize, double value) const
Definition: qwt_plot_abstract_barchart.cpp:225
QwtPlotAbstractBarChart::baseline
double baseline() const
Definition: qwt_plot_abstract_barchart.cpp:208
QwtPlotAbstractBarChart::spacing
int spacing() const
Definition: qwt_plot_abstract_barchart.cpp:146
QwtPlotAbstractBarChart::~QwtPlotAbstractBarChart
virtual ~QwtPlotAbstractBarChart()
Destructor.
Definition: qwt_plot_abstract_barchart.cpp:60
QwtPlotAbstractBarChart::margin
int margin() const
Definition: qwt_plot_abstract_barchart.cpp:176
QwtPlotAbstractBarChart::setMargin
void setMargin(int)
Set the margin.
Definition: qwt_plot_abstract_barchart.cpp:160
QwtPlotAbstractBarChart::getCanvasMarginHint
virtual void getCanvasMarginHint(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, double &left, double &top, double &right, double &bottom) const override
Calculate a hint for the canvas margin.
Definition: qwt_plot_abstract_barchart.cpp:289
QwtPlotAbstractBarChart::layoutPolicy
LayoutPolicy layoutPolicy() const
Definition: qwt_plot_abstract_barchart.cpp:89
QwtPlotAbstractBarChart::setLayoutPolicy
void setLayoutPolicy(LayoutPolicy)
Definition: qwt_plot_abstract_barchart.cpp:73
QwtPlotAbstractBarChart::setBaseline
void setBaseline(double)
Set the baseline.
Definition: qwt_plot_abstract_barchart.cpp:195
QwtPlotAbstractBarChart::LayoutPolicy
LayoutPolicy
Mode how to calculate the bar width.
Definition: qwt_plot_abstract_barchart.h:33
QwtPlotAbstractBarChart::FixedSampleSize
@ FixedSampleSize
Definition: qwt_plot_abstract_barchart.h:59
QwtPlotAbstractBarChart::AutoAdjustSamples
@ AutoAdjustSamples
Definition: qwt_plot_abstract_barchart.h:41
QwtPlotAbstractBarChart::ScaleSamplesToAxes
@ ScaleSamplesToAxes
Definition: qwt_plot_abstract_barchart.h:46
QwtPlotAbstractBarChart::ScaleSampleToCanvas
@ ScaleSampleToCanvas
Definition: qwt_plot_abstract_barchart.h:54
QwtPlotAbstractBarChart::setLayoutHint
void setLayoutHint(double)
Definition: qwt_plot_abstract_barchart.cpp:102
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition: qwt_plot_item.cpp:228
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition: qwt_plot_item.h:150
QwtPlotItem::Margins
@ Margins
Definition: qwt_plot_item.h:164
QwtPlotItem::AutoScale
@ AutoScale
Definition: qwt_plot_item.h:157
QwtPlotSeriesItem
Base class for plot items representing a series of samples.
Definition: qwt_plot_seriesitem.h:26
QwtPlotSeriesItem::orientation
Qt::Orientation orientation() const
Definition: qwt_plot_seriesitem.cpp:77
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtScaleMap::sDist
double sDist() const
Definition: qwt_scale_map.h:123
QwtText
A class representing a text.
Definition: qwt_text.h:52