using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Research.DynamicDataDisplay.Common.DataSearch;
using System.Windows;
using System.Collections;
using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
namespace Microsoft.Research.DynamicDataDisplay.Charts.Axes.GenericLocational
{
///
/// Represents a ticks provider which takes its ticks from given collection.
///
///
///
public class GenericLocationalTicksProvider : ITicksProvider where TAxis : IComparable
{
private IList collection;
///
/// Gets or sets the collection that is used to generate ticks.
///
/// The collection.
public IList Collection
{
get { return collection; }
set
{
if (value == null)
throw new ArgumentNullException("value");
Changed.Raise(this);
collection = value;
}
}
private Func axisMapping;
///
/// Gets or sets the axis mapping - user defined conversion from collection type to axis type.
///
/// The axis mapping.
public Func AxisMapping
{
get { return axisMapping; }
set
{
if (value == null)
throw new ArgumentNullException("value");
Changed.Raise(this);
axisMapping = value;
}
}
///
/// Initializes a new instance of the class.
///
public GenericLocationalTicksProvider() { }
///
/// Initializes a new instance of the class.
///
/// The collection of axis ticks and labels.
public GenericLocationalTicksProvider(IList collection)
{
Collection = collection;
}
///
/// Initializes a new instance of the class.
///
/// The collection.
/// The coordinate mapping.
public GenericLocationalTicksProvider(IList collection, Func coordinateMapping)
{
Collection = collection;
AxisMapping = coordinateMapping;
}
#region ITicksProvider Members
SearchResult1d minResult = SearchResult1d.Empty;
SearchResult1d maxResult = SearchResult1d.Empty;
GenericSearcher1d searcher;
///
/// Generates ticks for given range and preferred ticks count.
///
/// The range.
/// The ticks count.
///
public ITicksInfo GetTicks(Range range, int ticksCount)
{
EnsureSearcher();
//minResult = searcher.SearchBetween(range.Min, minResult);
//maxResult = searcher.SearchBetween(range.Max, maxResult);
minResult = searcher.SearchFirstLess(range.Min);
maxResult = searcher.SearchGreater(range.Max);
if (!(minResult.IsEmpty && maxResult.IsEmpty))
{
int startIndex = !minResult.IsEmpty ? minResult.Index : 0;
int endIndex = !maxResult.IsEmpty ? maxResult.Index : collection.Count - 1;
int count = endIndex - startIndex + 1;
TAxis[] ticks = new TAxis[count];
for (int i = startIndex; i <= endIndex; i++)
{
ticks[i - startIndex] = axisMapping(collection[i]);
}
TicksInfo result = new TicksInfo
{
Info = startIndex,
TickSizes = ArrayExtensions.CreateArray(count, 1.0),
Ticks = ticks
};
return result;
}
else
{
return TicksInfo.Empty;
}
}
private void EnsureSearcher()
{
if (searcher == null)
{
if (collection == null || axisMapping == null)
throw new InvalidOperationException(Strings.Exceptions.GenericLocationalProviderInvalidState);
searcher = new GenericSearcher1d(collection, axisMapping);
}
}
public int DecreaseTickCount(int ticksCount)
{
return collection.Count;
}
public int IncreaseTickCount(int ticksCount)
{
return collection.Count;
}
public ITicksProvider MinorProvider
{
get { return null; }
}
public ITicksProvider MajorProvider
{
get { return null; }
}
public event EventHandler Changed;
#endregion
}
}