DataManagerConverters.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 the Series and DataPoint properties.
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.ComponentModel;
  10. using System.Drawing;
  11. using System.Globalization;
  12. using System.Reflection;
  13. using FastReport.DataVisualization.Charting.ChartTypes;
  14. using FastReport.DataVisualization.Charting.Data;
  15. namespace FastReport.DataVisualization.Charting
  16. {
  17. /// <summary>
  18. /// Chart area name converter. Displays list of available areas names
  19. /// </summary>
  20. internal class SeriesAreaNameConverter : 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 the chart areas for the series.
  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. Chart chart = ConverterHelper.GetChartFromContext(context);
  50. if (chart != null)
  51. {
  52. foreach (ChartArea area in chart.ChartAreas)
  53. {
  54. values.Add(area.Name);
  55. }
  56. }
  57. return new StandardValuesCollection(values);
  58. }
  59. #endregion
  60. }
  61. /// <summary>
  62. /// Chart data source design-time converter. Displays list of available data sources.
  63. /// </summary>
  64. internal class ChartDataSourceConverter : StringConverter
  65. {
  66. #region Converter methods
  67. /// <summary>
  68. /// Standart values supported - return true
  69. /// </summary>
  70. /// <param name="context">Descriptor context.</param>
  71. /// <returns>Standard values supported.</returns>
  72. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  73. {
  74. return true;
  75. }
  76. /// <summary>
  77. /// Standart values are not exclusive - return false
  78. /// </summary>
  79. /// <param name="context">Descriptor context.</param>
  80. /// <returns>Non exclusive standard values.</returns>
  81. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  82. {
  83. return true;
  84. }
  85. /// <summary>
  86. /// Fill in the list of chart type names.
  87. /// </summary>
  88. /// <param name="context">Descriptor context.</param>
  89. /// <returns>Standard values collection.</returns>
  90. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  91. {
  92. ArrayList values = new ArrayList();
  93. if (context != null && context.Container != null)
  94. {
  95. // Loop through all components in the container
  96. foreach(IComponent comonent in context.Container.Components)
  97. {
  98. // Check if component can be a data source
  99. if(ChartImage.IsValidDataSource(comonent))
  100. {
  101. // Add component name
  102. values.Add(comonent.Site.Name);
  103. }
  104. }
  105. }
  106. // Add "None" data source
  107. values.Add("(none)");
  108. return new StandardValuesCollection(values);
  109. }
  110. #endregion
  111. }
  112. /// <summary>
  113. /// Series data source members converter.
  114. /// </summary>
  115. internal class SeriesDataSourceMemberConverter : StringConverter
  116. {
  117. #region Converter methods
  118. /// <summary>
  119. /// Standart values supported - return true
  120. /// </summary>
  121. /// <param name="context">Descriptor context.</param>
  122. /// <returns>Standard values supported.</returns>
  123. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  124. {
  125. return true;
  126. }
  127. /// <summary>
  128. /// Standart values are not exclusive - return false
  129. /// </summary>
  130. /// <param name="context">Descriptor context.</param>
  131. /// <returns>Non exclusive standard values.</returns>
  132. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  133. {
  134. return false;
  135. }
  136. /// <summary>
  137. /// Fill in the list of the data source members.
  138. /// </summary>
  139. /// <param name="context">Descriptor context.</param>
  140. /// <returns>Standart values collection.</returns>
  141. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  142. {
  143. ArrayList values = new ArrayList();
  144. Chart chart = ConverterHelper.GetChartFromContext(context);
  145. object dataSource = null;
  146. if(chart != null)
  147. {
  148. if (chart != null && ChartImage.IsValidDataSource(chart.DataSource))
  149. {
  150. dataSource = chart.DataSource;
  151. }
  152. // Check if it's Y values member
  153. bool usedForYValues = false;
  154. if (context.PropertyDescriptor != null && context.PropertyDescriptor.Name == "YValueMembers")
  155. {
  156. usedForYValues = true;
  157. }
  158. // Populate list with all members names
  159. ArrayList memberNames = ChartImage.GetDataSourceMemberNames(dataSource, usedForYValues);
  160. foreach(string name in memberNames)
  161. {
  162. values.Add(name);
  163. }
  164. values.Add("(none)");
  165. }
  166. return new StandardValuesCollection(values);
  167. }
  168. #endregion
  169. }
  170. /// <summary>
  171. /// Chart legend name converter. Displays list of available legend names
  172. /// </summary>
  173. internal class SeriesLegendNameConverter : StringConverter
  174. {
  175. #region Converter methods
  176. /// <summary>
  177. /// Standart values supported - return true
  178. /// </summary>
  179. /// <param name="context">Descriptor context.</param>
  180. /// <returns>Standard values supported.</returns>
  181. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  182. {
  183. return true;
  184. }
  185. /// <summary>
  186. /// Standart values are not exclusive - return false
  187. /// </summary>
  188. /// <param name="context">Descriptor context.</param>
  189. /// <returns>Non exclusive standard values.</returns>
  190. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  191. {
  192. return false;
  193. }
  194. /// <summary>
  195. /// Fill in the list of the chart legend for the series.
  196. /// </summary>
  197. /// <param name="context">Descriptor context.</param>
  198. /// <returns>Standart values collection.</returns>
  199. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  200. {
  201. ArrayList values = new ArrayList();
  202. Chart chart = ConverterHelper.GetChartFromContext(context);
  203. if (chart != null)
  204. {
  205. foreach (Legend legend in chart.Legends)
  206. {
  207. values.Add(legend.Name);
  208. }
  209. }
  210. return new StandardValuesCollection(values);
  211. }
  212. #endregion
  213. }
  214. /// <summary>
  215. /// Chart type converter. Displays list of available chart type names
  216. /// </summary>
  217. internal class ChartTypeConverter : StringConverter
  218. {
  219. #region Converter methods
  220. /// <summary>
  221. /// Standart values supported - return true
  222. /// </summary>
  223. /// <param name="context">Descriptor context.</param>
  224. /// <returns>Standard values supported.</returns>
  225. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  226. {
  227. return true;
  228. }
  229. /// <summary>
  230. /// Standart values are not exclusive - return false
  231. /// </summary>
  232. /// <param name="context">Descriptor context.</param>
  233. /// <returns>Non exclusive standard values.</returns>
  234. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  235. {
  236. return true;
  237. }
  238. /// <summary>
  239. /// Fill in the list of chart type names.
  240. /// </summary>
  241. /// <param name="context">Descriptor context.</param>
  242. /// <returns>Standard values collection.</returns>
  243. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  244. {
  245. ChartTypeRegistry registry = null;
  246. ArrayList values = new ArrayList();
  247. Chart chart = ConverterHelper.GetChartFromContext(context);
  248. if (chart!=null)
  249. {
  250. // Get chart type registry service
  251. registry = (ChartTypeRegistry)chart.GetService(typeof(ChartTypeRegistry));
  252. if(registry != null)
  253. {
  254. // Enumerate all chart types names
  255. foreach(Object obj in registry.registeredChartTypes.Keys)
  256. {
  257. if(obj is string)
  258. {
  259. values.Add(obj);
  260. }
  261. }
  262. }
  263. else
  264. {
  265. throw (new InvalidOperationException(SR.ExceptionEditorChartTypeRegistryServiceInaccessible));
  266. }
  267. }
  268. // Sort all values
  269. values.Sort();
  270. return new StandardValuesCollection(values);
  271. }
  272. #endregion
  273. }
  274. /// <summary>
  275. /// Data series name converter. Displays list of available series names
  276. /// </summary>
  277. internal class SeriesNameConverter : StringConverter
  278. {
  279. #region Converter methods
  280. /// <summary>
  281. /// Standart values supported - return true
  282. /// </summary>
  283. /// <param name="context">Descriptor context.</param>
  284. /// <returns>Standard values supported.</returns>
  285. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  286. {
  287. return true;
  288. }
  289. /// <summary>
  290. /// Standart values are not exclusive - return false
  291. /// </summary>
  292. /// <param name="context">Descriptor context.</param>
  293. /// <returns>Non exclusive standard values.</returns>
  294. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  295. {
  296. return false;
  297. }
  298. /// <summary>
  299. /// Fill in the list of data series names.
  300. /// </summary>
  301. /// <param name="context">Descriptor context.</param>
  302. /// <returns>Standard values collection.</returns>
  303. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  304. {
  305. DataManager dataManager = null;
  306. ArrayList values = new ArrayList();
  307. if (context != null && context.Instance != null)
  308. {
  309. // Call GetService method using reflection
  310. MethodInfo methodInfo = context.Instance.GetType().GetMethod("GetService");
  311. if(methodInfo != null)
  312. {
  313. object[] parameters = new object[1];
  314. parameters[0] = typeof(DataManager);
  315. dataManager = (DataManager)methodInfo.Invoke(context.Instance, parameters);
  316. }
  317. // If data manager service was seccesfully retrived
  318. if(dataManager != null)
  319. {
  320. foreach(Series series in dataManager.Series)
  321. {
  322. values.Add(series.Name);
  323. }
  324. }
  325. else
  326. {
  327. throw (new InvalidOperationException(SR.ExceptionEditorChartTypeRegistryServiceInObjectInaccessible(context.Instance.GetType().ToString())));
  328. }
  329. }
  330. return new StandardValuesCollection(values);
  331. }
  332. #endregion
  333. }
  334. /// <summary>
  335. /// Data point properties converter
  336. /// </summary>
  337. internal class NoNameExpandableObjectConverter : ExpandableObjectConverter
  338. {
  339. #region Converter methods
  340. /// <summary>
  341. /// Overrides the ConvertTo method of TypeConverter.
  342. /// </summary>
  343. /// <param name="context">Descriptor context.</param>
  344. /// <param name="culture">Culture information.</param>
  345. /// <param name="value">Value to convert.</param>
  346. /// <param name="destinationType">Convertion destination type.</param>
  347. /// <returns>Converted object.</returns>
  348. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  349. {
  350. if (context != null && context.Instance != null)
  351. {
  352. if (destinationType == typeof(string))
  353. {
  354. return "";
  355. }
  356. }
  357. return base.ConvertTo(context, culture, value, destinationType);
  358. }
  359. #endregion
  360. }
  361. /// <summary>
  362. /// Converter for the array of doubles
  363. /// </summary>
  364. internal class DoubleArrayConverter : ArrayConverter
  365. {
  366. #region Converter methods
  367. /// <summary>
  368. /// Overrides the CanConvertFrom method of TypeConverter.
  369. /// The ITypeDescriptorContext interface provides the context for the
  370. /// conversion. Typically this interface is used at design time to
  371. /// provide information about the design-time container.
  372. /// </summary>
  373. /// <param name="context">Descriptor context.</param>
  374. /// <param name="sourceType">Convertion source type.</param>
  375. /// <returns>Indicates if convertion is possible.</returns>
  376. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  377. {
  378. if (sourceType == typeof(string))
  379. {
  380. return true;
  381. }
  382. return base.CanConvertFrom(context, sourceType);
  383. }
  384. /// <summary>
  385. /// Overrides the ConvertFrom method of TypeConverter.
  386. /// </summary>
  387. /// <param name="context">Descriptor context.</param>
  388. /// <param name="culture">Culture information.</param>
  389. /// <param name="value">Value to convert from.</param>
  390. /// <returns>Converted object.</returns>
  391. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  392. {
  393. object result = null;
  394. bool convertFromDate = false;
  395. // Try to check if value type is date
  396. if (context != null && context.Instance != null)
  397. {
  398. DataPoint dataPoint = (DataPoint)context.Instance;
  399. if(dataPoint.series != null && dataPoint.series.IsYValueDateTime())
  400. {
  401. convertFromDate = true;
  402. }
  403. }
  404. // Can convert from string where each array element is separated by comma
  405. string stringValue = value as string;
  406. if (stringValue != null)
  407. {
  408. string[] values = stringValue.Split(new char[] {','});
  409. double[] array = new double[values.Length];
  410. for(int index = 0; index < values.Length; index ++)
  411. {
  412. // Try to convert from date-time string format
  413. if (convertFromDate)
  414. {
  415. DateTime valueAsDate;
  416. if (DateTime.TryParse(values[index], CultureInfo.InvariantCulture, DateTimeStyles.None, out valueAsDate))
  417. {
  418. result = valueAsDate;
  419. }
  420. else if (DateTime.TryParse(values[index], CultureInfo.CurrentCulture, DateTimeStyles.None, out valueAsDate))
  421. {
  422. result = valueAsDate;
  423. }
  424. else
  425. {
  426. result = null;
  427. }
  428. }
  429. // Save converted value in the array
  430. if(result != null)
  431. {
  432. array[index] = (double)result;
  433. }
  434. else
  435. {
  436. array[index] = CommonElements.ParseDouble(values[index]);
  437. }
  438. }
  439. return array;
  440. }
  441. // Call base class
  442. return base.ConvertFrom(context, culture, value);
  443. }
  444. /// <summary>
  445. /// Overrides the ConvertTo method of TypeConverter.
  446. /// </summary>
  447. /// <param name="context">Descriptor context.</param>
  448. /// <param name="culture">Culture information.</param>
  449. /// <param name="value">Value to convert.</param>
  450. /// <param name="destinationType">Convertion destination type.</param>
  451. /// <returns>Converted object.</returns>
  452. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  453. {
  454. bool convertToDate = false;
  455. // Check if we should convert to date string format
  456. if (context != null && context.Instance != null)
  457. {
  458. DataPoint dataPoint = (DataPoint)context.Instance;
  459. if(dataPoint.series != null && dataPoint.series.IsYValueDateTime())
  460. {
  461. convertToDate = true;
  462. }
  463. }
  464. if (destinationType == typeof(string))
  465. {
  466. double[] array = (double[]) value;
  467. string result = "";
  468. foreach(double d in array)
  469. {
  470. if(convertToDate)
  471. {
  472. result += DateTime.FromOADate(d).ToString("g", System.Globalization.CultureInfo.InvariantCulture) + ",";
  473. }
  474. else
  475. {
  476. result += d.ToString(System.Globalization.CultureInfo.InvariantCulture) + ",";
  477. }
  478. }
  479. return result.TrimEnd(',');
  480. }
  481. return base.ConvertTo(context, culture, value, destinationType);
  482. }
  483. #endregion
  484. }
  485. /// <summary>
  486. /// Converts data point values to and from date string format
  487. /// </summary>
  488. internal class DataPointValueConverter : DoubleConverter
  489. {
  490. #region Converter methods
  491. /// <summary>
  492. /// Convert values to date string
  493. /// </summary>
  494. /// <param name="context">Descriptor context.</param>
  495. /// <param name="culture">Culture information.</param>
  496. /// <param name="value">Value to convert.</param>
  497. /// <param name="destinationType">Convertion destination type.</param>
  498. /// <returns>Converted object.</returns>
  499. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  500. {
  501. if (context != null && context.Instance != null)
  502. {
  503. DataPoint dataPoint = (DataPoint)context.Instance;
  504. if (destinationType == typeof(string) && dataPoint.series.IsXValueDateTime())
  505. {
  506. DateTime valueAsSate = DateTime.FromOADate((double)value);
  507. return valueAsSate.ToString("g", System.Globalization.CultureInfo.CurrentCulture);
  508. }
  509. }
  510. return base.ConvertTo(context, culture, value, destinationType);
  511. }
  512. /// <summary>
  513. /// Convert values from date string.
  514. /// </summary>
  515. /// <param name="context">Descriptor context.</param>
  516. /// <param name="culture">Culture information.</param>
  517. /// <param name="value">Value to convert from.</param>
  518. /// <returns>Converted object.</returns>
  519. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  520. {
  521. if (context != null && context.Instance != null)
  522. {
  523. string stringValue = value as string;
  524. if (stringValue != null)
  525. {
  526. DataPoint dataPoint = (DataPoint)context.Instance;
  527. if (dataPoint.series.IsXValueDateTime())
  528. {
  529. DateTime valueAsSate = DateTime.Parse(stringValue, System.Globalization.CultureInfo.CurrentCulture);
  530. return valueAsSate.ToOADate();
  531. }
  532. }
  533. }
  534. return base.ConvertFrom(context, culture, value);
  535. }
  536. #endregion
  537. }
  538. /// <summary>
  539. /// Removes the String type for Y axes
  540. /// </summary>
  541. internal class SeriesYValueTypeConverter : EnumConverter
  542. {
  543. #region Converter methods
  544. /// <summary>
  545. /// Public constructor
  546. /// </summary>
  547. /// <param name="type">Enumeration type.</param>
  548. public SeriesYValueTypeConverter(Type type) : base(type)
  549. {
  550. }
  551. /// <summary>
  552. /// Fill in the list of data series names.
  553. /// </summary>
  554. /// <param name="context">Descriptor context.</param>
  555. /// <returns>Standard values collection.</returns>
  556. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  557. {
  558. ArrayList values = new ArrayList();
  559. // Call base class
  560. StandardValuesCollection val = base.GetStandardValues(context);
  561. // Remove string type
  562. foreach(object o in val)
  563. {
  564. if(o.ToString() != "String")
  565. {
  566. values.Add(o);
  567. }
  568. }
  569. return new StandardValuesCollection(values);
  570. }
  571. #endregion
  572. }
  573. /// <summary>
  574. /// Data point properties converter
  575. /// </summary>
  576. internal class ColorArrayConverter : TypeConverter
  577. {
  578. #region Converter methods
  579. /// <summary>
  580. /// This method overrides CanConvertTo from TypeConverter. This is called when someone
  581. /// wants to convert an instance of object to another type. Here,
  582. /// only conversion to an InstanceDescriptor is supported.
  583. /// </summary>
  584. /// <param name="context">Descriptor context.</param>
  585. /// <param name="destinationType">Destination type.</param>
  586. /// <returns>True if object can be converted.</returns>
  587. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  588. {
  589. if (destinationType == typeof(string))
  590. {
  591. return true;
  592. }
  593. // Always call the base to see if it can perform the conversion.
  594. return base.CanConvertTo(context, destinationType);
  595. }
  596. /// <summary>
  597. /// Overrides the CanConvertFrom method of TypeConverter.
  598. /// The ITypeDescriptorContext interface provides the context for the
  599. /// conversion. Typically this interface is used at design time to
  600. /// provide information about the design-time container.
  601. /// </summary>
  602. /// <param name="context">Descriptor context.</param>
  603. /// <param name="sourceType">Convertion source type.</param>
  604. /// <returns>Indicates if convertion is possible.</returns>
  605. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  606. {
  607. if (sourceType == typeof(string))
  608. {
  609. return true;
  610. }
  611. return base.CanConvertFrom(context, sourceType);
  612. }
  613. /// <summary>
  614. /// Overrides the ConvertTo method of TypeConverter.
  615. /// </summary>
  616. /// <param name="context">Descriptor context.</param>
  617. /// <param name="culture">Culture information.</param>
  618. /// <param name="value">Value to convert.</param>
  619. /// <param name="destinationType">Convertion destination type.</param>
  620. /// <returns>Converted object.</returns>
  621. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  622. {
  623. if (destinationType == typeof(string))
  624. {
  625. return ColorArrayToString(value as Color[]);
  626. }
  627. return base.ConvertTo(context, culture, value, destinationType);
  628. }
  629. /// <summary>
  630. /// Overrides the ConvertFrom method of TypeConverter.
  631. /// </summary>
  632. /// <param name="context">Descriptor context.</param>
  633. /// <param name="culture">Culture information.</param>
  634. /// <param name="value">Value to convert from.</param>
  635. /// <returns>Converted object.</returns>
  636. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  637. {
  638. // Can convert from string where each array element is separated by comma
  639. string stringValue = value as string;
  640. if (stringValue != null)
  641. {
  642. return StringToColorArray(stringValue);
  643. }
  644. // Call base class
  645. return base.ConvertFrom(context, culture, value);
  646. }
  647. /// <summary>
  648. /// Converts array of colors into string.
  649. /// </summary>
  650. /// <param name="colors">Colors array.</param>
  651. /// <returns>Result string.</returns>
  652. public static string ColorArrayToString(Color[] colors)
  653. {
  654. if(colors != null && colors.GetLength(0) > 0)
  655. {
  656. ColorConverter colorConverter = new ColorConverter();
  657. string result = string.Empty;
  658. foreach(Color color in colors)
  659. {
  660. if(result.Length > 0)
  661. {
  662. result += "; ";
  663. }
  664. result += colorConverter.ConvertToInvariantString(color);
  665. }
  666. return result;
  667. }
  668. return string.Empty;
  669. }
  670. /// <summary>
  671. /// Converts string into array of colors.
  672. /// </summary>
  673. /// <param name="colorNames">String data.</param>
  674. /// <returns>Array of colors.</returns>
  675. public static Color[] StringToColorArray(String colorNames)
  676. {
  677. ColorConverter colorConverter = new ColorConverter();
  678. Color[] array = new Color[0];
  679. if(colorNames.Length > 0)
  680. {
  681. string[] colorValues = colorNames.Split(';');
  682. array = new Color[colorValues.Length];
  683. int index = 0;
  684. foreach(string str in colorValues)
  685. {
  686. array[index++] = (Color)colorConverter.ConvertFromInvariantString(str);
  687. }
  688. }
  689. return array;
  690. }
  691. #endregion
  692. }
  693. /// <summary>
  694. /// Provides a set of helper methods used by converters
  695. /// </summary>
  696. internal static class ConverterHelper
  697. {
  698. #region Static
  699. /// <summary>
  700. /// Gets the chart from context.
  701. /// </summary>
  702. /// <param name="context">The context.</param>
  703. public static Chart GetChartFromContext(ITypeDescriptorContext context)
  704. {
  705. if (context == null || context.Instance == null)
  706. {
  707. return null;
  708. }
  709. IChartElement element = context.Instance as IChartElement;
  710. if (element != null && element.Common != null)
  711. {
  712. return element.Common.Chart;
  713. }
  714. IList list = context.Instance as IList;
  715. if (list != null && list.Count > 0)
  716. {
  717. element = list[0] as IChartElement;
  718. if (element.Common != null)
  719. {
  720. return element.Common.Chart;
  721. }
  722. }
  723. Chart chart = context.Instance as Chart;
  724. if (chart != null)
  725. {
  726. return chart;
  727. }
  728. IServiceProvider provider = context.Instance as IServiceProvider;
  729. if (provider != null)
  730. {
  731. chart = provider.GetService(typeof(Chart)) as Chart;
  732. if (chart != null)
  733. {
  734. return chart;
  735. }
  736. }
  737. return null;
  738. }
  739. #endregion
  740. }
  741. }