HotXLS Docs

TXLSXChart / TXLSXCharts classes

Drawing-anchored chart on the XLSX facade. The collection lives on TXLSXWorksheet.Charts; anchors are expressed as a from / to cell rectangle. SaveAs writes each chart to xl/charts/chartN.xml and binds it through the shared worksheet drawing part; Open parses the chart definition (type, title, axis titles, series) back into the collection.

Declaration

type
  TXLSXChartType = (
    xlsxChartColumn,
    xlsxChartBar,
    xlsxChartLine,
    xlsxChartPie);

  TXLSXChartSeries = class
    property Name: WideString;
    property CategoriesRange: WideString;
    property ValuesRange: WideString;
  end;

  TXLSXChart = class
    function AddSeries(const AName, ACategories, AValues: WideString): TXLSXChartSeries;
    procedure ClearSeries;
    property Id: Integer;             // workbook-global; assigned by SaveAs
    property ChartType: TXLSXChartType;
    property Title: WideString;
    property CategoryAxisTitle: WideString;
    property ValueAxisTitle: WideString;
    property FromRow, FromCol: Integer;     // anchor top-left (1-based)
    property ToRow, ToCol: Integer;         // anchor bottom-right (1-based)
    property SeriesCount: Integer;
    property Series[Index: Integer]: TXLSXChartSeries;
  end;

  TXLSXCharts = class
    function Add: TXLSXChart; overload;
    function Add(AChartType: TXLSXChartType; const ATitle: WideString;
      AFromRow, AFromCol, AToRow, AToCol: Integer): TXLSXChart; overload;
    procedure Clear;
    property Count: Integer;
    property Items[Index: Integer]: TXLSXChart; default;
  end;

Worksheet shortcut

TXLSXWorksheet exposes AddChart(Type, Title, FromRow, FromCol, ToRow, ToCol) which forwards to Charts.Add and returns the new chart.

Chart sheets (full-page charts)

To put a chart on its own dedicated sheet (no cell grid), use Workbook.AddChartSheet(Name, ChartType, Title). That helper creates a worksheet, sets IsChartSheet := True, and seeds Charts[0] with a sensible default anchor. SaveAs routes such sheets to xl/chartsheets/sheetN.xml.

Example

var
  ws: TXLSXWorksheet;
  chart: TXLSXChart;
begin
  // Header + data the chart will reference.
  ws := wb.Sheets.Add('Sales');
  ws.Cells.Item[1, 1].Value := 'Quarter';
  ws.Cells.Item[1, 2].Value := 'Units';
  ws.Cells.Item[2, 1].Value := 'Q1'; ws.Cells.Item[2, 2].Value := 120;
  ws.Cells.Item[3, 1].Value := 'Q2'; ws.Cells.Item[3, 2].Value := 150;
  ws.Cells.Item[4, 1].Value := 'Q3'; ws.Cells.Item[4, 2].Value := 180;
  ws.Cells.Item[5, 1].Value := 'Q4'; ws.Cells.Item[5, 2].Value := 210;

  chart := ws.AddChart(xlsxChartColumn, 'Quarterly sales',
    {from} 2, 4, {to} 18, 12);
  chart.CategoryAxisTitle := 'Quarter';
  chart.ValueAxisTitle    := 'Units';
  chart.AddSeries('Units', 'Sales!$A$2:$A$5', 'Sales!$B$2:$B$5');
end;