Skip to content

Commit

Permalink
Adds configurable graph min/max y-values (wpilibsuite#135)
Browse files Browse the repository at this point in the history
This also prevents the graph min/max y-values going above/below each other (and causing an exception).
  • Loading branch information
modelmat authored Oct 27, 2020
1 parent 55f6784 commit 75a3e15
Showing 1 changed file with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class LinePlot extends AbstractValueWidget {
public final IntegerProperty bufferSize
= new IntegerProperty(this, "Buffer Size (samples)", 5000);
public final BooleanProperty clear = new BooleanProperty(this, "Clear Graph", false);
public final BooleanProperty autoScale = new BooleanProperty(this, "Autoscale Y Values", true);
public final IntegerProperty minY = new IntegerProperty(this, "Minimum Y Value", 0);
public final IntegerProperty maxY = new IntegerProperty(this, "Maximum Y Value", 1);

JPanel m_chartPanel;
XYSeries m_data;
Expand All @@ -38,10 +41,10 @@ public void init() {

m_data = new XYSeries(getFieldName());
m_dataset = new XYSeriesCollection(m_data);

startTime = System.currentTimeMillis() / 1000.0;

JFreeChart chart = ChartFactory.createXYLineChart(
m_chart = ChartFactory.createXYLineChart(
getFieldName(),
"Time (Seconds)",
"Data",
Expand All @@ -51,7 +54,9 @@ public void init() {
true,
false);

m_chartPanel = new ChartPanel(chart);
updateChartRange();

m_chartPanel = new ChartPanel(m_chart);
m_chartPanel.setPreferredSize(new Dimension(400, 300));
m_chartPanel.setBackground(getBackground());

Expand Down Expand Up @@ -88,5 +93,23 @@ public void propertyChanged(Property property) {
startTime = System.currentTimeMillis() / 1000.0;
}
}

if (property == minY || property == maxY || property == autoScale) {
if (property == minY && minY.getValue() > maxY.getValue()) {
minY.setValue(maxY.getValue() - 1);
} else if (property == maxY && maxY.getValue() < minY.getValue()) {
maxY.setValue(minY.getValue() + 1);
}

updateChartRange();
}
}

public void updateChartRange() {
if (autoScale.getValue()) {
m_chart.getXYPlot().getRangeAxis().setAutoRange(true);
} else {
m_chart.getXYPlot().getRangeAxis().setRange(minY.getValue(), maxY.getValue());
}
}
}

0 comments on commit 75a3e15

Please sign in to comment.