Solved

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

Posted on 2016-07-29
4
108 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS subreport with parameter 3 29
Service Controller 1 23
Modal Popup Extender control 1 18
ASP.NET(C#) Eliminating weekends from a date range 2 33
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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