Skip to content

Commit 0c6444d

Browse files
committed
Add C# WithX/Yaxis extension methods, fix optional parameter usage in other extension methods
1 parent cfea76e commit 0c6444d

File tree

3 files changed

+237
-9
lines changed

3 files changed

+237
-9
lines changed

src/Plotly.NET.CSharp/GenericChartExtensions.cs

+216-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Plotly.NET;
2+
using Plotly.NET.LayoutObjects;
3+
using Plotly.NET.TraceObjects;
24

35
namespace Plotly.NET.CSharp
46
{
@@ -21,7 +23,7 @@ public static void SaveHtml(
2123
) =>
2224
Plotly.NET.Chart.SaveHtml(
2325
path: path,
24-
OpenInBrowser: OpenInBrowser
26+
OpenInBrowser: OpenInBrowser.ToOptionV()
2527
).Invoke(gChart);
2628

2729
/// <summary>
@@ -49,12 +51,12 @@ public static GenericChart.GenericChart WithTraceInfo(
4951
Title? LegendGroupTitle = null
5052
) =>
5153
Plotly.NET.Chart.WithTraceInfo(
52-
Name: Name,
53-
Visible: Visible,
54-
ShowLegend: ShowLegend,
55-
LegendRank: LegendRank,
56-
LegendGroup: LegendGroup,
57-
LegendGroupTitle: LegendGroupTitle
54+
Name: Name.ToOption(),
55+
Visible: Visible.ToOption(),
56+
ShowLegend: ShowLegend.ToOptionV(),
57+
LegendRank: LegendRank.ToOptionV(),
58+
LegendGroup: LegendGroup.ToOption(),
59+
LegendGroupTitle: LegendGroupTitle.ToOption()
5860
).Invoke(gChart);
5961

6062
/// Sets the size of a Chart (in pixels)
@@ -63,8 +65,213 @@ public static GenericChart.GenericChart WithSize(
6365
int? Width = null,
6466
int? Height = null
6567
) =>
66-
Plotly.NET.Chart.WithSize(Width: Width, Height: Height).Invoke(gChart);
67-
}
68+
Plotly.NET.Chart.WithSize(Width: Width.ToOptionV(), Height: Height.ToOptionV()).Invoke(gChart);
69+
70+
/// <summary>
71+
/// Sets the given x axis styles on the input chart's layout.
72+
///
73+
/// If there is already an axis set at the given id, the styles are applied to it. If there is no axis present, a new LinearAxis object with the given styles will be set.
74+
/// </summary>
75+
/// <param name="TitleText">Sets the text of the axis title.</param>
76+
/// <param name="TitleFont">Sets the font of the axis title.</param>
77+
/// <param name="TitleStandoff">Sets the standoff distance (in px) between the axis labels and the title text.</param>
78+
/// <param name="Title">Sets the Title (use this for more finegrained control than the other title-associated arguments)</param>
79+
/// <param name="Color">Sets default for all colors associated with this axis all at once: line, font, tick, and grid colors.</param>
80+
/// <param name="AxisType">Sets the axis type. By default, plotly attempts to determined the axis type by looking into the data of the traces that referenced the axis in question.</param>
81+
/// <param name="MinMax">Tuple of (Min*Max value). Sets the range of this axis (the axis will go from Min to Max). If the axis `type` is "log", then you must take the log of your desired range (e.g. to set the range from 1 to 100, set the range from 0 to 2).</param>
82+
/// <param name="Mirror">Determines if and how the axis lines or/and ticks are mirrored to the opposite side of the plotting area.</param>
83+
/// <param name="ShowSpikes">Determines whether or not spikes (aka droplines) are drawn for this axis.</param>
84+
/// <param name="SpikeColor">Sets the spike color. If not set, will use the series color</param>
85+
/// <param name="SpikeThickness">Sets the width (in px) of the zero line.</param>
86+
/// <param name="ShowLine">Determines whether or not a line bounding this axis is drawn.</param>
87+
/// <param name="LineColor">Sets the axis line color.</param>
88+
/// <param name="ShowGrid">Determines whether or not grid lines are drawn. If "true", the grid lines are drawn at every tick mark.</param>
89+
/// <param name="GridColor">Sets the color of the grid lines.</param>
90+
/// <param name="ZeroLine">Determines whether or not a line is drawn at along the 0 value of this axis. If "true", the zero line is drawn on top of the grid lines.</param>
91+
/// <param name="ZeroLineColor">Sets the line color of the zero line.</param>
92+
/// <param name="Anchor">If set to an opposite-letter axis id (e.g. `x2`, `y`), this axis is bound to the corresponding opposite-letter axis. If set to "free", this axis' position is determined by `position`.</param>
93+
/// <param name="Side">Determines whether a x (y) axis is positioned at the "bottom" ("left") or "top" ("right") of the plotting area.</param>
94+
/// <param name="Overlaying">If set a same-letter axis id, this axis is overlaid on top of the corresponding same-letter axis, with traces and axes visible for both axes. If "false", this axis does not overlay any same-letter axes. In this case, for axes with overlapping domains only the highest-numbered axis will be visible.</param>
95+
/// <param name="Domain">Tuple of (X*Y fractions). Sets the domain of this axis (in plot fraction).</param>
96+
/// <param name="Position">Sets the position of this axis in the plotting space (in normalized coordinates). Only has an effect if `anchor` is set to "free".</param>
97+
/// <param name="CategoryOrder">Specifies the ordering logic for the case of categorical variables. By default, plotly uses "trace", which specifies the order that is present in the data supplied. Set `categoryorder` to "category ascending" or "category descending" if order should be determined by the alphanumerical order of the category names. Set `categoryorder` to "array" to derive the ordering from the attribute `categoryarray`. If a category is not found in the `categoryarray` array, the sorting behavior for that attribute will be identical to the "trace" mode. The unspecified categories will follow the categories in `categoryarray`. Set `categoryorder` to "total ascending" or "total descending" if order should be determined by the numerical order of the values. Similarly, the order can be determined by the min, max, sum, mean or median of all the values.</param>
98+
/// <param name="CategoryArray">Sets the order in which categories on this axis appear. Only has an effect if `categoryorder` is set to "array". Used with `categoryorder`.</param>
99+
/// <param name="RangeSlider">Sets a range slider for this axis</param>
100+
/// <param name="RangeSelector">Sets a range selector for this axis. This object contains toggable presets for the rangeslider.</param>
101+
/// <param name="BackgroundColor">Sets the background color of this axis' wall. (Only has an effect on 3D scenes)</param>
102+
/// <param name="ShowBackground">Sets whether or not this axis' wall has a background color. (Only has an effect on 3D scenes)</param>
103+
/// <param name="Id">The target axis id on which the styles should be applied. Default is 1.</param>
104+
public static GenericChart.GenericChart WithXAxisStyle<MinType, MaxType, CategoryArrayType>(
105+
this GenericChart.GenericChart gChart,
106+
string? TitleText = null,
107+
Font? TitleFont = null,
108+
int? TitleStandoff = null,
109+
Title? Title = null,
110+
Color? Color = null,
111+
StyleParam.AxisType? AxisType = null,
112+
Tuple<MinType, MaxType>? MinMax = null,
113+
StyleParam.Mirror? Mirror = null,
114+
bool? ShowSpikes = null,
115+
Color? SpikeColor = null,
116+
int? SpikeThickness = null,
117+
bool? ShowLine = null,
118+
Color? LineColor = null,
119+
bool? ShowGrid = null,
120+
Color? GridColor = null,
121+
bool? ZeroLine = null,
122+
Color? ZeroLineColor = null,
123+
StyleParam.LinearAxisId? Anchor = null,
124+
StyleParam.Side? Side = null,
125+
StyleParam.LinearAxisId? Overlaying = null,
126+
Tuple<double, double>? Domain = null,
127+
double? Position = null,
128+
StyleParam.CategoryOrder? CategoryOrder = null,
129+
IEnumerable<CategoryArrayType>? CategoryArray = null,
130+
RangeSlider? RangeSlider = null,
131+
RangeSelector? RangeSelector = null,
132+
Color? BackgroundColor = null,
133+
bool? ShowBackground = null,
134+
StyleParam.SubPlotId? Id = null
135+
)
136+
where MinType : IConvertible
137+
where MaxType : IConvertible
138+
where CategoryArrayType : class, IConvertible
139+
=>
140+
Plotly.NET.Chart.WithXAxisStyle<MinType, MaxType, CategoryArrayType>(
141+
TitleText: TitleText.ToOption(),
142+
TitleFont: TitleFont.ToOption(),
143+
TitleStandoff: TitleStandoff.ToOptionV(),
144+
Title: Title.ToOption(),
145+
Color: Color.ToOption(),
146+
AxisType: AxisType.ToOption(),
147+
MinMax: MinMax.ToOption(),
148+
Mirror: Mirror.ToOption(),
149+
ShowSpikes: ShowSpikes.ToOptionV(),
150+
SpikeColor: SpikeColor.ToOption(),
151+
SpikeThickness: SpikeThickness.ToOptionV(),
152+
ShowLine: ShowLine.ToOptionV(),
153+
LineColor: LineColor.ToOption(),
154+
ShowGrid: ShowGrid.ToOptionV(),
155+
GridColor: GridColor.ToOption(),
156+
ZeroLine: ZeroLine.ToOptionV(),
157+
ZeroLineColor: ZeroLineColor.ToOption(),
158+
Anchor: Anchor.ToOption(),
159+
Side: Side.ToOption(),
160+
Overlaying: Overlaying.ToOption(),
161+
Domain: Domain.ToOption(),
162+
Position: Position.ToOptionV(),
163+
CategoryOrder: CategoryOrder.ToOption(),
164+
CategoryArray: CategoryArray.ToOption(),
165+
RangeSlider: RangeSlider.ToOption(),
166+
RangeSelector: RangeSelector.ToOption(),
167+
BackgroundColor: BackgroundColor.ToOption(),
168+
ShowBackground: ShowBackground.ToOptionV(),
169+
Id: Id.ToOption()
170+
171+
).Invoke(gChart);
68172

