2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ijb109
  • 942
  • 1
  • 19
  • 30

1 Answers1

2
  • 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 ;)

Community
  • 1
  • 1
bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • The second one doesn't quite work. The area for the chart doesn't change, but the width of the bars does. If I choose that route, it'll probably work to work with the width of each bar instead. Still going to try the first one. – ijb109 Jan 02 '13 at 15:20
  • @ijb109 I was just thinking of asking you how it went? I'll find sometime to code in with second route (no promise though) while you may try the `XAML` route. :) – bonCodigo Jan 02 '13 at 15:26
  • 1
    I've been investigating the `XAML` route, but it won't be a good fit because the application is written using ASP.Net and we are trying to avoid using Silverlight. I'm still trying to come up with different iterations on the above code to make it work. – ijb109 Jan 02 '13 at 16:49