home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Src Code / IMAGEBAR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  5.6 KB  |  191 lines

  1. {**************************************}
  2. {   TeeChart Pro Charting Library      }
  3. {   Custom Series Example: TImageBar   }
  4. {**************************************}
  5. {$I teedefs.inc}
  6. unit ImageBar;
  7.  
  8. interface
  9.  
  10. Uses WinTypes, WinProcs, Classes, Graphics, Series, Chart;
  11.  
  12. { This unit implements a custom TeeChart Series.
  13.   The TImageBarSeries is a normal BarSeries with an optional Image to
  14.   be displayed on each Bar point, stretched or tiled.
  15.   Only rectangular Bar style is allowed.
  16.   The bitmap image is not "mapped" to Bars in 3D mode with
  17.   rotation or elevation. Only 2D and 3D with Orthogonal projection.
  18. }
  19. type TImageBarSeries=class(TBarSeries)
  20.      private
  21.        FImage:TPicture;
  22.        FImageTiled:Boolean;
  23.        Procedure SetImage(Value:TPicture);
  24.        Procedure SetImageTiled(Value:Boolean);
  25.        Procedure DrawTiledImage(AImage:TPicture; Const R:TRect; StartFromTop:Boolean);
  26.      public
  27.        Constructor Create(AOwner:TComponent); override;
  28.        Destructor Destroy; override;
  29.        Procedure DrawBar(BarIndex,StartPos,EndPos:Longint); override;
  30.        Function GetEditorClass:String; override;    
  31.        Procedure PrepareForGallery(IsEnabled:Boolean); override;
  32.      published
  33.        property Image:TPicture read FImage write SetImage;
  34.        property ImageTiled:Boolean read FImageTiled write SetImageTiled default False;
  35.      end;
  36.  
  37. { used also at ImaPoint.pas unit }
  38. Procedure LoadBitmapFromResourceName(ABitmap:TBitmap; const ResName: string);
  39.  
  40. implementation
  41.  
  42. Uses TeeProcs,SysUtils,TeCanvas,TeeProco;  { <-- needed only for the "Samples" constant }
  43.  
  44. { This resource file contains the default bitmap image (Bulb.bmp) }
  45. {$IFDEF D1}
  46. {$R TEEIMABA.R16}
  47. {$ELSE}
  48. {$R TEEIMABA.RES}
  49. {$ENDIF}
  50.  
  51. { This function loads a bitmap from a resource linked to the executable }
  52. Procedure LoadBitmapFromResourceName(ABitmap:TBitmap; const ResName: string);
  53. {$IFDEF D1}
  54. var tmpSt:Array[0..255] of char;
  55. {$ENDIF}
  56. begin
  57.   {$IFDEF D1}
  58.   ABitmap.Handle:=LoadBitmap(Hinstance,StrPCopy(tmpSt,ResName));
  59.   {$ELSE}
  60.   ABitmap.LoadFromResourceName(HInstance,ResName);
  61.   {$ENDIF}
  62. end;
  63.  
  64. { overrided constructor to create the Image property }
  65. Constructor TImageBarSeries.Create(AOwner:TComponent);
  66. begin
  67.   inherited Create(AOwner);
  68.   FImage:=TPicture.Create;
  69.   FImage.OnChange:=CanvasChanged;
  70.   LoadBitmapFromResourceName(FImage.Bitmap,'TeeMoney');  { <-- load default }
  71. end;
  72.  
  73. Destructor TImageBarSeries.Destroy;
  74. begin
  75.   FImage.Free;  { <-- remember to destroy private properties }
  76.   inherited Destroy;
  77. end;
  78.  
  79. Procedure TImageBarSeries.SetImage(Value:TPicture);
  80. begin
  81.   FImage.Assign(Value);
  82. end;
  83.  
  84. Procedure TImageBarSeries.SetImageTiled(Value:Boolean);
  85. begin
  86.   SetBooleanProperty(FImageTiled,Value);
  87. end;
  88.  
  89. { Add two bars only to the gallery }
  90. Procedure TImageBarSeries.PrepareForGallery(IsEnabled:Boolean);
  91. begin
  92.   inherited PrepareForGallery(IsEnabled);
  93.   FillSampleValues(2);
  94.   ParentChart.View3DOptions.Orthogonal:=True;
  95. end;
  96.  
  97. { This method draws an image in tiled mode }
  98. Procedure TImageBarSeries.DrawTiledImage( AImage:TPicture;
  99.                                           Const R:TRect;
  100.                                           StartFromTop:Boolean );
  101. Var tmpX      : Integer;
  102.     tmpY      : Integer;
  103.     tmpWidth  : Integer;
  104.     tmpHeight : Integer;
  105.     RectH     : Longint;
  106.     RectW     : Longint;
  107.     tmpRect   : TRect;
  108. begin
  109.   tmpWidth :=AImage.Width;
  110.   tmpHeight:=AImage.Height;
  111.   if (tmpWidth>0) and (tmpHeight>0) then
  112.   Begin
  113.     ParentChart.Canvas.ClipRectangle(R);
  114.     RectSize(R,RectW,RectH);
  115.     tmpY:=0;
  116.     while tmpY<RectH do
  117.     begin
  118.       tmpX:=0;
  119.       while tmpX<RectW do
  120.       begin
  121.         if StartFromTop then
  122.            tmpRect:=Rect(R.Left,R.Top+tmpY,R.Right,R.Top+tmpY+tmpHeight)
  123.         else
  124.            tmpRect:=Rect(R.Left,R.Bottom-tmpY-tmpHeight,R.Right,R.Bottom-tmpY);
  125.         ParentChart.Canvas.StretchDraw(tmpRect,AImage.Graphic);
  126.         Inc(tmpX,tmpWidth);
  127.       end;
  128.       Inc(tmpY,tmpHeight);
  129.     end;
  130.     ParentChart.Canvas.UnClipRectangle;
  131.   end;
  132. end;
  133.  
  134. Procedure TImageBarSeries.DrawBar(BarIndex,StartPos,EndPos:Longint);
  135. Var R   : TRect;
  136.     tmp : Integer;
  137. begin
  138.   { first thing to do is to call the inherited DrawBar method of TBarSeries }
  139.   inherited DrawBar(BarIndex, StartPos, EndPos );
  140.  
  141.   if FImage.Graphic<>nil then { <-- if non empty image... }
  142.   Begin
  143.     { Calculate the exact rectangle, removing borders }
  144.     R:=BarBounds;
  145.     if R.Bottom<R.Top then SwapInteger(R.Top,R.Bottom);
  146.     if BarPen.Visible then
  147.     begin
  148.       tmp:=BarPen.Width;
  149.       if (tmp>1) and ((tmp mod 2)=0) then Dec(tmp);
  150.       Inc(R.Left,tmp);
  151.       Inc(R.Top,tmp);
  152.       if not ParentChart.View3D then
  153.       begin
  154.         Dec(R.Right);
  155.         Dec(R.Bottom);
  156.       end;
  157.     end;
  158.     With ParentChart.Canvas,R do
  159.     begin
  160.       TopLeft    :=Calculate3DPosition(Left,Top,StartZ);
  161.       BottomRight:=Calculate3DPosition(Right,Bottom,StartZ);
  162.     end;
  163.     { Draw the image }
  164.     if FImageTiled then { tiled }
  165.        DrawTiledImage(FImage,R,BarBounds.Bottom<BarBounds.Top)
  166.     else { stretched }
  167.        ParentChart.Canvas.StretchDraw(R,FImage.Graphic);
  168.   end;
  169. end;
  170.  
  171. Function TImageBarSeries.GetEditorClass:String; 
  172. begin
  173.   result:='TImageBarSeriesEditor';
  174. end;
  175.  
  176. { Series/Functions Registration/Un-Registration }
  177. Procedure ImageBarExitProc; far;
  178. begin
  179.   UnRegisterTeeSeries([ TImageBarSeries ]);
  180. end;
  181.  
  182. initialization
  183.   RegisterTeeSeries( TImageBarSeries, 'ImageBar', TeeMsg_GallerySamples, 1 );
  184. {$IFDEF D1}
  185.   AddExitProc(ImageBarExitProc);
  186. {$ELSE}
  187. finalization
  188.   ImageBarExitProc;
  189. {$ENDIF}
  190. end.
  191.