Skip to content

Commit 007d340

Browse files
author
Artem Makhno
committed
Add slider tests
1 parent 6af5f39 commit 007d340

File tree

7 files changed

+164
-33
lines changed

7 files changed

+164
-33
lines changed

Diff for: src/Plotly.NET/Layout/ObjectAbstractions/Common/Pad.fs renamed to src/Plotly.NET/Layout/ObjectAbstractions/Common/Padding.fs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
open DynamicObj
44

5-
type Pad() =
5+
type Padding() =
66
inherit DynamicObj ()
77

88
/// <summary>
@@ -18,17 +18,17 @@ type Pad() =
1818
?L : int,
1919
?R : int,
2020
?T : int
21-
) = Pad() |> Pad.style
21+
) = Padding() |> Padding.style
2222
(
2323
?B=B,
2424
?L=L,
2525
?R=R,
2626
?T=T
2727
)
2828

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)
29+
static member style(?B : int, ?L : int, ?R : int, ?T : int) = (fun (padding : Padding) ->
30+
B |> DynObj.setValueOpt padding "b"
31+
L |> DynObj.setValueOpt padding "l"
32+
R |> DynObj.setValueOpt padding "r"
33+
T |> DynObj.setValueOpt padding "t"
34+
padding)

Diff for: src/Plotly.NET/Layout/ObjectAbstractions/Common/Slider/Slider.fs

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
open Plotly.NET
44
open DynamicObj
55

6-
type Slider() =
6+
/// <summary>
7+
/// The layout object for custom slider implementation
8+
/// </summary>
9+
type Slider() =
710
inherit DynamicObj ()
811

912
/// <summary>
10-
/// Applies the style parameters of the mesh3d chart to the given trace
13+
/// Initializes the slider with style parameters
1114
/// </summary>
1215
/// <param name="Active">Determines which button (by index starting from 0) is considered active</param>
1316
/// <param name="ActiveBgColor">Sets the background color of the slider grip while dragging</param>
@@ -71,7 +74,7 @@ type Slider() =
7174
?LenMode : StyleParam.UnitMode,
7275
?MinorTickLen : int,
7376
?Name : string,
74-
?Pad : Pad,
77+
?Padding : Padding,
7578
?Steps : seq<SliderStep>,
7679
?TemplateItemName : string,
7780
?TickColor : Color,
@@ -97,7 +100,7 @@ type Slider() =
97100
?LenMode=LenMode,
98101
?MinorTickLen=MinorTickLen,
99102
?Name=Name,
100-
?Pad=Pad,
103+
?Padding=Padding,
101104
?Steps=Steps,
102105
?TemplateItemName=TemplateItemName,
103106
?TickColor=TickColor,
@@ -124,7 +127,7 @@ type Slider() =
124127
?LenMode : StyleParam.UnitMode,
125128
?MinorTickLen : int,
126129
?Name : string,
127-
?Pad : Pad,
130+
?Padding : Padding,
128131
?Steps : seq<SliderStep>,
129132
?TemplateItemName : string,
130133
?TickColor : Color,
@@ -149,7 +152,7 @@ type Slider() =
149152
LenMode |> DynObj.setValueOptBy slider "lenmode" StyleParam.UnitMode.convert
150153
MinorTickLen |> DynObj.setValueOpt slider "minorticklen"
151154
Name |> DynObj.setValueOpt slider "name"
152-
Pad |> DynObj.setValueOpt slider "pad"
155+
Padding |> DynObj.setValueOpt slider "pad"
153156
Steps |> DynObj.setValueOpt slider "steps"
154157
TemplateItemName |> DynObj.setValueOpt slider "templateitemname"
155158
TickColor |> DynObj.setValueOpt slider "tickcolor"

Diff for: src/Plotly.NET/Layout/ObjectAbstractions/Common/Slider/SliderStep.fs

+52-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,46 @@
33
open Plotly.NET
44
open DynamicObj
55

