Skip to content

Instantly share code, notes, and snippets.

@BartKeulen
Created February 21, 2017 18:19
Show Gist options
  • Save BartKeulen/1330a8e403d851f72c8d4c5d156a5237 to your computer and use it in GitHub Desktop.
Save BartKeulen/1330a8e403d851f72c8d4c5d156a5237 to your computer and use it in GitHub Desktop.
Real time line plot demo
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import smile.plot.*;
/**
*
* @author Haifeng Li
*/
@SuppressWarnings("serial")
public class LinePlotDemo extends JPanel {
PlotCanvas canvas;
public LinePlotDemo() {
super(new GridLayout(1,2));
double[][] data = getData2D(0);
canvas = LinePlot.plot("Line 1", data, Line.Style.DASH, Color.RED);
canvas.setTitle("2D Lines");
add(canvas);
}
@Override
public String toString() {
return "Line Plot";
}
public double[][] getData2D(int i) {
double[][] data = new double[100][2];
for (int j = 0; j < data.length; j++) {
data[j][0] = 2 * Math.PI * (double) j / data.length;
data[j][1] = Math.sin(data[j][0] + i*Math.PI/100);
}
return data;
}
public void update2D() {
for (int i = 1; i < 1000; i++) {
try {
Thread.sleep(25);
double[][] data = getData2D(i);
canvas.clear();
canvas.line("Line 1", data, Line.Style.DASH, Color.RED);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
LinePlotDemo linePlot = new LinePlotDemo();
JFrame frame = new JFrame("Line Plot");
frame.setMinimumSize(new Dimension(1000, 500));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(linePlot);
frame.setVisible(true);
linePlot.update2D();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment