[8127] | 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_SAMPLES_H
|
---|
| 11 | #define QWT_SAMPLES_H 1
|
---|
| 12 |
|
---|
| 13 | #include "qwt_global.h"
|
---|
| 14 | #include "qwt_interval.h"
|
---|
| 15 | #include <qvector.h>
|
---|
| 16 | #include <qrect.h>
|
---|
| 17 |
|
---|
| 18 | //! \brief A sample of the types (x1-x2, y) or (x, y1-y2)
|
---|
| 19 | class QWT_EXPORT QwtIntervalSample
|
---|
| 20 | {
|
---|
| 21 | public:
|
---|
| 22 | QwtIntervalSample();
|
---|
| 23 | QwtIntervalSample( double, const QwtInterval & );
|
---|
| 24 | QwtIntervalSample( double value, double min, double max );
|
---|
| 25 |
|
---|
| 26 | bool operator==( const QwtIntervalSample & ) const;
|
---|
| 27 | bool operator!=( const QwtIntervalSample & ) const;
|
---|
| 28 |
|
---|
| 29 | //! Value
|
---|
| 30 | double value;
|
---|
| 31 |
|
---|
| 32 | //! Interval
|
---|
| 33 | QwtInterval interval;
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | /*!
|
---|
| 37 | Constructor
|
---|
| 38 | The value is set to 0.0, the interval is invalid
|
---|
| 39 | */
|
---|
| 40 | inline QwtIntervalSample::QwtIntervalSample():
|
---|
| 41 | value( 0.0 )
|
---|
| 42 | {
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | //! Constructor
|
---|
| 46 | inline QwtIntervalSample::QwtIntervalSample(
|
---|
| 47 | double v, const QwtInterval &intv ):
|
---|
| 48 | value( v ),
|
---|
| 49 | interval( intv )
|
---|
| 50 | {
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | //! Constructor
|
---|
| 54 | inline QwtIntervalSample::QwtIntervalSample(
|
---|
| 55 | double v, double min, double max ):
|
---|
| 56 | value( v ),
|
---|
| 57 | interval( min, max )
|
---|
| 58 | {
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | //! Compare operator
|
---|
| 62 | inline bool QwtIntervalSample::operator==(
|
---|
| 63 | const QwtIntervalSample &other ) const
|
---|
| 64 | {
|
---|
| 65 | return value == other.value && interval == other.interval;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | //! Compare operator
|
---|
| 69 | inline bool QwtIntervalSample::operator!=(
|
---|
| 70 | const QwtIntervalSample &other ) const
|
---|
| 71 | {
|
---|
| 72 | return !( *this == other );
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | //! \brief A sample of the types (x1...xn, y) or (x, y1..yn)
|
---|
| 76 | class QWT_EXPORT QwtSetSample
|
---|
| 77 | {
|
---|
| 78 | public:
|
---|
| 79 | QwtSetSample();
|
---|
| 80 | QwtSetSample( double, const QVector<double> & = QVector<double>() );
|
---|
| 81 |
|
---|
| 82 | bool operator==( const QwtSetSample &other ) const;
|
---|
| 83 | bool operator!=( const QwtSetSample &other ) const;
|
---|
| 84 |
|
---|
| 85 | double added() const;
|
---|
| 86 |
|
---|
| 87 | //! value
|
---|
| 88 | double value;
|
---|
| 89 |
|
---|
| 90 | //! Vector of values associated to value
|
---|
| 91 | QVector<double> set;
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | /*!
|
---|
| 95 | Constructor
|
---|
| 96 | The value is set to 0.0
|
---|
| 97 | */
|
---|
| 98 | inline QwtSetSample::QwtSetSample():
|
---|
| 99 | value( 0.0 )
|
---|
| 100 | {
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /*!
|
---|
| 104 | Constructor
|
---|
| 105 |
|
---|
| 106 | \param v Value
|
---|
| 107 | \param s Set of values
|
---|
| 108 | */
|
---|
| 109 | inline QwtSetSample::QwtSetSample( double v, const QVector< double > &s ):
|
---|
| 110 | value( v ),
|
---|
| 111 | set( s )
|
---|
| 112 | {
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | //! Compare operator
|
---|
| 116 | inline bool QwtSetSample::operator==( const QwtSetSample &other ) const
|
---|
| 117 | {
|
---|
| 118 | return value == other.value && set == other.set;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | //! Compare operator
|
---|
| 122 | inline bool QwtSetSample::operator!=( const QwtSetSample &other ) const
|
---|
| 123 | {
|
---|
| 124 | return !( *this == other );
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | //! \return All values of the set added
|
---|
| 128 | inline double QwtSetSample::added() const
|
---|
| 129 | {
|
---|
| 130 | double y = 0.0;
|
---|
| 131 | for ( int i = 0; i < set.size(); i++ )
|
---|
| 132 | y += set[i];
|
---|
| 133 |
|
---|
| 134 | return y;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /*!
|
---|
| 138 | \brief Open-High-Low-Close sample used in financial charts
|
---|
| 139 |
|
---|
| 140 | In financial charts the movement of a price in a time interval is often
|
---|
| 141 | represented by the opening/closing prices and the lowest/highest prices
|
---|
| 142 | in this interval.
|
---|
| 143 |
|
---|
| 144 | \sa QwtTradingChartData
|
---|
| 145 | */
|
---|
| 146 | class QWT_EXPORT QwtOHLCSample
|
---|
| 147 | {
|
---|
| 148 | public:
|
---|
| 149 | QwtOHLCSample( double time = 0.0,
|
---|
| 150 | double open = 0.0, double high = 0.0,
|
---|
| 151 | double low = 0.0, double close = 0.0 );
|
---|
| 152 |
|
---|
| 153 | QwtInterval boundingInterval() const;
|
---|
| 154 |
|
---|
| 155 | bool isValid() const;
|
---|
| 156 |
|
---|
| 157 | /*!
|
---|
| 158 | Time of the sample, usually a number representing
|
---|
| 159 | a specific interval - like a day.
|
---|
| 160 | */
|
---|
| 161 | double time;
|
---|
| 162 |
|
---|
| 163 | //! Opening price
|
---|
| 164 | double open;
|
---|
| 165 |
|
---|
| 166 | //! Highest price
|
---|
| 167 | double high;
|
---|
| 168 |
|
---|
| 169 | //! Lowest price
|
---|
| 170 | double low;
|
---|
| 171 |
|
---|
| 172 | //! Closing price
|
---|
| 173 | double close;
|
---|
| 174 | };
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | /*!
|
---|
| 178 | Constructor
|
---|
| 179 |
|
---|
| 180 | \param t Time value
|
---|
| 181 | \param o Open value
|
---|
| 182 | \param h High value
|
---|
| 183 | \param l Low value
|
---|
| 184 | \param c Close value
|
---|
| 185 | */
|
---|
| 186 | inline QwtOHLCSample::QwtOHLCSample( double t,
|
---|
| 187 | double o, double h, double l, double c ):
|
---|
| 188 | time( t ),
|
---|
| 189 | open( o ),
|
---|
| 190 | high( h ),
|
---|
| 191 | low( l ),
|
---|
| 192 | close( c )
|
---|
| 193 | {
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /*!
|
---|
| 197 | \brief Check if a sample is valid
|
---|
| 198 |
|
---|
| 199 | A sample is valid, when all of the following checks are true:
|
---|
| 200 |
|
---|
| 201 | - low <= high
|
---|
| 202 | - low <= open <= high
|
---|
| 203 | - low <= close <= high
|
---|
| 204 |
|
---|
| 205 | \return True, when the sample is valid
|
---|
| 206 | */
|
---|
| 207 | inline bool QwtOHLCSample::isValid() const
|
---|
| 208 | {
|
---|
| 209 | return ( low <= high )
|
---|
| 210 | && ( open >= low )
|
---|
| 211 | && ( open <= high )
|
---|
| 212 | && ( close >= low )
|
---|
| 213 | && ( close <= high );
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /*!
|
---|
| 217 | \brief Calculate the bounding interval of the OHLC values
|
---|
| 218 |
|
---|
| 219 | For valid samples the limits of this interval are always low/high.
|
---|
| 220 |
|
---|
| 221 | \return Bounding interval
|
---|
| 222 | \sa isValid()
|
---|
| 223 | */
|
---|
| 224 | inline QwtInterval QwtOHLCSample::boundingInterval() const
|
---|
| 225 | {
|
---|
| 226 | double minY = open;
|
---|
| 227 | minY = qMin( minY, high );
|
---|
| 228 | minY = qMin( minY, low );
|
---|
| 229 | minY = qMin( minY, close );
|
---|
| 230 |
|
---|
| 231 | double maxY = open;
|
---|
| 232 | maxY = qMax( maxY, high );
|
---|
| 233 | maxY = qMax( maxY, low );
|
---|
| 234 | maxY = qMax( maxY, close );
|
---|
| 235 |
|
---|
| 236 | return QwtInterval( minY, maxY );
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | #endif
|
---|