Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to define/customize Legend for a chart? #460

kunjiangkun started this conversation in General
Discussion options

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.

You must be logged in to vote

Replies: 2 comments

Comment options

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);

You must be logged in to vote
0 replies
Comment options

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 or Layout.updateLegendById - static methods on the Layout class:
      static member setLegend(id: StyleParam.SubPlotId, legend: Legend) =
    • Chart.withLegend or Chart.withLegendStyle - static methods of the Chart API:
      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 :
      // 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 in Plotly.NET.C#:

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /