Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Axes/GenericLocational/GenericLocationalTicksProvider.cs @ 12503

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

#2283 added GUI and charts; fixed MCTS

File size: 4.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Research.DynamicDataDisplay.Common.DataSearch;
6using System.Windows;
7using System.Collections;
8using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
9
10namespace Microsoft.Research.DynamicDataDisplay.Charts.Axes.GenericLocational
11{
12  /// <summary>
13  /// Represents a ticks provider which takes its ticks from given collection.
14  /// </summary>
15  /// <typeparam name="TCollection"></typeparam>
16  /// <typeparam name="TAxis"></typeparam>
17  public class GenericLocationalTicksProvider<TCollection, TAxis> : ITicksProvider<TAxis> where TAxis : IComparable<TAxis>
18  {
19    private IList<TCollection> collection;
20    /// <summary>
21    /// Gets or sets the collection that is used to generate ticks.
22    /// </summary>
23    /// <value>The collection.</value>
24    public IList<TCollection> Collection
25    {
26      get { return collection; }
27      set
28      {
29        if (value == null)
30          throw new ArgumentNullException("value");
31
32        Changed.Raise(this);
33        collection = value;
34      }
35    }
36
37    private Func<TCollection, TAxis> axisMapping;
38    /// <summary>
39    /// Gets or sets the axis mapping - user defined conversion from collection type to axis type.
40    /// </summary>
41    /// <value>The axis mapping.</value>
42    public Func<TCollection, TAxis> AxisMapping
43    {
44      get { return axisMapping; }
45      set
46      {
47        if (value == null)
48          throw new ArgumentNullException("value");
49
50        Changed.Raise(this);
51        axisMapping = value;
52      }
53    }
54
55    /// <summary>
56    /// Initializes a new instance of the <see cref="GenericLocationalTicksProvider&lt;T&gt;"/> class.
57    /// </summary>
58    public GenericLocationalTicksProvider() { }
59
60    /// <summary>
61    /// Initializes a new instance of the <see cref="GenericLocationalTicksProvider&lt;T&gt;"/> class.
62    /// </summary>
63    /// <param name="collection">The collection of axis ticks and labels.</param>
64    public GenericLocationalTicksProvider(IList<TCollection> collection)
65    {
66      Collection = collection;
67    }
68
69    /// <summary>
70    /// Initializes a new instance of the <see cref="GenericLocationalTicksProvider&lt;TCollection, TAxis&gt;"/> class.
71    /// </summary>
72    /// <param name="collection">The collection.</param>
73    /// <param name="coordinateMapping">The coordinate mapping.</param>
74    public GenericLocationalTicksProvider(IList<TCollection> collection, Func<TCollection, TAxis> coordinateMapping)
75    {
76      Collection = collection;
77      AxisMapping = coordinateMapping;
78    }
79
80    #region ITicksProvider<T> Members
81
82    SearchResult1d minResult = SearchResult1d.Empty;
83    SearchResult1d maxResult = SearchResult1d.Empty;
84    GenericSearcher1d<TCollection, TAxis> searcher;
85    /// <summary>
86    /// Generates ticks for given range and preferred ticks count.
87    /// </summary>
88    /// <param name="range">The range.</param>
89    /// <param name="ticksCount">The ticks count.</param>
90    /// <returns></returns>
91    public ITicksInfo<TAxis> GetTicks(Range<TAxis> range, int ticksCount)
92    {
93      EnsureSearcher();
94
95      //minResult = searcher.SearchBetween(range.Min, minResult);
96      //maxResult = searcher.SearchBetween(range.Max, maxResult);
97
98      minResult = searcher.SearchFirstLess(range.Min);
99      maxResult = searcher.SearchGreater(range.Max);
100
101      if (!(minResult.IsEmpty && maxResult.IsEmpty))
102      {
103        int startIndex = !minResult.IsEmpty ? minResult.Index : 0;
104        int endIndex = !maxResult.IsEmpty ? maxResult.Index : collection.Count - 1;
105
106        int count = endIndex - startIndex + 1;
107
108        TAxis[] ticks = new TAxis[count];
109        for (int i = startIndex; i <= endIndex; i++)
110        {
111          ticks[i - startIndex] = axisMapping(collection[i]);
112        }
113
114        TicksInfo<TAxis> result = new TicksInfo<TAxis>
115        {
116          Info = startIndex,
117          TickSizes = ArrayExtensions.CreateArray(count, 1.0),
118          Ticks = ticks
119        };
120
121        return result;
122      }
123      else
124      {
125        return TicksInfo<TAxis>.Empty;
126      }
127    }
128
129    private void EnsureSearcher()
130    {
131      if (searcher == null)
132      {
133        if (collection == null || axisMapping == null)
134          throw new InvalidOperationException(Strings.Exceptions.GenericLocationalProviderInvalidState);
135
136        searcher = new GenericSearcher1d<TCollection, TAxis>(collection, axisMapping);
137      }
138    }
139
140    public int DecreaseTickCount(int ticksCount)
141    {
142      return collection.Count;
143    }
144
145    public int IncreaseTickCount(int ticksCount)
146    {
147      return collection.Count;
148    }
149
150    public ITicksProvider<TAxis> MinorProvider
151    {
152      get { return null; }
153    }
154
155    public ITicksProvider<TAxis> MajorProvider
156    {
157      get { return null; }
158    }
159
160    public event EventHandler Changed;
161
162    #endregion
163  }
164}
Note: See TracBrowser for help on using the repository browser.