Commit ece010f4 authored by edmundszagars's avatar edmundszagars
Browse files

-Implementing Graph class that will graph and show new window with graphed results.

-Also removing unused code
parent 9c242b34
File added
package gui;
import helper.Result;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Arrays;
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;
Graph(ArrayList<Result> results) {
xAxis = new NumberAxis();
yAxis = new NumberAxis();
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");
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;
}
}
bubbleLineChart.getData().add(bubbleSeries);
insertionLineChart.getData().add(insertionSeries);
mergeLineChart.getData().add(mergeSeries);
quickLineChart.getData().add(quickSeries);
selectionLineChart.getData().add(selectionSeries);
}
@Override
public void start(Stage primaryStage){
primaryStage.setTitle("Line Chart Sample");
xAxis.setLabel("Array size");
yAxis.setLabel("Time(mills)");
final LineChart<Number, Number> lineChart =
new LineChart<>(xAxis, yAxis);
Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().addAll(Arrays.asList(bubbleSeries, insertionSeries, mergeSeries, quickSeries, selectionSeries));
primaryStage.setScene(scene);
primaryStage.showAndWait();
}
}
...@@ -13,7 +13,10 @@ import javafx.stage.FileChooser; ...@@ -13,7 +13,10 @@ import javafx.stage.FileChooser;
import javafx.stage.Stage; import javafx.stage.Stage;
import sorting_algorithms.*; import sorting_algorithms.*;
import java.io.*; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -27,7 +30,6 @@ public class MainWindow extends Application { ...@@ -27,7 +30,6 @@ public class MainWindow extends Application {
@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: ");
TextField fileLocation = new TextField(); TextField fileLocation = new TextField();
...@@ -46,6 +48,21 @@ public class MainWindow extends Application { ...@@ -46,6 +48,21 @@ public class MainWindow extends Application {
} }
}); });
Button showGraphButton = new Button("Show graph");
showGraphButton.setOnAction(event -> {
if (results.isEmpty()) {
new ErrorBox("There are no results", "There are no results for graphing");
return;
}
Graph graph = new Graph(results);
try {
graph.start(new Stage());
} catch (Exception e) {
new ErrorBox("Could not display graph", "Was not able to display graph for your results");
e.printStackTrace();
}
});
Button writeToFileButton = new Button("Write results to file"); Button writeToFileButton = new Button("Write results to file");
writeToFileButton.setOnAction(event -> { writeToFileButton.setOnAction(event -> {
if (results.isEmpty()) { if (results.isEmpty()) {
...@@ -195,7 +212,8 @@ public class MainWindow extends Application { ...@@ -195,7 +212,8 @@ public class MainWindow extends Application {
mergeSortCheckBox, mergeSortCheckBox,
startButton, startButton,
table, table,
writeToFileButton); writeToFileButton,
showGraphButton);
Scene mainScene = new Scene(layout, 700, 500); Scene mainScene = new Scene(layout, 700, 500);
...@@ -209,10 +227,8 @@ public class MainWindow extends Application { ...@@ -209,10 +227,8 @@ public class MainWindow extends Application {
} }
private void writeResultsToFile(String fileName, File file) throws IOException { private void writeResultsToFile(String fileName, File file) throws IOException {
//FileOutputStream fileOutputStream = new FileOutputStream(fileName);
//DataOutputStream dataOutputStream = new DataOutputStream(file);
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.getSortTypeName() + " \t"
+ "Array size " + anResult.getArraySize() + " \t" + "Array size " + anResult.getArraySize() + " \t"
......
package helper; package helper;
import java.util.Arrays;
public class Result { public class Result {
private double[] startData; private double[] startData;
private double[] resultData; private double[] resultData;
......
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