File: main.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (300 lines) | stat: -rw-r--r-- 8,251 bytes parent folder | download | duplicates (7)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
unit Main;

{$mode objfpc}{$H+}

interface

uses
  ComCtrls, ExtCtrls, Forms, Spin, StdCtrls, Classes,
  TAAxisSource, TAFuncSeries, TAGraph, TASeries, TASources, TAStyles, TATools,
  TATransformations;

type

  { TForm1 }

  TForm1 = class(TForm)
    AxLabelSource: TListChartSource;
    catCumulNormDistrCumulNormDistrAxisTransform1: TCumulNormDistrAxisTransform;
    catCumulNormDistrLinearAxisTransform1: TLinearAxisTransform;
    catIndependent1Zoom: TLinearAxisTransform;
    catIndependent2Zoom: TLinearAxisTransform;
    catIndependent2: TChartAxisTransformations;
    catTAutoAutoScaleAxisTransform1: TAutoScaleAxisTransform;
    catTAutoScaleAxisTransform1: TAutoScaleAxisTransform;
    catTAuto: TChartAxisTransformations;
    cbAuto: TCheckBox;
    catUser: TChartAxisTransformations;
    catUserUserDefinedAxisTransform1: TUserDefinedAxisTransform;
    catIndependent1: TChartAxisTransformations;
    cbPercent: TCheckBox;
    cbUseAxisTransform: TCheckBox;
    ChartCumulNormDistr: TChart;
    catCumulNormDistr: TChartAxisTransformations;
    ChartIndependent: TChart;
    ChartIndependentLineSeries1: TLineSeries;
    ChartIndependentLineSeries2: TLineSeries;
    ChartCumulNormDistrLineSeries1: TLineSeries;
    ChartToolset1: TChartToolset;
    ChartToolset1DataPointDragTool1: TDataPointDragTool;
    ChartUser: TChart;
    ChartUserConstantLine1: TConstantLine;
    ChartUserLineSeries1: TLineSeries;
    csStripes: TChartStyles;
    ChartLog: TChart;
    cfsLog: TFuncSeries;
    cbLog: TCheckBox;
    ChartTWinterBar: TBarSeries;
    clsLogPoints: TLineSeries;
    ChartT: TChart;
    catLog: TChartAxisTransformations;
    ChartAxisTransformations1LinearAxisTransform2: TLinearAxisTransform;
    ChartAxisTransformations1LogarithmAxisTransform1: TLogarithmAxisTransform;
    catT: TChartAxisTransformations;
    catTFahrToCel: TLinearAxisTransform;
    ChartTSummer: TLineSeries;
    ChartTWinterLine: TLineSeries;
    edDataCount: TSpinEdit;
    fseIndependent1: TFloatSpinEdit;
    fseIndependent2: TFloatSpinEdit;
    lblTolerance: TLabel;
    lblDataCount: TLabel;
    lblIndependentScale1: TLabel;
    lblIndependentScale2: TLabel;
    PageControl1: TPageControl;
    pnCumulNormDistr: TPanel;
    pnlIndependentControls: TPanel;
    pnlLogControls: TPanel;
    pnlAutoControls: TPanel;
    rgSyncAxisMarks: TRadioGroup;
    rcsUser: TRandomChartSource;
    rcsTSummer: TRandomChartSource;
    rcsTWinter: TRandomChartSource;
    rgRandDistr: TRadioGroup;
    seTolerance: TSpinEdit;
    tsLinear: TTabSheet;
    tsCumulNormDistr: TTabSheet;
    tsIndependent: TTabSheet;
    tsUser: TTabSheet;
    tsLog: TTabSheet;
    procedure cbAutoChange(Sender: TObject);
    procedure cbLogChange(Sender: TObject);
    procedure catUserUserDefinedAxisTransform1AxisToGraph(
      AX: Double; out AT: Double);
    procedure cbPercentChange(Sender: TObject);
    procedure cbUseAxisTransformChange(Sender: TObject);
    procedure ChartLogFuncSeries1Calculate(const AX: Double; out AY: Double);
    procedure edDataCountChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure fseIndependent1Change(Sender: TObject);
    procedure fseIndependent2Change(Sender: TObject);
    procedure rgRandDistrClick(Sender: TObject);
    procedure rgSyncAxisMarksClick(Sender: TObject);
    procedure seToleranceChange(Sender: TObject);
  private
    FAxisSource: TCustomAxisChartSource;
    procedure FillIndependentSource;
    procedure FillCumulNormDistrSource;
  end;

var
  Form1: TForm1; 

implementation

uses
  Math, StrUtils, SysUtils, TAChartAxis, TAChartAxisUtils, TAChartUtils;

{$R *.lfm}

var
  GaussDevAvail: Boolean = false;
  GaussDev: Double = 0.0;

// Create a random number with normal distribution, mean value 0, standard
// deviation 1. See Numerical Recipes.
function RndNormal: Double;
var
  fac, r, v1, v2: Double;
begin
  if GaussDevAvail then
    Result := GaussDev
  else begin
    repeat
      v1 := 2.0 * Random - 1.0;
      v2 := 2.0 * Random - 1.0;
      r := Sqr(v1) + Sqr(v2);
    until (r > 0.0) and (r < 1.0);
    fac := Sqrt(-2.0 * Ln(r) / r);
    GaussDev := v1 * fac;
    Result := v2 * fac;
  end;
  GaussDevAvail := not GaussDevAvail;
