Rotating DrawLineItem Shapes

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
Quant
Advanced
Posts: 142
Joined: Tue Dec 30, 2014 12:00 am

Rotating DrawLineItem Shapes

Post by Quant » Fri Jan 15, 2016 9:36 am

Hi,

We want to rotate following DrawLineItem shapes using mouse:
1] Rectangle
2] Horizontal Parallel Line
3] Vertical Parallel Line

I did not find anything in the source files, Do you have any example code for the same?

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

Re: Rotating DrawLineItem Shapes

Post by Christopher » Tue Jan 19, 2016 10:09 am

Quant wrote:I did not find anything in the source files, Do you have any example code for the same?
A very basic example uses this class:

Code: Select all

  public class MyDrawLine : DrawLine
  {
    public MyDrawLine(Chart c) : base(c)
    {
      Brush = new ChartBrush(c);
    }

    public ChartBrush Brush { get; set; }


    protected override void ChartEvent(EventArgs e)
    {
      if (e is AfterDrawEventArgs)
      {
        if (Lines.Count > 0)
        {
          foreach (DrawLineItem l in Lines)
          {
            RedrawBrushLine(l);
          }
        }
        if (drawing)
        {
          RedrawBrushLine(Selected);
        }
      }

      base.ChartEvent(e);
    }


    protected override void MouseEvent(MouseEventKinds kind, MouseEventArgs e, ref Cursor c)
    {
      base.MouseEvent(kind, e, ref c);
      if (kind == MouseEventKinds.Wheel) 
      {
        Delta += e.Delta;
        Chart.Invalidate();
      }
    }


    private int Delta;

    private void RedrawBrushLine(DrawLineItem line)
    {
      Graphics3D g = Chart.Graphics3D;
      Graphics gdi = g.GDIplusCanvas;


      g.Brush = Brush;

      if ((Chart != null) && g.ValidState())
      {
        Matrix m = gdi.Transform;


        Rectangle r;
        int x, y;
        PointF p;
        Point tmpP1, tmpP2;

        if (line == null)
        {
          r = new Rectangle(FromPoint, new Size(Math.Abs(ToPoint.X - FromPoint.X), Math.Abs(ToPoint.Y - FromPoint.Y)));
        }
        else 
        {
          tmpP1 = AxisPoint(line.StartPos);
          tmpP2 = AxisPoint(line.EndPos);
          r = new Rectangle(tmpP1, new Size(Math.Abs(tmpP1.X - tmpP2.X), Math.Abs(tmpP1.Y - tmpP2.Y)));
        }

        Graphics3D.RectCenter(r, out x, out y);
        p = new PointF(x, y);
        m.RotateAt(Delta, p);
        gdi.MultiplyTransform(m);

        DrawLineStyle s = (line == null) ? this.Style : line.Style;

        bool oldPen = g.Pen.Visible;
        g.Pen.Visible = false;

        ClipDrawingRegion(g);
        if (line == null)
        {
          DoDrawLine(g, this.FromPoint, this.ToPoint, s);
        }
        else
        {
          DoDrawLine(g, AxisPoint(line.StartPos), AxisPoint(line.EndPos), s);
        }

        g.Pen.Visible = oldPen;
        g.UnClip();
        gdi.ResetTransform();
      }
    }

    private void DoDrawLine(Graphics3D g, Point StartPos, Point EndPos, DrawLineStyle AStyle)
    {
      if (!base.Chart.Aspect.View3D)
      {
        if (AStyle != DrawLineStyle.Line)
        {
          if (AStyle == DrawLineStyle.Rectangle)
          {
            g.Rectangle(Utils.FromLTRB(StartPos.X, StartPos.Y, EndPos.X, EndPos.Y));
          }
          else if (AStyle == DrawLineStyle.Ellipse)
          {
            g.Ellipse(StartPos.X, StartPos.Y, EndPos.X, EndPos.Y);
          }
        }
      }
      else if (AStyle != DrawLineStyle.Line)
      {
        if (AStyle == DrawLineStyle.Rectangle)
        {
          g.Rectangle(Utils.FromLTRB(StartPos.X, StartPos.Y, EndPos.X, EndPos.Y), 0);
        }
        else if (AStyle == DrawLineStyle.Ellipse)
        {
          g.Ellipse(StartPos.X, StartPos.Y, EndPos.X, EndPos.Y, 0);
        }
      }
    }
  }
And can be consumed like this:

Code: Select all

    private void InitializeChart()
    {
      tChart1.Aspect.View3D = false;

      Line line1 = new Line(tChart1.Chart);

      line1.Add(2, 2);
      line1.Add(8, 8);

      MyDrawLine drawLine1 = new MyDrawLine(tChart1.Chart);
      drawLine1.Pen.Visible = false;
      drawLine1.Brush.Color = Color.Red;

      Steema.TeeChart.Tools.DrawLineItem I = new Steema.TeeChart.Tools.DrawLineItem(drawLine1);
      double tmp = line1.YValues.Range / 5.0;
      I.StartPos = new PointDouble(5, line1.YValues.Maximum - tmp);
      I.EndPos = new PointDouble(7, line1.YValues.Minimum + tmp);
      I.Style = DrawLineStyle.Rectangle;
    }
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