Solved

C#, VS15, Chart, How to snap mouse cursor to line

Posted on 2016-07-29
4
194 Views
Last Modified: 2016-08-22
I managed to snap the x-axis on chart line (microsoft chart). But not sure how to snap to y-axis, which bring mouse cross line to line and follow it.
I could not find suitable answser in website.....

Attached code,

        private void cScope_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X < 0 || e.Y < 0 || e.Location == prevPos)
                return;
            prevPos = e.Location;
            PointF CursorPointer = new PointF(e.Location.X, e.Location.Y);
            cScope.ChartAreas[0].CursorY.SetCursorPixelPosition(CursorPointer, true);
            
            //cScope.ChartAreas[0].CursorX.SetCursorPixelPosition(CursorPointer, true);
            double searchVal;
            Point p = new Point(e.X, e.Y);
            try
            {
                searchVal = cScope.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
            }
            catch { return; }
            switch (iCursorSelectChannel)
            {
                case (0):
                    {
                        if (DSCH0 != null)
                        {
                            cScopeMouseMoveChannel(0, searchVal);
                            tbxAxisYAmp.BackColor = DSCH0.color;
                        }
                        break;
                    }
...etc
}

Open in new window


and

        private void cScopeMouseMoveChannel(int cc, double searchVal)
        {
            double xx = 0;
            double x0 = 0, x1 = 0, x2 = 0;
            double xup = 0, xdown = 0;
            int i = 0;
            try
            {
                while (i <= cScope.Series[cc].Points.Count - 1)
                {
                    if (i == 0)
                    {
                        xup = (cScope.Series[cc].Points[i + 1].XValue / 2);
                        if ((searchVal >= 0) & (searchVal <= xup))
                        {
                            xx = cScope.Series[cc].Points[0].XValue;
                            cScope.ChartAreas[0].CursorX.SetCursorPosition(xx);
                            foreach (double yD in cScope.Series[cc].Points[0].YValues)
                            {
                                CScopeUpdateAxisYAmpReadout(yD);
                            }
                            break;
                        }

                    }
                    else if (i == cScope.Series[cc].Points.Count - 1)
                    {
                        x0 = cScope.Series[cc].Points[i - 1].XValue;
                        x1 = cScope.Series[cc].Points[i].XValue;
                        xdown = x1 - ((x1 - x0) / 2);
                        if ((searchVal >= xdown))
                        {
                            xx = cScope.Series[cc].Points[i].XValue;
                            cScope.ChartAreas[0].CursorX.SetCursorPosition(xx);
                            foreach (double yD in cScope.Series[cc].Points[i].YValues)
                            {
                                CScopeUpdateAxisYAmpReadout(yD);
                            }
                            break;
                        }

                    }
                    else
                    {
                        x0 = cScope.Series[cc].Points[i - 1].XValue;
                        x1 = cScope.Series[cc].Points[i].XValue;
                        x2 = cScope.Series[cc].Points[i + 1].XValue;
                        xdown = x1 - ((x1 - x0) / 2);
                        xup = x1 + ((x2 - x1) / 2);
                        if ((searchVal >= xdown) & (searchVal <= xup))
                        {
                            xx = cScope.Series[cc].Points[i].XValue;
                            cScope.ChartAreas[0].CursorX.SetCursorPosition(xx);
                            foreach (double yD in cScope.Series[cc].Points[i].YValues)
                            {
                                CScopeUpdateAxisYAmpReadout(yD);
                            }
                            break;
                        }
                    }
                    i++;
                }
            }
            catch
            {
                tbxAxisXTime.Text = "";
                tbxAxisYAmp.Text = "";
            }
            tbxAxisXTime.Text = string.Format("{0}", xx);
        }
        #endregion

Open in new window

0
Comment
Question by:Richard Payne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points (awarded by participants)
ID: 41736678
I assume you have tried something like this (if that is what you intended to do in that function):
        void CScopeUpdateAxisYAmpReadout(double y) {
            cScope.ChartAreas[0].CursorY.SetCursorPosition(y);
        }

Open in new window


I found 2 issues with that:

1) it keeps jumping from 1 value to the other, to solve that comment out line 7 from the first block (cScope_MouseMove function) like you did with the CursorX setting there.

2) it may seem that it still doesn't work depending on what values are on your Y axis's cursor settings. You may need to adjust the cursor interval setting; you can do this perhaps in Form_Load or where you set your Chart data:
cScope.ChartAreas[0].CursorY.Interval = 0.001;

Open in new window


Note that this is the Interval on the CursorY, not AxisY.

For testing I added the coordinates in the title bar and you can see it in the picture below.
                            xx = cScope.Series[cc].Points[i].XValue;
                            cScope.ChartAreas[0].CursorX.SetCursorPosition(xx);
                            foreach (double yD in cScope.Series[cc].Points[i].YValues)
                            {
                                this.Text = string.Format("({0}, {1})", xx, yD); // set Form title, for debugging only
                                CScopeUpdateAxisYAmpReadout(yD);
                            }

Open in new window

capture
1
 

Author Comment

by:Richard Payne
ID: 41757640
I accept his answer and aplogise for not responsing. I have been very busy of the late.

I cannot see accept answer from the screen, so I powerless to make it happen.

I think the period is way too short......
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question