Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-07-29
4
Medium Priority
?
271 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
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

782 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