Skip to content

Commit a96af40

Browse files
committed
#63: Add Legend creation properties and related functions
1 parent 4c924b0 commit a96af40

File tree

8 files changed

+192
-18
lines changed

8 files changed

+192
-18
lines changed

src/Plotly.NET/ChartExtensions.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ module ChartExtensions =
381381
|> Layout.SetLayoutGrid layoutGrid
382382
GenericChart.setLayout layout ch)
383383

384+
// Set the LayoutGrid options of a Chart
385+
[<CompiledName("WithLegend")>]
386+
static member withLegend(legend:Legend) =
387+
(fun (ch:GenericChart) ->
388+
let layout =
389+
GenericChart.getLayout ch
390+
|> Layout.setLegend legend
391+
GenericChart.setLayout layout ch)
392+
384393
/// Sets a map for the given chart (will only work with traces supporting geo, e.g. choropleth, scattergeo)
385394
[<CompiledName("WithMap")>]
386395
static member withMap(map:Geo,[<Optional;DefaultParameterValue(null)>] ?Id ) =

src/Plotly.NET/GenericChart.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,11 @@ module GenericChart =
381381
| Chart (_) -> 1
382382
| MultiChart (traces,_,_) -> traces |> Seq.length
383383

384-
/// Creates a new GenericChart whose traces are the results of applying the given function to each of the trace of the GenericChart.
385-
let existsTrace (f:Trace->bool) gChart =
384+
/// Returns true if the given chart contains a trace for which the predicate function returns true
385+
let existsTrace (predicate: Trace -> bool) gChart =
386386
match gChart with
387-
| Chart (trace,_,_) -> f trace
388-
| MultiChart (traces,_,_) -> traces |> List.exists f
387+
| Chart (trace,_,_) -> predicate trace
388+
| MultiChart (traces,_,_) -> traces |> List.exists predicate
389389

390390
/// Converts from a trace object and a layout object into GenericChart
391391
let ofTraceObject trace = //layout =
@@ -395,6 +395,7 @@ module GenericChart =
395395
let ofTraceObjects traces = // layout =
396396
GenericChart.MultiChart(traces, Layout(), Config())
397397

398+
///
398399
let mapLayout f gChart =
399400
match gChart with
400401
| Chart (trace,layout,config) -> Chart (trace,f layout,config)

src/Plotly.NET/GenericChartExtensions.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ module GenericChartExtensions =
318318
|> Layout.SetLayoutGrid layoutGrid
319319
GenericChart.setLayout layout this
320320

321+
// Set the LayoutGrid options of a Chart
322+
[<CompiledName("WithLegend")>]
323+
member this.WithLegend(legend:Legend) =
324+
let layout =
325+
GenericChart.getLayout this
326+
|> Layout.setLegend legend
327+
GenericChart.setLayout layout this
328+
321329
/// Sets a map for the given chart (will only work with traces supporting geo, e.g. choropleth, scattergeo)
322330
[<CompiledName("WithMap")>]
323331
[<Extension>]

src/Plotly.NET/Layout.fs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
namespace Plotly.NET
22

33

4-
/// Legend
5-
type Legend() =
6-
inherit DynamicObj ()
7-
8-
/// Init Legend type
9-
static member init (applyStyle:Legend->Legend) =
10-
Legend() |> applyStyle
11-
124
/// Margin
135
type Margin() =
146
inherit DynamicObj ()
@@ -188,7 +180,7 @@ type Layout() =
188180
?Height : float ,
189181
//?xAxis : Axis.LinearAxis,
190182
//?yAxis : Axis.LinearAxis,
191-
?Legend ,
183+
?Legend : Legend,
192184
?Annotations : seq<Annotation> ,
193185
?Margin ,
194186

@@ -440,3 +432,9 @@ type Layout() =
440432

441433
layout
442434
)
435+
436+
static member setLegend(legend:Legend) =
437+
(fun (layout:Layout) ->
438+
legend |> DynObj.setValue layout "legend"
439+
layout
440+
)

