Skip to content

Commit e124efe

Browse files
committed
Homogenize Chart3d implementation
1 parent 338f16a commit e124efe

13 files changed

+780
-653
lines changed

docs/content/3d-scatter-plots.fsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
#r "../../bin/FSharp.Plotly.dll"
44

55
(**
6-
# FSharp.Plotly: Pie and Doughnut Charts
6+
# FSharp.Plotly: Scatter3d Charts
77
8-
*Summary:* This example shows how to create pie and doughnut charts in F#.
8+
*Summary:* This example shows how to create three-dimensional scatter charts in F#.
99
10-
A pie or a doughnut chart can be created using the `Chart.Pie` and `Chart.Doughnut` functions.
11-
When creating pie or doughnut charts, it is usually desirable to provide both labels and
12-
values.
10+
A Scatter3d chart report shows a three-dimensional spinnable view of your data
1311
*)
1412

1513
open FSharp.Plotly
@@ -20,10 +18,13 @@ let z = [19; 26; 55;]
2018

2119

2220
(*** define-output:scatter3d_1 ***)
23-
Chart3d.Scatter(x,y,z,StyleOption.Mode.Markers)
21+
Chart.Scatter3d(x,y,z,StyleOption.Mode.Markers)
22+
|> Chart.withX_AxisStyle("my x-axis")
23+
|> Chart.withSize(800.,800.)
24+
2425
(*** include-it:scatter3d_1 ***)
25-
|> Chart3d.withSize(900.,900.)
26-
|> Chart3d.Show
26+
|> Chart.Show
27+
2728

2829

2930

src/FSharp.Plotly/AxisObjects.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,9 +750,9 @@ module AxisObjects =
750750
let mutable _domain: Domain option = None
751751
let mutable _aspectmode: _ option = None
752752
let mutable _aspectratio: Aspectratio option = None
753-
let mutable _xaxis: Xaxis option = None
754-
let mutable _yaxis: Yaxis option = None
755-
let mutable _zaxis: Zaxis option = None
753+
let mutable _xaxis: LinearAxis option = None
754+
let mutable _yaxis: LinearAxis option = None
755+
let mutable _zaxis: LinearAxis option = None
756756
let mutable __isSubplotObj: bool option = Some true
757757
//let mutable _role: string option = Some "object"
758758

src/FSharp.Plotly/Chart.fs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Chart =
3636
GenericChart.Chart (trace,None)
3737

3838
/// Uses points to represent data points
39-
static member Point(xy,?Name,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont,?Dash,?Width) =
39+
static member Point(xy,?Name,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont) =
4040
let x,y = Seq.unzip xy
4141
Chart.Point(x, y, ?Name=Name,?Showlegend=Showlegend,?MarkerSymbol=MarkerSymbol,?Color=Color,?Opacity=Opacity,?Labels=Labels,?TextPosition=TextPosition,?TextFont=TextFont)
4242

@@ -351,8 +351,19 @@ type Chart =
351351

352352

353353

354-
355-
354+
// ---------------------------------------------------------------------------------------------------------------------------------------------------
355+
// 3d - Chart --->
356+
357+
/// Uses points, line or both depending on the mode to represent 3d-data points
358+
static member Scatter3d(x, y, z, mode, ?Name,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont,?Dash,?Width) =
359+
let trace =
360+
Trace3dObjects.Scatter3d()
361+
|> Options.Scatter3d(X = x,Y = y,Z=z, Mode=mode)
362+
|> Options.ITraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
363+
|> Options.ILine(Options.Line(?Color=Color,?Dash=Dash,?Width=Width))
364+
|> Options.IMarker(Options.Marker(?Color=Color,?Symbol=MarkerSymbol))
365+
|> Options.ITextLabel(?Text=Labels,?Textposition=TextPosition,?Textfont=TextFont)
366+
GenericChart.Chart (trace,None)
356367

357368

358369

src/FSharp.Plotly/Chart3d.fs

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/FSharp.Plotly/Chart3dExtensions.fs

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/FSharp.Plotly/ChartExtensions.fs

Lines changed: 97 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,23 @@ module ChartExtensions =
2727
| _ -> trace
2828
)
2929
)
30-
//
31-
// /// Set the name related properties of a trace
32-
// // TODO
33-
// static member withTraceInfo(?textinfo,?textinfoposition,?hoverinfo) =
34-
// (fun (ch:GenericChart) ->
35-
// ch |> mapiTrace (fun i trace ->
36-
// trace) // |> Helpers.ApplyTraceStyles())
37-
// )
3830

