Skip to content

Commit 86cf5f6

Browse files
committed
Add functions to get and update polar/polar axis on chart layout
1 parent a442587 commit 86cf5f6

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

src/Plotly.NET/ChartExtensions.fs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,4 +1144,59 @@ module ChartExtensions =
11441144
let file = sprintf "%s.html" guid
11451145
let path = Path.Combine(tempPath, file)
11461146
File.WriteAllText(path, html)
1147-
path |> openOsSpecificFile
1147+
path |> openOsSpecificFile
1148+
1149+
/// Sets the polar object with the given id on the chart layout
1150+
[<CompiledName("WithPolar")>]
1151+
static member withPolar(polar:Polar, [<Optional;DefaultParameterValue(null)>] ?Id) =
1152+
(fun (ch:GenericChart) ->
1153+
let layout =
1154+
let id = defaultArg Id 1
1155+
GenericChart.getLayout ch
1156+
|> Layout.updatePolarById(id,polar)
1157+
GenericChart.setLayout layout ch
1158+
)
1159+
1160+
1161+
/// Sets the angular axis of the polar object with the given id on the chart layout
1162+
[<CompiledName("WithAngularAxis")>]
1163+
static member withAngularAxis(angularAxis:Axis.AngularAxis, [<Optional;DefaultParameterValue(null)>] ?Id) =
1164+
(fun (ch:GenericChart) ->
1165+
1166+
let id = defaultArg Id 1
1167+
let layout = GenericChart.getLayout ch
1168+
1169+
let updatedPolar =
1170+
layout
1171+
|> Layout.tryGetPolarById(id)
1172+
|> Option.defaultValue (Polar.init())
1173+
|> Polar.style(AngularAxis = angularAxis)
1174+
1175+
let updatedLayout =
1176+
layout
1177+
|> Layout.updatePolarById(id,updatedPolar)
1178+
1179+
GenericChart.setLayout updatedLayout ch
1180+
)
1181+
1182+
/// Sets the radial axis of the polar object with the given id on the chart layout
1183+
[<CompiledName("WithRadialAxis")>]
1184+
static member withRadialAxis(radialAxis:Axis.RadialAxis, [<Optional;DefaultParameterValue(null)>] ?Id) =
1185+
(fun (ch:GenericChart) ->
1186+
let id = defaultArg Id 1
1187+
let layout = GenericChart.getLayout ch
1188+
1189+
let updatedPolar =
1190+
layout
1191+
|> Layout.tryGetPolarById(id)
1192+
|> Option.defaultValue (Polar.init())
1193+
|> Polar.style(RadialAxis = radialAxis)
1194+
1195+
let updatedLayout =
1196+
layout
1197+
|> Layout.updatePolarById(id,updatedPolar)
1198+
1199+
GenericChart.setLayout updatedLayout ch
1200+
)
1201+
1202+

src/Plotly.NET/Layout.fs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,34 @@ type Layout() =
336336
layout
337337
)
338338

339+
static member tryGetPolarById (id:int) =
340+
(fun (layout:Layout) ->
341+
let key = if id < 2 then "polar" else sprintf "polar%i" id
342+
layout.TryGetTypedValue<Polar>(key)
343+
)
344+
345+
/// Updates the style of current polar object with given Id.
346+
/// If there does not exist a polar object with the given id, sets it with the given polar object
347+
static member updatePolarById
348+
(
349+
id : int,
350+
polar : Polar
351+
) =
352+
(fun (layout:Layout) ->
353+
354+
let key = if id < 2 then "polar" else sprintf "polar%i" id
355+
356+
let polar' =
357+
match layout |> Layout.tryGetPolarById(id) with
358+
| Some a -> DynObj.combine (unbox a) polar
359+
| None -> polar :> DynamicObj
360+
361+
polar' |> DynObj.setValue layout key
362+
363+
layout
364+
)
365+
366+
339367
static member setLegend(legend:Legend) =
340368
(fun (layout:Layout) ->
341369
legend |> DynObj.setValue layout "legend"

src/Plotly.NET/Polar.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ type Polar () =
6363
(fun (polar:Polar) ->
6464

6565
Domain |> DynObj.setValueOpt polar "domain"
66-
Sector |> DynObj.setValueOpt polar "sector"
66+
Sector |> DynObj.setValueOptBy polar "sector" (fun (a,b) -> [|a;b|])
6767
Hole |> DynObj.setValueOpt polar "hole"
6868
BGColor |> DynObj.setValueOpt polar "bgcolor"
6969
RadialAxis |> DynObj.setValueOpt polar "radialaxis"
7070
AngularAxis |> DynObj.setValueOpt polar "angularaxis"
71-
GridShape |> DynObj.setValueOpt polar "gridshape"
71+
GridShape |> DynObj.setValueOptBy polar "gridshape" StyleParam.PolarGridShape.convert
7272
UIRevision |> DynObj.setValueOpt polar "uirevision"
7373

7474
polar

0 commit comments

Comments
 (0)