Export and download
Save a chart as PNG or SVG, or its data as CSV, straight from the browser. Images show the chart as it is drawn: the current size, zoom, hidden series and the page's theme colors. No server-side rendering or extra package is needed.
Export from code
The same exports are methods on the chart reference. Download…Async saves a file in the browser.
ExportPngAsync and ExportSvgAsync return the image to .NET (for uploads, emails or reports), and
ExportCsv returns the data of every series as text: an x column and one column per series, numbers in invariant culture,
times in ISO 8601.
Code
<RioChart @ref="chart" TItem="Reading">
<BarSeries Items="dailyOrders" Name="Orders" XValue="r => r.Time" YValue="r => r.Value" Format="#,0" />
</RioChart>
<button @onclick="() => chart.DownloadPngAsync(options: new ChartImageOptions { Scale = 3 })">Download PNG (3×)</button>
<button @onclick="() => chart.DownloadSvgAsync()">Download SVG</button>
<button @onclick="() => chart.DownloadCsvAsync(options: new CsvOptions { Delimiter = ';' })">Download CSV (;)</button>
@code {
RioChart<Reading> chart = null!;
async Task Upload()
{
byte[] png = await chart.ExportPngAsync();
string svg = await chart.ExportSvgAsync(new ChartImageOptions { Background = "transparent" });
string csv = chart.ExportCsv();
}
}Choose formats and handle the export
ExportFormats picks the menu items; with a single format the button saves it directly. OnExporting runs when
the user picks one, before the file is saved: change FileName, ImageOptions or CsvOptions, or set
Cancel and do something else with the content, such as uploading it. GetPngAsync, GetSvgAsync and
GetCsv make the content only when called, so a handler that just renames the file sends nothing to .NET.
Here PNG is saved with the date in its name at 3×, and CSV is shown below instead of downloaded.
Code
<RioChart TItem="Reading" Exportable="true" ExportFormats="ExportFormats.Png | ExportFormats.Csv" OnExporting="HandleExport">
<LineSeries Items="conversion" Name="Conversion" XValue="r => r.Time" YValue="r => r.Value" Format="0.00 '%'" />
</RioChart>
@code {
async Task HandleExport(ChartExportingEventArgs e)
{
e.FileName = $"conversion-{DateTime.Today:yyyy-MM-dd}.{e.Format.ToString().ToLowerInvariant()}";
if (e.Format == ExportFormat.Png)
{
e.ImageOptions = new ChartImageOptions { Scale = 3 };
}
else if (e.Format == ExportFormat.Csv)
{
e.Cancel = true; // no download
await storage.UploadAsync(e.FileName, e.GetCsv());
}
}
}Options
| Option | Default | Meaning |
|---|---|---|
ChartImageOptions.Scale | 2 | PNG pixels per CSS pixel. |
ChartImageOptions.Background | the surface color | Any CSS color; "transparent" for none. |
ExportFormats | All | Formats in the menu (Png, Svg, Csv); one format saves directly. |
OnExporting | Raised on a menu pick before the file is saved; not raised for calls from code. | |
CsvOptions.Delimiter | , | Column separator, e.g. ; for spreadsheets with a decimal comma. |
CsvOptions.Culture | invariant | Culture for numbers. |
CsvOptions.XHeader | the x axis title, else x | Header of the x column. |