Skip to content

Commit bfb6f39

Browse files
committed
Added labels to legend
1 parent 378736e commit bfb6f39

File tree

1 file changed

+63
-7
lines changed

1 file changed

+63
-7
lines changed

Diff for: app/src/processing/app/SerialPlotter.java

+63-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class SerialPlotter extends AbstractMonitor {
4646
private static class Graph {
4747
public CircularBuffer buffer;
4848
private Color color;
49+
public String label;
4950

5051
public Graph(int id) {
5152
buffer = new CircularBuffer(BUFFER_CAPACITY);
@@ -185,12 +186,24 @@ public void paintComponent(Graphics g1) {
185186

186187
g.setTransform(AffineTransform.getTranslateInstance(xOffset, 0));
187188
float xstep = (float) (bounds.width - xOffset - xPadding) / (float) BUFFER_CAPACITY;
188-
int legendLength = graphs.size() * 10 + (graphs.size() - 1) * 3;
189189

190+
// draw legend
191+
int legendXOffset = 0;
190192
for(int i = 0; i < graphs.size(); ++i) {
191193
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+
}
194207
}
195208
}
196209
}
@@ -282,17 +295,60 @@ public void message(final String s) {
282295
}
283296

284297
int validParts = 0;
298+
int validLabels = 0;
285299
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) {
288337
if(validParts >= graphs.size()) {
289338
graphs.add(new Graph(validParts));
290339
}
291340
graphs.get(validParts).buffer.add(value);
292341
validParts++;
293-
} catch (NumberFormatException e) {
294-
// ignore
295342
}
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;
296352
}
297353
}
298354

0 commit comments

Comments
 (0)