Skip to content

Commit 3dc5c32

Browse files
committed
Refactor Config creation andadd convenience functions to ChartA API
1 parent fbf9892 commit 3dc5c32

File tree

8 files changed

+299
-56
lines changed

8 files changed

+299
-56
lines changed

docs/00_3_chart-config.fsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ You can define fields that can be edited on the chart by setting `Editable = tru
105105
let editableConfig =
106106
Config.init(
107107
Editable = true,
108-
EditableAnnotations = [
109-
StyleParam.AnnotationEditOptions.LegendPosition
110-
StyleParam.AnnotationEditOptions.AxisTitleText
111-
StyleParam.AnnotationEditOptions.LegendText
112-
]
108+
Edits = Edits.init(
109+
LegendPosition = true,
110+
AxisTitleText = true,
111+
LegendText = true
112+
)
113113
)
114114

115115
let editablePlot =

src/Plotly.NET/ChartAPI/Chart.fs

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Plotly.NET
22

33
open Plotly.NET.LayoutObjects
44
open Plotly.NET.TraceObjects
5+
open Plotly.NET.ConfigObjects
56

67
open DynamicObj
78
open System
@@ -18,7 +19,7 @@ type Chart =
1819
//==============================================================================================================
1920

2021
/// <summary>
21-
/// Saves the given Chart as html file at the given path (.html file extension is added if not present).
22+
/// Saves the given Chart as html file at the given path (.html file extension is added if not present).
2223
/// Optionally opens the generated file in the browser.
2324
/// </summary>
2425
/// <param name="path">The path to save the chart html at.</param>
@@ -55,7 +56,7 @@ type Chart =
5556
/// Saves the given chart as a temporary html file containing a static image of the chart and opens it in the browser.
5657
///
5758
/// IMPORTANT: this is not the same as static image generation. The file still needs to be opened in the browser to generate the image, as it is done via a js script in the html.
58-
///
59+
///
5960
/// For real programmatic static image export use Plotly.NET.ImageExport (https://www.nuget.org/packages/Plotly.NET.ImageExport/)
6061
///
6162
/// This yields basically the same results as using `StaticPlot = true` for the chart's config.
@@ -729,11 +730,19 @@ type Chart =
729730
//======================================= General Layout object styling ========================================
730731
//==============================================================================================================
731732

733+
// <summary>
734+
/// Sets the given layout on the input chart.
735+
///
736+
/// If there is already an layout set, the object is replaced.
737+
/// </summary>
738+
[<CompiledName("SetLayout")>]
739+
static member setLayout(layout: Layout) =
740+
(fun (ch: GenericChart) -> GenericChart.setLayout layout ch)
732741

733742
/// <summary>
734743
/// Sets the given layout on the input chart.
735744
///
736-
/// If there is already an layout set at the given id, the axis objects are combined.
745+
/// If there is already an layout set, the objects are combined.
737746
/// </summary>
738747
[<CompiledName("WithLayout")>]
739748
static member withLayout(layout: Layout) =
@@ -2391,10 +2400,6 @@ type Chart =
23912400

23922401
ch |> Chart.withLegend legend)
23932402

2394-
[<CompiledName("WithConfig")>]
2395-
static member withConfig(config: Config) =
2396-
(fun (ch: GenericChart) -> GenericChart.setConfig config ch)
2397-
23982403
/// <summary>
23992404
///
24002405
/// </summary>
@@ -2547,6 +2552,75 @@ type Chart =
25472552
Chart.withShapes ([ shape ], ?Append = Append)
25482553

25492554

2555+
//==============================================================================================================
2556+
//======================================= General Config object styling ========================================
2557+
//==============================================================================================================
2558+
2559+
// <summary>
2560+
/// Sets the given config on the input chart.
2561+
///
2562+
/// If there is already a config set, the object is replaced.
2563+
/// </summary>
2564+
[<CompiledName("SetConfig")>]
2565+
static member setConfig(config: Config) =
2566+
(fun (ch: GenericChart) -> GenericChart.setConfig config ch)
2567+
2568+
// <summary>
2569+
/// Sets the given config on the input chart.
2570+
///
2571+
/// If there is already a config set, the objects are combined.
2572+
/// </summary>
2573+
[<CompiledName("WithConfig")>]
2574+
static member withConfig(config: Config) =
2575+
(fun (ch: GenericChart) -> GenericChart.addConfig config ch)
2576+
2577+
/// <summary>
2578+
/// Applies the given styles to the chart's Config object. Overwrites attributes with the same name that are already set.
2579+
/// </summary>
2580+
/// <param name="StaticPlot">Determines whether the plot is interactive or not. (default: false)</param>
2581+
/// <param name="PlotlyServerUrl">When set it determines base URL for the 'Edit in Chart Studio'/`showEditInChartStudio`/`showSendToCloud` mode bar button', and the showLink/sendData on-graph link. To enable sending your data to Chart Studio Cloud, you need to' set both `plotlyServerURL` to \'https://chart-studio.plotly.com\' and also set `showSendToCloud` to true.</param>
2582+
/// <param name="Editable">Determines whether the graph is editable or not. Sets all pieces of `edits` unless a separate `edits` config item overrides individual parts.</param>
2583+
/// <param name="Edits">Object holding individual editable pieces of the graph.</param>
2584+
/// <param name="Autosizable">Determines whether the graphs are plotted with respect to layout.autosize:true and infer its container size. (default: false)</param>
2585+
/// <param name="Responsive">Determines whether to change the layout size when window is resized.</param>
2586+
/// <param name="ShowSendToCloud">Should we include a ModeBar button, labeled "Edit in Chart Studio",that sends this chart to chart-studio.plotly.com (formerly plot.ly) or another plotly server as specified by `plotlyServerURL` for editing, export, etc? Note that this button can (depending on `plotlyServerURL` being set) send your data to an external server. However that server does not persist your data until you arrive at the Chart Studio and explicitly click "Save".</param>
2587+
/// <param name="ShowEditInChartStudio">Same as `showSendToCloud`, but use a pencil icon instead of a floppy-disk. Note that if both `showSendToCloud` and `showEditInChartStudio` are turned, only `showEditInChartStudio` will be honored.</param>
2588+
/// <param name="ToImageButtonOptions">Statically override options for toImage modebar button</param>
2589+
/// <param name="ModeBarButtonsToAdd">ModeBar buttons to add to the graph.</param>
2590+
[<CompiledName("WithConfigStyle")>]
2591+
static member withConfigStyle
2592+
(
2593+
[<Optional; DefaultParameterValue(null)>] ?StaticPlot: bool,
2594+
[<Optional; DefaultParameterValue(null)>] ?PlotlyServerUrl: string,
2595+
[<Optional; DefaultParameterValue(null)>] ?Autosizable: bool,
2596+
[<Optional; DefaultParameterValue(null)>] ?Editable: bool,
2597+
[<Optional; DefaultParameterValue(null)>] ?Edits: Edits,
2598+
[<Optional; DefaultParameterValue(null)>] ?ShowSendToCloud: bool,
2599+
[<Optional; DefaultParameterValue(null)>] ?ShowEditInChartStudio: bool,
2600+
[<Optional; DefaultParameterValue(null)>] ?Responsive: bool,
2601+
[<Optional; DefaultParameterValue(null)>] ?ToImageButtonOptions: ToImageButtonOptions,
2602+
[<Optional; DefaultParameterValue(null)>] ?ModeBarButtonsToAdd: seq<StyleParam.ModeBarButton>
2603+
) =
2604+
(fun (ch: GenericChart) ->
2605+
2606+
let config =
2607+
Config.init (
2608+
?StaticPlot = StaticPlot,
2609+
?PlotlyServerUrl = PlotlyServerUrl,
2610+
?Autosizable = Autosizable,
2611+
?Responsive = Responsive,
2612+
?ToImageButtonOptions = ToImageButtonOptions,
2613+
?ShowSendToCloud = ShowSendToCloud,
2614+
?ShowEditInChartStudio = ShowEditInChartStudio,
2615+
?Editable = Editable,
2616+
?Edits = Edits,
2617+
?ModeBarButtonsToAdd = ModeBarButtonsToAdd
2618+
)
2619+
2620+
ch |> Chart.withConfig config)
2621+
2622+
2623+
25502624
//==============================================================================================================
25512625
//================================= More complicated composite methods =========================================
25522626
//==============================================================================================================

src/Plotly.NET/ChartAPI/GenericChart.fs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ module GenericChart =
199199
| Chart (t, l, _, d) -> Chart(t, l, config, d)
200200
| MultiChart (t, l, _, d) -> MultiChart(t, l, config, d)
201201

202+
let addConfig config gChart =
203+
match gChart with
204+
| Chart (trace, l, c', d) -> Chart(trace, l, (DynObj.combine c' config |> unbox), d)
205+
| MultiChart (traces, l, c', d) -> MultiChart(traces, l, (DynObj.combine c' config |> unbox), d)
206+
202207
let getDisplayOptions gChart =
203208
match gChart with
204209
| Chart (_, _, _, d) -> d
@@ -276,19 +281,16 @@ module GenericChart =
276281

277282
let combineConfigs (first: Config) (second: Config) =
278283

279-
let editableAnnotations =
280-
combineOptSeqs
281-
(first.TryGetTypedValue<seq<StyleParam.AnnotationEditOptions>>("editable"))
282-
(second.TryGetTypedValue<seq<StyleParam.AnnotationEditOptions>>("editable"))
283-
284284
let modeBarButtonsToAdd =
285285
combineOptSeqs
286-
(first.TryGetTypedValue<seq<StyleParam.ModeBarButton>>("modeBarButtonsToAdd"))
287-
(second.TryGetTypedValue<seq<StyleParam.ModeBarButton>>("modeBarButtonsToAdd"))
286+
(first.TryGetTypedValue<seq<string>>("modeBarButtonsToAdd"))
287+
(second.TryGetTypedValue<seq<string>>("modeBarButtonsToAdd"))
288288

289289
DynObj.combine first second
290290
|> unbox
291-
|> Config.style (?EditableAnnotations = editableAnnotations, ?ModeBarButtonsToAdd = modeBarButtonsToAdd)
291+
|> Config.style (
292+
?ModeBarButtonsToAdd = (modeBarButtonsToAdd |> Option.map (Seq.map StyleParam.ModeBarButton.ofString))
293+
)
292294

293295
let combineDisplayOptions (first: DisplayOptions) (second: DisplayOptions) =
294296

src/Plotly.NET/CommonAbstractions/StyleParams.fs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ module StyleParam =
224224

225225
[<RequireQualifiedAccess>]
226226
/// Editable parts of a chart that can be set via Chart config.
227-
type AnnotationEditOptions =
227+
type Edits =
228228
///Determines if the main anchor of the annotation is editable.The main anchor corresponds to the',
229229
///text (if no arrow) or the arrow (which drags the whole thing leaving the arrow length & direction unchanged).
230230
| AnnotationPosition
@@ -257,9 +257,9 @@ module StyleParam =
257257
| LegendText -> "legendText"
258258
| ShapePosition -> "shapePosition"
259259

260-
static member convert = AnnotationEditOptions.toString >> box
261-
override this.ToString() = this |> AnnotationEditOptions.toString
262-
member this.Convert() = this |> AnnotationEditOptions.convert
260+
static member convert = Edits.toString >> box
261+
override this.ToString() = this |> Edits.toString
262+
member this.Convert() = this |> Edits.convert
263263

264264
[<RequireQualifiedAccess>]
265265
type AutoTypeNumbers =
@@ -1875,6 +1875,49 @@ module StyleParam =
18751875
| ZoomInMapbox -> "zoomInMapbox"
18761876
| ZoomOutMapbox -> "zoomOutMapbox"
18771877

1878+
static member ofString =
1879+
function
1880+
| "toImage" -> ToImage
1881+
| "sendDataToCloud" -> SendDataToCloud
1882+
| "editInChartStudio" -> EditInChartStudio
1883+
| "zoom2d" -> Zoom2d
1884+
| "pan2d" -> Pan2d
1885+
| "select2d" -> Select2d
1886+
| "lasso2d" -> Lasso2d
1887+
| "drawclosedpath" -> DrawClosedPath
1888+
| "drawopenpath" -> DrawOpenPath
1889+
| "drawline" -> DrawLine
1890+
| "drawrect" -> DrawRect
1891+
| "drawcircle" -> DrawCircle
1892+
| "eraseshape" -> EraseShape
1893+
| "zoomIn2d" -> ZoomIn2d
1894+
| "zoomOut2d" -> ZoomOut2d
1895+
| "autoScale2d" -> AutoScale2d
1896+
| "resetScale2d" -> ResetScale2d
1897+
| "hoverClosestCartesian" -> HoverClosestCartesian
1898+
| "hoverCompareCartesian" -> HoverCompareCartesian
1899+
| "zoom3d" -> Zoom3d
1900+
| "pan3d" -> Pan3d
1901+
| "orbitRotation" -> OrbitRotation
1902+
| "tableRotation" -> TableRotation
1903+
| "resetCameraDefault3d" -> ResetCameraDefault3d
1904+
| "resetCameraLastSave3d" -> ResetCameraLastSave3d
1905+
| "hoverClosest3d" -> HoverClosest3d
1906+
| "zoomInGeo" -> ZoomInGeo
1907+
| "zoomOutGeo" -> ZoomOutGeo
1908+
| "resetGeo" -> ResetGeo
1909+
| "hoverClosestGeo" -> HoverClosestGeo
1910+
| "hoverClosestGl2d" -> HoverClosestGl2d
1911+
| "hoverClosestPie" -> HoverClosestPie
1912+
| "resetSankeyGroup" -> ResetSankeyGroup
1913+
| "toggleHover" -> ToggleHover
1914+
| "v1hovermode" -> HoverMode
1915+
| "resetViews" -> ResetViews
1916+
| "toggleSpikelines" -> ToggleSpikelines
1917+
| "resetViewMapbox" -> ResetViewMapbox
1918+
| "zoomInMapbox" -> ZoomInMapbox
1919+
| "zoomOutMapbox" -> ZoomOutMapbox
1920+
| _ -> failwith "unsupported mode button"
18781921

18791922
static member convert = ModeBarButton.toString >> box
18801923
override this.ToString() = this |> ModeBarButton.toString

0 commit comments

Comments
 (0)