我正在使用TeeChart和Delphi XE5,并利用BubbleSeries组件在图表中显示X/Y/Radius气泡。
我正在使用我拥有的对象列表构建图表,动态计算这些对象的 X/Y/半径值,并使用 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;
我会使用一个通用的TObjectList。或者一个后代 og a 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.
下一页 扩展您的 TB 系列
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.
最后在以下来源使用它:
将 BubbleSeriesExtensionion 添加到 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被摧毁时,你的对象也会被摧毁,用于计算其中的元素。 并给你一种垃圾收集
您可以像这样利用未使用的 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]);