123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- //
- // Purpose: Serialization saves the state of the chart and also
- // provides the ability to load the serialized data back
- // into the chart. All chart properties can be persisted,
- // including the chart's data.
- //
- using System;
- using System.ComponentModel;
- using System.ComponentModel.Design;
- using System.IO;
- using FastReport.DataVisualization.Charting.Utilities;
- using System.Xml;
- namespace FastReport.DataVisualization.Charting
- {
- #region Serialization enumeration
- /// <summary>
- /// An enumeration of the formats of the chart serializer.
- /// </summary>
- public enum SerializationFormat
- {
- /// <summary>
- /// XML serializer format.
- /// </summary>
- Xml,
- /// <summary>
- /// Binary serializer format.
- /// </summary>
- Binary
- }
- /// <summary>
- /// An enumeration of chart serializable content definition flags
- /// </summary>
- [Flags]
- public enum SerializationContents
- {
- /// <summary>
- /// Default content.
- /// </summary>
- Default = 1,
- /// <summary>
- /// Serialize only series data.
- /// </summary>
- Data = 2,
- /// <summary>
- /// Serialize chart visual appearance (e.g. Colors, Line Styles).
- /// </summary>
- Appearance = 4,
- /// <summary>
- /// All content is serialized.
- /// </summary>
- All = Default | Data | Appearance
- }
- #endregion
- /// <summary>
- /// ChartSerializer class provides chart serialization.
- /// </summary>
- [
- SRDescription("DescriptionAttributeChartSerializer_ChartSerializer"),
- DefaultProperty("Format"),
- ]
- public class ChartSerializer
- {
- #region Private fields
- // Reference to the service container
- private IServiceContainer _serviceContainer = null;
- // Reference to the chart object
- private Chart _chart = null;
- // Reference to the serializer object
- private SerializerBase _serializer = new XmlFormatSerializer();
- // Format of the serializer in use
- private SerializationFormat _format = SerializationFormat.Xml;
- // Serialization content
- private SerializationContents _content = SerializationContents .Default;
- #endregion
- #region Constructors and Service Provider methods
- /// <summary>
- /// Default constructor is unavailable
- /// </summary>
- private ChartSerializer()
- {
- }
- /// <summary>
- /// Internal constructor
- /// </summary>
- /// <param name="container">Service container reference.</param>
- internal ChartSerializer(IServiceContainer container)
- {
- if(container == null)
- {
- throw(new ArgumentNullException(SR.ExceptionInvalidServiceContainer));
- }
- _serviceContainer = container;
- }
- /// <summary>
- /// Returns ChartSerializer service object
- /// </summary>
- /// <param name="serviceType">Requested service type.</param>
- /// <returns>ChartSerializer service object.</returns>
- internal object GetService(Type serviceType)
- {
- if(serviceType == typeof(ChartSerializer))
- {
- return this;
- }
- throw (new ArgumentException( SR.ExceptionChartSerializerUnsupportedType( serviceType.ToString())));
- }
- #endregion
- #region Public properties
- /// <summary>
- /// Gets or sets the serializable content.
- /// </summary>
- [
- SRCategory("CategoryAttributeMisc"),
- DefaultValue(typeof(SerializationContents ), "Default"),
- SRDescription("DescriptionAttributeChartSerializer_Content")
- ]
- public SerializationContents Content
- {
- get
- {
- return _content;
- }
- set
- {
- // Set content value
- _content = value;
- // Automatically set SerializableContent and NonSerializableContent properties
- SetSerializableContent();
- }
- }
- /// <summary>
- /// Gets or sets the format used to serialize the chart data.
- /// </summary>
- [
- SRCategory("CategoryAttributeMisc"),
- DefaultValue(typeof(SerializationFormat), "Xml"),
- SRDescription("DescriptionAttributeChartSerializer_Format")
- ]
- public SerializationFormat Format
- {
- get
- {
- return _format;
- }
- set
- {
- if(_format != value)
- {
- _format = value;
- // Create new serializer object
- SerializerBase newSerializer = null;
- if(_format == SerializationFormat.Binary)
- {
- newSerializer = new BinaryFormatSerializer();
- }
- else
- {
- newSerializer = new XmlFormatSerializer();
- }
- // Copy serializer settings
- newSerializer.IsUnknownAttributeIgnored = _serializer.IsUnknownAttributeIgnored;
- newSerializer.NonSerializableContent = _serializer.NonSerializableContent;
- newSerializer.IsResetWhenLoading = _serializer.IsResetWhenLoading;
- newSerializer.SerializableContent = _serializer.SerializableContent;
- _serializer = newSerializer;
- }
- }
- }
- /// <summary>
- /// Gets or sets a flag which indicates whether object properties are reset to default
- /// values before loading.
- /// </summary>
- [
- SRCategory("CategoryAttributeMisc"),
- DefaultValue(true),
- SRDescription("DescriptionAttributeChartSerializer_ResetWhenLoading")
- ]
- public bool IsResetWhenLoading
- {
- get
- {
- return _serializer.IsResetWhenLoading;
- }
- set
- {
- _serializer.IsResetWhenLoading = value;
- }
- }
- /// <summary>
- /// Gets or sets a flag which indicates whether unknown XML properties and elements will be
- /// ignored without throwing an exception.
- /// </summary>
- [
- SRCategory("CategoryAttributeMisc"),
- DefaultValue(false),
- SRDescription("DescriptionAttributeChartSerializer_IgnoreUnknownXmlAttributes")
- ]
- public bool IsUnknownAttributeIgnored
- {
- get
- {
- return _serializer.IsUnknownAttributeIgnored;
- }
- set
- {
- _serializer.IsUnknownAttributeIgnored = value;
- }
- }
- /// <summary>
- /// Gets or sets a flag which indicates whether chart
- /// serializer is working in template creation mode.
- /// </summary>
- [
- SRCategory("CategoryAttributeMisc"),
- DefaultValue(false),
- SRDescription("DescriptionAttributeChartSerializer_TemplateMode")
- ]
- public bool IsTemplateMode
- {
- get
- {
- return _serializer.IsTemplateMode;
- }
- set
- {
- _serializer.IsTemplateMode = value;
- }
- }
- /// <summary>
- /// Gets or sets the chart properties that can be serialized.
- /// Comma separated list of serializable (Save/Load/Reset) properties.
- /// "ClassName.PropertyName,[ClassName.PropertyName]".
- /// </summary>
- [
- SRCategory("CategoryAttributeMisc"),
- DefaultValue(""),
- SRDescription("DescriptionAttributeChartSerializer_SerializableContent")
- ]
- public string SerializableContent
- {
- get
- {
- return _serializer.SerializableContent;
- }
- set
- {
- _serializer.SerializableContent = value;
- }
- }
- /// <summary>
- /// Gets or sets the chart properties that will not be serialized.
- /// Comma separated list of non-serializable (Save/Load/Reset) properties.
- /// "ClassName.PropertyName,[ClassName.PropertyName]".
- /// </summary>
- [
- SRCategory("CategoryAttributeMisc"),
- DefaultValue(""),
- SRDescription("DescriptionAttributeChartSerializer_NonSerializableContent")
- ]
- public string NonSerializableContent
- {
- get
- {
- return _serializer.NonSerializableContent;
- }
- set
- {
- _serializer.NonSerializableContent = value;
- }
- }
- #endregion
- #region Public methods
- /// <summary>
- /// This method resets all properties of the chart to default values. By setting Content or
- /// SerializableContent/NonSerializableContent properties, specific set of
- /// properties can be reset.
- /// </summary>
- public void Reset()
- {
- // Set serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = true;
- GetChartObject().serializationStatus = SerializationStatus.Resetting;
- }
- // Reset properties
- _serializer.ResetObjectProperties(GetChartObject());
- // Clear serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = false;
- GetChartObject().serializationStatus = SerializationStatus.None;
- }
- }
- /// <summary>
- /// This method saves all properties of the chart into a file. By setting Content or
- /// SerializableContent/NonSerializableContent properties, specific set of
- /// properties can be saved.
- /// </summary>
- /// <param name="fileName">The file name used to write the data.</param>
- public void Save(string fileName)
- {
- //Check arguments
- if (fileName == null)
- throw new ArgumentNullException("fileName");
- // Set serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = true;
- GetChartObject().serializationStatus = SerializationStatus.Saving;
- //GetChartObject().BeginInit();
- }
- // Reset all auto-detected properties values
- GetChartObject().ResetAutoValues();
- // Serialize chart data
- _serializer.Serialize(GetChartObject(), fileName);
- // Clear serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = false;
- GetChartObject().serializationStatus = SerializationStatus.None;
- //GetChartObject().EndInit();
- }
- }
- /// <summary>
- /// This method saves all properties of the chart into a stream. By setting Content or
- /// SerializableContent/NonSerializableContent properties, specific set of
- /// properties can be saved.
- /// </summary>
- /// <param name="stream">The stream where to save the data.</param>
- public void Save(Stream stream)
- {
- //Check arguments
- if (stream == null)
- throw new ArgumentNullException("stream");
- // Set serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = true;
- GetChartObject().serializationStatus = SerializationStatus.Saving;
- //GetChartObject().BeginInit();
- }
- // Reset all auto-detected properties values
- GetChartObject().ResetAutoValues();
- // Serialize chart data
- _serializer.Serialize(GetChartObject(), stream);
- // Clear serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = false;
- GetChartObject().serializationStatus = SerializationStatus.None;
- //GetChartObject().EndInit();
- }
- }
- /// <summary>
- /// This method saves all properties of the chart into an XML writer. By setting Content or
- /// SerializableContent/NonSerializableContent properties, specific set of
- /// properties can be saved.
- /// </summary>
- /// <param name="writer">XML writer to save the data.</param>
- public void Save(XmlWriter writer)
- {
- //Check arguments
- if (writer == null)
- throw new ArgumentNullException("writer");
- // Set serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = true;
- GetChartObject().serializationStatus = SerializationStatus.Saving;
- //GetChartObject().BeginInit();
- }
- // Reset all auto-detected properties values
- GetChartObject().ResetAutoValues();
- // Serialize chart data
- _serializer.Serialize(GetChartObject(), writer);
- // Clear serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = false;
- GetChartObject().serializationStatus = SerializationStatus.None;
- //GetChartObject().EndInit();
- }
- }
- /// <summary>
- /// This method saves all properties of the chart into a text writer. By setting Content or
- /// SerializableContent/NonSerializableContent properties, specific set of
- /// properties can be saved.
- /// </summary>
- /// <param name="writer">Text writer to save the data.</param>
- public void Save(TextWriter writer)
- {
- //Check arguments
- if (writer == null)
- throw new ArgumentNullException("writer");
- // Set serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = true;
- GetChartObject().serializationStatus = SerializationStatus.Saving;
- //GetChartObject().BeginInit();
- }
- // Reset all auto-detected properties values
- GetChartObject().ResetAutoValues();
- // Serialize chart data
- _serializer.Serialize(GetChartObject(), writer);
- // Clear serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = false;
- GetChartObject().serializationStatus = SerializationStatus.None;
- //GetChartObject().EndInit();
- }
- }
- /// <summary>
- /// This method loads all properties of the chart from a file. By setting Content or
- /// SerializableContent/NonSerializableContent properties, specific set of
- /// properties can be loaded.
- /// </summary>
- /// <param name="fileName">The file to load the data from.</param>
- public void Load(string fileName)
- {
- //Check arguments
- if (fileName == null)
- throw new ArgumentNullException("fileName");
-
- // Set serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = true;
- GetChartObject().serializationStatus = SerializationStatus.Loading;
- }
- _serializer.Deserialize(GetChartObject(), fileName);
- // Clear serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = false;
- GetChartObject().serializationStatus = SerializationStatus.None;
- }
- }
- /// <summary>
- /// This method loads all properties of the chart from a stream. By setting Content or
- /// SerializableContent/NonSerializableContent properties, specific set of
- /// properties can be loaded.
- /// </summary>
- /// <param name="stream">The stream to load the data from.</param>
- public void Load(Stream stream)
- {
- //Check arguments
- if (stream == null)
- throw new ArgumentNullException("stream");
-
- // Set serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = true;
- GetChartObject().serializationStatus = SerializationStatus.Loading;
- }
- _serializer.Deserialize(GetChartObject(), stream);
- // Clear serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = false;
- GetChartObject().serializationStatus = SerializationStatus.None;
- }
- }
- /// <summary>
- /// This method loads all properties of the chart from an XML reader. By setting Content or
- /// SerializableContent/NonSerializableContent properties, specific set of
- /// properties can be loaded.
- /// </summary>
- /// <param name="reader">The XML reader to load the data from.</param>
- public void Load(XmlReader reader)
- {
- //Check arguments
- if (reader == null)
- throw new ArgumentNullException("reader");
- // Set serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = true;
- GetChartObject().serializationStatus = SerializationStatus.Loading;
- }
- _serializer.Deserialize(GetChartObject(), reader);
- // Clear serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = false;
- GetChartObject().serializationStatus = SerializationStatus.None;
- }
- }
- /// <summary>
- /// This method loads all properties of the chart from the text reader. By setting Content or
- /// SerializableContent/NonSerializableContent properties, specific set of
- /// properties can be loaded.
- /// </summary>
- /// <param name="reader">The text reader to load the data from.</param>
- public void Load(TextReader reader)
- {
- //Check arguments
- if (reader == null)
- throw new ArgumentNullException("reader");
- // Set serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = true;
- GetChartObject().serializationStatus = SerializationStatus.Loading;
- }
- _serializer.Deserialize(GetChartObject(), reader);
- // Clear serializing flag
- if(GetChartObject() != null)
- {
- GetChartObject().serializing = false;
- GetChartObject().serializationStatus = SerializationStatus.None;
- }
- }
- #endregion
- #region Protected helper methods
- /// <summary>
- /// Sets SerializableContent and NonSerializableContent properties
- /// depending on the flags in the Content property.
- /// </summary>
- internal void SetSerializableContent()
- {
- // Reset content definition strings
- this.SerializableContent = "";
- this.NonSerializableContent = "";
- // Loop through all enumeration flags
- Array enumValues = Enum.GetValues(typeof(SerializationContents ));
- foreach(object flagObject in enumValues)
- {
- if(flagObject is SerializationContents )
- {
- // Check if flag currently set
- SerializationContents flag = (SerializationContents )flagObject;
- if((this.Content & flag) == flag &&
- flag != SerializationContents .All &&
- this.Content != SerializationContents .All)
- {
- // Add comma at the end of existing string
- if(this.NonSerializableContent.Length != 0)
- {
- this.NonSerializableContent += ", ";
- }
- // Add serializable class/properties names
- this.NonSerializableContent += GetContentString(flag, false);
- this.NonSerializableContent = this.NonSerializableContent.TrimStart(',');
- // Add comma at the end of existing string
- if(this.SerializableContent.Length != 0)
- {
- this.SerializableContent += ", ";
- }
- // Add serializable class/properties names
- this.SerializableContent += GetContentString(flag, true);
- this.SerializableContent = this.SerializableContent.TrimStart(',');
- }
- }
- }
- }
- /// <summary>
- /// Return a serializable or non serializable class/properties names
- /// for the specific flag.
- /// </summary>
- /// <param name="content">Serializable content</param>
- /// <param name="serializable">True - get serializable string, False - non serializable.</param>
- /// <returns>Serializable or non serializable string with class/properties names.</returns>
- protected string GetContentString(SerializationContents content, bool serializable)
- {
- switch(content)
- {
- case(SerializationContents .All):
- return "";
- case(SerializationContents .Default):
- return "";
- case(SerializationContents .Data):
- if(serializable)
- {
- return
- "Chart.BuildNumber, " +
- "Chart.Series, " +
- "Series.Points, " +
- "Series.Name, " +
- "DataPoint.XValue, " +
- "DataPoint.YValues," +
- "DataPoint.LabelStyle," +
- "DataPoint.AxisLabel," +
- "DataPoint.LabelFormat," +
- "DataPoint.IsEmpty, " +
- "Series.YValuesPerPoint, " +
- "Series.IsXValueIndexed, " +
- "Series.XValueType, " +
- "Series.YValueType";
- }
- return "";
- case(SerializationContents .Appearance):
- if(serializable)
- {
- return
- "Chart.BuildNumber, " +
- "*.Name*, " +
- "*.Fore*, " +
- "*.Back*, " +
- "*.Border*, " +
- "*.Line*, " +
- "*.Frame*, " +
- "*.PageColor*, " +
- "*.SkinStyle*, " +
- "*.Palette, " +
- "*.PaletteCustomColors, " +
- "*.Font*, " +
- "*.*Font, " +
- "*.Color, " +
- "*.Shadow*, " +
- "*.MarkerColor, " +
- "*.MarkerStyle, " +
- "*.MarkerSize, " +
- "*.MarkerBorderColor, " +
- "*.MarkerImage, " +
- "*.MarkerImageTransparentColor, " +
- "*.LabelBackColor, " +
- "*.LabelBorder*, " +
- "*.Enable3D, " +
- "*.IsRightAngleAxes, " +
- "*.IsClustered, " +
- "*.LightStyle, " +
- "*.Perspective, " +
- "*.Inclination, " +
- "*.Rotation, " +
- "*.PointDepth, " +
- "*.PointGapDepth, " +
- "*.WallWidth";
- }
- return "";
- default:
- throw (new InvalidOperationException(SR.ExceptionChartSerializerContentFlagUnsupported));
- }
- }
- /// <summary>
- /// Returns chart object for serialization.
- /// </summary>
- /// <returns>Chart object.</returns>
- internal Chart GetChartObject()
- {
- if(_chart == null)
- {
- _chart = (Chart)_serviceContainer.GetService(typeof(Chart));
- }
- return _chart;
- }
- #endregion
- }
- }
|