private async void FunctionPlot_Click(object sender, RoutedEventArgs e) { var dlg = new FunctionPlotDialog(); dlg.Owner = App.Current.MainWindow; if (dlg.ShowDialog() == true) { string expression = dlg.expressionTextBox.Text; Func <double, double?> f_unsafe; try { f_unsafe = await CSharpScript.EvaluateAsync <Func <double, double?> >("x=>" + expression, ScriptOptions.Default.WithImports("System.Math")); } catch (CompilationErrorException error) { DialogUtilities.ShowSpecificPlotError("Compilation Error", error, false, "Make sure your expression is valid C#. Note that ternary operators that may return null are not supported by C# in this context. Returning double.NaN will work fine."); return; } double?f_temp(double x) { try { double?y = f_unsafe(x); if (y.HasValue && !double.IsFinite(y.Value)) {// Make sure it ain't infinity, negative infinity, NaN, etc return(null); } return(f_unsafe(x)); } catch (Exception e) { return(null); } } Func <double, double?> f = x => f_temp(x); PlotParameters plotParams = new PlotParameters() { data = f, drawSettings = new DrawSettings() { type = PlotType.function, label = "y = " + expression, } }; try { ((App)App.Current).AddSeries(plotParams); } catch { DialogUtilities.ShowGenericPlotNotAddedError(); return; } } }
private void LoadCSVSeries_Click(object sender, RoutedEventArgs e) { string plotType = ((MenuItem)e.OriginalSource).Header.ToString().ToUpperInvariant(); PlotType type = new PlotType(); switch (plotType) { case "SCATTER PLOT": type = PlotType.scatter; break; case "SIGNAL": type = PlotType.signal; break; case "BAR PLOT": type = PlotType.bar; break; case "HISTOGRAM": type = PlotType.histogram; break; case "BOX AND WHISKER": type = PlotType.box_whisker; break; case "GROUPED BAR PLOT": type = PlotType.bar_grouped; break; } SettingsDialog settingsDialog = new SettingsDialog(type); settingsDialog.Owner = App.Current.MainWindow; DrawSettings drawSettings; drawSettings = FetchSettingsFromDialog(settingsDialog, type); if (!drawSettings.valid) { return; } Dictionary <string, object> metadata = new Dictionary <string, object>(); if (type == PlotType.signal) { SignalFrequencyDialog dlg = new SignalFrequencyDialog(); dlg.Owner = App.Current.MainWindow; if (dlg.ShowDialog() != true) { //Nullable return; } double sampleRate; if (!double.TryParse(dlg.frequency, out sampleRate)) { DialogUtilities.ShowCouldNotParseNumberError("Sample Rate", dlg.frequency); return; } double xOffset; if (double.TryParse(dlg.xOffsetTextBox.Text, out xOffset)) { DialogUtilities.ShowCouldNotParseNumberError("X-Offset", dlg.xOffsetTextBox.Text); return; } metadata.Add("sampleRate", sampleRate); metadata.Add("xOffset", xOffset); } else if (type == PlotType.bar_grouped) { GroupedPlotDialog dlg = new GroupedPlotDialog(); dlg.Owner = App.Current.MainWindow; if (dlg.ShowDialog() != true) { //Nullable return; } if (dlg.groupNamesTextBox.Text != "") { metadata.Add("group_names", dlg.groupNamesTextBox.Text.Split(',')); } if (dlg.seriesNamesTextBox.Text != "") { metadata.Add("series_names", dlg.seriesNamesTextBox.Text.Split(',')); } } FilePickerDialog filePickerDialog = new FilePickerDialog(drawSettings.type == PlotType.bar_grouped); filePickerDialog.Owner = App.Current.MainWindow; try { if (filePickerDialog.ShowDialog() == true) { PlotParameters plotParams = new PlotParameters(); plotParams = (App.Current as App).AddSeriesFromCSVFile(filePickerDialog.filepath, drawSettings, metadata); if (settingsDialog.errorDataCSV != null) { ((App)App.Current).AddErrorFromCSVFile(plotParams, settingsDialog.errorDataCSV); } statusMessage.Text = $"{filePickerDialog.filepath} loaded"; } } catch { DialogUtilities.ShowGenericPlotNotAddedError(); return; } }
private void LineSpan_Click(object sender, RoutedEventArgs e) { string plotType = ((MenuItem)e.OriginalSource).Header.ToString().ToUpperInvariant(); PlotType type = new PlotType(); switch (plotType) { case "VERTICAL LINE": type = PlotType.vertical_line; break; case "HORIZONTAL LINE": type = PlotType.horizontal_line; break; case "VERTICAL SPAN": type = PlotType.vertical_span; break; case "HORIZONTAL SPAN": type = PlotType.horizontal_span; break; } SettingsDialog settingsDialog = new SettingsDialog(type); settingsDialog.Owner = App.Current.MainWindow; DrawSettings drawSettings; drawSettings = FetchSettingsFromDialog(settingsDialog, type); if (!drawSettings.valid) { return; } PlotParameters plotParams = new PlotParameters(); plotParams.drawSettings = drawSettings; if (type == PlotType.horizontal_line || type == PlotType.vertical_line) { LineSettingsDialog lineDialog = new LineSettingsDialog(); lineDialog.Owner = App.Current.MainWindow; if (lineDialog.ShowDialog() != true) { return; } double value = 0; DateTime date = new DateTime(); if (DateTime.TryParse(lineDialog.value.Text, out date)) { plotParams.data = date.ToOADate(); } else if (double.TryParse(lineDialog.value.Text, out value)) { plotParams.data = value; } else { DialogUtilities.ShowCouldNotParseValueError("Value", lineDialog.value.Text); return; } } else { SpanSettingsDialog spanDialog = new SpanSettingsDialog(); spanDialog.Owner = App.Current.MainWindow; if (spanDialog.ShowDialog() != true) { return; } double minValue = 0; double maxValue = 0; DateTime dateMin = new DateTime(); DateTime dateMax = new DateTime(); if (DateTime.TryParse(spanDialog.minValue.Text, out dateMin)) { minValue = dateMin.ToOADate(); } else if (!double.TryParse(spanDialog.minValue.Text, out minValue)) { DialogUtilities.ShowCouldNotParseValueError("Min Value", spanDialog.minValue.Text); return; } if (DateTime.TryParse(spanDialog.maxValue.Text, out dateMax)) { maxValue = dateMax.ToOADate(); } else if (!double.TryParse(spanDialog.maxValue.Text, out maxValue)) { DialogUtilities.ShowCouldNotParseValueError("Max Value", spanDialog.maxValue.Text); return; } plotParams.data = (minValue, maxValue); } try { ((App)App.Current).AddSeries(plotParams); } catch { DialogUtilities.ShowGenericPlotNotAddedError(); return; } }
private void PrefabSeries_Click(object sender, RoutedEventArgs e) { string plotType = ((MenuItem)e.OriginalSource).Header.ToString().ToUpperInvariant(); PlotType type = new PlotType(); switch (plotType) { case "SIGNAL": type = PlotType.signal; break; } SettingsDialog settingsDialog = new SettingsDialog(type); settingsDialog.Owner = App.Current.MainWindow; DrawSettings drawSettings; drawSettings = FetchSettingsFromDialog(settingsDialog, type); if (!drawSettings.valid) { return; } Dictionary <string, object> metadata = new Dictionary <string, object>(); if (type == PlotType.signal) { PrefabSignalDialog prefabSignalDialog = new PrefabSignalDialog(); prefabSignalDialog.Owner = App.Current.MainWindow; if (prefabSignalDialog.ShowDialog() != true) //Nullable { return; } double cycleCount = 10; double frequency = 1000; WaveType waveType = WaveType.sine; if (!double.TryParse(prefabSignalDialog.cycleCountTextBox.Text, out cycleCount)) { DialogUtilities.ShowCouldNotParseNumberError("Cycle Count", prefabSignalDialog.cycleCountTextBox.Text); return; } if (!double.TryParse(prefabSignalDialog.frequencyTextBox.Text, out frequency)) { DialogUtilities.ShowCouldNotParseNumberError("Frequency", prefabSignalDialog.frequencyTextBox.Text); return; } switch (prefabSignalDialog.waveTypeComboBox.Text.ToUpperInvariant()) { case "SINE WAVE": waveType = WaveType.sine; break; case "SQUARE WAVE": waveType = WaveType.square; break; } SignalFrequencyDialog dlg = new SignalFrequencyDialog(true); dlg.Owner = App.Current.MainWindow; if (dlg.ShowDialog() != true) { //Nullable return; } double sampleRate; if (!double.TryParse(dlg.frequency, out sampleRate)) { DialogUtilities.ShowCouldNotParseNumberError("Sample Rate", dlg.frequency); return; } double xOffset; if (!double.TryParse(dlg.xOffsetTextBox.Text, out xOffset)) { DialogUtilities.ShowCouldNotParseNumberError("X-Offset", dlg.xOffsetTextBox.Text); return; } metadata.Add("xOffset", xOffset); StringBuilder dataStr = new StringBuilder(); int upperBound = (int)Math.Ceiling(cycleCount * frequency); double[] data = new double[upperBound]; metadata.Add("sampleRate", frequency * frequency); if (waveType == WaveType.sine) { data = ScottPlot.DataGen.Sin(upperBound, cycleCount); } else if (waveType == WaveType.square) { bool high = false; for (int i = 0; i < upperBound; i++) { if (i % (frequency / 2) == 0) { high = !high; } data[i] = high ? 1 : 0; } } for (int i = 0; i < upperBound; i++) { dataStr.Append(data[i]); if (i != upperBound - 1) { dataStr.Append(','); } } try { ((App)App.Current).AddSeriesFromString(dataStr.ToString(), drawSettings, metadata); } catch { DialogUtilities.ShowGenericPlotNotAddedError(); return; } } }