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
+ # Images
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 visible the step chosen in the slider.
33
+ *)
34
+
35
+ open Plotly.NET
36
+ open Plotly.NET .LayoutObjects
37
+
38
+ let nparange ( start : double , stop : double , step : double ) =
39
+ let stepsCount = (( stop- start) / step) |> int
40
+ seq { for i in 0 .. stepsCount -> start + double( i) * step }
41
+ |> Array.ofSeq
42
+
43
+ let steps = nparange( 0. , 5. , 0.1 )
44
+ let scatters = steps
45
+ |> Seq.map
46
+ ( fun step ->
47
+ let x = nparange( 0. , 10. , 0.01 )
48
+ let y = seq { for curX in x -> sin( step * curX) }
49
+ // Some plot must be visible here or the chart is empty at the beginning
50
+ let chartVisibility = if step = 0. then StyleParam.Visible.True else StyleParam.Visible.False;
51
+ let go =
52
+ Chart2D.Chart.Scatter
53
+ (
54
+ x= x, y= y,
55
+ mode= StyleParam.Mode.Lines,
56
+ Name= " v = " + string( step),
57
+ Color= Color.fromHex( " #00CED1" ),
58
+ Width= 6.
59
+ )
60
+ |> Chart.withTraceName( Visible= chartVisibility)
61
+ go
62
+ )
63
+
64
+ let sliderSteps =
65
+ steps |>
66
+ Seq.indexed |>
67
+ Seq.map ( fun ( i , step ) ->
68
+ let visible =
69
+ ( fun index -> index= i)
70
+ |> Array.init( steps.Length)
71
+ |> box
72
+ let title =
73
+ sprintf " Slider switched to step: %s "
74
+ ( step |> string)
75
+ |> box
76
+ let args = [ " visible" , visible; " title" , title]
77
+ SliderStep.init( Args = args,
78
+ Method = StyleParam.Method.Update,
79
+ Label= " v = " + string( step))
80
+ )
81
+
82
+ let slider = Slider.init(
83
+ CurrentValue= SliderCurrentValue.init( Prefix= " Frequency: " ),
84
+ Padding= Padding.init( T= 50 ),
85
+ Steps= sliderSteps
86
+ )
87
+ let combinedChart = GenericChart.combine( scatters)
88
+ let chart =
89
+ combinedChart
90
+ |> Chart.withSlider slider
91
+
92
+ (*** condition: ipynb ***)
93
+ #if IPYNB
94
+ chart
95
+ #endif // IPYNB
96
+
97
+ (***hide***)
98
+ chart |> GenericChart.toChartHTML
99
+ (***include-it-raw***)
0 commit comments