Exemplo n.º 1
0
        private static TimeSpanEx CalculateMajorSpan(TimeSpan span, int targetNumberOfMajorTicks)
        {
            TimeSpanEx majorspan;

            if (span > _possibleMajorTickSpans[_possibleMajorTickSpans.Length - 1])
            {
                if (span >= TimeSpan.FromDays(365 * targetNumberOfMajorTicks + 1))
                {
                    double yearPerTick = LinearTickSpacing.CalculateMajorSpan(span.TotalDays / 365.25, targetNumberOfMajorTicks);
                    if (yearPerTick > 0.75)
                    {
                        return(TimeSpanEx.FromYears((long)Math.Round(yearPerTick)));
                    }
                }
                double monthPerTick = LinearTickSpacing.CalculateMajorSpan(span.TotalDays / (365.25 / 12), targetNumberOfMajorTicks);
                if (monthPerTick >= 0.8)
                {
                    return(TimeSpanEx.FromMonths(RoundTo1_2_3_4_6_8_12_18(monthPerTick)));
                }
            }
            int i = _possibleMajorTickSpans.Length - 1;
            var destMajorTickSpan = new TimeSpan(span.Ticks / targetNumberOfMajorTicks);

            for (i = _possibleMajorTickSpans.Length - 1; i >= 0; i--)
            {
                if (_possibleMajorTickSpans[i] < destMajorTickSpan)
                {
                    break;
                }
            }
            majorspan = TimeSpanEx.FromTimeSpan(_possibleMajorTickSpans[Math.Max(i, 0)]);

            return(majorspan);
        }
Exemplo n.º 2
0
            protected virtual LinearTickSpacing SDeserialize(object o, Altaxo.Serialization.Xml.IXmlDeserializationInfo info, object parent)
            {
                LinearTickSpacing s = null != o ? (LinearTickSpacing)o : new LinearTickSpacing();

                s._zeroLever     = info.GetDouble("ZeroLever");
                s._orgGrace      = info.GetDouble("MinGrace");
                s._endGrace      = info.GetDouble("MaxGrace");
                s._snapOrgToTick = (BoundaryTickSnapping)info.GetEnum("SnapOrgToTick", typeof(BoundaryTickSnapping));
                s._snapEndToTick = (BoundaryTickSnapping)info.GetEnum("SnapEndToTick", typeof(BoundaryTickSnapping));

                s._targetNumberOfMajorTicks = info.GetInt32("TargetNumberOfMajorTicks");
                s._targetNumberOfMinorTicks = info.GetInt32("TargetNumberOfMinorTicks");
                s._userDefinedMajorSpan     = info.GetNullableDouble("UserDefinedMajorSpan");
                s._userDefinedMinorTicks    = info.GetNullableInt32("UserDefinedMinorTicks");

                s._transformationOffset              = info.GetDouble("TransformationOffset");
                s._transformationDivider             = info.GetDouble("TransformationDivider");
                s._transformationOperationIsMultiply = info.GetBoolean("TransformationIsMultiply");

                s.SuppressedMajorTicks = (SuppressedTicks)info.GetValue("SuppressedMajorTicks", s);
                s.SuppressedMinorTicks = (SuppressedTicks)info.GetValue("SuppressedMinorTicks", s);
                s.AdditionalMajorTicks = (AdditionalTicks)info.GetValue("AdditionalMajorTicks", s);
                s.AdditionalMinorTicks = (AdditionalTicks)info.GetValue("AdditionalMinorTicks", s);

                return(s);
            }
Exemplo n.º 3
0
        /// <summary>
        /// Calculates the number of minor ticks from the major span value and the target number of minor ticks.
        /// </summary>
        /// <param name="majorSpan">Major span value.</param>
        /// <param name="targetNumberOfMinorTicks">Target number of minor ticks.</param>
        /// <returns></returns>
        private static TimeSpanEx CalculateNumberOfMinorTicks(TimeSpanEx majorSpan, int targetNumberOfMinorTicks)
        {
            if (targetNumberOfMinorTicks <= 0)
            {
                return(majorSpan);
            }

            switch (majorSpan._unit)
            {
            case TimeSpanExUnit.Years:
            {
                long years = majorSpan._span.Ticks;
                int  ticks = LinearTickSpacing.CalculateNumberOfMinorTicks(years, targetNumberOfMinorTicks);
                if (ticks < years && ticks > 0 && 0 == years % ticks)
                {
                    return(TimeSpanEx.FromYears(years / ticks));
                }
                double minDiff  = double.MaxValue;
                var    result   = TimeSpanEx.FromYears(1);
                var    divisors = Calc.PrimeNumberMath.GetNeighbouringDivisors(years, targetNumberOfMinorTicks, years);
                foreach (var c in divisors)
                {
                    double dticks = years / c;
                    var    diff   = GetMinorTickDifferencePenalty(dticks, targetNumberOfMinorTicks);
                    if (diff < minDiff)
                    {
                        minDiff = diff;
                        result  = TimeSpanEx.FromYears(years / c);
                    }
                }
                divisors = Calc.PrimeNumberMath.GetNeighbouringDivisors(years * 12, targetNumberOfMinorTicks, years * 12);
                foreach (var c in divisors)
                {
                    double dticks = (years * 12) / c;
                    var    diff   = GetMinorTickDifferencePenalty(dticks, targetNumberOfMinorTicks);
                    if (diff < minDiff)
                    {
                        minDiff = diff;
                        result  = TimeSpanEx.FromMonths((years * 12) / c);
                    }
                }
                return(result);
            }

            case TimeSpanExUnit.Month:
            {
                long months = majorSpan._span.Ticks;
                int  ticks  = LinearTickSpacing.CalculateNumberOfMinorTicks(months, targetNumberOfMinorTicks);
                if (ticks < months && ticks > 0 && 0 == months % ticks)
                {
                    return(TimeSpanEx.FromMonths(months / ticks));
                }

                double minDiff  = double.MaxValue;
                var    result   = TimeSpanEx.FromMonths(1);
                var    divisors = Calc.PrimeNumberMath.GetNeighbouringDivisors(months, targetNumberOfMinorTicks, months);
                foreach (var c in divisors)
                {
                    double dticks = months / c;
                    var    diff   = GetMinorTickDifferencePenalty(dticks, targetNumberOfMinorTicks);
                    if (diff < minDiff)
                    {
                        minDiff = diff;
                        result  = TimeSpanEx.FromMonths(months / c);
                    }
                }
                return(result);
            }

            case TimeSpanExUnit.Span:
            {
                double days = TimeSpanEx.Divide(majorSpan, TimeSpanEx.FromDays(1));
                if (days > 1)
                {
                    int ticks = LinearTickSpacing.CalculateNumberOfMinorTicks(days, targetNumberOfMinorTicks);
                    if (ticks < days)
                    {
                        return(TimeSpanEx.FromTicks(majorSpan._span.Ticks / ticks));
                    }
                }

                var    result  = TimeSpanEx.FromDays(1);
                double minDiff = double.MaxValue;
                foreach (var c in _possibleMinorTicksForDays)
                {
                    double dticks = TimeSpanEx.Divide(majorSpan, c);
                    var    diff   = Math.Abs(dticks - targetNumberOfMinorTicks);
                    if (diff < minDiff)
                    {
                        minDiff = diff;
                        result  = c;
                    }
                }
                return(result);
            }

            default:
                throw new NotImplementedException(string.Format("Unit {0} is unknown here", majorSpan._unit));
            }
        }
Exemplo n.º 4
0
 public LinearTickSpacing(LinearTickSpacing from)
     : base(from) // everything is done here, since CopyFrom is virtual!
 {
 }
Exemplo n.º 5
0
            public object Deserialize(object o, Altaxo.Serialization.Xml.IXmlDeserializationInfo info, object parent)
            {
                LinearTickSpacing s = SDeserialize(o, info, parent);

                return(s);
            }
Exemplo n.º 6
0
		public LinearTickSpacing(LinearTickSpacing from)
			: base(from) // everything is done here, since CopyFrom is virtual!
		{
		}