-
Notifications
You must be signed in to change notification settings - Fork 0
/
paintlabel.h
292 lines (237 loc) · 9.35 KB
/
paintlabel.h
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef PAINTLABEL_H
#define PAINTLABEL_H
#include<QPainter>
#include <QLabel>
#include <QPainter>
#include <QDebug>
#include"data.h"
#include <QtWidgets/qframe.h>
#include <QWidget>
#include <sys/time.h>
#include<QPen>
#include<time.h>
#include<QVector>
#include"info.h"
#define MAXTEM 50
#define MAXX 30
#define min(x,y) ((x) >(y)?(y):(x))
#define inf 0x7fffffff
class PaintLabel:public QLabel
{
Q_OBJECT
public:
explicit PaintLabel(QWidget *parent = 0):QLabel(parent){}
void paintR(QPainter& painter){
int pointx=20,pointy=this->height() - 20;//确定坐标轴起点坐标
int width = this->width() - pointx -10;
int height = this->height() - 30;
painter.fillRect(pointx,10,width,height,Qt::white);
painter.drawLine(pointx,10,pointx,pointy);
painter.drawLine(pointx,pointy,this->width() - 10,pointy);
double ky = (double)height/MAXTEM;
double kx=(double)width/30;//y方向的比例系数
//y轴刻度线
double _maStep=2;//y轴刻度间隔需根据最大值来表示
for(int i=0;i<25;i++)
{
//主要就是确定一个位置,然后画一条短短的直线表示刻度。
painter.drawLine(pointx,pointy-(i+1)*height/25,
pointx-4,pointy-(i+1)*height/25);
if(i% 2 == 0){
painter.drawText(pointx-20,pointy-(i+0.85)*height/25,
QString::number((int)(_maStep*(i+1))));
}
}
}
void paintA(QPainter& painter){
QVector<Data> ds = INFO::cus_A;
QPen p;
p.setColor(Qt::black);
p.setWidth(2);
painter.setPen(p);
int pointx=20,pointy=this->height() - 20;//确定坐标轴起点坐标
int width = this->width() - pointx -10;
int height = this->height() - 30;
int prx = pointx + width;
double ky = (double)height/MAXTEM;
double kx=(double)width/30;//y方向的比例系数
//y轴刻度线
double _maStep=2;//y轴刻度间隔需根据最大值来表示
int n =ds.size();
double sum=0;
double ave=0;
int _ma=0;//数组里的最大值
int _mi=inf;
int maxpos=0,minpos=0;
QPen pen,penPoint;
pen.setColor(Qt::green);
pen.setWidth(1);
penPoint.setColor(Qt::yellow);
penPoint.setWidth(2);
if(n <= 30){
for(int i =0;i < n;i++){
if(ds[i].temper >_ma){
maxpos = i;
_ma = ds[i].temper;
}
if(ds[i].temper< _mi){
minpos = i;
_mi = ds[i].temper;
}
sum += ds[i].temper;
}
for(int i = n-1;i>0;i--){
painter.setPen(pen);//黑色笔用于连线
painter.drawLine(prx -(n-1-i)*kx,pointy-ds[i].temper*ky,prx- kx*(n-1-i+1),pointy-ds[i-1].temper*ky);
painter.setPen(penPoint);//蓝色的笔,用于标记各个点
painter.drawPoint(prx- kx*(n-1-i),pointy-ds[i].temper*ky);
}
if(n>0)
painter.drawPoint(prx- kx*(n-1),pointy-ds[0].temper*ky);//绘制最后一个点
}else{
int start = n - 30;
for(int i =start;i < n;i++){
if(ds[i].temper >_ma){
maxpos = i;
_ma = ds[i].temper;
}
if(ds[i].temper< _mi){
minpos = i;
_mi = ds[i].temper;
}
sum += ds[i].temper;
}
for(int i = n-1;i>start;i--){
painter.setPen(pen);//黑色笔用于连线
painter.drawLine(prx -(n-1-i)*kx,pointy-ds[i].temper*ky,prx- kx*(n-1-i+1),pointy-ds[i-1].temper*ky);
painter.setPen(penPoint);//蓝色的笔,用于标记各个点
painter.drawPoint(prx- kx*(n-1-i),pointy-ds[i].temper*ky);
}
painter.drawPoint(prx- kx*(n-1),pointy-ds[start].temper*ky);//绘制最后一个点
}
ave=sum/min(MAXX,ds.size());//平均数
//绘制平均线
QPen penAve;
penAve.setColor(QColor(140,50,220));//选择红色
penAve.setWidth(1);
penAve.setStyle(Qt::DotLine);//线条类型为虚线
painter.setPen(penAve);
painter.drawLine(pointx,pointy-ave*ky,pointx+width,pointy-ave*ky);
//绘制最大值和最小值
if(n >0){
QPen penMaxMin;
penMaxMin.setColor(Qt::darkGreen);//暗绿色
painter.setPen(penMaxMin);
painter.drawText(prx - kx*(n - maxpos -1),pointy-ds[maxpos].temper*ky-5,
"最大值"+QString::number(_ma));
painter.drawText(prx-kx*(n -minpos -1),pointy-ds[minpos].temper*ky+15,
"最小值"+QString::number(_mi));
penMaxMin.setColor(Qt::red);
penMaxMin.setWidth(4);
painter.setPen(penMaxMin);
painter.drawPoint(prx -kx*(n -maxpos -1),pointy-ds[maxpos].temper*ky);//标记最大值点
painter.drawPoint(prx - kx*(n - minpos-1),pointy-ds[minpos].temper*ky);//标记最小值点
}
}
void paintB(QPainter& painter){
QVector<Data> ds = INFO::cus_B;
QPen p;
p.setColor(Qt::black);
p.setWidth(2);
painter.setPen(p);
int pointx=20,pointy=this->height() - 20;//确定坐标轴起点坐标
int width = this->width() - pointx -10;
int height = this->height() - 30;
int prx = pointx + width;
double ky = (double)height/MAXTEM;
double kx=(double)width/30;//y方向的比例系数
//y轴刻度线
double _maStep=2;//y轴刻度间隔需根据最大值来表示
int n =ds.size();
double sum=0;
double ave=0;
int _ma=0;//数组里的最大值
int _mi=inf;//inf为 #define inf 0x3f3f3f3f
int maxpos=0,minpos=0;
QPen pen,penPoint;
pen.setColor(Qt::black);
pen.setWidth(1);
penPoint.setColor(Qt::blue);
penPoint.setWidth(2);
if(n <= 30){
for(int i =0;i < n;i++){
if(ds[i].temper >_ma){
maxpos = i;
_ma = ds[i].temper;
}
if(ds[i].temper< _mi){
minpos = i;
_mi = ds[i].temper;
}
sum += ds[i].temper;
}
for(int i = n-1;i>0;i--){
painter.setPen(pen);//黑色笔用于连线
painter.drawLine(prx -(n-1-i)*kx,pointy-ds[i].temper*ky,prx- kx*(n-1-i+1),pointy-ds[i-1].temper*ky);
painter.setPen(penPoint);//蓝色的笔,用于标记各个点
painter.drawPoint(prx- kx*(n-1-i),pointy-ds[i].temper*ky);
}
if(n>0)
painter.drawPoint(prx- kx*(n-1),pointy-ds[0].temper*ky);//绘制最后一个点
}else{
int start = n - 30;
for(int i =start;i < n;i++){
if(ds[i].temper >_ma){
maxpos = i;
_ma = ds[i].temper;
}
if(ds[i].temper< _mi){
minpos = i;
_mi = ds[i].temper;
}
sum += ds[i].temper;
}
for(int i = n-1;i>start;i--){
painter.setPen(pen);//黑色笔用于连线
painter.drawLine(prx -(n-1-i)*kx,pointy-ds[i].temper*ky,prx- kx*(n-1-i+1),pointy-ds[i-1].temper*ky);
painter.setPen(penPoint);//蓝色的笔,用于标记各个点
painter.drawPoint(prx- kx*(n-1-i),pointy-ds[i].temper*ky);
}
painter.drawPoint(prx- kx*(n-1),pointy-ds[start].temper*ky);//绘制最后一个点
}
ave=sum/min(MAXX,ds.size());//平均数
//绘制平均线
QPen penAve;
penAve.setColor(Qt::red);//选择红色
penAve.setWidth(1);
penAve.setStyle(Qt::DotLine);//线条类型为虚线
painter.setPen(penAve);
painter.drawLine(pointx,pointy-ave*ky,pointx+width,pointy-ave*ky);
//绘制最大值和最小值
if(n >0){
QPen penMaxMin;
penMaxMin.setColor(Qt::darkGreen);//暗绿色
painter.setPen(penMaxMin);
painter.drawText(prx - kx*(n - maxpos -1),pointy-ds[maxpos].temper*ky-5,
"最大值"+QString::number(_ma));
painter.drawText(prx-kx*(n -minpos -1),pointy-ds[minpos].temper*ky+15,
"最小值"+QString::number(_mi));
penMaxMin.setColor(Qt::red);
penMaxMin.setWidth(4);
painter.setPen(penMaxMin);
painter.drawPoint(prx -kx*(n -maxpos -1),pointy-ds[maxpos].temper*ky);//标记最大值点
painter.drawPoint(prx - kx*(n - minpos-1),pointy-ds[minpos].temper*ky);//标记最小值点
}
}
void paintEvent(QPaintEvent *event)
{
QVector<Data> ds = INFO::cus_B;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
paintR(painter);
paintA(painter);
paintB(painter);
QLabel::paintEvent(event);
}
};
#endif // PAINTLABEL_H