1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
{ This project demonstrates application of a TChartLiveView to a multiple-axis
chart ("paned chart"). }
unit main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
Spin, TAGraph, TATransformations, TASeries, TAChartLiveView;
type
{ TForm1 }
TForm1 = class(TForm)
AccelerationAutoScaleTransform: TAutoScaleAxisTransform;
Bevel1: TBevel;
btnReset: TButton;
btnStopResume: TButton;
Chart1: TChart;
ChartLiveView1: TChartLiveView;
cbLiveView: TCheckBox;
cbFrozenAxes: TCheckBox;
Label1: TLabel;
PositionSeries: TLineSeries;
seViewportSize: TFloatSpinEdit;
Timer: TTimer;
VelocitySeries: TLineSeries;
AccelerationSeries: TLineSeries;
PositionTransformations: TChartAxisTransformations;
PositionAutoScaleTransform: TAutoScaleAxisTransform;
VelocityTransformations: TChartAxisTransformations;
AccelerationTransformations: TChartAxisTransformations;
Panel1: TPanel;
VelocityScaleTransform: TAutoScaleAxisTransform;
procedure btnResetClick(Sender: TObject);
procedure btnStopResumeClick(Sender: TObject);
procedure cbFrozenAxesChange(Sender: TObject);
procedure cbLiveViewChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure seViewportSizeChange(Sender: TObject);
procedure TimerTimer(Sender: TObject);
private
FStartTime: TDateTime;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses
//DateUtils,
Math;
const
A0 = 10.0; // (initial) oscillation amplitude
t0 = 5.0; // oscillation period
td = 20.0; // damping time constant
TWO_PI = 2.0*pi;
{ TForm1 }
{ Simulates position, velocity and acceleration of an oscillating body. The
oscillation is damped, i.e. the excursions are decreasing with time. Parameters
describing this damped oscillation are A0, t0 and td given above. }
procedure TForm1.TimerTimer(Sender: TObject);
var
t: Double;
x, v, a: Double;
exp_factor, sin_factor, cos_factor: Double;
begin
if PositionSeries.Count = 0 then
FStartTime := Now();
t := (Now() - FStartTime) * SecsPerDay;
exp_factor := exp(-t/td);
SinCos(TWO_PI * t / t0, sin_factor, cos_factor);
// Position: an exponentially damped sinusoidal motion
x := A0 * sin_factor * exp_factor;
// velocity = dx/dt
v := 1.0 / (t0*td) * (A0 * exp_factor * (2.0*pi*td * cos_factor - t0 * sin_factor));
// acceleration = dv/dt = d2x/dt2
a := x / sqr(td) - sqr(TWO_PI/t0) * x - 2.0*TWO_PI*A0/(t0*td) * exp_factor * cos_factor;
// Add values to series
PositionSeries.AddXY(t, x);
VelocitySeries.AddXY(t, v);
AccelerationSeries.AddXY(t, a);
end;
{ Toggles between normal view and scrolling live view. In case of normal view
the entire extent of the chart is shown. }
procedure TForm1.cbLiveViewChange(Sender: TObject);
begin
ChartLiveView1.Active := cbLiveView.Checked;
if not ChartLiveView1.Active then
Chart1.ZoomFull;
end;
{ Toggles between automatic scaling of the axes and predefined axis limits
(see FormCreate for the predefined values). }
procedure TForm1.cbFrozenAxesChange(Sender: TObject);
var
i: Integer;
wasActive: Boolean;
begin
wasActive := ChartLiveView1.Active;
ChartLiveView1.Active := false; // LiveView must be off when changing axes
for i := 1 to 3 do
with Chart1.AxisList[i].Range do
begin
UseMax := cbFrozenAxes.Checked;
UseMin := cbFrozenAxes.Checked;
end;
ChartLiveView1.Active := wasActive; // Restore liveview state
end;
{ Resets and starts a new data generation run. }
procedure TForm1.btnResetClick(Sender: TObject);
begin
Timer.Enabled := false;
PositionSeries.Clear;
VelocitySeries.Clear;
AccelerationSeries.Clear;
Timer.Enabled := true;
btnStopResume.Caption := 'Stop';
end;
{ Stop or resume the data generation. When stopped a line break is added to the
series. }
procedure TForm1.btnStopResumeClick(Sender: TObject);
var
t: Double;
begin
Timer.Enabled := not Timer.Enabled;
if Timer.Enabled then
btnStopResume.Caption := 'Stop'
else begin
btnStopResume.Caption := 'Resume';
// Add a break to the curves
t := (Now() - FStartTime) * SecsPerDay;
PositionSeries.AddXY(t, NaN);
VelocitySeries.AddXY(t, NaN);
AccelerationSeries.AddXY(t, NaN);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{ The following settings could also be made in the object inspector. }
// LiveView must be off when setting up the axes.
with Chart1.AxisList[1].Range do
begin
UseMax := true;
UseMin := true;
Max := 10;
Min := -10;
end;
with Chart1.AxisList[2].Range do
begin
UseMax := true;
UseMin := true;
Max := 15;
Min := -15;
end;
with Chart1.AxisList[3].Range do
begin
UseMax := true;
UseMin := true;
Max := 15.0;
Min := -15.0;
end;
// Activate the live view
ChartLiveView1.Active := true;
// Start the "measurement"
Timer.Enabled := true;
end;
{ Set the liveview's ViewportSize according to the value in the SpinEdit }
procedure TForm1.seViewportSizeChange(Sender: TObject);
begin
ChartLiveView1.ViewportSize := seViewportSize.Value;
end;
end.
|