File: main.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (225 lines) | stat: -rw-r--r-- 6,250 bytes parent folder | download | duplicates (3)
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
unit Main;

{$mode objfpc}{$H+}

interface

uses
  Classes, StdCtrls, ComCtrls, ExtCtrls, Spin, Forms,
  TAGraph, TASeries, TACustomSource, TASources,  TAMultiSeries, TAChartUtils;

type
  TDataForSorting = record
    Year: Integer;
    CityName: String;
    Visitors: Integer;
    Color: TChartColor;
  end;

  { TForm1 }

  TForm1 = class(TForm)
    btnSort: TButton;
    BubbleSeries: TBubbleSeries;
    cbAccDirDerivative: TComboBox;
    ccsDerivative: TCalculatedChartSource;
    cbCumulative: TCheckBox;
    ccsAvg: TCalculatedChartSource;
    ccsSum: TCalculatedChartSource;
    Chart1: TChart;
    Chart1BarSeries1: TBarSeries;
    Chart1LineSeries1: TLineSeries;
    BubbleChart: TChart;
    chDerivativeLineOrig: TLineSeries;
    chDerivativeLineDeriv: TLineSeries;
    Chart1LineSeries4: TLineSeries;
    Chart1LineSeries5: TLineSeries;
    Chart2: TChart;
    Chart2AreaSeries1: TAreaSeries;
    Chart2LineSeries1: TLineSeries;
    chDerivative: TChart;
    chCalc: TChart;
    chCalcLineSeries1: TLineSeries;
    chCalcLineSeriesAvg: TLineSeries;
    chCalcLineSeriesSum: TLineSeries;
    cbAccDirStatistics: TComboBox;
    cbSmooth: TCheckBox;
    Label1: TLabel;
    lblSortInfo: TLabel;
    seAccumulationRange: TSpinEdit;
    lblAccumulationRange: TLabel;
    ListChartSource1: TListChartSource;
    lcsDerivative: TListChartSource;
    PageControl1: TPageControl;
    Panel1: TPanel;
    Panel2: TPanel;
    rgDataShape: TRadioGroup;
    RandomChartSource1: TRandomChartSource;
    RandomChartSource2: TRandomChartSource;
    SortedChartSource: TSortedChartSource;
    Splitter1: TSplitter;
    tsSorted: TTabSheet;
    tsDerivative: TTabSheet;
    tsStatistics: TTabSheet;
    tsBasic: TTabSheet;
    UserDefinedChartSource: TUserDefinedChartSource;
    procedure btnSortClick(Sender: TObject);
    procedure BubbleChartAfterPaint({%H-}ASender: TChart);
    procedure cbAccDirDerivativeChange(Sender: TObject);
    procedure cbAccDirStatisticsChange(Sender: TObject);
    procedure cbCumulativeChange(Sender: TObject);
    procedure cbSmoothChange(Sender: TObject);
    procedure CreateDataForDerivative;
    procedure CreateDataForSorting;
    procedure seAccumulationRangeChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure rgDataShapeClick(Sender: TObject);
    procedure UserDefinedChartSourceGetChartDataItem(
      {%H-}ASource: TUserDefinedChartSource; AIndex: Integer;
      var AItem: TChartDataItem);
  private
    FDataForSorting: array of TDataForSorting;
  end;

var
  Form1: TForm1; 

implementation

{$R *.lfm}

uses
  Math;

{ TForm1 }

procedure TForm1.cbAccDirDerivativeChange(Sender: TObject);
begin
  ccsDerivative.AccumulationDirection :=
    TChartAccumulationDirection(cbAccDirDerivative.ItemIndex);
end;

procedure TForm1.btnSortClick(Sender: TObject);
begin
  SortedChartSource.Sorted := not SortedChartSource.Sorted;
end;

procedure TForm1.BubbleChartAfterPaint(ASender: TChart);
begin
  if SortedChartSource.IsSorted then
    lblSortInfo.Caption := 'Sorted by radius (descending)'
  else
    lblSortInfo.Caption := 'Not sorted';