6+
/// <summary>
7+
/// The object including the slider steps style and update parameters
8+
/// </summary>
69
type SliderStep() =
710
inherit DynamicObj ()
811

12+
/// <summary>
13+
/// Initializes the slider with style parameters
14+
/// </summary>
15+
/// <param name="Args">Sets the arguments values to be passed to the Plotly method set in `method` on slide</param>
16+
/// <param name="Execute">
17+
/// When true, the API method is executed. When false, all other behaviors are the same and command execution is skipped.
18+
/// This may be useful when hooking into, for example, the `plotly_sliderchange` method and executing the API command manually
19+
/// without losing the benefit of the slider automatically binding to the state of the plot through the specification of `method` and `args`.
20+
/// </param>
21+
/// <param name="Label">Sets the text label to appear on the slider</param>
22+
/// <param name="Method">
23+
/// Sets the Plotly method to be called when the slider value is changed.
24+
/// If the `skip` method is used, the API slider will function as normal but
25+
/// will perform no API calls and will not bind automatically to state updates.
26+
/// This may be used to create a component interface and attach to slider events manually via JavaScript
27+
/// </param>
28+
/// <param name="Name">
29+
/// When used in a template, named items are created in the output figure in addition to any items the figure already has in this array.
30+
/// You can modify these items in the output figure by making your own item with `templateitemname` matching this
31+
/// `name` alongside your modifications (including `visible: false` or `enabled: false` to hide it).
32+
/// Has no effect outside of a template
33+
/// </param>
34+
/// <param name="TemplateItemName">
35+
/// Used to refer to a named item in this array in the template.
36+
/// Named items from the template will be created even without a matching item in the input figure,
37+
/// but you can modify one by making an item with `templateitemname` matching its `name`, alongside your modifications
38+
/// (including `visible: false` or `enabled: false` to hide it). If there is no template or no matching item,
39+
/// this item will be hidden unless you explicitly show it with `visible: true`
40+
/// </param>
41+
/// <param name="Value">Sets the value of the slider step, used to refer to the step programatically. Defaults to the slider label if not provided</param>
42+
/// <param name="Visible">Determines whether or not this step is included in the slider</param>
943
static member init
1044
(
11-
?Args : seq<DynamicObj>,
45+
?Args : seq<(string * obj)>,
1246
?Execute : bool,
1347
?Label : string,
1448
?Method : StyleParam.Method,
@@ -31,7 +65,7 @@ type SliderStep() =
3165

3266
static member style
3367
(
34-
?Args : seq<DynamicObj>,
68+
?Args : seq<(string * obj)>,
3569
?Execute : bool,
3670
?Label : string,
3771
?Method : StyleParam.Method,
@@ -41,13 +75,21 @@ type SliderStep() =
4175
?Visible : bool
4276
) =
4377
(fun (step : SliderStep) ->
44-
Args |> DynObj.setValueOpt step "args"
45-
Execute |> DynObj.setValueOpt step "execute"
46-
Label |> DynObj.setValueOpt step "label"
47-
Method |> DynObj.setValueOptBy step "method" StyleParam.Method.convert
48-
Name |> DynObj.setValueOpt step "name"
49-
TemplateItemName |> DynObj.setValueOpt step "templateitemname"
50-
Value |> DynObj.setValueOpt step "value"
51-
Visible |> DynObj.setValueOpt step "visible"
78+
// A bit strange, that we create a dictionary for an every
79+
// object, but this is that way the Plotly works
80+
let argsAsDictionaries =
81+
Args
82+
|> Option.map (fun args ->
83+
args
84+
|> Seq.map (fun arg -> [arg] |> dict)
85+
)
86+
argsAsDictionaries |> DynObj.setValueOpt step "args"
87+
Execute |> DynObj.setValueOpt step "execute"
88+
Label |> DynObj.setValueOpt step "label"
89+
Method |> DynObj.setValueOptBy step "method" StyleParam.Method.convert
90+
Name |> DynObj.setValueOpt step "name"
91+
TemplateItemName |> DynObj.setValueOpt step "templateitemname"
92+
Value |> DynObj.setValueOpt step "value"
93+
Visible |> DynObj.setValueOpt step "visible"
5294
step
5395
)

Diff for: src/Plotly.NET/Plotly.NET.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<Compile Include="CommonAbstractions\Font.fs" />
4343
<Compile Include="CommonAbstractions\Title.fs" />
4444
<Compile Include="CommonAbstractions\Line.fs" />
45-
<Compile Include="Layout\ObjectAbstractions\Common\Pad.fs" />
45+
<Compile Include="Layout\ObjectAbstractions\Common\Padding.fs" />
4646
<Compile Include="Layout\ObjectAbstractions\Common\LayoutImage.fs" />
4747
<Compile Include="Layout\ObjectAbstractions\Common\Button.fs" />
4848
<Compile Include="Layout\ObjectAbstractions\Common\RangeSelector.fs" />

Diff for: tests/Plotly.NET.Tests.FSharpConsole/Program.fs

+11-8
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,23 @@ let main argv =
4040
steps |>
4141
Seq.indexed |>
4242
Seq.map (fun (i, step) ->
43-
let visible = (fun index -> index=i) |> Array.init(steps.Length)
44-
let title = sprintf "Slider switched to step: %s" (step |> string)
45-
let arg1 = new DynamicObj()
46-
let arg2 = new DynamicObj()
47-
visible |> DynObj.setValue arg1 "visible"
48-
title |> DynObj.setValue arg2 "title"
49-
SliderStep.init(Args = [arg1; arg2],
43+
let visible =
44+
(fun index -> index=i)
45+
|> Array.init(steps.Length)
46+
|> box
47+
let title =
48+
sprintf "Slider switched to step: %s"
49+
(step |> string)
50+
|> box
51+
let args = ["visible", visible; "title", title]
52+
SliderStep.init(Args = args,
5053
Method = StyleParam.Method.Update,
5154
Label="v = " + string(step))
5255
)
5356

5457
let slider = Slider.init(
5558
CurrentValue=SliderCurrentValue.init(Prefix="Frequency: "),
56-
Pad=Pad.init(T=50),
59+
Padding=Padding.init(T=50),
5760
Steps=sliderSteps
5861
)
5962
let combinedChart = GenericChart.combine(scatters)

Diff for: tests/Plotly.NET.Tests/LayoutObjects/Slider.fs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
module Tests.LayoutObjects.Slider
2+
3+
open Expecto
4+
open Plotly.NET
5+
open Plotly.NET.LayoutObjects
6+
7+
open TestUtils.LayoutObjects
8+
9+
let slider =
10+
Slider.init(
11+
Active=1,
12+
ActiveBgColor=Color.fromRGB 1 2 3,
13+
BgColor=Color.fromRGB 2 3 4,
14+
BorderColor=Color.fromRGB 3 4 5,
15+
BorderWidth=2,
16+
CurrentValue=SliderCurrentValue.init(
17+
Font=Font.init(StyleParam.FontFamily.Arial, Size=1, Color=Color.fromRGB 4 5 6),
18+
Offset=1,
19+
Prefix="prefix",
20+
Suffix="suffix",
21+
Visible=false,
22+
XAnchor=StyleParam.XAnchorPosition.Center
23+
),
24+
Font=Font.init(StyleParam.FontFamily.Balto, Size=2, Color=Color.fromRGB 5 6 7),
25+
Len=0.4,
26+
LenMode=StyleParam.UnitMode.Fraction,
27+
MinorTickLen=6,
28+
Name="SliderName",
29+
Padding=Padding.init(1, 2, 3, 4),
30+
Steps=[SliderStep.init(
31+
Args=["visible", box(false); "title", box("stepTitle")],
32+
Execute=true,
33+
Label="stepLabel",
34+
Method=StyleParam.Method.Update,
35+
Name="stepName",
36+
TemplateItemName="stepTemplateItemName",
37+
Value="stepValue",
38+
Visible=true
39+
)],
40+
TemplateItemName="TemplateItemName",
41+
TickColor=Color.fromRGB 6 7 8,
42+
TickLen=7,
43+
TickWidth=8,
44+
Transition=Transition.init(
45+
Duration=1,
46+
Easing=StyleParam.TransitionEasing.Back,
47+
Ordering=StyleParam.TransitionOrdering.LayoutFirst
48+
),
49+
Visible=true,
50+
X=9,
51+
XAnchor=StyleParam.XAnchorPosition.Left,
52+
Y=10,
53+
YAnchor=StyleParam.YAnchorPosition.Middle
54+
)
55+
56+
[<Tests>]
57+
let ``Slider tests`` =
58+
testList "LayoutObjects.Slider JSON field tests" [
59+
slider |> createJsonFieldTest "active" "1"
60+
slider |> createJsonFieldTest "activebgcolor" "\"rgba(1, 2, 3, 1.0)\""
61+
slider |> createJsonFieldTest "bgcolor" "\"rgba(2, 3, 4, 1.0)\""
62+
slider |> createJsonFieldTest "bordercolor" "\"rgba(3, 4, 5, 1.0)\""
63+
slider |> createJsonFieldTest "borderwidth" "2"
64+
slider |> createJsonFieldTest "currentvalue" "{\"font\":{\"family\":\"Arial\",\"size\":1.0,\"color\":\"rgba(4, 5, 6, 1.0)\"},\"offset\":1,\"prefix\":\"prefix\",\"suffix\":\"suffix\",\"visible\":false,\"xanchor\":\"center\"}"
65+
slider |> createJsonFieldTest "font" "{\"family\":\"Balto\",\"size\":2.0,\"color\":\"rgba(5, 6, 7, 1.0)\"}"
66+
slider |> createJsonFieldTest "len" "0.4"
67+
slider |> createJsonFieldTest "lenmode" "\"fraction\""
68+
slider |> createJsonFieldTest "minorticklen" "6"
69+
slider |> createJsonFieldTest "name" "\"SliderName\""
70+
slider |> createJsonFieldTest "pad" "{\"b\":1,\"l\":2,\"r\":3,\"t\":4}"
71+
slider |> createJsonFieldTest "templateitemname" "\"TemplateItemName\""
72+
slider |> createJsonFieldTest "tickcolor" "\"rgba(6, 7, 8, 1.0)\""
73+
slider |> createJsonFieldTest "ticklen" "7"
74+
slider |> createJsonFieldTest "tickwidth" "8"
75+
slider |> createJsonFieldTest "transition" "{\"duration\":1,\"easing\":\"back\",\"ordering\":\"layout first\"}"
76+
slider |> createJsonFieldTest "visible" "true"
77+
slider |> createJsonFieldTest "x" "9"
78+
slider |> createJsonFieldTest "xanchor" "\"left\""
79+
slider |> createJsonFieldTest "y" "10"
80+
slider |> createJsonFieldTest "yanchor" "\"middle\""
81+
slider |> createJsonFieldTest "steps" "[{\"args\":[{\"visible\":false},{\"title\":\"stepTitle\"}],\"execute\":true,\"label\":\"stepLabel\",\"method\":\"update\",\"name\":\"stepName\",\"templateitemname\":\"stepTemplateItemName\",\"value\":\"stepValue\",\"visible\":true}]"
82+
]

Diff for: tests/Plotly.NET.Tests/Plotly.NET.Tests.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<ItemGroup>
1010
<Compile Include="TestUtils.fs" />
1111
<Compile Include="CommonAbstractions\Colors.fs" />
12+
<Compile Include="LayoutObjects\Slider.fs" />
1213
<Compile Include="LayoutObjects\LinearAxis.fs" />
1314
<Compile Include="Traces\TraceID.fs" />
1415
<Compile Include="HtmlCodegen\SimpleTests.fs" />

0 commit comments

Comments
 (0)