Skip to content

Commit 335c689

Browse files
committed
Add C# bindings for Chart.OHLC
1 parent 64285ec commit 335c689

File tree

3 files changed

+106
-11
lines changed

3 files changed

+106
-11
lines changed

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,5 +971,72 @@ public static GenericChart.GenericChart PointDensity<XType, YType>(
971971
ShowScale: ShowScale.ToOption(),
972972
UseDefaults: UseDefaults.ToOption()
973973
);
974+
975+
/// <summary>
976+
/// Creates an OHLC chart.
977+
///
978+
/// The ohlc (short for Open-High-Low-Close) is a style of financial chart describing open, high, low and close for a given `x` coordinate (most likely time). The tip of the lines represent the `low` and `high` values and the horizontal segments represent the `open` and `close` values. Sample points where the close value is higher (lower) then the open value are called increasing (decreasing). By default, increasing items are drawn in green whereas decreasing are drawn in red.
979+
/// </summary>
980+
/// <param name="open">Sets the open values.</param>
981+
/// <param name="high">Sets the high values.</param>
982+
/// <param name="low">Sets the low values.</param>
983+
/// <param name="close">Sets the close values.</param>
984+
/// <param name="x">Sets the x coordinates. If absent, linear coordinate will be generated.</param>
985+
/// <param name="Name">Sets the trace name. The trace name appear as the legend item and on hover.</param>
986+
/// <param name="ShowLegend">Determines whether or not an item corresponding to this trace is shown in the legend.</param>
987+
/// <param name="Opacity">Sets the Opacity otf the trace.</param>
988+
/// <param name="Text">Sets a text associated with each datum</param>
989+
/// <param name="MultiText">Sets individual text for each datum</param>
990+
/// <param name="Line">Sets the line of this trace.</param>
991+
/// <param name="IncreasingColor">Sets the color of increasing values</param>
992+
/// <param name="Increasing">Sets the style options of increasing values (use this for more finegrained control than the other increasing-associated arguments).</param>
993+
/// <param name="DecreasingColor">Sets the color of decreasing values</param>
994+
/// <param name="Decreasing">Sets the style options of decreasing values (use this for more finegrained control than the other increasing-associated arguments).</param>
995+
/// <param name="TickWidth">Sets the width of the open/close tick marks relative to the "x" minimal interval.</param>
996+
/// <param name="UseDefaults">If set to false, ignore the global default settings set in `Defaults`</param>
997+
public static GenericChart.GenericChart OHLC<OHLCType, XType, TextType>(
998+
IEnumerable<OHLCType> open,
999+
IEnumerable<OHLCType> high,
1000+
IEnumerable<OHLCType> low,
1001+
IEnumerable<OHLCType> close,
1002+
IEnumerable<XType> x,
1003+
Optional<string> Name = default,
1004+
Optional<bool> ShowLegend = default,
1005+
Optional<double> Opacity = default,
1006+
Optional<TextType> Text = default,
1007+
Optional<IEnumerable<TextType>> MultiText = default,
1008+
Optional<Line> Line = default,
1009+
Optional<Color> IncreasingColor = default,
1010+
Optional<FinanceMarker> Increasing = default,
1011+
Optional<Color> DecreasingColor = default,
1012+
Optional<FinanceMarker> Decreasing = default,
1013+
Optional<double> TickWidth = default,
1014+
Optional<bool> UseDefaults = default
1015+
)
1016+
where OHLCType : IConvertible
1017+
where XType : IConvertible
1018+
where TextType : IConvertible
1019+
=>
1020+
Plotly.NET.Chart2D.Chart.OHLC<OHLCType, OHLCType, OHLCType, OHLCType, XType, TextType>(
1021+
open: open,
1022+
high: high,
1023+
low: low,
1024+
close: close,
1025+
x: x,
1026+
Name: Name.ToOption(),
1027+
ShowLegend: ShowLegend.ToOption(),
1028+
Opacity: Opacity.ToOption(),
1029+
Text: Text.ToOption(),
1030+
MultiText: MultiText.ToOption(),
1031+
Line: Line.ToOption(),
1032+
IncreasingColor: IncreasingColor.ToOption(),
1033+
Increasing : Increasing.ToOption(),
1034+
DecreasingColor: DecreasingColor.ToOption(),
1035+
Decreasing: Decreasing.ToOption(),
1036+
TickWidth: TickWidth.ToOption(),
1037+
UseDefaults: UseDefaults.ToOption()
1038+
);
1039+
9741040
};
1041+
9751042
}

src/Plotly.NET.CSharp/GenericChartExtensions.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,23 @@ public static GenericChart.GenericChart WithMaboxStyle(
347347
Layers: Layers.ToOption(),
348348
Id: Id.ToOption()
349349
).Invoke(gChart);
350-
}
351350

351+
/// <summary>
352+
/// Sets the range slider for the xAxis
353+
/// </summary>
354+
/// <param name="gChart">The chart for which to set the x axis range slider</param>
355+
/// <param name="rangeSlider">The rangeslider to set</param>
356+
/// <param name="Id">The id of the respective x axis</param>
357+
/// <returns></returns>
358+
public static GenericChart.GenericChart WithXAxisRangeSlider(
359+
this GenericChart.GenericChart gChart,
360+
RangeSlider rangeSlider,
361+
Optional<StyleParam.SubPlotId> Id = default
362+
)
363+
=>
364+
Plotly.NET.Chart.WithXAxisRangeSlider(
365+
rangeSlider: rangeSlider,
366+
Id: Id.ToOption()
367+
).Invoke(gChart);
368+
}
352369
}

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Program
1010
static void Main(string[] args)
1111
{
1212
Chart.Grid(
13-
nRows: 9,
13+
nRows: 10,
1414
nCols: 6,
1515
gCharts:
1616
new GenericChart[]
@@ -68,6 +68,23 @@ static void Main(string[] args)
6868
ShowScale: false
6969
),
7070

71+
//2D Finance traces
72+
Chart.OHLC<double,DateTime,string>(
73+
open: new double [] {1.2, 2.7},
74+
high: new double [] {1.8, 8.5},
75+
low: new double [] {0.5, 0.1},
76+
close: new double [] {1.1, 2.9},
77+
x: new DateTime [] {DateTime.Parse("07/07/2021"), DateTime.Parse("07/07/2022") }
78+
).WithXAxisRangeSlider(
79+
rangeSlider: Plotly.NET.LayoutObjects.RangeSlider.init(
80+
Visible: false
81+
)),
82+
Chart.Invisible(),
83+
Chart.Invisible(),
84+
Chart.Invisible(),
85+
Chart.Invisible(),
86+
Chart.Invisible(),
87+
7188
//3D traces
7289
Chart.Scatter3D<int,int,int,string>(
7390
x: new int[] { 12, 13 },
@@ -175,17 +192,11 @@ static void Main(string[] args)
175192
Chart.Invisible(),
176193
Chart.Invisible(),
177194
Chart.Invisible(),
178-
Chart.Invisible(),
195+
Chart.Invisible()
179196
}
180197
)
181-
.WithSize(1000, 1800)
182-
.Show();
183-
Chart.Column<int, string, string>(
184-
values: new int[] { 3, 4 },
185-
Keys: new string[] { "first", "second" },
186-
Width: 1,
187-
Base: 4
188-
).Show();
198+
.WithSize(1000, 1800)
199+
.Show();
189200
}
190201
}
191202
}

0 commit comments

Comments
 (0)