File: main.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (131 lines) | stat: -rw-r--r-- 3,828 bytes parent folder | download | duplicates (2)
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
unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Types, Forms, Controls,
  Graphics, Dialogs, StdCtrls, ExtCtrls,
  TAGraph, TACustomSeries, TASeries, TAFuncSeries;

type

  { TMainForm }

  TMainForm = class(TForm)
    Bevel1: TBevel;
    BtnAdd: TButton;
    BtnClear: TButton;
    Chart: TChart;
    CbPlotTypes: TComboBox;
    procedure BtnAddClick(Sender: TObject);
    procedure BtnClearClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private

  public

  end;

var
  MainForm: TMainForm;

implementation

{$R *.lfm}

uses
  TATypes, uplot;

const
  PALETTE: array[0..7] of TColor = (
    clRed, clGreen, clBlue, clOlive, clFuchsia, clAqua, clBlack, clLime);

  SYMBOLS: array[0..5] of TSeriesPointerStyle = (
    psCircle, psRectangle, psTriangle, psDiamond, psDownTriangle, psFullStar);

  LINESTYLES: array[0..4] of TPenStyle = (
    psSolid, psDot, psDash, psDashDot, psDashDotDot);


{ TMainForm }

procedure CreateData(ACount: Integer; var x, y: TDoubleDynArray);
const
  MIN = -10;
  MAX = 10;
var
  i: Integer;
begin
  SetLength(x, ACount);
  SetLength(y, ACount);
  for i:=0 to ACount-1 do
  begin
    x[i] := MIN + (MAX - MIN) * i / (ACount - 1);
    y[i] := random;
  end;
end;

procedure TMainForm.BtnAddClick(Sender: TObject);
var
  x, y: TDoubleDynArray;
  colorIndex: Integer;
  symbolIndex: Integer;
  linestyleIndex: Integer;
  plottype: Integer;
begin
  if Chart.SeriesCount = 0 then
    PrepareChart(Chart, 'x axis', 'y axis', 'TAChart Plot Demo', 'Written by Lazarus');
  CreateData(Random(20)+3, x, y);
  colorIndex := Chart.SeriesCount mod Length(PALETTE);
  symbolIndex := Chart.SeriesCount mod Length(SYMBOLS);
  lineStyleIndex := Chart.SeriesCount mod Length(LINESTYLES);
  plottype := PtrInt(CbPlotTypes.Items.Objects[CbPlotTypes.ItemIndex]);
  Plot(Chart,
    x, y,
    'Series #'+IntToStr(Chart.SeriesCount+1),
    PALETTE[colorIndex],
    SYMBOLS[symbolIndex],
    LINESTYLES[linestyleIndex],
    TPlotType(abs(plottype)),
    plottype < 0                // with symbols if value is negative
  );
end;

procedure TMainForm.BtnClearClick(Sender: TObject);
begin
  Chart.ClearSeries;
  Chart.Legend.Visible := false;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  Chart.Visible := false;

  CbPlotTypes.Items.Clear;

  // Encode plot types into the object of the CbPlotTypes combobox:
  // - The ord value of the plot type is cast to an object if the series is
  //   without symbols.
  // - The negative ord value of the plot type is cast if an object if the
  //   series is with symbols.
  CbPlotTypes.Items.AddObject('Symbols only', TObject(PtrInt(ptSymbolsOnly)));
  CbPlotTypes.Items.AddObject('Line segments w/ symbols', TObject(-PtrInt(ptSegments)));
  CbPlotTypes.Items.AddObject('Line segments w/o symbols', TObject(PtrInt(ptSegments)));
  CbPlotTypes.Items.AddObject('Cubic spline w/ symbols', TObject(-PtrInt(ptCubicSpline)));
  CbPlotTypes.Items.AddObject('Cubic spline w/o symbols', TObject(PtrInt(ptCubicSpline)));
  CbPlotTypes.Items.AddObject('B-Spline w/ symbols', TObject(-PtrInt(ptBSpline)));
  CbPlotTypes.Items.AddObject('B-Spline w/o symbols', TObject(PtrInt(ptBSpline)));
  CbPlotTypes.Items.AddObject('Linear fit w/ symbols', TObject(-PtrInt(ptLinearfit)));
  CbPlotTypes.Items.AddObject('Linear fit w/o symbols', TObject(PtrInt(ptLinearFit)));
  CbPlotTypes.Items.AddObject('Parabolic fit w/symbols', TObject(-PtrInt(ptSquareFit)));
  CbPlotTypes.Items.AddObject('Parabolic fit w/o symbols)', TObject(PtrInt(ptSquareFit)));
  CbPlotTypes.Items.AddObject('Area (no symbols)', TObject(PtrInt(ptArea)));
  CbPlotTypes.Items.AddObject('Bars (horizontal)', TObject(PtrInt(ptBars)));
  CbPlotTypes.Items.AddObject('Columns (vertical)', TObject(PtrInt(ptColumns)));
  CbPlotTypes.ItemIndex := 1;
end;

end.