home *** CD-ROM | disk | FTP | other *** search
- {**********************************************}
- { TeeChart Trend Drawing Demo }
- { Copyright (c) 1995-1996 by David Berneda }
- {**********************************************}
- unit FinaTren;
-
- (*
- This sample project demonstrates how manual line drawing can
- be performed.
- We'll use the left mouse button to:
- 1) Zoom and scroll (as usually).
- 2) Create a new trend line and drag the end line position.
- 3) Click a Trend line to remove it from the chart.
-
- At Form close, we'll store the drawed Trend Series values.
- At Form show, we'll create and retrieve the Trend Series values.
- *)
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, Chart, Series, ExtCtrls, StdCtrls, OHLChart, CandleCh,
- Teengine, Buttons, TeeProcs;
-
- type
- TFinancialTrendForm = class(TForm)
- Chart1: TChart;
- Panel1: TPanel;
- Label1: TLabel;
- CandleSeries1: TCandleSeries;
- RadioGroup1: TRadioGroup;
- Label2: TLabel;
- Button1: TButton;
- VolumeSeries1: TVolumeSeries;
- Button2: TButton;
- CheckBox1: TCheckBox;
- BitBtn1: TBitBtn;
- CBSave: TCheckBox;
- procedure FormCreate(Sender: TObject);
- procedure Chart1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- procedure Chart1MouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure RadioGroup1Click(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure Chart1Zoom(Sender: TObject);
- procedure Chart1ClickSeries(Sender: TCustomChart; Series: TChartSeries;
- ValueIndex: Longint; Button: TMouseButton; Shift: TShiftState; X,
- Y: Integer);
- procedure Button2Click(Sender: TObject);
- procedure CheckBox1Click(Sender: TObject);
- procedure Chart1UndoZoom(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- tmpTrend:TLineSeries;
- DragSeries:TChartSeries;
- DraggingPoint:Boolean;
- DragIndex:Longint;
- Function ClickedSeries(x,y:Longint):TChartSeries;
- Procedure PaintTrendValues;
- procedure ResetScales;
- end;
-
- var
- FinancialTrendForm: TFinancialTrendForm;
-
- implementation
-
- {$R *.DFM}
- Uses EditChar,EditPro;
-
- procedure TFinancialTrendForm.FormCreate(Sender: TObject);
- var tmp,t,tmpOpen:Longint;
- begin
- { some variables to control Point dragging }
- DragSeries:=nil;
- DraggingPoint:=False;
- DragIndex:=-1;
-
- CandleSeries1.Clear;
- VolumeSeries1.Clear;
- tmpOpen:=1000+Random(100);
- for t:=1 to (Screen.Width div 8) do
- begin
- tmp:=round(100*Random-50);
- CandleSeries1.AddOHLC( Date+t,tmpOpen,tmpOpen+20,tmpOpen-20,tmpOpen+tmp);
- tmpOpen:=tmpOpen+tmp;
- VolumeSeries1.AddXY( Date+t ,Random(65),'',clTeeColor);
- end;
- Chart1.ApplyZOrder:=False;
- Button1Click(Self); { <-- reset axis scales }
- RadioGroup1Click(Self);
- end;
-
- Function TFinancialTrendForm.ClickedSeries(x,y:Longint):TChartSeries;
- var t:Longint;
- begin
- for t:=0 to Chart1.Seriescount-1 do
- with Chart1.Series[t] do
- if Clicked(x,y)<>-1 then
- begin
- result:=Chart1.Series[t];
- exit;
- end;
- result:=nil;
- end;
-
- procedure TFinancialTrendForm.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- if (RadioGroup1.ItemIndex<>1) or
- (Button<>mbLeft) then Exit; { <-- we want only left button }
-
- DragSeries:=ClickedSeries(x,y);
- if (DragSeries<>nil) and (DragSeries is TLineSeries) then
- begin
- DragIndex:=DragSeries.Clicked(x,y);
- if DragIndex<>-1 then
- begin
- DraggingPoint:=True;
- end
- else DragSeries:=nil;
- end
- else
- if DragSeries=nil then
- begin
- tmpTrend:=TLineSeries.Create(Self); { <-- create a new lineseries }
- With tmpTrend do
- Begin
- ParentChart:=Chart1; { <-- first thing to do ! }
- Pointer.Visible:=True;
- Cursor:=crTeeHand;
- Name:=TeeGetUniqueName(Self,'Trend');
-
- { start point }
- AddXY( XScreenToValue(x), YScreenToValue(y), '', clTeeColor);
-
- { end point (now its the same as start point) }
- AddXY( XScreenToValue(x), YScreenToValue(y), '', clTeeColor);
- end;
- end;
- PaintTrendValues; { <-- paint values on Label1 }
- RadioGroup1.Enabled:=False;
- end;
-
- procedure TFinancialTrendForm.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- begin
- if RadioGroup1.ItemIndex=1 then { Draw new trends }
- begin
- if DraggingPoint then
- begin
- if Assigned(DragSeries) then { <-- safe check ! }
- With DragSeries do
- Begin
- XValue[DragIndex]:=XScreenToValue(x); { <-- change the X value for end point }
- YValue[DragIndex]:=YScreenToValue(y); { <-- change to Y value for end point }
- end;
- end
- else
- if Assigned(tmpTrend) then { <-- safe check ! }
- With tmpTrend do
- Begin
- XValue[1]:=XScreenToValue(x); { <-- change the X value for end point }
- YValue[1]:=YScreenToValue(y); { <-- change to Y value for end point }
- end;
- PaintTrendValues; { <-- nice to have }
- end;
- end;
-
- procedure TFinancialTrendForm.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- if Assigned(tmpTrend) then
- Begin
- tmpTrend:=nil; { finito !!! }
- RadioGroup1.Enabled:=True;
- end;
- if DraggingPoint then
- begin
- DragSeries:=nil;
- DragIndex:=-1;
- DraggingPoint:=False;
- RadioGroup1.Enabled:=True;
- end;
- end;
-
- Procedure TFinancialTrendForm.PaintTrendValues;
- Begin
- if Assigned(tmpTrend) then { <-- safe check ! }
- With tmpTrend do
- Begin
- Label1.Caption:= XValueToText(XValue[0])+' '+YValueToText(YValue[0]);
- Label2.Caption:= XValueToText(XValue[1])+' '+YValueToText(YValue[1]);
- end;
- End;
-
- procedure TFinancialTrendForm.FormClose(Sender: TObject; var Action: TCloseAction);
- var t:Longint;
- MyTextFile: TextFile;
- begin
- if CBSave.Checked then
- begin
- { we can store the trend series values }
- AssignFile( MyTextFile, 'trends.txt' );
- Rewrite( MyTextFile );
- try
- With Chart1 do
- Begin
- for t:=0 to SeriesCount-1 do
- begin
- if Series[t] is TLineSeries then { <-- no candle storing }
- Begin
- Writeln( MyTextFile, Series[t].XValue[0] );
- Writeln( MyTextFile, Series[t].YValue[0] );
- Writeln( MyTextFile, Series[t].XValue[1] );
- Writeln( MyTextFile, Series[t].YValue[1] );
- end;
- end;
- end;
- finally
- CloseFile( MyTextFile );
- end;
- end;
- end;
-
- procedure TFinancialTrendForm.RadioGroup1Click(Sender: TObject);
- begin
- Chart1.AllowZoom:=not (RadioGroup1.ItemIndex=1);
- if RadioGroup1.ItemIndex=0 then
- CandleSeries1.Cursor:=crTeeHand
- else
- begin
- CandleSeries1.Cursor:=crDefault;
- Chart1.Cursor:=crCross;
- end;
- VolumeSeries1.Cursor:=CandleSeries1.Cursor;
- end;
-
- procedure TFinancialTrendForm.FormShow(Sender: TObject);
- Var MyTextFile: TextFile;
- x0, y0, x1, y1 : Double;
- ATrend : TLineSeries;
- begin
- if not FileExists( 'trends.txt' ) then Exit;
- AssignFile( MyTextFile, 'trends.txt' );
- Reset( MyTextFile );
- try
- While not eof( MyTextFile) do
- Begin
- Readln( MyTextFile, x0 );
- Readln( MyTextFile, y0 );
- Readln( MyTextFile, x1 );
- Readln( MyTextFile, y1 );
-
- if (x0<>0) and (y0<>0) then
- begin
- ATrend:=TLineSeries.Create(Self); { <-- create a new lineseries }
- With ATrend do
- Begin
- ParentChart:=Chart1; { <-- first thing to do ! }
- { start point }
- AddXY( x0, y0, '', clTeeColor);
- { end point }
- AddXY( x1, y1, '', clTeeColor);
- end;
- end;
- end;
- finally
- CloseFile( MyTextFile );
- end;
- end;
-
- procedure TFinancialTrendForm.Button1Click(Sender: TObject);
- begin
- Chart1.UndoZoom;
- end;
-
- procedure TFinancialTrendForm.ResetScales;
- begin
- With VolumeSeries1,GetVertAxis do
- Begin
- Automatic:=False;
- Minimum:=0;
- Maximum:=MaxYValue*10;
- end;
- end;
-
- procedure TFinancialTrendForm.Chart1Zoom(Sender: TObject);
- begin
- ResetScales;
- end;
-
- procedure TFinancialTrendForm.Chart1ClickSeries(Sender: TCustomChart;
- Series: TChartSeries; ValueIndex: Longint; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- { Trend Deleting }
- if RadioGroup1.ItemIndex=2 then
- begin
- if Series is TLineSeries then
- Series.Free; { <-- remove the clicked line series }
- end
- else
- { Series Editing }
- if RadioGroup1.ItemIndex=0 then EditSeries(Self,Series);
- end;
-
- procedure TFinancialTrendForm.Button2Click(Sender: TObject);
- begin
- EditChart(Self,Chart1);
- end;
-
- procedure TFinancialTrendForm.CheckBox1Click(Sender: TObject);
- begin
- Chart1.View3d:=not Chart1.View3D;
- end;
-
- procedure TFinancialTrendForm.Chart1UndoZoom(Sender: TObject);
- begin
- ResetScales;
- end;
-
- end.
-