我正在使用以下代码向图表添加文本:
with form1.Chart1.Tools.Add(TRectangleTool) as TRectangleTool do
begin
Text:='THIS A TEXT';
Shape.Angle:=0;
Shape.Transparency:=0;
Shape.Pen.Hide;
Shape.AutoSize:=True;
Shape.Color := Talphacolorrec.null;
Shape.Font.Name:='Segoe UI';
Shape.Font.Size := 14;
Shape.Font.Color := Talphacolorrec.red;
AllowDrag:=False;
AllowResize:=False;
Left:= 4;
Top:= 0;
end;
代码运行良好。但是,稍后我不得不将其从图表中删除。
有什么建议吗?提前谢谢。
我建议你试试这个:
procedure TForm1.Button1Click(Sender: TObject);
begin
FRectTool := Chart1.Tools.Add(TRectangleTool) as TRectangleTool;
with FRectTool do begin
Text:='THIS A TEXT';
Shape.Angle:=0;
Shape.Transparency:=0;
Shape.Pen.Hide;
Shape.AutoSize:=True;
Shape.Color := Talphacolorrec.null;
Shape.Font.Name:='Segoe UI';
Shape.Font.Size := 14;
Shape.Font.Color := Talphacolorrec.red;
AllowDrag:=False;
AllowResize:=False;
Left:= 4;
Top:= 0;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Index : Integer;
begin
Index := Chart1.Tools.IndexOf(FRectTool);
Chart1.Tools.Delete(Index);
FreeAndNil(FRectTool); // Not sure it must be called
end;
您必须在表单类中声明FRectTool
private
FRectTool: TTeeCustomTool;
BTW:我不能自己尝试,因为我没有TeeChart Pro。发布一条评论来判断它是否有效。如果不起作用,请公布您收到的错误。
Var
myrectangle : array[0..2] of TrectangleTool;
indexrectangle : array[0..2] of integer;
i, j : integer;
//delete previous Trectangletools
for i := 0 to form1.Chart1.Tools.Count-1 do
If form1.Chart1.Tools[i] is TRectangleTool then form1.Chart1.Tools[i].destroy;
//adding two Trectangletools
form1.Chart1.Tools.Add(TRectangleTool);
form1.Chart1.Tools.Add(TRectangleTool);
j:= -1;
for i := 0 to form1.Chart1.Tools.Count-1 do
If form1.Chart1.Tools[i] is TRectangleTool then
begin
inc(j);
indexRectangle[j] := i;
myrectangle[i] := form1.Chart1.Tools.Items[i] as TRectangleTool;
end;
//Defining the two new Trectangletools
with myrectangle[indexRectangle[0]] do
begin
Text :=' 1 this is a text 1';
Shape.Angle:=0;
Shape.Transparency:=0;
Shape.Pen.Hide;
Shape.AutoSize:=True;
Shape.Color := Talphacolorrec.null;
// Shape.Font.Style:=[fsBold];
Shape.Font.Name:='Segoe UI';
Shape.Font.Size := 14;
Shape.Font.Color := Talphacolorrec.Blue;
AllowDrag:=False;
AllowResize:=False;
Left:= 4;
//Chart1.ChartRect.Left-5;
Top:= 0;
//Chart1.ChartRect.Bottom-40;
end;
with myrectangle[indexRectangle[1]] do
begin
Text :='2 this is a text 2';
Shape.Angle:=0;
Shape.Transparency:=0;
Shape.Pen.Hide;
Shape.AutoSize:=True;
Shape.Color := Talphacolorrec.null;
// Shape.Font.Style:=[fsBold];
Shape.Font.Name:='Segoe UI';
Shape.Font.Size := 14;
Shape.Font.Color := Talphacolorrec.red;
AllowDrag:=False;
AllowResize:=False;
Left:= 200;
Top:= 0;
end;
for I := 1 to form1.Chart1.Tools.Count do form1.Chart1.Tools.Delete(0);
我发现这是删除图表上所有工具的最佳方法。
我们必须考虑到,每次删除Tool时,Count值都会在同一时刻下降一个单位。
因此,我们删除位置0需要删除所有工具的次数。