能用图形分析

object FormChart: TFormChart
  Left = 106
  Top = 175
  Width = 758
  Height = 485
  Caption = '图形分析'
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = '宋体'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnClose = FormClose
  OnCreate = FormCreate
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 12
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 750
    Height = 65
    Align = alTop
    BevelOuter = bvNone
    TabOrder = 0
    object Label1: TLabel
      Left = 368
      Top = 13
      Width = 138
      Height = 12
      Caption = '标识字段(XLabelsSource)'
    end
    object Label2: TLabel
      Left = 368
      Top = 38
      Width = 126
      Height = 12
      Caption = '统计字段(ValueSource)'
    end
    object RadioGroup1: TRadioGroup
      Left = 8
      Top = 2
      Width = 265
      Height = 50
      Caption = '图形格式'
      Columns = 4
      Items.Strings = (
        '柱状图'
        '饼形图'
        '领域图'
        '曲线图')
      TabOrder = 0
      OnClick = RadioGroup1Click
    end
    object CheckBox1: TCheckBox
      Left = 289
      Top = 25
      Width = 62
      Height = 17
      BiDiMode = bdLeftToRight
      Caption = '3D风格'
      ParentBiDiMode = False
      TabOrder = 1
      OnClick = CheckBox1Click
    end
    object BitBtn1: TBitBtn
      Left = 663
      Top = 6
      Width = 82
      Height = 25
      Caption = '生成图形(&B)'
      TabOrder = 2
      OnClick = BitBtn1Click
    end
    object ComboBox1: TComboBox
      Left = 512
      Top = 8
      Width = 145
      Height = 20
      ImeName = '极品五笔输入法'
      ItemHeight = 12
      TabOrder = 3
    end
    object ComboBox2: TComboBox
      Left = 512
      Top = 32
      Width = 145
      Height = 20
      ImeName = '极品五笔输入法'
      ItemHeight = 12
      TabOrder = 4
    end
    object BitBtn2: TBitBtn
      Left = 663
      Top = 34
      Width = 34
      Height = 25
      Caption = '打印'
      TabOrder = 5
      OnClick = BitBtn2Click
    end
    object BitBtn3: TBitBtn
      Left = 709
      Top = 34
      Width = 34
      Height = 25
      Caption = '导出'
      TabOrder = 6
      OnClick = BitBtn3Click
    end
  end
  object DBChart1: TDBChart
    Left = 0
    Top = 65
    Width = 750
    Height = 393
    BackWall.Brush.Color = clWhite
    BackWall.Brush.Style = bsClear
    Title.Text.Strings = (
      'TDBChart')
    View3D = False
    Align = alClient
    TabOrder = 1
  end
end

 

//单元设计: 陈新光(CXG)
//设计时间: 2009-10-8 16:57:35
//单元功用: 图形分析窗体

 

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;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    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 BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(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 Execute(dataset: TDataSet; ATitle: string = '');

implementation

uses
  uFunction;

{$R *.dfm}

procedure Execute(dataset: TDataSet; ATitle: string = '');
begin
  FormChart := TFormChart.Create(nil);
  try
    FormChart.Fdataset := dataset;
    FormChart.FTitle := ATitle;
    FormChart.RadioGroup1.ItemIndex := 0;
    FormChart.DBChart1.Title.Text.Clear;
    FormChart.DBChart1.Title.Text.Add(FormChart.FTitle);
    FormChart.ShowModal;
  finally
    FreeAndNil(FormChart);
  end;
end;

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

procedure TFormChart.BitBtn2Click(Sender: TObject);
begin
  DBChart1.Print;
end;

procedure TFormChart.BitBtn3Click(Sender: TObject);
var
  sav: TSaveDialog;
begin
  sav := TSaveDialog.Create(nil);
  try
    sav.Filter := '位图(BMP)|*.BMP';
    sav.FileName := '文件1';
    sav.FilterIndex := 1;
    if sav.Execute then
      DBChart1.SaveToBitmapFile(sav.FileName + '.BMP');
  finally
    sav.Free;
  end;
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
  FreeAndNil(Bar);
  FreeAndNil(Pie);
  FreeAndNil(Area);
  FreeAndNil(FastLine);
  Action := caFree;
  FormChart := nil;
end;

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

procedure TFormChart.FormShow(Sender: TObject);
begin
  CheckBox1.Checked := True;
  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
  CreateChart;
end;

end.

posted @ 2011-01-22 21:23  delphi中间件  阅读(331)  评论(0编辑  收藏  举报