// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. using System; using System.Globalization; using System.Collections.Generic; using System.Linq; using System.Diagnostics; using System.Windows; namespace System.Windows.Controls.DataVisualization { /// /// A set of functions for data conversion operations. /// internal static class ValueHelper { /// /// The value of a single radian. /// public const double Radian = Math.PI / 180.0; /// /// Returns a value indicating whether this value can be graphed on a /// linear axis. /// /// The value to evaluate. /// A value indicating whether this value can be graphed on a /// linear axis. public static bool CanGraph(double value) { return !double.IsNaN(value) && !double.IsNegativeInfinity(value) && !double.IsPositiveInfinity(value) && !double.IsInfinity(value); } /// /// Attempts to convert an object into a double. /// /// The value to convert. /// The double value. /// A value indicating whether the value can be converted to a /// double. public static bool TryConvert(object value, out double doubleValue) { doubleValue = default(double); try { if (value != null && (value is double || value is int || value is byte || value is short || value is decimal || value is float || value is long || value is uint || value is sbyte || value is ushort || value is ulong)) { doubleValue = ValueHelper.ToDouble(value); return true; } } catch (FormatException) { } catch (InvalidCastException) { } return false; } /// /// Attempts to convert an object into a date time. /// /// The value to convert. /// The double value. /// A value indicating whether the value can be converted to a /// date time. public static bool TryConvert(object value, out DateTime dateTimeValue) { dateTimeValue = default(DateTime); if (value != null && value is DateTime) { dateTimeValue = (DateTime)value; return true; } return false; } /////// /////// Converts a value in an IComparable. /////// /////// The value to convert. /////// The converted value. ////public static IComparable ToComparable(object value) ////{ //// double doubleValue; //// DateTime dateTimeValue; //// if (TryConvert(value, out doubleValue)) //// { //// return doubleValue; //// } //// else if (TryConvert(value, out dateTimeValue)) //// { //// return dateTimeValue; //// } //// IComparable comparable = value as IComparable; //// return (comparable != null); ////} /// /// Converts an object into a double. /// /// The value to convert to a double. /// The converted double value. public static double ToDouble(object value) { return Convert.ToDouble(value, CultureInfo.InvariantCulture); } /// /// Converts a value to a date. /// /// The value to convert to a date. /// The converted date value. public static DateTime ToDateTime(object value) { return Convert.ToDateTime(value, CultureInfo.InvariantCulture); } /// /// Returns a sequence of date time values from a start and end date /// time inclusive. /// /// The start date time. /// The end date time. /// The number of values to return. /// A sequence of date time values. public static IEnumerable GetDateTimesBetweenInclusive(DateTime start, DateTime end, long count) { Debug.Assert(count >= 2L, "Count must be at least 2."); return GetIntervalsInclusive(start.Ticks, end.Ticks, count).Select(value => new DateTime(value)); } /// /// Returns a sequence of time span values within a time span inclusive. /// /// The time span to split. /// The number of time spans to return. /// A sequence of time spans. public static IEnumerable GetTimeSpanIntervalsInclusive(TimeSpan timeSpan, long count) { Debug.Assert(count >= 2L, "Count must be at least 2."); long distance = timeSpan.Ticks; return GetIntervalsInclusive(0, distance, count).Select(value => new TimeSpan(value)); } /// /// Returns that intervals between a start and end value, including those /// start and end values. /// /// The start value. /// The end value. /// The total number of intervals. /// A sequence of intervals. public static IEnumerable GetIntervalsInclusive(long start, long end, long count) { Debug.Assert(count >= 2L, "Count must be at least 2."); long interval = end - start; for (long index = 0; index < count; index++) { double ratio = (double)index / (double)(count - 1); long value = (long)((ratio * interval) + start); yield return value; } } /// /// Removes the noise from double math. /// /// The value. /// A double without a noise. internal static double RemoveNoiseFromDoubleMath(double value) { if (value == 0.0 || Math.Abs((Math.Log10(Math.Abs(value)))) < 27) { return (double)((decimal)value); } return Double.Parse(value.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture); } /// /// Converts a range into a double range. /// /// The range to convert. /// A range with its members converted to doubles. public static Range ToDoubleRange(this Range range) { if (!range.HasData) { return new Range(); } else { return new Range((double)range.Minimum, (double)range.Maximum); } } /// /// Converts a range into a date time range. /// /// The range to convert. /// A range with its members converted to date times. /// public static Range ToDateTimeRange(this Range range) { if (!range.HasData) { return new Range(); } else { return new Range((DateTime)range.Minimum, (DateTime)range.Maximum); } } /////// /////// Returns the point given an angle and a distanceFromOrigin. /////// /////// The angle of orientation. /////// The radius. /////// The point calculated from the angle and radius. ////public static Point GetPoint(double angle, double distanceFromOrigin) ////{ //// return new Point(Math.Cos(angle * Radian) * distanceFromOrigin, Math.Sin(angle * Radian) * distanceFromOrigin); ////} /// /// Compares two IComparables returning -1 if the left is null and 1 if /// the right is null. /// /// The left comparable. /// The right comparable. /// A value indicating which is larger. public static int Compare(IComparable left, IComparable right) { if (left == null && right == null) { return 0; } else if (left == null && right != null) { return -1; } else if (left != null && right == null) { return 1; } else { return left.CompareTo(right); } } /// /// Applies the translate transform to a point. /// /// The origin point. /// The offset point. /// The translated point. public static Point Translate(this Point origin, Point offset) { return new Point(origin.X + offset.X, origin.Y + offset.Y); } /// /// Converts any range to a range of IComparable. /// /// The range to be converted. /// The new range type. public static Range ToComparableRange(this Range range) { if (range.HasData) { return new Range(range.Minimum, range.Maximum); } else { return new Range(); } } /// /// Returns the left value of the rectangle. /// /// The rectangle. /// The default value. /// The left value of the rectangle. public static double LeftOrDefault(this Rect rectangle, double value) { return rectangle.IsEmpty ? value : rectangle.Left; } /// /// Returns the right value of the rectangle. /// /// The rectangle. /// The default value. /// The right value of the rectangle. public static double RightOrDefault(this Rect rectangle, double value) { return rectangle.IsEmpty ? value : rectangle.Right; } /// /// Returns the width value of the rectangle. /// /// The rectangle. /// The default value. /// The width value of the rectangle. public static double WidthOrDefault(this Rect rectangle, double value) { return rectangle.IsEmpty ? value : rectangle.Width; } /// /// Returns the height value of the rectangle. /// /// The rectangle. /// The default value. /// The height value of the rectangle. public static double HeightOrDefault(this Rect rectangle, double value) { return rectangle.IsEmpty ? value : rectangle.Height; } /// /// Returns the bottom value of the rectangle. /// /// The rectangle. /// The default value. /// The bottom value of the rectangle. public static double BottomOrDefault(this Rect rectangle, double value) { return rectangle.IsEmpty ? value : rectangle.Bottom; } /// /// Returns the top value of the rectangle. /// /// The rectangle. /// The default value. /// The top value of the rectangle. public static double TopOrDefault(this Rect rectangle, double value) { return rectangle.IsEmpty ? value : rectangle.Top; } /// /// Converts any range to a range of IComparable. /// /// The range to be converted. /// The new range type. public static Range ToComparableRange(this Range range) { if (range.HasData) { return new Range(range.Minimum, range.Maximum); } else { return new Range(); } } /// /// Returns the time span of a date range. /// /// The range of values. /// The length of the range. public static TimeSpan? GetLength(this Range range) { return range.HasData ? range.Maximum - range.Minimum : new TimeSpan?(); } /// /// Returns the time span of a date range. /// /// The range of values. /// The length of the range. public static double? GetLength(this Range range) { return range.HasData ? range.Maximum - range.Minimum : new double?(); } /// /// Returns a value indicating whether a rectangle is empty or has /// no width or height. /// /// The rectangle. /// A value indicating whether a rectangle is empty or has /// no width or height. public static bool IsEmptyOrHasNoSize(this Rect rect) { return rect.IsEmpty || (rect.Width == 0 && rect.Height == 0); } /// /// Sets the style property of an element. /// /// The element. /// The style. public static void SetStyle(this FrameworkElement element, Style style) { element.Style = style; } } }