Skip to content

Commit 81519e1

Browse files
committed
Add temporary hard fix for #45 (hard code combin for some members that are collections)
1 parent 21ab915 commit 81519e1

File tree

3 files changed

+149
-3
lines changed

3 files changed

+149
-3
lines changed

src/Plotly.NET/ChartAPI/GenericChart.fs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,85 @@ module GenericChart =
212212
// let l' = getLayouts gChart
213213
// MultiChart (traces, Some (layouts@l'))
214214

215+
open Plotly.NET.LayoutObjects
215216
// Combines two GenericChart
217+
216218
let combine(gCharts:seq<GenericChart>) =
219+
// temporary hard fix for some props, see https://github.com/CSBiology/DynamicObj/issues/11
220+
let combineOptSeqs (first:seq<'A> option) (second:seq<'A> option) =
221+
match first, second with
222+
| Some f, Some s -> Some (Seq.append f s)
223+
| Some f, None -> Some f
224+
| None, Some s -> Some s
225+
| _ -> None
217226
let combineLayouts (first:Layout) (second:Layout) =
218-
DynObj.combine first second |> unbox
227+
228+
let annotations =
229+
combineOptSeqs
230+
(first.TryGetTypedValue<seq<Annotation>>("annotations"))
231+
(second.TryGetTypedValue<seq<Annotation>>("annotations"))
232+
233+
let shapes =
234+
combineOptSeqs
235+
(first.TryGetTypedValue<seq<Shape>>("shapes"))
236+
(second.TryGetTypedValue<seq<Shape>>("shapes"))
237+
238+
let images =
239+
combineOptSeqs
240+
(first.TryGetTypedValue<seq<LayoutImage>>("images"))
241+
(second.TryGetTypedValue<seq<LayoutImage>>("images"))
242+
243+
let sliders =
244+
combineOptSeqs
245+
(first.TryGetTypedValue<seq<Slider>>("sliders"))
246+
(second.TryGetTypedValue<seq<Slider>>("sliders"))
247+
248+
let hiddenLabels =
249+
combineOptSeqs
250+
(first.TryGetTypedValue<seq<string>>("hiddenlabels"))
251+
(second.TryGetTypedValue<seq<string>>("hiddenlabels"))
252+
253+
DynObj.combine first second
254+
|> unbox
255+
|> Layout.style(
256+
?Annotations = annotations,
257+
?Shapes = shapes,
258+
?Images = images,
259+
?Sliders = sliders,
260+
?HiddenLabels = hiddenLabels
261+
)
219262

220263
let combineConfigs (first:Config) (second:Config) =
221-
DynObj.combine first second |> unbox
264+
265+
let editableAnnotations =
266+
combineOptSeqs
267+
(first.TryGetTypedValue<seq<StyleParam.AnnotationEditOptions>>("editable"))
268+
(second.TryGetTypedValue<seq<StyleParam.AnnotationEditOptions>>("editable"))
269+
270+
let modeBarButtonsToAdd =
271+
combineOptSeqs
272+
(first.TryGetTypedValue<seq<StyleParam.ModeBarButton>>("modeBarButtonsToAdd"))
273+
(second.TryGetTypedValue<seq<StyleParam.ModeBarButton>>("modeBarButtonsToAdd"))
274+
275+
DynObj.combine first second
276+
|> unbox
277+
|> Config.style(
278+
?EditableAnnotations = editableAnnotations,
279+
?ModeBarButtonsToAdd = modeBarButtonsToAdd
280+
)
222281

223282
let combineDisplayOptions (first:DisplayOptions) (second:DisplayOptions) =
224-
DynObj.combine first second |> unbox
283+
284+
let additionalHeadTags =
285+
combineOptSeqs
286+
(first.TryGetTypedValue<seq<string>>("AdditionalHeadTags"))
287+
(second.TryGetTypedValue<seq<string>>("AdditionalHeadTags"))
288+
289+
DynObj.combine first second
290+
|> unbox
291+
|> DisplayOptions.style(
292+
?AdditionalHeadTags = additionalHeadTags
293+
)
225294

