• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1287
  • Last Modified:

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:

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;
1 Solution
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now