Commit a8fa63ff authored by edmundszagars's avatar edmundszagars
Browse files

Refactoring graph drawing

parent 2e2025a3
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinCommonCompilerArguments">
<option name="languageVersion" value="1.1" />
<option name="apiVersion" value="1.1" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="true" project-jdk-name="10" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -18,5 +18,7 @@ ...@@ -18,5 +18,7 @@
<SOURCES /> <SOURCES />
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="library" name="R User Library" level="project" />
<orderEntry type="library" name="R Skeletons" level="application" />
</component> </component>
</module> </module>
\ No newline at end of file
import gui.MainWindow; import gui.MainWindow;
import javafx.application.Application; import javafx.application.Application;
import javafx.stage.Stage; import javafx.stage.Stage;
import sorting_algorithms.*;
import java.util.ArrayList;
import java.util.Collections;
public class Main extends Application{ public class Main extends Application{
public static void main(String[] args) { public static void main(String[] args) {
...@@ -9,7 +13,14 @@ public class Main extends Application{ ...@@ -9,7 +13,14 @@ public class Main extends Application{
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
MainWindow mainWindow = new MainWindow(); ArrayList<Sortable> allSorts = new ArrayList<>();
allSorts.add(new BubbleSort());
allSorts.add(new InsertionSort());
allSorts.add(new MergeSort());
allSorts.add(new QuickSort());
//allSorts.add(new ShitSort());
allSorts.add(new SelectionSort());
MainWindow mainWindow = new MainWindow(allSorts);
mainWindow.start(primaryStage); mainWindow.start(primaryStage);
} }
} }
...@@ -9,23 +9,14 @@ import javafx.scene.chart.XYChart; ...@@ -9,23 +9,14 @@ import javafx.scene.chart.XYChart;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.HashMap;
import java.util.List;
public class Graph extends Application { public class Graph extends Application {
private final NumberAxis xAxis; private final NumberAxis xAxis;
private final NumberAxis yAxis; private final NumberAxis yAxis;
private final LineChart<Number, Number> bubbleLineChart; private HashMap<String, XYChart.Series<Number, Number>> axes;
private final LineChart<Number, Number> insertionLineChart;
private final LineChart<Number, Number> mergeLineChart;
private final LineChart<Number, Number> quickLineChart;
private final LineChart<Number, Number> selectionLineChart;
private XYChart.Series<Number, Number> bubbleSeries;
private XYChart.Series<Number, Number> insertionSeries;
private XYChart.Series<Number, Number> mergeSeries;
private XYChart.Series<Number, Number> quickSeries;
private XYChart.Series<Number, Number> selectionSeries;
Graph(ArrayList<Result> results) { Graph(ArrayList<Result> results) {
xAxis = new NumberAxis(); xAxis = new NumberAxis();
...@@ -33,53 +24,16 @@ public class Graph extends Application { ...@@ -33,53 +24,16 @@ public class Graph extends Application {
xAxis.setLabel("Array size"); xAxis.setLabel("Array size");
yAxis.setLabel("Time"); yAxis.setLabel("Time");
this.bubbleLineChart = new LineChart<>(xAxis, yAxis); axes = new HashMap<>();
this.insertionLineChart = new LineChart<>(xAxis, yAxis);
this.mergeLineChart = new LineChart<>(xAxis, yAxis);
this.quickLineChart = new LineChart<>(xAxis, yAxis);
this.selectionLineChart = new LineChart<>(xAxis, yAxis);
bubbleSeries = new XYChart.Series<>();
bubbleSeries.setName("Bubble sort");
insertionSeries = new XYChart.Series<>();
insertionSeries.setName("Insertion sort");
mergeSeries = new XYChart.Series<>();
mergeSeries.setName("Merge sort");
quickSeries = new XYChart.Series<>();
quickSeries.setName("Quick sort");
selectionSeries = new XYChart.Series<>();
selectionSeries.setName("Selection sort");
for (Result anResult : results) { for (Result anResult : results) {
switch (anResult.getSortTypeName()) { if (!axes.containsKey(anResult.sortTypeName)) {
case "Bubble sort": XYChart.Series<Number, Number> value = new XYChart.Series<>();
bubbleSeries.getData().add(new XYChart.Data<>(Integer.parseInt(anResult.getArraySize()), anResult.getExecutionResultTime())); axes.put(anResult.sortTypeName, value);
bubbleSeries.setName(anResult.getSortTypeName()); value.setName(anResult.sortTypeName);
break;
case "Insertion sort":
insertionSeries.getData().add(new XYChart.Data<>(Integer.parseInt(anResult.getArraySize()), anResult.getExecutionResultTime()));
insertionSeries.setName(anResult.getSortTypeName());
break;
case "Merge sort":
mergeSeries.getData().add(new XYChart.Data<>(Integer.parseInt(anResult.getArraySize()), anResult.getExecutionResultTime()));
mergeSeries.setName(anResult.getSortTypeName());
break;
case "Quick sort":
quickSeries.getData().add(new XYChart.Data<>(Integer.parseInt(anResult.getArraySize()), anResult.getExecutionResultTime()));
quickSeries.setName(anResult.getSortTypeName());
break;
case "Selection sort":
selectionSeries.getData().add(new XYChart.Data<>(Integer.parseInt(anResult.getArraySize()), anResult.getExecutionResultTime()));
selectionSeries.setName(anResult.getSortTypeName());
break;
} }
XYChart.Series<Number, Number> series = axes.get(anResult.sortTypeName);
series.getData().add(new XYChart.Data<>(anResult.arraySize, anResult.executionResultTime));
} }
} }
...@@ -94,7 +48,7 @@ public class Graph extends Application { ...@@ -94,7 +48,7 @@ public class Graph extends Application {
new LineChart<>(xAxis, yAxis); new LineChart<>(xAxis, yAxis);
Scene scene = new Scene(lineChart, 800, 600); Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().addAll(Arrays.asList(bubbleSeries, insertionSeries, mergeSeries, quickSeries, selectionSeries)); lineChart.getData().addAll(axes.values());
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.showAndWait(); primaryStage.showAndWait();
......
...@@ -3,6 +3,10 @@ package gui; ...@@ -3,6 +3,10 @@ package gui;
import helper.GetDataFromCSV; import helper.GetDataFromCSV;
import helper.Result; import helper.Result;
import javafx.application.Application; import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.scene.Scene; import javafx.scene.Scene;
...@@ -11,6 +15,7 @@ import javafx.scene.control.cell.PropertyValueFactory; ...@@ -11,6 +15,7 @@ import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Callback;
import sorting_algorithms.*; import sorting_algorithms.*;
import java.io.File; import java.io.File;
...@@ -19,6 +24,8 @@ import java.io.IOException; ...@@ -19,6 +24,8 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainWindow extends Application { public class MainWindow extends Application {
private FileChooser fileChooser; private FileChooser fileChooser;
...@@ -28,6 +35,18 @@ public class MainWindow extends Application { ...@@ -28,6 +35,18 @@ public class MainWindow extends Application {
private ArrayList<Result> results = new ArrayList<>(); private ArrayList<Result> results = new ArrayList<>();
private ArrayList<Sortable> sortables = new ArrayList<>(); private ArrayList<Sortable> sortables = new ArrayList<>();
private List<CheckBoxBuilder> checkBoxBuilders = new ArrayList<>();
private List<Sortable> allSorts = new ArrayList<>();
private VBox layout = new VBox(15);
public MainWindow(List<Sortable> sortAlgorithms) {
this.allSorts = Collections.unmodifiableList(sortAlgorithms);
for (Sortable sortable : allSorts) {
checkBoxBuilders.add(new CheckBoxBuilder(sortables, sortable.getSortTypeName(), sortable));
}
}
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
Label label = new Label("Select file location: "); Label label = new Label("Select file location: ");
...@@ -71,91 +90,35 @@ public class MainWindow extends Application { ...@@ -71,91 +90,35 @@ public class MainWindow extends Application {
saveToFileDialog(); saveToFileDialog();
}); });
CheckBox bubbleSortCheckBox = new CheckBox(); for (Sortable sortable : allSorts) {
bubbleSortCheckBox.setText("Bubble sort"); layout.getChildren().add(new CheckBoxBuilder().build(sortables, sortable.getSortTypeName(), sortable));
bubbleSortCheckBox.setSelected(false); }
bubbleSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (bubbleSortCheckBox.isSelected()) {
System.out.println("Bubble selected");
sortables.add(new BubbleSort());
System.out.println("Sortable size now: " + sortables.size());
} else if (!bubbleSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals("Bubble sort"));
System.out.println("Sortable size now: " + sortables.size());
}
});
CheckBox selectionSortCheckBox = new CheckBox();
selectionSortCheckBox.setText("Selection sort");
selectionSortCheckBox.setSelected(false);
selectionSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (selectionSortCheckBox.isSelected()) {
System.out.println("Selection selected");
sortables.add(new SelectionSort());
System.out.println("Sortable size now: " + sortables.size());
} else if (!selectionSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals("Selection sort"));
System.out.println("Sortable size now: " + sortables.size());
}
});
CheckBox insertionSortCheckBox = new CheckBox();
insertionSortCheckBox.setText("Insertion sort");
insertionSortCheckBox.setSelected(false);
insertionSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (insertionSortCheckBox.isSelected()) {
System.out.println("Insertion selected");
sortables.add(new InsertionSort());
System.out.println("Sortable size now: " + sortables.size());
} else if (!insertionSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals("Insertion sort"));
System.out.println("Sortable size now: " + sortables.size());
}
});
CheckBox quickSortCheckBox = new CheckBox();
quickSortCheckBox.setText("Quick sort");
quickSortCheckBox.setSelected(false);
quickSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (quickSortCheckBox.isSelected()) {
System.out.println("Quick selected");
sortables.add(new QuickSort());
System.out.println("Sortable size now: " + sortables.size());
} else if (!quickSortCheckBox.isSelected()) { TableColumn<Result, String> dataColumn = new TableColumn<>("Sort type");
sortables.removeIf(p -> p.getSortTypeName().equals("Quick sort")); dataColumn.setMinWidth(300);
System.out.println("Sortable size now: " + sortables.size()); dataColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Result, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Result, String> param) {
return new SimpleStringProperty(param.getValue().sortTypeName);
} }
}); });
TableColumn<Result, Number> resultDataColumn = new TableColumn<>("Array size");
CheckBox mergeSortCheckBox = new CheckBox(); resultDataColumn.setMinWidth(200);
mergeSortCheckBox.setText("Merge sort"); resultDataColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Result, Number>, ObservableValue<Number>>() {
mergeSortCheckBox.setSelected(false); @Override
mergeSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> { public ObservableValue<Number> call(TableColumn.CellDataFeatures<Result, Number> param) {
if (mergeSortCheckBox.isSelected()) { return new SimpleIntegerProperty(param.getValue().arraySize);
System.out.println("Merge selected");
sortables.add(new MergeSort());
System.out.println("Sortable size now: " + sortables.size());
} else if (!mergeSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals("Merge sort"));
System.out.println("Sortable size now: " + sortables.size());
} }
}); });
TableColumn<Result, Double> dataColumn = new TableColumn<>("Sort type"); TableColumn<Result, Number> timeColumn = new TableColumn<>("Execution Time");
dataColumn.setMinWidth(300);
dataColumn.setCellValueFactory(new PropertyValueFactory<>("sortTypeName"));
TableColumn<Result, Double> resultDataColumn = new TableColumn<>("Array size");
resultDataColumn.setMinWidth(200);
resultDataColumn.setCellValueFactory(new PropertyValueFactory<>("arraySize"));
TableColumn<Result, Long> timeColumn = new TableColumn<>("Execution Time");
timeColumn.setMinWidth(200); timeColumn.setMinWidth(200);
timeColumn.setCellValueFactory(new PropertyValueFactory<>("executionResultTime")); timeColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Result, Number>, ObservableValue<Number>>() {
@Override
public ObservableValue<Number> call(TableColumn.CellDataFeatures<Result, Number> param) {
return new SimpleLongProperty(param.getValue().executionResultTime);
}
});
table = new TableView<>(); table = new TableView<>();
table.setItems(getResults(results)); table.setItems(getResults(results));
table.getColumns().addAll(Arrays.asList(dataColumn, resultDataColumn, timeColumn)); table.getColumns().addAll(Arrays.asList(dataColumn, resultDataColumn, timeColumn));
...@@ -187,29 +150,21 @@ public class MainWindow extends Application { ...@@ -187,29 +150,21 @@ public class MainWindow extends Application {
} }
for (Object aList : list) { for (Object aList : list) {
for (Sortable sortable : sortables) { for (Sortable sortable : sortables) {
Result result = new Result();
double[] ar = dataFromCSV.getDoubleArrayFromListItem((ArrayList) aList); double[] ar = dataFromCSV.getDoubleArrayFromListItem((ArrayList) aList);
sortable.sort(ar); sortable.sort(ar);
result.setExecutionResultTime(sortable.getExecutionEndTime()); Result result = new Result(sortable.getExecutionEndTime(), sortable.getSortTypeName(), ar.length);
result.setSortTypeName(sortable.getSortTypeName());
result.setArraySize(ar);
table.getItems().add(result); table.getItems().add(result);
results.add(result); results.add(result);
} }
} }
}); });
VBox layout = new VBox(15);
layout.maxHeight(50); layout.maxHeight(50);
layout.getChildren().addAll(label, layout.getChildren().addAll(label,
fileLocation, fileLocation,
selectFileButton, selectFileButton,
bubbleSortCheckBox,
selectionSortCheckBox,
insertionSortCheckBox,
quickSortCheckBox,
mergeSortCheckBox,
startButton, startButton,
table, table,
writeToFileButton, writeToFileButton,
...@@ -230,9 +185,9 @@ public class MainWindow extends Application { ...@@ -230,9 +185,9 @@ public class MainWindow extends Application {
PrintWriter writer = new PrintWriter(file); PrintWriter writer = new PrintWriter(file);
String result; String result;
for (Result anResult : results) { for (Result anResult : results) {
result = "Sort type: " + anResult.getSortTypeName() + " \t" result = "Sort type: " + anResult.sortTypeName + " \t"
+ "Array size " + anResult.getArraySize() + " \t" + "Array size " + anResult.arraySize + " \t"
+ "Time(mills): " + anResult.getExecutionResultTime() + "\n"; + "Time(mills): " + anResult.executionResultTime + "\n";
writer.println(result); writer.println(result);
} }
writer.close(); writer.close();
...@@ -257,3 +212,45 @@ public class MainWindow extends Application { ...@@ -257,3 +212,45 @@ public class MainWindow extends Application {
} }
class CheckBoxBuilder {
CheckBoxBuilder(ArrayList<Sortable> sortables, String sortTypeName, Sortable sortType) {
/*
CheckBox bubbleSortCheckBox = new CheckBox();
bubbleSortCheckBox.setText(sortTypeName);
bubbleSortCheckBox.setSelected(false);
bubbleSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (bubbleSortCheckBox.isSelected()) {
System.out.println("Bubble selected");
sortables.add(sortType);
System.out.println("Sortable size now: " + sortables.size());
} else if (!bubbleSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals(sortType.getSortTypeName()));
System.out.println("Sortable size now: " + sortables.size());
}
});
*/
}
public CheckBoxBuilder() {
}
public CheckBox build(ArrayList<Sortable> sortables, String sortTypeName, Sortable sortType) {
CheckBox checkBox = new CheckBox();
checkBox.setText(sortTypeName);
checkBox.setSelected(false);
checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (checkBox.isSelected()) {
System.out.println(sortType.getSortTypeName() + " selected");
sortables.add(sortType);
System.out.println("Sortable size now: " + sortables.size());
} else if (!checkBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals(sortType.getSortTypeName()));
System.out.println("Sortable size now: " + sortables.size());
}
});
return checkBox;
}
}
package helper; package helper;
public class Result { public class Result {
private double[] startData; public final String sortTypeName;
private double[] resultData; public final Integer arraySize;
private String sortTypeName; public final long executionResultTime;
private String arraySize;
private long executionResultTime;
public Result() { public Result(long executionResultTime, String sortTypeName, Integer arraySize) {
this.startData = new double[]{};
this.resultData = new double[]{};
this.executionResultTime = 0;
}
public String getSortTypeName() {
return sortTypeName;
}
public void setSortTypeName(String sortTypeName) {
this.sortTypeName = sortTypeName;
}
public String getArraySize() {
return arraySize;
}
public void setArraySize(double[] array) {
if(array.length > 0)
this.arraySize = Integer.toString(array.length);
}
public void setExecutionResultTime(long executionResultTime) {
this.executionResultTime = executionResultTime; this.executionResultTime = executionResultTime;
this.sortTypeName = sortTypeName;
this.arraySize = arraySize;
} }
public long getExecutionResultTime() {
return executionResultTime;
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment