Page 1 of 1

Find Nearest Series in case of chart with Multiple series

Posted: Wed Feb 04, 2009 8:19 am
by 13046610
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?

Posted: Wed Feb 04, 2009 11:07 am
by yeray
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.

Posted: Fri Feb 06, 2009 10:09 am
by narcis
Hi Anil,

GetNearestPoint has been made public for the next release which will be published imminently.