/// <summary> /// Sets the interval of the axis as close as possible to a specific number of pixels taking into account that the axis is used for values and the rules for allowed interval values. /// </summary> /// <param name="a"></param> /// <param name="pixelsForEachLabel">The number of pixels wanted between labels.</param> /// <param name="numberOfDecimalsAllowed">The number of decimals allowed in the axis values. Null if it should not be checked.</param> /// <param name="intervalMinPctOfAxisSize">The smallest allowed interval in percentage of axis size. Null if it should not be checked.</param> public static void SetIntervalForValuesByPixels(this Axis a, int pixelsForEachLabel, int?numberOfDecimalsAllowed, int?intervalMinPctOfAxisSize) { int numberOfLabels = Convert.ToInt32(a.PixelSize() / pixelsForEachLabel); double i = Math.Max(Math.Abs(a.Maximum), Math.Abs(a.Minimum)); i = i.AdjustUp(); double size = a.Size(); while (size / i < numberOfLabels) { double f = i.NumberSize() / 2; if (intervalMinPctOfAxisSize != null && (i - f).PctOf(size) < intervalMinPctOfAxisSize) { break; } i -= f; if (i <= 0) { i += f; break; } } a.Interval = numberOfDecimalsAllowed == null ? i : Math.Max(i, numberOfDecimalsAllowed == 0 ? 1 : 1 / (10 * (double)numberOfDecimalsAllowed)) ; if (a.Maximum != 0 && a.Maximum.ModWithDecimals(a.Interval) != 0) { a.Maximum = a.Maximum.AdjustUpToInterval(a.Interval); } if (a.Minimum != 0 && a.Minimum.ModWithDecimals(a.Interval) != 0) { a.Minimum = a.Minimum.AdjustDownToInterval(a.Interval); } if (a.Minimum < 0) { a.IntervalOffset = Math.Abs(a.Minimum.ModWithDecimals(i)); } }
/// <summary> /// Returns the first axis value higher than this data value if the axis interval is the NumberSize() of this value. /// </summary> /// <param name="d"></param> /// <returns></returns> public static double AdjustUp(this double d) { return(d.AdjustUp(d.NumberSize())); }
/// <summary> /// The appropriate maximum axis value for this maximum data value. /// </summary> /// <param name="d"></param> /// <returns></returns> public static double FindMax(this double d) { double i = d.intervalForAdjust(); return(d.AdjustUp(i)); }