Delphi 在FastReport中创建数据集分布图(钟形曲线)的最佳方法是什么?

Delphi 在FastReport中创建数据集分布图(钟形曲线)的最佳方法是什么?,delphi,charts,fastreport,Delphi,Charts,Fastreport,我正在将一个自制的report Delphi报告解决方案移植到FastReport中,我需要一个图表来显示数据集中字段的分布(“钟形曲线”或正态分布)。之前,我编写了将字段值排序到单元格中的代码(例如100),然后根据1-100(X)绘制了单元格计数(Y)的TChart直方图。FastReport与TChart集成良好,我可以轻松绘制字段值线。是否有绘制分布图的现有方法,或者我应该创建一个新的排序单元格数据集并绘制该数据集? 谢谢。当我意识到没有直接的解决方案时,我创建了一个其他人可能会觉得有用

我正在将一个自制的report Delphi报告解决方案移植到FastReport中,我需要一个图表来显示数据集中字段的分布(“钟形曲线”或正态分布)。之前,我编写了将字段值排序到单元格中的代码(例如100),然后根据1-100(X)绘制了单元格计数(Y)的TChart直方图。FastReport与TChart集成良好,我可以轻松绘制字段值线。是否有绘制分布图的现有方法,或者我应该创建一个新的排序单元格数据集并绘制该数据集?
谢谢。

当我意识到没有直接的解决方案时,我创建了一个其他人可能会觉得有用的类。它需要一个数据集,并完成为特定字段名构建频率单元格列表的所有艰苦工作,然后缓存该列表,以允许从名为“Distributions”的TfrxUserDataSet调用“GetValue”。然后,报表用户只需在报表中放置条形图,指定“分布”作为数据集,并为“Y值”选择所需字段即可“X值”必须设置为相同的字段名,但附加了“-X”-my class然后透明地返回已在第一次调用中构建频率单元格的图表的X和Y值。不涉及FastReport代码

虽然工作正常,但这是一个初出茅庐的代码,可以进一步改进,例如,目前X值的范围从最小到最大。更好的显示方式是使用3或6西格玛(标准偏差),但这很容易修改

unit UartFastReportsDistribution;
interface

uses
  DB,
  Classes;

const
  CellCount = 101;

type
  TCellArray = array[0..CellCount-1] of integer;
  TXValues   = array[0..CellCount-1] of double;

  TDistributionCells = class( TObject )
    constructor Create( ADataSet : TDataSet; const AFieldName : string );
  PRIVATE
    FDataSet : TDataSet;
    FFieldName : string;
    FCells : TCellArray;
    FLastRecNo : integer;
    FCellsMax : integer;
    FDataMin, FDataMax : double;
    procedure BuildCells;
    function  XValue( AIndex : integer ) : double;
    function  YValue( AIndex : integer ) : double;
    function  DataMean : double;
    function  DataDevPk : double;
  end;


  TArtFastReportsDistribution = class( TObject )
    constructor Create( ADataSet : TDataSet );
    destructor Destroy; override;
  private
    FDataSet : TDataSet;
    FDistributions : TStringList;

    function  NameToDistribution( const AFieldName: string) : TDistributionCells;
  PUBLIC
    procedure DoGetData( const AFieldName: string; ARecNo : integer; var Value: Variant);
    function  RecordCount : integer;
  end;

implementation

uses
  Math,
  SysUtils;

{ TArtFastReportsDistribution }

function TArtFastReportsDistribution.NameToDistribution( const AFieldName: string) : TDistributionCells;
var
  I : integer;
begin
  I := FDistributions.IndexOf( AFieldName );
  if I = -1 then
    begin
    Result := TDistributionCells.Create( FDataSet, AFieldName );
    FDistributions.AddObject( AfieldName, Result );
    end
   else
    Result := FDistributions.Objects[I] as TDistributionCells;
end;


constructor TArtFastReportsDistribution.Create(ADataSet: TDataSet);
begin
  inherited Create;
  FDataSet := ADataSet;
  FDistributions := TStringList.Create;
  FDistributions.OwnsObjects := True;
end;

destructor TArtFastReportsDistribution.Destroy;
begin
  FreeAndNil( FDistributions );
  inherited;
end;

