1
+ (**
2
+ ---
3
+ title: Sliders
4
+ category: Simple Charts
5
+ categoryindex: 3
6
+ index: 10
7
+ ---
8
+ *)
9
+
10
+ (*** hide ***)
11
+
12
+ (*** condition: prepare ***)
13
+ #r " nuget: Newtonsoft.JSON, 12.0.3"
14
+ #r " nuget: DynamicObj"
15
+ #r " ../bin/Plotly.NET/netstandard2.0/Plotly.NET.dll"
16
+
17
+ (*** condition: ipynb ***)
18
+ #if IPYNB
19
+ #r " nuget: Plotly.NET, {{fsdocs-package-version}}"
20
+ #r " nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}"
21
+ #endif // IPYNB
22
+
23
+ (**
24
+ # Sliders
25
+
26
+ [](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb) 
27
+ []({{root}}{{fsdocs-source-basename}}.fsx) 
28
+ []({{root}}{{fsdocs-source-basename}}.ipynb)
29
+
30
+ *Summary:* This example shows how to create charts with sliders in F#.
31
+
32
+ The sliders give the option of passing the arguments to the Plotly chart. In the example we use the visibility parameter to make the step chosen in the slider visible.
33
+
34
+ The original exapmle is made with python and can be found [here](https://plotly.com/python/sliders)
35
+ *)
36
+
37
+ open Plotly.NET
38
+ open Plotly.NET .LayoutObjects
39
+
40
+ /// Similar to numpy.arrange
41
+ let nparange ( start : double ) ( stop : double ) ( step : double ) =
42
+ let stepsCount = (( stop- start) / step) |> int
43
+ seq { for i in 0 .. stepsCount -> start + double( i) * step }
44
+ |> Array.ofSeq
45
+
46
+ let steps = nparange 0. 5. 0.1
47
+ let scattersChart =
48
+ steps
49
+ |> Seq.map
50
+ ( fun step ->
51
+ // Create a scatter plot for every step
52
+ let x = nparange 0. 10. 0.01
53
+ let y = seq { for x_ in x -> sin( step * x_) }
54
+ // Some plot must be visible here or the chart is empty at the beginning
55
+ let chartVisibility = if step = 0. then StyleParam.Visible.True else StyleParam.Visible.False;
56
+ let go =
57
+ Chart2D.Chart.Scatter
58
+ (
59
+ x= x, y= y,
60
+ mode= StyleParam.Mode.Lines,
61
+ Name= " v = " + string( step),
62
+ Color= Color.fromHex( " #00CED1" ),
63
+ Width= 6.
64
+ )
65
+ |> Chart.withTraceName( Visible= chartVisibility)
66
+ go
67
+ )
68
+ |> GenericChart.combine
69
+
70
+ let sliderSteps =
71
+ steps |>
72
+ Seq.indexed |>
73
+ Seq.map
74
+ ( fun ( i , step ) ->
75
+ // Create a visibility and a title parameters
76
+ // The visibility parameter includes an array where every parameter
77
+ // is mapped onto the trace visibility
78
+ let visible =
79
+ // Set true only for the current step
80
+ ( fun index -> index= i)
81
+ |> Array.init steps.Length
82
+ |> box
83
+ let title =
84
+ sprintf " Slider switched to step: %f " step
85
+ |> box
86
+ SliderStep.init(
87
+ Args = [ " visible" , visible; " title" , title],
88
+ Method = StyleParam.Method.Update,
89
+ Label= " v = " + string( step)
90
+ )
91
+ )
92
+
93
+ let slider =
94
+ Slider.init(
95
+ CurrentValue= SliderCurrentValue.init( Prefix= " Frequency: " ),
96
+ Padding= Padding.init( T= 50 ),
97
+ Steps= sliderSteps
98
+ )
99
+
100
+ let chart =
101
+ scattersChart
102
+ |> Chart.withSlider slider
103
+
104
+ (*** condition: ipynb ***)
105
+ #if IPYNB
106
+ chart
107
+ #endif // IPYNB
108
+
109
+ (***hide***)
110
+ chart |> GenericChart.toChartHTML
111
+ (***include-it-raw***)
0 commit comments