Skip to content

Instantly share code, notes, and snippets.

@juananpe
Created November 16, 2017 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juananpe/102f725719d9d1172dda93fef1f9c0ee to your computer and use it in GitHub Desktop.
Save juananpe/102f725719d9d1172dda93fef1f9c0ee to your computer and use it in GitHub Desktop.
package graphics;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYStepAreaRenderer;
import org.jfree.data.xy.*;
public class ChartMarketDepthWithThread extends JFrame {
XYSeriesCollection xYSeriesCollection;
//private Runnable task = () -> {
private Runnable task = new Runnable() {
@Override
public void run() {
double init0 = 94, init1 = 106;
while (true) {
try {
Thread.sleep(6000);
xYSeriesCollection.getSeries(0).add(init0++, 4D);
xYSeriesCollection.getSeries(1).add(init1++, 1D);
} catch (InterruptedException e) {
e.printStackTrace();
}
String threadName = Thread.currentThread().getName();
System.out.println("Hello " + threadName);
}
}
};
public ChartMarketDepthWithThread() {
};
public ChartMarketDepthWithThread(LayoutManager layout) {
super("market depth chart");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(this.createChart());
setSize(600, 200);
setLocationRelativeTo(null);
pack();
setVisible(true);
Thread thread = new Thread(task);
thread.start();
}
public ChartPanel createChart() {
// dataset & series
xYSeriesCollection = new XYSeriesCollection();
XYSeries xyseries = new XYSeries("bid");
xyseries.add(95D, 4D);
xyseries.add(96D, 3D);
xyseries.add(97D, 2D);
xyseries.add(98D, 1D);
XYSeries xyseries1 = new XYSeries("ask");
xyseries1.add(100D, 1D);
xyseries1.add(101D, 2D);
xyseries1.add(102D, 3D);
xyseries1.add(103D, 4D);
xyseries1.add(104D, 5D);
xYSeriesCollection.addSeries(xyseries);
xYSeriesCollection.addSeries(xyseries1);
// chart
JFreeChart jfreechart = ChartFactory.createXYLineChart("", "Price", "Amount", xYSeriesCollection, PlotOrientation.VERTICAL, false, false, false);
jfreechart.setBackgroundPaint(new Color(0xFF, 0xFF, 0xFF, 0));
XYPlot xyplot = (XYPlot) jfreechart.getPlot();
XYStepAreaRenderer xysteparearenderer = new XYStepAreaRenderer(1);
xysteparearenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
xyplot.setRenderer(xysteparearenderer);
// chart panel
final ChartPanel chartPanel = new ChartPanel(jfreechart);
// chartPanel.setBackground( Color.BLUE );
// chartPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
chartPanel.setMaximumDrawHeight(2000);
chartPanel.setMaximumDrawWidth(3000);
return chartPanel;
}
public static void main(String[] args) {
new ChartMarketDepthWithThread(new GridLayout(1, 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment