Main Page · Class Overview · Hierarchy · All Classes
plottable-financial.h
Go to the documentation of this file.
1 /***************************************************************************
2 ** **
3 ** QCustomPlot, an easy to use, modern plotting widget for Qt **
4 ** Copyright (C) 2011, 2012, 2013, 2014 Emanuel Eichhammer **
5 ** **
6 ** This program is free software: you can redistribute it and/or modify **
7 ** it under the terms of the GNU General Public License as published by **
8 ** the Free Software Foundation, either version 3 of the License, or **
9 ** (at your option) any later version. **
10 ** **
11 ** This program is distributed in the hope that it will be useful, **
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
14 ** GNU General Public License for more details. **
15 ** **
16 ** You should have received a copy of the GNU General Public License **
17 ** along with this program. If not, see http://www.gnu.org/licenses/. **
18 ** **
19 ****************************************************************************
20 ** Author: Emanuel Eichhammer **
21 ** Website/Contact: http://www.qcustomplot.com/ **
22 ** Date: 27.12.14 **
23 ** Version: 1.3.0 **
24 ****************************************************************************/
26 #ifndef QCP_PLOTTABLE_FINANCIAL_H
27 #define QCP_PLOTTABLE_FINANCIAL_H
28 
29 #include "../global.h"
30 #include "../range.h"
31 #include "../plottable.h"
32 #include "../painter.h"
33 
34 class QCPPainter;
35 class QCPAxis;
36 
37 class QCP_LIB_DECL QCPFinancialData
38 {
39 public:
41  QCPFinancialData(double key, double open, double high, double low, double close);
42  double key, open, high, low, close;
43 };
44 Q_DECLARE_TYPEINFO(QCPFinancialData, Q_MOVABLE_TYPE);
45 
53 typedef QMap<double, QCPFinancialData> QCPFinancialDataMap;
54 typedef QMapIterator<double, QCPFinancialData> QCPFinancialDataMapIterator;
55 typedef QMutableMapIterator<double, QCPFinancialData> QCPFinancialDataMutableMapIterator;
56 
57 
58 class QCP_LIB_DECL QCPFinancial : public QCPAbstractPlottable
59 {
60  Q_OBJECT
62  Q_PROPERTY(ChartStyle chartStyle READ chartStyle WRITE setChartStyle)
63  Q_PROPERTY(double width READ width WRITE setWidth)
64  Q_PROPERTY(bool twoColored READ twoColored WRITE setTwoColored)
65  Q_PROPERTY(QBrush brushPositive READ brushPositive WRITE setBrushPositive)
66  Q_PROPERTY(QBrush brushNegative READ brushNegative WRITE setBrushNegative)
67  Q_PROPERTY(QPen penPositive READ penPositive WRITE setPenPositive)
68  Q_PROPERTY(QPen penNegative READ penNegative WRITE setPenNegative)
70 public:
76  enum ChartStyle { csOhlc
77  ,csCandlestick
78  };
79  Q_ENUMS(ChartStyle)
80 
81  explicit QCPFinancial(QCPAxis *keyAxis, QCPAxis *valueAxis);
82  virtual ~QCPFinancial();
83 
84  // getters:
85  QCPFinancialDataMap *data() const { return mData; }
86  ChartStyle chartStyle() const { return mChartStyle; }
87  double width() const { return mWidth; }
88  bool twoColored() const { return mTwoColored; }
89  QBrush brushPositive() const { return mBrushPositive; }
90  QBrush brushNegative() const { return mBrushNegative; }
91  QPen penPositive() const { return mPenPositive; }
92  QPen penNegative() const { return mPenNegative; }
93 
94 
95  // setters:
96  void setData(QCPFinancialDataMap *data, bool copy=false);
97  void setData(const QVector<double> &key, const QVector<double> &open, const QVector<double> &high, const QVector<double> &low, const QVector<double> &close);
98  void setChartStyle(ChartStyle style);
99  void setWidth(double width);
100  void setTwoColored(bool twoColored);
101  void setBrushPositive(const QBrush &brush);
102  void setBrushNegative(const QBrush &brush);
103  void setPenPositive(const QPen &pen);
104  void setPenNegative(const QPen &pen);
105 
106  // non-property methods:
107  void addData(const QCPFinancialDataMap &dataMap);
108  void addData(const QCPFinancialData &data);
109  void addData(double key, double open, double high, double low, double close);
110  void addData(const QVector<double> &key, const QVector<double> &open, const QVector<double> &high, const QVector<double> &low, const QVector<double> &close);
111  void removeDataBefore(double key);
112  void removeDataAfter(double key);
113  void removeData(double fromKey, double toKey);
114  void removeData(double key);
115 
116  // reimplemented virtual methods:
117  virtual void clearData();
118  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const;
119 
120  // static methods:
121  static QCPFinancialDataMap timeSeriesToOhlc(const QVector<double> &time, const QVector<double> &value, double timeBinSize, double timeBinOffset = 0);
122 
123 protected:
124  // property members:
125  QCPFinancialDataMap *mData;
126  ChartStyle mChartStyle;
127  double mWidth;
128  bool mTwoColored;
129  QBrush mBrushPositive, mBrushNegative;
130  QPen mPenPositive, mPenNegative;
131 
132  // reimplemented virtual methods:
133  virtual void draw(QCPPainter *painter);
134  virtual void drawLegendIcon(QCPPainter *painter, const QRectF &rect) const;
135  virtual QCPRange getKeyRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const;
136  virtual QCPRange getValueRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const;
137 
138  // non-virtual methods:
139  void drawOhlcPlot(QCPPainter *painter, const QCPFinancialDataMap::const_iterator &begin, const QCPFinancialDataMap::const_iterator &end);
140  void drawCandlestickPlot(QCPPainter *painter, const QCPFinancialDataMap::const_iterator &begin, const QCPFinancialDataMap::const_iterator &end);
141  double ohlcSelectTest(const QPointF &pos, const QCPFinancialDataMap::const_iterator &begin, const QCPFinancialDataMap::const_iterator &end) const;
142  double candlestickSelectTest(const QPointF &pos, const QCPFinancialDataMap::const_iterator &begin, const QCPFinancialDataMap::const_iterator &end) const;
143  void getVisibleDataBounds(QCPFinancialDataMap::const_iterator &lower, QCPFinancialDataMap::const_iterator &upper) const;
144 
145  friend class QCustomPlot;
146  friend class QCPLegend;
147 };
148 
149 #endif // QCP_PLOTTABLE_FINANCIAL_H
virtual QCPRange getValueRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const =0
Represents the range an axis is encompassing.
Definition: range.h:31
virtual QCPRange getKeyRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const =0
virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const =0
The central class of the library. This is the QWidget which displays the plot and interacts with the ...
Definition: core.h:42
virtual void draw(QCPPainter *painter)=0
Manages a legend inside a QCustomPlot.
Definition: layoutelement-legend.h:126
QPainter subclass used internally.
Definition: painter.h:111
QMap< double, QCPFinancialData > QCPFinancialDataMap
Definition: plottable-financial.h:53
Manages a single axis inside a QCustomPlot.
Definition: axis.h:94
The abstract base class for all data representing objects in a plot.
Definition: plottable.h:36
ChartStyle
Definition: plottable-financial.h:76
A plottable representing a financial stock chart.
Definition: plottable-financial.h:58
virtual void drawLegendIcon(QCPPainter *painter, const QRectF &rect) const =0
Holds the data of one single data point for QCPFinancial.
Definition: plottable-financial.h:37
virtual void clearData()=0