ToDateTime() public static method

Converts a value to a date.
public static ToDateTime ( object value ) : DateTime
value object The value to convert to a date.
return DateTime
Exemplo n.º 1
0
        /// <summary>
        /// Returns the plot area coordinate of a value.
        /// </summary>
        /// <param name="value">The value to plot.</param>
        /// <param name="currentRange">The range to use determine the coordinate.</param>
        /// <param name="length">The length of the axis.</param>
        /// <returns>The plot area coordinate of a value.</returns>
        private static UnitValue GetPlotAreaCoordinate(object value, Range <DateTime> currentRange, double length)
        {
            if (currentRange.HasData)
            {
                DateTime dateTimeValue = ValueHelper.ToDateTime(value);

                double rangelength = currentRange.Maximum.Ticks - currentRange.Minimum.Ticks;
                double pixelLength = Math.Max(length - 1, 0);

                return(new UnitValue((dateTimeValue.Ticks - currentRange.Minimum.Ticks) * (pixelLength / rangelength), Unit.Pixels));
            }

            return(UnitValue.NaN());
        }
        protected override Range <IComparable> IRangeProviderGetRange(IRangeConsumer rangeConsumer)
        {
            if (rangeConsumer == ActualDependentAxis)
            {
                var dependentValuesByIndependentValue = IndependentValueDependentValues.Select(e => e.ToArray()).ToArray();

                var mostNegative = dependentValuesByIndependentValue
                                   .Select(g => g.Where(v => v < 0)
                                           .Sum())
                                   .Where(v => v < 0)
                                   .ToArray();
                var leastNegative = dependentValuesByIndependentValue
                                    .Select(g => g.Where(v => v <= 0)
                                            .DefaultIfEmpty(1.0)
                                            .First())
                                    .Where(v => v <= 0)
                                    .ToArray();
                var mostPositive = dependentValuesByIndependentValue
                                   .Select(g => g.Where(v => 0 < v)
                                           .Sum())
                                   .Where(v => 0 < v)
                                   .ToArray();
                var leastPositive = dependentValuesByIndependentValue
                                    .Select(g => g.Where(v => 0 <= v)
                                            .DefaultIfEmpty(-1.0)
                                            .First())
                                    .Where(v => 0 <= v)
                                    .ToArray();

                // Compute minimum
                double minimum = 0;
                if (mostNegative.Any())
                {
                    minimum = mostNegative.Min();
                }
                else if (leastPositive.Any())
                {
                    minimum = leastPositive.Min();
                }

                // Compute maximum
                double maximum = 0;
                if (mostPositive.Any())
                {
                    maximum = mostPositive.Max();
                }
                else if (leastNegative.Any())
                {
                    maximum = leastNegative.Max();
                }

                if (IsStacked100)
                {
                    minimum = Math.Min(minimum, 0);
                    maximum = Math.Max(maximum, 0);
                }

                return(new Range <IComparable>(minimum, maximum));
            }
            else if (rangeConsumer == ActualIndependentAxis)
            {
                // Using a non-ICategoryAxis for the independent axis
                // Need to specifically adjust for slot size of bars/columns so they don't overlap
                // Note: Calculation for slotSize is not perfect, but it's quick, close, and errs on the safe side
                Range <IComparable> range = base.IRangeProviderGetRange(rangeConsumer);
                int count = Math.Max(IndependentValueGroups.Count(), 1);
                if (ActualIndependentAxis.CanPlot(0.0))
                {
                    double minimum  = ValueHelper.ToDouble(range.Minimum);
                    double maximum  = ValueHelper.ToDouble(range.Maximum);
                    double slotSize = (maximum - minimum) / count;
                    return(new Range <IComparable>(minimum - slotSize, maximum + slotSize));
                }
                else
                {
                    DateTime minimum  = ValueHelper.ToDateTime(range.Minimum);
                    DateTime maximum  = ValueHelper.ToDateTime(range.Maximum);
                    TimeSpan slotSize = TimeSpan.FromTicks((maximum - minimum).Ticks / count);
                    return(new Range <IComparable>(minimum - slotSize, maximum + slotSize));
                }
            }
            else
            {
                return(base.IRangeProviderGetRange(rangeConsumer));
            }
        }
