home *** CD-ROM | disk | FTP | other *** search
/ PC Expert 29 / Pce29cd.iso / RUNIMAGE / DELPHI40 / DEMOS / TEECHART / UDRAW.PAS < prev    next >
Pascal/Delphi Source File  |  1998-06-16  |  5KB  |  194 lines

  1. {****************************************}
  2. {    TeeChart. Draw Example              }
  3. { Copyright (c) 1995,96 by David Berneda }
  4. {    All Rights Reserved                 }
  5. {****************************************}
  6. unit Udraw;
  7.  
  8. interface
  9.  
  10. { THIS EXAMPLE SHOWS HOW TO DRAW ADDITIONAL CUSTOMIZED THINGS TO A
  11.   CHART COMPONENT }
  12. uses
  13.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  14.   Forms, Dialogs, Chart, Series, ExtCtrls, Teengine, StdCtrls, Buttons,
  15.   TeeProcs;
  16.  
  17. type
  18.   TDrawForm = class(TForm)
  19.     Chart1: TChart;
  20.     LineSeries1: TLineSeries;
  21.     Panel1: TPanel;
  22.     BitBtn3: TBitBtn;
  23.     CheckBox1: TCheckBox;
  24.     Timer1: TTimer;
  25.     BitBtn1: TBitBtn;
  26.     CheckBox2: TCheckBox;
  27.     Memo1: TMemo;
  28.     procedure FormCreate(Sender: TObject);
  29.     procedure LineSeries1BeforeDrawValues(Sender: TObject);
  30.     procedure LineSeries1AfterDrawValues(Sender: TObject);
  31.     procedure Timer1Timer(Sender: TObject);
  32.     procedure CheckBox1Click(Sender: TObject);
  33.     procedure BitBtn1Click(Sender: TObject);
  34.     procedure CheckBox2Click(Sender: TObject);
  35.   private
  36.     { Private declarations }
  37.   public
  38.     { Public declarations }
  39.     Percent:Double;
  40.     DeltaPercent:Integer;
  41.   end;
  42.  
  43. implementation
  44.  
  45. {$R *.DFM}
  46.  
  47. procedure TDrawForm.FormCreate(Sender: TObject);
  48. begin
  49.   Percent:=50;  { <-- used for this demo only }
  50.   LineSeries1.FillSampleValues(20);
  51. end;
  52.  
  53. procedure TDrawForm.LineSeries1BeforeDrawValues(Sender: TObject);
  54. Const
  55.    MyColors:array[1..5] of TColor=
  56.     ( clNavy,
  57.       clGreen,
  58.       clYellow,
  59.       clRed,
  60.       $00000080 { very red }
  61.       );
  62. var t,partial:Longint;
  63.     tmpRect:TRect;
  64.     YPosition:Longint;
  65.     tmpYCenterValue:Double;
  66. begin
  67.   With Chart1 do
  68.   Begin
  69.     { we will divide the total chart width by 5 }
  70.     tmpRect:=ChartRect;
  71.     tmpRect.Right:=tmpRect.Left;
  72.     partial:=ChartWidth div 5;
  73.  
  74.     { change the brush style }
  75.     Canvas.Brush.Style:=bsDiagCross;
  76.     Canvas.Pen.Style:=psClear;
  77.  
  78.     { for each section, fill with a specific color }
  79.     for t:=1 to 5 do
  80.     Begin
  81.       { adjust the rectangle dimension }
  82.       tmpRect.Right :=tmpRect.Right+partial+1 ;
  83.  
  84.       { set the brush color }
  85.       Canvas.Brush.Color:=MyColors[t];
  86.  
  87.       { paint !!! }
  88.       With tmpRect do
  89.         Canvas.Rectangle( Left+Width3D,Top-Height3D,Right+Width3D,Bottom-Height3D );
  90.  
  91.       { adjust rectangle }
  92.       tmpRect.Left:=tmpRect.Right;
  93.     end;
  94.  
  95.     { first calculate the middle vertical value (based on LineSeries points) }
  96.     With LineSeries1.YValues do
  97.          tmpYCenterValue:=MinValue+Percent*(MaxValue-MinValue)/100.0;
  98.  
  99.     { then calculate the Screen Pixel coordinate of the above value }
  100.     YPosition:=LeftAxis.CalcYPosValue(tmpYCenterValue);
  101.  
  102.     With Canvas do
  103.     begin
  104.       { change pen and draw the line }
  105.       Pen.Width:=3;
  106.       Pen.Style:=psSolid;
  107.       Pen.Color:=clRed;
  108.       MoveTo(ChartRect.Left,YPosition);
  109.       LineTo(ChartRect.Left+Width3D,YPosition-Height3D);
  110.       LineTo(ChartRect.Right+Width3D,YPosition-Height3D);
  111.     end;
  112.   end;
  113. end;
  114.  
  115. procedure TDrawForm.LineSeries1AfterDrawValues(Sender: TObject);
  116. Var YPosition:Longint;
  117.     tmpYCenterValue:Double;
  118. begin
  119.   With Chart1,Canvas do
  120.   Begin
  121.     { first calculate the middle vertical value (based on LineSeries points) }
  122.     With LineSeries1.YValues do
  123.          tmpYCenterValue:=MinValue+Percent*(MaxValue-MinValue)/100.0;
  124.  
  125.     { then calculate the Screen Pixel coordinate of the above value }
  126.     YPosition:=LeftAxis.CalcYPosValue(tmpYCenterValue);
  127.  
  128.     { change pen and draw the line }
  129.     Pen.Width:=3;
  130.     Pen.Style:=psSolid;
  131.     Pen.Color:=clRed;
  132.     MoveTo(ChartRect.Left,YPosition);
  133.     LineTo(ChartRect.Right,YPosition);
  134.     LineTo(ChartRect.Right+Width3D,YPosition-Height3D);
  135.  
  136.     { change font and draw some text above the line }
  137.  
  138.     Font.Name:='Arial';
  139.  
  140.     { VERY IMPORTANT !!!!!! }
  141.     { THIS IS NECESSARY IF YOU'RE GOING TO PRINT !!!! }
  142.     { IT MAKES FONT SIZES TO WORK FINE BOTH AT SCREEN AND PRINTER. }
  143.  
  144.     Font.Height:=-24;   { <-- express font size in "Height", NOT "Size" }
  145.  
  146.     Font.Color:=clYellow;
  147.     Font.Style:=[fsBold];
  148.  
  149.     { Set transparent background... }
  150.     Brush.Style:=bsClear;
  151.  
  152.     { Output some text... }
  153.     TextOut( ChartRect.Left+20,
  154.              YPosition-24 ,
  155.              'This is '+FloatToStr(tmpYCenterValue));
  156.   end;
  157. end;
  158.  
  159. procedure TDrawForm.Timer1Timer(Sender: TObject);
  160. begin
  161.   if Percent+DeltaPercent>100 then
  162.   begin
  163.     Percent:=100;
  164.     DeltaPercent:=-5;
  165.   end
  166.   else
  167.   if Percent+DeltaPercent<0 then
  168.   begin
  169.     Percent:=0;
  170.     DeltaPercent:=5;
  171.   end
  172.   else Percent:=Percent+DeltaPercent;
  173.   Chart1.Repaint;
  174. end;
  175.  
  176. procedure TDrawForm.CheckBox1Click(Sender: TObject);
  177. begin
  178.   Timer1.Enabled:=CheckBox1.Checked;
  179.   DeltaPercent:=5;
  180. end;
  181.  
  182. procedure TDrawForm.BitBtn1Click(Sender: TObject);
  183. begin
  184.  { try with and without this --->  Chart1.PrintResolution := -100; }
  185.   Chart1.PrintLandscape;
  186. end;
  187.  
  188. procedure TDrawForm.CheckBox2Click(Sender: TObject);
  189. begin
  190.   Chart1.View3D:=CheckBox2.Checked;
  191. end;
  192.  
  193. end.
  194.