Page 1 of 1
Nearest point to a given x value
Posted: Thu Mar 29, 2007 9:29 pm
by 9792387
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
Posted: Fri Mar 30, 2007 10:39 am
by narcis
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:
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();
}
Nearest point to a given x value
Posted: Fri Mar 30, 2007 2:26 pm
by 9792387
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
Posted: Fri Mar 30, 2007 2:45 pm
by narcis
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.
Nearest point to a given x value
Posted: Fri Mar 30, 2007 3:56 pm
by 9792387
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
Posted: Thu Dec 13, 2007 2:30 pm
by 13045625
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:
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.##"));
}
}
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?
Posted: Thu Dec 13, 2007 3:24 pm
by narcis
Hi jenb,
Why don't you try using a NearestPoint tool and retrieve its Point property in the desired mouse event?
Posted: Thu Dec 13, 2007 3:31 pm
by 13045625
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
Posted: Thu Dec 13, 2007 3:38 pm
by narcis
Hi jenb,
Anyway your example using the cursor tool is exactly what I want, there's just a problem when it runs the first time.
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:
Posted: Thu Dec 13, 2007 3:55 pm
by 13045625
Which example do you mean?
The example is as shown here:
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();
}
The problem is this:
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.
I added Bitmap bmp = _tcChart.Bitmap before the call to set _tlCursor.XValue - it made no difference.
Posted: Fri Dec 14, 2007 9:02 am
by narcis
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:
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();
}