Skip to content

Commit db7ce67

Browse files
committed
Extend Chart.Stack to work with both 3D and 2D Charts
1 parent a27f4f1 commit db7ce67

File tree

7 files changed

+151
-46
lines changed

7 files changed

+151
-46
lines changed

src/FSharp.Plotly/ChartExtensions.fs

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -329,50 +329,86 @@ module ChartExtensions =
329329
else
330330
s
331331

332+
let contains3d ch =
333+
ch
334+
|> existsTrace (fun t ->
335+
match t with
336+
| :? Trace3d -> true
337+
| _ -> false)
338+
332339
charts
333340
|> Seq.mapi (fun i ch ->
334341
let colI,rowI,index = (i%col+1), (i/col+1),(i+1)
335342
let xdomain = (colWidth * float (colI-1), (colWidth * float colI) - space )
336343
let ydomain = (1. - ((rowWidth * float rowI) - space ),1. - (rowWidth * float (rowI-1)))
337-
let xaxis,yaxis,layout =
338-
let layout = GenericChart.getLayout ch
339-
let xName, yName = StyleParam.AxisId.X 1 |> StyleParam.AxisId.toString, StyleParam.AxisId.Y 1 |> StyleParam.AxisId.toString
340-
match (layout.TryGetTypedValue<Axis.LinearAxis> xName),(layout.TryGetTypedValue<Axis.LinearAxis> yName) with
341-
| Some x, Some y ->
342-
// remove axis
343-
DynObj.remove layout xName
344-
DynObj.remove layout yName
345-
346-
x |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
347-
y |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
348-
layout
349-
| Some x, None ->
350-
// remove x - axis
351-
DynObj.remove layout xName
352-
x |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
353-
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
354-
layout
355-
| None, Some y ->
356-
// remove y - axis
357-
DynObj.remove layout yName
358-
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
359-
y |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
360-
layout
361-
| None, None ->
362-
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
363-
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
364-
layout
365-
366-
ch
367-
|> GenericChart.setLayout layout
368-
|> Chart.withAxisAnchor(X=index,Y=index)
369-
|> Chart.withX_Axis(xaxis,index)
370-
|> Chart.withY_Axis(yaxis,index)
344+
345+
if contains3d ch then
346+
let sceneName = sprintf "scene%i" (i+1)
347+
348+
let scene =
349+
Scene.init (
350+
Domain =
351+
Domain.init(X = StyleParam.Range.MinMax xdomain,Y= StyleParam.Range.MinMax ydomain)
352+
)
353+
let layout =
354+
GenericChart.getLayout ch
355+
|> Layout.AddScene (
356+
sceneName,
357+
scene
358+
)
359+
ch
360+
|> mapTrace
361+
(fun t ->
362+
t?scene <- sceneName
363+
t
364+
)
365+
|> GenericChart.setLayout layout
366+
//|> Chart.withAxisAnchor(X=index,Y=index)
367+
else
368+
369+
let xaxis,yaxis,layout =
370+
let layout = GenericChart.getLayout ch
371+
let xName, yName = StyleParam.AxisId.X 1 |> StyleParam.AxisId.toString, StyleParam.AxisId.Y 1 |> StyleParam.AxisId.toString
372+
match (layout.TryGetTypedValue<Axis.LinearAxis> xName),(layout.TryGetTypedValue<Axis.LinearAxis> yName) with
373+
| Some x, Some y ->
374+
// remove axis
375+
DynObj.remove layout xName
376+
DynObj.remove layout yName
377+
378+
x |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
379+
y |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
380+
layout
381+
| Some x, None ->
382+
// remove x - axis
383+
DynObj.remove layout xName
384+
x |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
385+
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
386+
layout
387+
| None, Some y ->
388+
// remove y - axis
389+
DynObj.remove layout yName
390+
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
391+
y |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
392+
layout
393+
| None, None ->
394+
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
395+
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
396+
layout
397+
398+
ch
399+
|> GenericChart.setLayout layout
400+
|> Chart.withAxisAnchor(X=index,Y=index)
401+
|> Chart.withX_Axis(xaxis,index)
402+
|> Chart.withY_Axis(yaxis,index)
371403
)
372404

373405
|> Chart.Combine
374406
)
375-
407+
408+
//static member Stack3D (?Columns:int, ?Space) =
409+
// (fun (charts:#seq<GenericChart>) ->
410+
// ()
411+
// )
376412

377413
/// Save chart as html single page
378414
static member SaveHtmlAs pathName (ch:GenericChart,?Verbose) =