end;

function MyFunc(AX: Double): Double;
begin
  Result := Power(10, AX) + 3;
end;

{ TForm1 }

procedure TForm1.cbAutoChange(Sender: TObject);
begin
  catTAutoAutoScaleAxisTransform1.Enabled := cbAuto.Checked;
  catTAutoScaleAxisTransform1.Enabled := cbAuto.Checked;
end;

procedure TForm1.cbLogChange(Sender: TObject);
begin
  ChartAxisTransformations1LogarithmAxisTransform1.Enabled := cbLog.Checked;
end;

procedure TForm1.cbPercentChange(Sender: TObject);
begin
  catCumulNormDistrLinearAxisTransform1.Enabled := cbPercent.Checked;
  FillCumulNormDistrSource;
  ChartCumulNormDistr.LeftAxis.Title.Caption :=
    'Cumulative probability' + IfThen(cbPercent.Checked, ' (%)', '');
end;

procedure TForm1.cbUseAxisTransformChange(Sender: TObject);
begin
  catCumulNormDistrCumulNormDistrAxisTransform1.Enabled :=
    cbUseAxisTransform.Checked;
end;

procedure TForm1.catUserUserDefinedAxisTransform1AxisToGraph(
  AX: Double; out AT: Double);
const
  R1 = 8.0;
  C = 2.5;
  R2 = R1 / C;
var
  zx: Double;
begin
  zx := ChartUserConstantLine1.Position;
  if Abs(AX - zx) > R1 then AT := AX
  else if AX < zx - R2 then AT := zx - R1
  else if AX > zx + R2 then AT := zx + R1
  else AT := (AX - zx + R2) * C + zx - R1;
end;

procedure TForm1.ChartLogFuncSeries1Calculate(const AX: Double; out AY: Double);
begin
  AY := MyFunc(AX);
end;

procedure TForm1.edDataCountChange(Sender: TObject);
begin
  FillCumulNormDistrSource;
end;

procedure TForm1.FillCumulNormDistrSource;
var
  i: Integer;
  y: Double;
  s: TListChartSource;
begin
  RandSeed := 976896;
  s := ChartCumulNormDistrLineSeries1.ListSource;
  s.BeginUpdate;
  try
    s.Clear;
    // Add random test data as x values --> random values will
    // get sorted in ascending direction automatically.
    s.Sorted := false;
    for i := 1 to edDataCount.Value do
      case rgRandDistr.ItemIndex of
        0: s.Add(Random, 0);
        1: s.Add(RndNormal, 0);
      end;
    s.Sorted := true;
    // Calculate cumulative probability from index in sorted list.
    for i := 0 to s.Count - 1 do begin
      y := (i + 1) / (s.Count + 1); // Add 1 since y=0 and y=1 are not valid.
      s.Item[i]^.Y := IfThen(CbPercent.Checked, y * 100, y);
    end;
  finally
    s.EndUpdate;
  end;
end;

procedure TForm1.FillIndependentSource;

var
  i: Integer;
  v1, v2: Double;
begin
  RandSeed := 923875;
  v1 := 0;
  v2 := 0;
  for i := 1 to 100 do begin
    v1 += Random - 0.48;
    v2 += Random - 0.52;
    ChartIndependentLineSeries1.AddXY(i, v1);
    ChartIndependentLineSeries2.AddXY(i, v2);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  x: Double;
begin
  for i := 0 to 50 do begin
    with cfsLog.Extent do
      x := i / 50 * (XMax - XMin) + XMin;
    clsLogPoints.AddXY(x + Random - 0.5, MyFunc(x) + Random - 0.5);
  end;
  FillIndependentSource;
  FillCumulNormDistrSource;
  seTolerance.Value := ChartLog.LeftAxis.Intervals.Tolerance;
  FAxisSource := TCustomAxisChartSource.Create(Self);
end;

procedure TForm1.fseIndependent1Change(Sender: TObject);
begin
  catIndependent1Zoom.Scale := fseIndependent1.Value;
end;

procedure TForm1.fseIndependent2Change(Sender: TObject);
begin
  catIndependent2Zoom.Scale := fseIndependent2.Value;
end;

procedure TForm1.rgRandDistrClick(Sender: TObject);
begin
  FillCumulNormDistrSource;
end;

procedure TForm1.rgSyncAxisMarksClick(Sender: TObject);
var
  la, ra: TChartAxis;
begin
  la := ChartIndependent.LeftAxis;
  ra := ChartIndependent.AxisList.GetAxisByAlign(calRight);
  la.Marks.Source := nil;
  ra.Marks.Source := nil;
  case rgSyncAxisMarks.ItemIndex of
    0: begin
      FAxisSource.AxisFrom := ra;
      FAxisSource.AxisTo := la;
      la.Marks.Source := FAxisSource;
    end;
    2: begin
      FAxisSource.AxisFrom := la;
      FAxisSource.AxisTo := ra;
      ra.Marks.Source := FAxisSource;
    end;
  end;
  la.Grid.Visible := rgSyncAxisMarks.ItemIndex <> 1;
end;

procedure TForm1.seToleranceChange(Sender: TObject);
begin
  ChartLog.LeftAxis.Intervals.Tolerance := seTolerance.Value;
end;

end.