qwt_samples.h 4.84 KB
Newer Older
Bryant's avatar
Bryant committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_SAMPLES_H
#define QWT_SAMPLES_H 1

#include "qwt_global.h"
#include "qwt_interval.h"
#include <qvector.h>
#include <qrect.h>

//! \brief A sample of the types (x1-x2, y) or (x, y1-y2)
class QWT_EXPORT QwtIntervalSample
{
public:
    QwtIntervalSample();
    QwtIntervalSample( double, const QwtInterval & );
    QwtIntervalSample( double value, double min, double max );

    bool operator==( const QwtIntervalSample & ) const;
    bool operator!=( const QwtIntervalSample & ) const;

    //! Value
    double value;

    //! Interval
    QwtInterval interval;
};

/*!
  Constructor
  The value is set to 0.0, the interval is invalid
*/
inline QwtIntervalSample::QwtIntervalSample():
    value( 0.0 )
{
}

//! Constructor
inline QwtIntervalSample::QwtIntervalSample(
        double v, const QwtInterval &intv ):
    value( v ),
    interval( intv )
{
}

//! Constructor
inline QwtIntervalSample::QwtIntervalSample(
        double v, double min, double max ):
    value( v ),
    interval( min, max )
{
}

//! Compare operator
inline bool QwtIntervalSample::operator==(
    const QwtIntervalSample &other ) const
{
    return value == other.value && interval == other.interval;
}

//! Compare operator
inline bool QwtIntervalSample::operator!=(
    const QwtIntervalSample &other ) const
{
    return !( *this == other );
}

//! \brief A sample of the types (x1...xn, y) or (x, y1..yn)
class QWT_EXPORT QwtSetSample
{
public:
    QwtSetSample();
    QwtSetSample( double, const QVector<double> & = QVector<double>() );

    bool operator==( const QwtSetSample &other ) const;
    bool operator!=( const QwtSetSample &other ) const;

    double added() const;

    //! value
    double value;

    //! Vector of values associated to value
    QVector<double> set;
};

/*!
  Constructor
  The value is set to 0.0
*/
inline QwtSetSample::QwtSetSample():
    value( 0.0 )
{
}

/*!
  Constructor

  \param v Value
  \param s Set of values
*/
inline QwtSetSample::QwtSetSample( double v, const QVector< double > &s ):
    value( v ),
    set( s )
{
}

//! Compare operator
inline bool QwtSetSample::operator==( const QwtSetSample &other ) const
{
    return value == other.value && set == other.set;
}

//! Compare operator
inline bool QwtSetSample::operator!=( const QwtSetSample &other ) const
{
    return !( *this == other );
}

//! \return All values of the set added
inline double QwtSetSample::added() const
{
    double y = 0.0;
    for ( int i = 0; i < set.size(); i++ )
        y += set[i];

    return y;
}

/*!
   \brief Open-High-Low-Close sample used in financial charts

   In financial charts the movement of a price in a time interval is often
   represented by the opening/closing prices and the lowest/highest prices
   in this interval.

   \sa QwtTradingChartData
*/
class QWT_EXPORT QwtOHLCSample
{
public:
    QwtOHLCSample( double time = 0.0,
        double open = 0.0, double high = 0.0,
        double low = 0.0, double close = 0.0 );

    QwtInterval boundingInterval() const;

    bool isValid() const;

    /*!
      Time of the sample, usually a number representing
      a specific interval - like a day.
    */
    double time;

    //! Opening price
    double open;

    //! Highest price
    double high;

    //! Lowest price
    double low;

    //! Closing price
    double close;
};


/*!
  Constructor

  \param t Time value
  \param o Open value
  \param h High value
  \param l Low value
  \param c Close value
*/
inline QwtOHLCSample::QwtOHLCSample( double t,
        double o, double h, double l, double c ):
    time( t ),
    open( o ),
    high( h ),
    low( l ),
    close( c )
{
}

/*!
  \brief Check if a sample is valid

  A sample is valid, when all of the following checks are true:

  - low <= high
  - low <= open <= high
  - low <= close <= high

  \return True, when the sample is valid
 */
inline bool QwtOHLCSample::isValid() const
{
    return ( low <= high )
        && ( open >= low )
        && ( open <= high )
        && ( close >= low )
        && ( close <= high );
}

/*!
   \brief Calculate the bounding interval of the OHLC values

   For valid samples the limits of this interval are always low/high.

   \return Bounding interval
   \sa isValid()
 */
inline QwtInterval QwtOHLCSample::boundingInterval() const
{
    double minY = open;
    minY = qMin( minY, high );
    minY = qMin( minY, low );
    minY = qMin( minY, close );

    double maxY = open;
    maxY = qMax( maxY, high );
    maxY = qMax( maxY, low );
    maxY = qMax( maxY, close );

    return QwtInterval( minY, maxY );
}

#endif