Table of Contents

TFlexCelReport.CustomizeChart Event

Fires for each chart in each sheet, after the report has been generated. Allows to do custom modifications to the charts, like for example setting series colors.

Syntax

Unit: FlexCel.Report

property TFlexCelReport.CustomizeChart: TCustomizeChartEventHandler

Examples

Imagine you want to set the maximum of the x-axis so it ends exactly at the maximum point in your chart.

You can use the following code to do it:

procedure CustomizeAxis(const sender: TObject; const args: TCustomizeChartEventArgs);
var
  axis: TChartAxisArray;
  i: integer;
begin
  axis := args.Chart.GetChartAxis;
  for i := 0 to Length(axis) - 1 do
  begin
    if axis[i].CategoryAxis <> nil then
    begin
       //MaxValueForAxis is a method that returns the value we want to set for the Axis.
      ((axis[i].CategoryAxis as TValueAxis)).Max := MaxValueForAxis;
      ((axis[i].CategoryAxis as TValueAxis)).AxisOptions:= ((axis[i].CategoryAxis as TValueAxis)).AxisOptions - [TValueAxisOptions.AutoMax];
    end;

    args.Chart.SetChartAxis(axis[i]);
  end;
end;

procedure CustomizeChartAxis;
var
  flexcelReport: TFlexCelReport;
begin
  flexcelReport := TFlexCelReport.Create(true);
  try
    flexcelReport.CustomizeChart:= CustomizeAxis;
    flexcelReport.Run('Template.xlsx', 'Result.xlsx');
  finally
    flexcelReport.Free;
  end;
end;

Imagine you want to set each series color to a specific value.

You can use the following code to do it:

procedure CustomizeSeriesColors(const sender: TObject; const e: TCustomizeChartEventArgs);
var
  subChart: integer;
  series: integer;
  seriesDef: IChartSeries;
  seriesOptions: TChartSeriesOptions;
begin
  if e.ChartName = 'ChartIWantToModify' then
  begin
     //In this event we will set the colors of the series depending on the product.
     //Let's imagine each product has an associated color that we want to use for its series.
     //The method ColorForProducts returns the color we want to give to that series.
    for subChart := 1 to e.Chart.SubchartCount do
    begin
      for series := 1 to e.Chart.SeriesInSubchart(subChart) do
      begin
        seriesDef := e.Chart.GetSeriesInSubchart(subChart, series, true, true, true);
        seriesOptions := seriesDef.Options[-1];
        seriesOptions.FillOptions := TChartSeriesFillOptions_Create(TShapeFill_Create(true, TSolidFill_Create(ColorForProduct(series))), nil, false, false);        seriesDef.Options[-1] := seriesOptions;
        seriesOptions.LineOptions := TChartSeriesLineOptions_Create(TShapeLine_Create(true, TLineStyle_Create(TSolidFill_Create(ColorForProduct(series)))), false);
        e.Chart.SetSeriesInSubchart(subChart, series, seriesDef);
      end;
    end;
  end;
end;

procedure CustomizeChartSeriesColors;
var
  flexcelReport: TFlexCelReport;
begin
  flexcelReport := TFlexCelReport.Create(true);
  try
    flexcelReport.CustomizeChart:= CustomizeSeriesColors;
    flexcelReport.Run('Template.xlsx', 'Result.xlsx');
  finally
    flexcelReport.Free;
  end;
end;

See also