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 (217 lines) | stat: -rw-r--r-- 5,999 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
{ Demo project showing how chart text elements can be formatted individually
  by means of html code, e.g. sub-/superscript, dedicated colors, etc. 
  
  Shows also how to save the chart to wmf (Windows only) and svg files, and 
  to copy it to the clipboard as a bitmap.
  
  NOTE:
  The svg renderer may crash when a font is found which the FreeType routines
  cannot read. To prevent a crash of the application add the exception type 
  EFreeType to the "Language Exceptions" of the project. }

unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, TAGraph, TASeries, TASources, Forms, Controls,
  Graphics, Dialogs, ExtCtrls, StdCtrls, TAChartAxisUtils, TAFuncSeries,
  TATools, TADataTools;

type

  { TMainForm }

  TMainForm = class(TForm)
    BtnCopyToClipboard: TButton;
    BtnSaveWMF: TButton;
    BtnSaveSVG: TButton;
    Chart: TChart;
    CgHTML: TCheckGroup;
    CbRTL: TCheckBox;
    ChartTools: TChartToolset;
    CbRotateXLabels: TCheckBox;
    DistanceTool: TDataPointDistanceTool;
    FitSeries: TFitSeries;
    Label1: TLabel;
    ListChartSource: TListChartSource;
    DataSeries: TLineSeries;
    BottomPanel: TPanel;
    ListChartSource_Fit: TListChartSource;
    procedure BtnCopyToClipboardClick(Sender: TObject);
    procedure BtnSaveWMFClick(Sender: TObject);
    procedure BtnSaveSVGClick(Sender: TObject);
    procedure CbRotateXLabelsChange(Sender: TObject);
    procedure CgHTMLItemClick(Sender: TObject; Index: integer);
    procedure ChartAxisList1MarkToText(var AText: String; {%H-}AMark: Double);
    procedure CbRTLChange(Sender: TObject);
    procedure DistanceToolGetDistanceText(ASender: TDataPointDistanceTool;
      var AText: String);
    procedure FitSeriesFitComplete(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);

  private
    procedure CreateData;

  public

  end;

var
  MainForm: TMainForm;

implementation

{$R *.lfm}

uses
  TAChartUtils, {$IFDEF WINDOWS}TADrawerWMF,{$ENDIF} TADrawerSVG;

{ TMainForm }

procedure TMainForm.BtnCopyToClipboardClick(Sender: TObject);
begin
  Chart.CopyToClipboardBitmap;
end;

procedure TMainForm.BtnSaveWMFClick(Sender: TObject);
begin
  {$IFDEF WINDOWS}
  with Chart do
    Draw(TWindowsMetafileDrawer.Create('test.wmf'), Rect(0, 0, Width, Height));
  ShowMessage('Chart saved to file "test.wmf"');
  {$ENDIF}
end;

procedure TMainForm.CbRotateXLabelsChange(Sender: TObject);
begin
  if CbRotateXLabels.Checked then
    Chart.BottomAxis.Marks.LabelFont.Orientation := 450
  else
    Chart.BottomAxis.Marks.LabelFont.Orientation := 0;
end;

procedure TMainForm.BtnSaveSVGClick(Sender: TObject);
begin
  Chart.SaveToSVGFile('test.svg');
  ShowMessage('Chart saved to file "test.svg"');
end;

procedure TMainForm.CgHTMLItemClick(Sender: TObject; Index: integer);
var
  tf: TChartTextFormat;
begin
  if CgHTML.Checked[Index] then tf := tfNormal else tf := tfHTML;
  case Index of
    0: Chart.Title.TextFormat := tf;
    1: Chart.Foot.TextFormat := tf;
    2: Chart.Legend.TextFormat := tf;
    3: DataSeries.Marks.TextFormat := tf;
    4: Chart.BottomAxis.Marks.TextFormat := tf;
    5: Chart.BottomAxis.Title.TextFormat := tf;
    6: Chart.LeftAxis.Title.TextFormat := tf;
    7: DistanceTool.Marks.TextFormat := tf;
  end;
end;

procedure TMainForm.ChartAxisList1MarkToText(var AText: String; AMark: Double);
begin
  AText := AText + '°';
end;

procedure TMainForm.CbRTLChange(Sender: TObject);
begin
  if CbRTL.Checked then
    Chart.BiDiMode := bdRightToLeft else
    Chart.BiDiMode := bdLeftToRight;
end;

procedure TMainForm.CreateData;
const
  N = 20;
  MIN = 0;
  MAX = 90;
  OUTLIER_INDEX = 12;
var
  i: Integer;
  x, y: Double;
  s: String;
begin
  for i:=0 to N-1 do begin
    x := MIN + (MAX - MIN) * i / (N-1) + 5*(random - 0.5);
    if i = OUTLIER_INDEX then begin
      y := 631;
      s := 'Defective device!' + LineEnding + '(α = ' + FormatFloat('0.00', x) + '°)';
    end else
    begin
      y := x*x / 10 + (random - 0.5) * 100;
      s := '';
    end;
    ListChartSource.Add(x, y, s);
    if i <> OUTLIER_INDEX then
      ListChartSource_Fit.Add(x, y);
  end;
  DataSeries.Source := ListChartSource;
  FitSeries.Source := ListChartSource_Fit;
end;

procedure TMainForm.DistanceToolGetDistanceText(
  ASender: TDataPointDistanceTool; var AText: String);
begin
  AText := '&Delta;&alpha; = ' + FormatFloat('0.00', ASender.Distance) + '&deg;';
end;

procedure TMainForm.FitSeriesFitComplete(Sender: TObject);
var
  p: Array of Double = nil;
  i: Integer;
  s: String;
begin
  SetLength(p, FitSeries.ParamCount);
  for i:=0 to FitSeries.ParamCount-1 do
    p[i] := FitSeries.Param[i];

  s := FitSeries.EquationText.
    TextFormat(Chart.Legend.TextFormat).
    x('&alpha;').
    y('A').
    NumFormat('%.2f').
    DecimalSeparator('.').
    Params(p).
    Get;
  FitSeries.Title := '<font color="blue">Fitted:</font> ' + s;
end;

procedure TMainForm.FormActivate(Sender: TObject);
begin
  BottomPanel.Constraints.MinWidth := CbRotateXLabels.Left + CbRotateXLabels.Width +
    BtnCopyToClipBoard.Width;
  Constraints.MinWidth := BottomPanel.Constraints.MinWidth + BottomPanel.BorderSpacing.Left * 2;
  Width := 0;  // enforce constraints
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  CreateData;

  CgHTML.Checked[0] := Chart.Title.TextFormat = tfNormal;
  CgHTML.Checked[1] := Chart.Foot.TextFormat = tfNormal;
  CgHTML.Checked[2] := Chart.Legend.TextFormat = tfNormal;
  CgHTML.Checked[3] := DataSeries.Marks.TextFormat = tfNormal;
  CgHTML.Checked[4] := Chart.BottomAxis.Marks.TextFormat = tfNormal;
  CgHTML.Checked[5] := Chart.BottomAxis.Title.TextFormat = tfNormal;
  CgHTML.Checked[6] := Chart.LeftAxis.Title.TextFormat = tfNormal;
  CgHTML.Checked[7] := DistanceTool.Marks.TextFormat = tfNormal;

  {$IFDEF WINDOWS}
  Chart.Foot.Text[1] := '<font name="Times New Roman" color="gray">' + Chart.Foot.Text[1] + '</font>';
  {$ELSE}
  BtnSaveWMF.Hide;
  {$ENDIF}
end;

end.