@@ -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,17 @@ 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 ;
78- private final Color bgColor ;
78+ private final Color bgColor , gridColor , boundsColor ;
7979
8080 public GraphPanel () {
8181 font = Theme .getFont ("console.font" );
8282 bgColor = Theme .getColor ("plotting.bgcolor" );
83+ gridColor = Theme .getColor ("plotting.gridcolor" );
84+ boundsColor = Theme .getColor ("plotting.boundscolor" );
8385 xOffset = 20 ;
86+ xPadding = 20 ;
8487 }
8588
8689 private Ticks computeBounds () {
@@ -100,7 +103,7 @@ private Ticks computeBounds() {
100103 minY = mid - MIN_DELTA / 2 ;
101104 }
102105
103- Ticks ticks = new Ticks (minY , maxY , 3 );
106+ Ticks ticks = new Ticks (minY , maxY , 5 );
104107 minY = Math .min (minY , ticks .getTick (0 ));
105108 maxY = Math .max (maxY , ticks .getTick (ticks .getTickCount () - 1 ));
106109 rangeY = maxY - minY ;
@@ -132,16 +135,56 @@ public void paintComponent(Graphics g1) {
132135 Rectangle2D fRect = fm .getStringBounds (String .valueOf (tick ), g );
133136 xOffset = Math .max (xOffset , (int ) fRect .getWidth () + 15 );
134137
138+ g .setColor (boundsColor );
135139 // draw tick
136140 g .drawLine (xOffset - 5 , (int ) transformY (tick ), xOffset + 2 , (int ) transformY (tick ));
137141 // draw tick label
138142 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 ));
139146 }
140147
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+
143186 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 ;
145188 int legendLength = graphs .size () * 10 + (graphs .size () - 1 ) * 3 ;
146189
147190 for (int i = 0 ; i < graphs .size (); ++i ) {
@@ -206,6 +249,7 @@ protected void onCreateWindow(Container mainPane) {
206249
207250 serialRates .setMaximumSize (serialRates .getMinimumSize ());
208251
252+ pane .add (Box .createHorizontalGlue ());
209253 pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
210254 pane .add (serialRates );
211255
@@ -227,7 +271,7 @@ public void message(final String s) {
227271 if (linebreak == -1 ) {
228272 break ;
229273 }
230-
274+ xCount ++;
231275 String line = messageBuffer .substring (0 , linebreak );
232276 messageBuffer .delete (0 , linebreak + 1 );
233277
0 commit comments