src/Plotly.NET/Legend.fs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace Plotly.NET
2+
3+
/// Legend
4+
type Legend() =
5+
inherit DynamicObj ()
6+
7+
/// Init Legend type
8+
static member init
9+
(
10+
?BGColor,
11+
?BorderColor,
12+
?Borderwidth,
13+
?Orientation: StyleParam.Orientation,
14+
?TraceOrder: StyleParam.TraceOrder,
15+
?TraceGroupGap: float,
16+
?ItemSizing: StyleParam.TraceItemSizing,
17+
?ItemWidth: int,
18+
?ItemClick: StyleParam.TraceItemClickOptions,
19+
?ItemDoubleClick: StyleParam.TraceItemClickOptions,
20+
?X,
21+
?XAnchor: StyleParam.LegendXAnchorPosition,
22+
?Y,
23+
?YAnchor: StyleParam.LegendYAnchorPosition,
24+
?VerticalAlign : StyleParam.VerticalAlign,
25+
?Title: string
26+
) =
27+
Legend()
28+
|> Legend.style(
29+
?BGColor = BGColor ,
30+
?BorderColor = BorderColor ,
31+
?Borderwidth = Borderwidth ,
32+
?TraceGroupGap = TraceGroupGap ,
33+
?ItemWidth = ItemWidth ,
34+
?X = X ,
35+
?Y = Y ,
36+
?Title = Title ,
37+
?Orientation = Orientation ,
38+
?TraceOrder = TraceOrder ,
39+
?ItemSizing = ItemSizing ,
40+
?ItemClick = ItemClick ,
41+
?ItemDoubleClick = ItemDoubleClick,
42+
?XAnchor = XAnchor ,
43+
?YAnchor = YAnchor ,
44+
?VerticalAlign = VerticalAlign
45+
)
46+
47+
static member style
48+
(
49+
?BGColor,
50+
?BorderColor,
51+
?Borderwidth,
52+
?Orientation: StyleParam.Orientation,
53+
?TraceOrder: StyleParam.TraceOrder,
54+
?TraceGroupGap: float,
55+
?ItemSizing: StyleParam.TraceItemSizing,
56+
?ItemWidth: int,
57+
?ItemClick: StyleParam.TraceItemClickOptions,
58+
?ItemDoubleClick: StyleParam.TraceItemClickOptions,
59+
?X,
60+
?XAnchor: StyleParam.LegendXAnchorPosition,
61+
?Y,
62+
?YAnchor: StyleParam.LegendYAnchorPosition,
63+
?VerticalAlign : StyleParam.VerticalAlign,
64+
?Title: string
65+
) =
66+
(fun (legend:Legend) ->
67+
BGColor |> DynObj.setValueOpt legend "bgcolor"
68+
BorderColor |> DynObj.setValueOpt legend "bordercolor"
69+
Borderwidth |> DynObj.setValueOpt legend "borderwidth"
70+
TraceGroupGap |> DynObj.setValueOpt legend "tracegroupgap"
71+
ItemWidth |> DynObj.setValueOpt legend "itemwidth"
72+
X |> DynObj.setValueOpt legend "x"
73+
Y |> DynObj.setValueOpt legend "y"
74+
Title |> DynObj.setValueOpt legend "Title"
75+
76+
Orientation |> DynObj.setValueOptBy legend "orientation" StyleParam.Orientation.convert
77+
TraceOrder |> DynObj.setValueOptBy legend "traceorder" StyleParam.TraceOrder.convert
78+
ItemSizing |> DynObj.setValueOptBy legend "itemsizing" StyleParam.TraceItemSizing.convert
79+
ItemClick |> DynObj.setValueOptBy legend "itemclick" StyleParam.TraceItemClickOptions.convert
80+
ItemDoubleClick |> DynObj.setValueOptBy legend "itemdoubleclick" StyleParam.TraceItemClickOptions.convert
81+
XAnchor |> DynObj.setValueOptBy legend "yanchor" StyleParam.LegendXAnchorPosition.convert
82+
YAnchor |> DynObj.setValueOptBy legend "yanchor" StyleParam.LegendYAnchorPosition.convert
83+
VerticalAlign |> DynObj.setValueOptBy legend "valign" StyleParam.VerticalAlign.convert
84+
85+
legend
86+
)

src/Plotly.NET/Playground.fsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#load "Colorbar.fs"
1010
#load "RangeSlider.fs"
1111
#load "Light.fs"
12+
#load "Legend.fs"
1213
#load "Contours.fs"
1314
#load "Dimensions.fs"
1415
#load "Domain.fs"
@@ -49,6 +50,11 @@ let myTemplate =
4950
ChartTemplates.dark
5051
|> Template.withColorWay ChartTemplates.ColorWays.plotly
5152

