home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Delphi1_And_Delphi2 / EXAMPLES / EXTENDED / UTIPS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  4.6 KB  |  154 lines

  1. {*********************************************}
  2. {  TeeChart Pro 4 example                     }
  3. {  Copyright (c) 1995-1998 by David Berneda   }
  4. {  All rights reserved                        }
  5. {*********************************************}
  6. unit Utips;
  7. {$P-}  { <-- for Delphi 1 compatibility }
  8.  
  9. interface
  10.  
  11. { This example shows how to customize Series Marks.
  12.   A single Mark is displayed, when the user clicks on a Bar.
  13.   The OnGetMarkText event is used to select which Mark should
  14.   be displayed.
  15.  
  16.   The example also draws a rectangle around the Bar and the Legend
  17.   item.
  18. }
  19. uses
  20.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  21.   Forms, Dialogs, TeEngine, Series, ExtCtrls, TeeProcs, Chart, StdCtrls;
  22.  
  23. type
  24.   TFormMarkTips = class(TForm)
  25.     Chart1: TChart;
  26.     Series1: TBarSeries;
  27.     Button1: TButton;
  28.     CheckBox1: TCheckBox;
  29.     Memo1: TMemo;
  30.     procedure FormCreate(Sender: TObject);
  31.     procedure Series1GetMarkText(Sender: TChartSeries; ValueIndex: Integer;
  32.       var MarkText: String);
  33.     procedure Series1Click(Sender: TChartSeries; ValueIndex: Integer;
  34.       Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  35.     procedure Chart1AfterDraw(Sender: TObject);
  36.     procedure Button1Click(Sender: TObject);
  37.     procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  38.       Y: Integer);
  39.   private
  40.     { Private declarations }
  41.   public
  42.     { Public declarations }
  43.     ClickedBar:Integer;
  44.   end;
  45.  
  46. var
  47.   FormMarkTips: TFormMarkTips;
  48.  
  49. implementation
  50.  
  51. {$R *.DFM}
  52.  
  53. procedure TFormMarkTips.FormCreate(Sender: TObject);
  54. begin
  55.   ClickedBar:=-1;   { <-- temporary variable to store the "clicked" index }
  56.   With Series1 do
  57.   begin
  58.     FillSampleValues(8);      { <-- random data }
  59.     Cursor:=crTeeHand;        { <-- nice cursor }
  60.     AutoMarkPosition:=False;  { <-- do not move marks positions }
  61.   end;
  62. end;
  63.  
  64. { This event is called for each Mark in the Series }
  65. procedure TFormMarkTips.Series1GetMarkText(Sender: TChartSeries;
  66.   ValueIndex: Integer; var MarkText: String);
  67. begin
  68.   if ValueIndex<>ClickedBar then  { <-- is this the "clicked" bar? }
  69.      MarkText:=''  { <-- if not, do not draw mark }
  70.   else
  71.      { if yes, set mark color to be the same as bar color }
  72.      Series1.Marks.BackColor:=Series1.ValueColor[ClickedBar];
  73. end;
  74.  
  75. { This event is called when user clicks on a Bar point... }
  76. procedure TFormMarkTips.Series1Click(Sender: TChartSeries; ValueIndex: Integer;
  77.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  78. begin
  79.   ClickedBar:=ValueIndex;  { <-- User has clicked a bar !!  Store which }
  80.   Chart1.Repaint;  { <-- force repaint to show new clicked bar... }
  81. end;
  82.  
  83. { This event is called each time the Chart redraws... }
  84. procedure TFormMarkTips.Chart1AfterDraw(Sender: TObject);
  85. var tmp,x0,y0,x1,y1:Integer;
  86. begin
  87.   if ClickedBar<>-1 then   { <-- if user clicked on a bar... }
  88.   With Series1,Chart1.Canvas do
  89.   begin
  90.     Pen.Style:=psDot; { <-- set pen attributes }
  91.     Pen.Width:=1;
  92.  
  93.     { check pen color is not the same as clicked bar color,
  94.       otherwise the rectangle will not be visible ! }
  95.     if ValueColor[ClickedBar]=clWhite then
  96.        Pen.Color:=clRed
  97.     else
  98.        Pen.Color:=clWhite;
  99.  
  100.     { set brush to clear to not fill the rectangle... }
  101.     Brush.Style:=bsClear;
  102.  
  103.     { calculate the rectangle coordinates of the rectangle... }
  104.     tmp:=CalcXPos(ClickedBar);
  105.     x0:=tmp;
  106.     x1:=tmp+BarWidth;
  107.     y0:=CalcYPosValue(PointOrigin(ClickedBar,True));  { <-- bar bottom  }
  108.     y1:=CalcYPos(ClickedBar);
  109.  
  110.     { draw the rectangle }
  111.     {$IFDEF GL}
  112.     RectangleWithZ(Rect(x0,y0,x1,y1),0);
  113.     {$ELSE}
  114.     Rectangle(x0,y0,x1,y1);
  115.     {$ENDIF}
  116.  
  117.     { draw another rectangle at legend item... }
  118.     With (Chart1.Legend as TChartLegend).RectLegend do
  119.     begin
  120.       x0:=Left;
  121.       x1:=Right;
  122.       Chart1.FontCanvas(Chart1.Legend.Font);
  123.       tmp:=Chart1.Canvas.TextHeight('W');
  124.       y0:=Top+ClickedBar*tmp+1;
  125.       y1:=y0+tmp;
  126.     end;
  127.     Pen.Color:=clBlue;
  128.     Rectangle(x0,y0,x1,y1);
  129.   end;
  130. end;
  131.  
  132. procedure TFormMarkTips.Button1Click(Sender: TObject);
  133. begin
  134.   Close;
  135. end;
  136.  
  137. procedure TFormMarkTips.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  138.   Y: Integer);
  139. var NewClickedBar:Integer;
  140. begin
  141.   if CheckBox1.Checked then   { <-- if "Follow mouse" is checked ... }
  142.   begin
  143.     NewClickedBar:=Series1.Clicked(X,Y);
  144.     if (NewClickedBar<>-1) and (NewClickedBar<>ClickedBar) then
  145.     begin
  146.       ClickedBar:=NewClickedBar;  { <-- set ClickedBar automatically... }
  147.       Chart1.Repaint;
  148.     end;
  149.   end;
  150. end;
  151.  
  152. end.  { end for today... <g> }
  153.  
  154.