unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, TAGraph,
TATools, TASeries, TADrawUtils, TACustomSeries, Types, TAChartAxis, TASources, TACustomSource,TAChartUtils;
type
TFloatPoint = Record
X: Double;
Y: Double;
end;
TFloatPointArray = array of TFloatPoint;
{ TForm1 }
TForm1 = class(TForm)
Chart1: TChart;
Chart1LineSeries1: TLineSeries;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
ctMain: TChartToolset;
ctMainDataPointDrag: TDataPointDragTool;
ctMainAxisClick: TAxisClickTool;
procedure ctMainAxisClickToolOnClick(ASender: TChartTool; Axis: TChartAxis; AHitInfo: TChartAxisHitTests);
procedure ctMainDataPointDragAfterMouseMove(ATool: TChartTool; APoint: TPoint);
procedure ctMainDataPointDragTool1DragStart(ASender: TDataPointDragTool; var AGraphPoint: TDoublePoint);
procedure FormCreate(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
function GenSin (Frequency: Extended; Amplitude: Extended; SamPerSec:single; Offset: Extended=0; Duration: integer= 1000;PhaseShift:single=0): TFloatPointArray;
var
i: integer;
SampleCount: integer;
dt: double;
debY: double;
begin
SampleCount:=trunc(SamPerSec*Duration/1000)+1;
SetLength(Result,SampleCount);
dt:=1/SamPerSec;
for i:= 0 to SampleCount-1 do
begin
Result[i].X:=i/SamPerSec ;
debY:=sin(i*2*pi*Frequency/SamPerSec+(PhaseShift*2*pi/360))*Amplitude+Offset;
Result[i].Y:=sin(i*2*pi*Frequency/SamPerSec+(PhaseShift*2*pi/360))*Amplitude+Offset;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
LSeriesArray: array of TLineSeries;
i: integer;
sin1: TFloatPointArray;
begin
ctMain:=TChartToolset.Create (Chart1);
ctMainAxisClick := TAxisClickTool.Create(ctMain);
ctMainAxisClick.Shift := [ssLeft];
ctMainAxisClick.Enabled := True;
ctMainAxisClick.OnClick := @ctMainAxisClickToolOnCLick;
ctMainDataPointDrag:=TDataPointDragTool.Create(ctMain);
ctMainDataPointDrag.Shift:=[ssLeft];
ctMainDataPointDrag.Enabled:=True;
ctMainDataPointDrag.OnDragStart := @ctMainDataPointDragTool1DragStart;
ctMainDataPointDrag.OnAfterMouseMove:=@ctMainDataPointDragAfterMouseMove;
Chart1.Toolset := ctMain;
ctMainAxisClick.Toolset:=ctMain;
ctMainDataPointDrag.Toolset:=ctMain;
sin1 := GenSin(50,5,1000,0,1000);
for i:=0 to Length(sin1) do
Chart1LineSeries1.AddXY(sin1[i].x,sin1[i].y);
SetLength (LSeriesArray ,10);
for i:=0 to 9 do
begin
LSeriesArray[i] := TLineSeries.Create(self);
Chart1.AddSeries(LSeriesArray[i]);
LSeriesArray[i].Pointer.Visible := true;
LSeriesArray[i].AddXY(i/10,i);
end;
end;
procedure TForm1.ctMainDataPointDragAfterMouseMove(ATool: TChartTool; APoint: TPoint);
begin
if ctMainDataPointDrag.Series = nil then Label3.Caption := 'nil' else Label3.Caption := IntToStr( ctMainDataPointDrag.Series.Index);
Label1.Caption := {ChartToolset1DataPointDragTool1.Series.Name + '; ' +} IntToStr(APoint.X) + '; ' + IntToStr(APoint.y) ;
end;
procedure TForm1.ctMainAxisClickToolOnCLick(ASender: TChartTool; Axis: TChartAxis; AHitInfo: TChartAxisHitTests);
begin
Label2.Caption := Axis.DisplayName;
end;
procedure TForm1.ctMainDataPointDragTool1DragStart(ASender: TDataPointDragTool; var AGraphPoint: TDoublePoint);
begin
if ctMainDataPointDrag.Series = nil then exit; //Checks if a dot is dragger, to prevent crashing
if ctMainDataPointDrag.Series = Chart1LineSeries1 then ASender.Handled;; //Prevent dragging the main curve
end;
end.