226295
gCharts
227296
|> Seq.reduce (fun acc elem ->
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
module Tests.ChartAPIs.Combine
2+
3+
open Expecto
4+
open Plotly.NET
5+
open Plotly.NET.LayoutObjects
6+
open Plotly.NET.TraceObjects
7+
open Plotly.NET.GenericChart
8+
open Newtonsoft.Json
9+
10+
let testAnnotations1 = [Annotation.init(Text = "test")]
11+
let testAnnotations2 = [Annotation.init(Text = "another one")]
12+
13+
let testShapes1 = [Shape.init(X0 = 42)]
14+
let testShapes2 = [Shape.init(X0 = 69)]
15+
16+
let testImages1 = [LayoutImage.init(Name = "image1")]
17+
let testImages2 = [LayoutImage.init(Name = "image2")]
18+
19+
let testSliders1 = [Slider.init(X = 1337)]
20+
let testSliders2 = [Slider.init(X = 5002)]
21+
22+
let testHiddenLabels1 = ["first"]
23+
let testHiddenLabels2 = ["second"]
24+
25+
let chart1 =
26+
Chart.Invisible()
27+
|> Chart.withLayout(
28+
Layout.init(
29+
Annotations = testAnnotations1,
30+
Shapes = testShapes1,
31+
Images = testImages1,
32+
Sliders = testSliders1,
33+
HiddenLabels = testHiddenLabels1
34+
)
35+
)
36+
37+
let chart2 =
38+
Chart.Invisible()
39+
|> Chart.withLayout(
40+
Layout.init(
41+
Annotations = testAnnotations2,
42+
Shapes = testShapes2,
43+
Images = testImages2,
44+
Sliders = testSliders2,
45+
HiddenLabels = testHiddenLabels2
46+
)
47+
)
48+
49+
let combined = Chart.combine [chart1; chart2]
50+
51+
[<Tests>]
52+
let ``Chart.Combine layouts tests`` =
53+
testList "ChartAPIs.Combine" [
54+
testList "Combine Layouts" [
55+
testCase "should combine annotations" (fun _ ->
56+
let actual = combined |> GenericChart.getLayout |> fun l -> l?annotations |> unbox<seq<Annotation>>
57+
Expect.sequenceEqual actual (Seq.append testAnnotations1 testAnnotations2) "combined chart layout did not contain correct annotations"
58+
)
59+
testCase "should combine shapes" (fun _ ->
60+
let actual = combined |> GenericChart.getLayout |> fun l -> l?shapes |> unbox<seq<Shape>>
61+
Expect.sequenceEqual actual (Seq.append testShapes1 testShapes2) "combined chart layout did not contain correct shapes"
62+
)
63+
testCase "should combine images" (fun _ ->
64+
let actual = combined |> GenericChart.getLayout |> fun l -> l?images |> unbox<seq<LayoutImage>>
65+
Expect.sequenceEqual actual (Seq.append testImages1 testImages2) "combined chart layout did not contain correct images"
66+
)
67+
testCase "should combine sliders" (fun _ ->
68+
let actual = combined |> GenericChart.getLayout |> fun l -> l?sliders |> unbox<seq<Slider>>
69+
Expect.sequenceEqual actual (Seq.append testSliders1 testSliders2) "combined chart layout did not contain correct sliders"
70+
)
71+
testCase "should combine hidden labels" (fun _ ->
72+
let actual = combined |> GenericChart.getLayout |> fun l -> l?hiddenlabels |> unbox<seq<string>>
73+
Expect.sequenceEqual actual (Seq.append testHiddenLabels1 testHiddenLabels2) "combined chart layout did not contain correct hidden labels"
74+
)
75+
]
76+
]

tests/Plotly.NET.Tests/Plotly.NET.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<Compile Include="TestUtils.fs" />
11+
<Compile Include="ChartAPIs\Combine.fs" />
1112
<Compile Include="CommonAbstractions\Colors.fs" />
1213
<Compile Include="LayoutObjects\Slider.fs" />
1314
<Compile Include="LayoutObjects\LinearAxis.fs" />

0 commit comments

Comments
 (0)