Charts
XlsxDocumentSheet.Charts lists a sheet's charts as their parts state them: the type, as Excel names it, the title, each series with the ranges it reads, the axes, the legend, the chart style and where the chart is. A chart is made of a range as Excel makes one from a selection, changed in place, and removed; a chart sheet is added. Measured against Excel: every type and series formula as Excel's object model lists it, for Excel's charts and for those made here. A chart that is read and not changed is saved as the bytes it was.
dotnet add package DocWright.Spreadsheet
List a sheet's charts
using DocWright.Spreadsheet;
XlsxDocument workbook = XlsxDocument.Open(File.OpenRead("sales-charts.xlsx"));
XlsxDocumentSheet sheet = workbook.FindSheet("Charts")!;
Console.WriteLine($"{sheet.Charts.Count} charts");
foreach (XlsxChart chart in sheet.Charts.Take(2))
{
Console.WriteLine($"{chart.Name}: {chart.Type}, title \"{chart.Title}\", at {chart.Anchor}");
foreach (XlsxChartSeries series in chart.Series)
{
Console.WriteLine($" {series.Name}: values {series.Values}, categories {series.Categories}");
}
}
Type follows Excel's XlChartType names (ColumnClustered, LineMarkers, XYScatterSmooth, Pie3D...). A series' Values and Categories are the ranges it reads (a scatter's y and x values); Name is the name the chart shows, and NameReference the cell it is read from. A chart Excel titles itself shows the one series' name, or "Chart Title".
Combinations and secondary axes
A chart with several types in one plot area is a Combination; each series says its own type and the axes it is drawn against:
using DocWright.Spreadsheet;
XlsxChart combo = sheet.Charts.Find("combo-secondary")!;
foreach (XlsxChartSeries series in combo.Series)
{
Console.WriteLine($"{series} is {series.Type} on the {series.Axis} axes");
}
Console.WriteLine(string.Join(", ", combo.Axes.Select(a => $"{a.Kind} {a.Group} {a.Position}")));
The whole run prints:
30 charts
column-clustered: ColumnClustered, title "Chart Title", at TwoCell A1 +127000 +0 to H15 +431800 +76200
North: values Data!$B$2:$B$6, categories Data!$A$2:$A$6
South: values Data!$C$2:$C$6, categories Data!$A$2:$A$6
West: values Data!$D$2:$D$6, categories Data!$A$2:$A$6
column-stacked: ColumnStacked, title "Chart Title", at TwoCell A21 +127000 +0 to H35 +431800 +76200
North: values Data!$B$2:$B$6, categories Data!$A$2:$A$6
South: values Data!$C$2:$C$6, categories Data!$A$2:$A$6
West: values Data!$D$2:$D$6, categories Data!$A$2:$A$6
=SERIES(Data!$B$1,Data!$A$2:$A$6,Data!$B$2:$B$6,1) is ColumnClustered on the Primary axes
=SERIES(Data!$C$1,Data!$A$2:$A$6,Data!$C$2:$C$6,2) is ColumnClustered on the Primary axes
=SERIES(Data!$D$1,Data!$A$2:$A$6,Data!$D$2:$D$6,3) is Line on the Secondary axes
Category Primary Bottom, Value Primary Left, Value Secondary Right, Category Secondary Bottom
Series references follow rows and columns inserted and deleted and sheets renamed, as every formula does.
Make a chart
using DocWright.Spreadsheet;
// As Excel makes a chart from a selection: names from row 1, categories from column A.
XlsxChart chart = sheet.Charts.Add(XlsxChartType.ColumnClustered, new XlsxCellRange(1, 1, 5, 3), "F2",
new XlsxChartOptions { Title = "Sales by month" });
foreach (XlsxChartSeries series in chart.Series)
{
Console.WriteLine(series);
}
XlsxDocumentSheet share = workbook.AddChartSheet("Share", XlsxChartType.Pie, sheet, new XlsxCellRange(1, 1, 5, 2));
The series come from the range by Excel's rule: down its columns when it has more rows than columns, the first row's text as their names, the first column's text as their categories (both when the top-left cell is empty), a scatter's first column as its x values. The chart has a title Excel makes itself and, with more than one series, a legend below, as Excel's has; its caches hold the cells' values, so a viewer that does not calculate shows it. Every type is made but the stock, surface, combination and 2016 ones.
Change and remove a chart
using DocWright.Spreadsheet;
chart.AddSeries("Sales!$D$2:$D$5", "Sales!$A$2:$A$5", "Sales!$D$1");
chart.ChangeType(XlsxChartType.LineMarkers);
chart.SetLegend(XlsxLegendPosition.Right);
chart.SetAxisScale(1, 0d, 20d);
chart.SetAxisTitle(1, "Units");
Console.WriteLine($"{chart.Type}, {chart.Series.Count} series, values {chart.Axes[1].Minimum} to {chart.Axes[1].Maximum}");
=SERIES(Sales!$B$1,Sales!$A$2:$A$5,Sales!$B$2:$B$5,1)
=SERIES(Sales!$C$1,Sales!$A$2:$A$5,Sales!$C$2:$C$5,2)
LineMarkers, 3 series, values 0 to 20
Each change rewrites the elements it touches and keeps the rest of the chart as it was: its colours, fonts and data labels stay. Remove() takes a chart off its sheet with its parts; a chart sheet's chart goes with its sheet. Download the workbook: sales-charts-made.xlsx.
Draw a chart
SpreadsheetConverter draws a sheet's charts in the pages it writes, where their anchors put them, and
RenderChart draws one chart on its own as an image as large as it is on the sheet. The chart engine is the one Word
documents' charts are drawn by, with the workbook's theme for the colours and faces.
dotnet add package DocWright.Spreadsheet.Rendering
using DocWright.Spreadsheet;
using DocWright.Spreadsheet.Rendering;
var converter = new SpreadsheetConverter();
XlsxDocument workbook = XlsxDocument.Open(File.OpenRead("chart-page.xlsx"));
// One chart on its own, as large as it is on the sheet (a PNG at 96 dpi, or as the options say).
XlsxChart columns = workbook.Sheets[0].Charts.Find("Columns")!;
File.WriteAllBytes("columns.png", converter.RenderChart(columns,
new SpreadsheetImageOptions { Image = new ImageRenderOptions { Dpi = 192 } }));
// The sheet's page, with every chart where its anchor puts it.
using (FileStream pdf = File.Create("chart-page.pdf"))
{
converter.ToPdf(workbook, pdf);
}
IReadOnlyList<byte[]> pages = converter.ToImages(workbook);
File.WriteAllBytes("chart-page.png", pages[0]);
foreach (XlsxChart chart in workbook.Sheets[0].Charts)
{
Console.WriteLine($"{chart.Name}: {chart.Type}");
}

The page, six charts Excel inserted, as ToImages draws it (Excel's own PDF of the same sheet differs in 0.9 % of its
pixels; download DocWright's: chart-page.pdf):

Column, bar, line, area, pie, doughnut and scatter charts are drawn, with markers, data labels, titles, gridlines and
legends. Excel's automatic title (the one series' name, or "Chart Title"), its square frame and a scatter with no
X values are drawn as Excel draws them. Radar, bubble, surface, stock and 2016 charts are not drawn: RenderChart
refuses one, and a page leaves it out and reports it once through SpreadsheetLayoutOptions.Diagnostics:
using DocWright.Core;
using DocWright.Spreadsheet;
using DocWright.Spreadsheet.Rendering;
XlsxDocumentSheet sheet = workbook.Sheets[0];
XlsxChart radar = sheet.Charts.Add(XlsxChartType.Radar, new XlsxCellRange(1, 1, 6, 2), "J2");
try
{
converter.RenderChart(radar);
}
catch (UnsupportedFeatureException e)
{
Console.WriteLine(e.Message);
}
Columns: ColumnClustered
Lines: LineMarkers
Pie: Pie
Areas: AreaStacked
Scatter: XYScatter
Doughnut: Doughnut
The chart 'Chart 7' is a Radar chart, which is not drawn: bar, column, line, area, pie, doughnut and scatter charts are.
Warning
Known issue: a scatter's X axis spans its data with no labels where Excel draws a scale of its own, and a bar chart
whose values cross zero is drawn on a shorter scale than Excel's, with its category labels at the plot's edge where
Excel sets them beside the axis. A chart made with Charts.Add (no formatting of its own) is drawn with the
defaults of a Word document's chart rather than Excel's.
Note
A chart made here has no style part: Excel draws it with its default look until a style is chosen there.
Note
An Excel 2016 chart (waterfall, treemap, sunburst, histogram, box and whisker, funnel) is listed with the type Chart2016 and no series, and is not changed here. Data labels, trendlines, error bars, colours and fonts are kept in the part as they are; they are not part of the model.