Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Axes/Integer/IntegerTicksProvider.cs @ 13749

Last change on this file since 13749 was 12503, checked in by aballeit, 10 years ago

#2283 added GUI and charts; fixed MCTS

File size: 4.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
6
7namespace Microsoft.Research.DynamicDataDisplay.Charts.Axes
8{
9  /// <summary>
10  /// Represents a ticks provider for intefer values.
11  /// </summary>
12  public class IntegerTicksProvider : ITicksProvider<int>
13  {
14    /// <summary>
15    /// Initializes a new instance of the <see cref="IntegerTicksProvider"/> class.
16    /// </summary>
17    public IntegerTicksProvider() { }
18
19    private int minStep = 0;
20    /// <summary>
21    /// Gets or sets the minimal step between ticks.
22    /// </summary>
23    /// <value>The min step.</value>
24    public int MinStep
25    {
26      get { return minStep; }
27      set
28      {
29        Verify.IsTrue(value >= 0, "value");
30        if (minStep != value)
31        {
32          minStep = value;
33          RaiseChangedEvent();
34        }
35      }
36    }
37
38    private int maxStep = Int32.MaxValue;
39    /// <summary>
40    /// Gets or sets the maximal step between ticks.
41    /// </summary>
42    /// <value>The max step.</value>
43    public int MaxStep
44    {
45      get { return maxStep; }
46      set
47      {
48        if (maxStep != value)
49        {
50          if (value < 0)
51            throw new ArgumentOutOfRangeException("value", Strings.Exceptions.ParameterShouldBePositive);
52
53          maxStep = value;
54          RaiseChangedEvent();
55        }
56      }
57    }
58
59    #region ITicksProvider<int> Members
60
61    /// <summary>
62    /// Generates ticks for given range and preferred ticks count.
63    /// </summary>
64    /// <param name="range">The range.</param>
65    /// <param name="ticksCount">The ticks count.</param>
66    /// <returns></returns>
67    public ITicksInfo<int> GetTicks(Range<int> range, int ticksCount)
68    {
69      double start = range.Min;
70      double finish = range.Max;
71
72      double delta = finish - start;
73
74      int log = (int)Math.Round(Math.Log10(delta));
75
76      double newStart = RoundingHelper.Round(start, log);
77      double newFinish = RoundingHelper.Round(finish, log);
78      if (newStart == newFinish)
79      {
80        log--;
81        newStart = RoundingHelper.Round(start, log);
82        newFinish = RoundingHelper.Round(finish, log);
83      }
84
85      // calculating step between ticks
86      double unroundedStep = (newFinish - newStart) / ticksCount;
87      int stepLog = log;
88      // trying to round step
89      int step = (int)RoundingHelper.Round(unroundedStep, stepLog);
90      if (step == 0)
91      {
92        stepLog--;
93        step = (int)RoundingHelper.Round(unroundedStep, stepLog);
94        if (step == 0)
95        {
96          // step will not be rounded if attempts to be rounded to zero.
97          step = (int)unroundedStep;
98        }
99      }
100
101      if (step < minStep)
102        step = minStep;
103      if (step > maxStep)
104        step = maxStep;
105
106      if (step <= 0)
107        step = 1;
108
109      int[] ticks = CreateTicks(start, finish, step);
110
111      TicksInfo<int> res = new TicksInfo<int> { Info = log, Ticks = ticks };
112
113      return res;
114    }
115
116    private static int[] CreateTicks(double start, double finish, int step)
117    {
118      DebugVerify.Is(step != 0);
119
120      int x = (int)(step * Math.Floor(start / (double)step));
121      List<int> res = new List<int>();
122
123      checked
124      {
125        double increasedFinish = finish + step * 1.05;
126        while (x <= increasedFinish)
127        {
128          res.Add(x);
129          x += step;
130        }
131      }
132      return res.ToArray();
133    }
134
135    private static int[] tickCounts = new int[] { 20, 10, 5, 4, 2, 1 };
136
137    /// <summary>
138    /// Decreases the tick count.
139    /// Returned value should be later passed as ticksCount parameter to GetTicks method.
140    /// </summary>
141    /// <param name="ticksCount">The ticks count.</param>
142    /// <returns>Decreased ticks count.</returns>
143    public int DecreaseTickCount(int ticksCount)
144    {
145      return tickCounts.FirstOrDefault(tick => tick < ticksCount);
146    }
147
148    /// <summary>
149    /// Increases the tick count.
150    /// Returned value should be later passed as ticksCount parameter to GetTicks method.
151    /// </summary>
152    /// <param name="ticksCount">The ticks count.</param>
153    /// <returns>Increased ticks count.</returns>
154    public int IncreaseTickCount(int ticksCount)
155    {
156      int newTickCount = tickCounts.Reverse().FirstOrDefault(tick => tick > ticksCount);
157      if (newTickCount == 0)
158        newTickCount = tickCounts[0];
159
160      return newTickCount;
161    }
162
163    /// <summary>
164    /// Gets the minor ticks provider, used to generate ticks between each two adjacent ticks.
165    /// </summary>
166    /// <value>The minor provider.</value>
167    public ITicksProvider<int> MinorProvider
168    {
169      get { return null; }
170    }
171
172    /// <summary>
173    /// Gets the major provider, used to generate major ticks - for example, years for common ticks as months.
174    /// </summary>
175    /// <value>The major provider.</value>
176    public ITicksProvider<int> MajorProvider
177    {
178      get { return null; }
179    }
180
181    protected void RaiseChangedEvent()
182    {
183      Changed.Raise(this);
184    }
185    public event EventHandler Changed;
186
187    #endregion
188  }
189}
Note: See TracBrowser for help on using the repository browser.