changeset 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 4d7e566092a7
children f683b191dbe6
files layer/LinearNumericalScale.cpp layer/LogNumericalScale.cpp
diffstat 2 files changed, 11 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/layer/LinearNumericalScale.cpp	Thu May 04 15:37:43 2017 +0100
+++ b/layer/LinearNumericalScale.cpp	Thu May 04 15:42:40 2017 +0100
@@ -40,14 +40,14 @@
 				    double maxf)
 {
     int n = 10;
-
     auto ticks = ScaleTickIntervals::linear({ minf, maxf, n });
+    n = int(ticks.size());
 
     int w = getWidth(v, paint) + x0;
 
     int prevy = -1;
-                
-    for (int i = 0; i < int(ticks.size()); ++i) {
+
+    for (int i = 0; i < n; ++i) {
 
 	int y, ty;
         bool drawText = true;
--- a/layer/LogNumericalScale.cpp	Thu May 04 15:37:43 2017 +0100
+++ b/layer/LogNumericalScale.cpp	Thu May 04 15:42:40 2017 +0100
@@ -24,7 +24,7 @@
 
 #include "LayerGeometryProvider.h"
 
-//#define DEBUG_TIME_VALUE_LAYER 1
+#include "base/ScaleTickIntervals.h"
 
 int
 LogNumericalScale::getWidth(LayerGeometryProvider *,
@@ -41,36 +41,12 @@
 				 double minlog,
 				 double maxlog)
 {
+    int n = 10;
+    auto ticks = ScaleTickIntervals::logarithmicAlready({ minlog, maxlog, n });
+    n = int(ticks.size());
+
     int w = getWidth(v, paint) + x0;
 
-    int n = 10;
-
-    double val = minlog;
-    double inc = (maxlog - val) / n; // even increments of log scale
-
-    // smallest increment as displayed
-    double minDispInc = LogRange::unmap(minlog + inc) - LogRange::unmap(minlog);
-
-#ifdef DEBUG_TIME_VALUE_LAYER
-    cerr << "min = " << minlog << ", max = " << maxlog << ", inc = " << inc << ", minDispInc = " << minDispInc << endl;
-#endif
-
-    const int buflen = 40;
-    char buffer[buflen];
-
-    double round = 1.f;
-    int dp = 0;
-
-    if (minDispInc > 0) {
-        int prec = int(trunc(log10(minDispInc)));
-        if (prec < 0) dp = -prec;
-        round = pow(10.0, prec);
-        if (dp > 4) dp = 4;
-#ifdef DEBUG_TIME_VALUE_LAYER
-        cerr << "round = " << round << ", prec = " << prec << ", dp = " << dp << endl;
-#endif
-    }
-
     int prevy = -1;
                 
     for (int i = 0; i < n; ++i) {
@@ -83,32 +59,17 @@
 	    if (layer->getScaleUnits() != "") drawText = false;
 	}
 
-        double dispval = LogRange::unmap(val);
-	dispval = floor(dispval / round) * round;
+        double val = ticks[i].value;
+        QString label = QString::fromStdString(ticks[i].label);
 
-#ifdef DEBUG_TIME_VALUE_LAYER
-	cerr << "val = " << val << ", dispval = " << dispval << endl;
-#endif
-
-	y = layer->getYForValue(v, dispval);
+	y = layer->getYForValue(v, val);
 
 	ty = y - paint.fontMetrics().height() + paint.fontMetrics().ascent() + 2;
 	
 	if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
-	    val += inc;
 	    continue;
         }
 
-	int digits = int(trunc(log10(dispval)));
-	int sf = dp + (digits > 0 ? digits : 0);
-	if (sf < 4) sf = 4;
-#ifdef DEBUG_TIME_VALUE_LAYER
-        cerr << "sf = " << sf << endl;
-#endif
-	snprintf(buffer, buflen, "%.*g", sf, dispval);
-
-	QString label = QString(buffer);
-
 	paint.drawLine(w - 5, y, w, y);
 
         if (drawText) {
@@ -117,6 +78,5 @@
         }
 
         prevy = y;
-	val += inc;
     }
 }