Get all Points within Rectangle.

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
amol
Advanced
Posts: 231
Joined: Tue Mar 29, 2005 5:00 am

Get all Points within Rectangle.

Post by amol » Fri Sep 11, 2015 6:48 am

Hi steema,

We have one more query related to Points present in rectangle.

1. We create a dynamic rectangle on the Tchart and we want to get all the points within the rectangle.

thanks,

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Get all Points within Rectangle.

Post by Christopher » Mon Sep 14, 2015 10:53 am

amol wrote:1. We create a dynamic rectangle on the Tchart and we want to get all the points within the rectangle.
You can try something similar to this:

Code: Select all

    Points series;
    private void InitializeChart()
    {
      tChart1.Aspect.View3D = false;
      tChart1.Zoom.Direction = ZoomDirections.None;
      series = new Points(tChart1.Chart);
      series.FillSampleValues(50);

      tChart1.MouseDown += tChart1_MouseDown;
      tChart1.MouseUp += tChart1_MouseUp;
      tChart1.MouseMove += tChart1_MouseMove;

      tChart1.AfterDraw += tChart1_AfterDraw;
    }

    void tChart1_AfterDraw(object sender, Graphics3D g)
    {
      if (startPoint != Point.Empty && movingPoint != Point.Empty)
      {
        g.Brush.Visible = false;
        g.Pen.Color = Color.Red;
        g.Rectangle(Utils.FromLTRB(startPoint.X, startPoint.Y, movingPoint.X, movingPoint.Y));
      }
    }

    void tChart1_MouseUp(object sender, MouseEventArgs e)
    {
      endPoint = e.Location;

      rect = Utils.FromLTRB(startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
      startPoint = Point.Empty;
      endPoint = Point.Empty;
      movingPoint = Point.Empty;
      List<int> selection = CalcPointsInRect();
      tChart1.Invalidate();

      for (int i = 0; i < series.Count; i++)
      {
        series[i].Color = series.Color;
      }

      for (int i = 0; i < selection.Count; i++)
      {
        series[selection[i]].Color = Color.Red;
      }
    }

    private List<int> CalcPointsInRect()
    {
      double xMin = series.XScreenToValue(rect.Left);
      double xMax = series.XScreenToValue(rect.Right);

      double yMin = series.YScreenToValue(rect.Bottom);
      double yMax = series.YScreenToValue(rect.Top);

      List<int> result = new List<int>();


      for (int i = 0; i < series.Count; i++)
      {
        if(series[i].X >= xMin && series[i].X <= xMax && series[i].Y >= yMin && series[i].Y <= yMax)
        {
          result.Add(i);
        }
      }

      return result;
    }


    Rectangle rect;
    Point startPoint = Point.Empty;
    Point endPoint = Point.Empty;
    Point movingPoint = Point.Empty;

    void tChart1_MouseDown(object sender, MouseEventArgs e)
    {
      startPoint = e.Location;
    }

    void tChart1_MouseMove(object sender, MouseEventArgs e)
    {
      if (startPoint != Point.Empty)
      {
        movingPoint = e.Location;
        tChart1.Invalidate();
      }
    }
Best Regards,
Christopher Ireland / 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

Post Reply