@@ -46,6 +46,7 @@ public class SerialPlotter extends AbstractMonitor {
46
46
private static class Graph {
47
47
public CircularBuffer buffer ;
48
48
private Color color ;
49
+ public String label ;
49
50
50
51
public Graph (int id ) {
51
52
buffer = new CircularBuffer (BUFFER_CAPACITY );
@@ -185,12 +186,24 @@ public void paintComponent(Graphics g1) {
185
186
186
187
g .setTransform (AffineTransform .getTranslateInstance (xOffset , 0 ));
187
188
float xstep = (float ) (bounds .width - xOffset - xPadding ) / (float ) BUFFER_CAPACITY ;
188
- int legendLength = graphs .size () * 10 + (graphs .size () - 1 ) * 3 ;
189
189
190
+ // draw legend
191
+ int legendXOffset = 0 ;
190
192
for (int i = 0 ; i < graphs .size (); ++i ) {
191
193
graphs .get (i ).paint (g , xstep , minY , maxY , rangeY , bounds .height );
192
- if (graphs .size () > 1 ) {
193
- g .fillRect (bounds .width - (xOffset + legendLength + 10 ) + i * 13 , 10 , 10 , 10 );
194
+ if (graphs .size () > 1 ) {
195
+ //draw legend rectangle
196
+ g .fillRect (10 + legendXOffset , 10 , 10 , 10 );
197
+ legendXOffset += 13 ;
198
+ //draw label
199
+ g .setColor (boundsColor );
200
+ String s = graphs .get (i ).label ;
201
+ if (s != null && s .length () > 0 ) {
202
+ Rectangle2D fBounds = fm .getStringBounds (s , g );
203
+ int sWidth = (int )fBounds .getWidth ();
204
+ g .drawString (s , 10 + legendXOffset , 10 + (int )fBounds .getHeight () /2 );
205
+ legendXOffset += sWidth + 3 ;
206
+ }
194
207
}
195
208
}
196
209
}
@@ -282,17 +295,60 @@ public void message(final String s) {
282
295
}
283
296
284
297
int validParts = 0 ;
298
+ int validLabels = 0 ;
285
299
for (int i = 0 ; i < parts .length ; ++i ) {
286
- try {
287
- double value = Double .valueOf (parts [i ]);
300
+ Double value = null ;
301
+ String label = null ;
302
+
303
+ // column formated name value pair
304
+ if (parts [i ].contains (":" )) {
305
+ // get label
306
+ String [] subString = parts [i ].split ("[:]+" );
307
+
308
+ if (subString .length > 0 ) {
309
+ int labelLength = subString [0 ].length ();
310
+
311
+ if (labelLength > 32 ) {
312
+ labelLength = 32 ;
313
+ }
314
+ label = subString [0 ].substring (0 , labelLength );
315
+ } else {
316
+ label = "" ;
317
+ }
318
+
319
+ if (subString .length > 1 ) {
320
+ parts [i ] = subString [1 ];
321
+ } else {
322
+ parts [i ] = "" ;
323
+ }
324
+ }
325
+
326
+ try {
327
+ value = Double .valueOf (parts [i ]);
328
+ } catch (NumberFormatException e ) {
329
+ // ignored
330
+ }
331
+ //CSV header
332
+ if (label == null && value == null ) {
333
+ label = parts [i ];
334
+ }
335
+
336
+ if (value != null ) {
288
337
if (validParts >= graphs .size ()) {
289
338
graphs .add (new Graph (validParts ));
290
339
}
291
340
graphs .get (validParts ).buffer .add (value );
292
341
validParts ++;
293
- } catch (NumberFormatException e ) {
294
- // ignore
295
342
}
343
+ if (label != null ) {
344
+ if (validLabels >= graphs .size ()) {
345
+ graphs .add (new Graph (validLabels ));
346
+ }
347
+ graphs .get (validLabels ).label = label ;
348
+ validLabels ++;
349
+ }
350
+ if (validParts > validLabels ) validLabels = validParts ;
351
+ else if (validLabels > validParts ) validParts = validLabels ;
296
352
}
297
353
}
298
354
0 commit comments