【问题标题】:Is it possible to repaint only a region of TeeChart when modifying series points?修改系列点时是否可以仅重新绘制 TeeChart 的一个区域?
【发布时间】:2013-07-29 06:23:46
【问题描述】:

我在刷新带有很多点的整个图表时遇到了性能问题,想问一下是否有办法减少重绘图表所花费的时间。

例如,我的图表包含一百个 TAreaSeries,每个价值 1000 点。这些值只生成一次,之后不会更改。但是有一个短的 TLineSeries 有 2 个点在图形上“跳跃”,每 50 毫秒改变一次坐标。尽管只修改了图的一小部分区域(TLineSeries 区域中的像素),但整个图(具有数千个点)被更新和重绘,这会徒劳地消耗大量资源并导致巨大的延迟。在我的情况下,这个具有 2 个点的 TLineSeries 的下一个和上一个位置是已知的,所以我想知道是否有办法强制 TChart 只重绘一些预定义的绘图区域而不是重绘整个图形?

【问题讨论】:

  • 我怀念 Turbo Pascal 7 中的旧 PutPixel 函数。那真是过去的美好时光。 :-(

标签: delphi delphi-7 vcl teechart


【解决方案1】:

在实时应用程序中,我们通常建议遵循实时图表文章here中的说明。

至于仅重新绘制图表的一个区域,您不能调用 InvalidateRacte 并且只能在矩形中重新创建图表部分。 InvalidateRacte 只能用于重绘缓冲区位图的一部分(Chart1.Canvas.Bitmap,当使用 Chart1.BufferedDisplay 时),VCL框架不支持,我们也不会从中获得任何好处。

Here 您可以下载一个项目,该项目在另一个图表上绘制线系列而不重新创建它,这是最耗时的任务。有一个按钮可以计算 缓冲区位图 可以重绘的次数。我们每秒得到大约 5000 个。如果每次都需要重新创建图表,例如:绘制轴、系列、图例等),我们每秒最多得到 100 次。项目中显示的功能只能使用new 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.

【讨论】:

  • 请问新手问题,但我不是专业程序员,也不熟悉 .NET 语法,所以我很困惑:您是否建议从一个画布复制一个矩形并将其粘贴到Chart1 画布?
  • @Wrackage 您可以从“实时图表”文章建议开始。如果这没有帮助,您可以尝试其他解决方案。它基本上包括重新绘制给定矩形的区域而不是整个图表,以仅出于性能目的更新此特定部分。
  • 谢谢,我已经考虑了“实时图表”文章中的所有建议,但是它们并没有改变重绘延迟的整体情况。对不起,我很抱歉,但您能否详细说明应该如何使用 Draw(UserCanvas:TCanvas; Const UserRect:TRect) 方法来部分刷新图形?我应该传递什么作为参数?文档信息太少,能否详细介绍一下方法?
  • @Wrackage 抱歉,Draw 的作用与 .NET 的 Invalidate 不同。在这种情况下,您可能需要按照here 的建议做一些事情,因为 Delphi 没有可以使用矩形作为参数调用的无效覆盖。
  • 谢谢,但该链接中唯一对我有帮助的是“InvalidateRect(WindowHandle: HWND, RectanglePointer: PRect, EraseBackground: Boolean)” 方法。但是它不起作用:如果我更改 TLineSeries 坐标并使用 'InvalidateRect(Form1.Handle, @refresh_rect, False)' 而不是 'LineSeries1.Repaint' 没有任何反应,我看不到 LineSeries1 移动。即使'refresh_rect = Chart1.ChartRect'。顺便问一下,'Chart1.Repaint'和'InvalidateRect(Form1.Handle, refresh_rect,False)'函数效果一样吗(假设refresh_rect = Chart1.ChartRect)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 2019-06-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
相关资源
最近更新 更多