-
Notifications
You must be signed in to change notification settings - Fork 98
/
kvolumegrid.cpp
234 lines (179 loc) · 5.63 KB
/
kvolumegrid.cpp
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
#include "kvolumegrid.h"
#include <QMessageBox>
#include <QPainter>
#include <QPen>
#include <QString>
kVolumeGrid::kVolumeGrid(QWidget* parent) : AutoGrid( parent)
{
setAtomGridHeightMin(40);
initial();
}
void kVolumeGrid::paintEvent(QPaintEvent *event)
{
AutoGrid::paintEvent(event);
//画y轴坐标轴
drawYtick();
drawVolume();
drawAverageLine(5);
drawAverageLine(10);
}
void kVolumeGrid::initial()
{
//读取数据
QString file = tr("dataKLine.txt");
if( !mDataFile.readData(file) )
{
QMessageBox::about(this,tr("数据文件读取失败"),tr("确定"));
return ;
}
//初始化一些成员变量值
endDay = mDataFile.kline.size() - 1;
totalDay = 200;
beginDay = endDay - totalDay;
currentDay = beginDay + totalDay /2;
maxVolume = 0;
}
bool kVolumeGrid::readData(QString strFile)
{
if( mDataFile.readData(strFile) )
return true;
else
return false;
}
//获得画图所需的各项指标
void kVolumeGrid::getIndicator()
{
maxVolume = 0;
for(int i=beginDay;i<endDay;++i)
{
QString strVolume = mDataFile.kline[i].totalVolume;
strVolume = strVolume.mid(1,strVolume.length());
strVolume = strVolume.mid(0,strVolume.length()-1);
strVolume.replace(QString(","),QString(""));
if( strVolume.toInt() > maxVolume)
maxVolume = strVolume.toInt();
}
maxVolume = maxVolume / 100;
}
void kVolumeGrid::drawYtick()
{
getIndicator();
QPainter painter(this);
QPen pen;
pen.setColor(Qt::red);
painter.setPen(pen);
double ystep = maxVolume / getHGridNum() ;
QString str;
for( int i=0;i<=getHGridNum();++i)
{
str.sprintf("%d", (int)(i*ystep) );
painter.drawText( QPoint( getWidgetWidth() - getMarginLeft() + 10,
getWidgetHeight() - getMarginBottom() - i*getAtomGridHeight()),
str);
}
}
void kVolumeGrid::drawVolume()
{
QPainter painter(this);
QPen pen;
double xstep = getGridWidth() / totalDay;
double yscale = getGridHeight() / maxVolume;
for( int i= beginDay;i<endDay;++i)
{
if( mDataFile.kline[i].openingPrice > mDataFile.kline[i].closeingPrice )
pen.setColor(QColor(85,252,252));
else
pen.setColor(Qt::red);
lineWidth = getGridWidth() / totalDay;
//为了各个k线之间不贴在一起,设置一个间隔
lineWidth = lineWidth - 0.2*lineWidth;
//最小线宽为3
if( lineWidth < 3)
lineWidth = 3;
//画线连接的两个点
QPoint p1;
QPoint p2;
QPoint p3;
QPoint p4;
QString strtemp = mDataFile.kline[i].totalVolume;
strtemp = strtemp.mid(1,strtemp.length());
strtemp = strtemp.mid(0,strtemp.length()-1);
strtemp.replace(QString(","),QString(""));
int temp = strtemp.toInt()/100;
//阴线
if( mDataFile.kline[i].openingPrice > mDataFile.kline[i].closeingPrice )
{
pen.setWidth(lineWidth);
painter.setPen(pen);
p1.setX( getMarginLeft() + xstep *(i - beginDay) + 0.5*lineWidth);
p1.setY( getWidgetHeight() - (temp ) *yscale - getMarginBottom());
p2.setX( getMarginLeft() + xstep *(i - beginDay) + 0.5*lineWidth);
p2.setY( getWidgetHeight() - getMarginBottom() - 0.5*lineWidth);
painter.drawLine(p1,p2);
}
//阳线
else
{
pen.setWidth(1);
painter.setPen(pen);
p1.setX( getMarginLeft() + xstep *(i - beginDay) );
p1.setY( getWidgetHeight() - (temp ) *yscale - getMarginBottom());
p2.setX(getMarginLeft() + xstep *(i - beginDay) + lineWidth);
p2.setY( getWidgetHeight() - (temp ) *yscale - getMarginBottom());
p3.setX( getMarginLeft() + xstep *(i - beginDay) );
p3.setY( getWidgetHeight() - getMarginBottom() );
p4.setX( getMarginLeft() + xstep *(i - beginDay) + lineWidth);
p4.setY( getWidgetHeight() - getMarginBottom() );
painter.drawLine(p1,p2);
painter.drawLine(p1,p3);
painter.drawLine(p2,p4);
painter.drawLine(p3,p4);
}
}
}
void kVolumeGrid::drawAverageLine(int day){
//画线要连接的点
QVector<QPoint> point;
//临时点
QPoint temp;
//x轴步进
double xstep = getGridWidth() / totalDay;
double yscale = getGridHeight() / maxVolume;
switch(day)
{
case 5:
for( int i= beginDay;i<endDay;++i)
{
if( mDataFile.kline[i].volumeAverage5 == 0)
continue;
temp.setX(getMarginLeft() + xstep *(i - beginDay) + 0.5*lineWidth);
temp.setY(getWidgetHeight() - mDataFile.kline[i].volumeAverage5 /100 *yscale - getMarginBottom());
point.push_back(temp);
}
break;
case 10:
for( int i= beginDay;i<endDay;++i)
{
if( mDataFile.kline[i].volumeAverage10 == 0)
continue;
temp.setX(getMarginLeft() + xstep *(i - beginDay) + 0.5*lineWidth);
temp.setY(getWidgetHeight() - mDataFile.kline[i].volumeAverage10 /100 *yscale - getMarginBottom());
point.push_back(temp);
}
break;
}
QPainter painter(this);
QPen pen;
switch(day)
{
case 5:
pen.setColor(Qt::white);
break;
case 10:
pen.setColor(Qt::yellow);
break;
}
painter.setPen(pen);
QPolygon polykline(point);
painter.drawPolyline(polykline);
}