-
Notifications
You must be signed in to change notification settings - Fork 3
/
axis.cpp
157 lines (132 loc) · 3.31 KB
/
axis.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
#include "axis.h"
Axis::Axis(int x, int y, int w, int h, QObject *parent) :
QObject(parent)
{
graph_x = x;
graph_y = y;
graph_w = w;
graph_h = h;
xUnits = 50;
yUnits = 50;
xMin = float(graph_x);
xMax = float (graph_x + graph_w);
yMin = float(graph_y);
yMax = float(graph_y + graph_h);
}
void Axis::showAxis(QPainter *painter)
{
setDefaultLineProperties();
painter->setPen(axisLineProperties);
drawXaxis(painter);
drawYaxis(painter);
}
void Axis::setGeometry(int x, int y, int w, int h)
{
graph_x = x;
graph_y = y;
graph_w = w;
graph_h = h;
}
void Axis::setYaxisProperties()
{
double _scYmax = qRound(yMax / yUnits) * yUnits;
if(_scYmax < yMax) _scYmax = _scYmax + yUnits;
double _scYmin = qRound(yMin / yUnits) * yUnits;
if(_scYmin > yMin) _scYmin = _scYmin - yUnits;
YaxisMax = _scYmax;
YaxisMin = _scYmin;
}
/**
* @brief Axis::setYaxisAutoScale
* Funkcja automatycznie dobiera warość zmiennej yUnits do danych na wykresie tak aby siatka
* nie zawierała domyślnie więcej niż 10 linii.
*/
void Axis::setYaxisAutoScale()
{
double temp = (yMax - yMin) / 10;
if (temp >= 1 && temp <= 10)
{
yUnits = 10;
}
else if(temp > 10 && temp <= 50)
{
yUnits = 50;
}
else if(temp > 50 && temp <= 100)
{
yUnits = 100;
}
else if(temp > 100 && temp <= 200)
{
yUnits = 200;
}
else if(temp > 200 && temp <= 500)
{
yUnits = 500;
}
else if(temp > 500 && temp <= 1000)
{
yUnits = 1000;
}
else if(temp > 1000 && temp <= 1500)
{
yUnits = 1500;
}
else if(temp > 1500 && temp <= 2000)
{
yUnits = 1500;
}
}
void Axis::getMinMax(double x_min, double x_max, double y_min, double y_max)
{
xMin = x_min;
xMax = x_max;
yMin = y_min;
yMax = y_max;
}
void Axis::setLineData(QVector<long long> x_data, QVector<QVector<float> > y_data)
{
x_data_list = x_data;
y_data_list = y_data;
}
void Axis::drawXaxis(QPainter *painter)
{
painter->drawLine(graph_x,graph_y + graph_h, graph_x + graph_w, graph_y +graph_h);
int div = xMax - xMin;
double dt = div/10;
int idx = 0;
for(int i = 0 ; i <= graph_w ; i += xUnits)
{
painter->drawLine(i + graph_x, graph_h + graph_y + 5, i + graph_x, graph_h + graph_y - 10);
QTime time = QTime::fromMSecsSinceStartOfDay((xMin + idx) * 1000);
QString t_str = time.toString("hh:mm");
idx += 720;
painter->drawText(i + graph_x, graph_h + graph_y + 15, t_str);
}
idx = 0;
visibleX = true;
}
void Axis::drawYaxis(QPainter *painter)
{
setYaxisAutoScale();
setYaxisProperties();
double idx = 0;
int lines = (YaxisMax - YaxisMin) / yUnits;
double space = graph_h / lines;
painter->drawLine(graph_x,graph_y, graph_x, graph_y + graph_h);
for(int i = 0; i <= graph_h; i += space)
{
QString label = QString::number(YaxisMin + idx);
painter->drawText(graph_x - 20, graph_h - i + graph_y, label);
idx += yUnits;
}
idx = 0;
visibleY = true;
emit unitsSetted(xUnits, space);
}
void Axis::setDefaultLineProperties()
{
axisLineProperties.setColor(Qt::black);
axisLineProperties.setStyle(Qt::SolidLine);
axisLineProperties.setWidth(2);
}