Charts as images

LiveCharts can render a chart to an image without any UI framework: in a console app, on a server, or from a chart control that is already on screen. The image charts are SKCartesianChart, SKPieChart, SKPolarChart and SKGeoMap; they take the same properties as the CartesianChart, PieChart, PolarChart and GeoMap controls, and save the result as PNG, JPEG or WEBP, or draw it on any SkiaSharp canvas, for example to build an SVG file.

Every member is listed in the API explorer: SKCartesianChart, SKPieChart, SKPolarChart, SKGeoMap and their base class, InMemorySkiaSharpChart.

Image from a control

Pass a chart control to the constructor of the image chart of the same kind, and save it. This sample places a CartesianChart and a PieChart in the view and saves both as PNG files once the view is loaded.

Charts as images: Charts saved as PNG or SVG.

The images are built in the code behind of the view:

using LiveChartsCore.SkiaSharpView.Maui;
using LiveChartsCore.SkiaSharpView.SKCharts;

namespace MauiSample.General.ChartToImage;

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View : ContentPage
{
    private readonly string _folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

    public View()
    {
        InitializeComponent();

        Loaded += (_, _) =>
        {
            // in this case in the constructor of this view // mark
            // we render our chart controls as images // mark
            CreateImageFromCartesianControl();
            CreateImageFromPieControl();
        };
    }

    private void CreateImageFromCartesianControl()
    {
        // you can take any chart in the UI, and build an image from it // mark
        var chartControl = (CartesianChart)FindByName("cartesianChart");
        var skChart = new SKCartesianChart(chartControl);
        skChart.SaveImage(Path.Combine(_folderPath, "CartesianImageFromControl.png"));
    }

    private void CreateImageFromPieControl()
    {
        var chartControl = (PieChart)FindByName("pieChart");
        var skChart = new SKPieChart(chartControl);
        skChart.SaveImage(Path.Combine(_folderPath, "PieImageFromControl.png"));
    }
}

The image is what the control draws at that moment, with the size of the control: Width and Height are ignored. The control must be loaded and have a size, otherwise GetImage and SaveImage throw.

Charts without a UI

The image charts are in the LiveChartsCore.SkiaSharpView package. Every LiveCharts view package (WPF, WinForms, Avalonia, MAUI and the others) depends on it, so install it only in a project that does not use one, such as a console app or a web server:

Package reference
<PackageReference Include="LiveChartsCore.SkiaSharpView" Version="2.2.0" />
dotnet CLI
dotnet add package LiveChartsCore.SkiaSharpView --version 2.2.0

Create the chart, set its properties as you would on a control, and save it:

using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.SKCharts;
using SkiaSharp;

var cartesianChart = new SKCartesianChart
{
    Width = 900,
    Height = 600,
    Series = [
        new LineSeries<int> { Values = [1, 5, 4, 6] },
        new ColumnSeries<int> { Values = [4, 8, 2, 4] }
    ],
    LegendPosition = LiveChartsCore.Measure.LegendPosition.Right,
    Background = SKColors.White
};

cartesianChart.SaveImage("cartesianChart.png"); // mark

SKPieChart, SKPolarChart and SKGeoMap work the same way:

var pieChart = new SKPieChart
{
    Width = 900,
    Height = 600,
    Series = [
        new PieSeries<int> { Values = [10] },
        new PieSeries<int> { Values = [6] },
        new PieSeries<int> { Values = [4] }
    ]
};

pieChart.SaveImage("pieChart.png");

var geoHeatMap = new SKGeoMap
{
    Width = 900,
    Height = 600,
    Series = [
        new HeatLandSeries
        {
            Lands = [
                new() { Name = "mex", Value = 10 },
                new() { Name = "usa", Value = 15 },
                new() { Name = "can", Value = 8 }
            ]
        }
    ]
};

geoHeatMap.SaveImage("geoHeatMap.png");

An image chart has no animations: every time it builds an image, it measures the chart and draws it at its final state. The complete console app is in the repository, in samples/ConsoleSample.

Size and background

Width and Height set the size of the image in pixels, 900 by 600 by default.

Background is the color the image is filled with before the chart is drawn. When it is not set, the image uses the background color of the theme, white with a light theme and black with a dark one. Set it to SKColors.Transparent for an image with a transparent background:

var chart = new SKCartesianChart
{
    Width = 400, // mark
    Height = 300, // mark
    Background = SKColors.Transparent, // mark
    Series = [new LineSeries<int> { Values = [1, 5, 4, 6] }]
};

Formats and streams

SaveImage takes a file path or a Stream. The second argument is the format, an SKEncodedImageFormat that is Png by default, and the third the quality, from 0 to 100, 80 by default:

chart.SaveImage("chart.jpg", SKEncodedImageFormat.Jpeg, 90);

using var stream = new MemoryStream();
chart.SaveImage(stream, SKEncodedImageFormat.Webp);

GetImage returns the image as an SKImage, for any other operation, such as sending it from a web API as base64. Dispose it when you are done:

using var image = chart.GetImage();
using var data = image.Encode();
var base64 = Convert.ToBase64String(data.AsSpan());

SVG

DrawOnCanvas draws the chart on any SkiaSharp SKCanvas. To build an SVG file, draw on an SKSvgCanvas, and dispose the canvas before reading the stream: the SVG document is completed when the canvas is disposed.

using var stream = new MemoryStream();

var svgCanvas = SKSvgCanvas.Create(SKRect.Create(chart.Width, chart.Height), stream);
chart.DrawOnCanvas(svgCanvas); // mark
svgCanvas.Dispose(); // mark

stream.Position = 0;
using var file = File.Create("chart.svg");
stream.CopyTo(file);
~/overview/export.md