Skip to content

Commit 9998196

Browse files
committed
Add Chart.Histogram C# binding
1 parent 4e87bf2 commit 9998196

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,5 +436,96 @@ public static GenericChart.GenericChart Column<ValuesType, KeysType, TextType>(
436436
MultiTextPosition: MultiTextPosition.ToOption(),
437437
UseDefaults: UseDefaults.ToOptionV()
438438
);
439+
/// <summary>
440+
/// Visualizes the distribution of the input data as a histogram.
441+
///
442+
/// A histogram is an approximate representation of the distribution of numerical data. To construct a histogram, the first step is to "bin" the range of values - that is, divide the entire range of values into a series of intervals - and then count how many values fall into each interval.
443+
/// The bins are usually specified as consecutive, non-overlapping intervals of a variable.
444+
///
445+
/// The sample data from which statistics are computed is set in `x` for vertically spanning histograms and in `y` for horizontally spanning histograms. Binning options are set `xbins` and `ybins` respectively if no aggregation data is provided.
446+
/// </summary>
447+
/// <param name="X">Sets the sample data to be binned on the x axis.</param>
448+
/// <param name="Y">Sets the sample data to be binned on the y axis.</param>
449+
/// <param name="Orientation">Sets the orientation of the bars. With "v" ("h"), the value of the each bar spans along the vertical (horizontal).</param>
450+
/// <param name="Name">Sets the trace name. The trace name appear as the legend item and on hover</param>
451+
/// <param name="ShowLegend">Determines whether or not an item corresponding to this trace is shown in the legend.</param>
452+
/// <param name="Opacity">Sets the Opacity of the trace.</param>
453+
/// <param name="Text">Sets a text associated with each datum</param>
454+
/// <param name="MultiText">Sets individual text for each datum</param>
455+
/// <param name="HistFunc">Specifies the binning function used for this histogram trace. If "count", the histogram values are computed by counting the number of values lying inside each bin. If "sum", "avg", "min", "max", the histogram values are computed using the sum, the average, the minimum or the maximum of the values lying inside each bin respectively.</param>
456+
/// <param name="HistNorm">Specifies the type of normalization used for this histogram trace. If "", the span of each bar corresponds to the number of occurrences (i.e. the number of data points lying inside the bins). If "percent" / "probability", the span of each bar corresponds to the percentage / fraction of occurrences with respect to the total number of sample points (here, the sum of all bin HEIGHTS equals 100% / 1). If "density", the span of each bar corresponds to the number of occurrences in a bin divided by the size of the bin interval (here, the sum of all bin AREAS equals the total number of sample points). If "probability density", the area of each bar corresponds to the probability that an event will fall into the corresponding bin (here, the sum of all bin AREAS equals 1).</param>
457+
/// <param name="AlignmentGroup">Set several traces linked to the same position axis or matching axes to the same alignmentgroup. This controls whether bars compute their positional range dependently or independently.</param>
458+
/// <param name="OffsetGroup">Set several traces linked to the same position axis or matching axes to the same offsetgroup where bars of the same position coordinate will line up.</param>
459+
/// <param name="NBinsX">Specifies the maximum number of desired bins. This value will be used in an algorithm that will decide the optimal bin size such that the histogram best visualizes the distribution of the data. Ignored if `xbins.size` is provided.</param>
460+
/// <param name="NBinsY">Specifies the maximum number of desired bins. This value will be used in an algorithm that will decide the optimal bin size such that the histogram best visualizes the distribution of the data. Ignored if `ybins.size` is provided.</param>
461+
/// <param name="BinGroup">Set a group of histogram traces which will have compatible bin settings. Note that traces on the same subplot and with the same "orientation" under `barmode` "stack", "relative" and "group" are forced into the same bingroup, Using `bingroup`, traces under `barmode` "overlay" and on different axes (of the same axis type) can have compatible bin settings. Note that histogram and histogram2d" trace can share the same `bingroup`</param>
462+
/// <param name="XBins">Sets the binning across the x dimension</param>
463+
/// <param name="YBins">Sets the binning across the y dimension</param>
464+
/// <param name="MarkerColor">Sets the color of the histogram's bars.</param>
465+
/// <param name="Marker">Sets the marker for the histogram's bars (use this for more finegrained control than the other marker-associated arguments).</param>
466+
/// <param name="Line">Sets the outline of the histogram's bars.</param>
467+
/// <param name="XError">Sets the x error of this trace.</param>
468+
/// <param name="YError">Sets the y error of this trace.</param>
469+
/// <param name="Cumulative">Sets wether and how the cumulative distribution is displayed</param>
470+
/// <param name="HoverLabel">Sets the style of the hoverlabels of this trace.</param>
471+
/// <param name="UseDefaults">If set to false, ignore the global default settings set in `Defaults`</param>
472+
public static GenericChart.GenericChart Histogram<XType, YType, TextType>(
473+
IEnumerable<XType>? X = null,
474+
IEnumerable<YType>? Y = null,
475+
StyleParam.Orientation? Orientation = null,
476+
string? Name = null,
477+
bool? ShowLegend = null,
478+
double? Opacity = null,
479+
TextType? Text = null,
480+
IEnumerable<TextType>? MultiText = null,
481+
StyleParam.HistFunc? HistFunc = null,
482+
StyleParam.HistNorm? HistNorm = null,
483+
string? AlignmentGroup = null,
484+
string? OffsetGroup = null,
485+
int? NBinsX = null,
486+
int? NBinsY = null,
487+
string? BinGroup = null,
488+
Bins? XBins = null,
489+
Bins? YBins = null,
490+
Color? MarkerColor = null,
491+
Marker? Marker = null,
492+
Line? Line = null,
493+
Error? XError = null,
494+
Error? YError = null,
495+
Cumulative? Cumulative = null,
496+
Hoverlabel? HoverLabel = null,
497+
bool? UseDefaults = true
498+
)
499+
where XType : IConvertible
500+
where YType : IConvertible
501+
where TextType : class, IConvertible
502+
=>
503+
Plotly.NET.Chart2D.Chart.Histogram<XType, YType, TextType>(
504+
X: X.ToOption(),
505+
Y: Y.ToOption(),
506+
Orientation: Orientation.ToOption(),
507+
Name: Name.ToOption(),
508+
ShowLegend: ShowLegend.ToOptionV(),
509+
Opacity: Opacity.ToOptionV(),
510+
Text: Text.ToOption(),
511+
MultiText: MultiText.ToOption(),
512+
HistFunc: HistFunc.ToOption(),
513+
HistNorm: HistNorm.ToOption(),
514+
AlignmentGroup: AlignmentGroup.ToOption(),
515+
OffsetGroup: OffsetGroup.ToOption(),
516+
NBinsX: NBinsX.ToOptionV(),
517+
NBinsY: NBinsY.ToOptionV(),
518+
BinGroup: BinGroup.ToOption(),
519+
XBins: XBins.ToOption(),
520+
YBins: YBins.ToOption(),
521+
MarkerColor: MarkerColor.ToOption(),
522+
Marker: Marker.ToOption(),
523+
Line: Line.ToOption(),
524+
XError: XError.ToOption(),
525+
YError: YError.ToOption(),
526+
Cumulative: Cumulative.ToOption(),
527+
HoverLabel: HoverLabel.ToOption(),
528+
UseDefaults: UseDefaults.ToOptionV()
529+
);
439530
};
440531
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static void Main(string[] args)
1111
{
1212
Chart.Grid(
1313
nRows: 8,
14-
nCols: 5,
14+
nCols: 6,
1515
gCharts:
1616
new GenericChart[]
1717
{
@@ -36,6 +36,9 @@ static void Main(string[] args)
3636
values: new int [] { 1,2 },
3737
Keys: new string [] { "first", "second"}
3838
),
39+
Chart.Histogram<int,int,string>(
40+
X: new int [] { 1,2,2,2,3,4,5,5 }
41+
),
3942
Chart.Scatter3D<int,int,int,string>(
4043
x: new int[] { 12, 13 },
4144
y: new int [] { 13, 14 },
@@ -46,6 +49,7 @@ static void Main(string[] args)
4649
Chart.Invisible(),
4750
Chart.Invisible(),
4851
Chart.Invisible(),
52+
Chart.Invisible(),
4953
Chart.ScatterPolar<int,int,string>(
5054
theta: new int [] { 1, 2 },
5155
r: new int [] { 3, 4 },
@@ -55,6 +59,7 @@ static void Main(string[] args)
5559
Chart.Invisible(),
5660
Chart.Invisible(),
5761
Chart.Invisible(),
62+
Chart.Invisible(),
5863
Chart.ScatterGeo<int,int,string>(
5964
longitudes: new int [] { 1, 2 },
6065
latitudes: new int [] { 3, 4 },
@@ -64,6 +69,7 @@ static void Main(string[] args)
6469
Chart.Invisible(),
6570
Chart.Invisible(),
6671
Chart.Invisible(),
72+
Chart.Invisible(),
6773
Chart.ScatterTernary<int,int,int,IConvertible,string>(
6874
A: new int [] { 1, 2 },
6975
B: new int [] { 3, 4 },
@@ -73,6 +79,7 @@ static void Main(string[] args)
7379
Chart.Invisible(),
7480
Chart.Invisible(),
7581
Chart.Invisible(),
82+
Chart.Invisible(),
7683
Chart.Carpet<double,double,double,double,double,double>(
7784
carpetId: "testCarpet",
7885
A: new double [] {4.0, 4.0, 4.0, 4.5, 4.5, 4.5, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0},
@@ -83,6 +90,7 @@ static void Main(string[] args)
8390
Chart.Invisible(),
8491
Chart.Invisible(),
8592
Chart.Invisible(),
93+
Chart.Invisible(),
8694
Chart.Pie<double,string,string>(
8795
values: new double [] {1,2,3,4},
8896
Labels: new string [] {"soos", "saas", "fiif", "leel"}
@@ -91,6 +99,7 @@ static void Main(string[] args)
9199
Chart.Invisible(),
92100
Chart.Invisible(),
93101
Chart.Invisible(),
102+
Chart.Invisible(),
94103
Chart.ScatterSmith<double,double,string>(
95104
real: new double [] {1,2,3,4},
96105
imag: new double [] {1,2,3,4},
@@ -100,6 +109,7 @@ static void Main(string[] args)
100109
Chart.Invisible(),
101110
Chart.Invisible(),
102111
Chart.Invisible(),
112+
Chart.Invisible(),
103113
}
104114
)
105115
.WithSize(1250,2000)

0 commit comments

Comments
 (0)