Skip to content

Commit 9c8eb12

Browse files
committed
Add C# bindings for Chart.Histogram2D
1 parent 3453f12 commit 9c8eb12

File tree

2 files changed

+96
-16
lines changed

2 files changed

+96
-16
lines changed

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,5 +528,78 @@ [Optional] bool? UseDefaults
528528
HoverLabel: HoverLabel.ToOption(),
529529
UseDefaults: UseDefaults.ToOptionV()
530530
);
531+
/// <summary>
532+
/// Visualizes the distribution of the 2-dimensional input data as 2D Histogram.
533+
///
534+
///The sample data from which statistics are computed is set in `x` and `y` (where `x` and `y` represent marginal distributions, binning is set in `xbins` and `ybins` in this case) or `z` (where `z` represent the 2D distribution and binning set, binning is set by `x` and `y` in this case). The resulting distribution is visualized as a heatmap.
535+
/// </summary>
536+
/// <param name="x">Sets the sample data to be binned on the x axis.</param>
537+
/// <param name="y">Sets the sample data to be binned on the y axis.</param>
538+
/// <param name="Z">Sets the aggregation data.</param>
539+
/// <param name="Name">Sets the trace name. The trace name appear as the legend item and on hover</param>
540+
/// <param name="ShowLegend">Determines whether or not an item corresponding to this trace is shown in the legend.</param>
541+
/// <param name="Opacity">Sets the Opacity of the trace.</param>
542+
/// <param name="XGap">Sets the horizontal gap (in pixels) between bricks.</param>
543+
/// <param name="YGap">Sets the vertical gap (in pixels) between bricks.</param>
544+
/// <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>
545+
/// <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>
546+
/// <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>
547+
/// <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>
548+
/// <param name="XBins">Sets the binning across the x dimension</param>
549+
/// <param name="YBins">Sets the binning across the y dimension</param>
550+
/// <param name="ColorBar">Sets the styles of the colorbar for this trace.</param>
551+
/// <param name="ColorScale">Sets the colorscale for this trace.</param>
552+
/// <param name="ShowScale">Wether or not to show the colorscale/colorbar</param>
553+
/// <param name="ReverseScale">Reverses the color mapping if true. If true, `zmin` will correspond to the last color in the array and `zmax` will correspond to the first color.</param>
554+
/// <param name="ZSmooth">Picks a smoothing algorithm use to smooth `z` data.</param>
555+
/// <param name="UseDefaults">If set to false, ignore the global default settings set in `Defaults`</param>
556+
public static GenericChart.GenericChart Histogram2D<XType, YType, ZType>(
557+
IEnumerable<XType> x,
558+
IEnumerable<YType> y,
559+
[Optional] IEnumerable<IEnumerable<ZType>>? Z,
560+
[Optional] string? Name,
561+
[Optional] bool? ShowLegend,
562+
[Optional] double? Opacity,
563+
[Optional] int? XGap,
564+
[Optional] int? YGap,
565+
[Optional] StyleParam.HistFunc? HistFunc,
566+
[Optional] StyleParam.HistNorm? HistNorm,
567+
[Optional] int? NBinsX,
568+
[Optional] int? NBinsY,
569+
[Optional] Bins? XBins,
570+
[Optional] Bins? YBins,
571+
[Optional] ColorBar? ColorBar,
572+
[Optional] StyleParam.Colorscale? ColorScale,
573+
[Optional] bool? ShowScale,
574+
[Optional] bool? ReverseScale,
575+
[Optional] StyleParam.SmoothAlg? ZSmooth,
576+
[Optional] bool? UseDefaults
577+
)
578+
where XType : IConvertible
579+
where YType : IConvertible
580+
where ZType : IConvertible
581+
=>
582+
Plotly.NET.Chart2D.Chart.Histogram2D<XType, YType, IEnumerable<ZType>, ZType>(
583+
x: x,
584+
y: y,
585+
Z: Z.ToOption(),
586+
Name: Name.ToOption(),
587+
ShowLegend: ShowLegend.ToOptionV(),
588+
Opacity: Opacity.ToOptionV(),
589+
XGap: XGap.ToOptionV(),
590+
YGap: YGap.ToOptionV(),
591+
HistFunc: HistFunc.ToOption(),
592+
HistNorm: HistNorm.ToOption(),
593+
NBinsX: NBinsX.ToOptionV(),
594+
NBinsY: NBinsY.ToOptionV(),
595+
XBins: XBins.ToOption(),
596+
YBins: YBins.ToOption(),
597+
ColorBar: ColorBar.ToOption(),
598+
ColorScale: ColorScale.ToOption(),
599+
ShowScale: ShowScale.ToOptionV(),
600+
ReverseScale: ReverseScale.ToOptionV(),
601+
ZSmooth: ZSmooth.ToOption(),
602+
UseDefaults: UseDefaults.ToOptionV()
603+
);
531604
};
532605
}

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ static void Main(string[] args)
1111
{
1212
Chart.Grid(
1313
nRows: 8,
14-
nCols: 6,
14+
nCols: 7,
1515
gCharts:
1616
new GenericChart[]
1717
{
18-
Chart.Scatter<int,int,string>(
18+
//2D traces
19+
Chart.Scatter(
1920
x: new int [] { 1, 2 },
2021
y: new int [] { 3, 4 },
21-
mode: Mode.Markers
22+
mode: Mode.Markers,
23+
MultiText: new int [] { 3, 4 }
2224
),
2325
Chart.Point<int,int,string>(
2426
x: new int [] { 5, 6 },
@@ -33,12 +35,19 @@ static void Main(string[] args)
3335
Keys: new string [] { "first", "second"}
3436
),
3537
Chart.Column<int,string,string>(
36-
values: new int [] { 1,2 },
38+
values: new int [] { 3,4 },
3739
Keys: new string [] { "first", "second"}
3840
),
39-
Chart.Histogram<int,int,string>(
40-
X: new int [] { 1,2,2,2,3,4,5,5 }
41+
Chart.Histogram<int,int,int>(
42+
X: new int [] { 1,2,2,2,3,4,5,5 },
43+
MultiText: new int [] { 1,2,3,4,5,6,7}
4144
),
45+
Chart.Histogram2D<int,int,int>(
46+
x: new int [] { 1,2,2,2,3,4,5,5 },
47+
y: new int [] { 1,2,2,2,3,4,5,5 }
48+
),
49+
50+
//3D traces
4251
Chart.Scatter3D<int,int,int,string>(
4352
x: new int[] { 12, 13 },
4453
y: new int [] { 13, 14 },
@@ -50,6 +59,7 @@ static void Main(string[] args)
5059
Chart.Invisible(),
5160
Chart.Invisible(),
5261
Chart.Invisible(),
62+
Chart.Invisible(),
5363
Chart.ScatterPolar<int,int,string>(
5464
theta: new int [] { 1, 2 },
5565
r: new int [] { 3, 4 },
@@ -60,6 +70,7 @@ static void Main(string[] args)
6070
Chart.Invisible(),
6171
Chart.Invisible(),
6272
Chart.Invisible(),
73+
Chart.Invisible(),
6374
Chart.ScatterGeo<int,int,string>(
6475
longitudes: new int [] { 1, 2 },
6576
latitudes: new int [] { 3, 4 },
@@ -70,6 +81,7 @@ static void Main(string[] args)
7081
Chart.Invisible(),
7182
Chart.Invisible(),
7283
Chart.Invisible(),
84+
Chart.Invisible(),
7385
Chart.ScatterTernary<int,int,int,IConvertible,string>(
7486
A: new int [] { 1, 2 },
7587
B: new int [] { 3, 4 },
@@ -80,6 +92,7 @@ static void Main(string[] args)
8092
Chart.Invisible(),
8193
Chart.Invisible(),
8294
Chart.Invisible(),
95+
Chart.Invisible(),
8396
Chart.Carpet<double,double,double,double,double,double>(
8497
carpetId: "testCarpet",
8598
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},
@@ -91,6 +104,7 @@ static void Main(string[] args)
91104
Chart.Invisible(),
92105
Chart.Invisible(),
93106
Chart.Invisible(),
107+
Chart.Invisible(),
94108
Chart.Pie<double,string,string>(
95109
values: new double [] {1,2,3,4},
96110
Labels: new string [] {"soos", "saas", "fiif", "leel"}
@@ -100,6 +114,7 @@ static void Main(string[] args)
100114
Chart.Invisible(),
101115
Chart.Invisible(),
102116
Chart.Invisible(),
117+
Chart.Invisible(),
103118
Chart.ScatterSmith<double,double,string>(
104119
real: new double [] {1,2,3,4},
105120
imag: new double [] {1,2,3,4},
@@ -110,19 +125,11 @@ static void Main(string[] args)
110125
Chart.Invisible(),
111126
Chart.Invisible(),
112127
Chart.Invisible(),
128+
Chart.Invisible(),
113129
}
114130
)
115-
.WithSize(1250,2000)
131+
.WithSize(2000, 2000)
116132
.Show();
117-
118-
Chart.Point<int, int, string>(
119-
x: new int[] { 1, 2 },
120-
y: new int[] { 3, 4 }
121-
)
122-
.WithTraceInfo("Hello from C#", ShowLegend: true)
123-
.WithXAxisStyle<int, int, string>(TitleText: "x axis")
124-
.WithYAxisStyle<int, int, string>(TitleText: "y axis")
125-
.Show();
126133
}
127134
}
128135
}

0 commit comments

Comments
 (0)