File: uplot.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 (180 lines) | stat: -rw-r--r-- 5,344 bytes parent folder | download | duplicates (6)
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
unit uplot;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Graphics, types,
  TAGraph, TATypes, TACustomSeries;

type
  TPlotType = (ptSymbolsOnly, ptSegments, ptCubicSpline, ptBSpline,
    ptLinearFit, ptSquareFit, ptArea, ptColumns, ptBars);

procedure PrepareChart(AChart: TChart;
  xTitle, yTitle, AChartTitle, AChartFooter: String);

function Plot(AChart: TChart; x,y: TDoubleDynArray; ALegendText: String;
  AColor: TColor; ASymbol: TSeriesPointerStyle; ALineStyle: TPenStyle;
  APlotType: TPlotType; AShowSymbols: Boolean = false): TChartSeries;


implementation

uses
  Math, LCLIntf,
  TAFitUtils, TASeries, TAFuncSeries, TATools;

procedure PrepareChart(AChart: TChart;
  xTitle, yTitle, AChartTitle, AChartFooter: String);
begin
  AChart.BottomAxis.Title.Visible := xTitle <> '';
  AChart.BottomAxis.Title.Caption := xTitle;
  AChart.BottomAxis.Title.LabelFont.Style := [fsBold];
  AChart.BottomAxis.Title.LabelFont.Size := 10;
  AChart.BottomAxis.Grid.Color := $E0E0E0;
  AChart.BottomAxis.Grid.Style := psSolid;
  AChart.BottomAxis.AxisPen.Visible := true;

  AChart.LeftAxis.Title.Visible := yTitle <> '';
  AChart.LeftAxis.Title.Caption := yTitle;
  AChart.LeftAxis.Title.LabelFont.Style := [fsBold];
  AChart.LeftAxis.Title.LabelFont.Size := 10;
  AChart.LeftAxis.Grid.Color := $E0E0E0;
  AChart.LeftAxis.Grid.Style := psSolid;
  AChart.LeftAxis.AxisPen.Visible := true;

  AChart.Title.Visible := AChartTitle <> '';
  AChart.Title.Text.Text := AChartTitle;
  AChart.Title.Font.Style := [fsBold];
  AChart.Title.Font.Size := 14;

  AChart.Foot.Visible := AChartFooter <> '';
  AChart.Foot.Text.Text := AChartFooter;
  AChart.Foot.Font.Size := 8;
  AChart.Foot.Alignment := taLeftJustify;

  AChart.Legend.SymbolWidth := 40;
  AChart.Legend.Frame.Color := clSilver;

  AChart.Backcolor := clWhite;
  AChart.Frame.Color := clSilver;

  AChart.Visible := True;
end;

function Plot(AChart: TChart; x,y: TDoubleDynArray; ALegendText: String;
  AColor: TColor; ASymbol: TSeriesPointerStyle; ALineStyle: TPenStyle;
  APlotType: TPlotType; AShowSymbols: Boolean = false): TChartSeries;
var
  i: Integer;
begin
  case APlotType of
    ptSymbolsOnly, ptSegments:
      begin
        Result := TLineSeries.Create(AChart.Owner);
        with TLineSeries(Result) do
        begin
          ShowPoints := (APlotType = ptSymbolsOnly) or AShowSymbols;
          ShowLines := (APlotType <> ptSymbolsOnly);
          LinePen.Style := ALineStyle;
          SeriesColor := AColor;
          Pointer.Brush.Color := AColor;
          Pointer.Pen.Color := AColor;
          Pointer.Style := ASymbol;
        end;
      end;
    ptCubicSpline:
      begin
        Result := TCubicSplineSeries.create(AChart.Owner);
        with TCubicSplineSeries(Result) do
        begin
          Pen.Color := AColor;
          Pen.Style := ALineStyle;
          Pointer.Brush.Color := AColor;
          Pointer.Pen.Color := AColor;
          Pointer.Style := ASymbol;
          Pointer.Visible := AShowSymbols;
        end;
      end;
    ptBSpline:
      begin
        Result := TBSplineSeries.Create(AChart.Owner);
        with TBSplineSeries(Result) do
        begin
          Pen.Color := AColor;
          Pen.Style := ALineStyle;
          Pointer.Brush.Color := AColor;
          Pointer.Pen.Color := AColor;
          Pointer.Style := ASymbol;
          Pointer.Visible := AShowSymbols;
        end;
      end;
    ptLinearFit, ptSquareFit:
      begin
        Result := TFitSeries.Create(AChart.Owner);
        with TFitSeries(Result) do
        begin
          Pen.Color := AColor;
          Pen.Style := ALineStyle;
          if APlotType = ptLinearfit then
          begin
            FitEquation := feLinear;
            Title := 'linear fit to ' + ALegendText;
          end else
          begin
            FitEquation := fePolynomial;
            ParamCount := 3;  // parabolic fit needs 3 parameters
            Title := 'parabolic fit to ' + ALegendText;
          end;
          Pointer.Brush.Color := AColor;
          Pointer.Pen.Color := AColor;
          Pointer.Style := ASymbol;
          Pointer.Visible := AShowSymbols;
        end;
      end;
    ptArea:
      begin
        Result := TAreaSeries.Create(AChart.Owner);
        with TAreaSeries(Result) do
        begin
          SeriesColor := AColor;
          AreaContourPen.Color := AColor;
          AreaLinesPen.Color := AColor;
        end;
      end;
    ptColumns, ptBars:
      begin
        Result := TBarSeries.Create(AChart.Owner);
        with TBarSeries(Result) do
        begin
          BarBrush.Color := AColor;
          BarPen.Color := AColor;
          BarWidthStyle := bwPercentMin;
          if APlotType = ptBars then begin
            AxisIndexX := AChart.LeftAxis.Index;
            AxisIndexY := AChart.BottomAxis.Index;
          end else begin
            AxisIndexX := AChart.BottomAxis.Index;
            AxisIndexY := AChart.LeftAxis.Index;
          end;
        end;
      end;
  end;

  Result.Title := ALegendText;

  if Result is TBasicPointSeries then
    with TBasicPointSeries(Result) do
      for i:=0 to Min(High(x), High(y)) do
        AddXY(x[i], y[i]);

  AChart.AddSeries(Result);
  AChart.Legend.Visible := AChart.SeriesCount > 1;

  // Paint new series in front of the others
  Result.ZPosition := AChart.SeriesCount;
end;

end.