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
293
294
295
296
297
298
299
300
301
302
303
304
|
#include "qwt3d_scale.h"
using namespace Qwt3D;
Scale::Scale()
: start_p(0.), stop_p(0.),
majorintervals_p(0), minorintervals_p(0),
mstart_p(0.), mstop_p(0.)
{
}
/*! The function maps the double value at tic-position idx to a final
representation. The default return value is simply the tic values QString
representation. Overwrite this function, if you plan to transform the value
in some way. See e.g. LogScale::ticLabel.
\param idx the current major tic index
\return The QString representation for the value corresponding to a valid index,
an empty QString else.
*/
QString Scale::ticLabel(unsigned int idx) const
{
if (idx<majors_p.size())
{
return QString::number(majors_p[idx]);
}
return QString("");
}
//! Sets start and stop value for the scale;
void Scale::setLimits(double start, double stop)
{
if (start < stop)
{
start_p = start;
stop_p = stop;
return;
}
start_p = stop;
stop_p = start;
}
//! Sets value of first major tic
void Scale::setMajorLimits(double start, double stop)
{
if (start < stop)
{
mstart_p = start;
mstop_p = stop;
return;
}
mstart_p = stop;
mstop_p = start;
}
/*!
\param a First major tic after applying autoscaling
\param b Last major tic after applying autoscaling
\param start Scale begin
\param stop Scale end
\param ivals Requested number of major intervals
\return Number of major intervals after autoscaling\n
The default implementation sets a=start, b=stop and returns ivals.
*/
int Scale::autoscale(double& a, double& b, double start, double stop, int ivals)
{
a = start;
b = stop;
return ivals;
}
/***************************
*
* linear scales
*
***************************/
//! Applies LinearAutoScaler::execute()
int LinearScale::autoscale(double& a, double& b, double start, double stop, int ivals)
{
return autoscaler_p.execute(a, b, start, stop, ivals);
}
//! Creates the major and minor vector for the scale
void LinearScale::calculate()
{
majors_p.clear();
minors_p.clear();
double interval = mstop_p-mstart_p;
double runningval;
int i=0;
// majors
// first tic
// if (mstart_p<start_p || mstop_p>stop_p)
// return;
majors_p.push_back(mstart_p);
// remaining tics
for (i = 1; i <= majorintervals_p; ++i)
{
double t = double(i) / majorintervals_p;
runningval = mstart_p + t * interval;
if (runningval>stop_p)
break;
if (isPracticallyZero(mstart_p, -t*interval)) // prevent rounding errors near 0
runningval = 0.0;
majors_p.push_back(runningval);
}
majorintervals_p = majors_p.size();
if (majorintervals_p)
--majorintervals_p;
// minors
if (!majorintervals_p || !minorintervals_p) // no valid interval
{
minorintervals_p = 0;
return;
}
// start_p mstart_p
// |_____________|_____ _ _ _
double step = (majors_p[1]-majors_p[0]) / minorintervals_p;
if (isPracticallyZero(step))
return;
runningval = mstart_p-step;
while (runningval>start_p)
{
minors_p.push_back(runningval);
runningval -= step;
}
// mstart_p mstop_p
// ________|_____ _ _ _ _ _ ___|__________
for (i=0; i!=majorintervals_p; ++i)
{
runningval = majors_p[i] + step;
for (int j=0; j!=minorintervals_p; ++j)
{
minors_p.push_back(runningval);
runningval += step;
}
}
// mstop_p stop_p
// _ _ _|_____________|
runningval = mstop_p + step;
while (runningval<stop_p)
{
minors_p.push_back(runningval);
runningval += step;
}
}
void LogScale::setupCounter(double& k, int& step)
{
switch(minorintervals_p)
{
case 9:
k=9;
step=1;
break;
case 5:
k=8;
step=2;
break;
case 3:
k=5;
step=3;
break;
case 2:
k=5;
step=5;
break;
default:
k=9;
step=1;
}
}
/*! Creates major and minor vectors for the scale.
\warning If the interval is too small, the scale becomes empty
or will contain only a single major tic. There is no automatism
(also not planned for now) for an 'intelligent' guess, what to do.
Better switch manually to linear to scales in such cases.
*/
void LogScale::calculate()
{
majors_p.clear();
minors_p.clear();
if (start_p < DBL_MIN_10_EXP)
start_p = DBL_MIN_10_EXP;
if (stop_p > DBL_MAX_10_EXP)
stop_p = DBL_MAX_10_EXP;
double interval = stop_p-start_p;
if (interval<=0)
return;
double runningval = floor(start_p);
while(runningval<=stop_p)
{
if (runningval>=start_p)
majors_p.push_back(runningval);
++runningval;
}
majorintervals_p = majors_p.size();
if (majorintervals_p)
--majorintervals_p;
if (majors_p.size()<1) // not even a single major tic
{
return;
}
// minors
// start_p mstart_p
// |_____________|_____ _ _ _
double k;
int step;
setupCounter(k,step);
runningval = log10(k)+(majors_p[0]-1);
while (runningval>start_p && k>1)
{
minors_p.push_back(runningval);
k -=step;
runningval = log10(k)+(majors_p[0]-1);
}
// mstart_p mstop_p
// ________|_____ _ _ _ _ _ ___|__________
for (int i=0; i!=majorintervals_p; ++i)
{
setupCounter(k,step);
runningval = log10(k)+(majors_p[i]);
while (k>1)
{
minors_p.push_back(runningval);
k-=step;
runningval = log10(k)+(majors_p[i]);
}
}
// mstop_p stop_p
// _ _ _|_____________|
setupCounter(k,step);
runningval = log10(k)+(majors_p.back());
do
{
k-=step;
runningval = log10(k)+(majors_p.back());
}
while(runningval>=stop_p);
while (k>1)
{
minors_p.push_back(runningval);
k-=step;
runningval = log10(k)+(majors_p.back());
}
}
/*!
Sets the minor intervals for the logarithmic scale. Only values of 9,5,3 or 2
are accepted as arguments. They will produce mantissa sets of {2,3,4,5,6,7,8,9},
{2,4,6,8}, {2,5} or {5} respectively.
*/
void LogScale::setMinors(int val)
{
if ((val == 2) || (val == 3) || (val == 5) || (val == 9))
minorintervals_p = val;
}
LogScale::LogScale()
{
minorintervals_p = 9;
}
//! Returns a power of 10 associated to the major value at index idx.
QString LogScale::ticLabel(unsigned int idx) const
{
if (idx<majors_p.size())
{
double val = majors_p[idx];
return QString::number(pow(double(10), val));
}
return QString("");
}
|