-
Notifications
You must be signed in to change notification settings - Fork 99
How to define/customize Legend for a chart? #460
-
In version 5.0, legend is no longer an option for Layout.init. The legend option is also not exposed for trace such as Chart2D.Scatter. Looking at the code, it's defined for inner function Trace2DStyle.Scatter but Legend arg is set to null. Please provide an example for customizing Legend.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments
-
Somehow chart.WithLegend only supports arg of bool. Have to call GenericChartExtensions.WithLegend(trace, legendSetting) explicitly to get it working.
var boxPlot = box
.WithLegend(true)
.WithXAxis(xAxis)
.WithLayout(plotLayout)
.WithSize(plotWidth, plotHeight);
GenericChartExtensions.WithLegend(boxPlot, legendSetting);
Beta Was this translation helpful? Give feedback.
All reactions
-
It is hard for me to make out the problem because you are mixing some concepts here. Chart2D.Scatter does not create a trace - it creates a GenericChart
which contains trace(s), a layout, a config, and display options. The reason is that plotly strictly separates the layout of a chart from traces. Traces are just the data and style options of a single trace (e.g., the marker style). Layout options on the other hand are styling options for the whole chart, no matter how many traces it has. A legend is exactly such an object - something that is displayed for one or more traces of the chart.
So you are always aiming to set the legend on the layout, or use a method that changes the layout part of a GenericChart
, for which you have multiple options:
-
F# core API (these return F# functions, which you'd need to call
Invoke
on from C#):Layout.setLegend
orLayout.updateLegendById
- static methods on theLayout
class:Plotly.NET/src/Plotly.NET/Layout/Layout.fs
Line 1126 in 98a4d18
static member setLegend(id: StyleParam.SubPlotId, legend: Legend) =Chart.withLegend
orChart.withLegendStyle
- static methods of theChart
API:Plotly.NET/src/Plotly.NET/ChartAPI/Chart.fs
Line 2629 in 98a4d18
static member withLegend(legend: Legend, [<Optional; DefaultParameterValue(null)>] ?Id: int) =GenericChartExtensions
provides multiple overloads, one of them only takes boolean and needs to be removed, but there are others as well, see here :Plotly.NET/src/Plotly.NET/CSharpLayer/GenericChartExtensions.fs
Lines 610 to 613 in 98a4d18
// Set the LayoutGrid options of a Chart[<CompiledName("WithLegend")>]member this.WithLegend(legend: Legend, [<Optional; DefaultParameterValue(null)>] ?Id: int) =this |> Chart.withLegend (legend, ?Id = Id)
-
GenericChartExtensions
inPlotly.NET.C#
:WithLegend
orWithLegendStyle
:public static GenericChart WithLegend(
Beta Was this translation helpful? Give feedback.