3

By default, the angles on the polar chart go from 0 to 360 in a clockwise direction but I want them to go counter clockwise (anticlockwise)

chart.ChartAreas[0].AxisX.Title = "Elevation";
chart.ChartAreas[0].AxisY.Title = "Power(dBm)";
chart.ChartAreas[0].BackColor = System.Drawing.Color.FromArgb(211, 223, 240);
chart.ChartAreas[0].BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
chart.ChartAreas[0].AxisY.IsStartedFromZero = false;
chart.PrePaint += new EventHandler<ChartPaintEventArgs>(chart_prePaint);

I've tried changing the labels per some example code I found like this:

CustomLabelsCollection labels = chart.ChartAreas[0].AxisX.CustomLabels;

if (labels == null) return;

for (int i = 0; i < labels.Count - 1; i++)
{
    if (labels[0].Text == "360") break;
    labels[i].Text = (360 - int.Parse(labels[i].Text)).ToString();
    labels[i].ToolTip = "Angle in Degrees";
}

The code changes the labels in the object but not on the graph. And every time the event is fired and we come back into this event handler, the labels have been reset to the way it was originally.And the tooltips have been reset.

To add to the confusion, I'm not sure why the CustomLabels object is populated in the first place - I didn't do it.

Any idea why the changes are having no effect?

Thanks in advance!

  • Do you have any reason the code the PrePaint event? Adding fixed labels there is not a good idea, as it may be called quite often.. – TaW Oct 04 '18 at 12:36

1 Answers1

1

If you want something like this:..

enter image description here

..CustomLabels are indeed a way to achieve it. I couldn't find a way make the axis reverse itself..

Here is the C# code I used:

Axis ay = chart.ChartAreas[0].AxisY;
ay.LabelStyle.Enabled = false;

Axis ax = chart.ChartAreas[0].AxisX;
ax.CustomLabels.Clear();

int  step = (int)ax.Interval;
if (step == 0) step = 30;

for (int i = 0; i < 360; i+=step)
{
    int a = 360 - i;             // the angle to target
    var cl = new CustomLabel();
    cl.Text = a + "°";
    cl.FromPosition = a + 0.01;  // create a small..
    cl.ToPosition = a - 0.01;    // ..space to place the label !
    ax.CustomLabels.Add(cl);
}

Note that only the Labels are reversed, not the values!

To start at 0 simply change the loop condition to <= and check for i>0 before creating the labels!

If you didn't set an Interval I use a default interval of 30; change as needed!


The CustomLabels collection by default is created, so it isn't null but it is empty (Count==0). If you didn't create any, then there are none and the original AxisLabels are showing. (Only one type be shown!)

Unless you have a really good reason, like very dynamic data, you should not add of modify anything in a xxxPaint event! They may be called quite often and these event are really just for drawing. ((And sometimes for measuring))

TaW
  • 53,122
  • 8
  • 69
  • 111