TAChart: Use TCumulNormDistrAxisTransform in the axis transform demo. Based on code by Werner Pamler.

git-svn-id: trunk@32693 -
This commit is contained in:
ask 2011-10-05 07:49:52 +00:00
parent 6e376ce0a8
commit e3733ed9ff
3 changed files with 272 additions and 19 deletions

View File

@ -75,6 +75,10 @@
<SmartLinkUnit Value="True"/>
</CodeGeneration>
<Linking>
<Debugging>
<GenerateDebugInfo Value="True"/>
<DebugInfoType Value="dsAuto"/>
</Debugging>
<LinkSmart Value="True"/>
<Options>
<Win32>

View File

@ -1,30 +1,30 @@
object Form1: TForm1
Left = 1345
Height = 333
Height = 434
Top = 166
Width = 533
Caption = 'TAChart axis transformation demo'
ClientHeight = 333
ClientHeight = 434
ClientWidth = 533
OnCreate = FormCreate
Position = poScreenCenter
LCLVersion = '0.9.31'
object PageControl1: TPageControl
Left = 0
Height = 333
Height = 434
Top = 0
Width = 533
ActivePage = lsLinear
ActivePage = tsLinear
Align = alClient
TabIndex = 0
TabOrder = 0
object lsLinear: TTabSheet
object tsLinear: TTabSheet
Caption = 'Linear'
ClientHeight = 307
ClientHeight = 408
ClientWidth = 525
object ChartT: TChart
Left = 0
Height = 278
Height = 379
Top = 0
Width = 525
AxisList = <
@ -94,7 +94,7 @@ object Form1: TForm1
object pnlAutoControls: TPanel
Left = 0
Height = 29
Top = 278
Top = 379
Width = 525
Align = alBottom
ClientHeight = 29
@ -113,11 +113,11 @@ object Form1: TForm1
end
object tsIndependent: TTabSheet
Caption = 'Independent scale'
ClientHeight = 307
ClientHeight = 408
ClientWidth = 525
object ChartIndependent: TChart
Left = 0
Height = 269
Height = 370
Top = 0
Width = 525
AxisList = <
@ -164,7 +164,7 @@ object Form1: TForm1
object pnlIndependentControls: TPanel
Left = 0
Height = 38
Top = 269
Top = 370
Width = 525
Align = alBottom
ClientHeight = 38
@ -220,11 +220,11 @@ object Form1: TForm1
end
object tsLog: TTabSheet
Caption = 'Logarithm'
ClientHeight = 307
ClientHeight = 408
ClientWidth = 525
object ChartLog: TChart
Left = 0
Height = 278
Height = 379
Top = 0
Width = 525
AxisList = <
@ -238,6 +238,7 @@ object Form1: TForm1
item
Grid.Visible = False
Intervals.Count = 9
Intervals.MinLength = 5
Intervals.Options = [aipUseCount, aipUseMaxLength, aipUseMinLength, aipUseNiceSteps]
end>
Title.LabelFont.Orientation = 900
@ -284,7 +285,6 @@ object Form1: TForm1
ShowPoints = True
end
object cfsLog: TFuncSeries
Active = False
Extent.UseXMax = True
Extent.UseXMin = True
Extent.XMax = 3
@ -296,7 +296,7 @@ object Form1: TForm1
object pnlLogControls: TPanel
Left = 0
Height = 29
Top = 278
Top = 379
Width = 525
Align = alBottom
ClientHeight = 29
@ -315,13 +315,149 @@ object Form1: TForm1
end
end
end
object tsCumulNormDistr: TTabSheet
Caption = 'Normal distribution'
ClientHeight = 408
ClientWidth = 525
object ChartCumulNormDistr: TChart
Left = 0
Height = 342
Top = 66
Width = 525
AxisList = <
item
Grid.Color = clSilver
Grid.Visible = False
Intervals.Count = 30
Intervals.Options = [aipUseCount, aipUseNiceSteps]
Marks.OverlapPolicy = opHideNeighbour
Minors = <>
Title.LabelFont.Orientation = 900
Title.LabelFont.Style = [fsBold]
Title.Visible = True
Title.Caption = 'Cumulative probability'
Transformations = catCumulNormDistr
end
item
Grid.Color = clSilver
Grid.Visible = False
Alignment = calBottom
Minors = <>
Title.LabelFont.Style = [fsBold]
Title.Visible = True
Title.Caption = 'Random number'
end>
BackColor = clWhite
Extent.XMax = 3
Extent.XMin = -3
Extent.YMax = 3
Extent.YMin = -3
Foot.Brush.Color = clBtnFace
Foot.Font.Color = clBlue
MarginsExternal.Left = 8
MarginsExternal.Right = 12
MarginsExternal.Bottom = 8
Title.Brush.Color = clBtnFace
Title.Font.Color = clBlue
Title.Text.Strings = (
'TAChart'
)
Align = alClient
ParentColor = False
object ChartCumulNormDistrLineSeries1: TLineSeries
AxisIndexX = 1
AxisIndexY = 0
Pointer.Brush.Color = clBlue
Pointer.HorizSize = 2
Pointer.Pen.Color = clBlue
Pointer.Style = psCircle
Pointer.VertSize = 2
ShowPoints = True
end
end
object pnCumulNormDistr: TPanel
Left = 0
Height = 66
Top = 0
Width = 525
Align = alTop
BevelOuter = bvNone
ClientHeight = 66
ClientWidth = 525
TabOrder = 1
object rgRandDistr: TRadioGroup
Left = 0
Height = 66
Top = 0
Width = 185
Align = alLeft
AutoFill = True
Caption = ' Random number generation '
ChildSizing.LeftRightSpacing = 6
ChildSizing.TopBottomSpacing = 6
ChildSizing.EnlargeHorizontal = crsHomogenousChildResize
ChildSizing.EnlargeVertical = crsHomogenousChildResize
ChildSizing.ShrinkHorizontal = crsScaleChilds
ChildSizing.ShrinkVertical = crsScaleChilds
ChildSizing.Layout = cclLeftToRightThenTopToBottom
ChildSizing.ControlsPerLine = 1
ClientHeight = 48
ClientWidth = 181
ItemIndex = 1
Items.Strings = (
'uniform distribution'
'normal distribution'
)
OnClick = rgRandDistrClick
ParentFont = False
TabOrder = 0
end
object cbPercent: TCheckBox
Left = 192
Height = 17
Top = 43
Width = 57
Caption = 'Percent'
OnChange = cbPercentChange
TabOrder = 1
end
object cbUseAxisTransform: TCheckBox
Left = 280
Height = 17
Top = 43
Width = 110
Caption = 'Use AxisTransform'
OnChange = cbUseAxisTransformChange
TabOrder = 2
end
object edDataCount: TSpinEdit
Left = 281
Height = 21
Top = 10
Width = 82
Increment = 50
MaxValue = 1000000
OnChange = edDataCountChange
TabOrder = 3
Value = 300
end
object lblDataCount: TLabel
Left = 194
Height = 14
Top = 14
Width = 58
Caption = 'Data count:'
ParentColor = False
end
end
end
object tsUser: TTabSheet
Caption = 'User defined'
ClientHeight = 307
ClientHeight = 408
ClientWidth = 525
object ChartUser: TChart
Left = 0
Height = 307
Height = 408
Top = 0
Width = 525
AxisList = <
@ -462,4 +598,19 @@ object Form1: TForm1
object catIndependent2Zoom: TLinearAxisTransform
end
end
object AxLabelSource: TListChartSource
left = 311
top = 305
end
object catCumulNormDistr: TChartAxisTransformations
left = 310
top = 305
object catCumulNormDistrLinearAxisTransform1: TLinearAxisTransform
Enabled = False
Scale = 0.01
end
object catCumulNormDistrCumulNormDistrAxisTransform1: TCumulNormDistrAxisTransform
Enabled = False
end
end
end

