Charts As Image/Svg
Notice this web site builds the control from code behind but you could also grab it from the toolbox, this sample also uses a ViewModel to populate the properties of the control(s) in this sample.
View model
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Geo;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
namespace ViewModelsSamples.General.ChartToImage;
public partial class ViewModel : ObservableObject
{
public ISeries[] CatesianSeries { get; set; } =
{
new LineSeries<int> { Values = new int[] { 1, 5, 4, 6 } },
new ColumnSeries<int> { Values = new int[] { 4, 8, 2, 4 } }
};
public ISeries[] PieSeries { get; set; } =
{
new PieSeries<int> { Values = new int[] { 10, } },
new PieSeries<int> { Values = new int[] { 6 } },
new PieSeries<int> { Values = new int[] { 4 } }
};
public IGeoSeries[] GeoSeries { get; set; } = new HeatLandSeries[]
{
new()
{
Lands = new HeatLand[]
{
new() { Name = "bra", Value = 13 },
new() { Name = "mex", Value = 10 },
new() { Name = "usa", Value = 15 },
new() { Name = "can", Value = 8 },
new() { Name = "ind", Value = 12 },
new() { Name = "deu", Value = 13 },
new() { Name= "jpn", Value = 15 },
new() { Name = "chn", Value = 14 },
new() { Name = "rus", Value = 11 },
new() { Name = "fra", Value = 8 },
new() { Name = "esp", Value = 7 },
new() { Name = "kor", Value = 10 },
new() { Name = "zaf", Value = 12 },
new() { Name = "are", Value = 13 }
}
}
};
}
Form code behind
Having the previous data (ViewModel), we add 3 charts to the UI, a CartesianChart
, a PieChart
and a GeoMap
.
using System.Windows.Forms;
using LiveChartsCore.SkiaSharpView.SKCharts;
using LiveChartsCore.SkiaSharpView.WinForms;
using ViewModelsSamples.General.ChartToImage;
namespace WinFormsSample.General.ChartToImage;
public partial class View : UserControl
{
private readonly CartesianChart _cartesian;
private readonly PieChart _pie;
private readonly GeoMap _map;
public View()
{
InitializeComponent();
Size = new System.Drawing.Size(90, 90);
var viewModel = new ViewModel();
// Adding a cartesian chart to the UI...
_cartesian = new CartesianChart
{
Series = viewModel.CatesianSeries,
// out of livecharts properties...
Location = new System.Drawing.Point(0, 0),
Size = new System.Drawing.Size(400, 200)
};
Controls.Add(_cartesian);
// Adding a pie chart to the UI...
_pie = new PieChart
{
Series = viewModel.PieSeries,
// out of livecharts properties...
Location = new System.Drawing.Point(0, 200),
Size = new System.Drawing.Size(400, 200)
};
Controls.Add(_pie);
// Adding a map chart to the UI...
_map = new GeoMap
{
Series = viewModel.GeoSeries,
// out of livecharts properties...
Location = new System.Drawing.Point(0, 400),
Size = new System.Drawing.Size(400, 200)
};
Controls.Add(_map);
// now lets create the images // mark
CreateImageFromCartesianControl(); // mark
CreateImageFromPieControl(); // mark
CreateImageFromGeoControl(); // mark
}
private void CreateImageFromCartesianControl()
{
// you can take any chart in the UI, and build an image from it // mark
var chartControl = _cartesian;
var skChart = new SKCartesianChart(chartControl) { Width = 900, Height = 600, };
skChart.SaveImage("CartesianImageFromControl.png");
}
private void CreateImageFromPieControl()
{
var chartControl = _pie;
var skChart = new SKPieChart(chartControl) { Width = 900, Height = 600, };
skChart.SaveImage("PieImageFromControl.png");
}
private void CreateImageFromGeoControl()
{
var chartControl = _map;
var skChart = new SKGeoMap(chartControl) { Width = 900, Height = 600, };
skChart.SaveImage("MapImageFromControl.png");
}
}
You will get the following plot in the UI.
Build an image in the server side or console app
LiveCharts can render images without the need of any UI framework, you can build images in the server side or in a console application as far as you install the SkiaSharp view package, it is available from NuGet:
LiveChartsCore.SkiaSharpView
Notice any view of LiveCharts (WPF, WinForms, Maui, etc..) has a dependency on LiveChartsCore.SkiaSharpView package, thus you do not need to install this package again if you are already using LiveCharts to render any control in the UI.
The next code block build a CartesianChart
, a PieChart
and a GeoMap
chart as images, to reproduce this sample
create a new ConsoleApplication in VisualStudio 2022, select .Net 6.0 as the framework.
The Net 6.0 template is much cleaner than previous console app templates, notice also that LiveCharts is available in .NET 5.0, .NET core 3.1 or .NET framework (classic) 4.6.2 or greater.
Finally build the images in the Program.cs
file.
using LiveChartsCore;
using LiveChartsCore.Drawing;
using LiveChartsCore.Geo;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.SKCharts;
using LiveChartsCore.SkiaSharpView.VisualElements;
using SkiaSharp;
var cartesianChart = new SKCartesianChart
{
Width = 900,
Height = 600,
Series = new ISeries[]
{
new LineSeries<int> { Values = new int[] { 1, 5, 4, 6 } },
new ColumnSeries<int> { Values = new int[] { 4, 8, 2, 4 } }
},
Title = new LabelVisual
{
Text = "Hello LiveCharts",
TextSize = 30,
Padding = new Padding(15),
Paint = new SolidColorPaint(0xff303030)
},
LegendPosition = LiveChartsCore.Measure.LegendPosition.Right,
Background = SKColors.White
};
// you can save the image to png (by default)
// or use the second argument to specify another format.
cartesianChart.SaveImage("cartesianChart.png");
// additionally you can save a chart as svg:
// for more info see: https://github.com/mono/SkiaSharp/blob/main/tests/Tests/SKCanvasTest.cs#L396
using var stream = new MemoryStream();
var svgCanvas = SKSvgCanvas.Create(SKRect.Create(cartesianChart.Width, cartesianChart.Height), stream);
cartesianChart.DrawOnCanvas(svgCanvas);
svgCanvas.Dispose(); // <- dispose it before using the stream, otherwise the svg could not be completed.
stream.Position = 0;
using var fs = new FileStream("cartesianChart.svg", FileMode.OpenOrCreate);
stream.CopyTo(fs);
// you can also save the image of any other charts:
var pieChart = new SKPieChart
{
Width = 900,
Height = 600,
Series = new ISeries[]
{
new PieSeries<int> { Values = new int[] { 10, } },
new PieSeries<int> { Values = new int[] { 6 } },
new PieSeries<int> { Values = new int[] { 4 } }
},
LegendPosition = LiveChartsCore.Measure.LegendPosition.Right
};
pieChart.SaveImage("pieChart.png");
var geoHeatMap = new SKGeoMap
{
Width = 900,
Height = 600,
Series = new IGeoSeries[]
{
new HeatLandSeries
{
Lands = new HeatLand[]
{
new() { Name = "mex", Value = 10 },
new() { Name = "usa", Value = 15 },
new() { Name = "can", Value = 8 }
}
}
}
};
geoHeatMap.SaveImage("geoHeatMap.png");
// alternatively you can get the image and do different operations:
using var image = cartesianChart.GetImage();
using var data = image.Encode();
var base64CartesianChart = Convert.ToBase64String(data.AsSpan());
Console.WriteLine("Images saved at the root folder!");
Notice that the previous code also works in an ASP.net
project, as far as you are using .NET core 3.1 or greater