I'm trying to make a JFreeChart to compare the problem size of a tested method to its running time.
Here is the class that makes the scatter plot:
public class TestScatterPlot extends JFrame {
public TestScatterPlot(String title, XYSeriesCollection dataset){
super(title);
JFreeChart chart = ChartFactory.createScatterPlot(
"Time to problem size",
"problem size",
"time",
dataset);
XYPlot plot = (XYPlot)chart.getPlot();
plot.setBackgroundPaint(new Color(255,228,196));
// Create Panel
ChartPanel panel = new ChartPanel(chart);
setContentPane(panel);
}
}
Here is the test method:
@Test
public void testHierholzersAlgorithm() {
Map<Integer,Long> timeToProblemSize = new HashMap<>();
for(int trial = 0;trial<1000;trial++) {
//generate the test data
long startTime = System.nanoTime();
//run the method
long runTime = System.nanoTime() - startTime;
int dataSize = dataSize();
//test the data
timeToProblemSize.put(dataSize,runTime);
}
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series = new XYSeries("TimeToProblemSize");
for(Integer probSize:timeToProblemSize.keySet()){
series.add(probSize,timeToProblemSize.get(probSize));
}
dataset.addSeries(series);
SwingUtilities.invokeLater(() -> {
TestScatterPlot example = new TestScatterPlot("",dataset);
example.setSize(800, 400);
example.setLocationRelativeTo(null);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
example.setVisible(true);
});
}
When I run this, the chart frame seems to begin to appear then closes immediately.
How do I get my chart to show?
Note:
This is not a repeat of this question because the questioner is using a scanner reading user input. There is no scanner in this test method; all the input is generated randomly.
It is not a repeat of this question either. The questioner there had a Thread.sleep happening. There is no Thread.sleep here.