简体   繁体   中英

C# - Formatting Column Chart

Good evening everyone.

Trying to format the column chart with some data, and I've experiencing a problem. All columns are centered at their labels. I want each column to be between the labels.

Here's the picture

Code:

Random rnd = new Random();
Chart myChart = new Chart();
myChart.Parent = this;
myChart.Size = new System.Drawing.Size(300, 185);
myChart.Location = new Point(865, 56);
myChart.Titles.Add("Distribution of clients per time span");
ChartArea chArea = new ChartArea("clientsDistribution");
chArea.AxisX.Title = "Time span";
chArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;
chArea.AxisY.Title = "Count";
chArea.AxisY.LabelAutoFitMaxFontSize = 5;
myChart.ChartAreas.Add(chArea);
Series mySeriesOfPoint = new Series("timeSpans");
mySeriesOfPoint.ChartType = SeriesChartType.Column;
mySeriesOfPoint.ChartArea = "clientsDistribution";
for (int x = 0; x < 9; x++)
{
    mySeriesOfPoint.Points.AddXY(x + 9, countByTimeSpan[x]);
    mySeriesOfPoint.Points[x].Color = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
}
myChart.Series.Add(mySeriesOfPoint);
myChart.Series["timeSpans"]["PointWidth"] = "1";

It appears to me that you could increase the chArea.AxisX.IntervalOffset = 0.5; . Maybe try setting it to 1.

OR

You could try using chart alignment options:

AlignDataPointsByAxisLabel() Aligns data points along the X axis using their axis labels. Applicable when multiple series are indexed and their X-values are strings.

AlignDataPointsByAxisLabel(String) Aligns data points from different series along the X axis using their axis labels. The specified series in the chart are aligned using an ascending sort order.

AlignDataPointsByAxisLabel(PointSortOrder) Aligns data points using their axis labels. All series in the chart are aligned, using the specified sort order.

AlignDataPointsByAxisLabel(String, PointSortOrder) Aligns data points using their axis labels.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datavisualization.charting.chart.aligndatapointsbyaxislabel?view=netframework-4.7.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM