Charts (Delphi)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\Demo\Delphi\Modules\20.Reports\70.Charts and also at https://github.com/tmssoftware/TMS-FlexCel.VCL-demos/tree/master/Delphi/Modules/20.Reports/70.Charts
Overview
Some examples of how to create Charts with FlexCel.
Concepts
Charts might be difficult: Sometimes Excel goes out of its way guessing what you want to do and what are the cells you want to use. While this is very good in normal situations (Excel almost always gets it right), it gets difficult when you are trying to define a chart out of <#xx.yy> fields. So a nice trick is to write two rows of real data first, define the chart, and after that substitute the first row of real data with <#...> fields. You can leave the second with the test data, because as long as you use x-ranges, it will be deleted.
You can't use a <#delete row> tag to remove the extra row. <#delete row> tags are executed from bottom to top, at the same time that the insertions are made. So, the row will be deleted before the actual data is inserted and the range will not expand. To solve this, we now have x-ranges. If you put "X" as the last char on a named range (as in "_Products_X" on this demo), the range will work as a normal one, but after being inserted it will delete the next empty row. (or column if it is a column range)
Usually when you create a chart, series remain fixed and the data range expands. But some special charts might need that the data range is fixed and new series are created. For those cases, you can use swap series> tags. Look at the example Charts With Dynamic Series
FlexCelReport will not replace <#tags> inside the main title or the x/y axis titles. But you can drop a textbox inside the chart, and this text will be replaced.
Try and open the generated xls file with Custom Preview demo. You will see it looks very similar to the the way Excel displays it.
Files
UMainForm.pas
unit UMainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
FlexCel.VCLSupport, FlexCel.Core, FlexCel.XlsAdapter, FlexCel.Report, FlexCel.Render,
{$if CompilerVersion >= 23.0} System.UITypes, {$IFEND}
ShellApi,
Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type
TMainForm = class(TForm)
btnCancel: TButton;
btnGo: TButton;
SaveDialog: TSaveDialog;
Label1: TLabel;
procedure btnCancelClick(Sender: TObject);
procedure btnGoClick(Sender: TObject);
private
procedure RunReport;
function GetDataPath: string;
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses IOUtils, DemoData;
{$R *.dfm}
procedure TMainForm.btnCancelClick(Sender: TObject);
begin
Close;
end;
procedure TMainForm.btnGoClick(Sender: TObject);
begin
RunReport;
end;
function TMainForm.GetDataPath: string;
begin
Result := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..');
end;
procedure TMainForm.RunReport;
var
Report: TFlexCelReport;
begin
if not SaveDialog.Execute then exit;
Report := TFlexCelReport.Create(true);
try
Report.AddTable(DemoTables);
Report.SetValue('Date', Now);
Report.Run(
TPath.Combine(GetDataPath, 'Charts.template.xls'),
SaveDialog.FileName);
finally
Report.Free;
end;
if MessageDlg('Do you want to open the generated file?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
ShellExecute(0, 'open', PCHAR(SaveDialog.FileName), nil, nil, SW_SHOWNORMAL);
end;
end;
end.