home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Delphi / TeeChartPro / TeeChart5Delphi5Eval.exe / %MAINDIR% / Examples / Features / TeeAxisLabelTool.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-10  |  2.8 KB  |  108 lines

  1. unit TeeAxisLabelTool;
  2.  
  3. interface
  4.  
  5. Uses Classes, SysUtils, TeeProcs, TeEngine, Chart, TeeTools;
  6.  
  7. { TAxisLabelTool. Example of a Custom Chart Tool. }
  8. { This custom tool is used to change Axis labels.
  9.  
  10.   If axis labels are bigger than 1000, the tool
  11.   appends "K" (thousands) to the label.
  12.   If the label is bigger than 100000, then it shows
  13.   "M" (millions).
  14.  
  15.   Example: 2000 is displayed as "2K"
  16.            4000000 is displayed as "4M".
  17.  
  18.   This tool is useful to reduce the space used by
  19.   big axis label values.
  20. }
  21. type
  22.   TAxisLabelTool=class(TTeeCustomToolAxis)
  23.   private
  24.     FMillion: String;
  25.     FThousand: String;
  26.     procedure SetMillion(const Value: String);
  27.     procedure SetThousand(const Value: String);
  28.   protected
  29.     procedure SetParentChart(const Value: TCustomAxisPanel); override;
  30.     Procedure OnGetLabel( Sender:TChartAxis; Series:TChartSeries;
  31.                ValueIndex:Integer; Var LabelText:String);
  32.   public
  33.     Constructor Create(AOwner:TComponent); override;
  34.     class Function Description:String; override;
  35.   published
  36.     property MillionText:String read FMillion write SetMillion;
  37.     property ThousandText:String read FThousand write SetThousand;
  38.   end;
  39.  
  40. implementation
  41.  
  42. { Here it comes the tool code... }
  43.  
  44. { TAxisLabelTool }
  45. constructor TAxisLabelTool.Create(AOwner: TComponent);
  46. begin
  47.   inherited;
  48.   FMillion:='M';
  49.   FThousand:='K';
  50. end;
  51.  
  52. class function TAxisLabelTool.Description: String;
  53. begin
  54.   result:='Axis Label';
  55. end;
  56.  
  57. procedure TAxisLabelTool.OnGetLabel(Sender: TChartAxis;
  58.   Series: TChartSeries; ValueIndex: Integer; var LabelText: String);
  59. var tmp : Double;
  60.     tmpSt : String;
  61. begin
  62.   if Active and Assigned(Axis) and (Sender=Axis) then
  63.   begin
  64.     try
  65.       tmpSt:=LabelText;
  66.       While Pos(ThousandSeparator,tmpSt)>0 do
  67.         Delete(tmpSt,Pos(ThousandSeparator,tmpSt),1);
  68.       tmp:=StrToFloat(tmpSt);
  69.       if tmp>=100000 then LabelText:=FormatFloat('0.#',tmp/1000000)+MillionText
  70.       else
  71.       if tmp>=1000 then LabelText:=FormatFloat('0.#',tmp/1000)+ThousandText;
  72.     except
  73.       on EConvertError do ;
  74.     end;
  75.   end;
  76. end;
  77.  
  78. procedure TAxisLabelTool.SetMillion(const Value: String);
  79. begin
  80.   if FMillion<>Value then
  81.   begin
  82.     FMillion:=Value;
  83.     if Assigned(ParentChart) then ParentChart.Invalidate;
  84.   end;
  85. end;
  86.  
  87. procedure TAxisLabelTool.SetParentChart(const Value: TCustomAxisPanel);
  88. begin
  89.   inherited;
  90.   if Assigned(ParentChart) then ParentChart.OnGetAxisLabel:=OnGetLabel;
  91. end;
  92.  
  93. procedure TAxisLabelTool.SetThousand(const Value: String);
  94. begin
  95.   if FThousand<>Value then
  96.   begin
  97.     FThousand:=Value;
  98.     if Assigned(ParentChart) then ParentChart.Invalidate;
  99.   end;
  100. end;
  101.  
  102. initialization
  103.   { "register" the custom tool to show it at the editor tool gallery }
  104.   RegisterTeeTools([TAxisLabelTool]);
  105. finalization
  106.   UnRegisterTeeTools([TAxisLabelTool]);
  107. end.
  108.