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

📄 teemapseries.pas

📁 TeeChart7Source 控件
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{******************************************}
{    TeeChart Map Series                   }
{ Copyright (c) 2000-2004 by David Berneda }
{    All Rights Reserved                   }
{******************************************}
unit TeeMapSeries;
{$I TeeDefs.inc}

interface

Uses {$IFNDEF LINUX}
     Windows, Messages,
     {$ENDIF}
     SysUtils, Classes,
     {$IFDEF CLX}
     QGraphics, Types,
     {$ELSE}
     Graphics,
      {$IFDEF D6}
      Types,
      {$ENDIF}
     {$ENDIF}
     TeEngine, TeCanvas, TeeProcs, TeeSurfa;

type
  TTeePolygon=class;

  TPolygonSeries=class(TChartSeries)
  protected
    Procedure DrawLegendShape(ValueIndex:Integer; Const Rect:TRect); override;
    Procedure NotifyValue(ValueEvent:TValueEvent; ValueIndex:Integer); override;
    procedure PrepareLegendCanvas(ValueIndex:Integer; Var BackColor:TColor;
                                Var BrushStyle:TBrushStyle); override;
    Procedure SetActive(Value:Boolean); override;
    Procedure SetSeriesColor(AColor:TColor); override;
  public
    Procedure FillSampleValues(NumValues:Integer); override;
    Function Polygon:TTeePolygon;
  end;

  TMapSeries=class;

  TTeePolygon=class(TCollectionItem)
  private
    FClosed      : Boolean;
    FGradient    : TChartGradient;
    FParentBrush : Boolean;
    FParentPen   : Boolean;
    FPoints      : TPolygonSeries;
    FTransparency: TTeeTransparency;

    IPoints      : TPointArray;

    {$IFDEF CLR}
    procedure CanvasChanged(Sender: TObject);
    {$ENDIF}

    function GetBrush: TChartBrush;
    function GetColor: TColor;
    function GetGradient: TChartGradient;
    function GetPen: TChartPen;
    Function GetText:String;
    function GetZ: Double;

    procedure SetBrush(const Value: TChartBrush);
    procedure SetClosed(const Value: Boolean);
    procedure SetColor(const Value: TColor);
    procedure SetGradient(const Value: TChartGradient);
    procedure SetParentBrush(const Value: Boolean);
    procedure SetParentPen(const Value: Boolean);
    procedure SetPen(const Value: TChartPen);
    Procedure SetText(Const Value:String);
    procedure SetTransparency(const Value: TTeeTransparency);
    procedure SetZ(const Value: Double);

    //Function ZPosition:Double;
  public
    ParentSeries : TMapSeries;

    Constructor Create(Collection:TCollection); override;
    Destructor Destroy; override;

    Function AddXY(Const Point:TFloatPoint):Integer; overload;
    Function AddXY(Const X,Y:Double):Integer; overload;
    Procedure Draw(ACanvas:TCanvas3D; ValueIndex:Integer);
    Function GetPoints:TPointArray;
    Function Visible:Boolean;

    Function Bounds:TRect;  // 7.0
    property Points:TPolygonSeries read FPoints;
  published
    property Brush:TChartBrush read GetBrush write SetBrush;
    property Closed:Boolean read FClosed write SetClosed default True;
    property Color:TColor read GetColor write SetColor default clWhite;
    property Gradient:TChartGradient read GetGradient write SetGradient;
    property ParentBrush:Boolean read FParentBrush write SetParentBrush default True;
    property ParentPen:Boolean read FParentPen write SetParentPen default True;
    property Pen:TChartPen read GetPen write SetPen;
    property Text:String read GetText write SetText;
    property Transparency:TTeeTransparency read FTransparency write SetTransparency default 0;  // 7.0
    property Z:Double read GetZ write SetZ;
  end;

  TTeePolygonList=class(TOwnedCollection)
  private
    Procedure Delete(Start,Quantity:Integer); overload;
    Function Get(Index:Integer):TTeePolygon;
    Procedure Put(Index:Integer; Const Value:TTeePolygon);
    function GetByName(const AName: String): TTeePolygon;
  public
    Function Add:TTeePolygon;
    Function Owner:TMapSeries;
    property Polygon[Index:Integer]:TTeePolygon read Get write Put; default;
    property ByName[const AName:String]:TTeePolygon read GetByName;  // 7.0
  end;

  TMapSeries=class(TCustom3DPaletteSeries)
  private
    FShapes : TTeePolygonList;
    I3DList : Array of TTeePolygon;

    Function CompareOrder(a,b:Integer):Integer;
    Function GetPolygon(Index:Integer):TTeePolygon; // 7.0
    procedure SetShapes(const Value: TTeePolygonList);
    Procedure SwapPolygon(a,b:Integer);
  protected
    Procedure AddSampleValues(NumValues:Integer; OnlyMandatory:Boolean=False); override;
    Procedure CalcHorizMargins(Var LeftMargin,RightMargin:Integer); override;
    Procedure CalcVerticalMargins(Var TopMargin,BottomMargin:Integer); override;
    class procedure CreateSubGallery(AddSubChart: TChartSubGalleryProc); override;
    Procedure DrawAllValues; override;
    Procedure DrawMark( ValueIndex:Integer; Const St:String;
                        APosition:TSeriesMarkPosition); override;
    Procedure DrawValue(ValueIndex:Integer); override;
    Procedure GalleryChanged3D(Is3D:Boolean); override;
    class Function GetEditorClass:String; override;
    procedure PrepareForGallery(IsEnabled:Boolean); override;
    class procedure SetSubGallery(ASeries: TChartSeries; Index: Integer); override;
  public
    Constructor Create(AOwner:TComponent); override;
    Destructor Destroy; override;

    Procedure Clear; override;
    Function Clicked(x,y:Integer):Integer; override;
    Procedure Delete(ValueIndex:Integer); overload; override;
    Procedure Delete(Start,Quantity:Integer; RemoveGap:Boolean=False); overload; override;
    Function MaxXValue:Double; override;
    Function MaxYValue:Double; override;
    Function MinXValue:Double; override;
    Function MinYValue:Double; override;
    Function NumSampleValues:Integer; override;
    procedure SwapValueIndex(a,b:Integer); override;

    property Polygon[Index:Integer]:TTeePolygon read GetPolygon; default; // 7.0
  published
    { Published declarations }
    property Active;
    property ColorSource;
    property Cursor;
    property HorizAxis;
    property Marks;
    property ParentChart;
    property DataSource;
    property PercentFormat;
    property SeriesColor;
    property ShowInLegend;
    property Title;
    property ValueFormat;
    property VertAxis;
    property XLabelsSource;

    property Brush;
    property EndColor;
    property MidColor;
    property LegendEvery;
    property Pen;
    property PaletteMin;
    property PaletteStep;
    property PaletteSteps;
    property Shapes:TTeePolygonList read FShapes write SetShapes stored False;
    property StartColor;
    property UseColorRange;
    property UsePalette;
    property UsePaletteMin;
    property TimesZOrder;
    property XValues;
    property YValues;
    property ZValues;

    { events }
    property AfterDrawValues;
    property BeforeDrawValues;
    property OnAfterAdd;
    property OnBeforeAdd;
    property OnClearValues;
    property OnClick;
    property OnDblClick;
    property OnGetColor;
    property OnGetMarkText;
    property OnMouseEnter;
    property OnMouseLeave;
  end;

