-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Upgrade Serial Plotter #5443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Upgrade Serial Plotter #5443
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ public class SerialPlotter extends AbstractMonitor { | |
private final StringBuffer messageBuffer; | ||
private JComboBox<String> serialRates; | ||
private Serial serial; | ||
private int serialRate; | ||
private int serialRate, xCount; | ||
|
||
private ArrayList<Graph> graphs; | ||
private final static int BUFFER_CAPACITY = 500; | ||
|
@@ -73,14 +73,16 @@ private float transformY(double rawY, double minY, double rangeY, double height) | |
private class GraphPanel extends JPanel { | ||
private double minY, maxY, rangeY; | ||
private Rectangle bounds; | ||
private int xOffset; | ||
private int xOffset, xPadding; | ||
private final Font font; | ||
private final Color bgColor; | ||
private final Color gridColor = new Color(245, 245, 245, 245); | ||
|
||
public GraphPanel() { | ||
font = Theme.getFont("console.font"); | ||
bgColor = Theme.getColor("plotting.bgcolor"); | ||
xOffset = 20; | ||
xPadding = 20; | ||
} | ||
|
||
private Ticks computeBounds() { | ||
|
@@ -100,7 +102,7 @@ private Ticks computeBounds() { | |
minY = mid - MIN_DELTA / 2; | ||
} | ||
|
||
Ticks ticks = new Ticks(minY, maxY, 3); | ||
Ticks ticks = new Ticks(minY, maxY, 5); | ||
minY = Math.min(minY, ticks.getTick(0)); | ||
maxY = Math.max(maxY, ticks.getTick(ticks.getTickCount() - 1)); | ||
rangeY = maxY - minY; | ||
|
@@ -132,16 +134,58 @@ public void paintComponent(Graphics g1) { | |
Rectangle2D fRect = fm.getStringBounds(String.valueOf(tick), g); | ||
xOffset = Math.max(xOffset, (int) fRect.getWidth() + 15); | ||
|
||
g.setColor(Color.BLACK); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And probably this one too, even if it wasn't present before, it may be encoded as |
||
// draw tick | ||
g.drawLine(xOffset - 5, (int) transformY(tick), xOffset + 2, (int) transformY(tick)); | ||
// draw tick label | ||
g.drawString(String.valueOf(tick), xOffset - (int) fRect.getWidth() - 10, transformY(tick) - (float) fRect.getHeight() * 0.5f + fm.getAscent()); | ||
// draw horizontal grid lines | ||
g.setColor(gridColor); | ||
g.drawLine(xOffset + 3, (int) transformY(tick), bounds.width - xPadding, (int) transformY(tick)); | ||
} | ||
|
||
g.drawLine(bounds.x + xOffset, bounds.y + 5, bounds.x + xOffset, bounds.y + bounds.height - 10); | ||
|
||
//g.drawLine(bounds.x + xOffset, bounds.y + 5, bounds.x + xOffset, bounds.y + bounds.height - 10); | ||
|
||
// handle data count | ||
int cnt = xCount - BUFFER_CAPACITY; | ||
if (xCount < BUFFER_CAPACITY) cnt = 0; | ||
|
||
double zeroTick = ticks.getTick(0); | ||
double lastTick = ticks.getTick(ticks.getTickCount() - 1); | ||
double xTickRange = BUFFER_CAPACITY / ticks.getTickCount(); | ||
|
||
for (int i = 0; i < ticks.getTickCount() + 1; i++) { | ||
String s; | ||
int xValue; | ||
int sWidth; | ||
Rectangle2D fBounds; | ||
if (i == 0) { | ||
s = String.valueOf(cnt); | ||
fBounds = fm.getStringBounds(s, g); | ||
sWidth = (int)fBounds.getWidth()/2; | ||
xValue = xOffset; | ||
} else { | ||
s = String.valueOf((int)(xTickRange * i)+cnt); | ||
fBounds = fm.getStringBounds(s, g); | ||
sWidth = (int)fBounds.getWidth()/2; | ||
xValue = (int)((bounds.width - xOffset - xPadding) * ((xTickRange * i) / BUFFER_CAPACITY) + xOffset); | ||
} | ||
// draw graph x axis ticks and labels | ||
g.setColor(Color.BLACK); | ||
g.drawString(s, xValue - sWidth, (int) bounds.y + (int) transformY(zeroTick) + 15); | ||
g.drawLine(xValue, (int)transformY(zeroTick) - 2, xValue, bounds.y + (int)transformY(zeroTick) + 5); | ||
// draw vertical grid lines | ||
g.setColor(gridColor); | ||
g.drawLine(xValue, (int)transformY(zeroTick) - 3, xValue, bounds.y + (int)transformY(lastTick)); | ||
} | ||
g.setColor(Color.BLACK); | ||
// draw major y axis | ||
g.drawLine(bounds.x + xOffset, (int) transformY(lastTick) - 5, bounds.x + xOffset, bounds.y + (int) transformY(zeroTick) + 5); | ||
// draw major x axis | ||
g.drawLine(xOffset, (int) transformY(zeroTick), bounds.width - xPadding, (int)transformY(zeroTick)); | ||
|
||
g.setTransform(AffineTransform.getTranslateInstance(xOffset, 0)); | ||
float xstep = (float) (bounds.width - xOffset) / (float) BUFFER_CAPACITY; | ||
float xstep = (float) (bounds.width - xOffset - xPadding) / (float) BUFFER_CAPACITY; | ||
int legendLength = graphs.size() * 10 + (graphs.size() - 1) * 3; | ||
|
||
for(int i = 0; i < graphs.size(); ++i) { | ||
|
@@ -206,6 +250,7 @@ protected void onCreateWindow(Container mainPane) { | |
|
||
serialRates.setMaximumSize(serialRates.getMinimumSize()); | ||
|
||
pane.add(Box.createHorizontalGlue()); | ||
pane.add(Box.createRigidArea(new Dimension(8, 0))); | ||
pane.add(serialRates); | ||
|
||
|
@@ -227,7 +272,7 @@ public void message(final String s) { | |
if (linebreak == -1) { | ||
break; | ||
} | ||
|
||
xCount++; | ||
String line = messageBuffer.substring(0, linebreak); | ||
messageBuffer.delete(0, linebreak + 1); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This color should be put into the theme and retrieved with something like
Theme.getColor("plotting.gridcolor")