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

  1. {**********************************************}
  2. {   TeeChart Trend Drawing Demo                }
  3. {   Copyright (c) 1995-1996 by David Berneda   }
  4. {**********************************************}
  5. unit FinaTren;
  6.  
  7. (*
  8.   This sample project demonstrates how manual line drawing can
  9.   be performed.
  10.   We'll use the left mouse button to:
  11.     1) Zoom and scroll (as usually).
  12.     2) Create a new trend line and drag the end line position.
  13.     3) Click a Trend line to remove it from the chart.
  14.  
  15.   At Form close, we'll store the drawed Trend Series values.
  16.   At Form show, we'll create and retrieve the Trend Series values.
  17. *)
  18.  
  19. interface
  20.  
  21. uses
  22.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  23.   Forms, Dialogs, Chart, Series, ExtCtrls, StdCtrls, OHLChart, CandleCh,
  24.   Teengine, Buttons, TeeProcs;
  25.  
  26. type
  27.   TFinancialTrendForm = class(TForm)
  28.     Chart1: TChart;
  29.     Panel1: TPanel;
  30.     Label1: TLabel;
  31.     CandleSeries1: TCandleSeries;
  32.     RadioGroup1: TRadioGroup;
  33.     Label2: TLabel;
  34.     Button1: TButton;
  35.     VolumeSeries1: TVolumeSeries;
  36.     Button2: TButton;
  37.     CheckBox1: TCheckBox;
  38.     BitBtn1: TBitBtn;
  39.     CBSave: TCheckBox;
  40.     procedure FormCreate(Sender: TObject);
  41.     procedure Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  42.       Shift: TShiftState; X, Y: Integer);
  43.     procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  44.       Y: Integer);
  45.     procedure Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  46.       Shift: TShiftState; X, Y: Integer);
  47.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  48.     procedure RadioGroup1Click(Sender: TObject);
  49.     procedure FormShow(Sender: TObject);
  50.     procedure Button1Click(Sender: TObject);
  51.     procedure Chart1Zoom(Sender: TObject);
  52.     procedure Chart1ClickSeries(Sender: TCustomChart; Series: TChartSeries;
  53.       ValueIndex: Longint; Button: TMouseButton; Shift: TShiftState; X,
  54.       Y: Integer);
  55.     procedure Button2Click(Sender: TObject);
  56.     procedure CheckBox1Click(Sender: TObject);
  57.     procedure Chart1UndoZoom(Sender: TObject);
  58.   private
  59.     { Private declarations }
  60.   public
  61.     { Public declarations }
  62.     tmpTrend:TLineSeries;
  63.     DragSeries:TChartSeries;
  64.     DraggingPoint:Boolean;
  65.     DragIndex:Longint;
  66.     Function ClickedSeries(x,y:Longint):TChartSeries;
  67.     Procedure PaintTrendValues;
  68.     procedure ResetScales;
  69.   end;
  70.  
  71. var
  72.   FinancialTrendForm: TFinancialTrendForm;
  73.  
  74. implementation
  75.  
  76. {$R *.DFM}
  77. Uses EditChar,EditPro;
  78.  
  79. procedure TFinancialTrendForm.FormCreate(Sender: TObject);
  80. var tmp,t,tmpOpen:Longint;
  81. begin
  82.   { some variables to control Point dragging }
  83.   DragSeries:=nil;
  84.   DraggingPoint:=False;
  85.   DragIndex:=-1;
  86.  
  87.   CandleSeries1.Clear;
  88.   VolumeSeries1.Clear;
  89.   tmpOpen:=1000+Random(100);
  90.   for t:=1 to (Screen.Width div 8) do
  91.   begin
  92.     tmp:=round(100*Random-50);
  93.     CandleSeries1.AddOHLC( Date+t,tmpOpen,tmpOpen+20,tmpOpen-20,tmpOpen+tmp);
  94.     tmpOpen:=tmpOpen+tmp;
  95.     VolumeSeries1.AddXY( Date+t  ,Random(65),'',clTeeColor);
  96.   end;
  97.   Chart1.ApplyZOrder:=False;
  98.   Button1Click(Self); { <-- reset axis scales }
  99.   RadioGroup1Click(Self);
  100. end;
  101.  
  102. Function TFinancialTrendForm.ClickedSeries(x,y:Longint):TChartSeries;
  103. var t:Longint;
  104. begin
  105.   for t:=0 to Chart1.Seriescount-1 do
  106.      with Chart1.Series[t] do
  107.      if Clicked(x,y)<>-1 then
  108.      begin
  109.        result:=Chart1.Series[t];
  110.        exit;
  111.      end;
  112.   result:=nil;
  113. end;
  114.  
  115. procedure TFinancialTrendForm.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  116.   Shift: TShiftState; X, Y: Integer);
  117. begin
  118.   if (RadioGroup1.ItemIndex<>1) or
  119.      (Button<>mbLeft) then Exit;  { <-- we want only left button }
  120.  
  121.   DragSeries:=ClickedSeries(x,y);
  122.   if (DragSeries<>nil) and (DragSeries is TLineSeries) then
  123.   begin
  124.     DragIndex:=DragSeries.Clicked(x,y);
  125.     if DragIndex<>-1 then
  126.     begin
  127.       DraggingPoint:=True;
  128.     end
  129.     else DragSeries:=nil;
  130.   end
  131.   else
  132.   if DragSeries=nil then
  133.   begin
  134.     tmpTrend:=TLineSeries.Create(Self);  { <-- create a new lineseries }
  135.     With tmpTrend do
  136.     Begin
  137.       ParentChart:=Chart1;  { <-- first thing to do ! }
  138.       Pointer.Visible:=True;
  139.       Cursor:=crTeeHand;
  140.       Name:=TeeGetUniqueName(Self,'Trend');
  141.  
  142.       { start point }
  143.       AddXY( XScreenToValue(x), YScreenToValue(y), '', clTeeColor);
  144.  
  145.       { end point (now its the same as start point) }
  146.       AddXY( XScreenToValue(x), YScreenToValue(y), '', clTeeColor);
  147.     end;
  148.   end;
  149.   PaintTrendValues;  { <-- paint values on Label1 }
  150.   RadioGroup1.Enabled:=False;
  151. end;
  152.  
  153. procedure TFinancialTrendForm.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  154.   Y: Integer);
  155. begin
  156.   if RadioGroup1.ItemIndex=1 then { Draw new trends }
  157.   begin
  158.     if DraggingPoint then
  159.     begin
  160.       if Assigned(DragSeries) then { <-- safe check ! }
  161.       With DragSeries do
  162.       Begin
  163.         XValue[DragIndex]:=XScreenToValue(x);  { <-- change the X value for end point }
  164.         YValue[DragIndex]:=YScreenToValue(y);  { <-- change to Y value for end point }
  165.       end;
  166.     end
  167.     else
  168.     if Assigned(tmpTrend) then { <-- safe check ! }
  169.     With tmpTrend do
  170.     Begin
  171.       XValue[1]:=XScreenToValue(x);  { <-- change the X value for end point }
  172.       YValue[1]:=YScreenToValue(y);  { <-- change to Y value for end point }
  173.     end;
  174.     PaintTrendValues;              { <-- nice to have }
  175.   end;
  176. end;
  177.  
  178. procedure TFinancialTrendForm.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  179.   Shift: TShiftState; X, Y: Integer);
  180. begin
  181.   if Assigned(tmpTrend) then
  182.   Begin
  183.     tmpTrend:=nil; { finito !!! }
  184.     RadioGroup1.Enabled:=True;
  185.   end;
  186.   if DraggingPoint then
  187.   begin
  188.     DragSeries:=nil;
  189.     DragIndex:=-1;
  190.     DraggingPoint:=False;
  191.     RadioGroup1.Enabled:=True;
  192.   end;
  193. end;
  194.  
  195. Procedure TFinancialTrendForm.PaintTrendValues;
  196. Begin
  197.   if Assigned(tmpTrend) then { <-- safe check ! }
  198.   With tmpTrend do
  199.   Begin
  200.     Label1.Caption:= XValueToText(XValue[0])+' '+YValueToText(YValue[0]);
  201.     Label2.Caption:= XValueToText(XValue[1])+' '+YValueToText(YValue[1]);
  202.   end;
  203. End;
  204.  
  205. procedure TFinancialTrendForm.FormClose(Sender: TObject; var Action: TCloseAction);
  206. var t:Longint;
  207.     MyTextFile: TextFile;
  208. begin
  209.   if CBSave.Checked then
  210.   begin
  211.     { we can store the trend series values }
  212.     AssignFile( MyTextFile, 'trends.txt' );
  213.     Rewrite( MyTextFile );
  214.     try
  215.       With Chart1 do
  216.       Begin
  217.         for t:=0 to SeriesCount-1 do
  218.         begin
  219.           if Series[t] is TLineSeries then  { <-- no candle storing }
  220.           Begin
  221.             Writeln( MyTextFile, Series[t].XValue[0] );
  222.             Writeln( MyTextFile, Series[t].YValue[0] );
  223.             Writeln( MyTextFile, Series[t].XValue[1] );
  224.             Writeln( MyTextFile, Series[t].YValue[1] );
  225.           end;
  226.         end;
  227.       end;
  228.     finally
  229.       CloseFile( MyTextFile );
  230.     end;
  231.   end;
  232. end;
  233.  
  234. procedure TFinancialTrendForm.RadioGroup1Click(Sender: TObject);
  235. begin
  236.   Chart1.AllowZoom:=not (RadioGroup1.ItemIndex=1);
  237.   if RadioGroup1.ItemIndex=0 then
  238.      CandleSeries1.Cursor:=crTeeHand
  239.   else
  240.   begin
  241.      CandleSeries1.Cursor:=crDefault;
  242.      Chart1.Cursor:=crCross;
  243.   end;
  244.   VolumeSeries1.Cursor:=CandleSeries1.Cursor;
  245. end;
  246.  
  247. procedure TFinancialTrendForm.FormShow(Sender: TObject);
  248. Var MyTextFile: TextFile;
  249.     x0, y0, x1, y1 : Double;
  250.     ATrend  : TLineSeries;
  251. begin
  252.   if not FileExists( 'trends.txt' ) then Exit;
  253.   AssignFile( MyTextFile, 'trends.txt' );
  254.   Reset( MyTextFile );
  255.   try
  256.     While not eof( MyTextFile) do
  257.     Begin
  258.        Readln( MyTextFile, x0 );
  259.        Readln( MyTextFile, y0 );
  260.        Readln( MyTextFile, x1 );
  261.        Readln( MyTextFile, y1 );
  262.  
  263.        if (x0<>0) and (y0<>0) then
  264.        begin
  265.          ATrend:=TLineSeries.Create(Self);  { <-- create a new lineseries }
  266.          With ATrend do
  267.          Begin
  268.            ParentChart:=Chart1;  { <-- first thing to do ! }
  269.            { start point }
  270.            AddXY( x0, y0, '', clTeeColor);
  271.            { end point }
  272.            AddXY( x1, y1, '', clTeeColor);
  273.          end;
  274.        end;
  275.     end;
  276.   finally
  277.     CloseFile( MyTextFile );
  278.   end;
  279. end;
  280.  
  281. procedure TFinancialTrendForm.Button1Click(Sender: TObject);
  282. begin
  283.   Chart1.UndoZoom;
  284. end;
  285.  
  286. procedure TFinancialTrendForm.ResetScales;
  287. begin
  288.   With VolumeSeries1,GetVertAxis do
  289.   Begin
  290.     Automatic:=False;
  291.     Minimum:=0;
  292.     Maximum:=MaxYValue*10;
  293.   end;
  294. end;
  295.  
  296. procedure TFinancialTrendForm.Chart1Zoom(Sender: TObject);
  297. begin
  298.   ResetScales;
  299. end;
  300.  
  301. procedure TFinancialTrendForm.Chart1ClickSeries(Sender: TCustomChart;
  302.   Series: TChartSeries; ValueIndex: Longint; Button: TMouseButton;
  303.   Shift: TShiftState; X, Y: Integer);
  304. begin
  305. { Trend Deleting }
  306.   if RadioGroup1.ItemIndex=2 then
  307.   begin
  308.      if Series is TLineSeries then
  309.         Series.Free;  { <-- remove the clicked line series }
  310.   end
  311.   else
  312.   { Series Editing }
  313.   if RadioGroup1.ItemIndex=0 then EditSeries(Self,Series);
  314. end;
  315.  
  316. procedure TFinancialTrendForm.Button2Click(Sender: TObject);
  317. begin
  318.   EditChart(Self,Chart1);
  319. end;
  320.  
  321. procedure TFinancialTrendForm.CheckBox1Click(Sender: TObject);
  322. begin
  323.   Chart1.View3d:=not Chart1.View3D;
  324. end;
  325.  
  326. procedure TFinancialTrendForm.Chart1UndoZoom(Sender: TObject);
  327. begin
  328.   ResetScales;
  329. end;
  330.  
  331. end.
  332.