mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-16 19:29:34 +02:00
TAChart: Add fit demo based on code by Werner Pamler
git-svn-id: trunk@34849 -
This commit is contained in:
parent
1efdbd9cca
commit
54c94a710b
4
.gitattributes
vendored
4
.gitattributes
vendored
@ -2522,6 +2522,10 @@ components/tachart/demo/extent/extentdemo.lpi svneol=native#text/plain
|
||||
components/tachart/demo/extent/extentdemo.lpr svneol=native#text/plain
|
||||
components/tachart/demo/extent/main.lfm svneol=native#text/plain
|
||||
components/tachart/demo/extent/main.pas svneol=native#text/plain
|
||||
components/tachart/demo/fit/Main.lfm svneol=native#text/plain
|
||||
components/tachart/demo/fit/Main.pas svneol=native#text/pascal
|
||||
components/tachart/demo/fit/fitdemo.lpi svneol=native#text/plain
|
||||
components/tachart/demo/fit/fitdemo.lpr svneol=native#text/pascal
|
||||
components/tachart/demo/fpvectorial/Main.lfm svneol=native#text/plain
|
||||
components/tachart/demo/fpvectorial/Main.pas svneol=native#text/pascal
|
||||
components/tachart/demo/fpvectorial/fpvectorialdemo.lpi svneol=native#text/plain
|
||||
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -166,6 +166,10 @@ components/tachart/demo/events/lib
|
||||
components/tachart/demo/extent/*.exe
|
||||
components/tachart/demo/extent/*.lps
|
||||
components/tachart/demo/extent/lib
|
||||
components/tachart/demo/fit/*.exe
|
||||
components/tachart/demo/fit/*.lps
|
||||
components/tachart/demo/fit/*.res
|
||||
components/tachart/demo/fit/lib
|
||||
components/tachart/demo/fpvectorial/*.exe
|
||||
components/tachart/demo/fpvectorial/*.lps
|
||||
components/tachart/demo/fpvectorial/lib
|
||||
|
399
components/tachart/demo/fit/Main.lfm
Normal file
399
components/tachart/demo/fit/Main.lfm
Normal file
@ -0,0 +1,399 @@
|
||||
object frmMain: TfrmMain
|
||||
Left = 249
|
||||
Height = 503
|
||||
Top = 242
|
||||
Width = 800
|
||||
Caption = 'frmMain'
|
||||
ClientHeight = 503
|
||||
ClientWidth = 800
|
||||
OnCreate = FormCreate
|
||||
ShowHint = True
|
||||
LCLVersion = '0.9.31'
|
||||
object pnlParams: TPanel
|
||||
Left = 8
|
||||
Height = 487
|
||||
Top = 8
|
||||
Width = 376
|
||||
Align = alLeft
|
||||
BorderSpacing.Around = 8
|
||||
BevelOuter = bvNone
|
||||
ClientHeight = 487
|
||||
ClientWidth = 376
|
||||
TabOrder = 0
|
||||
object gbDataGeneration: TGroupBox
|
||||
Left = 0
|
||||
Height = 105
|
||||
Top = 0
|
||||
Width = 376
|
||||
Align = alTop
|
||||
Caption = ' Data generation '
|
||||
ClientHeight = 87
|
||||
ClientWidth = 372
|
||||
TabOrder = 0
|
||||
object btnSave: TSpeedButton
|
||||
Left = 335
|
||||
Height = 22
|
||||
Hint = 'Export data to verify fit with other software'
|
||||
Top = 44
|
||||
Width = 23
|
||||
Glyph.Data = {
|
||||
36040000424D3604000000000000360000002800000010000000100000000100
|
||||
2000000000000004000064000000640000000000000000000000FFFFFF00FFFF
|
||||
FF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFF
|
||||
FF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF006E01
|
||||
4A0066004445650043C60404048A0404048A0404048A0404048A0404048A0404
|
||||
048A650043C6650043C6650043C6650043C666004494FFFFFF00FFFFFF006F01
|
||||
4B416E014ABA9B4880EFB9ABABFFB16598FFB16598FFB5A7A7FFB5A7A7FFB9AB
|
||||
ABFFB16598FFC377AAFFC074A7FFD68ABDFF6E014ABAFFFFFF00FFFFFF007702
|
||||
50ADD488BBFFAD6194FFBAB1B1FFAD6194FFAD6194FFB1A8A8FFB1A8A8FFBAB1
|
||||
B1FFAD6194FFC377AAFFB86C9FFFD88CBFFF770250ADFFFFFF00FFFFFF007B02
|
||||
53A8D286B9FFA95D90FFC0BBBBFF95497CFF95497CFFB7B2B2FFB7B2B2FFC0BB
|
||||
BBFFA95D90FFC377AAFFB06497FFDA8EC1FF7B0253A8FFFFFF00FFFFFF007E02
|
||||
55A4D589BCFFA5598CFFC8C7C7FFC3C2C2FFC3C2C2FFC3C2C2FFC3C2C2FFC8C7
|
||||
C7FFA5598CFFC377AAFFA85C8FFFDD91C4FF7E0255A4FFFFFF00FFFFFF008002
|
||||
57A0D98DC0FFA25689FFA15588FFA15588FFA15588FFA15588FFA15588FFA155
|
||||
88FFA25689FFA25689FFA25689FFE195C8FF800257A0FFFFFF00FFFFFF008302
|
||||
599DDC90C3FFD387BAFFD387BAFFD387BAFFD387BAFFD387BAFFD387BAFFD387
|
||||
BAFFD387BAFFD387BAFFD387BAFFDC90C3FF8302599DFFFFFF00FFFFFF008502
|
||||
5A9AE094C7FFF0DDDEFFF4F4E4FFF4F4E4FFF4F4E4FFF4F4E4FFF4F4E4FFF4F4
|
||||
E4FFF4F4E4FFF4F4E4FFF0DDDEFFE094C7FF85025A9AFFFFFF00FFFFFF008702
|
||||
5C97E397CAFFF6F6E9FFECECDFFFECECDFFFECECDFFFECECDFFFECECDFFFECEC
|
||||
DFFFECECDFFFECECDFFFF6F6E9FFE397CAFF87025C97FFFFFF00FFFFFF008A03
|
||||
5E94E69ACDFFF8F8EFFFF1F1E7FFF1F1E7FFF1F1E7FFF1F1E7FFF1F1E7FFF1F1
|
||||
E7FFF1F1E7FFF1F1E7FFF8F8EFFFE69ACDFF8A035E94FFFFFF00FFFFFF008C03
|
||||
5F91EA9ED1FFFBFBF5FFF6F6F0FFF6F6F0FFF6F6F0FFF6F6F0FFF6F6F0FFF6F6
|
||||
F0FFF6F6F0FFF6F6F0FFFBFBF5FFEA9ED1FF8C035F91FFFFFF00FFFFFF008D03
|
||||
608FECA0D3FFFEFEFBFFFBFBF8FFFBFBF8FFFBFBF8FFFBFBF8FFFBFBF8FFFBFB
|
||||
F8FFFBFBF8FFFBFBF8FFFEFEFBFFECA0D3FF8D03608FFFFFFF00FFFFFF008F03
|
||||
618DF2A6D9FFFFFFFFFFFFFFFEFFFFFFFEFFFFFFFEFFFFFFFEFFFFFFFEFFFFFF
|
||||
FEFFFFFFFEFFFFFFFEFFFFFFFFFFF2A6D9FF8F03618DFFFFFF00FFFFFF009003
|
||||
62689003628B7F7F55667F7F55667F7F55667F7F55667F7F55667F7F55667F7F
|
||||
55667F7F55667F7F55667F7F55669003628B90036268FFFFFF00FFFFFF00FFFF
|
||||
FF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFF
|
||||
FF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00
|
||||
}
|
||||
NumGlyphs = 0
|
||||
OnClick = btnSaveClick
|
||||
end
|
||||
object cbTestFunction: TComboBox
|
||||
Left = 94
|
||||
Height = 21
|
||||
Hint = 'Select a dataset for fitting'
|
||||
Top = 8
|
||||
Width = 264
|
||||
ItemHeight = 13
|
||||
OnSelect = cbTestFunctionSelect
|
||||
Style = csDropDownList
|
||||
TabOrder = 0
|
||||
end
|
||||
object lblTestFunction: TLabel
|
||||
Left = 14
|
||||
Height = 14
|
||||
Top = 12
|
||||
Width = 64
|
||||
Caption = 'Test function'
|
||||
ParentColor = False
|
||||
end
|
||||
object lblOfRange: TLabel
|
||||
Left = 195
|
||||
Height = 14
|
||||
Top = 46
|
||||
Width = 56
|
||||
Caption = '% of range'
|
||||
ParentColor = False
|
||||
end
|
||||
object lblNoiseY: TLabel
|
||||
Left = 14
|
||||
Height = 14
|
||||
Top = 46
|
||||
Width = 79
|
||||
Caption = 'Noise amplitude '
|
||||
ParentColor = False
|
||||
end
|
||||
object edNoiseY: TFloatSpinEdit
|
||||
Left = 118
|
||||
Height = 21
|
||||
Top = 44
|
||||
Width = 66
|
||||
DecimalPlaces = 0
|
||||
Increment = 1
|
||||
MaxValue = 100
|
||||
MinValue = 0
|
||||
OnChange = edNoiseYChange
|
||||
TabOrder = 1
|
||||
Value = 2
|
||||
end
|
||||
end
|
||||
object gbFitting: TGroupBox
|
||||
Left = 0
|
||||
Height = 382
|
||||
Top = 105
|
||||
Width = 376
|
||||
Align = alClient
|
||||
Caption = ' Fitting '
|
||||
ClientHeight = 364
|
||||
ClientWidth = 372
|
||||
TabOrder = 1
|
||||
object gbResults: TGroupBox
|
||||
Left = 14
|
||||
Height = 142
|
||||
Top = 207
|
||||
Width = 344
|
||||
Anchors = [akTop, akLeft, akBottom]
|
||||
Caption = ' Fit results '
|
||||
ClientHeight = 124
|
||||
ClientWidth = 340
|
||||
TabOrder = 0
|
||||
object lbResults: TListBox
|
||||
Left = 12
|
||||
Height = 104
|
||||
Top = 8
|
||||
Width = 316
|
||||
Align = alClient
|
||||
BorderSpacing.Left = 12
|
||||
BorderSpacing.Top = 8
|
||||
BorderSpacing.Right = 12
|
||||
BorderSpacing.Bottom = 12
|
||||
ItemHeight = 0
|
||||
ParentFont = False
|
||||
TabOrder = 0
|
||||
end
|
||||
end
|
||||
object gbFitRange: TGroupBox
|
||||
Left = 14
|
||||
Height = 115
|
||||
Top = 84
|
||||
Width = 344
|
||||
Caption = 'Fit range'
|
||||
ClientHeight = 97
|
||||
ClientWidth = 340
|
||||
TabOrder = 1
|
||||
object cbFitRangeUseMin: TCheckBox
|
||||
Left = 17
|
||||
Height = 17
|
||||
Top = 7
|
||||
Width = 81
|
||||
Caption = 'Use minimum'
|
||||
OnClick = cbFitRangeUseMinClick
|
||||
TabOrder = 0
|
||||
end
|
||||
object cbFitRangeUseMax: TCheckBox
|
||||
Left = 17
|
||||
Height = 17
|
||||
Top = 33
|
||||
Width = 85
|
||||
Caption = 'Use maximum'
|
||||
OnClick = cbFitRangeUseMaxClick
|
||||
TabOrder = 1
|
||||
end
|
||||
object edFitRangeMin: TFloatSpinEdit
|
||||
Left = 150
|
||||
Height = 21
|
||||
Top = 7
|
||||
Width = 90
|
||||
Increment = 1
|
||||
MaxValue = 1E308
|
||||
MinValue = -1E308
|
||||
OnChange = edFitRangeMinChange
|
||||
TabOrder = 2
|
||||
Value = 5
|
||||
Visible = False
|
||||
end
|
||||
object edFitRangeMax: TFloatSpinEdit
|
||||
Left = 150
|
||||
Height = 21
|
||||
Top = 35
|
||||
Width = 90
|
||||
Increment = 1
|
||||
MaxValue = 1E308
|
||||
MinValue = -1E308
|
||||
OnChange = edFitRangeMaxChange
|
||||
TabOrder = 3
|
||||
Value = 20
|
||||
Visible = False
|
||||
end
|
||||
object cbDrawFitRangeOnly: TCheckBox
|
||||
Left = 17
|
||||
Height = 17
|
||||
Top = 64
|
||||
Width = 166
|
||||
Caption = 'Draw fit curve in fit range only'
|
||||
Checked = True
|
||||
Enabled = False
|
||||
OnClick = cbDrawFitRangeOnlyClick
|
||||
State = cbChecked
|
||||
TabOrder = 4
|
||||
end
|
||||
end
|
||||
object edFitOrder: TSpinEdit
|
||||
Left = 169
|
||||
Height = 21
|
||||
Top = 48
|
||||
Width = 53
|
||||
OnChange = edFitOrderChange
|
||||
TabOrder = 2
|
||||
Value = 2
|
||||
end
|
||||
object lblFitOrder: TLabel
|
||||
Left = 14
|
||||
Height = 14
|
||||
Top = 50
|
||||
Width = 102
|
||||
Caption = 'Degree of polynomial'
|
||||
ParentColor = False
|
||||
end
|
||||
object cbFitEquation: TComboBox
|
||||
Left = 86
|
||||
Height = 21
|
||||
Hint = 'Select a function type to be used for fitting to the generated data'
|
||||
Top = 16
|
||||
Width = 272
|
||||
ItemHeight = 13
|
||||
ItemIndex = 0
|
||||
Items.Strings = (
|
||||
'Polynomial (y = b0 + b1*x + ... bn*x^n)'
|
||||
'Linear (y = a + b*x)'
|
||||
'Exponential (y = a * exp (b * x), linear in semilog plot)'
|
||||
'Power (y = a x^b, linear in log-log plot)'
|
||||
)
|
||||
OnSelect = cbFitEquationSelect
|
||||
Style = csDropDownList
|
||||
TabOrder = 3
|
||||
Text = 'Polynomial (y = b0 + b1*x + ... bn*x^n)'
|
||||
end
|
||||
object lblFitEquation: TLabel
|
||||
Left = 14
|
||||
Height = 14
|
||||
Top = 19
|
||||
Width = 58
|
||||
Caption = 'Fit equation'
|
||||
ParentColor = False
|
||||
end
|
||||
end
|
||||
end
|
||||
object pnlChart: TPanel
|
||||
Left = 392
|
||||
Height = 487
|
||||
Top = 8
|
||||
Width = 400
|
||||
Align = alClient
|
||||
BorderSpacing.Around = 8
|
||||
BevelOuter = bvNone
|
||||
ClientHeight = 487
|
||||
ClientWidth = 400
|
||||
TabOrder = 1
|
||||
object Chart: TChart
|
||||
Left = 0
|
||||
Height = 456
|
||||
Top = 0
|
||||
Width = 400
|
||||
AxisList = <
|
||||
item
|
||||
Grid.Visible = False
|
||||
Intervals.Tolerance = 2
|
||||
Minors = <
|
||||
item
|
||||
Grid.Visible = False
|
||||
Intervals.MinLength = 5
|
||||
Intervals.Options = [aipUseCount, aipUseMinLength]
|
||||
end>
|
||||
Title.LabelFont.Orientation = 900
|
||||
Title.LabelFont.Style = [fsBold]
|
||||
Title.Visible = True
|
||||
Title.Caption = 'y'
|
||||
end
|
||||
item
|
||||
Grid.Visible = False
|
||||
Intervals.Tolerance = 2
|
||||
Alignment = calBottom
|
||||
Minors = <
|
||||
item
|
||||
Grid.Visible = False
|
||||
Intervals.MinLength = 5
|
||||
Intervals.Options = [aipUseCount, aipUseMinLength]
|
||||
end>
|
||||
Title.LabelFont.Style = [fsBold]
|
||||
Title.Visible = True
|
||||
Title.Caption = 'x'
|
||||
end>
|
||||
BackColor = clWhite
|
||||
Foot.Brush.Color = clBtnFace
|
||||
Foot.Font.Color = clBlue
|
||||
Legend.Alignment = laTopCenter
|
||||
Legend.Visible = True
|
||||
Title.Brush.Color = clBtnFace
|
||||
Title.Font.Color = clRed
|
||||
Title.Text.Strings = (
|
||||
'TAChart'
|
||||
)
|
||||
Align = alClient
|
||||
DoubleBuffered = True
|
||||
ParentColor = False
|
||||
object DataSeries: TLineSeries
|
||||
Title = 'Test data'
|
||||
AxisIndexX = 1
|
||||
AxisIndexY = 0
|
||||
LinePen.Style = psClear
|
||||
Pointer.Brush.Color = clBlack
|
||||
Pointer.HorizSize = 1
|
||||
Pointer.Style = psCross
|
||||
Pointer.VertSize = 1
|
||||
ShowPoints = True
|
||||
Source = ListChartSource
|
||||
end
|
||||
object FitSeries: TFitSeries
|
||||
Legend.Format = '%0:s: %2:s'
|
||||
Title = 'fitted data'
|
||||
AxisIndexX = 1
|
||||
AxisIndexY = 0
|
||||
OnFitComplete = FitCompleteHandler
|
||||
Pen.Color = clRed
|
||||
Source = ListChartSource
|
||||
end
|
||||
end
|
||||
object pnlLog: TPanel
|
||||
Left = 0
|
||||
Height = 31
|
||||
Top = 456
|
||||
Width = 400
|
||||
Align = alBottom
|
||||
BevelOuter = bvNone
|
||||
ClientHeight = 31
|
||||
ClientWidth = 400
|
||||
TabOrder = 1
|
||||
object cbLogX: TCheckBox
|
||||
Left = 48
|
||||
Height = 17
|
||||
Top = 3
|
||||
Width = 83
|
||||
Caption = 'Logarithmic x'
|
||||
OnClick = cbLogClick
|
||||
TabOrder = 0
|
||||
end
|
||||
object cbLogY: TCheckBox
|
||||
Left = 176
|
||||
Height = 17
|
||||
Top = 3
|
||||
Width = 83
|
||||
Caption = 'Logarithmic y'
|
||||
OnClick = cbLogClick
|
||||
TabOrder = 1
|
||||
end
|
||||
end
|
||||
end
|
||||
object ListChartSource: TListChartSource
|
||||
left = 584
|
||||
top = 120
|
||||
end
|
||||
object SaveDialog: TSaveDialog
|
||||
left = 480
|
||||
top = 64
|
||||
end
|
||||
object ChartAxisTransformations: TChartAxisTransformations
|
||||
left = 584
|
||||
top = 184
|
||||
object LogarithmAxisTransform: TLogarithmAxisTransform
|
||||
Base = 10
|
||||
end
|
||||
end
|
||||
end
|
276
components/tachart/demo/fit/Main.pas
Normal file
276
components/tachart/demo/fit/Main.pas
Normal file
@ -0,0 +1,276 @@
|
||||
unit Main;
|
||||
|
||||
{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
|
||||
StdCtrls, Spin, Buttons, TAGraph, TASeries, TASources,
|
||||
TAFuncSeries, TATransformations;
|
||||
|
||||
type
|
||||
|
||||
{ TfrmMain }
|
||||
|
||||
TfrmMain = class(TForm)
|
||||
cbTestFunction: TComboBox;
|
||||
Chart: TChart;
|
||||
cbDrawFitRangeOnly: TCheckBox;
|
||||
FitSeries: TFitSeries;
|
||||
DataSeries: TLineSeries;
|
||||
cbFitRangeUseMin:TCheckBox;
|
||||
cbFitRangeUseMax:TCheckBox;
|
||||
cbFitEquation: TComboBox;
|
||||
cbLogX: TCheckBox;
|
||||
cbLogY: TCheckBox;
|
||||
ChartAxisTransformations: TChartAxisTransformations;
|
||||
LogarithmAxisTransform: TLogarithmAxisTransform;
|
||||
edFitRangeMax:TFloatSpinEdit;
|
||||
edNoiseY: TFloatSpinEdit;
|
||||
edFitRangeMin:TFloatSpinEdit;
|
||||
gbFitRange:TGroupBox;
|
||||
gbDataGeneration: TGroupBox;
|
||||
gbFitting: TGroupBox;
|
||||
gbResults: TGroupBox;
|
||||
lblFitOrder:TLabel;
|
||||
lblNoiseY: TLabel;
|
||||
lblFitEquation: TLabel;
|
||||
lblOfRange: TLabel;
|
||||
lblTestFunction: TLabel;
|
||||
lbResults: TListBox;
|
||||
ListChartSource: TListChartSource;
|
||||
pnlParams: TPanel;
|
||||
edFitOrder:TSpinEdit;
|
||||
pnlLog: TPanel;
|
||||
pnlChart: TPanel;
|
||||
SaveDialog: TSaveDialog;
|
||||
btnSave: TSpeedButton;
|
||||
procedure btnSaveClick(Sender: TObject);
|
||||
procedure cbDrawFitRangeOnlyClick(Sender: TObject);
|
||||
procedure cbFitEquationSelect(Sender: TObject);
|
||||
procedure cbFitRangeUseMaxClick(Sender:TObject);
|
||||
procedure cbFitRangeUseMinClick(Sender:TObject);
|
||||
procedure cbLogClick(Sender: TObject);
|
||||
procedure cbTestFunctionSelect(Sender: TObject);
|
||||
procedure edFitOrderChange(Sender:TObject);
|
||||
procedure edFitRangeMaxChange(Sender:TObject);
|
||||
procedure edFitRangeMinChange(Sender:TObject);
|
||||
procedure edNoiseYChange(Sender: TObject);
|
||||
procedure FormCreate(Sender: TObject);
|
||||
procedure FitCompleteHandler(Sender:TObject);
|
||||
private
|
||||
procedure CreateData;
|
||||
end;
|
||||
|
||||
var
|
||||
frmMain: TfrmMain;
|
||||
|
||||
implementation
|
||||
|
||||
{$R *.lfm}
|
||||
|
||||
uses
|
||||
Math, TAChartAxis, TAChartUtils, TACustomSource;
|
||||
|
||||
const
|
||||
// Parameters used for data generation; should be reproduced by the fit.
|
||||
POLY_PARAMS: array[0..2] of Double = (100, -8, 0.2);
|
||||
LIN_PARAMS : array[0..1] of Double = (100.0, -2.5);
|
||||
EXP_PARAMS : array[0..1] of Double = (10.0, -0.05);
|
||||
PWR_PARAMS : array[0..1] of Double = (3.0, -0.5);
|
||||
|
||||
// Min and max for x axis of the various test functions
|
||||
// positive numbers only because of the logarithms involved in this example.
|
||||
XRANGE : array[TFitEquation, 0..1] of Double = (
|
||||
(0.1, 50),
|
||||
(1, 20),
|
||||
(0.001, 100),
|
||||
(1, 20)
|
||||
);
|
||||
|
||||
{ TfrmMain }
|
||||
|
||||
procedure TfrmMain.btnSaveClick(Sender: TObject);
|
||||
var
|
||||
s: TStream;
|
||||
fs: TFormatSettings;
|
||||
si: PChartDataItem;
|
||||
begin
|
||||
if not SaveDialog.Execute then exit;
|
||||
fs := DefaultFormatSettings;
|
||||
fs.DecimalSeparator := '.';
|
||||
s := TFileStream.Create(SaveDialog.FileName, fmCreate);
|
||||
try
|
||||
for si in ListChartSource do
|
||||
s.WriteAnsiString(Format('%.9g'#9'%.9g'#13#10, [si^.X, si^.Y], fs));
|
||||
finally
|
||||
s.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.cbDrawFitRangeOnlyClick(Sender: TObject);
|
||||
begin
|
||||
FitSeries.DrawFitRangeOnly := cbDrawFitRangeOnly.Checked;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.cbFitEquationSelect(Sender: TObject);
|
||||
var
|
||||
eq: TFitEquation;
|
||||
begin
|
||||
eq := TFitEquation(cbFitEquation.ItemIndex);
|
||||
FitSeries.FitEquation := eq;
|
||||
edFitOrder.Enabled := (eq = fePolynomial);
|
||||
lblFitOrder.Enabled := edFitOrder.Enabled;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.cbFitRangeUseMaxClick(Sender:TObject);
|
||||
begin
|
||||
edFitRangeMax.Visible := cbFitRangeUseMax.Checked;
|
||||
FitSeries.FitRange.UseMax := cbFitRangeUseMax.Checked;
|
||||
cbDrawFitRangeOnly.Enabled := cbFitRangeUseMin.Checked or cbFitRangeUseMax.Checked;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.cbFitRangeUseMinClick(Sender:TObject);
|
||||
begin
|
||||
edFitRangeMin.Visible := cbFitRangeUseMin.Checked;
|
||||
FitSeries.FitRange.UseMin := cbFitRangeUseMin.Checked;
|
||||
cbDrawFitRangeOnly.Enabled := cbFitRangeUseMin.Checked or cbFitRangeUseMax.Checked;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.cbLogClick(Sender: TObject);
|
||||
var
|
||||
axis: TChartAxis;
|
||||
begin
|
||||
if Sender = cbLogX then
|
||||
axis := Chart.BottomAxis
|
||||
else
|
||||
axis := Chart.LeftAxis;
|
||||
if (Sender as TCheckbox).Checked then begin
|
||||
axis.Transformations := ChartAxisTransformations;
|
||||
axis.Intervals.Options :=
|
||||
[aipUseMinLength, aipUseCount, aipGraphCoords, aipUseNiceSteps];
|
||||
axis.Intervals.NiceSteps :=
|
||||
Format('%g|%g|%g|%g', [Log10(2), Log10(3), Log10(5), Log10(10)]);
|
||||
end else begin
|
||||
axis.Transformations := nil;
|
||||
axis.Intervals.Options := [aipUseMinLength, aipUseMaxLength, aipUseNiceSteps];
|
||||
axis.Intervals.NiceSteps := '0.2|0.5|1.0';
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.cbTestFunctionSelect(Sender: TObject);
|
||||
begin
|
||||
CreateData;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.CreateData;
|
||||
const
|
||||
N = 100;
|
||||
var
|
||||
i: Integer;
|
||||
x, y, xmin, xmax, ymin, ymax, maxNoise: Double;
|
||||
xarr, yarr: array of Double;
|
||||
begin
|
||||
RandSeed := 875876; // Reproducible noise for testing.
|
||||
|
||||
// Calculate test data and store in temporary arrays.
|
||||
// This is because noise is relative to the data range in this example.
|
||||
xmin := XRANGE[TFitEquation(cbTestFunction.ItemIndex), 0];
|
||||
xmax := XRANGE[TFitEquation(cbTestFunction.ItemIndex), 1];
|
||||
SetLength(xarr, N);
|
||||
SetLength(yarr, N);
|
||||
for i := 0 to High(xarr) do begin
|
||||
x := xmin + (xmax - xmin) / (N - 1) * i;
|
||||
case TFitEquation(cbTestFunction.ItemIndex) of
|
||||
fePolynomial: y := POLY_PARAMS[0] + POLY_PARAMS[1]*x + POLY_PARAMS[2]*x*x;
|
||||
feLinear : y := LIN_PARAMS[0] + LIN_PARAMS[1]*x;
|
||||
feExp : y := EXP_PARAMS[0]*Exp(EXP_PARAMS[1]*x);
|
||||
fePower : y := PWR_PARAMS[0]*Power(x, PWR_PARAMS[1]);
|
||||
end;
|
||||
xarr[i] := x;
|
||||
yarr[i] := y;
|
||||
end;
|
||||
|
||||
// Add noise to the y values, and add data to line series.
|
||||
ymin := MinValue(yarr);
|
||||
ymax := MaxValue(yarr);
|
||||
maxNoise := edNoiseY.Value * (ymax - ymin) * 0.01;
|
||||
DataSeries.BeginUpdate;
|
||||
try
|
||||
DataSeries.Clear;
|
||||
for i := 0 to High(xarr) do begin
|
||||
x := xarr[i];
|
||||
y := yarr[i] + maxNoise * (Random - 0.5);
|
||||
if TFitEquation(cbTestFunction.ItemIndex) = feExp then
|
||||
// Make sure that the noise generation does not produce negative
|
||||
// values for the exponential data set.
|
||||
while y < 0 do
|
||||
y := yarr[i] + maxNoise * (Random - 0.5);
|
||||
DataSeries.AddXY(x, y);
|
||||
end;
|
||||
finally
|
||||
DataSeries.EndUpdate;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.edFitOrderChange(Sender:TObject);
|
||||
begin
|
||||
// Needs one parameter more than degree of fit polynomial.
|
||||
FitSeries.ParamCount := edFitOrder.Value + 1;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.edFitRangeMaxChange(Sender:TObject);
|
||||
begin
|
||||
FitSeries.FitRange.Max := edFitRangeMax.Value;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.edFitRangeMinChange(Sender:TObject);
|
||||
begin
|
||||
FitSeries.FitRange.Min := edFitRangeMin.Value;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.edNoiseYChange(Sender: TObject);
|
||||
begin
|
||||
CreateData;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.FitCompleteHandler(Sender:TObject);
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
with lbResults.Items do begin
|
||||
BeginUpdate;
|
||||
Clear;
|
||||
case TFitEquation(cbFitEquation.ItemIndex) of
|
||||
fePolynomial:
|
||||
for i := 0 to FitSeries.ParamCount - 1 do
|
||||
Add(Format('b[%d] = %g', [i, FitSeries.Param[i]]));
|
||||
else
|
||||
Add(Format('a = %g', [FitSeries.Param[0]]));
|
||||
Add(Format('b = %g', [FitSeries.Param[1]]));
|
||||
end;
|
||||
EndUpdate;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TfrmMain.FormCreate(Sender: TObject);
|
||||
const
|
||||
FMT = '%g';
|
||||
begin
|
||||
with cbTestFunction do begin
|
||||
Items.Add(ParamsToEquation(fePolynomial, POLY_PARAMS, FMT));
|
||||
Items.Add(ParamsToEquation(feLinear, LIN_PARAMS, FMT));
|
||||
Items.Add(ParamsToEquation(feExp, EXP_PARAMS, FMT));
|
||||
Items.Add(ParamsToEquation(fePower, PWR_PARAMS, FMT));
|
||||
ItemIndex := Ord(fePolynomial);
|
||||
end;
|
||||
|
||||
FitSeries.FitRange.Min := edFitRangeMin.Value;
|
||||
FitSeries.FitRange.Max := edFitRangeMax.Value;
|
||||
|
||||
CreateData;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
98
components/tachart/demo/fit/fitdemo.lpi
Normal file
98
components/tachart/demo/fit/fitdemo.lpi
Normal file
@ -0,0 +1,98 @@
|
||||
<?xml version="1.0"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="fitdemo"/>
|
||||
<ResourceType Value="res"/>
|
||||
<UseXPManifest Value="True"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
|
||||
<ExcludeFileFilter Value="*.(bak|ppu|o|so);*~;backup"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<RequiredPackages Count="2">
|
||||
<Item1>
|
||||
<PackageName Value="TAChartLazarusPkg"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<PackageName Value="LCL"/>
|
||||
</Item2>
|
||||
</RequiredPackages>
|
||||
<Units Count="2">
|
||||
<Unit0>
|
||||
<Filename Value="fitdemo.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="fitdemo"/>
|
||||
</Unit0>
|
||||
<Unit1>
|
||||
<Filename Value="Main.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<ComponentName Value="frmMain"/>
|
||||
<ResourceBaseClass Value="Form"/>
|
||||
<UnitName Value="Main"/>
|
||||
</Unit1>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="fitdemo"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
<CodeGeneration>
|
||||
<Checks>
|
||||
<RangeChecks Value="True"/>
|
||||
<OverflowChecks Value="True"/>
|
||||
</Checks>
|
||||
</CodeGeneration>
|
||||
<Linking>
|
||||
<Options>
|
||||
<Win32>
|
||||
<GraphicApplication Value="True"/>
|
||||
</Win32>
|
||||
</Options>
|
||||
</Linking>
|
||||
<Other>
|
||||
<CompilerMessages>
|
||||
<UseMsgFile Value="True"/>
|
||||
</CompilerMessages>
|
||||
<CompilerPath Value="$(CompPath)"/>
|
||||
</Other>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
20
components/tachart/demo/fit/fitdemo.lpr
Normal file
20
components/tachart/demo/fit/fitdemo.lpr
Normal file
@ -0,0 +1,20 @@
|
||||
program fitdemo;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
uses
|
||||
{$IFDEF UNIX}{$IFDEF UseCThreads}
|
||||
cthreads,
|
||||
{$ENDIF}{$ENDIF}
|
||||
Interfaces, // this includes the LCL widgetset
|
||||
Forms, Main, tachartlazaruspkg;
|
||||
|
||||
{$R *.res}
|
||||
|
||||
begin
|
||||
RequireDerivedFormResource := True;
|
||||
Application.Initialize;
|
||||
Application.CreateForm(TfrmMain, frmMain);
|
||||
Application.Run;
|
||||
end.
|
||||
|
Loading…
Reference in New Issue
Block a user