Technical Article

Excel Sparklines in Delphi: HotXLS and the x14 Extension

A dashboard workbook with 200 product rows and twelve monthly columns needs a trend glyph at the end of every row. Built with ordinary charts, that is 200 DrawingML chart parts, each with its own XML part, relationship entry and anchor rectangle, and Excel has to lay out every one of them when the file opens. Excel's own answer is the sparkline: a mini chart painted inside a single cell, declared in a few lines of worksheet metadata. All 200 trend lines fit in one sparkline group whose members say "draw B2:M2 into N2, B3:M3 into N3" and so on down the sheet

Why are sparklines an x14 extension rather than a chart?

Sparklines live in the worksheet's extension list because they arrived with Excel 2010, after the ECMA-376 base schema had already shipped, and adding new elements to the core worksheet markup would have broken every existing reader. The extension mechanism was built for exactly this. A worksheet may end with an <extLst> element holding <ext> blocks, and each block carries a URI naming the feature inside it ([MS-XLSX] §2.3.6). Sparklines use {05C60535-1F16-4fd2-B633-F4F36F0B64E0} and the x14 namespace

The rule for readers is short: recognize the GUID and parse the block, or skip it. Excel 2007 opens a sparkline workbook cleanly and shows the monthly numbers with an empty column N. Excel 2010 and later, plus any other reader that implements the x14 sparkline extension, paint the glyphs into the host cells. That degradation is the designed behavior of the format, and it means a sparkline workbook is safe to send to someone on an older Excel

A common assumption is that the worksheet root also needs an mc:Ignorable="x14" declaration to keep old readers quiet. It does not. The ext GUID is the whole gate, and Excel's own writer declares the x14 namespace locally on the <ext> element and leaves the root alone. HotXLS writes the block the same way, so adding sparklines to a sheet leaves the <worksheet> root element exactly as it was

Worksheet extLst structure for Excel sparklines written by HotXLS: an ext block carrying the x14 sparkline URI gates rendering, with Excel 2010 and later drawing the glyphs while Excel 2007 and unknown readers skip the block
Sparklines live in an extLst block tagged with the x14 sparkline URI at the end of the worksheet part. Readers that recognize the GUID render the glyphs while older readers skip the block

This is also where sparklines part ways with real charts. A chart is a separate DrawingML part with its own rendering pipeline, covered in the companion article on charts, images, and drawings. A sparkline has no part, no anchor and no relationship. The grid draws it as part of painting the cell

Building a sparkline group in Delphi

In HotXLS the one-call path is TXLSXWorksheet.AddSparklineGroup(AType, ASourceRange, ALocation). It creates a group of the requested type, seeds ColorSeries with $FF376092 (the blue Excel uses for a new sparkline), and adds the first member, which maps a sheet-qualified source range onto a host cell. The returned TXLSXSparklineGroup takes further members through Add(AF, ASqref). Every member shares the group's type, colors and axis settings, which is exactly the shape a 200-row dashboard wants: one set of options, many rows

var
  Book: TXLSXWorkbook;
  Sheet: TXLSXWorksheet;
  Grp: TXLSXSparklineGroup;
  Row: Integer;
begin
  Book := TXLSXWorkbook.Create;
  try
    Book.Open('dashboard.xlsx');
    Sheet := Book.Sheets[1];   // the Sheets indexer is 1-based

    // One group: every product row shares type and formatting
    Grp := Sheet.AddSparklineGroup(xlsxSparklineLine,
      'Sheet1!B2:M2', 'N2');
    for Row := 3 to 201 do
      Grp.Add(Format('Sheet1!B%d:M%d', [Row, Row]),
        Format('N%d', [Row]));

    Book.SaveAs('dashboard-trends.xlsx');
  finally
    Book.Free;
  end;
end;

Two details in that loop trip people up. Book.Sheets[1] is the first worksheet, because the workbook-level indexer counts from 1; Sheets[0] returns nil and the next line fails with an access violation. And the source range has to carry its sheet name. Excel expects a sheet-qualified reference in xm:f, and a bare B2:M2 gives it no sheet to resolve against. If the sheet name contains spaces, quote it the way a formula would: 'Q3 Sales'!B2:M2

