[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 |
|
---|
[9383] | 28 | A subclass of QwtSeriesData<QPointF> must implement:
|
---|
[8127] | 29 |
|
---|
[9383] | 30 | - size()\n
|
---|
[8127] | 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 |
|
---|
[9383] | 37 | - boundingRect()\n
|
---|
[8127] | 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
|
---|
[9383] | 41 | but often it is possible to implement a more efficient algorithm
|
---|
[8127] | 42 | depending on the characteristics of the series.
|
---|
| 43 | The member d_boundingRect is intended for caching the calculated rectangle.
|
---|
[9383] | 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 |
|
---|
[9383] | 56 | #ifndef QWT_PYTHON_WRAPPER
|
---|
| 57 |
|
---|
[4271] | 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
|
---|
[8127] | 76 | something faster f.e. by caching the bounding rectangle.
|
---|
| 77 |
|
---|
| 78 | \return Bounding rectangle
|
---|
[4271] | 79 | */
|
---|
| 80 | virtual QRectF boundingRect() const = 0;
|
---|
| 81 |
|
---|
[9383] | 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 |
|
---|
[8127] | 89 | /*!
|
---|
| 90 | Set a the "rect of interest"
|
---|
[4271] | 91 |
|
---|
[8127] | 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.
|
---|
[9383] | 97 |
|
---|
[8127] | 98 | \param rect Rectangle of interest
|
---|
| 99 | */
|
---|
| 100 | virtual void setRectOfInterest( const QRectF &rect );
|
---|
| 101 |
|
---|
[4271] | 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:
|
---|
[8127] | 136 | //! Constructor
|
---|
[4271] | 137 | QwtArraySeriesData();
|
---|
| 138 |
|
---|
[8127] | 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
|
---|
[4271] | 152 | const QVector<T> samples() const;
|
---|
| 153 |
|
---|
[8127] | 154 | //! \return Number of samples
|
---|
[4271] | 155 | virtual size_t size() const;
|
---|
| 156 |
|
---|
[8127] | 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 |
|
---|
[4271] | 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 | {
|
---|
[8127] | 203 | return d_samples[ static_cast<int>( i ) ];
|
---|
[4271] | 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 | /*!
|
---|
[8127] | 246 | Interface for iterating over an array of OHLC samples
|
---|
[4271] | 247 | */
|
---|
[8127] | 248 | class QWT_EXPORT QwtTradingChartData: public QwtArraySeriesData<QwtOHLCSample>
|
---|
[4271] | 249 | {
|
---|
| 250 | public:
|
---|
[8127] | 251 | QwtTradingChartData(
|
---|
| 252 | const QVector<QwtOHLCSample> & = QVector<QwtOHLCSample>() );
|
---|
[4271] | 253 |
|
---|
| 254 | virtual QRectF boundingRect() const;
|
---|
[8127] | 255 | };
|
---|
[4271] | 256 |
|
---|
[8127] | 257 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
| 258 | const QwtSeriesData<QPointF> &, int from = 0, int to = -1 );
|
---|
[4271] | 259 |
|
---|
[8127] | 260 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
| 261 | const QwtSeriesData<QwtPoint3D> &, int from = 0, int to = -1 );
|
---|
[4271] | 262 |
|
---|
[8127] | 263 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
| 264 | const QwtSeriesData<QwtPointPolar> &, int from = 0, int to = -1 );
|
---|
[4271] | 265 |
|
---|
[8127] | 266 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
| 267 | const QwtSeriesData<QwtIntervalSample> &, int from = 0, int to = -1 );
|
---|
[4271] | 268 |
|
---|
[8127] | 269 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
| 270 | const QwtSeriesData<QwtSetSample> &, int from = 0, int to = -1 );
|
---|
[4271] | 271 |
|
---|
[8127] | 272 | QWT_EXPORT QRectF qwtBoundingRect(
|
---|
| 273 | const QwtSeriesData<QwtOHLCSample> &, int from = 0, int to = -1 );
|
---|
[4271] | 274 |
|
---|
| 275 | /*!
|
---|
[8127] | 276 | Binary search for a sorted series of samples
|
---|
[4271] | 277 |
|
---|
[8127] | 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.
|
---|
[4271] | 282 |
|
---|
| 283 | \par Example
|
---|
[8127] | 284 | The following example shows finds a point of curve from an x
|
---|
| 285 | coordinate
|
---|
[9383] | 286 | \code
|
---|
| 287 | #include <qwt_series_data.h>
|
---|
| 288 | #include <qwt_plot_curve.h>
|
---|
[4271] | 289 |
|
---|
[9383] | 290 | struct compareX
|
---|
| 291 | {
|
---|
| 292 | inline bool operator()( const double x, const QPointF &pos ) const
|
---|
| 293 | {
|
---|
| 294 | return ( x < pos.x() );
|
---|
| 295 | }
|
---|
| 296 | };
|
---|
[4271] | 297 |
|
---|
[9383] | 298 | QLineF curveLineAt( const QwtPlotCurve *curve, double x )
|
---|
| 299 | {
|
---|
| 300 | int index = qwtUpperSampleIndex<QPointF>(
|
---|
| 301 | *curve->data(), x, compareX() );
|
---|
[4271] | 302 |
|
---|
[9383] | 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 | }
|
---|
[4271] | 309 |
|
---|
[9383] | 310 | QLineF line; // invalid
|
---|
| 311 | if ( index > 0 )
|
---|
| 312 | {
|
---|
| 313 | line.setP1( curve->sample( index - 1 ) );
|
---|
| 314 | line.setP2( curve->sample( index ) );
|
---|
| 315 | }
|
---|
[4271] | 316 |
|
---|
[9383] | 317 | return line;
|
---|
| 318 | }
|
---|
[4271] | 319 |
|
---|
[9383] | 320 | \endcode
|
---|
| 321 | \endpar
|
---|
[4271] | 322 |
|
---|
[9383] | 323 | \param series Series of samples
|
---|
| 324 | \param value Value
|
---|
| 325 | \param lessThan Compare operation
|
---|
[4271] | 326 |
|
---|
[9383] | 327 | \note The samples must be sorted according to the order specified
|
---|
| 328 | by the lessThan object
|
---|
[8127] | 329 | */
|
---|
| 330 | template <typename T, typename LessThan>
|
---|
| 331 | inline int qwtUpperSampleIndex( const QwtSeriesData<T> &series,
|
---|
[9383] | 332 | double value, LessThan lessThan )
|
---|
[8127] | 333 | {
|
---|
| 334 | const int indexMax = series.size() - 1;
|
---|
[4271] | 335 |
|
---|
[8127] | 336 | if ( indexMax < 0 || !lessThan( value, series.sample( indexMax ) ) )
|
---|
| 337 | return -1;
|
---|
[4271] | 338 |
|
---|
[8127] | 339 | int indexMin = 0;
|
---|
| 340 | int n = indexMax;
|
---|
[4271] | 341 |
|
---|
[8127] | 342 | while ( n > 0 )
|
---|
| 343 | {
|
---|
| 344 | const int half = n >> 1;
|
---|
| 345 | const int indexMid = indexMin + half;
|
---|
[4271] | 346 |
|
---|
[8127] | 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
|
---|