只加载当前数据到 TChart

Load only current data into TChart

我正在使用 Delphi10 和 TChart、TPointSeries、水平滚动,目前从 PostgreSQL 数据库加载的数据太多(100 条曲线,每条曲线包含数千个点)。

  • Is it possible to load from database (with LIMIT and OFFSET) only the data which needs to be displayed in current scrolling "window" of TChart?

您可以手动循环数据并调用 Add()/AddXY() 函数。
然后,在 OnScroll 事件中,您可以从轴范围中删除 out/far 的点,并将那些 in/next 添加到轴范围。

  • Is there some "Data needed" event that fires when I scroll to region with no data?

没有。您应该使用 OnScroll 事件并自行检查。

更新

下面提供了可以执行的操作的示例:

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  chart.OnScroll := chartScroll;
  chart.OnZoom := chartZoom;
end;

procedure TForm1.displayRange();
var startDate, endDate: TDateTime;
begin
  startDate := TDateTime(chart.BottomAxis.Minimum);
  endDate   := TDateTime(chart.BottomAxis.Maximum);
  Log(Format('start=%d, end=%d', [
    FindClosestIndex(startDate, chart.Series[0].XValues),
    FindClosestIndex(endDate, chart.Series[0].XValues)
  ]));
end;

procedure TForm1.chartScroll(Sender: TObject);
begin
  displayRange();
end;

procedure TForm1.chartZoom(Sender: TObject);
begin
  displayRange();
end;

可以举一个 FindClosestIndex 的例子

更新结束

  • Is it possible to inform TChart how much points I have without loading all this points?

不,但是您可以维护变量。

  • Is it possible to have all the curves' descriptions in Legend with loading actual data to chart only when I check the corresponding checkboxes in legend? I do not want that Chart displays all the series available in database but want it to show in legend all the available series. When user clicks at particular series in legend, corresponding series data will be loaded and represented as curve.

您可以使用 OnCLickLegend 事件并循环播放您的系列。目前,该系列的 Active 属性 是最新的,因此您可以 Clear 那些 not Active 并且您可以 Add/AddXY 指向那些 Active.