comparison layer/LogNumericalScale.cpp @ 1260:cdaeff1858af scale-ticks

Make use of ScaleTickIntervals in log scale as well
author Chris Cannam
date Thu, 04 May 2017 15:42:40 +0100
parents 5144d7185fb5
children a34a2a25907c
comparison
equal deleted inserted replaced
1259:4d7e566092a7 1260:cdaeff1858af
22 22
23 #include <cmath> 23 #include <cmath>
24 24
25 #include "LayerGeometryProvider.h" 25 #include "LayerGeometryProvider.h"
26 26
27 //#define DEBUG_TIME_VALUE_LAYER 1 27 #include "base/ScaleTickIntervals.h"
28 28
29 int 29 int
30 LogNumericalScale::getWidth(LayerGeometryProvider *, 30 LogNumericalScale::getWidth(LayerGeometryProvider *,
31 QPainter &paint) 31 QPainter &paint)
32 { 32 {
39 QPainter &paint, 39 QPainter &paint,
40 int x0, 40 int x0,
41 double minlog, 41 double minlog,
42 double maxlog) 42 double maxlog)
43 { 43 {
44 int n = 10;
45 auto ticks = ScaleTickIntervals::logarithmicAlready({ minlog, maxlog, n });
46 n = int(ticks.size());
47
44 int w = getWidth(v, paint) + x0; 48 int w = getWidth(v, paint) + x0;
45
46 int n = 10;
47
48 double val = minlog;
49 double inc = (maxlog - val) / n; // even increments of log scale
50
51 // smallest increment as displayed
52 double minDispInc = LogRange::unmap(minlog + inc) - LogRange::unmap(minlog);
53
54 #ifdef DEBUG_TIME_VALUE_LAYER
55 cerr << "min = " << minlog << ", max = " << maxlog << ", inc = " << inc << ", minDispInc = " << minDispInc << endl;
56 #endif
57
58 const int buflen = 40;
59 char buffer[buflen];
60
61 double round = 1.f;
62 int dp = 0;
63
64 if (minDispInc > 0) {
65 int prec = int(trunc(log10(minDispInc)));
66 if (prec < 0) dp = -prec;
67 round = pow(10.0, prec);
68 if (dp > 4) dp = 4;
69 #ifdef DEBUG_TIME_VALUE_LAYER
70 cerr << "round = " << round << ", prec = " << prec << ", dp = " << dp << endl;
71 #endif
72 }
73 49
74 int prevy = -1; 50 int prevy = -1;
75 51
76 for (int i = 0; i < n; ++i) { 52 for (int i = 0; i < n; ++i) {
77 53
81 if (i == n-1 && 57 if (i == n-1 &&
82 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) { 58 v->getPaintHeight() < paint.fontMetrics().height() * (n*2)) {
83 if (layer->getScaleUnits() != "") drawText = false; 59 if (layer->getScaleUnits() != "") drawText = false;
84 } 60 }
85 61
86 double dispval = LogRange::unmap(val); 62 double val = ticks[i].value;
87 dispval = floor(dispval / round) * round; 63 QString label = QString::fromStdString(ticks[i].label);
88 64
89 #ifdef DEBUG_TIME_VALUE_LAYER 65 y = layer->getYForValue(v, val);
90 cerr << "val = " << val << ", dispval = " << dispval << endl;
91 #endif
92
93 y = layer->getYForValue(v, dispval);
94 66
95 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2; 67 ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
96 68
97 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) { 69 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
98 val += inc;
99 continue; 70 continue;
100 } 71 }
101
102 int digits = int(trunc(log10(dispval)));
103 int sf = dp + (digits > 0 ? digits : 0);
104 if (sf < 4) sf = 4;
105 #ifdef DEBUG_TIME_VALUE_LAYER
106 cerr << "sf = " << sf << endl;
107 #endif
108 snprintf(buffer, buflen, "%.*g", sf, dispval);
109
110 QString label = QString(buffer);
111 72
112 paint.drawLine(w - 5, y, w, y); 73 paint.drawLine(w - 5, y, w, y);
113 74
114 if (drawText) { 75 if (drawText) {
115 paint.drawText(w - paint.fontMetrics().width(label) - 6, 76 paint.drawText(w - paint.fontMetrics().width(label) - 6,
116 ty, label); 77 ty, label);
117 } 78 }
118 79
119 prevy = y; 80 prevy = y;
120 val += inc;
121 } 81 }
122 } 82 }