Search code examples
c++c++builderteechart

How to draw a line limited to Steema TChart boundaries?


I am using Steema TChart in a C++ Builder program, and I need to draw lines, with a user defined inclination, that passes through a point and goes up to the limits of the TChart. By limits, I mean the rectangule that surrounds the graphic.

In the image below you can see that the lines go beyond the rectangle I mentioned.

enter image description here

Any help will be much appreciated!


Solution

  • You can draw manually those lines. Ie, in delphi:

    uses Series, Math, TeCanvas;
    
    procedure TForm1.Chart1AfterDraw(Sender: TObject);
    
      procedure DrawLine(ASeries: TChartSeries; ValueIndex: Integer; Angle: Integer);
      var P, P0, P1: TPoint;
          Alfa, AlfaTan: Double;
          R: TRect;
          Canvas: TCanvas3D;
      begin
        Alfa:=Angle*TeePiStep;
        R:=ASeries.ParentChart.ChartRect;
        Canvas:=ASeries.ParentChart.Canvas;
        P:=Point(ASeries.CalcXPos(ValueIndex), ASeries.CalcYPos(ValueIndex));
    
        if Angle mod 180 = 0 then
        begin
          if (P.Y > R.Top) and (P.Y < R.Bottom) then
             Canvas.HorizLine3D(R.Left, R.Right, P.Y, 0);
        end
        else if Angle mod 90 = 0 then
        begin
          if (P.X > R.Left) and (P.X < R.Right) then
             Canvas.VertLine3D(P.X, R.Top, R.Bottom, 0);
        end
        else
        begin
          AlfaTan:=Tan(Alfa);
          P0.Y:=P.Y+Round(AlfaTan*(P.X-R.Left));
          P0.X:=R.Left;
    
          if (P0.Y < R.Top) then
          begin
            P0.Y:=R.Top;
            P0.X:=P.X-Round((R.Top-P.Y)/AlfaTan);
          end
          else if (P0.Y > R.Bottom) then
          begin
            P0.Y:=R.Bottom;
            P0.X:=P.X-Round((R.Bottom-P.Y)/AlfaTan);
          end;
    
          P1.Y:=P.Y-Round(AlfaTan*(R.Right-P.X));
          P1.X:=R.Right;
    
          if (P1.Y < R.Top) then
          begin
            P1.Y:=R.Top;
            P1.X:=P.X-Round((R.Top-P.Y)/AlfaTan);
          end
          else if (P1.Y > R.Bottom) then
          begin
            P1.Y:=R.Bottom;
            P1.X:=P.X-Round((R.Bottom-P.Y)/AlfaTan);
          end;
    
          Canvas.Line(P0, P1);
        end;
      end;
    
    begin
      DrawLine(Chart1[0], 2, 180);
      DrawLine(Chart1[0], 2, 165);
      DrawLine(Chart1[0], 2, 150);
      DrawLine(Chart1[0], 2, 120);
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Chart1.View3D:=False;
      Chart1.AddSeries(TPointSeries).FillSampleValues(5);
    end;
    

    example