173+
/// <summary>
174+
/// Sets the given y axis styles on the input chart's layout.
175+
///
176+
/// If there is already an axis set at the given id, the styles are applied to it. If there is no axis present, a new LinearAxis object with the given styles will be set.
177+
/// </summary>
178+
/// <param name="TitleText">Sets the text of the axis title.</param>
179+
/// <param name="TitleFont">Sets the font of the axis title.</param>
180+
/// <param name="TitleStandoff">Sets the standoff distance (in px) between the axis labels and the title text.</param>
181+
/// <param name="Title">Sets the Title (use this for more finegrained control than the other title-associated arguments)</param>
182+
/// <param name="Color">Sets default for all colors associated with this axis all at once: line, font, tick, and grid colors.</param>
183+
/// <param name="AxisType">Sets the axis type. By default, plotly attempts to determined the axis type by looking into the data of the traces that referenced the axis in question.</param>
184+
/// <param name="MinMax">Tuple of (Min*Max value). Sets the range of this axis (the axis will go from Min to Max). If the axis `type` is "log", then you must take the log of your desired range (e.g. to set the range from 1 to 100, set the range from 0 to 2).</param>
185+
/// <param name="Mirror">Determines if and how the axis lines or/and ticks are mirrored to the opposite side of the plotting area.</param>
186+
/// <param name="ShowSpikes">Determines whether or not spikes (aka droplines) are drawn for this axis.</param>
187+
/// <param name="SpikeColor">Sets the spike color. If not set, will use the series color</param>
188+
/// <param name="SpikeThickness">Sets the width (in px) of the zero line.</param>
189+
/// <param name="ShowLine">Determines whether or not a line bounding this axis is drawn.</param>
190+
/// <param name="LineColor">Sets the axis line color.</param>
191+
/// <param name="ShowGrid">Determines whether or not grid lines are drawn. If "true", the grid lines are drawn at every tick mark.</param>
192+
/// <param name="GridColor">Sets the color of the grid lines.</param>
193+
/// <param name="ZeroLine">Determines whether or not a line is drawn at along the 0 value of this axis. If "true", the zero line is drawn on top of the grid lines.</param>
194+
/// <param name="ZeroLineColor">Sets the line color of the zero line.</param>
195+
/// <param name="Anchor">If set to an opposite-letter axis id (e.g. `x2`, `y`), this axis is bound to the corresponding opposite-letter axis. If set to "free", this axis' position is determined by `position`.</param>
196+
/// <param name="Side">Determines whether a x (y) axis is positioned at the "bottom" ("left") or "top" ("right") of the plotting area.</param>
197+
/// <param name="Overlaying">If set a same-letter axis id, this axis is overlaid on top of the corresponding same-letter axis, with traces and axes visible for both axes. If "false", this axis does not overlay any same-letter axes. In this case, for axes with overlapping domains only the highest-numbered axis will be visible.</param>
198+
/// <param name="Domain">Tuple of (X*Y fractions). Sets the domain of this axis (in plot fraction).</param>
199+
/// <param name="Position">Sets the position of this axis in the plotting space (in normalized coordinates). Only has an effect if `anchor` is set to "free".</param>
200+
/// <param name="CategoryOrder">Specifies the ordering logic for the case of categorical variables. By default, plotly uses "trace", which specifies the order that is present in the data supplied. Set `categoryorder` to "category ascending" or "category descending" if order should be determined by the alphanumerical order of the category names. Set `categoryorder` to "array" to derive the ordering from the attribute `categoryarray`. If a category is not found in the `categoryarray` array, the sorting behavior for that attribute will be identical to the "trace" mode. The unspecified categories will follow the categories in `categoryarray`. Set `categoryorder` to "total ascending" or "total descending" if order should be determined by the numerical order of the values. Similarly, the order can be determined by the min, max, sum, mean or median of all the values.</param>
201+
/// <param name="CategoryArray">Sets the order in which categories on this axis appear. Only has an effect if `categoryorder` is set to "array". Used with `categoryorder`.</param>
202+
/// <param name="RangeSlider">Sets a range slider for this axis</param>
203+
/// <param name="RangeSelector">Sets a range selector for this axis. This object contains toggable presets for the rangeslider.</param>
204+
/// <param name="BackgroundColor">Sets the background color of this axis' wall. (Only has an effect on 3D scenes)</param>
205+
/// <param name="ShowBackground">Sets whether or not this axis' wall has a background color. (Only has an effect on 3D scenes)</param>
206+
/// <param name="Id">The target axis id on which the styles should be applied. Default is 1.</param>
207+
public static GenericChart.GenericChart WithYAxisStyle<MinType, MaxType, CategoryArrayType>(
208+
this GenericChart.GenericChart gChart,
209+
string? TitleText = null,
210+
Font? TitleFont = null,
211+
int? TitleStandoff = null,
212+
Title? Title = null,
213+
Color? Color = null,
214+
StyleParam.AxisType? AxisType = null,
215+
Tuple<MinType, MaxType>? MinMax = null,
216+
StyleParam.Mirror? Mirror = null,
217+
bool? ShowSpikes = null,
218+
Color? SpikeColor = null,
219+
int? SpikeThickness = null,
220+
bool? ShowLine = null,
221+
Color? LineColor = null,
222+
bool? ShowGrid = null,
223+
Color? GridColor = null,
224+
bool? ZeroLine = null,
225+
Color? ZeroLineColor = null,
226+
StyleParam.LinearAxisId? Anchor = null,
227+
StyleParam.Side? Side = null,
228+
StyleParam.LinearAxisId? Overlaying = null,
229+
Tuple<double, double>? Domain = null,
230+
double? Position = null,
231+
StyleParam.CategoryOrder? CategoryOrder = null,
232+
IEnumerable<CategoryArrayType>? CategoryArray = null,
233+
RangeSlider? RangeSlider = null,
234+
RangeSelector? RangeSelector = null,
235+
Color? BackgroundColor = null,
236+
bool? ShowBackground = null,
237+
StyleParam.SubPlotId? Id = null
238+
)
239+
where MinType : IConvertible
240+
where MaxType : IConvertible
241+
where CategoryArrayType : class, IConvertible
242+
=>
243+
Plotly.NET.Chart.WithYAxisStyle<MinType, MaxType, CategoryArrayType>(
244+
TitleText: TitleText.ToOption(),
245+
TitleFont: TitleFont.ToOption(),
246+
TitleStandoff: TitleStandoff.ToOptionV(),
247+
Title: Title.ToOption(),
248+
Color: Color.ToOption(),
249+
AxisType: AxisType.ToOption(),
250+
MinMax: MinMax.ToOption(),
251+
Mirror: Mirror.ToOption(),
252+
ShowSpikes: ShowSpikes.ToOptionV(),
253+
SpikeColor: SpikeColor.ToOption(),
254+
SpikeThickness: SpikeThickness.ToOptionV(),
255+
ShowLine: ShowLine.ToOptionV(),
256+
LineColor: LineColor.ToOption(),
257+
ShowGrid: ShowGrid.ToOptionV(),
258+
GridColor: GridColor.ToOption(),
259+
ZeroLine: ZeroLine.ToOptionV(),
260+
ZeroLineColor: ZeroLineColor.ToOption(),
261+
Anchor: Anchor.ToOption(),
262+
Side: Side.ToOption(),
263+
Overlaying: Overlaying.ToOption(),
264+
Domain: Domain.ToOption(),
265+
Position: Position.ToOptionV(),
266+
CategoryOrder: CategoryOrder.ToOption(),
267+
CategoryArray: CategoryArray.ToOption(),
268+
RangeSlider: RangeSlider.ToOption(),
269+
RangeSelector: RangeSelector.ToOption(),
270+
BackgroundColor: BackgroundColor.ToOption(),
271+
ShowBackground: ShowBackground.ToOptionV(),
272+
Id: Id.ToOption()
273+
274+
).Invoke(gChart);
275+
}
69276

