Table of Contents

TXlsFile.ChartCount Property

Returns the count of charts on this sheet. Please take note that this method will not return the number of embedded objects with charts inside in a sheet, but just the number of charts in the sheet. In simpler terms, this method will return 0 for all worksheets, and 1 for all chart sheets. This is not a very useful method, but it needs to be this way to be consistent with TExcelFile.GetChart. So, looping like this:

  for i := 1 to xls.ChartCount do
  begin
    xls.GetChart(i, '');
  end;

will loop 0 times for worksheets and 1 time for workbooks.

Syntax

Unit: FlexCel.XlsAdapter

property TXlsFile.ChartCount: Integer

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