通用图形分析

{*******************************************************}
{                                                       }
{       图形分析                                        }
{                                                       }
{       版权所有 (C) 2008 咏南工作室(陈新光)            }
{                                                       }
{*******************************************************}

unit uChart;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, TeeProcs, TeEngine, Chart, DBChart, StdCtrls,db,Series,
  Buttons;

type
  TColParams = record
    FieldName: string;
    Title: string;
  end;
  TFormChart = class(TForm)
    Panel1: TPanel;
    DBChart1: TDBChart;
    RadioGroup1: TRadioGroup;
    CheckBox1: TCheckBox;
    Label1: TLabel;
    Label2: TLabel;
    BitBtn1: TBitBtn;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure CheckBox1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FFirstRun:Boolean;
    ColArray,ColArray2: array of TColParams;
    FDataSet:TDataSet;
    FTitle:string;
    Bar:TBarSeries;              //柱形
    Pie:TPieSeries;              //饼形
    Area:TAreaSeries;            //领域图
    FastLine:TFastLineSeries;    //曲线图
    procedure CreateSeries;
    procedure CreateChart;
    procedure FillField;
    function GetLableFieldName:string;
    function GetValueFieldName:string;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormChart: TFormChart;

const
  FLable='请录入标识字段';
  FValue='请录入统计字段';

//==============================================================================
// ATitle: TDBChart.title
//==============================================================================

procedure ShowChart(ADataSet:TDataSet;ATitle:string='');

implementation

{$R *.dfm}

procedure ShowChart(ADataSet:TDataSet;ATitle:string='');
begin
  FormChart:=TFormChart.Create(nil);
  try
    FormChart.FDataSet:=ADataSet;
    FormChart.FTitle:=ATitle;
    FormChart.RadioGroup1.ItemIndex:=0;
    FormChart.DBChart1.Title.Text.Clear;
    FormChart.DBChart1.Title.Text.Add(FormChart.FTitle);
    FormChart.ShowModal;
  finally
    FormChart.Free;
  end;
end;

procedure TFormChart.BitBtn1Click(Sender: TObject);
begin
//  DBChart1.FreeAllSeries();
  FFirstRun:=False;
  CreateChart;
end;

procedure TFormChart.CheckBox1Click(Sender: TObject);
begin
  DBChart1.View3D:=CheckBox1.Checked;
end;

procedure TFormChart.CreateChart;
begin
  if FFirstRun then exit;

  if Trim(ComboBox1.Text)='' then
  begin
    ShowMessage(FLable);
    Exit;
  end;
  if Trim(ComboBox2.Text)='' then
  begin
    ShowMessage(FValue);
    Exit;
  end;

  DBChart1.SeriesList.Clear;

  DBChart1.View3D:=CheckBox1.Checked;
 
  case RadioGroup1.ItemIndex of
    0:
    begin
      with Bar do
      begin
        ParentChart := DBChart1;
        marks.Style:= smsvalue;
        DataSource := FDataSet;
        XLabelsSource :=GetLableFieldName;
        YValues.ValueSource :=GetValueFieldName;
      end;
    end;
    1:
    begin
      with Pie do
      begin
        ParentChart := DBChart1;
        marks.Style:= smsvalue;
        DataSource := FDataSet;
        XLabelsSource :=GetLableFieldName;
        YValues.ValueSource :=GetValueFieldName;
      end;
    end;
    2:
    begin
      with Area do
      begin
        ParentChart := DBChart1;
        marks.Style:= smsvalue;
        DataSource := FDataSet;
        XLabelsSource :=GetLableFieldName;
        YValues.ValueSource :=GetValueFieldName;
      end;
    end;
    3:
    begin
      with FastLine do
      begin
        ParentChart := DBChart1;
        marks.Style:= smsvalue;
        DataSource := FDataSet;
        XLabelsSource :=GetLableFieldName;
        YValues.ValueSource :=GetValueFieldName;
      end;
    end;
  end;
  FFirstRun:=False;
end;

procedure TFormChart.CreateSeries;
begin
  Bar:=TBarSeries.Create(Self);
  Pie:=TPieSeries.Create(Self);
  Area:=TAreaSeries.Create(Self);
  FastLine:=TFastLineSeries.Create(Self);
end;

procedure TFormChart.FillField;
var
  i:Integer;
begin
  ComboBox1.Items.Clear;
  ComboBox2.Items.Clear;

  SetLength(ColArray,FDataSet.FieldCount);
  SetLength(ColArray2,FDataSet.FieldCount);
 
  for i:=0 to FDataSet.FieldCount-1 do
  begin
    if not (FDataSet.Fields[i] is TNumericField)
      or (FDataSet.Fields[i] is TIntegerField) then
    begin
      ColArray[i].FieldName:=FDataSet.Fields[i].FieldName;
      ColArray[i].Title:=FDataSet.Fields[i].DisplayLabel;
      ComboBox1.Items.Add(ColArray[i].Title);
      if ComboBox1.Items.Count>0 then
        ComboBox1.ItemIndex:=0;
    end else
    begin
      ColArray2[i].FieldName:=FDataSet.Fields[i].FieldName;
      ColArray2[i].Title:=FDataSet.Fields[i].DisplayLabel;
      ComboBox2.Items.Add(ColArray2[i].Title);
      if ComboBox2.Items.Count>0 then
        ComboBox2.ItemIndex:=0;
    end; 
  end;
end;

procedure TFormChart.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TFormChart.FormCreate(Sender: TObject);
begin
  FFirstRun:=True;
  CreateSeries;
end;

procedure TFormChart.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Bar);
  FreeAndNil(Pie);
  FreeAndNil(Area);
  FreeAndNil(FastLine);
  FormChart:=nil;
end;

procedure TFormChart.FormShow(Sender: TObject);
begin
  FillField;
end;

function TFormChart.GetLableFieldName: string;
var
  i:Integer;
begin
  for i := Low(ColArray) to High(ColArray) do
  begin
    if ColArray[i].Title=ComboBox1.Text then
      Result:=ColArray[i].FieldName;
  end;   
end;

function TFormChart.GetValueFieldName: string;
var
  i:Integer;
begin
  for i := Low(ColArray2) to High(ColArray2) do
  begin
    if ColArray2[i].Title=ComboBox2.Text then
      Result:=ColArray2[i].FieldName;
  end;   
end;

procedure TFormChart.RadioGroup1Click(Sender: TObject);
begin
//  DBChart1.FreeAllSeries();
  CreateChart;
end;

end.

原文地址:https://www.cnblogs.com/hnxxcxg/p/2940887.html