SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components

1 vote
903 views

Hello,

I try to add some TextAnnotation to my Surface by creating some TextAnnotationViewModel in my C# code and export the chart to PNG with non-default size. If I do it right when the MainWindow is loaded , I have no problem exporting the chart and I can see the annotation on the PNG:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var annotationViewModel = new TextAnnotationViewModel
        {
            CoordinateMode = AnnotationCoordinateMode.Relative,
            X1 = 0.3,
            X2 = 0.7,
            Y1 = 0,
            Y2 = 0.1,
            Text = "Test"
        };

        var annotationViewModels = new List<IAnnotationViewModel> { annotationViewModel };

        annotationViewModels.Add(annotationViewModel);
        var annotationsSourceCollection = new AnnotationsSourceCollection(annotationViewModels);

        sciChartSurface.Annotations = new AnnotationCollection(annotationsSourceCollection);

        var filePath = "{somePath}";
        sciChartSurface.ExportToFile(filePath, ExportType.Png, false, new System.Windows.Size(sciChartSurface.RenderSize.Width * 4,
            sciChartSurface.RenderSize.Height * 4));
    }
}

But if I try to trigger the sciChartSurface.ExportToFile with a Button (well after Loaded), it doesn’t work anymore and he gives me the following exception:

System.Exception: ‘An error occurred when using serialization to clone a chart for export to file. Please check the inner exception for details.’
Inner exception : FormatException: Input string was not in a correct format.

I know it comes from the annotation because if I remove it, the export works again.
Then I have tried reinitializing the sciChartSurface.Annotations just before exporting and by doing that, the export works again :

public partial class MainWindow : Window
{
    private List<IAnnotationViewModel> annotationViewModels = new List<IAnnotationViewModel>();
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var annotationViewModel = new TextAnnotationViewModel
        {
            CoordinateMode = AnnotationCoordinateMode.Relative,
            X1 = 0.3,
            X2 = 0.7,
            Y1 = 0,
            Y2 = 0.1,
            Text = "Test"
        };

        annotationViewModels.Add(annotationViewModel);
        var annotationsSourceCollection = new AnnotationsSourceCollection(annotationViewModels);

        sciChartSurface.Annotations = new AnnotationCollection(annotationsSourceCollection);
    }


    private void OnClick(object sender, RoutedEventArgs e)
    {
        // I added this:
        var annotationsSourceCollection = new AnnotationsSourceCollection(annotationViewModels);
        sciChartSurface.Annotations = new AnnotationCollection(annotationsSourceCollection);

        var filePath = "{somePath}";
        sciChartSurface.ExportToFile(filePath, ExportType.Png, false, new System.Windows.Size(sciChartSurface.RenderSize.Width * 4,
            sciChartSurface.RenderSize.Height * 4));
    }
}

What is going on here ?

Thank you in advance for your help.

Showing 1 result
This template supports the sidebar's widgets. Add one or use Full Width layout.