Beispiel #1
0
        protected override void CalculateAxisItems(Type propertyType, ref double minValue, ref double maxValue)
        {
            if (String.IsNullOrEmpty(Increment))
            {
                return;
            }

            double increment = ConvertIncrementStringToDouble(Increment, propertyType);

            // Find integer factors from the minimum and maximum values
            int minFactor = (int)Math.Floor(minValue / increment);
            int maxFactor = (int)Math.Ceiling(maxValue / increment);

            // Try to avoid division by zero, please
            if (minFactor == maxFactor)
            {
                minFactor--;
                maxFactor++;
            }

            // Gaps between the ticks
            double tickLength = Length / (maxFactor - minFactor);

            // Calculate AxisItem objects and add to collection
            for (int factor = minFactor; factor <= maxFactor; factor++)
            {
                AxisItem axisItem = new AxisItem()
                {
                    Item   = ConvertFromDouble(factor * increment, propertyType),
                    Offset = (factor - minFactor) * tickLength
                };

                if (IsFlipped)
                {
                    axisItem.Offset = Length - axisItem.Offset;
                }

                AxisItems.Add(axisItem);
            }

            // Calculate new minimums and maximums
            minValue = increment * minFactor;
            maxValue = increment * maxFactor;
        }
Beispiel #2
0
        protected override void CalculateAxisItems(Type propertyType, ref double minValue, ref double maxValue)
        {
            for (int index = 0; index < Parent.ItemsSource.Count; index++)
            {
                object item  = Parent.ItemsSource[index];
                double value = GetItemPropertyValue(item, PropertyName);

                AxisItem axisItem = new AxisItem()
                {
                    Item   = ConvertFromDouble(value, propertyType),
                    Offset = Length * (value - minValue) / (maxValue - minValue)
                };

                if (IsFlipped)
                {
                    axisItem.Offset = Length - axisItem.Offset;
                }

                AxisItems.Add(axisItem);
            }
        }
        protected override void CalculateAxisItems(Type propertyType, ref double minValue, ref double maxValue)
        {
            if (propertyType != typeof(DateTime))
            {
                throw new NotImplementedException("AdaptableDateTimeAxis is only for a DateTime axis.");
            }

            TestIncrementCollection(SecondIncrements, "Year");
            TestIncrementCollection(MinuteIncrements, "Minute");
            TestIncrementCollection(HourIncrements, "Hour");
            TestIncrementCollection(DayIncrements, "Day");
            TestIncrementCollection(MonthIncrements, "Month");
            TestIncrementCollection(YearIncrements, "Year");

            if (MaximumItems < 2)
            {
                throw new ArgumentException("MaximumItems must be at least 2");
            }

            if (minValue == maxValue)
            {
                minValue -= ConvertToDouble(TimeSpan.FromSeconds(1));
                maxValue += ConvertToDouble(TimeSpan.FromSeconds(1));
            }

            DateTime minDateTime = (DateTime)ConvertFromDouble(minValue, propertyType);
            DateTime maxDateTime = (DateTime)ConvertFromDouble(maxValue, propertyType);

            Int32Collection[] incrementsArray = { SecondIncrements, MinuteIncrements, HourIncrements,
                                                  DayIncrements,    MonthIncrements,  YearIncrements };
            int incrementsIndex       = 0;
            int incrementMultiplier   = 1;
            DateTimeInterval interval = DateTimeInterval.Second;

            while (true)
            {
                Int32Collection increments = incrementsArray[(int)interval];
                int             increment  = incrementMultiplier * increments[incrementsIndex];

                DateTime minAxisDateTime = DateTimeFloor(minDateTime, interval, increment);
                DateTime maxAxisDateTime = DateTimeCeiling(maxDateTime, interval, increment);
                DateTime dtTick          = minAxisDateTime;
                int      count           = 1;

                while (true)
                {
                    dtTick = AddIncrement(dtTick, interval, increment);
                    count++;

                    if (dtTick >= maxAxisDateTime)
                    {
                        break;
                    }

                    if (count > MaximumItems)
                    {
                        break;
                    }
                }

                if (count <= MaximumItems)
                {
                    minValue = ConvertToDouble(minAxisDateTime);
                    maxValue = ConvertToDouble(maxAxisDateTime);
                    dtTick   = minAxisDateTime;

                    for (int i = 0; i < count; i++)
                    {
                        AxisItem axisItem = new AxisItem()
                        {
                            Item   = dtTick,
                            Offset = Length * (ConvertToDouble(dtTick) - minValue) / (maxValue - minValue)
                        };

                        AxisItems.Add(axisItem);
                        dtTick = AddIncrement(dtTick, interval, increment);
                    }
                    DateTimeInterval = interval;
                    break;
                }

                if (incrementsIndex < increments.Count - 1)
                {
                    incrementsIndex++;
                }
                else
                {
                    incrementsIndex = 0;

                    if (interval != DateTimeInterval.Year)
                    {
                        interval++;
                    }
                    else
                    {
                        incrementMultiplier *= 10;
                    }
                }
            }
        }
