Skip to content

Commit aa3b605

Browse files
committed
Add Annotation Type and functionality
1 parent db7ce67 commit aa3b605

File tree

5 files changed

+165
-5
lines changed

5 files changed

+165
-5
lines changed

src/FSharp.Plotly/ChartExtensions.fs

+6
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ module ChartExtensions =
240240
(fun (ch:GenericChart) ->
241241
GenericChart.setConfig config ch)
242242

243+
static member withAnnotations(annotations:seq<Annotation>) =
244+
(fun (ch:GenericChart) ->
245+
ch
246+
|> GenericChart.mapLayout
247+
(Layout.style (Annotations = annotations)))
248+
243249
// Set the title of a Chart
244250
static member withTitle(title,?Titlefont) =
245251
(fun (ch:GenericChart) ->

src/FSharp.Plotly/GenericChart.fs

+5
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,8 @@ module GenericChart =
341341
/// Converts from a list of trace objects and a layout object into GenericChart
342342
let ofTraceObjects traces = // layout =
343343
GenericChart.MultiChart(traces, Layout(), Config())
344+
345+
let mapLayout f gChart =
346+
match gChart with
347+
| Chart (trace,layout,config) -> Chart (trace,f layout,config)
348+
| MultiChart (traces,layout,config) -> MultiChart (traces,f layout,config)

src/FSharp.Plotly/Layout.fs

+109-4
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,119 @@ type Margin() =
5858
)
5959

6060

61-
/// Margin
61+
/// Text annotations inside a plot
6262
type Annotation() =
6363
inherit DynamicObj ()
6464

6565
/// Init Annotation type
66-
static member init (applyStyle:Annotation->Annotation) =
67-
Annotation() |> applyStyle
66+
static member init
67+
(
68+
X : float,
69+
Y : float,
70+
?XRef,
71+
?YRef,
72+
?ArrowTailX : float,
73+
?ArrowTailY : float,
74+
?ShowArrow : bool,
75+
?ArrowColor,
76+
?ArrowHead : StyleParam.ArrowHead,
77+
?ArrowSize : float,
78+
?ArrowWidth : float,
79+
?Z : float,
80+
?Text : string,
81+
?TextAngle : float,
82+
?Font : Font,
83+
?Width : float,
84+
?Height : float,
85+
?Opacity : float,
86+
?HorizontalAlign: StyleParam.HorizontalAlign,
87+
?VerticalAlign : StyleParam.VerticalAlign,
88+
?BGColor,
89+
?BorderColor,
90+
?Visible : bool
91+
) =
92+
Annotation()
93+
|> Annotation.style
94+
(
95+
X = X ,
96+
Y = Y ,
97+
?XRef = XRef ,
98+
?YRef = YRef ,
99+
?ArrowTailX = ArrowTailX ,
100+
?ArrowTailY = ArrowTailY ,
101+
?ShowArrow = ShowArrow ,
102+
?ArrowColor = ArrowColor ,
103+
?ArrowHead = ArrowHead ,
104+
?ArrowSize = ArrowSize ,
105+
?ArrowWidth = ArrowWidth ,
106+
?Z = Z ,
107+
?Text = Text ,
108+
?TextAngle = TextAngle ,
109+
?Font = Font ,
110+
?Width = Width ,
111+
?Height = Height ,
112+
?Opacity = Opacity ,
113+
?HorizontalAlign = HorizontalAlign ,
114+
?VerticalAlign = VerticalAlign ,
115+
?BGColor = BGColor ,
116+
?BorderColor = BorderColor ,
117+
?Visible = Visible
118+
)
68119