src/FSharp.Plotly/Domain.fs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace FSharp.Plotly
2+
3+
open System
4+
5+
6+
/// Dimensions type inherits from dynamic object
7+
type Domain () =
8+
inherit DynamicObj ()
9+
10+
/// Initialized Dimensions object
11+
static member init
12+
(
13+
?X : StyleParam.Range,
14+
?Y : StyleParam.Range,
15+
?Row : int,
16+
?Column : int
17+
) =
18+
Domain ()
19+
|> Domain.style
20+
(
21+
?X = X,
22+
?Y = Y,
23+
?Row = Row,
24+
?Column = Column
25+
)
26+
27+
28+
// Applies the styles to Dimensions()
29+
static member style
30+
(
31+
?X : StyleParam.Range,
32+
?Y : StyleParam.Range,
33+
?Row : int,
34+
?Column : int
35+
) =
36+
(fun (dom:Domain) ->
37+
X |> DynObj.setValueOptBy dom "x" StyleParam.Range.convert
38+
Y |> DynObj.setValueOptBy dom "y" StyleParam.Range.convert
39+
Row |> DynObj.setValueOpt dom "row"
40+
Column |> DynObj.setValueOpt dom "column"
41+
42+
// out ->
43+
dom
44+
)

src/FSharp.Plotly/FSharp.Plotly.fsproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Compile Include="Light.fs" />
2222
<Compile Include="Contours.fs" />
2323
<Compile Include="Dimensions.fs" />
24+
<Compile Include="Domain.fs" />
2425
<Compile Include="Line.fs" />
2526
<Compile Include="Marker.fs" />
2627
<Compile Include="Font.fs" />
@@ -37,6 +38,7 @@
3738
<Compile Include="GenericChart.fs" />
3839
<Compile Include="Chart.fs" />
3940
<Compile Include="ChartExtensions.fs" />
41+
<Compile Include="Templates.fs" />
4042
<None Include="TestScript.fsx" />
4143
<None Include="paket.references" />
4244
<None Include="paket.template" />

src/FSharp.Plotly/Layout.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,14 @@ type Layout() =
273273
layout
274274
)
275275

276+
static member AddScene
277+
(
278+
name: string,
279+
scene:Scene
280+
) =
281+
(fun (layout:Layout) ->
282+
scene |> DynObj.setValue layout name
283+
layout
284+
)
285+
276286

src/FSharp.Plotly/Scene.fs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace FSharp.Plotly
2-
2+
open FSharp.Plotly.StyleParam
33

44

55
/// Scene
@@ -14,9 +14,9 @@ type Scene() =
1414
?yAxis ,
1515
?zAxis ,
1616
?isSubplotObj ,
17-
?BgColor
17+
?BgColor ,
1818
// ?Camera ,
19-
// ?Domain ,
19+
(?Domain : Domain)
2020
// ?Aspectmode ,
2121
// ?Aspectratio
2222
) =
@@ -27,7 +27,8 @@ type Scene() =
2727
?yAxis = yAxis ,
2828
?zAxis = zAxis ,
2929
?isSubplotObj = isSubplotObj ,
30-
?BgColor = BgColor
30+
?BgColor = BgColor ,
31+
?Domain = Domain
3132
)
3233

3334
// [<JsonIgnore>]
@@ -39,16 +40,16 @@ type Scene() =
3940
?yAxis:Axis.LinearAxis,
4041
?zAxis:Axis.LinearAxis,
4142
?isSubplotObj ,
42-
?BgColor
43+
?BgColor ,
4344
// ?Camera ,
44-
// ?Domain ,
45+
?Domain:Domain
4546
// ?Aspectmode ,
4647
// ?Aspectratio
4748
) =
4849
(fun (scene:Scene) ->
4950
isSubplotObj |> DynObj.setValueOpt scene "_isSubplotObj"
5051
BgColor |> DynObj.setValueOpt scene "bgcolor"
51-
52+
Domain |> DynObj.setValueOpt scene "domain"
5253
// Update
5354
xAxis |> DynObj.setValueOpt scene "xaxis"
5455
yAxis |> DynObj.setValueOpt scene "yaxis"

src/FSharp.Plotly/Templates.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
namespace FSharp.Plotly

src/FSharp.Plotly/Trace3d.fs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ module Trace3d =
4141
?Surfaceaxis,
4242
?Surfacecolor,
4343
?Projection : Projection,
44-
?Scene,
44+
?Scene : string,
4545
?Error_y: Error,
4646
?Error_x: Error,
4747
?Error_z: Error,
4848
?Xsrc : string,
4949
?Ysrc : string,
5050
?Zsrc : string
5151
) =
52-
//(fun (scatter:('T :> Trace3d)) ->
53-
(fun (scatter: Trace3d) ->
54-
//scatter.set_type plotType
52+
//(fun (scatter:('T :> Trace3d)) ->
53+
(fun (scatter: Trace3d) ->
54+
//scatter.set_type plotType
5555
X |> DynObj.setValueOpt scatter "x"
5656
Y |> DynObj.setValueOpt scatter "y"
5757
Z |> DynObj.setValueOpt scatter "z"
@@ -218,4 +218,15 @@ module Trace3d =
218218
mesh3d
219219
)
220220

221+
static member setScene
222+
(
223+
?SceneName:string
224+
) =
225+
fun (trace:Trace3d) ->
226+
SceneName |> DynObj.setValueOpt trace "scene"
227+
trace
228+
229+
230+
231+
221232

0 commit comments

Comments
 (0)