1

since I was struggling a bit transferring the F# example to use sliders in Plotly.Net (https://plotly.net/02_9_Sliders.html) to C# I just like to post my code. Maybe this helps some other people or someone has useful comments or improvements.

using Plotly.NET;
using Plotly.NET.LayoutObjects;
using Plotly.NET.TraceObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using static Plotly.NET.StyleParam;

namespace Test
{
    public class TestClass
    {
        public void SliderTest()
        {
            var steps = new Nparange(0, 5, .1);

            // Alternative to using Nparange
            //double[] steps = Enumerable.Range(0, 51).Select(i => 0.0 + i * .1).ToArray();

            var stepCharts =
                steps.GetNextValue().Select(        //Alternative: steps.Select(  //     
                    step =>
                    {
                        // Create a scatter plot for every step
                        var x = new Nparange(0, 10, .01).GetNextValue();
                        var y = x.Select(_x => Math.Sin(step * _x));

                        // Some plot must be visible here or the chart is empty at the beginning
                        Visible chartVisibility = step == 0 ? Visible.True : Visible.False;

                        var go = Chart2D.Chart.Scatter<double, double, string>(
                            x: x, y: y,
                            mode: Mode.Lines,
                            Name: "v = " + step.ToString(),
                            Marker: Marker.init(
                                Color: Color.fromHex("#00CED1"),
                                Size: 6))
                        .WithTraceInfo(Visible: chartVisibility);

                        return go;
                    });

            var scattersChart = GenericChart.combine(stepCharts);

            object sliderSteps =
                steps.GetNextValue().Select((x, i) => GetSliderStep(i, x, steps.Length));
            // Alternative:
            //object sliderSteps =
            //    steps.Select((x, i) => GetSliderStep(i, x, steps.Length));

            SliderCurrentValue sliderCurrentValue = new SliderCurrentValue();
            sliderCurrentValue.SetValue("prefix", "Frequency: ");

            Slider slider = new Slider();
            slider.SetValue("currentvalue", sliderCurrentValue);
            slider.SetValue("pad", Padding.init(T: 50));
            slider.SetValue("steps", sliderSteps);

            var chart = scattersChart.WithSlider(slider);
            chart.Show();
        }

        private SliderStep GetSliderStep(int i, double step, int length)
        {
            object vsbl = Enumerable.Range(0, length).Select(x => (x == i)).ToArray();
            object ttl = "slider switched to step: " + step.ToString();
            string label = "v = " + step.ToString();

            SliderStep sliderStep = new SliderStep();
            var args = new[] { new { visible = vsbl, title = ttl } }; // anything with Tuple doesn't work, cause it serializes to something like "Item1":"visible"
            sliderStep.SetValue("args", args);
            sliderStep.SetValue("method", Method.Update); //or "update");    
            sliderStep.SetValue("label", label);
            return sliderStep;
        }

        private class Nparange
        {
            public Nparange(double start, double stop, double step)
            {
                Start = start; Stop = stop; Step = step;
            }

            public double Start { get; }
            public double Stop { get; }
            public double Step { get; }

            public IEnumerable<double> GetNextValue()
            {
                for (int i = 0; i < Length; i++)
                {
                    yield return Start + i * Step;
                }
            }

            public int Length => (int)((Stop - Start) / Step);
        }
    }
}

I ran the code within a Nunit test environment. Perspectively, I'd like to debug mathematical functions written in .Net with Matlab-like possibilities for plotting.

0 Answers0