Page 1 of 1

Resolve the closest point

Posted: Tue Sep 09, 2008 8:13 am
by 13046940
Hello, I am using TeeChart for .Net version 3.5.3146.24805.

In some of the chart events, I need to find which point (X value) on the Line series my mouse is closest to.

I currently keep chart of the mouse positions with:

Code: Select all

protected override void OnMouseMove(MouseEventArgs e)
{
	base.OnMouseMove(e);

	m_mouseXPos = e.X;
	m_mouseYPos = e.Y;
}
At any given time, when an event is raised in the chart, I would like to be able to figure out the following:

Let's say that my line series has X values of 1, 2, 3, 4 and 8

At the time I am trying to resolve the closest point, mouseXPos and mouseYPos could look like this (192, 263).

If my mouse is around X value of (5), I would like to find 4.
If my mouse is around X value of (7), I would like to find 8.

I can find the X value index with the following call:

Code: Select all

int XIndex = series.Clicked(m_mouseXPos, m_mouseYPos);
But the problem is that it always gives me the point to the left of the mouse, even if I am very close to the point at the right of the mouse.

How would you resolve the closest point to the mouse?

Many thanks.

Posted: Tue Sep 09, 2008 9:11 am
by narcis
Hi Sim,

The easiest way to achieve what you request is using NearestPoint tool as shown in the example at All Features\Welcome !\Tools\Nearest Point in the features demo, available at TeeChart's program group.

You may also be interested in using NearestPoint's Change event and Point property.

Hope this helps!

Posted: Tue Sep 09, 2008 9:53 am
by 13046940
Hello thanks for your prompt reply.

Unfortunately, the NearestPoint tool is a little buggy so I cannot use it. I had reported an internal crash related to the use of the nearest tool in the following post: http://www.teechart.net/support/viewtop ... highlight=

The issue was marked as "fixed" in the latest build of TeeChart. The issue that I could easily reproduce was indeed fixed (see post and attachment I had provided), but unfortunately there is another sequence of events that made the chart to crash again (big red X) because of the nearest tool. I don't know exactly what the sequence of event is, but I will eventually post the stack trace so that you guys can have a look.

Regarding today's question, I have actually found a solution that I believe tops the nearest point tool.

Code: Select all

We are inside the MarksTip_GetText(...) event
(...)

// Get the index of the nearest X value in the series that triggered the tooltip
int nearestPointIndex = series.Clicked(m_mouseXPos, m_mouseYPos);
					
if (series is Line && nearestPointIndex >= 0)
{
	// Try to resolve more accurate nearest point value for Line series
	Line line = (Line)series;

	if (line.XValues.Count > 1 && (nearestPointIndex + 1) < line.XValues.Count)
	{
		// The nearest point as resolved by "series.Clicked(...)"
		// is ALWAYS the point to the left of the mouse even if very close to the point to the right.
		// Here we are measuring the distance in pixels between the mouse and the two potential neareast
		// points and adjusting the "real" nearest point if need be.

		double leftPointScreenValue = line.CalcXPos(nearestPointIndex);			// Point to the left of the mouse
		double rightPointScreenValue = line.CalcXPos(nearestPointIndex + 1);	// Point to the right of the mouse

		if (Math.Abs(m_mouseXPos - rightPointScreenValue) < Math.Abs(m_mouseXPos - leftPointScreenValue))
		{
			nearestPointIndex = nearestPointIndex + 1;
		}
	}
}
Let me know what you think and thanks for your help.

Posted: Tue Sep 09, 2008 10:10 am
by narcis
Hi Sim,
The issue was marked as "fixed" in the latest build of TeeChart. The issue that I could easily reproduce was indeed fixed (see post and attachment I had provided), but unfortunately there is another sequence of events that made the chart to crash again (big red X) because of the nearest tool. I don't know exactly what the sequence of event is, but I will eventually post the stack trace so that you guys can have a look.
Thanks, it would be very helpful for us if you could find the exact steps that should be followed or send a simple example project we can run "as-is" to reproduce the problem here.
Regarding today's question, I have actually found a solution that I believe tops the nearest point tool.
This solution seems fine to me if it fit your need but I don't think this event will be fired every time you move the mouse at over the chart.

Posted: Tue Sep 09, 2008 10:20 am
by 13046940
Hello,
Thanks, it would be very helpful for us if you could find the exact steps that should be followed or send a simple example project we can run "as-is" to reproduce the problem here.
I will try to provide we the appropriate steps to make the chart crash, but can't guarantee.
This solution seems fine to me if it fit your need but I don't think this event will be fired every time you move the mouse at over the chart.
The end goal to my question was really to see if anything else than the nearest point tool existed to be used in the MarksTip_GetText(...) event to show an appropriate tooltip on the real nearest point to the mouse.

Thanks again!

Posted: Tue Sep 09, 2008 10:37 am
by narcis
Hi Sim,
I will try to provide we the appropriate steps to make the chart crash, but can't guarantee.
Great, thanks!
The end goal to my question was really to see if anything else than the nearest point tool existed to be used in the MarksTip_GetText(...) event to show an appropriate tooltip on the real nearest point to the mouse.
For displaying tooltips I'd use MarkTips tool or Annotation tools. You can set annotation's custom position as in the example I posted here.