Diagram of a HotXLS sparkline group in Delphi: one TXLSXSparklineGroup fans out to per-row members such as Sheet1!B2:M2 mapped onto host cell N2, with line, column and win/loss type cards
One Delphi call creates a sparkline group and each member maps a source range onto a host cell. HotXLS offers the line, column and win/loss types Excel draws inside a single cell

TXLSXSparklineType has three values, matching the three buttons in Excel's Insert ribbon: xlsxSparklineLine for trend lines, xlsxSparklineColumn for one slim bar per value, and xlsxSparklineStacked for win/loss strips, where every value renders as an up or down block regardless of size. A member is just two strings, F for the source and Sqref for the host cell. You can also skip the helper and build a group from scratch with Sheet.SparklineGroups.Add. That group starts with every color slot at 0, so Excel picks all the colors itself

Markers, color slots and axis scaling

TXLSXSparklineGroup exposes the option set from Excel's Sparkline Tools ribbon as plain properties. Six boolean toggles pick out data points: Markers for every point on a line sparkline, plus HighPoint, LowPoint, NegativePoints, FirstPoint and LastPoint. Eight ARGB color slots pair with them: ColorSeries, ColorNegative, ColorAxis, ColorMarkers, ColorFirst, ColorLast, ColorHigh and ColorLow. A slot left at 0 is not written to the file at all, and Excel falls back to its own default for that slot. The ARGB form is the same one the rest of the library uses, so $FFD00000 is the red it would be in conditional formatting styles

Grp := Sheet.AddSparklineGroup(xlsxSparklineStacked,
  'Sheet1!B2:M2', 'N2');
Grp.NegativePoints := True;          // losing months stand out
Grp.ColorNegative := $FFD00000;
Grp.HighPoint := True;
Grp.ColorHigh := $FF00B050;
Grp.MinAxisType := xlsxSparklineAxisGroup;   // one scale for all rows
Grp.MaxAxisType := xlsxSparklineAxisGroup;
Grp.DisplayEmptyCellsAs := xlsxSparklineEmptyGap;

The axis settings decide whether the dashboard tells the truth. MinAxisType and MaxAxisType each take xlsxSparklineAxisIndividual, xlsxSparklineAxisGroup or xlsxSparklineAxisCustom, and individual is the default. Under individual scaling every sparkline stretches to fill its own cell height. A product drifting between 99 and 101 units draws a line just as jagged as one that went from 40 to 400, and a reader scanning down column N has no way to tell them apart

For any sheet where rows are meant to be compared, set both axes to group. Glyph height then means the same quantity in every row, and the flat products look flat. Individual scaling is the right choice only when each row is read on its own, such as a per-account health strip where the shape matters and the magnitude is printed in the next column anyway

DisplayEmptyCellsAs handles holes in the source data. xlsxSparklineEmptyGap breaks the line at a blank month, xlsxSparklineEmptyZero drops it to zero, and xlsxSparklineEmptySpan connects the neighbors across the hole. Zero is the schema default, and for sales data it is usually wrong: a month that has not been reported yet plunges to the axis and looks like a collapse. Gap shows the missing month as missing

Option surface of the HotXLS TXLSXSparklineGroup in Delphi: six point toggles, eight ARGB color slots where zero means omitted, and the individual, group and custom axis scaling modes
The typed group exposes the full Sparkline Tools surface: six point toggles plus eight ARGB color slots. Axis scaling can be individual, group or custom, and blank source cells can gap, zero or span

What the saved worksheet XML looks like

On save, each group becomes an x14:sparklineGroup element. The type and the point toggles go into attributes, the non-zero color slots become child elements such as <x14:colorNegative rgb="FFD00000"/>, and an x14:sparklines list holds one x14:sparkline per member, each pairing an xm:f source with an xm:sqref host cell. All groups on a sheet go into a single <ext> block with the sparkline URI, at the end of the worksheet part. A sheet with no sparklines and no other extension content gets no <extLst> at all, the same conservative approach described in the article on lossless round-trip infrastructure

