1  #region License Information


2 


3  /* HeuristicLab


4  * Copyright (C) 20022016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)


5  *


6  * This file is part of HeuristicLab.


7  *


8  * HeuristicLab is free software: you can redistribute it and/or modify


9  * it under the terms of the GNU General Public License as published by


10  * the Free Software Foundation, either version 3 of the License, or


11  * (at your option) any later version.


12  *


13  * HeuristicLab is distributed in the hope that it will be useful,


14  * but WITHOUT ANY WARRANTY; without even the implied warranty of


15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the


16  * GNU General Public License for more details.


17  *


18  * You should have received a copy of the GNU General Public License


19  * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.


20  */


21 


22  #endregion


23 


24  using System;


25 


26  namespace HeuristicLab.Visualization.ChartControlsExtensions {


27  public static class ChartUtil {


28  public static void CalculateAxisInterval(double min, double max, int ticks, out double axisMin, out double axisMax, out double axisInterval) {


29  if (double.IsInfinity(min)  double.IsNaN(min)  double.IsInfinity(max)  double.IsNaN(max)  (min >= max))


30  throw new ArgumentOutOfRangeException("Invalid range provided.");


31 


32  var range = max  min;


33  var dRange = (int)Math.Round(Math.Log10(range));


34  int decimalRank = dRange  1;


35  var aMin = min.RoundDown(decimalRank);


36  var aMax = max.RoundUp(decimalRank);


37 


38  // if one of the interval ends is a multiple of 5 or 10, change the other interval end to be a multiple as well


39  if (decimalRank > 0) {


40  if (((aMin % 5).IsAlmost(0)  (aMin % 10).IsAlmost(0)) && !((aMax % 5).IsAlmost(0)  (aMax % 10).IsAlmost(0))) {


41  aMax = Math.Min(aMax + 5  aMax % 5, aMax + 10  aMax % 10);


42  } else if (((aMax % 5).IsAlmost(0)  (aMax % 10).IsAlmost(0)) && !((aMin % 5).IsAlmost(0)  (aMin % 10).IsAlmost(0))) {


43  aMin = Math.Max(aMin  aMin % 5, aMin  aMin % 10);


44  }


45  }


46 


47  axisMin = aMin;


48  axisMax = aMax;


49  axisInterval = (aMax  aMin) / ticks;


50  }


51 


52  /// <summary>


53  /// Tries to find an axis interval with as few fractional digits as possible (because it looks nicer). we only try between 3 and 5 ticks (inclusive) because it wouldn't make sense to exceed this interval.


54  /// </summary>


55  public static void CalculateOptimalAxisInterval(double min, double max, out double axisMin, out double axisMax, out double axisInterval) {


56  CalculateAxisInterval(min, max, 5, out axisMin, out axisMax, out axisInterval);


57  int bestLsp = int.MaxValue;


58  for (int ticks = 3; ticks <= 5; ++ticks) {


59  double aMin, aMax, aInterval;


60  CalculateAxisInterval(min, max, ticks, out aMin, out aMax, out aInterval);


61  var x = aInterval;


62  int lsp = 0; // position of the least significant fractional digit


63  while (x  Math.Floor(x) > 0) {


64  ++lsp;


65  x *= 10;


66  }


67  if (lsp <= bestLsp) {


68  axisMin = aMin;


69  axisMax = aMax;


70  axisInterval = aInterval;


71  bestLsp = lsp;


72  }


73  }


74  }


75 


76  // find the number of decimals needed to represent the value


77  private static int Decimals(this double x) {


78  if (x.IsAlmost(0)  double.IsInfinity(x)  double.IsNaN(x))


79  return 0;


80 


81  var v = Math.Abs(x);


82  int d = 1;


83  while (v < 1) {


84  v *= 10;


85  d++;


86  }


87  return d;


88  }


89 


90  private static double RoundDown(this double value, int decimalRank) {


91  if (decimalRank > 0) {


92  var floor = (int)Math.Floor(value);


93  var pow = (int)Math.Pow(10, decimalRank);


94  var mod = floor % pow;


95  if (mod < 0) mod += pow;


96  return floor  mod;


97  }


98  return value.Floor(Math.Abs(decimalRank));


99  }


100 


101  private static double RoundUp(this double value, int decimalRank) {


102  if (decimalRank > 0) {


103  var ceil = (int)Math.Ceiling(value);


104  var pow = (int)Math.Pow(10, decimalRank);


105  var mod = ceil % pow;


106  if (mod < 0) mod += pow;


107  return ceil  mod + pow;


108  }


109  return value.Ceil(Math.Abs(decimalRank));


110  }


111 


112  private static double RoundNearest(this double value, int decimalRank) {


113  var nearestDown = value.RoundDown(decimalRank);


114  var nearestUp = value.RoundUp(decimalRank);


115 


116  if (nearestUp  value > value  nearestDown)


117  return nearestDown;


118 


119  return nearestUp;


120  }


121 


122  // rounds down to the nearest value according to the given number of decimal precision


123  private static double Floor(this double value, int precision) {


124  var n = Math.Pow(10, precision);


125  return Math.Round(Math.Floor(value * n) / n, precision);


126  }


127 


128  // rounds up to the nearest value according to the given number of decimal precision


129  private static double Ceil(this double value, int precision) {


130  var n = Math.Pow(10, precision);


131  return Math.Round(Math.Ceiling(value * n) / n, precision);


132  }


133 


134  private static bool IsAlmost(this double value, double other, double eps = 1e12) {


135  return Math.Abs(value  other) < eps;


136  }


137  }


138  }

