1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
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 | #ifndef QWT_SERIES_DATA_H
|
---|
11 | #define QWT_SERIES_DATA_H 1
|
---|
12 |
|
---|
13 | #include "qwt_global.h"
|
---|
14 | #include "qwt_samples.h"
|
---|
15 | #include "qwt_point_3d.h"
|
---|
16 | #include "qwt_point_polar.h"
|
---|
17 | #include <qvector.h>
|
---|
18 | #include <qrect.h>
|
---|
19 |
|
---|
20 | /*!
|
---|
21 | \brief Abstract interface for iterating over samples
|
---|
22 |
|
---|
23 | Qwt offers several implementations of the QwtSeriesData API,
|
---|
24 | but in situations, where data of an application specific format
|
---|
25 | needs to be displayed, without having to copy it, it is recommended
|
---|
26 | to implement an individual data access.
|
---|
27 |
|
---|
28 | A subclass of QwtSeriesData<QPointF> must implement:
|
---|
29 |
|
---|
30 | - size()\n
|
---|
31 | Should return number of data points.
|
---|
32 |
|
---|
33 | - sample()\n
|
---|
34 | Should return values x and y values of the sample at specific position
|
---|
35 | as QPointF object.
|
---|
36 |
|
---|
37 | - boundingRect()\n
|
---|
38 | Should return the bounding rectangle of the data series.
|
---|
39 | It is used for autoscaling and might help certain algorithms for displaying
|
---|
40 | the data. You can use qwtBoundingRect() for an implementation
|
---|
41 | but often it is possible to implement a more efficient algorithm
|
---|
42 | depending on the characteristics of the series.
|
---|
43 | The member d_boundingRect is intended for caching the calculated rectangle.
|
---|
44 |
|
---|
45 | */
|
---|
46 | template <typename T>
|
---|
47 | class QwtSeriesData
|
---|
48 | {
|
---|
49 | public:
|
---|
50 | //! Constructor
|
---|
51 | QwtSeriesData();
|
---|
52 |
|
---|
53 | //! Destructor
|
---|
54 | virtual ~QwtSeriesData();
|
---|
55 |
|
---|
56 | #ifndef QWT_PYTHON_WRAPPER
|
---|
57 |
|
---|
58 | //! \return Number of samples
|
---|
59 | virtual size_t size() const = 0;
|
---|
60 |
|
---|
61 | /*!
|
---|
62 | Return a sample
|
---|
63 | \param i Index
|
---|
64 | \return Sample at position i
|
---|
65 | */
|
---|
66 | virtual T sample( size_t i ) const = 0;
|
---|
67 |
|
---|
68 | /*!
|
---|
69 | Calculate the bounding rect of all samples
|
---|
70 |
|
---|
71 | The bounding rect is necessary for autoscaling and can be used
|
---|
72 | for a couple of painting optimizations.
|
---|
73 |
|
---|
74 | qwtBoundingRect(...) offers slow implementations iterating
|
---|
75 | over the samples. For large sets it is recommended to implement
|
---|
76 | something faster f.e. by caching the bounding rectangle.
|
---|
77 |
|
---|
78 | \return Bounding rectangle
|
---|
79 | */
|
---|
80 | virtual QRectF boundingRect() const = 0;
|
---|
81 |
|
---|
82 | #else
|
---|
83 | // Needed for generating the python bindings, but not for using them !
|
---|
84 | virtual size_t size() const { return 0; }
|
---|
85 | virtual T sample( size_t i ) const { return T(); }
|
---|
86 | virtual QRectF boundingRect() const { return d_boundingRect; }
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | /*!
|
---|
90 | Set a the "rect of interest"
|
---|
91 |
|
---|
92 | QwtPlotSeriesItem defines the current area of the plot canvas
|
---|
93 | as "rectangle of interest" ( QwtPlotSeriesItem::updateScaleDiv() ).
|
---|
94 | It can be used to implement different levels of details.
|
---|
95 |
|
---|
96 | The default implementation does nothing.
|
---|
97 |
|
---|
98 | \param rect Rectangle of interest
|
---|
99 | */
|
---|
100 | virtual void setRectOfInterest( const QRectF &rect );
|
---|
101 |
|
---|
102 | protected:
|
---|
103 | //! Can be used to cache a calculated bounding rectangle
|
---|
104 | mutable QRectF d_boundingRect;
|
---|
105 |
|
---|
106 | private:
|
---|
107 | QwtSeriesData<T> &operator=( const QwtSeriesData<T> & );
|
---|
108 | };
|
---|
109 |
|
---|
110 | template <typename T>
|
---|
111 | QwtSeriesData<T>::QwtSeriesData():
|
---|
112 | d_boundingRect( 0.0, 0.0, -1.0, -1.0 )
|
---|
113 | {
|
---|
114 | }
|
---|
115 |
|
---|
116 | template <typename T>
|
---|
117 | QwtSeriesData<T>::~QwtSeriesData()
|
---|
118 | {
|
---|
119 | }
|
---|
120 |
|
---|
121 | template <typename T>
|
---|
122 | void QwtSeriesData<T>::setRectOfInterest( const QRectF & )
|
---|
123 | {
|
---|
124 | }
|
---|
125 |
|
---|
126 | /*!
|
---|
127 | \brief Template class for data, that is organized as QVector
|
---|
128 |
|
---|
129 | QVector uses implicit data sharing and can be
|
---|
130 | passed around as argument efficiently.
|
---|
131 | */
|
---|
132 | template <typename T>
|
---|
133 | class QwtArraySeriesData: public QwtSeriesData<T>
|
---|
134 | {
|
---|
135 | public:
|
---|
136 | //! Constructor
|
---|
137 | QwtArraySeriesData();
|
---|
138 |
|
---|
139 | /*!
|
---|
140 | Constructor
|
---|
141 | \param samples Array of samples
|
---|
142 | */
|
---|
143 | QwtArraySeriesData( const QVector<T> &samples );
|
---|
144 |
|
---|
145 | /*!
|
---|
146 | Assign an array of samples
|
---|
147 | \param samples Array of samples
|
---|
148 | */
|
---|
149 | void setSamples( const QVector<T> &samples );
|
---|
150 |
|
---|
151 | //! \return Array of samples
|
---|
152 | const QVector<T> samples() const;
|
---|
153 |
|
---|
154 | //! \return Number of samples
|
---|
155 | virtual size_t size() const;
|
---|
156 |
|
---|
157 | /*!
|
---|
158 | \return Sample at a specific position
|
---|
159 |
|
---|
160 | \param index Index
|
---|
161 | \return Sample at position index
|
---|
162 | */
|
---|
163 | virtual T sample( size_t index ) const;
|
---|
164 |
|
---|
165 | protected:
|
---|
166 | //! Vector of samples
|
---|
167 | QVector<T> d_samples;
|
---|
168 | };
|
---|
169 |
|
---|
170 | template <typename T>
|
---|
171 | QwtArraySeriesData<T>::QwtArraySeriesData()
|
---|
172 | {
|
---|
173 | }
|
---|
174 |
|
---|
175 | template <typename T>
|
---|
176 | QwtArraySeriesData<T>::QwtArraySeriesData( const QVector<T> &samples ):
|
---|
177 | d_samples( samples )
|
---|
178 | {
|
---|
179 | }
|
---|
180 |
|
---|
181 | template <typename T>
|
---|
182 | void QwtArraySeriesData<T>::setSamples( const QVector<T> &samples )
|
---|
183 | {
|
---|
184 | QwtSeriesData<T>::d_boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 );
|
---|
185 | d_samples = samples;
|
---|
186 | }
|
---|
187 |
|
---|
188 | template <typename T>
|
---|
189 | const QVector<T> QwtArraySeriesData<T>::samples() const
|
---|
190 | {
|
---|
191 | return d_samples;
|
---|
192 | }
|
---|
193 |
|
---|
194 | template <typename T>
|
---|
195 | size_t QwtArraySeriesData<T>::size() const
|
---|
196 | {
|
---|
197 | return d_samples.size();
|
---|
198 | }
|
---|
199 |
|
---|
200 | template <typename T>
|
---|
201 | T QwtArraySeriesData<T>::sample( size_t i ) const
|
---|
202 | {
|
---|
203 | return d_samples[ static_cast<int>( i ) ];
|
---|
204 | }
|
---|
205 |
|
---|
206 | //! Interface for iterating over an array of points
|
---|
207 | class QWT_EXPORT QwtPointSeriesData: public QwtArraySeriesData<QPointF>
|
---|
208 | {
|
---|
209 | public:
|
---|
210 | QwtPointSeriesData(
|
---|
211 | const QVector<QPointF> & = QVector<QPointF>() );
|
---|
212 |
|
---|
213 | virtual QRectF boundingRect() const;
|
---|
214 | };
|
---|
215 |
|
---|
216 | //! Interface for iterating over an array of 3D points
|
---|
217 | class QWT_EXPORT QwtPoint3DSeriesData: public QwtArraySeriesData<QwtPoint3D>
|
---|
218 | {
|
---|
219 | public:
|
---|
220 | QwtPoint3DSeriesData(
|
---|
221 | const QVector<QwtPoint3D> & = QVector<QwtPoint3D>() );
|
---|
222 | virtual QRectF boundingRect() const;
|
---|
223 | };
|
---|
224 |
|
---|
225 | //! Interface for iterating over an array of intervals
|
---|
226 | class QWT_EXPORT QwtIntervalSeriesData: public QwtArraySeriesData<QwtIntervalSample>
|
---|
227 | {
|
---|
228 | public:
|
---|
229 | QwtIntervalSeriesData(
|
---|
230 | const QVector<QwtIntervalSample> & = QVector<QwtIntervalSample>() );
|
---|
231 |
|
---|
232 | virtual QRectF boundingRect() const;
|
---|
233 | };
|
---|
234 |
|
---|
235 | //! Interface for iterating over an array of samples
|
---|
236 | class QWT_EXPORT QwtSetSeriesData: public QwtArraySeriesData<QwtSetSample>
|
---|
237 | {
|
---|
238 | public:
|
---|
239 | QwtSetSeriesData(
|
---|
240 | const QVector<QwtSetSample> & = QVector<QwtSetSample>() );
|
---|
241 |
|
---|
242 | virtual QRectF boundingRect() const;
|
---|
243 | };
|
---|
244 |
|
---|
245 | /*!
|
---|
246 | Interface for iterating over an array of OHLC samples
|
---|
247 | */
|
---|
248 | class QWT_EXPORT QwtTradingChartData: public QwtArraySeriesData<QwtOHLCSample>
|
---|
249 | {
|
---|
250 | public:
|
---|
251 | QwtTradingChartData(
|
---|
252 | const QVector<QwtOHLCSample> & = QVector<QwtOHLCSample>() );
|
---|
253 |
|
---|
254 | virtual QRectF boundingRect() const;
|
---|
255 | };
|
---|
256 |
|
---|
257 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
258 | const QwtSeriesData<QPointF> &, int from = 0, int to = -1 );
|
---|
259 |
|
---|
260 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
261 | const QwtSeriesData<QwtPoint3D> &, int from = 0, int to = -1 );
|
---|
262 |
|
---|
263 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
264 | const QwtSeriesData<QwtPointPolar> &, int from = 0, int to = -1 );
|
---|
265 |
|
---|
266 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
267 | const QwtSeriesData<QwtIntervalSample> &, int from = 0, int to = -1 );
|
---|
268 |
|
---|
269 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
270 | const QwtSeriesData<QwtSetSample> &, int from = 0, int to = -1 );
|
---|
271 |
|
---|
272 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
273 | const QwtSeriesData<QwtOHLCSample> &, int from = 0, int to = -1 );
|
---|
274 |
|
---|
275 | /*!
|
---|
276 | Binary search for a sorted series of samples
|
---|
277 |
|
---|
278 | qwtUpperSampleIndex returns the index of sample that is the upper bound
|
---|
279 | of value. Is the the value smaller than the smallest value the return
|
---|
280 | value will be 0. Is the value greater or equal than the largest
|
---|
281 | value the return value will be -1.
|
---|
282 |
|
---|
283 | \par Example
|
---|
284 | The following example shows finds a point of curve from an x
|
---|
285 | coordinate
|
---|
286 | \code
|
---|
287 | #include <qwt_series_data.h>
|
---|
288 | #include <qwt_plot_curve.h>
|
---|
289 |
|
---|
290 | struct compareX
|
---|
291 | {
|
---|
292 | inline bool operator()( const double x, const QPointF &pos ) const
|
---|
293 | {
|
---|
294 | return ( x < pos.x() );
|
---|
295 | }
|
---|
296 | };
|
---|
297 |
|
---|
298 | QLineF curveLineAt( const QwtPlotCurve *curve, double x )
|
---|
299 | {
|
---|
300 | int index = qwtUpperSampleIndex<QPointF>(
|
---|
301 | *curve->data(), x, compareX() );
|
---|
302 |
|
---|
303 | if ( index == -1 &&
|
---|
304 | x == curve->sample( curve->dataSize() - 1 ).x() )
|
---|
305 | {
|
---|
306 | // the last sample is excluded from qwtUpperSampleIndex
|
---|
307 | index = curve->dataSize() - 1;
|
---|
308 | }
|
---|
309 |
|
---|
310 | QLineF line; // invalid
|
---|
311 | if ( index > 0 )
|
---|
312 | {
|
---|
313 | line.setP1( curve->sample( index - 1 ) );
|
---|
314 | line.setP2( curve->sample( index ) );
|
---|
315 | }
|
---|
316 |
|
---|
317 | return line;
|
---|
318 | }
|
---|
319 |
|
---|
320 | \endcode
|
---|
321 | \endpar
|
---|
322 |
|
---|
323 | \param series Series of samples
|
---|
324 | \param value Value
|
---|
325 | \param lessThan Compare operation
|
---|
326 |
|
---|
327 | \note The samples must be sorted according to the order specified
|
---|
328 | by the lessThan object
|
---|
329 | */
|
---|
330 | template <typename T, typename LessThan>
|
---|
331 | inline int qwtUpperSampleIndex( const QwtSeriesData<T> &series,
|
---|
332 | double value, LessThan lessThan )
|
---|
333 | {
|
---|
334 | const int indexMax = series.size() - 1;
|
---|
335 |
|
---|
336 | if ( indexMax < 0 || !lessThan( value, series.sample( indexMax ) ) )
|
---|
337 | return -1;
|
---|
338 |
|
---|
339 | int indexMin = 0;
|
---|
340 | int n = indexMax;
|
---|
341 |
|
---|
342 | while ( n > 0 )
|
---|
343 | {
|
---|
344 | const int half = n >> 1;
|
---|
345 | const int indexMid = indexMin + half;
|
---|
346 |
|
---|
347 | if ( lessThan( value, series.sample( indexMid ) ) )
|
---|
348 | {
|
---|
349 | n = half;
|
---|
350 | }
|
---|
351 | else
|
---|
352 | {
|
---|
353 | indexMin = indexMid + 1;
|
---|
354 | n -= half + 1;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | return indexMin;
|
---|
359 | }
|
---|
360 |
|
---|
361 | #endif
|
---|