Page 1 of 1

Calc Midpoint of chart

Posted: Tue Aug 30, 2011 9:26 am
by 15657351
I need to calculate the x midpoint of a chart in order to plot a string.

I am using:

Midpoint = ((Points1.Chart.Axes.Bottom.Maximum - Points1.Chart.Axes.Bottom.Minimum) / 2) + Points1.Chart.Axes.Bottom.Minimum

This works fine for a manual scale but if I set the chart scale to automatic, the calculation is out by a few percent:

Points1.Chart.Axes.Bottom.AutomaticMaximum = True
Points1.Chart.Axes.Right.AutomaticMaximum = True
Points1.Chart.Axes.Left.AutomaticMaximum = True
Points1.Chart.Axes.Top.AutomaticMaximum = True
Points1.Chart.Axes.Right.AutomaticMinimum = True
Points1.Chart.Axes.Bottom.AutomaticMinimum = True
Points1.Chart.Axes.Left.AutomaticMinimum = True
Points1.Chart.Axes.Top.AutomaticMinimum = True

The Points1.Chart.Axes.Bottom.Maximum and Points1.Chart.Axes.Bottom.Minimum do not match the plotted min and max shown on the x axis.

Is there a better way to calculate the midpoint for automatic scale?

Re: Calc Midpoint of chart

Posted: Tue Aug 30, 2011 10:19 am
by yeray
Hello lilo,

I think the internal offset could be causing this confusion. However, I think the internal minimum and maximum are in general equal in an axis so I'm not sure to see how this could affect the calculated midpoint. What series are you using?

Another possibility would be using the axes IStartPos and IEndPos:

Code: Select all

            double minX = tChart1.Axes.Bottom.CalcPosPoint(tChart1.Axes.Bottom.IStartPos);
            double maxX = tChart1.Axes.Bottom.CalcPosPoint(tChart1.Axes.Bottom.IEndPos);
            double minY = tChart1.Axes.Left.CalcPosPoint(tChart1.Axes.Left.IEndPos);
            double maxY = tChart1.Axes.Left.CalcPosPoint(tChart1.Axes.Left.IStartPos);

            double midX = ((maxX - minX) / 2) + minX;
            double midY = ((maxY - minY) / 2) + minY;

Re: Calc Midpoint of chart

Posted: Tue Aug 30, 2011 10:48 am
by 15657351
This works, thank you

Re: Calc Midpoint of chart

Posted: Tue Aug 30, 2011 1:31 pm
by yeray
Hi lilo,

I'm happy to hear that. You're welcome! :)