-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathlegends.fsx
196 lines (158 loc) · 5.43 KB
/
legends.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(**
---
title: Legends
category: Chart Layout
categoryindex: 2
index: 9
---
*)
(*** hide ***)
(*** condition: prepare ***)
#r "nuget: Newtonsoft.JSON, 13.0.1"
#r "nuget: DynamicObj, 2.0.0"
#r "nuget: Giraffe.ViewEngine.StrongName, 2.0.0-alpha1"
#r "../../src/Plotly.NET/bin/Release/netstandard2.0/Plotly.NET.dll"
Plotly.NET.Defaults.DefaultDisplayOptions <-
Plotly.NET.DisplayOptions.init (PlotlyJSReference = Plotly.NET.PlotlyJSReference.NoReference)
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: Plotly.NET, {{fsdocs-package-version}}"
#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}"
#endif // IPYNB
(**
# Legends
[](https://mybinder.org/v2/gh/plotly/plotly.net/gh-pages?urlpath=/tree/home/jovyan/{{fsdocs-source-basename}}.ipynb) 
[]({{root}}{{fsdocs-source-basename}}.ipynb)
*Summary:* This example shows how to create Legends and add them to the Charts in F#.
Let's first create some data for the purpose of creating example charts:
*)
open Plotly.NET
let x = [ 1.; 2.; 3.; 4.; 5.; 6.; 7.; 8.; 9.; 10. ]
let y = [ 2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1. ]
// note that legends are not shown on charts with only one trace,
// which is why we need to set the trace to visible manually on this chart
let simple_chart = Chart.Point(x, y, ShowLegend = true, Name = "test_chart")
(**
## Creating a legend
Legends are `LayoutObjects` that can be added to a chart's `Layout`. The `LayoutLegend.init` function is used to create a legend object, which can then be added to a chart using the `Chart.withLegend` function:
*)
open Plotly.NET.LayoutObjects
let my_legend =
Legend.init (
Title = Title.init("my first legend!"),
BorderColor = Color.fromString "black",
BorderWidth = 1
)
let first_legend_chart =
simple_chart
|> Chart.withLegend my_legend
(*** condition: ipynb ***)
#if IPYNB
first_legend_chart
#endif // IPYNB
(***hide***)
first_legend_chart |> GenericChart.toChartHTML
(***include-it-raw***)
(**
## Styling existing legends
The `Chart.withLegendStyle` function can be used to update the style of an existing legend.
The following code will move the existing legend to the center bottom of the chart:
*)
let styled_legend_chart =
first_legend_chart
|> Chart.withLegendStyle(
Orientation = StyleParam.Orientation.Horizontal,
X = 0.5,
XAnchor = StyleParam.XAnchorPosition.Center
)
(*** condition: ipynb ***)
#if IPYNB
styled_legend_chart
#endif // IPYNB
(***hide***)
styled_legend_chart |> GenericChart.toChartHTML
(***include-it-raw***)
(**
## Grouping legend items
You can group multiple traces as a single legend item by setting the `LegendGroup` property of the individual traces to the same value:
*)
let grouped_legend_chart =
[
Chart.Point(x, y)
|> GenericChart.mapTrace (
Trace2DStyle.Scatter(
LegendGroup = "Group A",
LegendGroupTitle = (Title.init (Text = "Group A"))
)
)
Chart.Point(y, x)
|> GenericChart.mapTrace (
Trace2DStyle.Scatter(
LegendGroup = "Group A"
)
)
Chart.Point(y, y)
|> GenericChart.mapTrace (
Trace2DStyle.Scatter(
LegendGroup = "Group B",
LegendGroupTitle = (Title.init (Text = "Group B"))
)
)
]
|> Chart.combine
(*** condition: ipynb ***)
#if IPYNB
grouped_legend_chart
#endif // IPYNB
(***hide***)
grouped_legend_chart |> GenericChart.toChartHTML
(***include-it-raw***)
(**
## Multiple legends
Starting with Plotly.NET 5.0.0, the multiple legends feature from plotl.js v2.22+ is supported.
However, plotly.js has a [regression bug starting from 2.24.3](https://github.com/plotly/plotly.js/issues/7023), which causes multiple legends to not display correctly
This means that the referenced plotly.js version has to be changed to <2.24.3 to use this feature. Note that features introduced in plotly.js/.NET after this version will not work on a chart using that reference.
Future versions of plotly.js will hopefully fix this issue.
Similarily to how multiple axes are handled, multiple legends are added by providing an additional `Id` argument when using the `Chart.withLegend` function:
To select which legend a trace should belong to, use `Chart.withLegendAnchor` with the corresponding `id` argument.
*)
let multi_legend_chart =
[
Chart.Point(x, y)
|> Chart.withLegendAnchor 1
Chart.Point(y, x)
|> Chart.withLegendAnchor 2
]
|> Chart.combine
|> Chart.withLegend(
Legend.init(
BorderColor = Color.fromKeyword Blue,
BorderWidth = 2,
Title = Title.init(
Text = "Legend 1"
)
)
)
|> Chart.withLegend(
Legend.init(
X = 0.75,
Y = 0.75,
BorderColor = Color.fromKeyword Red,
BorderWidth = 2,
Title = Title.init(
Text = "Legend 2"
)
),
Id = 2
)
// set lower plotly.js version to avoid regression bug
|> Chart.withDisplayOptionsStyle(
PlotlyJSReference = Plotly.NET.PlotlyJSReference.CDN "https://cdn.plot.ly/plotly-2.23.0.min.js"
)
(*** condition: ipynb ***)
#if IPYNB
multi_legend_chart
#endif // IPYNB
(***hide***)
multi_legend_chart |> GenericChart.toEmbeddedHTML
(***include-it-raw***)