123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- //
- // Purpose: Converter classes for the Series and DataPoint properties.
- //
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Drawing;
- using System.Globalization;
- using System.Reflection;
- using FastReport.DataVisualization.Charting.ChartTypes;
- using FastReport.DataVisualization.Charting.Data;
- namespace FastReport.DataVisualization.Charting
- {
- /// <summary>
- /// Chart area name converter. Displays list of available areas names
- /// </summary>
- internal class SeriesAreaNameConverter : StringConverter
- {
- #region Converter methods
- /// <summary>
- /// Standart values supported - return true
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values supported.</returns>
- public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- /// <summary>
- /// Standart values are not exclusive - return false
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Non exclusive standard values.</returns>
- public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
- {
- return false;
- }
- /// <summary>
- /// Fill in the list of the chart areas for the series.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standart values collection.</returns>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- ArrayList values = new ArrayList();
- Chart chart = ConverterHelper.GetChartFromContext(context);
- if (chart != null)
- {
- foreach (ChartArea area in chart.ChartAreas)
- {
- values.Add(area.Name);
- }
- }
- return new StandardValuesCollection(values);
- }
-
- #endregion
- }
- /// <summary>
- /// Chart data source design-time converter. Displays list of available data sources.
- /// </summary>
- internal class ChartDataSourceConverter : StringConverter
- {
- #region Converter methods
- /// <summary>
- /// Standart values supported - return true
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values supported.</returns>
- public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- /// <summary>
- /// Standart values are not exclusive - return false
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Non exclusive standard values.</returns>
- public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
- {
- return true;
- }
- /// <summary>
- /// Fill in the list of chart type names.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values collection.</returns>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- ArrayList values = new ArrayList();
- if (context != null && context.Container != null)
- {
- // Loop through all components in the container
- foreach(IComponent comonent in context.Container.Components)
- {
- // Check if component can be a data source
- if(ChartImage.IsValidDataSource(comonent))
- {
- // Add component name
- values.Add(comonent.Site.Name);
- }
- }
- }
- // Add "None" data source
- values.Add("(none)");
- return new StandardValuesCollection(values);
- }
- #endregion
- }
- /// <summary>
- /// Series data source members converter.
- /// </summary>
- internal class SeriesDataSourceMemberConverter : StringConverter
- {
- #region Converter methods
- /// <summary>
- /// Standart values supported - return true
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values supported.</returns>
- public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- /// <summary>
- /// Standart values are not exclusive - return false
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Non exclusive standard values.</returns>
- public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
- {
- return false;
- }
- /// <summary>
- /// Fill in the list of the data source members.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standart values collection.</returns>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- ArrayList values = new ArrayList();
- Chart chart = ConverterHelper.GetChartFromContext(context);
- object dataSource = null;
- if(chart != null)
- {
- if (chart != null && ChartImage.IsValidDataSource(chart.DataSource))
- {
- dataSource = chart.DataSource;
- }
- // Check if it's Y values member
- bool usedForYValues = false;
- if (context.PropertyDescriptor != null && context.PropertyDescriptor.Name == "YValueMembers")
- {
- usedForYValues = true;
- }
- // Populate list with all members names
- ArrayList memberNames = ChartImage.GetDataSourceMemberNames(dataSource, usedForYValues);
- foreach(string name in memberNames)
- {
- values.Add(name);
- }
- values.Add("(none)");
- }
- return new StandardValuesCollection(values);
- }
- #endregion
- }
- /// <summary>
- /// Chart legend name converter. Displays list of available legend names
- /// </summary>
- internal class SeriesLegendNameConverter : StringConverter
- {
- #region Converter methods
- /// <summary>
- /// Standart values supported - return true
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values supported.</returns>
- public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- /// <summary>
- /// Standart values are not exclusive - return false
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Non exclusive standard values.</returns>
- public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
- {
- return false;
- }
- /// <summary>
- /// Fill in the list of the chart legend for the series.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standart values collection.</returns>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- ArrayList values = new ArrayList();
- Chart chart = ConverterHelper.GetChartFromContext(context);
- if (chart != null)
- {
- foreach (Legend legend in chart.Legends)
- {
- values.Add(legend.Name);
- }
- }
- return new StandardValuesCollection(values);
- }
- #endregion
- }
- /// <summary>
- /// Chart type converter. Displays list of available chart type names
- /// </summary>
- internal class ChartTypeConverter : StringConverter
- {
- #region Converter methods
- /// <summary>
- /// Standart values supported - return true
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values supported.</returns>
- public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- /// <summary>
- /// Standart values are not exclusive - return false
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Non exclusive standard values.</returns>
- public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
- {
- return true;
- }
- /// <summary>
- /// Fill in the list of chart type names.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values collection.</returns>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- ChartTypeRegistry registry = null;
- ArrayList values = new ArrayList();
- Chart chart = ConverterHelper.GetChartFromContext(context);
- if (chart!=null)
- {
- // Get chart type registry service
- registry = (ChartTypeRegistry)chart.GetService(typeof(ChartTypeRegistry));
- if(registry != null)
- {
- // Enumerate all chart types names
- foreach(Object obj in registry.registeredChartTypes.Keys)
- {
- if(obj is string)
- {
- values.Add(obj);
- }
- }
- }
- else
- {
- throw (new InvalidOperationException(SR.ExceptionEditorChartTypeRegistryServiceInaccessible));
- }
- }
- // Sort all values
- values.Sort();
- return new StandardValuesCollection(values);
- }
-
- #endregion
- }
- /// <summary>
- /// Data series name converter. Displays list of available series names
- /// </summary>
- internal class SeriesNameConverter : StringConverter
- {
- #region Converter methods
- /// <summary>
- /// Standart values supported - return true
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values supported.</returns>
- public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- /// <summary>
- /// Standart values are not exclusive - return false
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Non exclusive standard values.</returns>
- public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
- {
- return false;
- }
- /// <summary>
- /// Fill in the list of data series names.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values collection.</returns>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- DataManager dataManager = null;
- ArrayList values = new ArrayList();
- if (context != null && context.Instance != null)
- {
- // Call GetService method using reflection
- MethodInfo methodInfo = context.Instance.GetType().GetMethod("GetService");
- if(methodInfo != null)
- {
- object[] parameters = new object[1];
- parameters[0] = typeof(DataManager);
- dataManager = (DataManager)methodInfo.Invoke(context.Instance, parameters);
- }
- // If data manager service was seccesfully retrived
- if(dataManager != null)
- {
- foreach(Series series in dataManager.Series)
- {
- values.Add(series.Name);
- }
- }
- else
- {
- throw (new InvalidOperationException(SR.ExceptionEditorChartTypeRegistryServiceInObjectInaccessible(context.Instance.GetType().ToString())));
- }
- }
- return new StandardValuesCollection(values);
- }
-
- #endregion
- }
- /// <summary>
- /// Data point properties converter
- /// </summary>
- internal class NoNameExpandableObjectConverter : ExpandableObjectConverter
- {
- #region Converter methods
- /// <summary>
- /// Overrides the ConvertTo method of TypeConverter.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="culture">Culture information.</param>
- /// <param name="value">Value to convert.</param>
- /// <param name="destinationType">Convertion destination type.</param>
- /// <returns>Converted object.</returns>
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- if (context != null && context.Instance != null)
- {
- if (destinationType == typeof(string))
- {
- return "";
- }
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
- #endregion
- }
- /// <summary>
- /// Converter for the array of doubles
- /// </summary>
- internal class DoubleArrayConverter : ArrayConverter
- {
- #region Converter methods
- /// <summary>
- /// Overrides the CanConvertFrom method of TypeConverter.
- /// The ITypeDescriptorContext interface provides the context for the
- /// conversion. Typically this interface is used at design time to
- /// provide information about the design-time container.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="sourceType">Convertion source type.</param>
- /// <returns>Indicates if convertion is possible.</returns>
- public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
- {
- if (sourceType == typeof(string))
- {
- return true;
- }
- return base.CanConvertFrom(context, sourceType);
- }
-
- /// <summary>
- /// Overrides the ConvertFrom method of TypeConverter.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="culture">Culture information.</param>
- /// <param name="value">Value to convert from.</param>
- /// <returns>Converted object.</returns>
- public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
- {
- object result = null;
- bool convertFromDate = false;
- // Try to check if value type is date
- if (context != null && context.Instance != null)
- {
- DataPoint dataPoint = (DataPoint)context.Instance;
- if(dataPoint.series != null && dataPoint.series.IsYValueDateTime())
- {
- convertFromDate = true;
- }
- }
- // Can convert from string where each array element is separated by comma
- string stringValue = value as string;
- if (stringValue != null)
- {
- string[] values = stringValue.Split(new char[] {','});
- double[] array = new double[values.Length];
- for(int index = 0; index < values.Length; index ++)
- {
- // Try to convert from date-time string format
- if (convertFromDate)
- {
- DateTime valueAsDate;
- if (DateTime.TryParse(values[index], CultureInfo.InvariantCulture, DateTimeStyles.None, out valueAsDate))
- {
- result = valueAsDate;
- }
- else if (DateTime.TryParse(values[index], CultureInfo.CurrentCulture, DateTimeStyles.None, out valueAsDate))
- {
- result = valueAsDate;
- }
- else
- {
- result = null;
- }
- }
- // Save converted value in the array
- if(result != null)
- {
- array[index] = (double)result;
- }
- else
- {
- array[index] = CommonElements.ParseDouble(values[index]);
- }
- }
-
- return array;
- }
- // Call base class
- return base.ConvertFrom(context, culture, value);
- }
-
- /// <summary>
- /// Overrides the ConvertTo method of TypeConverter.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="culture">Culture information.</param>
- /// <param name="value">Value to convert.</param>
- /// <param name="destinationType">Convertion destination type.</param>
- /// <returns>Converted object.</returns>
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- bool convertToDate = false;
- // Check if we should convert to date string format
- if (context != null && context.Instance != null)
- {
- DataPoint dataPoint = (DataPoint)context.Instance;
- if(dataPoint.series != null && dataPoint.series.IsYValueDateTime())
- {
- convertToDate = true;
- }
- }
- if (destinationType == typeof(string))
- {
- double[] array = (double[]) value;
- string result = "";
-
- foreach(double d in array)
- {
- if(convertToDate)
- {
- result += DateTime.FromOADate(d).ToString("g", System.Globalization.CultureInfo.InvariantCulture) + ",";
- }
- else
- {
- result += d.ToString(System.Globalization.CultureInfo.InvariantCulture) + ",";
- }
- }
- return result.TrimEnd(',');
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
-
- #endregion
- }
- /// <summary>
- /// Converts data point values to and from date string format
- /// </summary>
- internal class DataPointValueConverter : DoubleConverter
- {
- #region Converter methods
- /// <summary>
- /// Convert values to date string
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="culture">Culture information.</param>
- /// <param name="value">Value to convert.</param>
- /// <param name="destinationType">Convertion destination type.</param>
- /// <returns>Converted object.</returns>
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- if (context != null && context.Instance != null)
- {
- DataPoint dataPoint = (DataPoint)context.Instance;
- if (destinationType == typeof(string) && dataPoint.series.IsXValueDateTime())
- {
- DateTime valueAsSate = DateTime.FromOADate((double)value);
- return valueAsSate.ToString("g", System.Globalization.CultureInfo.CurrentCulture);
- }
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
- /// <summary>
- /// Convert values from date string.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="culture">Culture information.</param>
- /// <param name="value">Value to convert from.</param>
- /// <returns>Converted object.</returns>
- public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
- {
- if (context != null && context.Instance != null)
- {
- string stringValue = value as string;
- if (stringValue != null)
- {
- DataPoint dataPoint = (DataPoint)context.Instance;
- if (dataPoint.series.IsXValueDateTime())
- {
- DateTime valueAsSate = DateTime.Parse(stringValue, System.Globalization.CultureInfo.CurrentCulture);
- return valueAsSate.ToOADate();
- }
- }
- }
- return base.ConvertFrom(context, culture, value);
- }
- #endregion
- }
- /// <summary>
- /// Removes the String type for Y axes
- /// </summary>
- internal class SeriesYValueTypeConverter : EnumConverter
- {
- #region Converter methods
- /// <summary>
- /// Public constructor
- /// </summary>
- /// <param name="type">Enumeration type.</param>
- public SeriesYValueTypeConverter(Type type) : base(type)
- {
- }
- /// <summary>
- /// Fill in the list of data series names.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <returns>Standard values collection.</returns>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- ArrayList values = new ArrayList();
- // Call base class
- StandardValuesCollection val = base.GetStandardValues(context);
- // Remove string type
- foreach(object o in val)
- {
- if(o.ToString() != "String")
- {
-
- values.Add(o);
- }
- }
- return new StandardValuesCollection(values);
- }
-
- #endregion
- }
- /// <summary>
- /// Data point properties converter
- /// </summary>
- internal class ColorArrayConverter : TypeConverter
- {
- #region Converter methods
- /// <summary>
- /// This method overrides CanConvertTo from TypeConverter. This is called when someone
- /// wants to convert an instance of object to another type. Here,
- /// only conversion to an InstanceDescriptor is supported.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="destinationType">Destination type.</param>
- /// <returns>True if object can be converted.</returns>
- public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
- {
- if (destinationType == typeof(string))
- {
- return true;
- }
- // Always call the base to see if it can perform the conversion.
- return base.CanConvertTo(context, destinationType);
- }
- /// <summary>
- /// Overrides the CanConvertFrom method of TypeConverter.
- /// The ITypeDescriptorContext interface provides the context for the
- /// conversion. Typically this interface is used at design time to
- /// provide information about the design-time container.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="sourceType">Convertion source type.</param>
- /// <returns>Indicates if convertion is possible.</returns>
- public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
- {
- if (sourceType == typeof(string))
- {
- return true;
- }
- return base.CanConvertFrom(context, sourceType);
- }
- /// <summary>
- /// Overrides the ConvertTo method of TypeConverter.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="culture">Culture information.</param>
- /// <param name="value">Value to convert.</param>
- /// <param name="destinationType">Convertion destination type.</param>
- /// <returns>Converted object.</returns>
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- if (destinationType == typeof(string))
- {
- return ColorArrayToString(value as Color[]);
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
- /// <summary>
- /// Overrides the ConvertFrom method of TypeConverter.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="culture">Culture information.</param>
- /// <param name="value">Value to convert from.</param>
- /// <returns>Converted object.</returns>
- public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
- {
- // Can convert from string where each array element is separated by comma
- string stringValue = value as string;
- if (stringValue != null)
- {
- return StringToColorArray(stringValue);
- }
- // Call base class
- return base.ConvertFrom(context, culture, value);
- }
- /// <summary>
- /// Converts array of colors into string.
- /// </summary>
- /// <param name="colors">Colors array.</param>
- /// <returns>Result string.</returns>
- public static string ColorArrayToString(Color[] colors)
- {
- if(colors != null && colors.GetLength(0) > 0)
- {
- ColorConverter colorConverter = new ColorConverter();
- string result = string.Empty;
- foreach(Color color in colors)
- {
- if(result.Length > 0)
- {
- result += "; ";
- }
- result += colorConverter.ConvertToInvariantString(color);
- }
- return result;
- }
- return string.Empty;
- }
- /// <summary>
- /// Converts string into array of colors.
- /// </summary>
- /// <param name="colorNames">String data.</param>
- /// <returns>Array of colors.</returns>
- public static Color[] StringToColorArray(String colorNames)
- {
- ColorConverter colorConverter = new ColorConverter();
- Color[] array = new Color[0];
- if(colorNames.Length > 0)
- {
- string[] colorValues = colorNames.Split(';');
- array = new Color[colorValues.Length];
- int index = 0;
- foreach(string str in colorValues)
- {
- array[index++] = (Color)colorConverter.ConvertFromInvariantString(str);
- }
- }
- return array;
- }
- #endregion
- }
- /// <summary>
- /// Provides a set of helper methods used by converters
- /// </summary>
- internal static class ConverterHelper
- {
- #region Static
- /// <summary>
- /// Gets the chart from context.
- /// </summary>
- /// <param name="context">The context.</param>
- public static Chart GetChartFromContext(ITypeDescriptorContext context)
- {
- if (context == null || context.Instance == null)
- {
- return null;
- }
- IChartElement element = context.Instance as IChartElement;
- if (element != null && element.Common != null)
- {
- return element.Common.Chart;
- }
- IList list = context.Instance as IList;
- if (list != null && list.Count > 0)
- {
- element = list[0] as IChartElement;
- if (element.Common != null)
- {
- return element.Common.Chart;
- }
- }
- Chart chart = context.Instance as Chart;
- if (chart != null)
- {
- return chart;
- }
- IServiceProvider provider = context.Instance as IServiceProvider;
- if (provider != null)
- {
- chart = provider.GetService(typeof(Chart)) as Chart;
- if (chart != null)
- {
- return chart;
- }
- }
- return null;
- }
- #endregion
- }
- }
|