Skip to content

Commit 63f9ea1

Browse files
committed
Refactoring surface plot
1 parent cdd7653 commit 63f9ea1

17 files changed

+632
-177
lines changed

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
(*** hide ***)
2+
#r "../../bin/Newtonsoft.Json.dll"
3+
#r "../../bin/FSharp.Plotly.dll"
4+
5+
(**
6+
# FSharp.Plotly: Pie and Doughnut Charts
7+
8+
*Summary:* This example shows how to create pie and doughnut charts in F#.
9+
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.
13+
*)
14+
15+
open System
16+
open FSharp.Plotly
17+
18+
19+
// Generate linearly spaced vector
20+
let linspace (min,max,n) =
21+
if n <= 2 then failwithf "n needs to be larger then 2"
22+
let bw = float (max - min) / (float n - 1.)
23+
Array.init n (fun i -> min + (bw * float i))
24+
//[|min ..bw ..max|]
25+
26+
27+
// Create example data
28+
let size = 100
29+
let x = linspace(-2. * Math.PI, 2. * Math.PI, size)
30+
let y = linspace(-2. * Math.PI, 2. * Math.PI, size)
31+
32+
let f x y = - (5. * x / (x**2. + y**2. + 1.) )
33+
34+
let z =
35+
Array.init size (fun i ->
36+
Array.init size (fun j -> f x.[j] y.[i] )
37+
)
38+
39+
let rnd = System.Random()
40+
let a = Array.init 50 (fun _ -> rnd.NextDouble())
41+
let b = Array.init 50 (fun _ -> rnd.NextDouble())
42+
let c = Array.init 50 (fun _ -> rnd.NextDouble())
43+
44+
let cont = DynamicObj()
45+
//cont?color <- ""
46+
cont?show <- true
47+
cont?width <- 3
48+
49+
Trace3d.initMesh3d
50+
(fun mesh3d ->
51+
mesh3d?x <- a
52+
mesh3d?y <- b
53+
mesh3d?z <- c
54+
mesh3d?flatshading <- true
55+
//mesh3d?contour <- cont
56+
mesh3d
57+
)
58+
|> GenericChart.ofTraceObject
59+
|> Chart.Show
60+
61+
62+
//(*** define-output:contour1 ***)
63+
//z
64+
//|> Chart.Surface
65+
//|> Chart.withSize(600.,600.)
66+
//(*** include-it:contour1 ***)
67+
//
68+
//// Create simple example data were x y and z is given (z is a xy-Matrix)
69+
//let x' = [0.;2.5]
70+
//let y' = [0.;2.5]
71+
//let z' = [
72+
// [1.;1.;]; // row wise (length x)
73+
// [1.;1.;];
74+
// ] // column (length y)
75+
//
76+
//Chart.Surface(z',x',y')
77+
78+
79+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let z = [19; 26; 55;]
1818

1919

2020
(*** define-output:scatter3d_1 ***)
21-
Chart.Scatter3d(x,y,z,StyleOption.Mode.Markers)
21+
Chart.Scatter3d(x,y,z,StyleParam.Mode.Markers)
2222
|> Chart.withX_AxisStyle("my x-axis")
2323
|> Chart.withY_AxisStyle("my y-axis")
2424
|> Chart.withZ_AxisStyle("my z-axis")

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ let z =
3636
Array.init size (fun j -> f x.[j] y.[i] )
3737
)
3838

39+
let rnd = System.Random()
40+
let a = Array.init 50 (fun _ -> rnd.NextDouble())
41+
let b = Array.init 50 (fun _ -> rnd.NextDouble())
42+
let c = Array.init 50 (fun _ -> rnd.NextDouble())
43+
44+
//let contours =
45+
3946

40-
4147
(*** define-output:contour1 ***)
4248
z
4349
|> Chart.Surface
@@ -49,10 +55,10 @@ let x' = [0.;2.5]
4955
let y' = [0.;2.5]
5056
let z' = [
5157
[1.;1.;]; // row wise (length x)
52-
[1.;1.;];
58+
[1.;2.;];
5359
] // column (length y)
5460

