'Winform Charting Histogram - Draw bar from start to end of bin
I'm trying to create a proper histogram using the standard charting library in Winforms C#. Currently my histogram looks as follows:
var dataPointSeries = new System.Windows.Forms.DataVisualization.Charting.Series
{
Name = chartName,
Color = Colors.blue,
IsVisibleInLegend = false,
ChartType = SeriesChartType.Column,
MarkerStyle = MarkerStyle.None,
BorderWidth = 1,
BorderColor = Color.White
};
for (int i = 0; i < data.hDataPoints.Count(); i++)
{
dataPointSeries.Points.AddXY(Charts.Functions.RoundValue(data.hDataPoints[i].fromBin), data.hDataPoints[i].probability);
}
chart.Series.Add(dataPointSeries);
chart.Series[0]["PointWidth"] = "1";
The problem that I'm having is the bars are not displayed correctly. For a histogram, the bar needs to start at the lower bin and end at the upper bin. The chart control draws the bar in the middle of the upper or lower bin depending on which one you choose as the X data point.
Any idea how to fix this?
Solution 1:[1]
TaW's answer about dividing the interval in half is correct, and will center the bar between two values (as desired in historgrams which are used to represent bins, not discrete values).
But to get a truly gapless histogram - if that is the look you are going for - add the following code:
chart.Series[0]["PointWidth"] = "1";
This should push the thickness of your bars to the full width of your range.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paniom |