implementation

Uses Math,
     {$IFDEF CLR}
     Variants,
     {$ENDIF}
     TeeConst, TeeProCo, Chart;

{$IFNDEF CLR}
type
  TSeriesAccess=class(TCustomChartElement);
{$ENDIF}

{ TTeePolygon }
constructor TTeePolygon.Create(Collection: TCollection);
begin
  inherited;
  ParentSeries:=TTeePolygonList(Collection).Owner as TMapSeries;

  FClosed:=True;
  FPoints:=TPolygonSeries.Create(nil);  // 7.0
  FPoints.Tag:={$IFDEF CLR}Variant{$ELSE}Integer{$ENDIF}(Self);
  FPoints.XValues.Order:=loNone;
  FPoints.ShowInLegend:=False;

  FParentPen:=True;
  FParentBrush:=True;
  ParentSeries.AddXY(0,0);
end;

Destructor TTeePolygon.Destroy;
begin
  IPoints:=nil;
  FPoints.Free;
  FGradient.Free;
  inherited;
end;

function TTeePolygon.AddXY(const Point:TFloatPoint): Integer;
begin
  result:=FPoints.AddXY(Point.X,Point.Y);
end;

function TTeePolygon.AddXY(const X,Y: Double): Integer;
begin
  result:=FPoints.AddXY(X,Y);
