Select Nearest Point.

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
amol
Advanced
Posts: 231
Joined: Tue Mar 29, 2005 5:00 am

Select Nearest Point.

Post by amol » Fri Sep 11, 2015 6:39 am

Hi steeema,

We have some more query related to point selection of Tchart series given as below,

1. When we move the mouse on tchart series, if Mouse current position lies between two points, we want to select or Highlight both points of the Tchart series.

Thanks,

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Select Nearest Point.

Post by Christopher » Mon Sep 14, 2015 9:55 am

amol wrote:1. When we move the mouse on tchart series, if Mouse current position lies between two points, we want to select or Highlight both points of the Tchart series.
You could try something similar to:

Code: Select all

    Points series;
    private void InitializeChart()
    {
      tChart1.Aspect.View3D = false;
      series = new Points(tChart1.Chart);
      series.FillSampleValues(50);


      tChart1.MouseMove += tChart1_MouseMove;
    }

    void tChart1_MouseMove(object sender, MouseEventArgs e)
    {
      Tuple<int, int> result = GetNearestPoints(series, e.Location);

      tChart1.Header.Text = "first: " + result.Item1 + " second: " + result.Item2;

      for (int i = 0; i < series.Count; i++)
      {
        series[i].Color = series.Color;
      }

      if (result.Item1 > -1)
      {
        series[result.Item1].Color = Color.Red;
      }
      if (result.Item2 > -1)
      {
        series[result.Item2].Color = Color.Red;
      }
    }

    private bool GetFirstLastSeries(Steema.TeeChart.Styles.Series s, out int AMin, out int AMax)
    {
      AMin = s.FirstVisibleIndex;
      if (AMin < 0)
        AMin = 0;

      AMax = s.LastVisibleIndex;
      if (AMax < 0)
        AMax = s.Count - 1;
      else
        if (AMax >= s.Count)
          AMax = s.Count - 1; // 5.03

      return (s.Count > 0) && (AMin <= s.Count) && (AMax <= s.Count);
    }

    public Tuple<int, int> GetNearestPoints(Series iSeries, Point p)
    {
      int tmpMin;
      int tmpMax;

      List<int> Dist = new List<int>();
      if (GetFirstLastSeries(iSeries, out tmpMin, out tmpMax))

        // traverse all points in a Series... 
        for (int t = tmpMin; t <= tmpMax; t++)
        {

          int tmpX;
          int tmpY;
          Rectangle r = iSeries.Chart.ChartRect;

          iSeries.CalcSelectionPos(t, out tmpX, out tmpY);

          if (r.Contains(tmpX, tmpY))
          { 

            // calculate distance in pixels... 
            Dist.Add(Utils.Round(Math.Sqrt(Utils.Sqr(p.X - tmpX) + Utils.Sqr(p.Y - tmpY))));
          }
        }

      if(Dist.Count > 0)
      {
        int first = Dist.Min();
        int second = Dist.OrderByDescending(e => e).Skip(Dist.Count - 2).First();

        int oneIndex = Dist.IndexOf(first);
        int twoIndex = Dist.IndexOf(second);

        return new Tuple<int, int>(oneIndex, twoIndex);
      
      }
      else
      {
        return new Tuple<int, int>(-1, -1);
      }
    }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply