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

📄 chart.pas

📁 Delphi TeeChartPro.6.01的源代码
💻 PAS
📖 第 1 页 / 共 5 页
字号:
{******************************************}
{   TCustomChart & TChart Components       }
{ Copyright (c) 1995-2003 by David Berneda }
{    All Rights Reserved                   }
{******************************************}
unit Chart;
{$I TeeDefs.inc}

interface

uses {$IFNDEF LINUX}
     Windows,
     {$ENDIF}
     Classes, SysUtils,
     {$IFDEF CLX}
     QGraphics, QControls, QExtCtrls, QForms, QButtons, Types,
     {$ELSE}
     Graphics, Controls, ExtCtrls, Forms, Printers,
     {$IFNDEF D5}
     Buttons,
     {$ENDIF}
     {$ENDIF}
     TeeProcs, TeEngine, TeCanvas;

Const TeeMsg_DefaultFunctionName = 'TeeFunction';  { <-- dont translate }
      TeeMsg_DefaultSeriesName   = 'Series';       { <-- dont translate }
      TeeMsg_DefaultToolName     = 'ChartTool';    { <-- dont translate }
      ChartComponentPalette      = 'TeeChart';     { <-- dont translate }

      TeeMaxLegendColumns        = 2;
      TeeDefaultLegendSymbolWidth= 20;

var   AnimatedZoomFactor   : Double=3.0;   { controls the animated zoom "speed" }
      TeeScrollKeyShift    : TShiftState=[];  { keys that should be pressed to start scroll }

      TeeTitleFootDistance : Integer=5; { fixed pixels distance between title/foot and chart }
      TeeUseMouseWheel     : Boolean=True;  { use the mouse wheel to scroll the axes }

