Page 1 of 1

Clipping problem with Chart1.Canvas.LineTo & AfterDrawVa

Posted: Tue Dec 19, 2006 8:04 pm
by 9339638
I have a pointer series on a chart, and I am drawing custom lines from certain points to others (not like the sequential point to point ones that are built in). The lines draw fine and scroll with the chart but they do not clip at the axis like the points in the series do. How can I get my custom lines to clip like the points in the series do?

Code: Select all

procedure TForm1.Series1AfterDrawValues(Sender: TObject);
var x0, y0, x1, y1, x2, y2 : Integer ;
begin
Chart1.ClipPoints:=true;
x0:=Series1.CalcXPos(0);
y0:=Series1.CalcYPos(0);
x1:=Series1.CalcXPos(1);
y1:=Series1.CalcYPos(1);
x2:=Series1.CalcXPos(2);
y2:=Series1.CalcYPos(2);
Chart1.Canvas.MoveTo( x0, y0 );
Chart1.Canvas.LineTo( x1, y1 );
Chart1.Canvas.MoveTo( x0, y0 );
Chart1.Canvas.LineTo( x2, y2 );
end;

Thanks in advance,
David

Posted: Sat Dec 23, 2006 1:26 pm
by Pep
Hi David,

you can do :

Code: Select all

implementation

{$R *.dfm}

procedure TForm1.Series1AfterDrawValues(Sender: TObject);
var x0, y0, x1, y1, x2, y2 : Integer ;
begin 
Chart1.ClipPoints:=true;
x0:=Series1.CalcXPos(0);
y0:=Series1.CalcYPos(0);
x1:=Series1.CalcXPos(1);
y1:=Series1.CalcYPos(1); 
x2:=Series1.CalcXPos(2);
y2:=Series1.CalcYPos(2);
Chart1.Canvas.MoveTo( x0, y0 );
Chart1.Canvas.LineTo( x1, y1 );
Chart1.Canvas.MoveTo( x0, y0 );
Chart1.Canvas.LineTo( x2, y2 );

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Series1.BeforeDrawValues:=SeriesBeforeDraw;
end;

procedure TForm1.SeriesBeforeDraw(Sender: TObject);

  Function SeriesRect(Series:TChartSeries):TRect;
  begin
    With result do
    begin
      Left:=Series.GetHorizAxis.IStartPos;
      Right:=Series.GetHorizAxis.IEndPos;
      Top:=Series.GetVertAxis.IStartPos;
      Bottom:=Series.GetVertAxis.IEndPos;
    end;
  end;

begin
  // Clip
  With Chart1 do
       if CanClip then
          Canvas.ClipRectangle(SeriesRect( Sender as TChartSeries ));
end;
end.

Posted: Sun Dec 31, 2006 3:35 am
by 9339638
Thanks

Re: Clipping problem with Chart1.Canvas.LineTo & AfterDrawVa

Posted: Thu Jul 01, 2021 9:15 am
by Marc
Hello,

Update to previous answer. The Clip in OnAfterDraw should apply to the Chart rectangle.

ie.

Code: Select all

procedure TForm1.Series1AfterDrawValues(Sender: TObject);
var x0, y0, x1, y1, x2, y2 : Integer ;
begin
Chart1.Canvas.ClipRectangle(Chart1.ChartRect);  //<--here

x0:=Series1.CalcXPos(0);
y0:=Series1.CalcYPos(0);
x1:=Series1.CalcXPos(20); ...etc

Chart1.Canvas.UnClipRectangle;
Regards,
Marc Meumann