11import processing .core .PApplet ;
2+ import java .text .DecimalFormat ;
23
34class 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+ }
0 commit comments