⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teechartgrid.pas

📁 TeeChart7Source 控件
💻 PAS
📖 第 1 页 / 共 3 页
字号:
{******************************************}
{   TeeChart VCL Library                   }
{ Copyright (c) 1995-2004 by David Berneda }
{        All Rights Reserved               }
{******************************************}
unit TeeChartGrid;
{$I TeeDefs.inc}

interface

Uses {$IFNDEF LINUX}
     Windows, Messages,
     {$ENDIF}
     {$IFDEF CLX}
     QControls, QGrids, QGraphics, QExtCtrls, Qt, Types,
     {$ELSE}
     Controls, Grids, Graphics, ExtCtrls,
     {$ENDIF}
     Classes, Chart, TeEngine, TeeProcs, TeCanvas, TeeNavigator;

type
  TChartGridNavigator=class;

  TChartGridShow=(cgsAuto,cgsNo,cgsYes);

  TCustomChartGrid=class;

  TEditingCellEvent=procedure (Sender: TCustomChartGrid; ACol, ARow: Integer;
                               var Allow:Boolean) of object;

  TCustomChartGrid=class(TCustomGrid,ITeeEventListener)
  private
    FChart      : TCustomChart;
    FColors     : Boolean;
    FGrid3DMode : Boolean;
    FLabels     : Boolean;
    FXValues    : TChartGridShow;
    FOldValue   : String;
    FOnChangeColor : TNotifyEvent;
    FOnEditing  : TEditingCellEvent;
    FOnSetCell  : TSetEditEvent;
    FShowFields : Boolean;
    FWasNull    : Boolean;

    IHasNo     : Array[0..1000] of Boolean;
    INavigator : TChartGridNavigator;
    FSeries    : TChartSeries;
    ISeriesChart : TCustomAxisPanel;

    Procedure AddListener(AChart:TCustomAxisPanel); // 5.03
    Function FirstRowNum:Integer;
    Function ColorsColumn:Integer;
    Function LabelsColumn:Integer;
    procedure NotifyChange;
    procedure Regenerate;
    Procedure RemoveListener(AChart:TCustomAxisPanel); // 5.03
    Procedure SetBooleanProperty(Var Variable:Boolean; Value:Boolean);
    Procedure SetChart(AChart:TCustomChart);
    Procedure SetGrid3DMode(Value:Boolean);
    Procedure SetManualData(ASeries:TChartSeries);
    Procedure SetNavigator(ANavigator:TChartGridNavigator);
    Procedure SetShowColors(Value:Boolean);
    Procedure SetShowFields(Value:Boolean);
    Procedure SetShowLabels(Value:Boolean);
    Procedure SetShowXValues(Value:TChartGridShow);
    procedure SetSeries(const Value: TChartSeries);
  protected
    FActiveChanged   : TNotifyEvent;
    FSelectedChanged : TSelectCellEvent;

    function CanEditModify: Boolean; override;
    function CanEditShow: Boolean; override;
    procedure DblClick; override;
    procedure DrawCell(ACol, ARow: Integer; ARect: TRect;
                       AState: TGridDrawState); override;

    function GetEditText(ACol, ARow: Integer): {$IFDEF CLX}WideString{$ELSE}String{$ENDIF}; override;

    Function GetSeriesColor(var AColor:TColor):TChartSeries;

    Function HasPoints:Boolean;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure Notification( AComponent: TComponent;
                            Operation: TOperation); override;
    Procedure Loaded; override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); override;
    function SelectCell(ACol, ARow: Integer): Boolean; override;

    procedure SetEditText(ACol, ARow: Integer;
         const Value: {$IFDEF CLX}WideString{$ELSE}String{$ENDIF}); override;
    procedure TeeEvent(Event: TTeeEvent);
  public
    { Allow changing cells even if Series.DataSource is assigned }
    AllowChanges : Boolean; { 5.02 }

    Constructor Create(AOwner:TComponent); override;
    Destructor Destroy; override;

    Procedure AppendRow;

    Procedure ChangeColor; overload;
    Procedure ChangeColor(AColor:TColor); overload;
    Procedure ChangeColor(ASeries:TChartSeries; AColor:TColor); overload;

    Procedure Delete;
    Function FindXYZIndex(ASeries:TChartSeries; ARow,ACol:Integer):Integer;
    Function GetSeries(ACol:Integer; Var AList:TChartValueList):TChartSeries;
    Function GetXYZSeries:TChartSeries;
    Procedure Insert;
    Procedure RecalcDimensions;

    {$IFDEF TEEOCX}
    procedure SetFocus; override;
    {$ENDIF}

    Procedure StartEditing;
    Procedure StopEditing(Cancel:Boolean);

    property Chart:TCustomChart read FChart write SetChart;
    property Series:TChartSeries read FSeries write SetSeries;
    property ShowColors:Boolean read FColors write SetShowColors default False;
    property ShowFields:Boolean read FShowFields write SetShowFields default True;
    property ShowLabels:Boolean read FLabels write SetShowLabels default True;
    property ShowXValues:TChartGridShow read FXValues write SetShowXValues default cgsAuto;

    property FixedCols default 1;
    property DefaultRowHeight default {$IFDEF LINUX}20{$ELSE}16{$ENDIF};
    property Grid3DMode:Boolean read FGrid3DMode write
             SetGrid3DMode default False; // for XYZ series, set x by z grid mode
    property GridLineWidth default 1;

    {$IFDEF CLR}
    property Col;
    property EditorMode;
    property Options;
    property Row;
    property RowCount;
    {$ENDIF}
    
    { events }
    property OnChangeColor:TNotifyEvent read FOnChangeColor write FOnChangeColor; { 5.02 }
    property OnEditingCell:TEditingCellEvent read FOnEditing write FOnEditing; // 7.0
    property OnSetEditText:TSetEditEvent read FOnSetCell write FOnSetCell; { 5.02 }
  end;

  TChartGrid=class(TCustomChartGrid)
  public
    property Col;
    property ColCount;

    {$IFNDEF CLR}
    property ColWidths;
    {$ENDIF}

    property EditorMode;
    property GridHeight;
    property GridWidth;
    property LeftCol;
    property Selection;
    property Row;
    property RowCount;

    {$IFNDEF CLR}
    property RowHeights;
    property TabStops;
    {$ENDIF}

    property TopRow;
  published
    property Align;
    property Anchors;
    {$IFNDEF CLX}
    property BiDiMode;
    {$ENDIF}
    property BorderStyle;
    property Color;
    property Constraints;
    {$IFNDEF CLX}
    property Ctl3D;
    {$ENDIF}
    property DefaultColWidth;
    property DefaultRowHeight;
    property DefaultDrawing;
    property DragMode;
    {$IFNDEF CLX}
    property DragCursor;
    property DragKind;
    {$ENDIF}
    property Enabled;
    property FixedColor;
    property Font;
    property GridLineWidth;
    property Options;
    {$IFNDEF CLX}
    property ParentBiDiMode;
    {$ENDIF}
    property ParentColor;
    {$IFNDEF CLX}
    property ParentCtl3D;
    {$ENDIF}
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ScrollBars;
    property ShowHint;
    property TabOrder;
    property TabStop;
    property Visible;
    { events }
    property OnClick;
    {$IFDEF D5}
    property OnContextPopup;
    {$ENDIF}
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEndDrag;
    property OnStartDrag;
    {$IFNDEF CLX}
    property OnEndDock;
    {$ENDIF}
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnMouseWheelDown;
    property OnMouseWheelUp;
    {$IFNDEF CLX}
    property OnStartDock;
    {$ENDIF}

    property Chart;
    property Series;
    property ShowColors; { 5.02 }
    property ShowLabels;
    property ShowXValues; { 5.02 }

    { event }
    property OnChangeColor; // 7.0
    property OnEditingCell; // 7.0
    property OnSetEditText;
  end;

  TChartGridNavigator=class(TCustomTeeNavigator)
  private
    FGrid : TCustomChartGrid;
    procedure ActiveChanged(Sender:TObject);
    procedure EnableButtonsColRow(ACol,ARow:Integer);
    procedure SelectedChanged(Sender: TObject; ACol, ARow: Integer;
                              var CanSelect: Boolean);
    procedure SetGrid(Value:TCustomChartGrid);
  protected
    procedure BtnClick(Index: TTeeNavigateBtn); override;
    procedure Notification( AComponent: TComponent;
                            Operation: TOperation); override;
  public
    procedure EnableButtons; override;
  published
    property Grid:TCustomChartGrid read FGrid write SetGrid;
  end;

