@@ -38,7 +38,7 @@ public class SerialPlotter extends AbstractMonitor {
38
38
private final StringBuffer messageBuffer ;
39
39
private JComboBox <String > serialRates ;
40
40
private Serial serial ;
41
- private int serialRate ;
41
+ private int serialRate , xCount ;
42
42
43
43
private ArrayList <Graph > graphs ;
44
44
private final static int BUFFER_CAPACITY = 500 ;
@@ -73,14 +73,17 @@ private float transformY(double rawY, double minY, double rangeY, double height)
73
73
private class GraphPanel extends JPanel {
74
74
private double minY , maxY , rangeY ;
75
75
private Rectangle bounds ;
76
- private int xOffset ;
76
+ private int xOffset , xPadding ;
77
77
private final Font font ;
78
- private final Color bgColor ;
78
+ private final Color bgColor , gridColor , boundsColor ;
79
79
80
80
public GraphPanel () {
81
81
font = Theme .getFont ("console.font" );
82
82
bgColor = Theme .getColor ("plotting.bgcolor" );
83
+ gridColor = Theme .getColor ("plotting.gridcolor" );
84
+ boundsColor = Theme .getColor ("plotting.boundscolor" );
83
85
xOffset = 20 ;
86
+ xPadding = 20 ;
84
87
}
85
88
86
89
private Ticks computeBounds () {
@@ -100,7 +103,7 @@ private Ticks computeBounds() {
100
103
minY = mid - MIN_DELTA / 2 ;
101
104
}
102
105
103
- Ticks ticks = new Ticks (minY , maxY , 3 );
106
+ Ticks ticks = new Ticks (minY , maxY , 5 );
104
107
minY = Math .min (minY , ticks .getTick (0 ));
105
108
maxY = Math .max (maxY , ticks .getTick (ticks .getTickCount () - 1 ));
106
109
rangeY = maxY - minY ;
@@ -132,16 +135,56 @@ public void paintComponent(Graphics g1) {
132
135
Rectangle2D fRect = fm .getStringBounds (String .valueOf (tick ), g );
133
136
xOffset = Math .max (xOffset , (int ) fRect .getWidth () + 15 );
134
137
138
+ g .setColor (boundsColor );
135
139
// draw tick
136
140
g .drawLine (xOffset - 5 , (int ) transformY (tick ), xOffset + 2 , (int ) transformY (tick ));
137
141
// draw tick label
138
142
g .drawString (String .valueOf (tick ), xOffset - (int ) fRect .getWidth () - 10 , transformY (tick ) - (float ) fRect .getHeight () * 0.5f + fm .getAscent ());
143
+ // draw horizontal grid lines
144
+ g .setColor (gridColor );
145
+ g .drawLine (xOffset + 3 , (int ) transformY (tick ), bounds .width - xPadding , (int ) transformY (tick ));
139
146
}
140
147
141
- g .drawLine (bounds .x + xOffset , bounds .y + 5 , bounds .x + xOffset , bounds .y + bounds .height - 10 );
142
-
148
+ // handle data count
149
+ int cnt = xCount - BUFFER_CAPACITY ;
150
+ if (xCount < BUFFER_CAPACITY ) cnt = 0 ;
151
+
152
+ double zeroTick = ticks .getTick (0 );
153
+ double lastTick = ticks .getTick (ticks .getTickCount () - 1 );
154
+ double xTickRange = BUFFER_CAPACITY / ticks .getTickCount ();
155
+
156
+ for (int i = 0 ; i < ticks .getTickCount () + 1 ; i ++) {
157
+ String s ;
158
+ int xValue ;
159
+ int sWidth ;
160
+ Rectangle2D fBounds ;
161
+ if (i == 0 ) {
162
+ s = String .valueOf (cnt );
163
+ fBounds = fm .getStringBounds (s , g );
164
+ sWidth = (int )fBounds .getWidth ()/2 ;
165
+ xValue = xOffset ;
166
+ } else {
167
+ s = String .valueOf ((int )(xTickRange * i )+cnt );
168
+ fBounds = fm .getStringBounds (s , g );
169
+ sWidth = (int )fBounds .getWidth ()/2 ;
170
+ xValue = (int )((bounds .width - xOffset - xPadding ) * ((xTickRange * i ) / BUFFER_CAPACITY ) + xOffset );
171
+ }
172
+ // draw graph x axis, ticks and labels
173
+ g .setColor (boundsColor );
174
+ g .drawString (s , xValue - sWidth , (int ) bounds .y + (int ) transformY (zeroTick ) + 15 );
175
+ g .drawLine (xValue , (int )transformY (zeroTick ) - 2 , xValue , bounds .y + (int )transformY (zeroTick ) + 5 );
176
+ // draw vertical grid lines
177
+ g .setColor (gridColor );
178
+ g .drawLine (xValue , (int )transformY (zeroTick ) - 3 , xValue , bounds .y + (int )transformY (lastTick ));
179
+ }
180
+ g .setColor (boundsColor );
181
+ // draw major y axis
182
+ g .drawLine (bounds .x + xOffset , (int ) transformY (lastTick ) - 5 , bounds .x + xOffset , bounds .y + (int ) transformY (zeroTick ) + 5 );
183
+ // draw major x axis
184
+ g .drawLine (xOffset , (int ) transformY (zeroTick ), bounds .width - xPadding , (int )transformY (zeroTick ));
185
+
143
186
g .setTransform (AffineTransform .getTranslateInstance (xOffset , 0 ));
144
- float xstep = (float ) (bounds .width - xOffset ) / (float ) BUFFER_CAPACITY ;
187
+ float xstep = (float ) (bounds .width - xOffset - xPadding ) / (float ) BUFFER_CAPACITY ;
145
188
int legendLength = graphs .size () * 10 + (graphs .size () - 1 ) * 3 ;
146
189
147
190
for (int i = 0 ; i < graphs .size (); ++i ) {
@@ -206,6 +249,7 @@ protected void onCreateWindow(Container mainPane) {
206
249
207
250
serialRates .setMaximumSize (serialRates .getMinimumSize ());
208
251
252
+ pane .add (Box .createHorizontalGlue ());
209
253
pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
210
254
pane .add (serialRates );
211
255
@@ -227,7 +271,7 @@ public void message(final String s) {
227
271
if (linebreak == -1 ) {
228
272
break ;
229
273
}
230
-
274
+ xCount ++;
231
275
String line = messageBuffer .substring (0 , linebreak );
232
276
messageBuffer .delete (0 , linebreak + 1 );
233
277
0 commit comments