Mohammad AL-othman
Mohammad AL-othman

Reputation: 21

C# chart using crosshair cursor

i'm working with C# chart class to display a curve. the type of the series is spline. the thing would i do is show a crosshair cursor in the chart area. the vertical line of this cursor should move with the mouse when the mouse enter the chart. but the horizontal line should move whit the curve line not with mouse movement. the code which i typed is here:

    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
        Point mousePoint = new Point(e.X, e.Y);
        chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(mousePoint,false);
        chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(mousePoint,false);
     }

this code cause the cursor move whith the mouse and the result is like in this image: enter image description here

to make the horizontal line of the cursor follow the curve line which is a sinusoidal signal here. i should know the Point of intersection between the vertical line of the cursor and the curve line. as this picture show:

enter image description here

is there any direct way to find this point? any help plz!.

Upvotes: 1

Views: 5587

Answers (2)

antonioBraccio
antonioBraccio

Reputation: 1

In this function I get the position of cursor X with the method GetXOfCursor(); after that I take the series point with this statement:

points = _theChart.Series[i].Points;

After with the last if statement I see the point that matches with the position X of the cursor and I calculate the mean value of Y of this 2 Point because the point intercepted with the cursor is an interpolation line made by the windows chart control

public string GetPointInterceptedCursorX()
        {
            string values = string.Empty;
            // Far uscire le etichette ai punti intercettati
            var xPosCursor = GetXOfCursor();
            DataPointCollection points = null;

            for (int i = 0; i < _theChart.Series.Count; i++)
            {
                if (_theChart.Series[i].BorderWidth == ChartConst.THICKNESS_LINE_ENABLED)
                {
                    points = _theChart.Series[i].Points;
                    break;
                }
            }
            if (points == null) return "No Curve Selected";
            for (int i = 0; i < points.Count - 1; i++)
            {
                if ((xPosCursor > points[i].XValue & xPosCursor < points[i + 1].XValue) | xPosCursor > points[i + 1].XValue & xPosCursor < points[i].XValue)
                {
                    var Yval = (points[i].YValues[0] + points[i + 1].YValues[0]) / 2;

                    values += $"Y= {Yval} {Environment.NewLine}";
                }
            }
            values += "X=" + " " + String.Format("{0:0.00}", xPosCursor);
            return values;
        }

Upvotes: 0

DeathTails
DeathTails

Reputation: 468

You can get the y pixel-offset relative to the top of the chart area if you know the following:

  • height = Chart height in pixels
  • rangeMin/rangeMax = Chart range min/max (here -150/150)
  • value = function value (bottom image, approx. 75)

Taking the following formula:
yOffset = height * (rangeMax - value) / (rangeMax - rangeMin);

You should be able to plug yOffset into your MouseMove function like so:

private void chart1_MouseMove(object sender, MouseEventArgs e)
{
    double yOffset = GetYOffset(chart1, e.X);
    Point mousePoint = new Point(e.X, yOffset);
    chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(mousePoint,false);
    chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(mousePoint,false);
}

// ChartClass chart is just whatever chart you're using
// x is used here I'm assuming to find f(x), your value
private double GetYOffset(ChartClass chart, double x)
{
    double yOffset;
    // yOffset = height * (rangeMax - value) / (rangeMax - rangeMin);
    return yOffset;
}

Upvotes: 0

Related Questions