I am using a TPlotGrid
because I want to try to put some lines in it. Look at this simple code:
procedure TForm1.Button1Click(Sender: TObject);
var a,b: TPointF;
begin
a.X := 0;
a.Y := 0;
b.X := 1;
b.Y := 5;
PlotGrid.Canvas.DrawLine(a,b,1);
end;
As you can see here I have the PlotGrid on Tab2 and the button on Tab1. Why isn't this code adding a line to the PlotGrid? A line should appear when I click on the button (going from (0;0) to (1;5)).
I am new with Canvas and in particular with the TPlotGrid
but the latter is not very popular on Google and there isn't much on the documentation (only 1 page with a simple example).
TPlotGrid
provides a canvas with the gridlines but it has no means of storing your drawing elements (lines, rectangles etc.). The first thing you need to do, is arrange for storage of data. In your example move the points a
and b
to the private section of your form so they are available at any time.
In a real application you would store the elements in some data structure elsewhere.
In your button OnClick
event set values to those points and call PlotGrid.Repaint;
:
procedure TForm4.Button1Click(Sender: TObject);
begin
a.X := 0;
a.Y := 0;
b.X := 300;
b.Y := 100;
PlotGrid1.Repaint;
end;
Create an OnPaint
event for the TPlotGrid
, here you do the actual drawing:
procedure TForm4.PlotGrid1Paint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
begin
Canvas.Stroke.Color := TAlphaColors.Chocolate;
Canvas.DrawLine(a,b,1);
end;
Sample image with the previous code: