Skip to content

Commit 73fbe93

Browse files
committed
Improve timescale label formatting in LFP Viewer
1 parent a46bc77 commit 73fbe93

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

Plugins/LfpViewer/LfpTimescale.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,29 @@
2626
#include "LfpDisplay.h"
2727
#include "LfpDisplayCanvas.h"
2828

29-
#include <math.h>
29+
#include <cmath>
3030

3131
using namespace LfpViewer;
3232

33+
namespace
34+
{
35+
String formatTimescaleLabel (float timeInSeconds, float timebase, float stepSize)
36+
{
37+
if (timebase < 2.0f)
38+
{
39+
return String (static_cast<int> (std::lround (timeInSeconds * 1000.0f)));
40+
}
41+
42+
const double stepInSeconds = static_cast<double> (stepSize);
43+
const double snappedTime = stepInSeconds > 0.0
44+
? std::round (static_cast<double> (timeInSeconds) / stepInSeconds) * stepInSeconds
45+
: static_cast<double> (timeInSeconds);
46+
const int decimalPlaces = stepSize < 1.0f ? 1 : 0;
47+
48+
return String (snappedTime, decimalPlaces);
49+
}
50+
}
51+
3352
#pragma mark - LfpTimescale -
3453
// -------------------------------------------------------------
3554

@@ -254,8 +273,7 @@ void LfpTimescale::setTimebase (float timebase_, float offset_)
254273

255274
while ((time + offset) < timebase)
256275
{
257-
String labelString = String (time * ((timebase >= 2) ? (1) : (1000.0f)));
258-
labels.add (labelString.substring (0, 6));
276+
labels.add (formatTimescaleLabel (time, timebase, stepSize));
259277

260278
fractionWidth.add ((time + offset) / timebase);
261279

0 commit comments

Comments
 (0)