CommonElements.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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: CommonElements class provides references to common
  6. // chart classes like DataManager, ChartTypeRegistry,
  7. // ImageLoader and others. It is passed to different
  8. // chart elements to simplify access to those common
  9. // classes.
  10. //
  11. using System;
  12. using System.ComponentModel.Design;
  13. using System.Globalization;
  14. using FastReport.DataVisualization.Charting.Borders3D;
  15. using FastReport.DataVisualization.Charting.ChartTypes;
  16. using FastReport.DataVisualization.Charting.Data;
  17. using FastReport.DataVisualization.Charting.Formulas;
  18. using FastReport.DataVisualization.Charting.Utilities;
  19. namespace FastReport.DataVisualization.Charting
  20. {
  21. /// <summary>
  22. /// CommonElements class provides references to common chart classes like
  23. /// DataManager, ChartTypeRegistry, ImageLoader and others. It is passed
  24. /// to different chart elements to simplify access to those common classes.
  25. /// </summary>
  26. internal class CommonElements
  27. {
  28. #region Fields
  29. private Chart _chart;
  30. private ChartImage _chartPicture;
  31. // Reference to Chart Graphics Object
  32. internal ChartGraphics graph = null;
  33. /// <summary>
  34. /// Service Container
  35. /// </summary>
  36. internal IServiceContainer container = null;
  37. /// <summary>
  38. /// Indicates painting mode
  39. /// </summary>
  40. internal bool processModePaint = true;
  41. /// <summary>
  42. /// Indicates selection mode
  43. /// </summary>
  44. internal bool processModeRegions = false;
  45. // Private Fields
  46. private int _width = 0;
  47. private int _height = 0;
  48. #endregion
  49. #region Properties
  50. /// <summary>
  51. /// Reference to the Data Manager
  52. /// </summary>
  53. internal DataManager DataManager
  54. {
  55. get
  56. {
  57. return (DataManager)container.GetService(typeof(DataManager));
  58. }
  59. }
  60. /// <summary>
  61. /// True if painting mode is active
  62. /// </summary>
  63. public bool ProcessModePaint
  64. {
  65. get
  66. {
  67. return processModePaint;
  68. }
  69. }
  70. /// <summary>
  71. /// True if Hot region or image maps mode is active
  72. /// </summary>
  73. public bool ProcessModeRegions
  74. {
  75. get
  76. {
  77. return processModeRegions;
  78. }
  79. }
  80. /// <summary>
  81. /// Reference to the hot regions object
  82. /// </summary>
  83. public HotRegionsList HotRegionsList
  84. {
  85. get
  86. {
  87. return ChartPicture.hotRegionsList;
  88. }
  89. }
  90. /// <summary>
  91. /// Reference to the Data Manipulator
  92. /// </summary>
  93. public DataManipulator DataManipulator
  94. {
  95. get
  96. {
  97. return ChartPicture.DataManipulator;
  98. }
  99. }
  100. /// <summary>
  101. /// Reference to the ImageLoader
  102. /// </summary>
  103. internal ImageLoader ImageLoader
  104. {
  105. get
  106. {
  107. return (ImageLoader)container.GetService(typeof(ImageLoader));
  108. }
  109. }
  110. /// <summary>
  111. /// Reference to the Chart
  112. /// </summary>
  113. internal Chart Chart
  114. {
  115. get
  116. {
  117. if (_chart==null)
  118. _chart = (Chart)container.GetService(typeof(Chart));
  119. return _chart;
  120. }
  121. }
  122. /// <summary>
  123. /// Reference to the ChartTypeRegistry
  124. /// </summary>
  125. internal ChartTypeRegistry ChartTypeRegistry
  126. {
  127. get
  128. {
  129. return (ChartTypeRegistry)container.GetService(typeof(ChartTypeRegistry));
  130. }
  131. }
  132. /// <summary>
  133. /// Reference to the BorderTypeRegistry
  134. /// </summary>
  135. internal BorderTypeRegistry BorderTypeRegistry
  136. {
  137. get
  138. {
  139. return (BorderTypeRegistry)container.GetService(typeof(BorderTypeRegistry));
  140. }
  141. }
  142. /// <summary>
  143. /// Reference to the FormulaRegistry
  144. /// </summary>
  145. internal FormulaRegistry FormulaRegistry
  146. {
  147. get
  148. {
  149. return (FormulaRegistry)container.GetService(typeof(FormulaRegistry));
  150. }
  151. }
  152. /// <summary>
  153. /// Reference to the ChartPicture
  154. /// </summary>
  155. internal ChartImage ChartPicture
  156. {
  157. get
  158. {
  159. if (_chartPicture ==null)
  160. _chartPicture = (ChartImage)container.GetService(typeof(ChartImage));
  161. return _chartPicture;
  162. }
  163. }
  164. /// <summary>
  165. /// Width of the chart picture
  166. /// </summary>
  167. internal int Width
  168. {
  169. get
  170. {
  171. return _width;
  172. }
  173. set
  174. {
  175. _width = value;
  176. }
  177. }
  178. /// <summary>
  179. /// Height of the chart picture
  180. /// </summary>
  181. internal int Height
  182. {
  183. get
  184. {
  185. return _height;
  186. }
  187. set
  188. {
  189. _height = value;
  190. }
  191. }
  192. #endregion
  193. #region Methods
  194. /// <summary>
  195. /// Constructor
  196. /// </summary>
  197. /// <param name="container">Service container.</param>
  198. internal CommonElements(IServiceContainer container)
  199. {
  200. this.container = container;
  201. }
  202. #endregion
  203. #region String convertion helper methods
  204. /// <summary>
  205. /// Converts string to double.
  206. /// </summary>
  207. /// <param name="stringToParse">String to convert.</param>
  208. /// <returns>Double result.</returns>
  209. internal static double ParseDouble(string stringToParse)
  210. {
  211. return ParseDouble(stringToParse, false);
  212. }
  213. /// <summary>
  214. /// Converts string to double.
  215. /// </summary>
  216. /// <param name="stringToParse">String to convert.</param>
  217. /// <param name="throwException">if set to <c>true</c> the exception thrown.</param>
  218. /// <returns>Double result.</returns>
  219. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.Double.TryParse(System.String,System.Globalization.NumberStyles,System.IFormatProvider,System.Double@)")]
  220. internal static double ParseDouble(string stringToParse, bool throwException)
  221. {
  222. Double result = 0.0;
  223. if (throwException)
  224. {
  225. result = double.Parse(stringToParse, NumberStyles.Any, CultureInfo.InvariantCulture);
  226. }
  227. else
  228. {
  229. bool parseSucceed = double.TryParse(stringToParse, NumberStyles.Any, CultureInfo.InvariantCulture, out result);
  230. if (!parseSucceed)
  231. {
  232. double.TryParse(stringToParse, NumberStyles.Any, CultureInfo.CurrentCulture, out result);
  233. }
  234. }
  235. return result;
  236. }
  237. /// <summary>
  238. /// Converts string to double.
  239. /// </summary>
  240. /// <param name="stringToParse">String to convert.</param>
  241. /// <returns>Double result.</returns>
  242. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.Single.TryParse(System.String,System.Globalization.NumberStyles,System.IFormatProvider,System.Single@)")]
  243. internal static float ParseFloat(string stringToParse)
  244. {
  245. float result = 0f;
  246. bool parseSucceed = float.TryParse(stringToParse, NumberStyles.Any, CultureInfo.InvariantCulture, out result);
  247. if (!parseSucceed)
  248. {
  249. float.TryParse(stringToParse, NumberStyles.Any, CultureInfo.CurrentCulture, out result);
  250. }
  251. return result;
  252. }
  253. #endregion
  254. }
  255. }