implementation

Uses Math, SysUtils, TeeConst, TeePenDlg;

{ TChartGridNavigator }
procedure TChartGridNavigator.Notification( AComponent: TComponent;
                                        Operation: TOperation);
begin
  inherited;
  if (Operation=opRemove) and Assigned(Grid) and (AComponent=Grid) then
     Grid:=nil;
end;

procedure TChartGridNavigator.SetGrid(Value:TCustomChartGrid);
begin
  if FGrid<>Value then
  begin
    {$IFDEF D5}
    if Assigned(FGrid) then
       FGrid.RemoveFreeNotification(Self);
    {$ENDIF}

    FGrid:=Value;

    if Assigned(FGrid) then
    begin
      FGrid.FreeNotification(Self);
      FGrid.FActiveChanged:=ActiveChanged;
      FGrid.FSelectedChanged:=SelectedChanged;
      FGrid.SetNavigator(Self);
    end;

    EnableButtons;
  end;
end;

procedure TChartGridNavigator.ActiveChanged(Sender:TObject);
begin
  EnableButtons;
end;

procedure TChartGridNavigator.BtnClick(Index: TTeeNavigateBtn);
begin
  if Assigned(FGrid) then
  with FGrid do
  begin
    case Index of
      nbPrior : if Row>FirstRowNum then Row:=Row-1;
      nbNext  : if Row<RowCount-1 then Row:=Row+1;
      nbFirst : if Row>FirstRowNum then Row:=FirstRowNum;
      nbLast  : if Row<RowCount-1 then Row:=RowCount-1;
      nbInsert: Insert;
      nbEdit  : begin
                  SetFocus;
                  StartEditing;
                end;
      nbCancel: StopEditing(True);
      nbPost  : StopEditing(False);
      nbDelete: Delete;
    end;
  end;
  inherited;
