Skip to content

Commit 0fb539c

Browse files
committed
#378: Add WithLegend and WithLegendStyle extensions for Plotly.NET.CSharp
1 parent 34feafd commit 0fb539c

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

src/Plotly.NET.CSharp/GenericChartExtensions.cs

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Plotly.NET;
1+
using Microsoft.FSharp.Core;
2+
using Plotly.NET;
23
using Plotly.NET.LayoutObjects;
34
using Plotly.NET.TraceObjects;
45
using System.Runtime.InteropServices;
@@ -365,5 +366,101 @@ public static GenericChart.GenericChart WithXAxisRangeSlider(
365366
rangeSlider: rangeSlider,
366367
Id: Id.ToOption()
367368
).Invoke(gChart);
369+
370+
/// <summary>
371+
/// Sets the Legend for the chart's layout
372+
///
373+
/// If there is already a Legend set, the objects are combined.
374+
/// </summary>
375+
/// <param name="gChart">The chart for which to set the legend</param>
376+
/// <param name="legend">The new Legend for the chart's layout</param>
377+
public static GenericChart.GenericChart WithLegend(
378+
this GenericChart.GenericChart gChart,
379+
Legend legend
380+
)
381+
=>
382+
Plotly.NET.Chart.WithLegend(
383+
legend: legend
384+
).Invoke(gChart);
385+
386+
/// <summary>
387+
/// Sets the given Legend styles on the input chart's Legend.
388+
///
389+
/// If there is already a Legend set , the styles are applied to it. If there is no Legend present, a new Legend object with the given styles will be set.
390+
/// </summary>
391+
/// <param name="gChart">The chart for which to set the legend styles</param>
392+
/// <param name="BGColor">Sets the legend background color. Defaults to `layout.paper_bgcolor`.</param>
393+
/// <param name="BorderColor">Sets the color of the border enclosing the legend.</param>
394+
/// <param name="BorderWidth">Sets the width (in px) of the border enclosing the legend.</param>
395+
/// <param name="EntryWidth">Sets the width (in px or fraction) of the legend. Use 0 to size the entry based on the text width, when `entrywidthmode` is set to "pixels".</param>
396+
/// <param name="EntryWidthMode">Determines what entrywidth means.</param>
397+
/// <param name="Font">Sets the font used to text the legend items.</param>
398+
/// <param name="GroupClick">Determines the behavior on legend group item click. "toggleitem" toggles the visibility of the individual item clicked on the graph. "togglegroup" toggles the visibility of all items in the same legendgroup as the item clicked on the graph.</param>
399+
/// <param name="GroupTitleFont">Sets the font for group titles in legend. Defaults to `legend.font` with its size increased about 10%.</param>
400+
/// <param name="ItemClick">Determines the behavior on legend item click. "toggle" toggles the visibility of the item clicked on the graph. "toggleothers" makes the clicked item the sole visible item on the graph. "false" disables legend item click interactions.</param>
401+
/// <param name="ItemDoubleClick">Determines the behavior on legend item double-click. "toggle" toggles the visibility of the item clicked on the graph. "toggleothers" makes the clicked item the sole visible item on the graph. "false" disables legend item double-click interactions.</param>
402+
/// <param name="ItemSizing">Determines if the legend items symbols scale with their corresponding "trace" attributes or remain "constant" independent of the symbol size on the graph.</param>
403+
/// <param name="ItemWidth">Sets the width (in px) of the legend item symbols (the part other than the title.text).</param>
404+
/// <param name="Orientation">Sets the orientation of the legend.</param>
405+
/// <param name="Title">Sets the title of the legend.</param>
406+
/// <param name="TraceGroupGap">Sets the amount of vertical space (in px) between legend groups.</param>
407+
/// <param name="TraceOrder">Determines the order at which the legend items are displayed. If "normal", the items are displayed top-to-bottom in the same order as the input data. If "reversed", the items are displayed in the opposite order as "normal". If "grouped", the items are displayed in groups (when a trace `legendgroup` is provided). if "grouped+reversed", the items are displayed in the opposite order as "grouped".</param>
408+
/// <param name="UIRevision">Controls persistence of legend-driven changes in trace and pie label visibility. Defaults to `layout.uirevision`.</param>
409+
/// <param name="VerticalAlign">Sets the vertical alignment of the symbols with respect to their associated text.</param>
410+
/// <param name="X">Sets the x position (in normalized coordinates) of the legend. Defaults to "1.02" for vertical legends and defaults to "0" for horizontal legends.</param>
411+
/// <param name="XAnchor">Sets the legend's horizontal position anchor. This anchor binds the `x` position to the "left", "center" or "right" of the legend. Value "auto" anchors legends to the right for `x` values greater than or equal to 2/3, anchors legends to the left for `x` values less than or equal to 1/3 and anchors legends with respect to their center otherwise.</param>
412+
/// <param name="Y">Sets the y position (in normalized coordinates) of the legend. Defaults to "1" for vertical legends, defaults to "-0.1" for horizontal legends on graphs w/o range sliders and defaults to "1.1" for horizontal legends on graph with one or multiple range sliders.</param>
413+
/// <param name="YAnchor">Sets the legend's vertical position anchor This anchor binds the `y` position to the "top", "middle" or "bottom" of the legend. Value "auto" anchors legends at their bottom for `y` values less than or equal to 1/3, anchors legends to at their top for `y` values greater than or equal to 2/3 and anchors legends with respect to their middle otherwise.</param>
414+
public static GenericChart.GenericChart WithLegendStyle(
415+
this GenericChart.GenericChart gChart,
416+
Optional<Color> BGColor = default,
417+
Optional<Color> BorderColor = default,
418+
Optional<double> BorderWidth = default,
419+
Optional<double> EntryWidth = default,
420+
Optional<StyleParam.EntryWidthMode> EntryWidthMode = default,
421+
Optional<Font> Font = default,
422+
Optional<StyleParam.TraceGroupClickOptions> GroupClick = default,
423+
Optional<Font> GroupTitleFont = default,
424+
Optional<StyleParam.TraceItemClickOptions> ItemClick = default,
425+
Optional<StyleParam.TraceItemClickOptions> ItemDoubleClick = default,
426+
Optional<StyleParam.TraceItemSizing> ItemSizing = default,
427+
Optional<int> ItemWidth = default,
428+
Optional<StyleParam.Orientation> Orientation = default,
429+
Optional<Title> Title = default,
430+
Optional<double> TraceGroupGap = default,
431+
Optional<StyleParam.TraceOrder> TraceOrder = default,
432+
Optional<string> UIRevision = default,
433+
Optional<StyleParam.VerticalAlign> VerticalAlign = default,
434+
Optional<double> X = default,
435+
Optional<StyleParam.XAnchorPosition> XAnchor = default,
436+
Optional<double> Y = default,
437+
Optional<StyleParam.YAnchorPosition> YAnchor = default
438+
)
439+
=>
440+
Plotly.NET.Chart.WithLegendStyle(
441+
BGColor: BGColor.ToOption(),
442+
BorderColor: BorderColor.ToOption(),
443+
BorderWidth: BorderWidth.ToOption(),
444+
EntryWidth: EntryWidth.ToOption(),
445+
EntryWidthMode: EntryWidthMode.ToOption(),
446+
Font: Font.ToOption(),
447+
GroupClick: GroupClick.ToOption(),
448+
GroupTitleFont: GroupTitleFont.ToOption(),
449+
ItemClick: ItemClick.ToOption(),
450+
ItemDoubleClick: ItemDoubleClick.ToOption(),
451+
ItemSizing: ItemSizing.ToOption(),
452+
ItemWidth: ItemWidth.ToOption(),
453+
Orientation: Orientation.ToOption(),
454+
Title: Title.ToOption(),
455+
TraceGroupGap: TraceGroupGap.ToOption(),
456+
TraceOrder: TraceOrder.ToOption(),
457+
UIRevision: UIRevision.ToOption(),
458+
VerticalAlign: VerticalAlign.ToOption(),
459+
X: X.ToOption(),
460+
XAnchor: XAnchor.ToOption(),
461+
Y: Y.ToOption(),
462+
YAnchor: YAnchor.ToOption()
463+
).Invoke(gChart);
464+
368465
}
369466
}

