Skip to content

Commit

Permalink
Extend Chart.Stack to work with both 3D and 2D Charts
Browse files Browse the repository at this point in the history
  • Loading branch information
kMutagene committed Oct 15, 2019
1 parent a27f4f1 commit db7ce67
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 46 deletions.
106 changes: 71 additions & 35 deletions src/FSharp.Plotly/ChartExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -329,50 +329,86 @@ module ChartExtensions =
else
s

let contains3d ch =
ch
|> existsTrace (fun t ->
match t with
| :? Trace3d -> true
| _ -> false)

charts
|> Seq.mapi (fun i ch ->
let colI,rowI,index = (i%col+1), (i/col+1),(i+1)
let xdomain = (colWidth * float (colI-1), (colWidth * float colI) - space )
let ydomain = (1. - ((rowWidth * float rowI) - space ),1. - (rowWidth * float (rowI-1)))
let xaxis,yaxis,layout =
let layout = GenericChart.getLayout ch
let xName, yName = StyleParam.AxisId.X 1 |> StyleParam.AxisId.toString, StyleParam.AxisId.Y 1 |> StyleParam.AxisId.toString
match (layout.TryGetTypedValue<Axis.LinearAxis> xName),(layout.TryGetTypedValue<Axis.LinearAxis> yName) with
| Some x, Some y ->
// remove axis
DynObj.remove layout xName
DynObj.remove layout yName

x |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
y |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
layout
| Some x, None ->
// remove x - axis
DynObj.remove layout xName
x |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
layout
| None, Some y ->
// remove y - axis
DynObj.remove layout yName
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
y |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
layout
| None, None ->
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
layout

ch
|> GenericChart.setLayout layout
|> Chart.withAxisAnchor(X=index,Y=index)
|> Chart.withX_Axis(xaxis,index)
|> Chart.withY_Axis(yaxis,index)

if contains3d ch then
let sceneName = sprintf "scene%i" (i+1)

let scene =
Scene.init (
Domain =
Domain.init(X = StyleParam.Range.MinMax xdomain,Y= StyleParam.Range.MinMax ydomain)
)
let layout =
GenericChart.getLayout ch
|> Layout.AddScene (
sceneName,
scene
)
ch
|> mapTrace
(fun t ->
t?scene <- sceneName
t
)
|> GenericChart.setLayout layout
//|> Chart.withAxisAnchor(X=index,Y=index)
else

let xaxis,yaxis,layout =
let layout = GenericChart.getLayout ch
let xName, yName = StyleParam.AxisId.X 1 |> StyleParam.AxisId.toString, StyleParam.AxisId.Y 1 |> StyleParam.AxisId.toString
match (layout.TryGetTypedValue<Axis.LinearAxis> xName),(layout.TryGetTypedValue<Axis.LinearAxis> yName) with
| Some x, Some y ->
// remove axis
DynObj.remove layout xName
DynObj.remove layout yName

x |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
y |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
layout
| Some x, None ->
// remove x - axis
DynObj.remove layout xName
x |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
layout
| None, Some y ->
// remove y - axis
DynObj.remove layout yName
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
y |> Axis.LinearAxis.style(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
layout
| None, None ->
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.Y index,Domain=StyleParam.Range.MinMax xdomain),
Axis.LinearAxis.init(Anchor=StyleParam.AxisAnchorId.X index,Domain=StyleParam.Range.MinMax ydomain),
layout

ch
|> GenericChart.setLayout layout
|> Chart.withAxisAnchor(X=index,Y=index)
|> Chart.withX_Axis(xaxis,index)
|> Chart.withY_Axis(yaxis,index)
)

|> Chart.Combine
)


//static member Stack3D (?Columns:int, ?Space) =
// (fun (charts:#seq<GenericChart>) ->
// ()
// )

/// Save chart as html single page
static member SaveHtmlAs pathName (ch:GenericChart,?Verbose) =
Expand Down
44 changes: 44 additions & 0 deletions src/FSharp.Plotly/Domain.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace FSharp.Plotly

open System


/// Dimensions type inherits from dynamic object
type Domain () =
inherit DynamicObj ()

/// Initialized Dimensions object
static member init
(
?X : StyleParam.Range,
?Y : StyleParam.Range,
?Row : int,
?Column : int
) =
Domain ()
|> Domain.style
(
?X = X,
?Y = Y,
?Row = Row,
?Column = Column
)


