LegendConverters.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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: Converter classes for Legend.
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.ComponentModel;
  10. using System.Globalization;
  11. using System.Drawing;
  12. #if NETSTANDARD || !NETCOREWIN
  13. using FastReport.TypeConverters;
  14. #endif
  15. namespace FastReport.DataVisualization.Charting
  16. {
  17. /// <summary>
  18. /// Chart area name converter. Displays list of available areas names
  19. /// </summary>
  20. internal class LegendAreaNameConverter : StringConverter
  21. {
  22. #region Converter methods
  23. /// <summary>
  24. /// Standart values supported - return true
  25. /// </summary>
  26. /// <param name="context">Descriptor context.</param>
  27. /// <returns>Standard values supported.</returns>
  28. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  29. {
  30. return true;
  31. }
  32. /// <summary>
  33. /// Standart values are not exclusive - return false
  34. /// </summary>
  35. /// <param name="context">Descriptor context.</param>
  36. /// <returns>Non exclusive standard values.</returns>
  37. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  38. {
  39. return false;
  40. }
  41. /// <summary>
  42. /// Fill in the list of data series names.
  43. /// </summary>
  44. /// <param name="context">Descriptor context.</param>
  45. /// <returns>Standart values collection.</returns>
  46. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  47. {
  48. ArrayList values = new ArrayList();
  49. values.Add(Constants.NotSetValue);
  50. ChartAreaCollection areaCollection = null;
  51. string areaName = "";
  52. if (context != null && context.Instance != null)
  53. {
  54. if (context.Instance is Legend)
  55. {
  56. Legend legend = (Legend)context.Instance;
  57. if(legend.Common != null && legend.Common.ChartPicture != null)
  58. {
  59. areaCollection = legend.Common.ChartPicture.ChartAreas;
  60. }
  61. }
  62. else if (context.Instance is ChartArea)
  63. {
  64. ChartArea area = (ChartArea)context.Instance;
  65. if(area.Common != null && area.Common.ChartPicture != null)
  66. {
  67. areaCollection = area.Common.ChartPicture.ChartAreas;
  68. areaName = area.Name;
  69. }
  70. }
  71. else if (context.Instance is Title)
  72. {
  73. Title title = (Title)context.Instance;
  74. if(title.Chart != null && title.Chart.chartPicture != null)
  75. {
  76. areaCollection = title.Chart.chartPicture.ChartAreas;
  77. }
  78. }
  79. else if (context.Instance is Annotation)
  80. {
  81. Annotation annotation = (Annotation)context.Instance;
  82. if(annotation.Chart != null && annotation.Chart.chartPicture != null)
  83. {
  84. areaCollection = annotation.Chart.chartPicture.ChartAreas;
  85. }
  86. }
  87. else if (context.Instance is IServiceProvider)
  88. {
  89. IServiceProvider provider = context.Instance as IServiceProvider;
  90. Chart chart = provider.GetService(typeof(Chart)) as Chart;
  91. if (chart != null)
  92. {
  93. areaCollection = chart.ChartAreas;
  94. }
  95. }
  96. else if (context.Instance is Array)
  97. {
  98. if (((Array)context.Instance).Length > 0 && ((Array)context.Instance).GetValue(0) is Legend)
  99. {
  100. Legend legend = (Legend)((Array)context.Instance).GetValue(0);
  101. if (legend.Common != null && legend.Common.ChartPicture != null)
  102. {
  103. areaCollection = legend.Common.ChartPicture.ChartAreas;
  104. }
  105. }
  106. else if (((Array)context.Instance).Length > 0 && ((Array)context.Instance).GetValue(0) is ChartArea)
  107. {
  108. ChartArea area = (ChartArea)((Array)context.Instance).GetValue(0);
  109. if (area.Common != null && area.Common.ChartPicture != null)
  110. {
  111. areaCollection = area.Common.ChartPicture.ChartAreas;
  112. }
  113. }
  114. else if (((Array)context.Instance).Length > 0 && ((Array)context.Instance).GetValue(0) is Title)
  115. {
  116. Title title = (Title)((Array)context.Instance).GetValue(0);
  117. if (title.Chart != null && title.Chart.chartPicture != null)
  118. {
  119. areaCollection = title.Chart.chartPicture.ChartAreas;
  120. }
  121. }
  122. else if (((Array)context.Instance).Length > 0 && ((Array)context.Instance).GetValue(0) is Annotation)
  123. {
  124. Annotation annotation = (Annotation)((Array)context.Instance).GetValue(0);
  125. if (annotation.Chart != null && annotation.Chart.chartPicture != null)
  126. {
  127. areaCollection = annotation.Chart.chartPicture.ChartAreas;
  128. }
  129. }
  130. }
  131. }
  132. if (areaCollection != null)
  133. {
  134. foreach(ChartArea area in areaCollection)
  135. {
  136. if(area.Name != areaName)
  137. {
  138. values.Add(area.Name);
  139. }
  140. }
  141. }
  142. return new StandardValuesCollection(values);
  143. }
  144. #endregion
  145. }
  146. /// <summary>
  147. /// Legend converter
  148. /// </summary>
  149. internal class LegendConverter : NoNameExpandableObjectConverter
  150. {
  151. }
  152. /// <summary>
  153. /// Designer converter class
  154. /// Converts Size.Emty tofrom "Auto".
  155. /// </summary>
  156. internal class SizeEmptyValueConverter : SizeConverter
  157. {
  158. #region Converter methods
  159. /// <summary>
  160. /// Standard values supported - return true
  161. /// </summary>
  162. /// <param name="context">Descriptor context.</param>
  163. /// <returns>Standard values supported.</returns>
  164. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  165. {
  166. return true;
  167. }
  168. /// <summary>
  169. /// Standard values are not exclusive - return false
  170. /// </summary>
  171. /// <param name="context">Descriptor context.</param>
  172. /// <returns>Non exclusive standard values.</returns>
  173. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  174. {
  175. return false;
  176. }
  177. /// <summary>
  178. /// Fill in the list of predefined values.
  179. /// </summary>
  180. /// <param name="context">Descriptor context.</param>
  181. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  182. {
  183. ArrayList values = new ArrayList();
  184. values.Add(System.Drawing.Size.Empty);
  185. return new StandardValuesCollection(values);
  186. }
  187. /// <summary>
  188. /// Convert Size.IsEmpty value to string "Auto"
  189. /// </summary>
  190. /// <param name="context">Descriptor context.</param>
  191. /// <param name="culture">Culture information.</param>
  192. /// <param name="value">Value to convert.</param>
  193. /// <param name="destinationType">Convertion destination type.</param>
  194. /// <returns>Converted object.</returns>
  195. public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
  196. {
  197. if (destinationType == typeof(string))
  198. {
  199. if(((System.Drawing.Size)value).IsEmpty)
  200. {
  201. return Constants.AutoValue;
  202. }
  203. }
  204. // Call base class
  205. return base.ConvertTo(context, culture, value, destinationType);
  206. }
  207. /// <summary>
  208. /// Convert minimum or maximum values from string
  209. /// </summary>
  210. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  211. {
  212. // If converting from string value
  213. string stringValue = value as string;
  214. if (stringValue != null)
  215. {
  216. if (String.Compare(stringValue, Constants.AutoValue, StringComparison.OrdinalIgnoreCase) == 0)
  217. {
  218. return System.Drawing.Size.Empty;
  219. }
  220. }
  221. // Call base converter
  222. return base.ConvertFrom(context, culture, value);
  223. }
  224. #endregion
  225. }
  226. /// <summary>
  227. /// Data point properties converter
  228. /// </summary>
  229. internal class MarginExpandableObjectConverter : ExpandableObjectConverter
  230. {
  231. #region Converter methods
  232. /// <summary>
  233. /// This method overrides CanConvertTo from TypeConverter. This is called when someone
  234. /// wants to convert an instance of object to another type. Here,
  235. /// only conversion to an InstanceDescriptor is supported.
  236. /// </summary>
  237. /// <param name="context">Descriptor context.</param>
  238. /// <param name="destinationType">Destination type.</param>
  239. /// <returns>True if object can be converted.</returns>
  240. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  241. {
  242. if (destinationType == typeof(string))
  243. {
  244. return true;
  245. }
  246. // Always call the base to see if it can perform the conversion.
  247. return base.CanConvertTo(context, destinationType);
  248. }
  249. /// <summary>
  250. /// Returns whether this converter can convert an object of the given type
  251. /// to the type of this converter, using the specified context.
  252. /// </summary>
  253. /// <param name="context">Descriptor context.</param>
  254. /// <param name="sourceType">Source type.</param>
  255. /// <returns>True if object can be converted.</returns>
  256. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  257. {
  258. if (sourceType == typeof(string))
  259. {
  260. return true;
  261. }
  262. // Always call the base to see if it can perform the conversion.
  263. return base.CanConvertFrom(context, sourceType);
  264. }
  265. /// <summary>
  266. /// This code performs the actual conversion from an object to a string.
  267. /// </summary>
  268. /// <param name="context">Descriptor context.</param>
  269. /// <param name="culture">Culture information.</param>
  270. /// <param name="value">Object value.</param>
  271. /// <param name="destinationType">Destination type.</param>
  272. /// <returns>Converted object.</returns>
  273. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  274. {
  275. Margins margins = value as Margins;
  276. if (destinationType == typeof(string) && margins != null)
  277. {
  278. return string.Format(
  279. CultureInfo.InvariantCulture,
  280. "{0:D}, {1:D}, {2:D}, {3:D}",
  281. margins.Top,
  282. margins.Bottom,
  283. margins.Left,
  284. margins.Right);
  285. }
  286. // Always call base, even if you can't convert.
  287. return base.ConvertTo(context, culture, value, destinationType);
  288. }
  289. /// <summary>
  290. /// Overrides the ConvertFrom method of TypeConverter.
  291. /// </summary>
  292. /// <param name="context">Descriptor context.</param>
  293. /// <param name="culture">Culture information.</param>
  294. /// <param name="value">Value to convert from.</param>
  295. /// <returns>Converted object.</returns>
  296. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  297. {
  298. // Can convert from string where each array element is separated by comma
  299. string stringValue = value as string;
  300. if (stringValue != null)
  301. {
  302. Margins margins = new Margins();
  303. string[] values = stringValue.Split(',');
  304. if(values.Length == 4)
  305. {
  306. try
  307. {
  308. margins.Top = int.Parse(values[0].Trim(), CultureInfo.InvariantCulture);
  309. margins.Bottom = int.Parse(values[1].Trim(), CultureInfo.InvariantCulture);
  310. margins.Left = int.Parse(values[2].Trim(), CultureInfo.InvariantCulture);
  311. margins.Right = int.Parse(values[3].Trim(), CultureInfo.InvariantCulture);
  312. }
  313. catch
  314. {
  315. throw (new InvalidOperationException(SR.ExceptionLegendDesignerMarginObjectInvalid(stringValue)));
  316. }
  317. }
  318. else
  319. {
  320. throw (new InvalidOperationException(SR.ExceptionLegendDesignerMarginObjectInvalid(stringValue)));
  321. }
  322. return margins;
  323. }
  324. // Call base class
  325. return base.ConvertFrom(context, culture, value);
  326. }
  327. #endregion
  328. }
  329. /// <summary>
  330. /// Designer converter class
  331. /// Converts Integer value -1 to/from "Auto".
  332. /// </summary>
  333. internal class IntNanValueConverter : Int32Converter
  334. {
  335. #region Converter methods
  336. /// <summary>
  337. /// Standard values supported - return true
  338. /// </summary>
  339. /// <param name="context">Descriptor context.</param>
  340. /// <returns>Standard values supported.</returns>
  341. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  342. {
  343. return true;
  344. }
  345. /// <summary>
  346. /// Standard values are not exclusive - return false
  347. /// </summary>
  348. /// <param name="context">Descriptor context.</param>
  349. /// <returns>Non exclusive standard values.</returns>
  350. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  351. {
  352. return false;
  353. }
  354. /// <summary>
  355. /// Fill in the list of predefined values.
  356. /// </summary>
  357. /// <param name="context">Descriptor context.</param>
  358. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  359. {
  360. ArrayList values = new ArrayList();
  361. values.Add(-1);
  362. return new StandardValuesCollection(values);
  363. }
  364. /// <summary>
  365. /// Convert integer value -1 to string "Auto"
  366. /// </summary>
  367. /// <param name="context">Descriptor context.</param>
  368. /// <param name="culture">Culture information.</param>
  369. /// <param name="value">Value to convert.</param>
  370. /// <param name="destinationType">Convertion destination type.</param>
  371. /// <returns>Converted object.</returns>
  372. public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
  373. {
  374. int intValue = (int)value;
  375. if (destinationType == typeof(string))
  376. {
  377. if(intValue == -1)
  378. {
  379. return Constants.AutoValue;
  380. }
  381. }
  382. // Call base class
  383. return base.ConvertTo(context, culture, value, destinationType);
  384. }
  385. /// <summary>
  386. /// Convert minimum or maximum values from string
  387. /// </summary>
  388. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  389. {
  390. // If converting from string value
  391. string stringValue = value as string;
  392. if (stringValue != null)
  393. {
  394. if (String.Compare(stringValue, Constants.AutoValue, StringComparison.OrdinalIgnoreCase) == 0)
  395. {
  396. return -1;
  397. }
  398. }
  399. // Call base converter
  400. return base.ConvertFrom(context, culture, value);
  401. }
  402. #endregion
  403. }
  404. }