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