home *** CD-ROM | disk | FTP | other *** search
- unit Umovcur;
-
- interface
-
- { TeeChart-Pro. Example of Chart Cursors. }
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, TeEngine, Series, StdCtrls, ExtCtrls, TeeProcs, Chart;
-
- type
- TCursorStyle=(csHorizontal,csVertical);
-
- TMovableCursors = class(TForm)
- Chart1: TChart;
- RadioGroup1: TRadioGroup;
- CheckBox1: TCheckBox;
- Button1: TButton;
- CheckBoxSnap: TCheckBox;
- Series1: TLineSeries;
- Label1: TLabel;
- LabelPos: TLabel;
- Button2: TButton;
- procedure FormCreate(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure RadioGroup1Click(Sender: TObject);
- procedure CheckBox1Click(Sender: TObject);
- procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- procedure Chart1AfterDraw(Sender: TObject);
- procedure Chart1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure Chart1MouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure CheckBoxSnapClick(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- CursorStyle:TCursorStyle;
- CursorX,CursorY:Integer;
- DraggingCursor:Boolean;
- CursorColor:TColor;
- procedure SnapCursor;
- procedure RedrawCursor;
- end;
-
- var
- MovableCursors: TMovableCursors;
-
- implementation
-
- {$R *.DFM}
- Uses PenDlg;
-
- procedure TMovableCursors.FormCreate(Sender: TObject);
- begin
- { initialize private variables used in this demo }
- DraggingCursor:=False;
- CursorStyle:=csVertical;
- CursorX:=Chart1.Width div 2;
- CursorY:=Chart1.Height div 2;
- CursorColor:=clGreen;
-
- { fill sample values }
- Series1.FillSampleValues(20);
- end;
-
- procedure TMovableCursors.SnapCursor;
-
- { search the Series point with the nearest Y coordinate to the cursor }
- Procedure CalcNearestY;
- var t,tmpPoint:Integer;
- Difference, tmpDif:Double;
- begin
- Difference:=-1;
- tmpPoint:=-1;
- with Series1 do
- for t:=FirstValueIndex to LastValueIndex do
- begin
- tmpDif:=Abs(CursorY-CalcYPos(t));
- if (Difference=-1) or (tmpDif<Difference) then
- begin
- Difference:=tmpDif;
- tmpPoint:=t;
- end;
- end;
- if tmpPoint<>-1 then CursorY:=Series1.CalcYPos(tmpPoint);
- end;
-
- { search the Series point with the nearest X coordinate to the cursor }
- Procedure CalcNearestX;
- var t,tmpPoint:Integer;
- Difference, tmpDif:Double;
- begin
- Difference:=-1;
- tmpPoint:=-1;
- with Series1 do
- for t:=FirstValueIndex to LastValueIndex do
- begin
- tmpDif:=Abs(CursorX-CalcXPos(t));
- if (Difference=-1) or (tmpDif<Difference) then
- begin
- Difference:=tmpDif;
- tmpPoint:=t;
- end;
- end;
- if tmpPoint<>-1 then CursorX:=Series1.CalcXPos(tmpPoint);
- end;
-
- begin
- { snap to nearest Y or X depending the Cursor orientation }
- if CursorStyle=csHorizontal then CalcNearestY
- else CalcNearestX;
- end;
-
- procedure TMovableCursors.RedrawCursor;
- var tmpPos:Double;
- tmpColor:TColor;
- begin
- With Chart1,Canvas do { use the Chart Canvas property to paint }
- begin
- Pen.Style:=psSolid;
-
- { set pen color }
- tmpColor:=Chart1.BackColor;
- if tmpColor=clTeeColor then tmpColor:=clBtnFace;
- Pen.Color:=(CursorColor xor ColorToRGB(tmpColor));
-
- { set pen mode, useful to show /hide the cursor }
- Pen.Mode:=pmXor;
-
- if CursorStyle=csHorizontal then
- begin
- { draw an horizontal line }
- MoveTo( ChartRect.Left, CursorY );
- LineTo( ChartRect.Right, CursorY );
- tmpPos:=Series1.YScreenToValue(CursorY);
- end
- else
- begin
- { draw a vertical line }
- MoveTo( CursorX, ChartRect.Top );
- LineTo( CursorX, ChartRect.Bottom );
- tmpPos:=Series1.XScreenToValue(CursorX);
- end;
-
- { show in LabelPos the Cursor position in Axis scales }
- LabelPos.Caption:=FormatFloat('#,###.0',tmpPos);
- end;
- end;
-
- procedure TMovableCursors.Button1Click(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TMovableCursors.RadioGroup1Click(Sender: TObject);
- begin
- { Change cursor orientation }
-
- RedrawCursor; { hide the cursor }
- if RadioGroup1.ItemIndex=0 then CursorStyle:=csHorizontal
- else CursorStyle:=csVertical;
- Chart1.Repaint; { redraw again }
- end;
-
- procedure TMovableCursors.CheckBox1Click(Sender: TObject);
- begin
- { change the chart screen cursor }
- if CheckBox1.Checked then Chart1.Cursor:=crCross
- else Chart1.Cursor:=crDefault;
- Chart1.OriginalCursor:=Chart1.Cursor;
- end;
-
- procedure TMovableCursors.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- begin
- { user is moving the mouse... }
-
- { if mouse button is pressed and dragging then... }
- if DraggingCursor and PtInRect( Chart1.ChartRect,Point(X,Y) )then
- begin
- { hide the cursor }
- RedrawCursor;
- { set new cursor positions }
- CursorX:=X;
- CursorY:=Y;
- { snap to the nearest point if snap is selected }
- if CheckBoxSnap.Checked then SnapCursor;
- { draw again the cursor at the new position }
- RedrawCursor;
- end
- else
- begin
- { mouse button is not pressed, user is not dragging the cursor }
-
- { change the mouse cursor when passing over the Chart "Cursor" }
- if (CursorStyle=csHorizontal) and (Abs(Y-CursorY)<3) then
- Chart1.Cursor:=crVSplit
- else
- if (CursorStyle=csVertical) and (Abs(X-CursorX)<3) then
- Chart1.Cursor:=crHSplit
- else
- CheckBox1Click(Self); { reset default cursor }
-
- Chart1.OriginalCursor:=Chart1.Cursor;
- end;
- end;
-
- procedure TMovableCursors.Chart1AfterDraw(Sender: TObject);
- begin
- { When the Chart repaints (because zoom or scroll), the Cursor
- should be redrawn again. }
- RedrawCursor;
- end;
-
- procedure TMovableCursors.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- { user has clicked the cursor? }
- if (CursorStyle=csHorizontal) and (Abs(Y-CursorY)<3) then
- DraggingCursor:=True
- else
- if (CursorStyle=csVertical) and (Abs(X-CursorX)<3) then
- DraggingCursor:=True
- else
- DraggingCursor:=False;
-
- Chart1.CancelMouse:=DraggingCursor;
- end;
-
- procedure TMovableCursors.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- DraggingCursor:=False; { user has released the mouse button }
- end;
-
- procedure TMovableCursors.CheckBoxSnapClick(Sender: TObject);
- begin
- { snap Cursor position to an existing point coordinates }
- if CheckBoxSnap.Checked then
- begin
- RedrawCursor; { hide the cursor }
- SnapCursor;
- RedrawCursor; { show the cursor again }
- end;
- end;
-
- procedure TMovableCursors.Button2Click(Sender: TObject);
- begin
- RedrawCursor; { hide }
- CursorColor:=EditColor(Self,CursorColor);
- RedrawCursor; { show }
- end;
-
- end.
-