procedure TArtFastReportsDistribution.DoGetData(const AFieldName: string;
  ARecNo : integer; var Value: Variant);

var
  sFieldName : string;
  bIsXValue  : boolean;
  I          : integer;
  Dist       : TDistributionCells;
begin
  sFieldName := AFieldName;
  I := Pos( '-X', sFieldName );
  bIsXValue := I > 0;
  if bIsXValue then
    Delete( sFieldName, I, MaxInt );

  Dist := NameToDistribution( sFieldName );

  If (ARecNo = 1) and (Dist.FLastRecNo <> 1) then
    Dist.BuildCells;

  Dist.FLastRecNo := ARecNo;

  if bIsXValue then
    Value := Dist.XValue(ARecNo-1)
   else
    Value := Dist.YValue(ARecNo-1);
end;


function TArtFastReportsDistribution.RecordCount: integer;
begin
  Result := CellCount;
end;

{ TDistributionCells }


{ TDistributionCells }

procedure TDistributionCells.BuildCells;

  procedure ClearCells;
  var
    I : integer;
  begin
    for I := 0 to CellCount-1 do
      FCells[I] := 0;

    FCellsMax := 0;
    FDataMin := 0.0;
    FDataMax := 0.0;
  end;


  function GetDataSetFieldValues : TFloatArray;
  var
    I : integer;
    Field : TField;
  begin
    Field := FDataSet.FieldByName( FFieldName );
    if not Assigned( Field ) then
      Raise Exception.CreateFmt( 'Missing distribution field "%s"', [FFieldName] );

    SetLength( Result, FDataSet.RecordCount );
    FDataSet.First;
    I := 0;
    While not FDataset.EOF do
      begin
      Result[I] := Field.AsFloat;
      Inc(I);
      FDataSet.Next;
      end;
  end;


var
  I,
  iCellCount,
  iOffset : integer;
  F : double;
  Data : TFloatArray;
begin
  ClearCells;

  If FDataSet.RecordCount = 0 then
    Exit;

  Data := GetDataSetFieldValues;

  FDataMin  := MinValue( Data );
  FDataMax  := MaxValue( Data );

  FCellsMax := 0;
  iCellCount   := Length( FCells );

  for I := 0 to Length( Data )-1 do
    begin
    F := Data[I];

    F := (F - DataMean + DataDevPk)/(2*DataDevPk);
    iOffset := Trunc( iCellCount * F );
    If iOffset < 0 then
      iOffset := 0
     else
      If iOffset > iCellCount-1 then
       iOffset := CellCount-1;
    FCells[iOffset] := FCells[iOffset] + 1;

    If I = 0 then
      FCellsMax := FCells[iOffset]
     else
      FCellsMax := Max( FCells[iOffset], FCellsMax );
    end;

end;


constructor TDistributionCells.Create(ADataSet: TDataSet;
  const AFieldName: string);
begin
  inherited Create;
  FDataSet := ADataSet;
  FFieldName := AFieldName;
end;

function TDistributionCells.DataDevPk: double;
begin
  Result := FDataMax - DataMean;
end;

function TDistributionCells.DataMean: double;
begin
  Result := (FDataMin + FDataMax) / 2;
end;

function TDistributionCells.XValue(AIndex: integer): double;
begin
  Result := AIndex;
  Result := (Result / CellCount) - 0.5;               
  Result := DataMean + (Result*2*DataDevPk);
end;

function TDistributionCells.YValue(AIndex: integer): double;
begin
//  Result := 100.0 * FCells[AIndex] / FCellsMax;
  Result := FCells[AIndex];