Beispiel #4
0
        public void Recalculate()
        {
            AxisItems.Clear();

            if (Parent == null ||
                Parent.ItemsSource == null ||
                Parent.ItemsSource.Count == 0 ||
                String.IsNullOrEmpty(PropertyName) ||
                Length == 0)
            {
                return;
            }

            Type propertyType = GetItemProperty(Parent.ItemsSource[0], PropertyName).GetType();

            if (!typeof(IConvertible).IsAssignableFrom(propertyType))
            {
                throw new ApplicationException("Items being graphed must implement IConvertible");
            }

            double minValue = Double.MaxValue;
            double maxValue = Double.MinValue;

            for (int index = 0; index < Parent.ItemsSource.Count; index++)
            {
                object item  = Parent.ItemsSource[index];
                double value = GetItemPropertyValue(item, PropertyName);

                minValue = Math.Min(value, minValue);
                maxValue = Math.Max(value, maxValue);
            }

            if (!String.IsNullOrEmpty(Margin))
            {
                double increment = ConvertIncrementStringToDouble(Margin, propertyType);
                maxValue += increment;
                minValue -= increment;
            }

            if (IncludeZero)
            {
                if (minValue > 0)
                {
                    minValue = 0;
                }

                if (maxValue < 0)
                {
                    maxValue = 0;
                }
            }

            if (IsSymmetricAroundZero)
            {
                maxValue = Math.Max(Math.Abs(minValue), Math.Abs(maxValue));
                minValue = -maxValue;
            }

            // Abstract method implemented in derived classes
            CalculateAxisItems(propertyType, ref minValue, ref maxValue);

            for (int index = 0; index < Parent.ItemsSource.Count; index++)
            {
                object item   = Parent.ItemsSource[index];
                double value  = GetItemPropertyValue(item, PropertyName);
                double offset = Length * (value - minValue) / (maxValue - minValue);

                if (minValue == maxValue)
                {
                    offset = Length / 2;
                }

                if (IsFlipped)
                {
                    offset = Length - offset;
                }

                Point pt = Parent.Points[index];

                if (PointProperty == "X")
                {
                    pt.X = offset;
                }
                else
                {
                    pt.Y = offset;
                }

                Parent.Points[index]           = pt;
                Parent.ItemPoints[index].Point = pt;
            }
        }
        protected override void CalculateAxisItems(Type propertyType, ref double minValue, ref double maxValue)
        {
            if (propertyType == typeof(DateTime))
            {
                throw new NotImplementedException("AdaptableIncrementAxis is not supported for DateTime." +
                                                  "Use AdaptableDateTimeAxis instead.");
            }

            if (Increments == null || Increments.Count == 0)
            {
                throw new ArgumentException("Increments collection must contain have at least one item");
            }

            if (MaximumItems < 2)
            {
                throw new ArgumentException("MaximumItems must be at least 2");
            }

            if (minValue == maxValue)
            {
                minValue -= 1;
                maxValue += 1;
            }

            // Copy the increments to an array and sort them
            double[] increments = new double[Increments.Count];
            Increments.CopyTo(increments, 0);
            Array.Sort(increments);

            // Initialize IncrementFinder
            IncrementFinder finder = new IncrementFinder(increments, minValue, maxValue);

            // Try to find the optimum increment
            while (true)
            {
                double increment = finder.Increment;

                if (finder.TickCount == MaximumItems)
                {
                    break;
                }

                else if (finder.TickCount > MaximumItems)
                {
                    finder.BumpUp();

                    if (finder.TickCount <= MaximumItems)
                    {
                        break;
                    }
                }

                else if (finder.TickCount < MaximumItems)
                {
                    finder.KickDown();

                    if (finder.TickCount > MaximumItems)
                    {
                        finder.BumpUp();
                        break;
                    }
                }
            }

            // Now we're ready to find the axis items
            double tickLength = Length / (finder.MaxFactor - finder.MinFactor);

            // Calculate AxisItem objects and add to collection
            for (int factor = finder.MinFactor; factor <= finder.MaxFactor; factor++)
            {
                AxisItem axisItem = new AxisItem()
                {
                    Item   = ConvertFromDouble(factor * finder.Increment, propertyType),
                    Offset = (factor - finder.MinFactor) * tickLength
                };

                if (IsFlipped)
                {
                    axisItem.Offset = Length - axisItem.Offset;
                }

                AxisItems.Add(axisItem);
            }

            // New minValue and maxValue for return
            minValue = finder.Increment * finder.MinFactor;
            maxValue = finder.Increment * finder.MaxFactor;
        }