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?
Find Nearest Series in case of chart with Multiple series
-
- Newbie
- Posts: 18
- Joined: Tue Sep 04, 2007 12:00 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:
In the meanwhile, if you are source code customer, you could make the method GetNearestPoint public.
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);
}
}
Best Regards,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi Anil,
GetNearestPoint has been made public for the next release which will be published imminently.
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 |
Instructions - How to post in this forum |