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 will look at the:
33
+ - From 3 Dimensional color collections, where the inner arrays contain 3 (color dimensions without alpha channel) or 4 (color dimensions and alpha channel) values. The color model can be set separately as shown below.
34
+ - From a 2 dimensional collection Plotly.NETs `ARGB` type that represents rgba values
35
+ - From a base64 encoded image data source
36
+
37
+ ## Creating Image charts from raw color arrays
38
+ *)
39
+
40
+ // 3d collection containing color values
41
+ open Plotly.NET
42
+ open Plotly.NET .LayoutObjects
43
+ open DynamicObj
44
+
45
+ let nparange ( start : double , stop : double , step : double ) =
46
+ let stepsCount = (( stop- start) / step) |> int
47
+ seq { for i in 0 .. stepsCount -> start + double( i) * step }
48
+ |> Array.ofSeq
49
+
50
+ let steps = nparange( 0. , 5. , 0.1 )
51
+ let scatters = steps
52
+ |> Seq.map
53
+ ( fun step ->
54
+ let x = nparange( 0. , 10. , 0.01 )
55
+ let y = seq { for curX in x -> sin( step * curX) }
56
+ let go = Chart2D.Chart.Scatter
57
+ (
58
+ x= x, y= y,
59
+ mode= StyleParam.Mode.Lines,
60
+ // The visible plot must be visible here or the chart is empty at the beginning
61
+ Visible= ( if step = 0. then StyleParam.Visible.True else StyleParam.Visible.False),
62
+ Name= " v = " + string( step),
63
+ Color= Color.fromHex( " #00CED1" ),
64
+ Width= 6.
65
+ )
66
+ go
67
+ )
68
+
69
+ let sliderSteps =
70
+ steps |>
71
+ Seq.indexed |>
72
+ Seq.map ( fun ( i , step ) ->
73
+ let visible = ( fun index -> index= i) |> Array.init( steps.Length)
74
+ let title = sprintf " Slider switched to step: %s " ( step |> string)
75
+ let arg1 = new DynamicObj()
76
+ let arg2 = new DynamicObj()
77
+ visible |> DynObj.setValue arg1 " visible"
78
+ title |> DynObj.setValue arg2 " title"
79
+ SliderStep.init( Args = [ arg1; arg2],
80
+ Method = StyleParam.Method.Update,
81
+ Label= " v = " + string( step))
82
+ )
83
+
84
+ let slider = Slider.init(
85
+ CurrentValue= SliderCurrentValue.init( Prefix= " Frequency: " ),
86
+ Pad= Pad.init( T= 50 ),
87
+ Steps= sliderSteps
88
+ )
89
+ let combinedChart = GenericChart.combine( scatters)
90
+ let chart = combinedChart |> 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