home *** CD-ROM | disk | FTP | other *** search
- unit Uaxcent;
- {$I Teedefs.inc}
-
- interface
-
- { This example shows how to draw the Axis vertically centered.
- It also shows drag and drop using standard VCL "TDragObject"
- objects, and determining which part of a Chart is under
- the mouse cursor }
-
- uses
- Wintypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
- Dialogs, TeEngine, Series, ExtCtrls, TeeProcs, Chart, OHLChart, CandleCh,
- StdCtrls;
-
- type
- TAxisCenterForm = class(TForm)
- RadioGroup1: TRadioGroup;
- Series1: TLineSeries;
- Chart1: TChart;
- Button1: TButton;
- RadioGroup2: TRadioGroup;
- CheckBox1: TCheckBox;
- procedure FormCreate(Sender: TObject);
- procedure RadioGroup1Click(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure RadioGroup2Click(Sender: TObject);
- procedure CheckBox1Click(Sender: TObject);
- procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- procedure Chart1ClickAxis(Sender: TCustomChart; Axis: TChartAxis;
- Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
- {$IFNDEF D1}
- procedure Chart1StartDrag(Sender: TObject;
- var DragObject: TDragObject);
- {$ENDIF}
- procedure Chart1DragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- procedure Chart1DragDrop(Sender, Source: TObject; X, Y: Integer);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- AxisCenterForm: TAxisCenterForm;
-
- implementation
-
- {$R *.DFM}
- Uses TeCanvas;
-
- procedure TAxisCenterForm.FormCreate(Sender: TObject);
- var t:Integer;
- begin
- { adjust scales... }
- Chart1.Frame.Visible:=False;
- Chart1.LeftAxis.SetMinMax(-100,100);
-
- { add sample data... }
- Series1.Clear;
- for t:=1 to 100 do Series1.Add(Random(200)-100.0,'',clTeeColor);
-
- {$IFNDEF D1}
- Chart1.OnStartDrag:=Chart1StartDrag;
- {$ENDIF}
- end;
-
- { Select where to draw the HORIZONTAL axis }
- procedure TAxisCenterForm.RadioGroup1Click(Sender: TObject);
- begin
- Case RadioGroup1.ItemIndex of
- 0: Series1.HorizAxis:=aTopAxis; { top }
- 1: begin { center }
- Series1.HorizAxis :=aBottomAxis;
- Series1.GetHorizAxis.PositionPercent:=50;
- end;
- 2: begin { bottom }
- Series1.HorizAxis :=aBottomAxis;
- Series1.GetHorizAxis.PositionPercent:=0;
- end;
- end;
- end;
-
- procedure TAxisCenterForm.Button1Click(Sender: TObject);
- begin
- Close;
- end;
-
- { Select where to draw the VERTICAL axis }
- procedure TAxisCenterForm.RadioGroup2Click(Sender: TObject);
- begin
- Case RadioGroup2.ItemIndex of
- 0: Series1.VertAxis:=aLeftAxis; { left }
- 1: begin { center }
- Series1.VertAxis :=aRightAxis;
- Series1.GetVertAxis.PositionPercent:=50;
- end;
- 2: begin { right }
- Series1.VertAxis :=aRightAxis;
- Series1.GetVertAxis.PositionPercent:=0;
- end;
- end;
-
- end;
-
- procedure TAxisCenterForm.CheckBox1Click(Sender: TObject);
- begin
- TeeDrawAxisBeforeSeries:=CheckBox1.Checked;
- Chart1.Repaint;
- end;
-
- procedure TAxisCenterForm.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- var t : Integer;
- tmpChanged : Boolean;
- begin
- { Determine if the mouse cursor is over
- an axis... }
- tmpChanged:=False;
- for t:=0 to Chart1.AxesList.Count-1 do
- begin
- if TCustomChartAxis(Chart1.AxesList[t]).Clicked(X,Y) then
- begin
- { if mouse is over, then change the cursor ... }
- Chart1.Cursor:=crTeeHand;
- tmpChanged:=True;
- break;
- end;
- end;
- { if no axis are "clicked", then check the Legend... }
- if not tmpChanged then
- if Chart1.Legend.Clicked(X,Y)<>-1 then
- begin
- Chart1.Cursor:=crTeeHand;
- tmpChanged:=True;
- end;
- { if the Legend is not "clicked", then check the Title... }
- if not tmpChanged then
- if Chart1.Title.Clicked(X,Y) then
- begin
- Chart1.Cursor:=crTeeHand;
- tmpChanged:=True;
- end;
-
- { if nothing is under the mouse, set the cursor to
- default arrow... }
- if not tmpChanged then Chart1.Cursor:=crDefault;
- With Chart1 do OriginalCursor:=Cursor;
- end;
-
- procedure TAxisCenterForm.Chart1ClickAxis(Sender: TCustomChart; Axis: TChartAxis;
- Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
- begin
- { DraggedAxis:=Axis;}
- end;
-
- {$IFNDEF D1}
- procedure TAxisCenterForm.Chart1StartDrag(Sender: TObject;
- var DragObject: TDragObject);
- var Part:TChartClickedPart;
- begin
- Chart1.CalcClickedPart(Chart1.GetCursorPos,Part);
- DragObject:=TTeeDragObject.Create(Part);
- end;
- {$ENDIF}
-
- procedure TAxisCenterForm.Chart1DragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- begin
- {$IFNDEF D1}
- if Source is TTeeDragObject then
- begin
- Accept:=False;
- With TTeeDragObject(Source).Part do
- if Part=cpAxis then
- begin
- Accept:=PtInRect(Chart1.ChartRect,Point(X,Y));
- end
- else
- if Part=cpLegend then
- Accept:=PtInRect(Chart1.Legend.RectLegend,Point(X,Y))
- else
- if Part=cpTitle then
- Accept:=PtInRect(Chart1.Title.TitleRect,Point(X,Y))
- else
- if Part=cpFoot then
- Accept:=PtInRect(Chart1.Foot.TitleRect,Point(X,Y));
- if Accept then Chart1DragDrop(Self,Source,X,Y);
- end;
- {$ENDIF}
- end;
-
- procedure TAxisCenterForm.Chart1DragDrop(Sender, Source: TObject; X, Y: Integer);
- {$IFNDEF D1}
-
- var tmp,tmp2:Integer;
-
- Procedure DropAxis;
- begin
- With TTeeDragObject(Source).Part do
- if AAxis.Horizontal then
- begin
- With Chart1 do
- begin
- tmp:=(ChartRect.Bottom-ChartRect.Top);
- tmp2:=(Y-ChartRect.Top);
- AAxis.PositionPercent:=100-Round(100.0*tmp2/tmp);
- end;
- end
- else
- With Chart1 do
- begin
- tmp:=(ChartRect.Right-ChartRect.Left);
- tmp2:=(X-ChartRect.Left);
- AAxis.PositionPercent:=(Round(100.0*tmp2/tmp));
- end;
- end;
-
-
- Procedure DropLegend;
- Var tmpP,tmp2P:TLegendAlignment;
- begin
- tmp:=Abs(Chart1.Height-Y);
- tmpP:=laTop;
- if Abs(Chart1.Height-Y)<tmp then
- begin
- tmp:=Abs(Chart1.ChartRect.Bottom-Y);
- tmpP:=laBottom;
- end;
- tmp2:=Abs(X-Chart1.ChartRect.Left);
- tmp2P:=laLeft;
- if Abs(Chart1.ChartRect.Right-X)<tmp2 then
- begin
- tmp2:=Abs(Chart1.ChartRect.Right-X);
- tmp2P:=laRight;
- end;
-
- if tmp2<tmp then Chart1.Legend.Alignment:=tmpP
- else Chart1.Legend.Alignment:=tmp2P;
- if Chart1.Legend.Alignment=laRight then
- begin
- tmp:=(Chart1.Legend.RectLegend.Bottom-Chart1.Legend.RectLegend.Top);
- tmp2:=Abs(Y-Chart1.Legend.RectLegend.Top);
- end
- else
- begin
- tmp:=(Chart1.Legend.RectLegend.Right-Chart1.Legend.RectLegend.Left);
- tmp2:=Abs(X-Chart1.Legend.RectLegend.Left);
- end;
- { if tmp2>0 then
- Chart1.Legend.TopPos:=Round(100.0*tmp2/tmp)
- else
- Chart1.Legend.TopPos:=Round(100.0*tmp2/tmp)}
- end;
-
- {$ENDIF}
- begin
- {$IFNDEF D1}
- if Source is TTeeDragObject then
- With TTeeDragObject(Source).Part do
- if Part=cpAxis then DropAxis else
- if Part=cpLegend then DropLegend
- {$ENDIF}
- end;
-
- end.
-