@@ -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,16 @@ 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
78
private final Color bgColor ;
79
+ private final Color gridColor = new Color (245 , 245 , 245 , 245 );
79
80
80
81
public GraphPanel () {
81
82
font = Theme .getFont ("console.font" );
82
83
bgColor = Theme .getColor ("plotting.bgcolor" );
83
84
xOffset = 20 ;
85
+ xPadding = 20 ;
84
86
}
85
87
86
88
private Ticks computeBounds () {
@@ -100,7 +102,7 @@ private Ticks computeBounds() {
100
102
minY = mid - MIN_DELTA / 2 ;
101
103
}
102
104
103
- Ticks ticks = new Ticks (minY , maxY , 3 );
105
+ Ticks ticks = new Ticks (minY , maxY , 5 );
104
106
minY = Math .min (minY , ticks .getTick (0 ));
105
107
maxY = Math .max (maxY , ticks .getTick (ticks .getTickCount () - 1 ));
106
108
rangeY = maxY - minY ;
@@ -132,16 +134,58 @@ public void paintComponent(Graphics g1) {
132
134
Rectangle2D fRect = fm .getStringBounds (String .valueOf (tick ), g );
133
135
xOffset = Math .max (xOffset , (int ) fRect .getWidth () + 15 );
134
136
137
+ g .setColor (Color .BLACK );
135
138
// draw tick
136
139
g .drawLine (xOffset - 5 , (int ) transformY (tick ), xOffset + 2 , (int ) transformY (tick ));
137
140
// draw tick label
138
141
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 ));
139
145
}
140
146
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
+
143
187
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 ;
145
189
int legendLength = graphs .size () * 10 + (graphs .size () - 1 ) * 3 ;
146
190
147
191
for (int i = 0 ; i < graphs .size (); ++i ) {
@@ -206,6 +250,7 @@ protected void onCreateWindow(Container mainPane) {
206
250
207
251
serialRates .setMaximumSize (serialRates .getMinimumSize ());
208
252
253
+ pane .add (Box .createHorizontalGlue ());
209
254
pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
210
255
pane .add (serialRates );
211
256
@@ -227,7 +272,7 @@ public void message(final String s) {
227
272
if (linebreak == -1 ) {
228
273
break ;
229
274
}
230
-
275
+ xCount ++;
231
276
String line = messageBuffer .substring (0 , linebreak );
232
277
messageBuffer .delete (0 , linebreak + 1 );
233
278
0 commit comments