在 TeeChart 中存储对对象的引用

Storing reference to object in TeeChart

我将 TeeChart 与 Delphi XE5 一起使用,并利用 BubbleSeries 组件在图表中显示 X/Y/Radius 个气泡。

我正在使用我拥有的对象列表构建图表,动态计算这些对象的 X/Y/Radius 值并使用 TBubbleSeries.AddBubble 方法插入它们。

问题是当我想在相应的气泡为 hovered/clicked/etc 时对对象执行一些操作。我使用 TChartSeries.Clicked 方法找出点击了哪个气泡,但返回的索引仅可用于找出气泡的 xy/radius 值,而不是哪个对象产生的。

也许我遗漏了一些简单的东西,因为这似乎是任何图表库都应该轻松处理的事情。我尝试使用 AddBubble 方法返回的索引,但该索引仅在执行对 AddBubble 的另一次调用之前有效,此时,内部列表似乎被重新排序。

编辑:有人要求提供一些代码,就在这里!

procedure TBubbleReportForm.ChartMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  Index: Integer;
  Device: TDevice;
begin

  Index := BubbleSeries.Clicked(X,Y);

  if Index = -1 then
  begin
    BubbleChart.ShowHint := False;
    Exit;
  end;

  // This does not work as indexing does seems to correspond to the order which the bubbles was added.
  Device := FDevices[Index];


  BubbleChart.Hint := Device.Name;
  BubbleChart.ShowHint := True;

end;



procedure TBubbleReportForm.FormCreate(Sender: TObject);
var
  Device: TDevice;
begin


  BubbleChart.OnMouseMove := ChartMouseMove;

  // FDevices is of TObjectList type.
  for Device in FDevices do
  begin

    BubbleSeries.AddBubble(Device.CalculateXVal,Device.CalculateYVal,Device.CalculateRadius);

  end;

end;

我会使用通用 TObjectList。或 TObjectList 的后代。

首先我实现了您的 BoubleObject,以及它们的列表。在下面的示例中,我只是使用了一个虚拟实现:

unit BubbleU;

interface

uses

  System.Generics.Collections, System.SysUtils, Vcl.Graphics;

{$M+}

type
  TBubble = class
  private
    FX: Double;
    FRadius: Double;
    FY: Double;
    FLabelText: String;
    FColor: TColor;
    FIndex: Integer;
    FChartIndex: Integer;
    procedure SetChartIndex(const Value: Integer);
  protected
    procedure DoCalculation;
  public
    constructor Create(aIndex: Integer); reintroduce;
  published
    property X: Double read FX;
    property Y: Double read FY;
    property Radius: Double read FRadius;
    property LabelText: String read FLabelText;
    property Color: TColor read FColor;
    property ChartIndex: Integer read FChartIndex write SetChartIndex;
  end;

  TBubbleList = class(TObjectList<TBubble>)
  public
    function ElementFormChartIndex(ChartIndex: Integer): TBubble; overload;
  end;

implementation

{ TBubble }

constructor TBubble.Create(aIndex: Integer);
begin
  inherited Create;
  FIndex := aIndex;
  DoCalculation;
end;

procedure TBubble.DoCalculation;
begin
  FX := FIndex;
  FY := FIndex;
  FRadius := 1;
  FColor := clRed;
  FLabelText := 'Index: ' + FIndex.ToString;
end;

procedure TBubble.SetChartIndex(const Value: Integer);
begin
  FChartIndex := Value;
end;

{ TBubbleList }

function TBubbleList.ElementFormChartIndex(ChartIndex: Integer): TBubble;
var
  Element : TBubble;
begin
  for Element in Self do
    if Element.FChartIndex = ChartIndex then
      Exit(element);

  Exit(nil);
end;
end.

接下来扩展您的 TBubbleSeries

unit BubbleSeriesExtention;

interface

uses
  System.Classes, System.SysUtils,
  VclTee.BubbleCh,
  BubbleU;

type
  TBubbleSeries = class(VclTee.BubbleCh.TBubbleSeries)
  strict private
    FBoubleList: TBubbleList;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function AddBubble(aBubble: TBubble): Integer; reintroduce;
  published
    property BoubleList : TBubbleList read FBoubleList;
  end;

implementation

{ TBubbleSeries }

function TBubbleSeries.AddBubble(aBubble: TBubble): Integer;
begin
  aBubble.ChartIndex := Inherited AddBubble(aBubble.X, aBubble.Y, aBubble.Radius, aBubble.LabelText, aBubble.Color);
  FBoubleList.Add(aBubble);
  Result := aBubble.ChartIndex;
end;

constructor TBubbleSeries.Create(AOwner: TComponent);
begin
  inherited;
  FBoubleList := TBubbleList.Create(True);
end;

destructor TBubbleSeries.Destroy;
begin
  FreeAndNil(FBoubleList);
  inherited;
end;

end.

终于在你的发件人中使用它了:

在 VclTee.BubbleCh

之后将 BubbleSeriesExtention 添加到使用列表中
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VclTee.TeeGDIPlus, VclTee.TeEngine,
  VclTee.Series, VclTee.BubbleCh, Vcl.ExtCtrls, VclTee.TeeProcs, VclTee.Chart,

  BubbleU, BubbleSeriesExtention;

并使用它:

type
  TForm4 = class(TForm)
    Chart1: TChart;
    BubbleSeries: TBubbleSeries;
    procedure FormCreate(Sender: TObject);
    procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  Index: Integer;
  Bouble: TBubble;

begin
  Index := BubbleSeries.Clicked(X, Y);

  if index < 0 then
    exit;

  Bouble := BubbleSeries.BoubleList.ElementFormChartIndex(Index);
  Caption := Bouble.LabelText;
end;

procedure TForm4.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  //Add dummy data
  for i := 0 to 9 do
    BubbleSeries.AddBubble(TBubble.Create(i));
end;

end.

此解决方案的优势在于,您始终可以访问您的对象,当您的 BubbleSeries 被销毁时,用于计算其中元素的对象也会被销毁。并给你一种垃圾收集

您可以像这样利用未使用的 AXLabel 参数:

for DevIndex := 0 to DeviceCount - 1 do begin
    Device := FDevices[DevIndex]; 
    BubbleSeries.AddBubble(Device.CalculateXVal,Device.CalculateYVal, Device.CalculateRadius, IntToStr(DevIndex));
end; 

// to avoid labels' text ox X-Axis:
Chart1.BottomAxis.LabelStyle := talValue; 

  //in Clicked:
   DeviceIndex := StrToInt(BubbleSeries.Labels[Index]);