View File

@ -13,6 +13,9 @@ type
{ TForm1 }
TForm1 = class(TForm)
AxLabelSource: TListChartSource;
catCumulNormDistrCumulNormDistrAxisTransform1: TCumulNormDistrAxisTransform;
catCumulNormDistrLinearAxisTransform1: TLinearAxisTransform;
catIndependent1Zoom: TLinearAxisTransform;
catIndependent2Zoom: TLinearAxisTransform;
catIndependent2: TChartAxisTransformations;
@ -23,9 +26,14 @@ type
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;
@ -45,18 +53,23 @@ type
catTFahrToCel: TLinearAxisTransform;
ChartTSummer: TLineSeries;
ChartTWinterLine: TLineSeries;
edDataCount: TSpinEdit;
fseIndependent1: TFloatSpinEdit;
fseIndependent2: TFloatSpinEdit;
lblDataCount: TLabel;
lblIndependentScale1: TLabel;
lblIndependentScale2: TLabel;
PageControl1: TPageControl;
pnCumulNormDistr: TPanel;
pnlIndependentControls: TPanel;
pnlLogControls: TPanel;
pnlAutoControls: TPanel;
rcsUser: TRandomChartSource;
rcsTSummer: TRandomChartSource;
rcsTWinter: TRandomChartSource;
lsLinear: TTabSheet;
rgRandDistr: TRadioGroup;
tsLinear: TTabSheet;
tsCumulNormDistr: TTabSheet;
tsIndependent: TTabSheet;
tsUser: TTabSheet;
tsLog: TTabSheet;
@ -64,12 +77,17 @@ type
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);
private
procedure FillIndependentSource;
procedure FillCumulNormDistrSource;
end;
var
@ -78,10 +96,35 @@ var
implementation
uses
Math, SysUtils, TAChartAxis, TAChartUtils;
Math, StrUtils, SysUtils, TAChartAxis, 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;
@ -100,6 +143,20 @@ 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
@ -121,6 +178,41 @@ 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
@ -149,6 +241,7 @@ begin
clsLogPoints.AddXY(x + Random - 0.5, MyFunc(x) + Random - 0.5);
end;
FillIndependentSource;
FillCumulNormDistrSource;
end;
procedure TForm1.fseIndependent1Change(Sender: TObject);
@ -161,5 +254,10 @@ begin
catIndependent2Zoom.Scale := fseIndependent2.Value;
end;
procedure TForm1.rgRandDistrClick(Sender: TObject);
begin
FillCumulNormDistrSource;
end;
end.