Attributes that sit at their schema default are left out. type appears only for column and stacked groups, because line is the default. minAxisType and maxAxisType appear only when they are not individual, and displayEmptyCellsAs only when it is not zero. If you diff HotXLS output against a file Excel saved, missing attributes in those places mean "default", and both files describe the same group

Line weight is the attribute that catches people hand-checking the XML. Several secondary references list 1.25 points as the default for lineWeight, but [MS-XLSX] defines CT_SparklineGroup@lineWeight with a default of 0.75, which is also the weight Excel's ribbon applies. TXLSXSparklineGroup.LineWeight starts at 0.75, and the attribute is written only when the value differs. A writer that trusts the 1.25 figure and drops the attribute at 1.25 gets lines Excel draws at 0.75, and a reader that fills in 1.25 for a missing attribute reports every Excel-made sparkline as heavier than it really is. Neither mistake shows up until someone puts two files side by side and squints at column N

What happens to sparklines when rows move or sheets are copied?

Sparkline references follow structural edits. TXLSXWorksheet.InsertRows, DeleteRows, InsertCols and DeleteCols rewrite every member's source range on every sheet that points at the edited sheet, and move the host cells of members that live on the edited sheet itself. Delete row 50 and the sparkline that sat in N51 now sits in N50 and reads B50:M50. Delete a row that contains a host cell and that member is removed; a group whose last member goes is removed with it, so an empty group never reaches the file

Copying a sheet into another workbook is where references need your attention. TXLSXSheets.AddCopy clones every sparkline group with its options and members, but the F strings are copied as text. A member that read Template!B2:M2 in the source workbook still reads Template!B2:M2 in the destination, so it draws from whatever sheet carries that name there, or from nothing. That matches how HotXLS treats other textual sheet references on copy, such as protected-range addresses. Cell formulas follow their own rebinding rules, described in the article on cross-workbook copy and formula rebinding

For template-driven reports, then, repoint the members right after the copy. The loop is short, since groups and members are both plain 0-based indexed lists

var
  Report: TXLSXWorkbook;
  Region: TXLSXWorksheet;
  Grp: TXLSXSparklineGroup;
  I, J: Integer;
begin
  // Template is a sheet in another, already opened workbook
  Region := Report.Sheets.AddCopy(Template, 'North');
  for I := 0 to Region.SparklineGroups.Count - 1 do
  begin
    Grp := Region.SparklineGroups[I];
    for J := 0 to Grp.Count - 1 do
      Grp[J].F := StringReplace(Grp[J].F, 'Template!', 'North!',
        [rfIgnoreCase]);
  end;
end;

The replacement assumes the template's source ranges all point at the template sheet itself, which is the normal layout for a self-contained report page. If a template pulls its data from a separate data sheet, map that name instead. AddCopy carries sparkline groups when the destination is another workbook; for a copy inside the same workbook, recreate the groups on the new sheet with AddSparklineGroup

Workbooks that carry other extensions

A worksheet extLst written by Excel rarely holds sparklines alone. The same list can carry blocks for slicers, extended data validation, data bars and features newer than anything HotXLS models with typed classes. On open, HotXLS parses the sparkline block into SparklineGroups, routes the extensions it does model to their own objects, and captures every other <ext> block in Worksheet.RawWorksheetExts, a read-only list with one XML fragment per block

Those captured blocks are written back on save after the sparkline block, they travel with a sheet copied through AddCopy, and their cell references are shifted by the same row and column edits that move sparklines. The fragments are rebuilt from the parsed elements, so a text diff against the original shows equivalent markup with possibly different formatting. In practice this means opening a customer's workbook, adding a column of trend glyphs and saving it again leaves the extensions you never touched in place

Sparkline groups, along with the rest of the XLSX engine shown here, are part of the HotXLS Delphi Excel Component for Delphi and C++Builder, with the full Sparkline Tools option set available on TXLSXSparklineGroup