end;

procedure TForm1.cbAccDirStatisticsChange(Sender: TObject);
begin
  ccsAvg.AccumulationDirection :=
    TChartAccumulationDirection(cbAccDirStatistics.ItemIndex);
  ccsSum.AccumulationDirection := ccsAvg.AccumulationDirection;
end;

procedure TForm1.cbCumulativeChange(Sender: TObject);
begin
  chCalcLineSeriesSum.Active := cbCumulative.Checked;
end;

procedure TForm1.cbSmoothChange(Sender: TObject);
begin
  if cbSmooth.Checked then
    ccsDerivative.AccumulationMethod := camSmoothDerivative
  else
    ccsDerivative.AccumulationMethod := camDerivative;
end;

procedure TForm1.CreateDataForDerivative;
const
  N = 100;
  MIN_X = -10;
  MAX_X = 10;
  EPS = 1e-6;
var
  i: Integer;
  x, y: Double;
begin
  lcsDerivative.Clear;
  if rgDataShape.ItemIndex = 6 then
    for i := 0 to 9 do
      lcsDerivative.Add(i - IfThen(i > 6, 1, 0), i)
  else
    for i := 0 to N - 1 do begin
      x := MIN_X + (MAX_X - MIN_X) / (N - 1) * i;
      if SameValue(x, 0.0, EPS) then x := 0;
      case rgDataShape.ItemIndex of
        0: y := x;
        1: y := Sin(x);
        2: if x = 0 then y := 1 else y := Sin(x) / x;
        3: y := Exp(-x / 3);
        4: y := Exp(-Sqr((x - 2.5) / 2.5));
        5: y := Exp(-Sqr((x - 2.5) / 2.5)) + 0.05 * (Random - 0.5);
      end;
      lcsDerivative.Add(x, y);
    end;
end;

procedure TForm1.CreateDataForSorting;

  procedure AddCity(AYear: Integer; const ACityName: string;
    ANumberOfVisitors: Integer; AColor: TChartColor);
  begin
    SetLength(FDataForSorting, Length(FDataForSorting) + 1);
    with FDataForSorting[High(FDataForSorting)] do begin
      Year := AYear;
      CityName := ACityName;
      Visitors := ANumberOfVisitors;
      Color := AColor;
    end;
  end;

begin
  SetLength(FDataForSorting, 0);
  AddCity(1967, 'City A', 155000, $00FFFF);  // yellow
  AddCity(1968, 'City B', 30000, $FF0000);   // blue
  AddCity(1969, 'City C', 450000, $FF00FF);  // fuchsia
  AddCity(1970, 'City D', 50000, $FFFF00);   // aqua
  AddCity(1971, 'City E', 80000, $00FF00);   // lime
  AddCity(1972, 'City F', 615000, $0000FF);  // red
  UserDefinedChartSource.PointsNumber := Length(FDataForSorting);

  { These settings are made in the Object Inspector for the SortedChartSource:
    - Origin = UserDefinedChartSource
    - SortBy = sbY
    - SortIndex = 1            // --> sort by radius
    - SortDir = sdDescending   // --> draw large bubbles first
  }
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
  CreateDataForDerivative;
  CreateDataForSorting;
end;

procedure TForm1.rgDataShapeClick(Sender: TObject);
begin
  CreateDataForDerivative;
end;

procedure TForm1.UserDefinedChartSourceGetChartDataItem(
  ASource: TUserDefinedChartSource; AIndex: Integer; var AItem: TChartDataItem);
begin
  AItem.X := FDataForSorting[AIndex].Year;
  AItem.Y := 0;
  AItem.YList[0] := Sqrt(FDataForSorting[AIndex].Visitors/100000 / PI);
  AItem.Text := FDataForSorting[AIndex].CityName;
  AItem.Color := FDataForSorting[AIndex].Color;
end;

procedure TForm1.seAccumulationRangeChange(Sender: TObject);
begin
  ccsDerivative.AccumulationRange := seAccumulationRange.Value;
end;

end.