I have a method, that creates a Chart without showing it anywhere and saving it as picture file.
public async Task ExportChart(int width = 1920, int height = 1080)
{
//Create base chart
var exportChart = new SfChart();
var foreground = new SolidColorBrush(Color.FromArgb(255, 234, 172, 42));
exportChart.PrimaryAxis = new DateTimeAxis()
{
LabelFormat = "dd/MM - HH:mm",
Foreground = foreground,
LabelsIntersectAction = AxisLabelsIntersectAction.Auto
};
exportChart.SecondaryAxis = new NumericalAxis()
{
RangePadding = NumericalPadding.Round,
Foreground = foreground,
Header = new TextBlock() { Foreground = foreground, Text = MarketAsset + "/" + BaseAsset }
};
//Get Technical Indicator Charts
NumericalAxis yAxis = new NumericalAxis()
{
Header = "Indicators",
Foreground = foreground,
OpposedPosition = true
};
List> seriesList = new List>();
foreach (IndicatorLineCollection ilc in IndicatorLines)
{
seriesList.Add(await ilc.RenderIndicatorLines(yAxis)); //this method generates some FastLineSeries-Charts which get added to the main chart below.
}
//Create Static Candles Chart and add TIs
var chartCollection = new ChartSeriesCollection();
var candleChart = new FastCandleBitmapSeries()
{
Label = "Candles",
Foreground = foreground,
ItemsSource = Candles,
BearFillColor = new SolidColorBrush(Color.FromArgb(255, 203, 25, 25)),
BullFillColor = new SolidColorBrush(Colors.Green),
XBindingPath = "Date",
Open = "Open",
High = "High",
Low = "Low",
Close = "Close"
};
chartCollection.Add(candleChart);
foreach (List seriesCollection in seriesList)
{
foreach (FastLineSeries series in seriesCollection)
{
chartCollection.Add(series);
}
}
exportChart.Series = chartCollection;
exportChart.Measure(new Size(width, height));
exportChart.Arrange(new Rect(0, 0, width, height));
exportChart.GetType().GetMethod("RenderSeries", BindingFlags.NonPublic |
BindingFlags.Instance).Invoke(exportChart, null);
exportChart.UpdateLayout();
//Save PNG
StorageFile storageFile = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync("ExportedChart.png", CreationCollisionOption.ReplaceExisting);
Guid encoder = BitmapEncoder.PngEncoderId;
exportChart.Save(await storageFile.OpenAsync(FileAccessMode.ReadWrite), encoder);
}