Extract nearest X,Y data from line series during mouseover event


https://www.nevron.com/Forum/Topic11867.aspx
Print Topic | Close Window

By Michael Rovak - 8 Years Ago
I'm doing an evaluation of this product. I'm using VS2015, C#, winforms.
I'd like to know how to extract X,Y data from a line series chart at the nearest data point to where I mouse over the plotted line in the chart.

Thanks.
By Nevron Support - 8 Years Ago
Hi Michael,

The following code snippet shows how to get the data point which is closest to the current mouse position and highlight it dynamically:

using System;
using System.Drawing;
using System.Windows.Forms;
using Nevron.Chart;
using Nevron.GraphicsCore;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
   public Form1()
   {
    InitializeComponent();
   }
   /// <summary>
   /// Handles mouse move
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
   void nChartControl1_MouseMove(object sender, MouseEventArgs e)
   {
    NChart chart = nChartControl1.Charts[0];
    NXYScatterSeries series = (NXYScatterSeries)chart.Series[0];
    NPointF mousePos = new NPointF(e.X, e.Y);

    // find the data point closest to the mouse
    double minDistance = GetDistanceFromDataPoint(series, 0, mousePos);
    int minDistanceIndex = 0;

    for (int i = 1; i < series.Values.Count; i++)
    {
      double curDistance = GetDistanceFromDataPoint(series, i, mousePos);
      if (curDistance < minDistance)
      {
       minDistance = curDistance;
       minDistanceIndex = i;
      }
    }

    // mark it
    series.MarkerStyles.Clear();

    NMarkerStyle ms = new NMarkerStyle();
    ms.Visible = true;
    ms.FillStyle = new NColorFillStyle(Color.Red);
    series.MarkerStyles[minDistanceIndex] = ms;

    nChartControl1.Refresh();
   }
   /// <summary>
   /// Gets the distance between a data point and the mouse
   /// </summary>
   /// <param name="series"></param>
   /// <param name="dataPointIndex"></param>
   /// <param name="mousePosition"></param>
   /// <returns></returns>
   private static double GetDistanceFromDataPoint(NXYScatterSeries series, int dataPointIndex, NPointF mousePosition)
   {
    NScale2DToViewTransformation view2Scale = new NScale2DToViewTransformation(series.Chart, (int)StandardAxis.PrimaryX, (int)StandardAxis.PrimaryY);

    NPointF dataPointPosition = new NPointF();
    view2Scale.Transform(new NVector2DD(series.UseXValues ? (double)series.XValues[dataPointIndex] : dataPointIndex, (double)series.Values[dataPointIndex]), ref dataPointPosition);

    double dx = mousePosition.X - dataPointPosition.X;
    double dy = mousePosition.Y - dataPointPosition.Y;

    return Math.Sqrt(dx * dx + dy * dy);
   }

   private void Form1_Load(object sender, EventArgs e)
   {
    NChart chart = nChartControl1.Charts[0];

    // create a dymmy line series
    NLineSeries line = new NLineSeries();

    line.DataLabelStyle.Visible = false;

    chart.Series.Add(line);

    Random rand = new Random();

    for (int i = 0; i < 20; i++)
    {
      line.Values.Add(rand.Next(100));
    }

    nChartControl1.MouseMove += new MouseEventHandler(nChartControl1_MouseMove);
   }
  }
}

Hope this helps - let us know if you meet any problems or have any questions.