Skip to content

Commit 0fc5f27

Browse files
committed
Changed all drawing sizes to adjust based on size of frame. Switch between %e and %f explicitly instead of using %g
1 parent 08ab27e commit 0fc5f27

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

listener/Graph.java

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ public void Plot()
129129
{
130130
// Plot Background
131131
this.parent.fill( PLOT_COL );
132-
this.parent.stroke( 255 );
133-
this.parent.strokeWeight( PT_SZ );
132+
this.parent.stroke( PLOT_COL );
133+
this.parent.strokeWeight( (this.width + this.height) / 2.0f * PT_SZ );
134134
this.parent.rect( this.posX, this.posY, this.width, this.height );
135135

136136
// Title
137-
this.parent.textSize( TITLE_SZ );
137+
this.parent.textSize( (int)( (this.width + this.height) / 2.0f * TITLE_SZ ) );
138138
this.parent.fill( 255 );
139139
this.parent.textAlign( this.parent.CENTER, this.parent.TOP );
140-
this.parent.text( this.title, this.posX + this.width / 2, this.posY + TITLE_SZ);
140+
this.parent.text( this.title, this.posX + this.width / 2, this.posY + 10 );
141141

142142

143143
// Calculations for offset and scaling of graph ( vs. time )
@@ -179,17 +179,19 @@ public void Plot()
179179

180180
private void DrawTimeStuff()
181181
{
182+
int labelSz = (int) ( (this.width + this.height) / 2.0f * LABEL_SZ );
183+
182184
// Setup legend start
183-
float textPos = this.posY + LABEL_SZ;
185+
float textPos = this.posY + labelSz;
184186
this.parent.textAlign( this.parent.RIGHT, this.parent.TOP );
185-
this.parent.textSize( LABEL_SZ );
187+
this.parent.textSize( labelSz );
186188

187189
// Draw each legend entry
188190
for ( int i = 0; i < this.numVars; i++ )
189191
{
190192
this.parent.fill( this.colors[i] );
191193
this.parent.text( this.labels[i], this.posX + this.width - 10, textPos);
192-
textPos += ( LABEL_SZ + LABEL_SZ/4 );
194+
textPos += ( labelSz + labelSz/4 );
193195
this.parent.stroke( this.colors[i] );
194196
}
195197

@@ -198,12 +200,12 @@ private void DrawTimeStuff()
198200
private void DrawXYStuff()
199201
{
200202
// X and Y labels
201-
this.parent.textSize( LABEL_SZ );
203+
this.parent.textSize( (int)( (this.width + this.height) / 2.0f * LABEL_SZ) );
202204
this.parent.textAlign( this.parent.LEFT, this.parent.TOP );
203205
this.parent.text( this.labels[1], this.posX + 10, this.posY + 10);
204206

205207
this.parent.textAlign( this.parent.RIGHT, this.parent.BOTTOM );
206-
this.parent.text( this.labels[0], this.posX + this.width - 10, this.posY + this.height - 3*NUM_SZ);
208+
this.parent.text( this.labels[0], this.posX + this.width - 10, this.posY + this.height - 3.5f*TICK_LEN);
207209
}
208210

209211

@@ -212,7 +214,7 @@ private void DrawTicks( double xScale, double xOffset, double yScale, double yOf
212214
// Label graph with numbered tick marks
213215
this.parent.stroke( 255 );
214216
this.parent.fill( 255 );
215-
this.parent.textSize( NUM_SZ );
217+
this.parent.textSize( (int)( ( this.width + this.height ) / 2.0f * NUM_SZ ) );
216218
this.parent.textAlign( this.parent.LEFT, this.parent.CENTER );
217219

218220
// Draw ticks along y-axis
@@ -223,9 +225,7 @@ private void DrawTicks( double xScale, double xOffset, double yScale, double yOf
223225
tempY += tickInterval )
224226
{
225227
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 );
228+
String fmt = GetNumberFormat( val );
229229
this.parent.line( tempX, tempY, tempX + TICK_LEN, tempY );
230230
this.parent.text( String.format( fmt, val ), tempX + TICK_LEN + 5, tempY );
231231
}
@@ -242,8 +242,7 @@ private void DrawTicks( double xScale, double xOffset, double yScale, double yOf
242242
this.parent.line( tempX, tempY, tempX, tempY + TICK_LEN );
243243
if ( this.xvy )
244244
{
245-
int n = GetDecimalPlaces( val );
246-
String fmt = "%" + Integer.toString( 1 + SIG_DIGITS - n ) + "." + Integer.toString( n ) + "g";
245+
String fmt = GetNumberFormat( val );
247246
this.parent.text( String.format( fmt, val ), tempX, tempY - 5 );
248247
}
249248
else
@@ -254,16 +253,26 @@ private void DrawTicks( double xScale, double xOffset, double yScale, double yOf
254253

255254
}
256255

257-
private int GetDecimalPlaces( float value )
258-
{
256+
private String GetNumberFormat( float value )
257+
{
259258
int n = SIG_DIGITS;
260259
int d = 1;
261260
while ( n > 0 && Math.round( Math.abs( value / d ) ) > 0 )
262261
{
263262
n--;
264263
d *= 10;
265264
}
266-
return n;
265+
266+
String fmt = "%" + Integer.toString( 1 + SIG_DIGITS - n ) + "." + Integer.toString( n );
267+
if ( ( Math.abs( value ) > 1000 || Math.abs( value ) < 0.001 ) && value != 0 )
268+
{
269+
fmt += "e";
270+
}
271+
else
272+
{
273+
fmt += "f";
274+
}
275+
return fmt;
267276
}
268277

269278
private void CheckExtremes()
@@ -367,11 +376,11 @@ else if ( this.data[i][j][0] > this.extremes[1] ) // (max)
367376
// Constants
368377
private static final float AXIS_COV = 0.75f;
369378
private static final int PLOT_COL = 115;
370-
private static final int LABEL_SZ = 14;
371-
private static final int TITLE_SZ = 16;
372-
private static final int NUM_SZ = 10;
379+
private static final float LABEL_SZ = 0.025f;
380+
private static final float TITLE_SZ = 0.03f;
381+
private static final float NUM_SZ = 0.02f;
373382
private static final int TICK_LEN = 6;
374383
private static final int NUM_TICKS = 5;
375-
private static final float PT_SZ = 1.5f;
384+
private static final float PT_SZ = 0.0025f;
376385
private static final int SIG_DIGITS = 3;
377-
}
386+
}

0 commit comments

Comments
 (0)