Pre loader

Tag: TextAnnotationViewModel

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

1 vote
3k 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

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies