在实时应用程序中,我们通常建议遵循此处实时图表文章中的说明。
至于仅重新绘制图表的一个区域,您不能调用InvalidateRacte并且只能在矩形中重新创建图表部分。InvalidateRacte只能用于重新绘制缓冲区位图的一部分(Chart1.Canvas.Bitmap,当使用Chart1.BufferedDisplay时),VCL 框架不支持,我们也不会从中获得任何好处。
在这里,您可以下载一个项目,该项目将图表中的线条系列绘制在另一个图表上而不重新创建它,这是最耗时的任务。有一个按钮可以计算缓冲区位图可以重新绘制的次数。我们每秒得到大约 5000 个。如果每次都需要重新创建图表,例如:绘制轴、系列、图例等),我们每秒最多得到 100 次。项目中显示的功能仅在使用新的 TeeChart Pro beta时可用。项目的代码是这样的:
type
TSeriesAccess=class(TChartSeries);
TChartAccess=class(TCustomTeePanel);
procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
// Example: Modify a Line value
Series1.YValues[10]:=350+ScrollBar1.Position;
// Paint Line to Chart2, just to check the change (this is optional)
Series1.Repaint;
// Redraw Chart1 fast, just the buffer bitmap, without repainting everything:
TChartAccess(Chart1).Paint;
// Draw the Line over Chart1, on top of it, instead of "inside" it
TSeriesAccess(Series1).FParent:=Chart1;
TSeriesAccess(Series1).DrawAllValues;
TSeriesAccess(Series1).FParent:=Chart2;
end;
// Count the number of times a Chart buffer can be redisplayed
// (without redrawing everything)
procedure TForm1.Button1Click(Sender: TObject);
var t1 : Cardinal;
tmpCount : Integer;
begin
tmpCount:=0;
t1:=GetTickCount;
// Loop for one second
while GetTickCount-t1 < 1000 do
begin
TChartAccess(Chart1).Paint;
Inc(tmpCount);
end;
Caption:='Redraws per second: '+IntToStr(tmpCount);
end;
另一个选项是this,使用与我们的项目或多或少相同的 ColorLine ,使用Pen.Mode = pmXor进行绘制。
关于您的项目,您错过了在刷新系列之前将AutoRepaint设置为 true。此代码有效:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
// The 'jumping' TLineSeries will move to the right 1 point every 1 second.
Form1.Chart1.Series[1].XValue[0]:= Form1.Chart1.Series[1].XValue[0] + 1;
Form1.Chart1.Series[1].XValue[1]:= Form1.Chart1.Series[1].XValue[1] + 1;
Form1.Chart1.AutoRepaint:=True;
Form1.Chart1.Series[1].Repaint;
Form1.Chart1.AutoRepaint:=False;
end;
此外,您可以使用TColorLineTool而不是垂直线的系列。您应该删除第二行系列,在设计时添加一个 ColorLine 工具并实现如下代码:
unit UnitTeeChartRefresh;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, TeeProcs, TeEngine, Chart, Series, StdCtrls,
TeeGDIPlus, TeeTools;
type
TForm1 = class(TForm)
Chart1: TChart;
Series1: TAreaSeries;
Button1: TButton;
Timer1: TTimer;
Label1: TLabel;
ChartTool1: TColorLineTool;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Chart1AfterDraw(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
times_redrawn: cardinal = 0; // A counter indicating how many times our chart was redrawn.
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
//Let's add one constant TAreaSeries and one 'jumping' across the graph TLineSeries
Randomize;
Form1.Chart1.Axes.Left.SetMinMax(0, 10);
Form1.Chart1.Axes.Bottom.SetMinMax(0, 20);
for i:= 1 to 20 do
begin
Form1.Chart1.Series[0].AddXY(i, Random(10))
end;
ChartTool1.Axis:=Chart1.Axes.Bottom;
ChartTool1.Value:=0;
// If I enable AutoRepaint I will be able to see all changes on the whole plot in real-time
// and have no problem.
// But I want to manually control the refreshing process and even refresh some regions of the
// graph separately.
Form1.Chart1.AutoRepaint:=False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// This button launches the animation of the 'jumping' TLineSeries.
Form1.Timer1.Enabled:= not Form1.Timer1.Enabled;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
// The 'jumping' TLineSeries will move to the right 1 point every 1 second.
Form1.Chart1.AutoRepaint:=True;
ChartTool1.Value:=ChartTool1.Value + 1;
Form1.Chart1.AutoRepaint:=False;
end;
procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
times_redrawn:= times_redrawn + 1;
Form1.Label1.Caption:= intToStr(times_redrawn);
end;
end.