55-
Chart.Surface(z',x',y')
56-
61+
Chart.Surface(z',x',y',Opacity=0.,Contours=Contours.initContours(Contours.ContoursStyle.XyzContours(Show=true)))
62+
|> Chart.Show
5763

5864

docs/content/getting-started.fsx

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,18 @@ FSharp.Plotly is powered by popular JavaScript charting library [Plotly](https:/
1616
#r "../../bin/FSharp.Plotly.dll"
1717
open FSharp.Plotly
1818

19-
20-
21-
let trace =
22-
Scatter(
23-
24-
x = [1; 2; 3; 4],
25-
y = [12; 9; 15; 12],
26-
mode = "lines+markers",
27-
name = "lines and markers"
28-
)
29-
30-
let layout =
31-
Layout()
32-
33-
trace.get_type
34-
35-
36-
GenericChart.ofTraceObject trace layout
19+
let scatter =
20+
// let scatter =
21+
// Trace("scattergl")
22+
Trace.initScatter (fun scatter ->
23+
scatter?x <- [1; 2; 3; 4]
24+
scatter?y <- [1; 2; 3; 4]
25+
scatter?mode <- "lines+markers"
26+
scatter?name <- "lines and markers"
27+
scatter
28+
)
29+
30+
GenericChart.ofTraceObject scatter
3731
|> Chart.Show
3832

3933

docs/content/range-plots.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ Chart.Pie(values,labels)
2222
(*** include-it:pie1 ***)
2323

2424
(*** define-output:doughnut1 ***)
25-
Chart.Doughnut(values,labels,hole=0.3)
25+
Chart.Doughnut(values,labels,Hole=0.3)
2626
(*** include-it:doughnut1 ***)

src/FSharp.Plotly/Chart.fs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open FSharp.Care.Collections
66

77
open GenericChart
88
open Trace
9+
open Trace3d
910
open Layout
1011

1112
/// Provides a set of static methods for creating charts.
@@ -376,27 +377,33 @@ type Chart =
376377

377378
/// Uses points, line or both depending on the mode to represent 3d-data points
378379
static member Scatter3d(x, y, z, mode, ?Name,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont,?Dash,?Width) =
379-
Trace3d.initScatter3d (Trace3d.Trace3dStyle.Scatter3d(X = x,Y = y,Z=z, Mode=mode) )
380+
Trace3d.initScatter3d (Trace3dStyle.Scatter3d(X = x,Y = y,Z=z, Mode=mode) )
380381
|> TraceStyle.TraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
381382
|> TraceStyle.Line(?Color=Color,?Dash=Dash,?Width=Width)
382383
|> TraceStyle.Marker(?Color=Color,?Symbol=MarkerSymbol)
383384
|> TraceStyle.TextLabel(?Text=Labels,?Textposition=TextPosition,?Textfont=TextFont)
384385
|> GenericChart.ofTraceObject
385386

386387

387-
/// Displays the distribution of data based on the five number summary: minimum, first quartile, median, third quartile, and maximum.
388+
/// Uses points, line or both depending on the mode to represent 3d-data points
388389
static member Scatter3d(xyz, mode, ?Name,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont,?Dash,?Width) =
389390
let x,y,z = Seq.unzip3 xyz
390391
Chart.Scatter3d(x, y, z, mode, ?Name=Name,?Showlegend=Showlegend,?MarkerSymbol=MarkerSymbol,?Color=Color,?Opacity=Opacity,?Labels=Labels,?TextPosition=TextPosition,?TextFont=TextFont,?Dash=Dash,?Width=Width)
391392

392-
// /// Uses points, line or both depending on the mode to represent 3d-data points
393-
// static member Surface(data:seq<#seq<#IConvertible>>,?X,?Y, ?Name,?Showlegend,?Opacity,?Colorscale,?Showscale,?zSmooth,?Colorbar) =
394-
// let trace =
395-
// Trace3dObjects.Surface()
396-
// |> Options.IMapZ(Z=data,?X=X, ?Y=Y)
397-
// |> Options.IColormap(?Colorscale=Colorscale,?Showscale=Showscale,?zSmooth=zSmooth,?Colorbar=Colorbar)
398-
// |> Options.ITraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
399-
// //|> Options.ITextLabel(?Text=Labels,?Textposition=TextPosition,?Textfont=TextFont)
400-
// GenericChart.Chart (trace,None)
393+
/// Uses points, line or both depending on the mode to represent 3d-data points
394+
static member Surface(data:seq<#seq<#IConvertible>>,?X,?Y, ?Name,?Showlegend,?Opacity,?Contours,?Colorscale,?Showscale,?Colorbar) =
395+
Trace3d.initSurface (Trace3dStyle.Surface (Z=data,?X=X, ?Y=Y,?Contours=Contours,
396+
?Colorscale=Colorscale,?Showscale=Showscale,?Colorbar=Colorbar ) )
397+
|> TraceStyle.TraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
398+
//|> TraceStyle.TextLabel(?Text=Labels,?Textposition=TextPosition,?Textfont=TextFont)
399+
|> GenericChart.ofTraceObject
400+
401+
402+
/// Uses points, line or both depending on the mode to represent 3d-data points
403+
static member Mesh3d(x, y, z, mode, ?Name,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont,?Dash,?Width) =
404+
Trace3d.initMesh3d (Trace3dStyle.Mesh3d(X = x,Y = y,Z=z) )
405+
|> TraceStyle.TraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
406+
|> TraceStyle.TextLabel(?Text=Labels,?Textposition=TextPosition,?Textfont=TextFont)
407+
|> GenericChart.ofTraceObject
401408

402409

src/FSharp.Plotly/ChartExtensions.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ module ChartExtensions =
4949
)
5050

5151
/// Apply styling to the Line(s) of the chart.
52-
static member withLineStyle(?Width,?Color,?Shape,?Dash,?Smoothing,?ColorScale) =
52+
static member withLineStyle(?Width,?Color,?Shape,?Dash,?Smoothing,?Colorscale) =
5353
let line =
5454
Line.init ( Line.LineStyle.Apply
55-
(?Width=Width,?Color=Color,?Shape=Shape,?Dash=Dash,?Smoothing=Smoothing,?ColorScale=ColorScale)
55+
(?Width=Width,?Color=Color,?Shape=Shape,?Dash=Dash,?Smoothing=Smoothing,?Colorscale=Colorscale)
5656
)
5757
Chart.withLine(line)
5858

0 commit comments

Comments
 (0)