/components/tachart/demo/chartsource/main.pas

http://github.com/graemeg/lazarus · Pascal · 148 lines · 123 code · 21 blank · 4 comment · 7 complexity · f06abe93be1b03473f93783699382db6 MD5 · raw file

  1. unit Main;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. ComCtrls, ExtCtrls, Spin, StdCtrls, Forms, TAGraph, TASeries, TASources,
  6. Classes;
  7. type
  8. { TForm1 }
  9. TForm1 = class(TForm)
  10. cbAccDirDerivative: TComboBox;
  11. ccsDerivative: TCalculatedChartSource;
  12. cbCumulative: TCheckBox;
  13. ccsAvg: TCalculatedChartSource;
  14. ccsSum: TCalculatedChartSource;
  15. Chart1: TChart;
  16. Chart1BarSeries1: TBarSeries;
  17. Chart1LineSeries1: TLineSeries;
  18. chDerivativeLineOrig: TLineSeries;
  19. chDerivativeLineDeriv: TLineSeries;
  20. Chart1LineSeries4: TLineSeries;
  21. Chart1LineSeries5: TLineSeries;
  22. Chart2: TChart;
  23. Chart2AreaSeries1: TAreaSeries;
  24. Chart2LineSeries1: TLineSeries;
  25. chDerivative: TChart;
  26. chCalc: TChart;
  27. chCalcLineSeries1: TLineSeries;
  28. chCalcLineSeriesAvg: TLineSeries;
  29. chCalcLineSeriesSum: TLineSeries;
  30. cbAccDirStatistics: TComboBox;
  31. cbSmooth: TCheckBox;
  32. seAccumulationRange: TSpinEdit;
  33. lblAccumulationRange: TLabel;
  34. ListChartSource1: TListChartSource;
  35. lcsDerivative: TListChartSource;
  36. Memo1: TMemo;
  37. PageControl1: TPageControl;
  38. Panel1: TPanel;
  39. Panel2: TPanel;
  40. rgDataShape: TRadioGroup;
  41. RandomChartSource1: TRandomChartSource;
  42. RandomChartSource2: TRandomChartSource;
  43. Splitter1: TSplitter;
  44. tsDerivative: TTabSheet;
  45. tsStatistics: TTabSheet;
  46. tsBasic: TTabSheet;
  47. procedure cbAccDirDerivativeChange(Sender: TObject);
  48. procedure cbAccDirStatisticsChange(Sender: TObject);
  49. procedure cbCumulativeChange(Sender: TObject);
  50. procedure cbSmoothChange(Sender: TObject);
  51. procedure CreateData;
  52. procedure seAccumulationRangeChange(Sender: TObject);
  53. procedure FormCreate(Sender: TObject);
  54. procedure rgDataShapeClick(Sender: TObject);
  55. end;
  56. var
  57. Form1: TForm1;
  58. implementation
  59. {$R *.lfm}
  60. uses
  61. Math;
  62. { TForm1 }
  63. procedure TForm1.cbAccDirDerivativeChange(Sender: TObject);
  64. begin
  65. ccsDerivative.AccumulationDirection :=
  66. TChartAccumulationDirection(cbAccDirDerivative.ItemIndex);
  67. end;
  68. procedure TForm1.cbAccDirStatisticsChange(Sender: TObject);
  69. begin
  70. ccsAvg.AccumulationDirection :=
  71. TChartAccumulationDirection(cbAccDirStatistics.ItemIndex);
  72. ccsSum.AccumulationDirection := ccsAvg.AccumulationDirection;
  73. end;
  74. procedure TForm1.cbCumulativeChange(Sender: TObject);
  75. begin
  76. chCalcLineSeriesSum.Active := cbCumulative.Checked;
  77. end;
  78. procedure TForm1.cbSmoothChange(Sender: TObject);
  79. begin
  80. if cbSmooth.Checked then
  81. ccsDerivative.AccumulationMethod := camSmoothDerivative
  82. else
  83. ccsDerivative.AccumulationMethod := camDerivative;
  84. end;
  85. procedure TForm1.CreateData;
  86. const
  87. N = 100;
  88. MIN_X = -10;
  89. MAX_X = 10;
  90. EPS = 1e-6;
  91. var
  92. i: Integer;
  93. x, y: Double;
  94. begin
  95. lcsDerivative.Clear;
  96. if rgDataShape.ItemIndex = 6 then
  97. for i := 0 to 9 do
  98. lcsDerivative.Add(i - IfThen(i > 6, 1, 0), i)
  99. else
  100. for i := 0 to N - 1 do begin
  101. x := MIN_X + (MAX_X - MIN_X) / (N - 1) * i;
  102. if SameValue(x, 0.0, EPS) then x := 0;
  103. case rgDataShape.ItemIndex of
  104. 0: y := x;
  105. 1: y := Sin(x);
  106. 2: if x = 0 then y := 1 else y := Sin(x) / x;
  107. 3: y := Exp(-x / 3);
  108. 4: y := Exp(-Sqr((x - 2.5) / 2.5));
  109. 5: y := Exp(-Sqr((x - 2.5) / 2.5)) + 0.05 * (Random - 0.5);
  110. end;
  111. lcsDerivative.Add(x, y);
  112. end;
  113. end;
  114. procedure TForm1.FormCreate(Sender: TObject);
  115. begin
  116. Randomize;
  117. CreateData;
  118. end;
  119. procedure TForm1.rgDataShapeClick(Sender: TObject);
  120. begin
  121. CreateData;
  122. end;
  123. procedure TForm1.seAccumulationRangeChange(Sender: TObject);
  124. begin
  125. ccsDerivative.AccumulationRange := seAccumulationRange.Value;
  126. end;
  127. end.