ijb109
ijb109

Reputation: 972

.NET chart custom label rotation

I am making a chart in C# using custom labels on the X-axis. The scale is in dates, and it changes based on the selected values (from one month to two years). It works well and the width of the chart stays constant whenever I change the value of the scale; it just adjusts the bar width.

The problem happens when I try to rotate the labels. When I do, it resizes all of the bars differently on each different scale and never takes up the same amount of space as the originals. I want to be able to rotate the labels without resizing everything. Can I do this? Why is this happening? How can I fix it?

The code I'm using to add the custom labels is this:

DateTime StartMonthPos = XValues[0];
DateTime EndPos = new DateTime();

if (Time == 6 || Time == 12 || Time == 24)
{
    foreach (DateTime Date in XValues)
    {
        EndPos = Date;

        if (Date.Month != month)
        {
            Chart4.ChartAreas[0].AxisX.CustomLabels.Add(StartMonthPos.ToOADate(), EndPos.ToOADate(), StartMonthPos.ToString("MMMM"), 0, LabelMarkStyle.None);
            StartMonthPos = Date;
        }

        month = Date.Month;
    }

    XAxis.CustomLabels.Add(StartMonthPos.ToOADate(), EndPos.ToOADate(), StartMonthPos.ToString("MMMM"), 0, LabelMarkStyle.None);
}
else
{
    foreach (DateTime Date in XValues)
    {
        EndPos = Date;
        Chart4.ChartAreas[0].AxisX.CustomLabels.Add(StartMonthPos.ToOADate(), EndPos.ToOADate(), StartMonthPos.ToString("M"), 0, LabelMarkStyle.None);
        StartMonthPos = Date;
    }

    XAxis.CustomLabels.Add(StartMonthPos.ToOADate(), EndPos.ToOADate(), StartMonthPos.ToString("M"), 0, LabelMarkStyle.None);
}

If I add this line of code after that, things get messed up:

 Chart4.ChartAreas[0].AxisX.LabelStyle.Angle = 0;

Here is a picture from before the problematic code:

Enter image description here

And here is the after picture:

Enter image description here

Upvotes: 2

Views: 3653

Answers (1)

bonCodigo
bonCodigo

Reputation: 14361

  • Please check this article on how to easily rotate axis lables of a WPF chart.

    "The key here is to customize the Template of the AxisLabel instances that are used to render the labels. And it's quite simple to do so by providing a Style with a Template Setter for the AxisLabelStyle property of the Axis subclass"...

That post involves quite a lot of XAML though...

As you can see this line alone can't get the rotation:

Chart4.ChartAreas[0].AxisX.LabelStyle.Angle = 0;

There's more to the rotation ;)

Upvotes: 2

Related Questions