53+
let myLegend =
54+
Legend.init(
55+
Orientation = StyleParam.Orientation.Horizontal
56+
)
57+
5258
//F# functional pipeline to compose a chart with functions
5359
//
5460
[
@@ -66,10 +72,7 @@ let myTemplate =
6672
]
6773
|> List.map Chart.Line
6874
|> Chart.Combine
69-
|> Chart.withTraceName("Hello from F#",Showlegend=true)
70-
//|> Chart.withY_AxisStyle("xAxis",Showline=true)
71-
//|> Chart.withX_AxisStyle("yAxis",Showline=true)
72-
|> Chart.withTemplate ChartTemplates.fslab
75+
|> Chart.withLegend(myLegend)
7376
|> Chart.Show
7477

7578

src/Plotly.NET/Plotly.NET.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<FsDocsReleaseNotesLink>https://github.com/plotly/Plotly.NET/blob/dev/RELEASE_NOTES.md</FsDocsReleaseNotesLink>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<None Include="..\..\docs\img\logo.png" Pack="true" PackagePath="\"/>
35+
<None Include="..\..\docs\img\logo.png" Pack="true" PackagePath="\" />
3636
<Compile Include="AssemblyInfo.fs" />
3737
<Compile Include="StyleParams.fs" />
3838
<Compile Include="DynamicObj.fs" />
@@ -45,6 +45,7 @@
4545
<Compile Include="Colorbar.fs" />
4646
<Compile Include="RangeSlider.fs" />
4747
<Compile Include="Light.fs" />
48+
<Compile Include="Legend.fs" />
4849
<Compile Include="Contours.fs" />
4950
<Compile Include="Dimensions.fs" />
5051
<Compile Include="Domain.fs" />

src/Plotly.NET/StyleParams.fs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,35 @@ module StyleParam =
669669

670670
static member convert = LayoutGridYSide.toString >> box
671671

672+
[<RequireQualifiedAccess>]
673+
type LegendXAnchorPosition =
674+
| Auto
675+
| Left
676+
| Center
677+
| Right
678+
679+
static member toString = function
680+
| Auto -> "auto"
681+
| Left -> "left"
682+
| Center -> "center"
683+
| Right -> "right"
684+
685+
static member convert = LegendXAnchorPosition.toString >> box
672686

687+
[<RequireQualifiedAccess>]
688+
type LegendYAnchorPosition =
689+
| Auto
690+
| Top
691+
| Middle
692+
| Bottom
693+
694+
static member toString = function
695+
| Auto -> "auto"
696+
| Top -> "top"
697+
| Middle -> "middle"
698+
| Bottom -> "bottom"
699+
700+
static member convert = LegendYAnchorPosition.toString >> box
673701

674702
//--------------------------
675703
// #M#
@@ -1086,6 +1114,46 @@ module StyleParam =
10861114

10871115
static member convert = TreemapTilingPacking.toString >> box
10881116

1117+
[<RequireQualifiedAccess>]
1118+
type TraceOrder =
1119+
| Normal
1120+
| Reversed
1121+
| Grouped
1122+
| ReversedGrouped
1123+
1124+
static member toString = function
1125+
| Normal -> "normal"
1126+
| Reversed -> "reversed"
1127+
| Grouped -> "grouped"
1128+
| ReversedGrouped -> "reversed+grouped"
1129+
1130+
static member convert = TraceOrder.toString >> box
1131+
1132+
[<RequireQualifiedAccess>]
1133+
type TraceItemSizing =
1134+
| Trace
1135+
| Constant
1136+
1137+
static member toString = function
1138+
| Trace -> "trace"
1139+
| Constant -> "constant"
1140+
1141+
static member convert = TraceItemSizing.toString >> box
1142+
1143+
[<RequireQualifiedAccess>]
1144+
type TraceItemClickOptions =
1145+
| Toggle
1146+
| ToggleOthers
1147+
| False
1148+
1149+
static member toString = function
1150+
| Toggle -> "toggle"
1151+
| ToggleOthers -> "toggleothers"
1152+
| False -> "False"
1153+
1154+
static member convert = TraceItemClickOptions.toString >> box
1155+
1156+
10891157
//--------------------------
10901158
// #U#
10911159
//--------------------------

0 commit comments

Comments
 (0)