C#-格式化柱形图

时间:2019-02-28 16:58:54

标签: c# charts alignment

大家晚上好。

尝试使用一些数据设置柱形图的格式,但是我遇到了问题。 所有列均以其标签居中。我希望每一列都在标签之间。

Here's the picture

代码:

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";

1 个答案:

答案 0 :(得分:0)

在我看来,您可以增加chArea.AxisX.IntervalOffset = 0.5;。也许尝试将其设置为1。

OR

您可以尝试使用图表对齐选项:

  

AlignDataPointsByAxisLabel()沿X轴对齐数据点   使用其轴标签。索引多个序列时适用   并且它们的X值是字符串。

     

AlignDataPointsByAxisLabel(String)对齐来自不同位置的数据点   沿X轴使用其轴标签进行序列化。指定系列   图表中的字母以升序排列。

     

AlignDataPointsByAxisLabel(PointSortOrder)使用以下方法对齐数据点   他们的轴标签。图表中的所有系列均使用   指定的排序顺序。

     

AlignDataPointsByAxisLabel(String,PointSortOrder)对齐数据点   使用他们的轴标签。

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

相关问题