end;

{ return the array of Points in screen (pixel) coordinates }
Function TTeePolygon.GetPoints:TPointArray;
var t : Integer;
    tmpHoriz : TChartAxis;
    tmpVert  : TChartAxis;
    tmpX     : TChartValues;
    tmpY     : TChartValues;
begin
  SetLength(IPoints,FPoints.Count);
  result:=IPoints;

  tmpHoriz:=ParentSeries.GetHorizAxis;
  tmpVert:=ParentSeries.GetVertAxis;

  tmpX:=FPoints.XValues.Value;
  tmpY:=FPoints.YValues.Value;

  for t:=0 to FPoints.Count-1 do
  begin
    result[t].X:=tmpHoriz.CalcPosValue(tmpX[t]);
    result[t].Y:=tmpVert.CalcPosValue(tmpY[t]);
  end;
end;

// Returns True if the polygon contains points that lie inside
// the chart "ChartRect" (the visible chart area).
Function TTeePolygon.Visible:Boolean;

  // optimized version of InteresectRect
  function ContainsRect(R1:TRect; const R2:TRect): Boolean;
  begin
    if R2.Left > R1.Left then R1.Left := R2.Left;
    if R2.Right < R1.Right then R1.Right := R2.Right;

    if R2.Top > R1.Top then R1.Top := R2.Top;
    if R2.Bottom < R1.Bottom then R1.Bottom := R2.Bottom;
    result := not ((R1.Right < R1.Left) or (R1.Bottom < R1.Top));
  end;

var tmpR : TRect;
    tmpChart : TCustomAxisPanel;
begin
  tmpChart:=ParentSeries.ParentChart;
  result:=not tmpChart.ClipPoints;

  if not result then
  begin
    with ParentSeries.GetHorizAxis do
    begin
      tmpR.Left:=CalcPosValue(FPoints.XValues.MinValue);
      tmpR.Right:=CalcPosValue(FPoints.XValues.MaxValue);
    end;

    with ParentSeries.GetVertAxis do
    begin
      tmpR.Top:=CalcPosValue(FPoints.YValues.MaxValue);
      tmpR.Bottom:=CalcPosValue(FPoints.YValues.MinValue);
    end;

    result:=ContainsRect(tmpChart.ChartRect, tmpR);
  end;
end;

{ draw the polygon... }
procedure TTeePolygon.Draw(ACanvas: TCanvas3D; ValueIndex:Integer);
Var tmpZ  : Integer;
    tmpIs3D : Boolean;
    tmpBlend : TTeeBlend;
    tmpR  : TRect;
begin
  if FPoints.Active and (FPoints.Count>0) and Visible then
  begin
    // set pen and brush...
    if ParentPen then ACanvas.AssignVisiblePen(ParentSeries.Pen)
                 else ACanvas.AssignVisiblePen(Self.Pen);
    if ParentBrush then ACanvas.AssignBrush(ParentSeries.Brush,ParentSeries.ValueColor[ValueIndex])
                   else ACanvas.AssignBrush(Self.Brush,ParentSeries.ValueColor[ValueIndex]);

    GetPoints;

    tmpIs3D:=ParentSeries.ParentChart.View3D;

    // Calculate "Z" depth position
    if tmpIs3D then tmpZ:=ParentSeries.CalcZPos(Index)
               else tmpZ:=0;

    if Transparency>0 then
    begin
      tmpR:=PolygonBounds(IPoints);
      tmpBlend:=ACanvas.BeginBlending(ACanvas.RectFromRectZ(tmpR,tmpZ),Transparency)
    end
    else
       tmpBlend:=nil;

    // Fill background with gradient...
    if Assigned(Self.FGradient) and Self.FGradient.Visible
       and ParentSeries.ParentChart.CanClip then
    begin
      Self.Gradient.Draw(ACanvas,IPoints,tmpZ,tmpIs3D);
      ACanvas.Brush.Style:=bsClear;
    end;

    // Draw the shape...

    with ACanvas do
    if tmpIs3D then
    begin
      if Self.Closed then
         PolygonWithZ(IPoints,tmpZ)
      else
         {$IFDEF D5}
         Polyline(IPoints,tmpZ);
         {$ELSE}
         Polyline(IPoints);  // D4: Pending
         {$ENDIF}
    end
    else
    begin
      if Self.Closed then
         Polygon(IPoints)
      else
         Polyline(IPoints);
    end;

    if Assigned(tmpBlend) then
       ACanvas.EndBlending(tmpBlend);
  end;
