Nearest point to a given x value
Nearest point to a given x value
Is there a way (other than looping through the points) to find the index of the point nearest a given X-axis value?
For example, the X-axis minimum value is 20 and the X-axis maximum value is 50, and I want to know which point in TChart1.Series(0) has an X-value closest to 23?
Jay
For example, the X-axis minimum value is 20 and the X-axis maximum value is 50, and I want to know which point in TChart1.Series(0) has an X-value closest to 23?
Jay
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi JayG,
At the moment I can think of 2 ways to achieve what you request:
1. Using NearestPoint tool to see the neares point to your mouse position in the chart.
2. Using an invisible CursorTool and its methods to calculate nearest point.
At the code snippet below you'll find an example of both options:
At the moment I can think of 2 ways to achieve what you request:
1. Using NearestPoint tool to see the neares point to your mouse position in the chart.
2. Using an invisible CursorTool and its methods to calculate nearest point.
At the code snippet below you'll find an example of both options:
Code: Select all
public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
tChart1.Aspect.View3D = false;
points1 = new Steema.TeeChart.Styles.Points(tChart1.Chart);
points1.FillSampleValues();
nearesPoint1 = new Steema.TeeChart.Tools.NearestPoint(tChart1.Chart);
nearesPoint1.Series = points1;
cursorTool1 = new Steema.TeeChart.Tools.CursorTool(tChart1.Chart);
cursorTool1.Active = false;
cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;
cursorTool1.Series = points1;
cursorTool1.Snap = true;
}
private void button1_Click(object sender, EventArgs e)
{
cursorTool1.XValue = Convert.ToDouble(textBox1.Text);
cursorTool1.SnapToPoint();
label1.Text = cursorTool1.XValue.ToString();
}
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 |
Nearest point to a given x value
The problem with those methods is that they rely on the mouse cursor. I want to find the point based on some variables independant of where the mouse is.
Jay
Jay
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi JayG,
Sorry but I'm afraid can't do anything else on that if you don't want to use mentioned features nor looping through series' ValueLists.
Since, by default, XValues ValueList is sorted in ascending order, the only solution I can think of is using any of the widely known search algorithms that don't loop through an entire array (ValueList in that case). You should find information about that kind of algorithms in internet.
Sorry but I'm afraid can't do anything else on that if you don't want to use mentioned features nor looping through series' ValueLists.
Since, by default, XValues ValueList is sorted in ascending order, the only solution I can think of is using any of the widely known search algorithms that don't loop through an entire array (ValueList in that case). You should find information about that kind of algorithms in internet.
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 |
Nearest point to a given x value
Narcís,
Thanks, I thought that might be the case, but I didn't want to spend time to include a search algorithm if there was already a TChart method that did the same thing.
Jay
Thanks, I thought that might be the case, but I didn't want to spend time to include a search algorithm if there was already a TChart method that did the same thing.
Jay
Hi
I am trying to do find the nearest series x value to a mouse clicked point and am basing my code on your example. as shown below:
The series has 2048 values (integers 0 to 2047). The first time this function is performed the _tlCursor.XValue always goes to 1022 although dX is not 1022, and the label displays "X = 1022". If the function is performed again then the correct version of X is shown.
Any suggestions?
I am trying to do find the nearest series x value to a mouse clicked point and am basing my code on your example. as shown below:
Code: Select all
foreach (Steema.TeeChart.Styles.Series ts in _tcChart.Series)
{
if (ts is Steema.TeeChart.Styles.Line)
{
_tlCursor.Series = ts;
double dX = ts.XScreenToValue(e.X);
_tlCursor.XValue = dX;
_tlCursor.SnapToPoint();
strLabel += string.Format("X = {0}", _tlCursor.XValue.ToString("0.##"));
}
}
Any suggestions?
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi jenb,
Why don't you try using a NearestPoint tool and retrieve its Point property in the desired mouse event?
Why don't you try using a NearestPoint tool and retrieve its Point property in the desired mouse event?
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 |
Hi Narcis
I am using the Nearest point tool for another case but in this case I want to find the nearest point in the x direction only (i.e. in a horizontal line from the clicked point to intersect with the series line), as far as I know the nearest point tool uses the shortest line to the series, and of course this line generally isn't horizontal.
Anyway your example using the cursor tool is exactly what I want, there's just a problem when it runs the first time.
Thanks
jenb
I am using the Nearest point tool for another case but in this case I want to find the nearest point in the x direction only (i.e. in a horizontal line from the clicked point to intersect with the series line), as far as I know the nearest point tool uses the shortest line to the series, and of course this line generally isn't horizontal.
Anyway your example using the cursor tool is exactly what I want, there's just a problem when it runs the first time.
Thanks
jenb
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi jenb,
Which example do you mean? Which is the exact problem with it? Maybe forcing the chart being internally repainted after its initalizations solves this issue:Anyway your example using the cursor tool is exactly what I want, there's just a problem when it runs the first time.
Code: Select all
Bitmap bmp = tChart1.Bitmap;
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 |
The example is as shown here:Which example do you mean?
The problem is this:public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
tChart1.Aspect.View3D = false;
points1 = new Steema.TeeChart.Styles.Points(tChart1.Chart);
points1.FillSampleValues();
nearesPoint1 = new Steema.TeeChart.Tools.NearestPoint(tChart1.Chart);
nearesPoint1.Series = points1;
cursorTool1 = new Steema.TeeChart.Tools.CursorTool(tChart1.Chart);
cursorTool1.Active = false;
cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;
cursorTool1.Series = points1;
cursorTool1.Snap = true;
}
private void button1_Click(object sender, EventArgs e)
{
cursorTool1.XValue = Convert.ToDouble(textBox1.Text);
cursorTool1.SnapToPoint();
label1.Text = cursorTool1.XValue.ToString();
}
I added Bitmap bmp = _tcChart.Bitmap before the call to set _tlCursor.XValue - it made no difference.I am trying to do find the nearest series x value to a mouse clicked point and am basing my code on your example. as shown below:
Code:
foreach (Steema.TeeChart.Styles.Series ts in _tcChart.Series)
{
if (ts is Steema.TeeChart.Styles.Line)
{
_tlCursor.Series = ts;
double dX = ts.XScreenToValue(e.X);
_tlCursor.XValue = dX;
_tlCursor.SnapToPoint();
strLabel += string.Format("X = {0}", _tlCursor.XValue.ToString("0.##"));
}
}
The series has 2048 values (integers 0 to 2047). The first time this function is performed the _tlCursor.XValue always goes to 1022 although dX is not 1022, and the label displays "X = 1022". If the function is performed again then the correct version of X is shown.
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi jenb,
Thanks for the information.
This seems a bug to me and I've added it (TF02012649) to our defect list to be fixed for future releases. In the meantime, a workaround is calling the cursor setting code twice the first time, for example:
Thanks for the information.
This seems a bug to me and I've added it (TF02012649) to our defect list to be fixed for future releases. In the meantime, a workaround is calling the cursor setting code twice the first time, for example:
Code: Select all
public Form1()
{
InitializeComponent();
InitializeChart();
}
private Steema.TeeChart.Styles.Line line1;
private Steema.TeeChart.Tools.CursorTool cursorTool1;
private bool firstTime;
private void InitializeChart()
{
tChart1.Clear();
tChart1.Aspect.View3D = false;
line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
line1.FillSampleValues();
cursorTool1 = new Steema.TeeChart.Tools.CursorTool(tChart1.Chart);
cursorTool1.Active = false;
cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;
cursorTool1.Series = line1;
cursorTool1.Snap = true;
firstTime = true;
}
private void tChart1_MouseClick(object sender, MouseEventArgs e)
{
foreach (Steema.TeeChart.Styles.Series ts in tChart1.Series)
{
if (ts is Steema.TeeChart.Styles.Line)
{
cursorTool1.Series = ts;
double dX;
dX = SetCursor(e.X, ts);
//Workaround
if (firstTime)
{
dX = SetCursor(e.X, ts);
firstTime = false;
}
label1.Text = string.Format("X = {0}", cursorTool1.XValue.ToString("0.##"));
}
}
}
private double SetCursor(int X, Steema.TeeChart.Styles.Series ts)
{
double dX;
dX = ts.XScreenToValue(X);
cursorTool1.XValue = dX;
cursorTool1.SnapToPoint();
return dX;
}
private void button1_Click(object sender, EventArgs e)
{
InitializeChart();
}
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 |