File: Main.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (155 lines) | stat: -rw-r--r-- 4,399 bytes parent folder | download | duplicates (3)
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
{ Demonstrates how a chart can be painted by means of the AggPas library,
  either directly or via the corresponding GUIConnector.
  
  Note that the AggPas version coming with Lazarus is unmaintained at the moment 
  and rather buggy. Font support works fine only on Windows. On Linux, you must 
  specify the directory with your fonts in the constant FONT_DIR defined below. 
  On macOS (cocoa), text output is not supported at all.

  By default, system colors are not supported and usually rendered as black.
  Therefore, use clWhite instead of clWindow for Chart.BackColor etc.
  If you absolute need system color support assign the function
  ChartColorSysToFPColor (in unit TADrawerCanvas) to property DoChartColorToFPColor
  of the chart's drawer:

    Chart.Drawer.DoChartColorToFPColor := @ChartColorSysToFPColor.

  Note, however, that this pulls in the LCL.
  }

unit Main;

{$mode objfpc}{$H+}

{.$DEFINE USE_SYSTEM_COLORS}

interface

uses
  Classes, ExtCtrls, StdCtrls, SysUtils, FileUtil, Forms, Controls,
  Graphics, FPCanvas, Dialogs, 
  Agg_LCL, Agg_FPImage,
  TAGraph, TAGUIConnectorAggPas, TASeries, TASources, TADrawerAggPas, TADrawUtils;

type

  { TMainForm }

  TMainForm = class(TForm)
    Bevel1: TBevel;
    cbAggPas: TCheckBox;
    Chart: TChart;
    ChartConstantLine: TConstantLine;
    ChartLineSeries: TLineSeries;
    ChartPieSeries: TPieSeries;
    ChartGUIConnectorAggPas: TChartGUIConnectorAggPas;
    ChartPaintBox: TPaintBox;
    BottomPanel: TPanel;
    RandomChartSource: TRandomChartSource;
    procedure cbAggPasClick(Sender: TObject);
    procedure Chart1AfterPaint(ASender: TChart);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ChartPaintBoxPaint(Sender: TObject);
  private
    FAggCanvas: TAggLCLCanvas;
    FBmp: TBitmap;
  end;

var
  MainForm: TMainForm; 

implementation

{$R *.lfm}

uses
  TAChartUtils, TADrawerCanvas;

{$IF DEFINED(LCLGtk2) or DEFINED(LCLGtk3) or DEFINED(LCLQt) or DEFINED(LCLQt5)}
const
  FONT_DIR = '/usr/share/fonts/truetype/';
{$ENDIF}

{ TMainForm }

procedure TMainForm.cbAggPasClick(Sender: TObject);
begin
  if cbAggPas.Checked then
  begin
    Chart.GUIConnector := ChartGUIConnectorAggPas;
    {$IFDEF USE_SYSTEM_COLORS}
    Chart.Drawer.DoChartColorToFPColor := @ChartColorSysToFPColor;
    {$ENDIF}
  end else
    Chart.GUIConnector := nil;
end;

procedure TMainForm.Chart1AfterPaint(ASender: TChart);
begin
  Unused(ASender);
  ChartPaintBox.Invalidate;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  FBmp := TBitmap.Create;
  FAggCanvas := TAggLCLCanvas.Create;
  FAggCanvas.Image.PixelFormat := afpimRGBA32;
  
  {$IFDEF LCLWin32}
  ChartLineSeries.Transparency := 128;
  {$ENDIF}
  {$IF DEFINED(LCLGtk2) or DEFINED(LCLGtk3) or DEFINED(LCLQt) or DEFINED(LCLQt5)}
  ChartGUIConnectorAggPas.FontDir := FONT_DIR;
  {$ENDIF}

  {$IFDEF USE_SYSTEM_COLORS}
  Chart.Color := clForm;
  Chart.BackColor := clWindow;
  Chart.Legend.BackgroundBrush.Color := clDefault;
  Chart.Frame.Color := clBtnShadow;
  {$ENDIF}
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  FAggCanvas.Free;
  FBmp.Free;
end;

procedure TMainForm.ChartPaintBoxPaint(Sender: TObject);
var
  d: IChartDrawer;
begin
  FAggCanvas.Width := ChartPaintBox.Width;
  FAggCanvas.Height := ChartPaintBox.Height;

  Chart.DisableRedrawing;
  Chart.Title.Text.Text := 'AggPas';
  d := TAggPasDrawer.Create(FAggCanvas);
  d.DoGetFontOrientation := @CanvasGetFontOrientationFunc;
  // The next instruction is required if system colors are used
  {$IFDEF USE_SYSTEM_COLORS}
  d.DoChartColorToFPColor := @ChartColorSysToFPColor;
  {$ENDIF}
  {$IF DEFINED(LCLGtk2) or DEFINED(LCLGtk3) or DEFINED(LCLQt) or DEFINED(LCLQt5)}
  (d as TAggPasDrawer).FontDir := FONT_DIR;
  {$ENDIF}
  Chart.Draw(d, ChartPaintBox.Canvas.ClipRect);
  Chart.Title.Text.Text := 'Standard';
  Chart.EnableRedrawing;

  // On Windows the order of red, green and blue is blue-green-red, while on 
  // others it is red-green-blue. In principle, AggPas can handle this, but
  // since it is not maintained ATM, I chose the "easy way" to swap the red
  // and blue bytes.
  {$IF DEFINED(LCLGtk2) or DEFINED(LCLGtk3) or DEFINED(LCLQt) or DEFINED(LCLQt5)}
  SwapRedBlue(FAggCanvas.Image.IntfImg);
  {$ENDIF}
  FBmp.LoadFromIntfImage(FAggCanvas.Image.IntfImg);
  ChartPaintBox.Canvas.Draw(0, 0, FBmp);
end;

end.