Table of Contents

TXlsFile.GetChart Method

Returns a chart from an object position and path. If the object does not contain a chart, it returns null. Note that charts can be first-level objects (in chart sheets), or they can be embedded inside other objects, that can be themselves embedded inside other objects. So you need to recursively look inside all objects to see if there are charts anywhere.

Look at the example in this topic to see how to get all charts in a sheet.

Syntax

Unit: FlexCel.XlsAdapter

function TXlsFile.GetChart(const objectIndex: Integer; const objectPath: string): IExcelChart; override;

Parameters

<-> Parameter Type Description
const objectIndex Integer Index of the object (1-based)
const objectPath string Index to the child object where the chart is.
If it is a simple object, you can use String.Empty here, if not you need to get the ObjectPath from TExcelFile.GetObjectProperties(Integer, Boolean)
If it is "absolute"(it starts with "\"), then the path includes the objectIndex, and the objectIndex is not used. An object path of "\1\2\3" is exactly the same as using objectIndex = 1 and objectPath = "2\3"

Examples

The following example will retrieve all charts that are inserted as objects in a sheet, along with the chart sheets.

procedure ProcessAllCharts;
var
  xls: TXlsFile;
  iSheet: Int32;
  iChart: Int32;
  props: IShapeProperties;
  chart: IExcelChart;
begin
  xls := TXlsFile.Create(true);
  try
    xls.Open('filewithcharts.xls');
    for iSheet := 1 to xls.SheetCount do
    begin
      xls.ActiveSheet := iSheet;
       //Process charts embedded as objects in worksheets.
      for iChart := 1 to xls.ObjectCount do
      begin
        props := xls.GetObjectProperties(iChart, true);
        ProcessChart(xls, iChart, props);  //We need to process it even if it is not a chart, since it might be a group with a chart inside.
      end;

       //Process chart sheets.
      if xls.SheetType = TSheetType.Chart then
      begin
        chart := xls.GetChart(1, '');
        DoSomething(xls, chart);
      end;

    end;

    xls.Save('changedfilewithcharts.xls');
  finally
    xls.Free;
  end;
end;

procedure ProcessChart(const xls: TExcelFile; const iChart: Int32; const props: IShapeProperties);
var
  chart: IExcelChart;
  i: Int32;
  childProp: IShapeProperties;
begin
  if props.ObjectType = TObjectType.Chart then
  begin
    chart := xls.GetChart(iChart, props.ObjectPath);
    DoSomething(xls, chart);
  end;
  
  for i := 1 to props.ChildrenCount do
  begin
    childProp := props.Children(i);
    ProcessChart(xls, i, childProp);
  end;
end;

See also