Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now