70277
}

src/Plotly.NET.CSharp/Helpers.cs

+12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@
99
namespace Plotly.NET.CSharp
1010
{
1111
static class Helpers {
12+
/// <summary>
13+
/// Helper extension method to convert any nullable reference type to a FSharpOption, to be used with F# optional parameters.
14+
/// The resulting opton will be `None` when the value is null, and Some(value) otherwise
15+
/// </summary>
16+
/// <param name="thing">the thing to turn into a FSharpOption</param>
17+
/// <returns>The original value wrapped as a FSharpOption</returns>
1218
static internal Microsoft.FSharp.Core.FSharpOption<T> ToOption<T>(this T? thing) => thing is null ? Microsoft.FSharp.Core.FSharpOption<T>.None : new(thing);
19+
/// <summary>
20+
/// Helper extension method to convert any nullable value type to a FSharpOption, to be used with F# optional parameters.
21+
/// The resulting opton will be `None` when the value is null, and Some(value) otherwise
22+
/// </summary>
23+
/// <param name="thing">the thing to turn into a FSharpOption</param>
24+
/// <returns>The original value wrapped as a FSharpOption</returns>
1325
static internal Microsoft.FSharp.Core.FSharpOption<T> ToOptionV<T>(this T? thing) where T : struct => thing is { } some ? new(some) : Microsoft.FSharp.Core.FSharpOption<T>.None;
1426
}
1527
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ static void Main(string[] args)
8282
)
8383
.WithSize(750,2000)
8484
.Show();
85+
86+
Chart.Point<int, int, string>(
87+
x: new int[] { 1, 2 },
88+
y: new int[] { 3, 4 }
89+
)
90+
.WithTraceInfo("Hello from C#", ShowLegend: true)
91+
.WithXAxisStyle<int, int, string>(TitleText: "x axis")
92+
.WithYAxisStyle<int, int, string>(TitleText: "y axis")
93+
.Show();
8594
}
8695
}
8796
}

0 commit comments

Comments
 (0)