Repository navigation
Expand file tree
/
Copy pathCandleStick2D.cpp
More file actions
143 lines (128 loc) · 3.74 KB
/
Copy pathCandleStick2D.cpp
File metadata and controls
143 lines (128 loc) · 3.74 KB
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
#include "wplot/CandleStick2D.h"
#include "wplot/Plot2D.h"
#include <QPainter>
namespace WPlot {
///////////////////////////////////////////////////////////////////////////////
// PUBLIC SECTION //
///////////////////////////////////////////////////////////////////////////////
CandleStick2D::CandleStick2D() :
FinancialItem2D(),
m_lineWidth(1.0),
m_barWidth(10.0),
m_lineColor(Qt::black),
m_positiveColor(Qt::white),
m_negativeColor(Qt::black),
m_lineStyle(Qt::SolidLine)
{
m_pen.setColor(m_lineColor);
m_pen.setWidthF(m_lineWidth);
m_pen.setStyle(m_lineStyle);
m_brush.setStyle(Qt::SolidPattern);
}
/**
* Set the candlestick color when the close value is higher than the open
* value.
*
* @param[in] color Color.
*/
void CandleStick2D::setBarPositiveColor(const QColor &color) {
m_positiveColor = color;
}
/**
* Set the candlestick color when the close value is lower than the open
* value.
*
* @param[in] color Color.
*/
void CandleStick2D::setBarNegativeColor(const QColor &color) {
m_negativeColor = color;
}
/**
* Set candlestick colors that must be set when positive and when negative.
*
* @param[in] positive Color used when candlestick is positive.
* @param[in] negative Color used when candlestick is negative.
*/
void CandleStick2D::setBarColor(const QColor &positive, const QColor &negative) {
setBarPositiveColor(positive);
setBarNegativeColor(negative);
}
/**
* Set the color of candlestick lines.
*
* @param[in] color Color.
*/
void CandleStick2D::setLineColor(const QColor &color) {
m_lineColor = color;
m_pen.setColor(color);
}
/**
* Set the width of candlestick lines.
*
* @param[in] width Width.
*/
void CandleStick2D::setLineWidth(const double &width) {
m_lineWidth = width;
m_pen.setWidthF(width);
}
/**
* Set the style of candlestick lines.
*
* @param[in] style Style.
*/
void CandleStick2D::setLineStyle(const Qt::PenStyle &style) {
m_lineStyle = style;
m_pen.setStyle(style);
}
/**
* Set the width of the candlestick bar.
*
* @param[in] width Width.
*/
void CandleStick2D::setBarWidth(const double &width) {
m_barWidth = width;
}
///////////////////////////////////////////////////////////////////////////////
// VIRTUAL PUBLIC SECTION //
///////////////////////////////////////////////////////////////////////////////
/**
* Set the item color.
*
* It's an implementation of pure virtual method of base class used when in the
* Item2D the color is needed.
*
* @param[in] color Color.
*/
void CandleStick2D::setColor(const QColor &color) {
}
/**
* Draw the candlestick in specified plot.
*
* @param[in] plot Plot where candlestick must be drawn.
*/
void CandleStick2D::draw(Plot2D *plot) {
QPainter painter(plot);
plot->initializePainter(painter);
limitPainterInPaddingArea(painter, plot);
double rectTop;
double rectBottom;
painter.setPen(m_pen);
if (m_values.getClose() > m_values.getOpen()) {
m_brush.setColor(m_positiveColor);
rectTop = getScaledPositionY(m_values.getClose(), plot);
rectBottom = getScaledPositionY(m_values.getOpen(), plot);
} else {
m_brush.setColor(m_negativeColor);
rectTop = getScaledPositionY(m_values.getOpen(), plot);
rectBottom = getScaledPositionY(m_values.getClose(), plot);
}
painter.setBrush(m_brush);
const double barWidth = getScaledDimensionX(m_barWidth, plot);
const QPointF highPoint = getScaledPosition(m_xValue, m_values.getHigh(), plot);
const QPointF lowPoint = getScaledPosition(m_xValue, m_values.getLow(), plot);
const double rectLeft = getScaledPositionX(m_xValue, plot) - 0.5 * barWidth;
const QRectF rect(rectLeft, rectTop, barWidth, rectBottom - rectTop);
painter.drawLine(highPoint, lowPoint);
painter.drawRect(rect);
}
} // namespace WPlot