qwt_samples.h 4.8 KB

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