39-
// /// Apply styling to the Marker(s) of the chart as Object.
40-
// static member withMarkerOption(marker:Marker) =
41-
// (fun (ch:GenericChart) ->
42-
// ch
43-
// |> mapTrace (fun trace ->
44-
// match box trace with
45-
// | :? Trace as t when t:(member Walk : unit -> unit) -> t
46-
// | t -> trace
47-
// )
48-
// )
31+
/// Apply styling to the Marker(s) of the chart as Object.
32+
static member withMarkerOption(marker:MarkerOptions) =
33+
(fun (ch:GenericChart) ->
34+
ch |> mapTrace (fun trace ->
35+
match trace with
36+
| :? IMarker as mTrace ->
37+
mTrace
38+
|> Options.IMarker(marker) :?> ITrace
39+
| _ -> trace
40+
)
41+
)
4942

50-
// /// Apply styling to the Marker(s) of the chart.
51-
// static member withMarkerStyle(?Size,?Color,?Symbol,?Opacity) =
52-
// let marker =
53-
// Marker()
54-
// |> Helpers.ApplyMarkerStyles(?size=Size,?color=Color,?symbol=Symbol,?opacity=Opacity)
55-
//
56-
// Chart.withMarkerOption(marker)
43+
/// Apply styling to the Marker(s) of the chart.
44+
static member withMarkerStyle(?Size,?Color,?Symbol,?Opacity) =
45+
let marker = Options.Marker(?Size=Size,?Color=Color,?Symbol=Symbol,?Opacity=Opacity)
46+
Chart.withMarkerOption(marker)
5747

5848
/// Apply styling to the Line(s) of the chart as Object.
5949
static member withLineOption(line:LineOptions) =
@@ -73,62 +63,118 @@ module ChartExtensions =
7363
Chart.withLineOption(line)
7464

7565

76-
//
66+
7767
// ####################### Apply to layout
68+
69+
// Sets x-Axis of 2d and 3d- Charts
7870
static member withX_Axis(xAxis:AxisOptions) =
79-
(fun (ch:GenericChart) ->
80-
let layout =
81-
Options.Layout(xAxis=xAxis)
82-
GenericChart.addLayout layout ch)
71+
(fun (ch:GenericChart) ->
72+
let contains3d =
73+
ch
74+
|> existsTrace (fun t ->
75+
match t with
76+
| :? ITrace3d -> true
77+
| _ -> false)
78+
79+
match contains3d with
80+
| false ->
81+
let layout =
82+
Options.Layout(xAxis=xAxis)
83+
GenericChart.addLayout layout ch
84+
| true ->
85+
let layout =
86+
Options.Layout(Scene=Options.Scene(xAxis=xAxis))
87+
GenericChart.addLayout layout ch
88+
)
89+
90+
8391

84-
static member withX_AxisStyle(title,?MinMax) =
85-
(fun (ch:GenericChart) ->
86-
let range = if MinMax.IsSome then Some (StyleOption.RangeValues.MinMax (MinMax.Value)) else None
87-
let xaxis = Options.Axis(Title=title,?Range=range)
88-
let layout = Options.Layout(xAxis=xaxis)
89-
GenericChart.addLayout layout ch)
90-
92+
// Sets x-Axis of 2d and 3d- Charts
93+
static member withX_AxisStyle(title,?MinMax) =
94+
let range = if MinMax.IsSome then Some (StyleOption.RangeValues.MinMax (MinMax.Value)) else None
95+
let xaxis = Options.Axis(Title=title,?Range=range)
96+
Chart.withX_Axis(xaxis)
97+
9198

