mschart control how do you add crosshairs and know the value of x and y on the line?

I have a chart control which shows a curved line.  I want to add crosshairs so when a user mouses over, I can locate the position that x and y coordinates and figure out the value of the line at that point.

I can add them using this:
http://stackoverflow.com/questions/8426134/add-crosshair-to-charts-in-a-winform-project

I am not sure how to capture the values as it relates to my line though.

Here is the code to create my line:
List<double> theoreticalPrices = new List<double>();
for (double i = 0; i < 100; i = i + .1)
{
      foreach (OrderChain.OpenContracts openContractse in TsalChain.GetOpenContracts())
      {
            totalAtThisStrike += openContractse.price;
      }
      chart1.Series["Series1"].Points.Add(new DataPoint(i, totalAtThisStrike));
}
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
jackjohnson44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
I added a label to the form for testing and added the below code to the MouseMove function. I think it does what you need:
            string yval = "N/A";
            int pointMax = chart1.Series["Series1"].Points.Count - 1;
            int pointNum = -1;
            try {
                double xval = Math.Min(chart1.ChartAreas["ChartArea1"].AxisX.Maximum, Math.Max(chart1.ChartAreas["ChartArea1"].AxisX.Minimum, chart1.ChartAreas["ChartArea1"].AxisX.PixelPositionToValue(_point.X)));
                pointNum = (int)Math.Round(pointMax * (xval - chart1.ChartAreas["ChartArea1"].AxisX.Minimum) / (chart1.ChartAreas["ChartArea1"].AxisX.Maximum - chart1.ChartAreas["ChartArea1"].AxisX.Minimum));
                yval = chart1.Series["Series1"].Points[pointNum].YValues[0].ToString();
            }
            catch (Exception) {
            }
            label1.Text = string.Format("point {0} of {1}, y value = {2}", 1 + pointNum, chart1.Series["Series1"].Points.Count, yval);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 7

From novice to tech pro — start learning today.