在TeeChart中存储对象的引用

时间:2015-02-02 09:15:52

标签: delphi vcl delphi-xe5 teechart

我使用TeeChart和Delphi XE5并利用BubbleSeries组件在图表中显示X / Y / Radius气泡。

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

问题是当我想要在相应的气泡悬停/点击/等时对对象执行某些操作。我使用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;

2 个答案:

答案 0 :(得分:2)

我会使用Generic 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.

最后在你的from:

中使用它

将BubbleSeriesExtention添加到AFL VclTee.BubbleCh

后的使用列表中
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被破坏时,您的对象也可以用于计算其中的元素。并为您提供一种垃圾收集

答案 1 :(得分:0)

您可以像这样利用未使用的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]);