end;

procedure TChartGridNavigator.EnableButtons;
begin
  inherited;
  if Assigned(FGrid) then EnableButtonsColRow(FGrid.Col,FGrid.Row);
end;

procedure TChartGridNavigator.EnableButtonsColRow(ACol,ARow:Integer);
var UpEnable : Boolean;
    DnEnable : Boolean;
begin
  UpEnable := ARow>FGrid.FirstRowNum;
  DnEnable := ARow<FGrid.RowCount-1;
  Buttons[nbFirst].Enabled  := UpEnable;
  Buttons[nbPrior].Enabled  := UpEnable;
  Buttons[nbNext].Enabled   := DnEnable;
  Buttons[nbLast].Enabled   := DnEnable;
  Buttons[nbInsert].Enabled := (goEditing in FGrid.Options) and
                               (not FGrid.EditorMode) and
                               (Assigned(FGrid.Chart) and (FGrid.Chart.SeriesCount>0)) or
                               Assigned(FGrid.Series);

  Buttons[nbDelete].Enabled := (FGrid.RowCount>1) and
                               Buttons[nbInsert].Enabled and
                               FGrid.HasPoints;
  Buttons[nbPost].Enabled   := FGrid.EditorMode;
  Buttons[nbEdit].Enabled   := Buttons[nbInsert].Enabled;
  Buttons[nbCancel].Enabled := FGrid.EditorMode;
end;

procedure TChartGridNavigator.SelectedChanged(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  EnableButtonsColRow(ACol,ARow);
end;

{ TCustomChartGrid }
Constructor TCustomChartGrid.Create(AOwner:TComponent);
begin
  inherited;
  FSaveCellExtents:=False;
  FColors:=False;
  FLabels:=True;
  FXValues:=cgsAuto;
  FShowFields:=True;
  FWasNull:=False;
  GridLineWidth:=1;
  ColWidths[0]:=30;
  DefaultRowHeight:={$IFDEF LINUX}20{$ELSE}16{$ENDIF};
  FixedCols:=1;
  RowHeights[0]:=20;

  Options:=[ goHorzLine,
             goVertLine,
             goRangeSelect,
             goDrawFocusSelected,
             goRowSizing,
             goColSizing,
             goEditing,
             goTabs,
             goThumbTracking
           ];

  RecalcDimensions;
end;

Destructor TCustomChartGrid.Destroy;
begin
  Series:=nil; // 5.03
  Chart:=nil;
  inherited;
end;

Procedure TCustomChartGrid.Loaded;
begin
  inherited;
  RowHeights[0]:=20;
end;

Function TCustomChartGrid.HasPoints:Boolean;
var t: Integer;
begin
  result:=False;
  if Assigned(FSeries) then { single series }
     result:=FSeries.Count>0
  else
  if Assigned(Chart) then { all series in chart }
  With Chart do
  for t:=0 to SeriesCount-1 do
  if Series[t].Count>0 then
  begin
    result:=True;
    Exit;
  end;
end;

Function TCustomChartGrid.FirstRowNum:Integer;
begin
  if ShowFields then result:=2 else result:=1;
end;

function TCustomChartGrid.SelectCell(ACol, ARow: Integer): Boolean;
begin
  result:=inherited SelectCell(ACol,ARow) and (ACol>0) and (ARow>=FirstRowNum);
  // Call event
  if result and Assigned(FSelectedChanged) then
     FSelectedChanged(Self,ACol,ARow,result);
end;

function TCustomChartGrid.CanEditModify: Boolean;
begin
  result:=inherited CanEditModify;
  if result then
     if Assigned(INavigator) then INavigator.ActiveChanged(Self);
end;

procedure TCustomChartGrid.MouseDown(Button: TMouseButton; Shift: TShiftState;
                                     X, Y: Integer);

  Procedure SelectColumn;
  var tmpCoord     : TGridCoord;
      tmpSelection : TGridRect;
  begin
    tmpCoord:=MouseCoord(x,y);
    if (tmpCoord.X>FixedCols) and (tmpCoord.Y<FixedRows) then
    begin
      with tmpSelection do
      begin
        Left:=tmpCoord.X;
        Right:=Left;
        Top:=FixedRows;
        Bottom:=RowCount-1;
      end;
      Selection:=tmpSelection;
    end;
  end;

begin
  inherited;

  if FGridState=gsNormal then SelectColumn;

  if Assigned(INavigator) then INavigator.ActiveChanged(Self);
end;

Procedure TCustomChartGrid.StopEditing(Cancel:Boolean);
begin
  if EditorMode then
  begin
    if Cancel then InplaceEditor.Text:=''; // 5.02

    HideEditor;

    if Cancel and (not FWasNull) then SetEditText(Col,Row,FOldValue);

    NotifyChange;
  end;
end;

{$IFDEF TEEOCX}
procedure TCustomChartGrid.SetFocus;  // 6.02
begin
  if GetParentForm(Self)<>nil then inherited

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -