Skip to content

Commit cc3aeaf

Browse files
committed
Add support for Shapes
1 parent e14df8d commit cc3aeaf

File tree

9 files changed

+182
-17
lines changed

9 files changed

+182
-17
lines changed

FSharp.Plotly.sln

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 14
3-
VisualStudioVersion = 14.0.25123.0
3+
VisualStudioVersion = 14.0.25420.1
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".paket", ".paket", "{63297B98-5CED-492C-A5B7-A5B4F73CF142}"
66
ProjectSection(SolutionItems) = preProject
@@ -46,6 +46,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "content", "content", "{8E6D
4646
docs\content\plotly-wpf.fsx = docs\content\plotly-wpf.fsx
4747
docs\content\polar-charts.fsx = docs\content\polar-charts.fsx
4848
docs\content\range-plots.fsx = docs\content\range-plots.fsx
49+
docs\content\shapes.fsx = docs\content\shapes.fsx
4950
EndProjectSection
5051
EndProject
5152
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{ED8079DD-2B06-4030-9F0F-DC548F98E1C4}"

docs/content/bar-charts.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let keys = ["Product A"; "Product B"; "Product C";]
1919
let labels = ["27% market share"; "24% market share"; "19% market share";]
2020

2121
(*** define-output:bar1 ***)
22-
Chart.Column(keys,values,Labels=labels,Opacity=0.3,Marker=Options.Marker(Color="rgba(222,45,38,0.8)"))
22+
Chart.Column(keys,values,Labels=labels,Opacity=0.3,Marker=Options.Marker(Color="rgba(222,45,38,0.8)",Size=1)) // Changing the thickness of the bar is not possible at the moment
2323
(*** include-it:bar1 ***)
2424

2525
(*** define-output:bar2 ***)

docs/content/getting-started.fsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ let trace =
2323

2424
x = [1; 2; 3; 4],
2525
y = [12; 9; 15; 12],
26-
mode = "lines+markers"
27-
//name = "lines and markers"
26+
mode = "lines+markers",
27+
name = "lines and markers"
2828
)
2929

3030
let layout =
@@ -37,8 +37,11 @@ GenericChart.ofTraceObject trace layout
3737
|> Chart.Show
3838

3939

40-
let red = FSharp.Care.Colors.Table.Office.red
41-
let red' = FSharp.Care.Colors.toHex false red
40+
41+
42+
43+
open FSharp.Care.Colors
44+
let red' = FSharp.Care.Colors.toHex false Table.Office.red
4245

