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

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy. We also have a tag=SciChart on where you can earn rep for your questions!

Please note: SciChart team will only answer questions from customers with active support subscriptions. Expired support questions will be ignored. If your support status shows incorrectly, contact us and we will be glad to help.


I have created a custom annotation for displaying averages for a selected section of the displayed data. It works fine for printing, or copying to the clipboard, or saving a BMP file without specifying a size. However, if I export to XPS or try to specify a size for the BMP, I get the following exception:

    SciChart.Charting.Common.Helpers.ExportException occurred
  Message=Exception occured during serialization of the AverageAnnotation type. The Content property of the Object type cannot be processed. Please be advised that SciChart doesn't handle serialization of objects with propeties of interface type, collection type or custom type.You need to implement IXmlSerializible in such objects to have them handled properly. For more details, please check the InnerException.
   at SciChart.Charting.Visuals.SciChartSurfaceBase.CreateCloneOfSurfaceInMemory(Size newSize)
   at SciChart.Charting.Visuals.SciChartSurface.CreateCloneOfSurfaceInMemory(Size newSize)
   at SciChart.Charting.Visuals.SciChartSurfaceBase.GE(ExportType D, Boolean I, Nullable`1 J)
   at SciChart.Charting.Visuals.SciChartSurfaceBase.ExportToFileInternal(String fileName, ExportType exportType, Boolean useXamlRenderSurface, Nullable`1 size)
   at SciChart.Charting.Visuals.SciChartSurfaceBase.ExportToFile(String fileName, ExportType exportType, Boolean useXamlRenderSurface)
   at SciChart.Charting.Common.Extensions.SciChartSurfaceExtensions.ExportToXPS(SciChartSurface surface, String filePath, Nullable`1 size)
   at Profile6.ProfileTestResultsWindow.SaveGraph() in C:\Vericom\vc5000configurator\Profile6\ProfileTestResultsWindow.xaml.cs:line 75

Inner Exception 1:
InvalidOperationException: Cannot serialize a generic type 'System.Collections.Generic.KeyValuePair`2[System.String,System.Collections.Generic.List`1[Profile6.TestResultsViewModels.AverageAnnotationViewModel+AverageInfo]]'.

I tried wrapping the Averages list in a class that implemented IXmlSerializable, but that didn’t change anything. Does anyone know what I need to do to make this work?

I tried attaching the source code in various formats, but keep getting Forbidden errors. So, I put it inline below. (The AverageAnnotation.xaml.cs is the default code with only InitializeComponent();)


   <sci:CustomAnnotationForMvvm x:Class="Profile6.TestResultsViewModels.AverageAnnotation"
             d:DesignHeight="300" d:DesignWidth="300">
    <Border BorderBrush="Black" BorderThickness="1">
        <StackPanel Background="White">
                    <!-- Disable the highlighting for the lists -->
                    <Style TargetType="{x:Type ListViewItem}" x:Key="NoHighlight">
                        <Setter Property="Background" Value="Transparent" />
                        <Setter Property="Margin" Value="0"/>
                        <Setter Property="Padding" Value="0"/>
                        <Setter Property="Template">
                                <ControlTemplate TargetType="{x:Type ListViewItem}">
                                    <ContentPresenter />

            <Label Content="{x:Static localization:I18N.Averages}"
                   FontSize="12" FontWeight="Bold"/>  
            <ListView ItemsSource="{Binding Averages}" 
                      ItemContainerStyle="{StaticResource NoHighlight}">
                        <StackPanel Background="Transparent">
                            <Label Content="{Binding Key}" 
                                   Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}, Path=DataContext.HasMultipleRuns, Converter={StaticResource FalseToCollapsedConverter}}"
                                   FontSize="10" FontWeight="Bold"/>
                            <ListView ItemsSource="{Binding Value}"
                                      ItemContainerStyle="{StaticResource NoHighlight}">
                                        <Grid Margin="10,0,0,0"
                                                <ColumnDefinition Width="Auto" SharedSizeGroup="Name"/>
                                                <ColumnDefinition Width="Auto" SharedSizeGroup="Average"/>
                                                <RowDefinition Height="Auto"/>
                                            <Label Content="{Binding SensorName}" 
                                                   FontSize="10" FontWeight="Bold"
                                                   Foreground="{Binding TextBrush}"
                                            <Label Content="{Binding Average}"
                                                   FontSize="10" FontWeight="Bold"
                                                   Foreground="{Binding TextBrush}"
                                                   Grid.Row="0" Grid.Column="1"/>


using System;
using System.Collections.Generic;
using System.Windows.Media;
using SciChart.Charting.Model.ChartSeries;

namespace Profile6.TestResultsViewModels
    public class AverageAnnotationViewModel : CustomAnnotationViewModel
        public override Type ViewType => typeof(AverageAnnotation);

        private List<KeyValuePair<string, List<AverageInfo>>> averages;
        public List<KeyValuePair<string, List<AverageInfo>>> Averages
            get => averages;
                averages = value;

        public bool HasMultipleRuns => Averages.Count > 1;

        public class AverageInfo
            public AverageInfo(string sensorName, double average, Color textColor)
                SensorName = sensorName;
                Average = average;
                TextColor = textColor;
                TextBrush = new SolidColorBrush(TextColor);

            public string SensorName { get; }
            public double Average { get; }
            public Color TextColor { get; }
            public Brush TextBrush { get; }
  • Todd Burnside
    Sorry about the formatting of the code. Not sure what the problem is…
  • Andrew
    ^^ I resolved that. You press the 001100011 button to format code blocks.
  • You must to post comments

Hi Todd,

SciChart export works like this:

  1. If you export a chart to bitmap, at the current size, we simply screenshot the chart on screen and save it.
  2. If you specify a non-default size, or you export to XPS, or you use the XamlRenderSurface then we must clone the entire chart in-memory and reconstruct it before exporting.
  3. Cloning the entire chart is done using serialisation. SciChart elements are designed to serialise and deserialise. We use this to clone the chart, then to set properties such as Width,Height and then export it.
  4. During this serialisation process if there are any properties created by you which are not serialisable then they will crash the serializer. That is why the exception message says to implement IXmlSerializable for any custom types.

The exception message says

InvalidOperationException: Cannot serialize a generic type 'System.Collections.Generic.KeyValuePair`2[System.String,System.Collections.Generic.List`1[Profile6.TestResultsViewModels.AverageAnnotationViewModel+AverageInfo]]'.

So the property in your AverageAnnotationViewModel which cannot be serialised is

public List<KeyValuePair<string, List<AverageInfo>>> Averages

You need to either mark this with the [ShouldNotSerializeAttribute] attribute to exclude it, or, you need to make your AverageAnnotationViewModel implement IXmlSerializable and implement WriteXml / ReadXml to serialise this property.

Alternatively, an easy workaround is to ensure that you perform all Exporting to bitmap at the current chart resolution. This will avoid the cloning and serialisation step entirely.

Finally, there is another second workaround. You can mark your properties as ShouldNotSerialize and then override SciChartSurface.CreateCloneOfChartInMemory

// override in SciChartSurface
protected override SciChartSurfaceBase CreateCloneOfSurfaceInMemory(Size newSize)

This is the method where we clone the chart. You can call the base to clone the chart but add on any properties you need to. E.g.

public class SciChartSurfaceEx : SciChartSurface 
    protected override SciChartSurfaceBase CreateCloneOfSurfaceInMemory(Size newSize) 
         var cloned = (SciChartSurface)base.CreateCloneOfSurfaceInMemory(newSize);

         for(int i = 0; i < this.Annotations.Count; i++) 
              if (this.Annotations[i].DataContext is AverageAnnotationViewModel)
                   ((AverageAnnotationViewModel )cloned.Annotations[i].DataContext).Averages = ((AverageAnnotationsViewModel)this.Annotations[i].DataContext).Averages;
         return cloned;

I haven’t tested this code but it is an example of how to copy properties from Source to Destination during the clone process which takes place in export. Ignoring the serialisation and doing it this way is probably simpler.

Let me know if this helps, and if you need further support.

Best regards,

  • You must to post comments

Thanks for the quick response, Andrew. I will give it a try when I get a chance. 🙂

  • You must to post comments

I tried overriding CreateCloneOfSurfaceInMemory() as recommended above. It still appears to be trying to serialize the Averages property, even if I mark it (and the private averages field) with the [ShouldNotSerialize] attribute. I also included the attribute on the HasMultipleRuns property since it references Averages. I also tried [XmlIgnore].

I tried to simply remove the AverageAnnotation from the Annotations list prior to calling base.CreateCloneOfSurfaceInMemory() and then adding it back to the original chart AND the clone afterwards. This doesn’t break the original chart, but I can’t tell yet if it works for the clone until I figure out the issue in Ticket LDD-704-27229. I will try to make a minimal example that causes that issue.

PS. I did upgrade to version just to check.

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.