ChartSerializer.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. //
  5. // Purpose: Serialization saves the state of the chart and also
  6. // provides the ability to load the serialized data back
  7. // into the chart. All chart properties can be persisted,
  8. // including the chart's data.
  9. //
  10. using System;
  11. using System.ComponentModel;
  12. using System.ComponentModel.Design;
  13. using System.IO;
  14. using FastReport.DataVisualization.Charting.Utilities;
  15. using System.Xml;
  16. namespace FastReport.DataVisualization.Charting
  17. {
  18. #region Serialization enumeration
  19. /// <summary>
  20. /// An enumeration of the formats of the chart serializer.
  21. /// </summary>
  22. public enum SerializationFormat
  23. {
  24. /// <summary>
  25. /// XML serializer format.
  26. /// </summary>
  27. Xml,
  28. /// <summary>
  29. /// Binary serializer format.
  30. /// </summary>
  31. Binary
  32. }
  33. /// <summary>
  34. /// An enumeration of chart serializable content definition flags
  35. /// </summary>
  36. [Flags]
  37. public enum SerializationContents
  38. {
  39. /// <summary>
  40. /// Default content.
  41. /// </summary>
  42. Default = 1,
  43. /// <summary>
  44. /// Serialize only series data.
  45. /// </summary>
  46. Data = 2,
  47. /// <summary>
  48. /// Serialize chart visual appearance (e.g. Colors, Line Styles).
  49. /// </summary>
  50. Appearance = 4,
  51. /// <summary>
  52. /// All content is serialized.
  53. /// </summary>
  54. All = Default | Data | Appearance
  55. }
  56. #endregion
  57. /// <summary>
  58. /// ChartSerializer class provides chart serialization.
  59. /// </summary>
  60. [
  61. SRDescription("DescriptionAttributeChartSerializer_ChartSerializer"),
  62. DefaultProperty("Format"),
  63. ]
  64. public class ChartSerializer
  65. {
  66. #region Private fields
  67. // Reference to the service container
  68. private IServiceContainer _serviceContainer = null;
  69. // Reference to the chart object
  70. private Chart _chart = null;
  71. // Reference to the serializer object
  72. private SerializerBase _serializer = new XmlFormatSerializer();
  73. // Format of the serializer in use
  74. private SerializationFormat _format = SerializationFormat.Xml;
  75. // Serialization content
  76. private SerializationContents _content = SerializationContents .Default;
  77. #endregion
  78. #region Constructors and Service Provider methods
  79. /// <summary>
  80. /// Default constructor is unavailable
  81. /// </summary>
  82. private ChartSerializer()
  83. {
  84. }
  85. /// <summary>
  86. /// Internal constructor
  87. /// </summary>
  88. /// <param name="container">Service container reference.</param>
  89. internal ChartSerializer(IServiceContainer container)
  90. {
  91. if(container == null)
  92. {
  93. throw(new ArgumentNullException(SR.ExceptionInvalidServiceContainer));
  94. }
  95. _serviceContainer = container;
  96. }
  97. /// <summary>
  98. /// Returns ChartSerializer service object
  99. /// </summary>
  100. /// <param name="serviceType">Requested service type.</param>
  101. /// <returns>ChartSerializer service object.</returns>
  102. internal object GetService(Type serviceType)
  103. {
  104. if(serviceType == typeof(ChartSerializer))
  105. {
  106. return this;
  107. }
  108. throw (new ArgumentException( SR.ExceptionChartSerializerUnsupportedType( serviceType.ToString())));
  109. }
  110. #endregion
  111. #region Public properties
  112. /// <summary>
  113. /// Gets or sets the serializable content.
  114. /// </summary>
  115. [
  116. SRCategory("CategoryAttributeMisc"),
  117. DefaultValue(typeof(SerializationContents ), "Default"),
  118. SRDescription("DescriptionAttributeChartSerializer_Content")
  119. ]
  120. public SerializationContents Content
  121. {
  122. get
  123. {
  124. return _content;
  125. }
  126. set
  127. {
  128. // Set content value
  129. _content = value;
  130. // Automatically set SerializableContent and NonSerializableContent properties
  131. SetSerializableContent();
  132. }
  133. }
  134. /// <summary>
  135. /// Gets or sets the format used to serialize the chart data.
  136. /// </summary>
  137. [
  138. SRCategory("CategoryAttributeMisc"),
  139. DefaultValue(typeof(SerializationFormat), "Xml"),
  140. SRDescription("DescriptionAttributeChartSerializer_Format")
  141. ]
  142. public SerializationFormat Format
  143. {
  144. get
  145. {
  146. return _format;
  147. }
  148. set
  149. {
  150. if(_format != value)
  151. {
  152. _format = value;
  153. // Create new serializer object
  154. SerializerBase newSerializer = null;
  155. if(_format == SerializationFormat.Binary)
  156. {
  157. newSerializer = new BinaryFormatSerializer();
  158. }
  159. else
  160. {
  161. newSerializer = new XmlFormatSerializer();
  162. }
  163. // Copy serializer settings
  164. newSerializer.IsUnknownAttributeIgnored = _serializer.IsUnknownAttributeIgnored;
  165. newSerializer.NonSerializableContent = _serializer.NonSerializableContent;
  166. newSerializer.IsResetWhenLoading = _serializer.IsResetWhenLoading;
  167. newSerializer.SerializableContent = _serializer.SerializableContent;
  168. _serializer = newSerializer;
  169. }
  170. }
  171. }
  172. /// <summary>
  173. /// Gets or sets a flag which indicates whether object properties are reset to default
  174. /// values before loading.
  175. /// </summary>
  176. [
  177. SRCategory("CategoryAttributeMisc"),
  178. DefaultValue(true),
  179. SRDescription("DescriptionAttributeChartSerializer_ResetWhenLoading")
  180. ]
  181. public bool IsResetWhenLoading
  182. {
  183. get
  184. {
  185. return _serializer.IsResetWhenLoading;
  186. }
  187. set
  188. {
  189. _serializer.IsResetWhenLoading = value;
  190. }
  191. }
  192. /// <summary>
  193. /// Gets or sets a flag which indicates whether unknown XML properties and elements will be
  194. /// ignored without throwing an exception.
  195. /// </summary>
  196. [
  197. SRCategory("CategoryAttributeMisc"),
  198. DefaultValue(false),
  199. SRDescription("DescriptionAttributeChartSerializer_IgnoreUnknownXmlAttributes")
  200. ]
  201. public bool IsUnknownAttributeIgnored
  202. {
  203. get
  204. {
  205. return _serializer.IsUnknownAttributeIgnored;
  206. }
  207. set
  208. {
  209. _serializer.IsUnknownAttributeIgnored = value;
  210. }
  211. }
  212. /// <summary>
  213. /// Gets or sets a flag which indicates whether chart
  214. /// serializer is working in template creation mode.
  215. /// </summary>
  216. [
  217. SRCategory("CategoryAttributeMisc"),
  218. DefaultValue(false),
  219. SRDescription("DescriptionAttributeChartSerializer_TemplateMode")
  220. ]
  221. public bool IsTemplateMode
  222. {
  223. get
  224. {
  225. return _serializer.IsTemplateMode;
  226. }
  227. set
  228. {
  229. _serializer.IsTemplateMode = value;
  230. }
  231. }
  232. /// <summary>
  233. /// Gets or sets the chart properties that can be serialized.
  234. /// Comma separated list of serializable (Save/Load/Reset) properties.
  235. /// "ClassName.PropertyName,[ClassName.PropertyName]".
  236. /// </summary>
  237. [
  238. SRCategory("CategoryAttributeMisc"),
  239. DefaultValue(""),
  240. SRDescription("DescriptionAttributeChartSerializer_SerializableContent")
  241. ]
  242. public string SerializableContent
  243. {
  244. get
  245. {
  246. return _serializer.SerializableContent;
  247. }
  248. set
  249. {
  250. _serializer.SerializableContent = value;
  251. }
  252. }
  253. /// <summary>
  254. /// Gets or sets the chart properties that will not be serialized.
  255. /// Comma separated list of non-serializable (Save/Load/Reset) properties.
  256. /// "ClassName.PropertyName,[ClassName.PropertyName]".
  257. /// </summary>
  258. [
  259. SRCategory("CategoryAttributeMisc"),
  260. DefaultValue(""),
  261. SRDescription("DescriptionAttributeChartSerializer_NonSerializableContent")
  262. ]
  263. public string NonSerializableContent
  264. {
  265. get
  266. {
  267. return _serializer.NonSerializableContent;
  268. }
  269. set
  270. {
  271. _serializer.NonSerializableContent = value;
  272. }
  273. }
  274. #endregion
  275. #region Public methods
  276. /// <summary>
  277. /// This method resets all properties of the chart to default values. By setting Content or
  278. /// SerializableContent/NonSerializableContent properties, specific set of
  279. /// properties can be reset.
  280. /// </summary>
  281. public void Reset()
  282. {
  283. // Set serializing flag
  284. if(GetChartObject() != null)
  285. {
  286. GetChartObject().serializing = true;
  287. GetChartObject().serializationStatus = SerializationStatus.Resetting;
  288. }
  289. // Reset properties
  290. _serializer.ResetObjectProperties(GetChartObject());
  291. // Clear serializing flag
  292. if(GetChartObject() != null)
  293. {
  294. GetChartObject().serializing = false;
  295. GetChartObject().serializationStatus = SerializationStatus.None;
  296. }
  297. }
  298. /// <summary>
  299. /// This method saves all properties of the chart into a file. By setting Content or
  300. /// SerializableContent/NonSerializableContent properties, specific set of
  301. /// properties can be saved.
  302. /// </summary>
  303. /// <param name="fileName">The file name used to write the data.</param>
  304. public void Save(string fileName)
  305. {
  306. //Check arguments
  307. if (fileName == null)
  308. throw new ArgumentNullException("fileName");
  309. // Set serializing flag
  310. if(GetChartObject() != null)
  311. {
  312. GetChartObject().serializing = true;
  313. GetChartObject().serializationStatus = SerializationStatus.Saving;
  314. //GetChartObject().BeginInit();
  315. }
  316. // Reset all auto-detected properties values
  317. GetChartObject().ResetAutoValues();
  318. // Serialize chart data
  319. _serializer.Serialize(GetChartObject(), fileName);
  320. // Clear serializing flag
  321. if(GetChartObject() != null)
  322. {
  323. GetChartObject().serializing = false;
  324. GetChartObject().serializationStatus = SerializationStatus.None;
  325. //GetChartObject().EndInit();
  326. }
  327. }
  328. /// <summary>
  329. /// This method saves all properties of the chart into a stream. By setting Content or
  330. /// SerializableContent/NonSerializableContent properties, specific set of
  331. /// properties can be saved.
  332. /// </summary>
  333. /// <param name="stream">The stream where to save the data.</param>
  334. public void Save(Stream stream)
  335. {
  336. //Check arguments
  337. if (stream == null)
  338. throw new ArgumentNullException("stream");
  339. // Set serializing flag
  340. if(GetChartObject() != null)
  341. {
  342. GetChartObject().serializing = true;
  343. GetChartObject().serializationStatus = SerializationStatus.Saving;
  344. //GetChartObject().BeginInit();
  345. }
  346. // Reset all auto-detected properties values
  347. GetChartObject().ResetAutoValues();
  348. // Serialize chart data
  349. _serializer.Serialize(GetChartObject(), stream);
  350. // Clear serializing flag
  351. if(GetChartObject() != null)
  352. {
  353. GetChartObject().serializing = false;
  354. GetChartObject().serializationStatus = SerializationStatus.None;
  355. //GetChartObject().EndInit();
  356. }
  357. }
  358. /// <summary>
  359. /// This method saves all properties of the chart into an XML writer. By setting Content or
  360. /// SerializableContent/NonSerializableContent properties, specific set of
  361. /// properties can be saved.
  362. /// </summary>
  363. /// <param name="writer">XML writer to save the data.</param>
  364. public void Save(XmlWriter writer)
  365. {
  366. //Check arguments
  367. if (writer == null)
  368. throw new ArgumentNullException("writer");
  369. // Set serializing flag
  370. if(GetChartObject() != null)
  371. {
  372. GetChartObject().serializing = true;
  373. GetChartObject().serializationStatus = SerializationStatus.Saving;
  374. //GetChartObject().BeginInit();
  375. }
  376. // Reset all auto-detected properties values
  377. GetChartObject().ResetAutoValues();
  378. // Serialize chart data
  379. _serializer.Serialize(GetChartObject(), writer);
  380. // Clear serializing flag
  381. if(GetChartObject() != null)
  382. {
  383. GetChartObject().serializing = false;
  384. GetChartObject().serializationStatus = SerializationStatus.None;
  385. //GetChartObject().EndInit();
  386. }
  387. }
  388. /// <summary>
  389. /// This method saves all properties of the chart into a text writer. By setting Content or
  390. /// SerializableContent/NonSerializableContent properties, specific set of
  391. /// properties can be saved.
  392. /// </summary>
  393. /// <param name="writer">Text writer to save the data.</param>
  394. public void Save(TextWriter writer)
  395. {
  396. //Check arguments
  397. if (writer == null)
  398. throw new ArgumentNullException("writer");
  399. // Set serializing flag
  400. if(GetChartObject() != null)
  401. {
  402. GetChartObject().serializing = true;
  403. GetChartObject().serializationStatus = SerializationStatus.Saving;
  404. //GetChartObject().BeginInit();
  405. }
  406. // Reset all auto-detected properties values
  407. GetChartObject().ResetAutoValues();
  408. // Serialize chart data
  409. _serializer.Serialize(GetChartObject(), writer);
  410. // Clear serializing flag
  411. if(GetChartObject() != null)
  412. {
  413. GetChartObject().serializing = false;
  414. GetChartObject().serializationStatus = SerializationStatus.None;
  415. //GetChartObject().EndInit();
  416. }
  417. }
  418. /// <summary>
  419. /// This method loads all properties of the chart from a file. By setting Content or
  420. /// SerializableContent/NonSerializableContent properties, specific set of
  421. /// properties can be loaded.
  422. /// </summary>
  423. /// <param name="fileName">The file to load the data from.</param>
  424. public void Load(string fileName)
  425. {
  426. //Check arguments
  427. if (fileName == null)
  428. throw new ArgumentNullException("fileName");
  429. // Set serializing flag
  430. if(GetChartObject() != null)
  431. {
  432. GetChartObject().serializing = true;
  433. GetChartObject().serializationStatus = SerializationStatus.Loading;
  434. }
  435. _serializer.Deserialize(GetChartObject(), fileName);
  436. // Clear serializing flag
  437. if(GetChartObject() != null)
  438. {
  439. GetChartObject().serializing = false;
  440. GetChartObject().serializationStatus = SerializationStatus.None;
  441. }
  442. }
  443. /// <summary>
  444. /// This method loads all properties of the chart from a stream. By setting Content or
  445. /// SerializableContent/NonSerializableContent properties, specific set of
  446. /// properties can be loaded.
  447. /// </summary>
  448. /// <param name="stream">The stream to load the data from.</param>
  449. public void Load(Stream stream)
  450. {
  451. //Check arguments
  452. if (stream == null)
  453. throw new ArgumentNullException("stream");
  454. // Set serializing flag
  455. if(GetChartObject() != null)
  456. {
  457. GetChartObject().serializing = true;
  458. GetChartObject().serializationStatus = SerializationStatus.Loading;
  459. }
  460. _serializer.Deserialize(GetChartObject(), stream);
  461. // Clear serializing flag
  462. if(GetChartObject() != null)
  463. {
  464. GetChartObject().serializing = false;
  465. GetChartObject().serializationStatus = SerializationStatus.None;
  466. }
  467. }
  468. /// <summary>
  469. /// This method loads all properties of the chart from an XML reader. By setting Content or
  470. /// SerializableContent/NonSerializableContent properties, specific set of
  471. /// properties can be loaded.
  472. /// </summary>
  473. /// <param name="reader">The XML reader to load the data from.</param>
  474. public void Load(XmlReader reader)
  475. {
  476. //Check arguments
  477. if (reader == null)
  478. throw new ArgumentNullException("reader");
  479. // Set serializing flag
  480. if(GetChartObject() != null)
  481. {
  482. GetChartObject().serializing = true;
  483. GetChartObject().serializationStatus = SerializationStatus.Loading;
  484. }
  485. _serializer.Deserialize(GetChartObject(), reader);
  486. // Clear serializing flag
  487. if(GetChartObject() != null)
  488. {
  489. GetChartObject().serializing = false;
  490. GetChartObject().serializationStatus = SerializationStatus.None;
  491. }
  492. }
  493. /// <summary>
  494. /// This method loads all properties of the chart from the text reader. By setting Content or
  495. /// SerializableContent/NonSerializableContent properties, specific set of
  496. /// properties can be loaded.
  497. /// </summary>
  498. /// <param name="reader">The text reader to load the data from.</param>
  499. public void Load(TextReader reader)
  500. {
  501. //Check arguments
  502. if (reader == null)
  503. throw new ArgumentNullException("reader");
  504. // Set serializing flag
  505. if(GetChartObject() != null)
  506. {
  507. GetChartObject().serializing = true;
  508. GetChartObject().serializationStatus = SerializationStatus.Loading;
  509. }
  510. _serializer.Deserialize(GetChartObject(), reader);
  511. // Clear serializing flag
  512. if(GetChartObject() != null)
  513. {
  514. GetChartObject().serializing = false;
  515. GetChartObject().serializationStatus = SerializationStatus.None;
  516. }
  517. }
  518. #endregion
  519. #region Protected helper methods
  520. /// <summary>
  521. /// Sets SerializableContent and NonSerializableContent properties
  522. /// depending on the flags in the Content property.
  523. /// </summary>
  524. internal void SetSerializableContent()
  525. {
  526. // Reset content definition strings
  527. this.SerializableContent = "";
  528. this.NonSerializableContent = "";
  529. // Loop through all enumeration flags
  530. Array enumValues = Enum.GetValues(typeof(SerializationContents ));
  531. foreach(object flagObject in enumValues)
  532. {
  533. if(flagObject is SerializationContents )
  534. {
  535. // Check if flag currently set
  536. SerializationContents flag = (SerializationContents )flagObject;
  537. if((this.Content & flag) == flag &&
  538. flag != SerializationContents .All &&
  539. this.Content != SerializationContents .All)
  540. {
  541. // Add comma at the end of existing string
  542. if(this.NonSerializableContent.Length != 0)
  543. {
  544. this.NonSerializableContent += ", ";
  545. }
  546. // Add serializable class/properties names
  547. this.NonSerializableContent += GetContentString(flag, false);
  548. this.NonSerializableContent = this.NonSerializableContent.TrimStart(',');
  549. // Add comma at the end of existing string
  550. if(this.SerializableContent.Length != 0)
  551. {
  552. this.SerializableContent += ", ";
  553. }
  554. // Add serializable class/properties names
  555. this.SerializableContent += GetContentString(flag, true);
  556. this.SerializableContent = this.SerializableContent.TrimStart(',');
  557. }
  558. }
  559. }
  560. }
  561. /// <summary>
  562. /// Return a serializable or non serializable class/properties names
  563. /// for the specific flag.
  564. /// </summary>
  565. /// <param name="content">Serializable content</param>
  566. /// <param name="serializable">True - get serializable string, False - non serializable.</param>
  567. /// <returns>Serializable or non serializable string with class/properties names.</returns>
  568. protected string GetContentString(SerializationContents content, bool serializable)
  569. {
  570. switch(content)
  571. {
  572. case(SerializationContents .All):
  573. return "";
  574. case(SerializationContents .Default):
  575. return "";
  576. case(SerializationContents .Data):
  577. if(serializable)
  578. {
  579. return
  580. "Chart.BuildNumber, " +
  581. "Chart.Series, " +
  582. "Series.Points, " +
  583. "Series.Name, " +
  584. "DataPoint.XValue, " +
  585. "DataPoint.YValues," +
  586. "DataPoint.LabelStyle," +
  587. "DataPoint.AxisLabel," +
  588. "DataPoint.LabelFormat," +
  589. "DataPoint.IsEmpty, " +
  590. "Series.YValuesPerPoint, " +
  591. "Series.IsXValueIndexed, " +
  592. "Series.XValueType, " +
  593. "Series.YValueType";
  594. }
  595. return "";
  596. case(SerializationContents .Appearance):
  597. if(serializable)
  598. {
  599. return
  600. "Chart.BuildNumber, " +
  601. "*.Name*, " +
  602. "*.Fore*, " +
  603. "*.Back*, " +
  604. "*.Border*, " +
  605. "*.Line*, " +
  606. "*.Frame*, " +
  607. "*.PageColor*, " +
  608. "*.SkinStyle*, " +
  609. "*.Palette, " +
  610. "*.PaletteCustomColors, " +
  611. "*.Font*, " +
  612. "*.*Font, " +
  613. "*.Color, " +
  614. "*.Shadow*, " +
  615. "*.MarkerColor, " +
  616. "*.MarkerStyle, " +
  617. "*.MarkerSize, " +
  618. "*.MarkerBorderColor, " +
  619. "*.MarkerImage, " +
  620. "*.MarkerImageTransparentColor, " +
  621. "*.LabelBackColor, " +
  622. "*.LabelBorder*, " +
  623. "*.Enable3D, " +
  624. "*.IsRightAngleAxes, " +
  625. "*.IsClustered, " +
  626. "*.LightStyle, " +
  627. "*.Perspective, " +
  628. "*.Inclination, " +
  629. "*.Rotation, " +
  630. "*.PointDepth, " +
  631. "*.PointGapDepth, " +
  632. "*.WallWidth";
  633. }
  634. return "";
  635. default:
  636. throw (new InvalidOperationException(SR.ExceptionChartSerializerContentFlagUnsupported));
  637. }
  638. }
  639. /// <summary>
  640. /// Returns chart object for serialization.
  641. /// </summary>
  642. /// <returns>Chart object.</returns>
  643. internal Chart GetChartObject()
  644. {
  645. if(_chart == null)
  646. {
  647. _chart = (Chart)_serviceContainer.GetService(typeof(Chart));
  648. }
  649. return _chart;
  650. }
  651. #endregion
  652. }
  653. }