123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- // 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 Legend.
- //
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Globalization;
- using System.Drawing;
- #if NETSTANDARD || !NETCOREWIN
- using FastReport.TypeConverters;
- #endif
- namespace FastReport.DataVisualization.Charting
- {
- /// <summary>
- /// Chart area name converter. Displays list of available areas names
- /// </summary>
- internal class LegendAreaNameConverter : 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>Standart values collection.</returns>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- ArrayList values = new ArrayList();
- values.Add(Constants.NotSetValue);
- ChartAreaCollection areaCollection = null;
- string areaName = "";
- if (context != null && context.Instance != null)
- {
- if (context.Instance is Legend)
- {
- Legend legend = (Legend)context.Instance;
- if(legend.Common != null && legend.Common.ChartPicture != null)
- {
- areaCollection = legend.Common.ChartPicture.ChartAreas;
- }
- }
- else if (context.Instance is ChartArea)
- {
- ChartArea area = (ChartArea)context.Instance;
- if(area.Common != null && area.Common.ChartPicture != null)
- {
- areaCollection = area.Common.ChartPicture.ChartAreas;
- areaName = area.Name;
- }
- }
- else if (context.Instance is Title)
- {
- Title title = (Title)context.Instance;
- if(title.Chart != null && title.Chart.chartPicture != null)
- {
- areaCollection = title.Chart.chartPicture.ChartAreas;
- }
- }
- else if (context.Instance is Annotation)
- {
- Annotation annotation = (Annotation)context.Instance;
- if(annotation.Chart != null && annotation.Chart.chartPicture != null)
- {
- areaCollection = annotation.Chart.chartPicture.ChartAreas;
- }
- }
- else if (context.Instance is IServiceProvider)
- {
- IServiceProvider provider = context.Instance as IServiceProvider;
- Chart chart = provider.GetService(typeof(Chart)) as Chart;
- if (chart != null)
- {
- areaCollection = chart.ChartAreas;
- }
- }
- else if (context.Instance is Array)
- {
- if (((Array)context.Instance).Length > 0 && ((Array)context.Instance).GetValue(0) is Legend)
- {
- Legend legend = (Legend)((Array)context.Instance).GetValue(0);
- if (legend.Common != null && legend.Common.ChartPicture != null)
- {
- areaCollection = legend.Common.ChartPicture.ChartAreas;
- }
- }
- else if (((Array)context.Instance).Length > 0 && ((Array)context.Instance).GetValue(0) is ChartArea)
- {
- ChartArea area = (ChartArea)((Array)context.Instance).GetValue(0);
- if (area.Common != null && area.Common.ChartPicture != null)
- {
- areaCollection = area.Common.ChartPicture.ChartAreas;
- }
- }
- else if (((Array)context.Instance).Length > 0 && ((Array)context.Instance).GetValue(0) is Title)
- {
- Title title = (Title)((Array)context.Instance).GetValue(0);
- if (title.Chart != null && title.Chart.chartPicture != null)
- {
- areaCollection = title.Chart.chartPicture.ChartAreas;
- }
- }
- else if (((Array)context.Instance).Length > 0 && ((Array)context.Instance).GetValue(0) is Annotation)
- {
- Annotation annotation = (Annotation)((Array)context.Instance).GetValue(0);
- if (annotation.Chart != null && annotation.Chart.chartPicture != null)
- {
- areaCollection = annotation.Chart.chartPicture.ChartAreas;
- }
- }
- }
- }
- if (areaCollection != null)
- {
- foreach(ChartArea area in areaCollection)
- {
- if(area.Name != areaName)
- {
- values.Add(area.Name);
- }
- }
- }
- return new StandardValuesCollection(values);
- }
- #endregion
- }
- /// <summary>
- /// Legend converter
- /// </summary>
- internal class LegendConverter : NoNameExpandableObjectConverter
- {
- }
- /// <summary>
- /// Designer converter class
- /// Converts Size.Emty tofrom "Auto".
- /// </summary>
- internal class SizeEmptyValueConverter : SizeConverter
- {
- #region Converter methods
- /// <summary>
- /// Standard 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>
- /// Standard 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 predefined values.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- ArrayList values = new ArrayList();
- values.Add(System.Drawing.Size.Empty);
-
- return new StandardValuesCollection(values);
- }
- /// <summary>
- /// Convert Size.IsEmpty value to string "Auto"
- /// </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, System.Globalization.CultureInfo culture, object value, Type destinationType)
- {
- if (destinationType == typeof(string))
- {
- if(((System.Drawing.Size)value).IsEmpty)
- {
- return Constants.AutoValue;
- }
- }
- // Call base class
- return base.ConvertTo(context, culture, value, destinationType);
- }
- /// <summary>
- /// Convert minimum or maximum values from string
- /// </summary>
- public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
- {
- // If converting from string value
- string stringValue = value as string;
- if (stringValue != null)
- {
- if (String.Compare(stringValue, Constants.AutoValue, StringComparison.OrdinalIgnoreCase) == 0)
- {
- return System.Drawing.Size.Empty;
- }
- }
-
- // Call base converter
- return base.ConvertFrom(context, culture, value);
- }
- #endregion
- }
- /// <summary>
- /// Data point properties converter
- /// </summary>
- internal class MarginExpandableObjectConverter : ExpandableObjectConverter
- {
- #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>
- /// Returns whether this converter can convert an object of the given type
- /// to the type of this converter, using the specified context.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="sourceType">Source type.</param>
- /// <returns>True if object can be converted.</returns>
- public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
- {
- if (sourceType == typeof(string))
- {
- return true;
- }
- // Always call the base to see if it can perform the conversion.
- return base.CanConvertFrom(context, sourceType);
- }
- /// <summary>
- /// This code performs the actual conversion from an object to a string.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- /// <param name="culture">Culture information.</param>
- /// <param name="value">Object value.</param>
- /// <param name="destinationType">Destination type.</param>
- /// <returns>Converted object.</returns>
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- Margins margins = value as Margins;
- if (destinationType == typeof(string) && margins != null)
- {
- return string.Format(
- CultureInfo.InvariantCulture,
- "{0:D}, {1:D}, {2:D}, {3:D}",
- margins.Top,
- margins.Bottom,
- margins.Left,
- margins.Right);
- }
- // Always call base, even if you can't convert.
- 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)
- {
- Margins margins = new Margins();
- string[] values = stringValue.Split(',');
- if(values.Length == 4)
- {
- try
- {
- margins.Top = int.Parse(values[0].Trim(), CultureInfo.InvariantCulture);
- margins.Bottom = int.Parse(values[1].Trim(), CultureInfo.InvariantCulture);
- margins.Left = int.Parse(values[2].Trim(), CultureInfo.InvariantCulture);
- margins.Right = int.Parse(values[3].Trim(), CultureInfo.InvariantCulture);
- }
- catch
- {
- throw (new InvalidOperationException(SR.ExceptionLegendDesignerMarginObjectInvalid(stringValue)));
- }
- }
- else
- {
- throw (new InvalidOperationException(SR.ExceptionLegendDesignerMarginObjectInvalid(stringValue)));
- }
- return margins;
- }
- // Call base class
- return base.ConvertFrom(context, culture, value);
- }
- #endregion
- }
- /// <summary>
- /// Designer converter class
- /// Converts Integer value -1 to/from "Auto".
- /// </summary>
- internal class IntNanValueConverter : Int32Converter
- {
- #region Converter methods
- /// <summary>
- /// Standard 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>
- /// Standard 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 predefined values.
- /// </summary>
- /// <param name="context">Descriptor context.</param>
- public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
- {
- ArrayList values = new ArrayList();
- values.Add(-1);
-
- return new StandardValuesCollection(values);
- }
- /// <summary>
- /// Convert integer value -1 to string "Auto"
- /// </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, System.Globalization.CultureInfo culture, object value, Type destinationType)
- {
- int intValue = (int)value;
- if (destinationType == typeof(string))
- {
- if(intValue == -1)
- {
- return Constants.AutoValue;
- }
- }
- // Call base class
- return base.ConvertTo(context, culture, value, destinationType);
- }
- /// <summary>
- /// Convert minimum or maximum values from string
- /// </summary>
- public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
- {
- // If converting from string value
- string stringValue = value as string;
- if (stringValue != null)
- {
- if (String.Compare(stringValue, Constants.AutoValue, StringComparison.OrdinalIgnoreCase) == 0)
- {
- return -1;
- }
- }
-
- // Call base converter
- return base.ConvertFrom(context, culture, value);
- }
- #endregion
- }
- }
|