Skip to content

feat(xlsx): charts drawn as SVG, their values from the cells, in the file's look - #12

Open
mkroemer wants to merge 13 commits into
p10node:mainfrom
mkroemer:xlsx-charts
Open

mkroemer wants to merge 13 commits into
p10node:mainfrom
mkroemer:xlsx-charts

Conversation

@mkroemer

@mkroemer mkroemer commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

This draws a spreadsheet's charts instead of the placeholder box, from the live cells, in the look the file gives them. It is large (about 2,600 lines, 515 of them tests), but nearly all of it is two new files: src/core/xlsx/chart.ts reads a chart part into a model in the worker, and src/core/xlsx/chart-svg.ts draws that model as a self-contained SVG with no DOM, shown over the cells like a picture. The commits split it into the reader and drawing, review fixes, the looks, the last four kinds and the axis work, if you'd rather review it in steps; I can also split it into separate PRs along those lines.

  • Kinds: bar and column (clustered, stacked, 100%, upright or horizontal), line (markers, smoothing), area, pie and doughnut (exploded slices, first-slice angle, one ring per series), scatter, bubble (by area or width, bubbleScale, negative bubbles), radar (lines, markers, filled), stock (high-low lines, up-down bars), surface seen from above as Excel's contour chart or as a wireframe, and combinations on primary and secondary axes, each with its own X axis where the file gives one; 3D charts as their flat form. Only the chartex kinds (waterfall, treemap, ...) keep the placeholder.
  • Values: series references are read from the workbook's cells, loading a sheet not shown yet, so charts follow edits and recalculation. Dates are read as their serial numbers. A defined name, another workbook or an unreadable reference falls back to the chart's cached values. Hidden rows and columns are left out when plotVisOnly is set, and a point with its own look keeps it when rows before it are hidden. A whole-column reference is cut to the filled range; point counts are capped, so a huge range can't stall the worker.
  • Empty cells: shown as gaps, zero or bridged, as dispBlanksAs says; a range keeps its trailing empty cells, and in a scatter chart a point missing its X value is not drawn.
  • Axes: Excel-like automatic bounds (zero kept, steps of 1/2/5, a 100% axis ending at 100%, equal values still get a span); fixed min, max and major unit, also on one side only; reversed and log axes; date axes spaced by date; where the axes cross (crosses, crossesAt); gridlines; tick and data labels in their number formats, the axis's own or the source cells', tilted where they don't fit flat and kept inside the chart at the edges.
  • Layout: the title (Excel's automatic one included, and a title taken from a cell); the legend (positions, overlay, deleted entries); manual plot layout.
  • Look: series, point, chart area and plot area fills as colors, gradients (linear, radial) or pattern fills (the DrawingML presets); line colors, widths and dashes; markers, per series or from the chart; the text size, weight and color of the title, axes, legend and data labels; gridline and axis lines or none; the built-in chart style (c:style, and c14:style with its Excel 2010 numbering: its palettes, tinted plot areas and dark backgrounds, not bevels and shadows). Where the file gives nothing, the grays are shades of the theme's text color, as Excel derives them.
  • Pie and doughnut size a negative value by its magnitude, as Excel does, and keep the sign in its label. Everything plotted is clipped to the plot area, so a fixed axis range can't run over the title and legend; data labels sit above the clip so they stay readable. Each chart is its own <img>, so gradient, pattern and clip ids only need to be unique within one picture.

test/xlsx-chart.test.ts (39 tests) covers series read from the cells and the cache fallback, hidden rows and points, a chart read again after an edit, large and whole-column ranges, dates, Excel's axis bounds, equal values, one-sided bounds and the 100% axis, axis crossings and date axes, empty cells, the drawings of each kind, cell titles, negative pie values, scatter gaps, clipping, gradient and pattern fills, text and line colors, the plot area fill, the theme's grays and the built-in styles. bun run typecheck and bun test pass locally (apart from the Windows-only failures main has too); the e2e suite runs in CI.

🤖 Generated with Claude Code

markus-kroemer and others added 6 commits November 14, 2026 09:51
A sheet's charts were placeholders. Now src/core/xlsx/chart.ts reads a
chart part into a model in the worker, and src/core/xlsx/chart-svg.ts
draws it as a self-contained SVG (DOM-free), shown over the cells like a
picture.

- Kinds: bar and column (clustered, stacked, 100%, upright or
  horizontal), line (markers, smoothing, gaps for empty cells), area,
  pie and doughnut (exploded slices, first-slice angle), scatter, and
  combinations on primary and secondary axes; 3D charts as their flat
  form. Radar, bubble, stock, surface and chartex keep the placeholder.
- Values: series references are read from the workbook's cells, loading
  a sheet not shown yet, so charts follow edits (the view reads them
  again after an edit result). A defined name, another workbook or an
  unreadable reference falls back to the chart's numCache / strCache.
  Hidden rows and columns are left out when plotVisOnly is set.
- Look: Excel-like automatic axis bounds (zero kept, steps of 1/2/5),
  fixed min/max/major unit, reversed and log axes, gridlines, tick and
  data labels in their number formats, tilted category labels where they
  do not fit flat, title (Excel's automatic one included), legend
  (positions, overlay, deleted entries), theme accents for series
  without colors of their own, manual plot layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pie negatives, cell titles

- scatter: a point whose X value is missing is not drawn (it was placed at
  its ordinal position); value labels follow their own points when a Y
  value is missing
- bars, areas, lines and points are clipped to the plot area, so a fixed
  axis range does not let them run over the title and legend
- pie and doughnut: a negative value is a slice of its magnitude, as in
  Excel; its value label keeps the sign
- a title whose text is a cell (`c:tx/c:strRef`) shows that cell's text,
  else its cached text, before the automatic title

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… row stays in it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The automatic bounds add a step above a maximum that falls on a
gridline, so a 100% stacked axis ran to 120%. An axis carrying only
100% stacks now ends at 100% (and -100% with negatives), as Excel's does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- gradient fills (linear and radial) and pattern fills (DrawingML presets)
  for series, points, the chart area and the plot area
- the text size, weight and color of axis labels, legend and data labels
- gridline and axis lines: color, width, dash, or none
- the plot area's fill and border
- the built-in chart style (c:style, c14:style): gray, accent and
  one-accent palettes, tinted plot areas, dark backgrounds
- default grays (text, gridlines, axis lines) as shades of the theme's
  text color rather than fixed values

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kinds that were placeholders until now:
- radar: lines, lines with markers, filled; a spoke per category, rings
  at the value axis' ticks
- bubble: sizes read from the cells, by area or by width, scaled by
  bubbleScale; negative bubbles as outlines where the chart shows them;
  the axes leave the bubbles room
- stock: high-low lines and up-down bars, the series over them
- surface (2D and 3D): seen from above as Excel's contour chart, its
  value bands in the legend; or the wireframe

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mkroemer mkroemer changed the title feat(xlsx): charts drawn as SVG, their values from the cells feat(xlsx): charts drawn as SVG, their values from the cells, in the file's look Nov 15, 2026

@pierreneter pierreneter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, it is a lot of careful work, and the important part holds: with hostile titles, colours, dashes and formats across all kinds, the SVG stays well-formed, nothing is fetched, and it is shown as a blob in an <img>.

I can't merge it yet, because a crafted file can crash or stall the viewer. renderChart runs inside the grid's render and there is no error boundary, so a throw takes the whole view down:

  1. src/core/xlsx/chart.ts:398-399: new Array(ptCount) has no cap. A 3.9 KB file with <c:ptCount val="50000000"/> takes 3.3 s and 3.5 GB in the worker.
  2. src/core/xlsx/chart-svg.ts:40: DASH[d] on a plain object. <a:prstDash val="constructor"/> throws DASH[d].map is not a function. Same at chart.ts:613 (GROUPS[g.n]), where <c:constructor> in the plot area loses all of the sheet's drawings. dict() from src/core/dict avoids both.
  3. chart-svg.ts:317-319: an empty <c:pieChart/> beside a bar group with series throws on s.values.
  4. chart-svg.ts:408-409 and the other Math.min(...all) / Math.max(...all) calls: an ordinary line chart of 3 series x 45,000 rows overflows the stack in V8. Please use loops.
  5. No cap on the total: MAX_POINTS is per area, so one reference of 200 areas gives 10 million values (2.3 s, 1.3 GB). 200 anchors on one chart part read it 200 times. The series count has no cap either.

What I'd like to see: a try / catch around renderChart that falls back to the placeholder box, caps on ptCount, total points and series, a cache per chart part, and tests with hostile input for each case above.

Non-blocking: a 3 x 50,000 point chart is a 19 MB SVG rebuilt in render after every edit; a control character such as &#1; in a title gives invalid XML and a broken image; the XLSX row in docs/features.md lost its alignment.

#11 is approved, so this can be rebased onto main once that is in.

markus-kroemer and others added 7 commits November 19, 2026 11:32
Charts are read again after an edit beside upstream's sheet size limits for drawings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The features table keeps upstream's function count and this branch's charts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… values, scatter without X, doughnut rings, negative axes

- A chart's saved point count (`ptCount`) is capped before an array is
  made for it, so a small file cannot ask for a huge allocation.
- Charts read their cells once the values computed after loading are in,
  and read them again when a recalculation reports new values.
- Date cells are plotted by their serial, not as NaN from their ISO text.
- A scatter series without X values numbers its points again instead of
  drawing none.
- A doughnut draws a ring per series, the first innermost, as Excel does.
- Bars and areas on an axis that does not reach zero start at the bound
  nearest to it, so a value nearer zero is a shorter bar.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ints with their own look, chart-level markers

- Extremes of a chart's values are found by a loop, not by spreading tens
  of thousands of values into Math.min / Math.max, which exceeded the
  engine's argument limit and stopped the sheet from rendering.
- Each scatter or bubble group is placed on its own X axis's scale, and a
  log X axis is a log scale.
- Log axes take their bounds from the positive values; zero and negative
  values, which a log axis cannot place, are left out.
- With hidden rows left out, a point's own color or explosion follows
  its point; a hidden point's goes with it.
- A line chart that turns markers off, and a scatter chart of style line
  or smooth, have no markers on series without a symbol of their own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… axes and axis crossings

- A scatter or bubble X axis linked to its source is written in the X
  values' number format, not the Y values'.
- Data labels are drawn above the clipped series, so a label beside a
  point on the plot's edge stays whole.
- Lines show empty cells as the chart says (`dispBlanksAs`): a gap, zero,
  or spanned by the line.
- A date axis places its points by date, a day to a band, within its
  minimum and maximum where the file fixes them.
- The category axis crosses the value axis where the file says
  (`crosses`, `crossesAt`), and bars and areas grow from there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…shown as zero are in the axis range

A range given in full (B2:B6) keeps its size where the sheet is filled less far, so its points line up with categories read from elsewhere; only a whole column or row is cut to the filled cells. The automatic axis bounds take in the zeros a line or scatter chart draws for empty cells.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cel 2010 style numbers

- Values all alike keep zero in view, so columns of them have a height
  instead of none.
- A minimum or maximum fixed past all the values moves the other bound to
  give the axis a span, instead of a scale with no ticks.
- A c14 style number (101-148) the XML reader has already taken out of
  its AlternateContent is read as the style it stands for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants