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

  1. unit UCandle;
  2. {$P-} { <-- only for Delphi 1.0 }
  3.  
  4. interface
  5.  
  6. { This project shows how to use the Candle Series programatically,
  7.   to remove Weekends from the bottom axis.
  8. }
  9. uses
  10.   Wintypes,WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  11.   TeEngine, Series, OHLChart, CandleCh, ExtCtrls, TeeProcs, Chart, StdCtrls;
  12.  
  13. type
  14.   TCandleWeekForm = class(TForm)
  15.     Chart1: TChart;
  16.     Series1: TCandleSeries;
  17.     Button1: TButton;
  18.     RadioGroup1: TRadioGroup;
  19.     Memo1: TMemo;
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure Chart1GetAxisLabel(Sender: TChartAxis; Series: TChartSeries;
  22.       ValueIndex: Longint; var LabelText: String);
  23.     procedure Series1BeforeDrawValues(Sender: TObject);
  24.     procedure RadioGroup1Click(Sender: TObject);
  25.     procedure Button1Click(Sender: TObject);
  26.   private
  27.     { Private declarations }
  28.   public
  29.     { Public declarations }
  30.   end;
  31.  
  32. var
  33.   CandleWeekForm: TCandleWeekForm;
  34.  
  35. implementation
  36.  
  37. {$R *.DFM}
  38.  
  39. { My Sample Data }
  40. const MyOpen:Array[0..10] of Double=(100,110,120,130,140,150,160,170,160,150,140);
  41.       MyHigh:Array[0..10] of Double=(105,115,125,135,145,155,165,175,169,159,147);
  42.       MyLow :Array[0..10] of Double=(95,105,115,125,135,145,155,165,158,149,130);
  43.      MyClose:Array[0..10] of Double=(104,106,124,134,136,154,164,166,165,153,136);
  44.  
  45.                                      { Days of January 1998 }
  46.      MyDate :Array[0..10] of Integer=(5,6,7,8,9,12,13,14,15,16,19);
  47.  
  48. procedure TCandleWeekForm.FormCreate(Sender: TObject);
  49. var t:Integer;
  50. begin
  51.   { Add Candles to the Series... }
  52.   for t:=0 to 10 do
  53.   begin
  54.     { use "t" instead of dates to remove weekends }
  55.     Series1.AddCandle(t,MyOpen[t],MyHigh[t],MyLow[t],MyClose[t]);
  56.  
  57.     { change after the candle label "Mon", "Tue", "Wed", etc. }
  58.  
  59.     Series1.XLabel[t]:=ShortDayNames[DayOfWeek(EncodeDate(1998,1,MyDate[t]))];
  60.   end;
  61.  
  62.   { some nice settings... }
  63.   Series1.CandleWidth:=10;
  64.   Series1.Pen.Width:=2;
  65.   Chart1.View3D:=False;
  66.  
  67.   { change bottom axis labels font }
  68.   Chart1.BottomAxis.LabelsFont.Style:=[fsBold];
  69.   Chart1.BottomAxis.LabelsFont.Size:=12;
  70.  
  71.   Chart1.LeftAxis.Grid.Visible:=False;
  72.  
  73.   { change chart title... }
  74.   With Chart1.Title.Text do
  75.   begin
  76.     Clear;
  77.     Add('Candle Series without weekends');
  78.   end;
  79.  
  80.   { change memo1 }
  81.   With Memo1.Lines do
  82.   begin
  83.     Clear;
  84.     for t:=1 to 7 do Add(ShortDayNames[t]+'='+IntToStr(t));
  85.   end;
  86.   Memo1.Visible:=False;
  87.  
  88. end;
  89.  
  90. procedure TCandleWeekForm.Chart1GetAxisLabel(Sender: TChartAxis;
  91.   Series: TChartSeries; ValueIndex: Longint; var LabelText: String);
  92. begin
  93.  { this event is used to paint the Mondays label in color RED }
  94.   if ValueIndex>=0 then
  95.   With Sender do
  96.   if DayOfWeek(EncodeDate(1998,1,MyDate[ValueIndex]))=2 then
  97.   begin
  98.     LabelsFont.Color:=clRed;
  99.     Grid.Color:=clRed;
  100.     Grid.Style:=psSolid;
  101.   end
  102.   else
  103.   begin
  104.     LabelsFont.Color:=clBlack;
  105.     Grid.Color:=clGray;
  106.     Grid.Style:=psDot;
  107.   end;
  108. end;
  109.  
  110. procedure TCandleWeekForm.Series1BeforeDrawValues(Sender: TObject);
  111. var x1,x2:Integer;
  112. begin
  113.  { this Series event is used to paint the "Week 1" and "Week 2" strings }
  114.   With Chart1,Canvas do
  115.   begin
  116.     Font.Color:=clYellow;
  117.     Font.Size:=14;
  118.     TextAlign:=Ta_Center;
  119.     x1:=BottomAxis.CalcPosValue(0);
  120.     x2:=BottomAxis.CalcPosValue(5);
  121.     TextOut((x1+x2) div 2,ChartRect.Bottom-30,'Week 1');
  122.     x1:=BottomAxis.CalcPosValue(6);
  123.     x2:=BottomAxis.CalcPosValue(10);
  124.     TextOut((x1+x2) div 2,ChartRect.Bottom-30,'Week 2');
  125.     TextAlign:=Ta_Left;
  126.   end;
  127. end;
  128.  
  129. procedure TCandleWeekForm.RadioGroup1Click(Sender: TObject);
  130. var s:String;
  131.     TheDate:TDateTime;
  132.     t:Integer;
  133. begin
  134.   for t:=0 to Series1.Count-1 do
  135.   begin
  136.     TheDate:=EncodeDate(1998,1,MyDate[t]);
  137.     Case RadioGroup1.ItemIndex of
  138.       0: s:=ShortDayNames[DayOfWeek(TheDate)];
  139.       1: s:=IntToStr(DayOfWeek(TheDate));
  140.       2: s:=IntToStr(MyDate[t]);
  141.       3: s:=FormatDateTime('dd-mm-yy',TheDate);
  142.       4: s:=FormatDateTime('mmm-dd',TheDate);
  143.     end;
  144.     Series1.XLabel[t]:=s;
  145.   end;
  146.  
  147.   Memo1.Visible:=RadioGroup1.ItemIndex=1;
  148. end;
  149.  
  150. procedure TCandleWeekForm.Button1Click(Sender: TObject);
  151. begin
  152.   Close;
  153. end;
  154.  
  155. end.
  156.