Skip to content

Commit db8526c

Browse files
author
duff2013
committed
upgrade Serial Plotter
1 parent 653a052 commit db8526c

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

app/src/processing/app/SerialPlotter.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SerialPlotter extends AbstractMonitor {
3838
private final StringBuffer messageBuffer;
3939
private JComboBox<String> serialRates;
4040
private Serial serial;
41-
private int serialRate;
41+
private int serialRate, xCount;
4242

4343
private ArrayList<Graph> graphs;
4444
private final static int BUFFER_CAPACITY = 500;
@@ -73,14 +73,16 @@ private float transformY(double rawY, double minY, double rangeY, double height)
7373
private class GraphPanel extends JPanel {
7474
private double minY, maxY, rangeY;
7575
private Rectangle bounds;
76-
private int xOffset;
76+
private int xOffset, xPadding;
7777
private final Font font;
7878
private final Color bgColor;
79+
private final Color gridColor = new Color(245, 245, 245, 245);
7980

8081
public GraphPanel() {
8182
font = Theme.getFont("console.font");
8283
bgColor = Theme.getColor("plotting.bgcolor");
8384
xOffset = 20;
85+
xPadding = 20;
8486
}
8587

8688
private Ticks computeBounds() {
@@ -100,7 +102,7 @@ private Ticks computeBounds() {
100102
minY = mid - MIN_DELTA / 2;
101103
}
102104

103-
Ticks ticks = new Ticks(minY, maxY, 3);
105+
Ticks ticks = new Ticks(minY, maxY, 5);
104106
minY = Math.min(minY, ticks.getTick(0));
105107
maxY = Math.max(maxY, ticks.getTick(ticks.getTickCount() - 1));
106108
rangeY = maxY - minY;
@@ -132,16 +134,58 @@ public void paintComponent(Graphics g1) {
132134
Rectangle2D fRect = fm.getStringBounds(String.valueOf(tick), g);
133135
xOffset = Math.max(xOffset, (int) fRect.getWidth() + 15);
134136

137+
g.setColor(Color.BLACK);
135138
// draw tick
136139
g.drawLine(xOffset - 5, (int) transformY(tick), xOffset + 2, (int) transformY(tick));
137140
// draw tick label
138141
g.drawString(String.valueOf(tick), xOffset - (int) fRect.getWidth() - 10, transformY(tick) - (float) fRect.getHeight() * 0.5f + fm.getAscent());
142+
// draw horizontal grid lines
143+
g.setColor(gridColor);
144+
g.drawLine(xOffset + 3, (int) transformY(tick), bounds.width - xPadding, (int) transformY(tick));
139145
}
140146

141-
g.drawLine(bounds.x + xOffset, bounds.y + 5, bounds.x + xOffset, bounds.y + bounds.height - 10);
142-
147+
//g.drawLine(bounds.x + xOffset, bounds.y + 5, bounds.x + xOffset, bounds.y + bounds.height - 10);
148+
149+
// handle data count
150+
int cnt = xCount - BUFFER_CAPACITY;
151+
if (xCount < BUFFER_CAPACITY) cnt = 0;
152+
153+
double zeroTick = ticks.getTick(0);
154+
double lastTick = ticks.getTick(ticks.getTickCount() - 1);
155+
double xTickRange = BUFFER_CAPACITY / ticks.getTickCount();
156+
157+
for (int i = 0; i < ticks.getTickCount() + 1; i++) {
158+
String s;
159+
int xValue;
160+
int sWidth;
161+
Rectangle2D fBounds;
162+
if (i == 0) {
163+
s = String.valueOf(cnt);
164+
fBounds = fm.getStringBounds(s, g);
165+
sWidth = (int)fBounds.getWidth()/2;
166+
xValue = xOffset;
167+
} else {
168+
s = String.valueOf((int)(xTickRange * i)+cnt);
169+
fBounds = fm.getStringBounds(s, g);
170+
sWidth = (int)fBounds.getWidth()/2;
171+
xValue = (int)((bounds.width - xOffset - xPadding) * ((xTickRange * i) / BUFFER_CAPACITY) + xOffset);
172+
}
173+
// draw graph x axis ticks and labels
174+
g.setColor(Color.BLACK);
175+
g.drawString(s, xValue - sWidth, (int) bounds.y + (int) transformY(zeroTick) + 15);
176+
g.drawLine(xValue, (int)transformY(zeroTick) - 2, xValue, bounds.y + (int)transformY(zeroTick) + 5);
177+
// draw vertical grid lines
178+
g.setColor(gridColor);
179+
g.drawLine(xValue, (int)transformY(zeroTick) - 3, xValue, bounds.y + (int)transformY(lastTick));
180+
}
181+
g.setColor(Color.BLACK);
182+
// draw major y axis
183+
g.drawLine(bounds.x + xOffset, (int) transformY(lastTick) - 5, bounds.x + xOffset, bounds.y + (int) transformY(zeroTick) + 5);
184+
// draw major x axis
185+
g.drawLine(xOffset, (int) transformY(zeroTick), bounds.width - xPadding, (int)transformY(zeroTick));
186+
143187
g.setTransform(AffineTransform.getTranslateInstance(xOffset, 0));
144-
float xstep = (float) (bounds.width - xOffset) / (float) BUFFER_CAPACITY;
188+
float xstep = (float) (bounds.width - xOffset - xPadding) / (float) BUFFER_CAPACITY;
145189
int legendLength = graphs.size() * 10 + (graphs.size() - 1) * 3;
146190

147191
for(int i = 0; i < graphs.size(); ++i) {
@@ -206,6 +250,7 @@ protected void onCreateWindow(Container mainPane) {
206250

207251
serialRates.setMaximumSize(serialRates.getMinimumSize());
208252

253+
pane.add(Box.createHorizontalGlue());
209254
pane.add(Box.createRigidArea(new Dimension(8, 0)));
210255
pane.add(serialRates);
211256

@@ -227,7 +272,7 @@ public void message(final String s) {
227272
if (linebreak == -1) {
228273
break;
229274
}
230-
275+
xCount++;
231276
String line = messageBuffer.substring(0, linebreak);
232277
messageBuffer.delete(0, linebreak + 1);
233278

0 commit comments

Comments
 (0)