Find Nearest Series in case of chart with Multiple series

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
Anil Kumar
Newbie
Newbie
Posts: 18
Joined: Tue Sep 04, 2007 12:00 am

Find Nearest Series in case of chart with Multiple series

Post by Anil Kumar » Wed Feb 04, 2009 8:19 am

I have chart with Multiple series. How to find the nearest series for the given mouse pointer position?
I checked the 'NearestPoint' tool which returns the nearest point for given series.
Is there any way to find the nearest series from the given position?

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Post by Yeray » Wed Feb 04, 2009 11:07 am

Hi Anil,

The Nearest Point Tool needs to have assigned a series to calculate the nearest point to the mouse cursor. I've tried to convert this Delphi example to CSharp. In this example we obtain the nearest point for each series in the chart changing the series assigned to the tool and we compare the distances to the mouse cursor.

The problem is that the method GetNearestPoint in delphi is public but not in .NET so I've added this to the wish list and it will be implemented to the .NET version as soon as possible.

Then this code should work:

Code: Select all

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            tChart1.Draw();
        }

        private void tChart1_MouseMove(object sender, MouseEventArgs e)
        {
            int [] NearestPoints;
            int SeriesIndex, ValueIndex;
            double Dist, tmp;
            Point P1, P2;

            NearestPoints = new int[tChart1.Series.Count];
            P1 = new Point(0,0);
            P2 = new Point(tChart1.Width, tChart1.Height);
            SeriesIndex = 0;
            ValueIndex = 0;
            
            for (int i = 0; i < NearestPoints.Length; i++)
            {
                nearestPoint1.Series = tChart1[i];
                NearestPoints[i] = nearestPoint1.GetNearestPoint(new Point(e.X, e.Y));                     
            }

            nearestPoint1.Series = null;

            Dist = Distance(P1, P2);

            for (int i = 0; i < NearestPoints.Length; i++)
            {
                P1.X = tChart1[i].CalcXPos(NearestPoints[i]);
                P1.Y = tChart1[i].CalcYPos(NearestPoints[i]);
                P2.X = e.X;
                P2.Y = e.Y;
                tmp = Distance(P1, P2); 

                if ((i == 0) || (tmp < Dist))
                {
                    Dist = tmp;
                    SeriesIndex = i;
                    ValueIndex = NearestPoints[i];
                }
            }

            for (int i = 0; i < tChart1.Series.Count; i++)
            {
                if (i == SeriesIndex)
                {
                   ((Steema.TeeChart.Styles.Points)tChart1[i]).Pointer.Pen.Width = 3;
                }
                else
                {
                    ((Steema.TeeChart.Styles.Points)tChart1[i]).Pointer.Pen.Width = 1;
                }

                tChart1[i].RefreshSeries();
            }
            
            tChart1.Header.Text = "Series: " + SeriesIndex + " - ValueIndex: " + ValueIndex;
        }

        private double Distance(Point Pt1, Point Pt2)
        {
            int dx, dy;
            
            dx = Pt1.X - Pt2.X;
            dy = Pt1.Y - Pt2.Y;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            points1.FillSampleValues(25);
            points2.FillSampleValues(25);
            points3.FillSampleValues(25);
        }
    }
In the meanwhile, if you are source code customer, you could make the method GetNearestPoint public.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Fri Feb 06, 2009 10:09 am

Hi Anil,

GetNearestPoint has been made public for the next release which will be published imminently.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply