Skip to content

Commit 8580265

Browse files
committed
WIP optional parameter interop
1 parent 0fc47fd commit 8580265

File tree

5 files changed

+66
-81
lines changed

5 files changed

+66
-81
lines changed

src/Plotly.NET.CSharp/ChartAPI/Chart2D.cs

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@
66
using Plotly.NET;
77
using Plotly.NET.LayoutObjects;
88
using Plotly.NET.TraceObjects;
9+
910
namespace Plotly.NET.CSharp.ChartAPI
1011
{
1112
public static class Test
1213
{
13-
public static int Foo<T>(IEnumerable<T> notopt, IEnumerable<T>? opt1) =>
14-
Plotly.NET.Chart2D.Chart.Foo<T, T>(
14+
public static int Foo<T1,T2,T3>(
15+
IEnumerable<T1> notopt,
16+
IEnumerable<T2>? opt1 = null,
17+
T3? opt2 = null
18+
)
19+
where T1 : IConvertible
20+
where T2 : IConvertible
21+
where T3 : IConvertible =>
22+
Plotly.NET.Chart2D.Chart.Foo<T1, T2, T3>(
1523
notopt: notopt,
16-
opt1: opt1
24+
opt1: Helpers.ToOption<IEnumerable<T2>>(opt1),
25+
opt2: opt2
1726
);
1827
}
1928
public static class Chart2D
@@ -52,52 +61,58 @@ public static class Chart2D
5261
/// <param name="FillColor">ets the fill color. Defaults to a half-transparent variant of the line color, marker color, or marker line color, whichever is available.</param>
5362
/// <param name="UseWebGL">If true, plotly.js will use the WebGL engine to render this chart. use this when you want to render many objects at once.</param>
5463
/// <param name="UseDefaults">If set to false, ignore the global default settings set in `Defaults`</param>
55-
public static GenericChart.GenericChart Scatter(
56-
IEnumerable<IConvertible> x,
57-
IEnumerable<IConvertible> y,
64+
public static GenericChart.GenericChart Scatter<XType,YType,TextType>(
65+
IEnumerable<XType> x,
66+
IEnumerable<YType> y,
5867
StyleParam.Mode mode,
59-
string? Name,
60-
bool? ShowLegend,
61-
float? Opacity,
62-
IEnumerable<float>? MultiOpacity,
63-
IConvertible? Text,
64-
IEnumerable<IConvertible>? MultiText,
65-
StyleParam.TextPosition? TextPosition,
66-
IEnumerable<StyleParam.TextPosition>? MultiTextPosition,
67-
Color? MarkerColor,
68-
StyleParam.Colorscale? MarkerColorScale,
69-
Line? MarkerOutline,
70-
StyleParam.MarkerSymbol? MarkerSymbol,
71-
IEnumerable<StyleParam.MarkerSymbol>? MultiMarkerSymbol,
72-
Marker? Marker,
73-
Color? LineColor,
74-
StyleParam.Colorscale? LineColorScale,
75-
float? LineWidth,
76-
StyleParam.DrawingStyle? LineDash,
77-
Line? Line,
78-
string StackGroup,
79-
StyleParam.Orientation? Orientation,
80-
StyleParam.GroupNorm? GroupNorm,
81-
StyleParam.Fill? Fill,
82-
Color? FillColor,
83-
bool? UseWebGL,
84-
bool? UseDefaults
85-
) =>
86-
Plotly.NET.Chart2D.Chart.Scatter<IConvertible,IConvertible,IConvertible>(
87-
x, y, mode,
68+
string? Name = null,
69+
bool? ShowLegend = null,
70+
double? Opacity = null,
71+
IEnumerable<double>? MultiOpacity = null,
72+
TextType? Text = null,
73+
IEnumerable<TextType>? MultiText = null,
74+
StyleParam.TextPosition? TextPosition = null,
75+
IEnumerable<StyleParam.TextPosition>? MultiTextPosition = null,
76+
Color? MarkerColor = null,
77+
StyleParam.Colorscale? MarkerColorScale = null,
78+
Line? MarkerOutline = null,
79+
StyleParam.MarkerSymbol? MarkerSymbol = null,
80+
IEnumerable<StyleParam.MarkerSymbol>? MultiMarkerSymbol = null,
81+
Marker? Marker = null,
82+
Color? LineColor = null,
83+
StyleParam.Colorscale? LineColorScale = null,
84+
double? LineWidth = null,
85+
StyleParam.DrawingStyle? LineDash = null,
86+
Line? Line = null,
87+
string? StackGroup = null,
88+
StyleParam.Orientation? Orientation = null,
89+
StyleParam.GroupNorm? GroupNorm = null,
90+
StyleParam.Fill? Fill = null,
91+
Color? FillColor = null,
92+
bool? UseWebGL = null,
93+
bool? UseDefaults = null
94+
)
95+
where XType : IConvertible
96+
where YType : IConvertible
97+
where TextType : IConvertible
98+
=>
99+
Plotly.NET.Chart2D.Chart.Scatter(
100+
x: x,
101+
y: y,
102+
mode: mode,
88103
Name: Name,
89104
ShowLegend: ShowLegend,
90105
Opacity: Opacity,
91-
MultiOpacity: MultiOpacity,
92-
Text: Text,
93-
MultiText: MultiText,
106+
MultiOpacity: Helpers.ToOption(MultiOpacity),
107+
Text: Helpers.ToOption(Text),
108+
MultiText: Helpers.ToOption(MultiText),
94109
TextPosition: TextPosition,
95-
MultiTextPosition: MultiTextPosition,
110+
MultiTextPosition: Helpers.ToOption(MultiTextPosition),
96111
MarkerColor: MarkerColor,
97112
MarkerColorScale: MarkerColorScale,
98113
MarkerOutline: MarkerOutline,
99114
MarkerSymbol: MarkerSymbol,
100-
MultiMarkerSymbol: MultiMarkerSymbol,
115+
MultiMarkerSymbol: Helpers.ToOption(MultiMarkerSymbol),
101116
Marker: Marker,
102117
LineColor: LineColor,
103118
LineColorScale: LineColorScale,

src/Plotly.NET.CSharp/Helpers.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88

99
namespace Plotly.NET.CSharp
1010
{
11-
internal class OptionWrapper<T> {
12-
internal static FSharpOption<T> WrapAsOption(T? Value) {
13-
if (Value is null) {
14-
return FSharpOption<T>.None;
15-
} else {
16-
T v = (T)Value;
17-
return FSharpOption<T>.Some(v);
18-
}
19-
}
11+
static class Helpers {
12+
static internal Microsoft.FSharp.Core.FSharpOption<T> ToOption<T>(this T? thing) => thing is null ? Microsoft.FSharp.Core.FSharpOption<T>.None : new(thing);
2013
}
2114
}

src/Plotly.NET/ChartAPI/Chart2D.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Chart2D =
3030
Trace2D.initHeatmap style |> GenericChart.ofTraceObject useDefaults
3131

3232

33-
static member Foo(notopt: seq<#IConvertible>,?opt1: seq<#IConvertible>) = 2
33+
static member Foo(notopt: seq<#IConvertible>,?opt1: seq<#IConvertible>, ?opt2: #IConvertible) = 2
3434

3535
/// <summary>
3636
/// Creates a Scatter plot.

tests/Plotly.NET.Tests.CSharpConsole/Plotly.NET.Tests.CSharpConsole.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<ItemGroup>
44
<ProjectReference Include="..\..\src\Plotly.NET\Plotly.NET.fsproj" />
5-
<ProjectReference Include="..\..\src\Plotly.NET.CSharp\Plotly.NET.CSharp.fsproj" />
5+
<ProjectReference Include="..\..\src\Plotly.NET.CSharp\Plotly.NET.CSharp.csproj" />
66
</ItemGroup>
77

88
<PropertyGroup>

tests/Plotly.NET.Tests.CSharpConsole/Program.cs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2-
using Plotly.NET;
3-
using Microsoft.FSharp.Core; // use this for less verbose and more helpful intellisense
2+
using Plotly.NET.CSharp;
3+
using Plotly.NET.CSharp.ChartAPI;
44
using Plotly.NET.LayoutObjects;
55

66
namespace Plotly.NET.Tests.CSharpConsole
@@ -9,34 +9,11 @@ class Program
99
{
1010
static void Main(string[] args)
1111
{
12-
double[] x = new double[] { 1, 2 };
13-
double[] y = new double[] { 5, 10 };
14-
15-
LinearAxis xAxis = new LinearAxis();
16-
xAxis.SetValue("title", "xAxis");
17-
xAxis.SetValue("showgrid", false);
18-
xAxis.SetValue("showline", true);
19-
20-
LinearAxis yAxis = new LinearAxis();
21-
yAxis.SetValue("title", "yAxis");
22-
yAxis.SetValue("showgrid", false);
23-
yAxis.SetValue("showline", true);
24-
25-
Layout layout = new Layout();
26-
layout.SetValue("xaxis", xAxis);
27-
layout.SetValue("yaxis", yAxis);
28-
layout.SetValue("showlegend", true);
29-
30-
Trace trace = new Trace("scatter");
31-
trace.SetValue("x", x);
32-
trace.SetValue("y", y);
33-
trace.SetValue("mode", "markers");
34-
trace.SetValue("name", "Hello from C#");
35-
36-
GenericChart
37-
.ofTraceObject(true, trace)
38-
.WithLayout(layout)
39-
.Show();
12+
Plotly.NET.CSharp.ChartAPI.Chart2D.Scatter(
13+
x: new int [] { 1, 2 },
14+
y: new int [] { 1, 2 },
15+
mode: StyleParam.Mode.Markers
16+
);
4017
}
4118
static void Main2(string[] args)
4219
{

0 commit comments

Comments
 (0)