end;

function TTeePolygon.GetBrush: TChartBrush;
begin
  result:=FPoints.Brush;
end;

function TTeePolygon.GetColor: TColor;
begin
  result:=ParentSeries.ValueColor[Index];
end;

function TTeePolygon.GetPen: TChartPen;
begin
  result:=FPoints.Pen;
end;

procedure TTeePolygon.SetBrush(const Value: TChartBrush);
begin
  FPoints.Brush:=Value;
end;

procedure TTeePolygon.SetColor(const Value: TColor);
begin
  Points.SeriesColor:=Value;
end;

procedure TTeePolygon.SetClosed(const Value: Boolean);
begin
  ParentSeries.SetBooleanProperty(FClosed,Value);
end;

Function TTeePolygon.GetGradient: TChartGradient;
begin
  if not Assigned(FGradient) then
     FGradient:=TChartGradient.Create({$IFNDEF CLR}TSeriesAccess(ParentSeries).{$ENDIF}CanvasChanged);

  result:=FGradient;
end;

procedure TTeePolygon.SetGradient(const Value: TChartGradient);
begin
  if Assigned(Value) then
     Gradient.Assign(Value)
  else
     FreeAndNil(FGradient);
end;

procedure TTeePolygon.SetPen(const Value: TChartPen);
begin
  FPoints.Pen:=Value;
end;

procedure TTeePolygon.SetZ(const Value: Double);
begin
  ParentSeries.ZValues.Value[Index]:=Value;
  ParentSeries.Repaint;
end;

function TTeePolygon.GetZ: Double;
begin
  result:=ParentSeries.ZValues.Value[Index];
end;

function TTeePolygon.GetText: String;
begin
  result:=ParentSeries.Labels[Index];
end;

procedure TTeePolygon.SetText(const Value: String);
begin
  ParentSeries.Labels[Index]:=Value;
end;

procedure TTeePolygon.SetParentBrush(const Value: Boolean);
begin
  ParentSeries.SetBooleanProperty(FParentBrush,Value);
end;

procedure TTeePolygon.SetParentPen(const Value: Boolean);
begin
  ParentSeries.SetBooleanProperty(FParentPen,Value);
end;

(*
function TTeePolygon.ZPosition:Integer;
var x : Integer;
    y : Integer;
    P : TPoint;
    tmpZ : Integer;
begin
  result:=0;
  With ParentSeries do
  if FPoints.Count>0 then
  begin
    X:=GetHorizAxis.CalcPosValue(FPoints.XValues.Value[0]);
    Y:=GetVertAxis.CalcPosValue(FPoints.YValues.Value[0]);
    tmpZ:=ParentSeries.CalcZPos(0);
    P:=ParentSeries.ParentChart.Canvas.Calculate3DPosition(TeePoint(x,y),tmpZ);
  end;
end;
*)

function TTeePolygon.Bounds: TRect;  // 7.0
begin
  result:=PolygonBounds(GetPoints);
end;

{$IFDEF CLR}
procedure TTeePolygon.CanvasChanged(Sender: TObject);
begin
  ParentSeries.Repaint;
end;
{$ENDIF}

procedure TTeePolygon.SetTransparency(const Value: TTeeTransparency);
begin
  if FTransparency<>Value then
  begin
    FTransparency:=Value;
    ParentSeries.Repaint;
  end;
end;

{ TTeePolygonList }

⌨️ 快捷键说明

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