Skip to content

Commit 08ab27e

Browse files
committed
Improved formatting of number axis. Increased precision of values sent over serial
1 parent af4ca85 commit 08ab27e

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

listener/Graph.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import processing.core.PApplet;
2+
import java.text.DecimalFormat;
23

34
class Graph
45
{
@@ -221,9 +222,12 @@ private void DrawTicks( double xScale, double xOffset, double yScale, double yOf
221222
for ( float tempY = this.posY + tickOffset; tempY <= this.posY + this.height - tickOffset;
222223
tempY += tickInterval )
223224
{
224-
float val = (float) ( ( ( yOffset + this.posY ) - (double)tempY ) / yScale );
225+
float val = (float) ( ( ( yOffset + this.posY ) - (double)tempY ) / yScale );
226+
int n = GetDecimalPlaces( val );
227+
String fmt = "%" + Integer.toString( 1 + SIG_DIGITS - n ) + "." + Integer.toString( n ) + "g";
228+
this.parent.println( fmt );
225229
this.parent.line( tempX, tempY, tempX + TICK_LEN, tempY );
226-
this.parent.text( Float.toString( val ), tempX + TICK_LEN + 5, tempY );
230+
this.parent.text( String.format( fmt, val ), tempX + TICK_LEN + 5, tempY );
227231
}
228232

229233
// x-axis
@@ -236,11 +240,32 @@ private void DrawTicks( double xScale, double xOffset, double yScale, double yOf
236240
{
237241
float val = (float) ( ( (double)tempX + xOffset - this.posX ) / xScale );
238242
this.parent.line( tempX, tempY, tempX, tempY + TICK_LEN );
239-
this.parent.text( Float.toString( val ), tempX, tempY - 5 );
243+
if ( this.xvy )
244+
{
245+
int n = GetDecimalPlaces( val );
246+
String fmt = "%" + Integer.toString( 1 + SIG_DIGITS - n ) + "." + Integer.toString( n ) + "g";
247+
this.parent.text( String.format( fmt, val ), tempX, tempY - 5 );
248+
}
249+
else
250+
{
251+
this.parent.text( String.format( "%d", (int)val ), tempX, tempY - 5 );
252+
}
240253
}
241254

242255
}
243256

257+
private int GetDecimalPlaces( float value )
258+
{
259+
int n = SIG_DIGITS;
260+
int d = 1;
261+
while ( n > 0 && Math.round( Math.abs( value / d ) ) > 0 )
262+
{
263+
n--;
264+
d *= 10;
265+
}
266+
return n;
267+
}
268+
244269
private void CheckExtremes()
245270
{
246271
// Check new values
@@ -348,5 +373,5 @@ else if ( this.data[i][j][0] > this.extremes[1] ) // (max)
348373
private static final int TICK_LEN = 6;
349374
private static final int NUM_TICKS = 5;
350375
private static final float PT_SZ = 1.5f;
351-
352-
}
376+
private static final int SIG_DIGITS = 3;
377+
}

plotter/Plotter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,14 @@ void Plotter::Graph::Plot()
201201
Serial.print( xvy ); Serial.print( INNER_KEY );
202202
Serial.print( pointsDisplayed ); Serial.print( INNER_KEY );
203203
Serial.print( size ); Serial.print( INNER_KEY );
204+
205+
char val[15];
204206
for (int i = 0; i < size; i++)
205207
{
206208
Serial.print( wrappers[i].GetLabel() ); Serial.print( INNER_KEY );
207209
Serial.print( wrappers[i].GetColor() ); Serial.print( INNER_KEY );
208-
Serial.print( wrappers[i].GetValue() ); Serial.print( INNER_KEY );
210+
dtostrf( wrappers[i].GetValue(), 1, 7, val );
211+
Serial.print( val ); Serial.print( INNER_KEY );
209212
}
210213
}
211214

plotter/examples/set_colors/set_colors.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ void setup()
2626
// Set variable colors of graph with index 0 to pink and orange
2727
p.SetColor( 0, "pink", "orange" );
2828
p.SetColor( 1, "cyan" );
29+
2930
}
3031

3132
void loop() {
32-
x = 10*sin( 2.0*PI*( millis() / 5000.0 ) );
33-
y = 10*cos( 2.0*PI*( millis() / 5000.0 ) );
34-
33+
x = 0.0009*sin( 2.0*PI*( millis() / 5000.0 ) );
34+
y = 90000*cos( 2.0*PI*( millis() / 5000.0 ) );
35+
3536
p.Plot(); // usually called within loop()
3637
}

0 commit comments

Comments
 (0)