BaseClasses.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. using System;
  5. using System.ComponentModel;
  6. using System.Diagnostics.CodeAnalysis;
  7. namespace FastReport.DataVisualization.Charting
  8. {
  9. /// <summary>
  10. /// ChartElement is the most basic element of the chart element hierarchy.
  11. /// </summary>
  12. public abstract class ChartElement : IChartElement, IDisposable
  13. {
  14. #region Member variables
  15. private IChartElement _parent = null;
  16. private CommonElements _common = null;
  17. private object _tag = null;
  18. #endregion
  19. #region Properties
  20. /// <summary>
  21. /// Gets or sets an object associated with this chart element.
  22. /// </summary>
  23. /// <value>
  24. /// An <see cref="Object"/> associated with this chart element.
  25. /// </value>
  26. /// <remarks>
  27. /// This property may be used to store additional data with this chart element.
  28. /// </remarks>
  29. [
  30. Browsable(false),
  31. DefaultValue(null),
  32. DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
  33. Utilities.SerializationVisibilityAttribute(Utilities.SerializationVisibility.Hidden)
  34. ]
  35. public object Tag
  36. {
  37. get { return _tag; }
  38. set { _tag = value; }
  39. }
  40. /// <summary>
  41. /// Gets or sets the parent chart element or collection.
  42. /// </summary>
  43. /// <value>The parent chart element or collection.</value>
  44. internal virtual IChartElement Parent
  45. {
  46. get { return _parent; }
  47. set { _parent = value; }
  48. }
  49. /// <summary>
  50. /// Gets a shortcut to Common intance providing access to the various chart related services.
  51. /// </summary>
  52. /// <value>The Common instance.</value>
  53. internal CommonElements Common
  54. {
  55. get
  56. {
  57. if (_common == null && _parent != null)
  58. {
  59. _common = _parent.Common;
  60. }
  61. return _common;
  62. }
  63. set
  64. {
  65. _common = value;
  66. }
  67. }
  68. /// <summary>
  69. /// Gets the chart.
  70. /// </summary>
  71. /// <value>The chart.</value>
  72. internal Chart Chart
  73. {
  74. get
  75. {
  76. if (Common != null)
  77. return Common.Chart;
  78. else
  79. return null;
  80. }
  81. }
  82. #endregion
  83. #region Constructors
  84. /// <summary>
  85. /// Initializes a new instance of the <see cref="ChartElement"/> class.
  86. /// </summary>
  87. protected ChartElement()
  88. {
  89. }
  90. /// <summary>
  91. /// Initializes a new instance of the <see cref="ChartElement"/> class.
  92. /// </summary>
  93. /// <param name="parent">The parent chart element or collection.</param>
  94. internal ChartElement(IChartElement parent)
  95. {
  96. _parent = parent;
  97. }
  98. #endregion
  99. #region Methods
  100. /// <summary>
  101. /// Invalidates this chart element.
  102. /// </summary>
  103. internal virtual void Invalidate()
  104. {
  105. if (_parent != null)
  106. _parent.Invalidate();
  107. }
  108. public void CallOnModifing()
  109. {
  110. Chart?.CallOnModifing(this);
  111. }
  112. #endregion
  113. #region IChartElement Members
  114. IChartElement IChartElement.Parent
  115. {
  116. get { return _parent; }
  117. set { this.Parent = value; }
  118. }
  119. void IChartElement.Invalidate()
  120. {
  121. this.Invalidate();
  122. }
  123. CommonElements IChartElement.Common
  124. {
  125. get{ return this.Common; }
  126. }
  127. #endregion
  128. #region IDisposable Members
  129. /// <summary>
  130. /// Releases unmanaged and - optionally - managed resources
  131. /// </summary>
  132. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  133. protected virtual void Dispose(bool disposing)
  134. {
  135. }
  136. /// <summary>
  137. /// Performs freeing, releasing, or resetting managed resources.
  138. /// </summary>
  139. [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
  140. public void Dispose()
  141. {
  142. this.Dispose(true);
  143. GC.SuppressFinalize(this);
  144. }
  145. #endregion
  146. #region Methods
  147. /// <summary>
  148. /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
  149. /// </summary>
  150. /// <returns>
  151. /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
  152. /// </returns>
  153. /// <remarks>For internal use.</remarks>
  154. internal virtual string ToStringInternal()
  155. {
  156. return GetType().Name;
  157. }
  158. /// <summary>
  159. /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
  160. /// </summary>
  161. /// <returns>
  162. /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
  163. /// </returns>
  164. [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
  165. public override string ToString()
  166. {
  167. return this.ToStringInternal();
  168. }
  169. /// <summary>
  170. /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
  171. /// </summary>
  172. /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
  173. /// <returns>
  174. /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
  175. /// </returns>
  176. /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
  177. /// <remarks>For internal use.</remarks>
  178. internal virtual bool EqualsInternal(object obj)
  179. {
  180. return base.Equals(obj);
  181. }
  182. /// <summary>
  183. /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
  184. /// </summary>
  185. /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
  186. /// <returns>
  187. /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
  188. /// </returns>
  189. /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
  190. [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
  191. public override bool Equals(object obj)
  192. {
  193. return this.EqualsInternal(obj);
  194. }
  195. /// <summary>
  196. /// Serves as a hash function for a particular type.
  197. /// </summary>
  198. /// <returns>
  199. /// A hash code for the current <see cref="T:System.Object"/>.
  200. /// </returns>
  201. [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
  202. public override int GetHashCode()
  203. {
  204. return base.GetHashCode();
  205. }
  206. #endregion
  207. }
  208. /// <summary>
  209. /// ChartNamedElement is a base class for most chart elements. Series, ChartAreas, Legends and other chart elements have a Name and reuse the unique name generation and validation logic provided by the ChartNamedElementCollection.
  210. /// </summary>
  211. public abstract class ChartNamedElement : ChartElement
  212. {
  213. #region Member variables
  214. private string _name = String.Empty;
  215. #endregion
  216. #region Properties
  217. /// <summary>
  218. /// Gets or sets the name of the chart element.
  219. /// </summary>
  220. /// <value>The name.</value>
  221. [DefaultValue("")]
  222. public virtual string Name
  223. {
  224. get { return _name; }
  225. set
  226. {
  227. if (_name != value)
  228. {
  229. if (Parent is INameController)
  230. {
  231. INameController nameController = Parent as INameController;
  232. if (!nameController.IsUniqueName(value))
  233. throw new ArgumentException(SR.ExceptionNameAlreadyExistsInCollection(value, nameController.GetType().Name));
  234. // Fire the name change events in case when the old name is not empty
  235. NameReferenceChangedEventArgs args = new NameReferenceChangedEventArgs(this, _name, value);
  236. nameController.OnNameReferenceChanging(args);
  237. _name = value;
  238. nameController.OnNameReferenceChanged(args);
  239. }
  240. else
  241. {
  242. _name = value;
  243. }
  244. Invalidate();
  245. }
  246. }
  247. }
  248. #endregion
  249. #region Constructors
  250. /// <summary>
  251. /// Initializes a new instance of the <see cref="ChartNamedElement"/> class.
  252. /// </summary>
  253. protected ChartNamedElement()
  254. : base()
  255. {
  256. }
  257. /// <summary>
  258. /// Initializes a new instance of the <see cref="ChartNamedElement"/> class.
  259. /// </summary>
  260. /// <param name="name">The name of the new chart element.</param>
  261. protected ChartNamedElement(string name)
  262. : base()
  263. {
  264. _name = name;
  265. }
  266. /// <summary>
  267. /// Initializes a new instance of the <see cref="ChartNamedElement"/> class.
  268. /// </summary>
  269. /// <param name="parent">The parent chart element.</param>
  270. /// <param name="name">The name of the new chart element.</param>
  271. internal ChartNamedElement(IChartElement parent, string name) : base(parent)
  272. {
  273. _name = name;
  274. }
  275. #endregion
  276. #region Methods
  277. /// <summary>
  278. /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
  279. /// </summary>
  280. /// <returns>
  281. /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
  282. /// </returns>
  283. internal override string ToStringInternal()
  284. {
  285. string typeName = GetType().Name;
  286. return (string.IsNullOrEmpty(_name)) ? typeName : typeName + '-' + _name;
  287. }
  288. #endregion
  289. }
  290. /// <summary>
  291. /// NameReferenceChanged events help chart maintain referencial integrity.
  292. /// </summary>
  293. internal class NameReferenceChangedEventArgs : EventArgs
  294. {
  295. #region MemberValiables
  296. ChartNamedElement _oldElement;
  297. string _oldName;
  298. string _newName;
  299. #endregion
  300. #region Properties
  301. public ChartNamedElement OldElement
  302. {
  303. get { return _oldElement; }
  304. }
  305. public string OldName
  306. {
  307. get { return _oldName; }
  308. }
  309. public string NewName
  310. {
  311. get { return _newName; }
  312. }
  313. #endregion
  314. #region Constructor
  315. public NameReferenceChangedEventArgs(ChartNamedElement oldElement, ChartNamedElement newElement)
  316. {
  317. _oldElement = oldElement;
  318. _oldName = oldElement!=null ? oldElement.Name : string.Empty;
  319. _newName = newElement!=null ? newElement.Name : string.Empty;
  320. }
  321. public NameReferenceChangedEventArgs(ChartNamedElement oldElement, string oldName, string newName)
  322. {
  323. _oldElement = oldElement;
  324. _oldName = oldName;
  325. _newName = newName;
  326. }
  327. #endregion
  328. }
  329. }