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"?>
<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" />
</component>
</project>
\ No newline at end of file
......@@ -18,5 +18,7 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="R User Library" level="project" />
<orderEntry type="library" name="R Skeletons" level="application" />
</component>
</module>
\ No newline at end of file
import gui.MainWindow;
import javafx.application.Application;
import javafx.stage.Stage;
import sorting_algorithms.*;
import java.util.ArrayList;
import java.util.Collections;
public class Main extends Application{
public static void main(String[] args) {
......@@ -9,7 +13,14 @@ public class Main extends Application{
@Override
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);
}
}
......@@ -9,23 +9,14 @@ import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class Graph extends Application {
private final NumberAxis xAxis;
private final NumberAxis yAxis;
private final LineChart<Number, Number> bubbleLineChart;
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;
private HashMap<String, XYChart.Series<Number, Number>> axes;
Graph(ArrayList<Result> results) {
xAxis = new NumberAxis();
......@@ -33,53 +24,16 @@ public class Graph extends Application {
xAxis.setLabel("Array size");
yAxis.setLabel("Time");
this.bubbleLineChart = new LineChart<>(xAxis, yAxis);
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");
axes = new HashMap<>();
for (Result anResult : results) {
switch (anResult.getSortTypeName()) {
case "Bubble sort":
bubbleSeries.getData().add(new XYChart.Data<>(Integer.parseInt(anResult.getArraySize()), anResult.getExecutionResultTime()));
bubbleSeries.setName(anResult.getSortTypeName());
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;
if (!axes.containsKey(anResult.sortTypeName)) {
XYChart.Series<Number, Number> value = new XYChart.Series<>();
axes.put(anResult.sortTypeName, value);
value.setName(anResult.sortTypeName);
}
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 {
new LineChart<>(xAxis, yAxis);
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.showAndWait();
......
......@@ -3,6 +3,10 @@ package gui;
import helper.GetDataFromCSV;
import helper.Result;
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.ObservableList;
import javafx.scene.Scene;
......@@ -11,6 +15,7 @@ import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Callback;
import sorting_algorithms.*;
import java.io.File;
......@@ -19,6 +24,8 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainWindow extends Application {
private FileChooser fileChooser;
......@@ -28,6 +35,18 @@ public class MainWindow extends Application {
private ArrayList<Result> results = 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
public void start(Stage primaryStage) {
Label label = new Label("Select file location: ");
......@@ -71,91 +90,35 @@ public class MainWindow extends Application {
saveToFileDialog();
});
CheckBox bubbleSortCheckBox = new CheckBox();
bubbleSortCheckBox.setText("Bubble sort");
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());
for (Sortable sortable : allSorts) {
layout.getChildren().add(new CheckBoxBuilder().build(sortables, sortable.getSortTypeName(), sortable));
}
} else if (!quickSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals("Quick sort"));
System.out.println("Sortable size now: " + sortables.size());
TableColumn<Result, String> dataColumn = new TableColumn<>("Sort type");
dataColumn.setMinWidth(300);
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);
}
});
CheckBox mergeSortCheckBox = new CheckBox();
mergeSortCheckBox.setText("Merge sort");
mergeSortCheckBox.setSelected(false);
mergeSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (mergeSortCheckBox.isSelected()) {
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, Number> resultDataColumn = new TableColumn<>("Array size");
resultDataColumn.setMinWidth(200);
resultDataColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Result, Number>, ObservableValue<Number>>() {
@Override
public ObservableValue<Number> call(TableColumn.CellDataFeatures<Result, Number> param) {
return new SimpleIntegerProperty(param.getValue().arraySize);
}
});
TableColumn<Result, Double> dataColumn = new TableColumn<>("Sort type");
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");
TableColumn<Result, Number> timeColumn = new TableColumn<>("Execution Time");
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.setItems(getResults(results));
table.getColumns().addAll(Arrays.asList(dataColumn, resultDataColumn, timeColumn));
......@@ -187,29 +150,21 @@ public class MainWindow extends Application {
}
for (Object aList : list) {
for (Sortable sortable : sortables) {
Result result = new Result();
double[] ar = dataFromCSV.getDoubleArrayFromListItem((ArrayList) aList);
sortable.sort(ar);
result.setExecutionResultTime(sortable.getExecutionEndTime());
result.setSortTypeName(sortable.getSortTypeName());
result.setArraySize(ar);
Result result = new Result(sortable.getExecutionEndTime(), sortable.getSortTypeName(), ar.length);
table.getItems().add(result);
results.add(result);
}
}
});
VBox layout = new VBox(15);
layout.maxHeight(50);
layout.getChildren().addAll(label,
fileLocation,
selectFileButton,
bubbleSortCheckBox,
selectionSortCheckBox,
insertionSortCheckBox,
quickSortCheckBox,
mergeSortCheckBox,
startButton,
table,
writeToFileButton,
......@@ -230,9 +185,9 @@ public class MainWindow extends Application {
PrintWriter writer = new PrintWriter(file);
String result;
for (Result anResult : results) {
result = "Sort type: " + anResult.getSortTypeName() + " \t"
+ "Array size " + anResult.getArraySize() + " \t"
+ "Time(mills): " + anResult.getExecutionResultTime() + "\n";
result = "Sort type: " + anResult.sortTypeName + " \t"
+ "Array size " + anResult.arraySize + " \t"
+ "Time(mills): " + anResult.executionResultTime + "\n";
writer.println(result);
}
writer.close();
......@@ -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;
public class Result {
private double[] startData;
private double[] resultData;
private String sortTypeName;
private String arraySize;
private long executionResultTime;
public final String sortTypeName;
public final Integer arraySize;
public final long executionResultTime;
public Result() {
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) {
public Result(long executionResultTime, String sortTypeName, Integer arraySize) {
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