// Applies the styles to Dimensions()
static member style
(
?X : StyleParam.Range,
?Y : StyleParam.Range,
?Row : int,
?Column : int
) =
(fun (dom:Domain) ->
X |> DynObj.setValueOptBy dom "x" StyleParam.Range.convert
Y |> DynObj.setValueOptBy dom "y" StyleParam.Range.convert
Row |> DynObj.setValueOpt dom "row"
Column |> DynObj.setValueOpt dom "column"

// out ->
dom
)
2 changes: 2 additions & 0 deletions src/FSharp.Plotly/FSharp.Plotly.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Compile Include="Light.fs" />
<Compile Include="Contours.fs" />
<Compile Include="Dimensions.fs" />
<Compile Include="Domain.fs" />
<Compile Include="Line.fs" />
<Compile Include="Marker.fs" />
<Compile Include="Font.fs" />
Expand All @@ -37,6 +38,7 @@
<Compile Include="GenericChart.fs" />
<Compile Include="Chart.fs" />
<Compile Include="ChartExtensions.fs" />
<Compile Include="Templates.fs" />
<None Include="TestScript.fsx" />
<None Include="paket.references" />
<None Include="paket.template" />
Expand Down
10 changes: 10 additions & 0 deletions src/FSharp.Plotly/Layout.fs
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,14 @@ type Layout() =
layout
)

static member AddScene
(
name: string,
scene:Scene
) =
(fun (layout:Layout) ->
scene |> DynObj.setValue layout name
layout
)


15 changes: 8 additions & 7 deletions src/FSharp.Plotly/Scene.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace FSharp.Plotly

open FSharp.Plotly.StyleParam


/// Scene
Expand All @@ -14,9 +14,9 @@ type Scene() =
?yAxis ,
?zAxis ,
?isSubplotObj ,
?BgColor
?BgColor ,
// ?Camera ,
// ?Domain ,
(?Domain : Domain)
// ?Aspectmode ,
// ?Aspectratio
) =
Expand All @@ -27,7 +27,8 @@ type Scene() =
?yAxis = yAxis ,
?zAxis = zAxis ,
?isSubplotObj = isSubplotObj ,
?BgColor = BgColor
?BgColor = BgColor ,
?Domain = Domain
)

// [<JsonIgnore>]
Expand All @@ -39,16 +40,16 @@ type Scene() =
?yAxis:Axis.LinearAxis,
?zAxis:Axis.LinearAxis,
?isSubplotObj ,
?BgColor
?BgColor ,
// ?Camera ,
// ?Domain ,
?Domain:Domain
// ?Aspectmode ,
// ?Aspectratio
) =
(fun (scene:Scene) ->
isSubplotObj |> DynObj.setValueOpt scene "_isSubplotObj"
BgColor |> DynObj.setValueOpt scene "bgcolor"

Domain |> DynObj.setValueOpt scene "domain"
// Update
xAxis |> DynObj.setValueOpt scene "xaxis"
yAxis |> DynObj.setValueOpt scene "yaxis"
Expand Down
1 change: 1 addition & 0 deletions src/FSharp.Plotly/Templates.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
namespace FSharp.Plotly
19 changes: 15 additions & 4 deletions src/FSharp.Plotly/Trace3d.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ module Trace3d =
?Surfaceaxis,
?Surfacecolor,
?Projection : Projection,
?Scene,
?Scene : string,
?Error_y: Error,
?Error_x: Error,
?Error_z: Error,
?Xsrc : string,
?Ysrc : string,
?Zsrc : string
) =
//(fun (scatter:('T :> Trace3d)) ->
(fun (scatter: Trace3d) ->
//scatter.set_type plotType
//(fun (scatter:('T :> Trace3d)) ->
(fun (scatter: Trace3d) ->
//scatter.set_type plotType
X |> DynObj.setValueOpt scatter "x"
Y |> DynObj.setValueOpt scatter "y"
Z |> DynObj.setValueOpt scatter "z"
Expand Down Expand Up @@ -218,4 +218,15 @@ module Trace3d =
mesh3d
)

static member setScene
(
?SceneName:string
) =
fun (trace:Trace3d) ->
SceneName |> DynObj.setValueOpt trace "scene"
trace





0 comments on commit db7ce67

Please sign in to comment.