Example #1
0
 private void RenderFrame()
 {
     try
     {
         plotFrame.Render();
     }
     catch (Exception e)
     {
         DialogUtilities.ShowSpecificPlotError("Rendering Error", e, true, "The plot could not be drawn. Make sure your plots are valid.");
     }
 }
Example #2
0
        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;
                }
            }
        }