4346
Chart.Point([1; 2; 3; 4],[12; 9; 15; 12])
4447
|> Chart.withMarkerStyle(Color=red')

docs/content/shapes.fsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(*** hide ***)
2+
#r "../../bin/Newtonsoft.Json.dll"
3+
#r "../../bin/FSharp.Plotly.dll"
4+
5+
(**
6+
# FSharp.Plotly: Shapes
7+
8+
*Summary:* This example shows how to create Shapes and add them to the Charts in F#.
9+
10+
*)
11+
12+
open FSharp.Plotly
13+
14+
let x = [1.; 2.; 3.; 4.; 5.; 6.; 7.; 8.; 9.; 10.; ]
15+
let y' = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
16+
17+
let s1 = Options.Shape(StyleOption.ShapeType.Rectangle,2.,4.,3.,4.,Opacity=0.3,Fillcolor="#d3d3d3")
18+
let s2 = Options.Shape(StyleOption.ShapeType.Rectangle,5.,7.,3.,4.,Opacity=0.3,Fillcolor="#d3d3d3")
19+
(*** define-output:line1 ***)
20+
Chart.Line(x,y',Name="line")
21+
//|> Chart.withShape(Options.Shape(StyleOption.ShapeType.Rectangle,2.,4.,3.,4.,Opacity=0.3,Fillcolor="#d3d3d3"))
22+
|> Chart.withShapes([s1;s2])
23+
(*** include-it:line1 ***)
24+
|> Chart.Show

src/FSharp.Plotly/AxisObjects.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,7 @@ module AxisObjects =
11341134
let mutable _opacity: float option = None
11351135
let mutable _line: Line option = None
11361136
let mutable _fillcolor: string option = None
1137+
let mutable _layer: string option = None
11371138
//let mutable _role: string option = Some "object"
11381139

11391140
/// Specifies the shape type to be drawn. If *line*, a line is drawn from (`x0`,`y0`) to (`x1`,`y1`) If *circle*, a circle is drawn from ((`x0`+`x1`)/2, (`y0`+`y1`)/2)) with radius (|(`x0`+`x1`)/2 - `x0`|, |(`y0`+`y1`)/2 -`y0`)|) If *rect*, a rectangle is drawn linking (`x0`,`y0`), (`x1`,`y0`), (`x1`,`y1`), (`x0`,`y1`), (`x0`,`y0`) If *path*, draw a custom SVG path using `path`.
@@ -1190,6 +1191,11 @@ module AxisObjects =
11901191
with get () = Option.get _fillcolor
11911192
and set value = _fillcolor <- Some value
11921193

1194+
/// Sets if the shapes is drawn on top or above
1195+
member __.layer
1196+
with get () = Option.get _layer
1197+
and set value = _layer <- Some value
1198+
11931199
// member __.role
11941200
// with get () = Option.get _role
11951201
// and set value = _role <- Some value
@@ -1205,4 +1211,5 @@ module AxisObjects =
12051211
member __.ShouldSerializeopacity() = not _opacity.IsNone
12061212
member __.ShouldSerializeline() = not _line.IsNone
12071213
member __.ShouldSerializefillcolor() = not _fillcolor.IsNone
1214+
member __.ShouldSerializelayer() = not _layer.IsNone
12081215
//member __.ShouldSerializerole() = not _role.IsNone

src/FSharp.Plotly/Chart.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ type Chart =
334334
let values,labels = Seq.unzip data
335335
Chart.Doughnut(values,Labels=labels,?Name=Name,?Showlegend=Showlegend,?Color=Color,?Hole=Hole,?Text=Text,?Textposition=Textposition,?TextFont=TextFont,?Hoverinfo=Hoverinfo,?Textinfo=Textinfo,?Opacity=Opacity)
336336

337-
337+
338338
// /// Computes the bi-dimensional histogram of two data samples and auto-determines the bin size.
339339
// static member Histogram2d(x,y,?Name,?HistNorm,?HistFunc,?Colorscale,?Showscale,?zSmooth,?Colorbar,?zAuto,?zMin,?zMax,?nBinsx,?nBinsy,?Xbins,?Ybins) =
340340
// let trace =

src/FSharp.Plotly/ChartExtensions.fs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,23 @@ module ChartExtensions =
201201
// TODO: Include withError
202202

203203
// TODO: Include withShapes
204+
//Specifies the shape type to be drawn. If "line", a line is drawn from (`x0`,`y0`) to (`x1`,`y1`) If "circle", a circle is drawn from
205+
//((`x0`+`x1`)/2, (`y0`+`y1`)/2)) with radius (|(`x0`+`x1`)/2 - `x0`|, |(`y0`+`y1`)/2 -`y0`)|) If "rect", a rectangle is drawn linking
206+
//(`x0`,`y0`), (`x1`,`y0`), (`x1`,`y1`), (`x0`,`y1`), (`x0`,`y0`)
207+
static member withShape(shape:ShapeOptions) =
208+
(fun (ch:GenericChart) ->
209+
210+
let shape' = Shape() |> shape
211+
let layout = Options.Layout(Shapes=[shape'])
212+
GenericChart.addLayout layout ch)
213+
214+
static member withShapes(shapes:ShapeOptions seq) =
215+
(fun (ch:GenericChart) ->
216+
217+
let shapes' =
218+
shapes |> Seq.map (fun shape -> shape (Shape()))
219+
let layout = Options.Layout(Shapes=shapes')
220+
GenericChart.addLayout layout ch)
204221

205222

206223

src/FSharp.Plotly/Options.fs

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,60 @@ type Options() =
146146
?Color,
147147
?Symbol:StyleOption.Symbol,
148148
?Opacity:float,
149-
?MultiSizes:seq<#IConvertible>
149+
?MultiSizes:seq<#IConvertible>,
150+
?Line : LineOptions,
151+
?Colorbar ,
152+
?Colorscale ,
153+
?Colors ,
154+
155+
?Maxdisplayed ,
156+
?Sizeref ,
157+
?Sizemin ,
158+
?Sizemode ,
159+
?Cauto ,
160+
?Cmax ,
161+
?Cmin ,
162+
?Autocolorscale ,
163+
?Reversescale ,
164+
?Showscale ,
165+
166+
?Symbolsrc ,
167+
?Opacitysrc ,
168+
?Sizesrc ,
169+
?Colorsrc ,
170+
?Cutliercolor ,
171+
?Colorssrc
172+
150173
) =
151174
(fun (marker:('T :> Marker)) ->
152-
Size |> Option.iter marker.set_size
153-
Color |> Option.iter marker.set_color
154-
Symbol |> Option.iter marker.set_symbol
155-
Opacity |> Option.iter marker.set_opacity
156-
MultiSizes |> Option.iter marker.set_size
157-
175+
Size |> Option.iter marker.set_size
176+
Color |> Option.iter marker.set_color
177+
Symbol |> Option.iter marker.set_symbol
178+
Opacity |> Option.iter marker.set_opacity
179+
MultiSizes |> Option.iter marker.set_size
180+
Line |> Option.iter (updatePropertyValueAndIgnore marker <@ marker.line @>)
181+
Colorbar |> Option.iter marker.set_colorbar
182+
Colorscale |> Option.iter marker.set_colorscale
183+
Colors |> Option.iter marker.set_colors
184+
185+
Maxdisplayed |> Option.iter marker.set_maxdisplayed
186+
Sizeref |> Option.iter marker.set_sizeref
187+
Sizemin |> Option.iter marker.set_sizemin
188+
Sizemode |> Option.iter marker.set_sizemode
189+
Cauto |> Option.iter marker.set_cauto
190+
Cmax |> Option.iter marker.set_cmax
191+
Cmin |> Option.iter marker.set_cmin
192+
Autocolorscale |> Option.iter marker.set_autocolorscale
193+
Reversescale |> Option.iter marker.set_reversescale
194+
Showscale |> Option.iter marker.set_showscale
195+
196+
Symbolsrc |> Option.iter marker.set_symbolsrc
197+
Opacitysrc |> Option.iter marker.set_opacitysrc
198+
Sizesrc |> Option.iter marker.set_sizesrc
199+
Colorsrc |> Option.iter marker.set_colorsrc
200+
Cutliercolor |> Option.iter marker.set_outliercolor
201+
Colorssrc |> Option.iter marker.set_colorssrc
202+
158203
marker
159204
)
160205

@@ -285,12 +330,14 @@ type Options() =
285330
?Separators,
286331
?Barmode:StyleOption.Barmode,
287332
?Bargap, // Some bar.. /box... is missing
333+
// bargroupgap
334+
//
288335
?Radialaxis:RadialAxisOptions,
289336
?Angularaxis:AngularAxisOptions,
290337
?Scene:SceneOptions,
291338
?Direction:StyleOption.Direction,
292339
?Orientation,
293-
?Shapes:ShapeOptions,
340+
?Shapes:Shape seq,
294341

295342
?Hidesources,?Smith,?Geo
296343

@@ -319,6 +366,7 @@ type Options() =
319366
Orientation |> Option.iter layout.set_orientation
320367
Barmode |> Option.iter (StyleOption.Barmode.toString >> layout.set_barmode)
321368
Bargap |> Option.iter layout.set_bargap
369+
Shapes |> Option.iter layout.set_shapes
322370

323371
// Update
324372
Font |> Option.iter (updatePropertyValueAndIgnore layout <@ layout.font @>)
@@ -332,7 +380,7 @@ type Options() =
332380
Radialaxis |> Option.iter (updatePropertyValueAndIgnore layout <@ layout.radialaxis @>)
333381
Angularaxis |> Option.iter (updatePropertyValueAndIgnore layout <@ layout.angularaxis @>)
334382
Scene |> Option.iter (updatePropertyValueAndIgnore layout <@ layout.scene @>)
335-
Shapes |> Option.iter (updatePropertyValueAndIgnore layout <@ layout.shapes @>)
383+
//Shapes |> Option.iter (updatePropertyValueAndIgnore layout <@ layout.shapes @>)
336384

337385
layout
338386
)
@@ -906,6 +954,43 @@ type Options() =
906954
)
907955

908956

957+
// Applies the styles to Shape()
958+
static member Shape
959+
(
960+
?ShapeType : StyleOption.ShapeType,
961+
?X0 ,
962+
?X1 ,
963+
?Y0 ,
964+
?Y1 ,
965+
?Path ,
966+
?Opacity ,
967+
?Line : LineOptions,
968+
?Fillcolor ,
969+
?Layer :StyleOption.Layer,
970+
?Xref ,
971+
?Yref
972+
973+
974+
) =
975+
(fun (shape:('T :> Shape)) ->
976+
977+
978+
ShapeType |> Option.iter (StyleOption.ShapeType.toString >> shape.set_type)
979+
Xref |> Option.iter shape.set_xref
980+
X0 |> Option.iter shape.set_x0
981+
X1 |> Option.iter shape.set_x1
982+
Yref |> Option.iter shape.set_yref
983+
Y0 |> Option.iter shape.set_y0
984+
Y1 |> Option.iter shape.set_y1
985+
Path |> Option.iter shape.set_path
986+
Opacity |> Option.iter shape.set_opacity
987+
Line |> Option.iter (updatePropertyValueAndIgnore shape <@ shape.line @>)
988+
Fillcolor |> Option.iter shape.set_fillcolor
989+
Layer |> Option.iter (StyleOption.Layer.toString >> shape.set_layer)
990+
// out ->
991+
shape
992+
)
993+
909994

910995

911996

src/FSharp.Plotly/StyleOption.fs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,34 @@ module StyleOption =
506506

507507
static member convert = function
508508
| MinMax (min,max) -> box [|min;max|]
509-
509+
510+
511+
512+
513+
/// Specifies the shape type to be drawn. If "line", a line is drawn from (`x0`,`y0`) to (`x1`,`y1`) If "circle", a circle is drawn from ((`x0`+`x1`)/2, (`y0`+`y1`)/2))
514+
/// with radius (|(`x0`+`x1`)/2 - `x0`|, |(`y0`+`y1`)/2 -`y0`)|) If "rect", a rectangle is drawn linking (`x0`,`y0`), (`x1`,`y0`), (`x1`,`y1`), (`x0`,`y1`), (`x0`,`y0`)
515+
/// If "path", draw a custom SVG path using `path`.
516+
type ShapeType =
517+
| Circle | Rectangle | SvgPath | Line
518+
519+
static member toString = function
520+
| Circle -> "circle"
521+
| Rectangle -> "rect"
522+
| SvgPath -> "path"
523+
| Line -> "line"
524+
525+
526+
static member convert = ShapeType.toString >> box
527+
528+
529+
/// Specifies whether shapes are drawn below or above traces. Default is Above
530+
type Layer =
531+
| Below | Above
532+
533+
static member toString = function
534+
| Below -> "below"
535+
| Above -> "above"
536+
510537

511538

539+
static member convert = Layer.toString >> box

0 commit comments

Comments
 (0)