-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlot.java
More file actions
155 lines (126 loc) · 4.85 KB
/
Plot.java
File metadata and controls
155 lines (126 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.stream.DoubleStream;
import javax.swing.*;
import java.awt.*;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.style.Styler.ChartTheme;
public class Plot extends JFrame {
private XYChart chart;
public Plot() {
super();
chart = new XYChartBuilder()
.width(1200).height(800)
.xAxisTitle("n").yAxisTitle("time, ms")
.theme(ChartTheme.Matlab)
.build();
createUI();
}
private double[][] loadResults(String name){
try (Scanner sc = new Scanner(new File(name + "-results.txt"))) {
int resultsCount = sc.nextInt();
double[][] results = new double[2][resultsCount];
results[0] = DoubleStream.generate(sc::nextDouble)
.limit(resultsCount)
.toArray();
results[1] = DoubleStream.generate(sc::nextDouble)
.limit(resultsCount)
.toArray();
return results;
} catch (FileNotFoundException e) {
throw new RuntimeException();
}
}
private JTextField minNField;
private JTextField maxNField;
private JTextField deltaNField;
private JTextField itersField;
private JPanel buttonsPanel;
private JPanel chartPanel;
private void createUI() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
chartPanel = new XChartPanel<XYChart>(chart);
add(chartPanel);
JPanel fieldsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
add(fieldsPanel);
JLabel minNLabel = new JLabel("Min N:");
minNField = new JTextField("1000", 10);
minNLabel.setLabelFor(minNField);
fieldsPanel.add(minNLabel);
fieldsPanel.add(minNField);
JLabel maxNLabel = new JLabel("Max N:");
maxNField = new JTextField("15000", 10);
maxNLabel.setLabelFor(maxNField);
fieldsPanel.add(maxNLabel);
fieldsPanel.add(maxNField);
JLabel deltaNLabel = new JLabel("Delta N:");
deltaNField = new JTextField("500", 10);
deltaNLabel.setLabelFor(deltaNField);
fieldsPanel.add(deltaNLabel);
fieldsPanel.add(deltaNField);
JLabel itersLabel = new JLabel("Iters:");
itersField = new JTextField("5", 10);
itersLabel.setLabelFor(itersField);
fieldsPanel.add(itersLabel);
fieldsPanel.add(itersField);
buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
createSortingButton("selection");
createSortingButton("quick");
createSortingButton("counting");
createSortingButton("insertion");
createSortingButton("bubble");
createSortingButton("heap");
createSortingButton("merge");
add(buttonsPanel);
pack();
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private static Map<String, Consumer<int[]>> nameToMethod = new HashMap<>();
static {
nameToMethod.put("selection", Selection::sort);
nameToMethod.put("counting", Counting::sort);
nameToMethod.put("quick", Quick::sort);
nameToMethod.put("insertion", Insertion::sort);
nameToMethod.put("bubble", Bubble::sort);
nameToMethod.put("heap", Heap::sort);
nameToMethod.put("merge", Merge::sort);
}
private void createSortingButton(String name) {
JButton jb = new JButton(name);
jb.addActionListener(e -> {
Benchmarking bench;
try {
bench = new Benchmarking(nameToMethod.get(name), jb.getText(),
Integer.parseInt(minNField.getText()),
Integer.parseInt(maxNField.getText()),
Integer.parseInt(deltaNField.getText()),
Integer.parseInt(itersField.getText())
);
} catch (RuntimeException ex) {
bench = new Benchmarking(nameToMethod.get(name), jb.getText());
}
bench.run();
updateSeries(jb.getText());
chartPanel.repaint();
});
buttonsPanel.add(jb);
}
private void updateSeries(String name) {
double[][] results = loadResults(name);
try {
chart.updateXYSeries(name, results[0], results[1], null);
} catch (IllegalArgumentException e) {
chart.addSeries(name, results[0], results[1]);
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> new Plot());
}
}