120+
static member style
121+
(
122+
X : float,
123+
Y : float,
124+
?XRef,
125+
?YRef,
126+
?ArrowTailX : float,
127+
?ArrowTailY : float,
128+
?ShowArrow : bool,
129+
?ArrowColor,
130+
?ArrowHead : StyleParam.ArrowHead,
131+
?ArrowSize : float,
132+
?ArrowWidth : float,
133+
?Z : float,
134+
?Text : string,
135+
?TextAngle : float,
136+
?Font : Font,
137+
?Width : float,
138+
?Height : float,
139+
?Opacity : float,
140+
?HorizontalAlign: StyleParam.HorizontalAlign,
141+
?VerticalAlign : StyleParam.VerticalAlign,
142+
?BGColor,
143+
?BorderColor,
144+
?Visible : bool
145+
146+
) =
147+
(fun (ann:Annotation) ->
148+
X |> DynObj.setValue ann "x"
149+
Y |> DynObj.setValue ann "y"
150+
XRef |> DynObj.setValueOpt ann "xref"
151+
YRef |> DynObj.setValueOpt ann "yref"
152+
ArrowTailX |> DynObj.setValueOpt ann "ax"
153+
ArrowTailY |> DynObj.setValueOpt ann "ay"
154+
ArrowHead |> DynObj.setValueOptBy ann "arrowhead" StyleParam.ArrowHead.convert
155+
ArrowSize |> DynObj.setValueOpt ann "arrowsize"
156+
ArrowWidth |> DynObj.setValueOpt ann "arrowwidth"
157+
ShowArrow |> DynObj.setValueOpt ann "showarrow"
158+
ArrowColor |> DynObj.setValueOpt ann "arrowcolor"
159+
Z |> DynObj.setValueOpt ann "z"
160+
Text |> DynObj.setValueOpt ann "text"
161+
TextAngle |> DynObj.setValueOpt ann "textangle"
162+
Font |> DynObj.setValueOpt ann "font"
163+
Width |> DynObj.setValueOpt ann "width"
164+
Height |> DynObj.setValueOpt ann "height"
165+
Opacity |> DynObj.setValueOpt ann "opacity"
166+
HorizontalAlign |> DynObj.setValueOptBy ann "align" StyleParam.HorizontalAlign.convert
167+
VerticalAlign |> DynObj.setValueOptBy ann "valign" StyleParam.VerticalAlign.convert
168+
BGColor |> DynObj.setValueOpt ann "bgcolor"
169+
BorderColor |> DynObj.setValueOpt ann "bordercolor"
170+
Visible |> DynObj.setValueOpt ann "visible"
171+
172+
ann
173+
)
69174

70175
/// Layout
71176
type Layout() =
@@ -84,7 +189,7 @@ type Layout() =
84189
//?xAxis : Axis.LinearAxis,
85190
//?yAxis : Axis.LinearAxis,
86191
?Legend ,
87-
?Annotations : Annotation ,
192+
?Annotations : seq<Annotation> ,
88193
?Margin ,
89194

90195
?Paper_bgcolor ,

src/FSharp.Plotly/StyleParams.fs

+32-1
Original file line numberDiff line numberDiff line change
@@ -786,4 +786,35 @@ module StyleParam =
786786
| PNG -> "png"
787787
| JPEG -> "jpeg"
788788

789-
static member convert = ImageFormat.toString >> box
789+
static member convert = ImageFormat.toString >> box
790+
791+
type HorizontalAlign =
792+
|Left |Center |Right
793+
static member toString = function
794+
| Left -> "left"
795+
| Center-> "center"
796+
| Right -> "right"
797+
798+
static member convert = HorizontalAlign.toString >> box
799+
800+
type VerticalAlign =
801+
|Top |Middle |Bottom
802+
static member toString = function
803+
| Top -> "top"
804+
| Middle -> "middle"
805+
| Bottom -> "bottom"
806+
807+
static member convert = VerticalAlign.toString >> box
808+
809+
type ArrowHead =
810+
|TriangleShort |TriangleTall |Barbed |SimpleShort |SimpleTall |Cirle |Square |LineOnly
811+
static member toEnum = function
812+
|TriangleShort -> 1
813+
|TriangleTall -> 2
814+
|Barbed -> 3
815+
|SimpleShort -> 4
816+
|SimpleTall -> 5
817+
|Cirle -> 6
818+
|Square -> 7
819+
|LineOnly -> 8
820+
static member convert = ArrowHead.toEnum >> box

src/FSharp.Plotly/TestScript.fsx

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ let dims' =
4646
Chart.ParallelCategories(dims=dims',Color=[0.;1.;0.;1.;0.;0.;0.],Colorscale = StyleParam.Colorscale.Blackbody)
4747
|> Chart.Show
4848

49+
let data3d = List.zip3 [0 .. 15] [0 .. 15] [0 .. 15]
50+
let data2d = List.zip [0 .. 15] [0 .. 15]
51+
52+
[
53+
Chart.Point (data2d)
54+
Chart.Scatter3d(xyz=data3d,mode = StyleParam.Mode.Markers)
55+
Chart.Scatter3d(xyz=data3d,mode = StyleParam.Mode.Markers)
56+
Chart.Scatter3d(xyz=data3d,mode = StyleParam.Mode.Markers)
57+
Chart.Scatter3d(xyz=data3d,mode = StyleParam.Mode.Markers)
58+
]
59+
|> Chart.Stack 2
60+
|> Chart.Show
61+
4962

5063

5164

0 commit comments

Comments
 (0)