type
  TCustomChartWall=class(TTeeCustomShape)
  private
    FDark3D      : Boolean;
    FSize        : Integer;
    Function ApplyDark3D:Boolean;
    Function GetPen:TChartPen;
    Function HasGradient:Boolean;
    Procedure InitColor(AColor:TColor);
    Function IsColorStored:Boolean;
    Procedure SetDark3D(Value:Boolean);
    Procedure SetPen(Value:TChartPen);
    Procedure SetSize(Value:Integer);
    Function TryDrawWall(APos1,APos2:Integer):TTeeBlend;
  protected
    DefaultColor : TColor;
  public
    Constructor Create(AOwner:TCustomTeePanel);
    Procedure Assign(Source:TPersistent); override;

    property Color stored IsColorStored nodefault; { 5.02 }
    property Dark3D:Boolean read FDark3D write SetDark3D default True;
    property Pen:TChartPen read GetPen write SetPen;
    property Size:Integer read FSize write SetSize default 0;
    property Transparency;
  end;

  TChartWall=class(TCustomChartWall)
  published
    property Brush;
    property Color;
    property Dark3D;
    property Gradient;
    property Pen;
    property Size;
    property Transparency;
    property Transparent;
    property Visible default True;
  end;

  TChartLegendGradient=class(TChartGradient)
  public
    Constructor Create(ChangedEvent:TNotifyEvent); override;
  published
    property Direction default gdRightLeft;
    property EndColor default clWhite;
    property StartColor default clSilver;
  end;

  { TCustomChartLegend Component }

  TLegendStyle    =(lsAuto,lsSeries,lsValues,lsLastValues);
  TLegendAlignment=(laLeft,laRight,laTop,laBottom);
  LegendException =class(Exception);

  TOnGetLegendText=Procedure( Sender:TCustomAxisPanel;
			      LegendStyle:TLegendStyle;
			      Index:Integer;
			      Var LegendText:String) of Object;

  TCustomChartLegend=class;

  TLegendSymbolSize=(lcsPercent,lcsPixels);

  TLegendSymbolPosition=(spLeft,spRight);

  TLegendSymbol=class(TPersistent)
  private
    FContinuous : Boolean;
    FDefaultPen : Boolean;
    FPen        : TChartPen;
    FPosition   : TLegendSymbolPosition;
    FSquared    : Boolean;
    FVisible    : Boolean;
    FWidth      : Integer;
    FWidthUnits : TLegendSymbolSize;

    FLegend     : TCustomChartLegend;
    Procedure CanvasChanged(Sender:TObject);
    procedure SetContinuous(const Value: Boolean);
    procedure SetDefaultPen(const Value: Boolean);
    procedure SetPen(const Value: TChartPen);
    Procedure SetPosition(Const Value:TLegendSymbolPosition);
    procedure SetSquared(const Value: Boolean);
    procedure SetVisible(const Value: Boolean);
    procedure SetWidth(Const Value: Integer);
    procedure SetWidthUnits(const Value: TLegendSymbolSize);
  protected
    Function CalcWidth(Value:Integer):Integer;
  public
    Constructor Create(ALegend:TCustomChartLegend);
    Destructor Destroy; override;

    Procedure Assign(Source:TPersistent); override;
  published
    property Continuous:Boolean read FContinuous write SetContinuous default False;
    property DefaultPen:Boolean read FDefaultPen write SetDefaultPen default True;
    property Pen:TChartPen read FPen write SetPen;
    property Position:TLegendSymbolPosition read FPosition write SetPosition default spLeft;
    property Squared:Boolean read FSquared write SetSquared default False;
    property Visible:Boolean read FVisible write SetVisible default True;
    property Width:Integer read FWidth write SetWidth default TeeDefaultLegendSymbolWidth;
    property WidthUnits:TLegendSymbolSize read FWidthUnits
                                 write SetWidthUnits default lcsPercent;
  end;

  TTeeCustomShapePosition=class(TTeeCustomShape)
  private
    FCustomPosition : Boolean;
    Function GetLeft:Integer;
    Function GetTop:Integer;
    procedure SetCustomPosition(Const Value:Boolean);
    procedure SetLeft(Const Value: Integer);
    procedure SetTop(Const Value: Integer);
  public
    Procedure Assign(Source:TPersistent); override;

    property CustomPosition:Boolean read FCustomPosition write SetCustomPosition default False;
    property Left:Integer read GetLeft write SetLeft stored FCustomPosition;
    property Top:Integer read GetTop write SetTop stored FCustomPosition;
  end;

  TCustomChart=class;

  TCustomChartLegend=class(TTeeCustomShapePosition)
  private
    FAlignment       : TLegendAlignment;
    FCheckBoxes      : Boolean;
    FCurrentPage     : Boolean;
    FDividingLines   : TChartHiddenPen;
    FFirstValue      : Integer;
    FFontSeriesColor : Boolean;
    FHorizMargin     : Integer;
    FInverted        : Boolean;
    FLegendStyle     : TLegendStyle;
    FMaxNumRows      : Integer;
    FResizeChart     : Boolean;
    FSeries          : TChartSeries;
    FSymbol          : TLegendSymbol;
    FTextStyle       : TLegendTextStyle;
    FTopLeftPos      : Integer;
    FVertMargin      : Integer;
    FVertSpacing     : Integer;

    { Internal }
    IColorWidth  : Integer;
    ILastValue   : Integer;
    ITotalItems  : Integer;

    Function CalcItemHeight:Integer;
    Function GetRectLegend:TRect;
    Function GetSymbolWidth:Integer;
    Function GetVertical:Boolean;
    Function HasCheckBoxes:Boolean;
    Procedure PrepareSymbolPen;
    Procedure SetAlignment(Const Value:TLegendAlignment);
    Procedure SetCheckBoxes(Const Value:Boolean);
    Procedure SetDividingLines(Const Value:TChartHiddenPen);
    Procedure SetFirstValue(Const Value:Integer);
    Procedure SetHorizMargin(Const Value:Integer);
    Procedure SetInverted(Const Value:Boolean);
    Function  GetLegendSeries:TChartSeries;
    Procedure SetLegendStyle(Const Value:TLegendStyle);
    Procedure SetMaxNumRows(Const Value:Integer);
    Procedure SetResizeChart(Const Value:Boolean);
    Procedure SetSeries(Const Value:TChartSeries);
    Procedure SetSymbol(Const Value:TLegendSymbol);
    Procedure SetSymbolWidth(Const Value:Integer);
    Procedure SetTextStyle(Const Value:TLegendTextStyle);
    Procedure SetTopLeftPos(Const Value:Integer);
    Procedure SetVertMargin(Const Value:Integer);
    Procedure SetVertSpacing(Const Value:Integer);
    procedure SetCurrentPage(Const Value: Boolean);
    procedure SetFontSeriesColor(const Value: Boolean);
  protected
    InternalLegendStyle : TLegendStyle;
    Procedure CalcLegendStyle;
    Function DoMouseDown(Const x,y:Integer):Boolean;
    Function GetGradientClass:TChartGradientClass; override;
  public
    NumCols        : Integer;
    NumRows        : Integer;
    ColumnWidthAuto: Boolean;
    ColumnWidths   : Array[0..TeeMaxLegendColumns-1] of Integer;

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

    Procedure Assign(Source:TPersistent); override;
    Function FormattedValue(ASeries:TChartSeries; ValueIndex:Integer):String;
    Function FormattedLegend(SeriesOrValueIndex:Integer):String;
    Procedure DrawLegend;
    property TotalLegendItems:Integer read ITotalItems;
    Function Clicked(x,y:Integer):Integer;

    { public properties }
    property RectLegend:TRect read GetRectLegend;
    property Vertical:Boolean read GetVertical;

    { to be published }
    property Alignment:TLegendAlignment read FAlignment write SetAlignment
					default laRight;
    property CheckBoxes:Boolean read FCheckBoxes write SetCheckBoxes default False;
    property ColorWidth:Integer read GetSymbolWidth write SetSymbolWidth default TeeDefaultLegendSymbolWidth;
    property CurrentPage:Boolean read FCurrentPage write SetCurrentPage default True;
    property DividingLines:TChartHiddenPen read FDividingLines write SetDividingLines;
    property FirstValue:Integer read FFirstValue write SetFirstValue default 0;
    property FontSeriesColor:Boolean read FFontSeriesColor write SetFontSeriesColor default False;
    property HorizMargin:Integer read FHorizMargin write SetHorizMargin default 0;
    property Inverted:Boolean read FInverted write SetInverted default False;
    property LegendStyle:TLegendStyle read FLegendStyle
				      write SetLegendStyle default lsAuto;
    property MaxNumRows:Integer read FMaxNumRows write SetMaxNumRows default 10;
    property ResizeChart:Boolean read FResizeChart write SetResizeChart default True;
    property Series:TChartSeries read FSeries write SetSeries;
    property Symbol:TLegendSymbol read FSymbol write SetSymbol;
    property TextStyle:TLegendTextStyle read FTextStyle
					write SetTextStyle default ltsLeftValue;
    property TopPos:Integer read FTopLeftPos write SetTopLeftPos default 10;
    property VertMargin:Integer read FVertMargin write SetVertMargin default 0;
    property VertSpacing:Integer read FVertSpacing write SetVertSpacing default 0;
    property Visible default True;
  end;

  TChartLegend=class(TCustomChartLegend)
  published
    property Alignment;
    property Bevel;
    property BevelWidth;
    property Brush;
    property CheckBoxes;
    property Color;
    property ColorWidth;
    property CurrentPage; // 5.02
    property CustomPosition;
    property DividingLines;
    property FirstValue;
    property Font;
    property FontSeriesColor;
    property Frame;
    property Gradient;
    property HorizMargin;
    property Inverted;
    property Left;
    property LegendStyle;
    property MaxNumRows;
    property ResizeChart;
    property Shadow;
    property ShapeStyle;
    property Symbol;
    property TextStyle;
    property Top;
    property TopPos;
    property Transparency;
    property Transparent;
    property VertMargin;
    property VertSpacing;
    property Visible;
  end;

  TChartTitle=class(TTeeCustomShapePosition)
  private
    FAdjustFrame : Boolean;
    FAlignment   : TAlignment;
    FText        : TStrings;

    IOnTop       : Boolean;
    Function GetShapeBounds:TRect;
    Procedure SetAdjustFrame(Value:Boolean);
    Procedure SetAlignment(Value:TAlignment);
    Procedure SetText(Value:TStrings);
  public
    Constructor Create(AOwner: TCustomTeePanel); virtual;
    Destructor Destroy; override;

    Procedure Assign(Source:TPersistent); override;
    Function Clicked(x,y:Integer):Boolean;
    procedure DrawTitle;
    property TitleRect:TRect read GetShapeBounds;
  published
    property AdjustFrame:Boolean read FAdjustFrame write SetAdjustFrame
				 default True;
    property Alignment: TAlignment read FAlignment
				   write SetAlignment default taCenter;
    property Bevel;
    property BevelWidth;
    property Brush;
    property Color;
    property CustomPosition; { 5.01 }
    property Font;
    property Frame;
    property Gradient;
    property Left; { 5.01 }
    property Shadow;
    property ShapeStyle;
    property Text:TStrings read FText write SetText;
    property Top; { 5.01 }
    property Transparency;
    property Transparent default True;
    property Visible default True;
  end;

  TChartFootTitle=class(TChartTitle)
  public
    Constructor Create(AOwner: TCustomTeePanel); override;
  end;

  TChartClick=procedure( Sender:TCustomChart;
			 Button:TMouseButton;
			 Shift: TShiftState;
			 X, Y: Integer) of object;

  TChartClickAxis=procedure( Sender:TCustomChart;
			     Axis:TChartAxis;
			     Button:TMouseButton;
			     Shift: TShiftState;
			     X, Y: Integer) of object;

  TChartClickSeries=procedure( Sender:TCustomChart;
			       Series:TChartSeries;
			       ValueIndex:Integer;
			       Button:TMouseButton;
			       Shift: TShiftState;
			       X, Y: Integer) of object;

  TChartClickTitle=procedure( Sender:TCustomChart;
			      ATitle:TChartTitle;
			      Button:TMouseButton;
			      Shift: TShiftState;
			      X, Y: Integer) of object;

  TOnGetLegendPos=Procedure( Sender:TCustomChart; Index:Integer;
			     Var X,Y,XColor:Integer) of object;

  TOnGetLegendRect=Procedure( Sender:TCustomChart; Var Rect:TRect) of object;

  TAxisSavedScales=Packed Record
    Auto    : Boolean;
    AutoMin : Boolean;
    AutoMax : Boolean;
    Min     : Double;
    Max     : Double;
  end;

  TAllAxisSavedScales=Array of TAxisSavedScales;

  TChartBackWall=class(TChartWall)
  published
    property Color default clSilver;
    property Transparent default True;
  end;

  TChartRightWall=class(TChartWall)
  published
    property Color default clSilver;
    property Visible default False;
  end;

  TChartWalls=class(TPersistent)
  private
    FBack   : TChartBackWall;
    FBottom : TChartWall;
    FChart  : TCustomChart;
    FLeft   : TChartWall;
    FRight  : TChartRightWall;

    procedure SetBack(const Value: TChartBackWall);
    procedure SetBottom(const Value: TChartWall);
    procedure SetLeft(const Value: TChartWall);
    procedure SetRight(const Value: TChartRightWall);
    function GetVisible: Boolean;
    procedure SetVisible(const Value: Boolean);
  public
    Constructor Create(Chart:TCustomChart);
    Destructor Destroy; override;
    Procedure Assign(Source:TPersistent); override;

    property Left:TChartWall read FLeft write SetLeft;
    property Right:TChartRightWall read FRight write SetRight;
    property Back:TChartBackWall read FBack write SetBack;
    property Bottom:TChartWall read FBottom write SetBottom;
    property Visible:Boolean read GetVisible write SetVisible;
  end;

  TChartAllowScrollEvent=Procedure( Sender:TChartAxis; Var AMin,AMax:Double;
				    Var AllowScroll:Boolean ) of object;

  TCustomChart=class(TCustomAxisPanel)
  private
    FFoot              : TChartTitle;
    FLegend            : TChartLegend;
    FSavedScales       : TAllAxisSavedScales;
    FScrollMouse       : TMouseButton;
    FSubFoot           : TChartTitle;
    FSubTitle          : TChartTitle;
    FTitle             : TChartTitle;
    FWalls             : TChartWalls;

    { events }
    FOnAllowScroll     : TChartAllowScrollEvent;
    FOnClickAxis       : TChartClickAxis;
    FOnClickBackGround : TChartClick;
    FOnClickLegend     : TChartClick;
    FOnClickSeries     : TChartClickSeries;
    FOnClickTitle      : TChartClickTitle;
    FOnGetLegendPos    : TOnGetLegendPos;
    FOnGetLegendRect   : TOnGetLegendRect;
    FOnGetLegendText   : TOnGetLegendText;

    Function ActiveSeriesUseAxis:Boolean;
    procedure BroadcastMouseEvent(AEvent:TChartMouseEvent;

⌨️ 快捷键说明

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