99+
// Sets y-Axis of 2d and 3d- Charts
92100
static member withY_Axis(yAxis:AxisOptions) =
93-
(fun (ch:GenericChart) ->
101+
(fun (ch:GenericChart) ->
102+
let contains3d =
103+
ch
104+
|> existsTrace (fun t ->
105+
match t with
106+
| :? ITrace3d -> true
107+
| _ -> false)
108+
109+
match contains3d with
110+
| false ->
111+
let layout =
112+
Options.Layout(yAxis=yAxis)
113+
GenericChart.addLayout layout ch
114+
| true ->
115+
let layout =
116+
Options.Layout(Scene=Options.Scene(yAxis=yAxis))
117+
GenericChart.addLayout layout ch
118+
)
119+
120+
// Sets y-Axis of 3d- Charts
121+
static member withY_AxisStyle(title,?MinMax) =
122+
let range = if MinMax.IsSome then Some (StyleOption.RangeValues.MinMax (MinMax.Value)) else None
123+
let yaxis = Options.Axis(Title=title,?Range=range)
124+
Chart.withY_Axis(yaxis)
125+
126+
127+
128+
// Sets z-Axis of 3d- Charts
129+
static member withZ_Axis(zAxis:AxisOptions) =
130+
(fun (ch:GenericChart) ->
94131
let layout =
95-
Options.Layout(yAxis=yAxis)
96-
GenericChart.addLayout layout ch)
132+
Options.Layout(Scene=Options.Scene(zAxis=zAxis))
133+
GenericChart.addLayout layout ch
134+
)
135+
136+
// Sets z-Axis of 3d- Charts
137+
static member withZ_AxisStyle(title,?MinMax) =
138+
let range = if MinMax.IsSome then Some (StyleOption.RangeValues.MinMax (MinMax.Value)) else None
139+
let zaxis = Options.Axis(Title=title,?Range=range)
140+
Chart.withZ_Axis(zaxis)
141+
142+
143+
97144

98-
static member withY_AxisStyle(title,?MinMax) =
99-
(fun (ch:GenericChart) ->
100-
let range = if MinMax.IsSome then Some (StyleOption.RangeValues.MinMax (MinMax.Value)) else None
101-
let yaxis = Options.Axis(Title=title,?Range=range)
102-
let layout = Options.Layout(yAxis=yaxis)
103-
GenericChart.addLayout layout ch)
104145

105146

147+
148+
149+
// Set the Layout options of a Chart
106150
static member withLayout(layout:LayoutOptions) =
107151
(fun (ch:GenericChart) ->
108152
GenericChart.addLayout layout ch)
109-
153+
154+
// Set the size of a Chart
110155
static member withSize(width,heigth) =
111156
(fun (ch:GenericChart) ->
112157
let layout = Options.Layout(Width=width,Height=heigth)
113158
GenericChart.addLayout layout ch)
114159

160+
// Set the margin of a Chart
115161
static member withMargin(margin:MarginOptions) =
116162
(fun (ch:GenericChart) ->
117163
let layout = Options.Layout(Margin=margin)
118164
GenericChart.addLayout layout ch)
119165

166+
// Set the margin of a Chart
120167
static member withMarginSize(?Left,?Right,?Top,?Bottom,?Pad,?Autoexpand) =
121168
let margin = Options.Margin(?Left=Left,?Right=Right,?Top=Top,?Bottom=Bottom,?Pad=Pad,?Autoexpand=Autoexpand)
122169
Chart.withMargin(margin)
123170

124171

125172

173+
// TODO: Include withLegend & withLegendStyle
126174

175+
// TODO: Include withError
127176

128-
// // with Layout
129-
// static member withError(layout:Layout) =
130-
// (fun (ch:GenericChart) ->
131-
// GenericChart.setLayout layout ch)
177+
// TODO: Include withShapes
132178

133179

134180

src/FSharp.Plotly/FSharp.Plotly.fsproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,8 @@
6969
<Compile Include="Layout.fs" />
7070
<Compile Include="Options.fs" />
7171
<Compile Include="GenericChart.fs" />
72-
<Compile Include="GenericChart3d.fs" />
7372
<Compile Include="Chart.fs" />
74-
<Compile Include="Chart3d.fs" />
7573
<Compile Include="ChartExtensions.fs" />
76-
<Compile Include="Chart3dExtensions.fs" />
7774
<None Include="Script.fsx" />
7875
<None Include="paket.references" />
7976
<None Include="paket.template" />

src/FSharp.Plotly/GenericChart.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ module GenericChart =
174174
match gChart with
175175
| Chart (_) -> 1
176176
| MultiChart (traces,_) -> traces |> Seq.length
177+
178+
/// Creates a new GenericChart whose traces are the results of applying the given function to each of the trace of the GenericChart.
179+
let existsTrace (f:ITrace->bool) gChart =
180+
match gChart with
181+
| Chart (trace,_) -> f trace
182+
| MultiChart (traces,_) -> traces |> List.exists f
177183

178184
/// Converts from a trace object and a layout object into GenericChart
179185
let ofTraceObject trace layout =

0 commit comments

Comments
 (0)