TX Sheet Control

PivotTable.CreateChartAsync Method

Method
Namespace
TXTextControl.TXSheetControl.Server
Package
TXTextControl.TXSheetControl.Server as TXTextControl.TXSheetControl.Api.dll from the private NuGet feed or the supplied NuGet folder
Also in
TXTextControl.TXSheetControl.Blazor as TXTextControl.TXSheetControl.Api.dll

Creates a connected chart from visible pivot detail groups. Fields, measures, filters and source refresh update its series without duplicating subtotals or grand totals.

Syntax

public Task<Chart> CreateChartAsync(object? options = null, CancellationToken cancellationToken = default);

Parameters

options: Optional camelCase chart appearance and placement settings, such as type, title, legend, colors, style, anchor and size. The pivot controls chart data and orientation; dataRange, series and seriesOrientation cannot be supplied. Scatter, bubble and stock chart types are unsupported.

cancellationToken: Cancels the asynchronous operation.

Return Value

A Chart handle that stays connected through pivot field edits, filtering, sorting, expand/collapse, refresh and relocation. An empty value area or unmatched filter leaves a blank connected chart.

Example

Place this code in a .NET console application that references the server package. It creates source orders and a report before applying the setting or operation.

using TXTextControl.TXSheetControl.Server;

await using var control = new ServerSheetControl();
var workbook = control.Workbook;
var source = await workbook.Sheets.AtAsync(0);
await source.Range("A1:D5").SetValuesAsync([
    ["Region", "Category", "Revenue", "Cost"],
    ["Europe", "Software", 1250, 500], ["Europe", "Services", 700, 350],
    ["Americas", "Software", 1800, 900], ["Americas", "Services", 950, 400]
]);
var report = await workbook.Sheets.AddAsync("Sales Pivot");
var pivot = await report.PivotTables.AddAsync(new PivotTableOptions {
    Name = "RegionalSales", Source = new() { SheetId = source.Id, Range = "A1:D5" },
    Destination = new() { SheetId = report.Id, Cell = "B3" },
    Rows = [new() { Field = "Region" }], Columns = [new() { Field = "Category" }],
    Values = [new() { Field = "Revenue", Caption = "Sales", NumberFormatCode = "$#,##0.00" }]
});

var chart = await pivot.CreateChartAsync(new { type = "column-clustered", title = "Regional Revenue" });
await pivot.SetFiltersAsync([new() { Field = "Region", Type = "include", Items = ["Europe"] }]);
await chart.UpdateAsync(new { type = "line", legend = "right" });
await workbook.SaveAsync("pivot-chart.xlsx", StreamType.SpreadsheetML);

Remarks

Use UpdateAsync, SetFieldsAsync, SetFiltersAsync and SetExpandedAsync on the pivot to change chart data. Appearance updates through Chart.UpdateAsync retain the binding; direct chart range, series or orientation overrides reject without changing the workbook. Creation and pivot-driven chart changes use workbook undo history. XLSX writes the native chart-to-pivot association. Removing the pivot leaves a static chart containing the last displayed values.

See Pivot Tables for cache semantics, grouping, supported values and limits. Partial updates use camelCase anonymous objects; creation uses complete PivotTableOptions.