end;
单元UartFastReportsDistribution;
接口
使用
DB,
班级;
常数
细胞计数=101;
类型
TCellArray=整数的数组[0..CellCount-1];
TXValues=double的数组[0..CellCount-1];
TDistributionCells=类别(ToObject)
构造函数创建(ADataSet:TDataSet;const AFieldName:string);
私有的
FDataSet:TDataSet;
FFieldName:字符串;
FCells:TCellArray;
FLastRecNo:整数;
FCellsMax:整数;
FDataMin,FDataMax:双精度;
程序构建单元;
函数XValue(AIndex:integer):双精度;
函数YValue(AIndex:integer):双精度;
功能数据平均值:双;
函数DataDevPk:double;
结束;
TArtFastReportsDistribution=类(ToObject)
构造函数创建(ADataSet:TDataSet);
毁灭者毁灭;推翻
私有的
FDataSet:TDataSet;
分布:TStringList;
函数名ToDistribution(const-AFieldName:string):tdDistributionCells;
公开的
过程DoGetData(常量AFieldName:string;ARecNo:integer;变量值:Variant);
函数RecordCount:整数;
结束;
实施
使用
数学,
SysUtils;
{StartFastReportsDistribution}
函数TArtFastReportsDistribution.NameToDistribution(const-AFieldName:string):TDistributionCells;
变量
I:整数;
开始
I:=fddistributions.IndexOf(AFieldName);
如果I=-1,那么
开始
结果:=TDistributionCells.Create(FDataSet,AFieldName);
fddistributions.AddObject(AfieldName,Result);
结束
其他的
结果:=fddistributions.Objects[I]作为TDistributionCells;
结束;
构造函数TArtFastReportsDistribution.Create(ADataSet:TDataSet);
开始
继承创造;
FDataSet:=ADataSet;
FDistributions:=TStringList.Create;
fddistributions.OwnsObjects:=True;
结束;
析构函数:FastReportsDistribution.Destroy;
开始
FreeAndNil(FD分布);
继承;
结束;
过程TArtFastReportsDistribution.DoGetData(常量AFieldName:string;
ARecNo:整数;var值:变量);
变量
sFieldName:字符串;
bIsXValue:布尔值;
I:整数;
分布区:t分布细胞;
开始
sFieldName:=AFieldName;
I:=Pos('-X',sFieldName);
bIsXValue:=I>0;
如果bIsXValue那么
删除(sFieldName,I,MaxInt);
Dist:=名称到分布(sFieldName);
如果(ARecNo=1)和(Dist.FLastRecNo 1),则
分布细胞;
Dist.FLastRecNo:=ARecNo;
如果bIsXValue那么
值:=Dist.XValue(ARecNo-1)
其他的
值:=距离Y值(ARecNo-1);
结束;
函数TArtFastReportsDistribution.RecordCount:整数;
开始
结果:=细胞计数;
结束;
{TDistributionCells}
{TDistributionCells}
程序TDistributionCells.BuildCells;
程序透明细胞;
变量
I:整数;
开始
对于I:=0到CellCount-1 do
FCells[I]:=0;
FCellsMax:=0;
FDataMin:=0.0;
FDataMax:=0.0;
结束;
函数GetDataSetFieldValues:TFloatArray;
变量
I:整数;
字段:TField;
开始
字段:=FDataSet.FieldByName(FFieldName);
如果未分配(字段),则
引发异常。CreateFmt('Missing distribution field“%s”,[FFieldName]);
SetLength(结果,FDataSet.RecordCount);
FDataSet.First;
I:=0;
而不是FDataset.EOF do
开始
结果[I]:=Field.AsFloat;
公司(一);
FDataSet.Next;
结束;
结束;
变量
我
冰计数,
iOffset:整数;
F:双倍;
资料:TFloatArray;
开始
透明细胞;
如果FDataSet.RecordCount=0,则
出口
数据:=GetDataSetFieldValue;
FDataMin:=最小值(数据);
FDataMax:=最大值(数据);
FCellsMax:=0;
iCellCount:=长度(fcell);
对于I:=0到长度(数据)-1 do
开始
F:=数据[I];
F:=(F-数据平均值+数据devpk)/(2*数据devpk);
iOffset:=Trunc(iCellCount*F);
如果iOffset<0,则
偏移量:=0
其他的
如果iOffset>iCellCount-1,则
iOffset:=CellCount-1;
FCells[iOffset]:=FCells[iOffset]+1;
如果I=0,那么
FCellsMax:=FCells[iOffset]
其他的
FCellsMax:=Max(FCells[iOffset],FCellsMax);
结束;
结束;
构造函数TDistributionCells.Create(ADataSet:TDataSet;
常量AFieldName:string);
开始
继承创造;
FDataSet:=ADataSet;
FFieldName:=AFieldName;
结束;
函数TDistributionCells.DataDevPk:double;
开始
结果:=FDataMax-数据平均值;
结束;
函数TDistributionCells.DataMean:双精度;
开始
结果: