HotXLS 文档

AddChartSheet 方法

Programmatically appends a BIFF8 chart sheet to the workbook, returning the new TXLSWorksheet 标签页。此前,图表工作表在加载-保存往返中只能以不透明方式经由 保留 TXLSCustomChart; this method writes a complete chart substream directly into SaveAs(xlExcel97)。Excel 会把结果作为所请求类型的图表标签页打开。v2.41.0 起可用(Roadmap #10 Phase 1);系列数组重载在 v2.42.0 跟进

TXLSChartType enumeration

Selects the chart kind. Re-exported from lxHandle so user code does not need to import the chart-builder unit directly
type
  TXLSChartType = (
    xlsChartTypeColumn,
    xlsChartTypeBar,
    xlsChartTypeLine,
    xlsChartTypePie);

TXLSChartSeriesInfo record

Carries a single series declaration for the 6-parameter overload. Categories and Values are A1-range strings (e.g. 'Sheet1!$B$2:$B$5')
type
  TXLSChartSeriesInfo = record
    Name        : WideString;
    Categories  : WideString;
    Values      : WideString;
  end;

语法

Five overloads cover progressively richer chart-sheet definitions:
// 1) v2.41.0 — name only; default column chart, no titles, no series.
function AddChartSheet(const Name: WideString): TXLSWorksheet;

// 2) v2.41.0 — name + chart type.
function AddChartSheet(
  const Name : WideString;
  ChartType   : TXLSChartType): TXLSWorksheet;

// 3) v2.41.0 — + chart title.
function AddChartSheet(
  const Name : WideString;
  ChartType   : TXLSChartType;
  const Title: WideString): TXLSWorksheet;

// 4) v2.41.0 — + category-axis title + value-axis title.
function AddChartSheet(
  const Name       : WideString;
  ChartType         : TXLSChartType;
  const Title      : WideString;
  const CatAxisTitle: WideString;
  const ValAxisTitle: WideString): TXLSWorksheet;

// 5) v2.42.0 — + array of series (Name + Categories range + Values range).
function AddChartSheet(
  const Name        : WideString;
  ChartType          : TXLSChartType;
  const Title       : WideString;
  const CatAxisTitle: WideString;
  const ValAxisTitle: WideString;
  const Series      : array of TXLSChartSeriesInfo): TXLSWorksheet;

备注

发出的图表子流覆盖规范要求的 BIFF8 框架 — BOF (dt=$0020)、Chart / PlotGrowth / Frame / Series / SheetProperties / AxesUsed / AxisParent(POS + 分类轴 + 数值轴 + PlotArea + Frame + ChartFormat + 图表类型记录 + ChartFormatLink + Legend)、EOF。既有的图表工作表往返经由 TXLSCustomChart is unchanged

Series name binding (v2.42.0): each non-empty Series[i].Name is written into the chart substream as a literal series label via a tStr PTG token on the series-name BRAI (id=2)。Excel 会在图例和系列选择器中使用该标签,多系列图表因此显示正确的标题,而不是默认的 "Series1 / Series2" 占位符

Series range binding (v2.47.0): starting in v2.47.0, the Categories and Values A1-range strings are compiled into real tArea3D 的 BRAI 内单元格范围引用,经由公式编译器和工作簿 EXTERNSHEET 引用表完成。v2.47.0 之前,这两个字段在 API 上可接受,但发出的是 cce=0 占位符,因此渲染出的图表为空。v2.42.0 的既有调用方若将 Categories / Values empty are unaffected

The returned TXLSWorksheet appears in the Sheets 集合,并在保存时写入一个图表工作表 boundsheet 条目。结果上的单元格级方法不受支持 — 宿主工作表是图表容器,不是数据表

示例

此示例构建一个带数据表的工作簿,并让一个柱形图绑定到该数据表的两列

var
  Series: array of TXLSChartSeriesInfo;
begin
  // Populate cells on Sheet1.
  Workbook.Sheets[1].Name := 'Data';
  // ... fill A1:B5 with categories + values ...

  SetLength(Series, 1);
  Series[0].Name       := 'Quarterly Revenue';
  Series[0].Categories := 'Data!$A$2:$A$5';
  Series[0].Values     := 'Data!$B$2:$B$5';

  Workbook.Sheets.AddChartSheet(
    'Revenue',
    xlsChartTypeColumn,
    'Quarterly Revenue',
    'Quarter',
    'USD',
    Series);

  Workbook.SaveAs('Revenue.xls', xlExcel97);
end;

Limitations

各图表类型的格式细节(分类间距、条形重叠、饼图圆孔大小、标记样式)仍按图表构建器的默认值输出 — 尚未在 API 上公开。用 Excel 保存后重新打开可保留图表工作表的存在、类型、标题、坐标轴标题、系列标题,以及(从 v2.47.0 起)系列数据绑定

请参阅