3

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

4

1 回答 1

4

当我意识到没有直接的解决方案时,我创建了一个其他人可能会觉得有用的类。它需要一个数据集,并为特定字段名称构建频率单元列表,然后将其缓存以允许来自名为“Distributions”的 TfrxUserDataSet 的“GetValue”调用。然后,报告用户可以简单地在报告中放置一个条形图,指定“分布”作为数据集,并为“Y 值”选择所需的字段。“X 值”必须设置为相同的字段名称,但附加“-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;
于 2012-06-13T05:39:13.713 回答