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

1
0

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.

Version
6.5.1.26063
  • You must to post comments
0
0

Hello Maxime Legraive,

I have investigated your problem and found the cause of it.
The problem here that in our serialization we’re defining the type of X1\X2(and Y1\Y2 accordingly) based on the value in X1(Y1).
In your case you have Y1=0 which is considered as an integer type, but Y2=0.1 that is actually double. So we serialize both values as strings into xml and also we add attribute called ‘YType’ which is set based on Y1 value and it’s int32. Later on deserialization stage it tries to convert string “0.1” into an integer which causes exception.

Our team is working on fixing this bug on our side, and probably we’ll publish this fix with our nearest nightly build.

As for now I can recommend you a workaround:

Just set your Y1 value as follows: => Y1=”0d”;

Please try this and give us your feedback on it.

P.S. As for missing crash when export is performed from the Loaded event. The thing is the on that time when you create your TextAnnotationViewModel and set it via binding to Annotations collection your Annotation.X1X2Y1Y2 are still nulls as your binding were not raised yet(it’s raise right after leaving OnLoaded() method). And ‘null’ values for X1,X2,Y1,Y2 are skipped to serialize\deserialize.

  • You must to post comments
0
0

Hello Nazariy,

Indeed, setting Y1 to double solves the problem.

Thanks a lot !

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.