TAChart: Fix TConstantLineDragTool.GrabRadius. Add demo.

git-svn-id: trunk@25751 -
This commit is contained in:
ask 2010-05-29 15:54:37 +00:00
parent 3cdf5411bb
commit d919a5610d
3 changed files with 50 additions and 21 deletions

View File

@ -1,15 +1,15 @@
object Form1: TForm1
Left = 292
Height = 466
Height = 475
Top = 152
Width = 554
Caption = 'Form1'
ClientHeight = 466
ClientHeight = 475
ClientWidth = 554
LCLVersion = '0.9.29'
object Chart1: TChart
Left = 0
Height = 388
Height = 371
Top = 0
Width = 554
AxisList = <
@ -50,11 +50,27 @@ object Form1: TForm1
Source = RandomChartSource1
UseReticule = True
end
object ChartLine1: TConstantLine
LineStyle = lsVertical
Pen.Color = clLime
Pen.Style = psDash
Pen.Width = 2
Position = -1.5
SeriesColor = clLime
end
object ChartLine2: TConstantLine
LineStyle = lsVertical
Pen.Color = clFuchsia
Pen.Style = psDash
Pen.Width = 2
Position = 1.5
SeriesColor = clFuchsia
end
end
object Panel1: TPanel
Left = 0
Height = 78
Top = 388
Top = 371
Width = 554
Align = alBottom
ClientHeight = 78
@ -114,6 +130,15 @@ object Form1: TForm1
TabOrder = 1
end
end
object Panel2: TPanel
Left = 0
Height = 26
Top = 449
Width = 554
Align = alBottom
Caption = 'Press Ctrl to activate reticlue, Alt-drag to move vertical lines'
TabOrder = 2
end
object ChartToolset1: TChartToolset
left = 124
top = 75
@ -147,6 +172,10 @@ object Form1: TForm1
object ChartToolset1ReticuleTool1: TReticuleTool
Shift = [ssCtrl]
end
object ChartToolset1ConstantLineDragTool1: TConstantLineDragTool
Shift = [ssAlt, ssLeft]
GrabRadius = 4
end
end
object RandomChartSource1: TRandomChartSource
PointsNumber = 10

View File

@ -15,8 +15,11 @@ type
TForm1 = class(TForm)
Chart1: TChart;
Chart1BarSeries1: TBarSeries;
ChartLine2: TConstantLine;
ChartLine1: TConstantLine;
Chart1FuncSeries1: TFuncSeries;
ChartToolset1: TChartToolset;
ChartToolset1ConstantLineDragTool1: TConstantLineDragTool;
ChartToolset1PanAny: TPanDragTool;
ChartToolset1PanHor: TPanDragTool;
ChartToolset1PanVert: TPanDragTool;
@ -25,6 +28,7 @@ type
ChartToolset1ZoomOut: TZoomClickTool;
ChartToolset1ZoomIn: TZoomClickTool;
Panel1: TPanel;
Panel2: TPanel;
rgZoom: TRadioGroup;
RandomChartSource1: TRandomChartSource;
rgPan: TRadioGroup;

View File

@ -163,7 +163,6 @@ type
private
FGrabRadius: Integer;
FLine: TConstantLine;
function PointToPos(ALine: TConstantLine; APoint: TPoint): Double;
public
constructor Create(AOwner: TComponent); override;
procedure MouseDown(APoint: TPoint); override;
@ -748,24 +747,25 @@ end;
procedure TConstantLineDragTool.MouseDown(APoint: TPoint);
var
i: Integer;
i, d, bestd: Integer;
c, bestc: TConstantLine;
d, bestd: Double;
begin
{$R-}{$Q-}
bestd := Infinity;
bestd := MaxInt;
bestc := nil;
for i := 0 to FChart.SeriesCount - 1 do begin
if not (FChart.Series[i] is TConstantLine) then continue;
c := FChart.Series[i] as TConstantLine;
d := Abs(c.Position - PointToPos(c, APoint));
if c.LineStyle = lsVertical then
d := FChart.XGraphToImage(c.Position) - APoint.X
else
d := FChart.YGraphToImage(c.Position) - APoint.Y;
d := Abs(d);
if d < bestd then begin
bestd := d;
bestc := c;
end;
end;
if (bestc = nil) or (bestd > GrabRadius) then exit;
{$ifdef OverflowChecking}{$Q+}{$endif}{$ifdef RangeChecking}{$R+}{$endif}
if bestc.LineStyle = lsVertical then
ActiveCursor := crSizeWE
else
@ -777,26 +777,22 @@ end;
procedure TConstantLineDragTool.MouseMove(APoint: TPoint);
begin
FLine.Position := PointToPos(FLine, APoint);
if FLine = nil then exit;
if FLine.LineStyle = lsVertical then
FLine.Position := FChart.XImageToGraph(APoint.X)
else
FLine.Position := FChart.YImageToGraph(APoint.Y);
FChart.Invalidate;
end;
procedure TConstantLineDragTool.MouseUp(APoint: TPoint);
begin
Unused(APoint);
FLine := nil;
Deactivate;
Handled;
end;
function TConstantLineDragTool.PointToPos(
ALine: TConstantLine; APoint: TPoint): Double;
begin
if ALine.LineStyle = lsVertical then
Result := FChart.XImageToGraph(APoint.X)
else
Result := FChart.YImageToGraph(APoint.Y);
end;
initialization
ToolsClassRegistry := TStringList.Create;