Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization.ChartControlsExtensions/3.3/ChartUtil.cs @ 14152

Last change on this file since 14152 was 14152, checked in by bburlacu, 8 years ago

#2594: Improved axis scaling.

File size: 5.6 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 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
24using System;
25
26namespace 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 minLog = Math.Log10(Math.Abs(min));
34      var maxLog = Math.Log10(Math.Abs(max));
35      var rangeLog = Math.Log10(range);
36
37      var dMin = (int)Math.Floor(minLog);
38      var dMax = (int)Math.Floor(maxLog);
39      var dRange = (int)Math.Floor(rangeLog);
40
41      int decimalRank;
42      if (dMin == dMax && dMax == dRange)
43        decimalRank = dMin - 1;
44      else
45        decimalRank = Math.Min(Math.Min(dMin, dMax), dRange);
46
47      var aMin = min.RoundDown(decimalRank);
48      var aMax = max.RoundUp(decimalRank);
49
50      // if one of the interval ends is a multiple of 5 or 10, change the other interval end to be a multiple as well
51      if (((aMin % 5).IsAlmost(0) || (aMin % 10).IsAlmost(0)) && !((aMax % 5).IsAlmost(0) || (aMax % 10).IsAlmost(0))) {
52        aMax = Math.Min(aMax + 5 - aMax % 5, aMax + 10 - aMax % 10);
53      } else if (((aMax % 5).IsAlmost(0) || (aMax % 10).IsAlmost(0)) && !((aMin % 5).IsAlmost(0) || (aMin % 10).IsAlmost(0))) {
54        aMin = Math.Max(aMin - aMin % 5, aMin - aMin % 10);
55      }
56
57      axisMin = aMin;
58      axisMax = aMax;
59      axisInterval = (aMax - aMin) / ticks;
60    }
61
62    // this method tries to find an axis interval with as few fractional digits as possible (because it looks nicer)
63    // we only try between 3 and 5 ticks (inclusive) because it wouldn't make sense to exceed this interval
64    public static void CalculateOptimalAxisInterval(double min, double max, out double axisMin, out double axisMax, out double axisInterval) {
65      CalculateAxisInterval(min, max, 5, out axisMin, out axisMax, out axisInterval);
66      int bestLsp = int.MaxValue;
67      for (int ticks = 3; ticks <= 5; ++ticks) {
68        double aMin, aMax, aInterval;
69        CalculateAxisInterval(min, max, ticks, out aMin, out aMax, out aInterval);
70        var x = aInterval;
71        int lsp = 0; // position of the least significant fractional digit
72        while (x - Math.Floor(x) > 0) {
73          ++lsp;
74          x *= 10;
75        }
76        if (lsp <= bestLsp) {
77          axisMin = aMin;
78          axisMax = aMax;
79          axisInterval = aInterval;
80          bestLsp = lsp;
81        }
82      }
83    }
84
85    // find the number of decimals needed to represent the value
86    private static int Decimals(this double x) {
87      if (x.IsAlmost(0) || double.IsInfinity(x) || double.IsNaN(x))
88        return 0;
89
90      var v = Math.Abs(x);
91      int d = 1;
92      while (v < 1) {
93        v *= 10;
94        d++;
95      }
96      return d;
97    }
98
99    private static double RoundDown(this double value, int decimalRank) {
100      if (decimalRank > 0) {
101        var floor = (int)Math.Floor(value);
102        var pow = (int)Math.Pow(10, decimalRank);
103        // round down to nearest multiple of 5 or 10
104        var mod = Math.Min(floor % pow, floor % (pow / 2));
105        return floor - mod;
106      }
107      return value.Floor(Math.Abs(decimalRank));
108    }
109
110    private static double RoundUp(this double value, int decimalRank) {
111      if (decimalRank > 0) {
112        var ceil = (int)Math.Ceiling(value);
113        var pow = (int)Math.Pow(10, decimalRank);
114        // round up to nearest multiple of 5 or 10
115        var mod = ceil % (pow / 2) == 0 || ceil % pow == 0 ? 0 : Math.Min(pow / 2 - ceil % (pow / 2), pow - ceil % pow);
116        return ceil + mod;
117      }
118      return value.Ceil(Math.Abs(decimalRank));
119    }
120
121    private static double RoundNearest(this double value, int decimalRank) {
122      var nearestDown = value.RoundDown(decimalRank);
123      var nearestUp = value.RoundUp(decimalRank);
124
125      if (nearestUp - value > value - nearestDown)
126        return nearestDown;
127
128      return nearestUp;
129    }
130
131    // rounds down to the nearest value according to the given number of decimal precision
132    private static double Floor(this double value, int precision) {
133      var n = Math.Pow(10, precision);
134      return Math.Round(Math.Floor(value * n) / n, precision);
135    }
136
137    // rounds up to the nearest value according to the given number of decimal precision
138    private static double Ceil(this double value, int precision) {
139      var n = Math.Pow(10, precision);
140      return Math.Round(Math.Ceiling(value * n) / n, precision);
141    }
142
143    private static bool IsAlmost(this double value, double other, double eps = 1e-12) {
144      return Math.Abs(value - other) < eps;
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.