tests/Plotly.NET.CSharp.Tests/ExtensionMethodsTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System;
22
using Xunit;
33
using Plotly.NET.CSharp;
4-
4+
using CSharp.Tests;
55

66
namespace Plotly.NET.CSharp.Tests
77
{
88
public class ExtensionMethodsTests
99
{
10-
internal GenericChart.GenericChart chart = Chart2D.Chart.Point<double, double, string>(x: new double[] { 1, 2 }, y: new double[] { 5, 10 });
10+
internal GenericChart.GenericChart chart = Chart2D.Chart.Point<double, double, string>(x: new double[] { 1, 2 }, y: new double[] { 5, 10 }, UseDefaults: false);
1111

1212
[Fact]
1313
public void CanUseCSharpExtensionMethod()
@@ -20,5 +20,20 @@ public void CanUseCSharpExtensionMethod()
2020

2121
Assert.Equal("Trace Name", DynamicObj.DynamicObj.GetValue(actual,"name"));
2222
}
23+
24+
[Fact]
25+
public void WithLegendLayout()
26+
{
27+
TestUtils.ChartGeneratedContains(
28+
chart.WithLegendStyle(
29+
X: 0.5,
30+
Orientation: Plotly.NET.StyleParam.Orientation.Horizontal,
31+
XAnchor: Plotly.NET.StyleParam.XAnchorPosition.Center,
32+
EntryWidth: 0,
33+
EntryWidthMode: Plotly.NET.StyleParam.EntryWidthMode.Pixels
34+
),
35+
"var layout = {\"legend\":{\"entrywidth\":0.0,\"entrywidthmode\":\"pixels\",\"orientation\":\"h\",\"x\":0.5,\"xanchor\":\"center\"}};"
36+
);
37+
}
2338
}
2439
}

tests/Plotly.NET.CSharp.Tests/Plotly.NET.CSharp.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
<ProjectReference Include="..\..\src\Plotly.NET.CSharp\Plotly.NET.CSharp.csproj" />
2323
</ItemGroup>
2424

25+
<ItemGroup>
26+
<Folder Include="extensionmethods\" />
27+
</ItemGroup>
28+
2529
</Project>

0 commit comments

Comments
 (0)