如何画一条限制在 Steema TChart 边界内的线?

How to draw a line limited to Steema TChart boundaries?

我在 C++ Builder 程序中使用 Steema TChart,我需要绘制具有用户定义倾斜度的线,该线穿过一个点并上升到 TChart 的限制。就限制而言,我指的是图形周围的矩形。

在下图中,您可以看到线条超出了我提到的矩形。

任何帮助将不胜感激!

您可以手动绘制这些线条。即,在 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;