1
votes

I am creating a StackedColumn chart and I am unable to change the orientation of the custom x axis labels, which are always oriented vertically. My code below is in the Load event of a Form containing a Chart object named 'chart.'

The lines that are commented out are attempted fixes that I found while researching. These changes either made the labels disappear or did not have any effect:

  1. Toggling the IsLabelAutoFit property of the chart area's x-axis.
  2. Changing the LabelAutoFitStyle property (also tested with #1).
  3. Changing the IntervalType and Interval properties (also tested with #1).
  4. Toggling the LabelStyle.Enabled property (also tested with #1).
  5. Changing the LabelStyle.Angle property (also tested with #1).

Here is my code:

chart.BackColor = Color.FromArgb(211, 223, 240);
chart.BackGradientStyle = GradientStyle.TopBottom;
chart.BorderlineColor = Color.FromArgb(26, 59, 105);
chart.BorderlineDashStyle = ChartDashStyle.Solid;
chart.BorderlineWidth = 2;
chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
chart.Name = "Chart1";
chart.TabIndex = 1;

var title = new Title();

title.Alignment = ContentAlignment.TopCenter;
title.ForeColor = Color.FromArgb(26, 59, 105);
title.Font = new Font("Segoe UI", 14.25F, FontStyle.Bold);
title.ShadowColor = Color.FromArgb(32, 0, 0, 0);
title.ShadowOffset = 3;
title.Text = "Sales Report";

chart.Titles.Clear();
chart.Titles.Add(title);

var chartArea = new ChartArea();

chartArea.AxisX.Title = "Product Sold";
chartArea.AxisX.LabelStyle.Font = new Font("Segoe UI", 8.25F, FontStyle.Bold);
chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);

chartArea.AxisX.IsLabelAutoFit = false;
chartArea.AxisX.LabelStyle.Enabled = true;

//chartArea.AxisX.IntervalType = DateTimeIntervalType.Number;
//chartArea.AxisX.Interval = 1;

//chartArea.AxisX.IsLabelAutoFit = true;

//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.IncreaseFont;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep30;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep45;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep90;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.StaggeredLabels;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;

//chartArea.AxisX.LabelStyle.Enabled = false;

//chartArea.AxisX.LabelStyle.Angle = 0;
//chartArea.AxisX.LabelStyle.Angle = 30;

chartArea.AxisY.IsLabelAutoFit = false;
chartArea.AxisY.Title = "Number of Closed Sales";
chartArea.AxisY.LabelStyle.Font = new Font("Segoe UI", 8.25F, FontStyle.Bold);
chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);

chartArea.Area3DStyle.Enable3D = true;
chartArea.Area3DStyle.LightStyle = LightStyle.Simplistic;
chartArea.Area3DStyle.Inclination = 15;
chartArea.Area3DStyle.Rotation = 10;
chartArea.Area3DStyle.WallWidth = 0;

chartArea.BackColor = Color.FromArgb(64, 165, 191, 228);
chartArea.BackGradientStyle = GradientStyle.TopBottom;
chartArea.BackSecondaryColor = Color.Transparent;

chartArea.BorderColor = Color.FromArgb(64, 64, 64, 64);
chartArea.BorderDashStyle = ChartDashStyle.Solid;

chartArea.Name = "Default";
chartArea.Position.Auto = true;
chartArea.ShadowColor = Color.Transparent;

chart.ChartAreas.Clear();
chart.ChartAreas.Add(chartArea);

var legend = new Legend();

legend.BackColor = Color.Transparent;
legend.Enabled = true;
legend.Font = new Font("Segoe UI", 8.25F, FontStyle.Bold);
legend.IsTextAutoFit = false;
legend.Docking = Docking.Top;
legend.IsDockedInsideChartArea = false;
legend.Alignment = StringAlignment.Center;
legend.DockedToChartArea = "Default";
legend.LegendStyle = LegendStyle.Row;
legend.Name = "Default";

chart.Legends.Clear();
chart.Legends.Add(legend);

chart.ChartAreas["Default"].AxisX.CustomLabels.Clear();

var customLabel1 = new CustomLabel(1, 1, "Product A", 0, LabelMarkStyle.None);
var customLabel2 = new CustomLabel(2, 2, "Product B", 0, LabelMarkStyle.None);

chart.ChartAreas["Default"].AxisX.CustomLabels.Add(customLabel1);
chart.ChartAreas["Default"].AxisX.CustomLabels.Add(customLabel2);

chart.Series.Clear();

var newSeries1 = new Series();

newSeries1.BorderColor = Color.FromArgb(180, 26, 59, 105);
newSeries1.ChartArea = "Default";
newSeries1.ChartType = SeriesChartType.StackedColumn;
newSeries1.IsValueShownAsLabel = false;
newSeries1.Color = Color.FromArgb(255, 0, 0);
newSeries1.Legend = "Default";
newSeries1.Name = "Aaron";

var newSeries2 = new Series();

newSeries2.BorderColor = Color.FromArgb(180, 26, 59, 105);
newSeries2.ChartArea = "Default";
newSeries2.ChartType = SeriesChartType.StackedColumn;
newSeries2.IsValueShownAsLabel = false;
newSeries2.Color = Color.FromArgb(0, 255, 0);
newSeries2.Legend = "Default";
newSeries2.Name = "Tom";

var newSeries3 = new Series();

newSeries3.BorderColor = Color.FromArgb(180, 26, 59, 105);
newSeries3.ChartArea = "Default";
newSeries3.ChartType = SeriesChartType.StackedColumn;
newSeries3.IsValueShownAsLabel = false;
newSeries3.Color = Color.FromArgb(0, 255, 255);
newSeries3.Legend = "Default";
newSeries3.Name = "Ethan";

chart.Series.Add(newSeries1);
chart.Series.Add(newSeries2);
chart.Series.Add(newSeries3);

chart.Series["Aaron"].Points.AddXY(1, 6);
chart.Series["Aaron"].Points.AddXY(2, 3);

chart.Series["Tom"].Points.AddXY(1, 2);
chart.Series["Tom"].Points.AddXY(2, 4);

chart.Series["Ethan"].Points.AddXY(1, 1);
chart.Series["Ethan"].Points.AddXY(2, 7);

I don't have enough reputation to post an image, but I uploaded one here:

http://imgur.com/JJLyHN8

The x-axis should show "Product A" and "Product B" as labels for the two groups of stacked columns. The custom labels appear to be shown, but the text is oriented vertically and cannot be completely read.

1

1 Answers

0
votes

Change this:

var customLabel1 = new CustomLabel(1, 1, "Product A", 0, LabelMarkStyle.None);
var customLabel2 = new CustomLabel(2, 2, "Product B", 0, LabelMarkStyle.None);

To This:

    var customLabel1 = new CustomLabel(0.5, 1.5, "Product A", 0, LabelMarkStyle.None);
    var customLabel2 = new CustomLabel(1.5, 2.5, "Product B", 0, LabelMarkStyle.None);

Your example does not work because the range of the points is 0; you can read about the proper way to do it explained here