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

时间:2012-06-12 09:53:20

标签: delphi charts fastreport

我正在将一份家庭酿造报告Delphi报告解决方案移植到FastReport中,我需要一个图表来显示数据集中字段的分布(“贝尔曲线”或正态分布)。以前我编写了将字段值排序到单元格中的代码(例如100个说),然后绘制了针对1-100(X)的单元格计数(Y)的TChart直方图。 FastReport与TChart有很好的集成,我正在轻松地绘制字段值。是否存在绘制分布图的现有方法,还是应该创建一个新的排序单元数据集并绘制该图? 感谢。

1 个答案:

答案 0 :(得分:4)

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

虽然有效,但这是初出茅庐的代码并且可以进一步改进,例如目前X值跨越最小值到最大值。更好的显示是使用3或6-sigma(标准偏差),但这很容易修改。

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;