Page 1 of 1
Select Nearest Point.
Posted: Fri Sep 11, 2015 6:39 am
by 9526439
Hi steeema,
We have some more query related to point selection of Tchart series given as below,
1. When we move the mouse on tchart series, if Mouse current position lies between two points, we want to select or Highlight both points of the Tchart series.
Thanks,
Re: Select Nearest Point.
Posted: Mon Sep 14, 2015 9:55 am
by Christopher
amol wrote:1. When we move the mouse on tchart series, if Mouse current position lies between two points, we want to select or Highlight both points of the Tchart series.
You could try something similar to:
Code: Select all
Points series;
private void InitializeChart()
{
tChart1.Aspect.View3D = false;
series = new Points(tChart1.Chart);
series.FillSampleValues(50);
tChart1.MouseMove += tChart1_MouseMove;
}
void tChart1_MouseMove(object sender, MouseEventArgs e)
{
Tuple<int, int> result = GetNearestPoints(series, e.Location);
tChart1.Header.Text = "first: " + result.Item1 + " second: " + result.Item2;
for (int i = 0; i < series.Count; i++)
{
series[i].Color = series.Color;
}
if (result.Item1 > -1)
{
series[result.Item1].Color = Color.Red;
}
if (result.Item2 > -1)
{
series[result.Item2].Color = Color.Red;
}
}
private bool GetFirstLastSeries(Steema.TeeChart.Styles.Series s, out int AMin, out int AMax)
{
AMin = s.FirstVisibleIndex;
if (AMin < 0)
AMin = 0;
AMax = s.LastVisibleIndex;
if (AMax < 0)
AMax = s.Count - 1;
else
if (AMax >= s.Count)
AMax = s.Count - 1; // 5.03
return (s.Count > 0) && (AMin <= s.Count) && (AMax <= s.Count);
}
public Tuple<int, int> GetNearestPoints(Series iSeries, Point p)
{
int tmpMin;
int tmpMax;
List<int> Dist = new List<int>();
if (GetFirstLastSeries(iSeries, out tmpMin, out tmpMax))
// traverse all points in a Series...
for (int t = tmpMin; t <= tmpMax; t++)
{
int tmpX;
int tmpY;
Rectangle r = iSeries.Chart.ChartRect;
iSeries.CalcSelectionPos(t, out tmpX, out tmpY);
if (r.Contains(tmpX, tmpY))
{
// calculate distance in pixels...
Dist.Add(Utils.Round(Math.Sqrt(Utils.Sqr(p.X - tmpX) + Utils.Sqr(p.Y - tmpY))));
}
}
if(Dist.Count > 0)
{
int first = Dist.Min();
int second = Dist.OrderByDescending(e => e).Skip(Dist.Count - 2).First();
int oneIndex = Dist.IndexOf(first);
int twoIndex = Dist.IndexOf(second);
return new Tuple<int, int>(oneIndex, twoIndex);
}
else
{
return new Tuple<int, int>(-1, -1);
}
}