-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add FuncSeries (unused) and graphs * Remove factory methods from series * Add min, max time to TimeSeries * Add axes class for convenience * Decouple updating series and drawing * Add timespan * Add playground with data graph * Remove imports from series * Add polling option to series * Use polling in plot to standardize runtime * Simplify playground example * Simplify playground example * Replace timespan with domain * make mouse methods easier * Add everything to playground * Update readme * Add BStream to playground * Rename duration to capacity in Config * Add Series documentation * Replace isPolling() in Series with final var set in constructor * Add an in place sort and lower bound finding to Interpolator (#47) * Add lower bound method * Remove exceptions from getSortedPoints * Add in-place sort Co-authored-by: iwei20 <[email protected]> * Fix naming on polling variables/method * Revert "Add an in place sort and lower bound finding to Interpolator (#47)" (#48) This reverts commit b8d1730. * Update Series documentation * Add Plot documentation * Change isPolling documentation to not use isPolling() method * Make method protected * Proofread readme * fix config section * Update later sections * Update "Working with a Plot" * Add javadocs documentation to plot classes Co-authored-by: Myles Pasetsky <[email protected]> Co-authored-by: BenG49 <[email protected]> Co-authored-by: Ivan Wei <[email protected]> Co-authored-by: iwei20 <[email protected]> Co-authored-by: Myles Pasetsky <[email protected]>
- Loading branch information
1 parent
762ace2
commit 583a674
Showing
8 changed files
with
752 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.stuypulse.stuylib.util.plot; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.stuypulse.stuylib.streams.filters.IFilter; | ||
|
||
/** | ||
* A FuncSeries plots a function (IFilter) over a given domain. | ||
* | ||
* A FuncSeries data is precomputed, so its x and y values | ||
* are non-changing. This means that the series does not get | ||
* polled and does not implement pop() or poll(). | ||
* | ||
* @author Ben Goldfisher | ||
*/ | ||
public class FuncSeries extends Series { | ||
|
||
/** Domain describes the x-values that the series will be graphed over */ | ||
public static class Domain { | ||
|
||
/** min and max x-values that will be graphed */ | ||
public final double min, max; | ||
|
||
/** | ||
* Creates a Domain | ||
* | ||
* @param min smallest x-value that will be graphed | ||
* @param max largest x-value that will be graphed | ||
*/ | ||
public Domain(double min, double max) { | ||
this.min = min; | ||
this.max = max; | ||
} | ||
} | ||
|
||
/** Contains the precomputed (x, y) data values */ | ||
private List<Double> xValues; | ||
private List<Double> yValues; | ||
|
||
/** | ||
* Creates a FuncSeries and specifies that it is not polling. | ||
* | ||
* @param config series config | ||
* @param domain range of x-values inputted to the function | ||
* @param func a function with one input and output to be graphed | ||
*/ | ||
public FuncSeries(Config config, Domain domain, IFilter func) { | ||
super(config, false); | ||
|
||
xValues = new ArrayList<Double>(); | ||
yValues = new ArrayList<Double>(); | ||
|
||
// Fill the series capacity with evenly spaced points in the given domain | ||
for (int i = 0; i < config.getCapacity(); i++) { | ||
double x = (i * (domain.max - domain.min)) / config.getCapacity() + domain.min; | ||
|
||
xValues.add(x); | ||
yValues.add(func.get(x)); | ||
} | ||
} | ||
|
||
/** @return max number of stored (x, y) values */ | ||
@Override | ||
public int size() { | ||
return getConfig().getCapacity(); | ||
} | ||
|
||
/** | ||
* Returns reference to x values, which is safe because they | ||
* are precompted and non-changing | ||
* | ||
* @return reference to x values | ||
*/ | ||
@Override | ||
protected List<Double> getSafeXValues() { | ||
return xValues; | ||
} | ||
|
||
/** | ||
* Returns reference to y values, which is safe because they | ||
* are precompted and non-changing | ||
* | ||
* @return reference to y values | ||
*/ | ||
@Override | ||
protected List<Double> getSafeYValues() { | ||
return yValues; | ||
} | ||
|
||
/** | ||
* No-op because series is non-polling | ||
*/ | ||
@Override | ||
protected void pop() {} | ||
|
||
/** | ||
* No-op because series is non-polling | ||
*/ | ||
@Override | ||
protected void poll() {} | ||
|
||
} |
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -9,30 +9,58 @@ | |
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.swing.JFrame; | ||
|
||
import com.stuypulse.stuylib.math.Vector2D; | ||
|
||
import org.knowm.xchart.XChartPanel; | ||
import org.knowm.xchart.XYChart; | ||
import org.knowm.xchart.XYChartBuilder; | ||
|
||
/** | ||
* A plot contains and manages the window to which any data | ||
* is drawn. | ||
* | ||
* It stores the Series that it is going draw. It also has | ||
* methods that read the mouse's position, which can be used as | ||
* an input stream for a series. | ||
* | ||
* @author Myles Pasetsky ([email protected]) | ||
*/ | ||
public class Plot { | ||
|
||
/** A collection of Series to be graphed */ | ||
private List<Series> plots; | ||
|
||
/** The window that is created */ | ||
private JFrame frame; | ||
|
||
/** A reference to the XChart library */ | ||
private XYChart instance; | ||
private XChartPanel<XYChart> panel; | ||
|
||
/** A utility for finding mouse positions */ | ||
private MouseTracker mouse; | ||
|
||
/** A boolean to ensure the plot is updated at least once */ | ||
private boolean runOnce; | ||
|
||
/** | ||
* Creates a configured plot | ||
* | ||
* @param settings plot & window settings | ||
*/ | ||
public Plot(Settings settings) { | ||
// Setup series | ||
plots = new ArrayList<>(); | ||
|
||
// Setup window | ||
frame = new JFrame(settings.getTitle()); | ||
|
||
frame.getContentPane() | ||
.setPreferredSize(new Dimension(settings.getWidth(), settings.getHeight())); | ||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
// Create XYChart using settings | ||
instance = | ||
new XYChartBuilder() | ||
.title(settings.getTitle()) | ||
|
@@ -58,32 +86,74 @@ public Plot(Settings settings) { | |
|
||
frame.setLocationRelativeTo(null); | ||
frame.setVisible(true); | ||
|
||
runOnce = true; | ||
} | ||
|
||
/** Creates a plot with default settings */ | ||
public Plot() { | ||
this(new Settings()); | ||
} | ||
|
||
public MouseTracker getMouse() { | ||
return mouse; | ||
/** @return mouse position */ | ||
public Vector2D getMouse() { | ||
return mouse.getPosition(); | ||
} | ||
|
||
/** @return mouse y position */ | ||
public double getMouseY() { | ||
return mouse.getY(); | ||
} | ||
|
||
/** @return mouse x position */ | ||
public double getMouseX() { | ||
return mouse.getX(); | ||
} | ||
|
||
/** | ||
* Adds series to be graphed | ||
* | ||
* @return reference to self | ||
*/ | ||
public Plot addSeries(Series... series) { | ||
for (Series e : series) plots.add(e); | ||
return this; | ||
} | ||
|
||
public void update() { | ||
|
||
/** allows the series to update the XYChart */ | ||
public void updateSeries() { | ||
for (Series plot : plots) { | ||
plot.update(instance); | ||
} | ||
|
||
display(); | ||
} | ||
|
||
private void display() { | ||
/** repaints the screen */ | ||
public void display() { | ||
Toolkit.getDefaultToolkit().sync(); | ||
panel.revalidate(); | ||
panel.repaint(); | ||
} | ||
|
||
public void update() { | ||
updateSeries(); | ||
display(); | ||
} | ||
|
||
/** | ||
* Checks if any series are polling to see if the plot | ||
* should still update. | ||
* | ||
* @return if the plot should still run | ||
*/ | ||
public boolean isRunning() { | ||
if (runOnce) { | ||
runOnce = false; | ||
return true; | ||
} | ||
|
||
for (Series e : plots) { | ||
if (e.isPolling()) return true; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.