|
| 1 | +using OxyPlot.Series; |
| 2 | +using OxyPlot; |
| 3 | +using System.Windows; |
| 4 | +using OxyPlot.Axes; |
| 5 | +using OxyPlot.Legends; |
| 6 | +using System; |
| 7 | +using System.Text; |
| 8 | +using System.Data; |
| 9 | +using Microsoft.Win32; |
| 10 | +using System.IO; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using System.Windows.Interop; |
| 13 | +using System.Windows.Media; |
| 14 | + |
| 15 | +namespace AlgorithmGUI |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Interaction logic for MainWindow.xaml |
| 19 | + /// </summary> |
| 20 | + public partial class MainWindow : Window |
| 21 | + { |
| 22 | + public static string dataString; |
| 23 | + //9 questions, 8 iterations, 3 values (run times, averages, standard deviation) |
| 24 | + public MainWindow(double[,,] array) |
| 25 | + { |
| 26 | + InitializeComponent(); |
| 27 | + RenderOptions.ProcessRenderMode = RenderMode.Default; |
| 28 | + populateRichTextBox(this); |
| 29 | + //Plots the graphs |
| 30 | + PlotGraphs(array, this); |
| 31 | + //Loads the data to a csv format |
| 32 | + dataString = dataToCSV(array); |
| 33 | + //Polulates the datagrid with the csv format data |
| 34 | + makeDataTable(this); |
| 35 | + } |
| 36 | + |
| 37 | + private static void populateRichTextBox(MainWindow mainWindow) |
| 38 | + { |
| 39 | + mainWindow.TextBoxInfo.Text = "Graph Controls!\nMouse Wheel: Zoom\nRight Click: Move\nHide Elements: Click the Question Number at the bottom!\n\nQ: Graph showing an error?\nA: Just rerun D)!\n\nQ: Why is Algorithm H not running completely?\nA: Because it would take way too long otherwise, trust me.\n\nQ: Why are there 2 entries for 1 algorithm in the legend?\nA: So you can choose to hide the points or the line. This was the only way I was able to get a line with error bars.\n\nMade by Nove for Algorithm Analysis and Data Structures, 2023"; |
| 40 | + } |
| 41 | + |
| 42 | + private static void PlotGraphs(double[,,] array, MainWindow mainWindow) |
| 43 | + { |
| 44 | + //Plot the graphs |
| 45 | + var devGraph = new PlotModel(); |
| 46 | + var timeGraph = new PlotModel(); |
| 47 | + |
| 48 | + int[] sizes = new int[] { 8, 16, 32, 64, 128, 256, 512, 1024 }; |
| 49 | + |
| 50 | + //add the axis labels |
| 51 | + devGraph.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Cardinality of S" }); |
| 52 | + devGraph.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Average Deviation from ideal" }); |
| 53 | + timeGraph.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Cardinality of S" }); |
| 54 | + timeGraph.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Average Execution time (ms)" }); |
| 55 | + |
| 56 | + //add a legend |
| 57 | + var devLegend = new Legend { LegendTitle = "Question Number", LegendPosition = LegendPosition.BottomCenter, LegendPlacement = LegendPlacement.Outside, LegendOrientation = LegendOrientation.Horizontal }; |
| 58 | + var timeLegend = new Legend { LegendPosition = LegendPosition.BottomCenter, LegendPlacement = LegendPlacement.Outside, LegendOrientation = LegendOrientation.Horizontal }; |
| 59 | + |
| 60 | + devGraph.Legends.Add(devLegend); |
| 61 | + timeGraph.Legends.Add(timeLegend); |
| 62 | + |
| 63 | + //Synchronises the colours |
| 64 | + var customColors = new[] |
| 65 | + { |
| 66 | + OxyColors.Red, |
| 67 | + OxyColors.Orange, |
| 68 | + OxyColors.Yellow, |
| 69 | + OxyColors.LightGreen, |
| 70 | + OxyColors.Green, |
| 71 | + OxyColors.LightBlue, |
| 72 | + OxyColors.Blue, |
| 73 | + OxyColors.DarkBlue, |
| 74 | + OxyColors.Purple |
| 75 | + }; |
| 76 | + |
| 77 | + //populate the data into the graph |
| 78 | + |
| 79 | + for (int question = 1; question < 10; question++) |
| 80 | + { |
| 81 | + var devScatterPoints = new ScatterErrorSeries { Title = $"Question {(char)(question + 64)}", DataFieldX = "Array Length", DataFieldY = "Deviation from ideal", DataFieldErrorY = "Standard Deviation" }; |
| 82 | + var devLine = new LineSeries { Title = $"Question {(char)(question + 64)}", DataFieldX = "Array Length", DataFieldY = "Deviation from ideal" }; |
| 83 | + var timeSeries = new LineSeries { Title = $"Question {(char)(question + 64)}", DataFieldX = "Array Length", DataFieldY = "Runtime (ms)" }; |
| 84 | + |
| 85 | + //Sets the colours |
| 86 | + devLine.Color = customColors[question - 1]; |
| 87 | + devScatterPoints.MarkerFill = customColors[question - 1]; |
| 88 | + timeSeries.Color = customColors[question - 1]; |
| 89 | + |
| 90 | + for (int arrayLength = 0; arrayLength < 8; arrayLength++) |
| 91 | + { |
| 92 | + double devAverageValue = array[question - 1, arrayLength, 1]; |
| 93 | + double timeAverageValue = array[question - 1, arrayLength, 0]; |
| 94 | + |
| 95 | + devLine.Points.Add(new DataPoint(sizes[arrayLength], devAverageValue)); |
| 96 | + devScatterPoints.Points.Add(new ScatterErrorPoint(sizes[arrayLength], devAverageValue, 0, array[question - 1, arrayLength, 2])); |
| 97 | + timeSeries.Points.Add(new DataPoint(sizes[arrayLength], timeAverageValue)); |
| 98 | + } |
| 99 | + |
| 100 | + //Add the lines to the graph models |
| 101 | + devGraph.Series.Add(devLine); |
| 102 | + devGraph.Series.Add(devScatterPoints); |
| 103 | + timeGraph.Series.Add(timeSeries); |
| 104 | + } |
| 105 | + |
| 106 | + //assign the data to the graph views |
| 107 | + mainWindow.deviationGraph.Model = devGraph; |
| 108 | + mainWindow.runtimeGraph.Model = timeGraph; |
| 109 | + } |
| 110 | + |
| 111 | + private static void makeDataTable(MainWindow mainWindow) |
| 112 | + { |
| 113 | + string[] rows = dataString.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); |
| 114 | + string[] columns = rows[0].Split(new char[] { ',' }); |
| 115 | + |
| 116 | + DataTable rawInfo = new(); |
| 117 | + |
| 118 | + // Add the columns to the DataTable |
| 119 | + foreach (string column in columns) |
| 120 | + { |
| 121 | + rawInfo.Columns.Add(column); |
| 122 | + } |
| 123 | + |
| 124 | + // Add the rows to the DataTable |
| 125 | + for (int i = 1; i < rows.Length; i++) |
| 126 | + { |
| 127 | + string[] rowValues = rows[i].Split(new char[] { ',' }); |
| 128 | + DataRow dataRow = rawInfo.NewRow(); |
| 129 | + for (int j = 0; j < rowValues.Length; j++) |
| 130 | + { |
| 131 | + dataRow[j] = rowValues[j]; |
| 132 | + } |
| 133 | + rawInfo.Rows.Add(dataRow); |
| 134 | + } |
| 135 | + |
| 136 | + // Bind the DataTable to the DataGrid |
| 137 | + rawInfo.DefaultView.Sort = "Question ASC"; |
| 138 | + mainWindow.Data.ItemsSource = rawInfo.DefaultView; |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + public static string dataToCSV(double[,,] array) |
| 143 | + { |
| 144 | + StringBuilder csvBuilder = new(); |
| 145 | + string[] types = { "Runtime (ms)", "Average", "S.D" }; |
| 146 | + |
| 147 | + csvBuilder.Append("Question,Type,8,16,32,64,128,256,512,1024\n"); |
| 148 | + |
| 149 | + Parallel.For(1, 10, question => |
| 150 | + { |
| 151 | + for (int rowNum = 0; rowNum < 3; rowNum++) |
| 152 | + { |
| 153 | + var row = new StringBuilder(); |
| 154 | + row.Append((char)(question + 64) + "," + types[rowNum]); |
| 155 | + |
| 156 | + for (int arraySize = 0; arraySize < 8; arraySize++) |
| 157 | + { |
| 158 | + row.Append("," + array[question - 1, arraySize, rowNum]); |
| 159 | + } |
| 160 | + |
| 161 | + row.Append('\n'); |
| 162 | + lock (csvBuilder) // prevent multiple threads from writing to the CSV string at the same time |
| 163 | + { |
| 164 | + csvBuilder.Append(row); |
| 165 | + } |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + return csvBuilder.ToString(); |
| 170 | + |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + private void SaveButtonClick(object sender, RoutedEventArgs e) |
| 175 | + { |
| 176 | + // Create a SaveFileDialog object |
| 177 | + SaveFileDialog saveFileDialog = new(); |
| 178 | + saveFileDialog.Filter = "Comma Separated File (*.csv)|*.csv|Text File (*.txt)|*.txt|All files|*.*"; |
| 179 | + saveFileDialog.Title = "Save as a CSV file!"; |
| 180 | + |
| 181 | + // Show the dialog and get the result |
| 182 | + bool? result = saveFileDialog.ShowDialog(); |
| 183 | + |
| 184 | + if (result == true) |
| 185 | + { |
| 186 | + // Get the selected file name and path |
| 187 | + string filePath = saveFileDialog.FileName; |
| 188 | + |
| 189 | + // Writes the data to the file, meaning it only needs to be processed once. |
| 190 | + File.WriteAllText(filePath, dataString); |
| 191 | + } |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + private void Data_Sorting(object sender, System.Windows.Controls.DataGridSortingEventArgs e) |
| 196 | + { |
| 197 | + |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments