Skip to content

Commit 6af5f39

Browse files
author
Artem Makhno
committed
WIP
1 parent fbfe9d2 commit 6af5f39

File tree

13 files changed

+353
-204
lines changed

13 files changed

+353
-204
lines changed

Diff for: Plotly.NET.sln

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29709.97
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31825.309
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "project", "project", "{BF60BC93-E09B-4E5F-9D85-95A519479D54}"
77
ProjectSection(SolutionItems) = preProject
@@ -59,6 +59,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{7B09CC0A-F
5959
docs\02_6_table.fsx = docs\02_6_table.fsx
6060
docs\02_7_heatmaps.fsx = docs\02_7_heatmaps.fsx
6161
docs\02_8_Images.fsx = docs\02_8_Images.fsx
62+
docs\02_9_Sliders.fsx = docs\02_9_Sliders.fsx
6263
docs\03_0_3d-scatter-plots.fsx = docs\03_0_3d-scatter-plots.fsx
6364
docs\03_1_3d-surface-plots.fsx = docs\03_1_3d-surface-plots.fsx
6465
docs\03_2_3d-mesh-plots.fsx = docs\03_2_3d-mesh-plots.fsx

Diff for: docs/02_9_Sliders.fsx

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
[![Binder]({{root}}img/badge-binder.svg)](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb) 
27+
[![Script]({{root}}img/badge-script.svg)]({{root}}{{fsdocs-source-basename}}.fsx) 
28+
[![Notebook]({{root}}img/badge-notebook.svg)]({{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***)

Diff for: src/Plotly.NET/CSharpLayer/GenericChartExtensions.fs

+4
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,10 @@ module GenericChartExtensions =
716716
member this.WithLayoutImages(images:seq<LayoutImage>, [<Optional;DefaultParameterValue(true)>]?Append:bool) =
717717
this |> Chart.withLayoutImages(images, ?Append = Append)
718718

719+
[<CompiledName("WithSlider")>]
720+
[<Extension>]
721+
member this.WithSlider(slider:Slider) =
722+
this |> Chart.withSlider(slider)
719723

720724
[<CompiledName("WithSliders")>]
721725
[<Extension>]

Diff for: src/Plotly.NET/ChartAPI/Chart.fs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1716,9 +1716,16 @@ type Chart =
17161716
[<CompiledName("WithSliders")>]
17171717
static member withSliders
17181718
(
1719-
sliders : seq<Slider>
1719+
sliders:seq<Slider>
17201720
) =
17211721
fun (ch:GenericChart) ->
17221722
ch
17231723
|> GenericChart.mapLayout
1724-
(Layout.style (Sliders = sliders))
1724+
(Layout.style (Sliders = sliders))
1725+
1726+
[<CompiledName("WithSlider")>]
1727+
static member withSlider
1728+
(
1729+
slider:Slider
1730+
) =
1731+
Chart.withSliders([slider])

Diff for: src/Plotly.NET/CommonAbstractions/StyleParams.fs

+19-15
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,25 @@ module StyleParam =
14011401
//--------------------------
14021402
// #M#
14031403
//--------------------------
1404+
1405+
[<RequireQualifiedAccess>]
1406+
type Method =
1407+
| Restyle
1408+
| Relayout
1409+
| Animate
1410+
| Update
1411+
| Skip
1412+
1413+
static member toString = function
1414+
| Restyle -> "restyle"
1415+
| Relayout -> "relayout"
1416+
| Animate -> "animate"
1417+
| Update -> "update"
1418+
| Skip -> "skip"
1419+
1420+
static member convert = Method.toString >> box
1421+
override this.ToString() = this |> Method.toString
1422+
member this.Convert() = this |> Method.convert
14041423

14051424
[<RequireQualifiedAccess>]
14061425
type ModeBarButton =
@@ -2673,21 +2692,6 @@ module StyleParam =
26732692
| False -> "false"
26742693
| LegendOnly -> "legendonly"
26752694

2676-
(*static member applyToDynObj (dynObj : DynamicObj.DynamicObj, ?visible : Visible) =
2677-
match visible with
2678-
| None -> ()
2679-
| Some some ->
2680-
match some with
2681-
| Boolean value -> if value
2682-
then
2683-
true |> DynamicObj.DynObj.setValue dynObj "visible"
2684-
else
2685-
false |> DynamicObj.DynObj.setValue dynObj "visible"
2686-
| LegendOnly -> "legendonly" |> DynamicObj.DynObj.setValue dynObj "visible"*)
2687-
//function
2688-
// | Boolean value -> value
2689-
// | LegendOnly -> "legendonly"
2690-
26912695
static member convert = Visible.toObject >> box
26922696
override this.ToString() = this |> Visible.toString
26932697
member this.Convert() = this |> Visible.convert
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Plotly.NET.LayoutObjects
2+
3+
open DynamicObj
4+
5+
type Pad() =
6+
inherit DynamicObj ()
7+
8+
/// <summary>
9+
/// Set the padding of the slider component along each side
10+
/// </summary>
11+
/// <param name="B">The amount of padding (in px) along the bottom of the component</param>
12+
/// <param name="L">The amount of padding (in px) on the left side of the component</param>
13+
/// <param name="R">The amount of padding (in px) on the right side of the component</param>
14+
/// <param name="T">The amount of padding (in px) along the top of the component</param>
15+
static member init
16+
(
17+
?B : int,
18+
?L : int,
19+
?R : int,
20+
?T : int
21+
) = Pad() |> Pad.style
22+
(
23+
?B=B,
24+
?L=L,
25+
?R=R,
26+
?T=T
27+
)
28+
29+
static member style(?B : int, ?L : int, ?R : int, ?T : int) = (fun (pad : Pad) ->
30+
B |> DynObj.setValueOpt pad "b"
31+
L |> DynObj.setValueOpt pad "l"
32+
R |> DynObj.setValueOpt pad "r"
33+
T |> DynObj.setValueOpt pad "t"
34+
pad)

0 commit comments

Comments
 (0)