Exemplo n.º 3
0
        protected override Range <IComparable> OverrideDataRange(Range <IComparable> range)
        {
            Range <IComparable> overriddenActualRange = range;

            if (!overriddenActualRange.HasData)
            {
                int year = DateTime.Now.Year;
                return(new Range <IComparable>(new DateTime(year, 1, 1), new DateTime(year + 1, 1, 1)));
            }
            else if (ValueHelper.Compare(overriddenActualRange.Minimum, overriddenActualRange.Maximum) == 0)
            {
                DateTime minimum  = ValueHelper.ToDateTime(overriddenActualRange.Minimum);
                DateTime midpoint = ((DateTime.MinValue == minimum) ? DateTime.Now : minimum).Date;
                return(new Range <IComparable>(midpoint.AddMonths(-6), midpoint.AddMonths(6)));
            }

            // ActualLength of 1.0 or less maps all points to the same coordinate
            if (range.HasData && this.ActualLength > 1.0)
            {
                IList <ValueMarginCoordinateAndOverlap> valueMargins = new List <ValueMarginCoordinateAndOverlap>();
                foreach (ValueMargin valueMargin in
                         this.RegisteredListeners
                         .OfType <IValueMarginProvider>()
                         .SelectMany(provider => provider.GetValueMargins(this)))
                {
                    valueMargins.Add(
                        new ValueMarginCoordinateAndOverlap
                    {
                        ValueMargin = valueMargin,
                    });
                }

                if (valueMargins.Count > 0)
                {
                    double maximumPixelMarginLength =
                        valueMargins
                        .Select(valueMargin => valueMargin.ValueMargin.LowMargin + valueMargin.ValueMargin.HighMargin)
                        .MaxOrNullable().Value;

                    // Requested margin is larger than the axis so give up
                    // trying to find a range that will fit it.
                    if (maximumPixelMarginLength > this.ActualLength)
                    {
                        return(range);
                    }

                    Range <DateTime> currentRange = range.ToDateTimeRange();

                    // Ensure range is not empty.
                    if (currentRange.Minimum == currentRange.Maximum)
                    {
                        int year = DateTime.Now.Year;
                        currentRange = new Range <DateTime>(new DateTime(year, 1, 1), new DateTime(year + 1, 1, 1));
                    }

                    // priming the loop
                    double actualLength = this.ActualLength;
                    ValueMarginCoordinateAndOverlap maxLeftOverlapValueMargin;
                    ValueMarginCoordinateAndOverlap maxRightOverlapValueMargin;
                    UpdateValueMargins(valueMargins, currentRange.ToComparableRange());
                    GetMaxLeftAndRightOverlap(valueMargins, out maxLeftOverlapValueMargin, out maxRightOverlapValueMargin);

                    while (maxLeftOverlapValueMargin.LeftOverlap > 0 || maxRightOverlapValueMargin.RightOverlap > 0)
                    {
                        long     unitOverPixels = currentRange.GetLength().Value.Ticks / ((long)actualLength);
                        DateTime newMinimum     = new DateTime(currentRange.Minimum.Ticks - (long)((maxLeftOverlapValueMargin.LeftOverlap + 0.5) * unitOverPixels));
                        DateTime newMaximum     = new DateTime(currentRange.Maximum.Ticks + (long)((maxRightOverlapValueMargin.RightOverlap + 0.5) * unitOverPixels));

                        currentRange = new Range <DateTime>(newMinimum, newMaximum);
                        UpdateValueMargins(valueMargins, currentRange.ToComparableRange());
                        GetMaxLeftAndRightOverlap(valueMargins, out maxLeftOverlapValueMargin, out maxRightOverlapValueMargin);
                    }

                    return(currentRange.ToComparableRange());
                }
            }

            return(range);
        }