Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
14static 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
25class QwtPlotAbstractBarChart::PrivateData
26{
27 public:
28 PrivateData()
30 , layoutHint( 0.5 )
31 , spacing( 10 )
32 , margin( 5 )
33 , baseline( 0.0 )
34 {
35 }
36
38 double layoutHint;
39 int spacing;
40 int margin;
41 double baseline;
42};
43
49 : QwtPlotSeriesItem( title )
50{
51 m_data = new PrivateData;
52
56 setZ( 19.0 );
57}
58
64
74{
75 if ( policy != m_data->layoutPolicy )
76 {
77 m_data->layoutPolicy = policy;
79 }
80}
81
93
103{
104 hint = qwtMaxF( 0.0, hint );
105 if ( hint != m_data->layoutHint )
106 {
107 m_data->layoutHint = hint;
108 itemChanged();
109 }
110}
111
120{
121 return m_data->layoutHint;
122}
123
133{
134 spacing = qMax( spacing, 0 );
135 if ( spacing != m_data->spacing )
136 {
137 m_data->spacing = spacing;
138 itemChanged();
139 }
140}
141
147{
148 return m_data->spacing;
149}
161{
162 margin = qMax( margin, 0 );
163 if ( margin != m_data->margin )
164 {
165 m_data->margin = margin;
166 itemChanged();
167 }
168}
169
177{
178 return m_data->margin;
179}
180
196{
197 if ( value != m_data->baseline )
198 {
199 m_data->baseline = value;
200 itemChanged();
201 }
202}
203
209{
210 return m_data->baseline;
211}
212
226 double canvasSize, double boundingSize, double value ) const
227{
228 double width;
229
230 switch( m_data->layoutPolicy )
231 {
233 {
234 width = qwtTransformWidth( map, value, m_data->layoutHint );
235 break;
236 }
238 {
239 width = canvasSize * m_data->layoutHint;
240 break;
241 }
242 case FixedSampleSize:
243 {
244 width = m_data->layoutHint;
245 break;
246 }
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
288 const QwtScaleMap& yMap, const QRectF& canvasRect,
289 double& left, double& top, double& right, double& bottom ) const
290{
291 double hint = -1.0;
292
293 switch( layoutPolicy() )
294 {
296 {
297 if ( orientation() == Qt::Vertical )
298 hint = 0.5 * canvasRect.width() * m_data->layoutHint;
299 else
300 hint = 0.5 * canvasRect.height() * m_data->layoutHint;
301
302 break;
303 }
304 case FixedSampleSize:
305 {
306 hint = 0.5 * m_data->layoutHint;
307 break;
308 }
311 default:
312 {
313 const size_t numSamples = dataSize();
314 if ( numSamples <= 0 )
315 break;
316
317 // doesn't work for nonlinear scales
318
319 const QRectF br = dataRect();
320 double spacing = 0.0;
321 double sampleWidthS = 1.0;
322
324 {
325 sampleWidthS = qwtMaxF( m_data->layoutHint, 0.0 );
326 }
327 else
328 {
329 spacing = m_data->spacing;
330
331 if ( numSamples > 1 )
332 {
333 sampleWidthS = qAbs( br.width() / ( numSamples - 1 ) );
334 }
335 }
336
337 double ds, w;
338 if ( orientation() == Qt::Vertical )
339 {
340 ds = qAbs( xMap.sDist() );
341 w = canvasRect.width();
342 }
343 else
344 {
345 ds = qAbs( yMap.sDist() );
346 w = canvasRect.height();
347 }
348
349 const double sampleWidthP = ( w - spacing * ( numSamples - 1 ) )
350 * sampleWidthS / ( ds + sampleWidthS );
351
352 hint = 0.5 * sampleWidthP;
353 hint += qMax( m_data->margin, 0 );
354 }
355 }
356
357 if ( orientation() == Qt::Vertical )
358 {
359 left = right = hint;
360 top = bottom = -1.0; // no hint
361 }
362 else
363 {
364 left = right = -1.0; // no hint
365 top = bottom = hint;
366 }
367}
virtual QRectF dataRect() const =0
virtual size_t dataSize() const =0
Abstract base class for bar chart items.
void setSpacing(int)
Set the spacing.
QwtPlotAbstractBarChart(const QwtText &title)
double sampleWidth(const QwtScaleMap &map, double canvasSize, double boundingSize, double value) const
virtual ~QwtPlotAbstractBarChart()
Destructor.
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.
void setBaseline(double)
Set the baseline.
LayoutPolicy
Mode how to calculate the bar width.
void setZ(double z)
Set the z value.
void setItemAttribute(ItemAttribute, bool on=true)
virtual void itemChanged()
@ Legend
The item is represented on the legend.
Base class for plot items representing a series of samples.
Qt::Orientation orientation() const
A scale map.
double transform(double s) const
double sDist() const
A class representing a text.
Definition qwt_text.h:52